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, see <http://www.gnu.org/licenses/>.
14 * Copyright IBM Corp. 2008
19 /* This file implements emulation of the 32-bit PCI controller found in some
20 * 4xx SoCs, such as the 440EP. */
26 typedef target_phys_addr_t pci_addr_t;
33 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
35 #define DPRINTF(fmt, ...)
50 #define PPC4xx_PCI_NR_PMMS 3
51 #define PPC4xx_PCI_NR_PTMS 2
53 struct PPC4xxPCIState {
54 struct PCIMasterMap pmm[PPC4xx_PCI_NR_PMMS];
55 struct PCITargetMap ptm[PPC4xx_PCI_NR_PTMS];
57 PCIHostState pci_state;
60 typedef struct PPC4xxPCIState PPC4xxPCIState;
62 #define PCIC0_CFGADDR 0x0
63 #define PCIC0_CFGDATA 0x4
65 /* PLB Memory Map (PMM) registers specify which PLB addresses are translated to
67 #define PCIL0_PMM0LA 0x0
68 #define PCIL0_PMM0MA 0x4
69 #define PCIL0_PMM0PCILA 0x8
70 #define PCIL0_PMM0PCIHA 0xc
71 #define PCIL0_PMM1LA 0x10
72 #define PCIL0_PMM1MA 0x14
73 #define PCIL0_PMM1PCILA 0x18
74 #define PCIL0_PMM1PCIHA 0x1c
75 #define PCIL0_PMM2LA 0x20
76 #define PCIL0_PMM2MA 0x24
77 #define PCIL0_PMM2PCILA 0x28
78 #define PCIL0_PMM2PCIHA 0x2c
80 /* PCI Target Map (PTM) registers specify which PCI addresses are translated to
82 #define PCIL0_PTM1MS 0x30
83 #define PCIL0_PTM1LA 0x34
84 #define PCIL0_PTM2MS 0x38
85 #define PCIL0_PTM2LA 0x3c
86 #define PCI_REG_SIZE 0x40
89 static uint32_t pci4xx_cfgaddr_readl(void *opaque, target_phys_addr_t addr)
91 PPC4xxPCIState *ppc4xx_pci = opaque;
93 return ppc4xx_pci->pci_state.config_reg;
96 static CPUReadMemoryFunc * const pci4xx_cfgaddr_read[] = {
97 &pci4xx_cfgaddr_readl,
98 &pci4xx_cfgaddr_readl,
99 &pci4xx_cfgaddr_readl,
102 static void pci4xx_cfgaddr_writel(void *opaque, target_phys_addr_t addr,
105 PPC4xxPCIState *ppc4xx_pci = opaque;
107 #ifdef TARGET_WORDS_BIGENDIAN
108 value = bswap32(value);
111 ppc4xx_pci->pci_state.config_reg = value & ~0x3;
114 static CPUWriteMemoryFunc * const pci4xx_cfgaddr_write[] = {
115 &pci4xx_cfgaddr_writel,
116 &pci4xx_cfgaddr_writel,
117 &pci4xx_cfgaddr_writel,
120 static CPUReadMemoryFunc * const pci4xx_cfgdata_read[] = {
121 &pci_host_data_readb,
122 &pci_host_data_readw,
123 &pci_host_data_readl,
126 static CPUWriteMemoryFunc * const pci4xx_cfgdata_write[] = {
127 &pci_host_data_writeb,
128 &pci_host_data_writew,
129 &pci_host_data_writel,
132 static void ppc4xx_pci_reg_write4(void *opaque, target_phys_addr_t offset,
135 struct PPC4xxPCIState *pci = opaque;
137 #ifdef TARGET_WORDS_BIGENDIAN
138 value = bswap32(value);
141 /* We ignore all target attempts at PCI configuration, effectively
142 * assuming a bidirectional 1:1 mapping of PLB and PCI space. */
146 pci->pmm[0].la = value;
149 pci->pmm[0].ma = value;
151 case PCIL0_PMM0PCIHA:
152 pci->pmm[0].pciha = value;
154 case PCIL0_PMM0PCILA:
155 pci->pmm[0].pcila = value;
159 pci->pmm[1].la = value;
162 pci->pmm[1].ma = value;
164 case PCIL0_PMM1PCIHA:
165 pci->pmm[1].pciha = value;
167 case PCIL0_PMM1PCILA:
168 pci->pmm[1].pcila = value;
172 pci->pmm[2].la = value;
175 pci->pmm[2].ma = value;
177 case PCIL0_PMM2PCIHA:
178 pci->pmm[2].pciha = value;
180 case PCIL0_PMM2PCILA:
181 pci->pmm[2].pcila = value;
185 pci->ptm[0].ms = value;
188 pci->ptm[0].la = value;
191 pci->ptm[1].ms = value;
194 pci->ptm[1].la = value;
198 printf("%s: unhandled PCI internal register 0x%lx\n", __func__,
199 (unsigned long)offset);
204 static uint32_t ppc4xx_pci_reg_read4(void *opaque, target_phys_addr_t offset)
206 struct PPC4xxPCIState *pci = opaque;
211 value = pci->pmm[0].la;
214 value = pci->pmm[0].ma;
216 case PCIL0_PMM0PCIHA:
217 value = pci->pmm[0].pciha;
219 case PCIL0_PMM0PCILA:
220 value = pci->pmm[0].pcila;
224 value = pci->pmm[1].la;
227 value = pci->pmm[1].ma;
229 case PCIL0_PMM1PCIHA:
230 value = pci->pmm[1].pciha;
232 case PCIL0_PMM1PCILA:
233 value = pci->pmm[1].pcila;
237 value = pci->pmm[2].la;
240 value = pci->pmm[2].ma;
242 case PCIL0_PMM2PCIHA:
243 value = pci->pmm[2].pciha;
245 case PCIL0_PMM2PCILA:
246 value = pci->pmm[2].pcila;
250 value = pci->ptm[0].ms;
253 value = pci->ptm[0].la;
256 value = pci->ptm[1].ms;
259 value = pci->ptm[1].la;
263 printf("%s: invalid PCI internal register 0x%lx\n", __func__,
264 (unsigned long)offset);
268 #ifdef TARGET_WORDS_BIGENDIAN
269 value = bswap32(value);
275 static CPUReadMemoryFunc * const pci_reg_read[] = {
276 &ppc4xx_pci_reg_read4,
277 &ppc4xx_pci_reg_read4,
278 &ppc4xx_pci_reg_read4,
281 static CPUWriteMemoryFunc * const pci_reg_write[] = {
282 &ppc4xx_pci_reg_write4,
283 &ppc4xx_pci_reg_write4,
284 &ppc4xx_pci_reg_write4,
287 static void ppc4xx_pci_reset(void *opaque)
289 struct PPC4xxPCIState *pci = opaque;
291 memset(pci->pmm, 0, sizeof(pci->pmm));
292 memset(pci->ptm, 0, sizeof(pci->ptm));
295 /* On Bamboo, all pins from each slot are tied to a single board IRQ. This
296 * may need further refactoring for other boards. */
297 static int ppc4xx_pci_map_irq(PCIDevice *pci_dev, int irq_num)
299 int slot = pci_dev->devfn >> 3;
301 DPRINTF("%s: devfn %x irq %d -> %d\n", __func__,
302 pci_dev->devfn, irq_num, slot);
307 static void ppc4xx_pci_set_irq(void *opaque, int irq_num, int level)
309 qemu_irq *pci_irqs = opaque;
311 DPRINTF("%s: PCI irq %d\n", __func__, irq_num);
312 qemu_set_irq(pci_irqs[irq_num], level);
315 static void ppc4xx_pci_save(QEMUFile *f, void *opaque)
317 PPC4xxPCIState *controller = opaque;
320 pci_device_save(controller->pci_dev, f);
322 for (i = 0; i < PPC4xx_PCI_NR_PMMS; i++) {
323 qemu_put_be32s(f, &controller->pmm[i].la);
324 qemu_put_be32s(f, &controller->pmm[i].ma);
325 qemu_put_be32s(f, &controller->pmm[i].pcila);
326 qemu_put_be32s(f, &controller->pmm[i].pciha);
329 for (i = 0; i < PPC4xx_PCI_NR_PTMS; i++) {
330 qemu_put_be32s(f, &controller->ptm[i].ms);
331 qemu_put_be32s(f, &controller->ptm[i].la);
335 static int ppc4xx_pci_load(QEMUFile *f, void *opaque, int version_id)
337 PPC4xxPCIState *controller = opaque;
343 pci_device_load(controller->pci_dev, f);
345 for (i = 0; i < PPC4xx_PCI_NR_PMMS; i++) {
346 qemu_get_be32s(f, &controller->pmm[i].la);
347 qemu_get_be32s(f, &controller->pmm[i].ma);
348 qemu_get_be32s(f, &controller->pmm[i].pcila);
349 qemu_get_be32s(f, &controller->pmm[i].pciha);
352 for (i = 0; i < PPC4xx_PCI_NR_PTMS; i++) {
353 qemu_get_be32s(f, &controller->ptm[i].ms);
354 qemu_get_be32s(f, &controller->ptm[i].la);
360 /* XXX Interrupt acknowledge cycles not supported. */
361 PCIBus *ppc4xx_pci_init(CPUState *env, qemu_irq pci_irqs[4],
362 target_phys_addr_t config_space,
363 target_phys_addr_t int_ack,
364 target_phys_addr_t special_cycle,
365 target_phys_addr_t registers)
367 PPC4xxPCIState *controller;
369 static int ppc4xx_pci_id;
372 controller = qemu_mallocz(sizeof(PPC4xxPCIState));
374 controller->pci_state.bus = pci_register_bus(NULL, "pci",
379 controller->pci_dev = pci_register_device(controller->pci_state.bus,
380 "host bridge", sizeof(PCIDevice),
382 pci_conf = controller->pci_dev->config;
383 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_IBM);
384 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_IBM_440GX);
385 pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_OTHER);
388 index = cpu_register_io_memory(pci4xx_cfgaddr_read,
389 pci4xx_cfgaddr_write, controller);
392 cpu_register_physical_memory(config_space + PCIC0_CFGADDR, 4, index);
395 index = cpu_register_io_memory(pci4xx_cfgdata_read,
396 pci4xx_cfgdata_write,
397 &controller->pci_state);
400 cpu_register_physical_memory(config_space + PCIC0_CFGDATA, 4, index);
402 /* Internal registers */
403 index = cpu_register_io_memory(pci_reg_read, pci_reg_write, controller);
406 cpu_register_physical_memory(registers, PCI_REG_SIZE, index);
408 qemu_register_reset(ppc4xx_pci_reset, controller);
410 /* XXX load/save code not tested. */
411 register_savevm("ppc4xx_pci", ppc4xx_pci_id++, 1,
412 ppc4xx_pci_save, ppc4xx_pci_load, controller);
414 return controller->pci_state.bus;
417 printf("%s error\n", __func__);
418 qemu_free(controller);