irq-sun4i.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Allwinner A1X SoCs IRQ chip driver.
  3. *
  4. * Copyright (C) 2012 Maxime Ripard
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * Based on code from
  9. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  10. * Benn Huang <benn@allwinnertech.com>
  11. *
  12. * This file is licensed under the terms of the GNU General Public
  13. * License version 2. This program is licensed "as is" without any
  14. * warranty of any kind, whether express or implied.
  15. */
  16. #include <linux/io.h>
  17. #include <linux/irq.h>
  18. #include <linux/irqchip.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <asm/exception.h>
  23. #include <asm/mach/irq.h>
  24. #define SUN4I_IRQ_VECTOR_REG 0x00
  25. #define SUN4I_IRQ_PROTECTION_REG 0x08
  26. #define SUN4I_IRQ_NMI_CTRL_REG 0x0c
  27. #define SUN4I_IRQ_PENDING_REG(x) (0x10 + 0x4 * x)
  28. #define SUN4I_IRQ_FIQ_PENDING_REG(x) (0x20 + 0x4 * x)
  29. #define SUN4I_IRQ_ENABLE_REG(x) (0x40 + 0x4 * x)
  30. #define SUN4I_IRQ_MASK_REG(x) (0x50 + 0x4 * x)
  31. static void __iomem *sun4i_irq_base;
  32. static struct irq_domain *sun4i_irq_domain;
  33. static void __exception_irq_entry sun4i_handle_irq(struct pt_regs *regs);
  34. static void sun4i_irq_ack(struct irq_data *irqd)
  35. {
  36. unsigned int irq = irqd_to_hwirq(irqd);
  37. if (irq != 0)
  38. return; /* Only IRQ 0 / the ENMI needs to be acked */
  39. writel(BIT(0), sun4i_irq_base + SUN4I_IRQ_PENDING_REG(0));
  40. }
  41. static void sun4i_irq_mask(struct irq_data *irqd)
  42. {
  43. unsigned int irq = irqd_to_hwirq(irqd);
  44. unsigned int irq_off = irq % 32;
  45. int reg = irq / 32;
  46. u32 val;
  47. val = readl(sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(reg));
  48. writel(val & ~(1 << irq_off),
  49. sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(reg));
  50. }
  51. static void sun4i_irq_unmask(struct irq_data *irqd)
  52. {
  53. unsigned int irq = irqd_to_hwirq(irqd);
  54. unsigned int irq_off = irq % 32;
  55. int reg = irq / 32;
  56. u32 val;
  57. val = readl(sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(reg));
  58. writel(val | (1 << irq_off),
  59. sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(reg));
  60. }
  61. static struct irq_chip sun4i_irq_chip = {
  62. .name = "sun4i_irq",
  63. .irq_eoi = sun4i_irq_ack,
  64. .irq_mask = sun4i_irq_mask,
  65. .irq_unmask = sun4i_irq_unmask,
  66. .flags = IRQCHIP_EOI_THREADED | IRQCHIP_EOI_IF_HANDLED,
  67. };
  68. static int sun4i_irq_map(struct irq_domain *d, unsigned int virq,
  69. irq_hw_number_t hw)
  70. {
  71. irq_set_chip_and_handler(virq, &sun4i_irq_chip, handle_fasteoi_irq);
  72. irq_set_probe(virq);
  73. return 0;
  74. }
  75. static const struct irq_domain_ops sun4i_irq_ops = {
  76. .map = sun4i_irq_map,
  77. .xlate = irq_domain_xlate_onecell,
  78. };
  79. static int __init sun4i_of_init(struct device_node *node,
  80. struct device_node *parent)
  81. {
  82. sun4i_irq_base = of_iomap(node, 0);
  83. if (!sun4i_irq_base)
  84. panic("%s: unable to map IC registers\n",
  85. node->full_name);
  86. /* Disable all interrupts */
  87. writel(0, sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(0));
  88. writel(0, sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(1));
  89. writel(0, sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(2));
  90. /* Unmask all the interrupts, ENABLE_REG(x) is used for masking */
  91. writel(0, sun4i_irq_base + SUN4I_IRQ_MASK_REG(0));
  92. writel(0, sun4i_irq_base + SUN4I_IRQ_MASK_REG(1));
  93. writel(0, sun4i_irq_base + SUN4I_IRQ_MASK_REG(2));
  94. /* Clear all the pending interrupts */
  95. writel(0xffffffff, sun4i_irq_base + SUN4I_IRQ_PENDING_REG(0));
  96. writel(0xffffffff, sun4i_irq_base + SUN4I_IRQ_PENDING_REG(1));
  97. writel(0xffffffff, sun4i_irq_base + SUN4I_IRQ_PENDING_REG(2));
  98. /* Enable protection mode */
  99. writel(0x01, sun4i_irq_base + SUN4I_IRQ_PROTECTION_REG);
  100. /* Configure the external interrupt source type */
  101. writel(0x00, sun4i_irq_base + SUN4I_IRQ_NMI_CTRL_REG);
  102. sun4i_irq_domain = irq_domain_add_linear(node, 3 * 32,
  103. &sun4i_irq_ops, NULL);
  104. if (!sun4i_irq_domain)
  105. panic("%s: unable to create IRQ domain\n", node->full_name);
  106. set_handle_irq(sun4i_handle_irq);
  107. return 0;
  108. }
  109. IRQCHIP_DECLARE(allwinner_sun4i_ic, "allwinner,sun4i-a10-ic", sun4i_of_init);
  110. static void __exception_irq_entry sun4i_handle_irq(struct pt_regs *regs)
  111. {
  112. u32 hwirq;
  113. /*
  114. * hwirq == 0 can mean one of 3 things:
  115. * 1) no more irqs pending
  116. * 2) irq 0 pending
  117. * 3) spurious irq
  118. * So if we immediately get a reading of 0, check the irq-pending reg
  119. * to differentiate between 2 and 3. We only do this once to avoid
  120. * the extra check in the common case of 1 hapening after having
  121. * read the vector-reg once.
  122. */
  123. hwirq = readl(sun4i_irq_base + SUN4I_IRQ_VECTOR_REG) >> 2;
  124. if (hwirq == 0 &&
  125. !(readl(sun4i_irq_base + SUN4I_IRQ_PENDING_REG(0)) & BIT(0)))
  126. return;
  127. do {
  128. handle_domain_irq(sun4i_irq_domain, hwirq, regs);
  129. hwirq = readl(sun4i_irq_base + SUN4I_IRQ_VECTOR_REG) >> 2;
  130. } while (hwirq != 0);
  131. }