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
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>
22 #include <asm/bitops.h>
23 #include <asm/bootinfo.h>
26 #include <asm/mipsregs.h>
27 #include <asm/system.h>
29 #include <asm/baget/baget.h>
31 volatile unsigned long irq_err_count;
34 * This table is a correspondence between IRQ numbers and CPU PILs
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 */
44 static inline int irq_to_pil(int irq_nr)
48 if (irq_nr >= BAGET_IRQ_NR)
49 baget_printk("irq_to_pil: too large irq_nr = 0x%x\n", irq_nr);
51 pil = irq_to_pil_map[irq_nr];
53 baget_printk("irq_to_pil: unknown irq = 0x%x\n", irq_nr);
59 /* Function for careful CP0 interrupt mask access */
61 static inline void modify_cp0_intmask(unsigned clr_mask, unsigned set_mask)
63 unsigned long status = read_c0_status();
64 status &= ~((clr_mask & 0xFF) << 8);
65 status |= (set_mask & 0xFF) << 8;
66 write_c0_status(status);
70 * These two functions may be used for unconditional IRQ
71 * masking via their PIL protection.
74 static inline void mask_irq(unsigned int irq_nr)
76 modify_cp0_intmask(irq_to_pil(irq_nr), 0);
79 static inline void unmask_irq(unsigned int irq_nr)
81 modify_cp0_intmask(0, irq_to_pil(irq_nr));
85 * The following section is introduced for masking/unasking IRQ
86 * only while no more IRQs uses same CPU PIL.
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.
93 static volatile unsigned int pil_in_use[BAGET_PIL_NR] = { 0, };
95 void mask_irq_count(int irq_nr)
98 int pil = irq_to_pil(irq_nr);
100 local_irq_save(flags);
101 if (!--pil_in_use[pil])
103 local_irq_restore(flags);
106 void unmask_irq_count(int irq_nr)
109 int pil = irq_to_pil(irq_nr);
111 local_irq_save(flags);
112 if (!pil_in_use[pil]++)
114 local_irq_restore(flags);
118 * Two functions below are exported versions of mask/unmask IRQ
121 void disable_irq(unsigned int irq_nr)
125 local_irq_save(flags);
127 local_irq_restore(flags);
130 void enable_irq(unsigned int irq_nr)
134 local_irq_save(flags);
136 local_irq_restore(flags);
140 * Pointers to the low-level handlers: first the general ones, then the
141 * fast ones, then the bad ones.
143 static struct irqaction *irq_action[BAGET_IRQ_NR] = { NULL, };
145 int get_irq_list(char *buf)
148 struct irqaction * action;
151 for (i = 0 ; i < BAGET_IRQ_NR ; i++) {
152 local_irq_save(flags);
153 action = irq_action[i];
156 len += sprintf(buf+len, "%2d: %8d %c %s",
157 i, kstat_this_cpu.irqs[i],
158 (action->flags & SA_INTERRUPT) ? '+' : ' ',
160 for (action=action->next; action; action = action->next) {
161 len += sprintf(buf+len, ",%s %s",
162 (action->flags & SA_INTERRUPT) ? " +" : "",
165 len += sprintf(buf+len, "\n");
167 local_irq_restore(flags);
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
180 static void do_IRQ(int irq, struct pt_regs * regs)
182 struct irqaction *action;
185 cpu = smp_processor_id();
187 kstat_cpus(cpu).irqs[irq]++;
190 action = *(irq + irq_action);
192 if (!(action->flags & SA_INTERRUPT))
194 action = *(irq + irq_action);
197 do_random |= action->flags;
198 action->handler(irq, action->dev_id, regs);
199 action = action->next;
201 if (do_random & SA_SAMPLE_RANDOM)
202 add_interrupt_randomness(irq);
205 printk("do_IRQ: Unregistered IRQ (0x%X) occurred\n", irq);
210 /* unmasking and bottom half handling is done magically for us. */
214 * What to do in case of 'no VIC register available' for current interrupt
216 static void vic_reg_error(unsigned long address, unsigned char active_pils)
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); }
225 static char baget_fpu_irq = BAGET_FPU_IRQ;
226 #define BAGET_INT_FPU {(unsigned long)&baget_fpu_irq, 1}
229 * Main interrupt handler: interrupt demultiplexer
231 asmlinkage void baget_interrupt(struct pt_regs *regs)
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
237 unsigned char active_pils;
238 while ((active_pils = read_c0_cause()>>8)) {
240 struct baget_int_reg* reg;
242 for (pil = 0; pil < BAGET_PIL_NR; pil++) {
243 if (!(active_pils & (1<<pil))) continue;
248 extern int try_read(unsigned long,int);
249 int irq = try_read(reg->address, reg->size);
252 do_IRQ(BAGET_IRQ_MASK(irq), regs);
254 vic_reg_error(reg->address, active_pils);
256 printk("baget_interrupt: unknown interrupt "
257 "(pil = %d)\n", pil);
264 * Idea is to put all interrupts
265 * in a single table and differenciate them just by number.
267 int setup_baget_irq(int irq, struct irqaction * new)
270 struct irqaction *old, **p;
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))
279 /* Can't share interrupts unless both are same type */
280 if ((old->flags ^ new->flags) & SA_INTERRUPT)
283 /* add new interrupt at end of irq queue */
291 if (new->flags & SA_SAMPLE_RANDOM)
292 rand_initialize_irq(irq);
294 local_irq_save(flags);
296 local_irq_restore(flags);
299 unmask_irq_count(irq);
305 int request_irq(unsigned int irq,
306 void (*handler)(int, void *, struct pt_regs *),
307 unsigned long irqflags,
308 const char * devname,
312 struct irqaction * action;
314 if (irq >= BAGET_IRQ_NR)
318 if (irq_to_pil_map[irq] < 0)
321 action = (struct irqaction *)
322 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
326 action->handler = handler;
327 action->flags = irqflags;
328 cpus_clear(action->mask);
329 action->name = devname;
331 action->dev_id = dev_id;
333 retval = setup_baget_irq(irq, action);
341 EXPORT_SYMBOL(request_irq);
343 void free_irq(unsigned int irq, void *dev_id)
345 struct irqaction * action, **p;
348 if (irq >= BAGET_IRQ_NR)
349 printk("Trying to free IRQ%d\n",irq);
351 for (p = irq + irq_action; (action = *p) != NULL; p = &action->next) {
352 if (action->dev_id != dev_id)
355 /* Found it - now free it */
356 local_irq_save(flags);
358 if (!irq[irq_action])
359 unmask_irq_count(irq);
360 local_irq_restore(flags);
364 printk("Trying to free free IRQ%d\n",irq);
367 EXPORT_SYMBOL(free_irq);
369 unsigned long probe_irq_on (void)
375 EXPORT_SYMBOL(probe_irq_on);
377 int probe_irq_off (unsigned long irqs)
383 EXPORT_SYMBOL(probe_irq_off);
386 static void write_err_interrupt(int irq, void *dev_id, struct pt_regs * regs)
388 *(volatile char*) BAGET_WRERR_ACK = 0;
391 static struct irqaction irq0 =
392 { write_err_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "bus write error", NULL, NULL};
394 void __init init_IRQ(void)
398 /* Enable access to VIC interrupt registers */
399 vac_outw(0xacef | 0x8200, VAC_PIO_FUNC);
401 /* Enable interrupts for pils 2 and 3 (lines 0 and 1) */
402 modify_cp0_intmask(0, (1<<2)|(1<<3));
404 if (setup_baget_irq(0, &irq0) < 0)
405 printk("init_IRQ: unable to register write_err irq\n");