Overhaul PCI config functions.
[seabios.git] / src / pci.c
1 // PCI config space access functions.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "pci.h" // MaxBDF
9 #include "ioport.h" // outl
10 #include "util.h" // dprintf
11 #include "config.h" // CONFIG_*
12 #include "pci_regs.h" // PCI_VENDOR_ID
13 #include "farptr.h" // SET_VAR
14
15 void pci_config_writel(u16 bdf, u32 addr, u32 val)
16 {
17     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
18     outl(val, PORT_PCI_DATA);
19 }
20
21 void pci_config_writew(u16 bdf, u32 addr, u16 val)
22 {
23     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
24     outw(val, PORT_PCI_DATA + (addr & 2));
25 }
26
27 void pci_config_writeb(u16 bdf, u32 addr, u8 val)
28 {
29     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
30     outb(val, PORT_PCI_DATA + (addr & 3));
31 }
32
33 u32 pci_config_readl(u16 bdf, u32 addr)
34 {
35     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
36     return inl(PORT_PCI_DATA);
37 }
38
39 u16 pci_config_readw(u16 bdf, u32 addr)
40 {
41     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
42     return inw(PORT_PCI_DATA + (addr & 2));
43 }
44
45 u8 pci_config_readb(u16 bdf, u32 addr)
46 {
47     outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
48     return inb(PORT_PCI_DATA + (addr & 3));
49 }
50
51 #if MODE16
52 int MaxBDF VISIBLE16;
53 #endif
54
55 // Find the maximum bus number.
56 void
57 pci_bus_setup()
58 {
59     dprintf(3, "Scan for max PCI bus\n");
60
61     int max = 0x0100;
62     int bdf;
63     for (bdf=0; bdf < max; bdf++) {
64         u32 v = pci_config_readl(bdf, PCI_VENDOR_ID);
65         if (v == 0xffffffff || v == 0x00000000
66             || v == 0x0000ffff || v == 0xffff0000)
67             // No device present.
68             continue;
69         v = pci_config_readb(bdf, PCI_HEADER_TYPE);
70         v &= 0x7f;
71         if (v != PCI_HEADER_TYPE_BRIDGE && v != PCI_HEADER_TYPE_CARDBUS)
72             // Not a bridge
73             continue;
74         v = pci_config_readl(bdf, PCI_PRIMARY_BUS);
75         int newmax = (v & 0xff00) + 0x0100;
76         if (newmax > max)
77             max = newmax;
78     }
79     SET_VAR(CS, MaxBDF, max);
80
81     dprintf(1, "Found %d PCI buses\n", pci_bdf_to_bus(max));
82 }
83
84 // Search for a device with the specified vendor and device ids.
85 int
86 pci_find_device(u16 vendid, u16 devid, int start_bdf)
87 {
88     u32 id = (devid << 16) | vendid;
89     int max = GET_VAR(CS, MaxBDF);
90     int bdf;
91     for (bdf=start_bdf; bdf < max; bdf++) {
92         u32 v = pci_config_readl(bdf, PCI_VENDOR_ID);
93         if (v != id)
94             continue;
95         // Found it.
96         return bdf;
97     }
98     return -1;
99 }
100
101 // Search for a device with the specified class id and prog-if.
102 int
103 pci_find_classprog(u32 classprog, int start_bdf)
104 {
105     int max = GET_VAR(CS, MaxBDF);
106     int bdf;
107     for (bdf=start_bdf; bdf < max; bdf++) {
108         u32 v = pci_config_readl(bdf, PCI_CLASS_REVISION);
109         if ((v>>8) != classprog)
110             continue;
111         // Found it.
112         return bdf;
113     }
114     return -1;
115 }
116
117 // Search for a device with the specified class id.
118 int
119 pci_find_class(u16 classid, int start_bdf)
120 {
121     int max = GET_VAR(CS, MaxBDF);
122     int bdf;
123     for (bdf=start_bdf; bdf < max; bdf++) {
124         u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
125         if (v != classid)
126             continue;
127         // Found it.
128         return bdf;
129     }
130     return -1;
131 }