2 * Copyright 2001 MontaVista Software Inc.
3 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
5 * arch/mips/ddb5xxx/ddb5476/pci_ops.c
6 * Define the pci_ops for DB5477.
8 * Much of the code is derived from the original DDB5074 port by
9 * Geert Uytterhoeven <geert@sonycom.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/config.h>
18 #include <linux/pci.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
22 #include <asm/addrspace.h>
23 #include <asm/debug.h>
25 #include <asm/ddb5xxx/ddb5xxx.h>
28 * config_swap structure records what set of pdar/pmr are used
29 * to access pci config space. It also provides a place hold the
30 * original values for future restoring.
32 struct pci_config_swap
{
42 * On DDB5476, we have one set of swap registers
44 struct pci_config_swap ext_pci_swap
= {
51 static int pci_config_workaround
= 1;
56 static inline u32
ddb_access_config_base(struct pci_config_swap
*swap
, u32 bus
, /* 0 means top level bus */
60 u32 pciinit_offset
= 0;
61 u32 virt_addr
= swap
->config_base
;
64 if (pci_config_workaround
) {
65 /* [jsun] work around Vrc5476 controller itself, returnning
66 * slot 0 essentially makes vrc5476 invisible
72 /* BUG : skip P2P bridge for now */
78 /* now we have to be hornest, returning the true
79 * PCI config headers for vrc5476
82 swap
->pdar_backup
= ddb_in32(swap
->pdar
);
83 swap
->pmr_backup
= ddb_in32(swap
->pmr
);
84 return DDB_BASE
+ DDB_PCI_BASE
;
88 /* minimum pdar (window) size is 2MB */
89 db_assert(swap
->config_size
>= (2 << 20));
91 db_assert(slot_num
< (1 << 5));
92 db_assert(bus
< (1 << 8));
94 /* backup registers */
95 swap
->pdar_backup
= ddb_in32(swap
->pdar
);
96 swap
->pmr_backup
= ddb_in32(swap
->pmr
);
98 /* set the pdar (pci window) register */
99 ddb_set_pdar(swap
->pdar
, swap
->config_base
, swap
->config_size
, 32, /* 32 bit wide */
100 0, /* not on local memory bus */
101 0); /* not visible from PCI bus (N/A) */
104 * calcuate the absolute pci config addr;
105 * according to the spec, we start scanning from adr:11 (0x800)
109 pci_addr
= 0x800 << slot_num
;
112 pci_addr
= (bus
<< 16) | (slot_num
<< 11);
113 /* panic("ddb_access_config_base: we don't support type 1 config Yet"); */
117 * if pci_addr is less than pci config window size, we set
118 * pciinit_offset to 0 and adjust the virt_address.
119 * Otherwise we will try to adjust pciinit_offset.
121 if (pci_addr
< swap
->config_size
) {
122 virt_addr
= KSEG1ADDR(swap
->config_base
+ pci_addr
);
125 db_assert((pci_addr
& (swap
->config_size
- 1)) == 0);
126 virt_addr
= KSEG1ADDR(swap
->config_base
);
127 pciinit_offset
= pci_addr
;
130 /* set the pmr register */
131 option
= DDB_PCI_ACCESS_32
;
133 option
|= DDB_PCI_CFGTYPE1
;
134 ddb_set_pmr(swap
->pmr
, DDB_PCICMD_CFG
, pciinit_offset
, option
);
139 static inline void ddb_close_config_base(struct pci_config_swap
*swap
)
141 ddb_out32(swap
->pdar
, swap
->pdar_backup
);
142 ddb_out32(swap
->pmr
, swap
->pmr_backup
);
145 static int read_config_dword(struct pci_config_swap
*swap
,
146 struct pci_dev
*dev
, u32 where
, u32
* val
)
148 u32 bus
, slot_num
, func_num
;
151 db_assert((where
& 3) == 0);
152 db_assert(where
< (1 << 8));
154 /* check if the bus is top-level */
155 if (dev
->bus
->parent
!= NULL
) {
156 bus
= dev
->bus
->number
;
162 slot_num
= PCI_SLOT(dev
->devfn
);
163 func_num
= PCI_FUNC(dev
->devfn
);
164 base
= ddb_access_config_base(swap
, bus
, slot_num
);
165 *val
= *(volatile u32
*) (base
+ (func_num
<< 8) + where
);
166 ddb_close_config_base(swap
);
167 return PCIBIOS_SUCCESSFUL
;
170 static int read_config_word(struct pci_config_swap
*swap
,
171 struct pci_dev
*dev
, u32 where
, u16
* val
)
176 db_assert((where
& 1) == 0);
178 status
= read_config_dword(swap
, dev
, where
& ~3, &result
);
181 *val
= result
& 0xffff;
185 static int read_config_byte(struct pci_config_swap
*swap
,
186 struct pci_dev
*dev
, u32 where
, u8
* val
)
191 status
= read_config_dword(swap
, dev
, where
& ~3, &result
);
196 *val
= result
& 0xff;
200 static int write_config_dword(struct pci_config_swap
*swap
,
201 struct pci_dev
*dev
, u32 where
, u32 val
)
203 u32 bus
, slot_num
, func_num
;
206 db_assert((where
& 3) == 0);
207 db_assert(where
< (1 << 8));
209 /* check if the bus is top-level */
210 if (dev
->bus
->parent
!= NULL
) {
211 bus
= dev
->bus
->number
;
217 slot_num
= PCI_SLOT(dev
->devfn
);
218 func_num
= PCI_FUNC(dev
->devfn
);
219 base
= ddb_access_config_base(swap
, bus
, slot_num
);
220 *(volatile u32
*) (base
+ (func_num
<< 8) + where
) = val
;
221 ddb_close_config_base(swap
);
222 return PCIBIOS_SUCCESSFUL
;
225 static int write_config_word(struct pci_config_swap
*swap
,
226 struct pci_dev
*dev
, u32 where
, u16 val
)
228 int status
, shift
= 0;
231 db_assert((where
& 1) == 0);
233 status
= read_config_dword(swap
, dev
, where
& ~3, &result
);
234 if (status
!= PCIBIOS_SUCCESSFUL
)
239 result
&= ~(0xffff << shift
);
240 result
|= val
<< shift
;
241 return write_config_dword(swap
, dev
, where
& ~3, result
);
244 static int write_config_byte(struct pci_config_swap
*swap
,
245 struct pci_dev
*dev
, u32 where
, u8 val
)
247 int status
, shift
= 0;
250 status
= read_config_dword(swap
, dev
, where
& ~3, &result
);
251 if (status
!= PCIBIOS_SUCCESSFUL
)
258 result
&= ~(0xff << shift
);
259 result
|= val
<< shift
;
260 return write_config_dword(swap
, dev
, where
& ~3, result
);
263 #define MAKE_PCI_OPS(prefix, rw, unitname, unittype, pciswap) \
264 static int prefix##_##rw##_config_##unitname(struct pci_dev *dev, int where, unittype val) \
266 return rw##_config_##unitname(pciswap, \
272 MAKE_PCI_OPS(extpci
, read
, byte
, u8
*, &ext_pci_swap
)
273 MAKE_PCI_OPS(extpci
, read
, word
, u16
*, &ext_pci_swap
)
274 MAKE_PCI_OPS(extpci
, read
, dword
, u32
*, &ext_pci_swap
)
276 MAKE_PCI_OPS(extpci
, write
, byte
, u8
, &ext_pci_swap
)
277 MAKE_PCI_OPS(extpci
, write
, word
, u16
, &ext_pci_swap
)
278 MAKE_PCI_OPS(extpci
, write
, dword
, u32
, &ext_pci_swap
)
280 struct pci_ops ddb5476_ext_pci_ops
= {
281 extpci_read_config_byte
,
282 extpci_read_config_word
,
283 extpci_read_config_dword
,
284 extpci_write_config_byte
,
285 extpci_write_config_word
,
286 extpci_write_config_dword
290 #if defined(CONFIG_RUNTIME_DEBUG)
291 void jsun_scan_pci_bus(void)
298 pci_config_workaround
= 0;
300 bus
.parent
= NULL
; /* we scan the top level only */
304 /* scan ext pci bus and io pci bus */
305 for (j
= 0; j
< 1; j
++) {
306 printk(KERN_INFO
"scan ddb5476 external PCI bus:\n");
307 bus
.ops
= &ddb5476_ext_pci_ops
;
309 for (devfn
= 0; devfn
< 0x100; devfn
+= 8) {
316 db_verify(pci_read_config_dword(&dev
, 0, &temp
),
317 == PCIBIOS_SUCCESSFUL
);
318 if (temp
== 0xffffffff)
321 printk(KERN_INFO
"slot %d: (addr %d) \n",
322 devfn
/ 8, 11 + devfn
/ 8);
324 /* verify read word and byte */
325 db_verify(pci_read_config_word(&dev
, 2, &temp16
),
326 == PCIBIOS_SUCCESSFUL
);
327 db_assert(temp16
== (temp
>> 16));
328 db_verify(pci_read_config_byte(&dev
, 3, &temp8
),
329 == PCIBIOS_SUCCESSFUL
);
330 db_assert(temp8
== (temp
>> 24));
331 db_verify(pci_read_config_byte(&dev
, 1, &temp8
),
332 == PCIBIOS_SUCCESSFUL
);
333 db_assert(temp8
== ((temp
>> 8) & 0xff));
335 for (i
= 0; i
< 16; i
++) {
338 db_verify(pci_read_config_dword
339 (&dev
, i
* 4, &temp
),
340 == PCIBIOS_SUCCESSFUL
);
341 printk("\t%08X", temp
);
348 pci_config_workaround
= 1;