1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) Maxime Coquelin 2015
4 * Copyright (C) STMicroelectronics 2017
5 * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
6 */
7
8 #include <linux/bitops.h>
9 #include <linux/interrupt.h>
10 #include <linux/io.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>
17 #include <linux/syscore_ops.h>
18
19 #include <dt-bindings/interrupt-controller/arm-gic.h>
20
21 #define IRQS_PER_BANK 32
22
23 struct stm32_exti_bank {
24 u32 imr_ofst;
25 u32 emr_ofst;
26 u32 rtsr_ofst;
27 u32 ftsr_ofst;
28 u32 swier_ofst;
29 u32 rpr_ofst;
30 u32 fpr_ofst;
31 };
32
33 #define UNDEF_REG ~0
34
35 struct stm32_desc_irq {
36 u32 exti;
37 u32 irq_parent;
38 };
39
40 struct stm32_exti_drv_data {
41 const struct stm32_exti_bank **exti_banks;
42 const struct stm32_desc_irq *desc_irqs;
43 u32 bank_nr;
44 u32 irq_nr;
45 };
46
47 struct stm32_exti_chip_data {
48 struct stm32_exti_host_data *host_data;
49 const struct stm32_exti_bank *reg_bank;
50 struct raw_spinlock rlock;
51 u32 wake_active;
52 u32 mask_cache;
53 u32 rtsr_cache;
54 u32 ftsr_cache;
55 };
56
57 struct stm32_exti_host_data {
58 void __iomem *base;
59 struct stm32_exti_chip_data *chips_data;
60 const struct stm32_exti_drv_data *drv_data;
61 };
62
63 static struct stm32_exti_host_data *stm32_host_data;
64
65 static const struct stm32_exti_bank stm32f4xx_exti_b1 = {
66 .imr_ofst = 0x00,
67 .emr_ofst = 0x04,
68 .rtsr_ofst = 0x08,
69 .ftsr_ofst = 0x0C,
70 .swier_ofst = 0x10,
71 .rpr_ofst = 0x14,
72 .fpr_ofst = UNDEF_REG,
73 };
74
75 static const struct stm32_exti_bank *stm32f4xx_exti_banks[] = {
76 &stm32f4xx_exti_b1,
77 };
78
79 static const struct stm32_exti_drv_data stm32f4xx_drv_data = {
80 .exti_banks = stm32f4xx_exti_banks,
81 .bank_nr = ARRAY_SIZE(stm32f4xx_exti_banks),
82 };
83
84 static const struct stm32_exti_bank stm32h7xx_exti_b1 = {
85 .imr_ofst = 0x80,
86 .emr_ofst = 0x84,
87 .rtsr_ofst = 0x00,
88 .ftsr_ofst = 0x04,
89 .swier_ofst = 0x08,
90 .rpr_ofst = 0x88,
91 .fpr_ofst = UNDEF_REG,
92 };
93
94 static const struct stm32_exti_bank stm32h7xx_exti_b2 = {
95 .imr_ofst = 0x90,
96 .emr_ofst = 0x94,
97 .rtsr_ofst = 0x20,
98 .ftsr_ofst = 0x24,
99 .swier_ofst = 0x28,
100 .rpr_ofst = 0x98,
101 .fpr_ofst = UNDEF_REG,
102 };
103
104 static const struct stm32_exti_bank stm32h7xx_exti_b3 = {
105 .imr_ofst = 0xA0,
106 .emr_ofst = 0xA4,
107 .rtsr_ofst = 0x40,
108 .ftsr_ofst = 0x44,
109 .swier_ofst = 0x48,
110 .rpr_ofst = 0xA8,
111 .fpr_ofst = UNDEF_REG,
112 };
113
114 static const struct stm32_exti_bank *stm32h7xx_exti_banks[] = {
115 &stm32h7xx_exti_b1,
116 &stm32h7xx_exti_b2,
117 &stm32h7xx_exti_b3,
118 };
119
120 static const struct stm32_exti_drv_data stm32h7xx_drv_data = {
121 .exti_banks = stm32h7xx_exti_banks,
122 .bank_nr = ARRAY_SIZE(stm32h7xx_exti_banks),
123 };
124
125 static const struct stm32_exti_bank stm32mp1_exti_b1 = {
126 .imr_ofst = 0x80,
127 .emr_ofst = 0x84,
128 .rtsr_ofst = 0x00,
129 .ftsr_ofst = 0x04,
130 .swier_ofst = 0x08,
131 .rpr_ofst = 0x0C,
132 .fpr_ofst = 0x10,
133 };
134
135 static const struct stm32_exti_bank stm32mp1_exti_b2 = {
136 .imr_ofst = 0x90,
137 .emr_ofst = 0x94,
138 .rtsr_ofst = 0x20,
139 .ftsr_ofst = 0x24,
140 .swier_ofst = 0x28,
141 .rpr_ofst = 0x2C,
142 .fpr_ofst = 0x30,
143 };
144
145 static const struct stm32_exti_bank stm32mp1_exti_b3 = {
146 .imr_ofst = 0xA0,
147 .emr_ofst = 0xA4,
148 .rtsr_ofst = 0x40,
149 .ftsr_ofst = 0x44,
150 .swier_ofst = 0x48,
151 .rpr_ofst = 0x4C,
152 .fpr_ofst = 0x50,
153 };
154
155 static const struct stm32_exti_bank *stm32mp1_exti_banks[] = {
156 &stm32mp1_exti_b1,
157 &stm32mp1_exti_b2,
158 &stm32mp1_exti_b3,
159 };
160
161 static const struct stm32_desc_irq stm32mp1_desc_irq[] = {
162 { .exti = 0, .irq_parent = 6 },
163 { .exti = 1, .irq_parent = 7 },
164 { .exti = 2, .irq_parent = 8 },
165 { .exti = 3, .irq_parent = 9 },
166 { .exti = 4, .irq_parent = 10 },
167 { .exti = 5, .irq_parent = 23 },
168 { .exti = 6, .irq_parent = 64 },
169 { .exti = 7, .irq_parent = 65 },
170 { .exti = 8, .irq_parent = 66 },
171 { .exti = 9, .irq_parent = 67 },
172 { .exti = 10, .irq_parent = 40 },
173 { .exti = 11, .irq_parent = 42 },
174 { .exti = 12, .irq_parent = 76 },
175 { .exti = 13, .irq_parent = 77 },
176 { .exti = 14, .irq_parent = 121 },
177 { .exti = 15, .irq_parent = 127 },
178 { .exti = 16, .irq_parent = 1 },
179 { .exti = 65, .irq_parent = 144 },
180 { .exti = 68, .irq_parent = 143 },
181 { .exti = 73, .irq_parent = 129 },
182 };
183
184 static const struct stm32_exti_drv_data stm32mp1_drv_data = {
185 .exti_banks = stm32mp1_exti_banks,
186 .bank_nr = ARRAY_SIZE(stm32mp1_exti_banks),
187 .desc_irqs = stm32mp1_desc_irq,
188 .irq_nr = ARRAY_SIZE(stm32mp1_desc_irq),
189 };
190
stm32_exti_to_irq(const struct stm32_exti_drv_data * drv_data,irq_hw_number_t hwirq)191 static int stm32_exti_to_irq(const struct stm32_exti_drv_data *drv_data,
192 irq_hw_number_t hwirq)
193 {
194 const struct stm32_desc_irq *desc_irq;
195 int i;
196
197 if (!drv_data->desc_irqs)
198 return -EINVAL;
199
200 for (i = 0; i < drv_data->irq_nr; i++) {
201 desc_irq = &drv_data->desc_irqs[i];
202 if (desc_irq->exti == hwirq)
203 return desc_irq->irq_parent;
204 }
205
206 return -EINVAL;
207 }
208
stm32_exti_pending(struct irq_chip_generic * gc)209 static unsigned long stm32_exti_pending(struct irq_chip_generic *gc)
210 {
211 struct stm32_exti_chip_data *chip_data = gc->private;
212 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
213 unsigned long pending;
214
215 pending = irq_reg_readl(gc, stm32_bank->rpr_ofst);
216 if (stm32_bank->fpr_ofst != UNDEF_REG)
217 pending |= irq_reg_readl(gc, stm32_bank->fpr_ofst);
218
219 return pending;
220 }
221
stm32_irq_handler(struct irq_desc * desc)222 static void stm32_irq_handler(struct irq_desc *desc)
223 {
224 struct irq_domain *domain = irq_desc_get_handler_data(desc);
225 struct irq_chip *chip = irq_desc_get_chip(desc);
226 unsigned int virq, nbanks = domain->gc->num_chips;
227 struct irq_chip_generic *gc;
228 unsigned long pending;
229 int n, i, irq_base = 0;
230
231 chained_irq_enter(chip, desc);
232
233 for (i = 0; i < nbanks; i++, irq_base += IRQS_PER_BANK) {
234 gc = irq_get_domain_generic_chip(domain, irq_base);
235
236 while ((pending = stm32_exti_pending(gc))) {
237 for_each_set_bit(n, &pending, IRQS_PER_BANK) {
238 virq = irq_find_mapping(domain, irq_base + n);
239 generic_handle_irq(virq);
240 }
241 }
242 }
243
244 chained_irq_exit(chip, desc);
245 }
246
stm32_exti_set_type(struct irq_data * d,unsigned int type,u32 * rtsr,u32 * ftsr)247 static int stm32_exti_set_type(struct irq_data *d,
248 unsigned int type, u32 *rtsr, u32 *ftsr)
249 {
250 u32 mask = BIT(d->hwirq % IRQS_PER_BANK);
251
252 switch (type) {
253 case IRQ_TYPE_EDGE_RISING:
254 *rtsr |= mask;
255 *ftsr &= ~mask;
256 break;
257 case IRQ_TYPE_EDGE_FALLING:
258 *rtsr &= ~mask;
259 *ftsr |= mask;
260 break;
261 case IRQ_TYPE_EDGE_BOTH:
262 *rtsr |= mask;
263 *ftsr |= mask;
264 break;
265 default:
266 return -EINVAL;
267 }
268
269 return 0;
270 }
271
stm32_irq_set_type(struct irq_data * d,unsigned int type)272 static int stm32_irq_set_type(struct irq_data *d, unsigned int type)
273 {
274 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
275 struct stm32_exti_chip_data *chip_data = gc->private;
276 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
277 u32 rtsr, ftsr;
278 int err;
279
280 irq_gc_lock(gc);
281
282 rtsr = irq_reg_readl(gc, stm32_bank->rtsr_ofst);
283 ftsr = irq_reg_readl(gc, stm32_bank->ftsr_ofst);
284
285 err = stm32_exti_set_type(d, type, &rtsr, &ftsr);
286 if (err) {
287 irq_gc_unlock(gc);
288 return err;
289 }
290
291 irq_reg_writel(gc, rtsr, stm32_bank->rtsr_ofst);
292 irq_reg_writel(gc, ftsr, stm32_bank->ftsr_ofst);
293
294 irq_gc_unlock(gc);
295
296 return 0;
297 }
298
stm32_chip_suspend(struct stm32_exti_chip_data * chip_data,u32 wake_active)299 static void stm32_chip_suspend(struct stm32_exti_chip_data *chip_data,
300 u32 wake_active)
301 {
302 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
303 void __iomem *base = chip_data->host_data->base;
304
305 /* save rtsr, ftsr registers */
306 chip_data->rtsr_cache = readl_relaxed(base + stm32_bank->rtsr_ofst);
307 chip_data->ftsr_cache = readl_relaxed(base + stm32_bank->ftsr_ofst);
308
309 writel_relaxed(wake_active, base + stm32_bank->imr_ofst);
310 }
311
stm32_chip_resume(struct stm32_exti_chip_data * chip_data,u32 mask_cache)312 static void stm32_chip_resume(struct stm32_exti_chip_data *chip_data,
313 u32 mask_cache)
314 {
315 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
316 void __iomem *base = chip_data->host_data->base;
317
318 /* restore rtsr, ftsr, registers */
319 writel_relaxed(chip_data->rtsr_cache, base + stm32_bank->rtsr_ofst);
320 writel_relaxed(chip_data->ftsr_cache, base + stm32_bank->ftsr_ofst);
321
322 writel_relaxed(mask_cache, base + stm32_bank->imr_ofst);
323 }
324
stm32_irq_suspend(struct irq_chip_generic * gc)325 static void stm32_irq_suspend(struct irq_chip_generic *gc)
326 {
327 struct stm32_exti_chip_data *chip_data = gc->private;
328
329 irq_gc_lock(gc);
330 stm32_chip_suspend(chip_data, gc->wake_active);
331 irq_gc_unlock(gc);
332 }
333
stm32_irq_resume(struct irq_chip_generic * gc)334 static void stm32_irq_resume(struct irq_chip_generic *gc)
335 {
336 struct stm32_exti_chip_data *chip_data = gc->private;
337
338 irq_gc_lock(gc);
339 stm32_chip_resume(chip_data, gc->mask_cache);
340 irq_gc_unlock(gc);
341 }
342
stm32_exti_alloc(struct irq_domain * d,unsigned int virq,unsigned int nr_irqs,void * data)343 static int stm32_exti_alloc(struct irq_domain *d, unsigned int virq,
344 unsigned int nr_irqs, void *data)
345 {
346 struct irq_fwspec *fwspec = data;
347 irq_hw_number_t hwirq;
348
349 hwirq = fwspec->param[0];
350
351 irq_map_generic_chip(d, virq, hwirq);
352
353 return 0;
354 }
355
stm32_exti_free(struct irq_domain * d,unsigned int virq,unsigned int nr_irqs)356 static void stm32_exti_free(struct irq_domain *d, unsigned int virq,
357 unsigned int nr_irqs)
358 {
359 struct irq_data *data = irq_domain_get_irq_data(d, virq);
360
361 irq_domain_reset_irq_data(data);
362 }
363
364 static const struct irq_domain_ops irq_exti_domain_ops = {
365 .map = irq_map_generic_chip,
366 .alloc = stm32_exti_alloc,
367 .free = stm32_exti_free,
368 };
369
stm32_irq_ack(struct irq_data * d)370 static void stm32_irq_ack(struct irq_data *d)
371 {
372 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
373 struct stm32_exti_chip_data *chip_data = gc->private;
374 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
375
376 irq_gc_lock(gc);
377
378 irq_reg_writel(gc, d->mask, stm32_bank->rpr_ofst);
379 if (stm32_bank->fpr_ofst != UNDEF_REG)
380 irq_reg_writel(gc, d->mask, stm32_bank->fpr_ofst);
381
382 irq_gc_unlock(gc);
383 }
384
385 /* directly set the target bit without reading first. */
stm32_exti_write_bit(struct irq_data * d,u32 reg)386 static inline void stm32_exti_write_bit(struct irq_data *d, u32 reg)
387 {
388 struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
389 void __iomem *base = chip_data->host_data->base;
390 u32 val = BIT(d->hwirq % IRQS_PER_BANK);
391
392 writel_relaxed(val, base + reg);
393 }
394
stm32_exti_set_bit(struct irq_data * d,u32 reg)395 static inline u32 stm32_exti_set_bit(struct irq_data *d, u32 reg)
396 {
397 struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
398 void __iomem *base = chip_data->host_data->base;
399 u32 val;
400
401 val = readl_relaxed(base + reg);
402 val |= BIT(d->hwirq % IRQS_PER_BANK);
403 writel_relaxed(val, base + reg);
404
405 return val;
406 }
407
stm32_exti_clr_bit(struct irq_data * d,u32 reg)408 static inline u32 stm32_exti_clr_bit(struct irq_data *d, u32 reg)
409 {
410 struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
411 void __iomem *base = chip_data->host_data->base;
412 u32 val;
413
414 val = readl_relaxed(base + reg);
415 val &= ~BIT(d->hwirq % IRQS_PER_BANK);
416 writel_relaxed(val, base + reg);
417
418 return val;
419 }
420
stm32_exti_h_eoi(struct irq_data * d)421 static void stm32_exti_h_eoi(struct irq_data *d)
422 {
423 struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
424 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
425
426 raw_spin_lock(&chip_data->rlock);
427
428 stm32_exti_write_bit(d, stm32_bank->rpr_ofst);
429 if (stm32_bank->fpr_ofst != UNDEF_REG)
430 stm32_exti_write_bit(d, stm32_bank->fpr_ofst);
431
432 raw_spin_unlock(&chip_data->rlock);
433
434 if (d->parent_data->chip)
435 irq_chip_eoi_parent(d);
436 }
437
stm32_exti_h_mask(struct irq_data * d)438 static void stm32_exti_h_mask(struct irq_data *d)
439 {
440 struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
441 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
442
443 raw_spin_lock(&chip_data->rlock);
444 chip_data->mask_cache = stm32_exti_clr_bit(d, stm32_bank->imr_ofst);
445 raw_spin_unlock(&chip_data->rlock);
446
447 if (d->parent_data->chip)
448 irq_chip_mask_parent(d);
449 }
450
stm32_exti_h_unmask(struct irq_data * d)451 static void stm32_exti_h_unmask(struct irq_data *d)
452 {
453 struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
454 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
455
456 raw_spin_lock(&chip_data->rlock);
457 chip_data->mask_cache = stm32_exti_set_bit(d, stm32_bank->imr_ofst);
458 raw_spin_unlock(&chip_data->rlock);
459
460 if (d->parent_data->chip)
461 irq_chip_unmask_parent(d);
462 }
463
stm32_exti_h_set_type(struct irq_data * d,unsigned int type)464 static int stm32_exti_h_set_type(struct irq_data *d, unsigned int type)
465 {
466 struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
467 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
468 void __iomem *base = chip_data->host_data->base;
469 u32 rtsr, ftsr;
470 int err;
471
472 raw_spin_lock(&chip_data->rlock);
473 rtsr = readl_relaxed(base + stm32_bank->rtsr_ofst);
474 ftsr = readl_relaxed(base + stm32_bank->ftsr_ofst);
475
476 err = stm32_exti_set_type(d, type, &rtsr, &ftsr);
477 if (err) {
478 raw_spin_unlock(&chip_data->rlock);
479 return err;
480 }
481
482 writel_relaxed(rtsr, base + stm32_bank->rtsr_ofst);
483 writel_relaxed(ftsr, base + stm32_bank->ftsr_ofst);
484 raw_spin_unlock(&chip_data->rlock);
485
486 return 0;
487 }
488
stm32_exti_h_set_wake(struct irq_data * d,unsigned int on)489 static int stm32_exti_h_set_wake(struct irq_data *d, unsigned int on)
490 {
491 struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
492 u32 mask = BIT(d->hwirq % IRQS_PER_BANK);
493
494 raw_spin_lock(&chip_data->rlock);
495
496 if (on)
497 chip_data->wake_active |= mask;
498 else
499 chip_data->wake_active &= ~mask;
500
501 raw_spin_unlock(&chip_data->rlock);
502
503 return 0;
504 }
505
stm32_exti_h_set_affinity(struct irq_data * d,const struct cpumask * dest,bool force)506 static int stm32_exti_h_set_affinity(struct irq_data *d,
507 const struct cpumask *dest, bool force)
508 {
509 if (d->parent_data->chip)
510 return irq_chip_set_affinity_parent(d, dest, force);
511
512 return -EINVAL;
513 }
514
515 #ifdef CONFIG_PM
stm32_exti_h_suspend(void)516 static int stm32_exti_h_suspend(void)
517 {
518 struct stm32_exti_chip_data *chip_data;
519 int i;
520
521 for (i = 0; i < stm32_host_data->drv_data->bank_nr; i++) {
522 chip_data = &stm32_host_data->chips_data[i];
523 raw_spin_lock(&chip_data->rlock);
524 stm32_chip_suspend(chip_data, chip_data->wake_active);
525 raw_spin_unlock(&chip_data->rlock);
526 }
527
528 return 0;
529 }
530
stm32_exti_h_resume(void)531 static void stm32_exti_h_resume(void)
532 {
533 struct stm32_exti_chip_data *chip_data;
534 int i;
535
536 for (i = 0; i < stm32_host_data->drv_data->bank_nr; i++) {
537 chip_data = &stm32_host_data->chips_data[i];
538 raw_spin_lock(&chip_data->rlock);
539 stm32_chip_resume(chip_data, chip_data->mask_cache);
540 raw_spin_unlock(&chip_data->rlock);
541 }
542 }
543
544 static struct syscore_ops stm32_exti_h_syscore_ops = {
545 .suspend = stm32_exti_h_suspend,
546 .resume = stm32_exti_h_resume,
547 };
548
stm32_exti_h_syscore_init(void)549 static void stm32_exti_h_syscore_init(void)
550 {
551 register_syscore_ops(&stm32_exti_h_syscore_ops);
552 }
553 #else
stm32_exti_h_syscore_init(void)554 static inline void stm32_exti_h_syscore_init(void) {}
555 #endif
556
557 static struct irq_chip stm32_exti_h_chip = {
558 .name = "stm32-exti-h",
559 .irq_eoi = stm32_exti_h_eoi,
560 .irq_mask = stm32_exti_h_mask,
561 .irq_unmask = stm32_exti_h_unmask,
562 .irq_retrigger = irq_chip_retrigger_hierarchy,
563 .irq_set_type = stm32_exti_h_set_type,
564 .irq_set_wake = stm32_exti_h_set_wake,
565 .flags = IRQCHIP_MASK_ON_SUSPEND,
566 .irq_set_affinity = IS_ENABLED(CONFIG_SMP) ? stm32_exti_h_set_affinity : NULL,
567 };
568
stm32_exti_h_domain_alloc(struct irq_domain * dm,unsigned int virq,unsigned int nr_irqs,void * data)569 static int stm32_exti_h_domain_alloc(struct irq_domain *dm,
570 unsigned int virq,
571 unsigned int nr_irqs, void *data)
572 {
573 struct stm32_exti_host_data *host_data = dm->host_data;
574 struct stm32_exti_chip_data *chip_data;
575 struct irq_fwspec *fwspec = data;
576 struct irq_fwspec p_fwspec;
577 irq_hw_number_t hwirq;
578 int p_irq, bank;
579
580 hwirq = fwspec->param[0];
581 bank = hwirq / IRQS_PER_BANK;
582 chip_data = &host_data->chips_data[bank];
583
584 irq_domain_set_hwirq_and_chip(dm, virq, hwirq,
585 &stm32_exti_h_chip, chip_data);
586
587 p_irq = stm32_exti_to_irq(host_data->drv_data, hwirq);
588 if (p_irq >= 0) {
589 p_fwspec.fwnode = dm->parent->fwnode;
590 p_fwspec.param_count = 3;
591 p_fwspec.param[0] = GIC_SPI;
592 p_fwspec.param[1] = p_irq;
593 p_fwspec.param[2] = IRQ_TYPE_LEVEL_HIGH;
594
595 return irq_domain_alloc_irqs_parent(dm, virq, 1, &p_fwspec);
596 }
597
598 return 0;
599 }
600
601 static struct
stm32_exti_host_init(const struct stm32_exti_drv_data * dd,struct device_node * node)602 stm32_exti_host_data *stm32_exti_host_init(const struct stm32_exti_drv_data *dd,
603 struct device_node *node)
604 {
605 struct stm32_exti_host_data *host_data;
606
607 host_data = kzalloc(sizeof(*host_data), GFP_KERNEL);
608 if (!host_data)
609 return NULL;
610
611 host_data->drv_data = dd;
612 host_data->chips_data = kcalloc(dd->bank_nr,
613 sizeof(struct stm32_exti_chip_data),
614 GFP_KERNEL);
615 if (!host_data->chips_data)
616 goto free_host_data;
617
618 host_data->base = of_iomap(node, 0);
619 if (!host_data->base) {
620 pr_err("%pOF: Unable to map registers\n", node);
621 goto free_chips_data;
622 }
623
624 stm32_host_data = host_data;
625
626 return host_data;
627
628 free_chips_data:
629 kfree(host_data->chips_data);
630 free_host_data:
631 kfree(host_data);
632
633 return NULL;
634 }
635
636 static struct
stm32_exti_chip_init(struct stm32_exti_host_data * h_data,u32 bank_idx,struct device_node * node)637 stm32_exti_chip_data *stm32_exti_chip_init(struct stm32_exti_host_data *h_data,
638 u32 bank_idx,
639 struct device_node *node)
640 {
641 const struct stm32_exti_bank *stm32_bank;
642 struct stm32_exti_chip_data *chip_data;
643 void __iomem *base = h_data->base;
644 u32 irqs_mask;
645
646 stm32_bank = h_data->drv_data->exti_banks[bank_idx];
647 chip_data = &h_data->chips_data[bank_idx];
648 chip_data->host_data = h_data;
649 chip_data->reg_bank = stm32_bank;
650
651 raw_spin_lock_init(&chip_data->rlock);
652
653 /* Determine number of irqs supported */
654 writel_relaxed(~0UL, base + stm32_bank->rtsr_ofst);
655 irqs_mask = readl_relaxed(base + stm32_bank->rtsr_ofst);
656
657 /*
658 * This IP has no reset, so after hot reboot we should
659 * clear registers to avoid residue
660 */
661 writel_relaxed(0, base + stm32_bank->imr_ofst);
662 writel_relaxed(0, base + stm32_bank->emr_ofst);
663
664 pr_info("%s: bank%d, External IRQs available:%#x\n",
665 node->full_name, bank_idx, irqs_mask);
666
667 return chip_data;
668 }
669
stm32_exti_init(const struct stm32_exti_drv_data * drv_data,struct device_node * node)670 static int __init stm32_exti_init(const struct stm32_exti_drv_data *drv_data,
671 struct device_node *node)
672 {
673 struct stm32_exti_host_data *host_data;
674 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
675 int nr_irqs, ret, i;
676 struct irq_chip_generic *gc;
677 struct irq_domain *domain;
678
679 host_data = stm32_exti_host_init(drv_data, node);
680 if (!host_data)
681 return -ENOMEM;
682
683 domain = irq_domain_add_linear(node, drv_data->bank_nr * IRQS_PER_BANK,
684 &irq_exti_domain_ops, NULL);
685 if (!domain) {
686 pr_err("%s: Could not register interrupt domain.\n",
687 node->name);
688 ret = -ENOMEM;
689 goto out_unmap;
690 }
691
692 ret = irq_alloc_domain_generic_chips(domain, IRQS_PER_BANK, 1, "exti",
693 handle_edge_irq, clr, 0, 0);
694 if (ret) {
695 pr_err("%pOF: Could not allocate generic interrupt chip.\n",
696 node);
697 goto out_free_domain;
698 }
699
700 for (i = 0; i < drv_data->bank_nr; i++) {
701 const struct stm32_exti_bank *stm32_bank;
702 struct stm32_exti_chip_data *chip_data;
703
704 stm32_bank = drv_data->exti_banks[i];
705 chip_data = stm32_exti_chip_init(host_data, i, node);
706
707 gc = irq_get_domain_generic_chip(domain, i * IRQS_PER_BANK);
708
709 gc->reg_base = host_data->base;
710 gc->chip_types->type = IRQ_TYPE_EDGE_BOTH;
711 gc->chip_types->chip.irq_ack = stm32_irq_ack;
712 gc->chip_types->chip.irq_mask = irq_gc_mask_clr_bit;
713 gc->chip_types->chip.irq_unmask = irq_gc_mask_set_bit;
714 gc->chip_types->chip.irq_set_type = stm32_irq_set_type;
715 gc->chip_types->chip.irq_set_wake = irq_gc_set_wake;
716 gc->suspend = stm32_irq_suspend;
717 gc->resume = stm32_irq_resume;
718 gc->wake_enabled = IRQ_MSK(IRQS_PER_BANK);
719
720 gc->chip_types->regs.mask = stm32_bank->imr_ofst;
721 gc->private = (void *)chip_data;
722 }
723
724 nr_irqs = of_irq_count(node);
725 for (i = 0; i < nr_irqs; i++) {
726 unsigned int irq = irq_of_parse_and_map(node, i);
727
728 irq_set_handler_data(irq, domain);
729 irq_set_chained_handler(irq, stm32_irq_handler);
730 }
731
732 return 0;
733
734 out_free_domain:
735 irq_domain_remove(domain);
736 out_unmap:
737 iounmap(host_data->base);
738 kfree(host_data->chips_data);
739 kfree(host_data);
740 return ret;
741 }
742
743 static const struct irq_domain_ops stm32_exti_h_domain_ops = {
744 .alloc = stm32_exti_h_domain_alloc,
745 .free = irq_domain_free_irqs_common,
746 };
747
748 static int
stm32_exti_hierarchy_init(const struct stm32_exti_drv_data * drv_data,struct device_node * node,struct device_node * parent)749 __init stm32_exti_hierarchy_init(const struct stm32_exti_drv_data *drv_data,
750 struct device_node *node,
751 struct device_node *parent)
752 {
753 struct irq_domain *parent_domain, *domain;
754 struct stm32_exti_host_data *host_data;
755 int ret, i;
756
757 parent_domain = irq_find_host(parent);
758 if (!parent_domain) {
759 pr_err("interrupt-parent not found\n");
760 return -EINVAL;
761 }
762
763 host_data = stm32_exti_host_init(drv_data, node);
764 if (!host_data)
765 return -ENOMEM;
766
767 for (i = 0; i < drv_data->bank_nr; i++)
768 stm32_exti_chip_init(host_data, i, node);
769
770 domain = irq_domain_add_hierarchy(parent_domain, 0,
771 drv_data->bank_nr * IRQS_PER_BANK,
772 node, &stm32_exti_h_domain_ops,
773 host_data);
774
775 if (!domain) {
776 pr_err("%s: Could not register exti domain.\n", node->name);
777 ret = -ENOMEM;
778 goto out_unmap;
779 }
780
781 stm32_exti_h_syscore_init();
782
783 return 0;
784
785 out_unmap:
786 iounmap(host_data->base);
787 kfree(host_data->chips_data);
788 kfree(host_data);
789 return ret;
790 }
791
stm32f4_exti_of_init(struct device_node * np,struct device_node * parent)792 static int __init stm32f4_exti_of_init(struct device_node *np,
793 struct device_node *parent)
794 {
795 return stm32_exti_init(&stm32f4xx_drv_data, np);
796 }
797
798 IRQCHIP_DECLARE(stm32f4_exti, "st,stm32-exti", stm32f4_exti_of_init);
799
stm32h7_exti_of_init(struct device_node * np,struct device_node * parent)800 static int __init stm32h7_exti_of_init(struct device_node *np,
801 struct device_node *parent)
802 {
803 return stm32_exti_init(&stm32h7xx_drv_data, np);
804 }
805
806 IRQCHIP_DECLARE(stm32h7_exti, "st,stm32h7-exti", stm32h7_exti_of_init);
807
stm32mp1_exti_of_init(struct device_node * np,struct device_node * parent)808 static int __init stm32mp1_exti_of_init(struct device_node *np,
809 struct device_node *parent)
810 {
811 return stm32_exti_hierarchy_init(&stm32mp1_drv_data, np, parent);
812 }
813
814 IRQCHIP_DECLARE(stm32mp1_exti, "st,stm32mp1-exti", stm32mp1_exti_of_init);
815