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 struct PCIMasterMap pmm
[PPC4xx_PCI_NR_PMMS
];
53 struct PCITargetMap ptm
[PPC4xx_PCI_NR_PTMS
];
55 PCIHostState pci_state
;
57 MemoryRegion iomem_addr
;
58 MemoryRegion iomem_regs
;
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 uint64_t pci4xx_cfgaddr_read(void *opaque
, target_phys_addr_t addr
,
92 PPC4xxPCIState
*ppc4xx_pci
= opaque
;
94 return ppc4xx_pci
->pci_state
.config_reg
;
97 static void pci4xx_cfgaddr_write(void *opaque
, target_phys_addr_t addr
,
98 uint64_t value
, unsigned size
)
100 PPC4xxPCIState
*ppc4xx_pci
= opaque
;
102 ppc4xx_pci
->pci_state
.config_reg
= value
& ~0x3;
105 static const MemoryRegionOps pci4xx_cfgaddr_ops
= {
106 .read
= pci4xx_cfgaddr_read
,
107 .write
= pci4xx_cfgaddr_write
,
108 .endianness
= DEVICE_LITTLE_ENDIAN
,
111 static void ppc4xx_pci_reg_write4(void *opaque
, target_phys_addr_t offset
,
112 uint64_t value
, unsigned size
)
114 struct PPC4xxPCIState
*pci
= opaque
;
116 /* We ignore all target attempts at PCI configuration, effectively
117 * assuming a bidirectional 1:1 mapping of PLB and PCI space. */
121 pci
->pmm
[0].la
= value
;
124 pci
->pmm
[0].ma
= value
;
126 case PCIL0_PMM0PCIHA
:
127 pci
->pmm
[0].pciha
= value
;
129 case PCIL0_PMM0PCILA
:
130 pci
->pmm
[0].pcila
= value
;
134 pci
->pmm
[1].la
= value
;
137 pci
->pmm
[1].ma
= value
;
139 case PCIL0_PMM1PCIHA
:
140 pci
->pmm
[1].pciha
= value
;
142 case PCIL0_PMM1PCILA
:
143 pci
->pmm
[1].pcila
= value
;
147 pci
->pmm
[2].la
= value
;
150 pci
->pmm
[2].ma
= value
;
152 case PCIL0_PMM2PCIHA
:
153 pci
->pmm
[2].pciha
= value
;
155 case PCIL0_PMM2PCILA
:
156 pci
->pmm
[2].pcila
= value
;
160 pci
->ptm
[0].ms
= value
;
163 pci
->ptm
[0].la
= value
;
166 pci
->ptm
[1].ms
= value
;
169 pci
->ptm
[1].la
= value
;
173 printf("%s: unhandled PCI internal register 0x%lx\n", __func__
,
174 (unsigned long)offset
);
179 static uint64_t ppc4xx_pci_reg_read4(void *opaque
, target_phys_addr_t offset
,
182 struct PPC4xxPCIState
*pci
= opaque
;
187 value
= pci
->pmm
[0].la
;
190 value
= pci
->pmm
[0].ma
;
192 case PCIL0_PMM0PCIHA
:
193 value
= pci
->pmm
[0].pciha
;
195 case PCIL0_PMM0PCILA
:
196 value
= pci
->pmm
[0].pcila
;
200 value
= pci
->pmm
[1].la
;
203 value
= pci
->pmm
[1].ma
;
205 case PCIL0_PMM1PCIHA
:
206 value
= pci
->pmm
[1].pciha
;
208 case PCIL0_PMM1PCILA
:
209 value
= pci
->pmm
[1].pcila
;
213 value
= pci
->pmm
[2].la
;
216 value
= pci
->pmm
[2].ma
;
218 case PCIL0_PMM2PCIHA
:
219 value
= pci
->pmm
[2].pciha
;
221 case PCIL0_PMM2PCILA
:
222 value
= pci
->pmm
[2].pcila
;
226 value
= pci
->ptm
[0].ms
;
229 value
= pci
->ptm
[0].la
;
232 value
= pci
->ptm
[1].ms
;
235 value
= pci
->ptm
[1].la
;
239 printf("%s: invalid PCI internal register 0x%lx\n", __func__
,
240 (unsigned long)offset
);
247 static const MemoryRegionOps pci_reg_ops
= {
248 .read
= ppc4xx_pci_reg_read4
,
249 .write
= ppc4xx_pci_reg_write4
,
250 .endianness
= DEVICE_LITTLE_ENDIAN
,
253 static void ppc4xx_pci_reset(void *opaque
)
255 struct PPC4xxPCIState
*pci
= opaque
;
257 memset(pci
->pmm
, 0, sizeof(pci
->pmm
));
258 memset(pci
->ptm
, 0, sizeof(pci
->ptm
));
261 /* On Bamboo, all pins from each slot are tied to a single board IRQ. This
262 * may need further refactoring for other boards. */
263 static int ppc4xx_pci_map_irq(PCIDevice
*pci_dev
, int irq_num
)
265 int slot
= pci_dev
->devfn
>> 3;
267 DPRINTF("%s: devfn %x irq %d -> %d\n", __func__
,
268 pci_dev
->devfn
, irq_num
, slot
);
273 static void ppc4xx_pci_set_irq(void *opaque
, int irq_num
, int level
)
275 qemu_irq
*pci_irqs
= opaque
;
277 DPRINTF("%s: PCI irq %d\n", __func__
, irq_num
);
279 fprintf(stderr
, "%s: PCI irq %d\n", __func__
, irq_num
);
282 qemu_set_irq(pci_irqs
[irq_num
], level
);
285 static const VMStateDescription vmstate_pci_master_map
= {
286 .name
= "pci_master_map",
288 .minimum_version_id
= 0,
289 .minimum_version_id_old
= 0,
290 .fields
= (VMStateField
[]) {
291 VMSTATE_UINT32(la
, struct PCIMasterMap
),
292 VMSTATE_UINT32(ma
, struct PCIMasterMap
),
293 VMSTATE_UINT32(pcila
, struct PCIMasterMap
),
294 VMSTATE_UINT32(pciha
, struct PCIMasterMap
),
295 VMSTATE_END_OF_LIST()
299 static const VMStateDescription vmstate_pci_target_map
= {
300 .name
= "pci_target_map",
302 .minimum_version_id
= 0,
303 .minimum_version_id_old
= 0,
304 .fields
= (VMStateField
[]) {
305 VMSTATE_UINT32(ms
, struct PCITargetMap
),
306 VMSTATE_UINT32(la
, struct PCITargetMap
),
307 VMSTATE_END_OF_LIST()
311 static const VMStateDescription vmstate_ppc4xx_pci
= {
312 .name
= "ppc4xx_pci",
314 .minimum_version_id
= 1,
315 .minimum_version_id_old
= 1,
316 .fields
= (VMStateField
[]) {
317 VMSTATE_PCI_DEVICE_POINTER(pci_dev
, PPC4xxPCIState
),
318 VMSTATE_STRUCT_ARRAY(pmm
, PPC4xxPCIState
, PPC4xx_PCI_NR_PMMS
, 1,
319 vmstate_pci_master_map
,
320 struct PCIMasterMap
),
321 VMSTATE_STRUCT_ARRAY(ptm
, PPC4xxPCIState
, PPC4xx_PCI_NR_PTMS
, 1,
322 vmstate_pci_target_map
,
323 struct PCITargetMap
),
324 VMSTATE_END_OF_LIST()
328 /* XXX Interrupt acknowledge cycles not supported. */
329 PCIBus
*ppc4xx_pci_init(CPUState
*env
, qemu_irq pci_irqs
[4],
330 target_phys_addr_t config_space
,
331 target_phys_addr_t int_ack
,
332 target_phys_addr_t special_cycle
,
333 target_phys_addr_t registers
)
335 PPC4xxPCIState
*controller
;
336 static int ppc4xx_pci_id
;
339 controller
= g_malloc0(sizeof(PPC4xxPCIState
));
341 controller
->pci_state
.bus
= pci_register_bus(NULL
, "pci",
349 controller
->pci_dev
= pci_register_device(controller
->pci_state
.bus
,
350 "host bridge", sizeof(PCIDevice
),
352 pci_conf
= controller
->pci_dev
->config
;
353 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_IBM
);
354 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_IBM_440GX
);
355 pci_config_set_class(pci_conf
, PCI_CLASS_BRIDGE_OTHER
);
358 memory_region_init_io(&controller
->iomem_addr
, &pci4xx_cfgaddr_ops
,
359 controller
, "pci.cfgaddr", 4);
360 memory_region_add_subregion(get_system_memory(),
361 config_space
+ PCIC0_CFGADDR
,
362 &controller
->iomem_addr
);
365 memory_region_init_io(&controller
->pci_state
.data_mem
,
366 &pci_host_data_be_ops
,
367 &controller
->pci_state
, "pci-conf-data", 4);
368 memory_region_add_subregion(get_system_memory(),
369 config_space
+ PCIC0_CFGDATA
,
370 &controller
->pci_state
.data_mem
);
372 /* Internal registers */
373 memory_region_init_io(&controller
->iomem_regs
, &pci_reg_ops
, controller
,
374 "pci.regs", PCI_REG_SIZE
);
375 memory_region_add_subregion(get_system_memory(), registers
,
376 &controller
->iomem_regs
);
378 qemu_register_reset(ppc4xx_pci_reset
, controller
);
380 /* XXX load/save code not tested. */
381 vmstate_register(&controller
->pci_dev
->qdev
, ppc4xx_pci_id
++,
382 &vmstate_ppc4xx_pci
, controller
);
384 return controller
->pci_state
.bus
;