irq-stm32-exti.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #define IRQS_PER_BANK 32
  17. struct stm32_exti_bank {
  18. u32 imr_ofst;
  19. u32 emr_ofst;
  20. u32 rtsr_ofst;
  21. u32 ftsr_ofst;
  22. u32 swier_ofst;
  23. u32 pr_ofst;
  24. };
  25. static const struct stm32_exti_bank stm32f4xx_exti_b1 = {
  26. .imr_ofst = 0x00,
  27. .emr_ofst = 0x04,
  28. .rtsr_ofst = 0x08,
  29. .ftsr_ofst = 0x0C,
  30. .swier_ofst = 0x10,
  31. .pr_ofst = 0x14,
  32. };
  33. static const struct stm32_exti_bank *stm32f4xx_exti_banks[] = {
  34. &stm32f4xx_exti_b1,
  35. };
  36. static const struct stm32_exti_bank stm32h7xx_exti_b1 = {
  37. .imr_ofst = 0x80,
  38. .emr_ofst = 0x84,
  39. .rtsr_ofst = 0x00,
  40. .ftsr_ofst = 0x04,
  41. .swier_ofst = 0x08,
  42. .pr_ofst = 0x88,
  43. };
  44. static const struct stm32_exti_bank stm32h7xx_exti_b2 = {
  45. .imr_ofst = 0x90,
  46. .emr_ofst = 0x94,
  47. .rtsr_ofst = 0x20,
  48. .ftsr_ofst = 0x24,
  49. .swier_ofst = 0x28,
  50. .pr_ofst = 0x98,
  51. };
  52. static const struct stm32_exti_bank stm32h7xx_exti_b3 = {
  53. .imr_ofst = 0xA0,
  54. .emr_ofst = 0xA4,
  55. .rtsr_ofst = 0x40,
  56. .ftsr_ofst = 0x44,
  57. .swier_ofst = 0x48,
  58. .pr_ofst = 0xA8,
  59. };
  60. static const struct stm32_exti_bank *stm32h7xx_exti_banks[] = {
  61. &stm32h7xx_exti_b1,
  62. &stm32h7xx_exti_b2,
  63. &stm32h7xx_exti_b3,
  64. };
  65. static unsigned long stm32_exti_pending(struct irq_chip_generic *gc)
  66. {
  67. const struct stm32_exti_bank *stm32_bank = gc->private;
  68. return irq_reg_readl(gc, stm32_bank->pr_ofst);
  69. }
  70. static void stm32_exti_irq_ack(struct irq_chip_generic *gc, u32 mask)
  71. {
  72. const struct stm32_exti_bank *stm32_bank = gc->private;
  73. irq_reg_writel(gc, mask, stm32_bank->pr_ofst);
  74. }
  75. static void stm32_irq_handler(struct irq_desc *desc)
  76. {
  77. struct irq_domain *domain = irq_desc_get_handler_data(desc);
  78. struct irq_chip *chip = irq_desc_get_chip(desc);
  79. unsigned int virq, nbanks = domain->gc->num_chips;
  80. struct irq_chip_generic *gc;
  81. const struct stm32_exti_bank *stm32_bank;
  82. unsigned long pending;
  83. int n, i, irq_base = 0;
  84. chained_irq_enter(chip, desc);
  85. for (i = 0; i < nbanks; i++, irq_base += IRQS_PER_BANK) {
  86. gc = irq_get_domain_generic_chip(domain, irq_base);
  87. stm32_bank = gc->private;
  88. while ((pending = stm32_exti_pending(gc))) {
  89. for_each_set_bit(n, &pending, IRQS_PER_BANK) {
  90. virq = irq_find_mapping(domain, irq_base + n);
  91. generic_handle_irq(virq);
  92. stm32_exti_irq_ack(gc, BIT(n));
  93. }
  94. }
  95. }
  96. chained_irq_exit(chip, desc);
  97. }
  98. static int stm32_irq_set_type(struct irq_data *data, unsigned int type)
  99. {
  100. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  101. const struct stm32_exti_bank *stm32_bank = gc->private;
  102. int pin = data->hwirq % IRQS_PER_BANK;
  103. u32 rtsr, ftsr;
  104. irq_gc_lock(gc);
  105. rtsr = irq_reg_readl(gc, stm32_bank->rtsr_ofst);
  106. ftsr = irq_reg_readl(gc, stm32_bank->ftsr_ofst);
  107. switch (type) {
  108. case IRQ_TYPE_EDGE_RISING:
  109. rtsr |= BIT(pin);
  110. ftsr &= ~BIT(pin);
  111. break;
  112. case IRQ_TYPE_EDGE_FALLING:
  113. rtsr &= ~BIT(pin);
  114. ftsr |= BIT(pin);
  115. break;
  116. case IRQ_TYPE_EDGE_BOTH:
  117. rtsr |= BIT(pin);
  118. ftsr |= BIT(pin);
  119. break;
  120. default:
  121. irq_gc_unlock(gc);
  122. return -EINVAL;
  123. }
  124. irq_reg_writel(gc, rtsr, stm32_bank->rtsr_ofst);
  125. irq_reg_writel(gc, ftsr, stm32_bank->ftsr_ofst);
  126. irq_gc_unlock(gc);
  127. return 0;
  128. }
  129. static int stm32_irq_set_wake(struct irq_data *data, unsigned int on)
  130. {
  131. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  132. const struct stm32_exti_bank *stm32_bank = gc->private;
  133. int pin = data->hwirq % IRQS_PER_BANK;
  134. u32 imr;
  135. irq_gc_lock(gc);
  136. imr = irq_reg_readl(gc, stm32_bank->imr_ofst);
  137. if (on)
  138. imr |= BIT(pin);
  139. else
  140. imr &= ~BIT(pin);
  141. irq_reg_writel(gc, imr, stm32_bank->imr_ofst);
  142. irq_gc_unlock(gc);
  143. return 0;
  144. }
  145. static int stm32_exti_alloc(struct irq_domain *d, unsigned int virq,
  146. unsigned int nr_irqs, void *data)
  147. {
  148. struct irq_chip_generic *gc;
  149. struct irq_fwspec *fwspec = data;
  150. irq_hw_number_t hwirq;
  151. hwirq = fwspec->param[0];
  152. gc = irq_get_domain_generic_chip(d, hwirq);
  153. irq_map_generic_chip(d, virq, hwirq);
  154. irq_domain_set_info(d, virq, hwirq, &gc->chip_types->chip, gc,
  155. handle_simple_irq, NULL, NULL);
  156. return 0;
  157. }
  158. static void stm32_exti_free(struct irq_domain *d, unsigned int virq,
  159. unsigned int nr_irqs)
  160. {
  161. struct irq_data *data = irq_domain_get_irq_data(d, virq);
  162. irq_domain_reset_irq_data(data);
  163. }
  164. struct irq_domain_ops irq_exti_domain_ops = {
  165. .map = irq_map_generic_chip,
  166. .xlate = irq_domain_xlate_onetwocell,
  167. .alloc = stm32_exti_alloc,
  168. .free = stm32_exti_free,
  169. };
  170. static int
  171. __init stm32_exti_init(const struct stm32_exti_bank **stm32_exti_banks,
  172. int bank_nr, struct device_node *node)
  173. {
  174. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  175. int nr_irqs, nr_exti, ret, i;
  176. struct irq_chip_generic *gc;
  177. struct irq_domain *domain;
  178. void *base;
  179. base = of_iomap(node, 0);
  180. if (!base) {
  181. pr_err("%pOF: Unable to map registers\n", node);
  182. return -ENOMEM;
  183. }
  184. domain = irq_domain_add_linear(node, bank_nr * IRQS_PER_BANK,
  185. &irq_exti_domain_ops, NULL);
  186. if (!domain) {
  187. pr_err("%s: Could not register interrupt domain.\n",
  188. node->name);
  189. ret = -ENOMEM;
  190. goto out_unmap;
  191. }
  192. ret = irq_alloc_domain_generic_chips(domain, IRQS_PER_BANK, 1, "exti",
  193. handle_edge_irq, clr, 0, 0);
  194. if (ret) {
  195. pr_err("%pOF: Could not allocate generic interrupt chip.\n",
  196. node);
  197. goto out_free_domain;
  198. }
  199. for (i = 0; i < bank_nr; i++) {
  200. const struct stm32_exti_bank *stm32_bank = stm32_exti_banks[i];
  201. u32 irqs_mask;
  202. gc = irq_get_domain_generic_chip(domain, i * IRQS_PER_BANK);
  203. gc->reg_base = base;
  204. gc->chip_types->type = IRQ_TYPE_EDGE_BOTH;
  205. gc->chip_types->chip.irq_ack = irq_gc_ack_set_bit;
  206. gc->chip_types->chip.irq_mask = irq_gc_mask_clr_bit;
  207. gc->chip_types->chip.irq_unmask = irq_gc_mask_set_bit;
  208. gc->chip_types->chip.irq_set_type = stm32_irq_set_type;
  209. gc->chip_types->chip.irq_set_wake = stm32_irq_set_wake;
  210. gc->chip_types->regs.ack = stm32_bank->pr_ofst;
  211. gc->chip_types->regs.mask = stm32_bank->imr_ofst;
  212. gc->private = (void *)stm32_bank;
  213. /* Determine number of irqs supported */
  214. writel_relaxed(~0UL, base + stm32_bank->rtsr_ofst);
  215. irqs_mask = readl_relaxed(base + stm32_bank->rtsr_ofst);
  216. nr_exti = fls(readl_relaxed(base + stm32_bank->rtsr_ofst));
  217. /*
  218. * This IP has no reset, so after hot reboot we should
  219. * clear registers to avoid residue
  220. */
  221. writel_relaxed(0, base + stm32_bank->imr_ofst);
  222. writel_relaxed(0, base + stm32_bank->emr_ofst);
  223. writel_relaxed(0, base + stm32_bank->rtsr_ofst);
  224. writel_relaxed(0, base + stm32_bank->ftsr_ofst);
  225. writel_relaxed(~0UL, base + stm32_bank->pr_ofst);
  226. pr_info("%s: bank%d, External IRQs available:%#x\n",
  227. node->full_name, i, irqs_mask);
  228. }
  229. nr_irqs = of_irq_count(node);
  230. for (i = 0; i < nr_irqs; i++) {
  231. unsigned int irq = irq_of_parse_and_map(node, i);
  232. irq_set_handler_data(irq, domain);
  233. irq_set_chained_handler(irq, stm32_irq_handler);
  234. }
  235. return 0;
  236. out_free_domain:
  237. irq_domain_remove(domain);
  238. out_unmap:
  239. iounmap(base);
  240. return ret;
  241. }
  242. static int __init stm32f4_exti_of_init(struct device_node *np,
  243. struct device_node *parent)
  244. {
  245. return stm32_exti_init(stm32f4xx_exti_banks,
  246. ARRAY_SIZE(stm32f4xx_exti_banks), np);
  247. }
  248. IRQCHIP_DECLARE(stm32f4_exti, "st,stm32-exti", stm32f4_exti_of_init);
  249. static int __init stm32h7_exti_of_init(struct device_node *np,
  250. struct device_node *parent)
  251. {
  252. return stm32_exti_init(stm32h7xx_exti_banks,
  253. ARRAY_SIZE(stm32h7xx_exti_banks), np);
  254. }
  255. IRQCHIP_DECLARE(stm32h7_exti, "st,stm32h7-exti", stm32h7_exti_of_init);