blob: 74d91054db0969228e718cc8e2f7fa21d21063b1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * BRIEF MODULE DESCRIPTION
3 * Alchemy/AMD Au1x00 pci support.
4 *
5 * Copyright 2001,2002,2003 MontaVista Software Inc.
6 * Author: MontaVista Software, Inc.
7 * ppopov@mvista.com or source@mvista.com
8 *
9 * Support for all devices (greater than 16) added by David Gathright.
10 *
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 *
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 675 Mass Ave, Cambridge, MA 02139, USA.
30 */
31#include <linux/config.h>
32#include <linux/types.h>
33#include <linux/pci.h>
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/vmalloc.h>
37
38#include <asm/mach-au1x00/au1000.h>
39
40#undef DEBUG
41#ifdef DEBUG
42#define DBG(x...) printk(x)
43#else
44#define DBG(x...)
45#endif
46
47#define PCI_ACCESS_READ 0
48#define PCI_ACCESS_WRITE 1
49
50
51int (*board_pci_idsel)(unsigned int devsel, int assert);
52
53/* CP0 hazard avoidance. */
54#define BARRIER __asm__ __volatile__(".set noreorder\n\t" \
55 "nop; nop; nop; nop;\t" \
56 ".set reorder\n\t")
57
58void mod_wired_entry(int entry, unsigned long entrylo0,
59 unsigned long entrylo1, unsigned long entryhi,
60 unsigned long pagemask)
61{
62 unsigned long old_pagemask;
63 unsigned long old_ctx;
64
65 /* Save old context and create impossible VPN2 value */
66 old_ctx = read_c0_entryhi() & 0xff;
67 old_pagemask = read_c0_pagemask();
68 write_c0_index(entry);
69 BARRIER;
70 write_c0_pagemask(pagemask);
71 write_c0_entryhi(entryhi);
72 write_c0_entrylo0(entrylo0);
73 write_c0_entrylo1(entrylo1);
74 BARRIER;
75 tlb_write_indexed();
76 BARRIER;
77 write_c0_entryhi(old_ctx);
78 BARRIER;
79 write_c0_pagemask(old_pagemask);
80}
81
82struct vm_struct *pci_cfg_vm;
83static int pci_cfg_wired_entry;
84static int first_cfg = 1;
85unsigned long last_entryLo0, last_entryLo1;
86
87static int config_access(unsigned char access_type, struct pci_bus *bus,
88 unsigned int dev_fn, unsigned char where,
89 u32 * data)
90{
91#if defined( CONFIG_SOC_AU1500 ) || defined( CONFIG_SOC_AU1550 )
92 unsigned int device = PCI_SLOT(dev_fn);
93 unsigned int function = PCI_FUNC(dev_fn);
94 unsigned long offset, status;
95 unsigned long cfg_base;
96 unsigned long flags;
97 int error = PCIBIOS_SUCCESSFUL;
98 unsigned long entryLo0, entryLo1;
99
100 if (device > 19) {
101 *data = 0xffffffff;
102 return -1;
103 }
104
105 local_irq_save(flags);
106 au_writel(((0x2000 << 16) | (au_readl(Au1500_PCI_STATCMD) & 0xffff)),
107 Au1500_PCI_STATCMD);
108 au_sync_udelay(1);
109
110 /*
111 * We can't ioremap the entire pci config space because it's
112 * too large. Nor can we call ioremap dynamically because some
113 * device drivers use the pci config routines from within
114 * interrupt handlers and that becomes a problem in get_vm_area().
115 * We use one wired tlb to handle all config accesses for all
116 * busses. To improve performance, if the current device
117 * is the same as the last device accessed, we don't touch the
118 * tlb.
119 */
120 if (first_cfg) {
121 /* reserve a wired entry for pci config accesses */
122 first_cfg = 0;
123 pci_cfg_vm = get_vm_area(0x2000, 0);
124 if (!pci_cfg_vm)
125 panic (KERN_ERR "PCI unable to get vm area\n");
126 pci_cfg_wired_entry = read_c0_wired();
127 add_wired_entry(0, 0, (unsigned long)pci_cfg_vm->addr, PM_4K);
128 last_entryLo0 = last_entryLo1 = 0xffffffff;
129 }
130
Pete Popov13d1d732005-02-27 22:15:24 +0000131 /* Allow board vendors to implement their own off-chip idsel.
132 * If it doesn't succeed, may as well bail out at this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134 if (board_pci_idsel) {
135 if (board_pci_idsel(device, 1) == 0) {
136 *data = 0xffffffff;
137 local_irq_restore(flags);
138 return -1;
139 }
140 }
141
142 /* setup the config window */
143 if (bus->number == 0) {
144 cfg_base = ((1<<device)<<11);
145 } else {
146 cfg_base = 0x80000000 | (bus->number<<16) | (device<<11);
147 }
148
149 /* setup the lower bits of the 36 bit address */
150 offset = (function << 8) | (where & ~0x3);
151 /* pick up any address that falls below the page mask */
152 offset |= cfg_base & ~PAGE_MASK;
153
154 /* page boundary */
155 cfg_base = cfg_base & PAGE_MASK;
156
157 entryLo0 = (6 << 26) | (cfg_base >> 6) | (2 << 3) | 7;
158 entryLo1 = (6 << 26) | (cfg_base >> 6) | (0x1000 >> 6) | (2 << 3) | 7;
159
160 if ((entryLo0 != last_entryLo0) || (entryLo1 != last_entryLo1)) {
161 mod_wired_entry(pci_cfg_wired_entry, entryLo0, entryLo1,
162 (unsigned long)pci_cfg_vm->addr, PM_4K);
163 last_entryLo0 = entryLo0;
164 last_entryLo1 = entryLo1;
165 }
166
167 if (access_type == PCI_ACCESS_WRITE) {
168 au_writel(*data, (int)(pci_cfg_vm->addr + offset));
169 } else {
170 *data = au_readl((int)(pci_cfg_vm->addr + offset));
171 }
172 au_sync_udelay(2);
173
174 DBG("cfg_access %d bus->number %d dev %d at %x *data %x conf %x\n",
175 access_type, bus->number, device, where, *data, offset);
176
177 /* check master abort */
178 status = au_readl(Au1500_PCI_STATCMD);
179
180 if (status & (1<<29)) {
181 *data = 0xffffffff;
182 error = -1;
183 DBG("Au1x Master Abort\n");
184 } else if ((status >> 28) & 0xf) {
185 DBG("PCI ERR detected: status %x\n", status);
186 *data = 0xffffffff;
187 error = -1;
188 }
189
190 /* Take away the idsel.
191 */
192 if (board_pci_idsel) {
193 (void)board_pci_idsel(device, 0);
194 }
195
196 local_irq_restore(flags);
197 return error;
198#endif
199}
200
201static int read_config_byte(struct pci_bus *bus, unsigned int devfn,
202 int where, u8 * val)
203{
204 u32 data;
205 int ret;
206
207 ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
208 if (where & 1)
209 data >>= 8;
210 if (where & 2)
211 data >>= 16;
212 *val = data & 0xff;
213 return ret;
214}
215
216
217static int read_config_word(struct pci_bus *bus, unsigned int devfn,
218 int where, u16 * val)
219{
220 u32 data;
221 int ret;
222
223 ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
224 if (where & 2)
225 data >>= 16;
226 *val = data & 0xffff;
227 return ret;
228}
229
230static int read_config_dword(struct pci_bus *bus, unsigned int devfn,
231 int where, u32 * val)
232{
233 int ret;
234
235 ret = config_access(PCI_ACCESS_READ, bus, devfn, where, val);
236 return ret;
237}
238
239static int
240write_config_byte(struct pci_bus *bus, unsigned int devfn, int where,
241 u8 val)
242{
243 u32 data = 0;
244
245 if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
246 return -1;
247
248 data = (data & ~(0xff << ((where & 3) << 3))) |
249 (val << ((where & 3) << 3));
250
251 if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
252 return -1;
253
254 return PCIBIOS_SUCCESSFUL;
255}
256
257static int
258write_config_word(struct pci_bus *bus, unsigned int devfn, int where,
259 u16 val)
260{
261 u32 data = 0;
262
263 if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
264 return -1;
265
266 data = (data & ~(0xffff << ((where & 3) << 3))) |
267 (val << ((where & 3) << 3));
268
269 if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
270 return -1;
271
272
273 return PCIBIOS_SUCCESSFUL;
274}
275
276static int
277write_config_dword(struct pci_bus *bus, unsigned int devfn, int where,
278 u32 val)
279{
280 if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &val))
281 return -1;
282
283 return PCIBIOS_SUCCESSFUL;
284}
285
286static int config_read(struct pci_bus *bus, unsigned int devfn,
287 int where, int size, u32 * val)
288{
289 switch (size) {
290 case 1: {
291 u8 _val;
292 int rc = read_config_byte(bus, devfn, where, &_val);
293 *val = _val;
294 return rc;
295 }
296 case 2: {
297 u16 _val;
298 int rc = read_config_word(bus, devfn, where, &_val);
299 *val = _val;
300 return rc;
301 }
302 default:
303 return read_config_dword(bus, devfn, where, val);
304 }
305}
306
307static int config_write(struct pci_bus *bus, unsigned int devfn,
308 int where, int size, u32 val)
309{
310 switch (size) {
311 case 1:
312 return write_config_byte(bus, devfn, where, (u8) val);
313 case 2:
314 return write_config_word(bus, devfn, where, (u16) val);
315 default:
316 return write_config_dword(bus, devfn, where, val);
317 }
318}
319
320
321struct pci_ops au1x_pci_ops = {
322 config_read,
323 config_write
324};