Merge commit '413fb2412d' into stable-0.11
[qemu-kvm/fedora.git] / hw / ppce500_pci.c
blob5b4673a82f0dff6d5d66d94ea7303eddc603f93b
1 /*
2 * QEMU PowerPC E500 embedded processors pci controller emulation
4 * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
6 * Author: Yu Liu, <yu.liu@freescale.com>
8 * This file is derived from hw/ppc4xx_pci.c,
9 * the copyright for that material belongs to the original owners.
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.
17 #include "hw.h"
18 #include "ppc.h"
19 #include "ppce500.h"
20 typedef target_phys_addr_t pci_addr_t;
21 #include "pci.h"
22 #include "pci_host.h"
23 #include "bswap.h"
24 #include "qemu-log.h"
26 #ifdef DEBUG_PCI
27 #define pci_debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
28 #else
29 #define pci_debug(fmt, ...)
30 #endif
32 #define PCIE500_CFGADDR 0x0
33 #define PCIE500_CFGDATA 0x4
34 #define PCIE500_REG_BASE 0xC00
35 #define PCIE500_REG_SIZE (0x1000 - PCIE500_REG_BASE)
37 #define PPCE500_PCI_CONFIG_ADDR 0x0
38 #define PPCE500_PCI_CONFIG_DATA 0x4
39 #define PPCE500_PCI_INTACK 0x8
41 #define PPCE500_PCI_OW1 (0xC20 - PCIE500_REG_BASE)
42 #define PPCE500_PCI_OW2 (0xC40 - PCIE500_REG_BASE)
43 #define PPCE500_PCI_OW3 (0xC60 - PCIE500_REG_BASE)
44 #define PPCE500_PCI_OW4 (0xC80 - PCIE500_REG_BASE)
45 #define PPCE500_PCI_IW3 (0xDA0 - PCIE500_REG_BASE)
46 #define PPCE500_PCI_IW2 (0xDC0 - PCIE500_REG_BASE)
47 #define PPCE500_PCI_IW1 (0xDE0 - PCIE500_REG_BASE)
49 #define PPCE500_PCI_GASKET_TIMR (0xE20 - PCIE500_REG_BASE)
51 #define PCI_POTAR 0x0
52 #define PCI_POTEAR 0x4
53 #define PCI_POWBAR 0x8
54 #define PCI_POWAR 0x10
56 #define PCI_PITAR 0x0
57 #define PCI_PIWBAR 0x8
58 #define PCI_PIWBEAR 0xC
59 #define PCI_PIWAR 0x10
61 #define PPCE500_PCI_NR_POBS 5
62 #define PPCE500_PCI_NR_PIBS 3
64 struct pci_outbound {
65 uint32_t potar;
66 uint32_t potear;
67 uint32_t powbar;
68 uint32_t powar;
71 struct pci_inbound {
72 uint32_t pitar;
73 uint32_t piwbar;
74 uint32_t piwbear;
75 uint32_t piwar;
78 struct PPCE500PCIState {
79 struct pci_outbound pob[PPCE500_PCI_NR_POBS];
80 struct pci_inbound pib[PPCE500_PCI_NR_PIBS];
81 uint32_t gasket_time;
82 PCIHostState pci_state;
83 PCIDevice *pci_dev;
86 typedef struct PPCE500PCIState PPCE500PCIState;
88 static uint32_t pcie500_cfgaddr_readl(void *opaque, target_phys_addr_t addr)
90 PPCE500PCIState *pci = opaque;
92 pci_debug("%s: (addr:" TARGET_FMT_plx ") -> value:%x\n", __func__, addr,
93 pci->pci_state.config_reg);
94 return pci->pci_state.config_reg;
97 static CPUReadMemoryFunc *pcie500_cfgaddr_read[] = {
98 &pcie500_cfgaddr_readl,
99 &pcie500_cfgaddr_readl,
100 &pcie500_cfgaddr_readl,
103 static void pcie500_cfgaddr_writel(void *opaque, target_phys_addr_t addr,
104 uint32_t value)
106 PPCE500PCIState *controller = opaque;
108 pci_debug("%s: value:%x -> (addr:" TARGET_FMT_plx ")\n", __func__, value,
109 addr);
110 controller->pci_state.config_reg = value & ~0x3;
113 static CPUWriteMemoryFunc *pcie500_cfgaddr_write[] = {
114 &pcie500_cfgaddr_writel,
115 &pcie500_cfgaddr_writel,
116 &pcie500_cfgaddr_writel,
119 static CPUReadMemoryFunc *pcie500_cfgdata_read[] = {
120 &pci_host_data_readb,
121 &pci_host_data_readw,
122 &pci_host_data_readl,
125 static CPUWriteMemoryFunc *pcie500_cfgdata_write[] = {
126 &pci_host_data_writeb,
127 &pci_host_data_writew,
128 &pci_host_data_writel,
131 static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr)
133 PPCE500PCIState *pci = opaque;
134 unsigned long win;
135 uint32_t value = 0;
137 win = addr & 0xfe0;
139 switch (win) {
140 case PPCE500_PCI_OW1:
141 case PPCE500_PCI_OW2:
142 case PPCE500_PCI_OW3:
143 case PPCE500_PCI_OW4:
144 switch (addr & 0xC) {
145 case PCI_POTAR: value = pci->pob[(addr >> 5) & 0x7].potar; break;
146 case PCI_POTEAR: value = pci->pob[(addr >> 5) & 0x7].potear; break;
147 case PCI_POWBAR: value = pci->pob[(addr >> 5) & 0x7].powbar; break;
148 case PCI_POWAR: value = pci->pob[(addr >> 5) & 0x7].powar; break;
149 default: break;
151 break;
153 case PPCE500_PCI_IW3:
154 case PPCE500_PCI_IW2:
155 case PPCE500_PCI_IW1:
156 switch (addr & 0xC) {
157 case PCI_PITAR: value = pci->pib[(addr >> 5) & 0x3].pitar; break;
158 case PCI_PIWBAR: value = pci->pib[(addr >> 5) & 0x3].piwbar; break;
159 case PCI_PIWBEAR: value = pci->pib[(addr >> 5) & 0x3].piwbear; break;
160 case PCI_PIWAR: value = pci->pib[(addr >> 5) & 0x3].piwar; break;
161 default: break;
163 break;
165 case PPCE500_PCI_GASKET_TIMR:
166 value = pci->gasket_time;
167 break;
169 default:
170 break;
173 pci_debug("%s: win:%lx(addr:" TARGET_FMT_plx ") -> value:%x\n", __func__,
174 win, addr, value);
175 return value;
178 static CPUReadMemoryFunc *e500_pci_reg_read[] = {
179 &pci_reg_read4,
180 &pci_reg_read4,
181 &pci_reg_read4,
184 static void pci_reg_write4(void *opaque, target_phys_addr_t addr,
185 uint32_t value)
187 PPCE500PCIState *pci = opaque;
188 unsigned long win;
190 win = addr & 0xfe0;
192 pci_debug("%s: value:%x -> win:%lx(addr:" TARGET_FMT_plx ")\n",
193 __func__, value, win, addr);
195 switch (win) {
196 case PPCE500_PCI_OW1:
197 case PPCE500_PCI_OW2:
198 case PPCE500_PCI_OW3:
199 case PPCE500_PCI_OW4:
200 switch (addr & 0xC) {
201 case PCI_POTAR: pci->pob[(addr >> 5) & 0x7].potar = value; break;
202 case PCI_POTEAR: pci->pob[(addr >> 5) & 0x7].potear = value; break;
203 case PCI_POWBAR: pci->pob[(addr >> 5) & 0x7].powbar = value; break;
204 case PCI_POWAR: pci->pob[(addr >> 5) & 0x7].powar = value; break;
205 default: break;
207 break;
209 case PPCE500_PCI_IW3:
210 case PPCE500_PCI_IW2:
211 case PPCE500_PCI_IW1:
212 switch (addr & 0xC) {
213 case PCI_PITAR: pci->pib[(addr >> 5) & 0x3].pitar = value; break;
214 case PCI_PIWBAR: pci->pib[(addr >> 5) & 0x3].piwbar = value; break;
215 case PCI_PIWBEAR: pci->pib[(addr >> 5) & 0x3].piwbear = value; break;
216 case PCI_PIWAR: pci->pib[(addr >> 5) & 0x3].piwar = value; break;
217 default: break;
219 break;
221 case PPCE500_PCI_GASKET_TIMR:
222 pci->gasket_time = value;
223 break;
225 default:
226 break;
230 static CPUWriteMemoryFunc *e500_pci_reg_write[] = {
231 &pci_reg_write4,
232 &pci_reg_write4,
233 &pci_reg_write4,
236 static int mpc85xx_pci_map_irq(PCIDevice *pci_dev, int irq_num)
238 int devno = pci_dev->devfn >> 3, ret = 0;
240 switch (devno) {
241 /* Two PCI slot */
242 case 0x11:
243 case 0x12:
244 ret = (irq_num + devno - 0x10) % 4;
245 break;
246 default:
247 printf("Error:%s:unknow dev number\n", __func__);
250 pci_debug("%s: devfn %x irq %d -> %d devno:%x\n", __func__,
251 pci_dev->devfn, irq_num, ret, devno);
253 return ret;
256 static void mpc85xx_pci_set_irq(qemu_irq *pic, int irq_num, int level)
258 pci_debug("%s: PCI irq %d, level:%d\n", __func__, irq_num, level);
260 qemu_set_irq(pic[irq_num], level);
263 static void ppce500_pci_save(QEMUFile *f, void *opaque)
265 PPCE500PCIState *controller = opaque;
266 int i;
268 pci_device_save(controller->pci_dev, f);
270 for (i = 0; i < PPCE500_PCI_NR_POBS; i++) {
271 qemu_put_be32s(f, &controller->pob[i].potar);
272 qemu_put_be32s(f, &controller->pob[i].potear);
273 qemu_put_be32s(f, &controller->pob[i].powbar);
274 qemu_put_be32s(f, &controller->pob[i].powar);
277 for (i = 0; i < PPCE500_PCI_NR_PIBS; i++) {
278 qemu_put_be32s(f, &controller->pib[i].pitar);
279 qemu_put_be32s(f, &controller->pib[i].piwbar);
280 qemu_put_be32s(f, &controller->pib[i].piwbear);
281 qemu_put_be32s(f, &controller->pib[i].piwar);
283 qemu_put_be32s(f, &controller->gasket_time);
286 static int ppce500_pci_load(QEMUFile *f, void *opaque, int version_id)
288 PPCE500PCIState *controller = opaque;
289 int i;
291 if (version_id != 1)
292 return -EINVAL;
294 pci_device_load(controller->pci_dev, f);
296 for (i = 0; i < PPCE500_PCI_NR_POBS; i++) {
297 qemu_get_be32s(f, &controller->pob[i].potar);
298 qemu_get_be32s(f, &controller->pob[i].potear);
299 qemu_get_be32s(f, &controller->pob[i].powbar);
300 qemu_get_be32s(f, &controller->pob[i].powar);
303 for (i = 0; i < PPCE500_PCI_NR_PIBS; i++) {
304 qemu_get_be32s(f, &controller->pib[i].pitar);
305 qemu_get_be32s(f, &controller->pib[i].piwbar);
306 qemu_get_be32s(f, &controller->pib[i].piwbear);
307 qemu_get_be32s(f, &controller->pib[i].piwar);
309 qemu_get_be32s(f, &controller->gasket_time);
311 return 0;
314 PCIBus *ppce500_pci_init(qemu_irq pci_irqs[4], target_phys_addr_t registers)
316 PPCE500PCIState *controller;
317 PCIDevice *d;
318 int index;
319 static int ppce500_pci_id;
321 controller = qemu_mallocz(sizeof(PPCE500PCIState));
323 controller->pci_state.bus = pci_register_bus(NULL, "pci",
324 mpc85xx_pci_set_irq,
325 mpc85xx_pci_map_irq,
326 pci_irqs, 0x88, 4);
327 d = pci_register_device(controller->pci_state.bus,
328 "host bridge", sizeof(PCIDevice),
329 0, NULL, NULL);
331 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_FREESCALE);
332 pci_config_set_device_id(d->config, PCI_DEVICE_ID_MPC8533E);
333 pci_config_set_class(d->config, PCI_CLASS_PROCESSOR_POWERPC);
335 controller->pci_dev = d;
337 /* CFGADDR */
338 index = cpu_register_io_memory(pcie500_cfgaddr_read,
339 pcie500_cfgaddr_write, controller);
340 if (index < 0)
341 goto free;
342 cpu_register_physical_memory(registers + PCIE500_CFGADDR, 4, index);
344 /* CFGDATA */
345 index = cpu_register_io_memory(pcie500_cfgdata_read,
346 pcie500_cfgdata_write,
347 &controller->pci_state);
348 if (index < 0)
349 goto free;
350 cpu_register_physical_memory(registers + PCIE500_CFGDATA, 4, index);
352 index = cpu_register_io_memory(e500_pci_reg_read,
353 e500_pci_reg_write, controller);
354 if (index < 0)
355 goto free;
356 cpu_register_physical_memory(registers + PCIE500_REG_BASE,
357 PCIE500_REG_SIZE, index);
359 /* XXX load/save code not tested. */
360 register_savevm("ppce500_pci", ppce500_pci_id++, 1,
361 ppce500_pci_save, ppce500_pci_load, controller);
363 return controller->pci_state.bus;
365 free:
366 printf("%s error\n", __func__);
367 qemu_free(controller);
368 return NULL;