1
2
3
4
5
6
7
8
9#undef DEBUG
10
11#include <linux/init.h>
12#include <linux/ioport.h>
13#include <linux/interrupt.h>
14#include <linux/kernel.h>
15#include <linux/delay.h>
16#include <asm/io.h>
17#include <asm/i8259.h>
18#include <asm/prom.h>
19
20static volatile void __iomem *pci_intack;
21
22static unsigned char cached_8259[2] = { 0xff, 0xff };
23#define cached_A1 (cached_8259[0])
24#define cached_21 (cached_8259[1])
25
26static DEFINE_SPINLOCK(i8259_lock);
27
28static struct irq_host *i8259_host;
29
30
31
32
33
34
35
36unsigned int i8259_irq(void)
37{
38 int irq;
39 int lock = 0;
40
41
42 if (pci_intack)
43 irq = readb(pci_intack);
44 else {
45 spin_lock(&i8259_lock);
46 lock = 1;
47
48
49 outb(0x0C, 0x20);
50 irq = inb(0x20) & 7;
51 if (irq == 2 ) {
52
53
54
55
56 outb(0x0C, 0xA0);
57 irq = (inb(0xA0) & 7) + 8;
58 }
59 }
60
61 if (irq == 7) {
62
63
64
65
66
67
68
69 if (!pci_intack)
70 outb(0x0B, 0x20);
71 if(~inb(0x20) & 0x80)
72 irq = NO_IRQ;
73 } else if (irq == 0xff)
74 irq = NO_IRQ;
75
76 if (lock)
77 spin_unlock(&i8259_lock);
78 return irq;
79}
80
81static void i8259_mask_and_ack_irq(unsigned int irq_nr)
82{
83 unsigned long flags;
84
85 spin_lock_irqsave(&i8259_lock, flags);
86 if (irq_nr > 7) {
87 cached_A1 |= 1 << (irq_nr-8);
88 inb(0xA1);
89 outb(cached_A1, 0xA1);
90 outb(0x20, 0xA0);
91 outb(0x20, 0x20);
92 } else {
93 cached_21 |= 1 << irq_nr;
94 inb(0x21);
95 outb(cached_21, 0x21);
96 outb(0x20, 0x20);
97 }
98 spin_unlock_irqrestore(&i8259_lock, flags);
99}
100
101static void i8259_set_irq_mask(int irq_nr)
102{
103 outb(cached_A1,0xA1);
104 outb(cached_21,0x21);
105}
106
107static void i8259_mask_irq(unsigned int irq_nr)
108{
109 unsigned long flags;
110
111 pr_debug("i8259_mask_irq(%d)\n", irq_nr);
112
113 spin_lock_irqsave(&i8259_lock, flags);
114 if (irq_nr < 8)
115 cached_21 |= 1 << irq_nr;
116 else
117 cached_A1 |= 1 << (irq_nr-8);
118 i8259_set_irq_mask(irq_nr);
119 spin_unlock_irqrestore(&i8259_lock, flags);
120}
121
122static void i8259_unmask_irq(unsigned int irq_nr)
123{
124 unsigned long flags;
125
126 pr_debug("i8259_unmask_irq(%d)\n", irq_nr);
127
128 spin_lock_irqsave(&i8259_lock, flags);
129 if (irq_nr < 8)
130 cached_21 &= ~(1 << irq_nr);
131 else
132 cached_A1 &= ~(1 << (irq_nr-8));
133 i8259_set_irq_mask(irq_nr);
134 spin_unlock_irqrestore(&i8259_lock, flags);
135}
136
137static struct irq_chip i8259_pic = {
138 .typename = " i8259 ",
139 .mask = i8259_mask_irq,
140 .disable = i8259_mask_irq,
141 .unmask = i8259_unmask_irq,
142 .mask_ack = i8259_mask_and_ack_irq,
143};
144
145static struct resource pic1_iores = {
146 .name = "8259 (master)",
147 .start = 0x20,
148 .end = 0x21,
149 .flags = IORESOURCE_BUSY,
150};
151
152static struct resource pic2_iores = {
153 .name = "8259 (slave)",
154 .start = 0xa0,
155 .end = 0xa1,
156 .flags = IORESOURCE_BUSY,
157};
158
159static struct resource pic_edgectrl_iores = {
160 .name = "8259 edge control",
161 .start = 0x4d0,
162 .end = 0x4d1,
163 .flags = IORESOURCE_BUSY,
164};
165
166static int i8259_host_match(struct irq_host *h, struct device_node *node)
167{
168 return h->of_node == NULL || h->of_node == node;
169}
170
171static int i8259_host_map(struct irq_host *h, unsigned int virq,
172 irq_hw_number_t hw)
173{
174 pr_debug("i8259_host_map(%d, 0x%lx)\n", virq, hw);
175
176
177 if (hw == 2)
178 get_irq_desc(virq)->status |= IRQ_NOREQUEST;
179
180
181
182
183 get_irq_desc(virq)->status |= IRQ_LEVEL;
184 set_irq_chip_and_handler(virq, &i8259_pic, handle_level_irq);
185 return 0;
186}
187
188static void i8259_host_unmap(struct irq_host *h, unsigned int virq)
189{
190
191 i8259_mask_irq(virq);
192
193
194 set_irq_chip_and_handler(virq, NULL, NULL);
195
196
197 synchronize_irq(virq);
198}
199
200static int i8259_host_xlate(struct irq_host *h, struct device_node *ct,
201 u32 *intspec, unsigned int intsize,
202 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
203{
204 static unsigned char map_isa_senses[4] = {
205 IRQ_TYPE_LEVEL_LOW,
206 IRQ_TYPE_LEVEL_HIGH,
207 IRQ_TYPE_EDGE_FALLING,
208 IRQ_TYPE_EDGE_RISING,
209 };
210
211 *out_hwirq = intspec[0];
212 if (intsize > 1 && intspec[1] < 4)
213 *out_flags = map_isa_senses[intspec[1]];
214 else
215 *out_flags = IRQ_TYPE_NONE;
216
217 return 0;
218}
219
220static struct irq_host_ops i8259_host_ops = {
221 .match = i8259_host_match,
222 .map = i8259_host_map,
223 .unmap = i8259_host_unmap,
224 .xlate = i8259_host_xlate,
225};
226
227struct irq_host *i8259_get_host(void)
228{
229 return i8259_host;
230}
231
232
233
234
235
236
237
238
239void i8259_init(struct device_node *node, unsigned long intack_addr)
240{
241 unsigned long flags;
242
243
244 spin_lock_irqsave(&i8259_lock, flags);
245
246
247 outb(0xff, 0xA1);
248 outb(0xff, 0x21);
249
250
251 outb(0x11, 0x20);
252 outb(0x00, 0x21);
253 outb(0x04, 0x21);
254 outb(0x01, 0x21);
255
256
257 outb(0x11, 0xA0);
258 outb(0x08, 0xA1);
259 outb(0x02, 0xA1);
260 outb(0x01, 0xA1);
261
262
263 udelay(100);
264
265
266 outb(0x0B, 0x20);
267 outb(0x0B, 0xA0);
268
269
270 cached_21 &= ~(1 << 2);
271
272
273 outb(cached_A1, 0xA1);
274 outb(cached_21, 0x21);
275
276 spin_unlock_irqrestore(&i8259_lock, flags);
277
278
279 i8259_host = irq_alloc_host(node, IRQ_HOST_MAP_LEGACY,
280 0, &i8259_host_ops, 0);
281 if (i8259_host == NULL) {
282 printk(KERN_ERR "i8259: failed to allocate irq host !\n");
283 return;
284 }
285
286
287
288
289
290
291 request_resource(&ioport_resource, &pic1_iores);
292 request_resource(&ioport_resource, &pic2_iores);
293 request_resource(&ioport_resource, &pic_edgectrl_iores);
294
295 if (intack_addr != 0)
296 pci_intack = ioremap(intack_addr, 1);
297
298 printk(KERN_INFO "i8259 legacy interrupt controller initialized\n");
299}
300