GNU Linux-libre 4.4.284-gnu1
[releases.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/interrupt.h>
18 #include <linux/irq.h>
19
20 #include <asm/ptrace.h>
21 #include <asm/traps.h>
22
23 #include <asm/q40_master.h>
24 #include <asm/q40ints.h>
25
26 /*
27  * Q40 IRQs are defined as follows:
28  *            3,4,5,6,7,10,11,14,15 : ISA dev IRQs
29  *            16-31: reserved
30  *            32   : keyboard int
31  *            33   : frame int (50/200 Hz periodic timer)
32  *            34   : sample int (10/20 KHz periodic timer)
33  *
34 */
35
36 static void q40_irq_handler(unsigned int, struct pt_regs *fp);
37 static void q40_irq_enable(struct irq_data *data);
38 static void q40_irq_disable(struct irq_data *data);
39
40 unsigned short q40_ablecount[35];
41 unsigned short q40_state[35];
42
43 static unsigned int q40_irq_startup(struct irq_data *data)
44 {
45         unsigned int irq = data->irq;
46
47         /* test for ISA ints not implemented by HW */
48         switch (irq) {
49         case 1: case 2: case 8: case 9:
50         case 11: case 12: case 13:
51                 printk("%s: ISA IRQ %d not implemented by HW\n", __func__, irq);
52                 /* FIXME return -ENXIO; */
53         }
54         return 0;
55 }
56
57 static void q40_irq_shutdown(struct irq_data *data)
58 {
59 }
60
61 static struct irq_chip q40_irq_chip = {
62         .name           = "q40",
63         .irq_startup    = q40_irq_startup,
64         .irq_shutdown   = q40_irq_shutdown,
65         .irq_enable     = q40_irq_enable,
66         .irq_disable    = q40_irq_disable,
67 };
68
69 /*
70  * void q40_init_IRQ (void)
71  *
72  * Parameters:  None
73  *
74  * Returns:     Nothing
75  *
76  * This function is called during kernel startup to initialize
77  * the q40 IRQ handling routines.
78  */
79
80 static int disabled;
81
82 void __init q40_init_IRQ(void)
83 {
84         m68k_setup_irq_controller(&q40_irq_chip, handle_simple_irq, 1,
85                                   Q40_IRQ_MAX);
86
87         /* setup handler for ISA ints */
88         m68k_setup_auto_interrupt(q40_irq_handler);
89
90         m68k_irq_startup_irq(IRQ_AUTO_2);
91         m68k_irq_startup_irq(IRQ_AUTO_4);
92
93         /* now enable some ints.. */
94         master_outb(1, EXT_ENABLE_REG);  /* ISA IRQ 5-15 */
95
96         /* make sure keyboard IRQ is disabled */
97         master_outb(0, KEY_IRQ_ENABLE_REG);
98 }
99
100
101 /*
102  * this stuff doesn't really belong here..
103  */
104
105 int ql_ticks;              /* 200Hz ticks since last jiffie */
106 static int sound_ticks;
107
108 #define SVOL 45
109
110 void q40_mksound(unsigned int hz, unsigned int ticks)
111 {
112         /* for now ignore hz, except that hz==0 switches off sound */
113         /* simply alternate the ampl (128-SVOL)-(128+SVOL)-..-.. at 200Hz */
114         if (hz == 0) {
115                 if (sound_ticks)
116                         sound_ticks = 1;
117
118                 *DAC_LEFT = 128;
119                 *DAC_RIGHT = 128;
120
121                 return;
122         }
123         /* sound itself is done in q40_timer_int */
124         if (sound_ticks == 0)
125                 sound_ticks = 1000; /* pretty long beep */
126         sound_ticks = ticks << 1;
127 }
128
129 static irqreturn_t q40_timer_int(int irq, void *dev_id)
130 {
131         irq_handler_t timer_routine = dev_id;
132
133         ql_ticks = ql_ticks ? 0 : 1;
134         if (sound_ticks) {
135                 unsigned char sval=(sound_ticks & 1) ? 128-SVOL : 128+SVOL;
136                 sound_ticks--;
137                 *DAC_LEFT=sval;
138                 *DAC_RIGHT=sval;
139         }
140
141         if (!ql_ticks) {
142                 unsigned long flags;
143
144                 local_irq_save(flags);
145                 timer_routine(0, NULL);
146                 local_irq_restore(flags);
147         }
148         return IRQ_HANDLED;
149 }
150
151 void q40_sched_init (irq_handler_t timer_routine)
152 {
153         int timer_irq;
154
155         timer_irq = Q40_IRQ_FRAME;
156
157         if (request_irq(timer_irq, q40_timer_int, 0, "timer", timer_routine))
158                 panic("Couldn't register timer int");
159
160         master_outb(-1, FRAME_CLEAR_REG);
161         master_outb( 1, FRAME_RATE_REG);
162 }
163
164
165 /*
166  * tables to translate bits into IRQ numbers
167  * it is a good idea to order the entries by priority
168  *
169 */
170
171 struct IRQ_TABLE{ unsigned mask; int irq ;};
172 #if 0
173 static struct IRQ_TABLE iirqs[]={
174   {Q40_IRQ_FRAME_MASK,Q40_IRQ_FRAME},
175   {Q40_IRQ_KEYB_MASK,Q40_IRQ_KEYBOARD},
176   {0,0}};
177 #endif
178 static struct IRQ_TABLE eirqs[] = {
179   { .mask = Q40_IRQ3_MASK,      .irq = 3 },     /* ser 1 */
180   { .mask = Q40_IRQ4_MASK,      .irq = 4 },     /* ser 2 */
181   { .mask = Q40_IRQ14_MASK,     .irq = 14 },    /* IDE 1 */
182   { .mask = Q40_IRQ15_MASK,     .irq = 15 },    /* IDE 2 */
183   { .mask = Q40_IRQ6_MASK,      .irq = 6 },     /* floppy, handled elsewhere */
184   { .mask = Q40_IRQ7_MASK,      .irq = 7 },     /* par */
185   { .mask = Q40_IRQ5_MASK,      .irq = 5 },
186   { .mask = Q40_IRQ10_MASK,     .irq = 10 },
187   {0,0}
188 };
189
190 /* complain only this many times about spurious ints : */
191 static int ccleirq=60;    /* ISA dev IRQs*/
192 /*static int cclirq=60;*/     /* internal */
193
194 /* FIXME: add shared ints,mask,unmask,probing.... */
195
196 #define IRQ_INPROGRESS 1
197 /*static unsigned short saved_mask;*/
198 //static int do_tint=0;
199
200 #define DEBUG_Q40INT
201 /*#define IP_USE_DISABLE *//* would be nice, but crashes ???? */
202
203 static int mext_disabled=0;  /* ext irq disabled by master chip? */
204 static int aliased_irq=0;  /* how many times inside handler ?*/
205
206
207 /* got interrupt, dispatch to ISA or keyboard/timer IRQs */
208 static void q40_irq_handler(unsigned int irq, struct pt_regs *fp)
209 {
210         unsigned mir, mer;
211         int i;
212
213 //repeat:
214         mir = master_inb(IIRQ_REG);
215 #ifdef CONFIG_BLK_DEV_FD
216         if ((mir & Q40_IRQ_EXT_MASK) &&
217             (master_inb(EIRQ_REG) & Q40_IRQ6_MASK)) {
218                 floppy_hardint();
219                 return;
220         }
221 #endif
222         switch (irq) {
223         case 4:
224         case 6:
225                 do_IRQ(Q40_IRQ_SAMPLE, fp);
226                 return;
227         }
228         if (mir & Q40_IRQ_FRAME_MASK) {
229                 do_IRQ(Q40_IRQ_FRAME, fp);
230                 master_outb(-1, FRAME_CLEAR_REG);
231         }
232         if ((mir & Q40_IRQ_SER_MASK) || (mir & Q40_IRQ_EXT_MASK)) {
233                 mer = master_inb(EIRQ_REG);
234                 for (i = 0; eirqs[i].mask; i++) {
235                         if (mer & eirqs[i].mask) {
236                                 irq = eirqs[i].irq;
237 /*
238  * There is a little mess wrt which IRQ really caused this irq request. The
239  * main problem is that IIRQ_REG and EIRQ_REG reflect the state when they
240  * are read - which is long after the request came in. In theory IRQs should
241  * not just go away but they occasionally do
242  */
243                                 if (irq > 4 && irq <= 15 && mext_disabled) {
244                                         /*aliased_irq++;*/
245                                         goto iirq;
246                                 }
247                                 if (q40_state[irq] & IRQ_INPROGRESS) {
248                                         /* some handlers do local_irq_enable() for irq latency reasons, */
249                                         /* however reentering an active irq handler is not permitted */
250 #ifdef IP_USE_DISABLE
251                                         /* in theory this is the better way to do it because it still */
252                                         /* lets through eg the serial irqs, unfortunately it crashes */
253                                         disable_irq(irq);
254                                         disabled = 1;
255 #else
256                                         /*printk("IRQ_INPROGRESS detected for irq %d, disabling - %s disabled\n",
257                                                 irq, disabled ? "already" : "not yet"); */
258                                         fp->sr = (((fp->sr) & (~0x700))+0x200);
259                                         disabled = 1;
260 #endif
261                                         goto iirq;
262                                 }
263                                 q40_state[irq] |= IRQ_INPROGRESS;
264                                 do_IRQ(irq, fp);
265                                 q40_state[irq] &= ~IRQ_INPROGRESS;
266
267                                 /* naively enable everything, if that fails than    */
268                                 /* this function will be reentered immediately thus */
269                                 /* getting another chance to disable the IRQ        */
270
271                                 if (disabled) {
272 #ifdef IP_USE_DISABLE
273                                         if (irq > 4) {
274                                                 disabled = 0;
275                                                 enable_irq(irq);
276                                         }
277 #else
278                                         disabled = 0;
279                                         /*printk("reenabling irq %d\n", irq); */
280 #endif
281                                 }
282 // used to do 'goto repeat;' here, this delayed bh processing too long
283                                 return;
284                         }
285                 }
286                 if (mer && ccleirq > 0 && !aliased_irq) {
287                         printk("ISA interrupt from unknown source? EIRQ_REG = %x\n",mer);
288                         ccleirq--;
289                 }
290         }
291  iirq:
292         mir = master_inb(IIRQ_REG);
293         /* should test whether keyboard irq is really enabled, doing it in defhand */
294         if (mir & Q40_IRQ_KEYB_MASK)
295                 do_IRQ(Q40_IRQ_KEYBOARD, fp);
296
297         return;
298 }
299
300 void q40_irq_enable(struct irq_data *data)
301 {
302         unsigned int irq = data->irq;
303
304         if (irq >= 5 && irq <= 15) {
305                 mext_disabled--;
306                 if (mext_disabled > 0)
307                         printk("q40_irq_enable : nested disable/enable\n");
308                 if (mext_disabled == 0)
309                         master_outb(1, EXT_ENABLE_REG);
310         }
311 }
312
313
314 void q40_irq_disable(struct irq_data *data)
315 {
316         unsigned int irq = data->irq;
317
318         /* disable ISA iqs : only do something if the driver has been
319          * verified to be Q40 "compatible" - right now IDE, NE2K
320          * Any driver should not attempt to sleep across disable_irq !!
321          */
322
323         if (irq >= 5 && irq <= 15) {
324                 master_outb(0, EXT_ENABLE_REG);
325                 mext_disabled++;
326                 if (mext_disabled > 1)
327                         printk("disable_irq nesting count %d\n",mext_disabled);
328         }
329 }