ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / baget / irq.c
1 /*
2  * Code to handle Baget/MIPS IRQs plus some generic interrupt stuff.
3  *
4  * Copyright (C) 1998 Vladimir Roganov & Gleb Raiko
5  *      Code (mostly sleleton and comments) derived from DECstation IRQ
6  *      handling.
7  */
8 #include <linux/errno.h>
9 #include <linux/init.h>
10 #include <linux/kernel_stat.h>
11 #include <linux/module.h>
12 #include <linux/signal.h>
13 #include <linux/sched.h>
14 #include <linux/types.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/timex.h>
18 #include <linux/slab.h>
19 #include <linux/random.h>
20 #include <linux/delay.h>
21
22 #include <asm/bitops.h>
23 #include <asm/bootinfo.h>
24 #include <asm/io.h>
25 #include <asm/irq.h>
26 #include <asm/mipsregs.h>
27 #include <asm/system.h>
28
29 #include <asm/baget/baget.h>
30
31 volatile unsigned long irq_err_count;
32
33 /*
34  * This table is a correspondence between IRQ numbers and CPU PILs
35  */
36
37 static int irq_to_pil_map[BAGET_IRQ_NR] = {
38         7/*fixme: dma_err -1*/,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 0x00 - 0x0f */
39         -1,-1,-1,-1, 3,-1,-1,-1, 2, 2, 2,-1, 3,-1,-1,3/*fixme: lance*/, /* 0x10 - 0x1f */
40         -1,-1,-1,-1,-1,-1, 5,-1,-1,-1,-1,-1, 7,-1,-1,-1, /* 0x20 - 0x2f */
41         -1, 3, 2/*fixme systimer:3*/, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3  /* 0x30 - 0x3f */
42 };
43
44 static inline int irq_to_pil(int irq_nr)
45 {
46         int pil = -1;
47
48         if (irq_nr >= BAGET_IRQ_NR)
49                 baget_printk("irq_to_pil: too large irq_nr = 0x%x\n", irq_nr);
50         else {
51                 pil = irq_to_pil_map[irq_nr];
52                 if (pil == -1)
53                         baget_printk("irq_to_pil: unknown irq = 0x%x\n", irq_nr);
54         }
55
56         return pil;
57 }
58
59 /* Function for careful CP0 interrupt mask access */
60
61 static inline void modify_cp0_intmask(unsigned clr_mask, unsigned set_mask)
62 {
63         unsigned long status = read_c0_status();
64         status &= ~((clr_mask & 0xFF) << 8);
65         status |=   (set_mask & 0xFF) << 8;
66         write_c0_status(status);
67 }
68
69 /*
70  *  These two functions may be used for unconditional IRQ
71  *  masking via their PIL protection.
72  */
73
74 static inline void mask_irq(unsigned int irq_nr)
75 {
76         modify_cp0_intmask(irq_to_pil(irq_nr), 0);
77 }
78
79 static inline void unmask_irq(unsigned int irq_nr)
80 {
81         modify_cp0_intmask(0, irq_to_pil(irq_nr));
82 }
83
84 /*
85  * The following section is introduced for masking/unasking IRQ
86  * only while no more IRQs uses same CPU PIL.
87  *
88  * These functions are used in request_irq, free_irq, but it looks
89  * they cannot change something: CP0_STATUS is private for any
90  * process, and their action is invisible for system.
91  */
92
93 static volatile unsigned int pil_in_use[BAGET_PIL_NR] = { 0, };
94
95 void mask_irq_count(int irq_nr)
96 {
97         unsigned long flags;
98         int pil = irq_to_pil(irq_nr);
99
100         local_irq_save(flags);
101         if (!--pil_in_use[pil])
102                 mask_irq(irq_nr);
103         local_irq_restore(flags);
104 }
105
106 void unmask_irq_count(int irq_nr)
107 {
108         unsigned long flags;
109         int pil = irq_to_pil(irq_nr);
110
111         local_irq_save(flags);
112         if (!pil_in_use[pil]++)
113                 unmask_irq(irq_nr);
114         local_irq_restore(flags);
115 }
116
117 /*
118  * Two functions below are exported versions of mask/unmask IRQ
119  */
120
121 void disable_irq(unsigned int irq_nr)
122 {
123         unsigned long flags;
124
125         local_irq_save(flags);
126         mask_irq(irq_nr);
127         local_irq_restore(flags);
128 }
129
130 void enable_irq(unsigned int irq_nr)
131 {
132         unsigned long flags;
133
134         local_irq_save(flags);
135         unmask_irq(irq_nr);
136         local_irq_restore(flags);
137 }
138
139 /*
140  * Pointers to the low-level handlers: first the general ones, then the
141  * fast ones, then the bad ones.
142  */
143 static struct irqaction *irq_action[BAGET_IRQ_NR] = { NULL, };
144
145 int get_irq_list(char *buf)
146 {
147         int i, len = 0;
148         struct irqaction * action;
149         unsigned long flags;
150
151         for (i = 0 ; i < BAGET_IRQ_NR ; i++) {
152                 local_irq_save(flags);
153                 action = irq_action[i];
154                 if (!action)
155                         gotos skip;
156                 len += sprintf(buf+len, "%2d: %8d %c %s",
157                         i, kstat_this_cpu.irqs[i],
158                         (action->flags & SA_INTERRUPT) ? '+' : ' ',
159                         action->name);
160                 for (action=action->next; action; action = action->next) {
161                         len += sprintf(buf+len, ",%s %s",
162                                 (action->flags & SA_INTERRUPT) ? " +" : "",
163                                 action->name);
164                 }
165                 len += sprintf(buf+len, "\n");
166 skip:
167                 local_irq_restore(flags);
168         }
169         return len;
170 }
171
172
173 /*
174  * do_IRQ handles IRQ's that have been installed without the
175  * SA_INTERRUPT flag: it uses the full signal-handling return
176  * and runs with other interrupts enabled. All relatively slow
177  * IRQ's should use this format: notably the keyboard/timer
178  * routines.
179  */
180 static void do_IRQ(int irq, struct pt_regs * regs)
181 {
182         struct irqaction *action;
183         int do_random, cpu;
184
185         cpu = smp_processor_id();
186         irq_enter();
187         kstat_cpus(cpu).irqs[irq]++;
188
189         mask_irq(irq);
190         action = *(irq + irq_action);
191         if (action) {
192                 if (!(action->flags & SA_INTERRUPT))
193                         local_irq_enable();
194                 action = *(irq + irq_action);
195                 do_random = 0;
196                 do {
197                         do_random |= action->flags;
198                         action->handler(irq, action->dev_id, regs);
199                         action = action->next;
200                 } while (action);
201                 if (do_random & SA_SAMPLE_RANDOM)
202                         add_interrupt_randomness(irq);
203                 local_irq_disable();
204         } else {
205                 printk("do_IRQ: Unregistered IRQ (0x%X) occurred\n", irq);
206         }
207         unmask_irq(irq);
208         irq_exit();
209
210         /* unmasking and bottom half handling is done magically for us. */
211 }
212
213 /*
214  *  What to do in case of 'no VIC register available' for current interrupt
215  */
216 static void vic_reg_error(unsigned long address, unsigned char active_pils)
217 {
218         printk("\nNo VIC register found: reg=%08lx active_pils=%02x\n"
219                "Current interrupt mask from CP0_CAUSE: %02x\n",
220                address, 0xff & active_pils,
221                0xff & (read_c0_cause()>>8));
222         { int i; for (i=0; i<10000; i++) udelay(1000); }
223 }
224
225 static char baget_fpu_irq = BAGET_FPU_IRQ;
226 #define BAGET_INT_FPU {(unsigned long)&baget_fpu_irq, 1}
227
228 /*
229  *  Main interrupt handler: interrupt demultiplexer
230  */
231 asmlinkage void baget_interrupt(struct pt_regs *regs)
232 {
233         static struct baget_int_reg int_reg[BAGET_PIL_NR] = {
234                 BAGET_INT_NONE, BAGET_INT_NONE, BAGET_INT0_ACK, BAGET_INT1_ACK,
235                 BAGET_INT_NONE, BAGET_INT_FPU,  BAGET_INT_NONE, BAGET_INT5_ACK
236         };
237         unsigned char active_pils;
238         while ((active_pils = read_c0_cause()>>8)) {
239                 int pil;
240                 struct baget_int_reg* reg;
241
242                 for (pil = 0; pil < BAGET_PIL_NR; pil++) {
243                         if (!(active_pils & (1<<pil))) continue;
244
245                         reg = &int_reg[pil];
246
247                         if (reg->address) {
248                                 extern int try_read(unsigned long,int);
249                                 int irq  = try_read(reg->address, reg->size);
250
251                                 if (irq != -1)
252                                       do_IRQ(BAGET_IRQ_MASK(irq), regs);
253                                 else
254                                       vic_reg_error(reg->address, active_pils);
255                         } else {
256                                 printk("baget_interrupt: unknown interrupt "
257                                        "(pil = %d)\n", pil);
258                         }
259                 }
260         }
261 }
262
263 /*
264  * Idea is to put all interrupts
265  * in a single table and differenciate them just by number.
266  */
267 int setup_baget_irq(int irq, struct irqaction * new)
268 {
269         int shared = 0;
270         struct irqaction *old, **p;
271         unsigned long flags;
272
273         p = irq_action + irq;
274         if ((old = *p) != NULL) {
275                 /* Can't share interrupts unless both agree to */
276                 if (!(old->flags & new->flags & SA_SHIRQ))
277                         return -EBUSY;
278
279                 /* Can't share interrupts unless both are same type */
280                 if ((old->flags ^ new->flags) & SA_INTERRUPT)
281                         return -EBUSY;
282
283                 /* add new interrupt at end of irq queue */
284                 do {
285                         p = &old->next;
286                         old = *p;
287                 } while (old);
288                 shared = 1;
289         }
290
291         if (new->flags & SA_SAMPLE_RANDOM)
292                 rand_initialize_irq(irq);
293
294         local_irq_save(flags);
295         *p = new;
296         local_irq_restore(flags);
297
298         if (!shared) {
299                 unmask_irq_count(irq);
300         }
301
302         return 0;
303 }
304
305 int request_irq(unsigned int irq,
306                 void (*handler)(int, void *, struct pt_regs *),
307                 unsigned long irqflags,
308                 const char * devname,
309                 void *dev_id)
310 {
311         int retval;
312         struct irqaction * action;
313
314         if (irq >= BAGET_IRQ_NR)
315                 return -EINVAL;
316         if (!handler)
317                 return -EINVAL;
318         if (irq_to_pil_map[irq] < 0)
319                 return -EINVAL;
320
321         action = (struct irqaction *)
322                         kmalloc(sizeof(struct irqaction), GFP_KERNEL);
323         if (!action)
324                 return -ENOMEM;
325
326         action->handler = handler;
327         action->flags = irqflags;
328         action->mask = 0;
329         action->name = devname;
330         action->next = NULL;
331         action->dev_id = dev_id;
332
333         retval = setup_baget_irq(irq, action);
334
335         if (retval)
336                 kfree(action);
337
338         return retval;
339 }
340
341 EXPORT_SYMBOL(request_irq);
342
343 void free_irq(unsigned int irq, void *dev_id)
344 {
345         struct irqaction * action, **p;
346         unsigned long flags;
347
348         if (irq >= BAGET_IRQ_NR)
349                 printk("Trying to free IRQ%d\n",irq);
350
351         for (p = irq + irq_action; (action = *p) != NULL; p = &action->next) {
352                 if (action->dev_id != dev_id)
353                         continue;
354
355                 /* Found it - now free it */
356                 local_irq_save(flags);
357                 *p = action->next;
358                 if (!irq[irq_action])
359                         unmask_irq_count(irq);
360                 local_irq_restore(flags);
361                 kfree(action);
362                 return;
363         }
364         printk("Trying to free free IRQ%d\n",irq);
365 }
366
367 EXPORT_SYMBOL(free_irq);
368
369 unsigned long probe_irq_on (void)
370 {
371         /* TODO */
372         return 0;
373 }
374
375 EXPORT_SYMBOL(probe_irq_on);
376
377 int probe_irq_off (unsigned long irqs)
378 {
379         /* TODO */
380         return 0;
381 }
382
383 EXPORT_SYMBOL(probe_irq_off);
384
385
386 static void write_err_interrupt(int irq, void *dev_id, struct pt_regs * regs)
387 {
388         *(volatile char*) BAGET_WRERR_ACK = 0;
389 }
390
391 static struct irqaction irq0  =
392 { write_err_interrupt, SA_INTERRUPT, 0, "bus write error", NULL, NULL};
393
394 void __init init_IRQ(void)
395 {
396         irq_setup();
397
398         /* Enable access to VIC interrupt registers */
399         vac_outw(0xacef | 0x8200, VAC_PIO_FUNC);
400
401         /* Enable interrupts for pils 2 and 3 (lines 0 and 1) */
402         modify_cp0_intmask(0, (1<<2)|(1<<3));
403
404         if (setup_baget_irq(0, &irq0) < 0)
405                 printk("init_IRQ: unable to register write_err irq\n");
406 }