Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / arch / powerpc / platforms / wsp / opb_pic.c
1 /*
2  * IBM Onboard Peripheral Bus Interrupt Controller
3  *
4  * Copyright 2010 Jack Miller, IBM Corporation.
5  *
6  * This program is free software; you can redistribute  it and/or modify it
7  * under  the terms of  the GNU General  Public License as published by the
8  * Free Software Foundation;  either version 2 of the  License, or (at your
9  * option) any later version.
10  */
11
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/irq.h>
15 #include <linux/of.h>
16 #include <linux/slab.h>
17 #include <linux/time.h>
18 #include <linux/of_address.h>
19 #include <linux/of_irq.h>
20
21 #include <asm/reg_a2.h>
22 #include <asm/irq.h>
23
24 #define OPB_NR_IRQS 32
25
26 #define OPB_MLSASIER    0x04    /* MLS Accumulated Status IER */
27 #define OPB_MLSIR       0x50    /* MLS Interrupt Register */
28 #define OPB_MLSIER      0x54    /* MLS Interrupt Enable Register */
29 #define OPB_MLSIPR      0x58    /* MLS Interrupt Polarity Register */
30 #define OPB_MLSIIR      0x5c    /* MLS Interrupt Inputs Register */
31
32 static int opb_index = 0;
33
34 struct opb_pic {
35         struct irq_domain *host;
36         void *regs;
37         int index;
38         spinlock_t lock;
39 };
40
41 static u32 opb_in(struct opb_pic *opb, int offset)
42 {
43         return in_be32(opb->regs + offset);
44 }
45
46 static void opb_out(struct opb_pic *opb, int offset, u32 val)
47 {
48         out_be32(opb->regs + offset, val);
49 }
50
51 static void opb_unmask_irq(struct irq_data *d)
52 {
53         struct opb_pic *opb;
54         unsigned long flags;
55         u32 ier, bitset;
56
57         opb = d->chip_data;
58         bitset = (1 << (31 - irqd_to_hwirq(d)));
59
60         spin_lock_irqsave(&opb->lock, flags);
61
62         ier = opb_in(opb, OPB_MLSIER);
63         opb_out(opb, OPB_MLSIER, ier | bitset);
64         ier = opb_in(opb, OPB_MLSIER);
65
66         spin_unlock_irqrestore(&opb->lock, flags);
67 }
68
69 static void opb_mask_irq(struct irq_data *d)
70 {
71         struct opb_pic *opb;
72         unsigned long flags;
73         u32 ier, mask;
74
75         opb = d->chip_data;
76         mask = ~(1 << (31 - irqd_to_hwirq(d)));
77
78         spin_lock_irqsave(&opb->lock, flags);
79
80         ier = opb_in(opb, OPB_MLSIER);
81         opb_out(opb, OPB_MLSIER, ier & mask);
82         ier = opb_in(opb, OPB_MLSIER); // Flush posted writes
83
84         spin_unlock_irqrestore(&opb->lock, flags);
85 }
86
87 static void opb_ack_irq(struct irq_data *d)
88 {
89         struct opb_pic *opb;
90         unsigned long flags;
91         u32 bitset;
92
93         opb = d->chip_data;
94         bitset = (1 << (31 - irqd_to_hwirq(d)));
95
96         spin_lock_irqsave(&opb->lock, flags);
97
98         opb_out(opb, OPB_MLSIR, bitset);
99         opb_in(opb, OPB_MLSIR); // Flush posted writes
100
101         spin_unlock_irqrestore(&opb->lock, flags);
102 }
103
104 static void opb_mask_ack_irq(struct irq_data *d)
105 {
106         struct opb_pic *opb;
107         unsigned long flags;
108         u32 bitset;
109         u32 ier, ir;
110
111         opb = d->chip_data;
112         bitset = (1 << (31 - irqd_to_hwirq(d)));
113
114         spin_lock_irqsave(&opb->lock, flags);
115
116         ier = opb_in(opb, OPB_MLSIER);
117         opb_out(opb, OPB_MLSIER, ier & ~bitset);
118         ier = opb_in(opb, OPB_MLSIER); // Flush posted writes
119
120         opb_out(opb, OPB_MLSIR, bitset);
121         ir = opb_in(opb, OPB_MLSIR); // Flush posted writes
122
123         spin_unlock_irqrestore(&opb->lock, flags);
124 }
125
126 static int opb_set_irq_type(struct irq_data *d, unsigned int flow)
127 {
128         struct opb_pic *opb;
129         unsigned long flags;
130         int invert, ipr, mask, bit;
131
132         opb = d->chip_data;
133
134         /* The only information we're interested in in the type is whether it's
135          * a high or low trigger. For high triggered interrupts, the polarity
136          * set for it in the MLS Interrupt Polarity Register is 0, for low
137          * interrupts it's 1 so that the proper input in the MLS Interrupt Input
138          * Register is interrupted as asserting the interrupt. */
139
140         switch (flow) {
141                 case IRQ_TYPE_NONE:
142                         opb_mask_irq(d);
143                         return 0;
144
145                 case IRQ_TYPE_LEVEL_HIGH:
146                         invert = 0;
147                         break;
148
149                 case IRQ_TYPE_LEVEL_LOW:
150                         invert = 1;
151                         break;
152
153                 default:
154                         return -EINVAL;
155         }
156
157         bit = (1 << (31 - irqd_to_hwirq(d)));
158         mask = ~bit;
159
160         spin_lock_irqsave(&opb->lock, flags);
161
162         ipr = opb_in(opb, OPB_MLSIPR);
163         ipr = (ipr & mask) | (invert ? bit : 0);
164         opb_out(opb, OPB_MLSIPR, ipr);
165         ipr = opb_in(opb, OPB_MLSIPR);  // Flush posted writes
166
167         spin_unlock_irqrestore(&opb->lock, flags);
168
169         /* Record the type in the interrupt descriptor */
170         irqd_set_trigger_type(d, flow);
171
172         return 0;
173 }
174
175 static struct irq_chip opb_irq_chip = {
176         .name           = "OPB",
177         .irq_mask       = opb_mask_irq,
178         .irq_unmask     = opb_unmask_irq,
179         .irq_mask_ack   = opb_mask_ack_irq,
180         .irq_ack        = opb_ack_irq,
181         .irq_set_type   = opb_set_irq_type
182 };
183
184 static int opb_host_map(struct irq_domain *host, unsigned int virq,
185                 irq_hw_number_t hwirq)
186 {
187         struct opb_pic *opb;
188
189         opb = host->host_data;
190
191         /* Most of the important stuff is handled by the generic host code, like
192          * the lookup, so just attach some info to the virtual irq */
193
194         irq_set_chip_data(virq, opb);
195         irq_set_chip_and_handler(virq, &opb_irq_chip, handle_level_irq);
196         irq_set_irq_type(virq, IRQ_TYPE_NONE);
197
198         return 0;
199 }
200
201 static const struct irq_domain_ops opb_host_ops = {
202         .map = opb_host_map,
203         .xlate = irq_domain_xlate_twocell,
204 };
205
206 irqreturn_t opb_irq_handler(int irq, void *private)
207 {
208         struct opb_pic *opb;
209         u32 ir, src, subvirq;
210
211         opb = (struct opb_pic *) private;
212
213         /* Read the OPB MLS Interrupt Register for
214          * asserted interrupts */
215         ir = opb_in(opb, OPB_MLSIR);
216         if (!ir)
217                 return IRQ_NONE;
218
219         do {
220                 /* Get 1 - 32 source, *NOT* bit */
221                 src = 32 - ffs(ir);
222
223                 /* Translate from the OPB's conception of interrupt number to
224                  * Linux's virtual IRQ */
225
226                 subvirq = irq_linear_revmap(opb->host, src);
227
228                 generic_handle_irq(subvirq);
229         } while ((ir = opb_in(opb, OPB_MLSIR)));
230
231         return IRQ_HANDLED;
232 }
233
234 struct opb_pic *opb_pic_init_one(struct device_node *dn)
235 {
236         struct opb_pic *opb;
237         struct resource res;
238
239         if (of_address_to_resource(dn, 0, &res)) {
240                 printk(KERN_ERR "opb: Couldn't translate resource\n");
241                 return  NULL;
242         }
243
244         opb = kzalloc(sizeof(struct opb_pic), GFP_KERNEL);
245         if (!opb) {
246                 printk(KERN_ERR "opb: Failed to allocate opb struct!\n");
247                 return NULL;
248         }
249
250         /* Get access to the OPB MMIO registers */
251         opb->regs = ioremap(res.start + 0x10000, 0x1000);
252         if (!opb->regs) {
253                 printk(KERN_ERR "opb: Failed to allocate register space!\n");
254                 goto free_opb;
255         }
256
257         /* Allocate an irq domain so that Linux knows that despite only
258          * having one interrupt to issue, we're the controller for multiple
259          * hardware IRQs, so later we can lookup their virtual IRQs. */
260
261         opb->host = irq_domain_add_linear(dn, OPB_NR_IRQS, &opb_host_ops, opb);
262         if (!opb->host) {
263                 printk(KERN_ERR "opb: Failed to allocate IRQ host!\n");
264                 goto free_regs;
265         }
266
267         opb->index = opb_index++;
268         spin_lock_init(&opb->lock);
269
270         /* Disable all interrupts by default */
271         opb_out(opb, OPB_MLSASIER, 0);
272         opb_out(opb, OPB_MLSIER, 0);
273
274         /* ACK any interrupts left by FW */
275         opb_out(opb, OPB_MLSIR, 0xFFFFFFFF);
276
277         return opb;
278
279 free_regs:
280         iounmap(opb->regs);
281 free_opb:
282         kfree(opb);
283         return NULL;
284 }
285
286 void __init opb_pic_init(void)
287 {
288         struct device_node *dn;
289         struct opb_pic *opb;
290         int virq;
291         int rc;
292
293         /* Call init_one for each OPB device */
294         for_each_compatible_node(dn, NULL, "ibm,opb") {
295
296                 /* Fill in an OPB struct */
297                 opb = opb_pic_init_one(dn);
298                 if (!opb) {
299                         printk(KERN_WARNING "opb: Failed to init node, skipped!\n");
300                         continue;
301                 }
302
303                 /* Map / get opb's hardware virtual irq */
304                 virq = irq_of_parse_and_map(dn, 0);
305                 if (virq <= 0) {
306                         printk("opb: irq_op_parse_and_map failed!\n");
307                         continue;
308                 }
309
310                 /* Attach opb interrupt handler to new virtual IRQ */
311                 rc = request_irq(virq, opb_irq_handler, IRQF_NO_THREAD,
312                                  "OPB LS Cascade", opb);
313                 if (rc) {
314                         printk("opb: request_irq failed: %d\n", rc);
315                         continue;
316                 }
317
318                 printk("OPB%d init with %d IRQs at %p\n", opb->index,
319                                 OPB_NR_IRQS, opb->regs);
320         }
321 }