1*e4e53503SChanghuang Liang // SPDX-License-Identifier: GPL-2.0
2*e4e53503SChanghuang Liang /*
3*e4e53503SChanghuang Liang * StarFive JH8100 External Interrupt Controller driver
4*e4e53503SChanghuang Liang *
5*e4e53503SChanghuang Liang * Copyright (C) 2023 StarFive Technology Co., Ltd.
6*e4e53503SChanghuang Liang *
7*e4e53503SChanghuang Liang * Author: Changhuang Liang <changhuang.liang@starfivetech.com>
8*e4e53503SChanghuang Liang */
9*e4e53503SChanghuang Liang
10*e4e53503SChanghuang Liang #define pr_fmt(fmt) "irq-starfive-jh8100: " fmt
11*e4e53503SChanghuang Liang
12*e4e53503SChanghuang Liang #include <linux/bitops.h>
13*e4e53503SChanghuang Liang #include <linux/clk.h>
14*e4e53503SChanghuang Liang #include <linux/irq.h>
15*e4e53503SChanghuang Liang #include <linux/irqchip.h>
16*e4e53503SChanghuang Liang #include <linux/irqchip/chained_irq.h>
17*e4e53503SChanghuang Liang #include <linux/irqdomain.h>
18*e4e53503SChanghuang Liang #include <linux/of_address.h>
19*e4e53503SChanghuang Liang #include <linux/of_irq.h>
20*e4e53503SChanghuang Liang #include <linux/reset.h>
21*e4e53503SChanghuang Liang #include <linux/spinlock.h>
22*e4e53503SChanghuang Liang
23*e4e53503SChanghuang Liang #define STARFIVE_INTC_SRC0_CLEAR 0x10
24*e4e53503SChanghuang Liang #define STARFIVE_INTC_SRC0_MASK 0x14
25*e4e53503SChanghuang Liang #define STARFIVE_INTC_SRC0_INT 0x1c
26*e4e53503SChanghuang Liang
27*e4e53503SChanghuang Liang #define STARFIVE_INTC_SRC_IRQ_NUM 32
28*e4e53503SChanghuang Liang
29*e4e53503SChanghuang Liang struct starfive_irq_chip {
30*e4e53503SChanghuang Liang void __iomem *base;
31*e4e53503SChanghuang Liang struct irq_domain *domain;
32*e4e53503SChanghuang Liang raw_spinlock_t lock;
33*e4e53503SChanghuang Liang };
34*e4e53503SChanghuang Liang
starfive_intc_bit_set(struct starfive_irq_chip * irqc,u32 reg,u32 bit_mask)35*e4e53503SChanghuang Liang static void starfive_intc_bit_set(struct starfive_irq_chip *irqc,
36*e4e53503SChanghuang Liang u32 reg, u32 bit_mask)
37*e4e53503SChanghuang Liang {
38*e4e53503SChanghuang Liang u32 value;
39*e4e53503SChanghuang Liang
40*e4e53503SChanghuang Liang value = ioread32(irqc->base + reg);
41*e4e53503SChanghuang Liang value |= bit_mask;
42*e4e53503SChanghuang Liang iowrite32(value, irqc->base + reg);
43*e4e53503SChanghuang Liang }
44*e4e53503SChanghuang Liang
starfive_intc_bit_clear(struct starfive_irq_chip * irqc,u32 reg,u32 bit_mask)45*e4e53503SChanghuang Liang static void starfive_intc_bit_clear(struct starfive_irq_chip *irqc,
46*e4e53503SChanghuang Liang u32 reg, u32 bit_mask)
47*e4e53503SChanghuang Liang {
48*e4e53503SChanghuang Liang u32 value;
49*e4e53503SChanghuang Liang
50*e4e53503SChanghuang Liang value = ioread32(irqc->base + reg);
51*e4e53503SChanghuang Liang value &= ~bit_mask;
52*e4e53503SChanghuang Liang iowrite32(value, irqc->base + reg);
53*e4e53503SChanghuang Liang }
54*e4e53503SChanghuang Liang
starfive_intc_unmask(struct irq_data * d)55*e4e53503SChanghuang Liang static void starfive_intc_unmask(struct irq_data *d)
56*e4e53503SChanghuang Liang {
57*e4e53503SChanghuang Liang struct starfive_irq_chip *irqc = irq_data_get_irq_chip_data(d);
58*e4e53503SChanghuang Liang
59*e4e53503SChanghuang Liang raw_spin_lock(&irqc->lock);
60*e4e53503SChanghuang Liang starfive_intc_bit_clear(irqc, STARFIVE_INTC_SRC0_MASK, BIT(d->hwirq));
61*e4e53503SChanghuang Liang raw_spin_unlock(&irqc->lock);
62*e4e53503SChanghuang Liang }
63*e4e53503SChanghuang Liang
starfive_intc_mask(struct irq_data * d)64*e4e53503SChanghuang Liang static void starfive_intc_mask(struct irq_data *d)
65*e4e53503SChanghuang Liang {
66*e4e53503SChanghuang Liang struct starfive_irq_chip *irqc = irq_data_get_irq_chip_data(d);
67*e4e53503SChanghuang Liang
68*e4e53503SChanghuang Liang raw_spin_lock(&irqc->lock);
69*e4e53503SChanghuang Liang starfive_intc_bit_set(irqc, STARFIVE_INTC_SRC0_MASK, BIT(d->hwirq));
70*e4e53503SChanghuang Liang raw_spin_unlock(&irqc->lock);
71*e4e53503SChanghuang Liang }
72*e4e53503SChanghuang Liang
73*e4e53503SChanghuang Liang static struct irq_chip intc_dev = {
74*e4e53503SChanghuang Liang .name = "StarFive JH8100 INTC",
75*e4e53503SChanghuang Liang .irq_unmask = starfive_intc_unmask,
76*e4e53503SChanghuang Liang .irq_mask = starfive_intc_mask,
77*e4e53503SChanghuang Liang };
78*e4e53503SChanghuang Liang
starfive_intc_map(struct irq_domain * d,unsigned int irq,irq_hw_number_t hwirq)79*e4e53503SChanghuang Liang static int starfive_intc_map(struct irq_domain *d, unsigned int irq,
80*e4e53503SChanghuang Liang irq_hw_number_t hwirq)
81*e4e53503SChanghuang Liang {
82*e4e53503SChanghuang Liang irq_domain_set_info(d, irq, hwirq, &intc_dev, d->host_data,
83*e4e53503SChanghuang Liang handle_level_irq, NULL, NULL);
84*e4e53503SChanghuang Liang
85*e4e53503SChanghuang Liang return 0;
86*e4e53503SChanghuang Liang }
87*e4e53503SChanghuang Liang
88*e4e53503SChanghuang Liang static const struct irq_domain_ops starfive_intc_domain_ops = {
89*e4e53503SChanghuang Liang .xlate = irq_domain_xlate_onecell,
90*e4e53503SChanghuang Liang .map = starfive_intc_map,
91*e4e53503SChanghuang Liang };
92*e4e53503SChanghuang Liang
starfive_intc_irq_handler(struct irq_desc * desc)93*e4e53503SChanghuang Liang static void starfive_intc_irq_handler(struct irq_desc *desc)
94*e4e53503SChanghuang Liang {
95*e4e53503SChanghuang Liang struct starfive_irq_chip *irqc = irq_data_get_irq_handler_data(&desc->irq_data);
96*e4e53503SChanghuang Liang struct irq_chip *chip = irq_desc_get_chip(desc);
97*e4e53503SChanghuang Liang unsigned long value;
98*e4e53503SChanghuang Liang int hwirq;
99*e4e53503SChanghuang Liang
100*e4e53503SChanghuang Liang chained_irq_enter(chip, desc);
101*e4e53503SChanghuang Liang
102*e4e53503SChanghuang Liang value = ioread32(irqc->base + STARFIVE_INTC_SRC0_INT);
103*e4e53503SChanghuang Liang while (value) {
104*e4e53503SChanghuang Liang hwirq = ffs(value) - 1;
105*e4e53503SChanghuang Liang
106*e4e53503SChanghuang Liang generic_handle_domain_irq(irqc->domain, hwirq);
107*e4e53503SChanghuang Liang
108*e4e53503SChanghuang Liang starfive_intc_bit_set(irqc, STARFIVE_INTC_SRC0_CLEAR, BIT(hwirq));
109*e4e53503SChanghuang Liang starfive_intc_bit_clear(irqc, STARFIVE_INTC_SRC0_CLEAR, BIT(hwirq));
110*e4e53503SChanghuang Liang
111*e4e53503SChanghuang Liang __clear_bit(hwirq, &value);
112*e4e53503SChanghuang Liang }
113*e4e53503SChanghuang Liang
114*e4e53503SChanghuang Liang chained_irq_exit(chip, desc);
115*e4e53503SChanghuang Liang }
116*e4e53503SChanghuang Liang
starfive_intc_init(struct device_node * intc,struct device_node * parent)117*e4e53503SChanghuang Liang static int __init starfive_intc_init(struct device_node *intc,
118*e4e53503SChanghuang Liang struct device_node *parent)
119*e4e53503SChanghuang Liang {
120*e4e53503SChanghuang Liang struct starfive_irq_chip *irqc;
121*e4e53503SChanghuang Liang struct reset_control *rst;
122*e4e53503SChanghuang Liang struct clk *clk;
123*e4e53503SChanghuang Liang int parent_irq;
124*e4e53503SChanghuang Liang int ret;
125*e4e53503SChanghuang Liang
126*e4e53503SChanghuang Liang irqc = kzalloc(sizeof(*irqc), GFP_KERNEL);
127*e4e53503SChanghuang Liang if (!irqc)
128*e4e53503SChanghuang Liang return -ENOMEM;
129*e4e53503SChanghuang Liang
130*e4e53503SChanghuang Liang irqc->base = of_iomap(intc, 0);
131*e4e53503SChanghuang Liang if (!irqc->base) {
132*e4e53503SChanghuang Liang pr_err("Unable to map registers\n");
133*e4e53503SChanghuang Liang ret = -ENXIO;
134*e4e53503SChanghuang Liang goto err_free;
135*e4e53503SChanghuang Liang }
136*e4e53503SChanghuang Liang
137*e4e53503SChanghuang Liang rst = of_reset_control_get_exclusive(intc, NULL);
138*e4e53503SChanghuang Liang if (IS_ERR(rst)) {
139*e4e53503SChanghuang Liang pr_err("Unable to get reset control %pe\n", rst);
140*e4e53503SChanghuang Liang ret = PTR_ERR(rst);
141*e4e53503SChanghuang Liang goto err_unmap;
142*e4e53503SChanghuang Liang }
143*e4e53503SChanghuang Liang
144*e4e53503SChanghuang Liang clk = of_clk_get(intc, 0);
145*e4e53503SChanghuang Liang if (IS_ERR(clk)) {
146*e4e53503SChanghuang Liang pr_err("Unable to get clock %pe\n", clk);
147*e4e53503SChanghuang Liang ret = PTR_ERR(clk);
148*e4e53503SChanghuang Liang goto err_reset_put;
149*e4e53503SChanghuang Liang }
150*e4e53503SChanghuang Liang
151*e4e53503SChanghuang Liang ret = reset_control_deassert(rst);
152*e4e53503SChanghuang Liang if (ret)
153*e4e53503SChanghuang Liang goto err_clk_put;
154*e4e53503SChanghuang Liang
155*e4e53503SChanghuang Liang ret = clk_prepare_enable(clk);
156*e4e53503SChanghuang Liang if (ret)
157*e4e53503SChanghuang Liang goto err_reset_assert;
158*e4e53503SChanghuang Liang
159*e4e53503SChanghuang Liang raw_spin_lock_init(&irqc->lock);
160*e4e53503SChanghuang Liang
161*e4e53503SChanghuang Liang irqc->domain = irq_domain_add_linear(intc, STARFIVE_INTC_SRC_IRQ_NUM,
162*e4e53503SChanghuang Liang &starfive_intc_domain_ops, irqc);
163*e4e53503SChanghuang Liang if (!irqc->domain) {
164*e4e53503SChanghuang Liang pr_err("Unable to create IRQ domain\n");
165*e4e53503SChanghuang Liang ret = -EINVAL;
166*e4e53503SChanghuang Liang goto err_clk_disable;
167*e4e53503SChanghuang Liang }
168*e4e53503SChanghuang Liang
169*e4e53503SChanghuang Liang parent_irq = of_irq_get(intc, 0);
170*e4e53503SChanghuang Liang if (parent_irq < 0) {
171*e4e53503SChanghuang Liang pr_err("Failed to get main IRQ: %d\n", parent_irq);
172*e4e53503SChanghuang Liang ret = parent_irq;
173*e4e53503SChanghuang Liang goto err_remove_domain;
174*e4e53503SChanghuang Liang }
175*e4e53503SChanghuang Liang
176*e4e53503SChanghuang Liang irq_set_chained_handler_and_data(parent_irq, starfive_intc_irq_handler,
177*e4e53503SChanghuang Liang irqc);
178*e4e53503SChanghuang Liang
179*e4e53503SChanghuang Liang pr_info("Interrupt controller register, nr_irqs %d\n",
180*e4e53503SChanghuang Liang STARFIVE_INTC_SRC_IRQ_NUM);
181*e4e53503SChanghuang Liang
182*e4e53503SChanghuang Liang return 0;
183*e4e53503SChanghuang Liang
184*e4e53503SChanghuang Liang err_remove_domain:
185*e4e53503SChanghuang Liang irq_domain_remove(irqc->domain);
186*e4e53503SChanghuang Liang err_clk_disable:
187*e4e53503SChanghuang Liang clk_disable_unprepare(clk);
188*e4e53503SChanghuang Liang err_reset_assert:
189*e4e53503SChanghuang Liang reset_control_assert(rst);
190*e4e53503SChanghuang Liang err_clk_put:
191*e4e53503SChanghuang Liang clk_put(clk);
192*e4e53503SChanghuang Liang err_reset_put:
193*e4e53503SChanghuang Liang reset_control_put(rst);
194*e4e53503SChanghuang Liang err_unmap:
195*e4e53503SChanghuang Liang iounmap(irqc->base);
196*e4e53503SChanghuang Liang err_free:
197*e4e53503SChanghuang Liang kfree(irqc);
198*e4e53503SChanghuang Liang return ret;
199*e4e53503SChanghuang Liang }
200*e4e53503SChanghuang Liang
201*e4e53503SChanghuang Liang IRQCHIP_PLATFORM_DRIVER_BEGIN(starfive_intc)
202*e4e53503SChanghuang Liang IRQCHIP_MATCH("starfive,jh8100-intc", starfive_intc_init)
203*e4e53503SChanghuang Liang IRQCHIP_PLATFORM_DRIVER_END(starfive_intc)
204*e4e53503SChanghuang Liang
205*e4e53503SChanghuang Liang MODULE_DESCRIPTION("StarFive JH8100 External Interrupt Controller");
206*e4e53503SChanghuang Liang MODULE_LICENSE("GPL");
207*e4e53503SChanghuang Liang MODULE_AUTHOR("Changhuang Liang <changhuang.liang@starfivetech.com>");
208