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, reserved = 0, entry;
135 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
140 cb->crossbar_base = of_iomap(node, 0);
141 if (!cb->crossbar_base)
144 of_property_read_u32(node, "ti,max-irqs", &max);
145 cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
151 for (i = 0; i < max; i++)
152 cb->irq_map[i] = IRQ_FREE;
154 /* Get and mark reserved irqs */
155 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
157 size /= sizeof(__be32);
159 for (i = 0; i < size; i++) {
160 of_property_read_u32_index(node,
164 pr_err("Invalid reserved entry\n");
167 cb->irq_map[entry] = IRQ_RESERVED;
171 /* Skip irqs hardwired to bypass the crossbar */
172 irqsr = of_get_property(node, "ti,irqs-skip", &size);
174 size /= sizeof(__be32);
176 for (i = 0; i < size; i++) {
177 of_property_read_u32_index(node,
181 pr_err("Invalid skip entry\n");
185 cb->irq_map[entry] = IRQ_SKIP;
190 cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
191 if (!cb->register_offsets)
194 of_property_read_u32(node, "ti,reg-size", &size);
198 cb->write = crossbar_writeb;
201 cb->write = crossbar_writew;
204 cb->write = crossbar_writel;
207 pr_err("Invalid reg-size property\n");
213 * Register offsets are not linear because of the
214 * reserved irqs. so find and store the offsets once.
216 for (i = 0; i < max; i++) {
217 if (cb->irq_map[i] == IRQ_RESERVED)
220 cb->register_offsets[i] = reserved;
224 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
226 /* Initialize the crossbar with safe map to start with */
227 for (i = 0; i < max; i++) {
228 if (cb->irq_map[i] == IRQ_RESERVED ||
229 cb->irq_map[i] == IRQ_SKIP)
232 cb->write(i, cb->safe_map);
235 register_routable_domain_ops(&routable_irq_domain_ops);
239 kfree(cb->register_offsets);
243 iounmap(cb->crossbar_base);
249 static const struct of_device_id crossbar_match[] __initconst = {
250 { .compatible = "ti,irq-crossbar" },
254 int __init irqcrossbar_init(void)
256 struct device_node *np;
257 np = of_find_matching_node(NULL, crossbar_match);
261 crossbar_of_init(np);