irq-pic32-evic.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cristian Birsan <[email protected]>
  4. * Joshua Henderson <[email protected]>
  5. * Copyright (C) 2016 Microchip Technology Inc. All rights reserved.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irqdomain.h>
  11. #include <linux/of_address.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/irqchip.h>
  15. #include <linux/irq.h>
  16. #include <asm/irq.h>
  17. #include <asm/traps.h>
  18. #include <asm/mach-pic32/pic32.h>
  19. #define REG_INTCON 0x0000
  20. #define REG_INTSTAT 0x0020
  21. #define REG_IFS_OFFSET 0x0040
  22. #define REG_IEC_OFFSET 0x00C0
  23. #define REG_IPC_OFFSET 0x0140
  24. #define REG_OFF_OFFSET 0x0540
  25. #define MAJPRI_MASK 0x07
  26. #define SUBPRI_MASK 0x03
  27. #define PRIORITY_MASK 0x1F
  28. #define PIC32_INT_PRI(pri, subpri) \
  29. ((((pri) & MAJPRI_MASK) << 2) | ((subpri) & SUBPRI_MASK))
  30. struct evic_chip_data {
  31. u32 irq_types[NR_IRQS];
  32. u32 ext_irqs[8];
  33. };
  34. static struct irq_domain *evic_irq_domain;
  35. static void __iomem *evic_base;
  36. asmlinkage void __weak plat_irq_dispatch(void)
  37. {
  38. unsigned int hwirq;
  39. hwirq = readl(evic_base + REG_INTSTAT) & 0xFF;
  40. do_domain_IRQ(evic_irq_domain, hwirq);
  41. }
  42. static struct evic_chip_data *irqd_to_priv(struct irq_data *data)
  43. {
  44. return (struct evic_chip_data *)data->domain->host_data;
  45. }
  46. static int pic32_set_ext_polarity(int bit, u32 type)
  47. {
  48. /*
  49. * External interrupts can be either edge rising or edge falling,
  50. * but not both.
  51. */
  52. switch (type) {
  53. case IRQ_TYPE_EDGE_RISING:
  54. writel(BIT(bit), evic_base + PIC32_SET(REG_INTCON));
  55. break;
  56. case IRQ_TYPE_EDGE_FALLING:
  57. writel(BIT(bit), evic_base + PIC32_CLR(REG_INTCON));
  58. break;
  59. default:
  60. return -EINVAL;
  61. }
  62. return 0;
  63. }
  64. static int pic32_set_type_edge(struct irq_data *data,
  65. unsigned int flow_type)
  66. {
  67. struct evic_chip_data *priv = irqd_to_priv(data);
  68. int ret;
  69. int i;
  70. if (!(flow_type & IRQ_TYPE_EDGE_BOTH))
  71. return -EBADR;
  72. /* set polarity for external interrupts only */
  73. for (i = 0; i < ARRAY_SIZE(priv->ext_irqs); i++) {
  74. if (priv->ext_irqs[i] == data->hwirq) {
  75. ret = pic32_set_ext_polarity(i, flow_type);
  76. if (ret)
  77. return ret;
  78. }
  79. }
  80. irqd_set_trigger_type(data, flow_type);
  81. return IRQ_SET_MASK_OK;
  82. }
  83. static void pic32_bind_evic_interrupt(int irq, int set)
  84. {
  85. writel(set, evic_base + REG_OFF_OFFSET + irq * 4);
  86. }
  87. static void pic32_set_irq_priority(int irq, int priority)
  88. {
  89. u32 reg, shift;
  90. reg = irq / 4;
  91. shift = (irq % 4) * 8;
  92. writel(PRIORITY_MASK << shift,
  93. evic_base + PIC32_CLR(REG_IPC_OFFSET + reg * 0x10));
  94. writel(priority << shift,
  95. evic_base + PIC32_SET(REG_IPC_OFFSET + reg * 0x10));
  96. }
  97. #define IRQ_REG_MASK(_hwirq, _reg, _mask) \
  98. do { \
  99. _reg = _hwirq / 32; \
  100. _mask = 1 << (_hwirq % 32); \
  101. } while (0)
  102. static int pic32_irq_domain_map(struct irq_domain *d, unsigned int virq,
  103. irq_hw_number_t hw)
  104. {
  105. struct evic_chip_data *priv = d->host_data;
  106. struct irq_data *data;
  107. int ret;
  108. u32 iecclr, ifsclr;
  109. u32 reg, mask;
  110. ret = irq_map_generic_chip(d, virq, hw);
  111. if (ret)
  112. return ret;
  113. /*
  114. * Piggyback on xlate function to move to an alternate chip as necessary
  115. * at time of mapping instead of allowing the flow handler/chip to be
  116. * changed later. This requires all interrupts to be configured through
  117. * DT.
  118. */
  119. if (priv->irq_types[hw] & IRQ_TYPE_SENSE_MASK) {
  120. data = irq_domain_get_irq_data(d, virq);
  121. irqd_set_trigger_type(data, priv->irq_types[hw]);
  122. irq_setup_alt_chip(data, priv->irq_types[hw]);
  123. }
  124. IRQ_REG_MASK(hw, reg, mask);
  125. iecclr = PIC32_CLR(REG_IEC_OFFSET + reg * 0x10);
  126. ifsclr = PIC32_CLR(REG_IFS_OFFSET + reg * 0x10);
  127. /* mask and clear flag */
  128. writel(mask, evic_base + iecclr);
  129. writel(mask, evic_base + ifsclr);
  130. /* default priority is required */
  131. pic32_set_irq_priority(hw, PIC32_INT_PRI(2, 0));
  132. return ret;
  133. }
  134. int pic32_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
  135. const u32 *intspec, unsigned int intsize,
  136. irq_hw_number_t *out_hwirq, unsigned int *out_type)
  137. {
  138. struct evic_chip_data *priv = d->host_data;
  139. if (WARN_ON(intsize < 2))
  140. return -EINVAL;
  141. if (WARN_ON(intspec[0] >= NR_IRQS))
  142. return -EINVAL;
  143. *out_hwirq = intspec[0];
  144. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  145. priv->irq_types[intspec[0]] = intspec[1] & IRQ_TYPE_SENSE_MASK;
  146. return 0;
  147. }
  148. static const struct irq_domain_ops pic32_irq_domain_ops = {
  149. .map = pic32_irq_domain_map,
  150. .xlate = pic32_irq_domain_xlate,
  151. };
  152. static void __init pic32_ext_irq_of_init(struct irq_domain *domain)
  153. {
  154. struct device_node *node = irq_domain_get_of_node(domain);
  155. struct evic_chip_data *priv = domain->host_data;
  156. struct property *prop;
  157. const __le32 *p;
  158. u32 hwirq;
  159. int i = 0;
  160. const char *pname = "microchip,external-irqs";
  161. of_property_for_each_u32(node, pname, prop, p, hwirq) {
  162. if (i >= ARRAY_SIZE(priv->ext_irqs)) {
  163. pr_warn("More than %d external irq, skip rest\n",
  164. ARRAY_SIZE(priv->ext_irqs));
  165. break;
  166. }
  167. priv->ext_irqs[i] = hwirq;
  168. i++;
  169. }
  170. }
  171. static int __init pic32_of_init(struct device_node *node,
  172. struct device_node *parent)
  173. {
  174. struct irq_chip_generic *gc;
  175. struct evic_chip_data *priv;
  176. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  177. int nchips, ret;
  178. int i;
  179. nchips = DIV_ROUND_UP(NR_IRQS, 32);
  180. evic_base = of_iomap(node, 0);
  181. if (!evic_base)
  182. return -ENOMEM;
  183. priv = kcalloc(nchips, sizeof(*priv), GFP_KERNEL);
  184. if (!priv) {
  185. ret = -ENOMEM;
  186. goto err_iounmap;
  187. }
  188. evic_irq_domain = irq_domain_add_linear(node, nchips * 32,
  189. &pic32_irq_domain_ops,
  190. priv);
  191. if (!evic_irq_domain) {
  192. ret = -ENOMEM;
  193. goto err_free_priv;
  194. }
  195. /*
  196. * The PIC32 EVIC has a linear list of irqs and the type of each
  197. * irq is determined by the hardware peripheral the EVIC is arbitrating.
  198. * These irq types are defined in the datasheet as "persistent" and
  199. * "non-persistent" which are mapped here to level and edge
  200. * respectively. To manage the different flow handler requirements of
  201. * each irq type, different chip_types are used.
  202. */
  203. ret = irq_alloc_domain_generic_chips(evic_irq_domain, 32, 2,
  204. "evic-level", handle_level_irq,
  205. clr, 0, 0);
  206. if (ret)
  207. goto err_domain_remove;
  208. board_bind_eic_interrupt = &pic32_bind_evic_interrupt;
  209. for (i = 0; i < nchips; i++) {
  210. u32 ifsclr = PIC32_CLR(REG_IFS_OFFSET + (i * 0x10));
  211. u32 iec = REG_IEC_OFFSET + (i * 0x10);
  212. gc = irq_get_domain_generic_chip(evic_irq_domain, i * 32);
  213. gc->reg_base = evic_base;
  214. gc->unused = 0;
  215. /*
  216. * Level/persistent interrupts have a special requirement that
  217. * the condition generating the interrupt be cleared before the
  218. * interrupt flag (ifs) can be cleared. chip.irq_eoi is used to
  219. * complete the interrupt with an ack.
  220. */
  221. gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
  222. gc->chip_types[0].handler = handle_fasteoi_irq;
  223. gc->chip_types[0].regs.ack = ifsclr;
  224. gc->chip_types[0].regs.mask = iec;
  225. gc->chip_types[0].chip.name = "evic-level";
  226. gc->chip_types[0].chip.irq_eoi = irq_gc_ack_set_bit;
  227. gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit;
  228. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit;
  229. gc->chip_types[0].chip.flags = IRQCHIP_SKIP_SET_WAKE;
  230. /* Edge interrupts */
  231. gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
  232. gc->chip_types[1].handler = handle_edge_irq;
  233. gc->chip_types[1].regs.ack = ifsclr;
  234. gc->chip_types[1].regs.mask = iec;
  235. gc->chip_types[1].chip.name = "evic-edge";
  236. gc->chip_types[1].chip.irq_ack = irq_gc_ack_set_bit;
  237. gc->chip_types[1].chip.irq_mask = irq_gc_mask_clr_bit;
  238. gc->chip_types[1].chip.irq_unmask = irq_gc_mask_set_bit;
  239. gc->chip_types[1].chip.irq_set_type = pic32_set_type_edge;
  240. gc->chip_types[1].chip.flags = IRQCHIP_SKIP_SET_WAKE;
  241. gc->private = &priv[i];
  242. }
  243. irq_set_default_host(evic_irq_domain);
  244. /*
  245. * External interrupts have software configurable edge polarity. These
  246. * interrupts are defined in DT allowing polarity to be configured only
  247. * for these interrupts when requested.
  248. */
  249. pic32_ext_irq_of_init(evic_irq_domain);
  250. return 0;
  251. err_domain_remove:
  252. irq_domain_remove(evic_irq_domain);
  253. err_free_priv:
  254. kfree(priv);
  255. err_iounmap:
  256. iounmap(evic_base);
  257. return ret;
  258. }
  259. IRQCHIP_DECLARE(pic32_evic, "microchip,pic32mzda-evic", pic32_of_init);