1/*
2 *  Copyright (C) 1996 Roman Zippel
3 *
4 *  The concept of some functions bases on the original Amiga OS function
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
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/sched.h>
14#include <linux/interrupt.h>
15#include <linux/irq.h>
16#include <linux/kernel_stat.h>
17#include <linux/init.h>
18
19#include <asm/irq.h>
20#include <asm/amigahw.h>
21#include <asm/amigaints.h>
22
23struct ciabase {
24	volatile struct CIA *cia;
25	u_char icr_mask, icr_data;
26	u_short int_mask;
27	int handler_irq, cia_irq, server_irq;
28	char *name;
29} ciaa_base = {
30	&ciaa, 0, 0, IF_PORTS,
31	IRQ_AMIGA_AUTO_2, IRQ_AMIGA_CIAA,
32	IRQ_AMIGA_PORTS,
33	"CIAA handler"
34}, ciab_base = {
35	&ciab, 0, 0, IF_EXTER,
36	IRQ_AMIGA_AUTO_6, IRQ_AMIGA_CIAB,
37	IRQ_AMIGA_EXTER,
38	"CIAB handler"
39};
40
41#define CIA_SET_BASE_ADJUST_IRQ(base, irq)	\
42do {						\
43	if (irq >= IRQ_AMIGA_CIAB) {		\
44		base = &ciab_base;		\
45		irq -= IRQ_AMIGA_CIAB;		\
46	} else {				\
47		base = &ciaa_base;		\
48		irq -= IRQ_AMIGA_CIAA;		\
49	}					\
50} while (0)
51
52/*
53 *  Cause or clear CIA interrupts, return old interrupt status.
54 */
55
56static unsigned char cia_set_irq_private(struct ciabase *base,
57					 unsigned char mask)
58{
59	u_char old;
60
61	old = (base->icr_data |= base->cia->icr);
62	if (mask & CIA_ICR_SETCLR)
63		base->icr_data |= mask;
64	else
65		base->icr_data &= ~mask;
66	if (base->icr_data & base->icr_mask)
67		amiga_custom.intreq = IF_SETCLR | base->int_mask;
68	return old & base->icr_mask;
69}
70
71unsigned char cia_set_irq(unsigned int irq, int set)
72{
73	struct ciabase *base;
74	unsigned char mask;
75
76	if (irq >= IRQ_AMIGA_CIAB)
77		mask = (1 << (irq - IRQ_AMIGA_CIAB));
78	else
79		mask = (1 << (irq - IRQ_AMIGA_CIAA));
80	mask |= (set) ? CIA_ICR_SETCLR : 0;
81
82	CIA_SET_BASE_ADJUST_IRQ(base, irq);
83
84	return cia_set_irq_private(base, mask);
85}
86
87unsigned char cia_get_irq_mask(unsigned int irq)
88{
89	struct ciabase *base;
90
91	CIA_SET_BASE_ADJUST_IRQ(base, irq);
92
93	return base->cia->icr;
94}
95
96/*
97 *  Enable or disable CIA interrupts, return old interrupt mask.
98 */
99
100static unsigned char cia_able_irq_private(struct ciabase *base,
101					  unsigned char mask)
102{
103	u_char old;
104
105	old = base->icr_mask;
106	base->icr_data |= base->cia->icr;
107	base->cia->icr = mask;
108	if (mask & CIA_ICR_SETCLR)
109		base->icr_mask |= mask;
110	else
111		base->icr_mask &= ~mask;
112	base->icr_mask &= CIA_ICR_ALL;
113
114	if (base->icr_data & base->icr_mask)
115		amiga_custom.intreq = IF_SETCLR | base->int_mask;
116	return old;
117}
118
119unsigned char cia_able_irq(unsigned int irq, int enable)
120{
121	struct ciabase *base;
122	unsigned char mask;
123
124	if (irq >= IRQ_AMIGA_CIAB)
125		mask = (1 << (irq - IRQ_AMIGA_CIAB));
126	else
127		mask = (1 << (irq - IRQ_AMIGA_CIAA));
128	mask |= (enable) ? CIA_ICR_SETCLR : 0;
129
130	CIA_SET_BASE_ADJUST_IRQ(base, irq);
131
132	return cia_able_irq_private(base, mask);
133}
134
135static void cia_handler(int irq, void *dev_id, struct pt_regs *fp)
136{
137	struct ciabase *base = (struct ciabase *)dev_id;
138	irq_desc_t *desc;
139	struct irqaction *action;
140	int i;
141	unsigned char ints;
142
143	irq = base->cia_irq;
144	desc = irq_desc + irq;
145	ints = cia_set_irq_private(base, CIA_ICR_ALL);
146	amiga_custom.intreq = base->int_mask;
147	for (i = 0; i < CIA_IRQS; i++, irq++) {
148		if (ints & 1) {
149			kstat_cpu(0).irqs[irq]++;
150			action = desc->action;
151			action->handler(irq, action->dev_id, fp);
152		}
153		ints >>= 1;
154		desc++;
155	}
156	amiga_do_irq_list(base->server_irq, fp);
157}
158
159void __init cia_init_IRQ(struct ciabase *base)
160{
161	extern struct irqaction amiga_sys_irqaction[AUTO_IRQS];
162	struct irqaction *action;
163
164	/* clear any pending interrupt and turn off all interrupts */
165	cia_set_irq_private(base, CIA_ICR_ALL);
166	cia_able_irq_private(base, CIA_ICR_ALL);
167
168	/* install CIA handler */
169	action = &amiga_sys_irqaction[base->handler_irq-IRQ_AMIGA_AUTO];
170	action->handler = cia_handler;
171	action->dev_id = base;
172	action->name = base->name;
173	setup_irq(base->handler_irq, &amiga_sys_irqaction[base->handler_irq-IRQ_AMIGA_AUTO]);
174
175	amiga_custom.intena = IF_SETCLR | base->int_mask;
176}
177