ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / sgi-ip32 / ip32-irq.c
1 /*
2  * Code to handle IP32 IRQs
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (C) 2000 Harald Koerfgen
9  * Copyright (C) 2001 Keith M Wesolowski
10  */
11 #include <linux/init.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/types.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/bitops.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/mm.h>
20 #include <linux/random.h>
21 #include <linux/sched.h>
22
23 #include <asm/bitops.h>
24 #include <asm/mipsregs.h>
25 #include <asm/signal.h>
26 #include <asm/system.h>
27 #include <asm/time.h>
28 #include <asm/ip32/crime.h>
29 #include <asm/ip32/mace.h>
30 #include <asm/ip32/ip32_ints.h>
31
32 /* issue a PIO read to make sure no PIO writes are pending */
33 #define flush_crime_bus() crime_read(CRIME_CONTROL);
34 static void inline flush_mace_bus(void)
35 {
36         volatile unsigned long junk = mace_perif_ctrl_read(misc);
37 }
38
39 #undef DEBUG_IRQ
40 #ifdef DEBUG_IRQ
41 #define DBG(x...) printk(x)
42 #else
43 #define DBG(x...)
44 #endif
45
46 /* O2 irq map
47  *
48  * IP0 -> software (ignored)
49  * IP1 -> software (ignored)
50  * IP2 -> (irq0) C crime 1.1 all interrupts; crime 1.5 ???
51  * IP3 -> (irq1) X unknown
52  * IP4 -> (irq2) X unknown
53  * IP5 -> (irq3) X unknown
54  * IP6 -> (irq4) X unknown
55  * IP7 -> (irq5) 0 CPU count/compare timer (system timer)
56  *
57  * crime: (C)
58  *
59  * CRIME_INT_STAT 31:0:
60  *
61  * 0  -> 1  Video in 1
62  * 1  -> 2  Video in 2
63  * 2  -> 3  Video out
64  * 3  -> 4  Mace ethernet
65  * 4  -> S  SuperIO sub-interrupt
66  * 5  -> M  Miscellaneous sub-interrupt
67  * 6  -> A  Audio sub-interrupt
68  * 7  -> 8  PCI bridge errors
69  * 8  -> 9  PCI SCSI aic7xxx 0
70  * 9  -> 10 PCI SCSI aic7xxx 1
71  * 10 -> 11 PCI slot 0
72  * 11 -> 12 unused (PCI slot 1)
73  * 12 -> 13 unused (PCI slot 2)
74  * 13 -> 14 unused (PCI shared 0)
75  * 14 -> 15 unused (PCI shared 1)
76  * 15 -> 16 unused (PCI shared 2)
77  * 16 -> 17 GBE0 (E)
78  * 17 -> 18 GBE1 (E)
79  * 18 -> 19 GBE2 (E)
80  * 19 -> 20 GBE3 (E)
81  * 20 -> 21 CPU errors
82  * 21 -> 22 Memory errors
83  * 22 -> 23 RE empty edge (E)
84  * 23 -> 24 RE full edge (E)
85  * 24 -> 25 RE idle edge (E)
86  * 25 -> 26 RE empty level
87  * 26 -> 27 RE full level
88  * 27 -> 28 RE idle level
89  * 28 -> 29 unused (software 0) (E)
90  * 29 -> 30 unused (software 1) (E)
91  * 30 -> 31 unused (software 2) - crime 1.5 CPU SysCorError (E)
92  * 31 -> 32 VICE
93  *
94  * S, M, A: Use the MACE ISA interrupt register
95  * MACE_ISA_INT_STAT 31:0
96  *
97  * 0-7 -> 33-40 Audio
98  * 8 -> 41 RTC
99  * 9 -> 42 Keyboard
100  * 10 -> X Keyboard polled
101  * 11 -> 44 Mouse
102  * 12 -> X Mouse polled
103  * 13-15 -> 46-48 Count/compare timers
104  * 16-19 -> 49-52 Parallel (16 E)
105  * 20-25 -> 53-58 Serial 1 (22 E)
106  * 26-31 -> 59-64 Serial 2 (28 E)
107  *
108  * Note that this means IRQs 5-7, 43, and 45 do not exist.  This is a
109  * different IRQ map than IRIX uses, but that's OK as Linux irq handling
110  * is quite different anyway.
111  */
112
113 /*
114  * IRQ spinlock - Ralf says not to disable CPU interrupts,
115  * and I think he knows better.
116  */
117 static spinlock_t ip32_irq_lock = SPIN_LOCK_UNLOCKED;
118
119 /* Some initial interrupts to set up */
120 extern irqreturn_t crime_memerr_intr (int irq, void *dev_id,
121                                       struct pt_regs *regs);
122 extern irqreturn_t crime_cpuerr_intr (int irq, void *dev_id,
123                                       struct pt_regs *regs);
124
125 struct irqaction memerr_irq = { crime_memerr_intr, SA_INTERRUPT,
126                                 0, "CRIME memory error", NULL, NULL };
127 struct irqaction cpuerr_irq = { crime_cpuerr_intr, SA_INTERRUPT,
128                                 0, "CRIME CPU error", NULL, NULL };
129
130 extern void ip32_handle_int(void);
131
132 /*
133  * For interrupts wired from a single device to the CPU.  Only the clock
134  * uses this it seems, which is IRQ 0 and IP7.
135  */
136
137 static void enable_cpu_irq(unsigned int irq)
138 {
139         set_c0_status(STATUSF_IP7);
140 }
141
142 static unsigned int startup_cpu_irq(unsigned int irq)
143 {
144         enable_cpu_irq(irq);
145         return 0;
146 }
147
148 static void disable_cpu_irq(unsigned int irq)
149 {
150         clear_c0_status(STATUSF_IP7);
151 }
152
153 static void end_cpu_irq(unsigned int irq)
154 {
155         if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
156                 enable_cpu_irq (irq);
157 }
158
159 #define shutdown_cpu_irq disable_cpu_irq
160 #define mask_and_ack_cpu_irq disable_cpu_irq
161
162 static struct hw_interrupt_type ip32_cpu_interrupt = {
163         "IP32 CPU",
164         startup_cpu_irq,
165         shutdown_cpu_irq,
166         enable_cpu_irq,
167         disable_cpu_irq,
168         mask_and_ack_cpu_irq,
169         end_cpu_irq,
170         NULL
171 };
172
173 /*
174  * This is for pure CRIME interrupts - ie not MACE.  The advantage?
175  * We get to split the register in half and do faster lookups.
176  */
177
178 static uint64_t crime_mask;
179
180 static void enable_crime_irq(unsigned int irq)
181 {
182         unsigned long flags;
183
184         spin_lock_irqsave(&ip32_irq_lock, flags);
185         crime_mask |= 1 << (irq - 1);
186         crime_write(crime_mask, CRIME_INT_MASK);
187         spin_unlock_irqrestore(&ip32_irq_lock, flags);
188 }
189
190 static unsigned int startup_crime_irq(unsigned int irq)
191 {
192         enable_crime_irq(irq);
193         return 0; /* This is probably not right; we could have pending irqs */
194 }
195
196 static void disable_crime_irq(unsigned int irq)
197 {
198         unsigned long flags;
199
200         spin_lock_irqsave(&ip32_irq_lock, flags);
201         crime_mask &= ~(1 << (irq - 1));
202         crime_write(crime_mask, CRIME_INT_MASK);
203         flush_crime_bus();
204         spin_unlock_irqrestore(&ip32_irq_lock, flags);
205 }
206
207 static void mask_and_ack_crime_irq(unsigned int irq)
208 {
209         unsigned long flags;
210
211         /* Edge triggered interrupts must be cleared. */
212         if ((irq >= CRIME_GBE0_IRQ && irq <= CRIME_GBE3_IRQ)
213             || (irq >= CRIME_RE_EMPTY_E_IRQ && irq <= CRIME_RE_IDLE_E_IRQ)
214             || (irq >= CRIME_SOFT0_IRQ && irq <= CRIME_SOFT2_IRQ)) {
215                 uint64_t crime_int;
216                 spin_lock_irqsave(&ip32_irq_lock, flags);
217                 crime_int = crime_read(CRIME_HARD_INT);
218                 crime_int &= ~(1 << (irq - 1));
219                 crime_write(crime_int, CRIME_HARD_INT);
220                 spin_unlock_irqrestore(&ip32_irq_lock, flags);
221         }
222         disable_crime_irq(irq);
223 }
224
225 static void end_crime_irq(unsigned int irq)
226 {
227         if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
228                 enable_crime_irq(irq);
229 }
230
231 #define shutdown_crime_irq disable_crime_irq
232
233 static struct hw_interrupt_type ip32_crime_interrupt = {
234         "IP32 CRIME",
235         startup_crime_irq,
236         shutdown_crime_irq,
237         enable_crime_irq,
238         disable_crime_irq,
239         mask_and_ack_crime_irq,
240         end_crime_irq,
241         NULL
242 };
243
244 /*
245  * This is for MACE PCI interrupts.  We can decrease bus traffic by masking
246  * as close to the source as possible.  This also means we can take the
247  * next chunk of the CRIME register in one piece.
248  */
249
250 static unsigned long macepci_mask;
251
252 static void enable_macepci_irq(unsigned int irq)
253 {
254         unsigned long flags;
255
256         spin_lock_irqsave(&ip32_irq_lock, flags);
257         macepci_mask |= MACEPCI_CONTROL_INT(irq - 9);
258         mace->pci.control = macepci_mask;
259         crime_mask |= 1 << (irq - 1);
260         crime_write(crime_mask, CRIME_INT_MASK);
261         spin_unlock_irqrestore(&ip32_irq_lock, flags);
262 }
263
264 static unsigned int startup_macepci_irq(unsigned int irq)
265 {
266         enable_macepci_irq (irq);
267         return 0;
268 }
269
270 static void disable_macepci_irq(unsigned int irq)
271 {
272         unsigned long flags;
273
274         spin_lock_irqsave(&ip32_irq_lock, flags);
275         crime_mask &= ~(1 << (irq - 1));
276         crime_write(crime_mask, CRIME_INT_MASK);
277         flush_crime_bus();
278         macepci_mask &= ~MACEPCI_CONTROL_INT(irq - 9);
279         mace->pci.control = macepci_mask;
280         flush_mace_bus();
281         spin_unlock_irqrestore(&ip32_irq_lock, flags);
282 }
283
284 static void end_macepci_irq(unsigned int irq)
285 {
286         if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
287                 enable_macepci_irq(irq);
288 }
289
290 #define shutdown_macepci_irq disable_macepci_irq
291 #define mask_and_ack_macepci_irq disable_macepci_irq
292
293 static struct hw_interrupt_type ip32_macepci_interrupt = {
294         "IP32 MACE PCI",
295         startup_macepci_irq,
296         shutdown_macepci_irq,
297         enable_macepci_irq,
298         disable_macepci_irq,
299         mask_and_ack_macepci_irq,
300         end_macepci_irq,
301         NULL
302 };
303
304 /* This is used for MACE ISA interrupts.  That means bits 4-6 in the
305  * CRIME register.
306  */
307
308 #define MACEISA_AUDIO_INT       (MACEISA_AUDIO_SW_INT |         \
309                                  MACEISA_AUDIO_SC_INT |         \
310                                  MACEISA_AUDIO1_DMAT_INT |      \
311                                  MACEISA_AUDIO1_OF_INT |        \
312                                  MACEISA_AUDIO2_DMAT_INT |      \
313                                  MACEISA_AUDIO2_MERR_INT |      \
314                                  MACEISA_AUDIO3_DMAT_INT |      \
315                                  MACEISA_AUDIO3_MERR_INT)
316 #define MACEISA_MISC_INT        (MACEISA_RTC_INT |              \
317                                  MACEISA_KEYB_INT |             \
318                                  MACEISA_KEYB_POLL_INT |        \
319                                  MACEISA_MOUSE_INT |            \
320                                  MACEISA_MOUSE_POLL_INT |       \
321                                  MACEISA_TIMER0_INT |           \
322                                  MACEISA_TIMER1_INT |           \
323                                  MACEISA_TIMER2_INT)
324 #define MACEISA_SUPERIO_INT     (MACEISA_PARALLEL_INT |         \
325                                  MACEISA_PAR_CTXA_INT |         \
326                                  MACEISA_PAR_CTXB_INT |         \
327                                  MACEISA_PAR_MERR_INT |         \
328                                  MACEISA_SERIAL1_INT |          \
329                                  MACEISA_SERIAL1_TDMAT_INT |    \
330                                  MACEISA_SERIAL1_TDMAPR_INT |   \
331                                  MACEISA_SERIAL1_TDMAME_INT |   \
332                                  MACEISA_SERIAL1_RDMAT_INT |    \
333                                  MACEISA_SERIAL1_RDMAOR_INT |   \
334                                  MACEISA_SERIAL2_INT |          \
335                                  MACEISA_SERIAL2_TDMAT_INT |    \
336                                  MACEISA_SERIAL2_TDMAPR_INT |   \
337                                  MACEISA_SERIAL2_TDMAME_INT |   \
338                                  MACEISA_SERIAL2_RDMAT_INT |    \
339                                  MACEISA_SERIAL2_RDMAOR_INT)
340
341 static unsigned long maceisa_mask;
342
343 static void enable_maceisa_irq (unsigned int irq)
344 {
345         unsigned int crime_int = 0;
346         unsigned long flags;
347
348         DBG ("maceisa enable: %u\n", irq);
349
350         switch (irq) {
351         case MACEISA_AUDIO_SW_IRQ ... MACEISA_AUDIO3_MERR_IRQ:
352                 crime_int = MACE_AUDIO_INT;
353                 break;
354         case MACEISA_RTC_IRQ ... MACEISA_TIMER2_IRQ:
355                 crime_int = MACE_MISC_INT;
356                 break;
357         case MACEISA_PARALLEL_IRQ ... MACEISA_SERIAL2_RDMAOR_IRQ:
358                 crime_int = MACE_SUPERIO_INT;
359                 break;
360         }
361         DBG ("crime_int %08x enabled\n", crime_int);
362         spin_lock_irqsave(&ip32_irq_lock, flags);
363         crime_mask |= crime_int;
364         crime_write(crime_mask, CRIME_INT_MASK);
365         maceisa_mask |= 1 << (irq - 33);
366         mace_perif_ctrl_write(maceisa_mask, imask);
367         spin_unlock_irqrestore(&ip32_irq_lock, flags);
368 }
369
370 static unsigned int startup_maceisa_irq(unsigned int irq)
371 {
372         enable_maceisa_irq(irq);
373         return 0;
374 }
375
376 static void disable_maceisa_irq(unsigned int irq)
377 {
378         unsigned int crime_int = 0;
379         unsigned long flags;
380
381         spin_lock_irqsave(&ip32_irq_lock, flags);
382         maceisa_mask &= ~(1 << (irq - 33));
383         if(!(maceisa_mask & MACEISA_AUDIO_INT))
384                 crime_int |= MACE_AUDIO_INT;
385         if(!(maceisa_mask & MACEISA_MISC_INT))
386                 crime_int |= MACE_MISC_INT;
387         if(!(maceisa_mask & MACEISA_SUPERIO_INT))
388                 crime_int |= MACE_SUPERIO_INT;
389         crime_mask &= ~crime_int;
390         crime_write(crime_mask, CRIME_INT_MASK);
391         flush_crime_bus();
392         mace_perif_ctrl_write(maceisa_mask, imask);
393         flush_mace_bus();
394         spin_unlock_irqrestore(&ip32_irq_lock, flags);
395 }
396
397 static void mask_and_ack_maceisa_irq(unsigned int irq)
398 {
399         unsigned long mace_int, flags;
400
401         switch (irq) {
402         case MACEISA_PARALLEL_IRQ:
403         case MACEISA_SERIAL1_TDMAPR_IRQ:
404         case MACEISA_SERIAL2_TDMAPR_IRQ:
405                 /* edge triggered */
406                 spin_lock_irqsave(&ip32_irq_lock, flags);
407                 mace_int = mace_perif_ctrl_read(istat);
408                 mace_int &= ~(1 << (irq - 33));
409                 mace_perif_ctrl_write(mace_int, istat);
410                 spin_unlock_irqrestore(&ip32_irq_lock, flags);
411                 break;
412         }
413         disable_maceisa_irq(irq);
414 }
415
416 static void end_maceisa_irq(unsigned irq)
417 {
418         if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
419                 enable_maceisa_irq(irq);
420 }
421
422 #define shutdown_maceisa_irq disable_maceisa_irq
423
424 static struct hw_interrupt_type ip32_maceisa_interrupt = {
425         "IP32 MACE ISA",
426         startup_maceisa_irq,
427         shutdown_maceisa_irq,
428         enable_maceisa_irq,
429         disable_maceisa_irq,
430         mask_and_ack_maceisa_irq,
431         end_maceisa_irq,
432         NULL
433 };
434
435 /* This is used for regular non-ISA, non-PCI MACE interrupts.  That means
436  * bits 0-3 and 7 in the CRIME register.
437  */
438
439 static void enable_mace_irq(unsigned int irq)
440 {
441         unsigned long flags;
442
443         spin_lock_irqsave(&ip32_irq_lock, flags);
444         crime_mask |= 1 << (irq - 1);
445         crime_write(crime_mask, CRIME_INT_MASK);
446         spin_unlock_irqrestore(&ip32_irq_lock, flags);
447 }
448
449 static unsigned int startup_mace_irq(unsigned int irq)
450 {
451         enable_mace_irq(irq);
452         return 0;
453 }
454
455 static void disable_mace_irq(unsigned int irq)
456 {
457         unsigned long flags;
458
459         spin_lock_irqsave(&ip32_irq_lock, flags);
460         crime_mask &= ~(1 << (irq - 1));
461         crime_write(crime_mask, CRIME_INT_MASK);
462         flush_crime_bus();
463         spin_unlock_irqrestore(&ip32_irq_lock, flags);
464 }
465
466 static void end_mace_irq(unsigned int irq)
467 {
468         if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
469                 enable_mace_irq(irq);
470 }
471
472 #define shutdown_mace_irq disable_mace_irq
473 #define mask_and_ack_mace_irq disable_mace_irq
474
475 static struct hw_interrupt_type ip32_mace_interrupt = {
476         "IP32 MACE",
477         startup_mace_irq,
478         shutdown_mace_irq,
479         enable_mace_irq,
480         disable_mace_irq,
481         mask_and_ack_mace_irq,
482         end_mace_irq,
483         NULL
484 };
485
486 static void ip32_unknown_interrupt(struct pt_regs *regs)
487 {
488         uint64_t crime;
489
490         printk ("Unknown interrupt occurred!\n");
491         printk ("cp0_status: %08x\n", read_c0_status());
492         printk ("cp0_cause: %08x\n", read_c0_cause());
493         crime = crime_read(CRIME_INT_MASK);
494         printk ("CRIME intr mask: %016lx\n", crime);
495         crime = crime_read(CRIME_INT_STAT);
496         printk ("CRIME intr status: %016lx\n", crime);
497         crime = crime_read(CRIME_HARD_INT);
498         printk ("CRIME hardware intr register: %016lx\n", crime);
499         printk ("MACE ISA intr mask: %08lx\n", mace_perif_ctrl_read(imask));
500         printk ("MACE ISA intr status: %08lx\n", mace_perif_ctrl_read(istat));
501         printk ("MACE PCI control register: %08x\n", mace->pci.control);
502
503         printk("Register dump:\n");
504         show_regs(regs);
505
506         printk("Please mail this report to linux-mips@linux-mips.org\n");
507         printk("Spinning...");
508         while(1) ;
509 }
510
511 /* CRIME 1.1 appears to deliver all interrupts to this one pin. */
512 /* change this to loop over all edge-triggered irqs, exception masked out ones */
513 void ip32_irq0(struct pt_regs *regs)
514 {
515         uint64_t crime_int;
516         int irq = 0;
517
518         crime_int = crime_read(CRIME_INT_STAT) & crime_mask;
519         irq = ffs(crime_int);
520         crime_int = 1 << (irq - 1);
521
522         if (crime_int & CRIME_MACEISA_INT_MASK) {
523                 unsigned long mace_int = mace_perif_ctrl_read(istat);
524                 irq = ffs(mace_int & maceisa_mask) + 32;
525         }
526         DBG("*irq %u*\n", irq);
527         do_IRQ(irq, regs);
528 }
529
530 void ip32_irq1(struct pt_regs *regs)
531 {
532         ip32_unknown_interrupt(regs);
533 }
534
535 void ip32_irq2(struct pt_regs *regs)
536 {
537         ip32_unknown_interrupt(regs);
538 }
539
540 void ip32_irq3(struct pt_regs *regs)
541 {
542         ip32_unknown_interrupt(regs);
543 }
544
545 void ip32_irq4(struct pt_regs *regs)
546 {
547         ip32_unknown_interrupt(regs);
548 }
549
550 void ip32_irq5(struct pt_regs *regs)
551 {
552         ll_timer_interrupt(IP32_R4K_TIMER_IRQ, regs);
553 }
554
555 void __init init_IRQ(void)
556 {
557         unsigned int irq;
558
559         init_generic_irq();
560         /* Install our interrupt handler, then clear and disable all
561          * CRIME and MACE interrupts. */
562         crime_write(0, CRIME_INT_MASK);
563         crime_write(0, CRIME_HARD_INT);
564         crime_write(0, CRIME_SOFT_INT);
565         mace_perif_ctrl_write(0, istat);
566         mace_perif_ctrl_write(0, imask);
567         set_except_vector(0, ip32_handle_int);
568
569         for (irq = 0; irq <= IP32_IRQ_MAX; irq++) {
570                 hw_irq_controller *controller;
571
572                 if (irq == IP32_R4K_TIMER_IRQ)
573                         controller = &ip32_cpu_interrupt;
574                 else if (irq <= MACE_PCI_BRIDGE_IRQ && irq >= MACE_VID_IN1_IRQ)
575                         controller = &ip32_mace_interrupt;
576                 else if (irq <= MACEPCI_SHARED2_IRQ && irq >= MACEPCI_SCSI0_IRQ)
577                         controller = &ip32_macepci_interrupt;
578                 else if (irq <= CRIME_VICE_IRQ && irq >= CRIME_GBE0_IRQ)
579                         controller = &ip32_crime_interrupt;
580                 else
581                         controller = &ip32_maceisa_interrupt;
582
583                 irq_desc[irq].status = IRQ_DISABLED;
584                 irq_desc[irq].action = 0;
585                 irq_desc[irq].depth = 0;
586                 irq_desc[irq].handler = controller;
587         }
588         setup_irq(CRIME_MEMERR_IRQ, &memerr_irq);
589         setup_irq(CRIME_CPUERR_IRQ, &cpuerr_irq);
590
591 #define ALLINTS (IE_IRQ0 | IE_IRQ1 | IE_IRQ2 | IE_IRQ3 | IE_IRQ4 | IE_IRQ5)
592         change_c0_status(ST0_IM, ALLINTS);
593 }