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
21 #define GIC_IRQ_START 32
24 * @int_max: maximum number of supported interrupts
25 * @irq_map: array of interrupts to crossbar number mapping
26 * @crossbar_base: crossbar base address
27 * @register_offsets: offsets for each irq number
29 struct crossbar_device {
32 void __iomem *crossbar_base;
33 int *register_offsets;
34 void (*write) (int, int);
37 static struct crossbar_device *cb;
39 static inline void crossbar_writel(int irq_no, int cb_no)
41 writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
44 static inline void crossbar_writew(int irq_no, int cb_no)
46 writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
49 static inline void crossbar_writeb(int irq_no, int cb_no)
51 writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
54 static inline int get_prev_map_irq(int cb_no)
58 for (i = 0; i < cb->int_max; i++)
59 if (cb->irq_map[i] == cb_no)
65 static inline int allocate_free_irq(int cb_no)
69 for (i = 0; i < cb->int_max; i++) {
70 if (cb->irq_map[i] == IRQ_FREE) {
71 cb->irq_map[i] = cb_no;
79 static int crossbar_domain_map(struct irq_domain *d, unsigned int irq,
82 cb->write(hw - GIC_IRQ_START, cb->irq_map[hw - GIC_IRQ_START]);
86 static void crossbar_domain_unmap(struct irq_domain *d, unsigned int irq)
88 irq_hw_number_t hw = irq_get_irq_data(irq)->hwirq;
90 if (hw > GIC_IRQ_START)
91 cb->irq_map[hw - GIC_IRQ_START] = IRQ_FREE;
94 static int crossbar_domain_xlate(struct irq_domain *d,
95 struct device_node *controller,
96 const u32 *intspec, unsigned int intsize,
97 unsigned long *out_hwirq,
98 unsigned int *out_type)
102 ret = get_prev_map_irq(intspec[1]);
103 if (!IS_ERR_VALUE(ret))
106 ret = allocate_free_irq(intspec[1]);
108 if (IS_ERR_VALUE(ret))
112 *out_hwirq = ret + GIC_IRQ_START;
116 const struct irq_domain_ops routable_irq_domain_ops = {
117 .map = crossbar_domain_map,
118 .unmap = crossbar_domain_unmap,
119 .xlate = crossbar_domain_xlate
122 static int __init crossbar_of_init(struct device_node *node)
124 int i, size, max, reserved = 0, entry;
127 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
132 cb->crossbar_base = of_iomap(node, 0);
133 if (!cb->crossbar_base)
136 of_property_read_u32(node, "ti,max-irqs", &max);
137 cb->irq_map = kzalloc(max * sizeof(int), GFP_KERNEL);
143 for (i = 0; i < max; i++)
144 cb->irq_map[i] = IRQ_FREE;
146 /* Get and mark reserved irqs */
147 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
149 size /= sizeof(__be32);
151 for (i = 0; i < size; i++) {
152 of_property_read_u32_index(node,
156 pr_err("Invalid reserved entry\n");
159 cb->irq_map[entry] = IRQ_RESERVED;
163 cb->register_offsets = kzalloc(max * sizeof(int), GFP_KERNEL);
164 if (!cb->register_offsets)
167 of_property_read_u32(node, "ti,reg-size", &size);
171 cb->write = crossbar_writeb;
174 cb->write = crossbar_writew;
177 cb->write = crossbar_writel;
180 pr_err("Invalid reg-size property\n");
186 * Register offsets are not linear because of the
187 * reserved irqs. so find and store the offsets once.
189 for (i = 0; i < max; i++) {
190 if (cb->irq_map[i] == IRQ_RESERVED)
193 cb->register_offsets[i] = reserved;
197 register_routable_domain_ops(&routable_irq_domain_ops);
201 kfree(cb->register_offsets);
205 iounmap(cb->crossbar_base);
211 static const struct of_device_id crossbar_match[] __initconst = {
212 { .compatible = "ti,irq-crossbar" },
216 int __init irqcrossbar_init(void)
218 struct device_node *np;
219 np = of_find_matching_node(NULL, crossbar_match);
223 crossbar_of_init(np);