1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/irqchip/irq-crossbar.c
5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
6 * Author: Sricharan R <r.sricharan@ti.com>
10 #include <linux/irqchip.h>
11 #include <linux/irqdomain.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/slab.h>
17 #define IRQ_RESERVED -2
19 #define GIC_IRQ_START 32
22 * struct crossbar_device - crossbar device description
23 * @lock: spinlock serializing access to @irq_map
24 * @int_max: maximum number of supported interrupts
25 * @safe_map: safe default value to initialize the crossbar
26 * @max_crossbar_sources: Maximum number of crossbar sources
27 * @irq_map: array of interrupts to crossbar number mapping
28 * @crossbar_base: crossbar base address
29 * @register_offsets: offsets for each irq number
30 * @write: register write function pointer
32 struct crossbar_device {
36 uint max_crossbar_sources;
38 void __iomem *crossbar_base;
39 int *register_offsets;
40 void (*write)(int, int);
43 static struct crossbar_device *cb;
45 static void crossbar_writel(int irq_no, int cb_no)
47 writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
50 static void crossbar_writew(int irq_no, int cb_no)
52 writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
55 static void crossbar_writeb(int irq_no, int cb_no)
57 writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
60 static struct irq_chip crossbar_chip = {
62 .irq_eoi = irq_chip_eoi_parent,
63 .irq_mask = irq_chip_mask_parent,
64 .irq_unmask = irq_chip_unmask_parent,
65 .irq_retrigger = irq_chip_retrigger_hierarchy,
66 .irq_set_type = irq_chip_set_type_parent,
67 .flags = IRQCHIP_MASK_ON_SUSPEND |
68 IRQCHIP_SKIP_SET_WAKE,
70 .irq_set_affinity = irq_chip_set_affinity_parent,
74 static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
75 irq_hw_number_t hwirq)
77 struct irq_fwspec fwspec;
81 if (!irq_domain_get_of_node(domain->parent))
84 raw_spin_lock(&cb->lock);
85 for (i = cb->int_max - 1; i >= 0; i--) {
86 if (cb->irq_map[i] == IRQ_FREE) {
87 cb->irq_map[i] = hwirq;
91 raw_spin_unlock(&cb->lock);
96 fwspec.fwnode = domain->parent->fwnode;
97 fwspec.param_count = 3;
98 fwspec.param[0] = 0; /* SPI */
100 fwspec.param[2] = IRQ_TYPE_LEVEL_HIGH;
102 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
104 cb->irq_map[i] = IRQ_FREE;
111 static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
112 unsigned int nr_irqs, void *data)
114 struct irq_fwspec *fwspec = data;
115 irq_hw_number_t hwirq;
118 if (fwspec->param_count != 3)
119 return -EINVAL; /* Not GIC compliant */
120 if (fwspec->param[0] != 0)
121 return -EINVAL; /* No PPI should point to this domain */
123 hwirq = fwspec->param[1];
124 if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
125 return -EINVAL; /* Can't deal with this */
127 for (i = 0; i < nr_irqs; i++) {
128 int err = allocate_gic_irq(d, virq + i, hwirq + i);
133 irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
134 &crossbar_chip, NULL);
141 * crossbar_domain_free - unmap/free a crossbar<->irq connection
142 * @domain: domain of irq to unmap
144 * @nr_irqs: number of irqs to free
146 * We do not maintain a use count of total number of map/unmap
147 * calls for a particular irq to find out if a irq can be really
148 * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
149 * after which irq is anyways unusable. So an explicit map has to be called
152 static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
153 unsigned int nr_irqs)
157 raw_spin_lock(&cb->lock);
158 for (i = 0; i < nr_irqs; i++) {
159 struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
161 irq_domain_reset_irq_data(d);
162 cb->irq_map[d->hwirq] = IRQ_FREE;
163 cb->write(d->hwirq, cb->safe_map);
165 raw_spin_unlock(&cb->lock);
168 static int crossbar_domain_translate(struct irq_domain *d,
169 struct irq_fwspec *fwspec,
170 unsigned long *hwirq,
173 if (is_of_node(fwspec->fwnode)) {
174 if (fwspec->param_count != 3)
177 /* No PPI should point to this domain */
178 if (fwspec->param[0] != 0)
181 *hwirq = fwspec->param[1];
182 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
189 static const struct irq_domain_ops crossbar_domain_ops = {
190 .alloc = crossbar_domain_alloc,
191 .free = crossbar_domain_free,
192 .translate = crossbar_domain_translate,
195 static int __init crossbar_of_init(struct device_node *node)
197 u32 max = 0, entry, reg_size;
198 int i, size, reserved = 0;
202 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
207 cb->crossbar_base = of_iomap(node, 0);
208 if (!cb->crossbar_base)
211 of_property_read_u32(node, "ti,max-crossbar-sources",
212 &cb->max_crossbar_sources);
213 if (!cb->max_crossbar_sources) {
214 pr_err("missing 'ti,max-crossbar-sources' property\n");
219 of_property_read_u32(node, "ti,max-irqs", &max);
221 pr_err("missing 'ti,max-irqs' property\n");
225 cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
231 for (i = 0; i < max; i++)
232 cb->irq_map[i] = IRQ_FREE;
234 /* Get and mark reserved irqs */
235 irqsr = of_get_property(node, "ti,irqs-reserved", &size);
237 size /= sizeof(__be32);
239 for (i = 0; i < size; i++) {
240 of_property_read_u32_index(node,
244 pr_err("Invalid reserved entry\n");
248 cb->irq_map[entry] = IRQ_RESERVED;
252 /* Skip irqs hardwired to bypass the crossbar */
253 irqsr = of_get_property(node, "ti,irqs-skip", &size);
255 size /= sizeof(__be32);
257 for (i = 0; i < size; i++) {
258 of_property_read_u32_index(node,
262 pr_err("Invalid skip entry\n");
266 cb->irq_map[entry] = IRQ_SKIP;
271 cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
272 if (!cb->register_offsets)
275 of_property_read_u32(node, "ti,reg-size", ®_size);
279 cb->write = crossbar_writeb;
282 cb->write = crossbar_writew;
285 cb->write = crossbar_writel;
288 pr_err("Invalid reg-size property\n");
295 * Register offsets are not linear because of the
296 * reserved irqs. so find and store the offsets once.
298 for (i = 0; i < max; i++) {
299 if (cb->irq_map[i] == IRQ_RESERVED)
302 cb->register_offsets[i] = reserved;
303 reserved += reg_size;
306 of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
307 /* Initialize the crossbar with safe map to start with */
308 for (i = 0; i < max; i++) {
309 if (cb->irq_map[i] == IRQ_RESERVED ||
310 cb->irq_map[i] == IRQ_SKIP)
313 cb->write(i, cb->safe_map);
316 raw_spin_lock_init(&cb->lock);
321 kfree(cb->register_offsets);
325 iounmap(cb->crossbar_base);
333 static int __init irqcrossbar_init(struct device_node *node,
334 struct device_node *parent)
336 struct irq_domain *parent_domain, *domain;
340 pr_err("%pOF: no parent, giving up\n", node);
344 parent_domain = irq_find_host(parent);
345 if (!parent_domain) {
346 pr_err("%pOF: unable to obtain parent domain\n", node);
350 err = crossbar_of_init(node);
354 domain = irq_domain_add_hierarchy(parent_domain, 0,
355 cb->max_crossbar_sources,
356 node, &crossbar_domain_ops,
359 pr_err("%pOF: failed to allocated domain\n", node);
366 IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);