blob: 1240c4deda7517d63c789e53d49951ed82336452 [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>
Joel Porquet41a83e02015-07-07 17:11:46 -040014#include <linux/irqchip.h>
Marc Zyngier783d3182015-03-11 15:43:44 +000015#include <linux/irqdomain.h>
Sricharan R96ca8482013-12-03 15:57:23 +053016#include <linux/of_address.h>
17#include <linux/of_irq.h>
18#include <linux/slab.h>
Marc Zyngier783d3182015-03-11 15:43:44 +000019
Sricharan R96ca8482013-12-03 15:57:23 +053020#define IRQ_FREE -1
Nishanth Menon1d50d2c2014-06-26 12:40:19 +053021#define IRQ_RESERVED -2
Nishanth Menon64e0f8b2014-06-26 12:40:21 +053022#define IRQ_SKIP -3
Sricharan R96ca8482013-12-03 15:57:23 +053023#define GIC_IRQ_START 32
24
Nishanth Menone30ef8a2014-06-26 12:40:26 +053025/**
26 * struct crossbar_device - crossbar device description
Marc Zyngier783d3182015-03-11 15:43:44 +000027 * @lock: spinlock serializing access to @irq_map
Sricharan R96ca8482013-12-03 15:57:23 +053028 * @int_max: maximum number of supported interrupts
Nishanth Menona35057d2014-06-26 12:40:22 +053029 * @safe_map: safe default value to initialize the crossbar
Nishanth Menon2f7d2fb2014-06-26 12:40:31 +053030 * @max_crossbar_sources: Maximum number of crossbar sources
Sricharan R96ca8482013-12-03 15:57:23 +053031 * @irq_map: array of interrupts to crossbar number mapping
32 * @crossbar_base: crossbar base address
33 * @register_offsets: offsets for each irq number
Nishanth Menone30ef8a2014-06-26 12:40:26 +053034 * @write: register write function pointer
Sricharan R96ca8482013-12-03 15:57:23 +053035 */
36struct crossbar_device {
Marc Zyngier783d3182015-03-11 15:43:44 +000037 raw_spinlock_t lock;
Sricharan R96ca8482013-12-03 15:57:23 +053038 uint int_max;
Nishanth Menona35057d2014-06-26 12:40:22 +053039 uint safe_map;
Nishanth Menon2f7d2fb2014-06-26 12:40:31 +053040 uint max_crossbar_sources;
Sricharan R96ca8482013-12-03 15:57:23 +053041 uint *irq_map;
42 void __iomem *crossbar_base;
43 int *register_offsets;
Nishanth Menona35057d2014-06-26 12:40:22 +053044 void (*write)(int, int);
Sricharan R96ca8482013-12-03 15:57:23 +053045};
46
47static struct crossbar_device *cb;
48
Marc Zyngier783d3182015-03-11 15:43:44 +000049static void crossbar_writel(int irq_no, int cb_no)
Sricharan R96ca8482013-12-03 15:57:23 +053050{
51 writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
52}
53
Marc Zyngier783d3182015-03-11 15:43:44 +000054static void crossbar_writew(int irq_no, int cb_no)
Sricharan R96ca8482013-12-03 15:57:23 +053055{
56 writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
57}
58
Marc Zyngier783d3182015-03-11 15:43:44 +000059static void crossbar_writeb(int irq_no, int cb_no)
Sricharan R96ca8482013-12-03 15:57:23 +053060{
61 writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
62}
63
Marc Zyngier783d3182015-03-11 15:43:44 +000064static 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
76static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
77 irq_hw_number_t hwirq)
Nishanth Menon6f16fc82014-06-26 12:40:20 +053078{
Marc Zyngier783d3182015-03-11 15:43:44 +000079 struct of_phandle_args args;
Nishanth Menon6f16fc82014-06-26 12:40:20 +053080 int i;
Marc Zyngier783d3182015-03-11 15:43:44 +000081 int err;
Nishanth Menon6f16fc82014-06-26 12:40:20 +053082
Marc Zyngier783d3182015-03-11 15:43:44 +000083 raw_spin_lock(&cb->lock);
Nishanth Menonddee0fb2014-06-26 12:40:23 +053084 for (i = cb->int_max - 1; i >= 0; i--) {
Sricharan R96ca8482013-12-03 15:57:23 +053085 if (cb->irq_map[i] == IRQ_FREE) {
Marc Zyngier783d3182015-03-11 15:43:44 +000086 cb->irq_map[i] = hwirq;
87 break;
Sricharan R96ca8482013-12-03 15:57:23 +053088 }
89 }
Marc Zyngier783d3182015-03-11 15:43:44 +000090 raw_spin_unlock(&cb->lock);
Sricharan R96ca8482013-12-03 15:57:23 +053091
Marc Zyngier783d3182015-03-11 15:43:44 +000092 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;
Sricharan R96ca8482013-12-03 15:57:23 +0530108}
109
Marc Zyngier783d3182015-03-11 15:43:44 +0000110static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
111 unsigned int nr_irqs, void *data)
Nishanth Menon29918b62014-06-26 12:40:32 +0530112{
Marc Zyngier783d3182015-03-11 15:43:44 +0000113 struct of_phandle_args *args = data;
114 irq_hw_number_t hwirq;
115 int i;
Nishanth Menond3608922014-06-26 12:40:34 +0530116
Marc Zyngier783d3182015-03-11 15:43:44 +0000117 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);
Nishanth Menond3608922014-06-26 12:40:34 +0530134 }
Nishanth Menon29918b62014-06-26 12:40:32 +0530135
Sricharan R96ca8482013-12-03 15:57:23 +0530136 return 0;
137}
138
Sricharan R8b09a452014-06-26 12:40:30 +0530139/**
Marc Zyngier783d3182015-03-11 15:43:44 +0000140 * 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
Sricharan R8b09a452014-06-26 12:40:30 +0530144 *
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 */
Marc Zyngier783d3182015-03-11 15:43:44 +0000151static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
152 unsigned int nr_irqs)
Sricharan R96ca8482013-12-03 15:57:23 +0530153{
Marc Zyngier783d3182015-03-11 15:43:44 +0000154 int i;
Sricharan R96ca8482013-12-03 15:57:23 +0530155
Marc Zyngier783d3182015-03-11 15:43:44 +0000156 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);
Nishanth Menona35057d2014-06-26 12:40:22 +0530163 }
Marc Zyngier783d3182015-03-11 15:43:44 +0000164 raw_spin_unlock(&cb->lock);
Sricharan R96ca8482013-12-03 15:57:23 +0530165}
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{
Marc Zyngier783d3182015-03-11 15:43:44 +0000173 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 */
Sricharan R96ca8482013-12-03 15:57:23 +0530179
Marc Zyngier783d3182015-03-11 15:43:44 +0000180 *out_hwirq = intspec[1];
181 *out_type = intspec[2];
Sricharan R96ca8482013-12-03 15:57:23 +0530182 return 0;
183}
184
Marc Zyngier783d3182015-03-11 15:43:44 +0000185static const struct irq_domain_ops crossbar_domain_ops = {
186 .alloc = crossbar_domain_alloc,
187 .free = crossbar_domain_free,
188 .xlate = crossbar_domain_xlate,
Sricharan R96ca8482013-12-03 15:57:23 +0530189};
190
191static int __init crossbar_of_init(struct device_node *node)
192{
Nishanth Menonedb442d2014-06-26 12:40:27 +0530193 int i, size, max = 0, reserved = 0, entry;
Sricharan R96ca8482013-12-03 15:57:23 +0530194 const __be32 *irqsr;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530195 int ret = -ENOMEM;
Sricharan R96ca8482013-12-03 15:57:23 +0530196
Dan Carpenter3894e9e2014-04-03 10:21:34 +0300197 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530198
199 if (!cb)
Nishanth Menonedb442d2014-06-26 12:40:27 +0530200 return ret;
Sricharan R96ca8482013-12-03 15:57:23 +0530201
202 cb->crossbar_base = of_iomap(node, 0);
203 if (!cb->crossbar_base)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530204 goto err_cb;
Sricharan R96ca8482013-12-03 15:57:23 +0530205
Nishanth Menon2f7d2fb2014-06-26 12:40:31 +0530206 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
Sricharan R96ca8482013-12-03 15:57:23 +0530214 of_property_read_u32(node, "ti,max-irqs", &max);
Nishanth Menonedb442d2014-06-26 12:40:27 +0530215 if (!max) {
216 pr_err("missing 'ti,max-irqs' property\n");
217 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530218 goto err_base;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530219 }
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530220 cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530221 if (!cb->irq_map)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530222 goto err_base;
Sricharan R96ca8482013-12-03 15:57:23 +0530223
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);
Dan Carpenter702f7e32014-08-07 18:28:21 +0300238 if (entry >= max) {
Sricharan R96ca8482013-12-03 15:57:23 +0530239 pr_err("Invalid reserved entry\n");
Nishanth Menonedb442d2014-06-26 12:40:27 +0530240 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530241 goto err_irq_map;
Sricharan R96ca8482013-12-03 15:57:23 +0530242 }
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530243 cb->irq_map[entry] = IRQ_RESERVED;
Sricharan R96ca8482013-12-03 15:57:23 +0530244 }
245 }
246
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530247 /* 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);
Dan Carpenter702f7e32014-08-07 18:28:21 +0300256 if (entry >= max) {
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530257 pr_err("Invalid skip entry\n");
258 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530259 goto err_irq_map;
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530260 }
261 cb->irq_map[entry] = IRQ_SKIP;
262 }
263 }
264
265
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530266 cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530267 if (!cb->register_offsets)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530268 goto err_irq_map;
Sricharan R96ca8482013-12-03 15:57:23 +0530269
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");
Nishanth Menonedb442d2014-06-26 12:40:27 +0530284 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530285 goto err_reg_offset;
Sricharan R96ca8482013-12-03 15:57:23 +0530286 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++) {
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530294 if (cb->irq_map[i] == IRQ_RESERVED)
Sricharan R96ca8482013-12-03 15:57:23 +0530295 continue;
296
297 cb->register_offsets[i] = reserved;
298 reserved += size;
299 }
300
Nishanth Menona35057d2014-06-26 12:40:22 +0530301 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
Nishanth Menona35057d2014-06-26 12:40:22 +0530302 /* 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
Marc Zyngier783d3182015-03-11 15:43:44 +0000311 raw_spin_lock_init(&cb->lock);
312
Sricharan R96ca8482013-12-03 15:57:23 +0530313 return 0;
314
Nishanth Menon3c44d512014-06-26 12:40:28 +0530315err_reg_offset:
Sricharan R96ca8482013-12-03 15:57:23 +0530316 kfree(cb->register_offsets);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530317err_irq_map:
Sricharan R96ca8482013-12-03 15:57:23 +0530318 kfree(cb->irq_map);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530319err_base:
Sricharan R96ca8482013-12-03 15:57:23 +0530320 iounmap(cb->crossbar_base);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530321err_cb:
Sricharan R96ca8482013-12-03 15:57:23 +0530322 kfree(cb);
Sricharan R99e37d0e2014-06-26 12:40:29 +0530323
324 cb = NULL;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530325 return ret;
Sricharan R96ca8482013-12-03 15:57:23 +0530326}
327
Marc Zyngier783d3182015-03-11 15:43:44 +0000328static int __init irqcrossbar_init(struct device_node *node,
329 struct device_node *parent)
Sricharan R96ca8482013-12-03 15:57:23 +0530330{
Marc Zyngier783d3182015-03-11 15:43:44 +0000331 struct irq_domain *parent_domain, *domain;
332 int err;
Sricharan R96ca8482013-12-03 15:57:23 +0530333
Marc Zyngier783d3182015-03-11 15:43:44 +0000334 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
Sricharan R96ca8482013-12-03 15:57:23 +0530358 return 0;
359}
Marc Zyngier783d3182015-03-11 15:43:44 +0000360
361IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);