irq-sun4i.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <asm/exception.h>
  22. #include <asm/mach/irq.h>
  23. #include "irqchip.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 asmlinkage void __exception_irq_entry sun4i_handle_irq(struct pt_regs *regs);
  34. void sun4i_irq_ack(struct irq_data *irqd)
  35. {
  36. unsigned int irq = irqd_to_hwirq(irqd);
  37. unsigned int irq_off = irq % 32;
  38. int reg = irq / 32;
  39. u32 val;
  40. val = readl(sun4i_irq_base + SUN4I_IRQ_PENDING_REG(reg));
  41. writel(val | (1 << irq_off),
  42. sun4i_irq_base + SUN4I_IRQ_PENDING_REG(reg));
  43. }
  44. static void sun4i_irq_mask(struct irq_data *irqd)
  45. {
  46. unsigned int irq = irqd_to_hwirq(irqd);
  47. unsigned int irq_off = irq % 32;
  48. int reg = irq / 32;
  49. u32 val;
  50. val = readl(sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(reg));
  51. writel(val & ~(1 << irq_off),
  52. sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(reg));
  53. }
  54. static void sun4i_irq_unmask(struct irq_data *irqd)
  55. {
  56. unsigned int irq = irqd_to_hwirq(irqd);
  57. unsigned int irq_off = irq % 32;
  58. int reg = irq / 32;
  59. u32 val;
  60. val = readl(sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(reg));
  61. writel(val | (1 << irq_off),
  62. sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(reg));
  63. }
  64. static struct irq_chip sun4i_irq_chip = {
  65. .name = "sun4i_irq",
  66. .irq_ack = sun4i_irq_ack,
  67. .irq_mask = sun4i_irq_mask,
  68. .irq_unmask = sun4i_irq_unmask,
  69. };
  70. static int sun4i_irq_map(struct irq_domain *d, unsigned int virq,
  71. irq_hw_number_t hw)
  72. {
  73. irq_set_chip_and_handler(virq, &sun4i_irq_chip,
  74. handle_level_irq);
  75. set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
  76. return 0;
  77. }
  78. static struct irq_domain_ops sun4i_irq_ops = {
  79. .map = sun4i_irq_map,
  80. .xlate = irq_domain_xlate_onecell,
  81. };
  82. static int __init sun4i_of_init(struct device_node *node,
  83. struct device_node *parent)
  84. {
  85. sun4i_irq_base = of_iomap(node, 0);
  86. if (!sun4i_irq_base)
  87. panic("%s: unable to map IC registers\n",
  88. node->full_name);
  89. /* Disable all interrupts */
  90. writel(0, sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(0));
  91. writel(0, sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(1));
  92. writel(0, sun4i_irq_base + SUN4I_IRQ_ENABLE_REG(2));
  93. /* Mask all the interrupts */
  94. writel(0, sun4i_irq_base + SUN4I_IRQ_MASK_REG(0));
  95. writel(0, sun4i_irq_base + SUN4I_IRQ_MASK_REG(1));
  96. writel(0, sun4i_irq_base + SUN4I_IRQ_MASK_REG(2));
  97. /* Clear all the pending interrupts */
  98. writel(0xffffffff, sun4i_irq_base + SUN4I_IRQ_PENDING_REG(0));
  99. writel(0xffffffff, sun4i_irq_base + SUN4I_IRQ_PENDING_REG(1));
  100. writel(0xffffffff, sun4i_irq_base + SUN4I_IRQ_PENDING_REG(2));
  101. /* Enable protection mode */
  102. writel(0x01, sun4i_irq_base + SUN4I_IRQ_PROTECTION_REG);
  103. /* Configure the external interrupt source type */
  104. writel(0x00, sun4i_irq_base + SUN4I_IRQ_NMI_CTRL_REG);
  105. sun4i_irq_domain = irq_domain_add_linear(node, 3 * 32,
  106. &sun4i_irq_ops, NULL);
  107. if (!sun4i_irq_domain)
  108. panic("%s: unable to create IRQ domain\n", node->full_name);
  109. set_handle_irq(sun4i_handle_irq);
  110. return 0;
  111. }
  112. IRQCHIP_DECLARE(allwinner_sun4i_ic, "allwinner,sun4i-ic", sun4i_of_init);
  113. static asmlinkage void __exception_irq_entry sun4i_handle_irq(struct pt_regs *regs)
  114. {
  115. u32 irq, hwirq;
  116. hwirq = readl(sun4i_irq_base + SUN4I_IRQ_VECTOR_REG) >> 2;
  117. while (hwirq != 0) {
  118. irq = irq_find_mapping(sun4i_irq_domain, hwirq);
  119. handle_IRQ(irq, regs);
  120. hwirq = readl(sun4i_irq_base + SUN4I_IRQ_VECTOR_REG) >> 2;
  121. }
  122. }