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 |
Nishanth Menon | 2f7d2fb | 2014-06-26 12:40:31 +0530 | [diff] [blame] | 29 | * @max_crossbar_sources: Maximum number of crossbar sources |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 30 | * @irq_map: array of interrupts to crossbar number mapping |
| 31 | * @crossbar_base: crossbar base address |
| 32 | * @register_offsets: offsets for each irq number |
Nishanth Menon | e30ef8a | 2014-06-26 12:40:26 +0530 | [diff] [blame] | 33 | * @write: register write function pointer |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 34 | */ |
| 35 | struct crossbar_device { |
| 36 | uint int_max; |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 37 | uint safe_map; |
Nishanth Menon | 2f7d2fb | 2014-06-26 12:40:31 +0530 | [diff] [blame] | 38 | uint max_crossbar_sources; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 39 | uint *irq_map; |
| 40 | void __iomem *crossbar_base; |
| 41 | int *register_offsets; |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 42 | void (*write)(int, int); |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static struct crossbar_device *cb; |
| 46 | |
| 47 | static inline void crossbar_writel(int irq_no, int cb_no) |
| 48 | { |
| 49 | writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); |
| 50 | } |
| 51 | |
| 52 | static inline void crossbar_writew(int irq_no, int cb_no) |
| 53 | { |
| 54 | writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); |
| 55 | } |
| 56 | |
| 57 | static inline void crossbar_writeb(int irq_no, int cb_no) |
| 58 | { |
| 59 | writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); |
| 60 | } |
| 61 | |
Nishanth Menon | 6f16fc8 | 2014-06-26 12:40:20 +0530 | [diff] [blame] | 62 | static inline int get_prev_map_irq(int cb_no) |
| 63 | { |
| 64 | int i; |
| 65 | |
Nishanth Menon | ddee0fb | 2014-06-26 12:40:23 +0530 | [diff] [blame] | 66 | for (i = cb->int_max - 1; i >= 0; i--) |
Nishanth Menon | 6f16fc8 | 2014-06-26 12:40:20 +0530 | [diff] [blame] | 67 | if (cb->irq_map[i] == cb_no) |
| 68 | return i; |
| 69 | |
| 70 | return -ENODEV; |
| 71 | } |
| 72 | |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 73 | static inline int allocate_free_irq(int cb_no) |
| 74 | { |
| 75 | int i; |
| 76 | |
Nishanth Menon | ddee0fb | 2014-06-26 12:40:23 +0530 | [diff] [blame] | 77 | for (i = cb->int_max - 1; i >= 0; i--) { |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 78 | if (cb->irq_map[i] == IRQ_FREE) { |
| 79 | cb->irq_map[i] = cb_no; |
| 80 | return i; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return -ENODEV; |
| 85 | } |
| 86 | |
Nishanth Menon | 29918b6 | 2014-06-26 12:40:32 +0530 | [diff] [blame^] | 87 | static inline bool needs_crossbar_write(irq_hw_number_t hw) |
| 88 | { |
| 89 | if (hw > GIC_IRQ_START) |
| 90 | return true; |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 95 | static int crossbar_domain_map(struct irq_domain *d, unsigned int irq, |
| 96 | irq_hw_number_t hw) |
| 97 | { |
Nishanth Menon | 29918b6 | 2014-06-26 12:40:32 +0530 | [diff] [blame^] | 98 | if (needs_crossbar_write(hw)) |
| 99 | cb->write(hw - GIC_IRQ_START, cb->irq_map[hw - GIC_IRQ_START]); |
| 100 | |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Sricharan R | 8b09a45 | 2014-06-26 12:40:30 +0530 | [diff] [blame] | 104 | /** |
| 105 | * crossbar_domain_unmap - unmap a crossbar<->irq connection |
| 106 | * @d: domain of irq to unmap |
| 107 | * @irq: virq number |
| 108 | * |
| 109 | * We do not maintain a use count of total number of map/unmap |
| 110 | * calls for a particular irq to find out if a irq can be really |
| 111 | * unmapped. This is because unmap is called during irq_dispose_mapping(irq), |
| 112 | * after which irq is anyways unusable. So an explicit map has to be called |
| 113 | * after that. |
| 114 | */ |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 115 | static void crossbar_domain_unmap(struct irq_domain *d, unsigned int irq) |
| 116 | { |
| 117 | irq_hw_number_t hw = irq_get_irq_data(irq)->hwirq; |
| 118 | |
Nishanth Menon | 29918b6 | 2014-06-26 12:40:32 +0530 | [diff] [blame^] | 119 | if (needs_crossbar_write(hw)) { |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 120 | cb->irq_map[hw - GIC_IRQ_START] = IRQ_FREE; |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 121 | cb->write(hw - GIC_IRQ_START, cb->safe_map); |
| 122 | } |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static int crossbar_domain_xlate(struct irq_domain *d, |
| 126 | struct device_node *controller, |
| 127 | const u32 *intspec, unsigned int intsize, |
| 128 | unsigned long *out_hwirq, |
| 129 | unsigned int *out_type) |
| 130 | { |
Nishanth Menon | d4922a9 | 2014-06-26 12:40:24 +0530 | [diff] [blame] | 131 | int ret; |
Nishanth Menon | 2f7d2fb | 2014-06-26 12:40:31 +0530 | [diff] [blame] | 132 | int req_num = intspec[1]; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 133 | |
Nishanth Menon | 2f7d2fb | 2014-06-26 12:40:31 +0530 | [diff] [blame] | 134 | if (req_num >= cb->max_crossbar_sources) { |
| 135 | pr_err("%s: requested crossbar number %d > max %d\n", |
| 136 | __func__, req_num, cb->max_crossbar_sources); |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | |
| 140 | ret = get_prev_map_irq(req_num); |
Nishanth Menon | d4922a9 | 2014-06-26 12:40:24 +0530 | [diff] [blame] | 141 | if (ret >= 0) |
Nishanth Menon | 6f16fc8 | 2014-06-26 12:40:20 +0530 | [diff] [blame] | 142 | goto found; |
| 143 | |
Nishanth Menon | 2f7d2fb | 2014-06-26 12:40:31 +0530 | [diff] [blame] | 144 | ret = allocate_free_irq(req_num); |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 145 | |
Nishanth Menon | d4922a9 | 2014-06-26 12:40:24 +0530 | [diff] [blame] | 146 | if (ret < 0) |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 147 | return ret; |
| 148 | |
Nishanth Menon | 6f16fc8 | 2014-06-26 12:40:20 +0530 | [diff] [blame] | 149 | found: |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 150 | *out_hwirq = ret + GIC_IRQ_START; |
| 151 | return 0; |
| 152 | } |
| 153 | |
Nishanth Menon | 4dbf45e | 2014-06-26 12:40:25 +0530 | [diff] [blame] | 154 | static const struct irq_domain_ops routable_irq_domain_ops = { |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 155 | .map = crossbar_domain_map, |
| 156 | .unmap = crossbar_domain_unmap, |
| 157 | .xlate = crossbar_domain_xlate |
| 158 | }; |
| 159 | |
| 160 | static int __init crossbar_of_init(struct device_node *node) |
| 161 | { |
Nishanth Menon | edb442d | 2014-06-26 12:40:27 +0530 | [diff] [blame] | 162 | int i, size, max = 0, reserved = 0, entry; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 163 | const __be32 *irqsr; |
Nishanth Menon | edb442d | 2014-06-26 12:40:27 +0530 | [diff] [blame] | 164 | int ret = -ENOMEM; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 165 | |
Dan Carpenter | 3894e9e | 2014-04-03 10:21:34 +0300 | [diff] [blame] | 166 | cb = kzalloc(sizeof(*cb), GFP_KERNEL); |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 167 | |
| 168 | if (!cb) |
Nishanth Menon | edb442d | 2014-06-26 12:40:27 +0530 | [diff] [blame] | 169 | return ret; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 170 | |
| 171 | cb->crossbar_base = of_iomap(node, 0); |
| 172 | if (!cb->crossbar_base) |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 173 | goto err_cb; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 174 | |
Nishanth Menon | 2f7d2fb | 2014-06-26 12:40:31 +0530 | [diff] [blame] | 175 | of_property_read_u32(node, "ti,max-crossbar-sources", |
| 176 | &cb->max_crossbar_sources); |
| 177 | if (!cb->max_crossbar_sources) { |
| 178 | pr_err("missing 'ti,max-crossbar-sources' property\n"); |
| 179 | ret = -EINVAL; |
| 180 | goto err_base; |
| 181 | } |
| 182 | |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 183 | of_property_read_u32(node, "ti,max-irqs", &max); |
Nishanth Menon | edb442d | 2014-06-26 12:40:27 +0530 | [diff] [blame] | 184 | if (!max) { |
| 185 | pr_err("missing 'ti,max-irqs' property\n"); |
| 186 | ret = -EINVAL; |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 187 | goto err_base; |
Nishanth Menon | edb442d | 2014-06-26 12:40:27 +0530 | [diff] [blame] | 188 | } |
Nishanth Menon | 4dbf45e | 2014-06-26 12:40:25 +0530 | [diff] [blame] | 189 | cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL); |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 190 | if (!cb->irq_map) |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 191 | goto err_base; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 192 | |
| 193 | cb->int_max = max; |
| 194 | |
| 195 | for (i = 0; i < max; i++) |
| 196 | cb->irq_map[i] = IRQ_FREE; |
| 197 | |
| 198 | /* Get and mark reserved irqs */ |
| 199 | irqsr = of_get_property(node, "ti,irqs-reserved", &size); |
| 200 | if (irqsr) { |
| 201 | size /= sizeof(__be32); |
| 202 | |
| 203 | for (i = 0; i < size; i++) { |
| 204 | of_property_read_u32_index(node, |
| 205 | "ti,irqs-reserved", |
| 206 | i, &entry); |
| 207 | if (entry > max) { |
| 208 | pr_err("Invalid reserved entry\n"); |
Nishanth Menon | edb442d | 2014-06-26 12:40:27 +0530 | [diff] [blame] | 209 | ret = -EINVAL; |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 210 | goto err_irq_map; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 211 | } |
Nishanth Menon | 1d50d2c | 2014-06-26 12:40:19 +0530 | [diff] [blame] | 212 | cb->irq_map[entry] = IRQ_RESERVED; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Nishanth Menon | 64e0f8b | 2014-06-26 12:40:21 +0530 | [diff] [blame] | 216 | /* Skip irqs hardwired to bypass the crossbar */ |
| 217 | irqsr = of_get_property(node, "ti,irqs-skip", &size); |
| 218 | if (irqsr) { |
| 219 | size /= sizeof(__be32); |
| 220 | |
| 221 | for (i = 0; i < size; i++) { |
| 222 | of_property_read_u32_index(node, |
| 223 | "ti,irqs-skip", |
| 224 | i, &entry); |
| 225 | if (entry > max) { |
| 226 | pr_err("Invalid skip entry\n"); |
| 227 | ret = -EINVAL; |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 228 | goto err_irq_map; |
Nishanth Menon | 64e0f8b | 2014-06-26 12:40:21 +0530 | [diff] [blame] | 229 | } |
| 230 | cb->irq_map[entry] = IRQ_SKIP; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
Nishanth Menon | 4dbf45e | 2014-06-26 12:40:25 +0530 | [diff] [blame] | 235 | cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL); |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 236 | if (!cb->register_offsets) |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 237 | goto err_irq_map; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 238 | |
| 239 | of_property_read_u32(node, "ti,reg-size", &size); |
| 240 | |
| 241 | switch (size) { |
| 242 | case 1: |
| 243 | cb->write = crossbar_writeb; |
| 244 | break; |
| 245 | case 2: |
| 246 | cb->write = crossbar_writew; |
| 247 | break; |
| 248 | case 4: |
| 249 | cb->write = crossbar_writel; |
| 250 | break; |
| 251 | default: |
| 252 | pr_err("Invalid reg-size property\n"); |
Nishanth Menon | edb442d | 2014-06-26 12:40:27 +0530 | [diff] [blame] | 253 | ret = -EINVAL; |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 254 | goto err_reg_offset; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 255 | break; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Register offsets are not linear because of the |
| 260 | * reserved irqs. so find and store the offsets once. |
| 261 | */ |
| 262 | for (i = 0; i < max; i++) { |
Nishanth Menon | 1d50d2c | 2014-06-26 12:40:19 +0530 | [diff] [blame] | 263 | if (cb->irq_map[i] == IRQ_RESERVED) |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 264 | continue; |
| 265 | |
| 266 | cb->register_offsets[i] = reserved; |
| 267 | reserved += size; |
| 268 | } |
| 269 | |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 270 | of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map); |
Nishanth Menon | a35057d | 2014-06-26 12:40:22 +0530 | [diff] [blame] | 271 | /* Initialize the crossbar with safe map to start with */ |
| 272 | for (i = 0; i < max; i++) { |
| 273 | if (cb->irq_map[i] == IRQ_RESERVED || |
| 274 | cb->irq_map[i] == IRQ_SKIP) |
| 275 | continue; |
| 276 | |
| 277 | cb->write(i, cb->safe_map); |
| 278 | } |
| 279 | |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 280 | register_routable_domain_ops(&routable_irq_domain_ops); |
| 281 | return 0; |
| 282 | |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 283 | err_reg_offset: |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 284 | kfree(cb->register_offsets); |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 285 | err_irq_map: |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 286 | kfree(cb->irq_map); |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 287 | err_base: |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 288 | iounmap(cb->crossbar_base); |
Nishanth Menon | 3c44d51 | 2014-06-26 12:40:28 +0530 | [diff] [blame] | 289 | err_cb: |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 290 | kfree(cb); |
Sricharan R | 99e37d0e | 2014-06-26 12:40:29 +0530 | [diff] [blame] | 291 | |
| 292 | cb = NULL; |
Nishanth Menon | edb442d | 2014-06-26 12:40:27 +0530 | [diff] [blame] | 293 | return ret; |
Sricharan R | 96ca848 | 2013-12-03 15:57:23 +0530 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static const struct of_device_id crossbar_match[] __initconst = { |
| 297 | { .compatible = "ti,irq-crossbar" }, |
| 298 | {} |
| 299 | }; |
| 300 | |
| 301 | int __init irqcrossbar_init(void) |
| 302 | { |
| 303 | struct device_node *np; |
| 304 | np = of_find_matching_node(NULL, crossbar_match); |
| 305 | if (!np) |
| 306 | return -ENODEV; |
| 307 | |
| 308 | crossbar_of_init(np); |
| 309 | return 0; |
| 310 | } |