i8259.c 7.2 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * i8259 interrupt controller driver.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */
9 10
#undef DEBUG

L
Linus Torvalds 已提交
11 12 13
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
14 15
#include <linux/kernel.h>
#include <linux/delay.h>
L
Linus Torvalds 已提交
16 17
#include <asm/io.h>
#include <asm/i8259.h>
18
#include <asm/prom.h>
L
Linus Torvalds 已提交
19

20
static volatile void __iomem *pci_intack; /* RO, gives us the irq vector */
L
Linus Torvalds 已提交
21

22
static unsigned char cached_8259[2] = { 0xff, 0xff };
L
Linus Torvalds 已提交
23 24 25 26 27
#define cached_A1 (cached_8259[0])
#define cached_21 (cached_8259[1])

static DEFINE_SPINLOCK(i8259_lock);

28 29
static struct device_node *i8259_node;
static struct irq_host *i8259_host;
L
Linus Torvalds 已提交
30 31 32 33 34 35 36

/*
 * Acknowledge the IRQ using either the PCI host bridge's interrupt
 * acknowledge feature or poll.  How i8259_init() is called determines
 * which is called.  It should be noted that polling is broken on some
 * IBM and Motorola PReP boxes so we must use the int-ack feature on them.
 */
37
unsigned int i8259_irq(struct pt_regs *regs)
L
Linus Torvalds 已提交
38 39
{
	int irq;
40
	int lock = 0;
L
Linus Torvalds 已提交
41 42 43

	/* Either int-ack or poll for the IRQ */
	if (pci_intack)
44
		irq = readb(pci_intack);
L
Linus Torvalds 已提交
45
	else {
46 47 48
		spin_lock(&i8259_lock);
		lock = 1;

L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		/* Perform an interrupt acknowledge cycle on controller 1. */
		outb(0x0C, 0x20);		/* prepare for poll */
		irq = inb(0x20) & 7;
		if (irq == 2 ) {
			/*
			 * Interrupt is cascaded so perform interrupt
			 * acknowledge on controller 2.
			 */
			outb(0x0C, 0xA0);	/* prepare for poll */
			irq = (inb(0xA0) & 7) + 8;
		}
	}

	if (irq == 7) {
		/*
		 * This may be a spurious interrupt.
		 *
		 * Read the interrupt status register (ISR). If the most
		 * significant bit is not set then there is no valid
		 * interrupt.
		 */
		if (!pci_intack)
			outb(0x0B, 0x20);	/* ISR register */
		if(~inb(0x20) & 0x80)
73 74 75
			irq = NO_IRQ;
	} else if (irq == 0xff)
		irq = NO_IRQ;
L
Linus Torvalds 已提交
76

77 78 79
	if (lock)
		spin_unlock(&i8259_lock);
	return irq;
80 81
}

L
Linus Torvalds 已提交
82 83 84 85 86 87 88
static void i8259_mask_and_ack_irq(unsigned int irq_nr)
{
	unsigned long flags;

	spin_lock_irqsave(&i8259_lock, flags);
	if (irq_nr > 7) {
		cached_A1 |= 1 << (irq_nr-8);
89 90 91 92
		inb(0xA1); 	/* DUMMY */
		outb(cached_A1, 0xA1);
		outb(0x20, 0xA0);	/* Non-specific EOI */
		outb(0x20, 0x20);	/* Non-specific EOI to cascade */
L
Linus Torvalds 已提交
93 94
	} else {
		cached_21 |= 1 << irq_nr;
95 96 97
		inb(0x21); 	/* DUMMY */
		outb(cached_21, 0x21);
		outb(0x20, 0x20);	/* Non-specific EOI */
L
Linus Torvalds 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111
	}
	spin_unlock_irqrestore(&i8259_lock, flags);
}

static void i8259_set_irq_mask(int irq_nr)
{
	outb(cached_A1,0xA1);
	outb(cached_21,0x21);
}

static void i8259_mask_irq(unsigned int irq_nr)
{
	unsigned long flags;

112 113
	pr_debug("i8259_mask_irq(%d)\n", irq_nr);

L
Linus Torvalds 已提交
114
	spin_lock_irqsave(&i8259_lock, flags);
115
	if (irq_nr < 8)
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126
		cached_21 |= 1 << irq_nr;
	else
		cached_A1 |= 1 << (irq_nr-8);
	i8259_set_irq_mask(irq_nr);
	spin_unlock_irqrestore(&i8259_lock, flags);
}

static void i8259_unmask_irq(unsigned int irq_nr)
{
	unsigned long flags;

127 128
	pr_debug("i8259_unmask_irq(%d)\n", irq_nr);

L
Linus Torvalds 已提交
129
	spin_lock_irqsave(&i8259_lock, flags);
130
	if (irq_nr < 8)
L
Linus Torvalds 已提交
131 132 133 134 135 136 137
		cached_21 &= ~(1 << irq_nr);
	else
		cached_A1 &= ~(1 << (irq_nr-8));
	i8259_set_irq_mask(irq_nr);
	spin_unlock_irqrestore(&i8259_lock, flags);
}

138 139 140 141 142
static struct irq_chip i8259_pic = {
	.typename	= " i8259    ",
	.mask		= i8259_mask_irq,
	.unmask		= i8259_unmask_irq,
	.mask_ack	= i8259_mask_and_ack_irq,
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
};

static struct resource pic1_iores = {
	.name = "8259 (master)",
	.start = 0x20,
	.end = 0x21,
	.flags = IORESOURCE_BUSY,
};

static struct resource pic2_iores = {
	.name = "8259 (slave)",
	.start = 0xa0,
	.end = 0xa1,
	.flags = IORESOURCE_BUSY,
};

static struct resource pic_edgectrl_iores = {
	.name = "8259 edge control",
	.start = 0x4d0,
	.end = 0x4d1,
	.flags = IORESOURCE_BUSY,
};

166 167 168 169 170 171
static int i8259_host_match(struct irq_host *h, struct device_node *node)
{
	return i8259_node == NULL || i8259_node == node;
}

static int i8259_host_map(struct irq_host *h, unsigned int virq,
172
			  irq_hw_number_t hw)
173 174 175 176 177 178 179
{
	pr_debug("i8259_host_map(%d, 0x%lx)\n", virq, hw);

	/* We block the internal cascade */
	if (hw == 2)
		get_irq_desc(virq)->status |= IRQ_NOREQUEST;

180
	/* We use the level handler only for now, we might want to
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	 * be more cautious here but that works for now
	 */
	get_irq_desc(virq)->status |= IRQ_LEVEL;
	set_irq_chip_and_handler(virq, &i8259_pic, handle_level_irq);
	return 0;
}

static void i8259_host_unmap(struct irq_host *h, unsigned int virq)
{
	/* Make sure irq is masked in hardware */
	i8259_mask_irq(virq);

	/* remove chip and handler */
	set_irq_chip_and_handler(virq, NULL, NULL);

	/* Make sure it's completed */
	synchronize_irq(virq);
}

static int i8259_host_xlate(struct irq_host *h, struct device_node *ct,
			    u32 *intspec, unsigned int intsize,
			    irq_hw_number_t *out_hwirq, unsigned int *out_flags)
{
	static unsigned char map_isa_senses[4] = {
		IRQ_TYPE_LEVEL_LOW,
		IRQ_TYPE_LEVEL_HIGH,
		IRQ_TYPE_EDGE_FALLING,
		IRQ_TYPE_EDGE_RISING,
	};

	*out_hwirq = intspec[0];
	if (intsize > 1 && intspec[1] < 4)
		*out_flags = map_isa_senses[intspec[1]];
	else
		*out_flags = IRQ_TYPE_NONE;

	return 0;
}

static struct irq_host_ops i8259_host_ops = {
	.match = i8259_host_match,
	.map = i8259_host_map,
	.unmap = i8259_host_unmap,
	.xlate = i8259_host_xlate,
L
Linus Torvalds 已提交
225 226
};

227
/**
228 229 230 231 232
 * i8259_init - Initialize the legacy controller
 * @node: device node of the legacy PIC (can be NULL, but then, it will match
 *        all interrupts, so beware)
 * @intack_addr: PCI interrupt acknowledge (real) address which will return
 *             	 the active irq from the 8259
L
Linus Torvalds 已提交
233
 */
234
void i8259_init(struct device_node *node, unsigned long intack_addr)
L
Linus Torvalds 已提交
235 236 237
{
	unsigned long flags;

238
	/* initialize the controller */
L
Linus Torvalds 已提交
239
	spin_lock_irqsave(&i8259_lock, flags);
240 241 242 243

	/* Mask all first */
	outb(0xff, 0xA1);
	outb(0xff, 0x21);
244

L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256
	/* init master interrupt controller */
	outb(0x11, 0x20); /* Start init sequence */
	outb(0x00, 0x21); /* Vector base */
	outb(0x04, 0x21); /* edge tiggered, Cascade (slave) on IRQ2 */
	outb(0x01, 0x21); /* Select 8086 mode */

	/* init slave interrupt controller */
	outb(0x11, 0xA0); /* Start init sequence */
	outb(0x08, 0xA1); /* Vector base */
	outb(0x02, 0xA1); /* edge triggered, Cascade (slave) on IRQ2 */
	outb(0x01, 0xA1); /* Select 8086 mode */

257 258 259
	/* That thing is slow */
	udelay(100);

L
Linus Torvalds 已提交
260 261 262 263
	/* always read ISR */
	outb(0x0B, 0x20);
	outb(0x0B, 0xA0);

264 265 266 267
	/* Unmask the internal cascade */
	cached_21 &= ~(1 << 2);

	/* Set interrupt masks */
L
Linus Torvalds 已提交
268 269 270 271 272
	outb(cached_A1, 0xA1);
	outb(cached_21, 0x21);

	spin_unlock_irqrestore(&i8259_lock, flags);

273 274 275 276 277 278 279
	/* create a legacy host */
	if (node)
		i8259_node = of_node_get(node);
	i8259_host = irq_alloc_host(IRQ_HOST_MAP_LEGACY, 0, &i8259_host_ops, 0);
	if (i8259_host == NULL) {
		printk(KERN_ERR "i8259: failed to allocate irq host !\n");
		return;
280
	}
281

L
Linus Torvalds 已提交
282
	/* reserve our resources */
283 284 285 286
	/* XXX should we continue doing that ? it seems to cause problems
	 * with further requesting of PCI IO resources for that range...
	 * need to look into it.
	 */
L
Linus Torvalds 已提交
287 288 289 290 291 292
	request_resource(&ioport_resource, &pic1_iores);
	request_resource(&ioport_resource, &pic2_iores);
	request_resource(&ioport_resource, &pic_edgectrl_iores);

	if (intack_addr != 0)
		pci_intack = ioremap(intack_addr, 1);
293

294
	printk(KERN_INFO "i8259 legacy interrupt controller initialized\n");
L
Linus Torvalds 已提交
295
}