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. */
23 #include "hw/ppc/ppc.h"
24 #include "hw/ppc/ppc4xx.h"
25 #include "hw/pci/pci.h"
26 #include "hw/pci/pci_host.h"
27 #include "exec/address-spaces.h"
31 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
33 #define DPRINTF(fmt, ...)
48 #define PPC4xx_PCI_HOST_BRIDGE(obj) \
49 OBJECT_CHECK(PPC4xxPCIState, (obj), TYPE_PPC4xx_PCI_HOST_BRIDGE)
51 #define PPC4xx_PCI_NR_PMMS 3
52 #define PPC4xx_PCI_NR_PTMS 2
54 struct PPC4xxPCIState
{
55 PCIHostState parent_obj
;
57 struct PCIMasterMap pmm
[PPC4xx_PCI_NR_PMMS
];
58 struct PCITargetMap ptm
[PPC4xx_PCI_NR_PTMS
];
61 MemoryRegion container
;
64 typedef struct PPC4xxPCIState PPC4xxPCIState
;
66 #define PCIC0_CFGADDR 0x0
67 #define PCIC0_CFGDATA 0x4
69 /* PLB Memory Map (PMM) registers specify which PLB addresses are translated to
71 #define PCIL0_PMM0LA 0x0
72 #define PCIL0_PMM0MA 0x4
73 #define PCIL0_PMM0PCILA 0x8
74 #define PCIL0_PMM0PCIHA 0xc
75 #define PCIL0_PMM1LA 0x10
76 #define PCIL0_PMM1MA 0x14
77 #define PCIL0_PMM1PCILA 0x18
78 #define PCIL0_PMM1PCIHA 0x1c
79 #define PCIL0_PMM2LA 0x20
80 #define PCIL0_PMM2MA 0x24
81 #define PCIL0_PMM2PCILA 0x28
82 #define PCIL0_PMM2PCIHA 0x2c
84 /* PCI Target Map (PTM) registers specify which PCI addresses are translated to
86 #define PCIL0_PTM1MS 0x30
87 #define PCIL0_PTM1LA 0x34
88 #define PCIL0_PTM2MS 0x38
89 #define PCIL0_PTM2LA 0x3c
90 #define PCI_REG_BASE 0x800000
91 #define PCI_REG_SIZE 0x40
93 #define PCI_ALL_SIZE (PCI_REG_BASE + PCI_REG_SIZE)
95 static uint64_t pci4xx_cfgaddr_read(void *opaque
, hwaddr addr
,
98 PPC4xxPCIState
*ppc4xx_pci
= opaque
;
99 PCIHostState
*phb
= PCI_HOST_BRIDGE(ppc4xx_pci
);
101 return phb
->config_reg
;
104 static void pci4xx_cfgaddr_write(void *opaque
, hwaddr addr
,
105 uint64_t value
, unsigned size
)
107 PPC4xxPCIState
*ppc4xx_pci
= opaque
;
108 PCIHostState
*phb
= PCI_HOST_BRIDGE(ppc4xx_pci
);
110 phb
->config_reg
= value
& ~0x3;
113 static const MemoryRegionOps pci4xx_cfgaddr_ops
= {
114 .read
= pci4xx_cfgaddr_read
,
115 .write
= pci4xx_cfgaddr_write
,
116 .endianness
= DEVICE_LITTLE_ENDIAN
,
119 static void ppc4xx_pci_reg_write4(void *opaque
, hwaddr offset
,
120 uint64_t value
, unsigned size
)
122 struct PPC4xxPCIState
*pci
= opaque
;
124 /* We ignore all target attempts at PCI configuration, effectively
125 * assuming a bidirectional 1:1 mapping of PLB and PCI space. */
129 pci
->pmm
[0].la
= value
;
132 pci
->pmm
[0].ma
= value
;
134 case PCIL0_PMM0PCIHA
:
135 pci
->pmm
[0].pciha
= value
;
137 case PCIL0_PMM0PCILA
:
138 pci
->pmm
[0].pcila
= value
;
142 pci
->pmm
[1].la
= value
;
145 pci
->pmm
[1].ma
= value
;
147 case PCIL0_PMM1PCIHA
:
148 pci
->pmm
[1].pciha
= value
;
150 case PCIL0_PMM1PCILA
:
151 pci
->pmm
[1].pcila
= value
;
155 pci
->pmm
[2].la
= value
;
158 pci
->pmm
[2].ma
= value
;
160 case PCIL0_PMM2PCIHA
:
161 pci
->pmm
[2].pciha
= value
;
163 case PCIL0_PMM2PCILA
:
164 pci
->pmm
[2].pcila
= value
;
168 pci
->ptm
[0].ms
= value
;
171 pci
->ptm
[0].la
= value
;
174 pci
->ptm
[1].ms
= value
;
177 pci
->ptm
[1].la
= value
;
181 printf("%s: unhandled PCI internal register 0x%lx\n", __func__
,
182 (unsigned long)offset
);
187 static uint64_t ppc4xx_pci_reg_read4(void *opaque
, hwaddr offset
,
190 struct PPC4xxPCIState
*pci
= opaque
;
195 value
= pci
->pmm
[0].la
;
198 value
= pci
->pmm
[0].ma
;
200 case PCIL0_PMM0PCIHA
:
201 value
= pci
->pmm
[0].pciha
;
203 case PCIL0_PMM0PCILA
:
204 value
= pci
->pmm
[0].pcila
;
208 value
= pci
->pmm
[1].la
;
211 value
= pci
->pmm
[1].ma
;
213 case PCIL0_PMM1PCIHA
:
214 value
= pci
->pmm
[1].pciha
;
216 case PCIL0_PMM1PCILA
:
217 value
= pci
->pmm
[1].pcila
;
221 value
= pci
->pmm
[2].la
;
224 value
= pci
->pmm
[2].ma
;
226 case PCIL0_PMM2PCIHA
:
227 value
= pci
->pmm
[2].pciha
;
229 case PCIL0_PMM2PCILA
:
230 value
= pci
->pmm
[2].pcila
;
234 value
= pci
->ptm
[0].ms
;
237 value
= pci
->ptm
[0].la
;
240 value
= pci
->ptm
[1].ms
;
243 value
= pci
->ptm
[1].la
;
247 printf("%s: invalid PCI internal register 0x%lx\n", __func__
,
248 (unsigned long)offset
);
255 static const MemoryRegionOps pci_reg_ops
= {
256 .read
= ppc4xx_pci_reg_read4
,
257 .write
= ppc4xx_pci_reg_write4
,
258 .endianness
= DEVICE_LITTLE_ENDIAN
,
261 static void ppc4xx_pci_reset(void *opaque
)
263 struct PPC4xxPCIState
*pci
= opaque
;
265 memset(pci
->pmm
, 0, sizeof(pci
->pmm
));
266 memset(pci
->ptm
, 0, sizeof(pci
->ptm
));
269 /* On Bamboo, all pins from each slot are tied to a single board IRQ. This
270 * may need further refactoring for other boards. */
271 static int ppc4xx_pci_map_irq(PCIDevice
*pci_dev
, int irq_num
)
273 int slot
= pci_dev
->devfn
>> 3;
275 DPRINTF("%s: devfn %x irq %d -> %d\n", __func__
,
276 pci_dev
->devfn
, irq_num
, slot
);
281 static void ppc4xx_pci_set_irq(void *opaque
, int irq_num
, int level
)
283 qemu_irq
*pci_irqs
= opaque
;
285 DPRINTF("%s: PCI irq %d\n", __func__
, irq_num
);
287 fprintf(stderr
, "%s: PCI irq %d\n", __func__
, irq_num
);
290 qemu_set_irq(pci_irqs
[irq_num
], level
);
293 static const VMStateDescription vmstate_pci_master_map
= {
294 .name
= "pci_master_map",
296 .minimum_version_id
= 0,
297 .fields
= (VMStateField
[]) {
298 VMSTATE_UINT32(la
, struct PCIMasterMap
),
299 VMSTATE_UINT32(ma
, struct PCIMasterMap
),
300 VMSTATE_UINT32(pcila
, struct PCIMasterMap
),
301 VMSTATE_UINT32(pciha
, struct PCIMasterMap
),
302 VMSTATE_END_OF_LIST()
306 static const VMStateDescription vmstate_pci_target_map
= {
307 .name
= "pci_target_map",
309 .minimum_version_id
= 0,
310 .fields
= (VMStateField
[]) {
311 VMSTATE_UINT32(ms
, struct PCITargetMap
),
312 VMSTATE_UINT32(la
, struct PCITargetMap
),
313 VMSTATE_END_OF_LIST()
317 static const VMStateDescription vmstate_ppc4xx_pci
= {
318 .name
= "ppc4xx_pci",
320 .minimum_version_id
= 1,
321 .fields
= (VMStateField
[]) {
322 VMSTATE_STRUCT_ARRAY(pmm
, PPC4xxPCIState
, PPC4xx_PCI_NR_PMMS
, 1,
323 vmstate_pci_master_map
,
324 struct PCIMasterMap
),
325 VMSTATE_STRUCT_ARRAY(ptm
, PPC4xxPCIState
, PPC4xx_PCI_NR_PTMS
, 1,
326 vmstate_pci_target_map
,
327 struct PCITargetMap
),
328 VMSTATE_END_OF_LIST()
332 /* XXX Interrupt acknowledge cycles not supported. */
333 static int ppc4xx_pcihost_initfn(SysBusDevice
*dev
)
340 h
= PCI_HOST_BRIDGE(dev
);
341 s
= PPC4xx_PCI_HOST_BRIDGE(dev
);
343 for (i
= 0; i
< ARRAY_SIZE(s
->irq
); i
++) {
344 sysbus_init_irq(dev
, &s
->irq
[i
]);
347 b
= pci_register_bus(DEVICE(dev
), NULL
, ppc4xx_pci_set_irq
,
348 ppc4xx_pci_map_irq
, s
->irq
, get_system_memory(),
349 get_system_io(), 0, 4, TYPE_PCI_BUS
);
352 pci_create_simple(b
, 0, "ppc4xx-host-bridge");
354 /* XXX split into 2 memory regions, one for config space, one for regs */
355 memory_region_init(&s
->container
, OBJECT(s
), "pci-container", PCI_ALL_SIZE
);
356 memory_region_init_io(&h
->conf_mem
, OBJECT(s
), &pci_host_conf_le_ops
, h
,
358 memory_region_init_io(&h
->data_mem
, OBJECT(s
), &pci_host_data_le_ops
, h
,
360 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pci_reg_ops
, s
,
361 "pci.reg", PCI_REG_SIZE
);
362 memory_region_add_subregion(&s
->container
, PCIC0_CFGADDR
, &h
->conf_mem
);
363 memory_region_add_subregion(&s
->container
, PCIC0_CFGDATA
, &h
->data_mem
);
364 memory_region_add_subregion(&s
->container
, PCI_REG_BASE
, &s
->iomem
);
365 sysbus_init_mmio(dev
, &s
->container
);
366 qemu_register_reset(ppc4xx_pci_reset
, s
);
371 static void ppc4xx_host_bridge_class_init(ObjectClass
*klass
, void *data
)
373 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
374 DeviceClass
*dc
= DEVICE_CLASS(klass
);
376 dc
->desc
= "Host bridge";
377 k
->vendor_id
= PCI_VENDOR_ID_IBM
;
378 k
->device_id
= PCI_DEVICE_ID_IBM_440GX
;
379 k
->class_id
= PCI_CLASS_BRIDGE_OTHER
;
381 * PCI-facing part of the host bridge, not usable without the
382 * host-facing part, which can't be device_add'ed, yet.
384 dc
->cannot_instantiate_with_device_add_yet
= true;
387 static const TypeInfo ppc4xx_host_bridge_info
= {
388 .name
= "ppc4xx-host-bridge",
389 .parent
= TYPE_PCI_DEVICE
,
390 .instance_size
= sizeof(PCIDevice
),
391 .class_init
= ppc4xx_host_bridge_class_init
,
394 static void ppc4xx_pcihost_class_init(ObjectClass
*klass
, void *data
)
396 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
397 DeviceClass
*dc
= DEVICE_CLASS(klass
);
399 k
->init
= ppc4xx_pcihost_initfn
;
400 dc
->vmsd
= &vmstate_ppc4xx_pci
;
403 static const TypeInfo ppc4xx_pcihost_info
= {
404 .name
= TYPE_PPC4xx_PCI_HOST_BRIDGE
,
405 .parent
= TYPE_PCI_HOST_BRIDGE
,
406 .instance_size
= sizeof(PPC4xxPCIState
),
407 .class_init
= ppc4xx_pcihost_class_init
,
410 static void ppc4xx_pci_register_types(void)
412 type_register_static(&ppc4xx_pcihost_info
);
413 type_register_static(&ppc4xx_host_bridge_info
);
416 type_init(ppc4xx_pci_register_types
)