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