xref: /linux/drivers/irqchip/irq-ixp4xx.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
15b978c10SLinus Walleij // SPDX-License-Identifier: GPL-2.0
25b978c10SLinus Walleij /*
35b978c10SLinus Walleij  * irqchip for the IXP4xx interrupt controller
45b978c10SLinus Walleij  * Copyright (C) 2019 Linus Walleij <linus.walleij@linaro.org>
55b978c10SLinus Walleij  *
65b978c10SLinus Walleij  * Based on arch/arm/mach-ixp4xx/common.c
75b978c10SLinus Walleij  * Copyright 2002 (C) Intel Corporation
85b978c10SLinus Walleij  * Copyright 2003-2004 (C) MontaVista, Software, Inc.
95b978c10SLinus Walleij  * Copyright (C) Deepak Saxena <dsaxena@plexity.net>
105b978c10SLinus Walleij  */
115b978c10SLinus Walleij #include <linux/bitops.h>
125b978c10SLinus Walleij #include <linux/gpio/driver.h>
135b978c10SLinus Walleij #include <linux/irq.h>
145b978c10SLinus Walleij #include <linux/io.h>
155b978c10SLinus Walleij #include <linux/irqchip.h>
165b978c10SLinus Walleij #include <linux/irqdomain.h>
17f1497f3dSLinus Walleij #include <linux/of.h>
18f1497f3dSLinus Walleij #include <linux/of_address.h>
19f1497f3dSLinus Walleij #include <linux/of_irq.h>
205b978c10SLinus Walleij #include <linux/platform_device.h>
215b978c10SLinus Walleij #include <linux/cpu.h>
225b978c10SLinus Walleij 
235b978c10SLinus Walleij #include <asm/exception.h>
245b978c10SLinus Walleij #include <asm/mach/irq.h>
255b978c10SLinus Walleij 
265b978c10SLinus Walleij #define IXP4XX_ICPR	0x00 /* Interrupt Status */
275b978c10SLinus Walleij #define IXP4XX_ICMR	0x04 /* Interrupt Enable */
285b978c10SLinus Walleij #define IXP4XX_ICLR	0x08 /* Interrupt IRQ/FIQ Select */
295b978c10SLinus Walleij #define IXP4XX_ICIP	0x0C /* IRQ Status */
305b978c10SLinus Walleij #define IXP4XX_ICFP	0x10 /* FIQ Status */
315b978c10SLinus Walleij #define IXP4XX_ICHR	0x14 /* Interrupt Priority */
325b978c10SLinus Walleij #define IXP4XX_ICIH	0x18 /* IRQ Highest Pri Int */
335b978c10SLinus Walleij #define IXP4XX_ICFH	0x1C /* FIQ Highest Pri Int */
345b978c10SLinus Walleij 
355b978c10SLinus Walleij /* IXP43x and IXP46x-only */
365b978c10SLinus Walleij #define	IXP4XX_ICPR2	0x20 /* Interrupt Status 2 */
375b978c10SLinus Walleij #define	IXP4XX_ICMR2	0x24 /* Interrupt Enable 2 */
385b978c10SLinus Walleij #define	IXP4XX_ICLR2	0x28 /* Interrupt IRQ/FIQ Select 2 */
395b978c10SLinus Walleij #define IXP4XX_ICIP2	0x2C /* IRQ Status */
405b978c10SLinus Walleij #define IXP4XX_ICFP2	0x30 /* FIQ Status */
415b978c10SLinus Walleij #define IXP4XX_ICEEN	0x34 /* Error High Pri Enable */
425b978c10SLinus Walleij 
435b978c10SLinus Walleij /**
445b978c10SLinus Walleij  * struct ixp4xx_irq - state container for the Faraday IRQ controller
455b978c10SLinus Walleij  * @irqbase: IRQ controller memory base in virtual memory
465b978c10SLinus Walleij  * @is_356: if this is an IXP43x, IXP45x or IX46x SoC (with 64 IRQs)
475b978c10SLinus Walleij  * @irqchip: irqchip for this instance
485b978c10SLinus Walleij  * @domain: IRQ domain for this instance
495b978c10SLinus Walleij  */
505b978c10SLinus Walleij struct ixp4xx_irq {
515b978c10SLinus Walleij 	void __iomem *irqbase;
525b978c10SLinus Walleij 	bool is_356;
535b978c10SLinus Walleij 	struct irq_chip irqchip;
545b978c10SLinus Walleij 	struct irq_domain *domain;
555b978c10SLinus Walleij };
565b978c10SLinus Walleij 
575b978c10SLinus Walleij /* Local static state container */
585b978c10SLinus Walleij static struct ixp4xx_irq ixirq;
595b978c10SLinus Walleij 
605b978c10SLinus Walleij /* GPIO Clocks */
615b978c10SLinus Walleij #define IXP4XX_GPIO_CLK_0		14
625b978c10SLinus Walleij #define IXP4XX_GPIO_CLK_1		15
635b978c10SLinus Walleij 
ixp4xx_set_irq_type(struct irq_data * d,unsigned int type)645b978c10SLinus Walleij static int ixp4xx_set_irq_type(struct irq_data *d, unsigned int type)
655b978c10SLinus Walleij {
665b978c10SLinus Walleij 	/* All are level active high (asserted) here */
675b978c10SLinus Walleij 	if (type != IRQ_TYPE_LEVEL_HIGH)
685b978c10SLinus Walleij 		return -EINVAL;
695b978c10SLinus Walleij 	return 0;
705b978c10SLinus Walleij }
715b978c10SLinus Walleij 
ixp4xx_irq_mask(struct irq_data * d)725b978c10SLinus Walleij static void ixp4xx_irq_mask(struct irq_data *d)
735b978c10SLinus Walleij {
745b978c10SLinus Walleij 	struct ixp4xx_irq *ixi = irq_data_get_irq_chip_data(d);
755b978c10SLinus Walleij 	u32 val;
765b978c10SLinus Walleij 
775b978c10SLinus Walleij 	if (ixi->is_356 && d->hwirq >= 32) {
785b978c10SLinus Walleij 		val = __raw_readl(ixi->irqbase + IXP4XX_ICMR2);
795b978c10SLinus Walleij 		val &= ~BIT(d->hwirq - 32);
805b978c10SLinus Walleij 		__raw_writel(val, ixi->irqbase + IXP4XX_ICMR2);
815b978c10SLinus Walleij 	} else {
825b978c10SLinus Walleij 		val = __raw_readl(ixi->irqbase + IXP4XX_ICMR);
835b978c10SLinus Walleij 		val &= ~BIT(d->hwirq);
845b978c10SLinus Walleij 		__raw_writel(val, ixi->irqbase + IXP4XX_ICMR);
855b978c10SLinus Walleij 	}
865b978c10SLinus Walleij }
875b978c10SLinus Walleij 
885b978c10SLinus Walleij /*
895b978c10SLinus Walleij  * Level triggered interrupts on GPIO lines can only be cleared when the
905b978c10SLinus Walleij  * interrupt condition disappears.
915b978c10SLinus Walleij  */
ixp4xx_irq_unmask(struct irq_data * d)925b978c10SLinus Walleij static void ixp4xx_irq_unmask(struct irq_data *d)
935b978c10SLinus Walleij {
945b978c10SLinus Walleij 	struct ixp4xx_irq *ixi = irq_data_get_irq_chip_data(d);
955b978c10SLinus Walleij 	u32 val;
965b978c10SLinus Walleij 
975b978c10SLinus Walleij 	if (ixi->is_356 && d->hwirq >= 32) {
985b978c10SLinus Walleij 		val = __raw_readl(ixi->irqbase + IXP4XX_ICMR2);
995b978c10SLinus Walleij 		val |= BIT(d->hwirq - 32);
1005b978c10SLinus Walleij 		__raw_writel(val, ixi->irqbase + IXP4XX_ICMR2);
1015b978c10SLinus Walleij 	} else {
1025b978c10SLinus Walleij 		val = __raw_readl(ixi->irqbase + IXP4XX_ICMR);
1035b978c10SLinus Walleij 		val |= BIT(d->hwirq);
1045b978c10SLinus Walleij 		__raw_writel(val, ixi->irqbase + IXP4XX_ICMR);
1055b978c10SLinus Walleij 	}
1065b978c10SLinus Walleij }
1075b978c10SLinus Walleij 
ixp4xx_handle_irq(struct pt_regs * regs)108*b8fb82e4SJinjie Ruan static void __exception_irq_entry ixp4xx_handle_irq(struct pt_regs *regs)
1095b978c10SLinus Walleij {
1105b978c10SLinus Walleij 	struct ixp4xx_irq *ixi = &ixirq;
1115b978c10SLinus Walleij 	unsigned long status;
1125b978c10SLinus Walleij 	int i;
1135b978c10SLinus Walleij 
1145b978c10SLinus Walleij 	status = __raw_readl(ixi->irqbase + IXP4XX_ICIP);
1155b978c10SLinus Walleij 	for_each_set_bit(i, &status, 32)
1160953fb26SMark Rutland 		generic_handle_domain_irq(ixi->domain, i);
1175b978c10SLinus Walleij 
1185b978c10SLinus Walleij 	/*
1195b978c10SLinus Walleij 	 * IXP465/IXP435 has an upper IRQ status register
1205b978c10SLinus Walleij 	 */
1215b978c10SLinus Walleij 	if (ixi->is_356) {
1225b978c10SLinus Walleij 		status = __raw_readl(ixi->irqbase + IXP4XX_ICIP2);
1235b978c10SLinus Walleij 		for_each_set_bit(i, &status, 32)
1240953fb26SMark Rutland 			generic_handle_domain_irq(ixi->domain, i + 32);
1255b978c10SLinus Walleij 	}
1265b978c10SLinus Walleij }
1275b978c10SLinus Walleij 
ixp4xx_irq_domain_translate(struct irq_domain * domain,struct irq_fwspec * fwspec,unsigned long * hwirq,unsigned int * type)1285b978c10SLinus Walleij static int ixp4xx_irq_domain_translate(struct irq_domain *domain,
1295b978c10SLinus Walleij 				       struct irq_fwspec *fwspec,
1305b978c10SLinus Walleij 				       unsigned long *hwirq,
1315b978c10SLinus Walleij 				       unsigned int *type)
1325b978c10SLinus Walleij {
1335b978c10SLinus Walleij 	/* We support standard DT translation */
1345b978c10SLinus Walleij 	if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1355b978c10SLinus Walleij 		*hwirq = fwspec->param[0];
1365b978c10SLinus Walleij 		*type = fwspec->param[1];
1375b978c10SLinus Walleij 		return 0;
1385b978c10SLinus Walleij 	}
1395b978c10SLinus Walleij 
1405b978c10SLinus Walleij 	if (is_fwnode_irqchip(fwspec->fwnode)) {
1415b978c10SLinus Walleij 		if (fwspec->param_count != 2)
1425b978c10SLinus Walleij 			return -EINVAL;
1435b978c10SLinus Walleij 		*hwirq = fwspec->param[0];
1445b978c10SLinus Walleij 		*type = fwspec->param[1];
1455b978c10SLinus Walleij 		WARN_ON(*type == IRQ_TYPE_NONE);
1465b978c10SLinus Walleij 		return 0;
1475b978c10SLinus Walleij 	}
1485b978c10SLinus Walleij 
1495b978c10SLinus Walleij 	return -EINVAL;
1505b978c10SLinus Walleij }
1515b978c10SLinus Walleij 
ixp4xx_irq_domain_alloc(struct irq_domain * d,unsigned int irq,unsigned int nr_irqs,void * data)1525b978c10SLinus Walleij static int ixp4xx_irq_domain_alloc(struct irq_domain *d,
1535b978c10SLinus Walleij 				   unsigned int irq, unsigned int nr_irqs,
1545b978c10SLinus Walleij 				   void *data)
1555b978c10SLinus Walleij {
1565b978c10SLinus Walleij 	struct ixp4xx_irq *ixi = d->host_data;
1575b978c10SLinus Walleij 	irq_hw_number_t hwirq;
1585b978c10SLinus Walleij 	unsigned int type = IRQ_TYPE_NONE;
1595b978c10SLinus Walleij 	struct irq_fwspec *fwspec = data;
1605b978c10SLinus Walleij 	int ret;
1615b978c10SLinus Walleij 	int i;
1625b978c10SLinus Walleij 
1635b978c10SLinus Walleij 	ret = ixp4xx_irq_domain_translate(d, fwspec, &hwirq, &type);
1645b978c10SLinus Walleij 	if (ret)
1655b978c10SLinus Walleij 		return ret;
1665b978c10SLinus Walleij 
1675b978c10SLinus Walleij 	for (i = 0; i < nr_irqs; i++) {
1685b978c10SLinus Walleij 		/*
1695b978c10SLinus Walleij 		 * TODO: after converting IXP4xx to only device tree, set
1705b978c10SLinus Walleij 		 * handle_bad_irq as default handler and assume all consumers
1715b978c10SLinus Walleij 		 * call .set_type() as this is provided in the second cell in
1725b978c10SLinus Walleij 		 * the device tree phandle.
1735b978c10SLinus Walleij 		 */
1745b978c10SLinus Walleij 		irq_domain_set_info(d,
1755b978c10SLinus Walleij 				    irq + i,
1765b978c10SLinus Walleij 				    hwirq + i,
1775b978c10SLinus Walleij 				    &ixi->irqchip,
1785b978c10SLinus Walleij 				    ixi,
1795b978c10SLinus Walleij 				    handle_level_irq,
1805b978c10SLinus Walleij 				    NULL, NULL);
1815b978c10SLinus Walleij 		irq_set_probe(irq + i);
1825b978c10SLinus Walleij 	}
1835b978c10SLinus Walleij 
1845b978c10SLinus Walleij 	return 0;
1855b978c10SLinus Walleij }
1865b978c10SLinus Walleij 
1875b978c10SLinus Walleij /*
1885b978c10SLinus Walleij  * This needs to be a hierarchical irqdomain to work well with the
1895b978c10SLinus Walleij  * GPIO irqchip (which is lower in the hierarchy)
1905b978c10SLinus Walleij  */
1915b978c10SLinus Walleij static const struct irq_domain_ops ixp4xx_irqdomain_ops = {
1925b978c10SLinus Walleij 	.translate = ixp4xx_irq_domain_translate,
1935b978c10SLinus Walleij 	.alloc = ixp4xx_irq_domain_alloc,
1945b978c10SLinus Walleij 	.free = irq_domain_free_irqs_common,
1955b978c10SLinus Walleij };
1965b978c10SLinus Walleij 
1975b978c10SLinus Walleij /**
1985b978c10SLinus Walleij  * ixp4x_irq_setup() - Common setup code for the IXP4xx interrupt controller
1995b978c10SLinus Walleij  * @ixi: State container
2005b978c10SLinus Walleij  * @irqbase: Virtual memory base for the interrupt controller
2015b978c10SLinus Walleij  * @fwnode: Corresponding fwnode abstraction for this controller
2025b978c10SLinus Walleij  * @is_356: if this is an IXP43x, IXP45x or IXP46x SoC variant
2035b978c10SLinus Walleij  */
ixp4xx_irq_setup(struct ixp4xx_irq * ixi,void __iomem * irqbase,struct fwnode_handle * fwnode,bool is_356)2044ea10150SArnd Bergmann static int __init ixp4xx_irq_setup(struct ixp4xx_irq *ixi,
2055b978c10SLinus Walleij 				   void __iomem *irqbase,
2065b978c10SLinus Walleij 				   struct fwnode_handle *fwnode,
2075b978c10SLinus Walleij 				   bool is_356)
2085b978c10SLinus Walleij {
2095b978c10SLinus Walleij 	int nr_irqs;
2105b978c10SLinus Walleij 
2115b978c10SLinus Walleij 	ixi->irqbase = irqbase;
2125b978c10SLinus Walleij 	ixi->is_356 = is_356;
2135b978c10SLinus Walleij 
2145b978c10SLinus Walleij 	/* Route all sources to IRQ instead of FIQ */
2155b978c10SLinus Walleij 	__raw_writel(0x0, ixi->irqbase + IXP4XX_ICLR);
2165b978c10SLinus Walleij 
2175b978c10SLinus Walleij 	/* Disable all interrupts */
2185b978c10SLinus Walleij 	__raw_writel(0x0, ixi->irqbase + IXP4XX_ICMR);
2195b978c10SLinus Walleij 
2205b978c10SLinus Walleij 	if (is_356) {
2215b978c10SLinus Walleij 		/* Route upper 32 sources to IRQ instead of FIQ */
2225b978c10SLinus Walleij 		__raw_writel(0x0, ixi->irqbase + IXP4XX_ICLR2);
2235b978c10SLinus Walleij 
2245b978c10SLinus Walleij 		/* Disable upper 32 interrupts */
2255b978c10SLinus Walleij 		__raw_writel(0x0, ixi->irqbase + IXP4XX_ICMR2);
2265b978c10SLinus Walleij 
2275b978c10SLinus Walleij 		nr_irqs = 64;
2285b978c10SLinus Walleij 	} else {
2295b978c10SLinus Walleij 		nr_irqs = 32;
2305b978c10SLinus Walleij 	}
2315b978c10SLinus Walleij 
2325b978c10SLinus Walleij 	ixi->irqchip.name = "IXP4xx";
2335b978c10SLinus Walleij 	ixi->irqchip.irq_mask = ixp4xx_irq_mask;
2345b978c10SLinus Walleij 	ixi->irqchip.irq_unmask	= ixp4xx_irq_unmask;
2355b978c10SLinus Walleij 	ixi->irqchip.irq_set_type = ixp4xx_set_irq_type;
2365b978c10SLinus Walleij 
2375b978c10SLinus Walleij 	ixi->domain = irq_domain_create_linear(fwnode, nr_irqs,
2385b978c10SLinus Walleij 					       &ixp4xx_irqdomain_ops,
2395b978c10SLinus Walleij 					       ixi);
2405b978c10SLinus Walleij 	if (!ixi->domain) {
2415b978c10SLinus Walleij 		pr_crit("IXP4XX: can not add primary irqdomain\n");
2425b978c10SLinus Walleij 		return -ENODEV;
2435b978c10SLinus Walleij 	}
2445b978c10SLinus Walleij 
2455b978c10SLinus Walleij 	set_handle_irq(ixp4xx_handle_irq);
2465b978c10SLinus Walleij 
2475b978c10SLinus Walleij 	return 0;
2485b978c10SLinus Walleij }
2495b978c10SLinus Walleij 
ixp4xx_of_init_irq(struct device_node * np,struct device_node * parent)250c83227a5SLinus Walleij static int __init ixp4xx_of_init_irq(struct device_node *np,
251f1497f3dSLinus Walleij 				     struct device_node *parent)
252f1497f3dSLinus Walleij {
253f1497f3dSLinus Walleij 	struct ixp4xx_irq *ixi = &ixirq;
254f1497f3dSLinus Walleij 	void __iomem *base;
255f1497f3dSLinus Walleij 	struct fwnode_handle *fwnode;
256f1497f3dSLinus Walleij 	bool is_356;
257f1497f3dSLinus Walleij 	int ret;
258f1497f3dSLinus Walleij 
259f1497f3dSLinus Walleij 	base = of_iomap(np, 0);
260f1497f3dSLinus Walleij 	if (!base) {
261f1497f3dSLinus Walleij 		pr_crit("IXP4XX: could not ioremap interrupt controller\n");
262f1497f3dSLinus Walleij 		return -ENODEV;
263f1497f3dSLinus Walleij 	}
264f1497f3dSLinus Walleij 	fwnode = of_node_to_fwnode(np);
265f1497f3dSLinus Walleij 
266f1497f3dSLinus Walleij 	/* These chip variants have 64 interrupts */
267f1497f3dSLinus Walleij 	is_356 = of_device_is_compatible(np, "intel,ixp43x-interrupt") ||
268f1497f3dSLinus Walleij 		of_device_is_compatible(np, "intel,ixp45x-interrupt") ||
269f1497f3dSLinus Walleij 		of_device_is_compatible(np, "intel,ixp46x-interrupt");
270f1497f3dSLinus Walleij 
271f1497f3dSLinus Walleij 	ret = ixp4xx_irq_setup(ixi, base, fwnode, is_356);
272f1497f3dSLinus Walleij 	if (ret)
273f1497f3dSLinus Walleij 		pr_crit("IXP4XX: failed to set up irqchip\n");
274f1497f3dSLinus Walleij 
275f1497f3dSLinus Walleij 	return ret;
276f1497f3dSLinus Walleij }
277f1497f3dSLinus Walleij IRQCHIP_DECLARE(ixp42x, "intel,ixp42x-interrupt",
278f1497f3dSLinus Walleij 		ixp4xx_of_init_irq);
279f1497f3dSLinus Walleij IRQCHIP_DECLARE(ixp43x, "intel,ixp43x-interrupt",
280f1497f3dSLinus Walleij 		ixp4xx_of_init_irq);
281f1497f3dSLinus Walleij IRQCHIP_DECLARE(ixp45x, "intel,ixp45x-interrupt",
282f1497f3dSLinus Walleij 		ixp4xx_of_init_irq);
283f1497f3dSLinus Walleij IRQCHIP_DECLARE(ixp46x, "intel,ixp46x-interrupt",
284f1497f3dSLinus Walleij 		ixp4xx_of_init_irq);
285