xref: /linux/drivers/irqchip/irq-ingenic.c (revision 208caadce5d4d38f48af965206bbd4473d265080)
1a912e80bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
244e08e70SPaul Burton /*
344e08e70SPaul Burton  *  Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
444e08e70SPaul Burton  *  JZ4740 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 #include <asm/mach-jz4740/irq.h>
2144e08e70SPaul Burton 
2244e08e70SPaul Burton struct ingenic_intc_data {
2344e08e70SPaul Burton 	void __iomem *base;
24*208caadcSPaul Cercueil 	struct irq_domain *domain;
2544e08e70SPaul Burton 	unsigned num_chips;
2644e08e70SPaul Burton };
2744e08e70SPaul Burton 
2844e08e70SPaul Burton #define JZ_REG_INTC_STATUS	0x00
2944e08e70SPaul Burton #define JZ_REG_INTC_MASK	0x04
3044e08e70SPaul Burton #define JZ_REG_INTC_SET_MASK	0x08
3144e08e70SPaul Burton #define JZ_REG_INTC_CLEAR_MASK	0x0c
3244e08e70SPaul Burton #define JZ_REG_INTC_PENDING	0x10
3344e08e70SPaul Burton #define CHIP_SIZE		0x20
3444e08e70SPaul Burton 
3544e08e70SPaul Burton static irqreturn_t intc_cascade(int irq, void *data)
3644e08e70SPaul Burton {
3744e08e70SPaul Burton 	struct ingenic_intc_data *intc = irq_get_handler_data(irq);
38*208caadcSPaul Cercueil 	struct irq_domain *domain = intc->domain;
3944e08e70SPaul Burton 	uint32_t irq_reg;
4044e08e70SPaul Burton 	unsigned i;
4144e08e70SPaul Burton 
4244e08e70SPaul Burton 	for (i = 0; i < intc->num_chips; i++) {
4344e08e70SPaul Burton 		irq_reg = readl(intc->base + (i * CHIP_SIZE) +
4444e08e70SPaul Burton 				JZ_REG_INTC_PENDING);
4544e08e70SPaul Burton 		if (!irq_reg)
4644e08e70SPaul Burton 			continue;
4744e08e70SPaul Burton 
48*208caadcSPaul Cercueil 		irq = irq_find_mapping(domain, __fls(irq_reg) + (i * 32));
49*208caadcSPaul Cercueil 		generic_handle_irq(irq);
5044e08e70SPaul Burton 	}
5144e08e70SPaul Burton 
5244e08e70SPaul Burton 	return IRQ_HANDLED;
5344e08e70SPaul Burton }
5444e08e70SPaul Burton 
5544e08e70SPaul Burton static struct irqaction intc_cascade_action = {
5644e08e70SPaul Burton 	.handler = intc_cascade,
5744e08e70SPaul Burton 	.name = "SoC intc cascade interrupt",
5844e08e70SPaul Burton };
5944e08e70SPaul Burton 
6044e08e70SPaul Burton static int __init ingenic_intc_of_init(struct device_node *node,
6144e08e70SPaul Burton 				       unsigned num_chips)
6244e08e70SPaul Burton {
6344e08e70SPaul Burton 	struct ingenic_intc_data *intc;
6444e08e70SPaul Burton 	struct irq_chip_generic *gc;
6544e08e70SPaul Burton 	struct irq_chip_type *ct;
6644e08e70SPaul Burton 	struct irq_domain *domain;
6744e08e70SPaul Burton 	int parent_irq, err = 0;
6844e08e70SPaul Burton 	unsigned i;
6944e08e70SPaul Burton 
7044e08e70SPaul Burton 	intc = kzalloc(sizeof(*intc), GFP_KERNEL);
7144e08e70SPaul Burton 	if (!intc) {
7244e08e70SPaul Burton 		err = -ENOMEM;
7344e08e70SPaul Burton 		goto out_err;
7444e08e70SPaul Burton 	}
7544e08e70SPaul Burton 
7644e08e70SPaul Burton 	parent_irq = irq_of_parse_and_map(node, 0);
7744e08e70SPaul Burton 	if (!parent_irq) {
7844e08e70SPaul Burton 		err = -EINVAL;
7944e08e70SPaul Burton 		goto out_free;
8044e08e70SPaul Burton 	}
8144e08e70SPaul Burton 
8244e08e70SPaul Burton 	err = irq_set_handler_data(parent_irq, intc);
8344e08e70SPaul Burton 	if (err)
8444e08e70SPaul Burton 		goto out_unmap_irq;
8544e08e70SPaul Burton 
8644e08e70SPaul Burton 	intc->num_chips = num_chips;
8744e08e70SPaul Burton 	intc->base = of_iomap(node, 0);
8844e08e70SPaul Burton 	if (!intc->base) {
8944e08e70SPaul Burton 		err = -ENODEV;
9044e08e70SPaul Burton 		goto out_unmap_irq;
9144e08e70SPaul Burton 	}
9244e08e70SPaul Burton 
9352ecc876SPaul Cercueil 	domain = irq_domain_add_legacy(node, num_chips * 32,
9452ecc876SPaul Cercueil 				       JZ4740_IRQ_BASE, 0,
9552ecc876SPaul Cercueil 				       &irq_domain_simple_ops, NULL);
9652ecc876SPaul Cercueil 	if (!domain) {
9752ecc876SPaul Cercueil 		err = -ENOMEM;
9852ecc876SPaul Cercueil 		goto out_unmap_base;
9952ecc876SPaul Cercueil 	}
10052ecc876SPaul Cercueil 
101*208caadcSPaul Cercueil 	intc->domain = domain;
102*208caadcSPaul Cercueil 
10344e08e70SPaul Burton 	for (i = 0; i < num_chips; i++) {
10444e08e70SPaul Burton 		/* Mask all irqs */
10544e08e70SPaul Burton 		writel(0xffffffff, intc->base + (i * CHIP_SIZE) +
10644e08e70SPaul Burton 		       JZ_REG_INTC_SET_MASK);
10744e08e70SPaul Burton 
10844e08e70SPaul Burton 		gc = irq_alloc_generic_chip("INTC", 1,
10944e08e70SPaul Burton 					    JZ4740_IRQ_BASE + (i * 32),
11044e08e70SPaul Burton 					    intc->base + (i * CHIP_SIZE),
11144e08e70SPaul Burton 					    handle_level_irq);
11244e08e70SPaul Burton 
11344e08e70SPaul Burton 		gc->wake_enabled = IRQ_MSK(32);
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 
12444e08e70SPaul Burton 		irq_setup_generic_chip(gc, IRQ_MSK(32), 0, 0,
12544e08e70SPaul Burton 				       IRQ_NOPROBE | IRQ_LEVEL);
12644e08e70SPaul Burton 	}
12744e08e70SPaul Burton 
12844e08e70SPaul Burton 	setup_irq(parent_irq, &intc_cascade_action);
12944e08e70SPaul Burton 	return 0;
13044e08e70SPaul Burton 
13152ecc876SPaul Cercueil out_unmap_base:
13252ecc876SPaul Cercueil 	iounmap(intc->base);
13344e08e70SPaul Burton out_unmap_irq:
13444e08e70SPaul Burton 	irq_dispose_mapping(parent_irq);
13544e08e70SPaul Burton out_free:
13644e08e70SPaul Burton 	kfree(intc);
13744e08e70SPaul Burton out_err:
13844e08e70SPaul Burton 	return err;
13944e08e70SPaul Burton }
14044e08e70SPaul Burton 
14144e08e70SPaul Burton static int __init intc_1chip_of_init(struct device_node *node,
14244e08e70SPaul Burton 				     struct device_node *parent)
14344e08e70SPaul Burton {
14444e08e70SPaul Burton 	return ingenic_intc_of_init(node, 1);
14544e08e70SPaul Burton }
14644e08e70SPaul Burton IRQCHIP_DECLARE(jz4740_intc, "ingenic,jz4740-intc", intc_1chip_of_init);
1471047557cSPaul Cercueil IRQCHIP_DECLARE(jz4725b_intc, "ingenic,jz4725b-intc", intc_1chip_of_init);
14844e08e70SPaul Burton 
14944e08e70SPaul Burton static int __init intc_2chip_of_init(struct device_node *node,
15044e08e70SPaul Burton 	struct device_node *parent)
15144e08e70SPaul Burton {
15244e08e70SPaul Burton 	return ingenic_intc_of_init(node, 2);
15344e08e70SPaul Burton }
15444e08e70SPaul Burton IRQCHIP_DECLARE(jz4770_intc, "ingenic,jz4770-intc", intc_2chip_of_init);
15544e08e70SPaul Burton IRQCHIP_DECLARE(jz4775_intc, "ingenic,jz4775-intc", intc_2chip_of_init);
15644e08e70SPaul Burton IRQCHIP_DECLARE(jz4780_intc, "ingenic,jz4780-intc", intc_2chip_of_init);
157