blob: a00949f3e3788ca025bb58493e927ad49c9c19e0 [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>
David Gibsone58923e2007-04-18 16:36:26 +100021#include <linux/device.h>
David Gibsone58923e2007-04-18 16:36:26 +100022#include <linux/spinlock.h>
23#include <linux/irq.h>
24#include <linux/interrupt.h>
David Gibson868afce2007-08-14 13:52:42 +100025#include <linux/kernel_stat.h>
David Gibsone58923e2007-04-18 16:36:26 +100026#include <asm/irq.h>
27#include <asm/io.h>
28#include <asm/prom.h>
29#include <asm/dcr.h>
30
31#define NR_UIC_INTS 32
32
33#define UIC_SR 0x0
34#define UIC_ER 0x2
35#define UIC_CR 0x3
36#define UIC_PR 0x4
37#define UIC_TR 0x5
38#define UIC_MSR 0x6
39#define UIC_VR 0x7
40#define UIC_VCR 0x8
41
David Gibsone58923e2007-04-18 16:36:26 +100042struct uic *primary_uic;
43
44struct uic {
45 int index;
46 int dcrbase;
47
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +020048 raw_spinlock_t lock;
David Gibsone58923e2007-04-18 16:36:26 +100049
50 /* The remapper for this UIC */
Grant Likelybae1d8f2012-02-14 14:06:50 -070051 struct irq_domain *irqhost;
David Gibsone58923e2007-04-18 16:36:26 +100052};
53
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000054static void uic_unmask_irq(struct irq_data *d)
David Gibsone58923e2007-04-18 16:36:26 +100055{
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000056 struct uic *uic = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +100057 unsigned int src = irqd_to_hwirq(d);
David Gibsone58923e2007-04-18 16:36:26 +100058 unsigned long flags;
Valentine Barshakc8090562007-11-15 01:00:52 +110059 u32 er, sr;
David Gibsone58923e2007-04-18 16:36:26 +100060
Valentine Barshakc8090562007-11-15 01:00:52 +110061 sr = 1 << (31-src);
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +020062 raw_spin_lock_irqsave(&uic->lock, flags);
Valentine Barshakc8090562007-11-15 01:00:52 +110063 /* ack level-triggered interrupts here */
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +010064 if (irqd_is_level_type(d))
Valentine Barshakc8090562007-11-15 01:00:52 +110065 mtdcr(uic->dcrbase + UIC_SR, sr);
David Gibsone58923e2007-04-18 16:36:26 +100066 er = mfdcr(uic->dcrbase + UIC_ER);
Valentine Barshakc8090562007-11-15 01:00:52 +110067 er |= sr;
David Gibsone58923e2007-04-18 16:36:26 +100068 mtdcr(uic->dcrbase + UIC_ER, er);
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +020069 raw_spin_unlock_irqrestore(&uic->lock, flags);
David Gibsone58923e2007-04-18 16:36:26 +100070}
71
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000072static void uic_mask_irq(struct irq_data *d)
David Gibsone58923e2007-04-18 16:36:26 +100073{
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000074 struct uic *uic = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +100075 unsigned int src = irqd_to_hwirq(d);
David Gibsone58923e2007-04-18 16:36:26 +100076 unsigned long flags;
77 u32 er;
78
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +020079 raw_spin_lock_irqsave(&uic->lock, flags);
David Gibsone58923e2007-04-18 16:36:26 +100080 er = mfdcr(uic->dcrbase + UIC_ER);
81 er &= ~(1 << (31 - src));
82 mtdcr(uic->dcrbase + UIC_ER, er);
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +020083 raw_spin_unlock_irqrestore(&uic->lock, flags);
David Gibsone58923e2007-04-18 16:36:26 +100084}
85
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000086static void uic_ack_irq(struct irq_data *d)
David Gibsone58923e2007-04-18 16:36:26 +100087{
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000088 struct uic *uic = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +100089 unsigned int src = irqd_to_hwirq(d);
David Gibsone58923e2007-04-18 16:36:26 +100090 unsigned long flags;
91
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +020092 raw_spin_lock_irqsave(&uic->lock, flags);
David Gibsone58923e2007-04-18 16:36:26 +100093 mtdcr(uic->dcrbase + UIC_SR, 1 << (31-src));
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +020094 raw_spin_unlock_irqrestore(&uic->lock, flags);
David Gibsone58923e2007-04-18 16:36:26 +100095}
96
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000097static void uic_mask_ack_irq(struct irq_data *d)
Valentine Barshakb8b799a2007-11-14 07:25:21 +110098{
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +000099 struct uic *uic = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +1000100 unsigned int src = irqd_to_hwirq(d);
Valentine Barshakb8b799a2007-11-14 07:25:21 +1100101 unsigned long flags;
102 u32 er, sr;
103
104 sr = 1 << (31-src);
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +0200105 raw_spin_lock_irqsave(&uic->lock, flags);
Valentine Barshakb8b799a2007-11-14 07:25:21 +1100106 er = mfdcr(uic->dcrbase + UIC_ER);
107 er &= ~sr;
108 mtdcr(uic->dcrbase + UIC_ER, er);
Valentine Barshakc8090562007-11-15 01:00:52 +1100109 /* On the UIC, acking (i.e. clearing the SR bit)
110 * a level irq will have no effect if the interrupt
111 * is still asserted by the device, even if
112 * the interrupt is already masked. Therefore
113 * we only ack the egde interrupts here, while
114 * level interrupts are ack'ed after the actual
115 * isr call in the uic_unmask_irq()
116 */
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +0100117 if (!irqd_is_level_type(d))
Valentine Barshakc8090562007-11-15 01:00:52 +1100118 mtdcr(uic->dcrbase + UIC_SR, sr);
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +0200119 raw_spin_unlock_irqrestore(&uic->lock, flags);
Valentine Barshakb8b799a2007-11-14 07:25:21 +1100120}
121
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +0000122static int uic_set_irq_type(struct irq_data *d, unsigned int flow_type)
David Gibsone58923e2007-04-18 16:36:26 +1000123{
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +0000124 struct uic *uic = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +1000125 unsigned int src = irqd_to_hwirq(d);
David Gibsone58923e2007-04-18 16:36:26 +1000126 unsigned long flags;
127 int trigger, polarity;
128 u32 tr, pr, mask;
129
130 switch (flow_type & IRQ_TYPE_SENSE_MASK) {
131 case IRQ_TYPE_NONE:
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +0000132 uic_mask_irq(d);
David Gibsone58923e2007-04-18 16:36:26 +1000133 return 0;
134
135 case IRQ_TYPE_EDGE_RISING:
136 trigger = 1; polarity = 1;
137 break;
138 case IRQ_TYPE_EDGE_FALLING:
139 trigger = 1; polarity = 0;
140 break;
141 case IRQ_TYPE_LEVEL_HIGH:
142 trigger = 0; polarity = 1;
143 break;
144 case IRQ_TYPE_LEVEL_LOW:
145 trigger = 0; polarity = 0;
146 break;
147 default:
148 return -EINVAL;
149 }
150
151 mask = ~(1 << (31 - src));
152
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +0200153 raw_spin_lock_irqsave(&uic->lock, flags);
David Gibsone58923e2007-04-18 16:36:26 +1000154 tr = mfdcr(uic->dcrbase + UIC_TR);
155 pr = mfdcr(uic->dcrbase + UIC_PR);
156 tr = (tr & mask) | (trigger << (31-src));
157 pr = (pr & mask) | (polarity << (31-src));
158
159 mtdcr(uic->dcrbase + UIC_PR, pr);
160 mtdcr(uic->dcrbase + UIC_TR, tr);
161
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +0200162 raw_spin_unlock_irqrestore(&uic->lock, flags);
David Gibsone58923e2007-04-18 16:36:26 +1000163
164 return 0;
165}
166
167static struct irq_chip uic_irq_chip = {
Anton Blanchardfc380c02010-01-31 20:33:41 +0000168 .name = "UIC",
Lennert Buytenhek42a07ae2011-03-08 22:27:02 +0000169 .irq_unmask = uic_unmask_irq,
170 .irq_mask = uic_mask_irq,
171 .irq_mask_ack = uic_mask_ack_irq,
172 .irq_ack = uic_ack_irq,
173 .irq_set_type = uic_set_irq_type,
David Gibsone58923e2007-04-18 16:36:26 +1000174};
175
Grant Likelybae1d8f2012-02-14 14:06:50 -0700176static int uic_host_map(struct irq_domain *h, unsigned int virq,
David Gibsone58923e2007-04-18 16:36:26 +1000177 irq_hw_number_t hw)
178{
179 struct uic *uic = h->host_data;
180
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100181 irq_set_chip_data(virq, uic);
David Gibsone58923e2007-04-18 16:36:26 +1000182 /* Despite the name, handle_level_irq() works for both level
183 * and edge irqs on UIC. FIXME: check this is correct */
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100184 irq_set_chip_and_handler(virq, &uic_irq_chip, handle_level_irq);
David Gibsone58923e2007-04-18 16:36:26 +1000185
186 /* Set default irq type */
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100187 irq_set_irq_type(virq, IRQ_TYPE_NONE);
David Gibsone58923e2007-04-18 16:36:26 +1000188
189 return 0;
190}
191
Krzysztof Kozlowski202648a2015-04-27 21:48:47 +0900192static const struct irq_domain_ops uic_host_ops = {
David Gibsone58923e2007-04-18 16:36:26 +1000193 .map = uic_host_map,
Grant Likelyff8c3ab2012-01-24 17:09:13 -0700194 .xlate = irq_domain_xlate_twocell,
David Gibsone58923e2007-04-18 16:36:26 +1000195};
196
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200197static void uic_irq_cascade(struct irq_desc *desc)
David Gibsone58923e2007-04-18 16:36:26 +1000198{
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100199 struct irq_chip *chip = irq_desc_get_chip(desc);
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +0100200 struct irq_data *idata = irq_desc_get_irq_data(desc);
Jiang Liuc1231a72015-05-20 17:59:52 +0800201 struct uic *uic = irq_desc_get_handler_data(desc);
David Gibsone58923e2007-04-18 16:36:26 +1000202 u32 msr;
203 int src;
204 int subvirq;
205
Thomas Gleixner239007b2009-11-17 16:46:45 +0100206 raw_spin_lock(&desc->lock);
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +0100207 if (irqd_is_level_type(idata))
208 chip->irq_mask(idata);
Valentine Barshak5aac48d2007-12-07 00:48:26 +1100209 else
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +0100210 chip->irq_mask_ack(idata);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100211 raw_spin_unlock(&desc->lock);
Valentine Barshak5aac48d2007-12-07 00:48:26 +1100212
David Gibsone58923e2007-04-18 16:36:26 +1000213 msr = mfdcr(uic->dcrbase + UIC_MSR);
David Gibson553fdff2007-08-14 13:52:42 +1000214 if (!msr) /* spurious interrupt */
Valentine Barshak5aac48d2007-12-07 00:48:26 +1100215 goto uic_irq_ret;
David Gibson553fdff2007-08-14 13:52:42 +1000216
David Gibsone58923e2007-04-18 16:36:26 +1000217 src = 32 - ffs(msr);
218
219 subvirq = irq_linear_revmap(uic->irqhost, src);
220 generic_handle_irq(subvirq);
221
Valentine Barshak5aac48d2007-12-07 00:48:26 +1100222uic_irq_ret:
Thomas Gleixner239007b2009-11-17 16:46:45 +0100223 raw_spin_lock(&desc->lock);
Thomas Gleixner1ac06cd2011-03-25 16:23:57 +0100224 if (irqd_is_level_type(idata))
225 chip->irq_ack(idata);
226 if (!irqd_irq_disabled(idata) && chip->irq_unmask)
227 chip->irq_unmask(idata);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100228 raw_spin_unlock(&desc->lock);
David Gibsone58923e2007-04-18 16:36:26 +1000229}
230
231static struct uic * __init uic_init_one(struct device_node *node)
232{
233 struct uic *uic;
234 const u32 *indexp, *dcrreg;
235 int len;
236
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000237 BUG_ON(! of_device_is_compatible(node, "ibm,uic"));
David Gibsone58923e2007-04-18 16:36:26 +1000238
Anton Vorontsovea960252009-07-01 10:59:57 +0000239 uic = kzalloc(sizeof(*uic), GFP_KERNEL);
David Gibsone58923e2007-04-18 16:36:26 +1000240 if (! uic)
241 return NULL; /* FIXME: panic? */
242
Thomas Gleixnerbccc2f72010-04-06 09:44:10 +0200243 raw_spin_lock_init(&uic->lock);
Stephen Rothwell12d371a2007-04-29 16:29:08 +1000244 indexp = of_get_property(node, "cell-index", &len);
David Gibsone58923e2007-04-18 16:36:26 +1000245 if (!indexp || (len != sizeof(u32))) {
246 printk(KERN_ERR "uic: Device node %s has missing or invalid "
247 "cell-index property\n", node->full_name);
248 return NULL;
249 }
250 uic->index = *indexp;
251
Stephen Rothwell12d371a2007-04-29 16:29:08 +1000252 dcrreg = of_get_property(node, "dcr-reg", &len);
David Gibsone58923e2007-04-18 16:36:26 +1000253 if (!dcrreg || (len != 2*sizeof(u32))) {
254 printk(KERN_ERR "uic: Device node %s has missing or invalid "
255 "dcr-reg property\n", node->full_name);
256 return NULL;
257 }
258 uic->dcrbase = *dcrreg;
259
Grant Likelya8db8cf2012-02-14 14:06:54 -0700260 uic->irqhost = irq_domain_add_linear(node, NR_UIC_INTS, &uic_host_ops,
261 uic);
Michael Ellerman19fc65b2008-05-26 12:12:32 +1000262 if (! uic->irqhost)
David Gibsone58923e2007-04-18 16:36:26 +1000263 return NULL; /* FIXME: panic? */
David Gibsone58923e2007-04-18 16:36:26 +1000264
David Gibsone58923e2007-04-18 16:36:26 +1000265 /* Start with all interrupts disabled, level and non-critical */
266 mtdcr(uic->dcrbase + UIC_ER, 0);
267 mtdcr(uic->dcrbase + UIC_CR, 0);
268 mtdcr(uic->dcrbase + UIC_TR, 0);
269 /* Clear any pending interrupts, in case the firmware left some */
270 mtdcr(uic->dcrbase + UIC_SR, 0xffffffff);
271
272 printk ("UIC%d (%d IRQ sources) at DCR 0x%x\n", uic->index,
273 NR_UIC_INTS, uic->dcrbase);
274
275 return uic;
276}
277
278void __init uic_init_tree(void)
279{
280 struct device_node *np;
281 struct uic *uic;
282 const u32 *interrupts;
283
284 /* First locate and initialize the top-level UIC */
Cyrill Gorcunov26cb7d82007-11-30 06:44:36 +1100285 for_each_compatible_node(np, NULL, "ibm,uic") {
Stephen Rothwell12d371a2007-04-29 16:29:08 +1000286 interrupts = of_get_property(np, "interrupts", NULL);
Cyrill Gorcunov26cb7d82007-11-30 06:44:36 +1100287 if (!interrupts)
David Gibsone58923e2007-04-18 16:36:26 +1000288 break;
David Gibsone58923e2007-04-18 16:36:26 +1000289 }
290
291 BUG_ON(!np); /* uic_init_tree() assumes there's a UIC as the
292 * top-level interrupt controller */
293 primary_uic = uic_init_one(np);
Cyrill Gorcunov26cb7d82007-11-30 06:44:36 +1100294 if (!primary_uic)
David Gibsone58923e2007-04-18 16:36:26 +1000295 panic("Unable to initialize primary UIC %s\n", np->full_name);
296
297 irq_set_default_host(primary_uic->irqhost);
298 of_node_put(np);
299
300 /* The scan again for cascaded UICs */
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);
David Gibsone58923e2007-04-18 16:36:26 +1000303 if (interrupts) {
304 /* Secondary UIC */
305 int cascade_virq;
David Gibsone58923e2007-04-18 16:36:26 +1000306
307 uic = uic_init_one(np);
308 if (! uic)
309 panic("Unable to initialize a secondary UIC %s\n",
310 np->full_name);
311
312 cascade_virq = irq_of_parse_and_map(np, 0);
313
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100314 irq_set_handler_data(cascade_virq, uic);
315 irq_set_chained_handler(cascade_virq, uic_irq_cascade);
David Gibsone58923e2007-04-18 16:36:26 +1000316
317 /* FIXME: setup critical cascade?? */
318 }
David Gibsone58923e2007-04-18 16:36:26 +1000319 }
320}
321
Michael Ellermanef24ba72016-09-06 21:53:24 +1000322/* Return an interrupt vector or 0 if no interrupt is pending. */
David Gibsone58923e2007-04-18 16:36:26 +1000323unsigned int uic_get_irq(void)
324{
325 u32 msr;
326 int src;
327
328 BUG_ON(! primary_uic);
329
330 msr = mfdcr(primary_uic->dcrbase + UIC_MSR);
331 src = 32 - ffs(msr);
332
333 return irq_linear_revmap(primary_uic->irqhost, src);
334}