]>
Commit | Line | Data |
---|---|---|
96ca848e S |
1 | /* |
2 | * drivers/irqchip/irq-crossbar.c | |
3 | * | |
4 | * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com | |
5 | * Author: Sricharan R <[email protected]> | |
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> | |
41a83e06 | 14 | #include <linux/irqchip.h> |
783d3186 | 15 | #include <linux/irqdomain.h> |
96ca848e S |
16 | #include <linux/of_address.h> |
17 | #include <linux/of_irq.h> | |
18 | #include <linux/slab.h> | |
783d3186 | 19 | |
96ca848e | 20 | #define IRQ_FREE -1 |
1d50d2ce | 21 | #define IRQ_RESERVED -2 |
64e0f8ba | 22 | #define IRQ_SKIP -3 |
96ca848e S |
23 | #define GIC_IRQ_START 32 |
24 | ||
e30ef8ab NM |
25 | /** |
26 | * struct crossbar_device - crossbar device description | |
783d3186 | 27 | * @lock: spinlock serializing access to @irq_map |
96ca848e | 28 | * @int_max: maximum number of supported interrupts |
a35057d1 | 29 | * @safe_map: safe default value to initialize the crossbar |
2f7d2fb7 | 30 | * @max_crossbar_sources: Maximum number of crossbar sources |
96ca848e S |
31 | * @irq_map: array of interrupts to crossbar number mapping |
32 | * @crossbar_base: crossbar base address | |
33 | * @register_offsets: offsets for each irq number | |
e30ef8ab | 34 | * @write: register write function pointer |
96ca848e S |
35 | */ |
36 | struct crossbar_device { | |
783d3186 | 37 | raw_spinlock_t lock; |
96ca848e | 38 | uint int_max; |
a35057d1 | 39 | uint safe_map; |
2f7d2fb7 | 40 | uint max_crossbar_sources; |
96ca848e S |
41 | uint *irq_map; |
42 | void __iomem *crossbar_base; | |
43 | int *register_offsets; | |
a35057d1 | 44 | void (*write)(int, int); |
96ca848e S |
45 | }; |
46 | ||
47 | static struct crossbar_device *cb; | |
48 | ||
783d3186 | 49 | static void crossbar_writel(int irq_no, int cb_no) |
96ca848e S |
50 | { |
51 | writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); | |
52 | } | |
53 | ||
783d3186 | 54 | static void crossbar_writew(int irq_no, int cb_no) |
96ca848e S |
55 | { |
56 | writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); | |
57 | } | |
58 | ||
783d3186 | 59 | static void crossbar_writeb(int irq_no, int cb_no) |
96ca848e S |
60 | { |
61 | writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); | |
62 | } | |
63 | ||
783d3186 MZ |
64 | static 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, | |
e269ec42 | 70 | .irq_set_type = irq_chip_set_type_parent, |
8200fe43 GS |
71 | .flags = IRQCHIP_MASK_ON_SUSPEND | |
72 | IRQCHIP_SKIP_SET_WAKE, | |
783d3186 MZ |
73 | #ifdef CONFIG_SMP |
74 | .irq_set_affinity = irq_chip_set_affinity_parent, | |
75 | #endif | |
76 | }; | |
6f16fc87 | 77 | |
783d3186 MZ |
78 | static int allocate_gic_irq(struct irq_domain *domain, unsigned virq, |
79 | irq_hw_number_t hwirq) | |
96ca848e | 80 | { |
f833f57f | 81 | struct irq_fwspec fwspec; |
96ca848e | 82 | int i; |
783d3186 | 83 | int err; |
96ca848e | 84 | |
f833f57f MZ |
85 | if (!irq_domain_get_of_node(domain->parent)) |
86 | return -EINVAL; | |
87 | ||
783d3186 | 88 | raw_spin_lock(&cb->lock); |
ddee0fb4 | 89 | for (i = cb->int_max - 1; i >= 0; i--) { |
96ca848e | 90 | if (cb->irq_map[i] == IRQ_FREE) { |
783d3186 MZ |
91 | cb->irq_map[i] = hwirq; |
92 | break; | |
96ca848e S |
93 | } |
94 | } | |
783d3186 | 95 | raw_spin_unlock(&cb->lock); |
96ca848e | 96 | |
783d3186 MZ |
97 | if (i < 0) |
98 | return -ENODEV; | |
96ca848e | 99 | |
f833f57f MZ |
100 | fwspec.fwnode = domain->parent->fwnode; |
101 | fwspec.param_count = 3; | |
102 | fwspec.param[0] = 0; /* SPI */ | |
103 | fwspec.param[1] = i; | |
104 | fwspec.param[2] = IRQ_TYPE_LEVEL_HIGH; | |
d360892d | 105 | |
f833f57f | 106 | err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec); |
783d3186 MZ |
107 | if (err) |
108 | cb->irq_map[i] = IRQ_FREE; | |
109 | else | |
110 | cb->write(i, hwirq); | |
29918b67 | 111 | |
783d3186 | 112 | return err; |
29918b67 NM |
113 | } |
114 | ||
783d3186 MZ |
115 | static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq, |
116 | unsigned int nr_irqs, void *data) | |
96ca848e | 117 | { |
f833f57f | 118 | struct irq_fwspec *fwspec = data; |
783d3186 MZ |
119 | irq_hw_number_t hwirq; |
120 | int i; | |
121 | ||
f833f57f | 122 | if (fwspec->param_count != 3) |
783d3186 | 123 | return -EINVAL; /* Not GIC compliant */ |
f833f57f | 124 | if (fwspec->param[0] != 0) |
783d3186 MZ |
125 | return -EINVAL; /* No PPI should point to this domain */ |
126 | ||
f833f57f | 127 | hwirq = fwspec->param[1]; |
783d3186 MZ |
128 | if ((hwirq + nr_irqs) > cb->max_crossbar_sources) |
129 | return -EINVAL; /* Can't deal with this */ | |
130 | ||
131 | for (i = 0; i < nr_irqs; i++) { | |
132 | int err = allocate_gic_irq(d, virq + i, hwirq + i); | |
133 | ||
134 | if (err) | |
135 | return err; | |
136 | ||
137 | irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i, | |
138 | &crossbar_chip, NULL); | |
139 | } | |
29918b67 | 140 | |
96ca848e S |
141 | return 0; |
142 | } | |
143 | ||
8b09a45d | 144 | /** |
783d3186 MZ |
145 | * crossbar_domain_free - unmap/free a crossbar<->irq connection |
146 | * @domain: domain of irq to unmap | |
147 | * @virq: virq number | |
148 | * @nr_irqs: number of irqs to free | |
8b09a45d S |
149 | * |
150 | * We do not maintain a use count of total number of map/unmap | |
151 | * calls for a particular irq to find out if a irq can be really | |
152 | * unmapped. This is because unmap is called during irq_dispose_mapping(irq), | |
153 | * after which irq is anyways unusable. So an explicit map has to be called | |
154 | * after that. | |
155 | */ | |
783d3186 MZ |
156 | static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq, |
157 | unsigned int nr_irqs) | |
96ca848e | 158 | { |
783d3186 | 159 | int i; |
96ca848e | 160 | |
783d3186 MZ |
161 | raw_spin_lock(&cb->lock); |
162 | for (i = 0; i < nr_irqs; i++) { | |
163 | struct irq_data *d = irq_domain_get_irq_data(domain, virq + i); | |
164 | ||
165 | irq_domain_reset_irq_data(d); | |
166 | cb->irq_map[d->hwirq] = IRQ_FREE; | |
167 | cb->write(d->hwirq, cb->safe_map); | |
a35057d1 | 168 | } |
783d3186 | 169 | raw_spin_unlock(&cb->lock); |
96ca848e S |
170 | } |
171 | ||
f833f57f MZ |
172 | static int crossbar_domain_translate(struct irq_domain *d, |
173 | struct irq_fwspec *fwspec, | |
174 | unsigned long *hwirq, | |
175 | unsigned int *type) | |
96ca848e | 176 | { |
f833f57f MZ |
177 | if (is_of_node(fwspec->fwnode)) { |
178 | if (fwspec->param_count != 3) | |
179 | return -EINVAL; | |
783d3186 | 180 | |
f833f57f MZ |
181 | /* No PPI should point to this domain */ |
182 | if (fwspec->param[0] != 0) | |
183 | return -EINVAL; | |
184 | ||
185 | *hwirq = fwspec->param[1]; | |
a2a8fa55 | 186 | *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK; |
f833f57f MZ |
187 | return 0; |
188 | } | |
189 | ||
190 | return -EINVAL; | |
96ca848e S |
191 | } |
192 | ||
783d3186 | 193 | static const struct irq_domain_ops crossbar_domain_ops = { |
f833f57f MZ |
194 | .alloc = crossbar_domain_alloc, |
195 | .free = crossbar_domain_free, | |
196 | .translate = crossbar_domain_translate, | |
96ca848e S |
197 | }; |
198 | ||
199 | static int __init crossbar_of_init(struct device_node *node) | |
200 | { | |
edb442de | 201 | int i, size, max = 0, reserved = 0, entry; |
96ca848e | 202 | const __be32 *irqsr; |
edb442de | 203 | int ret = -ENOMEM; |
96ca848e | 204 | |
3894e9e8 | 205 | cb = kzalloc(sizeof(*cb), GFP_KERNEL); |
96ca848e S |
206 | |
207 | if (!cb) | |
edb442de | 208 | return ret; |
96ca848e S |
209 | |
210 | cb->crossbar_base = of_iomap(node, 0); | |
211 | if (!cb->crossbar_base) | |
3c44d515 | 212 | goto err_cb; |
96ca848e | 213 | |
2f7d2fb7 NM |
214 | of_property_read_u32(node, "ti,max-crossbar-sources", |
215 | &cb->max_crossbar_sources); | |
216 | if (!cb->max_crossbar_sources) { | |
217 | pr_err("missing 'ti,max-crossbar-sources' property\n"); | |
218 | ret = -EINVAL; | |
219 | goto err_base; | |
220 | } | |
221 | ||
96ca848e | 222 | of_property_read_u32(node, "ti,max-irqs", &max); |
edb442de NM |
223 | if (!max) { |
224 | pr_err("missing 'ti,max-irqs' property\n"); | |
225 | ret = -EINVAL; | |
3c44d515 | 226 | goto err_base; |
edb442de | 227 | } |
4dbf45e3 | 228 | cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL); |
96ca848e | 229 | if (!cb->irq_map) |
3c44d515 | 230 | goto err_base; |
96ca848e S |
231 | |
232 | cb->int_max = max; | |
233 | ||
234 | for (i = 0; i < max; i++) | |
235 | cb->irq_map[i] = IRQ_FREE; | |
236 | ||
237 | /* Get and mark reserved irqs */ | |
238 | irqsr = of_get_property(node, "ti,irqs-reserved", &size); | |
239 | if (irqsr) { | |
240 | size /= sizeof(__be32); | |
241 | ||
242 | for (i = 0; i < size; i++) { | |
243 | of_property_read_u32_index(node, | |
244 | "ti,irqs-reserved", | |
245 | i, &entry); | |
702f7e36 | 246 | if (entry >= max) { |
96ca848e | 247 | pr_err("Invalid reserved entry\n"); |
edb442de | 248 | ret = -EINVAL; |
3c44d515 | 249 | goto err_irq_map; |
96ca848e | 250 | } |
1d50d2ce | 251 | cb->irq_map[entry] = IRQ_RESERVED; |
96ca848e S |
252 | } |
253 | } | |
254 | ||
64e0f8ba NM |
255 | /* Skip irqs hardwired to bypass the crossbar */ |
256 | irqsr = of_get_property(node, "ti,irqs-skip", &size); | |
257 | if (irqsr) { | |
258 | size /= sizeof(__be32); | |
259 | ||
260 | for (i = 0; i < size; i++) { | |
261 | of_property_read_u32_index(node, | |
262 | "ti,irqs-skip", | |
263 | i, &entry); | |
702f7e36 | 264 | if (entry >= max) { |
64e0f8ba NM |
265 | pr_err("Invalid skip entry\n"); |
266 | ret = -EINVAL; | |
3c44d515 | 267 | goto err_irq_map; |
64e0f8ba NM |
268 | } |
269 | cb->irq_map[entry] = IRQ_SKIP; | |
270 | } | |
271 | } | |
272 | ||
273 | ||
4dbf45e3 | 274 | cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL); |
96ca848e | 275 | if (!cb->register_offsets) |
3c44d515 | 276 | goto err_irq_map; |
96ca848e S |
277 | |
278 | of_property_read_u32(node, "ti,reg-size", &size); | |
279 | ||
280 | switch (size) { | |
281 | case 1: | |
282 | cb->write = crossbar_writeb; | |
283 | break; | |
284 | case 2: | |
285 | cb->write = crossbar_writew; | |
286 | break; | |
287 | case 4: | |
288 | cb->write = crossbar_writel; | |
289 | break; | |
290 | default: | |
291 | pr_err("Invalid reg-size property\n"); | |
edb442de | 292 | ret = -EINVAL; |
3c44d515 | 293 | goto err_reg_offset; |
96ca848e S |
294 | break; |
295 | } | |
296 | ||
297 | /* | |
298 | * Register offsets are not linear because of the | |
299 | * reserved irqs. so find and store the offsets once. | |
300 | */ | |
301 | for (i = 0; i < max; i++) { | |
1d50d2ce | 302 | if (cb->irq_map[i] == IRQ_RESERVED) |
96ca848e S |
303 | continue; |
304 | ||
305 | cb->register_offsets[i] = reserved; | |
306 | reserved += size; | |
307 | } | |
308 | ||
a35057d1 | 309 | of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map); |
a35057d1 NM |
310 | /* Initialize the crossbar with safe map to start with */ |
311 | for (i = 0; i < max; i++) { | |
312 | if (cb->irq_map[i] == IRQ_RESERVED || | |
313 | cb->irq_map[i] == IRQ_SKIP) | |
314 | continue; | |
315 | ||
316 | cb->write(i, cb->safe_map); | |
317 | } | |
318 | ||
783d3186 MZ |
319 | raw_spin_lock_init(&cb->lock); |
320 | ||
96ca848e S |
321 | return 0; |
322 | ||
3c44d515 | 323 | err_reg_offset: |
96ca848e | 324 | kfree(cb->register_offsets); |
3c44d515 | 325 | err_irq_map: |
96ca848e | 326 | kfree(cb->irq_map); |
3c44d515 | 327 | err_base: |
96ca848e | 328 | iounmap(cb->crossbar_base); |
3c44d515 | 329 | err_cb: |
96ca848e | 330 | kfree(cb); |
99e37d0e S |
331 | |
332 | cb = NULL; | |
edb442de | 333 | return ret; |
96ca848e S |
334 | } |
335 | ||
783d3186 MZ |
336 | static int __init irqcrossbar_init(struct device_node *node, |
337 | struct device_node *parent) | |
96ca848e | 338 | { |
783d3186 MZ |
339 | struct irq_domain *parent_domain, *domain; |
340 | int err; | |
341 | ||
342 | if (!parent) { | |
343 | pr_err("%s: no parent, giving up\n", node->full_name); | |
96ca848e | 344 | return -ENODEV; |
783d3186 MZ |
345 | } |
346 | ||
347 | parent_domain = irq_find_host(parent); | |
348 | if (!parent_domain) { | |
349 | pr_err("%s: unable to obtain parent domain\n", node->full_name); | |
350 | return -ENXIO; | |
351 | } | |
352 | ||
353 | err = crossbar_of_init(node); | |
354 | if (err) | |
355 | return err; | |
356 | ||
357 | domain = irq_domain_add_hierarchy(parent_domain, 0, | |
358 | cb->max_crossbar_sources, | |
359 | node, &crossbar_domain_ops, | |
360 | NULL); | |
361 | if (!domain) { | |
362 | pr_err("%s: failed to allocated domain\n", node->full_name); | |
363 | return -ENOMEM; | |
364 | } | |
96ca848e | 365 | |
96ca848e S |
366 | return 0; |
367 | } | |
783d3186 MZ |
368 | |
369 | IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init); |