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 struct uic
*primary_uic
;
52 /* The remapper for this UIC */
53 struct irq_host
*irqhost
;
56 static void uic_unmask_irq(struct irq_data
*d
)
58 struct uic
*uic
= irq_data_get_irq_chip_data(d
);
59 unsigned int src
= irqd_to_hwirq(d
);
64 raw_spin_lock_irqsave(&uic
->lock
, flags
);
65 /* ack level-triggered interrupts here */
66 if (irqd_is_level_type(d
))
67 mtdcr(uic
->dcrbase
+ UIC_SR
, sr
);
68 er
= mfdcr(uic
->dcrbase
+ UIC_ER
);
70 mtdcr(uic
->dcrbase
+ UIC_ER
, er
);
71 raw_spin_unlock_irqrestore(&uic
->lock
, flags
);
74 static void uic_mask_irq(struct irq_data
*d
)
76 struct uic
*uic
= irq_data_get_irq_chip_data(d
);
77 unsigned int src
= irqd_to_hwirq(d
);
81 raw_spin_lock_irqsave(&uic
->lock
, flags
);
82 er
= mfdcr(uic
->dcrbase
+ UIC_ER
);
83 er
&= ~(1 << (31 - src
));
84 mtdcr(uic
->dcrbase
+ UIC_ER
, er
);
85 raw_spin_unlock_irqrestore(&uic
->lock
, flags
);
88 static void uic_ack_irq(struct irq_data
*d
)
90 struct uic
*uic
= irq_data_get_irq_chip_data(d
);
91 unsigned int src
= irqd_to_hwirq(d
);
94 raw_spin_lock_irqsave(&uic
->lock
, flags
);
95 mtdcr(uic
->dcrbase
+ UIC_SR
, 1 << (31-src
));
96 raw_spin_unlock_irqrestore(&uic
->lock
, flags
);
99 static void uic_mask_ack_irq(struct irq_data
*d
)
101 struct uic
*uic
= irq_data_get_irq_chip_data(d
);
102 unsigned int src
= irqd_to_hwirq(d
);
107 raw_spin_lock_irqsave(&uic
->lock
, flags
);
108 er
= mfdcr(uic
->dcrbase
+ UIC_ER
);
110 mtdcr(uic
->dcrbase
+ UIC_ER
, er
);
111 /* On the UIC, acking (i.e. clearing the SR bit)
112 * a level irq will have no effect if the interrupt
113 * is still asserted by the device, even if
114 * the interrupt is already masked. Therefore
115 * we only ack the egde interrupts here, while
116 * level interrupts are ack'ed after the actual
117 * isr call in the uic_unmask_irq()
119 if (!irqd_is_level_type(d
))
120 mtdcr(uic
->dcrbase
+ UIC_SR
, sr
);
121 raw_spin_unlock_irqrestore(&uic
->lock
, flags
);
124 static int uic_set_irq_type(struct irq_data
*d
, unsigned int flow_type
)
126 struct uic
*uic
= irq_data_get_irq_chip_data(d
);
127 unsigned int src
= irqd_to_hwirq(d
);
129 int trigger
, polarity
;
132 switch (flow_type
& IRQ_TYPE_SENSE_MASK
) {
137 case IRQ_TYPE_EDGE_RISING
:
138 trigger
= 1; polarity
= 1;
140 case IRQ_TYPE_EDGE_FALLING
:
141 trigger
= 1; polarity
= 0;
143 case IRQ_TYPE_LEVEL_HIGH
:
144 trigger
= 0; polarity
= 1;
146 case IRQ_TYPE_LEVEL_LOW
:
147 trigger
= 0; polarity
= 0;
153 mask
= ~(1 << (31 - src
));
155 raw_spin_lock_irqsave(&uic
->lock
, flags
);
156 tr
= mfdcr(uic
->dcrbase
+ UIC_TR
);
157 pr
= mfdcr(uic
->dcrbase
+ UIC_PR
);
158 tr
= (tr
& mask
) | (trigger
<< (31-src
));
159 pr
= (pr
& mask
) | (polarity
<< (31-src
));
161 mtdcr(uic
->dcrbase
+ UIC_PR
, pr
);
162 mtdcr(uic
->dcrbase
+ UIC_TR
, tr
);
164 raw_spin_unlock_irqrestore(&uic
->lock
, flags
);
169 static struct irq_chip uic_irq_chip
= {
171 .irq_unmask
= uic_unmask_irq
,
172 .irq_mask
= uic_mask_irq
,
173 .irq_mask_ack
= uic_mask_ack_irq
,
174 .irq_ack
= uic_ack_irq
,
175 .irq_set_type
= uic_set_irq_type
,
178 static int uic_host_map(struct irq_host
*h
, unsigned int virq
,
181 struct uic
*uic
= h
->host_data
;
183 irq_set_chip_data(virq
, uic
);
184 /* Despite the name, handle_level_irq() works for both level
185 * and edge irqs on UIC. FIXME: check this is correct */
186 irq_set_chip_and_handler(virq
, &uic_irq_chip
, handle_level_irq
);
188 /* Set default irq type */
189 irq_set_irq_type(virq
, IRQ_TYPE_NONE
);
194 static int uic_host_xlate(struct irq_host
*h
, struct device_node
*ct
,
195 const u32
*intspec
, unsigned int intsize
,
196 irq_hw_number_t
*out_hwirq
, unsigned int *out_type
)
199 /* UIC intspecs must have 2 cells */
200 BUG_ON(intsize
!= 2);
201 *out_hwirq
= intspec
[0];
202 *out_type
= intspec
[1];
206 static struct irq_host_ops uic_host_ops
= {
208 .xlate
= uic_host_xlate
,
211 void uic_irq_cascade(unsigned int virq
, struct irq_desc
*desc
)
213 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
214 struct irq_data
*idata
= irq_desc_get_irq_data(desc
);
215 struct uic
*uic
= irq_get_handler_data(virq
);
220 raw_spin_lock(&desc
->lock
);
221 if (irqd_is_level_type(idata
))
222 chip
->irq_mask(idata
);
224 chip
->irq_mask_ack(idata
);
225 raw_spin_unlock(&desc
->lock
);
227 msr
= mfdcr(uic
->dcrbase
+ UIC_MSR
);
228 if (!msr
) /* spurious interrupt */
233 subvirq
= irq_linear_revmap(uic
->irqhost
, src
);
234 generic_handle_irq(subvirq
);
237 raw_spin_lock(&desc
->lock
);
238 if (irqd_is_level_type(idata
))
239 chip
->irq_ack(idata
);
240 if (!irqd_irq_disabled(idata
) && chip
->irq_unmask
)
241 chip
->irq_unmask(idata
);
242 raw_spin_unlock(&desc
->lock
);
245 static struct uic
* __init
uic_init_one(struct device_node
*node
)
248 const u32
*indexp
, *dcrreg
;
251 BUG_ON(! of_device_is_compatible(node
, "ibm,uic"));
253 uic
= kzalloc(sizeof(*uic
), GFP_KERNEL
);
255 return NULL
; /* FIXME: panic? */
257 raw_spin_lock_init(&uic
->lock
);
258 indexp
= of_get_property(node
, "cell-index", &len
);
259 if (!indexp
|| (len
!= sizeof(u32
))) {
260 printk(KERN_ERR
"uic: Device node %s has missing or invalid "
261 "cell-index property\n", node
->full_name
);
264 uic
->index
= *indexp
;
266 dcrreg
= of_get_property(node
, "dcr-reg", &len
);
267 if (!dcrreg
|| (len
!= 2*sizeof(u32
))) {
268 printk(KERN_ERR
"uic: Device node %s has missing or invalid "
269 "dcr-reg property\n", node
->full_name
);
272 uic
->dcrbase
= *dcrreg
;
274 uic
->irqhost
= irq_alloc_host(node
, IRQ_HOST_MAP_LINEAR
,
275 NR_UIC_INTS
, &uic_host_ops
, -1);
277 return NULL
; /* FIXME: panic? */
279 uic
->irqhost
->host_data
= uic
;
281 /* Start with all interrupts disabled, level and non-critical */
282 mtdcr(uic
->dcrbase
+ UIC_ER
, 0);
283 mtdcr(uic
->dcrbase
+ UIC_CR
, 0);
284 mtdcr(uic
->dcrbase
+ UIC_TR
, 0);
285 /* Clear any pending interrupts, in case the firmware left some */
286 mtdcr(uic
->dcrbase
+ UIC_SR
, 0xffffffff);
288 printk ("UIC%d (%d IRQ sources) at DCR 0x%x\n", uic
->index
,
289 NR_UIC_INTS
, uic
->dcrbase
);
294 void __init
uic_init_tree(void)
296 struct device_node
*np
;
298 const u32
*interrupts
;
300 /* First locate and initialize the top-level UIC */
301 for_each_compatible_node(np
, NULL
, "ibm,uic") {
302 interrupts
= of_get_property(np
, "interrupts", NULL
);
307 BUG_ON(!np
); /* uic_init_tree() assumes there's a UIC as the
308 * top-level interrupt controller */
309 primary_uic
= uic_init_one(np
);
311 panic("Unable to initialize primary UIC %s\n", np
->full_name
);
313 irq_set_default_host(primary_uic
->irqhost
);
316 /* The scan again for cascaded UICs */
317 for_each_compatible_node(np
, NULL
, "ibm,uic") {
318 interrupts
= of_get_property(np
, "interrupts", NULL
);
323 uic
= uic_init_one(np
);
325 panic("Unable to initialize a secondary UIC %s\n",
328 cascade_virq
= irq_of_parse_and_map(np
, 0);
330 irq_set_handler_data(cascade_virq
, uic
);
331 irq_set_chained_handler(cascade_virq
, uic_irq_cascade
);
333 /* FIXME: setup critical cascade?? */
338 /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
339 unsigned int uic_get_irq(void)
344 BUG_ON(! primary_uic
);
346 msr
= mfdcr(primary_uic
->dcrbase
+ UIC_MSR
);
349 return irq_linear_revmap(primary_uic
->irqhost
, src
);