Correct default pci irq links
[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 LGPLv3 license.
7
8 #include "util.h" // dprintf
9 #include "pci.h" // pci_config_readl
10 #include "biosvar.h" // GET_EBDA
11 #include "pci_ids.h" // PCI_VENDOR_ID_INTEL
12 #include "pci_regs.h" // PCI_COMMAND
13
14 #define PCI_ROM_SLOT 6
15 #define PCI_NUM_REGIONS 7
16
17 static u32 pci_bios_io_addr;
18 static u32 pci_bios_mem_addr;
19 static u32 pci_bios_bigmem_addr;
20 /* host irqs corresponding to PCI irqs A-D */
21 static u8 pci_irqs[4] = {
22     10, 10, 11, 11
23 };
24
25 static void pci_set_io_region_addr(u16 bdf, int region_num, u32 addr)
26 {
27     u16 cmd;
28     u32 ofs, old_addr;
29
30     if (region_num == PCI_ROM_SLOT) {
31         ofs = PCI_ROM_ADDRESS;
32     } else {
33         ofs = PCI_BASE_ADDRESS_0 + region_num * 4;
34     }
35
36     old_addr = pci_config_readl(bdf, ofs);
37
38     pci_config_writel(bdf, ofs, addr);
39     dprintf(1, "region %d: 0x%08x\n", region_num, addr);
40
41     /* enable memory mappings */
42     cmd = pci_config_readw(bdf, PCI_COMMAND);
43     if (region_num == PCI_ROM_SLOT)
44         cmd |= PCI_COMMAND_MEMORY;
45     else if (old_addr & PCI_BASE_ADDRESS_SPACE_IO)
46         cmd |= PCI_COMMAND_IO;
47     else
48         cmd |= PCI_COMMAND_MEMORY;
49     pci_config_writew(bdf, 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(u16 bdf, int irq_num)
56 {
57     int slot_addend = pci_bdf_to_dev(bdf) - 1;
58     return (irq_num + slot_addend) & 3;
59 }
60
61 static void pci_bios_init_bridges(u16 bdf)
62 {
63     u16 vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
64     u16 device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
65
66     if (vendor_id == PCI_VENDOR_ID_INTEL
67         && (device_id == PCI_DEVICE_ID_INTEL_82371SB_0
68             || device_id == PCI_DEVICE_ID_INTEL_82371AB_0)) {
69         int i, irq;
70         u8 elcr[2];
71
72         /* PIIX3/PIIX4 PCI to ISA bridge */
73
74         elcr[0] = 0x00;
75         elcr[1] = 0x00;
76         for (i = 0; i < 4; i++) {
77             irq = pci_irqs[i];
78             /* set to trigger level */
79             elcr[irq >> 3] |= (1 << (irq & 7));
80             /* activate irq remapping in PIIX */
81             pci_config_writeb(bdf, 0x60 + i, irq);
82         }
83         outb(elcr[0], 0x4d0);
84         outb(elcr[1], 0x4d1);
85         dprintf(1, "PIIX3/PIIX4 init: elcr=%02x %02x\n",
86                 elcr[0], elcr[1]);
87     }
88 }
89
90 static void pci_bios_init_device(u16 bdf)
91 {
92     int class;
93     u32 *paddr;
94     int i, pin, pic_irq, vendor_id, device_id;
95
96     class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
97     vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
98     device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
99     dprintf(1, "PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n"
100             , pci_bdf_to_bus(bdf), pci_bdf_to_devfn(bdf), vendor_id, device_id);
101     switch (class) {
102     case PCI_CLASS_STORAGE_IDE:
103         if (vendor_id == PCI_VENDOR_ID_INTEL
104             && (device_id == PCI_DEVICE_ID_INTEL_82371SB_1
105                 || device_id == PCI_DEVICE_ID_INTEL_82371AB)) {
106             /* PIIX3/PIIX4 IDE */
107             pci_config_writew(bdf, 0x40, 0x8000); // enable IDE0
108             pci_config_writew(bdf, 0x42, 0x8000); // enable IDE1
109             goto default_map;
110         } else {
111             /* IDE: we map it as in ISA mode */
112             pci_set_io_region_addr(bdf, 0, 0x1f0);
113             pci_set_io_region_addr(bdf, 1, 0x3f4);
114             pci_set_io_region_addr(bdf, 2, 0x170);
115             pci_set_io_region_addr(bdf, 3, 0x374);
116         }
117         break;
118     case PCI_CLASS_SYSTEM_PIC:
119         /* PIC */
120         if (vendor_id == PCI_VENDOR_ID_IBM) {
121             /* IBM */
122             if (device_id == 0x0046 || device_id == 0xFFFF) {
123                 /* MPIC & MPIC2 */
124                 pci_set_io_region_addr(bdf, 0, 0x80800000 + 0x00040000);
125             }
126         }
127         break;
128     case 0xff00:
129         if (vendor_id == PCI_VENDOR_ID_APPLE &&
130             (device_id == 0x0017 || device_id == 0x0022)) {
131             /* macio bridge */
132             pci_set_io_region_addr(bdf, 0, 0x80800000);
133         }
134         break;
135     default:
136     default_map:
137         /* default memory mappings */
138         for (i = 0; i < PCI_NUM_REGIONS; i++) {
139             int ofs;
140             u32 val, size;
141
142             if (i == PCI_ROM_SLOT)
143                 ofs = PCI_ROM_ADDRESS;
144             else
145                 ofs = PCI_BASE_ADDRESS_0 + i * 4;
146             pci_config_writel(bdf, ofs, 0xffffffff);
147             val = pci_config_readl(bdf, ofs);
148             if (val != 0) {
149                 size = (~(val & ~0xf)) + 1;
150                 if (val & PCI_BASE_ADDRESS_SPACE_IO)
151                     paddr = &pci_bios_io_addr;
152                 else if (size >= 0x04000000)
153                     paddr = &pci_bios_bigmem_addr;
154                 else
155                     paddr = &pci_bios_mem_addr;
156                 *paddr = ALIGN(*paddr, size);
157                 pci_set_io_region_addr(bdf, i, *paddr);
158                 *paddr += size;
159             }
160         }
161         break;
162     }
163
164     /* map the interrupt */
165     pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
166     if (pin != 0) {
167         pin = pci_slot_get_pirq(bdf, pin - 1);
168         pic_irq = pci_irqs[pin];
169         pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pic_irq);
170     }
171
172     if (vendor_id == PCI_VENDOR_ID_INTEL
173         && device_id == PCI_DEVICE_ID_INTEL_82371AB_3) {
174         /* PIIX4 Power Management device (for ACPI) */
175
176         // acpi sci is hardwired to 9
177         pci_config_writeb(bdf, PCI_INTERRUPT_LINE, 9);
178
179         pci_config_writel(bdf, 0x40, PORT_ACPI_PM_BASE | 1);
180         pci_config_writeb(bdf, 0x80, 0x01); /* enable PM io space */
181         pci_config_writel(bdf, 0x90, PORT_SMB_BASE | 1);
182         pci_config_writeb(bdf, 0xd2, 0x09); /* enable SMBus io space */
183     }
184 }
185
186 void
187 pci_setup(void)
188 {
189     if (CONFIG_COREBOOT)
190         // Already done by coreboot.
191         return;
192
193     dprintf(3, "pci setup\n");
194
195     pci_bios_io_addr = 0xc000;
196     pci_bios_mem_addr = 0xc0000000;
197     pci_bios_bigmem_addr = RamSize;
198     if (pci_bios_bigmem_addr < 0x90000000)
199         pci_bios_bigmem_addr = 0x90000000;
200
201     int bdf, max;
202     foreachpci(bdf, max) {
203         pci_bios_init_bridges(bdf);
204     }
205     foreachpci(bdf, max) {
206         pci_bios_init_device(bdf);
207     }
208 }