blob: 984cd2029158db4924ae04d465c7dfc24160d507 [file] [log] [blame]
David Gibsone58923e2007-04-18 16:36:26 +10001/*
2 * arch/powerpc/sysdev/uic.c
3 *
4 * IBM PowerPC 4xx Universal Interrupt Controller
5 *
6 * Copyright 2007 David Gibson <dwg@au1.ibm.com>, IBM Corporation.
7 *
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.
12 */
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>
David Gibson868afce2007-08-14 13:52:42 +100027#include <linux/kernel_stat.h>
David Gibsone58923e2007-04-18 16:36:26 +100028#include <asm/irq.h>
29#include <asm/io.h>
30#include <asm/prom.h>
31#include <asm/dcr.h>
32
33#define NR_UIC_INTS 32
34
35#define UIC_SR 0x0
36#define UIC_ER 0x2
37#define UIC_CR 0x3
38#define UIC_PR 0x4
39#define UIC_TR 0x5
40#define UIC_MSR 0x6
41#define UIC_VR 0x7
42#define UIC_VCR 0x8
43
David Gibsone58923e2007-04-18 16:36:26 +100044struct uic *primary_uic;
45
46struct uic {
47 int index;
48 int dcrbase;
49
50 spinlock_t lock;
51
52 /* The remapper for this UIC */
53 struct irq_host *irqhost;
David Gibsone58923e2007-04-18 16:36:26 +100054};
55
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000056static void uic_unmask_irq(struct irq_data *d)
David Gibsone58923e2007-04-18 16:36:26 +100057{
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000058 struct uic *uic = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +100059 unsigned int src = irqd_to_hwirq(d);
David Gibsone58923e2007-04-18 16:36:26 +100060 unsigned long flags;
Valentine Barshakc8090562007-11-15 01:00:52 +110061 u32 er, sr;
David Gibsone58923e2007-04-18 16:36:26 +100062
Valentine Barshakc8090562007-11-15 01:00:52 +110063 sr = 1 << (31-src);
David Gibsone58923e2007-04-18 16:36:26 +100064 spin_lock_irqsave(&uic->lock, flags);
Valentine Barshakc8090562007-11-15 01:00:52 +110065 /* ack level-triggered interrupts here */
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +010066 if (irqd_is_level_type(d))
Valentine Barshakc8090562007-11-15 01:00:52 +110067 mtdcr(uic->dcrbase + UIC_SR, sr);
David Gibsone58923e2007-04-18 16:36:26 +100068 er = mfdcr(uic->dcrbase + UIC_ER);
Valentine Barshakc8090562007-11-15 01:00:52 +110069 er |= sr;
David Gibsone58923e2007-04-18 16:36:26 +100070 mtdcr(uic->dcrbase + UIC_ER, er);
71 spin_unlock_irqrestore(&uic->lock, flags);
72}
73
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000074static void uic_mask_irq(struct irq_data *d)
David Gibsone58923e2007-04-18 16:36:26 +100075{
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000076 struct uic *uic = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +100077 unsigned int src = irqd_to_hwirq(d);
David Gibsone58923e2007-04-18 16:36:26 +100078 unsigned long flags;
79 u32 er;
80
81 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 spin_unlock_irqrestore(&uic->lock, flags);
86}
87
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000088static void uic_ack_irq(struct irq_data *d)
David Gibsone58923e2007-04-18 16:36:26 +100089{
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000090 struct uic *uic = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +100091 unsigned int src = irqd_to_hwirq(d);
David Gibsone58923e2007-04-18 16:36:26 +100092 unsigned long flags;
93
94 spin_lock_irqsave(&uic->lock, flags);
95 mtdcr(uic->dcrbase + UIC_SR, 1 << (31-src));
96 spin_unlock_irqrestore(&uic->lock, flags);
97}
98
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000099static void uic_mask_ack_irq(struct irq_data *d)
Valentine Barshakb8b799a2007-11-14 07:25:21 +1100100{
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +0000101 struct uic *uic = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +1000102 unsigned int src = irqd_to_hwirq(d);
Valentine Barshakb8b799a2007-11-14 07:25:21 +1100103 unsigned long flags;
104 u32 er, sr;
105
106 sr = 1 << (31-src);
107 spin_lock_irqsave(&uic->lock, flags);
108 er = mfdcr(uic->dcrbase + UIC_ER);
109 er &= ~sr;
110 mtdcr(uic->dcrbase + UIC_ER, er);
Valentine Barshakc8090562007-11-15 01:00:52 +1100111 /* 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()
118 */
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +0100119 if (!irqd_is_level_type(d))
Valentine Barshakc8090562007-11-15 01:00:52 +1100120 mtdcr(uic->dcrbase + UIC_SR, sr);
Valentine Barshakb8b799a2007-11-14 07:25:21 +1100121 spin_unlock_irqrestore(&uic->lock, flags);
122}
123
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +0000124static int uic_set_irq_type(struct irq_data *d, unsigned int flow_type)
David Gibsone58923e2007-04-18 16:36:26 +1000125{
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +0000126 struct uic *uic = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +1000127 unsigned int src = irqd_to_hwirq(d);
David Gibsone58923e2007-04-18 16:36:26 +1000128 unsigned long flags;
129 int trigger, polarity;
130 u32 tr, pr, mask;
131
132 switch (flow_type & IRQ_TYPE_SENSE_MASK) {
133 case IRQ_TYPE_NONE:
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +0000134 uic_mask_irq(d);
David Gibsone58923e2007-04-18 16:36:26 +1000135 return 0;
136
137 case IRQ_TYPE_EDGE_RISING:
138 trigger = 1; polarity = 1;
139 break;
140 case IRQ_TYPE_EDGE_FALLING:
141 trigger = 1; polarity = 0;
142 break;
143 case IRQ_TYPE_LEVEL_HIGH:
144 trigger = 0; polarity = 1;
145 break;
146 case IRQ_TYPE_LEVEL_LOW:
147 trigger = 0; polarity = 0;
148 break;
149 default:
150 return -EINVAL;
151 }
152
153 mask = ~(1 << (31 - src));
154
155 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));
160
161 mtdcr(uic->dcrbase + UIC_PR, pr);
162 mtdcr(uic->dcrbase + UIC_TR, tr);
163
David Gibsone58923e2007-04-18 16:36:26 +1000164 spin_unlock_irqrestore(&uic->lock, flags);
165
166 return 0;
167}
168
169static struct irq_chip uic_irq_chip = {
Anton Blanchardfc380c02010-01-31 20:33:41 +0000170 .name = "UIC",
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +0000171 .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,
David Gibsone58923e2007-04-18 16:36:26 +1000176};
177
David Gibsone58923e2007-04-18 16:36:26 +1000178static int uic_host_map(struct irq_host *h, unsigned int virq,
179 irq_hw_number_t hw)
180{
181 struct uic *uic = h->host_data;
182
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100183 irq_set_chip_data(virq, uic);
David Gibsone58923e2007-04-18 16:36:26 +1000184 /* Despite the name, handle_level_irq() works for both level
185 * and edge irqs on UIC. FIXME: check this is correct */
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100186 irq_set_chip_and_handler(virq, &uic_irq_chip, handle_level_irq);
David Gibsone58923e2007-04-18 16:36:26 +1000187
188 /* Set default irq type */
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100189 irq_set_irq_type(virq, IRQ_TYPE_NONE);
David Gibsone58923e2007-04-18 16:36:26 +1000190
191 return 0;
192}
193
194static int uic_host_xlate(struct irq_host *h, struct device_node *ct,
Roman Fietze40d50cf2009-12-08 02:39:50 +0000195 const u32 *intspec, unsigned int intsize,
David Gibsone58923e2007-04-18 16:36:26 +1000196 irq_hw_number_t *out_hwirq, unsigned int *out_type)
197
198{
199 /* UIC intspecs must have 2 cells */
200 BUG_ON(intsize != 2);
201 *out_hwirq = intspec[0];
202 *out_type = intspec[1];
203 return 0;
204}
205
206static struct irq_host_ops uic_host_ops = {
David Gibsone58923e2007-04-18 16:36:26 +1000207 .map = uic_host_map,
208 .xlate = uic_host_xlate,
209};
210
Valentine Barshak5aac48d2007-12-07 00:48:26 +1100211void uic_irq_cascade(unsigned int virq, struct irq_desc *desc)
David Gibsone58923e2007-04-18 16:36:26 +1000212{
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100213 struct irq_chip *chip = irq_desc_get_chip(desc);
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +0100214 struct irq_data *idata = irq_desc_get_irq_data(desc);
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100215 struct uic *uic = irq_get_handler_data(virq);
David Gibsone58923e2007-04-18 16:36:26 +1000216 u32 msr;
217 int src;
218 int subvirq;
219
Thomas Gleixner239007b2009-11-17 16:46:45 +0100220 raw_spin_lock(&desc->lock);
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +0100221 if (irqd_is_level_type(idata))
222 chip->irq_mask(idata);
Valentine Barshak5aac48d2007-12-07 00:48:26 +1100223 else
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +0100224 chip->irq_mask_ack(idata);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100225 raw_spin_unlock(&desc->lock);
Valentine Barshak5aac48d2007-12-07 00:48:26 +1100226
David Gibsone58923e2007-04-18 16:36:26 +1000227 msr = mfdcr(uic->dcrbase + UIC_MSR);
David Gibson553fdff2007-08-14 13:52:42 +1000228 if (!msr) /* spurious interrupt */
Valentine Barshak5aac48d2007-12-07 00:48:26 +1100229 goto uic_irq_ret;
David Gibson553fdff2007-08-14 13:52:42 +1000230
David Gibsone58923e2007-04-18 16:36:26 +1000231 src = 32 - ffs(msr);
232
233 subvirq = irq_linear_revmap(uic->irqhost, src);
234 generic_handle_irq(subvirq);
235
Valentine Barshak5aac48d2007-12-07 00:48:26 +1100236uic_irq_ret:
Thomas Gleixner239007b2009-11-17 16:46:45 +0100237 raw_spin_lock(&desc->lock);
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +0100238 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);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100242 raw_spin_unlock(&desc->lock);
David Gibsone58923e2007-04-18 16:36:26 +1000243}
244
245static struct uic * __init uic_init_one(struct device_node *node)
246{
247 struct uic *uic;
248 const u32 *indexp, *dcrreg;
249 int len;
250
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000251 BUG_ON(! of_device_is_compatible(node, "ibm,uic"));
David Gibsone58923e2007-04-18 16:36:26 +1000252
Anton Vorontsovea960252009-07-01 10:59:57 +0000253 uic = kzalloc(sizeof(*uic), GFP_KERNEL);
David Gibsone58923e2007-04-18 16:36:26 +1000254 if (! uic)
255 return NULL; /* FIXME: panic? */
256
David Gibsone58923e2007-04-18 16:36:26 +1000257 spin_lock_init(&uic->lock);
Stephen Rothwell12d371a2007-04-29 16:29:08 +1000258 indexp = of_get_property(node, "cell-index", &len);
David Gibsone58923e2007-04-18 16:36:26 +1000259 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);
262 return NULL;
263 }
264 uic->index = *indexp;
265
Stephen Rothwell12d371a2007-04-29 16:29:08 +1000266 dcrreg = of_get_property(node, "dcr-reg", &len);
David Gibsone58923e2007-04-18 16:36:26 +1000267 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);
270 return NULL;
271 }
272 uic->dcrbase = *dcrreg;
273
Michael Ellerman19fc65b2008-05-26 12:12:32 +1000274 uic->irqhost = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR,
Michael Ellerman52964f82007-08-28 18:47:54 +1000275 NR_UIC_INTS, &uic_host_ops, -1);
Michael Ellerman19fc65b2008-05-26 12:12:32 +1000276 if (! uic->irqhost)
David Gibsone58923e2007-04-18 16:36:26 +1000277 return NULL; /* FIXME: panic? */
David Gibsone58923e2007-04-18 16:36:26 +1000278
279 uic->irqhost->host_data = uic;
280
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);
287
288 printk ("UIC%d (%d IRQ sources) at DCR 0x%x\n", uic->index,
289 NR_UIC_INTS, uic->dcrbase);
290
291 return uic;
292}
293
294void __init uic_init_tree(void)
295{
296 struct device_node *np;
297 struct uic *uic;
298 const u32 *interrupts;
299
300 /* First locate and initialize the top-level UIC */
Cyrill Gorcunov26cb7d82007-11-30 06:44:36 +1100301 for_each_compatible_node(np, NULL, "ibm,uic") {
Stephen Rothwell12d371a2007-04-29 16:29:08 +1000302 interrupts = of_get_property(np, "interrupts", NULL);
Cyrill Gorcunov26cb7d82007-11-30 06:44:36 +1100303 if (!interrupts)
David Gibsone58923e2007-04-18 16:36:26 +1000304 break;
David Gibsone58923e2007-04-18 16:36:26 +1000305 }
306
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);
Cyrill Gorcunov26cb7d82007-11-30 06:44:36 +1100310 if (!primary_uic)
David Gibsone58923e2007-04-18 16:36:26 +1000311 panic("Unable to initialize primary UIC %s\n", np->full_name);
312
313 irq_set_default_host(primary_uic->irqhost);
314 of_node_put(np);
315
316 /* The scan again for cascaded UICs */
Cyrill Gorcunov26cb7d82007-11-30 06:44:36 +1100317 for_each_compatible_node(np, NULL, "ibm,uic") {
Stephen Rothwell12d371a2007-04-29 16:29:08 +1000318 interrupts = of_get_property(np, "interrupts", NULL);
David Gibsone58923e2007-04-18 16:36:26 +1000319 if (interrupts) {
320 /* Secondary UIC */
321 int cascade_virq;
David Gibsone58923e2007-04-18 16:36:26 +1000322
323 uic = uic_init_one(np);
324 if (! uic)
325 panic("Unable to initialize a secondary UIC %s\n",
326 np->full_name);
327
328 cascade_virq = irq_of_parse_and_map(np, 0);
329
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100330 irq_set_handler_data(cascade_virq, uic);
331 irq_set_chained_handler(cascade_virq, uic_irq_cascade);
David Gibsone58923e2007-04-18 16:36:26 +1000332
333 /* FIXME: setup critical cascade?? */
334 }
David Gibsone58923e2007-04-18 16:36:26 +1000335 }
336}
337
338/* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
339unsigned int uic_get_irq(void)
340{
341 u32 msr;
342 int src;
343
344 BUG_ON(! primary_uic);
345
346 msr = mfdcr(primary_uic->dcrbase + UIC_MSR);
347 src = 32 - ffs(msr);
348
349 return irq_linear_revmap(primary_uic->irqhost, src);
350}