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. */
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 #ifdef TARGET_WORDS_BIGENDIAN
106 value
= bswap32(value
);
109 ppc4xx_pci
->pci_state
.config_reg
= value
& ~0x3;
112 static CPUWriteMemoryFunc
* const pci4xx_cfgaddr_write
[] = {
113 &pci4xx_cfgaddr_writel
,
114 &pci4xx_cfgaddr_writel
,
115 &pci4xx_cfgaddr_writel
,
118 static void ppc4xx_pci_reg_write4(void *opaque
, target_phys_addr_t offset
,
121 struct PPC4xxPCIState
*pci
= opaque
;
123 #ifdef TARGET_WORDS_BIGENDIAN
124 value
= bswap32(value
);
127 /* We ignore all target attempts at PCI configuration, effectively
128 * assuming a bidirectional 1:1 mapping of PLB and PCI space. */
132 pci
->pmm
[0].la
= value
;
135 pci
->pmm
[0].ma
= value
;
137 case PCIL0_PMM0PCIHA
:
138 pci
->pmm
[0].pciha
= value
;
140 case PCIL0_PMM0PCILA
:
141 pci
->pmm
[0].pcila
= value
;
145 pci
->pmm
[1].la
= value
;
148 pci
->pmm
[1].ma
= value
;
150 case PCIL0_PMM1PCIHA
:
151 pci
->pmm
[1].pciha
= value
;
153 case PCIL0_PMM1PCILA
:
154 pci
->pmm
[1].pcila
= value
;
158 pci
->pmm
[2].la
= value
;
161 pci
->pmm
[2].ma
= value
;
163 case PCIL0_PMM2PCIHA
:
164 pci
->pmm
[2].pciha
= value
;
166 case PCIL0_PMM2PCILA
:
167 pci
->pmm
[2].pcila
= value
;
171 pci
->ptm
[0].ms
= value
;
174 pci
->ptm
[0].la
= value
;
177 pci
->ptm
[1].ms
= value
;
180 pci
->ptm
[1].la
= value
;
184 printf("%s: unhandled PCI internal register 0x%lx\n", __func__
,
185 (unsigned long)offset
);
190 static uint32_t ppc4xx_pci_reg_read4(void *opaque
, target_phys_addr_t offset
)
192 struct PPC4xxPCIState
*pci
= opaque
;
197 value
= pci
->pmm
[0].la
;
200 value
= pci
->pmm
[0].ma
;
202 case PCIL0_PMM0PCIHA
:
203 value
= pci
->pmm
[0].pciha
;
205 case PCIL0_PMM0PCILA
:
206 value
= pci
->pmm
[0].pcila
;
210 value
= pci
->pmm
[1].la
;
213 value
= pci
->pmm
[1].ma
;
215 case PCIL0_PMM1PCIHA
:
216 value
= pci
->pmm
[1].pciha
;
218 case PCIL0_PMM1PCILA
:
219 value
= pci
->pmm
[1].pcila
;
223 value
= pci
->pmm
[2].la
;
226 value
= pci
->pmm
[2].ma
;
228 case PCIL0_PMM2PCIHA
:
229 value
= pci
->pmm
[2].pciha
;
231 case PCIL0_PMM2PCILA
:
232 value
= pci
->pmm
[2].pcila
;
236 value
= pci
->ptm
[0].ms
;
239 value
= pci
->ptm
[0].la
;
242 value
= pci
->ptm
[1].ms
;
245 value
= pci
->ptm
[1].la
;
249 printf("%s: invalid PCI internal register 0x%lx\n", __func__
,
250 (unsigned long)offset
);
254 #ifdef TARGET_WORDS_BIGENDIAN
255 value
= bswap32(value
);
261 static CPUReadMemoryFunc
* const pci_reg_read
[] = {
262 &ppc4xx_pci_reg_read4
,
263 &ppc4xx_pci_reg_read4
,
264 &ppc4xx_pci_reg_read4
,
267 static CPUWriteMemoryFunc
* const pci_reg_write
[] = {
268 &ppc4xx_pci_reg_write4
,
269 &ppc4xx_pci_reg_write4
,
270 &ppc4xx_pci_reg_write4
,
273 static void ppc4xx_pci_reset(void *opaque
)
275 struct PPC4xxPCIState
*pci
= opaque
;
277 memset(pci
->pmm
, 0, sizeof(pci
->pmm
));
278 memset(pci
->ptm
, 0, sizeof(pci
->ptm
));
281 /* On Bamboo, all pins from each slot are tied to a single board IRQ. This
282 * may need further refactoring for other boards. */
283 static int ppc4xx_pci_map_irq(PCIDevice
*pci_dev
, int irq_num
)
285 int slot
= pci_dev
->devfn
>> 3;
287 DPRINTF("%s: devfn %x irq %d -> %d\n", __func__
,
288 pci_dev
->devfn
, irq_num
, slot
);
293 static void ppc4xx_pci_set_irq(void *opaque
, int irq_num
, int level
)
295 qemu_irq
*pci_irqs
= opaque
;
297 DPRINTF("%s: PCI irq %d\n", __func__
, irq_num
);
298 qemu_set_irq(pci_irqs
[irq_num
], level
);
301 static void ppc4xx_pci_save(QEMUFile
*f
, void *opaque
)
303 PPC4xxPCIState
*controller
= opaque
;
306 pci_device_save(controller
->pci_dev
, f
);
308 for (i
= 0; i
< PPC4xx_PCI_NR_PMMS
; i
++) {
309 qemu_put_be32s(f
, &controller
->pmm
[i
].la
);
310 qemu_put_be32s(f
, &controller
->pmm
[i
].ma
);
311 qemu_put_be32s(f
, &controller
->pmm
[i
].pcila
);
312 qemu_put_be32s(f
, &controller
->pmm
[i
].pciha
);
315 for (i
= 0; i
< PPC4xx_PCI_NR_PTMS
; i
++) {
316 qemu_put_be32s(f
, &controller
->ptm
[i
].ms
);
317 qemu_put_be32s(f
, &controller
->ptm
[i
].la
);
321 static int ppc4xx_pci_load(QEMUFile
*f
, void *opaque
, int version_id
)
323 PPC4xxPCIState
*controller
= opaque
;
329 pci_device_load(controller
->pci_dev
, f
);
331 for (i
= 0; i
< PPC4xx_PCI_NR_PMMS
; i
++) {
332 qemu_get_be32s(f
, &controller
->pmm
[i
].la
);
333 qemu_get_be32s(f
, &controller
->pmm
[i
].ma
);
334 qemu_get_be32s(f
, &controller
->pmm
[i
].pcila
);
335 qemu_get_be32s(f
, &controller
->pmm
[i
].pciha
);
338 for (i
= 0; i
< PPC4xx_PCI_NR_PTMS
; i
++) {
339 qemu_get_be32s(f
, &controller
->ptm
[i
].ms
);
340 qemu_get_be32s(f
, &controller
->ptm
[i
].la
);
346 /* XXX Interrupt acknowledge cycles not supported. */
347 PCIBus
*ppc4xx_pci_init(CPUState
*env
, qemu_irq pci_irqs
[4],
348 target_phys_addr_t config_space
,
349 target_phys_addr_t int_ack
,
350 target_phys_addr_t special_cycle
,
351 target_phys_addr_t registers
)
353 PPC4xxPCIState
*controller
;
355 static int ppc4xx_pci_id
;
358 controller
= qemu_mallocz(sizeof(PPC4xxPCIState
));
360 controller
->pci_state
.bus
= pci_register_bus(NULL
, "pci",
365 controller
->pci_dev
= pci_register_device(controller
->pci_state
.bus
,
366 "host bridge", sizeof(PCIDevice
),
368 pci_conf
= controller
->pci_dev
->config
;
369 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_IBM
);
370 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_IBM_440GX
);
371 pci_config_set_class(pci_conf
, PCI_CLASS_BRIDGE_OTHER
);
374 index
= cpu_register_io_memory(pci4xx_cfgaddr_read
,
375 pci4xx_cfgaddr_write
, controller
);
378 cpu_register_physical_memory(config_space
+ PCIC0_CFGADDR
, 4, index
);
381 index
= pci_host_data_register_mmio(&controller
->pci_state
, 1);
384 cpu_register_physical_memory(config_space
+ PCIC0_CFGDATA
, 4, index
);
386 /* Internal registers */
387 index
= cpu_register_io_memory(pci_reg_read
, pci_reg_write
, controller
);
390 cpu_register_physical_memory(registers
, PCI_REG_SIZE
, index
);
392 qemu_register_reset(ppc4xx_pci_reset
, controller
);
394 /* XXX load/save code not tested. */
395 register_savevm(&controller
->pci_dev
->qdev
, "ppc4xx_pci", ppc4xx_pci_id
++,
396 1, ppc4xx_pci_save
, ppc4xx_pci_load
, controller
);
398 return controller
->pci_state
.bus
;
401 printf("%s error\n", __func__
);
402 qemu_free(controller
);