irqchip: Prepare for local stub header removal
[deliverable/linux.git] / drivers / irqchip / irq-crossbar.c
CommitLineData
96ca848e
S
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>
41a83e06 14#include <linux/irqchip.h>
783d3186 15#include <linux/irqdomain.h>
96ca848e
S
16#include <linux/of_address.h>
17#include <linux/of_irq.h>
18#include <linux/slab.h>
783d3186 19
96ca848e 20#define IRQ_FREE -1
1d50d2ce 21#define IRQ_RESERVED -2
64e0f8ba 22#define IRQ_SKIP -3
96ca848e
S
23#define GIC_IRQ_START 32
24
e30ef8ab
NM
25/**
26 * struct crossbar_device - crossbar device description
783d3186 27 * @lock: spinlock serializing access to @irq_map
96ca848e 28 * @int_max: maximum number of supported interrupts
a35057d1 29 * @safe_map: safe default value to initialize the crossbar
2f7d2fb7 30 * @max_crossbar_sources: Maximum number of crossbar sources
96ca848e
S
31 * @irq_map: array of interrupts to crossbar number mapping
32 * @crossbar_base: crossbar base address
33 * @register_offsets: offsets for each irq number
e30ef8ab 34 * @write: register write function pointer
96ca848e
S
35 */
36struct crossbar_device {
783d3186 37 raw_spinlock_t lock;
96ca848e 38 uint int_max;
a35057d1 39 uint safe_map;
2f7d2fb7 40 uint max_crossbar_sources;
96ca848e
S
41 uint *irq_map;
42 void __iomem *crossbar_base;
43 int *register_offsets;
a35057d1 44 void (*write)(int, int);
96ca848e
S
45};
46
47static struct crossbar_device *cb;
48
783d3186 49static void crossbar_writel(int irq_no, int cb_no)
96ca848e
S
50{
51 writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
52}
53
783d3186 54static void crossbar_writew(int irq_no, int cb_no)
96ca848e
S
55{
56 writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
57}
58
783d3186 59static void crossbar_writeb(int irq_no, int cb_no)
96ca848e
S
60{
61 writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
62}
63
783d3186
MZ
64static 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};
6f16fc87 75
783d3186
MZ
76static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
77 irq_hw_number_t hwirq)
96ca848e 78{
783d3186 79 struct of_phandle_args args;
96ca848e 80 int i;
783d3186 81 int err;
96ca848e 82
783d3186 83 raw_spin_lock(&cb->lock);
ddee0fb4 84 for (i = cb->int_max - 1; i >= 0; i--) {
96ca848e 85 if (cb->irq_map[i] == IRQ_FREE) {
783d3186
MZ
86 cb->irq_map[i] = hwirq;
87 break;
96ca848e
S
88 }
89 }
783d3186 90 raw_spin_unlock(&cb->lock);
96ca848e 91
783d3186
MZ
92 if (i < 0)
93 return -ENODEV;
96ca848e 94
783d3186
MZ
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;
d360892d 100
783d3186
MZ
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);
29918b67 106
783d3186 107 return err;
29918b67
NM
108}
109
783d3186
MZ
110static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
111 unsigned int nr_irqs, void *data)
96ca848e 112{
783d3186
MZ
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 }
29918b67 135
96ca848e
S
136 return 0;
137}
138
8b09a45d 139/**
783d3186
MZ
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
8b09a45d
S
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 */
783d3186
MZ
151static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
152 unsigned int nr_irqs)
96ca848e 153{
783d3186 154 int i;
96ca848e 155
783d3186
MZ
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);
a35057d1 163 }
783d3186 164 raw_spin_unlock(&cb->lock);
96ca848e
S
165}
166
167static 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{
783d3186
MZ
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];
96ca848e
S
182 return 0;
183}
184
783d3186
MZ
185static const struct irq_domain_ops crossbar_domain_ops = {
186 .alloc = crossbar_domain_alloc,
187 .free = crossbar_domain_free,
188 .xlate = crossbar_domain_xlate,
96ca848e
S
189};
190
191static int __init crossbar_of_init(struct device_node *node)
192{
edb442de 193 int i, size, max = 0, reserved = 0, entry;
96ca848e 194 const __be32 *irqsr;
edb442de 195 int ret = -ENOMEM;
96ca848e 196
3894e9e8 197 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
96ca848e
S
198
199 if (!cb)
edb442de 200 return ret;
96ca848e
S
201
202 cb->crossbar_base = of_iomap(node, 0);
203 if (!cb->crossbar_base)
3c44d515 204 goto err_cb;
96ca848e 205
2f7d2fb7
NM
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
96ca848e 214 of_property_read_u32(node, "ti,max-irqs", &max);
edb442de
NM
215 if (!max) {
216 pr_err("missing 'ti,max-irqs' property\n");
217 ret = -EINVAL;
3c44d515 218 goto err_base;
edb442de 219 }
4dbf45e3 220 cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
96ca848e 221 if (!cb->irq_map)
3c44d515 222 goto err_base;
96ca848e
S
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);
702f7e36 238 if (entry >= max) {
96ca848e 239 pr_err("Invalid reserved entry\n");
edb442de 240 ret = -EINVAL;
3c44d515 241 goto err_irq_map;
96ca848e 242 }
1d50d2ce 243 cb->irq_map[entry] = IRQ_RESERVED;
96ca848e
S
244 }
245 }
246
64e0f8ba
NM
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);
702f7e36 256 if (entry >= max) {
64e0f8ba
NM
257 pr_err("Invalid skip entry\n");
258 ret = -EINVAL;
3c44d515 259 goto err_irq_map;
64e0f8ba
NM
260 }
261 cb->irq_map[entry] = IRQ_SKIP;
262 }
263 }
264
265
4dbf45e3 266 cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
96ca848e 267 if (!cb->register_offsets)
3c44d515 268 goto err_irq_map;
96ca848e
S
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");
edb442de 284 ret = -EINVAL;
3c44d515 285 goto err_reg_offset;
96ca848e
S
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++) {
1d50d2ce 294 if (cb->irq_map[i] == IRQ_RESERVED)
96ca848e
S
295 continue;
296
297 cb->register_offsets[i] = reserved;
298 reserved += size;
299 }
300
a35057d1 301 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
a35057d1
NM
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
783d3186
MZ
311 raw_spin_lock_init(&cb->lock);
312
96ca848e
S
313 return 0;
314
3c44d515 315err_reg_offset:
96ca848e 316 kfree(cb->register_offsets);
3c44d515 317err_irq_map:
96ca848e 318 kfree(cb->irq_map);
3c44d515 319err_base:
96ca848e 320 iounmap(cb->crossbar_base);
3c44d515 321err_cb:
96ca848e 322 kfree(cb);
99e37d0e
S
323
324 cb = NULL;
edb442de 325 return ret;
96ca848e
S
326}
327
783d3186
MZ
328static int __init irqcrossbar_init(struct device_node *node,
329 struct device_node *parent)
96ca848e 330{
783d3186
MZ
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);
96ca848e 336 return -ENODEV;
783d3186
MZ
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 }
96ca848e 357
96ca848e
S
358 return 0;
359}
783d3186
MZ
360
361IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);
This page took 0.095192 seconds and 5 git commands to generate.