2 * drivers/irqchip/irq-crossbar.c
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5 * Author: Sricharan R <r.sricharan@ti.com>
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.
12 #include <linux/err.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 #include <linux/irqchip/irq-crossbar.h>
21 #define IRQ_RESERVED -2
23 #define GIC_IRQ_START 32
26 * struct crossbar_device - crossbar device description
27 * @int_max: maximum number of supported interrupts
28 * @safe_map: safe default value to initialize the crossbar
29 * @irq_map: array of interrupts to crossbar number mapping
30 * @crossbar_base: crossbar base address
31 * @register_offsets: offsets for each irq number
32 * @write: register write function pointer
34 struct crossbar_device {
38 void __iomem *crossbar_base;
39 int *register_offsets;
40 void (*write)(int, int);
43 static struct crossbar_device *cb;
45 static inline void crossbar_writel(int irq_no, int cb_no)
47 writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
50 static inline void crossbar_writew(int irq_no, int cb_no)
52 writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
55 static inline void crossbar_writeb(int irq_no, int cb_no)
57 writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
60 static inline int get_prev_map_irq(int cb_no)
64 for (i = cb->int_max - 1; i >= 0; i--)
65 if (cb->irq_map[i] == cb_no)
71 static inline int allocate_free_irq(int cb_no)
75 for (i = cb->int_max - 1; i >= 0; i--) {
76 if (cb->irq_map[i] == IRQ_FREE) {
77 cb->irq_map[i] = cb_no;
85 static int crossbar_domain_map(struct irq_domain *d, unsigned int irq,
88 cb->write(hw - GIC_IRQ_START, cb->irq_map[hw - GIC_IRQ_START]);
92 static void crossbar_domain_unmap(struct irq_domain *d, unsigned int irq)
94 irq_hw_number_t hw = irq_get_irq_data(irq)->hwirq;
96 if (hw > GIC_IRQ_START) {
97 cb->irq_map[hw - GIC_IRQ_START] = IRQ_FREE;
98 cb->write(hw - GIC_IRQ_START, cb->safe_map);
102 static int crossbar_domain_xlate(struct irq_domain *d,
103 struct device_node *controller,
104 const u32 *intspec, unsigned int intsize,
105 unsigned long *out_hwirq,
106 unsigned int *out_type)
110 ret = get_prev_map_irq(intspec[1]);
114 ret = allocate_free_irq(intspec[1]);
120 *out_hwirq = ret + GIC_IRQ_START;
124 static const struct irq_domain_ops routable_irq_domain_ops = {
125 .map = crossbar_domain_map,
126 .unmap = crossbar_domain_unmap,
127 .xlate = crossbar_domain_xlate
130 static int __init crossbar_of_init(struct device_node *node)
132 int i, size, max = 0, reserved = 0, entry;
136 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
141 cb->crossbar_base = of_iomap(node, 0);
142 if (!cb->crossbar_base)
145 of_property_read_u32(node, "ti,max-irqs", &max);
147 pr_err("missing 'ti,max-irqs' property\n");
151 cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
157 for (i = 0; i < max; i++)
158 cb->irq_map[i] = IRQ_FREE;
160 /* Get and mark reserved irqs */
161 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
163 size /= sizeof(__be32);
165 for (i = 0; i < size; i++) {
166 of_property_read_u32_index(node,
170 pr_err("Invalid reserved entry\n");
174 cb->irq_map[entry] = IRQ_RESERVED;
178 /* Skip irqs hardwired to bypass the crossbar */
179 irqsr = of_get_property(node, "ti,irqs-skip", &size);
181 size /= sizeof(__be32);
183 for (i = 0; i < size; i++) {
184 of_property_read_u32_index(node,
188 pr_err("Invalid skip entry\n");
192 cb->irq_map[entry] = IRQ_SKIP;
197 cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
198 if (!cb->register_offsets)
201 of_property_read_u32(node, "ti,reg-size", &size);
205 cb->write = crossbar_writeb;
208 cb->write = crossbar_writew;
211 cb->write = crossbar_writel;
214 pr_err("Invalid reg-size property\n");
221 * Register offsets are not linear because of the
222 * reserved irqs. so find and store the offsets once.
224 for (i = 0; i < max; i++) {
225 if (cb->irq_map[i] == IRQ_RESERVED)
228 cb->register_offsets[i] = reserved;
232 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
234 /* Initialize the crossbar with safe map to start with */
235 for (i = 0; i < max; i++) {
236 if (cb->irq_map[i] == IRQ_RESERVED ||
237 cb->irq_map[i] == IRQ_SKIP)
240 cb->write(i, cb->safe_map);
243 register_routable_domain_ops(&routable_irq_domain_ops);
247 kfree(cb->register_offsets);
251 iounmap(cb->crossbar_base);
257 static const struct of_device_id crossbar_match[] __initconst = {
258 { .compatible = "ti,irq-crossbar" },
262 int __init irqcrossbar_init(void)
264 struct device_node *np;
265 np = of_find_matching_node(NULL, crossbar_match);
269 crossbar_of_init(np);