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
16 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 /* This file implements emulation of the 32-bit PCI controller found in some
20 * 4xx SoCs, such as the 440EP. */
27 #include "exec-memory.h"
31 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
33 #define DPRINTF(fmt, ...)
48 #define PPC4xx_PCI_NR_PMMS 3
49 #define PPC4xx_PCI_NR_PTMS 2
51 struct PPC4xxPCIState
{
52 PCIHostState pci_state
;
54 struct PCIMasterMap pmm
[PPC4xx_PCI_NR_PMMS
];
55 struct PCITargetMap ptm
[PPC4xx_PCI_NR_PTMS
];
58 MemoryRegion container
;
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_BASE 0x800000
88 #define PCI_REG_SIZE 0x40
90 #define PCI_ALL_SIZE (PCI_REG_BASE + PCI_REG_SIZE)
92 static uint64_t pci4xx_cfgaddr_read(void *opaque
, target_phys_addr_t addr
,
95 PPC4xxPCIState
*ppc4xx_pci
= opaque
;
97 return ppc4xx_pci
->pci_state
.config_reg
;
100 static void pci4xx_cfgaddr_write(void *opaque
, target_phys_addr_t addr
,
101 uint64_t value
, unsigned size
)
103 PPC4xxPCIState
*ppc4xx_pci
= opaque
;
105 ppc4xx_pci
->pci_state
.config_reg
= value
& ~0x3;
108 static const MemoryRegionOps pci4xx_cfgaddr_ops
= {
109 .read
= pci4xx_cfgaddr_read
,
110 .write
= pci4xx_cfgaddr_write
,
111 .endianness
= DEVICE_LITTLE_ENDIAN
,
114 static void ppc4xx_pci_reg_write4(void *opaque
, target_phys_addr_t offset
,
115 uint64_t value
, unsigned size
)
117 struct PPC4xxPCIState
*pci
= opaque
;
119 /* We ignore all target attempts at PCI configuration, effectively
120 * assuming a bidirectional 1:1 mapping of PLB and PCI space. */
124 pci
->pmm
[0].la
= value
;
127 pci
->pmm
[0].ma
= value
;
129 case PCIL0_PMM0PCIHA
:
130 pci
->pmm
[0].pciha
= value
;
132 case PCIL0_PMM0PCILA
:
133 pci
->pmm
[0].pcila
= value
;
137 pci
->pmm
[1].la
= value
;
140 pci
->pmm
[1].ma
= value
;
142 case PCIL0_PMM1PCIHA
:
143 pci
->pmm
[1].pciha
= value
;
145 case PCIL0_PMM1PCILA
:
146 pci
->pmm
[1].pcila
= value
;
150 pci
->pmm
[2].la
= value
;
153 pci
->pmm
[2].ma
= value
;
155 case PCIL0_PMM2PCIHA
:
156 pci
->pmm
[2].pciha
= value
;
158 case PCIL0_PMM2PCILA
:
159 pci
->pmm
[2].pcila
= value
;
163 pci
->ptm
[0].ms
= value
;
166 pci
->ptm
[0].la
= value
;
169 pci
->ptm
[1].ms
= value
;
172 pci
->ptm
[1].la
= value
;
176 printf("%s: unhandled PCI internal register 0x%lx\n", __func__
,
177 (unsigned long)offset
);
182 static uint64_t ppc4xx_pci_reg_read4(void *opaque
, target_phys_addr_t offset
,
185 struct PPC4xxPCIState
*pci
= opaque
;
190 value
= pci
->pmm
[0].la
;
193 value
= pci
->pmm
[0].ma
;
195 case PCIL0_PMM0PCIHA
:
196 value
= pci
->pmm
[0].pciha
;
198 case PCIL0_PMM0PCILA
:
199 value
= pci
->pmm
[0].pcila
;
203 value
= pci
->pmm
[1].la
;
206 value
= pci
->pmm
[1].ma
;
208 case PCIL0_PMM1PCIHA
:
209 value
= pci
->pmm
[1].pciha
;
211 case PCIL0_PMM1PCILA
:
212 value
= pci
->pmm
[1].pcila
;
216 value
= pci
->pmm
[2].la
;
219 value
= pci
->pmm
[2].ma
;
221 case PCIL0_PMM2PCIHA
:
222 value
= pci
->pmm
[2].pciha
;
224 case PCIL0_PMM2PCILA
:
225 value
= pci
->pmm
[2].pcila
;
229 value
= pci
->ptm
[0].ms
;
232 value
= pci
->ptm
[0].la
;
235 value
= pci
->ptm
[1].ms
;
238 value
= pci
->ptm
[1].la
;
242 printf("%s: invalid PCI internal register 0x%lx\n", __func__
,
243 (unsigned long)offset
);
250 static const MemoryRegionOps pci_reg_ops
= {
251 .read
= ppc4xx_pci_reg_read4
,
252 .write
= ppc4xx_pci_reg_write4
,
253 .endianness
= DEVICE_LITTLE_ENDIAN
,
256 static void ppc4xx_pci_reset(void *opaque
)
258 struct PPC4xxPCIState
*pci
= opaque
;
260 memset(pci
->pmm
, 0, sizeof(pci
->pmm
));
261 memset(pci
->ptm
, 0, sizeof(pci
->ptm
));
264 /* On Bamboo, all pins from each slot are tied to a single board IRQ. This
265 * may need further refactoring for other boards. */
266 static int ppc4xx_pci_map_irq(PCIDevice
*pci_dev
, int irq_num
)
268 int slot
= pci_dev
->devfn
>> 3;
270 DPRINTF("%s: devfn %x irq %d -> %d\n", __func__
,
271 pci_dev
->devfn
, irq_num
, slot
);
276 static void ppc4xx_pci_set_irq(void *opaque
, int irq_num
, int level
)
278 qemu_irq
*pci_irqs
= opaque
;
280 DPRINTF("%s: PCI irq %d\n", __func__
, irq_num
);
282 fprintf(stderr
, "%s: PCI irq %d\n", __func__
, irq_num
);
285 qemu_set_irq(pci_irqs
[irq_num
], level
);
288 static const VMStateDescription vmstate_pci_master_map
= {
289 .name
= "pci_master_map",
291 .minimum_version_id
= 0,
292 .minimum_version_id_old
= 0,
293 .fields
= (VMStateField
[]) {
294 VMSTATE_UINT32(la
, struct PCIMasterMap
),
295 VMSTATE_UINT32(ma
, struct PCIMasterMap
),
296 VMSTATE_UINT32(pcila
, struct PCIMasterMap
),
297 VMSTATE_UINT32(pciha
, struct PCIMasterMap
),
298 VMSTATE_END_OF_LIST()
302 static const VMStateDescription vmstate_pci_target_map
= {
303 .name
= "pci_target_map",
305 .minimum_version_id
= 0,
306 .minimum_version_id_old
= 0,
307 .fields
= (VMStateField
[]) {
308 VMSTATE_UINT32(ms
, struct PCITargetMap
),
309 VMSTATE_UINT32(la
, struct PCITargetMap
),
310 VMSTATE_END_OF_LIST()
314 static const VMStateDescription vmstate_ppc4xx_pci
= {
315 .name
= "ppc4xx_pci",
317 .minimum_version_id
= 1,
318 .minimum_version_id_old
= 1,
319 .fields
= (VMStateField
[]) {
320 VMSTATE_STRUCT_ARRAY(pmm
, PPC4xxPCIState
, PPC4xx_PCI_NR_PMMS
, 1,
321 vmstate_pci_master_map
,
322 struct PCIMasterMap
),
323 VMSTATE_STRUCT_ARRAY(ptm
, PPC4xxPCIState
, PPC4xx_PCI_NR_PTMS
, 1,
324 vmstate_pci_target_map
,
325 struct PCITargetMap
),
326 VMSTATE_END_OF_LIST()
330 /* XXX Interrupt acknowledge cycles not supported. */
331 static int ppc4xx_pcihost_initfn(SysBusDevice
*dev
)
338 h
= FROM_SYSBUS(PCIHostState
, sysbus_from_qdev(dev
));
339 s
= DO_UPCAST(PPC4xxPCIState
, pci_state
, h
);
341 for (i
= 0; i
< ARRAY_SIZE(s
->irq
); i
++) {
342 sysbus_init_irq(dev
, &s
->irq
[i
]);
345 b
= pci_register_bus(&s
->pci_state
.busdev
.qdev
, NULL
, ppc4xx_pci_set_irq
,
346 ppc4xx_pci_map_irq
, s
->irq
, get_system_memory(),
347 get_system_io(), 0, 4);
348 s
->pci_state
.bus
= b
;
350 pci_create_simple(b
, 0, "ppc4xx-host-bridge");
352 /* XXX split into 2 memory regions, one for config space, one for regs */
353 memory_region_init(&s
->container
, "pci-container", PCI_ALL_SIZE
);
354 memory_region_init_io(&h
->conf_mem
, &pci_host_conf_le_ops
, h
,
356 memory_region_init_io(&h
->data_mem
, &pci_host_data_le_ops
, h
,
358 memory_region_init_io(&s
->iomem
, &pci_reg_ops
, s
,
359 "pci.reg", PCI_REG_SIZE
);
360 memory_region_add_subregion(&s
->container
, PCIC0_CFGADDR
, &h
->conf_mem
);
361 memory_region_add_subregion(&s
->container
, PCIC0_CFGDATA
, &h
->data_mem
);
362 memory_region_add_subregion(&s
->container
, PCI_REG_BASE
, &s
->iomem
);
363 sysbus_init_mmio(dev
, &s
->container
);
364 qemu_register_reset(ppc4xx_pci_reset
, s
);
369 static void ppc4xx_host_bridge_class_init(ObjectClass
*klass
, void *data
)
371 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
372 DeviceClass
*dc
= DEVICE_CLASS(klass
);
374 dc
->desc
= "Host bridge";
375 k
->vendor_id
= PCI_VENDOR_ID_IBM
;
376 k
->device_id
= PCI_DEVICE_ID_IBM_440GX
;
377 k
->class_id
= PCI_CLASS_BRIDGE_OTHER
;
380 static TypeInfo ppc4xx_host_bridge_info
= {
381 .name
= "ppc4xx-host-bridge",
382 .parent
= TYPE_PCI_DEVICE
,
383 .instance_size
= sizeof(PCIDevice
),
384 .class_init
= ppc4xx_host_bridge_class_init
,
387 static void ppc4xx_pcihost_class_init(ObjectClass
*klass
, void *data
)
389 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
390 DeviceClass
*dc
= DEVICE_CLASS(klass
);
392 k
->init
= ppc4xx_pcihost_initfn
;
393 dc
->vmsd
= &vmstate_ppc4xx_pci
;
396 static TypeInfo ppc4xx_pcihost_info
= {
397 .name
= "ppc4xx-pcihost",
398 .parent
= TYPE_SYS_BUS_DEVICE
,
399 .instance_size
= sizeof(PPC4xxPCIState
),
400 .class_init
= ppc4xx_pcihost_class_init
,
403 static void ppc4xx_pci_register_types(void)
405 type_register_static(&ppc4xx_pcihost_info
);
406 type_register_static(&ppc4xx_host_bridge_info
);
409 type_init(ppc4xx_pci_register_types
)