blob: 0533a71fa86ff6b96148550aabca20547aa39d29 [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>
18
19#define IRQ_FREE -1
Nishanth Menon1d50d2c2014-06-26 12:40:19 +053020#define IRQ_RESERVED -2
Nishanth Menon64e0f8b2014-06-26 12:40:21 +053021#define IRQ_SKIP -3
Sricharan R96ca8482013-12-03 15:57:23 +053022#define GIC_IRQ_START 32
23
24/*
25 * @int_max: maximum number of supported interrupts
26 * @irq_map: array of interrupts to crossbar number mapping
27 * @crossbar_base: crossbar base address
28 * @register_offsets: offsets for each irq number
29 */
30struct crossbar_device {
31 uint int_max;
32 uint *irq_map;
33 void __iomem *crossbar_base;
34 int *register_offsets;
35 void (*write) (int, int);
36};
37
38static struct crossbar_device *cb;
39
40static inline void crossbar_writel(int irq_no, int cb_no)
41{
42 writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
43}
44
45static inline void crossbar_writew(int irq_no, int cb_no)
46{
47 writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
48}
49
50static inline void crossbar_writeb(int irq_no, int cb_no)
51{
52 writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
53}
54
Nishanth Menon6f16fc82014-06-26 12:40:20 +053055static inline int get_prev_map_irq(int cb_no)
56{
57 int i;
58
59 for (i = 0; i < cb->int_max; i++)
60 if (cb->irq_map[i] == cb_no)
61 return i;
62
63 return -ENODEV;
64}
65
Sricharan R96ca8482013-12-03 15:57:23 +053066static inline int allocate_free_irq(int cb_no)
67{
68 int i;
69
70 for (i = 0; i < cb->int_max; i++) {
71 if (cb->irq_map[i] == IRQ_FREE) {
72 cb->irq_map[i] = cb_no;
73 return i;
74 }
75 }
76
77 return -ENODEV;
78}
79
80static int crossbar_domain_map(struct irq_domain *d, unsigned int irq,
81 irq_hw_number_t hw)
82{
83 cb->write(hw - GIC_IRQ_START, cb->irq_map[hw - GIC_IRQ_START]);
84 return 0;
85}
86
87static void crossbar_domain_unmap(struct irq_domain *d, unsigned int irq)
88{
89 irq_hw_number_t hw = irq_get_irq_data(irq)->hwirq;
90
91 if (hw > GIC_IRQ_START)
92 cb->irq_map[hw - GIC_IRQ_START] = IRQ_FREE;
93}
94
95static int crossbar_domain_xlate(struct irq_domain *d,
96 struct device_node *controller,
97 const u32 *intspec, unsigned int intsize,
98 unsigned long *out_hwirq,
99 unsigned int *out_type)
100{
101 unsigned long ret;
102
Nishanth Menon6f16fc82014-06-26 12:40:20 +0530103 ret = get_prev_map_irq(intspec[1]);
104 if (!IS_ERR_VALUE(ret))
105 goto found;
106
Sricharan R96ca8482013-12-03 15:57:23 +0530107 ret = allocate_free_irq(intspec[1]);
108
109 if (IS_ERR_VALUE(ret))
110 return ret;
111
Nishanth Menon6f16fc82014-06-26 12:40:20 +0530112found:
Sricharan R96ca8482013-12-03 15:57:23 +0530113 *out_hwirq = ret + GIC_IRQ_START;
114 return 0;
115}
116
117const struct irq_domain_ops routable_irq_domain_ops = {
118 .map = crossbar_domain_map,
119 .unmap = crossbar_domain_unmap,
120 .xlate = crossbar_domain_xlate
121};
122
123static int __init crossbar_of_init(struct device_node *node)
124{
125 int i, size, max, reserved = 0, entry;
126 const __be32 *irqsr;
127
Dan Carpenter3894e9e2014-04-03 10:21:34 +0300128 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Sricharan R96ca8482013-12-03 15:57:23 +0530129
130 if (!cb)
131 return -ENOMEM;
132
133 cb->crossbar_base = of_iomap(node, 0);
134 if (!cb->crossbar_base)
135 goto err1;
136
137 of_property_read_u32(node, "ti,max-irqs", &max);
138 cb->irq_map = kzalloc(max * sizeof(int), GFP_KERNEL);
139 if (!cb->irq_map)
140 goto err2;
141
142 cb->int_max = max;
143
144 for (i = 0; i < max; i++)
145 cb->irq_map[i] = IRQ_FREE;
146
147 /* Get and mark reserved irqs */
148 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
149 if (irqsr) {
150 size /= sizeof(__be32);
151
152 for (i = 0; i < size; i++) {
153 of_property_read_u32_index(node,
154 "ti,irqs-reserved",
155 i, &entry);
156 if (entry > max) {
157 pr_err("Invalid reserved entry\n");
158 goto err3;
159 }
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530160 cb->irq_map[entry] = IRQ_RESERVED;
Sricharan R96ca8482013-12-03 15:57:23 +0530161 }
162 }
163
Nishanth Menon64e0f8b2014-06-26 12:40:21 +0530164 /* Skip irqs hardwired to bypass the crossbar */
165 irqsr = of_get_property(node, "ti,irqs-skip", &size);
166 if (irqsr) {
167 size /= sizeof(__be32);
168
169 for (i = 0; i < size; i++) {
170 of_property_read_u32_index(node,
171 "ti,irqs-skip",
172 i, &entry);
173 if (entry > max) {
174 pr_err("Invalid skip entry\n");
175 ret = -EINVAL;
176 goto err3;
177 }
178 cb->irq_map[entry] = IRQ_SKIP;
179 }
180 }
181
182
Sricharan R96ca8482013-12-03 15:57:23 +0530183 cb->register_offsets = kzalloc(max * sizeof(int), GFP_KERNEL);
184 if (!cb->register_offsets)
185 goto err3;
186
187 of_property_read_u32(node, "ti,reg-size", &size);
188
189 switch (size) {
190 case 1:
191 cb->write = crossbar_writeb;
192 break;
193 case 2:
194 cb->write = crossbar_writew;
195 break;
196 case 4:
197 cb->write = crossbar_writel;
198 break;
199 default:
200 pr_err("Invalid reg-size property\n");
201 goto err4;
202 break;
203 }
204
205 /*
206 * Register offsets are not linear because of the
207 * reserved irqs. so find and store the offsets once.
208 */
209 for (i = 0; i < max; i++) {
Nishanth Menon1d50d2c2014-06-26 12:40:19 +0530210 if (cb->irq_map[i] == IRQ_RESERVED)
Sricharan R96ca8482013-12-03 15:57:23 +0530211 continue;
212
213 cb->register_offsets[i] = reserved;
214 reserved += size;
215 }
216
217 register_routable_domain_ops(&routable_irq_domain_ops);
218 return 0;
219
220err4:
221 kfree(cb->register_offsets);
222err3:
223 kfree(cb->irq_map);
224err2:
225 iounmap(cb->crossbar_base);
226err1:
227 kfree(cb);
228 return -ENOMEM;
229}
230
231static const struct of_device_id crossbar_match[] __initconst = {
232 { .compatible = "ti,irq-crossbar" },
233 {}
234};
235
236int __init irqcrossbar_init(void)
237{
238 struct device_node *np;
239 np = of_find_matching_node(NULL, crossbar_match);
240 if (!np)
241 return -ENODEV;
242
243 crossbar_of_init(np);
244 return 0;
245}