irq-stm32-exti.c 20 KB

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