Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[pandora-kernel.git] / arch / arm / mach-footbridge / dc21285.c
1 /*
2  *  linux/arch/arm/kernel/dec21285.c: PCI functions for DC21285
3  *
4  *  Copyright (C) 1998-2001 Russell King
5  *  Copyright (C) 1998-2000 Phil Blundell
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <linux/ptrace.h>
14 #include <linux/interrupt.h>
15 #include <linux/mm.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/ioport.h>
19
20 #include <asm/io.h>
21 #include <asm/irq.h>
22 #include <asm/system.h>
23 #include <asm/mach/pci.h>
24 #include <asm/hardware/dec21285.h>
25
26 #define MAX_SLOTS               21
27
28 #define PCICMD_ABORT            ((PCI_STATUS_REC_MASTER_ABORT| \
29                                   PCI_STATUS_REC_TARGET_ABORT)<<16)
30
31 #define PCICMD_ERROR_BITS       ((PCI_STATUS_DETECTED_PARITY | \
32                                   PCI_STATUS_REC_MASTER_ABORT | \
33                                   PCI_STATUS_REC_TARGET_ABORT | \
34                                   PCI_STATUS_PARITY) << 16)
35
36 extern int setup_arm_irq(int, struct irqaction *);
37 extern void pcibios_report_status(u_int status_mask, int warn);
38
39 static unsigned long
40 dc21285_base_address(struct pci_bus *bus, unsigned int devfn)
41 {
42         unsigned long addr = 0;
43
44         if (bus->number == 0) {
45                 if (PCI_SLOT(devfn) == 0)
46                         /*
47                          * For devfn 0, point at the 21285
48                          */
49                         addr = ARMCSR_BASE;
50                 else {
51                         devfn -= 1 << 3;
52
53                         if (devfn < PCI_DEVFN(MAX_SLOTS, 0))
54                                 addr = PCICFG0_BASE | 0xc00000 | (devfn << 8);
55                 }
56         } else
57                 addr = PCICFG1_BASE | (bus->number << 16) | (devfn << 8);
58
59         return addr;
60 }
61
62 static int
63 dc21285_read_config(struct pci_bus *bus, unsigned int devfn, int where,
64                     int size, u32 *value)
65 {
66         unsigned long addr = dc21285_base_address(bus, devfn);
67         u32 v = 0xffffffff;
68
69         if (addr)
70                 switch (size) {
71                 case 1:
72                         asm("ldr%?b     %0, [%1, %2]"
73                                 : "=r" (v) : "r" (addr), "r" (where));
74                         break;
75                 case 2:
76                         asm("ldr%?h     %0, [%1, %2]"
77                                 : "=r" (v) : "r" (addr), "r" (where));
78                         break;
79                 case 4:
80                         asm("ldr%?      %0, [%1, %2]"
81                                 : "=r" (v) : "r" (addr), "r" (where));
82                         break;
83                 }
84
85         *value = v;
86
87         v = *CSR_PCICMD;
88         if (v & PCICMD_ABORT) {
89                 *CSR_PCICMD = v & (0xffff|PCICMD_ABORT);
90                 return -1;
91         }
92
93         return PCIBIOS_SUCCESSFUL;
94 }
95
96 static int
97 dc21285_write_config(struct pci_bus *bus, unsigned int devfn, int where,
98                      int size, u32 value)
99 {
100         unsigned long addr = dc21285_base_address(bus, devfn);
101         u32 v;
102
103         if (addr)
104                 switch (size) {
105                 case 1:
106                         asm("str%?b     %0, [%1, %2]"
107                                 : : "r" (value), "r" (addr), "r" (where));
108                         break;
109                 case 2:
110                         asm("str%?h     %0, [%1, %2]"
111                                 : : "r" (value), "r" (addr), "r" (where));
112                         break;
113                 case 4:
114                         asm("str%?      %0, [%1, %2]"
115                                 : : "r" (value), "r" (addr), "r" (where));
116                         break;
117                 }
118
119         v = *CSR_PCICMD;
120         if (v & PCICMD_ABORT) {
121                 *CSR_PCICMD = v & (0xffff|PCICMD_ABORT);
122                 return -1;
123         }
124
125         return PCIBIOS_SUCCESSFUL;
126 }
127
128 static struct pci_ops dc21285_ops = {
129         .read   = dc21285_read_config,
130         .write  = dc21285_write_config,
131 };
132
133 static struct timer_list serr_timer;
134 static struct timer_list perr_timer;
135
136 static void dc21285_enable_error(unsigned long __data)
137 {
138         switch (__data) {
139         case IRQ_PCI_SERR:
140                 del_timer(&serr_timer);
141                 break;
142
143         case IRQ_PCI_PERR:
144                 del_timer(&perr_timer);
145                 break;
146         }
147
148         enable_irq(__data);
149 }
150
151 /*
152  * Warn on PCI errors.
153  */
154 static irqreturn_t dc21285_abort_irq(int irq, void *dev_id, struct pt_regs *regs)
155 {
156         unsigned int cmd;
157         unsigned int status;
158
159         cmd = *CSR_PCICMD;
160         status = cmd >> 16;
161         cmd = cmd & 0xffff;
162
163         if (status & PCI_STATUS_REC_MASTER_ABORT) {
164                 printk(KERN_DEBUG "PCI: master abort, pc=0x%08lx\n",
165                         instruction_pointer(regs));
166                 cmd |= PCI_STATUS_REC_MASTER_ABORT << 16;
167         }
168
169         if (status & PCI_STATUS_REC_TARGET_ABORT) {
170                 printk(KERN_DEBUG "PCI: target abort: ");
171                 pcibios_report_status(PCI_STATUS_REC_MASTER_ABORT |
172                                       PCI_STATUS_SIG_TARGET_ABORT |
173                                       PCI_STATUS_REC_TARGET_ABORT, 1);
174                 printk("\n");
175
176                 cmd |= PCI_STATUS_REC_TARGET_ABORT << 16;
177         }
178
179         *CSR_PCICMD = cmd;
180
181         return IRQ_HANDLED;
182 }
183
184 static irqreturn_t dc21285_serr_irq(int irq, void *dev_id, struct pt_regs *regs)
185 {
186         struct timer_list *timer = dev_id;
187         unsigned int cntl;
188
189         printk(KERN_DEBUG "PCI: system error received: ");
190         pcibios_report_status(PCI_STATUS_SIG_SYSTEM_ERROR, 1);
191         printk("\n");
192
193         cntl = *CSR_SA110_CNTL & 0xffffdf07;
194         *CSR_SA110_CNTL = cntl | SA110_CNTL_RXSERR;
195
196         /*
197          * back off this interrupt
198          */
199         disable_irq(irq);
200         timer->expires = jiffies + HZ;
201         add_timer(timer);
202
203         return IRQ_HANDLED;
204 }
205
206 static irqreturn_t dc21285_discard_irq(int irq, void *dev_id, struct pt_regs *regs)
207 {
208         printk(KERN_DEBUG "PCI: discard timer expired\n");
209         *CSR_SA110_CNTL &= 0xffffde07;
210
211         return IRQ_HANDLED;
212 }
213
214 static irqreturn_t dc21285_dparity_irq(int irq, void *dev_id, struct pt_regs *regs)
215 {
216         unsigned int cmd;
217
218         printk(KERN_DEBUG "PCI: data parity error detected: ");
219         pcibios_report_status(PCI_STATUS_PARITY | PCI_STATUS_DETECTED_PARITY, 1);
220         printk("\n");
221
222         cmd = *CSR_PCICMD & 0xffff;
223         *CSR_PCICMD = cmd | 1 << 24;
224
225         return IRQ_HANDLED;
226 }
227
228 static irqreturn_t dc21285_parity_irq(int irq, void *dev_id, struct pt_regs *regs)
229 {
230         struct timer_list *timer = dev_id;
231         unsigned int cmd;
232
233         printk(KERN_DEBUG "PCI: parity error detected: ");
234         pcibios_report_status(PCI_STATUS_PARITY | PCI_STATUS_DETECTED_PARITY, 1);
235         printk("\n");
236
237         cmd = *CSR_PCICMD & 0xffff;
238         *CSR_PCICMD = cmd | 1 << 31;
239
240         /*
241          * back off this interrupt
242          */
243         disable_irq(irq);
244         timer->expires = jiffies + HZ;
245         add_timer(timer);
246
247         return IRQ_HANDLED;
248 }
249
250 int __init dc21285_setup(int nr, struct pci_sys_data *sys)
251 {
252         struct resource *res;
253
254         if (nr || !footbridge_cfn_mode())
255                 return 0;
256
257         res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
258         if (!res) {
259                 printk("out of memory for root bus resources");
260                 return 0;
261         }
262
263         res[0].flags = IORESOURCE_MEM;
264         res[0].name  = "Footbridge non-prefetch";
265         res[1].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
266         res[1].name  = "Footbridge prefetch";
267
268         allocate_resource(&iomem_resource, &res[1], 0x20000000,
269                           0xa0000000, 0xffffffff, 0x20000000, NULL, NULL);
270         allocate_resource(&iomem_resource, &res[0], 0x40000000,
271                           0x80000000, 0xffffffff, 0x40000000, NULL, NULL);
272
273         sys->resource[0] = &ioport_resource;
274         sys->resource[1] = &res[0];
275         sys->resource[2] = &res[1];
276         sys->mem_offset  = DC21285_PCI_MEM;
277
278         return 1;
279 }
280
281 struct pci_bus * __init dc21285_scan_bus(int nr, struct pci_sys_data *sys)
282 {
283         return pci_scan_bus(0, &dc21285_ops, sys);
284 }
285
286 void __init dc21285_preinit(void)
287 {
288         unsigned int mem_size, mem_mask;
289         int cfn_mode;
290
291         mem_size = (unsigned int)high_memory - PAGE_OFFSET;
292         for (mem_mask = 0x00100000; mem_mask < 0x10000000; mem_mask <<= 1)
293                 if (mem_mask >= mem_size)
294                         break;          
295
296         /*
297          * These registers need to be set up whether we're the
298          * central function or not.
299          */
300         *CSR_SDRAMBASEMASK    = (mem_mask - 1) & 0x0ffc0000;
301         *CSR_SDRAMBASEOFFSET  = 0;
302         *CSR_ROMBASEMASK      = 0x80000000;
303         *CSR_CSRBASEMASK      = 0;
304         *CSR_CSRBASEOFFSET    = 0;
305         *CSR_PCIADDR_EXTN     = 0;
306
307         cfn_mode = __footbridge_cfn_mode();
308
309         printk(KERN_INFO "PCI: DC21285 footbridge, revision %02lX, in "
310                 "%s mode\n", *CSR_CLASSREV & 0xff, cfn_mode ?
311                 "central function" : "addin");
312
313         if (footbridge_cfn_mode()) {
314                 /*
315                  * Clear any existing errors - we aren't
316                  * interested in historical data...
317                  */
318                 *CSR_SA110_CNTL = (*CSR_SA110_CNTL & 0xffffde07) |
319                                   SA110_CNTL_RXSERR;
320                 *CSR_PCICMD = (*CSR_PCICMD & 0xffff) | PCICMD_ERROR_BITS;
321         }
322
323         init_timer(&serr_timer);
324         init_timer(&perr_timer);
325
326         serr_timer.data = IRQ_PCI_SERR;
327         serr_timer.function = dc21285_enable_error;
328         perr_timer.data = IRQ_PCI_PERR;
329         perr_timer.function = dc21285_enable_error;
330
331         /*
332          * We don't care if these fail.
333          */
334         request_irq(IRQ_PCI_SERR, dc21285_serr_irq, IRQF_DISABLED,
335                     "PCI system error", &serr_timer);
336         request_irq(IRQ_PCI_PERR, dc21285_parity_irq, IRQF_DISABLED,
337                     "PCI parity error", &perr_timer);
338         request_irq(IRQ_PCI_ABORT, dc21285_abort_irq, IRQF_DISABLED,
339                     "PCI abort", NULL);
340         request_irq(IRQ_DISCARD_TIMER, dc21285_discard_irq, IRQF_DISABLED,
341                     "Discard timer", NULL);
342         request_irq(IRQ_PCI_DPERR, dc21285_dparity_irq, IRQF_DISABLED,
343                     "PCI data parity", NULL);
344
345         if (cfn_mode) {
346                 static struct resource csrio;
347
348                 csrio.flags  = IORESOURCE_IO;
349                 csrio.name   = "Footbridge";
350
351                 allocate_resource(&ioport_resource, &csrio, 128,
352                                   0xff00, 0xffff, 128, NULL, NULL);
353
354                 /*
355                  * Map our SDRAM at a known address in PCI space, just in case
356                  * the firmware had other ideas.  Using a nonzero base is
357                  * necessary, since some VGA cards forcefully use PCI addresses
358                  * in the range 0x000a0000 to 0x000c0000. (eg, S3 cards).
359                  */
360                 *CSR_PCICSRBASE       = 0xf4000000;
361                 *CSR_PCICSRIOBASE     = csrio.start;
362                 *CSR_PCISDRAMBASE     = __virt_to_bus(PAGE_OFFSET);
363                 *CSR_PCIROMBASE       = 0;
364                 *CSR_PCICMD = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
365                               PCI_COMMAND_INVALIDATE | PCICMD_ERROR_BITS;
366         } else if (footbridge_cfn_mode() != 0) {
367                 /*
368                  * If we are not compiled to accept "add-in" mode, then
369                  * we are using a constant virt_to_bus translation which
370                  * can not hope to cater for the way the host BIOS  has
371                  * set up the machine.
372                  */
373                 panic("PCI: this kernel is compiled for central "
374                         "function mode only");
375         }
376 }
377
378 void __init dc21285_postinit(void)
379 {
380         register_isa_ports(DC21285_PCI_MEM, DC21285_PCI_IO, 0);
381 }