blob: 332748a6de137439db819eb8df3c12be4e22a66f [file] [log] [blame]
aurel3274c62ba2009-03-02 16:42:23 +00001/*
2 * QEMU PowerPC E500 embedded processors pci controller emulation
3 *
4 * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
5 *
6 * Author: Yu Liu, <yu.liu@freescale.com>
7 *
8 * This file is derived from hw/ppc4xx_pci.c,
9 * the copyright for that material belongs to the original owners.
10 *
11 * This is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17#include "hw.h"
aurel3274c62ba2009-03-02 16:42:23 +000018#include "pci.h"
19#include "pci_host.h"
20#include "bswap.h"
aurel3274c62ba2009-03-02 16:42:23 +000021
22#ifdef DEBUG_PCI
Blue Swirl001faf32009-05-13 17:53:17 +000023#define pci_debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
aurel3274c62ba2009-03-02 16:42:23 +000024#else
Blue Swirl001faf32009-05-13 17:53:17 +000025#define pci_debug(fmt, ...)
aurel3274c62ba2009-03-02 16:42:23 +000026#endif
27
28#define PCIE500_CFGADDR 0x0
29#define PCIE500_CFGDATA 0x4
30#define PCIE500_REG_BASE 0xC00
Alexander Grafbe13cc72010-08-31 00:22:28 +020031#define PCIE500_ALL_SIZE 0x1000
32#define PCIE500_REG_SIZE (PCIE500_ALL_SIZE - PCIE500_REG_BASE)
aurel3274c62ba2009-03-02 16:42:23 +000033
34#define PPCE500_PCI_CONFIG_ADDR 0x0
35#define PPCE500_PCI_CONFIG_DATA 0x4
36#define PPCE500_PCI_INTACK 0x8
37
38#define PPCE500_PCI_OW1 (0xC20 - PCIE500_REG_BASE)
39#define PPCE500_PCI_OW2 (0xC40 - PCIE500_REG_BASE)
40#define PPCE500_PCI_OW3 (0xC60 - PCIE500_REG_BASE)
41#define PPCE500_PCI_OW4 (0xC80 - PCIE500_REG_BASE)
42#define PPCE500_PCI_IW3 (0xDA0 - PCIE500_REG_BASE)
43#define PPCE500_PCI_IW2 (0xDC0 - PCIE500_REG_BASE)
44#define PPCE500_PCI_IW1 (0xDE0 - PCIE500_REG_BASE)
45
46#define PPCE500_PCI_GASKET_TIMR (0xE20 - PCIE500_REG_BASE)
47
48#define PCI_POTAR 0x0
49#define PCI_POTEAR 0x4
50#define PCI_POWBAR 0x8
51#define PCI_POWAR 0x10
52
53#define PCI_PITAR 0x0
54#define PCI_PIWBAR 0x8
55#define PCI_PIWBEAR 0xC
56#define PCI_PIWAR 0x10
57
58#define PPCE500_PCI_NR_POBS 5
59#define PPCE500_PCI_NR_PIBS 3
60
61struct pci_outbound {
62 uint32_t potar;
63 uint32_t potear;
64 uint32_t powbar;
65 uint32_t powar;
66};
67
68struct pci_inbound {
69 uint32_t pitar;
70 uint32_t piwbar;
71 uint32_t piwbear;
72 uint32_t piwar;
73};
74
Andreas Färber9c1a61f2012-08-20 19:08:03 +020075#define TYPE_PPC_E500_PCI_HOST_BRIDGE "e500-pcihost"
76
77#define PPC_E500_PCI_HOST_BRIDGE(obj) \
78 OBJECT_CHECK(PPCE500PCIState, (obj), TYPE_PPC_E500_PCI_HOST_BRIDGE)
79
aurel3274c62ba2009-03-02 16:42:23 +000080struct PPCE500PCIState {
Andreas Färber67c332f2012-08-20 19:08:09 +020081 PCIHostState parent_obj;
Andreas Färber9c1a61f2012-08-20 19:08:03 +020082
aurel3274c62ba2009-03-02 16:42:23 +000083 struct pci_outbound pob[PPCE500_PCI_NR_POBS];
84 struct pci_inbound pib[PPCE500_PCI_NR_PIBS];
85 uint32_t gasket_time;
Alexander Grafbe13cc72010-08-31 00:22:28 +020086 qemu_irq irq[4];
87 /* mmio maps */
Benoît Canetcb4e15c2011-12-16 23:37:47 +010088 MemoryRegion container;
Avi Kivitycd5cba72011-11-20 11:52:58 +020089 MemoryRegion iomem;
aurel3274c62ba2009-03-02 16:42:23 +000090};
91
92typedef struct PPCE500PCIState PPCE500PCIState;
93
Avi Kivitya8170e52012-10-23 12:30:10 +020094static uint64_t pci_reg_read4(void *opaque, hwaddr addr,
Avi Kivitycd5cba72011-11-20 11:52:58 +020095 unsigned size)
aurel3274c62ba2009-03-02 16:42:23 +000096{
97 PPCE500PCIState *pci = opaque;
98 unsigned long win;
99 uint32_t value = 0;
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000100 int idx;
aurel3274c62ba2009-03-02 16:42:23 +0000101
102 win = addr & 0xfe0;
103
104 switch (win) {
105 case PPCE500_PCI_OW1:
106 case PPCE500_PCI_OW2:
107 case PPCE500_PCI_OW3:
108 case PPCE500_PCI_OW4:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000109 idx = (addr >> 5) & 0x7;
aurel3274c62ba2009-03-02 16:42:23 +0000110 switch (addr & 0xC) {
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000111 case PCI_POTAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000112 value = pci->pob[idx].potar;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000113 break;
114 case PCI_POTEAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000115 value = pci->pob[idx].potear;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000116 break;
117 case PCI_POWBAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000118 value = pci->pob[idx].powbar;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000119 break;
120 case PCI_POWAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000121 value = pci->pob[idx].powar;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000122 break;
123 default:
124 break;
aurel3274c62ba2009-03-02 16:42:23 +0000125 }
126 break;
127
128 case PPCE500_PCI_IW3:
129 case PPCE500_PCI_IW2:
130 case PPCE500_PCI_IW1:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000131 idx = ((addr >> 5) & 0x3) - 1;
aurel3274c62ba2009-03-02 16:42:23 +0000132 switch (addr & 0xC) {
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000133 case PCI_PITAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000134 value = pci->pib[idx].pitar;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000135 break;
136 case PCI_PIWBAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000137 value = pci->pib[idx].piwbar;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000138 break;
139 case PCI_PIWBEAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000140 value = pci->pib[idx].piwbear;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000141 break;
142 case PCI_PIWAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000143 value = pci->pib[idx].piwar;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000144 break;
145 default:
146 break;
aurel3274c62ba2009-03-02 16:42:23 +0000147 };
148 break;
149
150 case PPCE500_PCI_GASKET_TIMR:
151 value = pci->gasket_time;
152 break;
153
154 default:
155 break;
156 }
157
Blue Swirlc0a2a092009-07-20 10:37:51 +0000158 pci_debug("%s: win:%lx(addr:" TARGET_FMT_plx ") -> value:%x\n", __func__,
159 win, addr, value);
aurel3274c62ba2009-03-02 16:42:23 +0000160 return value;
161}
162
Avi Kivitya8170e52012-10-23 12:30:10 +0200163static void pci_reg_write4(void *opaque, hwaddr addr,
Avi Kivitycd5cba72011-11-20 11:52:58 +0200164 uint64_t value, unsigned size)
aurel3274c62ba2009-03-02 16:42:23 +0000165{
166 PPCE500PCIState *pci = opaque;
167 unsigned long win;
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000168 int idx;
aurel3274c62ba2009-03-02 16:42:23 +0000169
170 win = addr & 0xfe0;
171
Blue Swirlc0a2a092009-07-20 10:37:51 +0000172 pci_debug("%s: value:%x -> win:%lx(addr:" TARGET_FMT_plx ")\n",
Avi Kivitycd5cba72011-11-20 11:52:58 +0200173 __func__, (unsigned)value, win, addr);
aurel3274c62ba2009-03-02 16:42:23 +0000174
175 switch (win) {
176 case PPCE500_PCI_OW1:
177 case PPCE500_PCI_OW2:
178 case PPCE500_PCI_OW3:
179 case PPCE500_PCI_OW4:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000180 idx = (addr >> 5) & 0x7;
aurel3274c62ba2009-03-02 16:42:23 +0000181 switch (addr & 0xC) {
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000182 case PCI_POTAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000183 pci->pob[idx].potar = value;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000184 break;
185 case PCI_POTEAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000186 pci->pob[idx].potear = value;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000187 break;
188 case PCI_POWBAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000189 pci->pob[idx].powbar = value;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000190 break;
191 case PCI_POWAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000192 pci->pob[idx].powar = value;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000193 break;
194 default:
195 break;
aurel3274c62ba2009-03-02 16:42:23 +0000196 };
197 break;
198
199 case PPCE500_PCI_IW3:
200 case PPCE500_PCI_IW2:
201 case PPCE500_PCI_IW1:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000202 idx = ((addr >> 5) & 0x3) - 1;
aurel3274c62ba2009-03-02 16:42:23 +0000203 switch (addr & 0xC) {
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000204 case PCI_PITAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000205 pci->pib[idx].pitar = value;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000206 break;
207 case PCI_PIWBAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000208 pci->pib[idx].piwbar = value;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000209 break;
210 case PCI_PIWBEAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000211 pci->pib[idx].piwbear = value;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000212 break;
213 case PCI_PIWAR:
Liu Yu-B13201eeae2e72011-09-29 17:52:50 +0000214 pci->pib[idx].piwar = value;
Liu Yu-B132016875dc82011-09-29 17:52:49 +0000215 break;
216 default:
217 break;
aurel3274c62ba2009-03-02 16:42:23 +0000218 };
219 break;
220
221 case PPCE500_PCI_GASKET_TIMR:
222 pci->gasket_time = value;
223 break;
224
225 default:
226 break;
227 };
228}
229
Avi Kivitycd5cba72011-11-20 11:52:58 +0200230static const MemoryRegionOps e500_pci_reg_ops = {
231 .read = pci_reg_read4,
232 .write = pci_reg_write4,
233 .endianness = DEVICE_BIG_ENDIAN,
aurel3274c62ba2009-03-02 16:42:23 +0000234};
235
236static int mpc85xx_pci_map_irq(PCIDevice *pci_dev, int irq_num)
237{
238 int devno = pci_dev->devfn >> 3, ret = 0;
239
240 switch (devno) {
241 /* Two PCI slot */
242 case 0x11:
243 case 0x12:
244 ret = (irq_num + devno - 0x10) % 4;
245 break;
246 default:
Scott Wood72b310e2011-04-08 17:06:37 -0500247 printf("Error:%s:unknown dev number\n", __func__);
aurel3274c62ba2009-03-02 16:42:23 +0000248 }
249
250 pci_debug("%s: devfn %x irq %d -> %d devno:%x\n", __func__,
251 pci_dev->devfn, irq_num, ret, devno);
252
253 return ret;
254}
255
Juan Quintela5d4e84c2009-08-28 15:28:17 +0200256static void mpc85xx_pci_set_irq(void *opaque, int irq_num, int level)
aurel3274c62ba2009-03-02 16:42:23 +0000257{
Juan Quintela5d4e84c2009-08-28 15:28:17 +0200258 qemu_irq *pic = opaque;
259
aurel3274c62ba2009-03-02 16:42:23 +0000260 pci_debug("%s: PCI irq %d, level:%d\n", __func__, irq_num, level);
261
262 qemu_set_irq(pic[irq_num], level);
263}
264
Juan Quintelae0433ec2010-12-02 15:29:42 +0100265static const VMStateDescription vmstate_pci_outbound = {
266 .name = "pci_outbound",
267 .version_id = 0,
268 .minimum_version_id = 0,
269 .minimum_version_id_old = 0,
270 .fields = (VMStateField[]) {
271 VMSTATE_UINT32(potar, struct pci_outbound),
272 VMSTATE_UINT32(potear, struct pci_outbound),
273 VMSTATE_UINT32(powbar, struct pci_outbound),
274 VMSTATE_UINT32(powar, struct pci_outbound),
275 VMSTATE_END_OF_LIST()
aurel3274c62ba2009-03-02 16:42:23 +0000276 }
Juan Quintelae0433ec2010-12-02 15:29:42 +0100277};
aurel3274c62ba2009-03-02 16:42:23 +0000278
Juan Quintelae0433ec2010-12-02 15:29:42 +0100279static const VMStateDescription vmstate_pci_inbound = {
280 .name = "pci_inbound",
281 .version_id = 0,
282 .minimum_version_id = 0,
283 .minimum_version_id_old = 0,
284 .fields = (VMStateField[]) {
285 VMSTATE_UINT32(pitar, struct pci_inbound),
286 VMSTATE_UINT32(piwbar, struct pci_inbound),
287 VMSTATE_UINT32(piwbear, struct pci_inbound),
288 VMSTATE_UINT32(piwar, struct pci_inbound),
289 VMSTATE_END_OF_LIST()
aurel3274c62ba2009-03-02 16:42:23 +0000290 }
Juan Quintelae0433ec2010-12-02 15:29:42 +0100291};
aurel3274c62ba2009-03-02 16:42:23 +0000292
Juan Quintelae0433ec2010-12-02 15:29:42 +0100293static const VMStateDescription vmstate_ppce500_pci = {
294 .name = "ppce500_pci",
295 .version_id = 1,
296 .minimum_version_id = 1,
297 .minimum_version_id_old = 1,
298 .fields = (VMStateField[]) {
Juan Quintelae0433ec2010-12-02 15:29:42 +0100299 VMSTATE_STRUCT_ARRAY(pob, PPCE500PCIState, PPCE500_PCI_NR_POBS, 1,
300 vmstate_pci_outbound, struct pci_outbound),
301 VMSTATE_STRUCT_ARRAY(pib, PPCE500PCIState, PPCE500_PCI_NR_PIBS, 1,
302 vmstate_pci_outbound, struct pci_inbound),
303 VMSTATE_UINT32(gasket_time, PPCE500PCIState),
304 VMSTATE_END_OF_LIST()
aurel3274c62ba2009-03-02 16:42:23 +0000305 }
Juan Quintelae0433ec2010-12-02 15:29:42 +0100306};
aurel3274c62ba2009-03-02 16:42:23 +0000307
Avi Kivity1e391012011-07-26 14:26:19 +0300308#include "exec-memory.h"
309
Alexander Grafbe13cc72010-08-31 00:22:28 +0200310static int e500_pcihost_initfn(SysBusDevice *dev)
311{
312 PCIHostState *h;
313 PPCE500PCIState *s;
314 PCIBus *b;
315 int i;
Avi Kivityaee97b82011-08-08 16:09:04 +0300316 MemoryRegion *address_space_mem = get_system_memory();
317 MemoryRegion *address_space_io = get_system_io();
Alexander Grafbe13cc72010-08-31 00:22:28 +0200318
Andreas Färber8558d942012-08-20 19:08:08 +0200319 h = PCI_HOST_BRIDGE(dev);
Andreas Färber9c1a61f2012-08-20 19:08:03 +0200320 s = PPC_E500_PCI_HOST_BRIDGE(dev);
Alexander Grafbe13cc72010-08-31 00:22:28 +0200321
322 for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
323 sysbus_init_irq(dev, &s->irq[i]);
324 }
325
Andreas Färber9c1a61f2012-08-20 19:08:03 +0200326 b = pci_register_bus(DEVICE(dev), NULL, mpc85xx_pci_set_irq,
Avi Kivityaee97b82011-08-08 16:09:04 +0300327 mpc85xx_pci_map_irq, s->irq, address_space_mem,
328 address_space_io, PCI_DEVFN(0x11, 0), 4);
Andreas Färber9c1a61f2012-08-20 19:08:03 +0200329 h->bus = b;
Alexander Grafbe13cc72010-08-31 00:22:28 +0200330
331 pci_create_simple(b, 0, "e500-host-bridge");
332
Benoît Canetcb4e15c2011-12-16 23:37:47 +0100333 memory_region_init(&s->container, "pci-container", PCIE500_ALL_SIZE);
Avi Kivityd0ed8072011-07-24 17:47:18 +0300334 memory_region_init_io(&h->conf_mem, &pci_host_conf_be_ops, h,
335 "pci-conf-idx", 4);
336 memory_region_init_io(&h->data_mem, &pci_host_data_le_ops, h,
337 "pci-conf-data", 4);
Avi Kivitycd5cba72011-11-20 11:52:58 +0200338 memory_region_init_io(&s->iomem, &e500_pci_reg_ops, s,
339 "pci.reg", PCIE500_REG_SIZE);
Benoît Canetcb4e15c2011-12-16 23:37:47 +0100340 memory_region_add_subregion(&s->container, PCIE500_CFGADDR, &h->conf_mem);
341 memory_region_add_subregion(&s->container, PCIE500_CFGDATA, &h->data_mem);
342 memory_region_add_subregion(&s->container, PCIE500_REG_BASE, &s->iomem);
343 sysbus_init_mmio(dev, &s->container);
Alexander Grafbe13cc72010-08-31 00:22:28 +0200344
345 return 0;
346}
347
Anthony Liguori40021f02011-12-04 12:22:06 -0600348static void e500_host_bridge_class_init(ObjectClass *klass, void *data)
349{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600350 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -0600351 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
352
353 k->vendor_id = PCI_VENDOR_ID_FREESCALE;
354 k->device_id = PCI_DEVICE_ID_MPC8533E;
355 k->class_id = PCI_CLASS_PROCESSOR_POWERPC;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600356 dc->desc = "Host bridge";
Anthony Liguori40021f02011-12-04 12:22:06 -0600357}
358
Andreas Färber4240abf2012-08-20 19:07:56 +0200359static const TypeInfo e500_host_bridge_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600360 .name = "e500-host-bridge",
361 .parent = TYPE_PCI_DEVICE,
362 .instance_size = sizeof(PCIDevice),
363 .class_init = e500_host_bridge_class_init,
Alexander Grafbe13cc72010-08-31 00:22:28 +0200364};
365
Anthony Liguori999e12b2012-01-24 13:12:29 -0600366static void e500_pcihost_class_init(ObjectClass *klass, void *data)
367{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600368 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600369 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
370
371 k->init = e500_pcihost_initfn;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600372 dc->vmsd = &vmstate_ppce500_pci;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600373}
374
Andreas Färber4240abf2012-08-20 19:07:56 +0200375static const TypeInfo e500_pcihost_info = {
Andreas Färber9c1a61f2012-08-20 19:08:03 +0200376 .name = TYPE_PPC_E500_PCI_HOST_BRIDGE,
Andreas Färber8558d942012-08-20 19:08:08 +0200377 .parent = TYPE_PCI_HOST_BRIDGE,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600378 .instance_size = sizeof(PPCE500PCIState),
379 .class_init = e500_pcihost_class_init,
Alexander Grafbe13cc72010-08-31 00:22:28 +0200380};
381
Andreas Färber83f7d432012-02-09 15:20:55 +0100382static void e500_pci_register_types(void)
Alexander Grafbe13cc72010-08-31 00:22:28 +0200383{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600384 type_register_static(&e500_pcihost_info);
385 type_register_static(&e500_host_bridge_info);
Alexander Grafbe13cc72010-08-31 00:22:28 +0200386}
Andreas Färber83f7d432012-02-09 15:20:55 +0100387
388type_init(e500_pci_register_types)