blob: d18bb27e4df92dff846122a867868164dc3d3aca [file] [log] [blame]
Paul Mackerrasf9bd1702005-10-26 16:47:42 +10001/*
2 * i8259 interrupt controller driver.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10009#undef DEBUG
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12#include <linux/ioport.h>
13#include <linux/interrupt.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100014#include <linux/kernel.h>
15#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/io.h>
17#include <asm/i8259.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100018#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Paul Mackerrasf9bd1702005-10-26 16:47:42 +100020static volatile void __iomem *pci_intack; /* RO, gives us the irq vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Paul Mackerrasf9bd1702005-10-26 16:47:42 +100022static unsigned char cached_8259[2] = { 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define cached_A1 (cached_8259[0])
24#define cached_21 (cached_8259[1])
25
Thomas Gleixner47e3c902010-02-18 02:23:11 +000026static DEFINE_RAW_SPINLOCK(i8259_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100028static struct irq_host *i8259_host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * Acknowledge the IRQ using either the PCI host bridge's interrupt
32 * acknowledge feature or poll. How i8259_init() is called determines
33 * which is called. It should be noted that polling is broken on some
34 * IBM and Motorola PReP boxes so we must use the int-ack feature on them.
35 */
Olaf Hering35a84c22006-10-07 22:08:26 +100036unsigned int i8259_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 int irq;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100039 int lock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 /* Either int-ack or poll for the IRQ */
42 if (pci_intack)
Paul Mackerrasf9bd1702005-10-26 16:47:42 +100043 irq = readb(pci_intack);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 else {
Thomas Gleixner47e3c902010-02-18 02:23:11 +000045 raw_spin_lock(&i8259_lock);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100046 lock = 1;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 /* Perform an interrupt acknowledge cycle on controller 1. */
49 outb(0x0C, 0x20); /* prepare for poll */
50 irq = inb(0x20) & 7;
51 if (irq == 2 ) {
52 /*
53 * Interrupt is cascaded so perform interrupt
54 * acknowledge on controller 2.
55 */
56 outb(0x0C, 0xA0); /* prepare for poll */
57 irq = (inb(0xA0) & 7) + 8;
58 }
59 }
60
61 if (irq == 7) {
62 /*
63 * This may be a spurious interrupt.
64 *
65 * Read the interrupt status register (ISR). If the most
66 * significant bit is not set then there is no valid
67 * interrupt.
68 */
69 if (!pci_intack)
70 outb(0x0B, 0x20); /* ISR register */
71 if(~inb(0x20) & 0x80)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100072 irq = NO_IRQ;
73 } else if (irq == 0xff)
74 irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100076 if (lock)
Thomas Gleixner47e3c902010-02-18 02:23:11 +000077 raw_spin_unlock(&i8259_lock);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100078 return irq;
Paul Mackerrasf9bd1702005-10-26 16:47:42 +100079}
80
Lennert Buytenhekd4201182011-03-07 13:59:56 +000081static void i8259_mask_and_ack_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 unsigned long flags;
84
Thomas Gleixner47e3c902010-02-18 02:23:11 +000085 raw_spin_lock_irqsave(&i8259_lock, flags);
Lennert Buytenhekd4201182011-03-07 13:59:56 +000086 if (d->irq > 7) {
87 cached_A1 |= 1 << (d->irq-8);
Paul Mackerrasf9bd1702005-10-26 16:47:42 +100088 inb(0xA1); /* DUMMY */
89 outb(cached_A1, 0xA1);
90 outb(0x20, 0xA0); /* Non-specific EOI */
91 outb(0x20, 0x20); /* Non-specific EOI to cascade */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 } else {
Lennert Buytenhekd4201182011-03-07 13:59:56 +000093 cached_21 |= 1 << d->irq;
Paul Mackerrasf9bd1702005-10-26 16:47:42 +100094 inb(0x21); /* DUMMY */
95 outb(cached_21, 0x21);
96 outb(0x20, 0x20); /* Non-specific EOI */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Thomas Gleixner47e3c902010-02-18 02:23:11 +000098 raw_spin_unlock_irqrestore(&i8259_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static void i8259_set_irq_mask(int irq_nr)
102{
103 outb(cached_A1,0xA1);
104 outb(cached_21,0x21);
105}
106
Lennert Buytenhekd4201182011-03-07 13:59:56 +0000107static void i8259_mask_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 unsigned long flags;
110
Lennert Buytenhekd4201182011-03-07 13:59:56 +0000111 pr_debug("i8259_mask_irq(%d)\n", d->irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000112
Thomas Gleixner47e3c902010-02-18 02:23:11 +0000113 raw_spin_lock_irqsave(&i8259_lock, flags);
Lennert Buytenhekd4201182011-03-07 13:59:56 +0000114 if (d->irq < 8)
115 cached_21 |= 1 << d->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 else
Lennert Buytenhekd4201182011-03-07 13:59:56 +0000117 cached_A1 |= 1 << (d->irq-8);
118 i8259_set_irq_mask(d->irq);
Thomas Gleixner47e3c902010-02-18 02:23:11 +0000119 raw_spin_unlock_irqrestore(&i8259_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Lennert Buytenhekd4201182011-03-07 13:59:56 +0000122static void i8259_unmask_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 unsigned long flags;
125
Lennert Buytenhekd4201182011-03-07 13:59:56 +0000126 pr_debug("i8259_unmask_irq(%d)\n", d->irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000127
Thomas Gleixner47e3c902010-02-18 02:23:11 +0000128 raw_spin_lock_irqsave(&i8259_lock, flags);
Lennert Buytenhekd4201182011-03-07 13:59:56 +0000129 if (d->irq < 8)
130 cached_21 &= ~(1 << d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 else
Lennert Buytenhekd4201182011-03-07 13:59:56 +0000132 cached_A1 &= ~(1 << (d->irq-8));
133 i8259_set_irq_mask(d->irq);
Thomas Gleixner47e3c902010-02-18 02:23:11 +0000134 raw_spin_unlock_irqrestore(&i8259_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000137static struct irq_chip i8259_pic = {
Anton Blanchardfc380c02010-01-31 20:33:41 +0000138 .name = "i8259",
Lennert Buytenhekd4201182011-03-07 13:59:56 +0000139 .irq_mask = i8259_mask_irq,
140 .irq_disable = i8259_mask_irq,
141 .irq_unmask = i8259_unmask_irq,
142 .irq_mask_ack = i8259_mask_and_ack_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
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
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000166static int i8259_host_match(struct irq_host *h, struct device_node *node)
167{
Michael Ellerman52964f82007-08-28 18:47:54 +1000168 return h->of_node == NULL || h->of_node == node;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000169}
170
171static int i8259_host_map(struct irq_host *h, unsigned int virq,
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700172 irq_hw_number_t hw)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000173{
174 pr_debug("i8259_host_map(%d, 0x%lx)\n", virq, hw);
175
176 /* We block the internal cascade */
177 if (hw == 2)
Thomas Gleixner98488db2011-03-25 15:43:57 +0100178 irq_set_status_flags(virq, IRQ_NOREQUEST);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000179
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700180 /* We use the level handler only for now, we might want to
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000181 * be more cautious here but that works for now
182 */
Thomas Gleixner98488db2011-03-25 15:43:57 +0100183 irq_set_status_flags(virq, IRQ_LEVEL);
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100184 irq_set_chip_and_handler(virq, &i8259_pic, handle_level_irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000185 return 0;
186}
187
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000188static int i8259_host_xlate(struct irq_host *h, struct device_node *ct,
Roman Fietze40d50cf2009-12-08 02:39:50 +0000189 const u32 *intspec, unsigned int intsize,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000190 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
191{
192 static unsigned char map_isa_senses[4] = {
193 IRQ_TYPE_LEVEL_LOW,
194 IRQ_TYPE_LEVEL_HIGH,
195 IRQ_TYPE_EDGE_FALLING,
196 IRQ_TYPE_EDGE_RISING,
197 };
198
199 *out_hwirq = intspec[0];
200 if (intsize > 1 && intspec[1] < 4)
201 *out_flags = map_isa_senses[intspec[1]];
202 else
203 *out_flags = IRQ_TYPE_NONE;
204
205 return 0;
206}
207
208static struct irq_host_ops i8259_host_ops = {
209 .match = i8259_host_match,
210 .map = i8259_host_map,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000211 .xlate = i8259_host_xlate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
Benjamin Herrenschmidtf4d4c352006-10-25 13:22:27 +1000214struct irq_host *i8259_get_host(void)
215{
216 return i8259_host;
217}
218
Michael Ellerman40681b92006-08-02 11:13:50 +1000219/**
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000220 * i8259_init - Initialize the legacy controller
221 * @node: device node of the legacy PIC (can be NULL, but then, it will match
222 * all interrupts, so beware)
223 * @intack_addr: PCI interrupt acknowledge (real) address which will return
224 * the active irq from the 8259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000226void i8259_init(struct device_node *node, unsigned long intack_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 unsigned long flags;
229
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000230 /* initialize the controller */
Thomas Gleixner47e3c902010-02-18 02:23:11 +0000231 raw_spin_lock_irqsave(&i8259_lock, flags);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000232
233 /* Mask all first */
234 outb(0xff, 0xA1);
235 outb(0xff, 0x21);
Paul Mackerrasf9bd1702005-10-26 16:47:42 +1000236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* init master interrupt controller */
238 outb(0x11, 0x20); /* Start init sequence */
239 outb(0x00, 0x21); /* Vector base */
240 outb(0x04, 0x21); /* edge tiggered, Cascade (slave) on IRQ2 */
241 outb(0x01, 0x21); /* Select 8086 mode */
242
243 /* init slave interrupt controller */
244 outb(0x11, 0xA0); /* Start init sequence */
245 outb(0x08, 0xA1); /* Vector base */
246 outb(0x02, 0xA1); /* edge triggered, Cascade (slave) on IRQ2 */
247 outb(0x01, 0xA1); /* Select 8086 mode */
248
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000249 /* That thing is slow */
250 udelay(100);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* always read ISR */
253 outb(0x0B, 0x20);
254 outb(0x0B, 0xA0);
255
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000256 /* Unmask the internal cascade */
257 cached_21 &= ~(1 << 2);
258
259 /* Set interrupt masks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 outb(cached_A1, 0xA1);
261 outb(cached_21, 0x21);
262
Thomas Gleixner47e3c902010-02-18 02:23:11 +0000263 raw_spin_unlock_irqrestore(&i8259_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000265 /* create a legacy host */
Michael Ellerman19fc65b2008-05-26 12:12:32 +1000266 i8259_host = irq_alloc_host(node, IRQ_HOST_MAP_LEGACY,
Michael Ellerman52964f82007-08-28 18:47:54 +1000267 0, &i8259_host_ops, 0);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000268 if (i8259_host == NULL) {
269 printk(KERN_ERR "i8259: failed to allocate irq host !\n");
270 return;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000271 }
David Woodhouse9d2ba6f2005-11-05 17:54:22 +0000272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* reserve our resources */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000274 /* XXX should we continue doing that ? it seems to cause problems
275 * with further requesting of PCI IO resources for that range...
276 * need to look into it.
277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 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);
Paul Mackerrasf9bd1702005-10-26 16:47:42 +1000284
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000285 printk(KERN_INFO "i8259 legacy interrupt controller initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}