blob: 71177bca8011e0dce613ef62d2aae8d0de884a3e [file] [log] [blame]
Kevin O'Connor0525d292008-07-04 06:18:30 -04001// Initialize PCI devices (on emulators)
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2006 Fabrice Bellard
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor0525d292008-07-04 06:18:30 -04007
8#include "util.h" // dprintf
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -05009#include "pci.h" // pci_config_readl
Kevin O'Connor9521e262008-07-04 13:04:29 -040010#include "biosvar.h" // GET_EBDA
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050011#include "pci_ids.h" // PCI_VENDOR_ID_INTEL
12#include "pci_regs.h" // PCI_COMMAND
Kevin O'Connor0525d292008-07-04 06:18:30 -040013
Kevin O'Connor0525d292008-07-04 06:18:30 -040014#define PCI_ROM_SLOT 6
15#define PCI_NUM_REGIONS 7
16
Kevin O'Connor0525d292008-07-04 06:18:30 -040017static u32 pci_bios_io_addr;
18static u32 pci_bios_mem_addr;
19static u32 pci_bios_bigmem_addr;
20/* host irqs corresponding to PCI irqs A-D */
Kevin O'Connor7061eb62009-01-04 21:48:22 -050021static u8 pci_irqs[4] = {
Kevin O'Connor7061eb62009-01-04 21:48:22 -050022 10, 10, 11, 11
Kevin O'Connor7061eb62009-01-04 21:48:22 -050023};
Kevin O'Connor0525d292008-07-04 06:18:30 -040024
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050025static void pci_set_io_region_addr(u16 bdf, int region_num, u32 addr)
Kevin O'Connor0525d292008-07-04 06:18:30 -040026{
Kevin O'Connor0525d292008-07-04 06:18:30 -040027 u32 ofs, old_addr;
28
Kevin O'Connor9649a962008-12-10 20:53:35 -050029 if (region_num == PCI_ROM_SLOT) {
30 ofs = PCI_ROM_ADDRESS;
31 } else {
32 ofs = PCI_BASE_ADDRESS_0 + region_num * 4;
Kevin O'Connor0525d292008-07-04 06:18:30 -040033 }
34
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050035 old_addr = pci_config_readl(bdf, ofs);
Kevin O'Connor0525d292008-07-04 06:18:30 -040036
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050037 pci_config_writel(bdf, ofs, addr);
Kevin O'Connor0525d292008-07-04 06:18:30 -040038 dprintf(1, "region %d: 0x%08x\n", region_num, addr);
Kevin O'Connor0525d292008-07-04 06:18:30 -040039}
40
41/* return the global irq number corresponding to a given device irq
42 pin. We could also use the bus number to have a more precise
43 mapping. */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050044static int pci_slot_get_pirq(u16 bdf, int irq_num)
Kevin O'Connor0525d292008-07-04 06:18:30 -040045{
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050046 int slot_addend = pci_bdf_to_dev(bdf) - 1;
Kevin O'Connor0525d292008-07-04 06:18:30 -040047 return (irq_num + slot_addend) & 3;
48}
49
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050050static void pci_bios_init_bridges(u16 bdf)
Kevin O'Connor0525d292008-07-04 06:18:30 -040051{
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050052 u16 vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
53 u16 device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
Kevin O'Connor0525d292008-07-04 06:18:30 -040054
Kevin O'Connor415c2dc2008-10-25 14:35:59 -040055 if (vendor_id == PCI_VENDOR_ID_INTEL
Kevin O'Connor2f840e32008-10-25 15:23:23 -040056 && (device_id == PCI_DEVICE_ID_INTEL_82371SB_0
57 || device_id == PCI_DEVICE_ID_INTEL_82371AB_0)) {
Kevin O'Connor0525d292008-07-04 06:18:30 -040058 int i, irq;
59 u8 elcr[2];
60
Kevin O'Connor2f840e32008-10-25 15:23:23 -040061 /* PIIX3/PIIX4 PCI to ISA bridge */
Kevin O'Connor0525d292008-07-04 06:18:30 -040062
63 elcr[0] = 0x00;
64 elcr[1] = 0x00;
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -040065 for (i = 0; i < 4; i++) {
Kevin O'Connor0525d292008-07-04 06:18:30 -040066 irq = pci_irqs[i];
67 /* set to trigger level */
68 elcr[irq >> 3] |= (1 << (irq & 7));
69 /* activate irq remapping in PIIX */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050070 pci_config_writeb(bdf, 0x60 + i, irq);
Kevin O'Connor0525d292008-07-04 06:18:30 -040071 }
72 outb(elcr[0], 0x4d0);
73 outb(elcr[1], 0x4d1);
Kevin O'Connor2f840e32008-10-25 15:23:23 -040074 dprintf(1, "PIIX3/PIIX4 init: elcr=%02x %02x\n",
Kevin O'Connor0525d292008-07-04 06:18:30 -040075 elcr[0], elcr[1]);
76 }
77}
78
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050079static void pci_bios_init_device(u16 bdf)
Kevin O'Connor0525d292008-07-04 06:18:30 -040080{
81 int class;
82 u32 *paddr;
83 int i, pin, pic_irq, vendor_id, device_id;
84
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050085 class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
86 vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
87 device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
88 dprintf(1, "PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n"
89 , pci_bdf_to_bus(bdf), pci_bdf_to_devfn(bdf), vendor_id, device_id);
Kevin O'Connor4dde5322009-10-08 21:34:45 -040090 switch (class) {
Kevin O'Connor9649a962008-12-10 20:53:35 -050091 case PCI_CLASS_STORAGE_IDE:
Kevin O'Connor415c2dc2008-10-25 14:35:59 -040092 if (vendor_id == PCI_VENDOR_ID_INTEL
Kevin O'Connor2f840e32008-10-25 15:23:23 -040093 && (device_id == PCI_DEVICE_ID_INTEL_82371SB_1
94 || device_id == PCI_DEVICE_ID_INTEL_82371AB)) {
95 /* PIIX3/PIIX4 IDE */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050096 pci_config_writew(bdf, 0x40, 0x8000); // enable IDE0
97 pci_config_writew(bdf, 0x42, 0x8000); // enable IDE1
Kevin O'Connor0525d292008-07-04 06:18:30 -040098 goto default_map;
99 } else {
100 /* IDE: we map it as in ISA mode */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500101 pci_set_io_region_addr(bdf, 0, 0x1f0);
102 pci_set_io_region_addr(bdf, 1, 0x3f4);
103 pci_set_io_region_addr(bdf, 2, 0x170);
104 pci_set_io_region_addr(bdf, 3, 0x374);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400105 }
106 break;
Kevin O'Connor9649a962008-12-10 20:53:35 -0500107 case PCI_CLASS_SYSTEM_PIC:
Kevin O'Connor0525d292008-07-04 06:18:30 -0400108 /* PIC */
Kevin O'Connor415c2dc2008-10-25 14:35:59 -0400109 if (vendor_id == PCI_VENDOR_ID_IBM) {
Kevin O'Connor0525d292008-07-04 06:18:30 -0400110 /* IBM */
111 if (device_id == 0x0046 || device_id == 0xFFFF) {
112 /* MPIC & MPIC2 */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500113 pci_set_io_region_addr(bdf, 0, 0x80800000 + 0x00040000);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400114 }
115 }
116 break;
117 case 0xff00:
Kevin O'Connor415c2dc2008-10-25 14:35:59 -0400118 if (vendor_id == PCI_VENDOR_ID_APPLE &&
Kevin O'Connor0525d292008-07-04 06:18:30 -0400119 (device_id == 0x0017 || device_id == 0x0022)) {
120 /* macio bridge */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500121 pci_set_io_region_addr(bdf, 0, 0x80800000);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400122 }
123 break;
124 default:
125 default_map:
126 /* default memory mappings */
Kevin O'Connor9649a962008-12-10 20:53:35 -0500127 for (i = 0; i < PCI_NUM_REGIONS; i++) {
Kevin O'Connor0525d292008-07-04 06:18:30 -0400128 int ofs;
Kevin O'Connore06363e2008-08-29 21:12:03 -0400129 u32 val, size;
Kevin O'Connor0525d292008-07-04 06:18:30 -0400130
131 if (i == PCI_ROM_SLOT)
Kevin O'Connor9649a962008-12-10 20:53:35 -0500132 ofs = PCI_ROM_ADDRESS;
Kevin O'Connor0525d292008-07-04 06:18:30 -0400133 else
Kevin O'Connor9649a962008-12-10 20:53:35 -0500134 ofs = PCI_BASE_ADDRESS_0 + i * 4;
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500135 pci_config_writel(bdf, ofs, 0xffffffff);
136 val = pci_config_readl(bdf, ofs);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400137 if (val != 0) {
138 size = (~(val & ~0xf)) + 1;
Kevin O'Connor9649a962008-12-10 20:53:35 -0500139 if (val & PCI_BASE_ADDRESS_SPACE_IO)
Kevin O'Connor0525d292008-07-04 06:18:30 -0400140 paddr = &pci_bios_io_addr;
141 else if (size >= 0x04000000)
142 paddr = &pci_bios_bigmem_addr;
143 else
144 paddr = &pci_bios_mem_addr;
Kevin O'Connore06363e2008-08-29 21:12:03 -0400145 *paddr = ALIGN(*paddr, size);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500146 pci_set_io_region_addr(bdf, i, *paddr);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400147 *paddr += size;
148 }
149 }
150 break;
151 }
152
Kevin O'Connorb82a1e42009-10-12 10:34:51 -0400153 /* enable memory mappings */
154 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
155
Kevin O'Connor0525d292008-07-04 06:18:30 -0400156 /* map the interrupt */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500157 pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400158 if (pin != 0) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500159 pin = pci_slot_get_pirq(bdf, pin - 1);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400160 pic_irq = pci_irqs[pin];
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500161 pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pic_irq);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400162 }
163
Kevin O'Connor415c2dc2008-10-25 14:35:59 -0400164 if (vendor_id == PCI_VENDOR_ID_INTEL
165 && device_id == PCI_DEVICE_ID_INTEL_82371AB_3) {
Kevin O'Connor0525d292008-07-04 06:18:30 -0400166 /* PIIX4 Power Management device (for ACPI) */
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500167
Kevin O'Connor8740e3a2009-06-21 09:35:10 -0400168 // acpi sci is hardwired to 9
169 pci_config_writeb(bdf, PCI_INTERRUPT_LINE, 9);
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500170
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500171 pci_config_writel(bdf, 0x40, PORT_ACPI_PM_BASE | 1);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500172 pci_config_writeb(bdf, 0x80, 0x01); /* enable PM io space */
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500173 pci_config_writel(bdf, 0x90, PORT_SMB_BASE | 1);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500174 pci_config_writeb(bdf, 0xd2, 0x09); /* enable SMBus io space */
Kevin O'Connor0525d292008-07-04 06:18:30 -0400175 }
176}
177
Kevin O'Connor0525d292008-07-04 06:18:30 -0400178void
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400179pci_setup(void)
Kevin O'Connor0525d292008-07-04 06:18:30 -0400180{
181 if (CONFIG_COREBOOT)
182 // Already done by coreboot.
183 return;
184
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400185 dprintf(3, "pci setup\n");
186
Kevin O'Connor0525d292008-07-04 06:18:30 -0400187 pci_bios_io_addr = 0xc000;
Kevin O'Connord1c4d642009-10-12 10:22:45 -0400188 pci_bios_mem_addr = 0xf0000000;
Kevin O'Connore7916362008-12-28 22:03:17 -0500189 pci_bios_bigmem_addr = RamSize;
Kevin O'Connor0525d292008-07-04 06:18:30 -0400190 if (pci_bios_bigmem_addr < 0x90000000)
191 pci_bios_bigmem_addr = 0x90000000;
192
Kevin O'Connore6338322008-11-29 20:31:49 -0500193 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500194 foreachpci(bdf, max) {
Kevin O'Connore6338322008-11-29 20:31:49 -0500195 pci_bios_init_bridges(bdf);
196 }
Kevin O'Connor4132e022008-12-04 19:39:10 -0500197 foreachpci(bdf, max) {
Kevin O'Connore6338322008-11-29 20:31:49 -0500198 pci_bios_init_device(bdf);
199 }
Kevin O'Connor0525d292008-07-04 06:18:30 -0400200}