1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License.  See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Code to handle x86 style IRQs plus some generic interrupt stuff.
7 *
8 * Copyright (C) 1992 Linus Torvalds
9 * Copyright (C) 1994 - 2000 Ralf Baechle
10 */
11#include <linux/delay.h>
12#include <linux/init.h>
13#include <linux/ioport.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <linux/spinlock.h>
17
18#include <asm/io.h>
19
20void enable_8259A_irq(unsigned int irq);
21void disable_8259A_irq(unsigned int irq);
22
23/*
24 * This is the 'legacy' 8259A Programmable Interrupt Controller,
25 * present in the majority of PC/AT boxes.
26 * plus some generic x86 specific things if generic specifics makes
27 * any sense at all.
28 * this file should become arch/i386/kernel/irq.c when the old irq.c
29 * moves to arch independent land
30 */
31
32static spinlock_t i8259A_lock = SPIN_LOCK_UNLOCKED;
33
34static void end_8259A_irq (unsigned int irq)
35{
36	if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
37		enable_8259A_irq(irq);
38}
39
40#define shutdown_8259A_irq	disable_8259A_irq
41
42void mask_and_ack_8259A(unsigned int);
43
44static unsigned int startup_8259A_irq(unsigned int irq)
45{
46	enable_8259A_irq(irq);
47
48	return 0; /* never anything pending */
49}
50
51static struct hw_interrupt_type i8259A_irq_type = {
52	"XT-PIC",
53	startup_8259A_irq,
54	shutdown_8259A_irq,
55	enable_8259A_irq,
56	disable_8259A_irq,
57	mask_and_ack_8259A,
58	end_8259A_irq,
59	NULL
60};
61
62/*
63 * 8259A PIC functions to handle ISA devices:
64 */
65
66/*
67 * This contains the irq mask for both 8259A irq controllers,
68 */
69static unsigned int cached_irq_mask = 0xffff;
70
71#define cached_21	(cached_irq_mask)
72#define cached_A1	(cached_irq_mask >> 8)
73
74void disable_8259A_irq(unsigned int irq)
75{
76	unsigned int mask = 1 << irq;
77	unsigned long flags;
78
79	spin_lock_irqsave(&i8259A_lock, flags);
80	cached_irq_mask |= mask;
81	if (irq & 8)
82		outb(cached_A1,0xA1);
83	else
84		outb(cached_21,0x21);
85	spin_unlock_irqrestore(&i8259A_lock, flags);
86}
87
88void enable_8259A_irq(unsigned int irq)
89{
90	unsigned int mask = ~(1 << irq);
91	unsigned long flags;
92
93	spin_lock_irqsave(&i8259A_lock, flags);
94	cached_irq_mask &= mask;
95	if (irq & 8)
96		outb(cached_A1,0xA1);
97	else
98		outb(cached_21,0x21);
99	spin_unlock_irqrestore(&i8259A_lock, flags);
100}
101
102int i8259A_irq_pending(unsigned int irq)
103{
104	unsigned int mask = 1 << irq;
105	unsigned long flags;
106	int ret;
107
108	spin_lock_irqsave(&i8259A_lock, flags);
109	if (irq < 8)
110		ret = inb(0x20) & mask;
111	else
112		ret = inb(0xA0) & (mask >> 8);
113	spin_unlock_irqrestore(&i8259A_lock, flags);
114
115	return ret;
116}
117
118void make_8259A_irq(unsigned int irq)
119{
120	disable_irq_nosync(irq);
121	irq_desc[irq].handler = &i8259A_irq_type;
122	enable_irq(irq);
123}
124
125/*
126 * This function assumes to be called rarely. Switching between
127 * 8259A registers is slow.
128 * This has to be protected by the irq controller spinlock
129 * before being called.
130 */
131static inline int i8259A_irq_real(unsigned int irq)
132{
133	int value;
134	int irqmask = 1 << irq;
135
136	if (irq < 8) {
137		outb(0x0B,0x20);		/* ISR register */
138		value = inb(0x20) & irqmask;
139		outb(0x0A,0x20);		/* back to the IRR register */
140		return value;
141	}
142	outb(0x0B,0xA0);		/* ISR register */
143	value = inb(0xA0) & (irqmask >> 8);
144	outb(0x0A,0xA0);		/* back to the IRR register */
145	return value;
146}
147
148/*
149 * Careful! The 8259A is a fragile beast, it pretty
150 * much _has_ to be done exactly like this (mask it
151 * first, _then_ send the EOI, and the order of EOI
152 * to the two 8259s is important!
153 */
154void mask_and_ack_8259A(unsigned int irq)
155{
156	unsigned int irqmask = 1 << irq;
157	unsigned long flags;
158
159	spin_lock_irqsave(&i8259A_lock, flags);
160	/*
161	 * Lightweight spurious IRQ detection. We do not want to overdo
162	 * spurious IRQ handling - it's usually a sign of hardware problems, so
163	 * we only do the checks we can do without slowing down good hardware
164	 * nnecesserily.
165	 *
166	 * Note that IRQ7 and IRQ15 (the two spurious IRQs usually resulting
167	 * rom the 8259A-1|2 PICs) occur even if the IRQ is masked in the 8259A.
168	 * Thus we can check spurious 8259A IRQs without doing the quite slow
169	 * i8259A_irq_real() call for every IRQ.  This does not cover 100% of
170	 * spurious interrupts, but should be enough to warn the user that
171	 * there is something bad going on ...
172	 */
173	if (cached_irq_mask & irqmask)
174		goto spurious_8259A_irq;
175	cached_irq_mask |= irqmask;
176
177handle_real_irq:
178	if (irq & 8) {
179		inb(0xA1);		/* DUMMY - (do we need this?) */
180		outb(cached_A1,0xA1);
181		outb(0x60+(irq&7),0xA0);/* 'Specific EOI' to slave */
182		outb(0x62,0x20);	/* 'Specific EOI' to master-IRQ2 */
183	} else {
184		inb(0x21);		/* DUMMY - (do we need this?) */
185		outb(cached_21,0x21);
186		outb(0x60+irq,0x20);	/* 'Specific EOI' to master */
187	}
188	spin_unlock_irqrestore(&i8259A_lock, flags);
189	return;
190
191spurious_8259A_irq:
192	/*
193	 * this is the slow path - should happen rarely.
194	 */
195	if (i8259A_irq_real(irq))
196		/*
197		 * oops, the IRQ _is_ in service according to the
198		 * 8259A - not spurious, go handle it.
199		 */
200		goto handle_real_irq;
201
202	{
203		static int spurious_irq_mask = 0;
204		/*
205		 * At this point we can be sure the IRQ is spurious,
206		 * lets ACK and report it. [once per IRQ]
207		 */
208		if (!(spurious_irq_mask & irqmask)) {
209			printk("spurious 8259A interrupt: IRQ%d.\n", irq);
210			spurious_irq_mask |= irqmask;
211		}
212		atomic_inc(&irq_err_count);
213		/*
214		 * Theoretically we do not have to handle this IRQ,
215		 * but in Linux this does not cause problems and is
216		 * simpler for us.
217		 */
218		goto handle_real_irq;
219	}
220}
221
222void __init init_8259A(int auto_eoi)
223{
224	unsigned long flags;
225
226	spin_lock_irqsave(&i8259A_lock, flags);
227
228	outb(0xff, 0x21);	/* mask all of 8259A-1 */
229	outb(0xff, 0xA1);	/* mask all of 8259A-2 */
230
231	/*
232	 * outb_p - this has to work on a wide range of PC hardware.
233	 */
234	outb_p(0x11, 0x20);	/* ICW1: select 8259A-1 init */
235	outb_p(0x00, 0x21);	/* ICW2: 8259A-1 IR0-7 mapped to 0x00-0x07 */
236	outb_p(0x04, 0x21);	/* 8259A-1 (the master) has a slave on IR2 */
237	if (auto_eoi)
238		outb_p(0x03, 0x21);	/* master does Auto EOI */
239	else
240		outb_p(0x01, 0x21);	/* master expects normal EOI */
241
242	outb_p(0x11, 0xA0);	/* ICW1: select 8259A-2 init */
243	outb_p(0x08, 0xA1);	/* ICW2: 8259A-2 IR0-7 mapped to 0x08-0x0f */
244	outb_p(0x02, 0xA1);	/* 8259A-2 is a slave on master's IR2 */
245	outb_p(0x01, 0xA1);	/* (slave's support for AEOI in flat mode
246				    is to be investigated) */
247
248	if (auto_eoi)
249		/*
250		 * in AEOI mode we just have to mask the interrupt
251		 * when acking.
252		 */
253		i8259A_irq_type.ack = disable_8259A_irq;
254	else
255		i8259A_irq_type.ack = mask_and_ack_8259A;
256
257	udelay(100);		/* wait for 8259A to initialize */
258
259	outb(cached_21, 0x21);	/* restore master IRQ mask */
260	outb(cached_A1, 0xA1);	/* restore slave IRQ mask */
261
262	spin_unlock_irqrestore(&i8259A_lock, flags);
263}
264
265asmlinkage void i8259_do_irq(int irq, struct pt_regs regs)
266{
267	panic("i8259_do_irq: I want to be implemented");
268}
269
270/*
271 * IRQ2 is cascade interrupt to second interrupt controller
272 */
273static struct irqaction irq2 = {
274	no_action, 0, 0, "cascade", NULL, NULL
275};
276
277static struct resource pic1_io_resource = {
278	"pic1", 0x20, 0x3f, IORESOURCE_BUSY
279};
280
281static struct resource pic2_io_resource = {
282	"pic2", 0xa0, 0xbf, IORESOURCE_BUSY
283};
284
285/*
286 * On systems with i8259-style interrupt controllers we assume for
287 * driver compatibility reasons interrupts 0 - 15 to be the i8295
288 * interrupts even if the hardware uses a different interrupt numbering.
289 */
290void __init init_i8259_irqs (void)
291{
292	int i;
293
294	request_resource(&ioport_resource, &pic1_io_resource);
295	request_resource(&ioport_resource, &pic2_io_resource);
296
297	init_8259A(0);
298
299	for (i = 0; i < 16; i++) {
300		irq_desc[i].status = IRQ_DISABLED;
301		irq_desc[i].action = 0;
302		irq_desc[i].depth = 1;
303		irq_desc[i].handler = &i8259A_irq_type;
304	}
305
306	setup_irq(2, &irq2);
307}
308