blob: a6070e7ae8c6baa6324fc75e0f307362c2bbef39 [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;
Kevin O'Connor0525d292008-07-04 06:18:30 -040019/* host irqs corresponding to PCI irqs A-D */
Kevin O'Connor7061eb62009-01-04 21:48:22 -050020static u8 pci_irqs[4] = {
Kevin O'Connor7061eb62009-01-04 21:48:22 -050021 10, 10, 11, 11
Kevin O'Connor7061eb62009-01-04 21:48:22 -050022};
Kevin O'Connor0525d292008-07-04 06:18:30 -040023
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050024static void pci_set_io_region_addr(u16 bdf, int region_num, u32 addr)
Kevin O'Connor0525d292008-07-04 06:18:30 -040025{
Kevin O'Connor0525d292008-07-04 06:18:30 -040026 u32 ofs, old_addr;
27
Kevin O'Connor9649a962008-12-10 20:53:35 -050028 if (region_num == PCI_ROM_SLOT) {
29 ofs = PCI_ROM_ADDRESS;
30 } else {
31 ofs = PCI_BASE_ADDRESS_0 + region_num * 4;
Kevin O'Connor0525d292008-07-04 06:18:30 -040032 }
33
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050034 old_addr = pci_config_readl(bdf, ofs);
Kevin O'Connor0525d292008-07-04 06:18:30 -040035
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050036 pci_config_writel(bdf, ofs, addr);
Kevin O'Connor0525d292008-07-04 06:18:30 -040037 dprintf(1, "region %d: 0x%08x\n", region_num, addr);
Kevin O'Connor0525d292008-07-04 06:18:30 -040038}
39
40/* return the global irq number corresponding to a given device irq
41 pin. We could also use the bus number to have a more precise
42 mapping. */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050043static int pci_slot_get_pirq(u16 bdf, int irq_num)
Kevin O'Connor0525d292008-07-04 06:18:30 -040044{
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050045 int slot_addend = pci_bdf_to_dev(bdf) - 1;
Kevin O'Connor0525d292008-07-04 06:18:30 -040046 return (irq_num + slot_addend) & 3;
47}
48
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050049static void pci_bios_init_bridges(u16 bdf)
Kevin O'Connor0525d292008-07-04 06:18:30 -040050{
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050051 u16 vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
52 u16 device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
Kevin O'Connor0525d292008-07-04 06:18:30 -040053
Kevin O'Connor415c2dc2008-10-25 14:35:59 -040054 if (vendor_id == PCI_VENDOR_ID_INTEL
Kevin O'Connor2f840e32008-10-25 15:23:23 -040055 && (device_id == PCI_DEVICE_ID_INTEL_82371SB_0
56 || device_id == PCI_DEVICE_ID_INTEL_82371AB_0)) {
Kevin O'Connor0525d292008-07-04 06:18:30 -040057 int i, irq;
58 u8 elcr[2];
59
Kevin O'Connor2f840e32008-10-25 15:23:23 -040060 /* PIIX3/PIIX4 PCI to ISA bridge */
Kevin O'Connor0525d292008-07-04 06:18:30 -040061
62 elcr[0] = 0x00;
63 elcr[1] = 0x00;
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -040064 for (i = 0; i < 4; i++) {
Kevin O'Connor0525d292008-07-04 06:18:30 -040065 irq = pci_irqs[i];
66 /* set to trigger level */
67 elcr[irq >> 3] |= (1 << (irq & 7));
68 /* activate irq remapping in PIIX */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050069 pci_config_writeb(bdf, 0x60 + i, irq);
Kevin O'Connor0525d292008-07-04 06:18:30 -040070 }
71 outb(elcr[0], 0x4d0);
72 outb(elcr[1], 0x4d1);
Kevin O'Connor2f840e32008-10-25 15:23:23 -040073 dprintf(1, "PIIX3/PIIX4 init: elcr=%02x %02x\n",
Kevin O'Connor0525d292008-07-04 06:18:30 -040074 elcr[0], elcr[1]);
75 }
76}
77
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050078static void pci_bios_init_device(u16 bdf)
Kevin O'Connor0525d292008-07-04 06:18:30 -040079{
80 int class;
81 u32 *paddr;
82 int i, pin, pic_irq, vendor_id, device_id;
83
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050084 class = pci_config_readw(bdf, PCI_CLASS_DEVICE);
85 vendor_id = pci_config_readw(bdf, PCI_VENDOR_ID);
86 device_id = pci_config_readw(bdf, PCI_DEVICE_ID);
87 dprintf(1, "PCI: bus=%d devfn=0x%02x: vendor_id=0x%04x device_id=0x%04x\n"
88 , pci_bdf_to_bus(bdf), pci_bdf_to_devfn(bdf), vendor_id, device_id);
Kevin O'Connor4dde5322009-10-08 21:34:45 -040089 switch (class) {
Kevin O'Connor9649a962008-12-10 20:53:35 -050090 case PCI_CLASS_STORAGE_IDE:
Kevin O'Connor415c2dc2008-10-25 14:35:59 -040091 if (vendor_id == PCI_VENDOR_ID_INTEL
Kevin O'Connor2f840e32008-10-25 15:23:23 -040092 && (device_id == PCI_DEVICE_ID_INTEL_82371SB_1
93 || device_id == PCI_DEVICE_ID_INTEL_82371AB)) {
94 /* PIIX3/PIIX4 IDE */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050095 pci_config_writew(bdf, 0x40, 0x8000); // enable IDE0
96 pci_config_writew(bdf, 0x42, 0x8000); // enable IDE1
Kevin O'Connor0525d292008-07-04 06:18:30 -040097 goto default_map;
98 } else {
99 /* IDE: we map it as in ISA mode */
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500100 pci_set_io_region_addr(bdf, 0, PORT_ATA1_CMD_BASE);
Kevin O'Connor525219b2009-12-05 13:36:18 -0500101 pci_set_io_region_addr(bdf, 1, PORT_ATA1_CTRL_BASE);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500102 pci_set_io_region_addr(bdf, 2, PORT_ATA2_CMD_BASE);
Kevin O'Connor525219b2009-12-05 13:36:18 -0500103 pci_set_io_region_addr(bdf, 3, PORT_ATA2_CTRL_BASE);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400104 }
105 break;
Kevin O'Connor9649a962008-12-10 20:53:35 -0500106 case PCI_CLASS_SYSTEM_PIC:
Kevin O'Connor0525d292008-07-04 06:18:30 -0400107 /* PIC */
Kevin O'Connor415c2dc2008-10-25 14:35:59 -0400108 if (vendor_id == PCI_VENDOR_ID_IBM) {
Kevin O'Connor0525d292008-07-04 06:18:30 -0400109 /* IBM */
110 if (device_id == 0x0046 || device_id == 0xFFFF) {
111 /* MPIC & MPIC2 */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500112 pci_set_io_region_addr(bdf, 0, 0x80800000 + 0x00040000);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400113 }
114 }
115 break;
116 case 0xff00:
Kevin O'Connor415c2dc2008-10-25 14:35:59 -0400117 if (vendor_id == PCI_VENDOR_ID_APPLE &&
Kevin O'Connor0525d292008-07-04 06:18:30 -0400118 (device_id == 0x0017 || device_id == 0x0022)) {
119 /* macio bridge */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500120 pci_set_io_region_addr(bdf, 0, 0x80800000);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400121 }
122 break;
123 default:
124 default_map:
125 /* default memory mappings */
Kevin O'Connor9649a962008-12-10 20:53:35 -0500126 for (i = 0; i < PCI_NUM_REGIONS; i++) {
Kevin O'Connor0525d292008-07-04 06:18:30 -0400127 int ofs;
Kevin O'Connor0525d292008-07-04 06:18:30 -0400128 if (i == PCI_ROM_SLOT)
Kevin O'Connor9649a962008-12-10 20:53:35 -0500129 ofs = PCI_ROM_ADDRESS;
Kevin O'Connor0525d292008-07-04 06:18:30 -0400130 else
Kevin O'Connor9649a962008-12-10 20:53:35 -0500131 ofs = PCI_BASE_ADDRESS_0 + i * 4;
Kevin O'Connor2f442fd2009-10-14 19:28:04 -0400132
Kevin O'Connor9dbc2ad2009-11-09 19:15:08 -0500133 u32 old = pci_config_readl(bdf, ofs);
134 u32 mask;
Kevin O'Connor2f442fd2009-10-14 19:28:04 -0400135 if (i == PCI_ROM_SLOT) {
136 mask = PCI_ROM_ADDRESS_MASK;
137 pci_config_writel(bdf, ofs, mask);
138 } else {
Kevin O'Connor9dbc2ad2009-11-09 19:15:08 -0500139 if (old & PCI_BASE_ADDRESS_SPACE_IO)
Kevin O'Connor2f442fd2009-10-14 19:28:04 -0400140 mask = PCI_BASE_ADDRESS_IO_MASK;
141 else
142 mask = PCI_BASE_ADDRESS_MEM_MASK;
143 pci_config_writel(bdf, ofs, ~0);
144 }
Kevin O'Connor9dbc2ad2009-11-09 19:15:08 -0500145 u32 val = pci_config_readl(bdf, ofs);
Kevin O'Connor2f442fd2009-10-14 19:28:04 -0400146 pci_config_writel(bdf, ofs, old);
147
Kevin O'Connor0525d292008-07-04 06:18:30 -0400148 if (val != 0) {
Kevin O'Connor9dbc2ad2009-11-09 19:15:08 -0500149 u32 size = (~(val & mask)) + 1;
Kevin O'Connor9649a962008-12-10 20:53:35 -0500150 if (val & PCI_BASE_ADDRESS_SPACE_IO)
Kevin O'Connor0525d292008-07-04 06:18:30 -0400151 paddr = &pci_bios_io_addr;
Kevin O'Connor0525d292008-07-04 06:18:30 -0400152 else
153 paddr = &pci_bios_mem_addr;
Kevin O'Connore06363e2008-08-29 21:12:03 -0400154 *paddr = ALIGN(*paddr, size);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500155 pci_set_io_region_addr(bdf, i, *paddr);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400156 *paddr += size;
157 }
158 }
159 break;
160 }
161
Kevin O'Connorb82a1e42009-10-12 10:34:51 -0400162 /* enable memory mappings */
163 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
164
Kevin O'Connor0525d292008-07-04 06:18:30 -0400165 /* map the interrupt */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500166 pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400167 if (pin != 0) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500168 pin = pci_slot_get_pirq(bdf, pin - 1);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400169 pic_irq = pci_irqs[pin];
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500170 pci_config_writeb(bdf, PCI_INTERRUPT_LINE, pic_irq);
Kevin O'Connor0525d292008-07-04 06:18:30 -0400171 }
172
Kevin O'Connor415c2dc2008-10-25 14:35:59 -0400173 if (vendor_id == PCI_VENDOR_ID_INTEL
174 && device_id == PCI_DEVICE_ID_INTEL_82371AB_3) {
Kevin O'Connor0525d292008-07-04 06:18:30 -0400175 /* PIIX4 Power Management device (for ACPI) */
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500176
Kevin O'Connor8740e3a2009-06-21 09:35:10 -0400177 // acpi sci is hardwired to 9
178 pci_config_writeb(bdf, PCI_INTERRUPT_LINE, 9);
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500179
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500180 pci_config_writel(bdf, 0x40, PORT_ACPI_PM_BASE | 1);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500181 pci_config_writeb(bdf, 0x80, 0x01); /* enable PM io space */
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500182 pci_config_writel(bdf, 0x90, PORT_SMB_BASE | 1);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500183 pci_config_writeb(bdf, 0xd2, 0x09); /* enable SMBus io space */
Kevin O'Connor0525d292008-07-04 06:18:30 -0400184 }
185}
186
Kevin O'Connor0525d292008-07-04 06:18:30 -0400187void
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400188pci_setup(void)
Kevin O'Connor0525d292008-07-04 06:18:30 -0400189{
190 if (CONFIG_COREBOOT)
191 // Already done by coreboot.
192 return;
193
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400194 dprintf(3, "pci setup\n");
195
Kevin O'Connor0525d292008-07-04 06:18:30 -0400196 pci_bios_io_addr = 0xc000;
Kevin O'Connord9104ff2009-12-26 23:23:15 -0500197 pci_bios_mem_addr = BUILD_MAX_HIGHMEM;
Kevin O'Connor0525d292008-07-04 06:18:30 -0400198
Kevin O'Connore6338322008-11-29 20:31:49 -0500199 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500200 foreachpci(bdf, max) {
Kevin O'Connore6338322008-11-29 20:31:49 -0500201 pci_bios_init_bridges(bdf);
202 }
Kevin O'Connor4132e022008-12-04 19:39:10 -0500203 foreachpci(bdf, max) {
Kevin O'Connore6338322008-11-29 20:31:49 -0500204 pci_bios_init_device(bdf);
205 }
Kevin O'Connor0525d292008-07-04 06:18:30 -0400206}