blob: c12bb93334ff99e7a5ddc72d274b00c4eeb9c24c [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,
Grygorii Strashkoe269ec42015-08-14 15:20:27 +030071 .irq_set_type = irq_chip_set_type_parent,
Grygorii Strashko8200fe42015-08-14 15:20:30 +030072 .flags = IRQCHIP_MASK_ON_SUSPEND |
73 IRQCHIP_SKIP_SET_WAKE,
Marc Zyngier783d3182015-03-11 15:43:44 +000074#ifdef CONFIG_SMP
75 .irq_set_affinity = irq_chip_set_affinity_parent,
76#endif
77};
78
79static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
80 irq_hw_number_t hwirq)
Nishanth Menon6f16fc82014-06-26 12:40:20 +053081{
Marc Zyngier783d3182015-03-11 15:43:44 +000082 struct of_phandle_args args;
Nishanth Menon6f16fc82014-06-26 12:40:20 +053083 int i;
Marc Zyngier783d3182015-03-11 15:43:44 +000084 int err;
Nishanth Menon6f16fc82014-06-26 12:40:20 +053085
Marc Zyngier783d3182015-03-11 15:43:44 +000086 raw_spin_lock(&cb->lock);
Nishanth Menonddee0fb2014-06-26 12:40:23 +053087 for (i = cb->int_max - 1; i >= 0; i--) {
Sricharan R96ca8482013-12-03 15:57:23 +053088 if (cb->irq_map[i] == IRQ_FREE) {
Marc Zyngier783d3182015-03-11 15:43:44 +000089 cb->irq_map[i] = hwirq;
90 break;
Sricharan R96ca8482013-12-03 15:57:23 +053091 }
92 }
Marc Zyngier783d3182015-03-11 15:43:44 +000093 raw_spin_unlock(&cb->lock);
Sricharan R96ca8482013-12-03 15:57:23 +053094
Marc Zyngier783d3182015-03-11 15:43:44 +000095 if (i < 0)
96 return -ENODEV;
97
98 args.np = domain->parent->of_node;
99 args.args_count = 3;
100 args.args[0] = 0; /* SPI */
101 args.args[1] = i;
102 args.args[2] = IRQ_TYPE_LEVEL_HIGH;
103
104 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &args);
105 if (err)
106 cb->irq_map[i] = IRQ_FREE;
107 else
108 cb->write(i, hwirq);
109
110 return err;
Sricharan R96ca8482013-12-03 15:57:23 +0530111}
112
Marc Zyngier783d3182015-03-11 15:43:44 +0000113static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
114 unsigned int nr_irqs, void *data)
Nishanth Menon29918b62014-06-26 12:40:32 +0530115{
Marc Zyngier783d3182015-03-11 15:43:44 +0000116 struct of_phandle_args *args = data;
117 irq_hw_number_t hwirq;
118 int i;
Nishanth Menond3608922014-06-26 12:40:34 +0530119
Marc Zyngier783d3182015-03-11 15:43:44 +0000120 if (args->args_count != 3)
121 return -EINVAL; /* Not GIC compliant */
122 if (args->args[0] != 0)
123 return -EINVAL; /* No PPI should point to this domain */
124
125 hwirq = args->args[1];
126 if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
127 return -EINVAL; /* Can't deal with this */
128
129 for (i = 0; i < nr_irqs; i++) {
130 int err = allocate_gic_irq(d, virq + i, hwirq + i);
131
132 if (err)
133 return err;
134
135 irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
136 &crossbar_chip, NULL);
Nishanth Menond3608922014-06-26 12:40:34 +0530137 }
Nishanth Menon29918b62014-06-26 12:40:32 +0530138
Sricharan R96ca8482013-12-03 15:57:23 +0530139 return 0;
140}
141
Sricharan R8b09a452014-06-26 12:40:30 +0530142/**
Marc Zyngier783d3182015-03-11 15:43:44 +0000143 * crossbar_domain_free - unmap/free a crossbar<->irq connection
144 * @domain: domain of irq to unmap
145 * @virq: virq number
146 * @nr_irqs: number of irqs to free
Sricharan R8b09a452014-06-26 12:40:30 +0530147 *
148 * We do not maintain a use count of total number of map/unmap
149 * calls for a particular irq to find out if a irq can be really
150 * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
151 * after which irq is anyways unusable. So an explicit map has to be called
152 * after that.
153 */
Marc Zyngier783d3182015-03-11 15:43:44 +0000154static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
155 unsigned int nr_irqs)
Sricharan R96ca8482013-12-03 15:57:23 +0530156{
Marc Zyngier783d3182015-03-11 15:43:44 +0000157 int i;
Sricharan R96ca8482013-12-03 15:57:23 +0530158
Marc Zyngier783d3182015-03-11 15:43:44 +0000159 raw_spin_lock(&cb->lock);
160 for (i = 0; i < nr_irqs; i++) {
161 struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
162
163 irq_domain_reset_irq_data(d);
164 cb->irq_map[d->hwirq] = IRQ_FREE;
165 cb->write(d->hwirq, cb->safe_map);
Nishanth Menona35057d2014-06-26 12:40:22 +0530166 }
Marc Zyngier783d3182015-03-11 15:43:44 +0000167 raw_spin_unlock(&cb->lock);
Sricharan R96ca8482013-12-03 15:57:23 +0530168}
169
170static int crossbar_domain_xlate(struct irq_domain *d,
171 struct device_node *controller,
172 const u32 *intspec, unsigned int intsize,
173 unsigned long *out_hwirq,
174 unsigned int *out_type)
175{
Marc Zyngier783d3182015-03-11 15:43:44 +0000176 if (d->of_node != controller)
177 return -EINVAL; /* Shouldn't happen, really... */
178 if (intsize != 3)
179 return -EINVAL; /* Not GIC compliant */
180 if (intspec[0] != 0)
181 return -EINVAL; /* No PPI should point to this domain */
Sricharan R96ca8482013-12-03 15:57:23 +0530182
Marc Zyngier783d3182015-03-11 15:43:44 +0000183 *out_hwirq = intspec[1];
184 *out_type = intspec[2];
Sricharan R96ca8482013-12-03 15:57:23 +0530185 return 0;
186}
187
Marc Zyngier783d3182015-03-11 15:43:44 +0000188static const struct irq_domain_ops crossbar_domain_ops = {
189 .alloc = crossbar_domain_alloc,
190 .free = crossbar_domain_free,
191 .xlate = crossbar_domain_xlate,
Sricharan R96ca8482013-12-03 15:57:23 +0530192};
193
194static int __init crossbar_of_init(struct device_node *node)
195{
Nishanth Menonedb442d2014-06-26 12:40:27 +0530196 int i, size, max = 0, reserved = 0, entry;
Sricharan R96ca8482013-12-03 15:57:23 +0530197 const __be32 *irqsr;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530198 int ret = -ENOMEM;
Sricharan R96ca8482013-12-03 15:57:23 +0530199
Dan Carpenter3894e9e2014-04-03 10:21:34 +0300200 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530201
202 if (!cb)
Nishanth Menonedb442d2014-06-26 12:40:27 +0530203 return ret;
Sricharan R96ca8482013-12-03 15:57:23 +0530204
205 cb->crossbar_base = of_iomap(node, 0);
206 if (!cb->crossbar_base)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530207 goto err_cb;
Sricharan R96ca8482013-12-03 15:57:23 +0530208
Nishanth Menon2f7d2fb2014-06-26 12:40:31 +0530209 of_property_read_u32(node, "ti,max-crossbar-sources",
210 &cb->max_crossbar_sources);
211 if (!cb->max_crossbar_sources) {
212 pr_err("missing 'ti,max-crossbar-sources' property\n");
213 ret = -EINVAL;
214 goto err_base;
215 }
216
Sricharan R96ca8482013-12-03 15:57:23 +0530217 of_property_read_u32(node, "ti,max-irqs", &max);
Nishanth Menonedb442d2014-06-26 12:40:27 +0530218 if (!max) {
219 pr_err("missing 'ti,max-irqs' property\n");
220 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530221 goto err_base;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530222 }
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530223 cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530224 if (!cb->irq_map)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530225 goto err_base;
Sricharan R96ca8482013-12-03 15:57:23 +0530226
227 cb->int_max = max;
228
229 for (i = 0; i < max; i++)
230 cb->irq_map[i] = IRQ_FREE;
231
232 /* Get and mark reserved irqs */
233 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
234 if (irqsr) {
235 size /= sizeof(__be32);
236
237 for (i = 0; i < size; i++) {
238 of_property_read_u32_index(node,
239 "ti,irqs-reserved",
240 i, &entry);
Dan Carpenter702f7e32014-08-07 18:28:21 +0300241 if (entry >= max) {
Sricharan R96ca8482013-12-03 15:57:23 +0530242 pr_err("Invalid reserved entry\n");
Nishanth Menonedb442d2014-06-26 12:40:27 +0530243 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530244 goto err_irq_map;
Sricharan R96ca8482013-12-03 15:57:23 +0530245 }
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530246 cb->irq_map[entry] = IRQ_RESERVED;
Sricharan R96ca8482013-12-03 15:57:23 +0530247 }
248 }
249
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530250 /* Skip irqs hardwired to bypass the crossbar */
251 irqsr = of_get_property(node, "ti,irqs-skip", &size);
252 if (irqsr) {
253 size /= sizeof(__be32);
254
255 for (i = 0; i < size; i++) {
256 of_property_read_u32_index(node,
257 "ti,irqs-skip",
258 i, &entry);
Dan Carpenter702f7e32014-08-07 18:28:21 +0300259 if (entry >= max) {
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530260 pr_err("Invalid skip entry\n");
261 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530262 goto err_irq_map;
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530263 }
264 cb->irq_map[entry] = IRQ_SKIP;
265 }
266 }
267
268
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530269 cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530270 if (!cb->register_offsets)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530271 goto err_irq_map;
Sricharan R96ca8482013-12-03 15:57:23 +0530272
273 of_property_read_u32(node, "ti,reg-size", &size);
274
275 switch (size) {
276 case 1:
277 cb->write = crossbar_writeb;
278 break;
279 case 2:
280 cb->write = crossbar_writew;
281 break;
282 case 4:
283 cb->write = crossbar_writel;
284 break;
285 default:
286 pr_err("Invalid reg-size property\n");
Nishanth Menonedb442d2014-06-26 12:40:27 +0530287 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530288 goto err_reg_offset;
Sricharan R96ca8482013-12-03 15:57:23 +0530289 break;
290 }
291
292 /*
293 * Register offsets are not linear because of the
294 * reserved irqs. so find and store the offsets once.
295 */
296 for (i = 0; i < max; i++) {
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530297 if (cb->irq_map[i] == IRQ_RESERVED)
Sricharan R96ca8482013-12-03 15:57:23 +0530298 continue;
299
300 cb->register_offsets[i] = reserved;
301 reserved += size;
302 }
303
Nishanth Menona35057d2014-06-26 12:40:22 +0530304 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
Nishanth Menona35057d2014-06-26 12:40:22 +0530305 /* Initialize the crossbar with safe map to start with */
306 for (i = 0; i < max; i++) {
307 if (cb->irq_map[i] == IRQ_RESERVED ||
308 cb->irq_map[i] == IRQ_SKIP)
309 continue;
310
311 cb->write(i, cb->safe_map);
312 }
313
Marc Zyngier783d3182015-03-11 15:43:44 +0000314 raw_spin_lock_init(&cb->lock);
315
Sricharan R96ca8482013-12-03 15:57:23 +0530316 return 0;
317
Nishanth Menon3c44d512014-06-26 12:40:28 +0530318err_reg_offset:
Sricharan R96ca8482013-12-03 15:57:23 +0530319 kfree(cb->register_offsets);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530320err_irq_map:
Sricharan R96ca8482013-12-03 15:57:23 +0530321 kfree(cb->irq_map);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530322err_base:
Sricharan R96ca8482013-12-03 15:57:23 +0530323 iounmap(cb->crossbar_base);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530324err_cb:
Sricharan R96ca8482013-12-03 15:57:23 +0530325 kfree(cb);
Sricharan R99e37d0e2014-06-26 12:40:29 +0530326
327 cb = NULL;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530328 return ret;
Sricharan R96ca8482013-12-03 15:57:23 +0530329}
330
Marc Zyngier783d3182015-03-11 15:43:44 +0000331static int __init irqcrossbar_init(struct device_node *node,
332 struct device_node *parent)
Sricharan R96ca8482013-12-03 15:57:23 +0530333{
Marc Zyngier783d3182015-03-11 15:43:44 +0000334 struct irq_domain *parent_domain, *domain;
335 int err;
Sricharan R96ca8482013-12-03 15:57:23 +0530336
Marc Zyngier783d3182015-03-11 15:43:44 +0000337 if (!parent) {
338 pr_err("%s: no parent, giving up\n", node->full_name);
339 return -ENODEV;
340 }
341
342 parent_domain = irq_find_host(parent);
343 if (!parent_domain) {
344 pr_err("%s: unable to obtain parent domain\n", node->full_name);
345 return -ENXIO;
346 }
347
348 err = crossbar_of_init(node);
349 if (err)
350 return err;
351
352 domain = irq_domain_add_hierarchy(parent_domain, 0,
353 cb->max_crossbar_sources,
354 node, &crossbar_domain_ops,
355 NULL);
356 if (!domain) {
357 pr_err("%s: failed to allocated domain\n", node->full_name);
358 return -ENOMEM;
359 }
360
Sricharan R96ca8482013-12-03 15:57:23 +0530361 return 0;
362}
Marc Zyngier783d3182015-03-11 15:43:44 +0000363
364IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);