xref: /linux/drivers/irqchip/irq-ingenic.c (revision 821fc9e261f3af235752f46e59084467cfd440c4)
1a912e80bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
244e08e70SPaul Burton /*
344e08e70SPaul Burton  *  Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
4b8b0145fSZhou Yanjie  *  Ingenic XBurst platform IRQ support
544e08e70SPaul Burton  */
644e08e70SPaul Burton 
744e08e70SPaul Burton #include <linux/errno.h>
844e08e70SPaul Burton #include <linux/init.h>
944e08e70SPaul Burton #include <linux/types.h>
1044e08e70SPaul Burton #include <linux/interrupt.h>
1144e08e70SPaul Burton #include <linux/ioport.h>
1241a83e06SJoel Porquet #include <linux/irqchip.h>
1344e08e70SPaul Burton #include <linux/of_address.h>
1444e08e70SPaul Burton #include <linux/of_irq.h>
1544e08e70SPaul Burton #include <linux/timex.h>
1644e08e70SPaul Burton #include <linux/slab.h>
1744e08e70SPaul Burton #include <linux/delay.h>
1844e08e70SPaul Burton 
1944e08e70SPaul Burton #include <asm/io.h>
2044e08e70SPaul Burton 
2144e08e70SPaul Burton struct ingenic_intc_data {
2244e08e70SPaul Burton 	void __iomem *base;
23208caadcSPaul Cercueil 	struct irq_domain *domain;
2444e08e70SPaul Burton 	unsigned num_chips;
2544e08e70SPaul Burton };
2644e08e70SPaul Burton 
2744e08e70SPaul Burton #define JZ_REG_INTC_STATUS	0x00
2844e08e70SPaul Burton #define JZ_REG_INTC_MASK	0x04
2944e08e70SPaul Burton #define JZ_REG_INTC_SET_MASK	0x08
3044e08e70SPaul Burton #define JZ_REG_INTC_CLEAR_MASK	0x0c
3144e08e70SPaul Burton #define JZ_REG_INTC_PENDING	0x10
3244e08e70SPaul Burton #define CHIP_SIZE		0x20
3344e08e70SPaul Burton 
3444e08e70SPaul Burton static irqreturn_t intc_cascade(int irq, void *data)
3544e08e70SPaul Burton {
3644e08e70SPaul Burton 	struct ingenic_intc_data *intc = irq_get_handler_data(irq);
37208caadcSPaul Cercueil 	struct irq_domain *domain = intc->domain;
388bc7464bSPaul Cercueil 	struct irq_chip_generic *gc;
39b8b0145fSZhou Yanjie 	uint32_t pending;
4044e08e70SPaul Burton 	unsigned i;
4144e08e70SPaul Burton 
4244e08e70SPaul Burton 	for (i = 0; i < intc->num_chips; i++) {
438bc7464bSPaul Cercueil 		gc = irq_get_domain_generic_chip(domain, i * 32);
448bc7464bSPaul Cercueil 
45b8b0145fSZhou Yanjie 		pending = irq_reg_readl(gc, JZ_REG_INTC_PENDING);
46b8b0145fSZhou Yanjie 		if (!pending)
4744e08e70SPaul Burton 			continue;
4844e08e70SPaul Burton 
49b8b0145fSZhou Yanjie 		while (pending) {
50b8b0145fSZhou Yanjie 			int bit = __fls(pending);
51b8b0145fSZhou Yanjie 
521fd224e3SPaul Cercueil 			irq = irq_linear_revmap(domain, bit + (i * 32));
53208caadcSPaul Cercueil 			generic_handle_irq(irq);
54b8b0145fSZhou Yanjie 			pending &= ~BIT(bit);
55b8b0145fSZhou Yanjie 		}
5644e08e70SPaul Burton 	}
5744e08e70SPaul Burton 
5844e08e70SPaul Burton 	return IRQ_HANDLED;
5944e08e70SPaul Burton }
6044e08e70SPaul Burton 
6144e08e70SPaul Burton static int __init ingenic_intc_of_init(struct device_node *node,
6244e08e70SPaul Burton 				       unsigned num_chips)
6344e08e70SPaul Burton {
6444e08e70SPaul Burton 	struct ingenic_intc_data *intc;
6544e08e70SPaul Burton 	struct irq_chip_generic *gc;
6644e08e70SPaul Burton 	struct irq_chip_type *ct;
6744e08e70SPaul Burton 	struct irq_domain *domain;
6844e08e70SPaul Burton 	int parent_irq, err = 0;
6944e08e70SPaul Burton 	unsigned i;
7044e08e70SPaul Burton 
7144e08e70SPaul Burton 	intc = kzalloc(sizeof(*intc), GFP_KERNEL);
7244e08e70SPaul Burton 	if (!intc) {
7344e08e70SPaul Burton 		err = -ENOMEM;
7444e08e70SPaul Burton 		goto out_err;
7544e08e70SPaul Burton 	}
7644e08e70SPaul Burton 
7744e08e70SPaul Burton 	parent_irq = irq_of_parse_and_map(node, 0);
7844e08e70SPaul Burton 	if (!parent_irq) {
7944e08e70SPaul Burton 		err = -EINVAL;
8044e08e70SPaul Burton 		goto out_free;
8144e08e70SPaul Burton 	}
8244e08e70SPaul Burton 
8344e08e70SPaul Burton 	err = irq_set_handler_data(parent_irq, intc);
8444e08e70SPaul Burton 	if (err)
8544e08e70SPaul Burton 		goto out_unmap_irq;
8644e08e70SPaul Burton 
8744e08e70SPaul Burton 	intc->num_chips = num_chips;
8844e08e70SPaul Burton 	intc->base = of_iomap(node, 0);
8944e08e70SPaul Burton 	if (!intc->base) {
9044e08e70SPaul Burton 		err = -ENODEV;
9144e08e70SPaul Burton 		goto out_unmap_irq;
9244e08e70SPaul Burton 	}
9344e08e70SPaul Burton 
941fd224e3SPaul Cercueil 	domain = irq_domain_add_linear(node, num_chips * 32,
958bc7464bSPaul Cercueil 				       &irq_generic_chip_ops, NULL);
9652ecc876SPaul Cercueil 	if (!domain) {
9752ecc876SPaul Cercueil 		err = -ENOMEM;
9852ecc876SPaul Cercueil 		goto out_unmap_base;
9952ecc876SPaul Cercueil 	}
10052ecc876SPaul Cercueil 
101208caadcSPaul Cercueil 	intc->domain = domain;
102208caadcSPaul Cercueil 
1038bc7464bSPaul Cercueil 	err = irq_alloc_domain_generic_chips(domain, 32, 1, "INTC",
1048bc7464bSPaul Cercueil 					     handle_level_irq, 0,
1058bc7464bSPaul Cercueil 					     IRQ_NOPROBE | IRQ_LEVEL, 0);
1068bc7464bSPaul Cercueil 	if (err)
1078bc7464bSPaul Cercueil 		goto out_domain_remove;
10844e08e70SPaul Burton 
1098bc7464bSPaul Cercueil 	for (i = 0; i < num_chips; i++) {
1108bc7464bSPaul Cercueil 		gc = irq_get_domain_generic_chip(domain, i * 32);
11144e08e70SPaul Burton 
11244e08e70SPaul Burton 		gc->wake_enabled = IRQ_MSK(32);
1138bc7464bSPaul Cercueil 		gc->reg_base = intc->base + (i * CHIP_SIZE);
11444e08e70SPaul Burton 
11544e08e70SPaul Burton 		ct = gc->chip_types;
11644e08e70SPaul Burton 		ct->regs.enable = JZ_REG_INTC_CLEAR_MASK;
11744e08e70SPaul Burton 		ct->regs.disable = JZ_REG_INTC_SET_MASK;
11844e08e70SPaul Burton 		ct->chip.irq_unmask = irq_gc_unmask_enable_reg;
11944e08e70SPaul Burton 		ct->chip.irq_mask = irq_gc_mask_disable_reg;
12044e08e70SPaul Burton 		ct->chip.irq_mask_ack = irq_gc_mask_disable_reg;
12144e08e70SPaul Burton 		ct->chip.irq_set_wake = irq_gc_set_wake;
12220b44b4dSPaul Cercueil 		ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND;
12344e08e70SPaul Burton 
1248bc7464bSPaul Cercueil 		/* Mask all irqs */
1258bc7464bSPaul Cercueil 		irq_reg_writel(gc, IRQ_MSK(32), JZ_REG_INTC_SET_MASK);
12644e08e70SPaul Burton 	}
12744e08e70SPaul Burton 
128*821fc9e2SPaul Cercueil 	if (request_irq(parent_irq, intc_cascade, IRQF_NO_SUSPEND,
1292ef1cb76Safzal mohammed 			"SoC intc cascade interrupt", NULL))
1302ef1cb76Safzal mohammed 		pr_err("Failed to register SoC intc cascade interrupt\n");
13144e08e70SPaul Burton 	return 0;
13244e08e70SPaul Burton 
1338bc7464bSPaul Cercueil out_domain_remove:
1348bc7464bSPaul Cercueil 	irq_domain_remove(domain);
13552ecc876SPaul Cercueil out_unmap_base:
13652ecc876SPaul Cercueil 	iounmap(intc->base);
13744e08e70SPaul Burton out_unmap_irq:
13844e08e70SPaul Burton 	irq_dispose_mapping(parent_irq);
13944e08e70SPaul Burton out_free:
14044e08e70SPaul Burton 	kfree(intc);
14144e08e70SPaul Burton out_err:
14244e08e70SPaul Burton 	return err;
14344e08e70SPaul Burton }
14444e08e70SPaul Burton 
14544e08e70SPaul Burton static int __init intc_1chip_of_init(struct device_node *node,
14644e08e70SPaul Burton 				     struct device_node *parent)
14744e08e70SPaul Burton {
14844e08e70SPaul Burton 	return ingenic_intc_of_init(node, 1);
14944e08e70SPaul Burton }
15044e08e70SPaul Burton IRQCHIP_DECLARE(jz4740_intc, "ingenic,jz4740-intc", intc_1chip_of_init);
1511047557cSPaul Cercueil IRQCHIP_DECLARE(jz4725b_intc, "ingenic,jz4725b-intc", intc_1chip_of_init);
15244e08e70SPaul Burton 
15344e08e70SPaul Burton static int __init intc_2chip_of_init(struct device_node *node,
15444e08e70SPaul Burton 	struct device_node *parent)
15544e08e70SPaul Burton {
15644e08e70SPaul Burton 	return ingenic_intc_of_init(node, 2);
15744e08e70SPaul Burton }
15844e08e70SPaul Burton IRQCHIP_DECLARE(jz4770_intc, "ingenic,jz4770-intc", intc_2chip_of_init);
15944e08e70SPaul Burton IRQCHIP_DECLARE(jz4775_intc, "ingenic,jz4775-intc", intc_2chip_of_init);
16044e08e70SPaul Burton IRQCHIP_DECLARE(jz4780_intc, "ingenic,jz4780-intc", intc_2chip_of_init);
161