2 * direct.c - Low-level direct PCI config space access
6 #include <linux/init.h>
11 * Functions for accessing PCI configuration space with type 1 accesses
14 #define PCI_CONF1_ADDRESS(bus, devfn, reg) \
15 (0x80000000 | (bus << 16) | (devfn << 8) | (reg & ~3))
17 int pci_conf1_read(unsigned int seg
, unsigned int bus
,
18 unsigned int devfn
, int reg
, int len
, u32
*value
)
22 if ((bus
> 255) || (devfn
> 255) || (reg
> 255)) {
27 spin_lock_irqsave(&pci_config_lock
, flags
);
29 outl(PCI_CONF1_ADDRESS(bus
, devfn
, reg
), 0xCF8);
33 *value
= inb(0xCFC + (reg
& 3));
36 *value
= inw(0xCFC + (reg
& 2));
43 spin_unlock_irqrestore(&pci_config_lock
, flags
);
48 int pci_conf1_write(unsigned int seg
, unsigned int bus
,
49 unsigned int devfn
, int reg
, int len
, u32 value
)
53 if ((bus
> 255) || (devfn
> 255) || (reg
> 255))
56 spin_lock_irqsave(&pci_config_lock
, flags
);
58 outl(PCI_CONF1_ADDRESS(bus
, devfn
, reg
), 0xCF8);
62 outb((u8
)value
, 0xCFC + (reg
& 3));
65 outw((u16
)value
, 0xCFC + (reg
& 2));
68 outl((u32
)value
, 0xCFC);
72 spin_unlock_irqrestore(&pci_config_lock
, flags
);
77 #undef PCI_CONF1_ADDRESS
79 struct pci_raw_ops pci_direct_conf1
= {
80 .read
= pci_conf1_read
,
81 .write
= pci_conf1_write
,
86 * Functions for accessing PCI configuration space with type 2 accesses
89 #define PCI_CONF2_ADDRESS(dev, reg) (u16)(0xC000 | (dev << 8) | reg)
91 static int pci_conf2_read(unsigned int seg
, unsigned int bus
,
92 unsigned int devfn
, int reg
, int len
, u32
*value
)
97 if ((bus
> 255) || (devfn
> 255) || (reg
> 255)) {
102 dev
= PCI_SLOT(devfn
);
103 fn
= PCI_FUNC(devfn
);
106 return PCIBIOS_DEVICE_NOT_FOUND
;
108 spin_lock_irqsave(&pci_config_lock
, flags
);
110 outb((u8
)(0xF0 | (fn
<< 1)), 0xCF8);
111 outb((u8
)bus
, 0xCFA);
115 *value
= inb(PCI_CONF2_ADDRESS(dev
, reg
));
118 *value
= inw(PCI_CONF2_ADDRESS(dev
, reg
));
121 *value
= inl(PCI_CONF2_ADDRESS(dev
, reg
));
127 spin_unlock_irqrestore(&pci_config_lock
, flags
);
132 static int pci_conf2_write(unsigned int seg
, unsigned int bus
,
133 unsigned int devfn
, int reg
, int len
, u32 value
)
138 if ((bus
> 255) || (devfn
> 255) || (reg
> 255))
141 dev
= PCI_SLOT(devfn
);
142 fn
= PCI_FUNC(devfn
);
145 return PCIBIOS_DEVICE_NOT_FOUND
;
147 spin_lock_irqsave(&pci_config_lock
, flags
);
149 outb((u8
)(0xF0 | (fn
<< 1)), 0xCF8);
150 outb((u8
)bus
, 0xCFA);
154 outb((u8
)value
, PCI_CONF2_ADDRESS(dev
, reg
));
157 outw((u16
)value
, PCI_CONF2_ADDRESS(dev
, reg
));
160 outl((u32
)value
, PCI_CONF2_ADDRESS(dev
, reg
));
166 spin_unlock_irqrestore(&pci_config_lock
, flags
);
171 #undef PCI_CONF2_ADDRESS
173 static struct pci_raw_ops pci_direct_conf2
= {
174 .read
= pci_conf2_read
,
175 .write
= pci_conf2_write
,
180 * Before we decide to use direct hardware access mechanisms, we try to do some
181 * trivial checks to ensure it at least _seems_ to be working -- we just test
182 * whether bus 00 contains a host bridge (this is similar to checking
183 * techniques used in XFree86, but ours should be more reliable since we
184 * attempt to make use of direct access hints provided by the PCI BIOS).
186 * This should be close to trivial, but it isn't, because there are buggy
187 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
189 static int __init
pci_sanity_check(struct pci_raw_ops
*o
)
194 if (pci_probe
& PCI_NO_CHECKS
)
196 /* Assume Type 1 works for newer systems.
197 This handles machines that don't have anything on PCI Bus 0. */
198 if (dmi_get_year(DMI_BIOS_DATE
) >= 2001)
201 for (devfn
= 0; devfn
< 0x100; devfn
++) {
202 if (o
->read(0, 0, devfn
, PCI_CLASS_DEVICE
, 2, &x
))
204 if (x
== PCI_CLASS_BRIDGE_HOST
|| x
== PCI_CLASS_DISPLAY_VGA
)
207 if (o
->read(0, 0, devfn
, PCI_VENDOR_ID
, 2, &x
))
209 if (x
== PCI_VENDOR_ID_INTEL
|| x
== PCI_VENDOR_ID_COMPAQ
)
213 DBG(KERN_WARNING
"PCI: Sanity check failed\n");
217 static int __init
pci_check_type1(void)
223 local_irq_save(flags
);
227 outl(0x80000000, 0xCF8);
228 if (inl(0xCF8) == 0x80000000 && pci_sanity_check(&pci_direct_conf1
)) {
232 local_irq_restore(flags
);
237 static int __init
pci_check_type2(void)
242 local_irq_save(flags
);
247 if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00 &&
248 pci_sanity_check(&pci_direct_conf2
)) {
252 local_irq_restore(flags
);
257 void __init
pci_direct_init(int type
)
261 printk(KERN_INFO
"PCI: Using configuration type %d\n", type
);
263 raw_pci_ops
= &pci_direct_conf1
;
265 raw_pci_ops
= &pci_direct_conf2
;
268 int __init
pci_direct_probe(void)
270 struct resource
*region
, *region2
;
272 if ((pci_probe
& PCI_PROBE_CONF1
) == 0)
274 region
= request_region(0xCF8, 8, "PCI conf1");
278 if (pci_check_type1())
280 release_resource(region
);
283 if ((pci_probe
& PCI_PROBE_CONF2
) == 0)
285 region
= request_region(0xCF8, 4, "PCI conf2");
288 region2
= request_region(0xC000, 0x1000, "PCI conf2");
292 if (pci_check_type2()) {
293 printk(KERN_INFO
"PCI: Using configuration type 2\n");
294 raw_pci_ops
= &pci_direct_conf2
;
298 release_resource(region2
);
300 release_resource(region
);