1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
6 * Combiner irqchip for EXYNOS
9 #include <linux/export.h>
10 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/syscore_ops.h>
14 #include <linux/irqdomain.h>
15 #include <linux/irqchip.h>
16 #include <linux/irqchip/chained_irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/of_address.h>
19 #include <linux/of_irq.h>
21 #define COMBINER_ENABLE_SET 0x0
22 #define COMBINER_ENABLE_CLEAR 0x4
23 #define COMBINER_INT_STATUS 0xC
25 #define IRQ_IN_COMBINER 8
27 static DEFINE_SPINLOCK(irq_controller_lock);
29 struct combiner_chip_data {
30 unsigned int hwirq_offset;
31 unsigned int irq_mask;
33 unsigned int parent_irq;
39 static struct combiner_chip_data *combiner_data;
40 static struct irq_domain *combiner_irq_domain;
41 static unsigned int max_nr = 20;
43 static inline void __iomem *combiner_base(struct irq_data *data)
45 struct combiner_chip_data *combiner_data =
46 irq_data_get_irq_chip_data(data);
48 return combiner_data->base;
51 static void combiner_mask_irq(struct irq_data *data)
53 u32 mask = 1 << (data->hwirq % 32);
55 writel_relaxed(mask, combiner_base(data) + COMBINER_ENABLE_CLEAR);
58 static void combiner_unmask_irq(struct irq_data *data)
60 u32 mask = 1 << (data->hwirq % 32);
62 writel_relaxed(mask, combiner_base(data) + COMBINER_ENABLE_SET);
65 static void combiner_handle_cascade_irq(struct irq_desc *desc)
67 struct combiner_chip_data *chip_data = irq_desc_get_handler_data(desc);
68 struct irq_chip *chip = irq_desc_get_chip(desc);
69 unsigned int combiner_irq;
73 chained_irq_enter(chip, desc);
75 spin_lock(&irq_controller_lock);
76 status = readl_relaxed(chip_data->base + COMBINER_INT_STATUS);
77 spin_unlock(&irq_controller_lock);
78 status &= chip_data->irq_mask;
83 combiner_irq = chip_data->hwirq_offset + __ffs(status);
84 ret = generic_handle_domain_irq(combiner_irq_domain, combiner_irq);
89 chained_irq_exit(chip, desc);
93 static int combiner_set_affinity(struct irq_data *d,
94 const struct cpumask *mask_val, bool force)
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);
100 if (chip && chip->irq_set_affinity)
101 return chip->irq_set_affinity(data, mask_val, force);
107 static struct irq_chip combiner_chip = {
109 .irq_mask = combiner_mask_irq,
110 .irq_unmask = combiner_unmask_irq,
112 .irq_set_affinity = combiner_set_affinity,
116 static void __init combiner_cascade_irq(struct combiner_chip_data *combiner_data,
119 irq_set_chained_handler_and_data(irq, combiner_handle_cascade_irq,
123 static void __init combiner_init_one(struct combiner_chip_data *combiner_data,
124 unsigned int combiner_nr,
125 void __iomem *base, unsigned int irq)
127 combiner_data->base = base;
128 combiner_data->hwirq_offset = (combiner_nr & ~3) * IRQ_IN_COMBINER;
129 combiner_data->irq_mask = 0xff << ((combiner_nr % 4) << 3);
130 combiner_data->parent_irq = irq;
132 /* Disable all interrupts */
133 writel_relaxed(combiner_data->irq_mask, base + COMBINER_ENABLE_CLEAR);
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)
142 if (irq_domain_get_of_node(d) != controller)
148 *out_hwirq = intspec[0] * IRQ_IN_COMBINER + intspec[1];
154 static int combiner_irq_domain_map(struct irq_domain *d, unsigned int irq,
157 struct combiner_chip_data *combiner_data = d->host_data;
159 irq_set_chip_and_handler(irq, &combiner_chip, handle_level_irq);
160 irq_set_chip_data(irq, &combiner_data[hw >> 3]);
166 static const struct irq_domain_ops combiner_irq_domain_ops = {
167 .xlate = combiner_irq_domain_xlate,
168 .map = combiner_irq_domain_map,
171 static void __init combiner_init(void __iomem *combiner_base,
172 struct device_node *np)
177 nr_irq = max_nr * IRQ_IN_COMBINER;
179 combiner_data = kcalloc(max_nr, sizeof (*combiner_data), GFP_KERNEL);
183 combiner_irq_domain = irq_domain_add_linear(np, nr_irq,
184 &combiner_irq_domain_ops, combiner_data);
185 if (WARN_ON(!combiner_irq_domain)) {
186 pr_warn("%s: irq domain init failed\n", __func__);
190 for (i = 0; i < max_nr; i++) {
191 irq = irq_of_parse_and_map(np, i);
193 combiner_init_one(&combiner_data[i], i,
194 combiner_base + (i >> 2) * 0x10, irq);
195 combiner_cascade_irq(&combiner_data[i], irq);
202 * combiner_suspend - save interrupt combiner state before suspend
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.
208 static int combiner_suspend(void)
212 for (i = 0; i < max_nr; i++)
213 combiner_data[i].pm_save =
214 readl_relaxed(combiner_data[i].base + COMBINER_ENABLE_SET);
220 * combiner_resume - restore interrupt combiner state after resume
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.
226 static void combiner_resume(void)
230 for (i = 0; i < max_nr; i++) {
231 writel_relaxed(combiner_data[i].irq_mask,
232 combiner_data[i].base + COMBINER_ENABLE_CLEAR);
233 writel_relaxed(combiner_data[i].pm_save,
234 combiner_data[i].base + COMBINER_ENABLE_SET);
239 #define combiner_suspend NULL
240 #define combiner_resume NULL
243 static struct syscore_ops combiner_syscore_ops = {
244 .suspend = combiner_suspend,
245 .resume = combiner_resume,
248 static int __init combiner_of_init(struct device_node *np,
249 struct device_node *parent)
251 void __iomem *combiner_base;
253 combiner_base = of_iomap(np, 0);
254 if (!combiner_base) {
255 pr_err("%s: failed to map combiner registers\n", __func__);
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",
265 combiner_init(combiner_base, np);
267 register_syscore_ops(&combiner_syscore_ops);
271 IRQCHIP_DECLARE(exynos4210_combiner, "samsung,exynos4210-combiner",