Skip to content

Commit 6dd64ee

Browse files
ludovicbarreMarc Zyngier
authored andcommitted
irqchip/stm32: Add multi-bank management
-Prepare to manage multi-bank of external interrupts (N banks of 32 inputs). -Prepare to manage registers offsets by compatible (registers offsets could be different follow per stm32 platform). Signed-off-by: Ludovic Barre <[email protected]> Signed-off-by: Marc Zyngier <[email protected]>
1 parent 0e7d780 commit 6dd64ee

File tree

1 file changed

+103
-46
lines changed

1 file changed

+103
-46
lines changed

drivers/irqchip/irq-stm32-exti.c

Lines changed: 103 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,66 @@
1414
#include <linux/of_address.h>
1515
#include <linux/of_irq.h>
1616

17-
#define EXTI_IMR 0x0
18-
#define EXTI_EMR 0x4
19-
#define EXTI_RTSR 0x8
20-
#define EXTI_FTSR 0xc
21-
#define EXTI_SWIER 0x10
22-
#define EXTI_PR 0x14
17+
#define IRQS_PER_BANK 32
18+
19+
struct stm32_exti_bank {
20+
u32 imr_ofst;
21+
u32 emr_ofst;
22+
u32 rtsr_ofst;
23+
u32 ftsr_ofst;
24+
u32 swier_ofst;
25+
u32 pr_ofst;
26+
};
27+
28+
static const struct stm32_exti_bank stm32f4xx_exti_b1 = {
29+
.imr_ofst = 0x00,
30+
.emr_ofst = 0x04,
31+
.rtsr_ofst = 0x08,
32+
.ftsr_ofst = 0x0C,
33+
.swier_ofst = 0x10,
34+
.pr_ofst = 0x14,
35+
};
36+
37+
static const struct stm32_exti_bank *stm32f4xx_exti_banks[] = {
38+
&stm32f4xx_exti_b1,
39+
};
40+
41+
static unsigned long stm32_exti_pending(struct irq_chip_generic *gc)
42+
{
43+
const struct stm32_exti_bank *stm32_bank = gc->private;
44+
45+
return irq_reg_readl(gc, stm32_bank->pr_ofst);
46+
}
47+
48+
static void stm32_exti_irq_ack(struct irq_chip_generic *gc, u32 mask)
49+
{
50+
const struct stm32_exti_bank *stm32_bank = gc->private;
51+
52+
irq_reg_writel(gc, mask, stm32_bank->pr_ofst);
53+
}
2354

2455
static void stm32_irq_handler(struct irq_desc *desc)
2556
{
2657
struct irq_domain *domain = irq_desc_get_handler_data(desc);
27-
struct irq_chip_generic *gc = domain->gc->gc[0];
2858
struct irq_chip *chip = irq_desc_get_chip(desc);
59+
unsigned int virq, nbanks = domain->gc->num_chips;
60+
struct irq_chip_generic *gc;
61+
const struct stm32_exti_bank *stm32_bank;
2962
unsigned long pending;
30-
int n;
63+
int n, i, irq_base = 0;
3164

3265
chained_irq_enter(chip, desc);
3366

34-
while ((pending = irq_reg_readl(gc, EXTI_PR))) {
35-
for_each_set_bit(n, &pending, BITS_PER_LONG) {
36-
generic_handle_irq(irq_find_mapping(domain, n));
37-
irq_reg_writel(gc, BIT(n), EXTI_PR);
67+
for (i = 0; i < nbanks; i++, irq_base += IRQS_PER_BANK) {
68+
gc = irq_get_domain_generic_chip(domain, irq_base);
69+
stm32_bank = gc->private;
70+
71+
while ((pending = stm32_exti_pending(gc))) {
72+
for_each_set_bit(n, &pending, IRQS_PER_BANK) {
73+
virq = irq_find_mapping(domain, irq_base + n);
74+
generic_handle_irq(virq);
75+
stm32_exti_irq_ack(gc, BIT(n));
76+
}
3877
}
3978
}
4079

@@ -44,13 +83,14 @@ static void stm32_irq_handler(struct irq_desc *desc)
4483
static int stm32_irq_set_type(struct irq_data *data, unsigned int type)
4584
{
4685
struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
47-
int pin = data->hwirq;
86+
const struct stm32_exti_bank *stm32_bank = gc->private;
87+
int pin = data->hwirq % IRQS_PER_BANK;
4888
u32 rtsr, ftsr;
4989

5090
irq_gc_lock(gc);
5191

52-
rtsr = irq_reg_readl(gc, EXTI_RTSR);
53-
ftsr = irq_reg_readl(gc, EXTI_FTSR);
92+
rtsr = irq_reg_readl(gc, stm32_bank->rtsr_ofst);
93+
ftsr = irq_reg_readl(gc, stm32_bank->ftsr_ofst);
5494

5595
switch (type) {
5696
case IRQ_TYPE_EDGE_RISING:
@@ -70,8 +110,8 @@ static int stm32_irq_set_type(struct irq_data *data, unsigned int type)
70110
return -EINVAL;
71111
}
72112

73-
irq_reg_writel(gc, rtsr, EXTI_RTSR);
74-
irq_reg_writel(gc, ftsr, EXTI_FTSR);
113+
irq_reg_writel(gc, rtsr, stm32_bank->rtsr_ofst);
114+
irq_reg_writel(gc, ftsr, stm32_bank->ftsr_ofst);
75115

76116
irq_gc_unlock(gc);
77117

@@ -81,17 +121,18 @@ static int stm32_irq_set_type(struct irq_data *data, unsigned int type)
81121
static int stm32_irq_set_wake(struct irq_data *data, unsigned int on)
82122
{
83123
struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
84-
int pin = data->hwirq;
124+
const struct stm32_exti_bank *stm32_bank = gc->private;
125+
int pin = data->hwirq % IRQS_PER_BANK;
85126
u32 emr;
86127

87128
irq_gc_lock(gc);
88129

89-
emr = irq_reg_readl(gc, EXTI_EMR);
130+
emr = irq_reg_readl(gc, stm32_bank->emr_ofst);
90131
if (on)
91132
emr |= BIT(pin);
92133
else
93134
emr &= ~BIT(pin);
94-
irq_reg_writel(gc, emr, EXTI_EMR);
135+
irq_reg_writel(gc, emr, stm32_bank->emr_ofst);
95136

96137
irq_gc_unlock(gc);
97138

@@ -101,11 +142,12 @@ static int stm32_irq_set_wake(struct irq_data *data, unsigned int on)
101142
static int stm32_exti_alloc(struct irq_domain *d, unsigned int virq,
102143
unsigned int nr_irqs, void *data)
103144
{
104-
struct irq_chip_generic *gc = d->gc->gc[0];
145+
struct irq_chip_generic *gc;
105146
struct irq_fwspec *fwspec = data;
106147
irq_hw_number_t hwirq;
107148

108149
hwirq = fwspec->param[0];
150+
gc = irq_get_domain_generic_chip(d, hwirq);
109151

110152
irq_map_generic_chip(d, virq, hwirq);
111153
irq_domain_set_info(d, virq, hwirq, &gc->chip_types->chip, gc,
@@ -129,8 +171,9 @@ struct irq_domain_ops irq_exti_domain_ops = {
129171
.free = stm32_exti_free,
130172
};
131173

132-
static int __init stm32_exti_init(struct device_node *node,
133-
struct device_node *parent)
174+
static int
175+
__init stm32_exti_init(const struct stm32_exti_bank **stm32_exti_banks,
176+
int bank_nr, struct device_node *node)
134177
{
135178
unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
136179
int nr_irqs, nr_exti, ret, i;
@@ -144,42 +187,49 @@ static int __init stm32_exti_init(struct device_node *node,
144187
return -ENOMEM;
145188
}
146189

147-
/* Determine number of irqs supported */
148-
writel_relaxed(~0UL, base + EXTI_RTSR);
149-
nr_exti = fls(readl_relaxed(base + EXTI_RTSR));
150-
writel_relaxed(0, base + EXTI_RTSR);
151-
152-
pr_info("%pOF: %d External IRQs detected\n", node, nr_exti);
153-
154-
domain = irq_domain_add_linear(node, nr_exti,
190+
domain = irq_domain_add_linear(node, bank_nr * IRQS_PER_BANK,
155191
&irq_exti_domain_ops, NULL);
156192
if (!domain) {
157193
pr_err("%s: Could not register interrupt domain.\n",
158-
node->name);
194+
node->name);
159195
ret = -ENOMEM;
160196
goto out_unmap;
161197
}
162198

163-
ret = irq_alloc_domain_generic_chips(domain, nr_exti, 1, "exti",
199+
ret = irq_alloc_domain_generic_chips(domain, IRQS_PER_BANK, 1, "exti",
164200
handle_edge_irq, clr, 0, 0);
165201
if (ret) {
166202
pr_err("%pOF: Could not allocate generic interrupt chip.\n",
167203
node);
168204
goto out_free_domain;
169205
}
170206

171-
gc = domain->gc->gc[0];
172-
gc->reg_base = base;
173-
gc->chip_types->type = IRQ_TYPE_EDGE_BOTH;
174-
gc->chip_types->chip.name = gc->chip_types[0].chip.name;
175-
gc->chip_types->chip.irq_ack = irq_gc_ack_set_bit;
176-
gc->chip_types->chip.irq_mask = irq_gc_mask_clr_bit;
177-
gc->chip_types->chip.irq_unmask = irq_gc_mask_set_bit;
178-
gc->chip_types->chip.irq_set_type = stm32_irq_set_type;
179-
gc->chip_types->chip.irq_set_wake = stm32_irq_set_wake;
180-
gc->chip_types->regs.ack = EXTI_PR;
181-
gc->chip_types->regs.mask = EXTI_IMR;
182-
gc->chip_types->handler = handle_edge_irq;
207+
for (i = 0; i < bank_nr; i++) {
208+
const struct stm32_exti_bank *stm32_bank = stm32_exti_banks[i];
209+
u32 irqs_mask;
210+
211+
gc = irq_get_domain_generic_chip(domain, i * IRQS_PER_BANK);
212+
213+
gc->reg_base = base;
214+
gc->chip_types->type = IRQ_TYPE_EDGE_BOTH;
215+
gc->chip_types->chip.irq_ack = irq_gc_ack_set_bit;
216+
gc->chip_types->chip.irq_mask = irq_gc_mask_clr_bit;
217+
gc->chip_types->chip.irq_unmask = irq_gc_mask_set_bit;
218+
gc->chip_types->chip.irq_set_type = stm32_irq_set_type;
219+
gc->chip_types->chip.irq_set_wake = stm32_irq_set_wake;
220+
gc->chip_types->regs.ack = stm32_bank->pr_ofst;
221+
gc->chip_types->regs.mask = stm32_bank->imr_ofst;
222+
gc->private = (void *)stm32_bank;
223+
224+
/* Determine number of irqs supported */
225+
writel_relaxed(~0UL, base + stm32_bank->rtsr_ofst);
226+
irqs_mask = readl_relaxed(base + stm32_bank->rtsr_ofst);
227+
nr_exti = fls(readl_relaxed(base + stm32_bank->rtsr_ofst));
228+
writel_relaxed(0, base + stm32_bank->rtsr_ofst);
229+
230+
pr_info("%s: bank%d, External IRQs available:%#x\n",
231+
node->full_name, i, irqs_mask);
232+
}
183233

184234
nr_irqs = of_irq_count(node);
185235
for (i = 0; i < nr_irqs; i++) {
@@ -198,4 +248,11 @@ static int __init stm32_exti_init(struct device_node *node,
198248
return ret;
199249
}
200250

201-
IRQCHIP_DECLARE(stm32_exti, "st,stm32-exti", stm32_exti_init);
251+
static int __init stm32f4_exti_of_init(struct device_node *np,
252+
struct device_node *parent)
253+
{
254+
return stm32_exti_init(stm32f4xx_exti_banks,
255+
ARRAY_SIZE(stm32f4xx_exti_banks), np);
256+
}
257+
258+
IRQCHIP_DECLARE(stm32f4_exti, "st,stm32-exti", stm32f4_exti_of_init);

0 commit comments

Comments
 (0)