006-5.12-irqchip-add-support-for-realtek-rtl838x-rtl839x-interrupt-controller.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. From 9f3a0f34b84ad1b9a8f2bdae44b66f16685b2143 Mon Sep 17 00:00:00 2001
  2. From: Bert Vermeulen <[email protected]>
  3. Date: Fri, 22 Jan 2021 21:42:24 +0100
  4. Subject: irqchip: Add support for Realtek RTL838x/RTL839x interrupt controller
  5. This is a standard IRQ driver with only status and mask registers.
  6. The mapping from SoC interrupts (18-31) to MIPS core interrupts is
  7. done via an interrupt-map in device tree.
  8. Signed-off-by: Bert Vermeulen <[email protected]>
  9. Signed-off-by: Birger Koblitz <[email protected]>
  10. Acked-by: John Crispin <[email protected]>
  11. Signed-off-by: Marc Zyngier <[email protected]>
  12. Link: https://lore.kernel.org/r/[email protected]
  13. ---
  14. drivers/irqchip/Makefile | 1 +
  15. drivers/irqchip/irq-realtek-rtl.c | 180 ++++++++++++++++++++++++++++++++++++++
  16. 2 files changed, 181 insertions(+)
  17. create mode 100644 drivers/irqchip/irq-realtek-rtl.c
  18. --- a/drivers/irqchip/Makefile
  19. +++ b/drivers/irqchip/Makefile
  20. @@ -114,3 +114,4 @@ obj-$(CONFIG_LOONGSON_PCH_PIC) += irq-l
  21. obj-$(CONFIG_LOONGSON_PCH_MSI) += irq-loongson-pch-msi.o
  22. obj-$(CONFIG_MST_IRQ) += irq-mst-intc.o
  23. obj-$(CONFIG_SL28CPLD_INTC) += irq-sl28cpld.o
  24. +obj-$(CONFIG_MACH_REALTEK_RTL) += irq-realtek-rtl.o
  25. --- /dev/null
  26. +++ b/drivers/irqchip/irq-realtek-rtl.c
  27. @@ -0,0 +1,180 @@
  28. +// SPDX-License-Identifier: GPL-2.0-only
  29. +/*
  30. + * Copyright (C) 2020 Birger Koblitz <[email protected]>
  31. + * Copyright (C) 2020 Bert Vermeulen <[email protected]>
  32. + * Copyright (C) 2020 John Crispin <[email protected]>
  33. + */
  34. +
  35. +#include <linux/of_irq.h>
  36. +#include <linux/irqchip.h>
  37. +#include <linux/spinlock.h>
  38. +#include <linux/of_address.h>
  39. +#include <linux/irqchip/chained_irq.h>
  40. +
  41. +/* Global Interrupt Mask Register */
  42. +#define RTL_ICTL_GIMR 0x00
  43. +/* Global Interrupt Status Register */
  44. +#define RTL_ICTL_GISR 0x04
  45. +/* Interrupt Routing Registers */
  46. +#define RTL_ICTL_IRR0 0x08
  47. +#define RTL_ICTL_IRR1 0x0c
  48. +#define RTL_ICTL_IRR2 0x10
  49. +#define RTL_ICTL_IRR3 0x14
  50. +
  51. +#define REG(x) (realtek_ictl_base + x)
  52. +
  53. +static DEFINE_RAW_SPINLOCK(irq_lock);
  54. +static void __iomem *realtek_ictl_base;
  55. +
  56. +static void realtek_ictl_unmask_irq(struct irq_data *i)
  57. +{
  58. + unsigned long flags;
  59. + u32 value;
  60. +
  61. + raw_spin_lock_irqsave(&irq_lock, flags);
  62. +
  63. + value = readl(REG(RTL_ICTL_GIMR));
  64. + value |= BIT(i->hwirq);
  65. + writel(value, REG(RTL_ICTL_GIMR));
  66. +
  67. + raw_spin_unlock_irqrestore(&irq_lock, flags);
  68. +}
  69. +
  70. +static void realtek_ictl_mask_irq(struct irq_data *i)
  71. +{
  72. + unsigned long flags;
  73. + u32 value;
  74. +
  75. + raw_spin_lock_irqsave(&irq_lock, flags);
  76. +
  77. + value = readl(REG(RTL_ICTL_GIMR));
  78. + value &= ~BIT(i->hwirq);
  79. + writel(value, REG(RTL_ICTL_GIMR));
  80. +
  81. + raw_spin_unlock_irqrestore(&irq_lock, flags);
  82. +}
  83. +
  84. +static struct irq_chip realtek_ictl_irq = {
  85. + .name = "realtek-rtl-intc",
  86. + .irq_mask = realtek_ictl_mask_irq,
  87. + .irq_unmask = realtek_ictl_unmask_irq,
  88. +};
  89. +
  90. +static int intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  91. +{
  92. + irq_set_chip_and_handler(hw, &realtek_ictl_irq, handle_level_irq);
  93. +
  94. + return 0;
  95. +}
  96. +
  97. +static const struct irq_domain_ops irq_domain_ops = {
  98. + .map = intc_map,
  99. + .xlate = irq_domain_xlate_onecell,
  100. +};
  101. +
  102. +static void realtek_irq_dispatch(struct irq_desc *desc)
  103. +{
  104. + struct irq_chip *chip = irq_desc_get_chip(desc);
  105. + struct irq_domain *domain;
  106. + unsigned int pending;
  107. +
  108. + chained_irq_enter(chip, desc);
  109. + pending = readl(REG(RTL_ICTL_GIMR)) & readl(REG(RTL_ICTL_GISR));
  110. + if (unlikely(!pending)) {
  111. + spurious_interrupt();
  112. + goto out;
  113. + }
  114. + domain = irq_desc_get_handler_data(desc);
  115. + generic_handle_irq(irq_find_mapping(domain, __ffs(pending)));
  116. +
  117. +out:
  118. + chained_irq_exit(chip, desc);
  119. +}
  120. +
  121. +/*
  122. + * SoC interrupts are cascaded to MIPS CPU interrupts according to the
  123. + * interrupt-map in the device tree. Each SoC interrupt gets 4 bits for
  124. + * the CPU interrupt in an Interrupt Routing Register. Max 32 SoC interrupts
  125. + * thus go into 4 IRRs.
  126. + */
  127. +static int __init map_interrupts(struct device_node *node, struct irq_domain *domain)
  128. +{
  129. + struct device_node *cpu_ictl;
  130. + const __be32 *imap;
  131. + u32 imaplen, soc_int, cpu_int, tmp, regs[4];
  132. + int ret, i, irr_regs[] = {
  133. + RTL_ICTL_IRR3,
  134. + RTL_ICTL_IRR2,
  135. + RTL_ICTL_IRR1,
  136. + RTL_ICTL_IRR0,
  137. + };
  138. + u8 mips_irqs_set;
  139. +
  140. + ret = of_property_read_u32(node, "#address-cells", &tmp);
  141. + if (ret || tmp)
  142. + return -EINVAL;
  143. +
  144. + imap = of_get_property(node, "interrupt-map", &imaplen);
  145. + if (!imap || imaplen % 3)
  146. + return -EINVAL;
  147. +
  148. + mips_irqs_set = 0;
  149. + memset(regs, 0, sizeof(regs));
  150. + for (i = 0; i < imaplen; i += 3 * sizeof(u32)) {
  151. + soc_int = be32_to_cpup(imap);
  152. + if (soc_int > 31)
  153. + return -EINVAL;
  154. +
  155. + cpu_ictl = of_find_node_by_phandle(be32_to_cpup(imap + 1));
  156. + if (!cpu_ictl)
  157. + return -EINVAL;
  158. + ret = of_property_read_u32(cpu_ictl, "#interrupt-cells", &tmp);
  159. + if (ret || tmp != 1)
  160. + return -EINVAL;
  161. + of_node_put(cpu_ictl);
  162. +
  163. + cpu_int = be32_to_cpup(imap + 2);
  164. + if (cpu_int > 7)
  165. + return -EINVAL;
  166. +
  167. + if (!(mips_irqs_set & BIT(cpu_int))) {
  168. + irq_set_chained_handler_and_data(cpu_int, realtek_irq_dispatch,
  169. + domain);
  170. + mips_irqs_set |= BIT(cpu_int);
  171. + }
  172. +
  173. + regs[(soc_int * 4) / 32] |= cpu_int << (soc_int * 4) % 32;
  174. + imap += 3;
  175. + }
  176. +
  177. + for (i = 0; i < 4; i++)
  178. + writel(regs[i], REG(irr_regs[i]));
  179. +
  180. + return 0;
  181. +}
  182. +
  183. +static int __init realtek_rtl_of_init(struct device_node *node, struct device_node *parent)
  184. +{
  185. + struct irq_domain *domain;
  186. + int ret;
  187. +
  188. + realtek_ictl_base = of_iomap(node, 0);
  189. + if (!realtek_ictl_base)
  190. + return -ENXIO;
  191. +
  192. + /* Disable all cascaded interrupts */
  193. + writel(0, REG(RTL_ICTL_GIMR));
  194. +
  195. + domain = irq_domain_add_simple(node, 32, 0,
  196. + &irq_domain_ops, NULL);
  197. +
  198. + ret = map_interrupts(node, domain);
  199. + if (ret) {
  200. + pr_err("invalid interrupt map\n");
  201. + return ret;
  202. + }
  203. +
  204. + return 0;
  205. +}
  206. +
  207. +IRQCHIP_DECLARE(realtek_rtl_intc, "realtek,rtl-intc", realtek_rtl_of_init);