blob: 0b85d9a3fbff8bd8c83b23cc6697ce367934481e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Rob Herringa900e5d2013-02-12 16:04:52 -06002/*
3 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
6 * Combiner irqchip for EXYNOS
Rob Herringa900e5d2013-02-12 16:04:52 -06007 */
8#include <linux/err.h>
9#include <linux/export.h>
10#include <linux/init.h>
11#include <linux/io.h>
Arnd Bergmannd34f03d2013-04-10 15:31:11 +020012#include <linux/slab.h>
Javier Martinez Canillas6fd48992015-06-12 07:43:15 +020013#include <linux/syscore_ops.h>
Rob Herringa900e5d2013-02-12 16:04:52 -060014#include <linux/irqdomain.h>
Joel Porquet41a83e062015-07-07 17:11:46 -040015#include <linux/irqchip.h>
Catalin Marinasde88cbb2013-01-18 15:31:37 +000016#include <linux/irqchip/chained_irq.h>
Naveen Krishna Chatradhibc646902014-09-03 11:02:09 +053017#include <linux/interrupt.h>
Rob Herringa900e5d2013-02-12 16:04:52 -060018#include <linux/of_address.h>
19#include <linux/of_irq.h>
Rob Herringa900e5d2013-02-12 16:04:52 -060020
Rob Herringa900e5d2013-02-12 16:04:52 -060021#define COMBINER_ENABLE_SET 0x0
22#define COMBINER_ENABLE_CLEAR 0x4
23#define COMBINER_INT_STATUS 0xC
24
Arnd Bergmann6761dcf2013-04-10 15:17:47 +020025#define IRQ_IN_COMBINER 8
26
Rob Herringa900e5d2013-02-12 16:04:52 -060027static DEFINE_SPINLOCK(irq_controller_lock);
28
29struct combiner_chip_data {
Arnd Bergmann20adee82013-04-18 23:57:26 +020030 unsigned int hwirq_offset;
Rob Herringa900e5d2013-02-12 16:04:52 -060031 unsigned int irq_mask;
32 void __iomem *base;
Chanho Parkdf7ef462012-12-12 14:02:45 +090033 unsigned int parent_irq;
Javier Martinez Canillas6fd48992015-06-12 07:43:15 +020034#ifdef CONFIG_PM
35 u32 pm_save;
36#endif
Rob Herringa900e5d2013-02-12 16:04:52 -060037};
38
Javier Martinez Canillas6fd48992015-06-12 07:43:15 +020039static struct combiner_chip_data *combiner_data;
Rob Herringa900e5d2013-02-12 16:04:52 -060040static struct irq_domain *combiner_irq_domain;
Javier Martinez Canillas6fd48992015-06-12 07:43:15 +020041static unsigned int max_nr = 20;
Rob Herringa900e5d2013-02-12 16:04:52 -060042
43static 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
51static void combiner_mask_irq(struct irq_data *data)
52{
53 u32 mask = 1 << (data->hwirq % 32);
54
Ben Dooks2a4fe142016-06-21 11:20:29 +010055 writel_relaxed(mask, combiner_base(data) + COMBINER_ENABLE_CLEAR);
Rob Herringa900e5d2013-02-12 16:04:52 -060056}
57
58static void combiner_unmask_irq(struct irq_data *data)
59{
60 u32 mask = 1 << (data->hwirq % 32);
61
Ben Dooks2a4fe142016-06-21 11:20:29 +010062 writel_relaxed(mask, combiner_base(data) + COMBINER_ENABLE_SET);
Rob Herringa900e5d2013-02-12 16:04:52 -060063}
64
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +020065static void combiner_handle_cascade_irq(struct irq_desc *desc)
Rob Herringa900e5d2013-02-12 16:04:52 -060066{
Jiang Liu5b292642015-06-04 12:13:20 +080067 struct combiner_chip_data *chip_data = irq_desc_get_handler_data(desc);
68 struct irq_chip *chip = irq_desc_get_chip(desc);
Rob Herringa900e5d2013-02-12 16:04:52 -060069 unsigned int cascade_irq, combiner_irq;
70 unsigned long status;
71
72 chained_irq_enter(chip, desc);
73
74 spin_lock(&irq_controller_lock);
Ben Dooks2a4fe142016-06-21 11:20:29 +010075 status = readl_relaxed(chip_data->base + COMBINER_INT_STATUS);
Rob Herringa900e5d2013-02-12 16:04:52 -060076 spin_unlock(&irq_controller_lock);
77 status &= chip_data->irq_mask;
78
79 if (status == 0)
80 goto out;
81
Arnd Bergmann20adee82013-04-18 23:57:26 +020082 combiner_irq = chip_data->hwirq_offset + __ffs(status);
83 cascade_irq = irq_find_mapping(combiner_irq_domain, combiner_irq);
Rob Herringa900e5d2013-02-12 16:04:52 -060084
Arnd Bergmann20adee82013-04-18 23:57:26 +020085 if (unlikely(!cascade_irq))
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +020086 handle_bad_irq(desc);
Rob Herringa900e5d2013-02-12 16:04:52 -060087 else
88 generic_handle_irq(cascade_irq);
89
90 out:
91 chained_irq_exit(chip, desc);
92}
93
Chanho Parkdf7ef462012-12-12 14:02:45 +090094#ifdef CONFIG_SMP
95static int combiner_set_affinity(struct irq_data *d,
96 const struct cpumask *mask_val, bool force)
97{
98 struct combiner_chip_data *chip_data = irq_data_get_irq_chip_data(d);
99 struct irq_chip *chip = irq_get_chip(chip_data->parent_irq);
100 struct irq_data *data = irq_get_irq_data(chip_data->parent_irq);
101
102 if (chip && chip->irq_set_affinity)
103 return chip->irq_set_affinity(data, mask_val, force);
104 else
105 return -EINVAL;
106}
107#endif
108
Rob Herringa900e5d2013-02-12 16:04:52 -0600109static struct irq_chip combiner_chip = {
Chanho Parkdf7ef462012-12-12 14:02:45 +0900110 .name = "COMBINER",
111 .irq_mask = combiner_mask_irq,
112 .irq_unmask = combiner_unmask_irq,
113#ifdef CONFIG_SMP
114 .irq_set_affinity = combiner_set_affinity,
115#endif
Rob Herringa900e5d2013-02-12 16:04:52 -0600116};
117
Arnd Bergmannd34f03d2013-04-10 15:31:11 +0200118static void __init combiner_cascade_irq(struct combiner_chip_data *combiner_data,
Chanho Park4e164dc2012-12-12 14:02:49 +0900119 unsigned int irq)
120{
Thomas Gleixner741ff962015-06-21 21:10:49 +0200121 irq_set_chained_handler_and_data(irq, combiner_handle_cascade_irq,
122 combiner_data);
Rob Herringa900e5d2013-02-12 16:04:52 -0600123}
124
Arnd Bergmannd34f03d2013-04-10 15:31:11 +0200125static void __init combiner_init_one(struct combiner_chip_data *combiner_data,
126 unsigned int combiner_nr,
Chanho Parkdf7ef462012-12-12 14:02:45 +0900127 void __iomem *base, unsigned int irq)
Rob Herringa900e5d2013-02-12 16:04:52 -0600128{
Arnd Bergmannd34f03d2013-04-10 15:31:11 +0200129 combiner_data->base = base;
Arnd Bergmann20adee82013-04-18 23:57:26 +0200130 combiner_data->hwirq_offset = (combiner_nr & ~3) * IRQ_IN_COMBINER;
Arnd Bergmannd34f03d2013-04-10 15:31:11 +0200131 combiner_data->irq_mask = 0xff << ((combiner_nr % 4) << 3);
132 combiner_data->parent_irq = irq;
Rob Herringa900e5d2013-02-12 16:04:52 -0600133
134 /* Disable all interrupts */
Ben Dooks2a4fe142016-06-21 11:20:29 +0100135 writel_relaxed(combiner_data->irq_mask, base + COMBINER_ENABLE_CLEAR);
Rob Herringa900e5d2013-02-12 16:04:52 -0600136}
137
Rob Herringa900e5d2013-02-12 16:04:52 -0600138static int combiner_irq_domain_xlate(struct irq_domain *d,
139 struct device_node *controller,
140 const u32 *intspec, unsigned int intsize,
141 unsigned long *out_hwirq,
142 unsigned int *out_type)
143{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100144 if (irq_domain_get_of_node(d) != controller)
Rob Herringa900e5d2013-02-12 16:04:52 -0600145 return -EINVAL;
146
147 if (intsize < 2)
148 return -EINVAL;
149
Arnd Bergmann6761dcf2013-04-10 15:17:47 +0200150 *out_hwirq = intspec[0] * IRQ_IN_COMBINER + intspec[1];
Rob Herringa900e5d2013-02-12 16:04:52 -0600151 *out_type = 0;
152
153 return 0;
154}
Rob Herringa900e5d2013-02-12 16:04:52 -0600155
156static int combiner_irq_domain_map(struct irq_domain *d, unsigned int irq,
157 irq_hw_number_t hw)
158{
Arnd Bergmannd34f03d2013-04-10 15:31:11 +0200159 struct combiner_chip_data *combiner_data = d->host_data;
160
Rob Herringa900e5d2013-02-12 16:04:52 -0600161 irq_set_chip_and_handler(irq, &combiner_chip, handle_level_irq);
162 irq_set_chip_data(irq, &combiner_data[hw >> 3]);
Rob Herringd17cab42015-08-29 18:01:22 -0500163 irq_set_probe(irq);
Rob Herringa900e5d2013-02-12 16:04:52 -0600164
165 return 0;
166}
167
Krzysztof Kozlowski96009732015-04-27 21:54:24 +0900168static const struct irq_domain_ops combiner_irq_domain_ops = {
Rob Herringa900e5d2013-02-12 16:04:52 -0600169 .xlate = combiner_irq_domain_xlate,
170 .map = combiner_irq_domain_map,
171};
172
Sachin Kamatb8394de2013-06-26 17:06:37 +0530173static void __init combiner_init(void __iomem *combiner_base,
Javier Martinez Canillas6fd48992015-06-12 07:43:15 +0200174 struct device_node *np)
Rob Herringa900e5d2013-02-12 16:04:52 -0600175{
Arnd Bergmann863a08d2013-04-12 15:27:09 +0200176 int i, irq;
Arnd Bergmann6761dcf2013-04-10 15:17:47 +0200177 unsigned int nr_irq;
Rob Herringa900e5d2013-02-12 16:04:52 -0600178
Arnd Bergmann6761dcf2013-04-10 15:17:47 +0200179 nr_irq = max_nr * IRQ_IN_COMBINER;
Chanho Park4e164dc2012-12-12 14:02:49 +0900180
Arnd Bergmannd34f03d2013-04-10 15:31:11 +0200181 combiner_data = kcalloc(max_nr, sizeof (*combiner_data), GFP_KERNEL);
182 if (!combiner_data) {
Wang Longfaca10b2015-07-21 08:11:01 +0000183 pr_warn("%s: could not allocate combiner data\n", __func__);
Arnd Bergmannd34f03d2013-04-10 15:31:11 +0200184 return;
Rob Herringa900e5d2013-02-12 16:04:52 -0600185 }
Chanho Park4e164dc2012-12-12 14:02:49 +0900186
Chander Kashyap9403ac82013-10-21 06:01:40 +0900187 combiner_irq_domain = irq_domain_add_linear(np, nr_irq,
Arnd Bergmannd34f03d2013-04-10 15:31:11 +0200188 &combiner_irq_domain_ops, combiner_data);
Rob Herringa900e5d2013-02-12 16:04:52 -0600189 if (WARN_ON(!combiner_irq_domain)) {
Wang Longfaca10b2015-07-21 08:11:01 +0000190 pr_warn("%s: irq domain init failed\n", __func__);
Rob Herringa900e5d2013-02-12 16:04:52 -0600191 return;
192 }
193
194 for (i = 0; i < max_nr; i++) {
Kukjin Kim0f561512013-07-16 12:18:19 +0900195 irq = irq_of_parse_and_map(np, i);
Arnd Bergmann92c8e492013-04-10 15:59:58 +0200196
Arnd Bergmannd34f03d2013-04-10 15:31:11 +0200197 combiner_init_one(&combiner_data[i], i,
198 combiner_base + (i >> 2) * 0x10, irq);
199 combiner_cascade_irq(&combiner_data[i], irq);
Rob Herringa900e5d2013-02-12 16:04:52 -0600200 }
201}
202
Javier Martinez Canillas6fd48992015-06-12 07:43:15 +0200203#ifdef CONFIG_PM
204
205/**
206 * combiner_suspend - save interrupt combiner state before suspend
207 *
208 * Save the interrupt enable set register for all combiner groups since
209 * the state is lost when the system enters into a sleep state.
210 *
211 */
212static int combiner_suspend(void)
213{
214 int i;
215
216 for (i = 0; i < max_nr; i++)
217 combiner_data[i].pm_save =
Ben Dooks2a4fe142016-06-21 11:20:29 +0100218 readl_relaxed(combiner_data[i].base + COMBINER_ENABLE_SET);
Javier Martinez Canillas6fd48992015-06-12 07:43:15 +0200219
220 return 0;
221}
222
223/**
224 * combiner_resume - restore interrupt combiner state after resume
225 *
226 * Restore the interrupt enable set register for all combiner groups since
227 * the state is lost when the system enters into a sleep state on suspend.
228 *
229 */
230static void combiner_resume(void)
231{
232 int i;
233
234 for (i = 0; i < max_nr; i++) {
Ben Dooks2a4fe142016-06-21 11:20:29 +0100235 writel_relaxed(combiner_data[i].irq_mask,
Javier Martinez Canillas6fd48992015-06-12 07:43:15 +0200236 combiner_data[i].base + COMBINER_ENABLE_CLEAR);
Ben Dooks2a4fe142016-06-21 11:20:29 +0100237 writel_relaxed(combiner_data[i].pm_save,
Javier Martinez Canillas6fd48992015-06-12 07:43:15 +0200238 combiner_data[i].base + COMBINER_ENABLE_SET);
239 }
240}
241
242#else
243#define combiner_suspend NULL
244#define combiner_resume NULL
245#endif
246
247static struct syscore_ops combiner_syscore_ops = {
248 .suspend = combiner_suspend,
249 .resume = combiner_resume,
250};
251
Rob Herringa900e5d2013-02-12 16:04:52 -0600252static int __init combiner_of_init(struct device_node *np,
253 struct device_node *parent)
254{
255 void __iomem *combiner_base;
256
257 combiner_base = of_iomap(np, 0);
258 if (!combiner_base) {
259 pr_err("%s: failed to map combiner registers\n", __func__);
260 return -ENXIO;
261 }
262
Arnd Bergmann6761dcf2013-04-10 15:17:47 +0200263 if (of_property_read_u32(np, "samsung,combiner-nr", &max_nr)) {
264 pr_info("%s: number of combiners not specified, "
265 "setting default as %d.\n",
266 __func__, max_nr);
267 }
268
Javier Martinez Canillas6fd48992015-06-12 07:43:15 +0200269 combiner_init(combiner_base, np);
270
271 register_syscore_ops(&combiner_syscore_ops);
Rob Herringa900e5d2013-02-12 16:04:52 -0600272
273 return 0;
274}
275IRQCHIP_DECLARE(exynos4210_combiner, "samsung,exynos4210-combiner",
276 combiner_of_init);