1 // SPDX-License-Identifier: GPL-2.0
3 * StarFive JH8100 External Interrupt Controller driver
5 * Copyright (C) 2023 StarFive Technology Co., Ltd.
7 * Author: Changhuang Liang <changhuang.liang@starfivetech.com>
10 #define pr_fmt(fmt) "irq-starfive-jh8100: " fmt
12 #include <linux/bitops.h>
13 #include <linux/clk.h>
14 #include <linux/irq.h>
15 #include <linux/irqchip.h>
16 #include <linux/irqchip/chained_irq.h>
17 #include <linux/irqdomain.h>
18 #include <linux/of_address.h>
19 #include <linux/of_irq.h>
20 #include <linux/reset.h>
21 #include <linux/spinlock.h>
23 #define STARFIVE_INTC_SRC0_CLEAR 0x10
24 #define STARFIVE_INTC_SRC0_MASK 0x14
25 #define STARFIVE_INTC_SRC0_INT 0x1c
27 #define STARFIVE_INTC_SRC_IRQ_NUM 32
29 struct starfive_irq_chip
{
31 struct irq_domain
*domain
;
35 static void starfive_intc_bit_set(struct starfive_irq_chip
*irqc
,
36 u32 reg
, u32 bit_mask
)
40 value
= ioread32(irqc
->base
+ reg
);
42 iowrite32(value
, irqc
->base
+ reg
);
45 static void starfive_intc_bit_clear(struct starfive_irq_chip
*irqc
,
46 u32 reg
, u32 bit_mask
)
50 value
= ioread32(irqc
->base
+ reg
);
52 iowrite32(value
, irqc
->base
+ reg
);
55 static void starfive_intc_unmask(struct irq_data
*d
)
57 struct starfive_irq_chip
*irqc
= irq_data_get_irq_chip_data(d
);
59 raw_spin_lock(&irqc
->lock
);
60 starfive_intc_bit_clear(irqc
, STARFIVE_INTC_SRC0_MASK
, BIT(d
->hwirq
));
61 raw_spin_unlock(&irqc
->lock
);
64 static void starfive_intc_mask(struct irq_data
*d
)
66 struct starfive_irq_chip
*irqc
= irq_data_get_irq_chip_data(d
);
68 raw_spin_lock(&irqc
->lock
);
69 starfive_intc_bit_set(irqc
, STARFIVE_INTC_SRC0_MASK
, BIT(d
->hwirq
));
70 raw_spin_unlock(&irqc
->lock
);
73 static struct irq_chip intc_dev
= {
74 .name
= "StarFive JH8100 INTC",
75 .irq_unmask
= starfive_intc_unmask
,
76 .irq_mask
= starfive_intc_mask
,
79 static int starfive_intc_map(struct irq_domain
*d
, unsigned int irq
,
80 irq_hw_number_t hwirq
)
82 irq_domain_set_info(d
, irq
, hwirq
, &intc_dev
, d
->host_data
,
83 handle_level_irq
, NULL
, NULL
);
88 static const struct irq_domain_ops starfive_intc_domain_ops
= {
89 .xlate
= irq_domain_xlate_onecell
,
90 .map
= starfive_intc_map
,
93 static void starfive_intc_irq_handler(struct irq_desc
*desc
)
95 struct starfive_irq_chip
*irqc
= irq_data_get_irq_handler_data(&desc
->irq_data
);
96 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
100 chained_irq_enter(chip
, desc
);
102 value
= ioread32(irqc
->base
+ STARFIVE_INTC_SRC0_INT
);
104 hwirq
= ffs(value
) - 1;
106 generic_handle_domain_irq(irqc
->domain
, hwirq
);
108 starfive_intc_bit_set(irqc
, STARFIVE_INTC_SRC0_CLEAR
, BIT(hwirq
));
109 starfive_intc_bit_clear(irqc
, STARFIVE_INTC_SRC0_CLEAR
, BIT(hwirq
));
111 __clear_bit(hwirq
, &value
);
114 chained_irq_exit(chip
, desc
);
117 static int __init
starfive_intc_init(struct device_node
*intc
,
118 struct device_node
*parent
)
120 struct starfive_irq_chip
*irqc
;
121 struct reset_control
*rst
;
126 irqc
= kzalloc(sizeof(*irqc
), GFP_KERNEL
);
130 irqc
->base
= of_iomap(intc
, 0);
132 pr_err("Unable to map registers\n");
137 rst
= of_reset_control_get_exclusive(intc
, NULL
);
139 pr_err("Unable to get reset control %pe\n", rst
);
144 clk
= of_clk_get(intc
, 0);
146 pr_err("Unable to get clock %pe\n", clk
);
151 ret
= reset_control_deassert(rst
);
155 ret
= clk_prepare_enable(clk
);
157 goto err_reset_assert
;
159 raw_spin_lock_init(&irqc
->lock
);
161 irqc
->domain
= irq_domain_add_linear(intc
, STARFIVE_INTC_SRC_IRQ_NUM
,
162 &starfive_intc_domain_ops
, irqc
);
164 pr_err("Unable to create IRQ domain\n");
166 goto err_clk_disable
;
169 parent_irq
= of_irq_get(intc
, 0);
170 if (parent_irq
< 0) {
171 pr_err("Failed to get main IRQ: %d\n", parent_irq
);
173 goto err_remove_domain
;
176 irq_set_chained_handler_and_data(parent_irq
, starfive_intc_irq_handler
,
179 pr_info("Interrupt controller register, nr_irqs %d\n",
180 STARFIVE_INTC_SRC_IRQ_NUM
);
185 irq_domain_remove(irqc
->domain
);
187 clk_disable_unprepare(clk
);
189 reset_control_assert(rst
);
193 reset_control_put(rst
);
201 IRQCHIP_PLATFORM_DRIVER_BEGIN(starfive_intc
)
202 IRQCHIP_MATCH("starfive,jh8100-intc", starfive_intc_init
)
203 IRQCHIP_PLATFORM_DRIVER_END(starfive_intc
)
205 MODULE_DESCRIPTION("StarFive JH8100 External Interrupt Controller");
206 MODULE_LICENSE("GPL");
207 MODULE_AUTHOR("Changhuang Liang <changhuang.liang@starfivetech.com>");