]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/m68k/q40/q40ints.c
[PATCH] m68k: introduce irq controller
[linux-2.6-omap-h63xx.git] / arch / m68k / q40 / q40ints.c
1 /*
2  * arch/m68k/q40/q40ints.c
3  *
4  * Copyright (C) 1999,2001 Richard Zidlicky
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file COPYING in the main directory of this archive
8  * for more details.
9  *
10  * .. used to be loosely based on bvme6000ints.c
11  *
12  */
13
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/sched.h>
19 #include <linux/seq_file.h>
20 #include <linux/interrupt.h>
21 #include <linux/hardirq.h>
22
23 #include <asm/rtc.h>
24 #include <asm/ptrace.h>
25 #include <asm/system.h>
26 #include <asm/irq.h>
27 #include <asm/traps.h>
28
29 #include <asm/q40_master.h>
30 #include <asm/q40ints.h>
31
32 /*
33  * Q40 IRQs are defined as follows:
34  *            3,4,5,6,7,10,11,14,15 : ISA dev IRQs
35  *            16-31: reserved
36  *            32   : keyboard int
37  *            33   : frame int (50/200 Hz periodic timer)
38  *            34   : sample int (10/20 KHz periodic timer)
39  *
40 */
41
42 extern int ints_inited;
43
44
45 irqreturn_t q40_irq2_handler (int, void *, struct pt_regs *fp);
46
47
48 static irqreturn_t q40_defhand (int irq, void *dev_id, struct pt_regs *fp);
49
50
51 #define DEVNAME_SIZE 24
52
53 static struct q40_irq_node {
54         irqreturn_t     (*handler)(int, void *, struct pt_regs *);
55         unsigned long   flags;
56         void            *dev_id;
57   /*        struct q40_irq_node *next;*/
58         char            devname[DEVNAME_SIZE];
59         unsigned        count;
60         unsigned short  state;
61 } irq_tab[Q40_IRQ_MAX+1];
62
63 short unsigned q40_ablecount[Q40_IRQ_MAX+1];
64
65 /*
66  * void q40_init_IRQ (void)
67  *
68  * Parameters:  None
69  *
70  * Returns:     Nothing
71  *
72  * This function is called during kernel startup to initialize
73  * the q40 IRQ handling routines.
74  */
75
76 static int disabled=0;
77
78 void q40_init_IRQ (void)
79 {
80         int i;
81
82         disabled=0;
83         for (i = 0; i <= Q40_IRQ_MAX; i++) {
84                 irq_tab[i].handler = q40_defhand;
85                 irq_tab[i].flags = 0;
86                 irq_tab[i].dev_id = NULL;
87                 /*              irq_tab[i].next = NULL;*/
88                 irq_tab[i].devname[0] = 0;
89                 irq_tab[i].count = 0;
90                 irq_tab[i].state =0;
91                 q40_ablecount[i]=0;   /* all enabled */
92         }
93
94         /* setup handler for ISA ints */
95         cpu_request_irq(IRQ_AUTO_2, q40_irq2_handler, 0,
96                         "q40 ISA and master chip", NULL);
97
98         /* now enable some ints.. */
99         master_outb(1,EXT_ENABLE_REG);  /* ISA IRQ 5-15 */
100
101         /* make sure keyboard IRQ is disabled */
102         master_outb(0,KEY_IRQ_ENABLE_REG);
103 }
104
105 int q40_request_irq(unsigned int irq,
106                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
107                 unsigned long flags, const char *devname, void *dev_id)
108 {
109   /*printk("q40_request_irq %d, %s\n",irq,devname);*/
110
111         if (irq > Q40_IRQ_MAX || (irq>15 && irq<32)) {
112                 printk("%s: Incorrect IRQ %d from %s\n", __FUNCTION__, irq, devname);
113                 return -ENXIO;
114         }
115
116         /* test for ISA ints not implemented by HW */
117         switch (irq)
118           {
119           case 1: case 2: case 8: case 9:
120           case 12: case 13:
121             printk("%s: ISA IRQ %d from %s not implemented by HW\n", __FUNCTION__, irq, devname);
122             return -ENXIO;
123           case 11:
124             printk("warning IRQ 10 and 11 not distinguishable\n");
125             irq=10;
126           default:
127             ;
128           }
129
130         if (irq<Q40_IRQ_SAMPLE)
131           {
132             if (irq_tab[irq].dev_id != NULL)
133                   {
134                     printk("%s: IRQ %d from %s is not replaceable\n",
135                            __FUNCTION__, irq, irq_tab[irq].devname);
136                     return -EBUSY;
137                   }
138             /*printk("IRQ %d set to handler %p\n",irq,handler);*/
139             if (dev_id==NULL)
140                   {
141                 printk("WARNING: dev_id == NULL in request_irq\n");
142                 dev_id=(void*)1;
143               }
144             irq_tab[irq].handler = handler;
145             irq_tab[irq].flags   = flags;
146             irq_tab[irq].dev_id  = dev_id;
147             strlcpy(irq_tab[irq].devname,devname,sizeof(irq_tab[irq].devname));
148             irq_tab[irq].state = 0;
149             return 0;
150           }
151         else {
152           /* Q40_IRQ_SAMPLE :somewhat special actions required here ..*/
153           cpu_request_irq(4, handler, flags, devname, dev_id);
154           cpu_request_irq(6, handler, flags, devname, dev_id);
155           return 0;
156         }
157 }
158
159 void q40_free_irq(unsigned int irq, void *dev_id)
160 {
161         if (irq > Q40_IRQ_MAX || (irq>15 && irq<32)) {
162                 printk("%s: Incorrect IRQ %d, dev_id %x \n", __FUNCTION__, irq, (unsigned)dev_id);
163                 return;
164         }
165
166         /* test for ISA ints not implemented by HW */
167         switch (irq)
168           {
169           case 1: case 2: case 8: case 9:
170           case 12: case 13:
171             printk("%s: ISA IRQ %d from %x invalid\n", __FUNCTION__, irq, (unsigned)dev_id);
172             return;
173           case 11: irq=10;
174           default:
175             ;
176           }
177
178         if (irq<Q40_IRQ_SAMPLE)
179           {
180             if (irq_tab[irq].dev_id != dev_id)
181               printk("%s: Removing probably wrong IRQ %d from %s\n",
182                      __FUNCTION__, irq, irq_tab[irq].devname);
183
184             irq_tab[irq].handler = q40_defhand;
185             irq_tab[irq].flags   = 0;
186             irq_tab[irq].dev_id  = NULL;
187             /* irq_tab[irq].devname = NULL; */
188             /* do not reset state !! */
189           }
190         else
191           { /* == Q40_IRQ_SAMPLE */
192             cpu_free_irq(4, dev_id);
193             cpu_free_irq(6, dev_id);
194           }
195 }
196
197
198 irqreturn_t q40_process_int (int level, struct pt_regs *fp)
199 {
200   printk("unexpected interrupt vec=%x, pc=%lx, d0=%lx, d0_orig=%lx, d1=%lx, d2=%lx\n",
201           level, fp->pc, fp->d0, fp->orig_d0, fp->d1, fp->d2);
202   printk("\tIIRQ_REG = %x, EIRQ_REG = %x\n",master_inb(IIRQ_REG),master_inb(EIRQ_REG));
203   return IRQ_HANDLED;
204 }
205
206 /*
207  * this stuff doesn't really belong here..
208 */
209
210 int ql_ticks;              /* 200Hz ticks since last jiffie */
211 static int sound_ticks;
212
213 #define SVOL 45
214
215 void q40_mksound(unsigned int hz, unsigned int ticks)
216 {
217   /* for now ignore hz, except that hz==0 switches off sound */
218   /* simply alternate the ampl (128-SVOL)-(128+SVOL)-..-.. at 200Hz */
219   if (hz==0)
220     {
221       if (sound_ticks)
222         sound_ticks=1;
223
224       *DAC_LEFT=128;
225       *DAC_RIGHT=128;
226
227       return;
228     }
229   /* sound itself is done in q40_timer_int */
230   if (sound_ticks == 0) sound_ticks=1000; /* pretty long beep */
231   sound_ticks=ticks<<1;
232 }
233
234 static irqreturn_t (*q40_timer_routine)(int, void *, struct pt_regs *);
235
236 static irqreturn_t q40_timer_int (int irq, void * dev, struct pt_regs * regs)
237 {
238     ql_ticks = ql_ticks ? 0 : 1;
239     if (sound_ticks)
240       {
241         unsigned char sval=(sound_ticks & 1) ? 128-SVOL : 128+SVOL;
242         sound_ticks--;
243         *DAC_LEFT=sval;
244         *DAC_RIGHT=sval;
245       }
246
247     if (!ql_ticks)
248         q40_timer_routine(irq, dev, regs);
249     return IRQ_HANDLED;
250 }
251
252 void q40_sched_init (irqreturn_t (*timer_routine)(int, void *, struct pt_regs *))
253 {
254     int timer_irq;
255
256     q40_timer_routine = timer_routine;
257     timer_irq=Q40_IRQ_FRAME;
258
259     if (request_irq(timer_irq, q40_timer_int, 0,
260                                 "timer", q40_timer_int))
261         panic ("Couldn't register timer int");
262
263     master_outb(-1,FRAME_CLEAR_REG);
264     master_outb( 1,FRAME_RATE_REG);
265 }
266
267
268 /*
269  * tables to translate bits into IRQ numbers
270  * it is a good idea to order the entries by priority
271  *
272 */
273
274 struct IRQ_TABLE{ unsigned mask; int irq ;};
275 #if 0
276 static struct IRQ_TABLE iirqs[]={
277   {Q40_IRQ_FRAME_MASK,Q40_IRQ_FRAME},
278   {Q40_IRQ_KEYB_MASK,Q40_IRQ_KEYBOARD},
279   {0,0}};
280 #endif
281 static struct IRQ_TABLE eirqs[] = {
282   { .mask = Q40_IRQ3_MASK,      .irq = 3 },     /* ser 1 */
283   { .mask = Q40_IRQ4_MASK,      .irq = 4 },     /* ser 2 */
284   { .mask = Q40_IRQ14_MASK,     .irq = 14 },    /* IDE 1 */
285   { .mask = Q40_IRQ15_MASK,     .irq = 15 },    /* IDE 2 */
286   { .mask = Q40_IRQ6_MASK,      .irq = 6 },     /* floppy, handled elsewhere */
287   { .mask = Q40_IRQ7_MASK,      .irq = 7 },     /* par */
288   { .mask = Q40_IRQ5_MASK,      .irq = 5 },
289   { .mask = Q40_IRQ10_MASK,     .irq = 10 },
290   {0,0}
291 };
292
293 /* complain only this many times about spurious ints : */
294 static int ccleirq=60;    /* ISA dev IRQ's*/
295 /*static int cclirq=60;*/     /* internal */
296
297 /* FIXME: add shared ints,mask,unmask,probing.... */
298
299 #define IRQ_INPROGRESS 1
300 /*static unsigned short saved_mask;*/
301 //static int do_tint=0;
302
303 #define DEBUG_Q40INT
304 /*#define IP_USE_DISABLE *//* would be nice, but crashes ???? */
305
306 static int mext_disabled=0;  /* ext irq disabled by master chip? */
307 static int aliased_irq=0;  /* how many times inside handler ?*/
308
309
310 /* got level 2 interrupt, dispatch to ISA or keyboard/timer IRQs */
311 irqreturn_t q40_irq2_handler (int vec, void *devname, struct pt_regs *fp)
312 {
313   unsigned mir, mer;
314   int irq,i;
315
316 //repeat:
317   mir=master_inb(IIRQ_REG);
318   if (mir&Q40_IRQ_FRAME_MASK) {
319           irq_tab[Q40_IRQ_FRAME].count++;
320           irq_tab[Q40_IRQ_FRAME].handler(Q40_IRQ_FRAME,irq_tab[Q40_IRQ_FRAME].dev_id,fp);
321           master_outb(-1,FRAME_CLEAR_REG);
322   }
323   if ((mir&Q40_IRQ_SER_MASK) || (mir&Q40_IRQ_EXT_MASK)) {
324           mer=master_inb(EIRQ_REG);
325           for (i=0; eirqs[i].mask; i++) {
326                   if (mer&(eirqs[i].mask)) {
327                           irq=eirqs[i].irq;
328 /*
329  * There is a little mess wrt which IRQ really caused this irq request. The
330  * main problem is that IIRQ_REG and EIRQ_REG reflect the state when they
331  * are read - which is long after the request came in. In theory IRQs should
332  * not just go away but they occassionally do
333  */
334                           if (irq>4 && irq<=15 && mext_disabled) {
335                                   /*aliased_irq++;*/
336                                   goto iirq;
337                           }
338                           if (irq_tab[irq].handler == q40_defhand ) {
339                                   printk("handler for IRQ %d not defined\n",irq);
340                                   continue; /* ignore uninited INTs :-( */
341                           }
342                           if ( irq_tab[irq].state & IRQ_INPROGRESS ) {
343                                   /* some handlers do local_irq_enable() for irq latency reasons, */
344                                   /* however reentering an active irq handler is not permitted */
345 #ifdef IP_USE_DISABLE
346                                   /* in theory this is the better way to do it because it still */
347                                   /* lets through eg the serial irqs, unfortunately it crashes */
348                                   disable_irq(irq);
349                                   disabled=1;
350 #else
351                                   /*printk("IRQ_INPROGRESS detected for irq %d, disabling - %s disabled\n",irq,disabled ? "already" : "not yet"); */
352                                   fp->sr = (((fp->sr) & (~0x700))+0x200);
353                                   disabled=1;
354 #endif
355                                   goto iirq;
356                           }
357                           irq_tab[irq].count++;
358                           irq_tab[irq].state |= IRQ_INPROGRESS;
359                           irq_tab[irq].handler(irq,irq_tab[irq].dev_id,fp);
360                           irq_tab[irq].state &= ~IRQ_INPROGRESS;
361
362                           /* naively enable everything, if that fails than    */
363                           /* this function will be reentered immediately thus */
364                           /* getting another chance to disable the IRQ        */
365
366                           if ( disabled ) {
367 #ifdef IP_USE_DISABLE
368                                   if (irq>4){
369                                           disabled=0;
370                                           enable_irq(irq);}
371 #else
372                                   disabled=0;
373                                   /*printk("reenabling irq %d\n",irq); */
374 #endif
375                           }
376 // used to do 'goto repeat;' here, this delayed bh processing too long
377                           return IRQ_HANDLED;
378                   }
379           }
380           if (mer && ccleirq>0 && !aliased_irq)
381                   printk("ISA interrupt from unknown source? EIRQ_REG = %x\n",mer),ccleirq--;
382   }
383  iirq:
384   mir=master_inb(IIRQ_REG);
385   /* should test whether keyboard irq is really enabled, doing it in defhand */
386   if (mir&Q40_IRQ_KEYB_MASK) {
387           irq_tab[Q40_IRQ_KEYBOARD].count++;
388           irq_tab[Q40_IRQ_KEYBOARD].handler(Q40_IRQ_KEYBOARD,irq_tab[Q40_IRQ_KEYBOARD].dev_id,fp);
389   }
390   return IRQ_HANDLED;
391 }
392
393 int show_q40_interrupts (struct seq_file *p, void *v)
394 {
395         int i;
396
397         for (i = 0; i <= Q40_IRQ_MAX; i++) {
398                 if (irq_tab[i].count)
399                       seq_printf(p, "%sIRQ %02d: %8d  %s%s\n",
400                               (i<=15) ? "ISA-" : "    " ,
401                             i, irq_tab[i].count,
402                             irq_tab[i].devname[0] ? irq_tab[i].devname : "?",
403                             irq_tab[i].handler == q40_defhand ?
404                                         " (now unassigned)" : "");
405         }
406         return 0;
407 }
408
409
410 static irqreturn_t q40_defhand (int irq, void *dev_id, struct pt_regs *fp)
411 {
412         if (irq!=Q40_IRQ_KEYBOARD)
413              printk ("Unknown q40 interrupt %d\n", irq);
414         else master_outb(-1,KEYBOARD_UNLOCK_REG);
415         return IRQ_NONE;
416 }
417
418
419 void q40_enable_irq (unsigned int irq)
420 {
421   if ( irq>=5 && irq<=15 )
422   {
423     mext_disabled--;
424     if (mext_disabled>0)
425           printk("q40_enable_irq : nested disable/enable\n");
426     if (mext_disabled==0)
427     master_outb(1,EXT_ENABLE_REG);
428     }
429 }
430
431
432 void q40_disable_irq (unsigned int irq)
433 {
434   /* disable ISA iqs : only do something if the driver has been
435    * verified to be Q40 "compatible" - right now IDE, NE2K
436    * Any driver should not attempt to sleep across disable_irq !!
437    */
438
439   if ( irq>=5 && irq<=15 ) {
440     master_outb(0,EXT_ENABLE_REG);
441     mext_disabled++;
442     if (mext_disabled>1) printk("disable_irq nesting count %d\n",mext_disabled);
443   }
444 }
445
446 unsigned long q40_probe_irq_on (void)
447 {
448   printk("irq probing not working - reconfigure the driver to avoid this\n");
449   return -1;
450 }
451 int q40_probe_irq_off (unsigned long irqs)
452 {
453   return -1;
454 }
455 /*
456  * Local variables:
457  * compile-command: "m68k-linux-gcc -D__KERNEL__ -I/home/rz/lx/linux-2.2.6/include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -pipe -fno-strength-reduce -ffixed-a2 -m68040   -c -o q40ints.o q40ints.c"
458  * End:
459  */