2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 * Copyright IBM Corp. 2008
20 /* This file implements emulation of the 32-bit PCI controller found in some
21 * 4xx SoCs, such as the 440EP. */
27 typedef target_phys_addr_t pci_addr_t;
34 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
36 #define DPRINTF(fmt, ...)
51 #define PPC4xx_PCI_NR_PMMS 3
52 #define PPC4xx_PCI_NR_PTMS 2
54 struct PPC4xxPCIState {
55 struct PCIMasterMap pmm[PPC4xx_PCI_NR_PMMS];
56 struct PCITargetMap ptm[PPC4xx_PCI_NR_PTMS];
58 PCIHostState pci_state;
61 typedef struct PPC4xxPCIState PPC4xxPCIState;
63 #define PCIC0_CFGADDR 0x0
64 #define PCIC0_CFGDATA 0x4
66 /* PLB Memory Map (PMM) registers specify which PLB addresses are translated to
68 #define PCIL0_PMM0LA 0x0
69 #define PCIL0_PMM0MA 0x4
70 #define PCIL0_PMM0PCILA 0x8
71 #define PCIL0_PMM0PCIHA 0xc
72 #define PCIL0_PMM1LA 0x10
73 #define PCIL0_PMM1MA 0x14
74 #define PCIL0_PMM1PCILA 0x18
75 #define PCIL0_PMM1PCIHA 0x1c
76 #define PCIL0_PMM2LA 0x20
77 #define PCIL0_PMM2MA 0x24
78 #define PCIL0_PMM2PCILA 0x28
79 #define PCIL0_PMM2PCIHA 0x2c
81 /* PCI Target Map (PTM) registers specify which PCI addresses are translated to
83 #define PCIL0_PTM1MS 0x30
84 #define PCIL0_PTM1LA 0x34
85 #define PCIL0_PTM2MS 0x38
86 #define PCIL0_PTM2LA 0x3c
87 #define PCI_REG_SIZE 0x40
90 static uint32_t pci4xx_cfgaddr_readl(void *opaque, target_phys_addr_t addr)
92 PPC4xxPCIState *ppc4xx_pci = opaque;
94 return ppc4xx_pci->pci_state.config_reg;
97 static CPUReadMemoryFunc *pci4xx_cfgaddr_read[] = {
98 &pci4xx_cfgaddr_readl,
99 &pci4xx_cfgaddr_readl,
100 &pci4xx_cfgaddr_readl,
103 static void pci4xx_cfgaddr_writel(void *opaque, target_phys_addr_t addr,
106 PPC4xxPCIState *ppc4xx_pci = opaque;
108 #ifdef TARGET_WORDS_BIGENDIAN
109 value = bswap32(value);
112 ppc4xx_pci->pci_state.config_reg = value & ~0x3;
115 static CPUWriteMemoryFunc *pci4xx_cfgaddr_write[] = {
116 &pci4xx_cfgaddr_writel,
117 &pci4xx_cfgaddr_writel,
118 &pci4xx_cfgaddr_writel,
121 static CPUReadMemoryFunc *pci4xx_cfgdata_read[] = {
122 &pci_host_data_readb,
123 &pci_host_data_readw,
124 &pci_host_data_readl,
127 static CPUWriteMemoryFunc *pci4xx_cfgdata_write[] = {
128 &pci_host_data_writeb,
129 &pci_host_data_writew,
130 &pci_host_data_writel,
133 static void ppc4xx_pci_reg_write4(void *opaque, target_phys_addr_t offset,
136 struct PPC4xxPCIState *pci = opaque;
138 #ifdef TARGET_WORDS_BIGENDIAN
139 value = bswap32(value);
142 /* We ignore all target attempts at PCI configuration, effectively
143 * assuming a bidirectional 1:1 mapping of PLB and PCI space. */
147 pci->pmm[0].la = value;
150 pci->pmm[0].ma = value;
152 case PCIL0_PMM0PCIHA:
153 pci->pmm[0].pciha = value;
155 case PCIL0_PMM0PCILA:
156 pci->pmm[0].pcila = value;
160 pci->pmm[1].la = value;
163 pci->pmm[1].ma = value;
165 case PCIL0_PMM1PCIHA:
166 pci->pmm[1].pciha = value;
168 case PCIL0_PMM1PCILA:
169 pci->pmm[1].pcila = value;
173 pci->pmm[2].la = value;
176 pci->pmm[2].ma = value;
178 case PCIL0_PMM2PCIHA:
179 pci->pmm[2].pciha = value;
181 case PCIL0_PMM2PCILA:
182 pci->pmm[2].pcila = value;
186 pci->ptm[0].ms = value;
189 pci->ptm[0].la = value;
192 pci->ptm[1].ms = value;
195 pci->ptm[1].la = value;
199 printf("%s: unhandled PCI internal register 0x%lx\n", __func__,
200 (unsigned long)offset);
205 static uint32_t ppc4xx_pci_reg_read4(void *opaque, target_phys_addr_t offset)
207 struct PPC4xxPCIState *pci = opaque;
212 value = pci->pmm[0].la;
215 value = pci->pmm[0].ma;
217 case PCIL0_PMM0PCIHA:
218 value = pci->pmm[0].pciha;
220 case PCIL0_PMM0PCILA:
221 value = pci->pmm[0].pcila;
225 value = pci->pmm[1].la;
228 value = pci->pmm[1].ma;
230 case PCIL0_PMM1PCIHA:
231 value = pci->pmm[1].pciha;
233 case PCIL0_PMM1PCILA:
234 value = pci->pmm[1].pcila;
238 value = pci->pmm[2].la;
241 value = pci->pmm[2].ma;
243 case PCIL0_PMM2PCIHA:
244 value = pci->pmm[2].pciha;
246 case PCIL0_PMM2PCILA:
247 value = pci->pmm[2].pcila;
251 value = pci->ptm[0].ms;
254 value = pci->ptm[0].la;
257 value = pci->ptm[1].ms;
260 value = pci->ptm[1].la;
264 printf("%s: invalid PCI internal register 0x%lx\n", __func__,
265 (unsigned long)offset);
269 #ifdef TARGET_WORDS_BIGENDIAN
270 value = bswap32(value);
276 static CPUReadMemoryFunc *pci_reg_read[] = {
277 &ppc4xx_pci_reg_read4,
278 &ppc4xx_pci_reg_read4,
279 &ppc4xx_pci_reg_read4,
282 static CPUWriteMemoryFunc *pci_reg_write[] = {
283 &ppc4xx_pci_reg_write4,
284 &ppc4xx_pci_reg_write4,
285 &ppc4xx_pci_reg_write4,
288 static void ppc4xx_pci_reset(void *opaque)
290 struct PPC4xxPCIState *pci = opaque;
292 memset(pci->pmm, 0, sizeof(pci->pmm));
293 memset(pci->ptm, 0, sizeof(pci->ptm));
296 /* On Bamboo, all pins from each slot are tied to a single board IRQ. This
297 * may need further refactoring for other boards. */
298 static int ppc4xx_pci_map_irq(PCIDevice *pci_dev, int irq_num)
300 int slot = pci_dev->devfn >> 3;
302 DPRINTF("%s: devfn %x irq %d -> %d\n", __func__,
303 pci_dev->devfn, irq_num, slot);
308 static void ppc4xx_pci_set_irq(qemu_irq *pci_irqs, int irq_num, int level)
310 DPRINTF("%s: PCI irq %d\n", __func__, irq_num);
311 qemu_set_irq(pci_irqs[irq_num], level);
314 static void ppc4xx_pci_save(QEMUFile *f, void *opaque)
316 PPC4xxPCIState *controller = opaque;
319 pci_device_save(controller->pci_dev, f);
321 for (i = 0; i < PPC4xx_PCI_NR_PMMS; i++) {
322 qemu_put_be32s(f, &controller->pmm[i].la);
323 qemu_put_be32s(f, &controller->pmm[i].ma);
324 qemu_put_be32s(f, &controller->pmm[i].pcila);
325 qemu_put_be32s(f, &controller->pmm[i].pciha);
328 for (i = 0; i < PPC4xx_PCI_NR_PTMS; i++) {
329 qemu_put_be32s(f, &controller->ptm[i].ms);
330 qemu_put_be32s(f, &controller->ptm[i].la);
334 static int ppc4xx_pci_load(QEMUFile *f, void *opaque, int version_id)
336 PPC4xxPCIState *controller = opaque;
342 pci_device_load(controller->pci_dev, f);
344 for (i = 0; i < PPC4xx_PCI_NR_PMMS; i++) {
345 qemu_get_be32s(f, &controller->pmm[i].la);
346 qemu_get_be32s(f, &controller->pmm[i].ma);
347 qemu_get_be32s(f, &controller->pmm[i].pcila);
348 qemu_get_be32s(f, &controller->pmm[i].pciha);
351 for (i = 0; i < PPC4xx_PCI_NR_PTMS; i++) {
352 qemu_get_be32s(f, &controller->ptm[i].ms);
353 qemu_get_be32s(f, &controller->ptm[i].la);
359 /* XXX Interrupt acknowledge cycles not supported. */
360 PCIBus *ppc4xx_pci_init(CPUState *env, qemu_irq pci_irqs[4],
361 target_phys_addr_t config_space,
362 target_phys_addr_t int_ack,
363 target_phys_addr_t special_cycle,
364 target_phys_addr_t registers)
366 PPC4xxPCIState *controller;
368 static int ppc4xx_pci_id;
371 controller = qemu_mallocz(sizeof(PPC4xxPCIState));
373 controller->pci_state.bus = pci_register_bus(NULL, "pci",
378 controller->pci_dev = pci_register_device(controller->pci_state.bus,
379 "host bridge", sizeof(PCIDevice),
381 pci_conf = controller->pci_dev->config;
382 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_IBM);
383 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_IBM_440GX);
384 pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_OTHER);
387 index = cpu_register_io_memory(pci4xx_cfgaddr_read,
388 pci4xx_cfgaddr_write, controller);
391 cpu_register_physical_memory(config_space + PCIC0_CFGADDR, 4, index);
394 index = cpu_register_io_memory(pci4xx_cfgdata_read,
395 pci4xx_cfgdata_write,
396 &controller->pci_state);
399 cpu_register_physical_memory(config_space + PCIC0_CFGDATA, 4, index);
401 /* Internal registers */
402 index = cpu_register_io_memory(pci_reg_read, pci_reg_write, controller);
405 cpu_register_physical_memory(registers, PCI_REG_SIZE, index);
407 qemu_register_reset(ppc4xx_pci_reset, controller);
409 /* XXX load/save code not tested. */
410 register_savevm("ppc4xx_pci", ppc4xx_pci_id++, 1,
411 ppc4xx_pci_save, ppc4xx_pci_load, controller);
413 return controller->pci_state.bus;
416 printf("%s error\n", __func__);
417 qemu_free(controller);