blob: 3ba58e7b4724c7e008afe0cabd4856a740a57ed7 [file] [log] [blame]
Sricharan R96ca8482013-12-03 15:57:23 +05301/*
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>
Marc Zyngier783d3182015-03-11 15:43:44 +000014#include <linux/irqdomain.h>
Sricharan R96ca8482013-12-03 15:57:23 +053015#include <linux/of_address.h>
16#include <linux/of_irq.h>
17#include <linux/slab.h>
Marc Zyngier783d3182015-03-11 15:43:44 +000018
19#include "irqchip.h"
Sricharan R96ca8482013-12-03 15:57:23 +053020
21#define IRQ_FREE -1
Nishanth Menon1d50d2c2014-06-26 12:40:19 +053022#define IRQ_RESERVED -2
Nishanth Menon64e0f8b2014-06-26 12:40:21 +053023#define IRQ_SKIP -3
Sricharan R96ca8482013-12-03 15:57:23 +053024#define GIC_IRQ_START 32
25
Nishanth Menone30ef8a2014-06-26 12:40:26 +053026/**
27 * struct crossbar_device - crossbar device description
Marc Zyngier783d3182015-03-11 15:43:44 +000028 * @lock: spinlock serializing access to @irq_map
Sricharan R96ca8482013-12-03 15:57:23 +053029 * @int_max: maximum number of supported interrupts
Nishanth Menona35057d2014-06-26 12:40:22 +053030 * @safe_map: safe default value to initialize the crossbar
Nishanth Menon2f7d2fb2014-06-26 12:40:31 +053031 * @max_crossbar_sources: Maximum number of crossbar sources
Sricharan R96ca8482013-12-03 15:57:23 +053032 * @irq_map: array of interrupts to crossbar number mapping
33 * @crossbar_base: crossbar base address
34 * @register_offsets: offsets for each irq number
Nishanth Menone30ef8a2014-06-26 12:40:26 +053035 * @write: register write function pointer
Sricharan R96ca8482013-12-03 15:57:23 +053036 */
37struct crossbar_device {
Marc Zyngier783d3182015-03-11 15:43:44 +000038 raw_spinlock_t lock;
Sricharan R96ca8482013-12-03 15:57:23 +053039 uint int_max;
Nishanth Menona35057d2014-06-26 12:40:22 +053040 uint safe_map;
Nishanth Menon2f7d2fb2014-06-26 12:40:31 +053041 uint max_crossbar_sources;
Sricharan R96ca8482013-12-03 15:57:23 +053042 uint *irq_map;
43 void __iomem *crossbar_base;
44 int *register_offsets;
Nishanth Menona35057d2014-06-26 12:40:22 +053045 void (*write)(int, int);
Sricharan R96ca8482013-12-03 15:57:23 +053046};
47
48static struct crossbar_device *cb;
49
Marc Zyngier783d3182015-03-11 15:43:44 +000050static void crossbar_writel(int irq_no, int cb_no)
Sricharan R96ca8482013-12-03 15:57:23 +053051{
52 writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
53}
54
Marc Zyngier783d3182015-03-11 15:43:44 +000055static void crossbar_writew(int irq_no, int cb_no)
Sricharan R96ca8482013-12-03 15:57:23 +053056{
57 writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
58}
59
Marc Zyngier783d3182015-03-11 15:43:44 +000060static void crossbar_writeb(int irq_no, int cb_no)
Sricharan R96ca8482013-12-03 15:57:23 +053061{
62 writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
63}
64
Marc Zyngier783d3182015-03-11 15:43:44 +000065static struct irq_chip crossbar_chip = {
66 .name = "CBAR",
67 .irq_eoi = irq_chip_eoi_parent,
68 .irq_mask = irq_chip_mask_parent,
69 .irq_unmask = irq_chip_unmask_parent,
70 .irq_retrigger = irq_chip_retrigger_hierarchy,
71 .irq_set_wake = irq_chip_set_wake_parent,
Grygorii Strashkoe269ec42015-08-14 15:20:27 +030072 .irq_set_type = irq_chip_set_type_parent,
Marc Zyngier783d3182015-03-11 15:43:44 +000073#ifdef CONFIG_SMP
74 .irq_set_affinity = irq_chip_set_affinity_parent,
75#endif
76};
77
78static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
79 irq_hw_number_t hwirq)
Nishanth Menon6f16fc82014-06-26 12:40:20 +053080{
Marc Zyngier783d3182015-03-11 15:43:44 +000081 struct of_phandle_args args;
Nishanth Menon6f16fc82014-06-26 12:40:20 +053082 int i;
Marc Zyngier783d3182015-03-11 15:43:44 +000083 int err;
Nishanth Menon6f16fc82014-06-26 12:40:20 +053084
Marc Zyngier783d3182015-03-11 15:43:44 +000085 raw_spin_lock(&cb->lock);
Nishanth Menonddee0fb2014-06-26 12:40:23 +053086 for (i = cb->int_max - 1; i >= 0; i--) {
Sricharan R96ca8482013-12-03 15:57:23 +053087 if (cb->irq_map[i] == IRQ_FREE) {
Marc Zyngier783d3182015-03-11 15:43:44 +000088 cb->irq_map[i] = hwirq;
89 break;
Sricharan R96ca8482013-12-03 15:57:23 +053090 }
91 }
Marc Zyngier783d3182015-03-11 15:43:44 +000092 raw_spin_unlock(&cb->lock);
Sricharan R96ca8482013-12-03 15:57:23 +053093
Marc Zyngier783d3182015-03-11 15:43:44 +000094 if (i < 0)
95 return -ENODEV;
96
97 args.np = domain->parent->of_node;
98 args.args_count = 3;
99 args.args[0] = 0; /* SPI */
100 args.args[1] = i;
101 args.args[2] = IRQ_TYPE_LEVEL_HIGH;
102
103 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &args);
104 if (err)
105 cb->irq_map[i] = IRQ_FREE;
106 else
107 cb->write(i, hwirq);
108
109 return err;
Sricharan R96ca8482013-12-03 15:57:23 +0530110}
111
Marc Zyngier783d3182015-03-11 15:43:44 +0000112static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
113 unsigned int nr_irqs, void *data)
Nishanth Menon29918b62014-06-26 12:40:32 +0530114{
Marc Zyngier783d3182015-03-11 15:43:44 +0000115 struct of_phandle_args *args = data;
116 irq_hw_number_t hwirq;
117 int i;
Nishanth Menond3608922014-06-26 12:40:34 +0530118
Marc Zyngier783d3182015-03-11 15:43:44 +0000119 if (args->args_count != 3)
120 return -EINVAL; /* Not GIC compliant */
121 if (args->args[0] != 0)
122 return -EINVAL; /* No PPI should point to this domain */
123
124 hwirq = args->args[1];
125 if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
126 return -EINVAL; /* Can't deal with this */
127
128 for (i = 0; i < nr_irqs; i++) {
129 int err = allocate_gic_irq(d, virq + i, hwirq + i);
130
131 if (err)
132 return err;
133
134 irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
135 &crossbar_chip, NULL);
Nishanth Menond3608922014-06-26 12:40:34 +0530136 }
Nishanth Menon29918b62014-06-26 12:40:32 +0530137
Sricharan R96ca8482013-12-03 15:57:23 +0530138 return 0;
139}
140
Sricharan R8b09a452014-06-26 12:40:30 +0530141/**
Marc Zyngier783d3182015-03-11 15:43:44 +0000142 * crossbar_domain_free - unmap/free a crossbar<->irq connection
143 * @domain: domain of irq to unmap
144 * @virq: virq number
145 * @nr_irqs: number of irqs to free
Sricharan R8b09a452014-06-26 12:40:30 +0530146 *
147 * We do not maintain a use count of total number of map/unmap
148 * calls for a particular irq to find out if a irq can be really
149 * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
150 * after which irq is anyways unusable. So an explicit map has to be called
151 * after that.
152 */
Marc Zyngier783d3182015-03-11 15:43:44 +0000153static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
154 unsigned int nr_irqs)
Sricharan R96ca8482013-12-03 15:57:23 +0530155{
Marc Zyngier783d3182015-03-11 15:43:44 +0000156 int i;
Sricharan R96ca8482013-12-03 15:57:23 +0530157
Marc Zyngier783d3182015-03-11 15:43:44 +0000158 raw_spin_lock(&cb->lock);
159 for (i = 0; i < nr_irqs; i++) {
160 struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
161
162 irq_domain_reset_irq_data(d);
163 cb->irq_map[d->hwirq] = IRQ_FREE;
164 cb->write(d->hwirq, cb->safe_map);
Nishanth Menona35057d2014-06-26 12:40:22 +0530165 }
Marc Zyngier783d3182015-03-11 15:43:44 +0000166 raw_spin_unlock(&cb->lock);
Sricharan R96ca8482013-12-03 15:57:23 +0530167}
168
169static int crossbar_domain_xlate(struct irq_domain *d,
170 struct device_node *controller,
171 const u32 *intspec, unsigned int intsize,
172 unsigned long *out_hwirq,
173 unsigned int *out_type)
174{
Marc Zyngier783d3182015-03-11 15:43:44 +0000175 if (d->of_node != controller)
176 return -EINVAL; /* Shouldn't happen, really... */
177 if (intsize != 3)
178 return -EINVAL; /* Not GIC compliant */
179 if (intspec[0] != 0)
180 return -EINVAL; /* No PPI should point to this domain */
Sricharan R96ca8482013-12-03 15:57:23 +0530181
Marc Zyngier783d3182015-03-11 15:43:44 +0000182 *out_hwirq = intspec[1];
183 *out_type = intspec[2];
Sricharan R96ca8482013-12-03 15:57:23 +0530184 return 0;
185}
186
Marc Zyngier783d3182015-03-11 15:43:44 +0000187static const struct irq_domain_ops crossbar_domain_ops = {
188 .alloc = crossbar_domain_alloc,
189 .free = crossbar_domain_free,
190 .xlate = crossbar_domain_xlate,
Sricharan R96ca8482013-12-03 15:57:23 +0530191};
192
193static int __init crossbar_of_init(struct device_node *node)
194{
Nishanth Menonedb442d2014-06-26 12:40:27 +0530195 int i, size, max = 0, reserved = 0, entry;
Sricharan R96ca8482013-12-03 15:57:23 +0530196 const __be32 *irqsr;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530197 int ret = -ENOMEM;
Sricharan R96ca8482013-12-03 15:57:23 +0530198
Dan Carpenter3894e9e2014-04-03 10:21:34 +0300199 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530200
201 if (!cb)
Nishanth Menonedb442d2014-06-26 12:40:27 +0530202 return ret;
Sricharan R96ca8482013-12-03 15:57:23 +0530203
204 cb->crossbar_base = of_iomap(node, 0);
205 if (!cb->crossbar_base)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530206 goto err_cb;
Sricharan R96ca8482013-12-03 15:57:23 +0530207
Nishanth Menon2f7d2fb2014-06-26 12:40:31 +0530208 of_property_read_u32(node, "ti,max-crossbar-sources",
209 &cb->max_crossbar_sources);
210 if (!cb->max_crossbar_sources) {
211 pr_err("missing 'ti,max-crossbar-sources' property\n");
212 ret = -EINVAL;
213 goto err_base;
214 }
215
Sricharan R96ca8482013-12-03 15:57:23 +0530216 of_property_read_u32(node, "ti,max-irqs", &max);
Nishanth Menonedb442d2014-06-26 12:40:27 +0530217 if (!max) {
218 pr_err("missing 'ti,max-irqs' property\n");
219 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530220 goto err_base;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530221 }
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530222 cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530223 if (!cb->irq_map)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530224 goto err_base;
Sricharan R96ca8482013-12-03 15:57:23 +0530225
226 cb->int_max = max;
227
228 for (i = 0; i < max; i++)
229 cb->irq_map[i] = IRQ_FREE;
230
231 /* Get and mark reserved irqs */
232 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
233 if (irqsr) {
234 size /= sizeof(__be32);
235
236 for (i = 0; i < size; i++) {
237 of_property_read_u32_index(node,
238 "ti,irqs-reserved",
239 i, &entry);
Dan Carpenter702f7e32014-08-07 18:28:21 +0300240 if (entry >= max) {
Sricharan R96ca8482013-12-03 15:57:23 +0530241 pr_err("Invalid reserved entry\n");
Nishanth Menonedb442d2014-06-26 12:40:27 +0530242 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530243 goto err_irq_map;
Sricharan R96ca8482013-12-03 15:57:23 +0530244 }
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530245 cb->irq_map[entry] = IRQ_RESERVED;
Sricharan R96ca8482013-12-03 15:57:23 +0530246 }
247 }
248
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530249 /* Skip irqs hardwired to bypass the crossbar */
250 irqsr = of_get_property(node, "ti,irqs-skip", &size);
251 if (irqsr) {
252 size /= sizeof(__be32);
253
254 for (i = 0; i < size; i++) {
255 of_property_read_u32_index(node,
256 "ti,irqs-skip",
257 i, &entry);
Dan Carpenter702f7e32014-08-07 18:28:21 +0300258 if (entry >= max) {
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530259 pr_err("Invalid skip entry\n");
260 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530261 goto err_irq_map;
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530262 }
263 cb->irq_map[entry] = IRQ_SKIP;
264 }
265 }
266
267
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530268 cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530269 if (!cb->register_offsets)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530270 goto err_irq_map;
Sricharan R96ca8482013-12-03 15:57:23 +0530271
272 of_property_read_u32(node, "ti,reg-size", &size);
273
274 switch (size) {
275 case 1:
276 cb->write = crossbar_writeb;
277 break;
278 case 2:
279 cb->write = crossbar_writew;
280 break;
281 case 4:
282 cb->write = crossbar_writel;
283 break;
284 default:
285 pr_err("Invalid reg-size property\n");
Nishanth Menonedb442d2014-06-26 12:40:27 +0530286 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530287 goto err_reg_offset;
Sricharan R96ca8482013-12-03 15:57:23 +0530288 break;
289 }
290
291 /*
292 * Register offsets are not linear because of the
293 * reserved irqs. so find and store the offsets once.
294 */
295 for (i = 0; i < max; i++) {
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530296 if (cb->irq_map[i] == IRQ_RESERVED)
Sricharan R96ca8482013-12-03 15:57:23 +0530297 continue;
298
299 cb->register_offsets[i] = reserved;
300 reserved += size;
301 }
302
Nishanth Menona35057d2014-06-26 12:40:22 +0530303 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
Nishanth Menona35057d2014-06-26 12:40:22 +0530304 /* Initialize the crossbar with safe map to start with */
305 for (i = 0; i < max; i++) {
306 if (cb->irq_map[i] == IRQ_RESERVED ||
307 cb->irq_map[i] == IRQ_SKIP)
308 continue;
309
310 cb->write(i, cb->safe_map);
311 }
312
Marc Zyngier783d3182015-03-11 15:43:44 +0000313 raw_spin_lock_init(&cb->lock);
314
Sricharan R96ca8482013-12-03 15:57:23 +0530315 return 0;
316
Nishanth Menon3c44d512014-06-26 12:40:28 +0530317err_reg_offset:
Sricharan R96ca8482013-12-03 15:57:23 +0530318 kfree(cb->register_offsets);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530319err_irq_map:
Sricharan R96ca8482013-12-03 15:57:23 +0530320 kfree(cb->irq_map);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530321err_base:
Sricharan R96ca8482013-12-03 15:57:23 +0530322 iounmap(cb->crossbar_base);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530323err_cb:
Sricharan R96ca8482013-12-03 15:57:23 +0530324 kfree(cb);
Sricharan R99e37d0e2014-06-26 12:40:29 +0530325
326 cb = NULL;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530327 return ret;
Sricharan R96ca8482013-12-03 15:57:23 +0530328}
329
Marc Zyngier783d3182015-03-11 15:43:44 +0000330static int __init irqcrossbar_init(struct device_node *node,
331 struct device_node *parent)
Sricharan R96ca8482013-12-03 15:57:23 +0530332{
Marc Zyngier783d3182015-03-11 15:43:44 +0000333 struct irq_domain *parent_domain, *domain;
334 int err;
Sricharan R96ca8482013-12-03 15:57:23 +0530335
Marc Zyngier783d3182015-03-11 15:43:44 +0000336 if (!parent) {
337 pr_err("%s: no parent, giving up\n", node->full_name);
338 return -ENODEV;
339 }
340
341 parent_domain = irq_find_host(parent);
342 if (!parent_domain) {
343 pr_err("%s: unable to obtain parent domain\n", node->full_name);
344 return -ENXIO;
345 }
346
347 err = crossbar_of_init(node);
348 if (err)
349 return err;
350
351 domain = irq_domain_add_hierarchy(parent_domain, 0,
352 cb->max_crossbar_sources,
353 node, &crossbar_domain_ops,
354 NULL);
355 if (!domain) {
356 pr_err("%s: failed to allocated domain\n", node->full_name);
357 return -ENOMEM;
358 }
359
Sricharan R96ca8482013-12-03 15:57:23 +0530360 return 0;
361}
Marc Zyngier783d3182015-03-11 15:43:44 +0000362
363IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);