1 /*
2  * linux/arch/m68k/amiga/amiints.c -- Amiga Linux interrupt handling code
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  * 11/07/96: rewritten interrupt handling, irq lists are exists now only for
9  *           this sources where it makes sense (VERTB/PORTS/EXTER) and you must
10  *           be careful that dev_id for this sources is unique since this the
11  *           only possibility to distinguish between different handlers for
12  *           free_irq. irq lists also have different irq flags:
13  *           - IRQ_FLG_FAST: handler is inserted at top of list (after other
14  *                           fast handlers)
15  *           - IRQ_FLG_SLOW: handler is inserted at bottom of list and before
16  *                           they're executed irq level is set to the previous
17  *                           one, but handlers don't need to be reentrant, if
18  *                           reentrance occurred, slow handlers will be just
19  *                           called again.
20  *           The whole interrupt handling for CIAs is moved to cia.c
21  *           /Roman Zippel
22  *
23  * 07/08/99: rewamp of the interrupt handling - we now have two types of
24  *           interrupts, normal and fast handlers, fast handlers being
25  *           marked with SA_INTERRUPT and runs with all other interrupts
26  *           disabled. Normal interrupts disable their own source but
27  *           run with all other interrupt sources enabled.
28  *           PORTS and EXTER interrupts are always shared even if the
29  *           drivers do not explicitly mark this when calling
30  *           request_irq which they really should do.
31  *           This is similar to the way interrupts are handled on all
32  *           other architectures and makes a ton of sense besides
33  *           having the advantage of making it easier to share
34  *           drivers.
35  *           /Jes
36  */
37 
38 #include <linux/types.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/kernel_stat.h>
42 #include <linux/init.h>
43 
44 #include <asm/system.h>
45 #include <asm/irq.h>
46 #include <asm/traps.h>
47 #include <asm/amigahw.h>
48 #include <asm/amigaints.h>
49 #include <asm/amipcmcia.h>
50 
51 extern int cia_request_irq(struct ciabase *base,int irq,
52                            void (*handler)(int, void *, struct pt_regs *),
53                            unsigned long flags, const char *devname, void *dev_id);
54 extern void cia_free_irq(struct ciabase *base, unsigned int irq, void *dev_id);
55 extern void cia_init_IRQ(struct ciabase *base);
56 extern int cia_get_irq_list(struct ciabase *base, char *buf);
57 
58 /* irq node variables for amiga interrupt sources */
59 static irq_node_t *ami_irq_list[AMI_STD_IRQS];
60 
61 static unsigned short amiga_intena_vals[AMI_STD_IRQS] = {
62 	IF_VERTB, IF_COPER, IF_AUD0, IF_AUD1, IF_AUD2, IF_AUD3, IF_BLIT,
63 	IF_DSKSYN, IF_DSKBLK, IF_RBF, IF_TBE, IF_SOFT, IF_PORTS, IF_EXTER
64 };
65 static const unsigned char ami_servers[AMI_STD_IRQS] = {
66 	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1
67 };
68 
69 static short ami_ablecount[AMI_IRQS];
70 
ami_badint(int irq,void * dev_id,struct pt_regs * fp)71 static void ami_badint(int irq, void *dev_id, struct pt_regs *fp)
72 {
73 	num_spurious += 1;
74 }
75 
76 /*
77  * void amiga_init_IRQ(void)
78  *
79  * Parameters:	None
80  *
81  * Returns:	Nothing
82  *
83  * This function should be called during kernel startup to initialize
84  * the amiga IRQ handling routines.
85  */
86 
amiga_init_IRQ(void)87 void __init amiga_init_IRQ(void)
88 {
89 	int i;
90 
91 	/* initialize handlers */
92 	for (i = 0; i < AMI_STD_IRQS; i++) {
93 		if (ami_servers[i]) {
94 			ami_irq_list[i] = NULL;
95 		} else {
96 			ami_irq_list[i] = new_irq_node();
97 			ami_irq_list[i]->handler = ami_badint;
98 			ami_irq_list[i]->flags   = 0;
99 			ami_irq_list[i]->dev_id  = NULL;
100 			ami_irq_list[i]->devname = NULL;
101 			ami_irq_list[i]->next    = NULL;
102 		}
103 	}
104 	for (i = 0; i < AMI_IRQS; i++)
105 		ami_ablecount[i] = 0;
106 
107 	/* turn off PCMCIA interrupts */
108 	if (AMIGAHW_PRESENT(PCMCIA))
109 		gayle.inten = GAYLE_IRQ_IDE;
110 
111 	/* turn off all interrupts and enable the master interrupt bit */
112 	custom.intena = 0x7fff;
113 	custom.intreq = 0x7fff;
114 	custom.intena = IF_SETCLR | IF_INTEN;
115 
116 	cia_init_IRQ(&ciaa_base);
117 	cia_init_IRQ(&ciab_base);
118 }
119 
amiga_insert_irq(irq_node_t ** list,irq_node_t * node)120 static inline int amiga_insert_irq(irq_node_t **list, irq_node_t *node)
121 {
122 	unsigned long flags;
123 	irq_node_t *cur;
124 
125 	if (!node->dev_id)
126 		printk("%s: Warning: dev_id of %s is zero\n",
127 		       __FUNCTION__, node->devname);
128 
129 	save_flags(flags);
130 	cli();
131 
132 	cur = *list;
133 
134 	if (node->flags & SA_INTERRUPT) {
135 		if (node->flags & SA_SHIRQ)
136 			return -EBUSY;
137 		/*
138 		 * There should never be more than one
139 		 */
140 		while (cur && cur->flags & SA_INTERRUPT) {
141 			list = &cur->next;
142 			cur = cur->next;
143 		}
144 	} else {
145 		while (cur) {
146 			list = &cur->next;
147 			cur = cur->next;
148 		}
149 	}
150 
151 	node->next = cur;
152 	*list = node;
153 
154 	restore_flags(flags);
155 	return 0;
156 }
157 
amiga_delete_irq(irq_node_t ** list,void * dev_id)158 static inline void amiga_delete_irq(irq_node_t **list, void *dev_id)
159 {
160 	unsigned long flags;
161 	irq_node_t *node;
162 
163 	save_flags(flags);
164 	cli();
165 
166 	for (node = *list; node; list = &node->next, node = *list) {
167 		if (node->dev_id == dev_id) {
168 			*list = node->next;
169 			/* Mark it as free. */
170 			node->handler = NULL;
171 			restore_flags(flags);
172 			return;
173 		}
174 	}
175 	restore_flags(flags);
176 	printk ("%s: tried to remove invalid irq\n", __FUNCTION__);
177 }
178 
179 /*
180  * amiga_request_irq : add an interrupt service routine for a particular
181  *                     machine specific interrupt source.
182  *                     If the addition was successful, it returns 0.
183  */
184 
amiga_request_irq(unsigned int irq,void (* handler)(int,void *,struct pt_regs *),unsigned long flags,const char * devname,void * dev_id)185 int amiga_request_irq(unsigned int irq,
186 		      void (*handler)(int, void *, struct pt_regs *),
187                       unsigned long flags, const char *devname, void *dev_id)
188 {
189 	irq_node_t *node;
190 	int error = 0;
191 
192 	if (irq >= AMI_IRQS) {
193 		printk ("%s: Unknown IRQ %d from %s\n", __FUNCTION__,
194 			irq, devname);
195 		return -ENXIO;
196 	}
197 
198 	if (irq >= IRQ_AMIGA_AUTO)
199 		return sys_request_irq(irq - IRQ_AMIGA_AUTO, handler,
200 		                       flags, devname, dev_id);
201 
202 	if (irq >= IRQ_AMIGA_CIAB)
203 		return cia_request_irq(&ciab_base, irq - IRQ_AMIGA_CIAB,
204 		                       handler, flags, devname, dev_id);
205 
206 	if (irq >= IRQ_AMIGA_CIAA)
207 		return cia_request_irq(&ciaa_base, irq - IRQ_AMIGA_CIAA,
208 		                       handler, flags, devname, dev_id);
209 
210 	/*
211 	 * IRQ_AMIGA_PORTS & IRQ_AMIGA_EXTER defaults to shared,
212 	 * we could add a check here for the SA_SHIRQ flag but all drivers
213 	 * should be aware of sharing anyway.
214 	 */
215 	if (ami_servers[irq]) {
216 		if (!(node = new_irq_node()))
217 			return -ENOMEM;
218 		node->handler = handler;
219 		node->flags   = flags;
220 		node->dev_id  = dev_id;
221 		node->devname = devname;
222 		node->next    = NULL;
223 		error = amiga_insert_irq(&ami_irq_list[irq], node);
224 	} else {
225 		ami_irq_list[irq]->handler = handler;
226 		ami_irq_list[irq]->flags   = flags;
227 		ami_irq_list[irq]->dev_id  = dev_id;
228 		ami_irq_list[irq]->devname = devname;
229 	}
230 
231 	/* enable the interrupt */
232 	if (irq < IRQ_AMIGA_PORTS && !ami_ablecount[irq])
233 		custom.intena = IF_SETCLR | amiga_intena_vals[irq];
234 
235 	return error;
236 }
237 
amiga_free_irq(unsigned int irq,void * dev_id)238 void amiga_free_irq(unsigned int irq, void *dev_id)
239 {
240 	if (irq >= AMI_IRQS) {
241 		printk ("%s: Unknown IRQ %d\n", __FUNCTION__, irq);
242 		return;
243 	}
244 
245 	if (irq >= IRQ_AMIGA_AUTO)
246 		sys_free_irq(irq - IRQ_AMIGA_AUTO, dev_id);
247 
248 	if (irq >= IRQ_AMIGA_CIAB) {
249 		cia_free_irq(&ciab_base, irq - IRQ_AMIGA_CIAB, dev_id);
250 		return;
251 	}
252 
253 	if (irq >= IRQ_AMIGA_CIAA) {
254 		cia_free_irq(&ciaa_base, irq - IRQ_AMIGA_CIAA, dev_id);
255 		return;
256 	}
257 
258 	if (ami_servers[irq]) {
259 		amiga_delete_irq(&ami_irq_list[irq], dev_id);
260 		/* if server list empty, disable the interrupt */
261 		if (!ami_irq_list[irq] && irq < IRQ_AMIGA_PORTS)
262 			custom.intena = amiga_intena_vals[irq];
263 	} else {
264 		if (ami_irq_list[irq]->dev_id != dev_id)
265 			printk("%s: removing probably wrong IRQ %d from %s\n",
266 			       __FUNCTION__, irq, ami_irq_list[irq]->devname);
267 		ami_irq_list[irq]->handler = ami_badint;
268 		ami_irq_list[irq]->flags   = 0;
269 		ami_irq_list[irq]->dev_id  = NULL;
270 		ami_irq_list[irq]->devname = NULL;
271 		custom.intena = amiga_intena_vals[irq];
272 	}
273 }
274 
275 /*
276  * Enable/disable a particular machine specific interrupt source.
277  * Note that this may affect other interrupts in case of a shared interrupt.
278  * This function should only be called for a _very_ short time to change some
279  * internal data, that may not be changed by the interrupt at the same time.
280  * ami_(enable|disable)_irq calls may also be nested.
281  */
282 
amiga_enable_irq(unsigned int irq)283 void amiga_enable_irq(unsigned int irq)
284 {
285 	if (irq >= AMI_IRQS) {
286 		printk("%s: Unknown IRQ %d\n", __FUNCTION__, irq);
287 		return;
288 	}
289 
290 	if (--ami_ablecount[irq])
291 		return;
292 
293 	/* No action for auto-vector interrupts */
294 	if (irq >= IRQ_AMIGA_AUTO){
295 		printk("%s: Trying to enable auto-vector IRQ %i\n",
296 		       __FUNCTION__, irq - IRQ_AMIGA_AUTO);
297 		return;
298 	}
299 
300 	if (irq >= IRQ_AMIGA_CIAB) {
301 		cia_set_irq(&ciab_base, (1 << (irq - IRQ_AMIGA_CIAB)));
302 		cia_able_irq(&ciab_base, CIA_ICR_SETCLR |
303 		             (1 << (irq - IRQ_AMIGA_CIAB)));
304 		return;
305 	}
306 
307 	if (irq >= IRQ_AMIGA_CIAA) {
308 		cia_set_irq(&ciaa_base, (1 << (irq - IRQ_AMIGA_CIAA)));
309 		cia_able_irq(&ciaa_base, CIA_ICR_SETCLR |
310 		             (1 << (irq - IRQ_AMIGA_CIAA)));
311 		return;
312 	}
313 
314 	/* enable the interrupt */
315 	custom.intena = IF_SETCLR | amiga_intena_vals[irq];
316 }
317 
amiga_disable_irq(unsigned int irq)318 void amiga_disable_irq(unsigned int irq)
319 {
320 	if (irq >= AMI_IRQS) {
321 		printk("%s: Unknown IRQ %d\n", __FUNCTION__, irq);
322 		return;
323 	}
324 
325 	if (ami_ablecount[irq]++)
326 		return;
327 
328 	/* No action for auto-vector interrupts */
329 	if (irq >= IRQ_AMIGA_AUTO) {
330 		printk("%s: Trying to disable auto-vector IRQ %i\n",
331 		       __FUNCTION__, irq - IRQ_AMIGA_AUTO);
332 		return;
333 	}
334 
335 	if (irq >= IRQ_AMIGA_CIAB) {
336 		cia_able_irq(&ciab_base, 1 << (irq - IRQ_AMIGA_CIAB));
337 		return;
338 	}
339 
340 	if (irq >= IRQ_AMIGA_CIAA) {
341 		cia_able_irq(&ciaa_base, 1 << (irq - IRQ_AMIGA_CIAA));
342 		return;
343 	}
344 
345 	/* disable the interrupt */
346 	custom.intena = amiga_intena_vals[irq];
347 }
348 
amiga_do_irq(int irq,struct pt_regs * fp)349 inline void amiga_do_irq(int irq, struct pt_regs *fp)
350 {
351 	kstat.irqs[0][SYS_IRQS + irq]++;
352 	ami_irq_list[irq]->handler(irq, ami_irq_list[irq]->dev_id, fp);
353 }
354 
amiga_do_irq_list(int irq,struct pt_regs * fp)355 void amiga_do_irq_list(int irq, struct pt_regs *fp)
356 {
357 	irq_node_t *node;
358 
359 	kstat.irqs[0][SYS_IRQS + irq]++;
360 
361 	custom.intreq = amiga_intena_vals[irq];
362 
363 	for (node = ami_irq_list[irq]; node; node = node->next)
364 		node->handler(irq, node->dev_id, fp);
365 }
366 
367 /*
368  * The builtin Amiga hardware interrupt handlers.
369  */
370 
ami_int1(int irq,void * dev_id,struct pt_regs * fp)371 static void ami_int1(int irq, void *dev_id, struct pt_regs *fp)
372 {
373 	unsigned short ints = custom.intreqr & custom.intenar;
374 
375 	/* if serial transmit buffer empty, interrupt */
376 	if (ints & IF_TBE) {
377 		custom.intreq = IF_TBE;
378 		amiga_do_irq(IRQ_AMIGA_TBE, fp);
379 	}
380 
381 	/* if floppy disk transfer complete, interrupt */
382 	if (ints & IF_DSKBLK) {
383 		custom.intreq = IF_DSKBLK;
384 		amiga_do_irq(IRQ_AMIGA_DSKBLK, fp);
385 	}
386 
387 	/* if software interrupt set, interrupt */
388 	if (ints & IF_SOFT) {
389 		custom.intreq = IF_SOFT;
390 		amiga_do_irq(IRQ_AMIGA_SOFT, fp);
391 	}
392 }
393 
ami_int3(int irq,void * dev_id,struct pt_regs * fp)394 static void ami_int3(int irq, void *dev_id, struct pt_regs *fp)
395 {
396 	unsigned short ints = custom.intreqr & custom.intenar;
397 
398 	/* if a blitter interrupt */
399 	if (ints & IF_BLIT) {
400 		custom.intreq = IF_BLIT;
401 		amiga_do_irq(IRQ_AMIGA_BLIT, fp);
402 	}
403 
404 	/* if a copper interrupt */
405 	if (ints & IF_COPER) {
406 		custom.intreq = IF_COPER;
407 		amiga_do_irq(IRQ_AMIGA_COPPER, fp);
408 	}
409 
410 	/* if a vertical blank interrupt */
411 	if (ints & IF_VERTB)
412 		amiga_do_irq_list(IRQ_AMIGA_VERTB, fp);
413 }
414 
ami_int4(int irq,void * dev_id,struct pt_regs * fp)415 static void ami_int4(int irq, void *dev_id, struct pt_regs *fp)
416 {
417 	unsigned short ints = custom.intreqr & custom.intenar;
418 
419 	/* if audio 0 interrupt */
420 	if (ints & IF_AUD0) {
421 		custom.intreq = IF_AUD0;
422 		amiga_do_irq(IRQ_AMIGA_AUD0, fp);
423 	}
424 
425 	/* if audio 1 interrupt */
426 	if (ints & IF_AUD1) {
427 		custom.intreq = IF_AUD1;
428 		amiga_do_irq(IRQ_AMIGA_AUD1, fp);
429 	}
430 
431 	/* if audio 2 interrupt */
432 	if (ints & IF_AUD2) {
433 		custom.intreq = IF_AUD2;
434 		amiga_do_irq(IRQ_AMIGA_AUD2, fp);
435 	}
436 
437 	/* if audio 3 interrupt */
438 	if (ints & IF_AUD3) {
439 		custom.intreq = IF_AUD3;
440 		amiga_do_irq(IRQ_AMIGA_AUD3, fp);
441 	}
442 }
443 
ami_int5(int irq,void * dev_id,struct pt_regs * fp)444 static void ami_int5(int irq, void *dev_id, struct pt_regs *fp)
445 {
446 	unsigned short ints = custom.intreqr & custom.intenar;
447 
448 	/* if serial receive buffer full interrupt */
449 	if (ints & IF_RBF) {
450 		/* acknowledge of IF_RBF must be done by the serial interrupt */
451 		amiga_do_irq(IRQ_AMIGA_RBF, fp);
452 	}
453 
454 	/* if a disk sync interrupt */
455 	if (ints & IF_DSKSYN) {
456 		custom.intreq = IF_DSKSYN;
457 		amiga_do_irq(IRQ_AMIGA_DSKSYN, fp);
458 	}
459 }
460 
ami_int7(int irq,void * dev_id,struct pt_regs * fp)461 static void ami_int7(int irq, void *dev_id, struct pt_regs *fp)
462 {
463 	panic ("level 7 interrupt received\n");
464 }
465 
466 void (*amiga_default_handler[SYS_IRQS])(int, void *, struct pt_regs *) = {
467 	ami_badint, ami_int1, ami_badint, ami_int3,
468 	ami_int4, ami_int5, ami_badint, ami_int7
469 };
470 
amiga_get_irq_list(char * buf)471 int amiga_get_irq_list(char *buf)
472 {
473 	int i, len = 0;
474 	irq_node_t *node;
475 
476 	for (i = 0; i < AMI_STD_IRQS; i++) {
477 		if (!(node = ami_irq_list[i]))
478 			continue;
479 		len += sprintf(buf+len, "ami  %2d: %10u ", i,
480 		               kstat.irqs[0][SYS_IRQS + i]);
481 		do {
482 			if (node->flags & SA_INTERRUPT)
483 				len += sprintf(buf+len, "F ");
484 			else
485 				len += sprintf(buf+len, "  ");
486 			len += sprintf(buf+len, "%s\n", node->devname);
487 			if ((node = node->next))
488 				len += sprintf(buf+len, "                    ");
489 		} while (node);
490 	}
491 
492 	len += cia_get_irq_list(&ciaa_base, buf+len);
493 	len += cia_get_irq_list(&ciab_base, buf+len);
494 	return len;
495 }
496