]> Pileus Git - ~andy/linux/blob - arch/powerpc/sysdev/uic.c
[POWERPC] 4xx: make UIC use generic level irq handler
[~andy/linux] / arch / powerpc / sysdev / uic.c
1 /*
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>
27 #include <linux/kernel_stat.h>
28 #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
44 #define uic_irq_to_hw(virq)     (irq_map[virq].hwirq)
45
46 struct uic *primary_uic;
47
48 struct uic {
49         int index;
50         int dcrbase;
51
52         spinlock_t lock;
53
54         /* The remapper for this UIC */
55         struct irq_host *irqhost;
56
57         /* For secondary UICs, the cascade interrupt's irqaction */
58         struct irqaction cascade;
59 };
60
61 static void uic_unmask_irq(unsigned int virq)
62 {
63         struct irq_desc *desc = get_irq_desc(virq);
64         struct uic *uic = get_irq_chip_data(virq);
65         unsigned int src = uic_irq_to_hw(virq);
66         unsigned long flags;
67         u32 er, sr;
68
69         sr = 1 << (31-src);
70         spin_lock_irqsave(&uic->lock, flags);
71         /* ack level-triggered interrupts here */
72         if (desc->status & IRQ_LEVEL)
73                 mtdcr(uic->dcrbase + UIC_SR, sr);
74         er = mfdcr(uic->dcrbase + UIC_ER);
75         er |= sr;
76         mtdcr(uic->dcrbase + UIC_ER, er);
77         spin_unlock_irqrestore(&uic->lock, flags);
78 }
79
80 static void uic_mask_irq(unsigned int virq)
81 {
82         struct uic *uic = get_irq_chip_data(virq);
83         unsigned int src = uic_irq_to_hw(virq);
84         unsigned long flags;
85         u32 er;
86
87         spin_lock_irqsave(&uic->lock, flags);
88         er = mfdcr(uic->dcrbase + UIC_ER);
89         er &= ~(1 << (31 - src));
90         mtdcr(uic->dcrbase + UIC_ER, er);
91         spin_unlock_irqrestore(&uic->lock, flags);
92 }
93
94 static void uic_ack_irq(unsigned int virq)
95 {
96         struct uic *uic = get_irq_chip_data(virq);
97         unsigned int src = uic_irq_to_hw(virq);
98         unsigned long flags;
99
100         spin_lock_irqsave(&uic->lock, flags);
101         mtdcr(uic->dcrbase + UIC_SR, 1 << (31-src));
102         spin_unlock_irqrestore(&uic->lock, flags);
103 }
104
105 static void uic_mask_ack_irq(unsigned int virq)
106 {
107         struct irq_desc *desc = get_irq_desc(virq);
108         struct uic *uic = get_irq_chip_data(virq);
109         unsigned int src = uic_irq_to_hw(virq);
110         unsigned long flags;
111         u32 er, sr;
112
113         sr = 1 << (31-src);
114         spin_lock_irqsave(&uic->lock, flags);
115         er = mfdcr(uic->dcrbase + UIC_ER);
116         er &= ~sr;
117         mtdcr(uic->dcrbase + UIC_ER, er);
118         /* On the UIC, acking (i.e. clearing the SR bit)
119          * a level irq will have no effect if the interrupt
120          * is still asserted by the device, even if
121          * the interrupt is already masked. Therefore
122          * we only ack the egde interrupts here, while
123          * level interrupts are ack'ed after the actual
124          * isr call in the uic_unmask_irq()
125          */
126         if (!(desc->status & IRQ_LEVEL))
127                 mtdcr(uic->dcrbase + UIC_SR, sr);
128         spin_unlock_irqrestore(&uic->lock, flags);
129 }
130
131 static int uic_set_irq_type(unsigned int virq, unsigned int flow_type)
132 {
133         struct uic *uic = get_irq_chip_data(virq);
134         unsigned int src = uic_irq_to_hw(virq);
135         struct irq_desc *desc = get_irq_desc(virq);
136         unsigned long flags;
137         int trigger, polarity;
138         u32 tr, pr, mask;
139
140         switch (flow_type & IRQ_TYPE_SENSE_MASK) {
141         case IRQ_TYPE_NONE:
142                 uic_mask_irq(virq);
143                 return 0;
144
145         case IRQ_TYPE_EDGE_RISING:
146                 trigger = 1; polarity = 1;
147                 break;
148         case IRQ_TYPE_EDGE_FALLING:
149                 trigger = 1; polarity = 0;
150                 break;
151         case IRQ_TYPE_LEVEL_HIGH:
152                 trigger = 0; polarity = 1;
153                 break;
154         case IRQ_TYPE_LEVEL_LOW:
155                 trigger = 0; polarity = 0;
156                 break;
157         default:
158                 return -EINVAL;
159         }
160
161         mask = ~(1 << (31 - src));
162
163         spin_lock_irqsave(&uic->lock, flags);
164         tr = mfdcr(uic->dcrbase + UIC_TR);
165         pr = mfdcr(uic->dcrbase + UIC_PR);
166         tr = (tr & mask) | (trigger << (31-src));
167         pr = (pr & mask) | (polarity << (31-src));
168
169         mtdcr(uic->dcrbase + UIC_PR, pr);
170         mtdcr(uic->dcrbase + UIC_TR, tr);
171
172         desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
173         desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
174         if (!trigger)
175                 desc->status |= IRQ_LEVEL;
176
177         spin_unlock_irqrestore(&uic->lock, flags);
178
179         return 0;
180 }
181
182 static struct irq_chip uic_irq_chip = {
183         .typename       = " UIC  ",
184         .unmask         = uic_unmask_irq,
185         .mask           = uic_mask_irq,
186         .mask_ack       = uic_mask_ack_irq,
187         .ack            = uic_ack_irq,
188         .set_type       = uic_set_irq_type,
189 };
190
191 static int uic_host_map(struct irq_host *h, unsigned int virq,
192                         irq_hw_number_t hw)
193 {
194         struct uic *uic = h->host_data;
195
196         set_irq_chip_data(virq, uic);
197         /* Despite the name, handle_level_irq() works for both level
198          * and edge irqs on UIC.  FIXME: check this is correct */
199         set_irq_chip_and_handler(virq, &uic_irq_chip, handle_level_irq);
200
201         /* Set default irq type */
202         set_irq_type(virq, IRQ_TYPE_NONE);
203
204         return 0;
205 }
206
207 static int uic_host_xlate(struct irq_host *h, struct device_node *ct,
208                           u32 *intspec, unsigned int intsize,
209                           irq_hw_number_t *out_hwirq, unsigned int *out_type)
210
211 {
212         /* UIC intspecs must have 2 cells */
213         BUG_ON(intsize != 2);
214         *out_hwirq = intspec[0];
215         *out_type = intspec[1];
216         return 0;
217 }
218
219 static struct irq_host_ops uic_host_ops = {
220         .map    = uic_host_map,
221         .xlate  = uic_host_xlate,
222 };
223
224 irqreturn_t uic_cascade(int virq, void *data)
225 {
226         struct uic *uic = data;
227         u32 msr;
228         int src;
229         int subvirq;
230
231         msr = mfdcr(uic->dcrbase + UIC_MSR);
232         if (!msr) /* spurious interrupt */
233                 return IRQ_HANDLED;
234
235         src = 32 - ffs(msr);
236
237         subvirq = irq_linear_revmap(uic->irqhost, src);
238         generic_handle_irq(subvirq);
239
240         return IRQ_HANDLED;
241 }
242
243 static struct uic * __init uic_init_one(struct device_node *node)
244 {
245         struct uic *uic;
246         const u32 *indexp, *dcrreg;
247         int len;
248
249         BUG_ON(! of_device_is_compatible(node, "ibm,uic"));
250
251         uic = alloc_bootmem(sizeof(*uic));
252         if (! uic)
253                 return NULL; /* FIXME: panic? */
254
255         memset(uic, 0, sizeof(*uic));
256         spin_lock_init(&uic->lock);
257         indexp = of_get_property(node, "cell-index", &len);
258         if (!indexp || (len != sizeof(u32))) {
259                 printk(KERN_ERR "uic: Device node %s has missing or invalid "
260                        "cell-index property\n", node->full_name);
261                 return NULL;
262         }
263         uic->index = *indexp;
264
265         dcrreg = of_get_property(node, "dcr-reg", &len);
266         if (!dcrreg || (len != 2*sizeof(u32))) {
267                 printk(KERN_ERR "uic: Device node %s has missing or invalid "
268                        "dcr-reg property\n", node->full_name);
269                 return NULL;
270         }
271         uic->dcrbase = *dcrreg;
272
273         uic->irqhost = irq_alloc_host(of_node_get(node), IRQ_HOST_MAP_LINEAR,
274                                       NR_UIC_INTS, &uic_host_ops, -1);
275         if (! uic->irqhost) {
276                 of_node_put(node);
277                 return NULL; /* FIXME: panic? */
278         }
279
280         uic->irqhost->host_data = uic;
281
282         /* Start with all interrupts disabled, level and non-critical */
283         mtdcr(uic->dcrbase + UIC_ER, 0);
284         mtdcr(uic->dcrbase + UIC_CR, 0);
285         mtdcr(uic->dcrbase + UIC_TR, 0);
286         /* Clear any pending interrupts, in case the firmware left some */
287         mtdcr(uic->dcrbase + UIC_SR, 0xffffffff);
288
289         printk ("UIC%d (%d IRQ sources) at DCR 0x%x\n", uic->index,
290                 NR_UIC_INTS, uic->dcrbase);
291
292         return uic;
293 }
294
295 void __init uic_init_tree(void)
296 {
297         struct device_node *np;
298         struct uic *uic;
299         const u32 *interrupts;
300
301         /* First locate and initialize the top-level UIC */
302
303         np = of_find_compatible_node(NULL, NULL, "ibm,uic");
304         while (np) {
305                 interrupts = of_get_property(np, "interrupts", NULL);
306                 if (! interrupts)
307                         break;
308
309                 np = of_find_compatible_node(np, NULL, "ibm,uic");
310         }
311
312         BUG_ON(!np); /* uic_init_tree() assumes there's a UIC as the
313                       * top-level interrupt controller */
314         primary_uic = uic_init_one(np);
315         if (! primary_uic)
316                 panic("Unable to initialize primary UIC %s\n", np->full_name);
317
318         irq_set_default_host(primary_uic->irqhost);
319         of_node_put(np);
320
321         /* The scan again for cascaded UICs */
322         np = of_find_compatible_node(NULL, NULL, "ibm,uic");
323         while (np) {
324                 interrupts = of_get_property(np, "interrupts", NULL);
325                 if (interrupts) {
326                         /* Secondary UIC */
327                         int cascade_virq;
328                         int ret;
329
330                         uic = uic_init_one(np);
331                         if (! uic)
332                                 panic("Unable to initialize a secondary UIC %s\n",
333                                       np->full_name);
334
335                         cascade_virq = irq_of_parse_and_map(np, 0);
336
337                         uic->cascade.handler = uic_cascade;
338                         uic->cascade.name = "UIC cascade";
339                         uic->cascade.dev_id = uic;
340
341                         ret = setup_irq(cascade_virq, &uic->cascade);
342                         if (ret)
343                                 printk(KERN_ERR "Failed to setup_irq(%d) for "
344                                        "UIC%d cascade\n", cascade_virq,
345                                        uic->index);
346
347                         /* FIXME: setup critical cascade?? */
348                 }
349
350                 np = of_find_compatible_node(np, NULL, "ibm,uic");
351         }
352 }
353
354 /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
355 unsigned int uic_get_irq(void)
356 {
357         u32 msr;
358         int src;
359
360         BUG_ON(! primary_uic);
361
362         msr = mfdcr(primary_uic->dcrbase + UIC_MSR);
363         src = 32 - ffs(msr);
364
365         return irq_linear_revmap(primary_uic->irqhost, src);
366 }