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;
57 /* For secondary UICs, the cascade interrupt's irqaction */
58 struct irqaction cascade;
60 /* The device node of the interrupt controller */
61 struct device_node *of_node;
64 static void uic_unmask_irq(unsigned int virq)
66 struct uic *uic = get_irq_chip_data(virq);
67 unsigned int src = uic_irq_to_hw(virq);
71 spin_lock_irqsave(&uic->lock, flags);
72 er = mfdcr(uic->dcrbase + UIC_ER);
73 er |= 1 << (31 - src);
74 mtdcr(uic->dcrbase + UIC_ER, er);
75 spin_unlock_irqrestore(&uic->lock, flags);
78 static void uic_mask_irq(unsigned int virq)
80 struct uic *uic = get_irq_chip_data(virq);
81 unsigned int src = uic_irq_to_hw(virq);
85 spin_lock_irqsave(&uic->lock, flags);
86 er = mfdcr(uic->dcrbase + UIC_ER);
87 er &= ~(1 << (31 - src));
88 mtdcr(uic->dcrbase + UIC_ER, er);
89 spin_unlock_irqrestore(&uic->lock, flags);
92 static void uic_ack_irq(unsigned int virq)
94 struct uic *uic = get_irq_chip_data(virq);
95 unsigned int src = uic_irq_to_hw(virq);
98 spin_lock_irqsave(&uic->lock, flags);
99 mtdcr(uic->dcrbase + UIC_SR, 1 << (31-src));
100 spin_unlock_irqrestore(&uic->lock, flags);
103 static int uic_set_irq_type(unsigned int virq, unsigned int flow_type)
105 struct uic *uic = get_irq_chip_data(virq);
106 unsigned int src = uic_irq_to_hw(virq);
107 struct irq_desc *desc = get_irq_desc(virq);
109 int trigger, polarity;
112 switch (flow_type & IRQ_TYPE_SENSE_MASK) {
117 case IRQ_TYPE_EDGE_RISING:
118 trigger = 1; polarity = 1;
120 case IRQ_TYPE_EDGE_FALLING:
121 trigger = 1; polarity = 0;
123 case IRQ_TYPE_LEVEL_HIGH:
124 trigger = 0; polarity = 1;
126 case IRQ_TYPE_LEVEL_LOW:
127 trigger = 0; polarity = 0;
133 mask = ~(1 << (31 - src));
135 spin_lock_irqsave(&uic->lock, flags);
136 tr = mfdcr(uic->dcrbase + UIC_TR);
137 pr = mfdcr(uic->dcrbase + UIC_PR);
138 tr = (tr & mask) | (trigger << (31-src));
139 pr = (pr & mask) | (polarity << (31-src));
141 mtdcr(uic->dcrbase + UIC_PR, pr);
142 mtdcr(uic->dcrbase + UIC_TR, tr);
144 desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
145 desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
147 desc->status |= IRQ_LEVEL;
149 spin_unlock_irqrestore(&uic->lock, flags);
154 static struct irq_chip uic_irq_chip = {
156 .unmask = uic_unmask_irq,
157 .mask = uic_mask_irq,
158 /* .mask_ack = uic_mask_irq_and_ack, */
160 .set_type = uic_set_irq_type,
164 * handle_uic_irq - irq flow handler for UIC
165 * @irq: the interrupt number
166 * @desc: the interrupt description structure for this irq
168 * This is modified version of the generic handle_level_irq() suitable
169 * for the UIC. On the UIC, acking (i.e. clearing the SR bit) a level
170 * irq will have no effect if the interrupt is still asserted by the
171 * device, even if the interrupt is already masked. Therefore, unlike
172 * the standard handle_level_irq(), we must ack the interrupt *after*
173 * invoking the ISR (which should have de-asserted the interrupt in
174 * the external source). For edge interrupts we ack at the beginning
175 * instead of the end, to keep the window in which we can miss an
176 * interrupt as small as possible.
178 void fastcall handle_uic_irq(unsigned int irq, struct irq_desc *desc)
180 unsigned int cpu = smp_processor_id();
181 struct irqaction *action;
182 irqreturn_t action_ret;
184 spin_lock(&desc->lock);
185 if (desc->status & IRQ_LEVEL)
186 desc->chip->mask(irq);
188 desc->chip->mask_ack(irq);
190 if (unlikely(desc->status & IRQ_INPROGRESS))
192 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
193 kstat_cpu(cpu).irqs[irq]++;
196 * If its disabled or no action available
197 * keep it masked and get out of here
199 action = desc->action;
200 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
201 desc->status |= IRQ_PENDING;
205 desc->status |= IRQ_INPROGRESS;
206 desc->status &= ~IRQ_PENDING;
207 spin_unlock(&desc->lock);
209 action_ret = handle_IRQ_event(irq, action);
211 spin_lock(&desc->lock);
212 desc->status &= ~IRQ_INPROGRESS;
213 if (desc->status & IRQ_LEVEL)
214 desc->chip->ack(irq);
215 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
216 desc->chip->unmask(irq);
218 spin_unlock(&desc->lock);
221 static int uic_host_match(struct irq_host *h, struct device_node *node)
223 struct uic *uic = h->host_data;
224 return uic->of_node == node;
227 static int uic_host_map(struct irq_host *h, unsigned int virq,
230 struct uic *uic = h->host_data;
232 set_irq_chip_data(virq, uic);
233 /* Despite the name, handle_level_irq() works for both level
234 * and edge irqs on UIC. FIXME: check this is correct */
235 set_irq_chip_and_handler(virq, &uic_irq_chip, handle_uic_irq);
237 /* Set default irq type */
238 set_irq_type(virq, IRQ_TYPE_NONE);
243 static int uic_host_xlate(struct irq_host *h, struct device_node *ct,
244 u32 *intspec, unsigned int intsize,
245 irq_hw_number_t *out_hwirq, unsigned int *out_type)
248 /* UIC intspecs must have 2 cells */
249 BUG_ON(intsize != 2);
250 *out_hwirq = intspec[0];
251 *out_type = intspec[1];
255 static struct irq_host_ops uic_host_ops = {
256 .match = uic_host_match,
258 .xlate = uic_host_xlate,
261 irqreturn_t uic_cascade(int virq, void *data)
263 struct uic *uic = data;
268 msr = mfdcr(uic->dcrbase + UIC_MSR);
269 if (!msr) /* spurious interrupt */
274 subvirq = irq_linear_revmap(uic->irqhost, src);
275 generic_handle_irq(subvirq);
280 static struct uic * __init uic_init_one(struct device_node *node)
283 const u32 *indexp, *dcrreg;
286 BUG_ON(! of_device_is_compatible(node, "ibm,uic"));
288 uic = alloc_bootmem(sizeof(*uic));
290 return NULL; /* FIXME: panic? */
292 memset(uic, 0, sizeof(*uic));
293 spin_lock_init(&uic->lock);
294 uic->of_node = of_node_get(node);
295 indexp = of_get_property(node, "cell-index", &len);
296 if (!indexp || (len != sizeof(u32))) {
297 printk(KERN_ERR "uic: Device node %s has missing or invalid "
298 "cell-index property\n", node->full_name);
301 uic->index = *indexp;
303 dcrreg = of_get_property(node, "dcr-reg", &len);
304 if (!dcrreg || (len != 2*sizeof(u32))) {
305 printk(KERN_ERR "uic: Device node %s has missing or invalid "
306 "dcr-reg property\n", node->full_name);
309 uic->dcrbase = *dcrreg;
311 uic->irqhost = irq_alloc_host(IRQ_HOST_MAP_LINEAR, NR_UIC_INTS,
313 if (! uic->irqhost) {
315 return NULL; /* FIXME: panic? */
318 uic->irqhost->host_data = uic;
320 /* Start with all interrupts disabled, level and non-critical */
321 mtdcr(uic->dcrbase + UIC_ER, 0);
322 mtdcr(uic->dcrbase + UIC_CR, 0);
323 mtdcr(uic->dcrbase + UIC_TR, 0);
324 /* Clear any pending interrupts, in case the firmware left some */
325 mtdcr(uic->dcrbase + UIC_SR, 0xffffffff);
327 printk ("UIC%d (%d IRQ sources) at DCR 0x%x\n", uic->index,
328 NR_UIC_INTS, uic->dcrbase);
333 void __init uic_init_tree(void)
335 struct device_node *np;
337 const u32 *interrupts;
339 /* First locate and initialize the top-level UIC */
341 np = of_find_compatible_node(NULL, NULL, "ibm,uic");
343 interrupts = of_get_property(np, "interrupts", NULL);
347 np = of_find_compatible_node(np, NULL, "ibm,uic");
350 BUG_ON(!np); /* uic_init_tree() assumes there's a UIC as the
351 * top-level interrupt controller */
352 primary_uic = uic_init_one(np);
354 panic("Unable to initialize primary UIC %s\n", np->full_name);
356 irq_set_default_host(primary_uic->irqhost);
359 /* The scan again for cascaded UICs */
360 np = of_find_compatible_node(NULL, NULL, "ibm,uic");
362 interrupts = of_get_property(np, "interrupts", NULL);
368 uic = uic_init_one(np);
370 panic("Unable to initialize a secondary UIC %s\n",
373 cascade_virq = irq_of_parse_and_map(np, 0);
375 uic->cascade.handler = uic_cascade;
376 uic->cascade.name = "UIC cascade";
377 uic->cascade.dev_id = uic;
379 ret = setup_irq(cascade_virq, &uic->cascade);
381 printk(KERN_ERR "Failed to setup_irq(%d) for "
382 "UIC%d cascade\n", cascade_virq,
385 /* FIXME: setup critical cascade?? */
388 np = of_find_compatible_node(np, NULL, "ibm,uic");
392 /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
393 unsigned int uic_get_irq(void)
398 BUG_ON(! primary_uic);
400 msr = mfdcr(primary_uic->dcrbase + UIC_MSR);
403 return irq_linear_revmap(primary_uic->irqhost, src);