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