irq-realtek-rtl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 Birger Koblitz <[email protected]>
  4. * Copyright (C) 2020 Bert Vermeulen <[email protected]>
  5. * Copyright (C) 2020 John Crispin <[email protected]>
  6. */
  7. #include <linux/of_irq.h>
  8. #include <linux/irqchip.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/of_address.h>
  11. #include <linux/irqchip/chained_irq.h>
  12. /* Global Interrupt Mask Register */
  13. #define RTL_ICTL_GIMR 0x00
  14. /* Global Interrupt Status Register */
  15. #define RTL_ICTL_GISR 0x04
  16. /* Interrupt Routing Registers */
  17. #define RTL_ICTL_IRR0 0x08
  18. #define RTL_ICTL_IRR1 0x0c
  19. #define RTL_ICTL_IRR2 0x10
  20. #define RTL_ICTL_IRR3 0x14
  21. #define RTL_ICTL_NUM_INPUTS 32
  22. #define REG(x) (realtek_ictl_base + x)
  23. static DEFINE_RAW_SPINLOCK(irq_lock);
  24. static void __iomem *realtek_ictl_base;
  25. /*
  26. * IRR0-IRR3 store 4 bits per interrupt, but Realtek uses inverted numbering,
  27. * placing IRQ 31 in the first four bits. A routing value of '0' means the
  28. * interrupt is left disconnected. Routing values {1..15} connect to output
  29. * lines {0..14}.
  30. */
  31. #define IRR_OFFSET(idx) (4 * (3 - (idx * 4) / 32))
  32. #define IRR_SHIFT(idx) ((idx * 4) % 32)
  33. static void write_irr(void __iomem *irr0, int idx, u32 value)
  34. {
  35. unsigned int offset = IRR_OFFSET(idx);
  36. unsigned int shift = IRR_SHIFT(idx);
  37. u32 irr;
  38. irr = readl(irr0 + offset) & ~(0xf << shift);
  39. irr |= (value & 0xf) << shift;
  40. writel(irr, irr0 + offset);
  41. }
  42. static void realtek_ictl_unmask_irq(struct irq_data *i)
  43. {
  44. unsigned long flags;
  45. u32 value;
  46. raw_spin_lock_irqsave(&irq_lock, flags);
  47. value = readl(REG(RTL_ICTL_GIMR));
  48. value |= BIT(i->hwirq);
  49. writel(value, REG(RTL_ICTL_GIMR));
  50. raw_spin_unlock_irqrestore(&irq_lock, flags);
  51. }
  52. static void realtek_ictl_mask_irq(struct irq_data *i)
  53. {
  54. unsigned long flags;
  55. u32 value;
  56. raw_spin_lock_irqsave(&irq_lock, flags);
  57. value = readl(REG(RTL_ICTL_GIMR));
  58. value &= ~BIT(i->hwirq);
  59. writel(value, REG(RTL_ICTL_GIMR));
  60. raw_spin_unlock_irqrestore(&irq_lock, flags);
  61. }
  62. static struct irq_chip realtek_ictl_irq = {
  63. .name = "realtek-rtl-intc",
  64. .irq_mask = realtek_ictl_mask_irq,
  65. .irq_unmask = realtek_ictl_unmask_irq,
  66. };
  67. static int intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  68. {
  69. unsigned long flags;
  70. irq_set_chip_and_handler(irq, &realtek_ictl_irq, handle_level_irq);
  71. raw_spin_lock_irqsave(&irq_lock, flags);
  72. write_irr(REG(RTL_ICTL_IRR0), hw, 1);
  73. raw_spin_unlock_irqrestore(&irq_lock, flags);
  74. return 0;
  75. }
  76. static const struct irq_domain_ops irq_domain_ops = {
  77. .map = intc_map,
  78. .xlate = irq_domain_xlate_onecell,
  79. };
  80. static void realtek_irq_dispatch(struct irq_desc *desc)
  81. {
  82. struct irq_chip *chip = irq_desc_get_chip(desc);
  83. struct irq_domain *domain;
  84. unsigned long pending;
  85. unsigned int soc_int;
  86. chained_irq_enter(chip, desc);
  87. pending = readl(REG(RTL_ICTL_GIMR)) & readl(REG(RTL_ICTL_GISR));
  88. if (unlikely(!pending)) {
  89. spurious_interrupt();
  90. goto out;
  91. }
  92. domain = irq_desc_get_handler_data(desc);
  93. for_each_set_bit(soc_int, &pending, 32)
  94. generic_handle_domain_irq(domain, soc_int);
  95. out:
  96. chained_irq_exit(chip, desc);
  97. }
  98. static int __init realtek_rtl_of_init(struct device_node *node, struct device_node *parent)
  99. {
  100. struct of_phandle_args oirq;
  101. struct irq_domain *domain;
  102. unsigned int soc_irq;
  103. int parent_irq;
  104. realtek_ictl_base = of_iomap(node, 0);
  105. if (!realtek_ictl_base)
  106. return -ENXIO;
  107. /* Disable all cascaded interrupts and clear routing */
  108. writel(0, REG(RTL_ICTL_GIMR));
  109. for (soc_irq = 0; soc_irq < RTL_ICTL_NUM_INPUTS; soc_irq++)
  110. write_irr(REG(RTL_ICTL_IRR0), soc_irq, 0);
  111. if (WARN_ON(!of_irq_count(node))) {
  112. /*
  113. * If DT contains no parent interrupts, assume MIPS CPU IRQ 2
  114. * (HW0) is connected to the first output. This is the case for
  115. * all known hardware anyway. "interrupt-map" is deprecated, so
  116. * don't bother trying to parse that.
  117. */
  118. oirq.np = of_find_compatible_node(NULL, NULL, "mti,cpu-interrupt-controller");
  119. oirq.args_count = 1;
  120. oirq.args[0] = 2;
  121. parent_irq = irq_create_of_mapping(&oirq);
  122. of_node_put(oirq.np);
  123. } else {
  124. parent_irq = of_irq_get(node, 0);
  125. }
  126. if (parent_irq < 0)
  127. return parent_irq;
  128. else if (!parent_irq)
  129. return -ENODEV;
  130. domain = irq_domain_add_linear(node, RTL_ICTL_NUM_INPUTS, &irq_domain_ops, NULL);
  131. if (!domain)
  132. return -ENOMEM;
  133. irq_set_chained_handler_and_data(parent_irq, realtek_irq_dispatch, domain);
  134. return 0;
  135. }
  136. IRQCHIP_DECLARE(realtek_rtl_intc, "realtek,rtl-intc", realtek_rtl_of_init);