blob: cee839ca627ee5ece3b354a8aaed53951204c22c [file] [log] [blame]
Thomas Gleixnera912e802019-05-27 08:55:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lars-Peter Clausen98698482010-07-17 11:08:43 +00002/*
3 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
Zhou Yanjieb8b01452019-10-02 19:25:25 +08004 * Ingenic XBurst platform IRQ support
Lars-Peter Clausen98698482010-07-17 11:08:43 +00005 */
6
7#include <linux/errno.h>
8#include <linux/init.h>
9#include <linux/types.h>
10#include <linux/interrupt.h>
11#include <linux/ioport.h>
Joel Porquet41a83e062015-07-07 17:11:46 -040012#include <linux/irqchip.h>
Paul Burton3aa94592015-05-24 16:11:28 +010013#include <linux/of_address.h>
Paul Burtonadbdce72015-05-24 16:11:21 +010014#include <linux/of_irq.h>
Lars-Peter Clausen98698482010-07-17 11:08:43 +000015#include <linux/timex.h>
16#include <linux/slab.h>
17#include <linux/delay.h>
18
Lars-Peter Clausen98698482010-07-17 11:08:43 +000019#include <asm/io.h>
Brian Norris942e22d2014-12-17 18:39:01 -080020
Paul Burtonfe778ec2015-05-24 16:11:25 +010021struct ingenic_intc_data {
22 void __iomem *base;
Paul Cercueil208caad2019-10-02 19:25:23 +080023 struct irq_domain *domain;
Paul Burton943d69c2015-05-24 16:11:26 +010024 unsigned num_chips;
Paul Burtonfe778ec2015-05-24 16:11:25 +010025};
Lars-Peter Clausen98698482010-07-17 11:08:43 +000026
27#define JZ_REG_INTC_STATUS 0x00
28#define JZ_REG_INTC_MASK 0x04
29#define JZ_REG_INTC_SET_MASK 0x08
30#define JZ_REG_INTC_CLEAR_MASK 0x0c
31#define JZ_REG_INTC_PENDING 0x10
Paul Burton943d69c2015-05-24 16:11:26 +010032#define CHIP_SIZE 0x20
Lars-Peter Clausen98698482010-07-17 11:08:43 +000033
Paul Burton2da01882015-05-24 16:11:29 +010034static irqreturn_t intc_cascade(int irq, void *data)
Lars-Peter Clausen98698482010-07-17 11:08:43 +000035{
Paul Burtonfe778ec2015-05-24 16:11:25 +010036 struct ingenic_intc_data *intc = irq_get_handler_data(irq);
Paul Cercueil208caad2019-10-02 19:25:23 +080037 struct irq_domain *domain = intc->domain;
Paul Cercueil8bc74642019-10-02 19:25:24 +080038 struct irq_chip_generic *gc;
Zhou Yanjieb8b01452019-10-02 19:25:25 +080039 uint32_t pending;
Paul Burton943d69c2015-05-24 16:11:26 +010040 unsigned i;
Lars-Peter Clausen98698482010-07-17 11:08:43 +000041
Paul Burton943d69c2015-05-24 16:11:26 +010042 for (i = 0; i < intc->num_chips; i++) {
Paul Cercueil8bc74642019-10-02 19:25:24 +080043 gc = irq_get_domain_generic_chip(domain, i * 32);
44
Zhou Yanjieb8b01452019-10-02 19:25:25 +080045 pending = irq_reg_readl(gc, JZ_REG_INTC_PENDING);
46 if (!pending)
Paul Burton943d69c2015-05-24 16:11:26 +010047 continue;
Lars-Peter Clausen98698482010-07-17 11:08:43 +000048
Zhou Yanjieb8b01452019-10-02 19:25:25 +080049 while (pending) {
50 int bit = __fls(pending);
51
Marc Zyngier046a6ee2021-05-04 17:42:18 +010052 generic_handle_domain_irq(domain, bit + (i * 32));
Zhou Yanjieb8b01452019-10-02 19:25:25 +080053 pending &= ~BIT(bit);
54 }
Paul Burton943d69c2015-05-24 16:11:26 +010055 }
Lars-Peter Clausen98698482010-07-17 11:08:43 +000056
57 return IRQ_HANDLED;
58}
59
Paul Burton943d69c2015-05-24 16:11:26 +010060static int __init ingenic_intc_of_init(struct device_node *node,
61 unsigned num_chips)
Lars-Peter Clausen98698482010-07-17 11:08:43 +000062{
Paul Burtonfe778ec2015-05-24 16:11:25 +010063 struct ingenic_intc_data *intc;
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +020064 struct irq_chip_generic *gc;
65 struct irq_chip_type *ct;
Paul Burton638c8852015-05-24 16:11:23 +010066 struct irq_domain *domain;
Paul Burtonfe778ec2015-05-24 16:11:25 +010067 int parent_irq, err = 0;
Paul Burton943d69c2015-05-24 16:11:26 +010068 unsigned i;
Paul Burtonfe778ec2015-05-24 16:11:25 +010069
70 intc = kzalloc(sizeof(*intc), GFP_KERNEL);
71 if (!intc) {
72 err = -ENOMEM;
73 goto out_err;
74 }
Paul Burton69ce4b22015-05-24 16:11:22 +010075
76 parent_irq = irq_of_parse_and_map(node, 0);
Paul Burtonfe778ec2015-05-24 16:11:25 +010077 if (!parent_irq) {
78 err = -EINVAL;
79 goto out_free;
80 }
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +020081
Paul Burtonfe778ec2015-05-24 16:11:25 +010082 err = irq_set_handler_data(parent_irq, intc);
83 if (err)
84 goto out_unmap_irq;
85
Paul Burton943d69c2015-05-24 16:11:26 +010086 intc->num_chips = num_chips;
Paul Burton3aa94592015-05-24 16:11:28 +010087 intc->base = of_iomap(node, 0);
88 if (!intc->base) {
89 err = -ENODEV;
90 goto out_unmap_irq;
91 }
Lars-Peter Clausen98698482010-07-17 11:08:43 +000092
Paul Cercueil1fd224e2020-01-13 13:33:29 -030093 domain = irq_domain_add_linear(node, num_chips * 32,
Paul Cercueil8bc74642019-10-02 19:25:24 +080094 &irq_generic_chip_ops, NULL);
Paul Cercueil52ecc872019-10-02 19:25:22 +080095 if (!domain) {
96 err = -ENOMEM;
97 goto out_unmap_base;
98 }
99
Paul Cercueil208caad2019-10-02 19:25:23 +0800100 intc->domain = domain;
101
Paul Cercueil8bc74642019-10-02 19:25:24 +0800102 err = irq_alloc_domain_generic_chips(domain, 32, 1, "INTC",
103 handle_level_irq, 0,
104 IRQ_NOPROBE | IRQ_LEVEL, 0);
105 if (err)
106 goto out_domain_remove;
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000107
Paul Cercueil8bc74642019-10-02 19:25:24 +0800108 for (i = 0; i < num_chips; i++) {
109 gc = irq_get_domain_generic_chip(domain, i * 32);
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200110
Paul Burton943d69c2015-05-24 16:11:26 +0100111 gc->wake_enabled = IRQ_MSK(32);
Paul Cercueil8bc74642019-10-02 19:25:24 +0800112 gc->reg_base = intc->base + (i * CHIP_SIZE);
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200113
Paul Burton943d69c2015-05-24 16:11:26 +0100114 ct = gc->chip_types;
115 ct->regs.enable = JZ_REG_INTC_CLEAR_MASK;
116 ct->regs.disable = JZ_REG_INTC_SET_MASK;
117 ct->chip.irq_unmask = irq_gc_unmask_enable_reg;
118 ct->chip.irq_mask = irq_gc_mask_disable_reg;
119 ct->chip.irq_mask_ack = irq_gc_mask_disable_reg;
120 ct->chip.irq_set_wake = irq_gc_set_wake;
Paul Cercueil20b44b42019-10-02 19:25:21 +0800121 ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND;
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200122
Paul Cercueil8bc74642019-10-02 19:25:24 +0800123 /* Mask all irqs */
124 irq_reg_writel(gc, IRQ_MSK(32), JZ_REG_INTC_SET_MASK);
Paul Burton943d69c2015-05-24 16:11:26 +0100125 }
Lars-Peter Clausen98698482010-07-17 11:08:43 +0000126
Paul Cercueil821fc9e2020-08-19 20:06:02 +0200127 if (request_irq(parent_irq, intc_cascade, IRQF_NO_SUSPEND,
afzal mohammed2ef1cb72020-03-04 06:18:38 +0530128 "SoC intc cascade interrupt", NULL))
129 pr_err("Failed to register SoC intc cascade interrupt\n");
Paul Burtonadbdce72015-05-24 16:11:21 +0100130 return 0;
Paul Burtonfe778ec2015-05-24 16:11:25 +0100131
Paul Cercueil8bc74642019-10-02 19:25:24 +0800132out_domain_remove:
133 irq_domain_remove(domain);
Paul Cercueil52ecc872019-10-02 19:25:22 +0800134out_unmap_base:
135 iounmap(intc->base);
Paul Burtonfe778ec2015-05-24 16:11:25 +0100136out_unmap_irq:
137 irq_dispose_mapping(parent_irq);
138out_free:
139 kfree(intc);
140out_err:
141 return err;
Lars-Peter Clausen98698482010-07-17 11:08:43 +0000142}
Paul Burton943d69c2015-05-24 16:11:26 +0100143
144static int __init intc_1chip_of_init(struct device_node *node,
145 struct device_node *parent)
146{
147 return ingenic_intc_of_init(node, 1);
148}
149IRQCHIP_DECLARE(jz4740_intc, "ingenic,jz4740-intc", intc_1chip_of_init);
Paul Cercueil10475572018-07-13 16:49:09 +0200150IRQCHIP_DECLARE(jz4725b_intc, "ingenic,jz4725b-intc", intc_1chip_of_init);
Paul Burton24ccfa02015-05-24 16:11:30 +0100151
152static int __init intc_2chip_of_init(struct device_node *node,
153 struct device_node *parent)
154{
155 return ingenic_intc_of_init(node, 2);
156}
Paul Cercueil5fbecd22021-03-07 17:20:14 +0000157IRQCHIP_DECLARE(jz4760_intc, "ingenic,jz4760-intc", intc_2chip_of_init);
Paul Burton24ccfa02015-05-24 16:11:30 +0100158IRQCHIP_DECLARE(jz4770_intc, "ingenic,jz4770-intc", intc_2chip_of_init);
159IRQCHIP_DECLARE(jz4775_intc, "ingenic,jz4775-intc", intc_2chip_of_init);
160IRQCHIP_DECLARE(jz4780_intc, "ingenic,jz4780-intc", intc_2chip_of_init);