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]);
93 * crossbar_domain_unmap - unmap a crossbar<->irq connection
94 * @d: domain of irq to unmap
97 * We do not maintain a use count of total number of map/unmap
98 * calls for a particular irq to find out if a irq can be really
99 * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
100 * after which irq is anyways unusable. So an explicit map has to be called
103 static void crossbar_domain_unmap(struct irq_domain *d, unsigned int irq)
105 irq_hw_number_t hw = irq_get_irq_data(irq)->hwirq;
107 if (hw > GIC_IRQ_START) {
108 cb->irq_map[hw - GIC_IRQ_START] = IRQ_FREE;
109 cb->write(hw - GIC_IRQ_START, cb->safe_map);
113 static int crossbar_domain_xlate(struct irq_domain *d,
114 struct device_node *controller,
115 const u32 *intspec, unsigned int intsize,
116 unsigned long *out_hwirq,
117 unsigned int *out_type)
121 ret = get_prev_map_irq(intspec[1]);
125 ret = allocate_free_irq(intspec[1]);
131 *out_hwirq = ret + GIC_IRQ_START;
135 static const struct irq_domain_ops routable_irq_domain_ops = {
136 .map = crossbar_domain_map,
137 .unmap = crossbar_domain_unmap,
138 .xlate = crossbar_domain_xlate
141 static int __init crossbar_of_init(struct device_node *node)
143 int i, size, max = 0, reserved = 0, entry;
147 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
152 cb->crossbar_base = of_iomap(node, 0);
153 if (!cb->crossbar_base)
156 of_property_read_u32(node, "ti,max-irqs", &max);
158 pr_err("missing 'ti,max-irqs' property\n");
162 cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
168 for (i = 0; i < max; i++)
169 cb->irq_map[i] = IRQ_FREE;
171 /* Get and mark reserved irqs */
172 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
174 size /= sizeof(__be32);
176 for (i = 0; i < size; i++) {
177 of_property_read_u32_index(node,
181 pr_err("Invalid reserved entry\n");
185 cb->irq_map[entry] = IRQ_RESERVED;
189 /* Skip irqs hardwired to bypass the crossbar */
190 irqsr = of_get_property(node, "ti,irqs-skip", &size);
192 size /= sizeof(__be32);
194 for (i = 0; i < size; i++) {
195 of_property_read_u32_index(node,
199 pr_err("Invalid skip entry\n");
203 cb->irq_map[entry] = IRQ_SKIP;
208 cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
209 if (!cb->register_offsets)
212 of_property_read_u32(node, "ti,reg-size", &size);
216 cb->write = crossbar_writeb;
219 cb->write = crossbar_writew;
222 cb->write = crossbar_writel;
225 pr_err("Invalid reg-size property\n");
232 * Register offsets are not linear because of the
233 * reserved irqs. so find and store the offsets once.
235 for (i = 0; i < max; i++) {
236 if (cb->irq_map[i] == IRQ_RESERVED)
239 cb->register_offsets[i] = reserved;
243 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
244 /* Initialize the crossbar with safe map to start with */
245 for (i = 0; i < max; i++) {
246 if (cb->irq_map[i] == IRQ_RESERVED ||
247 cb->irq_map[i] == IRQ_SKIP)
250 cb->write(i, cb->safe_map);
253 register_routable_domain_ops(&routable_irq_domain_ops);
257 kfree(cb->register_offsets);
261 iounmap(cb->crossbar_base);
269 static const struct of_device_id crossbar_match[] __initconst = {
270 { .compatible = "ti,irq-crossbar" },
274 int __init irqcrossbar_init(void)
276 struct device_node *np;
277 np = of_find_matching_node(NULL, crossbar_match);
281 crossbar_of_init(np);