irq-stm32-exti.c 25 KB

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