2 * direct.c - Low-level direct PCI config space access
6 #include <linux/init.h>
10 * Functions for accessing PCI configuration space with type 1 accesses
13 #define PCI_CONF1_ADDRESS(bus, devfn, reg) \
14 (0x80000000 | (bus << 16) | (devfn << 8) | (reg & ~3))
16 static int pci_conf1_read(unsigned int seg, unsigned int bus,
17 unsigned int devfn, int reg, int len, u32 *value)
21 if (!value || (bus > 255) || (devfn > 255) || (reg > 255))
24 spin_lock_irqsave(&pci_config_lock, flags);
26 outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
30 *value = inb(0xCFC + (reg & 3));
33 *value = inw(0xCFC + (reg & 2));
40 spin_unlock_irqrestore(&pci_config_lock, flags);
45 static int pci_conf1_write(unsigned int seg, unsigned int bus,
46 unsigned int devfn, int reg, int len, u32 value)
50 if ((bus > 255) || (devfn > 255) || (reg > 255))
53 spin_lock_irqsave(&pci_config_lock, flags);
55 outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
59 outb((u8)value, 0xCFC + (reg & 3));
62 outw((u16)value, 0xCFC + (reg & 2));
65 outl((u32)value, 0xCFC);
69 spin_unlock_irqrestore(&pci_config_lock, flags);
74 #undef PCI_CONF1_ADDRESS
76 struct pci_raw_ops pci_direct_conf1 = {
77 .read = pci_conf1_read,
78 .write = pci_conf1_write,
83 * Functions for accessing PCI configuration space with type 2 accesses
86 #define PCI_CONF2_ADDRESS(dev, reg) (u16)(0xC000 | (dev << 8) | reg)
88 static int pci_conf2_read(unsigned int seg, unsigned int bus,
89 unsigned int devfn, int reg, int len, u32 *value)
94 if (!value || (bus > 255) || (devfn > 255) || (reg > 255))
97 dev = PCI_SLOT(devfn);
101 return PCIBIOS_DEVICE_NOT_FOUND;
103 spin_lock_irqsave(&pci_config_lock, flags);
105 outb((u8)(0xF0 | (fn << 1)), 0xCF8);
106 outb((u8)bus, 0xCFA);
110 *value = inb(PCI_CONF2_ADDRESS(dev, reg));
113 *value = inw(PCI_CONF2_ADDRESS(dev, reg));
116 *value = inl(PCI_CONF2_ADDRESS(dev, reg));
122 spin_unlock_irqrestore(&pci_config_lock, flags);
127 static int pci_conf2_write(unsigned int seg, unsigned int bus,
128 unsigned int devfn, int reg, int len, u32 value)
133 if ((bus > 255) || (devfn > 255) || (reg > 255))
136 dev = PCI_SLOT(devfn);
137 fn = PCI_FUNC(devfn);
140 return PCIBIOS_DEVICE_NOT_FOUND;
142 spin_lock_irqsave(&pci_config_lock, flags);
144 outb((u8)(0xF0 | (fn << 1)), 0xCF8);
145 outb((u8)bus, 0xCFA);
149 outb((u8)value, PCI_CONF2_ADDRESS(dev, reg));
152 outw((u16)value, PCI_CONF2_ADDRESS(dev, reg));
155 outl((u32)value, PCI_CONF2_ADDRESS(dev, reg));
161 spin_unlock_irqrestore(&pci_config_lock, flags);
166 #undef PCI_CONF2_ADDRESS
168 static struct pci_raw_ops pci_direct_conf2 = {
169 .read = pci_conf2_read,
170 .write = pci_conf2_write,
175 * Before we decide to use direct hardware access mechanisms, we try to do some
176 * trivial checks to ensure it at least _seems_ to be working -- we just test
177 * whether bus 00 contains a host bridge (this is similar to checking
178 * techniques used in XFree86, but ours should be more reliable since we
179 * attempt to make use of direct access hints provided by the PCI BIOS).
181 * This should be close to trivial, but it isn't, because there are buggy
182 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
184 static int __init pci_sanity_check(struct pci_raw_ops *o)
189 if (pci_probe & PCI_NO_CHECKS)
192 for (devfn = 0; devfn < 0x100; devfn++) {
193 if (o->read(0, 0, devfn, PCI_CLASS_DEVICE, 2, &x))
195 if (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA)
198 if (o->read(0, 0, devfn, PCI_VENDOR_ID, 2, &x))
200 if (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ)
204 DBG("PCI: Sanity check failed\n");
208 static int __init pci_check_type1(void)
214 local_irq_save(flags);
218 outl(0x80000000, 0xCF8);
219 if (inl(0xCF8) == 0x80000000 && pci_sanity_check(&pci_direct_conf1)) {
223 local_irq_restore(flags);
228 static int __init pci_check_type2(void)
233 local_irq_save(flags);
238 if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00 &&
239 pci_sanity_check(&pci_direct_conf2)) {
243 local_irq_restore(flags);
248 static int __init pci_direct_init(void)
250 struct resource *region, *region2;
252 if ((pci_probe & PCI_PROBE_CONF1) == 0)
254 region = request_region(0xCF8, 8, "PCI conf1");
258 if (pci_check_type1()) {
259 printk(KERN_INFO "PCI: Using configuration type 1\n");
260 raw_pci_ops = &pci_direct_conf1;
263 release_resource(region);
266 if ((pci_probe & PCI_PROBE_CONF2) == 0)
268 region = request_region(0xCF8, 4, "PCI conf2");
271 region2 = request_region(0xC000, 0x1000, "PCI conf2");
275 if (pci_check_type2()) {
276 printk(KERN_INFO "PCI: Using configuration type 2\n");
277 raw_pci_ops = &pci_direct_conf2;
281 release_resource(region2);
283 release_resource(region);
289 arch_initcall(pci_direct_init);