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