irq-mtk-cirq.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2016 MediaTek Inc.
  3. * Author: Youlin.Pei <youlin.pei@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqchip.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/of.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_address.h>
  22. #include <linux/slab.h>
  23. #include <linux/syscore_ops.h>
  24. #define CIRQ_ACK 0x40
  25. #define CIRQ_MASK_SET 0xc0
  26. #define CIRQ_MASK_CLR 0x100
  27. #define CIRQ_SENS_SET 0x180
  28. #define CIRQ_SENS_CLR 0x1c0
  29. #define CIRQ_POL_SET 0x240
  30. #define CIRQ_POL_CLR 0x280
  31. #define CIRQ_CONTROL 0x300
  32. #define CIRQ_EN 0x1
  33. #define CIRQ_EDGE 0x2
  34. #define CIRQ_FLUSH 0x4
  35. struct mtk_cirq_chip_data {
  36. void __iomem *base;
  37. unsigned int ext_irq_start;
  38. unsigned int ext_irq_end;
  39. struct irq_domain *domain;
  40. };
  41. static struct mtk_cirq_chip_data *cirq_data;
  42. static void mtk_cirq_write_mask(struct irq_data *data, unsigned int offset)
  43. {
  44. struct mtk_cirq_chip_data *chip_data = data->chip_data;
  45. unsigned int cirq_num = data->hwirq;
  46. u32 mask = 1 << (cirq_num % 32);
  47. writel_relaxed(mask, chip_data->base + offset + (cirq_num / 32) * 4);
  48. }
  49. static void mtk_cirq_mask(struct irq_data *data)
  50. {
  51. mtk_cirq_write_mask(data, CIRQ_MASK_SET);
  52. irq_chip_mask_parent(data);
  53. }
  54. static void mtk_cirq_unmask(struct irq_data *data)
  55. {
  56. mtk_cirq_write_mask(data, CIRQ_MASK_CLR);
  57. irq_chip_unmask_parent(data);
  58. }
  59. static int mtk_cirq_set_type(struct irq_data *data, unsigned int type)
  60. {
  61. int ret;
  62. switch (type & IRQ_TYPE_SENSE_MASK) {
  63. case IRQ_TYPE_EDGE_FALLING:
  64. mtk_cirq_write_mask(data, CIRQ_POL_CLR);
  65. mtk_cirq_write_mask(data, CIRQ_SENS_CLR);
  66. break;
  67. case IRQ_TYPE_EDGE_RISING:
  68. mtk_cirq_write_mask(data, CIRQ_POL_SET);
  69. mtk_cirq_write_mask(data, CIRQ_SENS_CLR);
  70. break;
  71. case IRQ_TYPE_LEVEL_LOW:
  72. mtk_cirq_write_mask(data, CIRQ_POL_CLR);
  73. mtk_cirq_write_mask(data, CIRQ_SENS_SET);
  74. break;
  75. case IRQ_TYPE_LEVEL_HIGH:
  76. mtk_cirq_write_mask(data, CIRQ_POL_SET);
  77. mtk_cirq_write_mask(data, CIRQ_SENS_SET);
  78. break;
  79. default:
  80. break;
  81. }
  82. data = data->parent_data;
  83. ret = data->chip->irq_set_type(data, type);
  84. return ret;
  85. }
  86. static struct irq_chip mtk_cirq_chip = {
  87. .name = "MT_CIRQ",
  88. .irq_mask = mtk_cirq_mask,
  89. .irq_unmask = mtk_cirq_unmask,
  90. .irq_eoi = irq_chip_eoi_parent,
  91. .irq_set_type = mtk_cirq_set_type,
  92. .irq_retrigger = irq_chip_retrigger_hierarchy,
  93. #ifdef CONFIG_SMP
  94. .irq_set_affinity = irq_chip_set_affinity_parent,
  95. #endif
  96. };
  97. static int mtk_cirq_domain_translate(struct irq_domain *d,
  98. struct irq_fwspec *fwspec,
  99. unsigned long *hwirq,
  100. unsigned int *type)
  101. {
  102. if (is_of_node(fwspec->fwnode)) {
  103. if (fwspec->param_count != 3)
  104. return -EINVAL;
  105. /* No PPI should point to this domain */
  106. if (fwspec->param[0] != 0)
  107. return -EINVAL;
  108. /* cirq support irq number check */
  109. if (fwspec->param[1] < cirq_data->ext_irq_start ||
  110. fwspec->param[1] > cirq_data->ext_irq_end)
  111. return -EINVAL;
  112. *hwirq = fwspec->param[1] - cirq_data->ext_irq_start;
  113. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  114. return 0;
  115. }
  116. return -EINVAL;
  117. }
  118. static int mtk_cirq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  119. unsigned int nr_irqs, void *arg)
  120. {
  121. int ret;
  122. irq_hw_number_t hwirq;
  123. unsigned int type;
  124. struct irq_fwspec *fwspec = arg;
  125. struct irq_fwspec parent_fwspec = *fwspec;
  126. ret = mtk_cirq_domain_translate(domain, fwspec, &hwirq, &type);
  127. if (ret)
  128. return ret;
  129. if (WARN_ON(nr_irqs != 1))
  130. return -EINVAL;
  131. irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  132. &mtk_cirq_chip,
  133. domain->host_data);
  134. parent_fwspec.fwnode = domain->parent->fwnode;
  135. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
  136. &parent_fwspec);
  137. }
  138. static const struct irq_domain_ops cirq_domain_ops = {
  139. .translate = mtk_cirq_domain_translate,
  140. .alloc = mtk_cirq_domain_alloc,
  141. .free = irq_domain_free_irqs_common,
  142. };
  143. #ifdef CONFIG_PM_SLEEP
  144. static int mtk_cirq_suspend(void)
  145. {
  146. u32 value, mask;
  147. unsigned int irq, hwirq_num;
  148. bool pending, masked;
  149. int i, pendret, maskret;
  150. /*
  151. * When external interrupts happened, CIRQ will record the status
  152. * even CIRQ is not enabled. When execute flush command, CIRQ will
  153. * resend the signals according to the status. So if don't clear the
  154. * status, CIRQ will resend the wrong signals.
  155. *
  156. * arch_suspend_disable_irqs() will be called before CIRQ suspend
  157. * callback. If clear all the status simply, the external interrupts
  158. * which happened between arch_suspend_disable_irqs and CIRQ suspend
  159. * callback will be lost. Using following steps to avoid this issue;
  160. *
  161. * - Iterate over all the CIRQ supported interrupts;
  162. * - For each interrupt, inspect its pending and masked status at GIC
  163. * level;
  164. * - If pending and unmasked, it happened between
  165. * arch_suspend_disable_irqs and CIRQ suspend callback, don't ACK
  166. * it. Otherwise, ACK it.
  167. */
  168. hwirq_num = cirq_data->ext_irq_end - cirq_data->ext_irq_start + 1;
  169. for (i = 0; i < hwirq_num; i++) {
  170. irq = irq_find_mapping(cirq_data->domain, i);
  171. if (irq) {
  172. pendret = irq_get_irqchip_state(irq,
  173. IRQCHIP_STATE_PENDING,
  174. &pending);
  175. maskret = irq_get_irqchip_state(irq,
  176. IRQCHIP_STATE_MASKED,
  177. &masked);
  178. if (pendret == 0 && maskret == 0 &&
  179. (pending && !masked))
  180. continue;
  181. }
  182. mask = 1 << (i % 32);
  183. writel_relaxed(mask, cirq_data->base + CIRQ_ACK + (i / 32) * 4);
  184. }
  185. /* set edge_only mode, record edge-triggerd interrupts */
  186. /* enable cirq */
  187. value = readl_relaxed(cirq_data->base + CIRQ_CONTROL);
  188. value |= (CIRQ_EDGE | CIRQ_EN);
  189. writel_relaxed(value, cirq_data->base + CIRQ_CONTROL);
  190. return 0;
  191. }
  192. static void mtk_cirq_resume(void)
  193. {
  194. u32 value;
  195. /* flush recored interrupts, will send signals to parent controller */
  196. value = readl_relaxed(cirq_data->base + CIRQ_CONTROL);
  197. writel_relaxed(value | CIRQ_FLUSH, cirq_data->base + CIRQ_CONTROL);
  198. /* disable cirq */
  199. value = readl_relaxed(cirq_data->base + CIRQ_CONTROL);
  200. value &= ~(CIRQ_EDGE | CIRQ_EN);
  201. writel_relaxed(value, cirq_data->base + CIRQ_CONTROL);
  202. }
  203. static struct syscore_ops mtk_cirq_syscore_ops = {
  204. .suspend = mtk_cirq_suspend,
  205. .resume = mtk_cirq_resume,
  206. };
  207. static void mtk_cirq_syscore_init(void)
  208. {
  209. register_syscore_ops(&mtk_cirq_syscore_ops);
  210. }
  211. #else
  212. static inline void mtk_cirq_syscore_init(void) {}
  213. #endif
  214. static int __init mtk_cirq_of_init(struct device_node *node,
  215. struct device_node *parent)
  216. {
  217. struct irq_domain *domain, *domain_parent;
  218. unsigned int irq_num;
  219. int ret;
  220. domain_parent = irq_find_host(parent);
  221. if (!domain_parent) {
  222. pr_err("mtk_cirq: interrupt-parent not found\n");
  223. return -EINVAL;
  224. }
  225. cirq_data = kzalloc(sizeof(*cirq_data), GFP_KERNEL);
  226. if (!cirq_data)
  227. return -ENOMEM;
  228. cirq_data->base = of_iomap(node, 0);
  229. if (!cirq_data->base) {
  230. pr_err("mtk_cirq: unable to map cirq register\n");
  231. ret = -ENXIO;
  232. goto out_free;
  233. }
  234. ret = of_property_read_u32_index(node, "mediatek,ext-irq-range", 0,
  235. &cirq_data->ext_irq_start);
  236. if (ret)
  237. goto out_unmap;
  238. ret = of_property_read_u32_index(node, "mediatek,ext-irq-range", 1,
  239. &cirq_data->ext_irq_end);
  240. if (ret)
  241. goto out_unmap;
  242. irq_num = cirq_data->ext_irq_end - cirq_data->ext_irq_start + 1;
  243. domain = irq_domain_add_hierarchy(domain_parent, 0,
  244. irq_num, node,
  245. &cirq_domain_ops, cirq_data);
  246. if (!domain) {
  247. ret = -ENOMEM;
  248. goto out_unmap;
  249. }
  250. cirq_data->domain = domain;
  251. mtk_cirq_syscore_init();
  252. return 0;
  253. out_unmap:
  254. iounmap(cirq_data->base);
  255. out_free:
  256. kfree(cirq_data);
  257. return ret;
  258. }
  259. IRQCHIP_DECLARE(mtk_cirq, "mediatek,mtk-cirq", mtk_cirq_of_init);