2 * arch/powerpc/sysdev/uic.c
4 * IBM PowerPC 4xx Universal Interrupt Controller
6 * Copyright 2007 David Gibson <dwg@au1.ibm.com>, IBM Corporation.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/reboot.h>
17 #include <linux/slab.h>
18 #include <linux/stddef.h>
19 #include <linux/sched.h>
20 #include <linux/signal.h>
21 #include <linux/sysdev.h>
22 #include <linux/device.h>
23 #include <linux/bootmem.h>
24 #include <linux/spinlock.h>
25 #include <linux/irq.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel_stat.h>
33 #define NR_UIC_INTS 32
44 #define uic_irq_to_hw(virq) (irq_map[virq].hwirq)
46 struct uic *primary_uic;
54 /* The remapper for this UIC */
55 struct irq_host *irqhost;
58 static void uic_unmask_irq(unsigned int virq)
60 struct irq_desc *desc = get_irq_desc(virq);
61 struct uic *uic = get_irq_chip_data(virq);
62 unsigned int src = uic_irq_to_hw(virq);
67 spin_lock_irqsave(&uic->lock, flags);
68 /* ack level-triggered interrupts here */
69 if (desc->status & IRQ_LEVEL)
70 mtdcr(uic->dcrbase + UIC_SR, sr);
71 er = mfdcr(uic->dcrbase + UIC_ER);
73 mtdcr(uic->dcrbase + UIC_ER, er);
74 spin_unlock_irqrestore(&uic->lock, flags);
77 static void uic_mask_irq(unsigned int virq)
79 struct uic *uic = get_irq_chip_data(virq);
80 unsigned int src = uic_irq_to_hw(virq);
84 spin_lock_irqsave(&uic->lock, flags);
85 er = mfdcr(uic->dcrbase + UIC_ER);
86 er &= ~(1 << (31 - src));
87 mtdcr(uic->dcrbase + UIC_ER, er);
88 spin_unlock_irqrestore(&uic->lock, flags);
91 static void uic_ack_irq(unsigned int virq)
93 struct uic *uic = get_irq_chip_data(virq);
94 unsigned int src = uic_irq_to_hw(virq);
97 spin_lock_irqsave(&uic->lock, flags);
98 mtdcr(uic->dcrbase + UIC_SR, 1 << (31-src));
99 spin_unlock_irqrestore(&uic->lock, flags);
102 static void uic_mask_ack_irq(unsigned int virq)
104 struct irq_desc *desc = get_irq_desc(virq);
105 struct uic *uic = get_irq_chip_data(virq);
106 unsigned int src = uic_irq_to_hw(virq);
111 spin_lock_irqsave(&uic->lock, flags);
112 er = mfdcr(uic->dcrbase + UIC_ER);
114 mtdcr(uic->dcrbase + UIC_ER, er);
115 /* On the UIC, acking (i.e. clearing the SR bit)
116 * a level irq will have no effect if the interrupt
117 * is still asserted by the device, even if
118 * the interrupt is already masked. Therefore
119 * we only ack the egde interrupts here, while
120 * level interrupts are ack'ed after the actual
121 * isr call in the uic_unmask_irq()
123 if (!(desc->status & IRQ_LEVEL))
124 mtdcr(uic->dcrbase + UIC_SR, sr);
125 spin_unlock_irqrestore(&uic->lock, flags);
128 static int uic_set_irq_type(unsigned int virq, unsigned int flow_type)
130 struct uic *uic = get_irq_chip_data(virq);
131 unsigned int src = uic_irq_to_hw(virq);
132 struct irq_desc *desc = get_irq_desc(virq);
134 int trigger, polarity;
137 switch (flow_type & IRQ_TYPE_SENSE_MASK) {
142 case IRQ_TYPE_EDGE_RISING:
143 trigger = 1; polarity = 1;
145 case IRQ_TYPE_EDGE_FALLING:
146 trigger = 1; polarity = 0;
148 case IRQ_TYPE_LEVEL_HIGH:
149 trigger = 0; polarity = 1;
151 case IRQ_TYPE_LEVEL_LOW:
152 trigger = 0; polarity = 0;
158 mask = ~(1 << (31 - src));
160 spin_lock_irqsave(&uic->lock, flags);
161 tr = mfdcr(uic->dcrbase + UIC_TR);
162 pr = mfdcr(uic->dcrbase + UIC_PR);
163 tr = (tr & mask) | (trigger << (31-src));
164 pr = (pr & mask) | (polarity << (31-src));
166 mtdcr(uic->dcrbase + UIC_PR, pr);
167 mtdcr(uic->dcrbase + UIC_TR, tr);
169 desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
170 desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
172 desc->status |= IRQ_LEVEL;
174 spin_unlock_irqrestore(&uic->lock, flags);
179 static struct irq_chip uic_irq_chip = {
181 .unmask = uic_unmask_irq,
182 .mask = uic_mask_irq,
183 .mask_ack = uic_mask_ack_irq,
185 .set_type = uic_set_irq_type,
188 static int uic_host_map(struct irq_host *h, unsigned int virq,
191 struct uic *uic = h->host_data;
193 set_irq_chip_data(virq, uic);
194 /* Despite the name, handle_level_irq() works for both level
195 * and edge irqs on UIC. FIXME: check this is correct */
196 set_irq_chip_and_handler(virq, &uic_irq_chip, handle_level_irq);
198 /* Set default irq type */
199 set_irq_type(virq, IRQ_TYPE_NONE);
204 static int uic_host_xlate(struct irq_host *h, struct device_node *ct,
205 u32 *intspec, unsigned int intsize,
206 irq_hw_number_t *out_hwirq, unsigned int *out_type)
209 /* UIC intspecs must have 2 cells */
210 BUG_ON(intsize != 2);
211 *out_hwirq = intspec[0];
212 *out_type = intspec[1];
216 static struct irq_host_ops uic_host_ops = {
218 .xlate = uic_host_xlate,
221 void uic_irq_cascade(unsigned int virq, struct irq_desc *desc)
223 struct uic *uic = get_irq_data(virq);
228 spin_lock(&desc->lock);
229 if (desc->status & IRQ_LEVEL)
230 desc->chip->mask(virq);
232 desc->chip->mask_ack(virq);
233 spin_unlock(&desc->lock);
235 msr = mfdcr(uic->dcrbase + UIC_MSR);
236 if (!msr) /* spurious interrupt */
241 subvirq = irq_linear_revmap(uic->irqhost, src);
242 generic_handle_irq(subvirq);
245 spin_lock(&desc->lock);
246 if (desc->status & IRQ_LEVEL)
247 desc->chip->ack(virq);
248 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
249 desc->chip->unmask(virq);
250 spin_unlock(&desc->lock);
253 static struct uic * __init uic_init_one(struct device_node *node)
256 const u32 *indexp, *dcrreg;
259 BUG_ON(! of_device_is_compatible(node, "ibm,uic"));
261 uic = alloc_bootmem(sizeof(*uic));
263 return NULL; /* FIXME: panic? */
265 memset(uic, 0, sizeof(*uic));
266 spin_lock_init(&uic->lock);
267 indexp = of_get_property(node, "cell-index", &len);
268 if (!indexp || (len != sizeof(u32))) {
269 printk(KERN_ERR "uic: Device node %s has missing or invalid "
270 "cell-index property\n", node->full_name);
273 uic->index = *indexp;
275 dcrreg = of_get_property(node, "dcr-reg", &len);
276 if (!dcrreg || (len != 2*sizeof(u32))) {
277 printk(KERN_ERR "uic: Device node %s has missing or invalid "
278 "dcr-reg property\n", node->full_name);
281 uic->dcrbase = *dcrreg;
283 uic->irqhost = irq_alloc_host(of_node_get(node), IRQ_HOST_MAP_LINEAR,
284 NR_UIC_INTS, &uic_host_ops, -1);
285 if (! uic->irqhost) {
287 return NULL; /* FIXME: panic? */
290 uic->irqhost->host_data = uic;
292 /* Start with all interrupts disabled, level and non-critical */
293 mtdcr(uic->dcrbase + UIC_ER, 0);
294 mtdcr(uic->dcrbase + UIC_CR, 0);
295 mtdcr(uic->dcrbase + UIC_TR, 0);
296 /* Clear any pending interrupts, in case the firmware left some */
297 mtdcr(uic->dcrbase + UIC_SR, 0xffffffff);
299 printk ("UIC%d (%d IRQ sources) at DCR 0x%x\n", uic->index,
300 NR_UIC_INTS, uic->dcrbase);
305 void __init uic_init_tree(void)
307 struct device_node *np;
309 const u32 *interrupts;
311 /* First locate and initialize the top-level UIC */
312 for_each_compatible_node(np, NULL, "ibm,uic") {
313 interrupts = of_get_property(np, "interrupts", NULL);
318 BUG_ON(!np); /* uic_init_tree() assumes there's a UIC as the
319 * top-level interrupt controller */
320 primary_uic = uic_init_one(np);
322 panic("Unable to initialize primary UIC %s\n", np->full_name);
324 irq_set_default_host(primary_uic->irqhost);
327 /* The scan again for cascaded UICs */
328 for_each_compatible_node(np, NULL, "ibm,uic") {
329 interrupts = of_get_property(np, "interrupts", NULL);
334 uic = uic_init_one(np);
336 panic("Unable to initialize a secondary UIC %s\n",
339 cascade_virq = irq_of_parse_and_map(np, 0);
341 set_irq_data(cascade_virq, uic);
342 set_irq_chained_handler(cascade_virq, uic_irq_cascade);
344 /* FIXME: setup critical cascade?? */
349 /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
350 unsigned int uic_get_irq(void)
355 BUG_ON(! primary_uic);
357 msr = mfdcr(primary_uic->dcrbase + UIC_MSR);
360 return irq_linear_revmap(primary_uic->irqhost, src);