2 * QEMU Ultrasparc APB PCI host
4 * Copyright (c) 2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* XXX This file and most of its contents are somewhat misnamed. The
26 Ultrasparc PCI host is called the PCI Bus Module (PBM). The APB is
27 the secondary PCI bridge. */
36 #define APB_DPRINTF(fmt, ...) \
37 do { printf("APB: " fmt , ## __VA_ARGS__); } while (0)
39 #define APB_DPRINTF(fmt, ...)
42 typedef target_phys_addr_t pci_addr_t
;
45 typedef PCIHostState APBState
;
47 static void pci_apb_config_writel (void *opaque
, target_phys_addr_t addr
,
52 #ifdef TARGET_WORDS_BIGENDIAN
55 APB_DPRINTF("config_writel addr " TARGET_FMT_plx
" val %x\n", addr
,
60 static uint32_t pci_apb_config_readl (void *opaque
,
61 target_phys_addr_t addr
)
67 #ifdef TARGET_WORDS_BIGENDIAN
70 APB_DPRINTF("config_readl addr " TARGET_FMT_plx
" val %x\n", addr
,
75 static CPUWriteMemoryFunc
*pci_apb_config_write
[] = {
76 &pci_apb_config_writel
,
77 &pci_apb_config_writel
,
78 &pci_apb_config_writel
,
81 static CPUReadMemoryFunc
*pci_apb_config_read
[] = {
82 &pci_apb_config_readl
,
83 &pci_apb_config_readl
,
84 &pci_apb_config_readl
,
87 static void apb_config_writel (void *opaque
, target_phys_addr_t addr
,
92 switch (addr
& 0x3f) {
93 case 0x00: // Control/Status
96 case 0x20: // Diagnostic
97 case 0x28: // Target address space
104 static uint32_t apb_config_readl (void *opaque
,
105 target_phys_addr_t addr
)
107 //PCIBus *s = opaque;
110 switch (addr
& 0x3f) {
111 case 0x00: // Control/Status
114 case 0x20: // Diagnostic
115 case 0x28: // Target address space
124 static CPUWriteMemoryFunc
*apb_config_write
[] = {
130 static CPUReadMemoryFunc
*apb_config_read
[] = {
136 static CPUWriteMemoryFunc
*pci_apb_write
[] = {
137 &pci_host_data_writeb
,
138 &pci_host_data_writew
,
139 &pci_host_data_writel
,
142 static CPUReadMemoryFunc
*pci_apb_read
[] = {
143 &pci_host_data_readb
,
144 &pci_host_data_readw
,
145 &pci_host_data_readl
,
148 static void pci_apb_iowriteb (void *opaque
, target_phys_addr_t addr
,
151 cpu_outb(NULL
, addr
& IOPORTS_MASK
, val
);
154 static void pci_apb_iowritew (void *opaque
, target_phys_addr_t addr
,
157 cpu_outw(NULL
, addr
& IOPORTS_MASK
, val
);
160 static void pci_apb_iowritel (void *opaque
, target_phys_addr_t addr
,
163 cpu_outl(NULL
, addr
& IOPORTS_MASK
, val
);
166 static uint32_t pci_apb_ioreadb (void *opaque
, target_phys_addr_t addr
)
170 val
= cpu_inb(NULL
, addr
& IOPORTS_MASK
);
174 static uint32_t pci_apb_ioreadw (void *opaque
, target_phys_addr_t addr
)
178 val
= cpu_inw(NULL
, addr
& IOPORTS_MASK
);
182 static uint32_t pci_apb_ioreadl (void *opaque
, target_phys_addr_t addr
)
186 val
= cpu_inl(NULL
, addr
& IOPORTS_MASK
);
190 static CPUWriteMemoryFunc
*pci_apb_iowrite
[] = {
196 static CPUReadMemoryFunc
*pci_apb_ioread
[] = {
202 /* The APB host has an IRQ line for each IRQ line of each slot. */
203 static int pci_apb_map_irq(PCIDevice
*pci_dev
, int irq_num
)
205 return ((pci_dev
->devfn
& 0x18) >> 1) + irq_num
;
208 static int pci_pbm_map_irq(PCIDevice
*pci_dev
, int irq_num
)
211 if (pci_dev
->devfn
& 1)
215 return bus_offset
+ irq_num
;
218 static void pci_apb_set_irq(qemu_irq
*pic
, int irq_num
, int level
)
220 /* PCI IRQ map onto the first 32 INO. */
221 qemu_set_irq(pic
[irq_num
], level
);
224 PCIBus
*pci_apb_init(target_phys_addr_t special_base
,
225 target_phys_addr_t mem_base
,
226 qemu_irq
*pic
, PCIBus
**bus2
, PCIBus
**bus3
)
230 int pci_mem_config
, pci_mem_data
, apb_config
, pci_ioport
;
232 s
= qemu_mallocz(sizeof(APBState
));
233 /* Ultrasparc PBM main bus */
234 s
->bus
= pci_register_bus(NULL
, "pci",
235 pci_apb_set_irq
, pci_pbm_map_irq
, pic
, 0, 32);
237 pci_mem_config
= cpu_register_io_memory(pci_apb_config_read
,
238 pci_apb_config_write
, s
);
239 apb_config
= cpu_register_io_memory(apb_config_read
,
240 apb_config_write
, s
);
241 pci_mem_data
= cpu_register_io_memory(pci_apb_read
,
243 pci_ioport
= cpu_register_io_memory(pci_apb_ioread
,
246 cpu_register_physical_memory(special_base
+ 0x2000ULL
, 0x40, apb_config
);
247 cpu_register_physical_memory(special_base
+ 0x1000000ULL
, 0x10,
249 cpu_register_physical_memory(special_base
+ 0x2000000ULL
, 0x10000,
251 cpu_register_physical_memory(mem_base
, 0x10000000,
252 pci_mem_data
); // XXX size should be 4G-prom
254 d
= pci_register_device(s
->bus
, "Advanced PCI Bus", sizeof(PCIDevice
),
256 pci_config_set_vendor_id(d
->config
, PCI_VENDOR_ID_SUN
);
257 pci_config_set_device_id(d
->config
, PCI_DEVICE_ID_SUN_SABRE
);
258 d
->config
[0x04] = 0x06; // command = bus master, pci mem
259 d
->config
[0x05] = 0x00;
260 d
->config
[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
261 d
->config
[0x07] = 0x03; // status = medium devsel
262 d
->config
[0x08] = 0x00; // revision
263 d
->config
[0x09] = 0x00; // programming i/f
264 pci_config_set_class(d
->config
, PCI_CLASS_BRIDGE_HOST
);
265 d
->config
[0x0D] = 0x10; // latency_timer
266 d
->config
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
268 /* APB secondary busses */
269 *bus2
= pci_bridge_init(s
->bus
, 8, PCI_VENDOR_ID_SUN
,
270 PCI_DEVICE_ID_SUN_SIMBA
, pci_apb_map_irq
,
271 "Advanced PCI Bus secondary bridge 1");
272 *bus3
= pci_bridge_init(s
->bus
, 9, PCI_VENDOR_ID_SUN
,
273 PCI_DEVICE_ID_SUN_SIMBA
, pci_apb_map_irq
,
274 "Advanced PCI Bus secondary bridge 2");