irqchip: Prepare for local stub header removal
[linux-block.git] / drivers / irqchip / irq-crossbar.c
1 /*
2  *  drivers/irqchip/irq-crossbar.c
3  *
4  *  Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5  *  Author: Sricharan R <r.sricharan@ti.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  */
12 #include <linux/err.h>
13 #include <linux/io.h>
14 #include <linux/irqchip.h>
15 #include <linux/irqdomain.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/slab.h>
19
20 #define IRQ_FREE        -1
21 #define IRQ_RESERVED    -2
22 #define IRQ_SKIP        -3
23 #define GIC_IRQ_START   32
24
25 /**
26  * struct crossbar_device - crossbar device description
27  * @lock: spinlock serializing access to @irq_map
28  * @int_max: maximum number of supported interrupts
29  * @safe_map: safe default value to initialize the crossbar
30  * @max_crossbar_sources: Maximum number of crossbar sources
31  * @irq_map: array of interrupts to crossbar number mapping
32  * @crossbar_base: crossbar base address
33  * @register_offsets: offsets for each irq number
34  * @write: register write function pointer
35  */
36 struct crossbar_device {
37         raw_spinlock_t lock;
38         uint int_max;
39         uint safe_map;
40         uint max_crossbar_sources;
41         uint *irq_map;
42         void __iomem *crossbar_base;
43         int *register_offsets;
44         void (*write)(int, int);
45 };
46
47 static struct crossbar_device *cb;
48
49 static void crossbar_writel(int irq_no, int cb_no)
50 {
51         writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
52 }
53
54 static void crossbar_writew(int irq_no, int cb_no)
55 {
56         writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
57 }
58
59 static void crossbar_writeb(int irq_no, int cb_no)
60 {
61         writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
62 }
63
64 static struct irq_chip crossbar_chip = {
65         .name                   = "CBAR",
66         .irq_eoi                = irq_chip_eoi_parent,
67         .irq_mask               = irq_chip_mask_parent,
68         .irq_unmask             = irq_chip_unmask_parent,
69         .irq_retrigger          = irq_chip_retrigger_hierarchy,
70         .irq_set_wake           = irq_chip_set_wake_parent,
71 #ifdef CONFIG_SMP
72         .irq_set_affinity       = irq_chip_set_affinity_parent,
73 #endif
74 };
75
76 static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
77                             irq_hw_number_t hwirq)
78 {
79         struct of_phandle_args args;
80         int i;
81         int err;
82
83         raw_spin_lock(&cb->lock);
84         for (i = cb->int_max - 1; i >= 0; i--) {
85                 if (cb->irq_map[i] == IRQ_FREE) {
86                         cb->irq_map[i] = hwirq;
87                         break;
88                 }
89         }
90         raw_spin_unlock(&cb->lock);
91
92         if (i < 0)
93                 return -ENODEV;
94
95         args.np = domain->parent->of_node;
96         args.args_count = 3;
97         args.args[0] = 0;       /* SPI */
98         args.args[1] = i;
99         args.args[2] = IRQ_TYPE_LEVEL_HIGH;
100
101         err = irq_domain_alloc_irqs_parent(domain, virq, 1, &args);
102         if (err)
103                 cb->irq_map[i] = IRQ_FREE;
104         else
105                 cb->write(i, hwirq);
106
107         return err;
108 }
109
110 static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
111                                  unsigned int nr_irqs, void *data)
112 {
113         struct of_phandle_args *args = data;
114         irq_hw_number_t hwirq;
115         int i;
116
117         if (args->args_count != 3)
118                 return -EINVAL; /* Not GIC compliant */
119         if (args->args[0] != 0)
120                 return -EINVAL; /* No PPI should point to this domain */
121
122         hwirq = args->args[1];
123         if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
124                 return -EINVAL; /* Can't deal with this */
125
126         for (i = 0; i < nr_irqs; i++) {
127                 int err = allocate_gic_irq(d, virq + i, hwirq + i);
128
129                 if (err)
130                         return err;
131
132                 irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
133                                               &crossbar_chip, NULL);
134         }
135
136         return 0;
137 }
138
139 /**
140  * crossbar_domain_free - unmap/free a crossbar<->irq connection
141  * @domain: domain of irq to unmap
142  * @virq: virq number
143  * @nr_irqs: number of irqs to free
144  *
145  * We do not maintain a use count of total number of map/unmap
146  * calls for a particular irq to find out if a irq can be really
147  * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
148  * after which irq is anyways unusable. So an explicit map has to be called
149  * after that.
150  */
151 static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
152                                  unsigned int nr_irqs)
153 {
154         int i;
155
156         raw_spin_lock(&cb->lock);
157         for (i = 0; i < nr_irqs; i++) {
158                 struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
159
160                 irq_domain_reset_irq_data(d);
161                 cb->irq_map[d->hwirq] = IRQ_FREE;
162                 cb->write(d->hwirq, cb->safe_map);
163         }
164         raw_spin_unlock(&cb->lock);
165 }
166
167 static int crossbar_domain_xlate(struct irq_domain *d,
168                                  struct device_node *controller,
169                                  const u32 *intspec, unsigned int intsize,
170                                  unsigned long *out_hwirq,
171                                  unsigned int *out_type)
172 {
173         if (d->of_node != controller)
174                 return -EINVAL; /* Shouldn't happen, really... */
175         if (intsize != 3)
176                 return -EINVAL; /* Not GIC compliant */
177         if (intspec[0] != 0)
178                 return -EINVAL; /* No PPI should point to this domain */
179
180         *out_hwirq = intspec[1];
181         *out_type = intspec[2];
182         return 0;
183 }
184
185 static const struct irq_domain_ops crossbar_domain_ops = {
186         .alloc  = crossbar_domain_alloc,
187         .free   = crossbar_domain_free,
188         .xlate  = crossbar_domain_xlate,
189 };
190
191 static int __init crossbar_of_init(struct device_node *node)
192 {
193         int i, size, max = 0, reserved = 0, entry;
194         const __be32 *irqsr;
195         int ret = -ENOMEM;
196
197         cb = kzalloc(sizeof(*cb), GFP_KERNEL);
198
199         if (!cb)
200                 return ret;
201
202         cb->crossbar_base = of_iomap(node, 0);
203         if (!cb->crossbar_base)
204                 goto err_cb;
205
206         of_property_read_u32(node, "ti,max-crossbar-sources",
207                              &cb->max_crossbar_sources);
208         if (!cb->max_crossbar_sources) {
209                 pr_err("missing 'ti,max-crossbar-sources' property\n");
210                 ret = -EINVAL;
211                 goto err_base;
212         }
213
214         of_property_read_u32(node, "ti,max-irqs", &max);
215         if (!max) {
216                 pr_err("missing 'ti,max-irqs' property\n");
217                 ret = -EINVAL;
218                 goto err_base;
219         }
220         cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
221         if (!cb->irq_map)
222                 goto err_base;
223
224         cb->int_max = max;
225
226         for (i = 0; i < max; i++)
227                 cb->irq_map[i] = IRQ_FREE;
228
229         /* Get and mark reserved irqs */
230         irqsr = of_get_property(node, "ti,irqs-reserved", &size);
231         if (irqsr) {
232                 size /= sizeof(__be32);
233
234                 for (i = 0; i < size; i++) {
235                         of_property_read_u32_index(node,
236                                                    "ti,irqs-reserved",
237                                                    i, &entry);
238                         if (entry >= max) {
239                                 pr_err("Invalid reserved entry\n");
240                                 ret = -EINVAL;
241                                 goto err_irq_map;
242                         }
243                         cb->irq_map[entry] = IRQ_RESERVED;
244                 }
245         }
246
247         /* Skip irqs hardwired to bypass the crossbar */
248         irqsr = of_get_property(node, "ti,irqs-skip", &size);
249         if (irqsr) {
250                 size /= sizeof(__be32);
251
252                 for (i = 0; i < size; i++) {
253                         of_property_read_u32_index(node,
254                                                    "ti,irqs-skip",
255                                                    i, &entry);
256                         if (entry >= max) {
257                                 pr_err("Invalid skip entry\n");
258                                 ret = -EINVAL;
259                                 goto err_irq_map;
260                         }
261                         cb->irq_map[entry] = IRQ_SKIP;
262                 }
263         }
264
265
266         cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
267         if (!cb->register_offsets)
268                 goto err_irq_map;
269
270         of_property_read_u32(node, "ti,reg-size", &size);
271
272         switch (size) {
273         case 1:
274                 cb->write = crossbar_writeb;
275                 break;
276         case 2:
277                 cb->write = crossbar_writew;
278                 break;
279         case 4:
280                 cb->write = crossbar_writel;
281                 break;
282         default:
283                 pr_err("Invalid reg-size property\n");
284                 ret = -EINVAL;
285                 goto err_reg_offset;
286                 break;
287         }
288
289         /*
290          * Register offsets are not linear because of the
291          * reserved irqs. so find and store the offsets once.
292          */
293         for (i = 0; i < max; i++) {
294                 if (cb->irq_map[i] == IRQ_RESERVED)
295                         continue;
296
297                 cb->register_offsets[i] = reserved;
298                 reserved += size;
299         }
300
301         of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
302         /* Initialize the crossbar with safe map to start with */
303         for (i = 0; i < max; i++) {
304                 if (cb->irq_map[i] == IRQ_RESERVED ||
305                     cb->irq_map[i] == IRQ_SKIP)
306                         continue;
307
308                 cb->write(i, cb->safe_map);
309         }
310
311         raw_spin_lock_init(&cb->lock);
312
313         return 0;
314
315 err_reg_offset:
316         kfree(cb->register_offsets);
317 err_irq_map:
318         kfree(cb->irq_map);
319 err_base:
320         iounmap(cb->crossbar_base);
321 err_cb:
322         kfree(cb);
323
324         cb = NULL;
325         return ret;
326 }
327
328 static int __init irqcrossbar_init(struct device_node *node,
329                                    struct device_node *parent)
330 {
331         struct irq_domain *parent_domain, *domain;
332         int err;
333
334         if (!parent) {
335                 pr_err("%s: no parent, giving up\n", node->full_name);
336                 return -ENODEV;
337         }
338
339         parent_domain = irq_find_host(parent);
340         if (!parent_domain) {
341                 pr_err("%s: unable to obtain parent domain\n", node->full_name);
342                 return -ENXIO;
343         }
344
345         err = crossbar_of_init(node);
346         if (err)
347                 return err;
348
349         domain = irq_domain_add_hierarchy(parent_domain, 0,
350                                           cb->max_crossbar_sources,
351                                           node, &crossbar_domain_ops,
352                                           NULL);
353         if (!domain) {
354                 pr_err("%s: failed to allocated domain\n", node->full_name);
355                 return -ENOMEM;
356         }
357
358         return 0;
359 }
360
361 IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);