Make PCI CONF2 support a compile time option
[coreboot.git] / src / arch / x86 / lib / pci_ops_auto.c
1 #include <stddef.h>
2 #include <console/console.h>
3 #include <arch/io.h>
4 #include <arch/pciconf.h>
5 #include <device/pci.h>
6 #include <device/pci_ids.h>
7 #include <device/pci_ops.h>
8
9 #if CONFIG_PCI_CONF2
10 /*
11  * Before we decide to use direct hardware access mechanisms, we try to do some
12  * trivial checks to ensure it at least _seems_ to be working -- we just test
13  * whether bus 00 contains a host bridge (this is similar to checking
14  * techniques used in XFree86, but ours should be more reliable since we
15  * attempt to make use of direct access hints provided by the PCI BIOS).
16  *
17  * This should be close to trivial, but it isn't, because there are buggy
18  * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
19  */
20 static int pci_sanity_check(const struct pci_bus_operations *o)
21 {
22         uint16_t class, vendor;
23         unsigned bus;
24         int devfn;
25         struct bus pbus; /* Dummy device */
26 #define PCI_CLASS_BRIDGE_HOST           0x0600
27 #define PCI_CLASS_DISPLAY_VGA           0x0300
28 #define PCI_VENDOR_ID_COMPAQ            0x0e11
29 #define PCI_VENDOR_ID_INTEL             0x8086
30 #define PCI_VENDOR_ID_MOTOROLA          0x1057
31
32         for (bus = 0, devfn = 0; devfn < 0x100; devfn++) {
33                 class = o->read16(&pbus, bus, devfn, PCI_CLASS_DEVICE);
34                 vendor = o->read16(&pbus, bus, devfn, PCI_VENDOR_ID);
35                 if (((class == PCI_CLASS_BRIDGE_HOST) || (class == PCI_CLASS_DISPLAY_VGA)) ||
36                         ((vendor == PCI_VENDOR_ID_INTEL) || (vendor == PCI_VENDOR_ID_COMPAQ) ||
37                                 (vendor == PCI_VENDOR_ID_MOTOROLA))) {
38                         return 1;
39                 }
40         }
41         printk(BIOS_ERR, "PCI: Sanity check failed\n");
42         return 0;
43 }
44
45 static struct pci_bus_operations *pci_bus_fallback_ops = NULL;
46
47 static const struct pci_bus_operations *pci_check_direct(void)
48 {
49         unsigned int tmp;
50
51         /*
52          * Check if configuration type 1 works.
53          */
54         {
55                 outb(0x01, 0xCFB);
56                 tmp = inl(0xCF8);
57                 outl(0x80000000, 0xCF8);
58                 if ((inl(0xCF8) == 0x80000000) &&
59                         pci_sanity_check(&pci_cf8_conf1))
60                 {
61                         outl(tmp, 0xCF8);
62                         printk(BIOS_DEBUG, "PCI: Using configuration type 1\n");
63                         return &pci_cf8_conf1;
64                 }
65                 outl(tmp, 0xCF8);
66         }
67
68         /*
69          * Check if configuration type 2 works.
70          */
71         {
72                 outb(0x00, 0xCFB);
73                 outb(0x00, 0xCF8);
74                 outb(0x00, 0xCFA);
75                 if ((inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00) &&
76                         pci_sanity_check(&pci_cf8_conf2))
77                 {
78                         printk(BIOS_DEBUG, "PCI: Using configuration type 2\n");
79                         return &pci_cf8_conf2;
80                 }
81         }
82
83         die("pci_check_direct failed\n");
84         return NULL;
85 }
86
87 const struct pci_bus_operations *pci_remember_direct(void)
88 {
89         if (!pci_bus_fallback_ops)
90                 pci_bus_fallback_ops = (struct pci_bus_operations *)pci_check_direct();
91         return pci_bus_fallback_ops;
92 }
93 #else
94 const struct pci_bus_operations *pci_remember_direct(void)
95 {
96         return &pci_cf8_conf1;
97 }
98 #endif
99
100 /** Set the method to be used for PCI, type I or type II
101  */
102 void pci_set_method(device_t dev)
103 {
104         printk(BIOS_INFO, "Finding PCI configuration type.\n");
105         dev->ops->ops_pci_bus = pci_remember_direct();
106         post_code(0x5f);
107 }