1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) Maxime Coquelin 2015
4 * Copyright (C) STMicroelectronics 2017-2024
5 * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
8 #include <linux/bitops.h>
9 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/irqchip.h>
13 #include <linux/irqchip/chained_irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
18 #define IRQS_PER_BANK 32
20 struct stm32_exti_bank
{
29 struct stm32_exti_drv_data
{
30 const struct stm32_exti_bank
**exti_banks
;
35 struct stm32_exti_chip_data
{
36 struct stm32_exti_host_data
*host_data
;
37 const struct stm32_exti_bank
*reg_bank
;
45 struct stm32_exti_host_data
{
48 struct stm32_exti_chip_data
*chips_data
;
49 const struct stm32_exti_drv_data
*drv_data
;
52 static const struct stm32_exti_bank stm32f4xx_exti_b1
= {
61 static const struct stm32_exti_bank
*stm32f4xx_exti_banks
[] = {
65 static const struct stm32_exti_drv_data stm32f4xx_drv_data
= {
66 .exti_banks
= stm32f4xx_exti_banks
,
67 .bank_nr
= ARRAY_SIZE(stm32f4xx_exti_banks
),
70 static const struct stm32_exti_bank stm32h7xx_exti_b1
= {
79 static const struct stm32_exti_bank stm32h7xx_exti_b2
= {
88 static const struct stm32_exti_bank stm32h7xx_exti_b3
= {
97 static const struct stm32_exti_bank
*stm32h7xx_exti_banks
[] = {
103 static const struct stm32_exti_drv_data stm32h7xx_drv_data
= {
104 .exti_banks
= stm32h7xx_exti_banks
,
105 .bank_nr
= ARRAY_SIZE(stm32h7xx_exti_banks
),
108 static unsigned long stm32_exti_pending(struct irq_chip_generic
*gc
)
110 struct stm32_exti_chip_data
*chip_data
= gc
->private;
111 const struct stm32_exti_bank
*stm32_bank
= chip_data
->reg_bank
;
113 return irq_reg_readl(gc
, stm32_bank
->rpr_ofst
);
116 static void stm32_irq_handler(struct irq_desc
*desc
)
118 struct irq_domain
*domain
= irq_desc_get_handler_data(desc
);
119 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
120 unsigned int nbanks
= domain
->gc
->num_chips
;
121 struct irq_chip_generic
*gc
;
122 unsigned long pending
;
123 int n
, i
, irq_base
= 0;
125 chained_irq_enter(chip
, desc
);
127 for (i
= 0; i
< nbanks
; i
++, irq_base
+= IRQS_PER_BANK
) {
128 gc
= irq_get_domain_generic_chip(domain
, irq_base
);
130 while ((pending
= stm32_exti_pending(gc
))) {
131 for_each_set_bit(n
, &pending
, IRQS_PER_BANK
)
132 generic_handle_domain_irq(domain
, irq_base
+ n
);
136 chained_irq_exit(chip
, desc
);
139 static int stm32_exti_set_type(struct irq_data
*d
,
140 unsigned int type
, u32
*rtsr
, u32
*ftsr
)
142 u32 mask
= BIT(d
->hwirq
% IRQS_PER_BANK
);
145 case IRQ_TYPE_EDGE_RISING
:
149 case IRQ_TYPE_EDGE_FALLING
:
153 case IRQ_TYPE_EDGE_BOTH
:
164 static int stm32_irq_set_type(struct irq_data
*d
, unsigned int type
)
166 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
167 struct stm32_exti_chip_data
*chip_data
= gc
->private;
168 const struct stm32_exti_bank
*stm32_bank
= chip_data
->reg_bank
;
174 rtsr
= irq_reg_readl(gc
, stm32_bank
->rtsr_ofst
);
175 ftsr
= irq_reg_readl(gc
, stm32_bank
->ftsr_ofst
);
177 err
= stm32_exti_set_type(d
, type
, &rtsr
, &ftsr
);
181 irq_reg_writel(gc
, rtsr
, stm32_bank
->rtsr_ofst
);
182 irq_reg_writel(gc
, ftsr
, stm32_bank
->ftsr_ofst
);
190 static void stm32_chip_suspend(struct stm32_exti_chip_data
*chip_data
,
193 const struct stm32_exti_bank
*stm32_bank
= chip_data
->reg_bank
;
194 void __iomem
*base
= chip_data
->host_data
->base
;
196 /* save rtsr, ftsr registers */
197 chip_data
->rtsr_cache
= readl_relaxed(base
+ stm32_bank
->rtsr_ofst
);
198 chip_data
->ftsr_cache
= readl_relaxed(base
+ stm32_bank
->ftsr_ofst
);
200 writel_relaxed(wake_active
, base
+ stm32_bank
->imr_ofst
);
203 static void stm32_chip_resume(struct stm32_exti_chip_data
*chip_data
,
206 const struct stm32_exti_bank
*stm32_bank
= chip_data
->reg_bank
;
207 void __iomem
*base
= chip_data
->host_data
->base
;
209 /* restore rtsr, ftsr, registers */
210 writel_relaxed(chip_data
->rtsr_cache
, base
+ stm32_bank
->rtsr_ofst
);
211 writel_relaxed(chip_data
->ftsr_cache
, base
+ stm32_bank
->ftsr_ofst
);
213 writel_relaxed(mask_cache
, base
+ stm32_bank
->imr_ofst
);
216 static void stm32_irq_suspend(struct irq_chip_generic
*gc
)
218 struct stm32_exti_chip_data
*chip_data
= gc
->private;
221 stm32_chip_suspend(chip_data
, gc
->wake_active
);
225 static void stm32_irq_resume(struct irq_chip_generic
*gc
)
227 struct stm32_exti_chip_data
*chip_data
= gc
->private;
230 stm32_chip_resume(chip_data
, gc
->mask_cache
);
234 static int stm32_exti_alloc(struct irq_domain
*d
, unsigned int virq
,
235 unsigned int nr_irqs
, void *data
)
237 struct irq_fwspec
*fwspec
= data
;
238 irq_hw_number_t hwirq
;
240 hwirq
= fwspec
->param
[0];
242 irq_map_generic_chip(d
, virq
, hwirq
);
247 static void stm32_exti_free(struct irq_domain
*d
, unsigned int virq
,
248 unsigned int nr_irqs
)
250 struct irq_data
*data
= irq_domain_get_irq_data(d
, virq
);
252 irq_domain_reset_irq_data(data
);
255 static const struct irq_domain_ops irq_exti_domain_ops
= {
256 .map
= irq_map_generic_chip
,
257 .alloc
= stm32_exti_alloc
,
258 .free
= stm32_exti_free
,
259 .xlate
= irq_domain_xlate_twocell
,
262 static void stm32_irq_ack(struct irq_data
*d
)
264 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
265 struct stm32_exti_chip_data
*chip_data
= gc
->private;
266 const struct stm32_exti_bank
*stm32_bank
= chip_data
->reg_bank
;
270 irq_reg_writel(gc
, d
->mask
, stm32_bank
->rpr_ofst
);
276 stm32_exti_host_data
*stm32_exti_host_init(const struct stm32_exti_drv_data
*dd
,
277 struct device_node
*node
)
279 struct stm32_exti_host_data
*host_data
;
281 host_data
= kzalloc(sizeof(*host_data
), GFP_KERNEL
);
285 host_data
->drv_data
= dd
;
286 host_data
->chips_data
= kcalloc(dd
->bank_nr
,
287 sizeof(struct stm32_exti_chip_data
),
289 if (!host_data
->chips_data
)
292 host_data
->base
= of_iomap(node
, 0);
293 if (!host_data
->base
) {
294 pr_err("%pOF: Unable to map registers\n", node
);
295 goto free_chips_data
;
301 kfree(host_data
->chips_data
);
309 stm32_exti_chip_data
*stm32_exti_chip_init(struct stm32_exti_host_data
*h_data
,
311 struct device_node
*node
)
313 const struct stm32_exti_bank
*stm32_bank
;
314 struct stm32_exti_chip_data
*chip_data
;
315 void __iomem
*base
= h_data
->base
;
317 stm32_bank
= h_data
->drv_data
->exti_banks
[bank_idx
];
318 chip_data
= &h_data
->chips_data
[bank_idx
];
319 chip_data
->host_data
= h_data
;
320 chip_data
->reg_bank
= stm32_bank
;
323 * This IP has no reset, so after hot reboot we should
324 * clear registers to avoid residue
326 writel_relaxed(0, base
+ stm32_bank
->imr_ofst
);
327 writel_relaxed(0, base
+ stm32_bank
->emr_ofst
);
329 pr_info("%pOF: bank%d\n", node
, bank_idx
);
334 static int __init
stm32_exti_init(const struct stm32_exti_drv_data
*drv_data
,
335 struct device_node
*node
)
337 struct stm32_exti_host_data
*host_data
;
338 unsigned int clr
= IRQ_NOREQUEST
| IRQ_NOPROBE
| IRQ_NOAUTOEN
;
340 struct irq_chip_generic
*gc
;
341 struct irq_domain
*domain
;
343 host_data
= stm32_exti_host_init(drv_data
, node
);
347 domain
= irq_domain_add_linear(node
, drv_data
->bank_nr
* IRQS_PER_BANK
,
348 &irq_exti_domain_ops
, NULL
);
350 pr_err("%pOFn: Could not register interrupt domain.\n",
356 ret
= irq_alloc_domain_generic_chips(domain
, IRQS_PER_BANK
, 1, "exti",
357 handle_edge_irq
, clr
, 0, 0);
359 pr_err("%pOF: Could not allocate generic interrupt chip.\n",
361 goto out_free_domain
;
364 for (i
= 0; i
< drv_data
->bank_nr
; i
++) {
365 const struct stm32_exti_bank
*stm32_bank
;
366 struct stm32_exti_chip_data
*chip_data
;
368 stm32_bank
= drv_data
->exti_banks
[i
];
369 chip_data
= stm32_exti_chip_init(host_data
, i
, node
);
371 gc
= irq_get_domain_generic_chip(domain
, i
* IRQS_PER_BANK
);
373 gc
->reg_base
= host_data
->base
;
374 gc
->chip_types
->type
= IRQ_TYPE_EDGE_BOTH
;
375 gc
->chip_types
->chip
.irq_ack
= stm32_irq_ack
;
376 gc
->chip_types
->chip
.irq_mask
= irq_gc_mask_clr_bit
;
377 gc
->chip_types
->chip
.irq_unmask
= irq_gc_mask_set_bit
;
378 gc
->chip_types
->chip
.irq_set_type
= stm32_irq_set_type
;
379 gc
->chip_types
->chip
.irq_set_wake
= irq_gc_set_wake
;
380 gc
->suspend
= stm32_irq_suspend
;
381 gc
->resume
= stm32_irq_resume
;
382 gc
->wake_enabled
= IRQ_MSK(IRQS_PER_BANK
);
384 gc
->chip_types
->regs
.mask
= stm32_bank
->imr_ofst
;
385 gc
->private = (void *)chip_data
;
388 nr_irqs
= of_irq_count(node
);
389 for (i
= 0; i
< nr_irqs
; i
++) {
390 unsigned int irq
= irq_of_parse_and_map(node
, i
);
392 irq_set_handler_data(irq
, domain
);
393 irq_set_chained_handler(irq
, stm32_irq_handler
);
399 irq_domain_remove(domain
);
401 iounmap(host_data
->base
);
402 kfree(host_data
->chips_data
);
407 static int __init
stm32f4_exti_of_init(struct device_node
*np
,
408 struct device_node
*parent
)
410 return stm32_exti_init(&stm32f4xx_drv_data
, np
);
413 IRQCHIP_DECLARE(stm32f4_exti
, "st,stm32-exti", stm32f4_exti_of_init
);
415 static int __init
stm32h7_exti_of_init(struct device_node
*np
,
416 struct device_node
*parent
)
418 return stm32_exti_init(&stm32h7xx_drv_data
, np
);
421 IRQCHIP_DECLARE(stm32h7_exti
, "st,stm32h7-exti", stm32h7_exti_of_init
);