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> |
Nishanth Menon | 4dbf45e | 2014-06-26 12:40:25 +0530 | [diff] [blame] | 18 | #include <linux/irqchip/irq-crossbar.h> |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 19 | |
| 20 | #define IRQ_FREE -1 |
Nishanth Menon | 1d50d2c | 2014-06-26 12:40:19 +0530 | [diff] [blame] | 21 | #define IRQ_RESERVED -2 |
Nishanth Menon | 64e0f8b | 2014-06-26 12:40:21 +0530 | [diff] [blame] | 22 | #define IRQ_SKIP -3 |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 23 | #define GIC_IRQ_START 32 |
| 24 | |
Nishanth Menon | e30ef8a | 2014-06-26 12:40:26 +0530 | [diff] [blame^] | 25 | /** |
| 26 | * struct crossbar_device - crossbar device description |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 27 | * @int_max: maximum number of supported interrupts |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 28 | * @safe_map: safe default value to initialize the crossbar |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 29 | * @irq_map: array of interrupts to crossbar number mapping |
| 30 | * @crossbar_base: crossbar base address |
| 31 | * @register_offsets: offsets for each irq number |
Nishanth Menon | e30ef8a | 2014-06-26 12:40:26 +0530 | [diff] [blame^] | 32 | * @write: register write function pointer |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 33 | */ |
| 34 | struct crossbar_device { |
| 35 | uint int_max; |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 36 | uint safe_map; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 37 | uint *irq_map; |
| 38 | void __iomem *crossbar_base; |
| 39 | int *register_offsets; |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 40 | void (*write)(int, int); |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static struct crossbar_device *cb; |
| 44 | |
| 45 | static inline void crossbar_writel(int irq_no, int cb_no) |
| 46 | { |
| 47 | writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); |
| 48 | } |
| 49 | |
| 50 | static inline void crossbar_writew(int irq_no, int cb_no) |
| 51 | { |
| 52 | writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); |
| 53 | } |
| 54 | |
| 55 | static inline void crossbar_writeb(int irq_no, int cb_no) |
| 56 | { |
| 57 | writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); |
| 58 | } |
| 59 | |
Nishanth Menon | 6f16fc8 | 2014-06-26 12:40:20 +0530 | [diff] [blame] | 60 | static inline int get_prev_map_irq(int cb_no) |
| 61 | { |
| 62 | int i; |
| 63 | |
Nishanth Menon | ddee0fb | 2014-06-26 12:40:23 +0530 | [diff] [blame] | 64 | for (i = cb->int_max - 1; i >= 0; i--) |
Nishanth Menon | 6f16fc8 | 2014-06-26 12:40:20 +0530 | [diff] [blame] | 65 | if (cb->irq_map[i] == cb_no) |
| 66 | return i; |
| 67 | |
| 68 | return -ENODEV; |
| 69 | } |
| 70 | |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 71 | static inline int allocate_free_irq(int cb_no) |
| 72 | { |
| 73 | int i; |
| 74 | |
Nishanth Menon | ddee0fb | 2014-06-26 12:40:23 +0530 | [diff] [blame] | 75 | for (i = cb->int_max - 1; i >= 0; i--) { |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 76 | if (cb->irq_map[i] == IRQ_FREE) { |
| 77 | cb->irq_map[i] = cb_no; |
| 78 | return i; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return -ENODEV; |
| 83 | } |
| 84 | |
| 85 | static int crossbar_domain_map(struct irq_domain *d, unsigned int irq, |
| 86 | irq_hw_number_t hw) |
| 87 | { |
| 88 | cb->write(hw - GIC_IRQ_START, cb->irq_map[hw - GIC_IRQ_START]); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static void crossbar_domain_unmap(struct irq_domain *d, unsigned int irq) |
| 93 | { |
| 94 | irq_hw_number_t hw = irq_get_irq_data(irq)->hwirq; |
| 95 | |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 96 | if (hw > GIC_IRQ_START) { |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 97 | cb->irq_map[hw - GIC_IRQ_START] = IRQ_FREE; |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 98 | cb->write(hw - GIC_IRQ_START, cb->safe_map); |
| 99 | } |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 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) |
| 107 | { |
Nishanth Menon | d4922a9 | 2014-06-26 12:40:24 +0530 | [diff] [blame] | 108 | int ret; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 109 | |
Nishanth Menon | 6f16fc8 | 2014-06-26 12:40:20 +0530 | [diff] [blame] | 110 | ret = get_prev_map_irq(intspec[1]); |
Nishanth Menon | d4922a9 | 2014-06-26 12:40:24 +0530 | [diff] [blame] | 111 | if (ret >= 0) |
Nishanth Menon | 6f16fc8 | 2014-06-26 12:40:20 +0530 | [diff] [blame] | 112 | goto found; |
| 113 | |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 114 | ret = allocate_free_irq(intspec[1]); |
| 115 | |
Nishanth Menon | d4922a9 | 2014-06-26 12:40:24 +0530 | [diff] [blame] | 116 | if (ret < 0) |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 117 | return ret; |
| 118 | |
Nishanth Menon | 6f16fc8 | 2014-06-26 12:40:20 +0530 | [diff] [blame] | 119 | found: |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 120 | *out_hwirq = ret + GIC_IRQ_START; |
| 121 | return 0; |
| 122 | } |
| 123 | |
Nishanth Menon | 4dbf45e | 2014-06-26 12:40:25 +0530 | [diff] [blame] | 124 | static const struct irq_domain_ops routable_irq_domain_ops = { |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 125 | .map = crossbar_domain_map, |
| 126 | .unmap = crossbar_domain_unmap, |
| 127 | .xlate = crossbar_domain_xlate |
| 128 | }; |
| 129 | |
| 130 | static int __init crossbar_of_init(struct device_node *node) |
| 131 | { |
| 132 | int i, size, max, reserved = 0, entry; |
| 133 | const __be32 *irqsr; |
| 134 | |
Dan Carpenter | 3894e9e | 2014-04-03 10:21:34 +0300 | [diff] [blame] | 135 | cb = kzalloc(sizeof(*cb), GFP_KERNEL); |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 136 | |
| 137 | if (!cb) |
| 138 | return -ENOMEM; |
| 139 | |
| 140 | cb->crossbar_base = of_iomap(node, 0); |
| 141 | if (!cb->crossbar_base) |
| 142 | goto err1; |
| 143 | |
| 144 | of_property_read_u32(node, "ti,max-irqs", &max); |
Nishanth Menon | 4dbf45e | 2014-06-26 12:40:25 +0530 | [diff] [blame] | 145 | cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL); |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 146 | if (!cb->irq_map) |
| 147 | goto err2; |
| 148 | |
| 149 | cb->int_max = max; |
| 150 | |
| 151 | for (i = 0; i < max; i++) |
| 152 | cb->irq_map[i] = IRQ_FREE; |
| 153 | |
| 154 | /* Get and mark reserved irqs */ |
| 155 | irqsr = of_get_property(node, "ti,irqs-reserved", &size); |
| 156 | if (irqsr) { |
| 157 | size /= sizeof(__be32); |
| 158 | |
| 159 | for (i = 0; i < size; i++) { |
| 160 | of_property_read_u32_index(node, |
| 161 | "ti,irqs-reserved", |
| 162 | i, &entry); |
| 163 | if (entry > max) { |
| 164 | pr_err("Invalid reserved entry\n"); |
| 165 | goto err3; |
| 166 | } |
Nishanth Menon | 1d50d2c | 2014-06-26 12:40:19 +0530 | [diff] [blame] | 167 | cb->irq_map[entry] = IRQ_RESERVED; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Nishanth Menon | 64e0f8b | 2014-06-26 12:40:21 +0530 | [diff] [blame] | 171 | /* Skip irqs hardwired to bypass the crossbar */ |
| 172 | irqsr = of_get_property(node, "ti,irqs-skip", &size); |
| 173 | if (irqsr) { |
| 174 | size /= sizeof(__be32); |
| 175 | |
| 176 | for (i = 0; i < size; i++) { |
| 177 | of_property_read_u32_index(node, |
| 178 | "ti,irqs-skip", |
| 179 | i, &entry); |
| 180 | if (entry > max) { |
| 181 | pr_err("Invalid skip entry\n"); |
| 182 | ret = -EINVAL; |
| 183 | goto err3; |
| 184 | } |
| 185 | cb->irq_map[entry] = IRQ_SKIP; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
Nishanth Menon | 4dbf45e | 2014-06-26 12:40:25 +0530 | [diff] [blame] | 190 | cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL); |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 191 | if (!cb->register_offsets) |
| 192 | goto err3; |
| 193 | |
| 194 | of_property_read_u32(node, "ti,reg-size", &size); |
| 195 | |
| 196 | switch (size) { |
| 197 | case 1: |
| 198 | cb->write = crossbar_writeb; |
| 199 | break; |
| 200 | case 2: |
| 201 | cb->write = crossbar_writew; |
| 202 | break; |
| 203 | case 4: |
| 204 | cb->write = crossbar_writel; |
| 205 | break; |
| 206 | default: |
| 207 | pr_err("Invalid reg-size property\n"); |
| 208 | goto err4; |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Register offsets are not linear because of the |
| 214 | * reserved irqs. so find and store the offsets once. |
| 215 | */ |
| 216 | for (i = 0; i < max; i++) { |
Nishanth Menon | 1d50d2c | 2014-06-26 12:40:19 +0530 | [diff] [blame] | 217 | if (cb->irq_map[i] == IRQ_RESERVED) |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 218 | continue; |
| 219 | |
| 220 | cb->register_offsets[i] = reserved; |
| 221 | reserved += size; |
| 222 | } |
| 223 | |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 224 | of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map); |
| 225 | |
| 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) |
| 230 | continue; |
| 231 | |
| 232 | cb->write(i, cb->safe_map); |
| 233 | } |
| 234 | |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 235 | register_routable_domain_ops(&routable_irq_domain_ops); |
| 236 | return 0; |
| 237 | |
| 238 | err4: |
| 239 | kfree(cb->register_offsets); |
| 240 | err3: |
| 241 | kfree(cb->irq_map); |
| 242 | err2: |
| 243 | iounmap(cb->crossbar_base); |
| 244 | err1: |
| 245 | kfree(cb); |
| 246 | return -ENOMEM; |
| 247 | } |
| 248 | |
| 249 | static const struct of_device_id crossbar_match[] __initconst = { |
| 250 | { .compatible = "ti,irq-crossbar" }, |
| 251 | {} |
| 252 | }; |
| 253 | |
| 254 | int __init irqcrossbar_init(void) |
| 255 | { |
| 256 | struct device_node *np; |
| 257 | np = of_find_matching_node(NULL, crossbar_match); |
| 258 | if (!np) |
| 259 | return -ENODEV; |
| 260 | |
| 261 | crossbar_of_init(np); |
| 262 | return 0; |
| 263 | } |