blob: 692fe2bc81979b6b48f984ec6d88c117fbfdebdd [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,
72#ifdef CONFIG_SMP
73 .irq_set_affinity = irq_chip_set_affinity_parent,
74#endif
75};
76
77static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
78 irq_hw_number_t hwirq)
Nishanth Menon6f16fc82014-06-26 12:40:20 +053079{
Marc Zyngier783d3182015-03-11 15:43:44 +000080 struct of_phandle_args args;
Nishanth Menon6f16fc82014-06-26 12:40:20 +053081 int i;
Marc Zyngier783d3182015-03-11 15:43:44 +000082 int err;
Nishanth Menon6f16fc82014-06-26 12:40:20 +053083
Marc Zyngier783d3182015-03-11 15:43:44 +000084 raw_spin_lock(&cb->lock);
Nishanth Menonddee0fb2014-06-26 12:40:23 +053085 for (i = cb->int_max - 1; i >= 0; i--) {
Sricharan R96ca8482013-12-03 15:57:23 +053086 if (cb->irq_map[i] == IRQ_FREE) {
Marc Zyngier783d3182015-03-11 15:43:44 +000087 cb->irq_map[i] = hwirq;
88 break;
Sricharan R96ca8482013-12-03 15:57:23 +053089 }
90 }
Marc Zyngier783d3182015-03-11 15:43:44 +000091 raw_spin_unlock(&cb->lock);
Sricharan R96ca8482013-12-03 15:57:23 +053092
Marc Zyngier783d3182015-03-11 15:43:44 +000093 if (i < 0)
94 return -ENODEV;
95
96 args.np = domain->parent->of_node;
97 args.args_count = 3;
98 args.args[0] = 0; /* SPI */
99 args.args[1] = i;
100 args.args[2] = IRQ_TYPE_LEVEL_HIGH;
101
102 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &args);
103 if (err)
104 cb->irq_map[i] = IRQ_FREE;
105 else
106 cb->write(i, hwirq);
107
108 return err;
Sricharan R96ca8482013-12-03 15:57:23 +0530109}
110
Marc Zyngier783d3182015-03-11 15:43:44 +0000111static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
112 unsigned int nr_irqs, void *data)
Nishanth Menon29918b62014-06-26 12:40:32 +0530113{
Marc Zyngier783d3182015-03-11 15:43:44 +0000114 struct of_phandle_args *args = data;
115 irq_hw_number_t hwirq;
116 int i;
Nishanth Menond3608922014-06-26 12:40:34 +0530117
Marc Zyngier783d3182015-03-11 15:43:44 +0000118 if (args->args_count != 3)
119 return -EINVAL; /* Not GIC compliant */
120 if (args->args[0] != 0)
121 return -EINVAL; /* No PPI should point to this domain */
122
123 hwirq = args->args[1];
124 if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
125 return -EINVAL; /* Can't deal with this */
126
127 for (i = 0; i < nr_irqs; i++) {
128 int err = allocate_gic_irq(d, virq + i, hwirq + i);
129
130 if (err)
131 return err;
132
133 irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
134 &crossbar_chip, NULL);
Nishanth Menond3608922014-06-26 12:40:34 +0530135 }
Nishanth Menon29918b62014-06-26 12:40:32 +0530136
Sricharan R96ca8482013-12-03 15:57:23 +0530137 return 0;
138}
139
Sricharan R8b09a452014-06-26 12:40:30 +0530140/**
Marc Zyngier783d3182015-03-11 15:43:44 +0000141 * crossbar_domain_free - unmap/free a crossbar<->irq connection
142 * @domain: domain of irq to unmap
143 * @virq: virq number
144 * @nr_irqs: number of irqs to free
Sricharan R8b09a452014-06-26 12:40:30 +0530145 *
146 * We do not maintain a use count of total number of map/unmap
147 * calls for a particular irq to find out if a irq can be really
148 * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
149 * after which irq is anyways unusable. So an explicit map has to be called
150 * after that.
151 */
Marc Zyngier783d3182015-03-11 15:43:44 +0000152static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
153 unsigned int nr_irqs)
Sricharan R96ca8482013-12-03 15:57:23 +0530154{
Marc Zyngier783d3182015-03-11 15:43:44 +0000155 int i;
Sricharan R96ca8482013-12-03 15:57:23 +0530156
Marc Zyngier783d3182015-03-11 15:43:44 +0000157 raw_spin_lock(&cb->lock);
158 for (i = 0; i < nr_irqs; i++) {
159 struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
160
161 irq_domain_reset_irq_data(d);
162 cb->irq_map[d->hwirq] = IRQ_FREE;
163 cb->write(d->hwirq, cb->safe_map);
Nishanth Menona35057d2014-06-26 12:40:22 +0530164 }
Marc Zyngier783d3182015-03-11 15:43:44 +0000165 raw_spin_unlock(&cb->lock);
Sricharan R96ca8482013-12-03 15:57:23 +0530166}
167
168static int crossbar_domain_xlate(struct irq_domain *d,
169 struct device_node *controller,
170 const u32 *intspec, unsigned int intsize,
171 unsigned long *out_hwirq,
172 unsigned int *out_type)
173{
Marc Zyngier783d3182015-03-11 15:43:44 +0000174 if (d->of_node != controller)
175 return -EINVAL; /* Shouldn't happen, really... */
176 if (intsize != 3)
177 return -EINVAL; /* Not GIC compliant */
178 if (intspec[0] != 0)
179 return -EINVAL; /* No PPI should point to this domain */
Sricharan R96ca8482013-12-03 15:57:23 +0530180
Marc Zyngier783d3182015-03-11 15:43:44 +0000181 *out_hwirq = intspec[1];
182 *out_type = intspec[2];
Sricharan R96ca8482013-12-03 15:57:23 +0530183 return 0;
184}
185
Marc Zyngier783d3182015-03-11 15:43:44 +0000186static const struct irq_domain_ops crossbar_domain_ops = {
187 .alloc = crossbar_domain_alloc,
188 .free = crossbar_domain_free,
189 .xlate = crossbar_domain_xlate,
Sricharan R96ca8482013-12-03 15:57:23 +0530190};
191
192static int __init crossbar_of_init(struct device_node *node)
193{
Nishanth Menonedb442d2014-06-26 12:40:27 +0530194 int i, size, max = 0, reserved = 0, entry;
Sricharan R96ca8482013-12-03 15:57:23 +0530195 const __be32 *irqsr;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530196 int ret = -ENOMEM;
Sricharan R96ca8482013-12-03 15:57:23 +0530197
Dan Carpenter3894e9e2014-04-03 10:21:34 +0300198 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530199
200 if (!cb)
Nishanth Menonedb442d2014-06-26 12:40:27 +0530201 return ret;
Sricharan R96ca8482013-12-03 15:57:23 +0530202
203 cb->crossbar_base = of_iomap(node, 0);
204 if (!cb->crossbar_base)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530205 goto err_cb;
Sricharan R96ca8482013-12-03 15:57:23 +0530206
Nishanth Menon2f7d2fb2014-06-26 12:40:31 +0530207 of_property_read_u32(node, "ti,max-crossbar-sources",
208 &cb->max_crossbar_sources);
209 if (!cb->max_crossbar_sources) {
210 pr_err("missing 'ti,max-crossbar-sources' property\n");
211 ret = -EINVAL;
212 goto err_base;
213 }
214
Sricharan R96ca8482013-12-03 15:57:23 +0530215 of_property_read_u32(node, "ti,max-irqs", &max);
Nishanth Menonedb442d2014-06-26 12:40:27 +0530216 if (!max) {
217 pr_err("missing 'ti,max-irqs' property\n");
218 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530219 goto err_base;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530220 }
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530221 cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530222 if (!cb->irq_map)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530223 goto err_base;
Sricharan R96ca8482013-12-03 15:57:23 +0530224
225 cb->int_max = max;
226
227 for (i = 0; i < max; i++)
228 cb->irq_map[i] = IRQ_FREE;
229
230 /* Get and mark reserved irqs */
231 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
232 if (irqsr) {
233 size /= sizeof(__be32);
234
235 for (i = 0; i < size; i++) {
236 of_property_read_u32_index(node,
237 "ti,irqs-reserved",
238 i, &entry);
Dan Carpenter702f7e32014-08-07 18:28:21 +0300239 if (entry >= max) {
Sricharan R96ca8482013-12-03 15:57:23 +0530240 pr_err("Invalid reserved entry\n");
Nishanth Menonedb442d2014-06-26 12:40:27 +0530241 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530242 goto err_irq_map;
Sricharan R96ca8482013-12-03 15:57:23 +0530243 }
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530244 cb->irq_map[entry] = IRQ_RESERVED;
Sricharan R96ca8482013-12-03 15:57:23 +0530245 }
246 }
247
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530248 /* Skip irqs hardwired to bypass the crossbar */
249 irqsr = of_get_property(node, "ti,irqs-skip", &size);
250 if (irqsr) {
251 size /= sizeof(__be32);
252
253 for (i = 0; i < size; i++) {
254 of_property_read_u32_index(node,
255 "ti,irqs-skip",
256 i, &entry);
Dan Carpenter702f7e32014-08-07 18:28:21 +0300257 if (entry >= max) {
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530258 pr_err("Invalid skip entry\n");
259 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530260 goto err_irq_map;
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530261 }
262 cb->irq_map[entry] = IRQ_SKIP;
263 }
264 }
265
266
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530267 cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530268 if (!cb->register_offsets)
Nishanth Menon3c44d512014-06-26 12:40:28 +0530269 goto err_irq_map;
Sricharan R96ca8482013-12-03 15:57:23 +0530270
271 of_property_read_u32(node, "ti,reg-size", &size);
272
273 switch (size) {
274 case 1:
275 cb->write = crossbar_writeb;
276 break;
277 case 2:
278 cb->write = crossbar_writew;
279 break;
280 case 4:
281 cb->write = crossbar_writel;
282 break;
283 default:
284 pr_err("Invalid reg-size property\n");
Nishanth Menonedb442d2014-06-26 12:40:27 +0530285 ret = -EINVAL;
Nishanth Menon3c44d512014-06-26 12:40:28 +0530286 goto err_reg_offset;
Sricharan R96ca8482013-12-03 15:57:23 +0530287 break;
288 }
289
290 /*
291 * Register offsets are not linear because of the
292 * reserved irqs. so find and store the offsets once.
293 */
294 for (i = 0; i < max; i++) {
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530295 if (cb->irq_map[i] == IRQ_RESERVED)
Sricharan R96ca8482013-12-03 15:57:23 +0530296 continue;
297
298 cb->register_offsets[i] = reserved;
299 reserved += size;
300 }
301
Nishanth Menona35057d2014-06-26 12:40:22 +0530302 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
Nishanth Menona35057d2014-06-26 12:40:22 +0530303 /* Initialize the crossbar with safe map to start with */
304 for (i = 0; i < max; i++) {
305 if (cb->irq_map[i] == IRQ_RESERVED ||
306 cb->irq_map[i] == IRQ_SKIP)
307 continue;
308
309 cb->write(i, cb->safe_map);
310 }
311
Marc Zyngier783d3182015-03-11 15:43:44 +0000312 raw_spin_lock_init(&cb->lock);
313
Sricharan R96ca8482013-12-03 15:57:23 +0530314 return 0;
315
Nishanth Menon3c44d512014-06-26 12:40:28 +0530316err_reg_offset:
Sricharan R96ca8482013-12-03 15:57:23 +0530317 kfree(cb->register_offsets);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530318err_irq_map:
Sricharan R96ca8482013-12-03 15:57:23 +0530319 kfree(cb->irq_map);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530320err_base:
Sricharan R96ca8482013-12-03 15:57:23 +0530321 iounmap(cb->crossbar_base);
Nishanth Menon3c44d512014-06-26 12:40:28 +0530322err_cb:
Sricharan R96ca8482013-12-03 15:57:23 +0530323 kfree(cb);
Sricharan R99e37d0e2014-06-26 12:40:29 +0530324
325 cb = NULL;
Nishanth Menonedb442d2014-06-26 12:40:27 +0530326 return ret;
Sricharan R96ca8482013-12-03 15:57:23 +0530327}
328
Marc Zyngier783d3182015-03-11 15:43:44 +0000329static int __init irqcrossbar_init(struct device_node *node,
330 struct device_node *parent)
Sricharan R96ca8482013-12-03 15:57:23 +0530331{
Marc Zyngier783d3182015-03-11 15:43:44 +0000332 struct irq_domain *parent_domain, *domain;
333 int err;
Sricharan R96ca8482013-12-03 15:57:23 +0530334
Marc Zyngier783d3182015-03-11 15:43:44 +0000335 if (!parent) {
336 pr_err("%s: no parent, giving up\n", node->full_name);
337 return -ENODEV;
338 }
339
340 parent_domain = irq_find_host(parent);
341 if (!parent_domain) {
342 pr_err("%s: unable to obtain parent domain\n", node->full_name);
343 return -ENXIO;
344 }
345
346 err = crossbar_of_init(node);
347 if (err)
348 return err;
349
350 domain = irq_domain_add_hierarchy(parent_domain, 0,
351 cb->max_crossbar_sources,
352 node, &crossbar_domain_ops,
353 NULL);
354 if (!domain) {
355 pr_err("%s: failed to allocated domain\n", node->full_name);
356 return -ENOMEM;
357 }
358
Sricharan R96ca8482013-12-03 15:57:23 +0530359 return 0;
360}
Marc Zyngier783d3182015-03-11 15:43:44 +0000361
362IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);