Import 2.3.9pre5
[davej-history.git] / arch / mips / baget / irq.c
blob8fc7ec179e43d276b50b2742a58d0c8de30afe67
1 /*
2 * Code to handle Baget/MIPS IRQs plus some generic interrupt stuff.
4 * Copyright (C) 1998 Vladimir Roganov & Gleb Raiko
5 * Code (mostly sleleton and comments) derived from DECstation IRQ
6 * handling.
8 * $Id$
9 */
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/signal.h>
14 #include <linux/sched.h>
15 #include <linux/types.h>
16 #include <linux/interrupt.h>
17 #include <linux/ioport.h>
18 #include <linux/timex.h>
19 #include <linux/malloc.h>
20 #include <linux/random.h>
21 #include <linux/delay.h>
23 #include <asm/bitops.h>
24 #include <asm/bootinfo.h>
25 #include <asm/io.h>
26 #include <asm/irq.h>
27 #include <asm/mipsregs.h>
28 #include <asm/system.h>
30 #include <asm/baget/baget.h>
32 unsigned int local_bh_count[NR_CPUS];
33 unsigned int local_irq_count[NR_CPUS];
34 unsigned long spurious_count = 0;
36 atomic_t __mips_bh_counter;
39 * This table is a correspondence between IRQ numbers and CPU PILs
42 static int irq_to_pil_map[BAGET_IRQ_NR] = {
43 7/*fixme: dma_err -1*/,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 0x00 - 0x0f */
44 -1,-1,-1,-1, 3,-1,-1,-1, 2, 2, 2,-1, 3,-1,-1,3/*fixme: lance*/, /* 0x10 - 0x1f */
45 -1,-1,-1,-1,-1,-1, 5,-1,-1,-1,-1,-1, 7,-1,-1,-1, /* 0x20 - 0x2f */
46 -1, 3, 2/*fixme systimer:3*/, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 /* 0x30 - 0x3f */
49 static inline int irq_to_pil(int irq_nr)
51 int pil = -1;
53 if (irq_nr >= BAGET_IRQ_NR)
54 baget_printk("irq_to_pil: too large irq_nr = 0x%x\n", irq_nr);
55 else {
56 pil = irq_to_pil_map[irq_nr];
57 if (pil == -1)
58 baget_printk("irq_to_pil: unknown irq = 0x%x\n", irq_nr);
61 return pil;
64 /* Function for careful CP0 interrupt mask access */
66 static inline void modify_cp0_intmask(unsigned clr_mask, unsigned set_mask)
68 unsigned long status = read_32bit_cp0_register(CP0_STATUS);
69 status &= ~((clr_mask & 0xFF) << 8);
70 status |= (set_mask & 0xFF) << 8;
71 write_32bit_cp0_register(CP0_STATUS, status);
74 /*
75 * These two functions may be used for unconditional IRQ
76 * masking via their PIL protection.
79 static inline void mask_irq(unsigned int irq_nr)
81 modify_cp0_intmask(irq_to_pil(irq_nr), 0);
84 static inline void unmask_irq(unsigned int irq_nr)
86 modify_cp0_intmask(0, irq_to_pil(irq_nr));
90 * The following section is introduced for masking/unasking IRQ
91 * only while no more IRQs uses same CPU PIL.
93 * These functions are used in request_irq, free_irq, but it looks
94 * they cannot change something: CP0_STATUS is private for any
95 * process, and their action is invisible for system.
98 static volatile unsigned int pil_in_use[BAGET_PIL_NR] = { 0, };
100 void mask_irq_count(int irq_nr)
102 unsigned long flags;
103 int pil = irq_to_pil(irq_nr);
105 save_and_cli(flags);
106 if (!--pil_in_use[pil])
107 mask_irq(irq_nr);
108 restore_flags(flags);
111 void unmask_irq_count(int irq_nr)
113 unsigned long flags;
114 int pil = irq_to_pil(irq_nr);
116 save_and_cli(flags);
117 if (!pil_in_use[pil]++)
118 unmask_irq(irq_nr);
119 restore_flags(flags);
123 * Two functions below are exported versions of mask/unmask IRQ
126 void disable_irq(unsigned int irq_nr)
128 unsigned long flags;
130 save_and_cli(flags);
131 mask_irq(irq_nr);
132 restore_flags(flags);
135 void enable_irq(unsigned int irq_nr)
137 unsigned long flags;
139 save_and_cli(flags);
140 unmask_irq(irq_nr);
141 restore_flags(flags);
145 * Data definition for static irqaction allocation.
146 * It is used while SLAB module is not initialized.
149 #define MAX_STATIC_ALLOC 4
150 struct irqaction static_irqaction[MAX_STATIC_ALLOC];
151 int static_irq_count = 0;
154 * Pointers to the low-level handlers: first the general ones, then the
155 * fast ones, then the bad ones.
157 static struct irqaction *irq_action[BAGET_IRQ_NR] = { NULL, };
159 int get_irq_list(char *buf)
161 int i, len = 0;
162 struct irqaction * action;
164 for (i = 0 ; i < BAGET_IRQ_NR ; i++) {
165 action = irq_action[i];
166 if (!action)
167 continue;
168 len += sprintf(buf+len, "%2d: %8d %c %s",
169 i, kstat.irqs[0][i],
170 (action->flags & SA_INTERRUPT) ? '+' : ' ',
171 action->name);
172 for (action=action->next; action; action = action->next) {
173 len += sprintf(buf+len, ",%s %s",
174 (action->flags & SA_INTERRUPT) ? " +" : "",
175 action->name);
177 len += sprintf(buf+len, "\n");
179 return len;
184 * do_IRQ handles IRQ's that have been installed without the
185 * SA_INTERRUPT flag: it uses the full signal-handling return
186 * and runs with other interrupts enabled. All relatively slow
187 * IRQ's should use this format: notably the keyboard/timer
188 * routines.
190 static void do_IRQ(int irq, struct pt_regs * regs)
192 struct irqaction *action;
193 int do_random, cpu;
195 cpu = smp_processor_id();
196 hardirq_enter(cpu);
197 kstat.irqs[cpu][irq]++;
199 mask_irq(irq);
200 action = *(irq + irq_action);
201 if (action) {
202 if (!(action->flags & SA_INTERRUPT))
203 __sti();
204 action = *(irq + irq_action);
205 do_random = 0;
206 do {
207 do_random |= action->flags;
208 action->handler(irq, action->dev_id, regs);
209 action = action->next;
210 } while (action);
211 if (do_random & SA_SAMPLE_RANDOM)
212 add_interrupt_randomness(irq);
213 __cli();
214 } else {
215 printk("do_IRQ: Unregistered IRQ (0x%X) occured\n", irq);
217 unmask_irq(irq);
218 hardirq_exit(cpu);
220 /* unmasking and bottom half handling is done magically for us. */
224 * What to do in case of 'no VIC register available' for current interrupt
226 static void vic_reg_error(unsigned long address, unsigned char active_pils)
228 printk("\nNo VIC register found: reg=%08lx active_pils=%02x\n"
229 "Current interrupt mask from CP0_CAUSE: %02x\n",
230 address, 0xff & active_pils,
231 0xff & (read_32bit_cp0_register(CP0_CAUSE)>>8));
232 { int i; for (i=0; i<10000; i++) udelay(1000); }
235 static char baget_fpu_irq = BAGET_FPU_IRQ;
236 #define BAGET_INT_FPU {(unsigned long)&baget_fpu_irq, 1}
239 * Main interrupt handler: interrupt demultiplexer
241 asmlinkage void baget_interrupt(struct pt_regs *regs)
243 static struct baget_int_reg int_reg[BAGET_PIL_NR] = {
244 BAGET_INT_NONE, BAGET_INT_NONE, BAGET_INT0_ACK, BAGET_INT1_ACK,
245 BAGET_INT_NONE, BAGET_INT_FPU, BAGET_INT_NONE, BAGET_INT5_ACK
247 unsigned char active_pils;
248 while ((active_pils = read_32bit_cp0_register(CP0_CAUSE)>>8)) {
249 int pil;
250 struct baget_int_reg* reg;
252 for (pil = 0; pil < BAGET_PIL_NR; pil++) {
253 if (!(active_pils & (1<<pil))) continue;
255 reg = &int_reg[pil];
257 if (reg->address) {
258 extern int try_read(unsigned long,int);
259 int irq = try_read(reg->address, reg->size);
261 if (irq != -1)
262 do_IRQ(BAGET_IRQ_MASK(irq), regs);
263 else
264 vic_reg_error(reg->address, active_pils);
265 } else {
266 printk("baget_interrupt: unknown interrupt "
267 "(pil = %d)\n", pil);
274 * Idea is to put all interrupts
275 * in a single table and differenciate them just by number.
277 int setup_baget_irq(int irq, struct irqaction * new)
279 int shared = 0;
280 struct irqaction *old, **p;
281 unsigned long flags;
283 p = irq_action + irq;
284 if ((old = *p) != NULL) {
285 /* Can't share interrupts unless both agree to */
286 if (!(old->flags & new->flags & SA_SHIRQ))
287 return -EBUSY;
289 /* Can't share interrupts unless both are same type */
290 if ((old->flags ^ new->flags) & SA_INTERRUPT)
291 return -EBUSY;
293 /* add new interrupt at end of irq queue */
294 do {
295 p = &old->next;
296 old = *p;
297 } while (old);
298 shared = 1;
301 if (new->flags & SA_SAMPLE_RANDOM)
302 rand_initialize_irq(irq);
304 save_and_cli(flags);
305 *p = new;
306 restore_flags(flags);
308 if (!shared) {
309 unmask_irq_count(irq);
312 return 0;
315 int request_irq(unsigned int irq,
316 void (*handler)(int, void *, struct pt_regs *),
317 unsigned long irqflags,
318 const char * devname,
319 void *dev_id)
321 int retval;
322 struct irqaction * action = NULL;
324 if (irq >= BAGET_IRQ_NR)
325 return -EINVAL;
326 if (!handler)
327 return -EINVAL;
328 if (irq_to_pil_map[irq] < 0)
329 return -EINVAL;
331 if (irqflags & SA_STATIC_ALLOC) {
332 unsigned long flags;
334 save_and_cli(flags);
335 if (static_irq_count < MAX_STATIC_ALLOC)
336 action = &static_irqaction[static_irq_count++];
337 else
338 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed "
339 "using kmalloc\n", irq, devname);
340 restore_flags(flags);
343 if (action == NULL)
344 action = (struct irqaction *)
345 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
347 if (!action)
348 return -ENOMEM;
350 action->handler = handler;
351 action->flags = irqflags;
352 action->mask = 0;
353 action->name = devname;
354 action->next = NULL;
355 action->dev_id = dev_id;
357 retval = setup_baget_irq(irq, action);
359 if (retval)
360 kfree(action);
362 return retval;
365 void free_irq(unsigned int irq, void *dev_id)
367 struct irqaction * action, **p;
368 unsigned long flags;
370 if (irq >= BAGET_IRQ_NR)
371 printk("Trying to free IRQ%d\n",irq);
373 for (p = irq + irq_action; (action = *p) != NULL; p = &action->next) {
374 if (action->dev_id != dev_id)
375 continue;
377 /* Found it - now free it */
378 save_and_cli(flags);
379 *p = action->next;
380 if (!irq[irq_action])
381 unmask_irq_count(irq);
382 restore_flags(flags);
384 if (action->flags & SA_STATIC_ALLOC)
386 /* This interrupt is marked as specially allocated
387 * so it is a bad idea to free it.
389 printk("Attempt to free statically allocated "
390 "IRQ%d (%s)\n", irq, action->name);
391 return;
394 kfree(action);
395 return;
397 printk("Trying to free free IRQ%d\n",irq);
400 static int baget_irq_canonicalize(int irq)
402 return irq;
405 int (*irq_cannonicalize)(int irq) = baget_irq_canonicalize;
407 unsigned long probe_irq_on (void)
409 /* TODO */
410 return 0;
413 int probe_irq_off (unsigned long irqs)
415 /* TODO */
416 return 0;
420 static void write_err_interrupt(int irq, void *dev_id, struct pt_regs * regs)
422 *(volatile char*) BAGET_WRERR_ACK = 0;
425 __initfunc(void init_IRQ(void))
427 irq_setup();
429 /* Enable access to VIC interrupt registers */
430 vac_outw(0xacef | 0x8200, VAC_PIO_FUNC);
432 /* Enable interrupts for pils 2 and 3 (lines 0 and 1) */
433 modify_cp0_intmask(0, (1<<2)|(1<<3));
435 if (request_irq(0/*fixme*/, write_err_interrupt,
436 SA_INTERRUPT|SA_STATIC_ALLOC, "write_err", NULL) < 0)
437 printk("init_IRQ: unable to register write_err irq\n");