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