blob: f7eda9f5997eb514948f6481c31cf756df54f36d [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>
14#include <linux/of_address.h>
15#include <linux/of_irq.h>
16#include <linux/slab.h>
17#include <linux/irqchip/arm-gic.h>
Nishanth Menon4dbf45e2014-06-26 12:40:25 +053018#include <linux/irqchip/irq-crossbar.h>
Sricharan R96ca8482013-12-03 15:57:23 +053019
20#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
25/*
26 * @int_max: maximum number of supported interrupts
Nishanth Menona35057d2014-06-26 12:40:22 +053027 * @safe_map: safe default value to initialize the crossbar
Sricharan R96ca8482013-12-03 15:57:23 +053028 * @irq_map: array of interrupts to crossbar number mapping
29 * @crossbar_base: crossbar base address
30 * @register_offsets: offsets for each irq number
31 */
32struct crossbar_device {
33 uint int_max;
Nishanth Menona35057d2014-06-26 12:40:22 +053034 uint safe_map;
Sricharan R96ca8482013-12-03 15:57:23 +053035 uint *irq_map;
36 void __iomem *crossbar_base;
37 int *register_offsets;
Nishanth Menona35057d2014-06-26 12:40:22 +053038 void (*write)(int, int);
Sricharan R96ca8482013-12-03 15:57:23 +053039};
40
41static struct crossbar_device *cb;
42
43static inline void crossbar_writel(int irq_no, int cb_no)
44{
45 writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
46}
47
48static inline void crossbar_writew(int irq_no, int cb_no)
49{
50 writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
51}
52
53static inline void crossbar_writeb(int irq_no, int cb_no)
54{
55 writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
56}
57
Nishanth Menon6f16fc82014-06-26 12:40:20 +053058static inline int get_prev_map_irq(int cb_no)
59{
60 int i;
61
Nishanth Menonddee0fb2014-06-26 12:40:23 +053062 for (i = cb->int_max - 1; i >= 0; i--)
Nishanth Menon6f16fc82014-06-26 12:40:20 +053063 if (cb->irq_map[i] == cb_no)
64 return i;
65
66 return -ENODEV;
67}
68
Sricharan R96ca8482013-12-03 15:57:23 +053069static inline int allocate_free_irq(int cb_no)
70{
71 int i;
72
Nishanth Menonddee0fb2014-06-26 12:40:23 +053073 for (i = cb->int_max - 1; i >= 0; i--) {
Sricharan R96ca8482013-12-03 15:57:23 +053074 if (cb->irq_map[i] == IRQ_FREE) {
75 cb->irq_map[i] = cb_no;
76 return i;
77 }
78 }
79
80 return -ENODEV;
81}
82
83static int crossbar_domain_map(struct irq_domain *d, unsigned int irq,
84 irq_hw_number_t hw)
85{
86 cb->write(hw - GIC_IRQ_START, cb->irq_map[hw - GIC_IRQ_START]);
87 return 0;
88}
89
90static void crossbar_domain_unmap(struct irq_domain *d, unsigned int irq)
91{
92 irq_hw_number_t hw = irq_get_irq_data(irq)->hwirq;
93
Nishanth Menona35057d2014-06-26 12:40:22 +053094 if (hw > GIC_IRQ_START) {
Sricharan R96ca8482013-12-03 15:57:23 +053095 cb->irq_map[hw - GIC_IRQ_START] = IRQ_FREE;
Nishanth Menona35057d2014-06-26 12:40:22 +053096 cb->write(hw - GIC_IRQ_START, cb->safe_map);
97 }
Sricharan R96ca8482013-12-03 15:57:23 +053098}
99
100static int crossbar_domain_xlate(struct irq_domain *d,
101 struct device_node *controller,
102 const u32 *intspec, unsigned int intsize,
103 unsigned long *out_hwirq,
104 unsigned int *out_type)
105{
Nishanth Menond4922a92014-06-26 12:40:24 +0530106 int ret;
Sricharan R96ca8482013-12-03 15:57:23 +0530107
Nishanth Menon6f16fc82014-06-26 12:40:20 +0530108 ret = get_prev_map_irq(intspec[1]);
Nishanth Menond4922a92014-06-26 12:40:24 +0530109 if (ret >= 0)
Nishanth Menon6f16fc82014-06-26 12:40:20 +0530110 goto found;
111
Sricharan R96ca8482013-12-03 15:57:23 +0530112 ret = allocate_free_irq(intspec[1]);
113
Nishanth Menond4922a92014-06-26 12:40:24 +0530114 if (ret < 0)
Sricharan R96ca8482013-12-03 15:57:23 +0530115 return ret;
116
Nishanth Menon6f16fc82014-06-26 12:40:20 +0530117found:
Sricharan R96ca8482013-12-03 15:57:23 +0530118 *out_hwirq = ret + GIC_IRQ_START;
119 return 0;
120}
121
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530122static const struct irq_domain_ops routable_irq_domain_ops = {
Sricharan R96ca8482013-12-03 15:57:23 +0530123 .map = crossbar_domain_map,
124 .unmap = crossbar_domain_unmap,
125 .xlate = crossbar_domain_xlate
126};
127
128static int __init crossbar_of_init(struct device_node *node)
129{
130 int i, size, max, reserved = 0, entry;
131 const __be32 *irqsr;
132
Dan Carpenter3894e9e2014-04-03 10:21:34 +0300133 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530134
135 if (!cb)
136 return -ENOMEM;
137
138 cb->crossbar_base = of_iomap(node, 0);
139 if (!cb->crossbar_base)
140 goto err1;
141
142 of_property_read_u32(node, "ti,max-irqs", &max);
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530143 cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530144 if (!cb->irq_map)
145 goto err2;
146
147 cb->int_max = max;
148
149 for (i = 0; i < max; i++)
150 cb->irq_map[i] = IRQ_FREE;
151
152 /* Get and mark reserved irqs */
153 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
154 if (irqsr) {
155 size /= sizeof(__be32);
156
157 for (i = 0; i < size; i++) {
158 of_property_read_u32_index(node,
159 "ti,irqs-reserved",
160 i, &entry);
161 if (entry > max) {
162 pr_err("Invalid reserved entry\n");
163 goto err3;
164 }
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530165 cb->irq_map[entry] = IRQ_RESERVED;
Sricharan R96ca8482013-12-03 15:57:23 +0530166 }
167 }
168
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530169 /* Skip irqs hardwired to bypass the crossbar */
170 irqsr = of_get_property(node, "ti,irqs-skip", &size);
171 if (irqsr) {
172 size /= sizeof(__be32);
173
174 for (i = 0; i < size; i++) {
175 of_property_read_u32_index(node,
176 "ti,irqs-skip",
177 i, &entry);
178 if (entry > max) {
179 pr_err("Invalid skip entry\n");
180 ret = -EINVAL;
181 goto err3;
182 }
183 cb->irq_map[entry] = IRQ_SKIP;
184 }
185 }
186
187
Nishanth Menon4dbf45e2014-06-26 12:40:25 +0530188 cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530189 if (!cb->register_offsets)
190 goto err3;
191
192 of_property_read_u32(node, "ti,reg-size", &size);
193
194 switch (size) {
195 case 1:
196 cb->write = crossbar_writeb;
197 break;
198 case 2:
199 cb->write = crossbar_writew;
200 break;
201 case 4:
202 cb->write = crossbar_writel;
203 break;
204 default:
205 pr_err("Invalid reg-size property\n");
206 goto err4;
207 break;
208 }
209
210 /*
211 * Register offsets are not linear because of the
212 * reserved irqs. so find and store the offsets once.
213 */
214 for (i = 0; i < max; i++) {
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530215 if (cb->irq_map[i] == IRQ_RESERVED)
Sricharan R96ca8482013-12-03 15:57:23 +0530216 continue;
217
218 cb->register_offsets[i] = reserved;
219 reserved += size;
220 }
221
Nishanth Menona35057d2014-06-26 12:40:22 +0530222 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
223
224 /* Initialize the crossbar with safe map to start with */
225 for (i = 0; i < max; i++) {
226 if (cb->irq_map[i] == IRQ_RESERVED ||
227 cb->irq_map[i] == IRQ_SKIP)
228 continue;
229
230 cb->write(i, cb->safe_map);
231 }
232
Sricharan R96ca8482013-12-03 15:57:23 +0530233 register_routable_domain_ops(&routable_irq_domain_ops);
234 return 0;
235
236err4:
237 kfree(cb->register_offsets);
238err3:
239 kfree(cb->irq_map);
240err2:
241 iounmap(cb->crossbar_base);
242err1:
243 kfree(cb);
244 return -ENOMEM;
245}
246
247static const struct of_device_id crossbar_match[] __initconst = {
248 { .compatible = "ti,irq-crossbar" },
249 {}
250};
251
252int __init irqcrossbar_init(void)
253{
254 struct device_node *np;
255 np = of_find_matching_node(NULL, crossbar_match);
256 if (!np)
257 return -ENODEV;
258
259 crossbar_of_init(np);
260 return 0;
261}