blob: 4abb5c88dcb475a392f0ec7ad4676373b3b804f8 [file] [log] [blame]
pbrook502a5392006-05-13 16:11:23 +00001/*
2 * QEMU Uninorth PCI host (for all Mac99 and newer machines)
3 *
4 * Copyright (c) 2006 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
pbrook502a5392006-05-13 16:11:23 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw.h"
25#include "ppc_mac.h"
26#include "pci.h"
27
blueswir1f3902382009-02-05 20:22:07 +000028/* debug UniNorth */
29//#define DEBUG_UNIN
30
31#ifdef DEBUG_UNIN
Blue Swirl001faf32009-05-13 17:53:17 +000032#define UNIN_DPRINTF(fmt, ...) \
33 do { printf("UNIN: " fmt , ## __VA_ARGS__); } while (0)
blueswir1f3902382009-02-05 20:22:07 +000034#else
Blue Swirl001faf32009-05-13 17:53:17 +000035#define UNIN_DPRINTF(fmt, ...)
blueswir1f3902382009-02-05 20:22:07 +000036#endif
37
Anthony Liguoric227f092009-10-01 16:12:16 -050038typedef target_phys_addr_t pci_addr_t;
pbrook502a5392006-05-13 16:11:23 +000039#include "pci_host.h"
40
Blue Swirl2e29bd02009-07-31 20:23:28 +000041typedef struct UNINState {
42 SysBusDevice busdev;
43 PCIHostState host_state;
44} UNINState;
pbrook502a5392006-05-13 16:11:23 +000045
Anthony Liguoric227f092009-10-01 16:12:16 -050046static void pci_unin_main_config_writel (void *opaque, target_phys_addr_t addr,
pbrook502a5392006-05-13 16:11:23 +000047 uint32_t val)
48{
49 UNINState *s = opaque;
pbrook502a5392006-05-13 16:11:23 +000050
blueswir1f3902382009-02-05 20:22:07 +000051 UNIN_DPRINTF("config_writel addr " TARGET_FMT_plx " val %x\n", addr, val);
pbrook502a5392006-05-13 16:11:23 +000052#ifdef TARGET_WORDS_BIGENDIAN
53 val = bswap32(val);
54#endif
55
Blue Swirl2e29bd02009-07-31 20:23:28 +000056 s->host_state.config_reg = val;
pbrook502a5392006-05-13 16:11:23 +000057}
58
59static uint32_t pci_unin_main_config_readl (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -050060 target_phys_addr_t addr)
pbrook502a5392006-05-13 16:11:23 +000061{
62 UNINState *s = opaque;
63 uint32_t val;
pbrook502a5392006-05-13 16:11:23 +000064
Blue Swirl2e29bd02009-07-31 20:23:28 +000065 val = s->host_state.config_reg;
pbrook502a5392006-05-13 16:11:23 +000066#ifdef TARGET_WORDS_BIGENDIAN
67 val = bswap32(val);
68#endif
blueswir1f3902382009-02-05 20:22:07 +000069 UNIN_DPRINTF("config_readl addr " TARGET_FMT_plx " val %x\n", addr, val);
pbrook502a5392006-05-13 16:11:23 +000070
71 return val;
72}
73
Blue Swirld60efc62009-08-25 18:29:31 +000074static CPUWriteMemoryFunc * const pci_unin_main_config_write[] = {
pbrook502a5392006-05-13 16:11:23 +000075 &pci_unin_main_config_writel,
76 &pci_unin_main_config_writel,
77 &pci_unin_main_config_writel,
78};
79
Blue Swirld60efc62009-08-25 18:29:31 +000080static CPUReadMemoryFunc * const pci_unin_main_config_read[] = {
pbrook502a5392006-05-13 16:11:23 +000081 &pci_unin_main_config_readl,
82 &pci_unin_main_config_readl,
83 &pci_unin_main_config_readl,
84};
85
Blue Swirld60efc62009-08-25 18:29:31 +000086static CPUWriteMemoryFunc * const pci_unin_main_write[] = {
pbrook502a5392006-05-13 16:11:23 +000087 &pci_host_data_writeb,
88 &pci_host_data_writew,
89 &pci_host_data_writel,
90};
91
Blue Swirld60efc62009-08-25 18:29:31 +000092static CPUReadMemoryFunc * const pci_unin_main_read[] = {
pbrook502a5392006-05-13 16:11:23 +000093 &pci_host_data_readb,
94 &pci_host_data_readw,
95 &pci_host_data_readl,
96};
97
Anthony Liguoric227f092009-10-01 16:12:16 -050098static void pci_unin_config_writel (void *opaque, target_phys_addr_t addr,
pbrook502a5392006-05-13 16:11:23 +000099 uint32_t val)
100{
101 UNINState *s = opaque;
102
Blue Swirl2e29bd02009-07-31 20:23:28 +0000103 s->host_state.config_reg = val;
pbrook502a5392006-05-13 16:11:23 +0000104}
105
106static uint32_t pci_unin_config_readl (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500107 target_phys_addr_t addr)
pbrook502a5392006-05-13 16:11:23 +0000108{
109 UNINState *s = opaque;
pbrook502a5392006-05-13 16:11:23 +0000110
Blue Swirl2e29bd02009-07-31 20:23:28 +0000111 return s->host_state.config_reg;
pbrook502a5392006-05-13 16:11:23 +0000112}
113
Blue Swirld60efc62009-08-25 18:29:31 +0000114static CPUWriteMemoryFunc * const pci_unin_config_write[] = {
pbrook502a5392006-05-13 16:11:23 +0000115 &pci_unin_config_writel,
116 &pci_unin_config_writel,
117 &pci_unin_config_writel,
118};
119
Blue Swirld60efc62009-08-25 18:29:31 +0000120static CPUReadMemoryFunc * const pci_unin_config_read[] = {
pbrook502a5392006-05-13 16:11:23 +0000121 &pci_unin_config_readl,
122 &pci_unin_config_readl,
123 &pci_unin_config_readl,
124};
125
Blue Swirld60efc62009-08-25 18:29:31 +0000126static CPUWriteMemoryFunc * const pci_unin_write[] = {
Blue Swirl2e29bd02009-07-31 20:23:28 +0000127 &pci_host_data_writeb,
128 &pci_host_data_writew,
129 &pci_host_data_writel,
pbrook502a5392006-05-13 16:11:23 +0000130};
131
Blue Swirld60efc62009-08-25 18:29:31 +0000132static CPUReadMemoryFunc * const pci_unin_read[] = {
Blue Swirl2e29bd02009-07-31 20:23:28 +0000133 &pci_host_data_readb,
134 &pci_host_data_readw,
135 &pci_host_data_readl,
pbrook502a5392006-05-13 16:11:23 +0000136};
pbrook502a5392006-05-13 16:11:23 +0000137
pbrookd2b59312006-09-24 00:16:34 +0000138/* Don't know if this matches real hardware, but it agrees with OHW. */
139static int pci_unin_map_irq(PCIDevice *pci_dev, int irq_num)
pbrook502a5392006-05-13 16:11:23 +0000140{
pbrookd2b59312006-09-24 00:16:34 +0000141 return (irq_num + (pci_dev->devfn >> 3)) & 3;
142}
143
Juan Quintela5d4e84c2009-08-28 15:28:17 +0200144static void pci_unin_set_irq(void *opaque, int irq_num, int level)
pbrookd2b59312006-09-24 00:16:34 +0000145{
Juan Quintela5d4e84c2009-08-28 15:28:17 +0200146 qemu_irq *pic = opaque;
147
pbrookd537cf62007-04-07 18:14:41 +0000148 qemu_set_irq(pic[irq_num + 8], level);
pbrook502a5392006-05-13 16:11:23 +0000149}
150
blueswir1f3902382009-02-05 20:22:07 +0000151static void pci_unin_save(QEMUFile* f, void *opaque)
152{
153 PCIDevice *d = opaque;
154
155 pci_device_save(d, f);
156}
157
158static int pci_unin_load(QEMUFile* f, void *opaque, int version_id)
159{
160 PCIDevice *d = opaque;
161
162 if (version_id != 1)
163 return -EINVAL;
164
165 return pci_device_load(d, f);
166}
167
168static void pci_unin_reset(void *opaque)
169{
170}
171
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200172static int pci_unin_main_init_device(SysBusDevice *dev)
pbrook502a5392006-05-13 16:11:23 +0000173{
174 UNINState *s;
pbrook502a5392006-05-13 16:11:23 +0000175 int pci_mem_config, pci_mem_data;
176
177 /* Use values found on a real PowerMac */
178 /* Uninorth main bus */
Blue Swirl2e29bd02009-07-31 20:23:28 +0000179 s = FROM_SYSBUS(UNINState, dev);
pbrook502a5392006-05-13 16:11:23 +0000180
Avi Kivity1eed09c2009-06-14 11:38:51 +0300181 pci_mem_config = cpu_register_io_memory(pci_unin_main_config_read,
pbrook502a5392006-05-13 16:11:23 +0000182 pci_unin_main_config_write, s);
Avi Kivity1eed09c2009-06-14 11:38:51 +0300183 pci_mem_data = cpu_register_io_memory(pci_unin_main_read,
Blue Swirl2e29bd02009-07-31 20:23:28 +0000184 pci_unin_main_write, &s->host_state);
185
186 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
187 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
188
189 register_savevm("uninorth", 0, 1, pci_unin_save, pci_unin_load, &s->host_state);
190 qemu_register_reset(pci_unin_reset, &s->host_state);
191 pci_unin_reset(&s->host_state);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200192 return 0;
Blue Swirl2e29bd02009-07-31 20:23:28 +0000193}
194
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200195static int pci_dec_21154_init_device(SysBusDevice *dev)
Blue Swirl2e29bd02009-07-31 20:23:28 +0000196{
197 UNINState *s;
198 int pci_mem_config, pci_mem_data;
199
200 /* Uninorth bridge */
201 s = FROM_SYSBUS(UNINState, dev);
202
203 // XXX: s = &pci_bridge[2];
204 pci_mem_config = cpu_register_io_memory(pci_unin_config_read,
205 pci_unin_config_write, s);
206 pci_mem_data = cpu_register_io_memory(pci_unin_main_read,
207 pci_unin_main_write, &s->host_state);
208 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
209 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200210 return 0;
Blue Swirl2e29bd02009-07-31 20:23:28 +0000211}
212
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200213static int pci_unin_agp_init_device(SysBusDevice *dev)
Blue Swirl2e29bd02009-07-31 20:23:28 +0000214{
215 UNINState *s;
216 int pci_mem_config, pci_mem_data;
217
218 /* Uninorth AGP bus */
219 s = FROM_SYSBUS(UNINState, dev);
220
221 pci_mem_config = cpu_register_io_memory(pci_unin_config_read,
222 pci_unin_config_write, s);
223 pci_mem_data = cpu_register_io_memory(pci_unin_main_read,
224 pci_unin_main_write, &s->host_state);
225 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
226 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200227 return 0;
Blue Swirl2e29bd02009-07-31 20:23:28 +0000228}
229
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200230static int pci_unin_internal_init_device(SysBusDevice *dev)
Blue Swirl2e29bd02009-07-31 20:23:28 +0000231{
232 UNINState *s;
233 int pci_mem_config, pci_mem_data;
234
235 /* Uninorth internal bus */
236 s = FROM_SYSBUS(UNINState, dev);
237
238 pci_mem_config = cpu_register_io_memory(pci_unin_config_read,
239 pci_unin_config_write, s);
240 pci_mem_data = cpu_register_io_memory(pci_unin_read,
241 pci_unin_write, s);
242 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
243 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200244 return 0;
Blue Swirl2e29bd02009-07-31 20:23:28 +0000245}
246
247PCIBus *pci_pmac_init(qemu_irq *pic)
248{
249 DeviceState *dev;
250 SysBusDevice *s;
251 UNINState *d;
252
253 /* Use values found on a real PowerMac */
254 /* Uninorth main bus */
255 dev = qdev_create(NULL, "Uni-north main");
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200256 qdev_init_nofail(dev);
Blue Swirl2e29bd02009-07-31 20:23:28 +0000257 s = sysbus_from_qdev(dev);
258 d = FROM_SYSBUS(UNINState, s);
Blue Swirlcdd09352009-09-19 17:59:10 +0000259 d->host_state.bus = pci_register_bus(&d->busdev.qdev, "pci",
Blue Swirl2e29bd02009-07-31 20:23:28 +0000260 pci_unin_set_irq, pci_unin_map_irq,
261 pic, 11 << 3, 4);
262
263 pci_create_simple(d->host_state.bus, 11 << 3, "Uni-north main");
264
265 sysbus_mmio_map(s, 0, 0xf2800000);
266 sysbus_mmio_map(s, 1, 0xf2c00000);
267
268 /* DEC 21154 bridge */
269#if 0
270 /* XXX: not activated as PPC BIOS doesn't handle multiple buses properly */
271 pci_create_simple(d->host_state.bus, 12 << 3, "DEC 21154");
272#endif
273
274 /* Uninorth AGP bus */
275 pci_create_simple(d->host_state.bus, 13 << 3, "Uni-north AGP");
276
277 /* Uninorth internal bus */
278#if 0
279 /* XXX: not needed for now */
280 pci_create_simple(d->host_state.bus, 14 << 3, "Uni-north internal");
281#endif
282
283 return d->host_state.bus;
284}
285
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200286static int unin_main_pci_host_init(PCIDevice *d)
Blue Swirl2e29bd02009-07-31 20:23:28 +0000287{
aliguorideb54392009-01-26 15:37:35 +0000288 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE);
blueswir14ebcf882009-02-01 12:01:04 +0000289 pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_PCI);
pbrook502a5392006-05-13 16:11:23 +0000290 d->config[0x08] = 0x00; // revision
blueswir1173a5432009-02-01 19:26:20 +0000291 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
pbrook502a5392006-05-13 16:11:23 +0000292 d->config[0x0C] = 0x08; // cache_line_size
293 d->config[0x0D] = 0x10; // latency_timer
Isaku Yamahata6407f372009-05-03 19:03:00 +0000294 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
pbrook502a5392006-05-13 16:11:23 +0000295 d->config[0x34] = 0x00; // capabilities_pointer
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200296 return 0;
Blue Swirl2e29bd02009-07-31 20:23:28 +0000297}
pbrook502a5392006-05-13 16:11:23 +0000298
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200299static int dec_21154_pci_host_init(PCIDevice *d)
Blue Swirl2e29bd02009-07-31 20:23:28 +0000300{
pbrook502a5392006-05-13 16:11:23 +0000301 /* pci-to-pci bridge */
blueswir14ebcf882009-02-01 12:01:04 +0000302 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_DEC);
303 pci_config_set_device_id(d->config, PCI_DEVICE_ID_DEC_21154);
pbrook502a5392006-05-13 16:11:23 +0000304 d->config[0x08] = 0x05; // revision
blueswir1173a5432009-02-01 19:26:20 +0000305 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_PCI);
pbrook502a5392006-05-13 16:11:23 +0000306 d->config[0x0C] = 0x08; // cache_line_size
307 d->config[0x0D] = 0x20; // latency_timer
Isaku Yamahata6407f372009-05-03 19:03:00 +0000308 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_BRIDGE; // header_type
pbrook502a5392006-05-13 16:11:23 +0000309
310 d->config[0x18] = 0x01; // primary_bus
311 d->config[0x19] = 0x02; // secondary_bus
312 d->config[0x1A] = 0x02; // subordinate_bus
313 d->config[0x1B] = 0x20; // secondary_latency_timer
314 d->config[0x1C] = 0x11; // io_base
315 d->config[0x1D] = 0x01; // io_limit
316 d->config[0x20] = 0x00; // memory_base
317 d->config[0x21] = 0x80;
318 d->config[0x22] = 0x00; // memory_limit
319 d->config[0x23] = 0x80;
320 d->config[0x24] = 0x01; // prefetchable_memory_base
321 d->config[0x25] = 0x80;
322 d->config[0x26] = 0xF1; // prefectchable_memory_limit
323 d->config[0x27] = 0x7F;
324 // d->config[0x34] = 0xdc // capabilities_pointer
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200325 return 0;
Blue Swirl2e29bd02009-07-31 20:23:28 +0000326}
blueswir1783a20d2009-03-07 20:53:18 +0000327
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200328static int unin_agp_pci_host_init(PCIDevice *d)
Blue Swirl2e29bd02009-07-31 20:23:28 +0000329{
aliguorideb54392009-01-26 15:37:35 +0000330 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE);
331 pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_AGP);
pbrook502a5392006-05-13 16:11:23 +0000332 d->config[0x08] = 0x00; // revision
blueswir1173a5432009-02-01 19:26:20 +0000333 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
pbrook502a5392006-05-13 16:11:23 +0000334 d->config[0x0C] = 0x08; // cache_line_size
335 d->config[0x0D] = 0x10; // latency_timer
Isaku Yamahata6407f372009-05-03 19:03:00 +0000336 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
pbrook502a5392006-05-13 16:11:23 +0000337 // d->config[0x34] = 0x80; // capabilities_pointer
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200338 return 0;
Blue Swirl2e29bd02009-07-31 20:23:28 +0000339}
pbrook502a5392006-05-13 16:11:23 +0000340
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200341static int unin_internal_pci_host_init(PCIDevice *d)
Blue Swirl2e29bd02009-07-31 20:23:28 +0000342{
aliguorideb54392009-01-26 15:37:35 +0000343 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE);
blueswir14ebcf882009-02-01 12:01:04 +0000344 pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_I_PCI);
pbrook502a5392006-05-13 16:11:23 +0000345 d->config[0x08] = 0x00; // revision
blueswir1173a5432009-02-01 19:26:20 +0000346 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
pbrook502a5392006-05-13 16:11:23 +0000347 d->config[0x0C] = 0x08; // cache_line_size
348 d->config[0x0D] = 0x10; // latency_timer
Isaku Yamahata6407f372009-05-03 19:03:00 +0000349 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
pbrook502a5392006-05-13 16:11:23 +0000350 d->config[0x34] = 0x00; // capabilities_pointer
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200351 return 0;
pbrook502a5392006-05-13 16:11:23 +0000352}
Blue Swirl2e29bd02009-07-31 20:23:28 +0000353
354static PCIDeviceInfo unin_main_pci_host_info = {
355 .qdev.name = "Uni-north main",
356 .qdev.size = sizeof(PCIDevice),
357 .init = unin_main_pci_host_init,
358};
359
360static PCIDeviceInfo dec_21154_pci_host_info = {
361 .qdev.name = "DEC 21154",
362 .qdev.size = sizeof(PCIDevice),
363 .init = dec_21154_pci_host_init,
364};
365
366static PCIDeviceInfo unin_agp_pci_host_info = {
367 .qdev.name = "Uni-north AGP",
368 .qdev.size = sizeof(PCIDevice),
369 .init = unin_agp_pci_host_init,
370};
371
372static PCIDeviceInfo unin_internal_pci_host_info = {
373 .qdev.name = "Uni-north internal",
374 .qdev.size = sizeof(PCIDevice),
375 .init = unin_internal_pci_host_init,
376};
377
378static void unin_register_devices(void)
379{
380 sysbus_register_dev("Uni-north main", sizeof(UNINState),
381 pci_unin_main_init_device);
382 pci_qdev_register(&unin_main_pci_host_info);
383 sysbus_register_dev("DEC 21154", sizeof(UNINState),
384 pci_dec_21154_init_device);
385 pci_qdev_register(&dec_21154_pci_host_info);
386 sysbus_register_dev("Uni-north AGP", sizeof(UNINState),
387 pci_unin_agp_init_device);
388 pci_qdev_register(&unin_agp_pci_host_info);
389 sysbus_register_dev("Uni-north internal", sizeof(UNINState),
390 pci_unin_internal_init_device);
391 pci_qdev_register(&unin_internal_pci_host_info);
392}
393
394device_init(unin_register_devices)