Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) Maxime Coquelin 2015
0004  * Copyright (C) STMicroelectronics 2017
0005  * Author:  Maxime Coquelin <mcoquelin.stm32@gmail.com>
0006  */
0007 
0008 #include <linux/bitops.h>
0009 #include <linux/delay.h>
0010 #include <linux/hwspinlock.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/io.h>
0013 #include <linux/irq.h>
0014 #include <linux/irqchip.h>
0015 #include <linux/irqchip/chained_irq.h>
0016 #include <linux/irqdomain.h>
0017 #include <linux/module.h>
0018 #include <linux/of_address.h>
0019 #include <linux/of_irq.h>
0020 #include <linux/of_platform.h>
0021 #include <linux/syscore_ops.h>
0022 
0023 #include <dt-bindings/interrupt-controller/arm-gic.h>
0024 
0025 #define IRQS_PER_BANK 32
0026 
0027 #define HWSPNLCK_TIMEOUT    1000 /* usec */
0028 
0029 struct stm32_exti_bank {
0030     u32 imr_ofst;
0031     u32 emr_ofst;
0032     u32 rtsr_ofst;
0033     u32 ftsr_ofst;
0034     u32 swier_ofst;
0035     u32 rpr_ofst;
0036     u32 fpr_ofst;
0037     u32 trg_ofst;
0038 };
0039 
0040 #define UNDEF_REG ~0
0041 
0042 struct stm32_exti_drv_data {
0043     const struct stm32_exti_bank **exti_banks;
0044     const u8 *desc_irqs;
0045     u32 bank_nr;
0046 };
0047 
0048 struct stm32_exti_chip_data {
0049     struct stm32_exti_host_data *host_data;
0050     const struct stm32_exti_bank *reg_bank;
0051     struct raw_spinlock rlock;
0052     u32 wake_active;
0053     u32 mask_cache;
0054     u32 rtsr_cache;
0055     u32 ftsr_cache;
0056 };
0057 
0058 struct stm32_exti_host_data {
0059     void __iomem *base;
0060     struct stm32_exti_chip_data *chips_data;
0061     const struct stm32_exti_drv_data *drv_data;
0062     struct hwspinlock *hwlock;
0063 };
0064 
0065 static struct stm32_exti_host_data *stm32_host_data;
0066 
0067 static const struct stm32_exti_bank stm32f4xx_exti_b1 = {
0068     .imr_ofst   = 0x00,
0069     .emr_ofst   = 0x04,
0070     .rtsr_ofst  = 0x08,
0071     .ftsr_ofst  = 0x0C,
0072     .swier_ofst = 0x10,
0073     .rpr_ofst   = 0x14,
0074     .fpr_ofst   = UNDEF_REG,
0075     .trg_ofst   = UNDEF_REG,
0076 };
0077 
0078 static const struct stm32_exti_bank *stm32f4xx_exti_banks[] = {
0079     &stm32f4xx_exti_b1,
0080 };
0081 
0082 static const struct stm32_exti_drv_data stm32f4xx_drv_data = {
0083     .exti_banks = stm32f4xx_exti_banks,
0084     .bank_nr = ARRAY_SIZE(stm32f4xx_exti_banks),
0085 };
0086 
0087 static const struct stm32_exti_bank stm32h7xx_exti_b1 = {
0088     .imr_ofst   = 0x80,
0089     .emr_ofst   = 0x84,
0090     .rtsr_ofst  = 0x00,
0091     .ftsr_ofst  = 0x04,
0092     .swier_ofst = 0x08,
0093     .rpr_ofst   = 0x88,
0094     .fpr_ofst   = UNDEF_REG,
0095     .trg_ofst   = UNDEF_REG,
0096 };
0097 
0098 static const struct stm32_exti_bank stm32h7xx_exti_b2 = {
0099     .imr_ofst   = 0x90,
0100     .emr_ofst   = 0x94,
0101     .rtsr_ofst  = 0x20,
0102     .ftsr_ofst  = 0x24,
0103     .swier_ofst = 0x28,
0104     .rpr_ofst   = 0x98,
0105     .fpr_ofst   = UNDEF_REG,
0106     .trg_ofst   = UNDEF_REG,
0107 };
0108 
0109 static const struct stm32_exti_bank stm32h7xx_exti_b3 = {
0110     .imr_ofst   = 0xA0,
0111     .emr_ofst   = 0xA4,
0112     .rtsr_ofst  = 0x40,
0113     .ftsr_ofst  = 0x44,
0114     .swier_ofst = 0x48,
0115     .rpr_ofst   = 0xA8,
0116     .fpr_ofst   = UNDEF_REG,
0117     .trg_ofst   = UNDEF_REG,
0118 };
0119 
0120 static const struct stm32_exti_bank *stm32h7xx_exti_banks[] = {
0121     &stm32h7xx_exti_b1,
0122     &stm32h7xx_exti_b2,
0123     &stm32h7xx_exti_b3,
0124 };
0125 
0126 static const struct stm32_exti_drv_data stm32h7xx_drv_data = {
0127     .exti_banks = stm32h7xx_exti_banks,
0128     .bank_nr = ARRAY_SIZE(stm32h7xx_exti_banks),
0129 };
0130 
0131 static const struct stm32_exti_bank stm32mp1_exti_b1 = {
0132     .imr_ofst   = 0x80,
0133     .emr_ofst   = UNDEF_REG,
0134     .rtsr_ofst  = 0x00,
0135     .ftsr_ofst  = 0x04,
0136     .swier_ofst = 0x08,
0137     .rpr_ofst   = 0x0C,
0138     .fpr_ofst   = 0x10,
0139     .trg_ofst   = 0x3EC,
0140 };
0141 
0142 static const struct stm32_exti_bank stm32mp1_exti_b2 = {
0143     .imr_ofst   = 0x90,
0144     .emr_ofst   = UNDEF_REG,
0145     .rtsr_ofst  = 0x20,
0146     .ftsr_ofst  = 0x24,
0147     .swier_ofst = 0x28,
0148     .rpr_ofst   = 0x2C,
0149     .fpr_ofst   = 0x30,
0150     .trg_ofst   = 0x3E8,
0151 };
0152 
0153 static const struct stm32_exti_bank stm32mp1_exti_b3 = {
0154     .imr_ofst   = 0xA0,
0155     .emr_ofst   = UNDEF_REG,
0156     .rtsr_ofst  = 0x40,
0157     .ftsr_ofst  = 0x44,
0158     .swier_ofst = 0x48,
0159     .rpr_ofst   = 0x4C,
0160     .fpr_ofst   = 0x50,
0161     .trg_ofst   = 0x3E4,
0162 };
0163 
0164 static const struct stm32_exti_bank *stm32mp1_exti_banks[] = {
0165     &stm32mp1_exti_b1,
0166     &stm32mp1_exti_b2,
0167     &stm32mp1_exti_b3,
0168 };
0169 
0170 static struct irq_chip stm32_exti_h_chip;
0171 static struct irq_chip stm32_exti_h_chip_direct;
0172 
0173 #define EXTI_INVALID_IRQ       U8_MAX
0174 #define STM32MP1_DESC_IRQ_SIZE (ARRAY_SIZE(stm32mp1_exti_banks) * IRQS_PER_BANK)
0175 
0176 static const u8 stm32mp1_desc_irq[] = {
0177     /* default value */
0178     [0 ... (STM32MP1_DESC_IRQ_SIZE - 1)] = EXTI_INVALID_IRQ,
0179 
0180     [0] = 6,
0181     [1] = 7,
0182     [2] = 8,
0183     [3] = 9,
0184     [4] = 10,
0185     [5] = 23,
0186     [6] = 64,
0187     [7] = 65,
0188     [8] = 66,
0189     [9] = 67,
0190     [10] = 40,
0191     [11] = 42,
0192     [12] = 76,
0193     [13] = 77,
0194     [14] = 121,
0195     [15] = 127,
0196     [16] = 1,
0197     [19] = 3,
0198     [21] = 31,
0199     [22] = 33,
0200     [23] = 72,
0201     [24] = 95,
0202     [25] = 107,
0203     [26] = 37,
0204     [27] = 38,
0205     [28] = 39,
0206     [29] = 71,
0207     [30] = 52,
0208     [31] = 53,
0209     [32] = 82,
0210     [33] = 83,
0211     [47] = 93,
0212     [48] = 138,
0213     [50] = 139,
0214     [52] = 140,
0215     [53] = 141,
0216     [54] = 135,
0217     [61] = 100,
0218     [65] = 144,
0219     [68] = 143,
0220     [70] = 62,
0221     [73] = 129,
0222 };
0223 
0224 static const u8 stm32mp13_desc_irq[] = {
0225     /* default value */
0226     [0 ... (STM32MP1_DESC_IRQ_SIZE - 1)] = EXTI_INVALID_IRQ,
0227 
0228     [0] = 6,
0229     [1] = 7,
0230     [2] = 8,
0231     [3] = 9,
0232     [4] = 10,
0233     [5] = 24,
0234     [6] = 65,
0235     [7] = 66,
0236     [8] = 67,
0237     [9] = 68,
0238     [10] = 41,
0239     [11] = 43,
0240     [12] = 77,
0241     [13] = 78,
0242     [14] = 106,
0243     [15] = 109,
0244     [16] = 1,
0245     [19] = 3,
0246     [21] = 32,
0247     [22] = 34,
0248     [23] = 73,
0249     [24] = 93,
0250     [25] = 114,
0251     [26] = 38,
0252     [27] = 39,
0253     [28] = 40,
0254     [29] = 72,
0255     [30] = 53,
0256     [31] = 54,
0257     [32] = 83,
0258     [33] = 84,
0259     [44] = 96,
0260     [47] = 92,
0261     [48] = 116,
0262     [50] = 117,
0263     [52] = 118,
0264     [53] = 119,
0265     [68] = 63,
0266     [70] = 98,
0267 };
0268 
0269 static const struct stm32_exti_drv_data stm32mp1_drv_data = {
0270     .exti_banks = stm32mp1_exti_banks,
0271     .bank_nr = ARRAY_SIZE(stm32mp1_exti_banks),
0272     .desc_irqs = stm32mp1_desc_irq,
0273 };
0274 
0275 static const struct stm32_exti_drv_data stm32mp13_drv_data = {
0276     .exti_banks = stm32mp1_exti_banks,
0277     .bank_nr = ARRAY_SIZE(stm32mp1_exti_banks),
0278     .desc_irqs = stm32mp13_desc_irq,
0279 };
0280 
0281 static unsigned long stm32_exti_pending(struct irq_chip_generic *gc)
0282 {
0283     struct stm32_exti_chip_data *chip_data = gc->private;
0284     const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
0285     unsigned long pending;
0286 
0287     pending = irq_reg_readl(gc, stm32_bank->rpr_ofst);
0288     if (stm32_bank->fpr_ofst != UNDEF_REG)
0289         pending |= irq_reg_readl(gc, stm32_bank->fpr_ofst);
0290 
0291     return pending;
0292 }
0293 
0294 static void stm32_irq_handler(struct irq_desc *desc)
0295 {
0296     struct irq_domain *domain = irq_desc_get_handler_data(desc);
0297     struct irq_chip *chip = irq_desc_get_chip(desc);
0298     unsigned int nbanks = domain->gc->num_chips;
0299     struct irq_chip_generic *gc;
0300     unsigned long pending;
0301     int n, i, irq_base = 0;
0302 
0303     chained_irq_enter(chip, desc);
0304 
0305     for (i = 0; i < nbanks; i++, irq_base += IRQS_PER_BANK) {
0306         gc = irq_get_domain_generic_chip(domain, irq_base);
0307 
0308         while ((pending = stm32_exti_pending(gc))) {
0309             for_each_set_bit(n, &pending, IRQS_PER_BANK)
0310                 generic_handle_domain_irq(domain, irq_base + n);
0311         }
0312     }
0313 
0314     chained_irq_exit(chip, desc);
0315 }
0316 
0317 static int stm32_exti_set_type(struct irq_data *d,
0318                    unsigned int type, u32 *rtsr, u32 *ftsr)
0319 {
0320     u32 mask = BIT(d->hwirq % IRQS_PER_BANK);
0321 
0322     switch (type) {
0323     case IRQ_TYPE_EDGE_RISING:
0324         *rtsr |= mask;
0325         *ftsr &= ~mask;
0326         break;
0327     case IRQ_TYPE_EDGE_FALLING:
0328         *rtsr &= ~mask;
0329         *ftsr |= mask;
0330         break;
0331     case IRQ_TYPE_EDGE_BOTH:
0332         *rtsr |= mask;
0333         *ftsr |= mask;
0334         break;
0335     default:
0336         return -EINVAL;
0337     }
0338 
0339     return 0;
0340 }
0341 
0342 static int stm32_irq_set_type(struct irq_data *d, unsigned int type)
0343 {
0344     struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
0345     struct stm32_exti_chip_data *chip_data = gc->private;
0346     const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
0347     struct hwspinlock *hwlock = chip_data->host_data->hwlock;
0348     u32 rtsr, ftsr;
0349     int err;
0350 
0351     irq_gc_lock(gc);
0352 
0353     if (hwlock) {
0354         err = hwspin_lock_timeout_in_atomic(hwlock, HWSPNLCK_TIMEOUT);
0355         if (err) {
0356             pr_err("%s can't get hwspinlock (%d)\n", __func__, err);
0357             goto unlock;
0358         }
0359     }
0360 
0361     rtsr = irq_reg_readl(gc, stm32_bank->rtsr_ofst);
0362     ftsr = irq_reg_readl(gc, stm32_bank->ftsr_ofst);
0363 
0364     err = stm32_exti_set_type(d, type, &rtsr, &ftsr);
0365     if (err)
0366         goto unspinlock;
0367 
0368     irq_reg_writel(gc, rtsr, stm32_bank->rtsr_ofst);
0369     irq_reg_writel(gc, ftsr, stm32_bank->ftsr_ofst);
0370 
0371 unspinlock:
0372     if (hwlock)
0373         hwspin_unlock_in_atomic(hwlock);
0374 unlock:
0375     irq_gc_unlock(gc);
0376 
0377     return err;
0378 }
0379 
0380 static void stm32_chip_suspend(struct stm32_exti_chip_data *chip_data,
0381                    u32 wake_active)
0382 {
0383     const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
0384     void __iomem *base = chip_data->host_data->base;
0385 
0386     /* save rtsr, ftsr registers */
0387     chip_data->rtsr_cache = readl_relaxed(base + stm32_bank->rtsr_ofst);
0388     chip_data->ftsr_cache = readl_relaxed(base + stm32_bank->ftsr_ofst);
0389 
0390     writel_relaxed(wake_active, base + stm32_bank->imr_ofst);
0391 }
0392 
0393 static void stm32_chip_resume(struct stm32_exti_chip_data *chip_data,
0394                   u32 mask_cache)
0395 {
0396     const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
0397     void __iomem *base = chip_data->host_data->base;
0398 
0399     /* restore rtsr, ftsr, registers */
0400     writel_relaxed(chip_data->rtsr_cache, base + stm32_bank->rtsr_ofst);
0401     writel_relaxed(chip_data->ftsr_cache, base + stm32_bank->ftsr_ofst);
0402 
0403     writel_relaxed(mask_cache, base + stm32_bank->imr_ofst);
0404 }
0405 
0406 static void stm32_irq_suspend(struct irq_chip_generic *gc)
0407 {
0408     struct stm32_exti_chip_data *chip_data = gc->private;
0409 
0410     irq_gc_lock(gc);
0411     stm32_chip_suspend(chip_data, gc->wake_active);
0412     irq_gc_unlock(gc);
0413 }
0414 
0415 static void stm32_irq_resume(struct irq_chip_generic *gc)
0416 {
0417     struct stm32_exti_chip_data *chip_data = gc->private;
0418 
0419     irq_gc_lock(gc);
0420     stm32_chip_resume(chip_data, gc->mask_cache);
0421     irq_gc_unlock(gc);
0422 }
0423 
0424 static int stm32_exti_alloc(struct irq_domain *d, unsigned int virq,
0425                 unsigned int nr_irqs, void *data)
0426 {
0427     struct irq_fwspec *fwspec = data;
0428     irq_hw_number_t hwirq;
0429 
0430     hwirq = fwspec->param[0];
0431 
0432     irq_map_generic_chip(d, virq, hwirq);
0433 
0434     return 0;
0435 }
0436 
0437 static void stm32_exti_free(struct irq_domain *d, unsigned int virq,
0438                 unsigned int nr_irqs)
0439 {
0440     struct irq_data *data = irq_domain_get_irq_data(d, virq);
0441 
0442     irq_domain_reset_irq_data(data);
0443 }
0444 
0445 static const struct irq_domain_ops irq_exti_domain_ops = {
0446     .map    = irq_map_generic_chip,
0447     .alloc  = stm32_exti_alloc,
0448     .free   = stm32_exti_free,
0449 };
0450 
0451 static void stm32_irq_ack(struct irq_data *d)
0452 {
0453     struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
0454     struct stm32_exti_chip_data *chip_data = gc->private;
0455     const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
0456 
0457     irq_gc_lock(gc);
0458 
0459     irq_reg_writel(gc, d->mask, stm32_bank->rpr_ofst);
0460     if (stm32_bank->fpr_ofst != UNDEF_REG)
0461         irq_reg_writel(gc, d->mask, stm32_bank->fpr_ofst);
0462 
0463     irq_gc_unlock(gc);
0464 }
0465 
0466 /* directly set the target bit without reading first. */
0467 static inline void stm32_exti_write_bit(struct irq_data *d, u32 reg)
0468 {
0469     struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
0470     void __iomem *base = chip_data->host_data->base;
0471     u32 val = BIT(d->hwirq % IRQS_PER_BANK);
0472 
0473     writel_relaxed(val, base + reg);
0474 }
0475 
0476 static inline u32 stm32_exti_set_bit(struct irq_data *d, u32 reg)
0477 {
0478     struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
0479     void __iomem *base = chip_data->host_data->base;
0480     u32 val;
0481 
0482     val = readl_relaxed(base + reg);
0483     val |= BIT(d->hwirq % IRQS_PER_BANK);
0484     writel_relaxed(val, base + reg);
0485 
0486     return val;
0487 }
0488 
0489 static inline u32 stm32_exti_clr_bit(struct irq_data *d, u32 reg)
0490 {
0491     struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
0492     void __iomem *base = chip_data->host_data->base;
0493     u32 val;
0494 
0495     val = readl_relaxed(base + reg);
0496     val &= ~BIT(d->hwirq % IRQS_PER_BANK);
0497     writel_relaxed(val, base + reg);
0498 
0499     return val;
0500 }
0501 
0502 static void stm32_exti_h_eoi(struct irq_data *d)
0503 {
0504     struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
0505     const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
0506 
0507     raw_spin_lock(&chip_data->rlock);
0508 
0509     stm32_exti_write_bit(d, stm32_bank->rpr_ofst);
0510     if (stm32_bank->fpr_ofst != UNDEF_REG)
0511         stm32_exti_write_bit(d, stm32_bank->fpr_ofst);
0512 
0513     raw_spin_unlock(&chip_data->rlock);
0514 
0515     if (d->parent_data->chip)
0516         irq_chip_eoi_parent(d);
0517 }
0518 
0519 static void stm32_exti_h_mask(struct irq_data *d)
0520 {
0521     struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
0522     const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
0523 
0524     raw_spin_lock(&chip_data->rlock);
0525     chip_data->mask_cache = stm32_exti_clr_bit(d, stm32_bank->imr_ofst);
0526     raw_spin_unlock(&chip_data->rlock);
0527 
0528     if (d->parent_data->chip)
0529         irq_chip_mask_parent(d);
0530 }
0531 
0532 static void stm32_exti_h_unmask(struct irq_data *d)
0533 {
0534     struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
0535     const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
0536 
0537     raw_spin_lock(&chip_data->rlock);
0538     chip_data->mask_cache = stm32_exti_set_bit(d, stm32_bank->imr_ofst);
0539     raw_spin_unlock(&chip_data->rlock);
0540 
0541     if (d->parent_data->chip)
0542         irq_chip_unmask_parent(d);
0543 }
0544 
0545 static int stm32_exti_h_set_type(struct irq_data *d, unsigned int type)
0546 {
0547     struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
0548     const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
0549     struct hwspinlock *hwlock = chip_data->host_data->hwlock;
0550     void __iomem *base = chip_data->host_data->base;
0551     u32 rtsr, ftsr;
0552     int err;
0553 
0554     raw_spin_lock(&chip_data->rlock);
0555 
0556     if (hwlock) {
0557         err = hwspin_lock_timeout_in_atomic(hwlock, HWSPNLCK_TIMEOUT);
0558         if (err) {
0559             pr_err("%s can't get hwspinlock (%d)\n", __func__, err);
0560             goto unlock;
0561         }
0562     }
0563 
0564     rtsr = readl_relaxed(base + stm32_bank->rtsr_ofst);
0565     ftsr = readl_relaxed(base + stm32_bank->ftsr_ofst);
0566 
0567     err = stm32_exti_set_type(d, type, &rtsr, &ftsr);
0568     if (err)
0569         goto unspinlock;
0570 
0571     writel_relaxed(rtsr, base + stm32_bank->rtsr_ofst);
0572     writel_relaxed(ftsr, base + stm32_bank->ftsr_ofst);
0573 
0574 unspinlock:
0575     if (hwlock)
0576         hwspin_unlock_in_atomic(hwlock);
0577 unlock:
0578     raw_spin_unlock(&chip_data->rlock);
0579 
0580     return err;
0581 }
0582 
0583 static int stm32_exti_h_set_wake(struct irq_data *d, unsigned int on)
0584 {
0585     struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
0586     u32 mask = BIT(d->hwirq % IRQS_PER_BANK);
0587 
0588     raw_spin_lock(&chip_data->rlock);
0589 
0590     if (on)
0591         chip_data->wake_active |= mask;
0592     else
0593         chip_data->wake_active &= ~mask;
0594 
0595     raw_spin_unlock(&chip_data->rlock);
0596 
0597     return 0;
0598 }
0599 
0600 static int stm32_exti_h_set_affinity(struct irq_data *d,
0601                      const struct cpumask *dest, bool force)
0602 {
0603     if (d->parent_data->chip)
0604         return irq_chip_set_affinity_parent(d, dest, force);
0605 
0606     return IRQ_SET_MASK_OK_DONE;
0607 }
0608 
0609 static int __maybe_unused stm32_exti_h_suspend(void)
0610 {
0611     struct stm32_exti_chip_data *chip_data;
0612     int i;
0613 
0614     for (i = 0; i < stm32_host_data->drv_data->bank_nr; i++) {
0615         chip_data = &stm32_host_data->chips_data[i];
0616         raw_spin_lock(&chip_data->rlock);
0617         stm32_chip_suspend(chip_data, chip_data->wake_active);
0618         raw_spin_unlock(&chip_data->rlock);
0619     }
0620 
0621     return 0;
0622 }
0623 
0624 static void __maybe_unused stm32_exti_h_resume(void)
0625 {
0626     struct stm32_exti_chip_data *chip_data;
0627     int i;
0628 
0629     for (i = 0; i < stm32_host_data->drv_data->bank_nr; i++) {
0630         chip_data = &stm32_host_data->chips_data[i];
0631         raw_spin_lock(&chip_data->rlock);
0632         stm32_chip_resume(chip_data, chip_data->mask_cache);
0633         raw_spin_unlock(&chip_data->rlock);
0634     }
0635 }
0636 
0637 static struct syscore_ops stm32_exti_h_syscore_ops = {
0638 #ifdef CONFIG_PM_SLEEP
0639     .suspend    = stm32_exti_h_suspend,
0640     .resume     = stm32_exti_h_resume,
0641 #endif
0642 };
0643 
0644 static void stm32_exti_h_syscore_init(struct stm32_exti_host_data *host_data)
0645 {
0646     stm32_host_data = host_data;
0647     register_syscore_ops(&stm32_exti_h_syscore_ops);
0648 }
0649 
0650 static void stm32_exti_h_syscore_deinit(void)
0651 {
0652     unregister_syscore_ops(&stm32_exti_h_syscore_ops);
0653 }
0654 
0655 static int stm32_exti_h_retrigger(struct irq_data *d)
0656 {
0657     struct stm32_exti_chip_data *chip_data = irq_data_get_irq_chip_data(d);
0658     const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
0659     void __iomem *base = chip_data->host_data->base;
0660     u32 mask = BIT(d->hwirq % IRQS_PER_BANK);
0661 
0662     writel_relaxed(mask, base + stm32_bank->swier_ofst);
0663 
0664     return 0;
0665 }
0666 
0667 static struct irq_chip stm32_exti_h_chip = {
0668     .name           = "stm32-exti-h",
0669     .irq_eoi        = stm32_exti_h_eoi,
0670     .irq_mask       = stm32_exti_h_mask,
0671     .irq_unmask     = stm32_exti_h_unmask,
0672     .irq_retrigger      = stm32_exti_h_retrigger,
0673     .irq_set_type       = stm32_exti_h_set_type,
0674     .irq_set_wake       = stm32_exti_h_set_wake,
0675     .flags          = IRQCHIP_MASK_ON_SUSPEND,
0676     .irq_set_affinity   = IS_ENABLED(CONFIG_SMP) ? stm32_exti_h_set_affinity : NULL,
0677 };
0678 
0679 static struct irq_chip stm32_exti_h_chip_direct = {
0680     .name           = "stm32-exti-h-direct",
0681     .irq_eoi        = irq_chip_eoi_parent,
0682     .irq_ack        = irq_chip_ack_parent,
0683     .irq_mask       = stm32_exti_h_mask,
0684     .irq_unmask     = stm32_exti_h_unmask,
0685     .irq_retrigger      = irq_chip_retrigger_hierarchy,
0686     .irq_set_type       = irq_chip_set_type_parent,
0687     .irq_set_wake       = stm32_exti_h_set_wake,
0688     .flags          = IRQCHIP_MASK_ON_SUSPEND,
0689     .irq_set_affinity   = IS_ENABLED(CONFIG_SMP) ? irq_chip_set_affinity_parent : NULL,
0690 };
0691 
0692 static int stm32_exti_h_domain_alloc(struct irq_domain *dm,
0693                      unsigned int virq,
0694                      unsigned int nr_irqs, void *data)
0695 {
0696     struct stm32_exti_host_data *host_data = dm->host_data;
0697     struct stm32_exti_chip_data *chip_data;
0698     u8 desc_irq;
0699     struct irq_fwspec *fwspec = data;
0700     struct irq_fwspec p_fwspec;
0701     irq_hw_number_t hwirq;
0702     int bank;
0703     u32 event_trg;
0704     struct irq_chip *chip;
0705 
0706     hwirq = fwspec->param[0];
0707     if (hwirq >= host_data->drv_data->bank_nr * IRQS_PER_BANK)
0708         return -EINVAL;
0709 
0710     bank  = hwirq / IRQS_PER_BANK;
0711     chip_data = &host_data->chips_data[bank];
0712 
0713     event_trg = readl_relaxed(host_data->base + chip_data->reg_bank->trg_ofst);
0714     chip = (event_trg & BIT(hwirq % IRQS_PER_BANK)) ?
0715            &stm32_exti_h_chip : &stm32_exti_h_chip_direct;
0716 
0717     irq_domain_set_hwirq_and_chip(dm, virq, hwirq, chip, chip_data);
0718 
0719     if (!host_data->drv_data->desc_irqs)
0720         return -EINVAL;
0721 
0722     desc_irq = host_data->drv_data->desc_irqs[hwirq];
0723     if (desc_irq != EXTI_INVALID_IRQ) {
0724         p_fwspec.fwnode = dm->parent->fwnode;
0725         p_fwspec.param_count = 3;
0726         p_fwspec.param[0] = GIC_SPI;
0727         p_fwspec.param[1] = desc_irq;
0728         p_fwspec.param[2] = IRQ_TYPE_LEVEL_HIGH;
0729 
0730         return irq_domain_alloc_irqs_parent(dm, virq, 1, &p_fwspec);
0731     }
0732 
0733     return 0;
0734 }
0735 
0736 static struct
0737 stm32_exti_host_data *stm32_exti_host_init(const struct stm32_exti_drv_data *dd,
0738                        struct device_node *node)
0739 {
0740     struct stm32_exti_host_data *host_data;
0741 
0742     host_data = kzalloc(sizeof(*host_data), GFP_KERNEL);
0743     if (!host_data)
0744         return NULL;
0745 
0746     host_data->drv_data = dd;
0747     host_data->chips_data = kcalloc(dd->bank_nr,
0748                     sizeof(struct stm32_exti_chip_data),
0749                     GFP_KERNEL);
0750     if (!host_data->chips_data)
0751         goto free_host_data;
0752 
0753     host_data->base = of_iomap(node, 0);
0754     if (!host_data->base) {
0755         pr_err("%pOF: Unable to map registers\n", node);
0756         goto free_chips_data;
0757     }
0758 
0759     stm32_host_data = host_data;
0760 
0761     return host_data;
0762 
0763 free_chips_data:
0764     kfree(host_data->chips_data);
0765 free_host_data:
0766     kfree(host_data);
0767 
0768     return NULL;
0769 }
0770 
0771 static struct
0772 stm32_exti_chip_data *stm32_exti_chip_init(struct stm32_exti_host_data *h_data,
0773                        u32 bank_idx,
0774                        struct device_node *node)
0775 {
0776     const struct stm32_exti_bank *stm32_bank;
0777     struct stm32_exti_chip_data *chip_data;
0778     void __iomem *base = h_data->base;
0779 
0780     stm32_bank = h_data->drv_data->exti_banks[bank_idx];
0781     chip_data = &h_data->chips_data[bank_idx];
0782     chip_data->host_data = h_data;
0783     chip_data->reg_bank = stm32_bank;
0784 
0785     raw_spin_lock_init(&chip_data->rlock);
0786 
0787     /*
0788      * This IP has no reset, so after hot reboot we should
0789      * clear registers to avoid residue
0790      */
0791     writel_relaxed(0, base + stm32_bank->imr_ofst);
0792     if (stm32_bank->emr_ofst != UNDEF_REG)
0793         writel_relaxed(0, base + stm32_bank->emr_ofst);
0794 
0795     pr_info("%pOF: bank%d\n", node, bank_idx);
0796 
0797     return chip_data;
0798 }
0799 
0800 static int __init stm32_exti_init(const struct stm32_exti_drv_data *drv_data,
0801                   struct device_node *node)
0802 {
0803     struct stm32_exti_host_data *host_data;
0804     unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
0805     int nr_irqs, ret, i;
0806     struct irq_chip_generic *gc;
0807     struct irq_domain *domain;
0808 
0809     host_data = stm32_exti_host_init(drv_data, node);
0810     if (!host_data)
0811         return -ENOMEM;
0812 
0813     domain = irq_domain_add_linear(node, drv_data->bank_nr * IRQS_PER_BANK,
0814                        &irq_exti_domain_ops, NULL);
0815     if (!domain) {
0816         pr_err("%pOFn: Could not register interrupt domain.\n",
0817                node);
0818         ret = -ENOMEM;
0819         goto out_unmap;
0820     }
0821 
0822     ret = irq_alloc_domain_generic_chips(domain, IRQS_PER_BANK, 1, "exti",
0823                          handle_edge_irq, clr, 0, 0);
0824     if (ret) {
0825         pr_err("%pOF: Could not allocate generic interrupt chip.\n",
0826                node);
0827         goto out_free_domain;
0828     }
0829 
0830     for (i = 0; i < drv_data->bank_nr; i++) {
0831         const struct stm32_exti_bank *stm32_bank;
0832         struct stm32_exti_chip_data *chip_data;
0833 
0834         stm32_bank = drv_data->exti_banks[i];
0835         chip_data = stm32_exti_chip_init(host_data, i, node);
0836 
0837         gc = irq_get_domain_generic_chip(domain, i * IRQS_PER_BANK);
0838 
0839         gc->reg_base = host_data->base;
0840         gc->chip_types->type = IRQ_TYPE_EDGE_BOTH;
0841         gc->chip_types->chip.irq_ack = stm32_irq_ack;
0842         gc->chip_types->chip.irq_mask = irq_gc_mask_clr_bit;
0843         gc->chip_types->chip.irq_unmask = irq_gc_mask_set_bit;
0844         gc->chip_types->chip.irq_set_type = stm32_irq_set_type;
0845         gc->chip_types->chip.irq_set_wake = irq_gc_set_wake;
0846         gc->suspend = stm32_irq_suspend;
0847         gc->resume = stm32_irq_resume;
0848         gc->wake_enabled = IRQ_MSK(IRQS_PER_BANK);
0849 
0850         gc->chip_types->regs.mask = stm32_bank->imr_ofst;
0851         gc->private = (void *)chip_data;
0852     }
0853 
0854     nr_irqs = of_irq_count(node);
0855     for (i = 0; i < nr_irqs; i++) {
0856         unsigned int irq = irq_of_parse_and_map(node, i);
0857 
0858         irq_set_handler_data(irq, domain);
0859         irq_set_chained_handler(irq, stm32_irq_handler);
0860     }
0861 
0862     return 0;
0863 
0864 out_free_domain:
0865     irq_domain_remove(domain);
0866 out_unmap:
0867     iounmap(host_data->base);
0868     kfree(host_data->chips_data);
0869     kfree(host_data);
0870     return ret;
0871 }
0872 
0873 static const struct irq_domain_ops stm32_exti_h_domain_ops = {
0874     .alloc  = stm32_exti_h_domain_alloc,
0875     .free   = irq_domain_free_irqs_common,
0876     .xlate = irq_domain_xlate_twocell,
0877 };
0878 
0879 static void stm32_exti_remove_irq(void *data)
0880 {
0881     struct irq_domain *domain = data;
0882 
0883     irq_domain_remove(domain);
0884 }
0885 
0886 static int stm32_exti_remove(struct platform_device *pdev)
0887 {
0888     stm32_exti_h_syscore_deinit();
0889     return 0;
0890 }
0891 
0892 static int stm32_exti_probe(struct platform_device *pdev)
0893 {
0894     int ret, i;
0895     struct device *dev = &pdev->dev;
0896     struct device_node *np = dev->of_node;
0897     struct irq_domain *parent_domain, *domain;
0898     struct stm32_exti_host_data *host_data;
0899     const struct stm32_exti_drv_data *drv_data;
0900 
0901     host_data = devm_kzalloc(dev, sizeof(*host_data), GFP_KERNEL);
0902     if (!host_data)
0903         return -ENOMEM;
0904 
0905     /* check for optional hwspinlock which may be not available yet */
0906     ret = of_hwspin_lock_get_id(np, 0);
0907     if (ret == -EPROBE_DEFER)
0908         /* hwspinlock framework not yet ready */
0909         return ret;
0910 
0911     if (ret >= 0) {
0912         host_data->hwlock = devm_hwspin_lock_request_specific(dev, ret);
0913         if (!host_data->hwlock) {
0914             dev_err(dev, "Failed to request hwspinlock\n");
0915             return -EINVAL;
0916         }
0917     } else if (ret != -ENOENT) {
0918         /* note: ENOENT is a valid case (means 'no hwspinlock') */
0919         dev_err(dev, "Failed to get hwspinlock\n");
0920         return ret;
0921     }
0922 
0923     /* initialize host_data */
0924     drv_data = of_device_get_match_data(dev);
0925     if (!drv_data) {
0926         dev_err(dev, "no of match data\n");
0927         return -ENODEV;
0928     }
0929     host_data->drv_data = drv_data;
0930 
0931     host_data->chips_data = devm_kcalloc(dev, drv_data->bank_nr,
0932                          sizeof(*host_data->chips_data),
0933                          GFP_KERNEL);
0934     if (!host_data->chips_data)
0935         return -ENOMEM;
0936 
0937     host_data->base = devm_platform_ioremap_resource(pdev, 0);
0938     if (IS_ERR(host_data->base))
0939         return PTR_ERR(host_data->base);
0940 
0941     for (i = 0; i < drv_data->bank_nr; i++)
0942         stm32_exti_chip_init(host_data, i, np);
0943 
0944     parent_domain = irq_find_host(of_irq_find_parent(np));
0945     if (!parent_domain) {
0946         dev_err(dev, "GIC interrupt-parent not found\n");
0947         return -EINVAL;
0948     }
0949 
0950     domain = irq_domain_add_hierarchy(parent_domain, 0,
0951                       drv_data->bank_nr * IRQS_PER_BANK,
0952                       np, &stm32_exti_h_domain_ops,
0953                       host_data);
0954 
0955     if (!domain) {
0956         dev_err(dev, "Could not register exti domain\n");
0957         return -ENOMEM;
0958     }
0959 
0960     ret = devm_add_action_or_reset(dev, stm32_exti_remove_irq, domain);
0961     if (ret)
0962         return ret;
0963 
0964     stm32_exti_h_syscore_init(host_data);
0965 
0966     return 0;
0967 }
0968 
0969 /* platform driver only for MP1 */
0970 static const struct of_device_id stm32_exti_ids[] = {
0971     { .compatible = "st,stm32mp1-exti", .data = &stm32mp1_drv_data},
0972     { .compatible = "st,stm32mp13-exti", .data = &stm32mp13_drv_data},
0973     {},
0974 };
0975 MODULE_DEVICE_TABLE(of, stm32_exti_ids);
0976 
0977 static struct platform_driver stm32_exti_driver = {
0978     .probe      = stm32_exti_probe,
0979     .remove     = stm32_exti_remove,
0980     .driver     = {
0981         .name   = "stm32_exti",
0982         .of_match_table = stm32_exti_ids,
0983     },
0984 };
0985 
0986 static int __init stm32_exti_arch_init(void)
0987 {
0988     return platform_driver_register(&stm32_exti_driver);
0989 }
0990 
0991 static void __exit stm32_exti_arch_exit(void)
0992 {
0993     return platform_driver_unregister(&stm32_exti_driver);
0994 }
0995 
0996 arch_initcall(stm32_exti_arch_init);
0997 module_exit(stm32_exti_arch_exit);
0998 
0999 /* no platform driver for F4 and H7 */
1000 static int __init stm32f4_exti_of_init(struct device_node *np,
1001                        struct device_node *parent)
1002 {
1003     return stm32_exti_init(&stm32f4xx_drv_data, np);
1004 }
1005 
1006 IRQCHIP_DECLARE(stm32f4_exti, "st,stm32-exti", stm32f4_exti_of_init);
1007 
1008 static int __init stm32h7_exti_of_init(struct device_node *np,
1009                        struct device_node *parent)
1010 {
1011     return stm32_exti_init(&stm32h7xx_drv_data, np);
1012 }
1013 
1014 IRQCHIP_DECLARE(stm32h7_exti, "st,stm32h7-exti", stm32h7_exti_of_init);