Support pci init of both PIIX3 and PIIX4 - patch from bochs bios.
[seabios.git] / src / pciinit.c
1 // Initialize PCI devices (on emulators)
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "util.h" // dprintf
9 #include "pci.h" // PCIDevice
10 #include "biosvar.h" // GET_EBDA
11
12 #define PCI_ADDRESS_SPACE_MEM           0x00
13 #define PCI_ADDRESS_SPACE_IO            0x01
14 #define PCI_ADDRESS_SPACE_MEM_PREFETCH  0x08
15
16 #define PCI_ROM_SLOT 6
17 #define PCI_NUM_REGIONS 7
18
19 static u32 pci_bios_io_addr;
20 static u32 pci_bios_mem_addr;
21 static u32 pci_bios_bigmem_addr;
22 /* host irqs corresponding to PCI irqs A-D */
23 static u8 pci_irqs[4] = { 11, 9, 11, 9 };
24
25 static void pci_set_io_region_addr(PCIDevice d, int region_num, u32 addr)
26 {
27     u16 cmd;
28     u32 ofs, old_addr;
29
30     if ( region_num == PCI_ROM_SLOT ) {
31         ofs = 0x30;
32     }else{
33         ofs = 0x10 + region_num * 4;
34     }
35
36     old_addr = pci_config_readl(d, ofs);
37
38     pci_config_writel(d, ofs, addr);
39     dprintf(1, "region %d: 0x%08x\n", region_num, addr);
40
41     /* enable memory mappings */
42     cmd = pci_config_readw(d, PCI_COMMAND);
43     if ( region_num == PCI_ROM_SLOT )
44         cmd |= 2;
45     else if (old_addr & PCI_ADDRESS_SPACE_IO)
46         cmd |= 1;
47     else
48         cmd |= 2;
49     pci_config_writew(d, PCI_COMMAND, cmd);
50 }
51
52 /* return the global irq number corresponding to a given device irq
53    pin. We could also use the bus number to have a more precise
54    mapping. */
55 static int pci_slot_get_pirq(PCIDevice pci_dev, int irq_num)
56 {
57     int slot_addend;
58     slot_addend = (pci_dev.devfn >> 3) - 1;
59     return (irq_num + slot_addend) & 3;
60 }
61
62 static void pci_bios_init_bridges(PCIDevice d)
63 {
64     u16 vendor_id, device_id;
65
66     vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
67     device_id = pci_config_readw(d, PCI_DEVICE_ID);
68
69     if (vendor_id == PCI_VENDOR_ID_INTEL
70         && (device_id == PCI_DEVICE_ID_INTEL_82371SB_0
71             || device_id == PCI_DEVICE_ID_INTEL_82371AB_0)) {
72         int i, irq;
73         u8 elcr[2];
74
75         /* PIIX3/PIIX4 PCI to ISA bridge */
76
77         elcr[0] = 0x00;
78         elcr[1] = 0x00;
79         for(i = 0; i < 4; i++) {
80             irq = pci_irqs[i];
81             /* set to trigger level */
82             elcr[irq >> 3] |= (1 << (irq & 7));
83             /* activate irq remapping in PIIX */
84             pci_config_writeb(d, 0x60 + i, irq);
85         }
86         outb(elcr[0], 0x4d0);
87         outb(elcr[1], 0x4d1);
88         dprintf(1, "PIIX3/PIIX4 init: elcr=%02x %02x\n",
89                 elcr[0], elcr[1]);
90     }
91 }
92
93 static void pci_bios_init_device(PCIDevice d)
94 {
95     int class;
96     u32 *paddr;
97     int i, pin, pic_irq, vendor_id, device_id;
98
99     class = pci_config_readw(d, PCI_CLASS_DEVICE);
100     vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
101     device_id = pci_config_readw(d, PCI_DEVICE_ID);
102     dprintf(1, "PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n",
103             d.bus, d.devfn, vendor_id, device_id);
104     switch(class) {
105     case 0x0101:
106         if (vendor_id == PCI_VENDOR_ID_INTEL
107             && (device_id == PCI_DEVICE_ID_INTEL_82371SB_1
108                 || device_id == PCI_DEVICE_ID_INTEL_82371AB)) {
109             /* PIIX3/PIIX4 IDE */
110             pci_config_writew(d, 0x40, 0x8000); // enable IDE0
111             pci_config_writew(d, 0x42, 0x8000); // enable IDE1
112             goto default_map;
113         } else {
114             /* IDE: we map it as in ISA mode */
115             pci_set_io_region_addr(d, 0, 0x1f0);
116             pci_set_io_region_addr(d, 1, 0x3f4);
117             pci_set_io_region_addr(d, 2, 0x170);
118             pci_set_io_region_addr(d, 3, 0x374);
119         }
120         break;
121     case 0x0300:
122         if (vendor_id != 0x1234)
123             goto default_map;
124         /* VGA: map frame buffer to default Bochs VBE address */
125         pci_set_io_region_addr(d, 0, 0xE0000000);
126         break;
127     case 0x0800:
128         /* PIC */
129         if (vendor_id == PCI_VENDOR_ID_IBM) {
130             /* IBM */
131             if (device_id == 0x0046 || device_id == 0xFFFF) {
132                 /* MPIC & MPIC2 */
133                 pci_set_io_region_addr(d, 0, 0x80800000 + 0x00040000);
134             }
135         }
136         break;
137     case 0xff00:
138         if (vendor_id == PCI_VENDOR_ID_APPLE &&
139             (device_id == 0x0017 || device_id == 0x0022)) {
140             /* macio bridge */
141             pci_set_io_region_addr(d, 0, 0x80800000);
142         }
143         break;
144     default:
145     default_map:
146         /* default memory mappings */
147         for(i = 0; i < PCI_NUM_REGIONS; i++) {
148             int ofs;
149             u32 val, size;
150
151             if (i == PCI_ROM_SLOT)
152                 ofs = 0x30;
153             else
154                 ofs = 0x10 + i * 4;
155             pci_config_writel(d, ofs, 0xffffffff);
156             val = pci_config_readl(d, ofs);
157             if (val != 0) {
158                 size = (~(val & ~0xf)) + 1;
159                 if (val & PCI_ADDRESS_SPACE_IO)
160                     paddr = &pci_bios_io_addr;
161                 else if (size >= 0x04000000)
162                     paddr = &pci_bios_bigmem_addr;
163                 else
164                     paddr = &pci_bios_mem_addr;
165                 *paddr = ALIGN(*paddr, size);
166                 pci_set_io_region_addr(d, i, *paddr);
167                 *paddr += size;
168             }
169         }
170         break;
171     }
172
173     /* map the interrupt */
174     pin = pci_config_readb(d, PCI_INTERRUPT_PIN);
175     if (pin != 0) {
176         pin = pci_slot_get_pirq(d, pin - 1);
177         pic_irq = pci_irqs[pin];
178         pci_config_writeb(d, PCI_INTERRUPT_LINE, pic_irq);
179     }
180
181     if (vendor_id == PCI_VENDOR_ID_INTEL
182         && device_id == PCI_DEVICE_ID_INTEL_82371AB_3) {
183         /* PIIX4 Power Management device (for ACPI) */
184         u32 pm_io_base = BUILD_PM_IO_BASE;
185         pci_config_writel(d, 0x40, pm_io_base | 1);
186         pci_config_writeb(d, 0x80, 0x01); /* enable PM io space */
187         u32 smb_io_base = BUILD_SMB_IO_BASE;
188         pci_config_writel(d, 0x90, smb_io_base | 1);
189         pci_config_writeb(d, 0xd2, 0x09); /* enable SMBus io space */
190     }
191 }
192
193 static void pci_for_each_device(void (*init_func)(PCIDevice d))
194 {
195     int bus, devfn;
196     u16 vendor_id, device_id;
197
198     for(bus = 0; bus < 1; bus++) {
199         for(devfn = 0; devfn < 256; devfn++) {
200             PCIDevice d = pci_bd(bus, devfn);
201             vendor_id = pci_config_readw(d, PCI_VENDOR_ID);
202             device_id = pci_config_readw(d, PCI_DEVICE_ID);
203             if (vendor_id != 0xffff || device_id != 0xffff) {
204                 init_func(d);
205             }
206         }
207     }
208 }
209
210 void
211 pci_bios_setup(void)
212 {
213     if (CONFIG_COREBOOT)
214         // Already done by coreboot.
215         return;
216
217     pci_bios_io_addr = 0xc000;
218     pci_bios_mem_addr = 0xf0000000;
219     pci_bios_bigmem_addr = GET_EBDA(ram_size);
220     if (pci_bios_bigmem_addr < 0x90000000)
221         pci_bios_bigmem_addr = 0x90000000;
222
223     pci_for_each_device(pci_bios_init_bridges);
224
225     pci_for_each_device(pci_bios_init_device);
226 }