blob: 908e04638c70df8d9de5758857f43c99a12a15cc [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
pbrook502a5392006-05-13 16:11:23 +000038typedef target_phys_addr_t pci_addr_t;
39#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
46static void pci_unin_main_config_writel (void *opaque, target_phys_addr_t addr,
47 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,
60 target_phys_addr_t addr)
61{
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
pbrook502a5392006-05-13 16:11:23 +000098static void pci_unin_config_writel (void *opaque, target_phys_addr_t addr,
99 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,
107 target_phys_addr_t addr)
108{
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
pbrookd537cf62007-04-07 18:14:41 +0000144static void pci_unin_set_irq(qemu_irq *pic, int irq_num, int level)
pbrookd2b59312006-09-24 00:16:34 +0000145{
pbrookd537cf62007-04-07 18:14:41 +0000146 qemu_set_irq(pic[irq_num + 8], level);
pbrook502a5392006-05-13 16:11:23 +0000147}
148
blueswir1f3902382009-02-05 20:22:07 +0000149static void pci_unin_save(QEMUFile* f, void *opaque)
150{
151 PCIDevice *d = opaque;
152
153 pci_device_save(d, f);
154}
155
156static int pci_unin_load(QEMUFile* f, void *opaque, int version_id)
157{
158 PCIDevice *d = opaque;
159
160 if (version_id != 1)
161 return -EINVAL;
162
163 return pci_device_load(d, f);
164}
165
166static void pci_unin_reset(void *opaque)
167{
168}
169
Blue Swirl2e29bd02009-07-31 20:23:28 +0000170static void pci_unin_main_init_device(SysBusDevice *dev)
pbrook502a5392006-05-13 16:11:23 +0000171{
172 UNINState *s;
pbrook502a5392006-05-13 16:11:23 +0000173 int pci_mem_config, pci_mem_data;
174
175 /* Use values found on a real PowerMac */
176 /* Uninorth main bus */
Blue Swirl2e29bd02009-07-31 20:23:28 +0000177 s = FROM_SYSBUS(UNINState, dev);
pbrook502a5392006-05-13 16:11:23 +0000178
Avi Kivity1eed09c2009-06-14 11:38:51 +0300179 pci_mem_config = cpu_register_io_memory(pci_unin_main_config_read,
pbrook502a5392006-05-13 16:11:23 +0000180 pci_unin_main_config_write, s);
Avi Kivity1eed09c2009-06-14 11:38:51 +0300181 pci_mem_data = cpu_register_io_memory(pci_unin_main_read,
Blue Swirl2e29bd02009-07-31 20:23:28 +0000182 pci_unin_main_write, &s->host_state);
183
184 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
185 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
186
187 register_savevm("uninorth", 0, 1, pci_unin_save, pci_unin_load, &s->host_state);
188 qemu_register_reset(pci_unin_reset, &s->host_state);
189 pci_unin_reset(&s->host_state);
190}
191
192static void pci_dec_21154_init_device(SysBusDevice *dev)
193{
194 UNINState *s;
195 int pci_mem_config, pci_mem_data;
196
197 /* Uninorth bridge */
198 s = FROM_SYSBUS(UNINState, dev);
199
200 // XXX: s = &pci_bridge[2];
201 pci_mem_config = cpu_register_io_memory(pci_unin_config_read,
202 pci_unin_config_write, s);
203 pci_mem_data = cpu_register_io_memory(pci_unin_main_read,
204 pci_unin_main_write, &s->host_state);
205 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
206 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
207}
208
209static void pci_unin_agp_init_device(SysBusDevice *dev)
210{
211 UNINState *s;
212 int pci_mem_config, pci_mem_data;
213
214 /* Uninorth AGP bus */
215 s = FROM_SYSBUS(UNINState, dev);
216
217 pci_mem_config = cpu_register_io_memory(pci_unin_config_read,
218 pci_unin_config_write, s);
219 pci_mem_data = cpu_register_io_memory(pci_unin_main_read,
220 pci_unin_main_write, &s->host_state);
221 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
222 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
223}
224
225static void pci_unin_internal_init_device(SysBusDevice *dev)
226{
227 UNINState *s;
228 int pci_mem_config, pci_mem_data;
229
230 /* Uninorth internal bus */
231 s = FROM_SYSBUS(UNINState, dev);
232
233 pci_mem_config = cpu_register_io_memory(pci_unin_config_read,
234 pci_unin_config_write, s);
235 pci_mem_data = cpu_register_io_memory(pci_unin_read,
236 pci_unin_write, s);
237 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
238 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
239}
240
241PCIBus *pci_pmac_init(qemu_irq *pic)
242{
243 DeviceState *dev;
244 SysBusDevice *s;
245 UNINState *d;
246
247 /* Use values found on a real PowerMac */
248 /* Uninorth main bus */
249 dev = qdev_create(NULL, "Uni-north main");
250 qdev_init(dev);
251 s = sysbus_from_qdev(dev);
252 d = FROM_SYSBUS(UNINState, s);
253 d->host_state.bus = pci_register_bus(NULL, "pci",
254 pci_unin_set_irq, pci_unin_map_irq,
255 pic, 11 << 3, 4);
256
257 pci_create_simple(d->host_state.bus, 11 << 3, "Uni-north main");
258
259 sysbus_mmio_map(s, 0, 0xf2800000);
260 sysbus_mmio_map(s, 1, 0xf2c00000);
261
262 /* DEC 21154 bridge */
263#if 0
264 /* XXX: not activated as PPC BIOS doesn't handle multiple buses properly */
265 pci_create_simple(d->host_state.bus, 12 << 3, "DEC 21154");
266#endif
267
268 /* Uninorth AGP bus */
269 pci_create_simple(d->host_state.bus, 13 << 3, "Uni-north AGP");
270
271 /* Uninorth internal bus */
272#if 0
273 /* XXX: not needed for now */
274 pci_create_simple(d->host_state.bus, 14 << 3, "Uni-north internal");
275#endif
276
277 return d->host_state.bus;
278}
279
280static void unin_main_pci_host_init(PCIDevice *d)
281{
aliguorideb54392009-01-26 15:37:35 +0000282 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE);
blueswir14ebcf882009-02-01 12:01:04 +0000283 pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_PCI);
pbrook502a5392006-05-13 16:11:23 +0000284 d->config[0x08] = 0x00; // revision
blueswir1173a5432009-02-01 19:26:20 +0000285 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
pbrook502a5392006-05-13 16:11:23 +0000286 d->config[0x0C] = 0x08; // cache_line_size
287 d->config[0x0D] = 0x10; // latency_timer
Isaku Yamahata6407f372009-05-03 19:03:00 +0000288 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
pbrook502a5392006-05-13 16:11:23 +0000289 d->config[0x34] = 0x00; // capabilities_pointer
Blue Swirl2e29bd02009-07-31 20:23:28 +0000290}
pbrook502a5392006-05-13 16:11:23 +0000291
Blue Swirl2e29bd02009-07-31 20:23:28 +0000292static void dec_21154_pci_host_init(PCIDevice *d)
293{
pbrook502a5392006-05-13 16:11:23 +0000294 /* pci-to-pci bridge */
blueswir14ebcf882009-02-01 12:01:04 +0000295 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_DEC);
296 pci_config_set_device_id(d->config, PCI_DEVICE_ID_DEC_21154);
pbrook502a5392006-05-13 16:11:23 +0000297 d->config[0x08] = 0x05; // revision
blueswir1173a5432009-02-01 19:26:20 +0000298 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_PCI);
pbrook502a5392006-05-13 16:11:23 +0000299 d->config[0x0C] = 0x08; // cache_line_size
300 d->config[0x0D] = 0x20; // latency_timer
Isaku Yamahata6407f372009-05-03 19:03:00 +0000301 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_BRIDGE; // header_type
pbrook502a5392006-05-13 16:11:23 +0000302
303 d->config[0x18] = 0x01; // primary_bus
304 d->config[0x19] = 0x02; // secondary_bus
305 d->config[0x1A] = 0x02; // subordinate_bus
306 d->config[0x1B] = 0x20; // secondary_latency_timer
307 d->config[0x1C] = 0x11; // io_base
308 d->config[0x1D] = 0x01; // io_limit
309 d->config[0x20] = 0x00; // memory_base
310 d->config[0x21] = 0x80;
311 d->config[0x22] = 0x00; // memory_limit
312 d->config[0x23] = 0x80;
313 d->config[0x24] = 0x01; // prefetchable_memory_base
314 d->config[0x25] = 0x80;
315 d->config[0x26] = 0xF1; // prefectchable_memory_limit
316 d->config[0x27] = 0x7F;
317 // d->config[0x34] = 0xdc // capabilities_pointer
Blue Swirl2e29bd02009-07-31 20:23:28 +0000318}
blueswir1783a20d2009-03-07 20:53:18 +0000319
Blue Swirl2e29bd02009-07-31 20:23:28 +0000320static void unin_agp_pci_host_init(PCIDevice *d)
321{
aliguorideb54392009-01-26 15:37:35 +0000322 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE);
323 pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_AGP);
pbrook502a5392006-05-13 16:11:23 +0000324 d->config[0x08] = 0x00; // revision
blueswir1173a5432009-02-01 19:26:20 +0000325 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
pbrook502a5392006-05-13 16:11:23 +0000326 d->config[0x0C] = 0x08; // cache_line_size
327 d->config[0x0D] = 0x10; // latency_timer
Isaku Yamahata6407f372009-05-03 19:03:00 +0000328 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
pbrook502a5392006-05-13 16:11:23 +0000329 // d->config[0x34] = 0x80; // capabilities_pointer
Blue Swirl2e29bd02009-07-31 20:23:28 +0000330}
pbrook502a5392006-05-13 16:11:23 +0000331
Blue Swirl2e29bd02009-07-31 20:23:28 +0000332static void unin_internal_pci_host_init(PCIDevice *d)
333{
aliguorideb54392009-01-26 15:37:35 +0000334 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE);
blueswir14ebcf882009-02-01 12:01:04 +0000335 pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_I_PCI);
pbrook502a5392006-05-13 16:11:23 +0000336 d->config[0x08] = 0x00; // revision
blueswir1173a5432009-02-01 19:26:20 +0000337 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
pbrook502a5392006-05-13 16:11:23 +0000338 d->config[0x0C] = 0x08; // cache_line_size
339 d->config[0x0D] = 0x10; // latency_timer
Isaku Yamahata6407f372009-05-03 19:03:00 +0000340 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
pbrook502a5392006-05-13 16:11:23 +0000341 d->config[0x34] = 0x00; // capabilities_pointer
pbrook502a5392006-05-13 16:11:23 +0000342}
Blue Swirl2e29bd02009-07-31 20:23:28 +0000343
344static PCIDeviceInfo unin_main_pci_host_info = {
345 .qdev.name = "Uni-north main",
346 .qdev.size = sizeof(PCIDevice),
347 .init = unin_main_pci_host_init,
348};
349
350static PCIDeviceInfo dec_21154_pci_host_info = {
351 .qdev.name = "DEC 21154",
352 .qdev.size = sizeof(PCIDevice),
353 .init = dec_21154_pci_host_init,
354};
355
356static PCIDeviceInfo unin_agp_pci_host_info = {
357 .qdev.name = "Uni-north AGP",
358 .qdev.size = sizeof(PCIDevice),
359 .init = unin_agp_pci_host_init,
360};
361
362static PCIDeviceInfo unin_internal_pci_host_info = {
363 .qdev.name = "Uni-north internal",
364 .qdev.size = sizeof(PCIDevice),
365 .init = unin_internal_pci_host_init,
366};
367
368static void unin_register_devices(void)
369{
370 sysbus_register_dev("Uni-north main", sizeof(UNINState),
371 pci_unin_main_init_device);
372 pci_qdev_register(&unin_main_pci_host_info);
373 sysbus_register_dev("DEC 21154", sizeof(UNINState),
374 pci_dec_21154_init_device);
375 pci_qdev_register(&dec_21154_pci_host_info);
376 sysbus_register_dev("Uni-north AGP", sizeof(UNINState),
377 pci_unin_agp_init_device);
378 pci_qdev_register(&unin_agp_pci_host_info);
379 sysbus_register_dev("Uni-north internal", sizeof(UNINState),
380 pci_unin_internal_init_device);
381 pci_qdev_register(&unin_internal_pci_host_info);
382}
383
384device_init(unin_register_devices)