blob: 67355a0e398d697512bdfbebd2a033d3821f1a12 [file] [log] [blame]
swissChilie8491742021-04-08 20:38:06 -07001#include <dri/pci/pci.h>
swissChilica268482021-05-28 18:31:46 -07002#include <dri/pci/vendors.h>
3#include <io.h>
4#include <log.h>
swissChili0d248832021-04-08 18:16:02 -07005
swissChili93214982021-05-28 21:32:26 -07006uint pci_config_readd(uchar bus, uchar slot, uchar func, uchar offset)
swissChili0d248832021-04-08 18:16:02 -07007{
swissChilica268482021-05-28 18:31:46 -07008 uint address = (bus << 16) | (slot << 11) | (func << 8) | (offset << 2) | 0x80000000;
9
10 outl(PCI_CONFIG_ADDRESS, address);
11
12 return inl(PCI_CONFIG_DATA);
13}
14
swissChili93214982021-05-28 21:32:26 -070015struct pci_vendor *pci_check_vendor(uchar bus, uchar slot, uchar func, uint *v)
swissChilica268482021-05-28 18:31:46 -070016{
swissChili77eb1472021-05-28 21:40:00 -070017 uint vendor = pci_config_readd(bus, slot, func, 0);
swissChilica268482021-05-28 18:31:46 -070018
swissChili77eb1472021-05-28 21:40:00 -070019 if (v)
20 *v = vendor;
21
22 if (vendor != ~0)
swissChili0d248832021-04-08 18:16:02 -070023 {
swissChili93214982021-05-28 21:32:26 -070024 return pci_vendor_by_id(vendor);
swissChilica268482021-05-28 18:31:46 -070025 }
swissChili93214982021-05-28 21:32:26 -070026 return NULL;
swissChilica268482021-05-28 18:31:46 -070027}
28
29struct pci_vendor *pci_vendor_by_id(ushort id)
30{
31 // Find vendor using binary search
32
33 uint start = 0,
34 max = pci_num_vendors;
35
36 while (true)
37 {
38 if (max == start)
39 // Can't find one
40 return NULL;
41
42 uint guess = (max - start) / 2 + start;
43
44 if (pci_vendors[guess].id == id)
45 return &pci_vendors[guess];
46 else if (pci_vendors[guess].id > id)
47 max = guess;
48 else
49 start = guess;
50 }
swissChili0d248832021-04-08 18:16:02 -070051}