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
intc_cascade(int irq,void * data)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
52*046a6ee2SMarc Zyngier generic_handle_domain_irq(domain, bit + (i * 32));
53b8b0145fSZhou Yanjie pending &= ~BIT(bit);
54b8b0145fSZhou Yanjie }
5544e08e70SPaul Burton }
5644e08e70SPaul Burton
5744e08e70SPaul Burton return IRQ_HANDLED;
5844e08e70SPaul Burton }
5944e08e70SPaul Burton
ingenic_intc_of_init(struct device_node * node,unsigned num_chips)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
931fd224e3SPaul Cercueil domain = irq_domain_add_linear(node, num_chips * 32,
948bc7464bSPaul Cercueil &irq_generic_chip_ops, NULL);
9552ecc876SPaul Cercueil if (!domain) {
9652ecc876SPaul Cercueil err = -ENOMEM;
9752ecc876SPaul Cercueil goto out_unmap_base;
9852ecc876SPaul Cercueil }
9952ecc876SPaul Cercueil
100208caadcSPaul Cercueil intc->domain = domain;
101208caadcSPaul Cercueil
1028bc7464bSPaul Cercueil err = irq_alloc_domain_generic_chips(domain, 32, 1, "INTC",
1038bc7464bSPaul Cercueil handle_level_irq, 0,
1048bc7464bSPaul Cercueil IRQ_NOPROBE | IRQ_LEVEL, 0);
1058bc7464bSPaul Cercueil if (err)
1068bc7464bSPaul Cercueil goto out_domain_remove;
10744e08e70SPaul Burton
1088bc7464bSPaul Cercueil for (i = 0; i < num_chips; i++) {
1098bc7464bSPaul Cercueil gc = irq_get_domain_generic_chip(domain, i * 32);
11044e08e70SPaul Burton
11144e08e70SPaul Burton gc->wake_enabled = IRQ_MSK(32);
1128bc7464bSPaul Cercueil gc->reg_base = intc->base + (i * CHIP_SIZE);
11344e08e70SPaul Burton
11444e08e70SPaul Burton ct = gc->chip_types;
11544e08e70SPaul Burton ct->regs.enable = JZ_REG_INTC_CLEAR_MASK;
11644e08e70SPaul Burton ct->regs.disable = JZ_REG_INTC_SET_MASK;
11744e08e70SPaul Burton ct->chip.irq_unmask = irq_gc_unmask_enable_reg;
11844e08e70SPaul Burton ct->chip.irq_mask = irq_gc_mask_disable_reg;
11944e08e70SPaul Burton ct->chip.irq_mask_ack = irq_gc_mask_disable_reg;
12044e08e70SPaul Burton ct->chip.irq_set_wake = irq_gc_set_wake;
12120b44b4dSPaul Cercueil ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND;
12244e08e70SPaul Burton
1238bc7464bSPaul Cercueil /* Mask all irqs */
1248bc7464bSPaul Cercueil irq_reg_writel(gc, IRQ_MSK(32), JZ_REG_INTC_SET_MASK);
12544e08e70SPaul Burton }
12644e08e70SPaul Burton
127821fc9e2SPaul Cercueil if (request_irq(parent_irq, intc_cascade, IRQF_NO_SUSPEND,
1282ef1cb76Safzal mohammed "SoC intc cascade interrupt", NULL))
1292ef1cb76Safzal mohammed pr_err("Failed to register SoC intc cascade interrupt\n");
13044e08e70SPaul Burton return 0;
13144e08e70SPaul Burton
1328bc7464bSPaul Cercueil out_domain_remove:
1338bc7464bSPaul Cercueil irq_domain_remove(domain);
13452ecc876SPaul Cercueil out_unmap_base:
13552ecc876SPaul Cercueil iounmap(intc->base);
13644e08e70SPaul Burton out_unmap_irq:
13744e08e70SPaul Burton irq_dispose_mapping(parent_irq);
13844e08e70SPaul Burton out_free:
13944e08e70SPaul Burton kfree(intc);
14044e08e70SPaul Burton out_err:
14144e08e70SPaul Burton return err;
14244e08e70SPaul Burton }
14344e08e70SPaul Burton
intc_1chip_of_init(struct device_node * node,struct device_node * parent)14444e08e70SPaul Burton static int __init intc_1chip_of_init(struct device_node *node,
14544e08e70SPaul Burton struct device_node *parent)
14644e08e70SPaul Burton {
14744e08e70SPaul Burton return ingenic_intc_of_init(node, 1);
14844e08e70SPaul Burton }
14944e08e70SPaul Burton IRQCHIP_DECLARE(jz4740_intc, "ingenic,jz4740-intc", intc_1chip_of_init);
1501047557cSPaul Cercueil IRQCHIP_DECLARE(jz4725b_intc, "ingenic,jz4725b-intc", intc_1chip_of_init);
15144e08e70SPaul Burton
intc_2chip_of_init(struct device_node * node,struct device_node * parent)15244e08e70SPaul Burton static int __init intc_2chip_of_init(struct device_node *node,
15344e08e70SPaul Burton struct device_node *parent)
15444e08e70SPaul Burton {
15544e08e70SPaul Burton return ingenic_intc_of_init(node, 2);
15644e08e70SPaul Burton }
1575fbecd23SPaul Cercueil IRQCHIP_DECLARE(jz4760_intc, "ingenic,jz4760-intc", intc_2chip_of_init);
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