3 * Most 8xx boards use the QSpan PCI bridge. The config address register
4 * is located 0x500 from the base of the bridge control/status registers.
5 * The data register is located at 0x504.
6 * This is a two step operation. First, the address register is written,
7 * then the data register is read/written as required.
8 * I don't know what to do about interrupts (yet).
10 * The RPX Classic implementation shares a chip select for normal
11 * PCI access and QSpan control register addresses. The selection is
12 * further selected by a bit setting in a board control register.
13 * Although it should happen, we disable interrupts during this operation
14 * to make sure some driver doesn't accidentally access the PCI while
15 * we have switched the chip select.
18 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/pci.h>
21 #include <linux/delay.h>
22 #include <linux/string.h>
23 #include <linux/init.h>
26 #include <asm/mpc8xx.h>
27 #include <asm/system.h>
28 #include <asm/machdep.h>
29 #include <asm/pci-bridge.h>
34 * When reading the configuration space, if something does not respond
35 * the bus times out and we get a machine check interrupt. So, the
36 * good ol' exception tables come to mind to trap it and return some
39 * On an error we just return a -1, since that is what the caller wants
40 * returned if nothing is present. I copied this from __get_user_asm,
41 * with the only difference of returning -1 instead of EFAULT.
42 * There is an associated hack in the machine check trap code.
44 * The QSPAN is also a big endian device, that is it makes the PCI
45 * look big endian to us. This presents a problem for the Linux PCI
46 * functions, which assume little endian. For example, we see the
47 * first 32-bit word like this:
48 * ------------------------
49 * | Device ID | Vendor ID |
50 * ------------------------
51 * If we read/write as a double word, that's OK. But in our world,
52 * when read as a word, device ID is at location 0, not location 2 as
53 * the little endian PCI would believe. We have to switch bits in
54 * the PCI addresses given to us to get the data to/from the correct
57 * The QSPAN only supports 4 bits of "slot" in the dev_fn instead of 5.
58 * It always forces the MS bit to zero. Therefore, dev_fn values
59 * greater than 128 are returned as "no device found" errors.
61 * The QSPAN can only perform long word (32-bit) configuration cycles.
62 * The "offset" must have the two LS bits set to zero. Read operations
63 * require we read the entire word and then sort out what should be
64 * returned. Write operations other than long word require that we
65 * read the long word, update the proper word or byte, then write the
66 * entire long word back.
68 * PCI Bridge hack. We assume (correctly) that bus 0 is the primary
69 * PCI bus from the QSPAN. If we are called with a bus number other
70 * than zero, we create a Type 1 configuration access that a downstream
71 * PCI bridge will interpret.
74 #define __get_qspan_pci_config(x, addr, op) \
75 __asm__ __volatile__( \
76 "1: "op" %0,0(%1)\n" \
79 ".section .fixup,\"ax\"\n" \
82 ".section __ex_table,\"a\"\n" \
86 : "=r"(x) : "r"(addr) : " %0")
88 #define QS_CONFIG_ADDR ((volatile uint *)(PCI_CSR_ADDR + 0x500))
89 #define QS_CONFIG_DATA ((volatile uint *)(PCI_CSR_ADDR + 0x504))
91 #define mk_config_addr(bus, dev, offset) \
92 (((bus)<<16) | ((dev)<<8) | (offset & 0xfc))
94 #define mk_config_type1(bus, dev, offset) \
95 mk_config_addr(bus, dev, offset) | 1;
97 static DEFINE_SPINLOCK(pcibios_lock
);
99 int qspan_pcibios_read_config_byte(unsigned char bus
, unsigned char dev_fn
,
100 unsigned char offset
, unsigned char *val
)
104 #ifdef CONFIG_RPXCLASSIC
108 if ((bus
> 7) || (dev_fn
> 127)) {
110 return PCIBIOS_DEVICE_NOT_FOUND
;
113 #ifdef CONFIG_RPXCLASSIC
114 /* disable interrupts */
115 spin_lock_irqsave(&pcibios_lock
, flags
);
116 *((uint
*)RPX_CSR_ADDR
) &= ~BCSR2_QSPACESEL
;
121 *QS_CONFIG_ADDR
= mk_config_addr(bus
, dev_fn
, offset
);
123 *QS_CONFIG_ADDR
= mk_config_type1(bus
, dev_fn
, offset
);
124 __get_qspan_pci_config(temp
, QS_CONFIG_DATA
, "lwz");
126 #ifdef CONFIG_RPXCLASSIC
127 *((uint
*)RPX_CSR_ADDR
) |= BCSR2_QSPACESEL
;
129 spin_unlock_irqrestore(&pcibios_lock
, flags
);
133 cp
= ((u_char
*)&temp
) + (offset
& 0x03);
135 return PCIBIOS_SUCCESSFUL
;
138 int qspan_pcibios_read_config_word(unsigned char bus
, unsigned char dev_fn
,
139 unsigned char offset
, unsigned short *val
)
143 #ifdef CONFIG_RPXCLASSIC
147 if ((bus
> 7) || (dev_fn
> 127)) {
149 return PCIBIOS_DEVICE_NOT_FOUND
;
152 #ifdef CONFIG_RPXCLASSIC
153 /* disable interrupts */
154 spin_lock_irqsave(&pcibios_lock
, flags
);
155 *((uint
*)RPX_CSR_ADDR
) &= ~BCSR2_QSPACESEL
;
160 *QS_CONFIG_ADDR
= mk_config_addr(bus
, dev_fn
, offset
);
162 *QS_CONFIG_ADDR
= mk_config_type1(bus
, dev_fn
, offset
);
163 __get_qspan_pci_config(temp
, QS_CONFIG_DATA
, "lwz");
166 #ifdef CONFIG_RPXCLASSIC
167 *((uint
*)RPX_CSR_ADDR
) |= BCSR2_QSPACESEL
;
169 spin_unlock_irqrestore(&pcibios_lock
, flags
);
172 sp
= ((ushort
*)&temp
) + ((offset
>> 1) & 1);
174 return PCIBIOS_SUCCESSFUL
;
177 int qspan_pcibios_read_config_dword(unsigned char bus
, unsigned char dev_fn
,
178 unsigned char offset
, unsigned int *val
)
180 #ifdef CONFIG_RPXCLASSIC
184 if ((bus
> 7) || (dev_fn
> 127)) {
186 return PCIBIOS_DEVICE_NOT_FOUND
;
189 #ifdef CONFIG_RPXCLASSIC
190 /* disable interrupts */
191 spin_lock_irqsave(&pcibios_lock
, flags
);
192 *((uint
*)RPX_CSR_ADDR
) &= ~BCSR2_QSPACESEL
;
197 *QS_CONFIG_ADDR
= mk_config_addr(bus
, dev_fn
, offset
);
199 *QS_CONFIG_ADDR
= mk_config_type1(bus
, dev_fn
, offset
);
200 __get_qspan_pci_config(*val
, QS_CONFIG_DATA
, "lwz");
202 #ifdef CONFIG_RPXCLASSIC
203 *((uint
*)RPX_CSR_ADDR
) |= BCSR2_QSPACESEL
;
205 spin_unlock_irqrestore(&pcibios_lock
, flags
);
208 return PCIBIOS_SUCCESSFUL
;
211 int qspan_pcibios_write_config_byte(unsigned char bus
, unsigned char dev_fn
,
212 unsigned char offset
, unsigned char val
)
216 #ifdef CONFIG_RPXCLASSIC
220 if ((bus
> 7) || (dev_fn
> 127))
221 return PCIBIOS_DEVICE_NOT_FOUND
;
223 qspan_pcibios_read_config_dword(bus
, dev_fn
, offset
, &temp
);
226 cp
= ((u_char
*)&temp
) + (offset
& 0x03);
229 #ifdef CONFIG_RPXCLASSIC
230 /* disable interrupts */
231 spin_lock_irqsave(&pcibios_lock
, flags
);
232 *((uint
*)RPX_CSR_ADDR
) &= ~BCSR2_QSPACESEL
;
237 *QS_CONFIG_ADDR
= mk_config_addr(bus
, dev_fn
, offset
);
239 *QS_CONFIG_ADDR
= mk_config_type1(bus
, dev_fn
, offset
);
240 *QS_CONFIG_DATA
= temp
;
242 #ifdef CONFIG_RPXCLASSIC
243 *((uint
*)RPX_CSR_ADDR
) |= BCSR2_QSPACESEL
;
245 spin_unlock_irqrestore(&pcibios_lock
, flags
);
248 return PCIBIOS_SUCCESSFUL
;
251 int qspan_pcibios_write_config_word(unsigned char bus
, unsigned char dev_fn
,
252 unsigned char offset
, unsigned short val
)
256 #ifdef CONFIG_RPXCLASSIC
260 if ((bus
> 7) || (dev_fn
> 127))
261 return PCIBIOS_DEVICE_NOT_FOUND
;
263 qspan_pcibios_read_config_dword(bus
, dev_fn
, offset
, &temp
);
266 sp
= ((ushort
*)&temp
) + ((offset
>> 1) & 1);
269 #ifdef CONFIG_RPXCLASSIC
270 /* disable interrupts */
271 spin_lock_irqsave(&pcibios_lock
, flags
);
272 *((uint
*)RPX_CSR_ADDR
) &= ~BCSR2_QSPACESEL
;
277 *QS_CONFIG_ADDR
= mk_config_addr(bus
, dev_fn
, offset
);
279 *QS_CONFIG_ADDR
= mk_config_type1(bus
, dev_fn
, offset
);
280 *QS_CONFIG_DATA
= temp
;
282 #ifdef CONFIG_RPXCLASSIC
283 *((uint
*)RPX_CSR_ADDR
) |= BCSR2_QSPACESEL
;
285 spin_unlock_irqrestore(&pcibios_lock
, flags
);
288 return PCIBIOS_SUCCESSFUL
;
291 int qspan_pcibios_write_config_dword(unsigned char bus
, unsigned char dev_fn
,
292 unsigned char offset
, unsigned int val
)
294 #ifdef CONFIG_RPXCLASSIC
298 if ((bus
> 7) || (dev_fn
> 127))
299 return PCIBIOS_DEVICE_NOT_FOUND
;
301 #ifdef CONFIG_RPXCLASSIC
302 /* disable interrupts */
303 spin_lock_irqsave(&pcibios_lock
, flags
);
304 *((uint
*)RPX_CSR_ADDR
) &= ~BCSR2_QSPACESEL
;
309 *QS_CONFIG_ADDR
= mk_config_addr(bus
, dev_fn
, offset
);
311 *QS_CONFIG_ADDR
= mk_config_type1(bus
, dev_fn
, offset
);
312 *(unsigned int *)QS_CONFIG_DATA
= val
;
314 #ifdef CONFIG_RPXCLASSIC
315 *((uint
*)RPX_CSR_ADDR
) |= BCSR2_QSPACESEL
;
317 spin_unlock_irqrestore(&pcibios_lock
, flags
);
320 return PCIBIOS_SUCCESSFUL
;
323 int qspan_pcibios_find_device(unsigned short vendor
, unsigned short dev_id
,
324 unsigned short index
, unsigned char *bus_ptr
,
325 unsigned char *dev_fn_ptr
)
328 unsigned int x
, vendev
;
330 if (vendor
== 0xffff)
331 return PCIBIOS_BAD_VENDOR_ID
;
332 vendev
= (dev_id
<< 16) + vendor
;
334 for (devfn
= 0; devfn
< 32; devfn
++) {
335 qspan_pcibios_read_config_dword(0, devfn
<<3, PCI_VENDOR_ID
, &x
);
339 *dev_fn_ptr
= devfn
<<3;
340 return PCIBIOS_SUCCESSFUL
;
345 return PCIBIOS_DEVICE_NOT_FOUND
;
348 int qspan_pcibios_find_class(unsigned int class_code
, unsigned short index
,
349 unsigned char *bus_ptr
, unsigned char *dev_fn_ptr
)
354 for (devnr
= 0; devnr
< 32; devnr
++) {
355 qspan_pcibios_read_config_dword(0, devnr
<<3, PCI_CLASS_REVISION
, &x
);
356 if ((x
>>8) == class_code
) {
359 *dev_fn_ptr
= devnr
<<3;
360 return PCIBIOS_SUCCESSFUL
;
365 return PCIBIOS_DEVICE_NOT_FOUND
;
369 m8xx_pcibios_fixup(void))
371 /* Lots to do here, all board and configuration specific. */
375 m8xx_setup_pci_ptrs(void))
377 set_config_access_method(qspan
);
379 ppc_md
.pcibios_fixup
= m8xx_pcibios_fixup
;