1 /*
2  *  linux/arch/m68k/amiga/cia.c - CIA support
3  *
4  *  Copyright (C) 1996 Roman Zippel
5  *
6  *  The concept of some functions bases on the original Amiga OS function
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License.  See the file COPYING in the main directory of this archive
10  * for more details.
11  */
12 
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/init.h>
19 
20 #include <asm/irq.h>
21 #include <asm/amigahw.h>
22 #include <asm/amigaints.h>
23 
24 struct ciabase {
25 	volatile struct CIA *cia;
26 	unsigned char icr_mask, icr_data;
27 	unsigned short int_mask;
28 	int handler_irq, cia_irq, server_irq;
29 	char *name;
30 	irq_handler_t irq_list[CIA_IRQS];
31 } ciaa_base = {
32 	&ciaa, 0, 0, IF_PORTS,
33 	IRQ_AMIGA_AUTO_2, IRQ_AMIGA_CIAA,
34 	IRQ_AMIGA_PORTS,
35 	"CIAA handler"
36 }, ciab_base = {
37 	&ciab, 0, 0, IF_EXTER,
38 	IRQ_AMIGA_AUTO_6, IRQ_AMIGA_CIAB,
39 	IRQ_AMIGA_EXTER,
40 	"CIAB handler"
41 };
42 
43 /*
44  *  Cause or clear CIA interrupts, return old interrupt status.
45  */
46 
cia_set_irq(struct ciabase * base,unsigned char mask)47 unsigned char cia_set_irq(struct ciabase *base, unsigned char mask)
48 {
49 	unsigned char old;
50 
51 	old = (base->icr_data |= base->cia->icr);
52 	if (mask & CIA_ICR_SETCLR)
53 		base->icr_data |= mask;
54 	else
55 		base->icr_data &= ~mask;
56 	if (base->icr_data & base->icr_mask)
57 		custom.intreq = IF_SETCLR | base->int_mask;
58 	return old & base->icr_mask;
59 }
60 
61 /*
62  *  Enable or disable CIA interrupts, return old interrupt mask,
63  *  interrupts will only be enabled if a handler exists
64  */
65 
cia_able_irq(struct ciabase * base,unsigned char mask)66 unsigned char cia_able_irq(struct ciabase *base, unsigned char mask)
67 {
68 	unsigned char old, tmp;
69 	int i;
70 
71 	old = base->icr_mask;
72 	base->icr_data |= base->cia->icr;
73 	base->cia->icr = mask;
74 	if (mask & CIA_ICR_SETCLR)
75 		base->icr_mask |= mask;
76 	else
77 		base->icr_mask &= ~mask;
78 	base->icr_mask &= CIA_ICR_ALL;
79 	for (i = 0, tmp = 1; i < CIA_IRQS; i++, tmp <<= 1) {
80 		if ((tmp & base->icr_mask) && !base->irq_list[i].handler) {
81 			base->icr_mask &= ~tmp;
82 			base->cia->icr = tmp;
83 		}
84 	}
85 	if (base->icr_data & base->icr_mask)
86 		custom.intreq = IF_SETCLR | base->int_mask;
87 	return old;
88 }
89 
cia_request_irq(struct ciabase * base,unsigned int irq,void (* handler)(int,void *,struct pt_regs *),unsigned long flags,const char * devname,void * dev_id)90 int cia_request_irq(struct ciabase *base, unsigned int irq,
91                     void (*handler)(int, void *, struct pt_regs *),
92                     unsigned long flags, const char *devname, void *dev_id)
93 {
94 	unsigned char mask;
95 
96 	base->irq_list[irq].handler = handler;
97 	base->irq_list[irq].flags   = flags;
98 	base->irq_list[irq].dev_id  = dev_id;
99 	base->irq_list[irq].devname = devname;
100 
101 	/* enable the interrupt */
102 	mask = 1 << irq;
103 	cia_set_irq(base, mask);
104 	cia_able_irq(base, CIA_ICR_SETCLR | mask);
105 	return 0;
106 }
107 
cia_free_irq(struct ciabase * base,unsigned int irq,void * dev_id)108 void cia_free_irq(struct ciabase *base, unsigned int irq, void *dev_id)
109 {
110 	if (base->irq_list[irq].dev_id != dev_id)
111 		printk("%s: removing probably wrong IRQ %i from %s\n",
112 		       __FUNCTION__, base->cia_irq + irq,
113 		       base->irq_list[irq].devname);
114 
115 	base->irq_list[irq].handler = NULL;
116 	base->irq_list[irq].flags   = 0;
117 
118 	cia_able_irq(base, 1 << irq);
119 }
120 
cia_handler(int irq,void * dev_id,struct pt_regs * fp)121 static void cia_handler(int irq, void *dev_id, struct pt_regs *fp)
122 {
123 	struct ciabase *base = (struct ciabase *)dev_id;
124 	int mach_irq, i;
125 	unsigned char ints;
126 
127 	mach_irq = base->cia_irq;
128 	irq = SYS_IRQS + mach_irq;
129 	ints = cia_set_irq(base, CIA_ICR_ALL);
130 	custom.intreq = base->int_mask;
131 	for (i = 0; i < CIA_IRQS; i++, irq++, mach_irq++) {
132 		if (ints & 1) {
133 			kstat.irqs[0][irq]++;
134 			base->irq_list[i].handler(mach_irq, base->irq_list[i].dev_id, fp);
135 		}
136 		ints >>= 1;
137 	}
138 	amiga_do_irq_list(base->server_irq, fp);
139 }
140 
cia_init_IRQ(struct ciabase * base)141 void __init cia_init_IRQ(struct ciabase *base)
142 {
143 	int i;
144 
145 	/* init isr handlers */
146 	for (i = 0; i < CIA_IRQS; i++) {
147 		base->irq_list[i].handler = NULL;
148 		base->irq_list[i].flags   = 0;
149 	}
150 
151 	/* clear any pending interrupt and turn off all interrupts */
152 	cia_set_irq(base, CIA_ICR_ALL);
153 	cia_able_irq(base, CIA_ICR_ALL);
154 
155 	/* install CIA handler */
156 	request_irq(base->handler_irq, cia_handler, 0, base->name, base);
157 
158 	custom.intena = IF_SETCLR | base->int_mask;
159 }
160 
cia_get_irq_list(struct ciabase * base,char * buf)161 int cia_get_irq_list(struct ciabase *base, char *buf)
162 {
163 	int i, j, len = 0;
164 
165 	j = base->cia_irq;
166 	for (i = 0; i < CIA_IRQS; i++) {
167 		len += sprintf(buf+len, "cia  %2d: %10d ", j + i,
168 			       kstat.irqs[0][SYS_IRQS + j + i]);
169 			len += sprintf(buf+len, "  ");
170 		len += sprintf(buf+len, "%s\n", base->irq_list[i].devname);
171 	}
172 	return len;
173 }
174