irq-sun6i-r.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * The R_INTC in Allwinner A31 and newer SoCs manages several types of
  4. * interrupts, as shown below:
  5. *
  6. * NMI IRQ DIRECT IRQs MUXED IRQs
  7. * bit 0 bits 1-15^ bits 19-31
  8. *
  9. * +---------+ +---------+ +---------+ +---------+
  10. * | NMI Pad | | IRQ d | | IRQ m | | IRQ m+7 |
  11. * +---------+ +---------+ +---------+ +---------+
  12. * | | | | | | |
  13. * | | | | |......| |
  14. * +------V------+ +------------+ | | | +--V------V--+ |
  15. * | Invert/ | | Write 1 to | | | | | AND with | |
  16. * | Edge Detect | | PENDING[0] | | | | | MUX[m/8] | |
  17. * +-------------+ +------------+ | | | +------------+ |
  18. * | | | | | | |
  19. * +--V-------V--+ +--V--+ | +--V--+ | +--V--+
  20. * | Set Reset| | GIC | | | GIC | | | GIC |
  21. * | Latch | | SPI | | | SPI |... | ...| SPI |
  22. * +-------------+ | N+d | | | m | | | m+7 |
  23. * | | +-----+ | +-----+ | +-----+
  24. * | | | |
  25. * +-------V-+ +-V----------+ +---------V--+ +--------V--------+
  26. * | GIC SPI | | AND with | | AND with | | AND with |
  27. * | N (=32) | | ENABLE[0] | | ENABLE[d] | | ENABLE[19+m/8] |
  28. * +---------+ +------------+ +------------+ +-----------------+
  29. * | | |
  30. * +------V-----+ +------V-----+ +--------V--------+
  31. * | Read | | Read | | Read |
  32. * | PENDING[0] | | PENDING[d] | | PENDING[19+m/8] |
  33. * +------------+ +------------+ +-----------------+
  34. *
  35. * ^ bits 16-18 are direct IRQs for peripherals with banked interrupts, such as
  36. * the MSGBOX. These IRQs do not map to any GIC SPI.
  37. *
  38. * The H6 variant adds two more (banked) direct IRQs and implements the full
  39. * set of 128 mux bits. This requires a second set of top-level registers.
  40. */
  41. #include <linux/bitmap.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/irq.h>
  44. #include <linux/irqchip.h>
  45. #include <linux/irqdomain.h>
  46. #include <linux/of.h>
  47. #include <linux/of_address.h>
  48. #include <linux/of_irq.h>
  49. #include <linux/syscore_ops.h>
  50. #include <dt-bindings/interrupt-controller/arm-gic.h>
  51. #define SUN6I_NMI_CTRL (0x0c)
  52. #define SUN6I_IRQ_PENDING(n) (0x10 + 4 * (n))
  53. #define SUN6I_IRQ_ENABLE(n) (0x40 + 4 * (n))
  54. #define SUN6I_MUX_ENABLE(n) (0xc0 + 4 * (n))
  55. #define SUN6I_NMI_SRC_TYPE_LEVEL_LOW 0
  56. #define SUN6I_NMI_SRC_TYPE_EDGE_FALLING 1
  57. #define SUN6I_NMI_SRC_TYPE_LEVEL_HIGH 2
  58. #define SUN6I_NMI_SRC_TYPE_EDGE_RISING 3
  59. #define SUN6I_NMI_BIT BIT(0)
  60. #define SUN6I_NMI_NEEDS_ACK ((void *)1)
  61. #define SUN6I_NR_TOP_LEVEL_IRQS 64
  62. #define SUN6I_NR_DIRECT_IRQS 16
  63. #define SUN6I_NR_MUX_BITS 128
  64. struct sun6i_r_intc_variant {
  65. u32 first_mux_irq;
  66. u32 nr_mux_irqs;
  67. u32 mux_valid[BITS_TO_U32(SUN6I_NR_MUX_BITS)];
  68. };
  69. static void __iomem *base;
  70. static irq_hw_number_t nmi_hwirq;
  71. static DECLARE_BITMAP(wake_irq_enabled, SUN6I_NR_TOP_LEVEL_IRQS);
  72. static DECLARE_BITMAP(wake_mux_enabled, SUN6I_NR_MUX_BITS);
  73. static DECLARE_BITMAP(wake_mux_valid, SUN6I_NR_MUX_BITS);
  74. static void sun6i_r_intc_ack_nmi(void)
  75. {
  76. writel_relaxed(SUN6I_NMI_BIT, base + SUN6I_IRQ_PENDING(0));
  77. }
  78. static void sun6i_r_intc_nmi_ack(struct irq_data *data)
  79. {
  80. if (irqd_get_trigger_type(data) & IRQ_TYPE_EDGE_BOTH)
  81. sun6i_r_intc_ack_nmi();
  82. else
  83. data->chip_data = SUN6I_NMI_NEEDS_ACK;
  84. }
  85. static void sun6i_r_intc_nmi_eoi(struct irq_data *data)
  86. {
  87. /* For oneshot IRQs, delay the ack until the IRQ is unmasked. */
  88. if (data->chip_data == SUN6I_NMI_NEEDS_ACK && !irqd_irq_masked(data)) {
  89. data->chip_data = NULL;
  90. sun6i_r_intc_ack_nmi();
  91. }
  92. irq_chip_eoi_parent(data);
  93. }
  94. static void sun6i_r_intc_nmi_unmask(struct irq_data *data)
  95. {
  96. if (data->chip_data == SUN6I_NMI_NEEDS_ACK) {
  97. data->chip_data = NULL;
  98. sun6i_r_intc_ack_nmi();
  99. }
  100. irq_chip_unmask_parent(data);
  101. }
  102. static int sun6i_r_intc_nmi_set_type(struct irq_data *data, unsigned int type)
  103. {
  104. u32 nmi_src_type;
  105. switch (type) {
  106. case IRQ_TYPE_EDGE_RISING:
  107. nmi_src_type = SUN6I_NMI_SRC_TYPE_EDGE_RISING;
  108. break;
  109. case IRQ_TYPE_EDGE_FALLING:
  110. nmi_src_type = SUN6I_NMI_SRC_TYPE_EDGE_FALLING;
  111. break;
  112. case IRQ_TYPE_LEVEL_HIGH:
  113. nmi_src_type = SUN6I_NMI_SRC_TYPE_LEVEL_HIGH;
  114. break;
  115. case IRQ_TYPE_LEVEL_LOW:
  116. nmi_src_type = SUN6I_NMI_SRC_TYPE_LEVEL_LOW;
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. writel_relaxed(nmi_src_type, base + SUN6I_NMI_CTRL);
  122. /*
  123. * The "External NMI" GIC input connects to a latch inside R_INTC, not
  124. * directly to the pin. So the GIC trigger type does not depend on the
  125. * NMI pin trigger type.
  126. */
  127. return irq_chip_set_type_parent(data, IRQ_TYPE_LEVEL_HIGH);
  128. }
  129. static int sun6i_r_intc_nmi_set_irqchip_state(struct irq_data *data,
  130. enum irqchip_irq_state which,
  131. bool state)
  132. {
  133. if (which == IRQCHIP_STATE_PENDING && !state)
  134. sun6i_r_intc_ack_nmi();
  135. return irq_chip_set_parent_state(data, which, state);
  136. }
  137. static int sun6i_r_intc_irq_set_wake(struct irq_data *data, unsigned int on)
  138. {
  139. unsigned long offset_from_nmi = data->hwirq - nmi_hwirq;
  140. if (offset_from_nmi < SUN6I_NR_DIRECT_IRQS)
  141. assign_bit(offset_from_nmi, wake_irq_enabled, on);
  142. else if (test_bit(data->hwirq, wake_mux_valid))
  143. assign_bit(data->hwirq, wake_mux_enabled, on);
  144. else
  145. /* Not wakeup capable. */
  146. return -EPERM;
  147. return 0;
  148. }
  149. static struct irq_chip sun6i_r_intc_nmi_chip = {
  150. .name = "sun6i-r-intc",
  151. .irq_ack = sun6i_r_intc_nmi_ack,
  152. .irq_mask = irq_chip_mask_parent,
  153. .irq_unmask = sun6i_r_intc_nmi_unmask,
  154. .irq_eoi = sun6i_r_intc_nmi_eoi,
  155. .irq_set_affinity = irq_chip_set_affinity_parent,
  156. .irq_set_type = sun6i_r_intc_nmi_set_type,
  157. .irq_set_irqchip_state = sun6i_r_intc_nmi_set_irqchip_state,
  158. .irq_set_wake = sun6i_r_intc_irq_set_wake,
  159. .flags = IRQCHIP_SET_TYPE_MASKED,
  160. };
  161. static struct irq_chip sun6i_r_intc_wakeup_chip = {
  162. .name = "sun6i-r-intc",
  163. .irq_mask = irq_chip_mask_parent,
  164. .irq_unmask = irq_chip_unmask_parent,
  165. .irq_eoi = irq_chip_eoi_parent,
  166. .irq_set_affinity = irq_chip_set_affinity_parent,
  167. .irq_set_type = irq_chip_set_type_parent,
  168. .irq_set_wake = sun6i_r_intc_irq_set_wake,
  169. .flags = IRQCHIP_SET_TYPE_MASKED,
  170. };
  171. static int sun6i_r_intc_domain_translate(struct irq_domain *domain,
  172. struct irq_fwspec *fwspec,
  173. unsigned long *hwirq,
  174. unsigned int *type)
  175. {
  176. /* Accept the old two-cell binding for the NMI only. */
  177. if (fwspec->param_count == 2 && fwspec->param[0] == 0) {
  178. *hwirq = nmi_hwirq;
  179. *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
  180. return 0;
  181. }
  182. /* Otherwise this binding should match the GIC SPI binding. */
  183. if (fwspec->param_count < 3)
  184. return -EINVAL;
  185. if (fwspec->param[0] != GIC_SPI)
  186. return -EINVAL;
  187. *hwirq = fwspec->param[1];
  188. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  189. return 0;
  190. }
  191. static int sun6i_r_intc_domain_alloc(struct irq_domain *domain,
  192. unsigned int virq,
  193. unsigned int nr_irqs, void *arg)
  194. {
  195. struct irq_fwspec *fwspec = arg;
  196. struct irq_fwspec gic_fwspec;
  197. unsigned long hwirq;
  198. unsigned int type;
  199. int i, ret;
  200. ret = sun6i_r_intc_domain_translate(domain, fwspec, &hwirq, &type);
  201. if (ret)
  202. return ret;
  203. if (hwirq + nr_irqs > SUN6I_NR_MUX_BITS)
  204. return -EINVAL;
  205. /* Construct a GIC-compatible fwspec from this fwspec. */
  206. gic_fwspec = (struct irq_fwspec) {
  207. .fwnode = domain->parent->fwnode,
  208. .param_count = 3,
  209. .param = { GIC_SPI, hwirq, type },
  210. };
  211. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &gic_fwspec);
  212. if (ret)
  213. return ret;
  214. for (i = 0; i < nr_irqs; ++i, ++hwirq, ++virq) {
  215. if (hwirq == nmi_hwirq) {
  216. irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  217. &sun6i_r_intc_nmi_chip,
  218. NULL);
  219. irq_set_handler(virq, handle_fasteoi_ack_irq);
  220. } else {
  221. irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  222. &sun6i_r_intc_wakeup_chip,
  223. NULL);
  224. }
  225. }
  226. return 0;
  227. }
  228. static const struct irq_domain_ops sun6i_r_intc_domain_ops = {
  229. .translate = sun6i_r_intc_domain_translate,
  230. .alloc = sun6i_r_intc_domain_alloc,
  231. .free = irq_domain_free_irqs_common,
  232. };
  233. static int sun6i_r_intc_suspend(void)
  234. {
  235. u32 buf[BITS_TO_U32(max(SUN6I_NR_TOP_LEVEL_IRQS, SUN6I_NR_MUX_BITS))];
  236. int i;
  237. /* Wake IRQs are enabled during system sleep and shutdown. */
  238. bitmap_to_arr32(buf, wake_irq_enabled, SUN6I_NR_TOP_LEVEL_IRQS);
  239. for (i = 0; i < BITS_TO_U32(SUN6I_NR_TOP_LEVEL_IRQS); ++i)
  240. writel_relaxed(buf[i], base + SUN6I_IRQ_ENABLE(i));
  241. bitmap_to_arr32(buf, wake_mux_enabled, SUN6I_NR_MUX_BITS);
  242. for (i = 0; i < BITS_TO_U32(SUN6I_NR_MUX_BITS); ++i)
  243. writel_relaxed(buf[i], base + SUN6I_MUX_ENABLE(i));
  244. return 0;
  245. }
  246. static void sun6i_r_intc_resume(void)
  247. {
  248. int i;
  249. /* Only the NMI is relevant during normal operation. */
  250. writel_relaxed(SUN6I_NMI_BIT, base + SUN6I_IRQ_ENABLE(0));
  251. for (i = 1; i < BITS_TO_U32(SUN6I_NR_TOP_LEVEL_IRQS); ++i)
  252. writel_relaxed(0, base + SUN6I_IRQ_ENABLE(i));
  253. }
  254. static void sun6i_r_intc_shutdown(void)
  255. {
  256. sun6i_r_intc_suspend();
  257. }
  258. static struct syscore_ops sun6i_r_intc_syscore_ops = {
  259. .suspend = sun6i_r_intc_suspend,
  260. .resume = sun6i_r_intc_resume,
  261. .shutdown = sun6i_r_intc_shutdown,
  262. };
  263. static int __init sun6i_r_intc_init(struct device_node *node,
  264. struct device_node *parent,
  265. const struct sun6i_r_intc_variant *v)
  266. {
  267. struct irq_domain *domain, *parent_domain;
  268. struct of_phandle_args nmi_parent;
  269. int ret;
  270. /* Extract the NMI hwirq number from the OF node. */
  271. ret = of_irq_parse_one(node, 0, &nmi_parent);
  272. if (ret)
  273. return ret;
  274. if (nmi_parent.args_count < 3 ||
  275. nmi_parent.args[0] != GIC_SPI ||
  276. nmi_parent.args[2] != IRQ_TYPE_LEVEL_HIGH)
  277. return -EINVAL;
  278. nmi_hwirq = nmi_parent.args[1];
  279. bitmap_set(wake_irq_enabled, v->first_mux_irq, v->nr_mux_irqs);
  280. bitmap_from_arr32(wake_mux_valid, v->mux_valid, SUN6I_NR_MUX_BITS);
  281. parent_domain = irq_find_host(parent);
  282. if (!parent_domain) {
  283. pr_err("%pOF: Failed to obtain parent domain\n", node);
  284. return -ENXIO;
  285. }
  286. base = of_io_request_and_map(node, 0, NULL);
  287. if (IS_ERR(base)) {
  288. pr_err("%pOF: Failed to map MMIO region\n", node);
  289. return PTR_ERR(base);
  290. }
  291. domain = irq_domain_add_hierarchy(parent_domain, 0, 0, node,
  292. &sun6i_r_intc_domain_ops, NULL);
  293. if (!domain) {
  294. pr_err("%pOF: Failed to allocate domain\n", node);
  295. iounmap(base);
  296. return -ENOMEM;
  297. }
  298. register_syscore_ops(&sun6i_r_intc_syscore_ops);
  299. sun6i_r_intc_ack_nmi();
  300. sun6i_r_intc_resume();
  301. return 0;
  302. }
  303. static const struct sun6i_r_intc_variant sun6i_a31_r_intc_variant __initconst = {
  304. .first_mux_irq = 19,
  305. .nr_mux_irqs = 13,
  306. .mux_valid = { 0xffffffff, 0xfff80000, 0xffffffff, 0x0000000f },
  307. };
  308. static int __init sun6i_a31_r_intc_init(struct device_node *node,
  309. struct device_node *parent)
  310. {
  311. return sun6i_r_intc_init(node, parent, &sun6i_a31_r_intc_variant);
  312. }
  313. IRQCHIP_DECLARE(sun6i_a31_r_intc, "allwinner,sun6i-a31-r-intc", sun6i_a31_r_intc_init);
  314. static const struct sun6i_r_intc_variant sun50i_h6_r_intc_variant __initconst = {
  315. .first_mux_irq = 21,
  316. .nr_mux_irqs = 16,
  317. .mux_valid = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
  318. };
  319. static int __init sun50i_h6_r_intc_init(struct device_node *node,
  320. struct device_node *parent)
  321. {
  322. return sun6i_r_intc_init(node, parent, &sun50i_h6_r_intc_variant);
  323. }
  324. IRQCHIP_DECLARE(sun50i_h6_r_intc, "allwinner,sun50i-h6-r-intc", sun50i_h6_r_intc_init);