Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. |
| 4 | * http://www.samsung.com |
| 5 | * |
| 6 | * Combiner irqchip for EXYNOS |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/export.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/io.h> |
Arnd Bergmann | d34f03d | 2013-04-10 15:31:11 +0200 | [diff] [blame] | 12 | #include <linux/slab.h> |
Javier Martinez Canillas | 6fd4899 | 2015-06-12 07:43:15 +0200 | [diff] [blame] | 13 | #include <linux/syscore_ops.h> |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 14 | #include <linux/irqdomain.h> |
Joel Porquet | 41a83e06 | 2015-07-07 17:11:46 -0400 | [diff] [blame] | 15 | #include <linux/irqchip.h> |
Catalin Marinas | de88cbb | 2013-01-18 15:31:37 +0000 | [diff] [blame] | 16 | #include <linux/irqchip/chained_irq.h> |
Naveen Krishna Chatradhi | bc64690 | 2014-09-03 11:02:09 +0530 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 18 | #include <linux/of_address.h> |
| 19 | #include <linux/of_irq.h> |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 20 | |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 21 | #define COMBINER_ENABLE_SET 0x0 |
| 22 | #define COMBINER_ENABLE_CLEAR 0x4 |
| 23 | #define COMBINER_INT_STATUS 0xC |
| 24 | |
Arnd Bergmann | 6761dcf | 2013-04-10 15:17:47 +0200 | [diff] [blame] | 25 | #define IRQ_IN_COMBINER 8 |
| 26 | |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 27 | static DEFINE_SPINLOCK(irq_controller_lock); |
| 28 | |
| 29 | struct combiner_chip_data { |
Arnd Bergmann | 20adee8 | 2013-04-18 23:57:26 +0200 | [diff] [blame] | 30 | unsigned int hwirq_offset; |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 31 | unsigned int irq_mask; |
| 32 | void __iomem *base; |
Chanho Park | df7ef46 | 2012-12-12 14:02:45 +0900 | [diff] [blame] | 33 | unsigned int parent_irq; |
Javier Martinez Canillas | 6fd4899 | 2015-06-12 07:43:15 +0200 | [diff] [blame] | 34 | #ifdef CONFIG_PM |
| 35 | u32 pm_save; |
| 36 | #endif |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 37 | }; |
| 38 | |
Javier Martinez Canillas | 6fd4899 | 2015-06-12 07:43:15 +0200 | [diff] [blame] | 39 | static struct combiner_chip_data *combiner_data; |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 40 | static struct irq_domain *combiner_irq_domain; |
Javier Martinez Canillas | 6fd4899 | 2015-06-12 07:43:15 +0200 | [diff] [blame] | 41 | static unsigned int max_nr = 20; |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 42 | |
| 43 | static inline void __iomem *combiner_base(struct irq_data *data) |
| 44 | { |
| 45 | struct combiner_chip_data *combiner_data = |
| 46 | irq_data_get_irq_chip_data(data); |
| 47 | |
| 48 | return combiner_data->base; |
| 49 | } |
| 50 | |
| 51 | static void combiner_mask_irq(struct irq_data *data) |
| 52 | { |
| 53 | u32 mask = 1 << (data->hwirq % 32); |
| 54 | |
Ben Dooks | 2a4fe14 | 2016-06-21 11:20:29 +0100 | [diff] [blame] | 55 | writel_relaxed(mask, combiner_base(data) + COMBINER_ENABLE_CLEAR); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static void combiner_unmask_irq(struct irq_data *data) |
| 59 | { |
| 60 | u32 mask = 1 << (data->hwirq % 32); |
| 61 | |
Ben Dooks | 2a4fe14 | 2016-06-21 11:20:29 +0100 | [diff] [blame] | 62 | writel_relaxed(mask, combiner_base(data) + COMBINER_ENABLE_SET); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 63 | } |
| 64 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 65 | static void combiner_handle_cascade_irq(struct irq_desc *desc) |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 66 | { |
Jiang Liu | 5b29264 | 2015-06-04 12:13:20 +0800 | [diff] [blame] | 67 | struct combiner_chip_data *chip_data = irq_desc_get_handler_data(desc); |
| 68 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Marc Zyngier | 046a6ee | 2021-05-04 17:42:18 +0100 | [diff] [blame] | 69 | unsigned int combiner_irq; |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 70 | unsigned long status; |
Marc Zyngier | 046a6ee | 2021-05-04 17:42:18 +0100 | [diff] [blame] | 71 | int ret; |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 72 | |
| 73 | chained_irq_enter(chip, desc); |
| 74 | |
| 75 | spin_lock(&irq_controller_lock); |
Ben Dooks | 2a4fe14 | 2016-06-21 11:20:29 +0100 | [diff] [blame] | 76 | status = readl_relaxed(chip_data->base + COMBINER_INT_STATUS); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 77 | spin_unlock(&irq_controller_lock); |
| 78 | status &= chip_data->irq_mask; |
| 79 | |
| 80 | if (status == 0) |
| 81 | goto out; |
| 82 | |
Arnd Bergmann | 20adee8 | 2013-04-18 23:57:26 +0200 | [diff] [blame] | 83 | combiner_irq = chip_data->hwirq_offset + __ffs(status); |
Marc Zyngier | 046a6ee | 2021-05-04 17:42:18 +0100 | [diff] [blame] | 84 | ret = generic_handle_domain_irq(combiner_irq_domain, combiner_irq); |
| 85 | if (unlikely(ret)) |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 86 | handle_bad_irq(desc); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 87 | |
| 88 | out: |
| 89 | chained_irq_exit(chip, desc); |
| 90 | } |
| 91 | |
Chanho Park | df7ef46 | 2012-12-12 14:02:45 +0900 | [diff] [blame] | 92 | #ifdef CONFIG_SMP |
| 93 | static int combiner_set_affinity(struct irq_data *d, |
| 94 | const struct cpumask *mask_val, bool force) |
| 95 | { |
| 96 | struct combiner_chip_data *chip_data = irq_data_get_irq_chip_data(d); |
| 97 | struct irq_chip *chip = irq_get_chip(chip_data->parent_irq); |
| 98 | struct irq_data *data = irq_get_irq_data(chip_data->parent_irq); |
| 99 | |
| 100 | if (chip && chip->irq_set_affinity) |
| 101 | return chip->irq_set_affinity(data, mask_val, force); |
| 102 | else |
| 103 | return -EINVAL; |
| 104 | } |
| 105 | #endif |
| 106 | |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 107 | static struct irq_chip combiner_chip = { |
Chanho Park | df7ef46 | 2012-12-12 14:02:45 +0900 | [diff] [blame] | 108 | .name = "COMBINER", |
| 109 | .irq_mask = combiner_mask_irq, |
| 110 | .irq_unmask = combiner_unmask_irq, |
| 111 | #ifdef CONFIG_SMP |
| 112 | .irq_set_affinity = combiner_set_affinity, |
| 113 | #endif |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 114 | }; |
| 115 | |
Arnd Bergmann | d34f03d | 2013-04-10 15:31:11 +0200 | [diff] [blame] | 116 | static void __init combiner_cascade_irq(struct combiner_chip_data *combiner_data, |
Chanho Park | 4e164dc | 2012-12-12 14:02:49 +0900 | [diff] [blame] | 117 | unsigned int irq) |
| 118 | { |
Thomas Gleixner | 741ff96 | 2015-06-21 21:10:49 +0200 | [diff] [blame] | 119 | irq_set_chained_handler_and_data(irq, combiner_handle_cascade_irq, |
| 120 | combiner_data); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 121 | } |
| 122 | |
Arnd Bergmann | d34f03d | 2013-04-10 15:31:11 +0200 | [diff] [blame] | 123 | static void __init combiner_init_one(struct combiner_chip_data *combiner_data, |
| 124 | unsigned int combiner_nr, |
Chanho Park | df7ef46 | 2012-12-12 14:02:45 +0900 | [diff] [blame] | 125 | void __iomem *base, unsigned int irq) |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 126 | { |
Arnd Bergmann | d34f03d | 2013-04-10 15:31:11 +0200 | [diff] [blame] | 127 | combiner_data->base = base; |
Arnd Bergmann | 20adee8 | 2013-04-18 23:57:26 +0200 | [diff] [blame] | 128 | combiner_data->hwirq_offset = (combiner_nr & ~3) * IRQ_IN_COMBINER; |
Arnd Bergmann | d34f03d | 2013-04-10 15:31:11 +0200 | [diff] [blame] | 129 | combiner_data->irq_mask = 0xff << ((combiner_nr % 4) << 3); |
| 130 | combiner_data->parent_irq = irq; |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 131 | |
| 132 | /* Disable all interrupts */ |
Ben Dooks | 2a4fe14 | 2016-06-21 11:20:29 +0100 | [diff] [blame] | 133 | writel_relaxed(combiner_data->irq_mask, base + COMBINER_ENABLE_CLEAR); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 134 | } |
| 135 | |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 136 | static int combiner_irq_domain_xlate(struct irq_domain *d, |
| 137 | struct device_node *controller, |
| 138 | const u32 *intspec, unsigned int intsize, |
| 139 | unsigned long *out_hwirq, |
| 140 | unsigned int *out_type) |
| 141 | { |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 142 | if (irq_domain_get_of_node(d) != controller) |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 143 | return -EINVAL; |
| 144 | |
| 145 | if (intsize < 2) |
| 146 | return -EINVAL; |
| 147 | |
Arnd Bergmann | 6761dcf | 2013-04-10 15:17:47 +0200 | [diff] [blame] | 148 | *out_hwirq = intspec[0] * IRQ_IN_COMBINER + intspec[1]; |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 149 | *out_type = 0; |
| 150 | |
| 151 | return 0; |
| 152 | } |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 153 | |
| 154 | static int combiner_irq_domain_map(struct irq_domain *d, unsigned int irq, |
| 155 | irq_hw_number_t hw) |
| 156 | { |
Arnd Bergmann | d34f03d | 2013-04-10 15:31:11 +0200 | [diff] [blame] | 157 | struct combiner_chip_data *combiner_data = d->host_data; |
| 158 | |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 159 | irq_set_chip_and_handler(irq, &combiner_chip, handle_level_irq); |
| 160 | irq_set_chip_data(irq, &combiner_data[hw >> 3]); |
Rob Herring | d17cab4 | 2015-08-29 18:01:22 -0500 | [diff] [blame] | 161 | irq_set_probe(irq); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Krzysztof Kozlowski | 9600973 | 2015-04-27 21:54:24 +0900 | [diff] [blame] | 166 | static const struct irq_domain_ops combiner_irq_domain_ops = { |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 167 | .xlate = combiner_irq_domain_xlate, |
| 168 | .map = combiner_irq_domain_map, |
| 169 | }; |
| 170 | |
Sachin Kamat | b8394de | 2013-06-26 17:06:37 +0530 | [diff] [blame] | 171 | static void __init combiner_init(void __iomem *combiner_base, |
Javier Martinez Canillas | 6fd4899 | 2015-06-12 07:43:15 +0200 | [diff] [blame] | 172 | struct device_node *np) |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 173 | { |
Arnd Bergmann | 863a08d | 2013-04-12 15:27:09 +0200 | [diff] [blame] | 174 | int i, irq; |
Arnd Bergmann | 6761dcf | 2013-04-10 15:17:47 +0200 | [diff] [blame] | 175 | unsigned int nr_irq; |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 176 | |
Arnd Bergmann | 6761dcf | 2013-04-10 15:17:47 +0200 | [diff] [blame] | 177 | nr_irq = max_nr * IRQ_IN_COMBINER; |
Chanho Park | 4e164dc | 2012-12-12 14:02:49 +0900 | [diff] [blame] | 178 | |
Arnd Bergmann | d34f03d | 2013-04-10 15:31:11 +0200 | [diff] [blame] | 179 | combiner_data = kcalloc(max_nr, sizeof (*combiner_data), GFP_KERNEL); |
Zhen Lei | da30e66 | 2021-06-09 22:03:35 +0800 | [diff] [blame] | 180 | if (!combiner_data) |
Arnd Bergmann | d34f03d | 2013-04-10 15:31:11 +0200 | [diff] [blame] | 181 | return; |
Chanho Park | 4e164dc | 2012-12-12 14:02:49 +0900 | [diff] [blame] | 182 | |
Chander Kashyap | 9403ac8 | 2013-10-21 06:01:40 +0900 | [diff] [blame] | 183 | combiner_irq_domain = irq_domain_add_linear(np, nr_irq, |
Arnd Bergmann | d34f03d | 2013-04-10 15:31:11 +0200 | [diff] [blame] | 184 | &combiner_irq_domain_ops, combiner_data); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 185 | if (WARN_ON(!combiner_irq_domain)) { |
Wang Long | faca10b | 2015-07-21 08:11:01 +0000 | [diff] [blame] | 186 | pr_warn("%s: irq domain init failed\n", __func__); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
| 190 | for (i = 0; i < max_nr; i++) { |
Kukjin Kim | 0f56151 | 2013-07-16 12:18:19 +0900 | [diff] [blame] | 191 | irq = irq_of_parse_and_map(np, i); |
Arnd Bergmann | 92c8e49 | 2013-04-10 15:59:58 +0200 | [diff] [blame] | 192 | |
Arnd Bergmann | d34f03d | 2013-04-10 15:31:11 +0200 | [diff] [blame] | 193 | combiner_init_one(&combiner_data[i], i, |
| 194 | combiner_base + (i >> 2) * 0x10, irq); |
| 195 | combiner_cascade_irq(&combiner_data[i], irq); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Javier Martinez Canillas | 6fd4899 | 2015-06-12 07:43:15 +0200 | [diff] [blame] | 199 | #ifdef CONFIG_PM |
| 200 | |
| 201 | /** |
| 202 | * combiner_suspend - save interrupt combiner state before suspend |
| 203 | * |
| 204 | * Save the interrupt enable set register for all combiner groups since |
| 205 | * the state is lost when the system enters into a sleep state. |
| 206 | * |
| 207 | */ |
| 208 | static int combiner_suspend(void) |
| 209 | { |
| 210 | int i; |
| 211 | |
| 212 | for (i = 0; i < max_nr; i++) |
| 213 | combiner_data[i].pm_save = |
Ben Dooks | 2a4fe14 | 2016-06-21 11:20:29 +0100 | [diff] [blame] | 214 | readl_relaxed(combiner_data[i].base + COMBINER_ENABLE_SET); |
Javier Martinez Canillas | 6fd4899 | 2015-06-12 07:43:15 +0200 | [diff] [blame] | 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * combiner_resume - restore interrupt combiner state after resume |
| 221 | * |
| 222 | * Restore the interrupt enable set register for all combiner groups since |
| 223 | * the state is lost when the system enters into a sleep state on suspend. |
| 224 | * |
| 225 | */ |
| 226 | static void combiner_resume(void) |
| 227 | { |
| 228 | int i; |
| 229 | |
| 230 | for (i = 0; i < max_nr; i++) { |
Ben Dooks | 2a4fe14 | 2016-06-21 11:20:29 +0100 | [diff] [blame] | 231 | writel_relaxed(combiner_data[i].irq_mask, |
Javier Martinez Canillas | 6fd4899 | 2015-06-12 07:43:15 +0200 | [diff] [blame] | 232 | combiner_data[i].base + COMBINER_ENABLE_CLEAR); |
Ben Dooks | 2a4fe14 | 2016-06-21 11:20:29 +0100 | [diff] [blame] | 233 | writel_relaxed(combiner_data[i].pm_save, |
Javier Martinez Canillas | 6fd4899 | 2015-06-12 07:43:15 +0200 | [diff] [blame] | 234 | combiner_data[i].base + COMBINER_ENABLE_SET); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | #else |
| 239 | #define combiner_suspend NULL |
| 240 | #define combiner_resume NULL |
| 241 | #endif |
| 242 | |
| 243 | static struct syscore_ops combiner_syscore_ops = { |
| 244 | .suspend = combiner_suspend, |
| 245 | .resume = combiner_resume, |
| 246 | }; |
| 247 | |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 248 | static int __init combiner_of_init(struct device_node *np, |
| 249 | struct device_node *parent) |
| 250 | { |
| 251 | void __iomem *combiner_base; |
| 252 | |
| 253 | combiner_base = of_iomap(np, 0); |
| 254 | if (!combiner_base) { |
| 255 | pr_err("%s: failed to map combiner registers\n", __func__); |
| 256 | return -ENXIO; |
| 257 | } |
| 258 | |
Arnd Bergmann | 6761dcf | 2013-04-10 15:17:47 +0200 | [diff] [blame] | 259 | if (of_property_read_u32(np, "samsung,combiner-nr", &max_nr)) { |
| 260 | pr_info("%s: number of combiners not specified, " |
| 261 | "setting default as %d.\n", |
| 262 | __func__, max_nr); |
| 263 | } |
| 264 | |
Javier Martinez Canillas | 6fd4899 | 2015-06-12 07:43:15 +0200 | [diff] [blame] | 265 | combiner_init(combiner_base, np); |
| 266 | |
| 267 | register_syscore_ops(&combiner_syscore_ops); |
Rob Herring | a900e5d | 2013-02-12 16:04:52 -0600 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | IRQCHIP_DECLARE(exynos4210_combiner, "samsung,exynos4210-combiner", |
| 272 | combiner_of_init); |