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