1 // PCI config space access functions.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "pci.h" // PCIDevice
9 #include "ioport.h" // outl
10 #include "util.h" // dprintf
11 #include "config.h" // CONFIG_*
13 void pci_config_writel(PCIDevice d, u32 addr, u32 val)
15 outl(0x80000000 | (d.bus << 16) | (d.devfn << 8) | (addr & 0xfc)
17 outl(val, PORT_PCI_DATA);
20 void pci_config_writew(PCIDevice d, u32 addr, u16 val)
22 outl(0x80000000 | (d.bus << 16) | (d.devfn << 8) | (addr & 0xfc)
24 outw(val, PORT_PCI_DATA + (addr & 2));
27 void pci_config_writeb(PCIDevice d, u32 addr, u8 val)
29 outl(0x80000000 | (d.bus << 16) | (d.devfn << 8) | (addr & 0xfc)
31 outb(val, PORT_PCI_DATA + (addr & 3));
34 u32 pci_config_readl(PCIDevice d, u32 addr)
36 outl(0x80000000 | (d.bus << 16) | (d.devfn << 8) | (addr & 0xfc)
38 return inl(PORT_PCI_DATA);
41 u16 pci_config_readw(PCIDevice d, u32 addr)
43 outl(0x80000000 | (d.bus << 16) | (d.devfn << 8) | (addr & 0xfc)
45 return inw(PORT_PCI_DATA + (addr & 2));
48 u8 pci_config_readb(PCIDevice d, u32 addr)
50 outl(0x80000000 | (d.bus << 16) | (d.devfn << 8) | (addr & 0xfc)
52 return inb(PORT_PCI_DATA + (addr & 3));
56 pci_find_device(u16 vendid, u16 devid, int index, PCIDevice *dev)
59 u32 id = (devid << 16) | vendid;
60 for (bus=0; bus < CONFIG_PCI_BUS_COUNT; bus++) {
61 for (devfn=0; devfn<0x100; devfn++) {
62 PCIDevice d = pci_bd(bus, devfn);
63 u32 v = pci_config_readl(d, PCI_VENDOR_ID);
78 // Search for a device with the specified class id and prog-if.
80 pci_find_classprog(u32 classprog, int index, PCIDevice *dev)
83 for (bus=0; bus < CONFIG_PCI_BUS_COUNT; bus++) {
84 for (devfn=0; devfn<0x100; devfn++) {
85 PCIDevice d = pci_bd(bus, devfn);
86 u32 v = pci_config_readl(d, 0x08);
87 if ((v>>8) != classprog)
101 // Search for a device with the specified class id.
103 pci_find_class(u16 classid, int index, PCIDevice *dev)
106 for (bus=0; bus < CONFIG_PCI_BUS_COUNT; bus++) {
107 for (devfn=0; devfn<0x100; devfn++) {
108 PCIDevice d = pci_bd(bus, devfn);
109 u16 v = pci_config_readw(d, PCI_CLASS_DEVICE);