1 /***********************************************************************
2 * Copyright 2001 MontaVista Software Inc.
3 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
5 * arch/mips/ddb5xxx/ddb5477/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.
15 ***********************************************************************
19 * DDB5477 has two PCI channels, external PCI and IOPIC (internal)
20 * Therefore we provide two sets of pci_ops.
22 #include <linux/pci.h>
23 #include <linux/kernel.h>
24 #include <linux/types.h>
26 #include <asm/addrspace.h>
27 #include <asm/debug.h>
29 #include <asm/ddb5xxx/ddb5xxx.h>
32 * config_swap structure records what set of pdar/pmr are used
33 * to access pci config space. It also provides a place hold the
34 * original values for future restoring.
36 struct pci_config_swap
{
46 * On DDB5477, we have two sets of swap registers, for ext PCI and IOPCI.
48 struct pci_config_swap ext_pci_swap
= {
54 struct pci_config_swap io_pci_swap
= {
65 static inline u32
ddb_access_config_base(struct pci_config_swap
*swap
, u32 bus
, /* 0 means top level bus */
69 u32 pciinit_offset
= 0;
70 u32 virt_addr
= swap
->config_base
;
73 /* minimum pdar (window) size is 2MB */
74 db_assert(swap
->config_size
>= (2 << 20));
76 db_assert(slot_num
< (1 << 5));
77 db_assert(bus
< (1 << 8));
79 /* backup registers */
80 swap
->pdar_backup
= ddb_in32(swap
->pdar
);
81 swap
->pmr_backup
= ddb_in32(swap
->pmr
);
83 /* set the pdar (pci window) register */
84 ddb_set_pdar(swap
->pdar
, swap
->config_base
, swap
->config_size
, 32, /* 32 bit wide */
85 0, /* not on local memory bus */
86 0); /* not visible from PCI bus (N/A) */
89 * calcuate the absolute pci config addr;
90 * according to the spec, we start scanning from adr:11 (0x800)
94 pci_addr
= 0x800 << slot_num
;
97 pci_addr
= (bus
<< 16) | (slot_num
<< 11);
101 * if pci_addr is less than pci config window size, we set
102 * pciinit_offset to 0 and adjust the virt_address.
103 * Otherwise we will try to adjust pciinit_offset.
105 if (pci_addr
< swap
->config_size
) {
106 virt_addr
= KSEG1ADDR(swap
->config_base
+ pci_addr
);
109 db_assert((pci_addr
& (swap
->config_size
- 1)) == 0);
110 virt_addr
= KSEG1ADDR(swap
->config_base
);
111 pciinit_offset
= pci_addr
;
114 /* set the pmr register */
115 option
= DDB_PCI_ACCESS_32
;
117 option
|= DDB_PCI_CFGTYPE1
;
118 ddb_set_pmr(swap
->pmr
, DDB_PCICMD_CFG
, pciinit_offset
, option
);
123 static inline void ddb_close_config_base(struct pci_config_swap
*swap
)
125 ddb_out32(swap
->pdar
, swap
->pdar_backup
);
126 ddb_out32(swap
->pmr
, swap
->pmr_backup
);
129 static int read_config_dword(struct pci_config_swap
*swap
,
130 struct pci_bus
*bus
, u32 where
, u32
* val
)
132 u32 bus
, slot_num
, func_num
;
135 db_assert((where
& 3) == 0);
136 db_assert(where
< (1 << 8));
138 /* check if the bus is top-level */
139 if (dev
->bus
->parent
!= NULL
) {
140 bus
= dev
->bus
->number
;
146 slot_num
= PCI_SLOT(dev
->devfn
);
147 func_num
= PCI_FUNC(dev
->devfn
);
148 base
= ddb_access_config_base(swap
, bus
, slot_num
);
149 *val
= *(volatile u32
*) (base
+ (func_num
<< 8) + where
);
150 ddb_close_config_base(swap
);
151 return PCIBIOS_SUCCESSFUL
;
154 static int read_config_word(struct pci_config_swap
*swap
,
155 struct pci_bus
*bus
, u32 where
, u16
* val
)
160 db_assert((where
& 1) == 0);
162 status
= read_config_dword(swap
, bus
, where
& ~3, &result
);
165 *val
= result
& 0xffff;
169 static int read_config_byte(struct pci_config_swap
*swap
,
170 struct pci_bus
*bus
, unsigned int devfn
,
176 status
= read_config_dword(swap
, bus
, where
& ~3, &result
);
181 *val
= result
& 0xff;
186 static int write_config_dword(struct pci_config_swap
*swap
,
187 struct pci_bus
*bus
, unsigned int devfn
,
190 u32 busno
, slot_num
, func_num
;
193 db_assert((where
& 3) == 0);
194 db_assert(where
< (1 << 8));
196 /* check if the bus is top-level */
197 if (bus
->parent
!= NULL
) {
199 db_assert(busno
!= 0);
204 slot_num
= PCI_SLOT(devfn
);
205 func_num
= PCI_FUNC(devfn
);
206 base
= ddb_access_config_base(swap
, busno
, slot_num
);
207 *(volatile u32
*) (base
+ (func_num
<< 8) + where
) = val
;
208 ddb_close_config_base(swap
);
209 return PCIBIOS_SUCCESSFUL
;
212 static int write_config_word(struct pci_config_swap
*swap
,
213 struct pci_bus
*bus
, unsigned int devfn
,
216 int status
, shift
= 0;
219 db_assert((where
& 1) == 0);
221 status
= read_config_dword(swap
, dev
, where
& ~3, &result
);
222 if (status
!= PCIBIOS_SUCCESSFUL
)
227 result
&= ~(0xffff << shift
);
228 result
|= val
<< shift
;
229 return write_config_dword(swap
, dev
, where
& ~3, result
);
232 static int write_config_byte(struct pci_config_swap
*swap
,
233 struct pci_bus
*bus
, unsigned int devfn
,
236 int status
, shift
= 0;
239 status
= read_config_dword(swap
, dev
, where
& ~3, &result
);
240 if (status
!= PCIBIOS_SUCCESSFUL
)
247 result
&= ~(0xff << shift
);
248 result
|= val
<< shift
;
249 return write_config_dword(swap
, dev
, where
& ~3, result
);
253 * Dump solution for now so I don't break hw I can't test on ...
255 #define MAKE_PCI_OPS(prefix, rw, unitname, unittype, pciswap) \
256 static int prefix##_##rw##_config(struct pci_bus *bus, int where, int size, unittype val) \
259 return rw##_config_byte(pciswap, bus, where, val); \
260 else if (size == 2) \
261 return rw##_config_word(pciswap, bus, where, val); \
262 /* Size must be 4 */ \
263 return rw##_config_dword(pciswap, bus, where, val); \
266 MAKE_PCI_OPS(extpci
, read
, &ext_pci_swap
)
267 MAKE_PCI_OPS(extpci
, write
, &ext_pci_swap
)
269 MAKE_PCI_OPS(iopci
, read
, &io_pci_swap
)
270 MAKE_PCI_OPS(iopci
, write
, &io_pci_swap
)
272 struct pci_ops ddb5477_ext_pci_ops
= {
273 .read
= extpci_read_config
,
274 .write
= extpci_write_config
278 struct pci_ops ddb5477_io_pci_ops
= {
279 .read
= iopci_read_config
,
280 .write
= iopci_write_config