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
;
58 typedef struct PPC4xxPCIState PPC4xxPCIState
;
60 #define PCIC0_CFGADDR 0x0
61 #define PCIC0_CFGDATA 0x4
63 /* PLB Memory Map (PMM) registers specify which PLB addresses are translated to
65 #define PCIL0_PMM0LA 0x0
66 #define PCIL0_PMM0MA 0x4
67 #define PCIL0_PMM0PCILA 0x8
68 #define PCIL0_PMM0PCIHA 0xc
69 #define PCIL0_PMM1LA 0x10
70 #define PCIL0_PMM1MA 0x14
71 #define PCIL0_PMM1PCILA 0x18
72 #define PCIL0_PMM1PCIHA 0x1c
73 #define PCIL0_PMM2LA 0x20
74 #define PCIL0_PMM2MA 0x24
75 #define PCIL0_PMM2PCILA 0x28
76 #define PCIL0_PMM2PCIHA 0x2c
78 /* PCI Target Map (PTM) registers specify which PCI addresses are translated to
80 #define PCIL0_PTM1MS 0x30
81 #define PCIL0_PTM1LA 0x34
82 #define PCIL0_PTM2MS 0x38
83 #define PCIL0_PTM2LA 0x3c
84 #define PCI_REG_SIZE 0x40
87 static uint32_t pci4xx_cfgaddr_readl(void *opaque
, target_phys_addr_t addr
)
89 PPC4xxPCIState
*ppc4xx_pci
= opaque
;
91 return ppc4xx_pci
->pci_state
.config_reg
;
94 static CPUReadMemoryFunc
* const pci4xx_cfgaddr_read
[] = {
95 &pci4xx_cfgaddr_readl
,
96 &pci4xx_cfgaddr_readl
,
97 &pci4xx_cfgaddr_readl
,
100 static void pci4xx_cfgaddr_writel(void *opaque
, target_phys_addr_t addr
,
103 PPC4xxPCIState
*ppc4xx_pci
= opaque
;
105 ppc4xx_pci
->pci_state
.config_reg
= value
& ~0x3;
108 static CPUWriteMemoryFunc
* const pci4xx_cfgaddr_write
[] = {
109 &pci4xx_cfgaddr_writel
,
110 &pci4xx_cfgaddr_writel
,
111 &pci4xx_cfgaddr_writel
,
114 static void ppc4xx_pci_reg_write4(void *opaque
, target_phys_addr_t offset
,
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 uint32_t ppc4xx_pci_reg_read4(void *opaque
, target_phys_addr_t offset
)
184 struct PPC4xxPCIState
*pci
= opaque
;
189 value
= pci
->pmm
[0].la
;
192 value
= pci
->pmm
[0].ma
;
194 case PCIL0_PMM0PCIHA
:
195 value
= pci
->pmm
[0].pciha
;
197 case PCIL0_PMM0PCILA
:
198 value
= pci
->pmm
[0].pcila
;
202 value
= pci
->pmm
[1].la
;
205 value
= pci
->pmm
[1].ma
;
207 case PCIL0_PMM1PCIHA
:
208 value
= pci
->pmm
[1].pciha
;
210 case PCIL0_PMM1PCILA
:
211 value
= pci
->pmm
[1].pcila
;
215 value
= pci
->pmm
[2].la
;
218 value
= pci
->pmm
[2].ma
;
220 case PCIL0_PMM2PCIHA
:
221 value
= pci
->pmm
[2].pciha
;
223 case PCIL0_PMM2PCILA
:
224 value
= pci
->pmm
[2].pcila
;
228 value
= pci
->ptm
[0].ms
;
231 value
= pci
->ptm
[0].la
;
234 value
= pci
->ptm
[1].ms
;
237 value
= pci
->ptm
[1].la
;
241 printf("%s: invalid PCI internal register 0x%lx\n", __func__
,
242 (unsigned long)offset
);
249 static CPUReadMemoryFunc
* const pci_reg_read
[] = {
250 &ppc4xx_pci_reg_read4
,
251 &ppc4xx_pci_reg_read4
,
252 &ppc4xx_pci_reg_read4
,
255 static CPUWriteMemoryFunc
* const pci_reg_write
[] = {
256 &ppc4xx_pci_reg_write4
,
257 &ppc4xx_pci_reg_write4
,
258 &ppc4xx_pci_reg_write4
,
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
);
286 qemu_set_irq(pci_irqs
[irq_num
], level
);
289 static const VMStateDescription vmstate_pci_master_map
= {
290 .name
= "pci_master_map",
292 .minimum_version_id
= 0,
293 .minimum_version_id_old
= 0,
294 .fields
= (VMStateField
[]) {
295 VMSTATE_UINT32(la
, struct PCIMasterMap
),
296 VMSTATE_UINT32(ma
, struct PCIMasterMap
),
297 VMSTATE_UINT32(pcila
, struct PCIMasterMap
),
298 VMSTATE_UINT32(pciha
, struct PCIMasterMap
),
299 VMSTATE_END_OF_LIST()
303 static const VMStateDescription vmstate_pci_target_map
= {
304 .name
= "pci_target_map",
306 .minimum_version_id
= 0,
307 .minimum_version_id_old
= 0,
308 .fields
= (VMStateField
[]) {
309 VMSTATE_UINT32(ms
, struct PCITargetMap
),
310 VMSTATE_UINT32(la
, struct PCITargetMap
),
311 VMSTATE_END_OF_LIST()
315 static const VMStateDescription vmstate_ppc4xx_pci
= {
316 .name
= "ppc4xx_pci",
318 .minimum_version_id
= 1,
319 .minimum_version_id_old
= 1,
320 .fields
= (VMStateField
[]) {
321 VMSTATE_PCI_DEVICE_POINTER(pci_dev
, PPC4xxPCIState
),
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 PCIBus
*ppc4xx_pci_init(CPUState
*env
, qemu_irq pci_irqs
[4],
334 target_phys_addr_t config_space
,
335 target_phys_addr_t int_ack
,
336 target_phys_addr_t special_cycle
,
337 target_phys_addr_t registers
)
339 PPC4xxPCIState
*controller
;
341 static int ppc4xx_pci_id
;
344 controller
= qemu_mallocz(sizeof(PPC4xxPCIState
));
346 controller
->pci_state
.bus
= pci_register_bus(NULL
, "pci",
354 controller
->pci_dev
= pci_register_device(controller
->pci_state
.bus
,
355 "host bridge", sizeof(PCIDevice
),
357 pci_conf
= controller
->pci_dev
->config
;
358 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_IBM
);
359 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_IBM_440GX
);
360 pci_config_set_class(pci_conf
, PCI_CLASS_BRIDGE_OTHER
);
363 index
= cpu_register_io_memory(pci4xx_cfgaddr_read
,
364 pci4xx_cfgaddr_write
, controller
,
365 DEVICE_LITTLE_ENDIAN
);
368 cpu_register_physical_memory(config_space
+ PCIC0_CFGADDR
, 4, index
);
371 index
= pci_host_data_register_mmio(&controller
->pci_state
, 1);
374 cpu_register_physical_memory(config_space
+ PCIC0_CFGDATA
, 4, index
);
376 /* Internal registers */
377 index
= cpu_register_io_memory(pci_reg_read
, pci_reg_write
, controller
,
378 DEVICE_LITTLE_ENDIAN
);
381 cpu_register_physical_memory(registers
, PCI_REG_SIZE
, index
);
383 qemu_register_reset(ppc4xx_pci_reset
, controller
);
385 /* XXX load/save code not tested. */
386 vmstate_register(&controller
->pci_dev
->qdev
, ppc4xx_pci_id
++,
387 &vmstate_ppc4xx_pci
, controller
);
389 return controller
->pci_state
.bus
;
392 printf("%s error\n", __func__
);
393 qemu_free(controller
);