blob: 8daae9b230ea93035919848f562636173a5d0d18 [file] [log] [blame]
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +01001/*
2 * arch/arm/plat-iop/pci.c
3 *
4 * PCI support for the Intel IOP32X and IOP33X processors
5 *
6 * Author: Rory Bolt <rorybolt@pacbell.net>
7 * Copyright (C) 2002 Rory Bolt
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/kernel.h>
15#include <linux/pci.h>
16#include <linux/slab.h>
17#include <linux/mm.h>
18#include <linux/init.h>
19#include <linux/ioport.h>
Russell Kingfced80c2008-09-06 12:10:45 +010020#include <linux/io.h>
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +010021#include <asm/irq.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040022#include <asm/signal.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010023#include <mach/hardware.h>
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +010024#include <asm/mach/pci.h>
25#include <asm/hardware/iop3xx.h>
26
27// #define DEBUG
28
29#ifdef DEBUG
30#define DBG(x...) printk(x)
31#else
32#define DBG(x...) do { } while (0)
33#endif
34
35/*
36 * This routine builds either a type0 or type1 configuration command. If the
37 * bus is on the 803xx then a type0 made, else a type1 is created.
38 */
39static u32 iop3xx_cfg_address(struct pci_bus *bus, int devfn, int where)
40{
41 struct pci_sys_data *sys = bus->sysdata;
42 u32 addr;
43
44 if (sys->busnr == bus->number)
45 addr = 1 << (PCI_SLOT(devfn) + 16) | (PCI_SLOT(devfn) << 11);
46 else
47 addr = bus->number << 16 | PCI_SLOT(devfn) << 11 | 1;
48
49 addr |= PCI_FUNC(devfn) << 8 | (where & ~3);
50
51 return addr;
52}
53
54/*
55 * This routine checks the status of the last configuration cycle. If an error
56 * was detected it returns a 1, else it returns a 0. The errors being checked
57 * are parity, master abort, target abort (master and target). These types of
Dan Williamse90ddd82007-05-02 17:59:44 +010058 * errors occur during a config cycle where there is no device, like during
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +010059 * the discovery stage.
60 */
61static int iop3xx_pci_status(void)
62{
63 unsigned int status;
64 int ret = 0;
65
66 /*
67 * Check the status registers.
68 */
69 status = *IOP3XX_ATUSR;
70 if (status & 0xf900) {
71 DBG("\t\t\tPCI: P0 - status = 0x%08x\n", status);
72 *IOP3XX_ATUSR = status & 0xf900;
73 ret = 1;
74 }
75
76 status = *IOP3XX_ATUISR;
77 if (status & 0x679f) {
78 DBG("\t\t\tPCI: P1 - status = 0x%08x\n", status);
79 *IOP3XX_ATUISR = status & 0x679f;
80 ret = 1;
81 }
82
83 return ret;
84}
85
86/*
87 * Simply write the address register and read the configuration
Simon Arlott6cbdc8c2007-05-11 20:40:30 +010088 * data. Note that the 4 nops ensure that we are able to handle
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +010089 * a delayed abort (in theory.)
90 */
Dan Williamsd73d8012007-05-15 01:03:36 +010091static u32 iop3xx_read(unsigned long addr)
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +010092{
93 u32 val;
94
95 __asm__ __volatile__(
96 "str %1, [%2]\n\t"
97 "ldr %0, [%3]\n\t"
98 "nop\n\t"
99 "nop\n\t"
100 "nop\n\t"
101 "nop\n\t"
102 : "=r" (val)
103 : "r" (addr), "r" (IOP3XX_OCCAR), "r" (IOP3XX_OCCDR));
104
105 return val;
106}
107
108/*
109 * The read routines must check the error status of the last configuration
110 * cycle. If there was an error, the routine returns all hex f's.
111 */
112static int
113iop3xx_read_config(struct pci_bus *bus, unsigned int devfn, int where,
114 int size, u32 *value)
115{
116 unsigned long addr = iop3xx_cfg_address(bus, devfn, where);
117 u32 val = iop3xx_read(addr) >> ((where & 3) * 8);
118
119 if (iop3xx_pci_status())
120 val = 0xffffffff;
121
122 *value = val;
123
124 return PCIBIOS_SUCCESSFUL;
125}
126
127static int
128iop3xx_write_config(struct pci_bus *bus, unsigned int devfn, int where,
129 int size, u32 value)
130{
131 unsigned long addr = iop3xx_cfg_address(bus, devfn, where);
132 u32 val;
133
134 if (size != 4) {
135 val = iop3xx_read(addr);
136 if (iop3xx_pci_status())
137 return PCIBIOS_SUCCESSFUL;
138
139 where = (where & 3) * 8;
140
141 if (size == 1)
142 val &= ~(0xff << where);
143 else
144 val &= ~(0xffff << where);
145
146 *IOP3XX_OCCDR = val | value << where;
147 } else {
148 asm volatile(
149 "str %1, [%2]\n\t"
150 "str %0, [%3]\n\t"
151 "nop\n\t"
152 "nop\n\t"
153 "nop\n\t"
154 "nop\n\t"
155 :
156 : "r" (value), "r" (addr),
157 "r" (IOP3XX_OCCAR), "r" (IOP3XX_OCCDR));
158 }
159
160 return PCIBIOS_SUCCESSFUL;
161}
162
Russell Kingc23bfc32012-03-10 12:49:16 +0000163struct pci_ops iop3xx_ops = {
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +0100164 .read = iop3xx_read_config,
165 .write = iop3xx_write_config,
166};
167
168/*
169 * When a PCI device does not exist during config cycles, the 80200 gets a
170 * bus error instead of returning 0xffffffff. This handler simply returns.
171 */
172static int
173iop3xx_pci_abort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
174{
175 DBG("PCI abort: address = 0x%08lx fsr = 0x%03x PC = 0x%08lx LR = 0x%08lx\n",
176 addr, fsr, regs->ARM_pc, regs->ARM_lr);
177
178 /*
179 * If it was an imprecise abort, then we need to correct the
180 * return address to be _after_ the instruction.
181 */
182 if (fsr & (1 << 10))
183 regs->ARM_pc += 4;
184
185 return 0;
186}
187
188int iop3xx_pci_setup(int nr, struct pci_sys_data *sys)
189{
190 struct resource *res;
191
192 if (nr != 0)
193 return 0;
194
195 res = kzalloc(2 * sizeof(struct resource), GFP_KERNEL);
196 if (!res)
197 panic("PCI: unable to alloc resources");
198
Dan Williams6df26702007-02-13 17:11:04 +0100199 res[0].start = IOP3XX_PCI_LOWER_IO_PA;
200 res[0].end = IOP3XX_PCI_LOWER_IO_PA + IOP3XX_PCI_IO_WINDOW_SIZE - 1;
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +0100201 res[0].name = "IOP3XX PCI I/O Space";
202 res[0].flags = IORESOURCE_IO;
203 request_resource(&ioport_resource, &res[0]);
204
205 res[1].start = IOP3XX_PCI_LOWER_MEM_PA;
206 res[1].end = IOP3XX_PCI_LOWER_MEM_PA + IOP3XX_PCI_MEM_WINDOW_SIZE - 1;
207 res[1].name = "IOP3XX PCI Memory Space";
208 res[1].flags = IORESOURCE_MEM;
209 request_resource(&iomem_resource, &res[1]);
210
Russell King27eedbf2008-03-26 18:44:58 -0700211 /*
212 * Use whatever translation is already setup.
213 */
214 sys->mem_offset = IOP3XX_PCI_LOWER_MEM_PA - *IOP3XX_OMWTVR0;
215 sys->io_offset = IOP3XX_PCI_LOWER_IO_PA - *IOP3XX_OIOWTVR;
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +0100216
Bjorn Helgaas9f786d02012-02-23 20:19:01 -0700217 pci_add_resource_offset(&sys->resources, &res[0], sys->io_offset);
218 pci_add_resource_offset(&sys->resources, &res[1], sys->mem_offset);
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +0100219
220 return 1;
221}
222
Dan Williamse90ddd82007-05-02 17:59:44 +0100223void __init iop3xx_atu_setup(void)
224{
225 /* BAR 0 ( Disabled ) */
226 *IOP3XX_IAUBAR0 = 0x0;
227 *IOP3XX_IABAR0 = 0x0;
228 *IOP3XX_IATVR0 = 0x0;
229 *IOP3XX_IALR0 = 0x0;
230
231 /* BAR 1 ( Disabled ) */
232 *IOP3XX_IAUBAR1 = 0x0;
233 *IOP3XX_IABAR1 = 0x0;
234 *IOP3XX_IALR1 = 0x0;
235
236 /* BAR 2 (1:1 mapping with Physical RAM) */
237 /* Set limit and enable */
238 *IOP3XX_IALR2 = ~((u32)IOP3XX_MAX_RAM_SIZE - 1) & ~0x1;
239 *IOP3XX_IAUBAR2 = 0x0;
240
241 /* Align the inbound bar with the base of memory */
242 *IOP3XX_IABAR2 = PHYS_OFFSET |
243 PCI_BASE_ADDRESS_MEM_TYPE_64 |
244 PCI_BASE_ADDRESS_MEM_PREFETCH;
245
246 *IOP3XX_IATVR2 = PHYS_OFFSET;
247
248 /* Outbound window 0 */
Russell King97c46042008-03-26 18:46:42 -0700249 *IOP3XX_OMWTVR0 = IOP3XX_PCI_LOWER_MEM_BA;
Dan Williamse90ddd82007-05-02 17:59:44 +0100250 *IOP3XX_OUMWTVR0 = 0;
251
252 /* Outbound window 1 */
Aaro Koskinen5b9eda32009-07-31 12:16:21 +0300253 *IOP3XX_OMWTVR1 = IOP3XX_PCI_LOWER_MEM_BA +
254 IOP3XX_PCI_MEM_WINDOW_SIZE / 2;
Dan Williamse90ddd82007-05-02 17:59:44 +0100255 *IOP3XX_OUMWTVR1 = 0;
256
257 /* BAR 3 ( Disabled ) */
258 *IOP3XX_IAUBAR3 = 0x0;
259 *IOP3XX_IABAR3 = 0x0;
260 *IOP3XX_IATVR3 = 0x0;
261 *IOP3XX_IALR3 = 0x0;
262
263 /* Setup the I/O Bar
264 */
Russell King97c46042008-03-26 18:46:42 -0700265 *IOP3XX_OIOWTVR = IOP3XX_PCI_LOWER_IO_BA;
Dan Williamse90ddd82007-05-02 17:59:44 +0100266
267 /* Enable inbound and outbound cycles
268 */
269 *IOP3XX_ATUCMD |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
270 PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
271 *IOP3XX_ATUCR |= IOP3XX_ATUCR_OUT_EN;
272}
273
274void __init iop3xx_atu_disable(void)
275{
276 *IOP3XX_ATUCMD = 0;
277 *IOP3XX_ATUCR = 0;
278
279 /* wait for cycles to quiesce */
280 while (*IOP3XX_PCSR & (IOP3XX_PCSR_OUT_Q_BUSY |
281 IOP3XX_PCSR_IN_Q_BUSY))
282 cpu_relax();
283
284 /* BAR 0 ( Disabled ) */
285 *IOP3XX_IAUBAR0 = 0x0;
286 *IOP3XX_IABAR0 = 0x0;
287 *IOP3XX_IATVR0 = 0x0;
288 *IOP3XX_IALR0 = 0x0;
289
290 /* BAR 1 ( Disabled ) */
291 *IOP3XX_IAUBAR1 = 0x0;
292 *IOP3XX_IABAR1 = 0x0;
293 *IOP3XX_IALR1 = 0x0;
294
295 /* BAR 2 ( Disabled ) */
296 *IOP3XX_IAUBAR2 = 0x0;
297 *IOP3XX_IABAR2 = 0x0;
298 *IOP3XX_IATVR2 = 0x0;
299 *IOP3XX_IALR2 = 0x0;
300
301 /* BAR 3 ( Disabled ) */
302 *IOP3XX_IAUBAR3 = 0x0;
303 *IOP3XX_IABAR3 = 0x0;
304 *IOP3XX_IATVR3 = 0x0;
305 *IOP3XX_IALR3 = 0x0;
306
307 /* Clear the outbound windows */
308 *IOP3XX_OIOWTVR = 0;
309
310 /* Outbound window 0 */
311 *IOP3XX_OMWTVR0 = 0;
312 *IOP3XX_OUMWTVR0 = 0;
313
314 /* Outbound window 1 */
315 *IOP3XX_OMWTVR1 = 0;
316 *IOP3XX_OUMWTVR1 = 0;
317}
318
319/* Flag to determine whether the ATU is initialized and the PCI bus scanned */
320int init_atu;
321
Dan Williamsc34002c2008-03-26 19:12:38 -0700322int iop3xx_get_init_atu(void) {
323 /* check if default has been overridden */
324 if (init_atu != IOP3XX_INIT_ATU_DEFAULT)
325 return init_atu;
326 else
327 return IOP3XX_INIT_ATU_DISABLE;
328}
Dan Williamse90ddd82007-05-02 17:59:44 +0100329
Dan Williamsc34002c2008-03-26 19:12:38 -0700330static void __init iop3xx_atu_debug(void)
331{
Russell Kingc3a1c9c2008-03-26 18:42:10 -0700332 DBG("PCI: Intel IOP3xx PCI init.\n");
333 DBG("PCI: Outbound memory window 0: PCI 0x%08x%08x\n",
334 *IOP3XX_OUMWTVR0, *IOP3XX_OMWTVR0);
335 DBG("PCI: Outbound memory window 1: PCI 0x%08x%08x\n",
336 *IOP3XX_OUMWTVR1, *IOP3XX_OMWTVR1);
337 DBG("PCI: Outbound IO window: PCI 0x%08x\n",
338 *IOP3XX_OIOWTVR);
339
340 DBG("PCI: Inbound memory window 0: PCI 0x%08x%08x 0x%08x -> 0x%08x\n",
341 *IOP3XX_IAUBAR0, *IOP3XX_IABAR0, *IOP3XX_IALR0, *IOP3XX_IATVR0);
342 DBG("PCI: Inbound memory window 1: PCI 0x%08x%08x 0x%08x\n",
343 *IOP3XX_IAUBAR1, *IOP3XX_IABAR1, *IOP3XX_IALR1);
344 DBG("PCI: Inbound memory window 2: PCI 0x%08x%08x 0x%08x -> 0x%08x\n",
345 *IOP3XX_IAUBAR2, *IOP3XX_IABAR2, *IOP3XX_IALR2, *IOP3XX_IATVR2);
346 DBG("PCI: Inbound memory window 3: PCI 0x%08x%08x 0x%08x -> 0x%08x\n",
347 *IOP3XX_IAUBAR3, *IOP3XX_IABAR3, *IOP3XX_IALR3, *IOP3XX_IATVR3);
348
349 DBG("PCI: Expansion ROM window: PCI 0x%08x%08x 0x%08x -> 0x%08x\n",
350 0, *IOP3XX_ERBAR, *IOP3XX_ERLR, *IOP3XX_ERTVR);
351
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +0100352 DBG("ATU: IOP3XX_ATUCMD=0x%04x\n", *IOP3XX_ATUCMD);
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +0100353 DBG("ATU: IOP3XX_ATUCR=0x%08x\n", *IOP3XX_ATUCR);
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +0100354
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +0100355 hook_fault_code(16+6, iop3xx_pci_abort, SIGBUS, 0, "imprecise external abort");
Lennert Buytenhek0cb015f2006-09-18 23:16:23 +0100356}
Dan Williamse90ddd82007-05-02 17:59:44 +0100357
Dan Williamsc34002c2008-03-26 19:12:38 -0700358/* for platforms that might be host-bus-adapters */
359void __init iop3xx_pci_preinit_cond(void)
360{
361 if (iop3xx_get_init_atu() == IOP3XX_INIT_ATU_ENABLE) {
362 iop3xx_atu_disable();
363 iop3xx_atu_setup();
364 iop3xx_atu_debug();
365 }
366}
367
368void __init iop3xx_pci_preinit(void)
369{
Rob Herringc9d95fb2011-06-28 21:16:13 -0500370 pcibios_min_io = 0;
371 pcibios_min_mem = 0;
372
Dan Williamsc34002c2008-03-26 19:12:38 -0700373 iop3xx_atu_disable();
374 iop3xx_atu_setup();
375 iop3xx_atu_debug();
376}
377
Dan Williamse90ddd82007-05-02 17:59:44 +0100378/* allow init_atu to be user overridden */
379static int __init iop3xx_init_atu_setup(char *str)
380{
381 init_atu = IOP3XX_INIT_ATU_DEFAULT;
382 if (str) {
383 while (*str != '\0') {
384 switch (*str) {
385 case 'y':
386 case 'Y':
387 init_atu = IOP3XX_INIT_ATU_ENABLE;
388 break;
389 case 'n':
390 case 'N':
391 init_atu = IOP3XX_INIT_ATU_DISABLE;
392 break;
393 case ',':
394 case '=':
395 break;
396 default:
397 printk(KERN_DEBUG "\"%s\" malformed at "
398 "character: \'%c\'",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800399 __func__,
Dan Williamse90ddd82007-05-02 17:59:44 +0100400 *str);
401 *(str + 1) = '\0';
402 }
403 str++;
404 }
405 }
406
407 return 1;
408}
409
410__setup("iop3xx_init_atu", iop3xx_init_atu_setup);
411