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>
20 #define IRQ_RESERVED -2
22 #define GIC_IRQ_START 32
25 * @int_max: maximum number of supported interrupts
26 * @safe_map: safe default value to initialize the crossbar
27 * @irq_map: array of interrupts to crossbar number mapping
28 * @crossbar_base: crossbar base address
29 * @register_offsets: offsets for each irq number
31 struct crossbar_device {
35 void __iomem *crossbar_base;
36 int *register_offsets;
37 void (*write)(int, int);
40 static struct crossbar_device *cb;
42 static inline void crossbar_writel(int irq_no, int cb_no)
44 writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
47 static inline void crossbar_writew(int irq_no, int cb_no)
49 writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
52 static inline void crossbar_writeb(int irq_no, int cb_no)
54 writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
57 static inline int get_prev_map_irq(int cb_no)
61 for (i = cb->int_max - 1; i >= 0; i--)
62 if (cb->irq_map[i] == cb_no)
68 static inline int allocate_free_irq(int cb_no)
72 for (i = cb->int_max - 1; i >= 0; i--) {
73 if (cb->irq_map[i] == IRQ_FREE) {
74 cb->irq_map[i] = cb_no;
82 static int crossbar_domain_map(struct irq_domain *d, unsigned int irq,
85 cb->write(hw - GIC_IRQ_START, cb->irq_map[hw - GIC_IRQ_START]);
89 static void crossbar_domain_unmap(struct irq_domain *d, unsigned int irq)
91 irq_hw_number_t hw = irq_get_irq_data(irq)->hwirq;
93 if (hw > GIC_IRQ_START) {
94 cb->irq_map[hw - GIC_IRQ_START] = IRQ_FREE;
95 cb->write(hw - GIC_IRQ_START, cb->safe_map);
99 static int crossbar_domain_xlate(struct irq_domain *d,
100 struct device_node *controller,
101 const u32 *intspec, unsigned int intsize,
102 unsigned long *out_hwirq,
103 unsigned int *out_type)
107 ret = get_prev_map_irq(intspec[1]);
108 if (!IS_ERR_VALUE(ret))
111 ret = allocate_free_irq(intspec[1]);
113 if (IS_ERR_VALUE(ret))
117 *out_hwirq = ret + GIC_IRQ_START;
121 const struct irq_domain_ops routable_irq_domain_ops = {
122 .map = crossbar_domain_map,
123 .unmap = crossbar_domain_unmap,
124 .xlate = crossbar_domain_xlate
127 static int __init crossbar_of_init(struct device_node *node)
129 int i, size, max, reserved = 0, entry;
132 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
137 cb->crossbar_base = of_iomap(node, 0);
138 if (!cb->crossbar_base)
141 of_property_read_u32(node, "ti,max-irqs", &max);
142 cb->irq_map = kzalloc(max * sizeof(int), GFP_KERNEL);
148 for (i = 0; i < max; i++)
149 cb->irq_map[i] = IRQ_FREE;
151 /* Get and mark reserved irqs */
152 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
154 size /= sizeof(__be32);
156 for (i = 0; i < size; i++) {
157 of_property_read_u32_index(node,
161 pr_err("Invalid reserved entry\n");
164 cb->irq_map[entry] = IRQ_RESERVED;
168 /* Skip irqs hardwired to bypass the crossbar */
169 irqsr = of_get_property(node, "ti,irqs-skip", &size);
171 size /= sizeof(__be32);
173 for (i = 0; i < size; i++) {
174 of_property_read_u32_index(node,
178 pr_err("Invalid skip entry\n");
182 cb->irq_map[entry] = IRQ_SKIP;
187 cb->register_offsets = kzalloc(max * sizeof(int), GFP_KERNEL);
188 if (!cb->register_offsets)
191 of_property_read_u32(node, "ti,reg-size", &size);
195 cb->write = crossbar_writeb;
198 cb->write = crossbar_writew;
201 cb->write = crossbar_writel;
204 pr_err("Invalid reg-size property\n");
210 * Register offsets are not linear because of the
211 * reserved irqs. so find and store the offsets once.
213 for (i = 0; i < max; i++) {
214 if (cb->irq_map[i] == IRQ_RESERVED)
217 cb->register_offsets[i] = reserved;
221 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
223 /* Initialize the crossbar with safe map to start with */
224 for (i = 0; i < max; i++) {
225 if (cb->irq_map[i] == IRQ_RESERVED ||
226 cb->irq_map[i] == IRQ_SKIP)
229 cb->write(i, cb->safe_map);
232 register_routable_domain_ops(&routable_irq_domain_ops);
236 kfree(cb->register_offsets);
240 iounmap(cb->crossbar_base);
246 static const struct of_device_id crossbar_match[] __initconst = {
247 { .compatible = "ti,irq-crossbar" },
251 int __init irqcrossbar_init(void)
253 struct device_node *np;
254 np = of_find_matching_node(NULL, crossbar_match);
258 crossbar_of_init(np);