Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 1 | /* |
| 2 | * drivers/irqchip/irq-crossbar.c |
| 3 | * |
| 4 | * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com |
| 5 | * Author: Sricharan R <r.sricharan@ti.com> |
| 6 | * |
| 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. |
| 10 | * |
| 11 | */ |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/io.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 | |
| 19 | #define IRQ_FREE -1 |
Nishanth Menon | 1d50d2c | 2014-06-26 12:40:19 +0530 | [diff] [blame^] | 20 | #define IRQ_RESERVED -2 |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 21 | #define GIC_IRQ_START 32 |
| 22 | |
| 23 | /* |
| 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 |
| 28 | */ |
| 29 | struct crossbar_device { |
| 30 | uint int_max; |
| 31 | uint *irq_map; |
| 32 | void __iomem *crossbar_base; |
| 33 | int *register_offsets; |
| 34 | void (*write) (int, int); |
| 35 | }; |
| 36 | |
| 37 | static struct crossbar_device *cb; |
| 38 | |
| 39 | static inline void crossbar_writel(int irq_no, int cb_no) |
| 40 | { |
| 41 | writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); |
| 42 | } |
| 43 | |
| 44 | static inline void crossbar_writew(int irq_no, int cb_no) |
| 45 | { |
| 46 | writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); |
| 47 | } |
| 48 | |
| 49 | static inline void crossbar_writeb(int irq_no, int cb_no) |
| 50 | { |
| 51 | writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); |
| 52 | } |
| 53 | |
| 54 | static inline int allocate_free_irq(int cb_no) |
| 55 | { |
| 56 | int i; |
| 57 | |
| 58 | for (i = 0; i < cb->int_max; i++) { |
| 59 | if (cb->irq_map[i] == IRQ_FREE) { |
| 60 | cb->irq_map[i] = cb_no; |
| 61 | return i; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return -ENODEV; |
| 66 | } |
| 67 | |
| 68 | static int crossbar_domain_map(struct irq_domain *d, unsigned int irq, |
| 69 | irq_hw_number_t hw) |
| 70 | { |
| 71 | cb->write(hw - GIC_IRQ_START, cb->irq_map[hw - GIC_IRQ_START]); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static void crossbar_domain_unmap(struct irq_domain *d, unsigned int irq) |
| 76 | { |
| 77 | irq_hw_number_t hw = irq_get_irq_data(irq)->hwirq; |
| 78 | |
| 79 | if (hw > GIC_IRQ_START) |
| 80 | cb->irq_map[hw - GIC_IRQ_START] = IRQ_FREE; |
| 81 | } |
| 82 | |
| 83 | static int crossbar_domain_xlate(struct irq_domain *d, |
| 84 | struct device_node *controller, |
| 85 | const u32 *intspec, unsigned int intsize, |
| 86 | unsigned long *out_hwirq, |
| 87 | unsigned int *out_type) |
| 88 | { |
| 89 | unsigned long ret; |
| 90 | |
| 91 | ret = allocate_free_irq(intspec[1]); |
| 92 | |
| 93 | if (IS_ERR_VALUE(ret)) |
| 94 | return ret; |
| 95 | |
| 96 | *out_hwirq = ret + GIC_IRQ_START; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | const struct irq_domain_ops routable_irq_domain_ops = { |
| 101 | .map = crossbar_domain_map, |
| 102 | .unmap = crossbar_domain_unmap, |
| 103 | .xlate = crossbar_domain_xlate |
| 104 | }; |
| 105 | |
| 106 | static int __init crossbar_of_init(struct device_node *node) |
| 107 | { |
| 108 | int i, size, max, reserved = 0, entry; |
| 109 | const __be32 *irqsr; |
| 110 | |
Dan Carpenter | 3894e9e | 2014-04-03 10:21:34 +0300 | [diff] [blame] | 111 | cb = kzalloc(sizeof(*cb), GFP_KERNEL); |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 112 | |
| 113 | if (!cb) |
| 114 | return -ENOMEM; |
| 115 | |
| 116 | cb->crossbar_base = of_iomap(node, 0); |
| 117 | if (!cb->crossbar_base) |
| 118 | goto err1; |
| 119 | |
| 120 | of_property_read_u32(node, "ti,max-irqs", &max); |
| 121 | cb->irq_map = kzalloc(max * sizeof(int), GFP_KERNEL); |
| 122 | if (!cb->irq_map) |
| 123 | goto err2; |
| 124 | |
| 125 | cb->int_max = max; |
| 126 | |
| 127 | for (i = 0; i < max; i++) |
| 128 | cb->irq_map[i] = IRQ_FREE; |
| 129 | |
| 130 | /* Get and mark reserved irqs */ |
| 131 | irqsr = of_get_property(node, "ti,irqs-reserved", &size); |
| 132 | if (irqsr) { |
| 133 | size /= sizeof(__be32); |
| 134 | |
| 135 | for (i = 0; i < size; i++) { |
| 136 | of_property_read_u32_index(node, |
| 137 | "ti,irqs-reserved", |
| 138 | i, &entry); |
| 139 | if (entry > max) { |
| 140 | pr_err("Invalid reserved entry\n"); |
| 141 | goto err3; |
| 142 | } |
Nishanth Menon | 1d50d2c | 2014-06-26 12:40:19 +0530 | [diff] [blame^] | 143 | cb->irq_map[entry] = IRQ_RESERVED; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | cb->register_offsets = kzalloc(max * sizeof(int), GFP_KERNEL); |
| 148 | if (!cb->register_offsets) |
| 149 | goto err3; |
| 150 | |
| 151 | of_property_read_u32(node, "ti,reg-size", &size); |
| 152 | |
| 153 | switch (size) { |
| 154 | case 1: |
| 155 | cb->write = crossbar_writeb; |
| 156 | break; |
| 157 | case 2: |
| 158 | cb->write = crossbar_writew; |
| 159 | break; |
| 160 | case 4: |
| 161 | cb->write = crossbar_writel; |
| 162 | break; |
| 163 | default: |
| 164 | pr_err("Invalid reg-size property\n"); |
| 165 | goto err4; |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Register offsets are not linear because of the |
| 171 | * reserved irqs. so find and store the offsets once. |
| 172 | */ |
| 173 | for (i = 0; i < max; i++) { |
Nishanth Menon | 1d50d2c | 2014-06-26 12:40:19 +0530 | [diff] [blame^] | 174 | if (cb->irq_map[i] == IRQ_RESERVED) |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 175 | continue; |
| 176 | |
| 177 | cb->register_offsets[i] = reserved; |
| 178 | reserved += size; |
| 179 | } |
| 180 | |
| 181 | register_routable_domain_ops(&routable_irq_domain_ops); |
| 182 | return 0; |
| 183 | |
| 184 | err4: |
| 185 | kfree(cb->register_offsets); |
| 186 | err3: |
| 187 | kfree(cb->irq_map); |
| 188 | err2: |
| 189 | iounmap(cb->crossbar_base); |
| 190 | err1: |
| 191 | kfree(cb); |
| 192 | return -ENOMEM; |
| 193 | } |
| 194 | |
| 195 | static const struct of_device_id crossbar_match[] __initconst = { |
| 196 | { .compatible = "ti,irq-crossbar" }, |
| 197 | {} |
| 198 | }; |
| 199 | |
| 200 | int __init irqcrossbar_init(void) |
| 201 | { |
| 202 | struct device_node *np; |
| 203 | np = of_find_matching_node(NULL, crossbar_match); |
| 204 | if (!np) |
| 205 | return -ENODEV; |
| 206 | |
| 207 | crossbar_of_init(np); |
| 208 | return 0; |
| 209 | } |