1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
3 * Copyright (C) Sunplus Technology Co., Ltd.
7 #include <linux/irqdomain.h>
9 #include <linux/irqchip.h>
10 #include <linux/irqchip/chained_irq.h>
11 #include <linux/of_address.h>
12 #include <linux/of_irq.h>
14 #define SP_INTC_HWIRQ_MIN 0
15 #define SP_INTC_HWIRQ_MAX 223
17 #define SP_INTC_NR_IRQS (SP_INTC_HWIRQ_MAX - SP_INTC_HWIRQ_MIN + 1)
18 #define SP_INTC_NR_GROUPS DIV_ROUND_UP(SP_INTC_NR_IRQS, 32)
19 #define SP_INTC_REG_SIZE (SP_INTC_NR_GROUPS * 4)
21 /* REG_GROUP_0 regs */
22 #define REG_INTR_TYPE (sp_intc.g0)
23 #define REG_INTR_POLARITY (REG_INTR_TYPE + SP_INTC_REG_SIZE)
24 #define REG_INTR_PRIORITY (REG_INTR_POLARITY + SP_INTC_REG_SIZE)
25 #define REG_INTR_MASK (REG_INTR_PRIORITY + SP_INTC_REG_SIZE)
27 /* REG_GROUP_1 regs */
28 #define REG_INTR_CLEAR (sp_intc.g1)
29 #define REG_MASKED_EXT1 (REG_INTR_CLEAR + SP_INTC_REG_SIZE)
30 #define REG_MASKED_EXT0 (REG_MASKED_EXT1 + SP_INTC_REG_SIZE)
31 #define REG_INTR_GROUP (REG_INTR_CLEAR + 31 * 4)
33 #define GROUP_MASK (BIT(SP_INTC_NR_GROUPS) - 1)
34 #define GROUP_SHIFT_EXT1 (0)
35 #define GROUP_SHIFT_EXT0 (8)
38 * When GPIO_INT0~7 set to edge trigger, doesn't work properly.
39 * WORKAROUND: change it to level trigger, and toggle the polarity
40 * at ACK/Handler to make the HW work.
42 #define GPIO_INT0_HWIRQ 120
43 #define GPIO_INT7_HWIRQ 127
44 #define IS_GPIO_INT(irq) \
47 (i >= GPIO_INT0_HWIRQ) && (i <= GPIO_INT7_HWIRQ); \
57 #define STATE_BIT(irq, idx) (((irq) - GPIO_INT0_HWIRQ) * 3 + (idx))
58 #define ASSIGN_STATE(irq, idx, v) assign_bit(STATE_BIT(irq, idx), sp_intc.states, v)
59 #define TEST_STATE(irq, idx) test_bit(STATE_BIT(irq, idx), sp_intc.states)
61 static struct sp_intctl
{
63 * REG_GROUP_0: include type/polarity/priority/mask regs.
64 * REG_GROUP_1: include clear/masked_ext0/masked_ext1/group regs.
66 void __iomem
*g0
; // REG_GROUP_0 base
67 void __iomem
*g1
; // REG_GROUP_1 base
69 struct irq_domain
*domain
;
73 * store GPIO_INT states
74 * each interrupt has 3 states: is_edge, is_low, is_active
76 DECLARE_BITMAP(states
, (GPIO_INT7_HWIRQ
- GPIO_INT0_HWIRQ
+ 1) * 3);
79 static struct irq_chip sp_intc_chip
;
81 static void sp_intc_assign_bit(u32 hwirq
, void __iomem
*base
, bool value
)
87 offset
= (hwirq
/ 32) * 4;
90 raw_spin_lock_irqsave(&sp_intc
.lock
, flags
);
91 mask
= readl_relaxed(reg
);
93 mask
|= BIT(hwirq
% 32);
95 mask
&= ~BIT(hwirq
% 32);
96 writel_relaxed(mask
, reg
);
97 raw_spin_unlock_irqrestore(&sp_intc
.lock
, flags
);
100 static void sp_intc_ack_irq(struct irq_data
*d
)
102 u32 hwirq
= d
->hwirq
;
104 if (unlikely(IS_GPIO_INT(hwirq
) && TEST_STATE(hwirq
, _IS_EDGE
))) { // WORKAROUND
105 sp_intc_assign_bit(hwirq
, REG_INTR_POLARITY
, !TEST_STATE(hwirq
, _IS_LOW
));
106 ASSIGN_STATE(hwirq
, _IS_ACTIVE
, true);
109 sp_intc_assign_bit(hwirq
, REG_INTR_CLEAR
, 1);
112 static void sp_intc_mask_irq(struct irq_data
*d
)
114 sp_intc_assign_bit(d
->hwirq
, REG_INTR_MASK
, 0);
117 static void sp_intc_unmask_irq(struct irq_data
*d
)
119 sp_intc_assign_bit(d
->hwirq
, REG_INTR_MASK
, 1);
122 static int sp_intc_set_type(struct irq_data
*d
, unsigned int type
)
124 u32 hwirq
= d
->hwirq
;
125 bool is_edge
= !(type
& IRQ_TYPE_LEVEL_MASK
);
126 bool is_low
= (type
== IRQ_TYPE_LEVEL_LOW
|| type
== IRQ_TYPE_EDGE_FALLING
);
128 irq_set_handler_locked(d
, is_edge
? handle_edge_irq
: handle_level_irq
);
130 if (unlikely(IS_GPIO_INT(hwirq
) && is_edge
)) { // WORKAROUND
132 ASSIGN_STATE(hwirq
, _IS_EDGE
, is_edge
);
133 ASSIGN_STATE(hwirq
, _IS_LOW
, is_low
);
134 ASSIGN_STATE(hwirq
, _IS_ACTIVE
, false);
135 /* change to level */
139 sp_intc_assign_bit(hwirq
, REG_INTR_TYPE
, is_edge
);
140 sp_intc_assign_bit(hwirq
, REG_INTR_POLARITY
, is_low
);
145 static int sp_intc_get_ext_irq(int ext_num
)
147 void __iomem
*base
= ext_num
? REG_MASKED_EXT1
: REG_MASKED_EXT0
;
148 u32 shift
= ext_num
? GROUP_SHIFT_EXT1
: GROUP_SHIFT_EXT0
;
154 groups
= readl_relaxed(REG_INTR_GROUP
);
155 pending_group
= (groups
>> shift
) & GROUP_MASK
;
159 group
= fls(pending_group
) - 1;
160 pending_irq
= readl_relaxed(base
+ group
* 4);
164 return (group
* 32) + fls(pending_irq
) - 1;
167 static void sp_intc_handle_ext_cascaded(struct irq_desc
*desc
)
169 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
170 int ext_num
= (uintptr_t)irq_desc_get_handler_data(desc
);
173 chained_irq_enter(chip
, desc
);
175 while ((hwirq
= sp_intc_get_ext_irq(ext_num
)) >= 0) {
176 if (unlikely(IS_GPIO_INT(hwirq
) && TEST_STATE(hwirq
, _IS_ACTIVE
))) { // WORKAROUND
177 ASSIGN_STATE(hwirq
, _IS_ACTIVE
, false);
178 sp_intc_assign_bit(hwirq
, REG_INTR_POLARITY
, TEST_STATE(hwirq
, _IS_LOW
));
180 generic_handle_domain_irq(sp_intc
.domain
, hwirq
);
184 chained_irq_exit(chip
, desc
);
187 static struct irq_chip sp_intc_chip
= {
189 .irq_ack
= sp_intc_ack_irq
,
190 .irq_mask
= sp_intc_mask_irq
,
191 .irq_unmask
= sp_intc_unmask_irq
,
192 .irq_set_type
= sp_intc_set_type
,
195 static int sp_intc_irq_domain_map(struct irq_domain
*domain
,
196 unsigned int irq
, irq_hw_number_t hwirq
)
198 irq_set_chip_and_handler(irq
, &sp_intc_chip
, handle_level_irq
);
199 irq_set_chip_data(irq
, &sp_intc_chip
);
200 irq_set_noprobe(irq
);
205 static const struct irq_domain_ops sp_intc_dm_ops
= {
206 .xlate
= irq_domain_xlate_twocell
,
207 .map
= sp_intc_irq_domain_map
,
210 static int sp_intc_irq_map(struct device_node
*node
, int i
)
214 irq
= irq_of_parse_and_map(node
, i
);
218 irq_set_chained_handler_and_data(irq
, sp_intc_handle_ext_cascaded
, (void *)(uintptr_t)i
);
223 static int __init
sp_intc_init_dt(struct device_node
*node
, struct device_node
*parent
)
227 sp_intc
.g0
= of_iomap(node
, 0);
231 sp_intc
.g1
= of_iomap(node
, 1);
237 ret
= sp_intc_irq_map(node
, 0); // EXT_INT0
241 ret
= sp_intc_irq_map(node
, 1); // EXT_INT1
246 for (i
= 0; i
< SP_INTC_NR_GROUPS
; i
++) {
248 writel_relaxed(0, REG_INTR_MASK
+ i
* 4);
250 writel_relaxed(~0, REG_INTR_TYPE
+ i
* 4);
251 /* all high-active */
252 writel_relaxed(0, REG_INTR_POLARITY
+ i
* 4);
254 writel_relaxed(~0, REG_INTR_PRIORITY
+ i
* 4);
256 writel_relaxed(~0, REG_INTR_CLEAR
+ i
* 4);
259 sp_intc
.domain
= irq_domain_add_linear(node
, SP_INTC_NR_IRQS
,
260 &sp_intc_dm_ops
, &sp_intc
);
261 if (!sp_intc
.domain
) {
266 raw_spin_lock_init(&sp_intc
.lock
);
278 IRQCHIP_DECLARE(sp_intc
, "sunplus,sp7021-intc", sp_intc_init_dt
);