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
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>
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
)
53 if (irq_nr
>= BAGET_IRQ_NR
)
54 baget_printk("irq_to_pil: too large irq_nr = 0x%x\n", irq_nr
);
56 pil
= irq_to_pil_map
[irq_nr
];
58 baget_printk("irq_to_pil: unknown irq = 0x%x\n", irq_nr
);
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
);
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
)
103 int pil
= irq_to_pil(irq_nr
);
106 if (!--pil_in_use
[pil
])
108 restore_flags(flags
);
111 void unmask_irq_count(int irq_nr
)
114 int pil
= irq_to_pil(irq_nr
);
117 if (!pil_in_use
[pil
]++)
119 restore_flags(flags
);
123 * Two functions below are exported versions of mask/unmask IRQ
126 void disable_irq(unsigned int irq_nr
)
132 restore_flags(flags
);
135 void enable_irq(unsigned int 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
)
162 struct irqaction
* action
;
164 for (i
= 0 ; i
< BAGET_IRQ_NR
; i
++) {
165 action
= irq_action
[i
];
168 len
+= sprintf(buf
+len
, "%2d: %8d %c %s",
170 (action
->flags
& SA_INTERRUPT
) ? '+' : ' ',
172 for (action
=action
->next
; action
; action
= action
->next
) {
173 len
+= sprintf(buf
+len
, ",%s %s",
174 (action
->flags
& SA_INTERRUPT
) ? " +" : "",
177 len
+= sprintf(buf
+len
, "\n");
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
190 static void do_IRQ(int irq
, struct pt_regs
* regs
)
192 struct irqaction
*action
;
195 cpu
= smp_processor_id();
197 kstat
.irqs
[cpu
][irq
]++;
200 action
= *(irq
+ irq_action
);
202 if (!(action
->flags
& SA_INTERRUPT
))
204 action
= *(irq
+ irq_action
);
207 do_random
|= action
->flags
;
208 action
->handler(irq
, action
->dev_id
, regs
);
209 action
= action
->next
;
211 if (do_random
& SA_SAMPLE_RANDOM
)
212 add_interrupt_randomness(irq
);
215 printk("do_IRQ: Unregistered IRQ (0x%X) occured\n", irq
);
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)) {
250 struct baget_int_reg
* reg
;
252 for (pil
= 0; pil
< BAGET_PIL_NR
; pil
++) {
253 if (!(active_pils
& (1<<pil
))) continue;
258 extern int try_read(unsigned long,int);
259 int irq
= try_read(reg
->address
, reg
->size
);
262 do_IRQ(BAGET_IRQ_MASK(irq
), regs
);
264 vic_reg_error(reg
->address
, active_pils
);
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)
280 struct irqaction
*old
, **p
;
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
))
289 /* Can't share interrupts unless both are same type */
290 if ((old
->flags
^ new->flags
) & SA_INTERRUPT
)
293 /* add new interrupt at end of irq queue */
301 if (new->flags
& SA_SAMPLE_RANDOM
)
302 rand_initialize_irq(irq
);
306 restore_flags(flags
);
309 unmask_irq_count(irq
);
315 int request_irq(unsigned int irq
,
316 void (*handler
)(int, void *, struct pt_regs
*),
317 unsigned long irqflags
,
318 const char * devname
,
322 struct irqaction
* action
= NULL
;
324 if (irq
>= BAGET_IRQ_NR
)
328 if (irq_to_pil_map
[irq
] < 0)
331 if (irqflags
& SA_STATIC_ALLOC
) {
335 if (static_irq_count
< MAX_STATIC_ALLOC
)
336 action
= &static_irqaction
[static_irq_count
++];
338 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed "
339 "using kmalloc\n", irq
, devname
);
340 restore_flags(flags
);
344 action
= (struct irqaction
*)
345 kmalloc(sizeof(struct irqaction
), GFP_KERNEL
);
350 action
->handler
= handler
;
351 action
->flags
= irqflags
;
353 action
->name
= devname
;
355 action
->dev_id
= dev_id
;
357 retval
= setup_baget_irq(irq
, action
);
365 void free_irq(unsigned int irq
, void *dev_id
)
367 struct irqaction
* action
, **p
;
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
)
377 /* Found it - now free it */
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
);
397 printk("Trying to free free IRQ%d\n",irq
);
400 static int baget_irq_canonicalize(int irq
)
405 int (*irq_cannonicalize
)(int irq
) = baget_irq_canonicalize
;
407 unsigned long probe_irq_on (void)
413 int probe_irq_off (unsigned long irqs
)
420 static void write_err_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
422 *(volatile char*) BAGET_WRERR_ACK
= 0;
425 void __init
init_IRQ(void)
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");