Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / irqchip / irq-ixp4xx.c
blob37e0749215c70d428087f8b4fedaa320d423aeee
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * irqchip for the IXP4xx interrupt controller
4 * Copyright (C) 2019 Linus Walleij <linus.walleij@linaro.org>
6 * Based on arch/arm/mach-ixp4xx/common.c
7 * Copyright 2002 (C) Intel Corporation
8 * Copyright 2003-2004 (C) MontaVista, Software, Inc.
9 * Copyright (C) Deepak Saxena <dsaxena@plexity.net>
11 #include <linux/bitops.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/irq.h>
14 #include <linux/io.h>
15 #include <linux/irqchip.h>
16 #include <linux/irqchip/irq-ixp4xx.h>
17 #include <linux/irqdomain.h>
18 #include <linux/of.h>
19 #include <linux/of_address.h>
20 #include <linux/of_irq.h>
21 #include <linux/platform_device.h>
22 #include <linux/cpu.h>
24 #include <asm/exception.h>
25 #include <asm/mach/irq.h>
27 #define IXP4XX_ICPR 0x00 /* Interrupt Status */
28 #define IXP4XX_ICMR 0x04 /* Interrupt Enable */
29 #define IXP4XX_ICLR 0x08 /* Interrupt IRQ/FIQ Select */
30 #define IXP4XX_ICIP 0x0C /* IRQ Status */
31 #define IXP4XX_ICFP 0x10 /* FIQ Status */
32 #define IXP4XX_ICHR 0x14 /* Interrupt Priority */
33 #define IXP4XX_ICIH 0x18 /* IRQ Highest Pri Int */
34 #define IXP4XX_ICFH 0x1C /* FIQ Highest Pri Int */
36 /* IXP43x and IXP46x-only */
37 #define IXP4XX_ICPR2 0x20 /* Interrupt Status 2 */
38 #define IXP4XX_ICMR2 0x24 /* Interrupt Enable 2 */
39 #define IXP4XX_ICLR2 0x28 /* Interrupt IRQ/FIQ Select 2 */
40 #define IXP4XX_ICIP2 0x2C /* IRQ Status */
41 #define IXP4XX_ICFP2 0x30 /* FIQ Status */
42 #define IXP4XX_ICEEN 0x34 /* Error High Pri Enable */
44 /**
45 * struct ixp4xx_irq - state container for the Faraday IRQ controller
46 * @irqbase: IRQ controller memory base in virtual memory
47 * @is_356: if this is an IXP43x, IXP45x or IX46x SoC (with 64 IRQs)
48 * @irqchip: irqchip for this instance
49 * @domain: IRQ domain for this instance
51 struct ixp4xx_irq {
52 void __iomem *irqbase;
53 bool is_356;
54 struct irq_chip irqchip;
55 struct irq_domain *domain;
58 /* Local static state container */
59 static struct ixp4xx_irq ixirq;
61 /* GPIO Clocks */
62 #define IXP4XX_GPIO_CLK_0 14
63 #define IXP4XX_GPIO_CLK_1 15
65 static int ixp4xx_set_irq_type(struct irq_data *d, unsigned int type)
67 /* All are level active high (asserted) here */
68 if (type != IRQ_TYPE_LEVEL_HIGH)
69 return -EINVAL;
70 return 0;
73 static void ixp4xx_irq_mask(struct irq_data *d)
75 struct ixp4xx_irq *ixi = irq_data_get_irq_chip_data(d);
76 u32 val;
78 if (ixi->is_356 && d->hwirq >= 32) {
79 val = __raw_readl(ixi->irqbase + IXP4XX_ICMR2);
80 val &= ~BIT(d->hwirq - 32);
81 __raw_writel(val, ixi->irqbase + IXP4XX_ICMR2);
82 } else {
83 val = __raw_readl(ixi->irqbase + IXP4XX_ICMR);
84 val &= ~BIT(d->hwirq);
85 __raw_writel(val, ixi->irqbase + IXP4XX_ICMR);
90 * Level triggered interrupts on GPIO lines can only be cleared when the
91 * interrupt condition disappears.
93 static void ixp4xx_irq_unmask(struct irq_data *d)
95 struct ixp4xx_irq *ixi = irq_data_get_irq_chip_data(d);
96 u32 val;
98 if (ixi->is_356 && d->hwirq >= 32) {
99 val = __raw_readl(ixi->irqbase + IXP4XX_ICMR2);
100 val |= BIT(d->hwirq - 32);
101 __raw_writel(val, ixi->irqbase + IXP4XX_ICMR2);
102 } else {
103 val = __raw_readl(ixi->irqbase + IXP4XX_ICMR);
104 val |= BIT(d->hwirq);
105 __raw_writel(val, ixi->irqbase + IXP4XX_ICMR);
109 asmlinkage void __exception_irq_entry ixp4xx_handle_irq(struct pt_regs *regs)
111 struct ixp4xx_irq *ixi = &ixirq;
112 unsigned long status;
113 int i;
115 status = __raw_readl(ixi->irqbase + IXP4XX_ICIP);
116 for_each_set_bit(i, &status, 32)
117 handle_domain_irq(ixi->domain, i, regs);
120 * IXP465/IXP435 has an upper IRQ status register
122 if (ixi->is_356) {
123 status = __raw_readl(ixi->irqbase + IXP4XX_ICIP2);
124 for_each_set_bit(i, &status, 32)
125 handle_domain_irq(ixi->domain, i + 32, regs);
129 static int ixp4xx_irq_domain_translate(struct irq_domain *domain,
130 struct irq_fwspec *fwspec,
131 unsigned long *hwirq,
132 unsigned int *type)
134 /* We support standard DT translation */
135 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
136 *hwirq = fwspec->param[0];
137 *type = fwspec->param[1];
138 return 0;
141 if (is_fwnode_irqchip(fwspec->fwnode)) {
142 if (fwspec->param_count != 2)
143 return -EINVAL;
144 *hwirq = fwspec->param[0];
145 *type = fwspec->param[1];
146 WARN_ON(*type == IRQ_TYPE_NONE);
147 return 0;
150 return -EINVAL;
153 static int ixp4xx_irq_domain_alloc(struct irq_domain *d,
154 unsigned int irq, unsigned int nr_irqs,
155 void *data)
157 struct ixp4xx_irq *ixi = d->host_data;
158 irq_hw_number_t hwirq;
159 unsigned int type = IRQ_TYPE_NONE;
160 struct irq_fwspec *fwspec = data;
161 int ret;
162 int i;
164 ret = ixp4xx_irq_domain_translate(d, fwspec, &hwirq, &type);
165 if (ret)
166 return ret;
168 for (i = 0; i < nr_irqs; i++) {
170 * TODO: after converting IXP4xx to only device tree, set
171 * handle_bad_irq as default handler and assume all consumers
172 * call .set_type() as this is provided in the second cell in
173 * the device tree phandle.
175 irq_domain_set_info(d,
176 irq + i,
177 hwirq + i,
178 &ixi->irqchip,
179 ixi,
180 handle_level_irq,
181 NULL, NULL);
182 irq_set_probe(irq + i);
185 return 0;
189 * This needs to be a hierarchical irqdomain to work well with the
190 * GPIO irqchip (which is lower in the hierarchy)
192 static const struct irq_domain_ops ixp4xx_irqdomain_ops = {
193 .translate = ixp4xx_irq_domain_translate,
194 .alloc = ixp4xx_irq_domain_alloc,
195 .free = irq_domain_free_irqs_common,
199 * ixp4xx_get_irq_domain() - retrieve the ixp4xx irq domain
201 * This function will go away when we transition to DT probing.
203 struct irq_domain *ixp4xx_get_irq_domain(void)
205 struct ixp4xx_irq *ixi = &ixirq;
207 return ixi->domain;
209 EXPORT_SYMBOL_GPL(ixp4xx_get_irq_domain);
212 * This is the Linux IRQ to hwirq mapping table. This goes away when
213 * we have DT support as all IRQ resources are defined in the device
214 * tree. It will register all the IRQs that are not used by the hierarchical
215 * GPIO IRQ chip. The "holes" inbetween these IRQs will be requested by
216 * the GPIO driver using . This is a step-gap solution.
218 struct ixp4xx_irq_chunk {
219 int irq;
220 int hwirq;
221 int nr_irqs;
224 static const struct ixp4xx_irq_chunk ixp4xx_irq_chunks[] = {
226 .irq = 16,
227 .hwirq = 0,
228 .nr_irqs = 6,
231 .irq = 24,
232 .hwirq = 8,
233 .nr_irqs = 11,
236 .irq = 46,
237 .hwirq = 30,
238 .nr_irqs = 2,
240 /* Only on the 436 variants */
242 .irq = 48,
243 .hwirq = 32,
244 .nr_irqs = 10,
249 * ixp4x_irq_setup() - Common setup code for the IXP4xx interrupt controller
250 * @ixi: State container
251 * @irqbase: Virtual memory base for the interrupt controller
252 * @fwnode: Corresponding fwnode abstraction for this controller
253 * @is_356: if this is an IXP43x, IXP45x or IXP46x SoC variant
255 static int __init ixp4xx_irq_setup(struct ixp4xx_irq *ixi,
256 void __iomem *irqbase,
257 struct fwnode_handle *fwnode,
258 bool is_356)
260 int nr_irqs;
262 ixi->irqbase = irqbase;
263 ixi->is_356 = is_356;
265 /* Route all sources to IRQ instead of FIQ */
266 __raw_writel(0x0, ixi->irqbase + IXP4XX_ICLR);
268 /* Disable all interrupts */
269 __raw_writel(0x0, ixi->irqbase + IXP4XX_ICMR);
271 if (is_356) {
272 /* Route upper 32 sources to IRQ instead of FIQ */
273 __raw_writel(0x0, ixi->irqbase + IXP4XX_ICLR2);
275 /* Disable upper 32 interrupts */
276 __raw_writel(0x0, ixi->irqbase + IXP4XX_ICMR2);
278 nr_irqs = 64;
279 } else {
280 nr_irqs = 32;
283 ixi->irqchip.name = "IXP4xx";
284 ixi->irqchip.irq_mask = ixp4xx_irq_mask;
285 ixi->irqchip.irq_unmask = ixp4xx_irq_unmask;
286 ixi->irqchip.irq_set_type = ixp4xx_set_irq_type;
288 ixi->domain = irq_domain_create_linear(fwnode, nr_irqs,
289 &ixp4xx_irqdomain_ops,
290 ixi);
291 if (!ixi->domain) {
292 pr_crit("IXP4XX: can not add primary irqdomain\n");
293 return -ENODEV;
296 set_handle_irq(ixp4xx_handle_irq);
298 return 0;
302 * ixp4xx_irq_init() - Function to initialize the irqchip from boardfiles
303 * @irqbase: physical base for the irq controller
304 * @is_356: if this is an IXP43x, IXP45x or IXP46x SoC variant
306 void __init ixp4xx_irq_init(resource_size_t irqbase,
307 bool is_356)
309 struct ixp4xx_irq *ixi = &ixirq;
310 void __iomem *base;
311 struct fwnode_handle *fwnode;
312 struct irq_fwspec fwspec;
313 int nr_chunks;
314 int ret;
315 int i;
317 base = ioremap(irqbase, 0x100);
318 if (!base) {
319 pr_crit("IXP4XX: could not ioremap interrupt controller\n");
320 return;
322 fwnode = irq_domain_alloc_fwnode(&irqbase);
323 if (!fwnode) {
324 pr_crit("IXP4XX: no domain handle\n");
325 return;
327 ret = ixp4xx_irq_setup(ixi, base, fwnode, is_356);
328 if (ret) {
329 pr_crit("IXP4XX: failed to set up irqchip\n");
330 irq_domain_free_fwnode(fwnode);
333 nr_chunks = ARRAY_SIZE(ixp4xx_irq_chunks);
334 if (!is_356)
335 nr_chunks--;
338 * After adding OF support, this is no longer needed: irqs
339 * will be allocated for the respective fwnodes.
341 for (i = 0; i < nr_chunks; i++) {
342 const struct ixp4xx_irq_chunk *chunk = &ixp4xx_irq_chunks[i];
344 pr_info("Allocate Linux IRQs %d..%d HW IRQs %d..%d\n",
345 chunk->irq, chunk->irq + chunk->nr_irqs - 1,
346 chunk->hwirq, chunk->hwirq + chunk->nr_irqs - 1);
347 fwspec.fwnode = fwnode;
348 fwspec.param[0] = chunk->hwirq;
349 fwspec.param[1] = IRQ_TYPE_LEVEL_HIGH;
350 fwspec.param_count = 2;
351 ret = __irq_domain_alloc_irqs(ixi->domain,
352 chunk->irq,
353 chunk->nr_irqs,
354 NUMA_NO_NODE,
355 &fwspec,
356 false,
357 NULL);
358 if (ret < 0) {
359 pr_crit("IXP4XX: can not allocate irqs in hierarchy %d\n",
360 ret);
361 return;
365 EXPORT_SYMBOL_GPL(ixp4xx_irq_init);
367 #ifdef CONFIG_OF
368 int __init ixp4xx_of_init_irq(struct device_node *np,
369 struct device_node *parent)
371 struct ixp4xx_irq *ixi = &ixirq;
372 void __iomem *base;
373 struct fwnode_handle *fwnode;
374 bool is_356;
375 int ret;
377 base = of_iomap(np, 0);
378 if (!base) {
379 pr_crit("IXP4XX: could not ioremap interrupt controller\n");
380 return -ENODEV;
382 fwnode = of_node_to_fwnode(np);
384 /* These chip variants have 64 interrupts */
385 is_356 = of_device_is_compatible(np, "intel,ixp43x-interrupt") ||
386 of_device_is_compatible(np, "intel,ixp45x-interrupt") ||
387 of_device_is_compatible(np, "intel,ixp46x-interrupt");
389 ret = ixp4xx_irq_setup(ixi, base, fwnode, is_356);
390 if (ret)
391 pr_crit("IXP4XX: failed to set up irqchip\n");
393 return ret;
395 IRQCHIP_DECLARE(ixp42x, "intel,ixp42x-interrupt",
396 ixp4xx_of_init_irq);
397 IRQCHIP_DECLARE(ixp43x, "intel,ixp43x-interrupt",
398 ixp4xx_of_init_irq);
399 IRQCHIP_DECLARE(ixp45x, "intel,ixp45x-interrupt",
400 ixp4xx_of_init_irq);
401 IRQCHIP_DECLARE(ixp46x, "intel,ixp46x-interrupt",
402 ixp4xx_of_init_irq);
403 #endif