irq-sunxi.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <linux/irqchip/sunxi.h>
  22. #define SUNXI_IRQ_VECTOR_REG 0x00
  23. #define SUNXI_IRQ_PROTECTION_REG 0x08
  24. #define SUNXI_IRQ_NMI_CTRL_REG 0x0c
  25. #define SUNXI_IRQ_PENDING_REG(x) (0x10 + 0x4 * x)
  26. #define SUNXI_IRQ_FIQ_PENDING_REG(x) (0x20 + 0x4 * x)
  27. #define SUNXI_IRQ_ENABLE_REG(x) (0x40 + 0x4 * x)
  28. #define SUNXI_IRQ_MASK_REG(x) (0x50 + 0x4 * x)
  29. static void __iomem *sunxi_irq_base;
  30. static struct irq_domain *sunxi_irq_domain;
  31. void sunxi_irq_ack(struct irq_data *irqd)
  32. {
  33. unsigned int irq = irqd_to_hwirq(irqd);
  34. unsigned int irq_off = irq % 32;
  35. int reg = irq / 32;
  36. u32 val;
  37. val = readl(sunxi_irq_base + SUNXI_IRQ_PENDING_REG(reg));
  38. writel(val | (1 << irq_off),
  39. sunxi_irq_base + SUNXI_IRQ_PENDING_REG(reg));
  40. }
  41. static void sunxi_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(sunxi_irq_base + SUNXI_IRQ_ENABLE_REG(reg));
  48. writel(val & ~(1 << irq_off),
  49. sunxi_irq_base + SUNXI_IRQ_ENABLE_REG(reg));
  50. }
  51. static void sunxi_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(sunxi_irq_base + SUNXI_IRQ_ENABLE_REG(reg));
  58. writel(val | (1 << irq_off),
  59. sunxi_irq_base + SUNXI_IRQ_ENABLE_REG(reg));
  60. }
  61. static struct irq_chip sunxi_irq_chip = {
  62. .name = "sunxi_irq",
  63. .irq_ack = sunxi_irq_ack,
  64. .irq_mask = sunxi_irq_mask,
  65. .irq_unmask = sunxi_irq_unmask,
  66. };
  67. static int sunxi_irq_map(struct irq_domain *d, unsigned int virq,
  68. irq_hw_number_t hw)
  69. {
  70. irq_set_chip_and_handler(virq, &sunxi_irq_chip,
  71. handle_level_irq);
  72. set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
  73. return 0;
  74. }
  75. static struct irq_domain_ops sunxi_irq_ops = {
  76. .map = sunxi_irq_map,
  77. .xlate = irq_domain_xlate_onecell,
  78. };
  79. static int __init sunxi_of_init(struct device_node *node,
  80. struct device_node *parent)
  81. {
  82. sunxi_irq_base = of_iomap(node, 0);
  83. if (!sunxi_irq_base)
  84. panic("%s: unable to map IC registers\n",
  85. node->full_name);
  86. /* Disable all interrupts */
  87. writel(0, sunxi_irq_base + SUNXI_IRQ_ENABLE_REG(0));
  88. writel(0, sunxi_irq_base + SUNXI_IRQ_ENABLE_REG(1));
  89. writel(0, sunxi_irq_base + SUNXI_IRQ_ENABLE_REG(2));
  90. /* Mask all the interrupts */
  91. writel(0, sunxi_irq_base + SUNXI_IRQ_MASK_REG(0));
  92. writel(0, sunxi_irq_base + SUNXI_IRQ_MASK_REG(1));
  93. writel(0, sunxi_irq_base + SUNXI_IRQ_MASK_REG(2));
  94. /* Clear all the pending interrupts */
  95. writel(0xffffffff, sunxi_irq_base + SUNXI_IRQ_PENDING_REG(0));
  96. writel(0xffffffff, sunxi_irq_base + SUNXI_IRQ_PENDING_REG(1));
  97. writel(0xffffffff, sunxi_irq_base + SUNXI_IRQ_PENDING_REG(2));
  98. /* Enable protection mode */
  99. writel(0x01, sunxi_irq_base + SUNXI_IRQ_PROTECTION_REG);
  100. /* Configure the external interrupt source type */
  101. writel(0x00, sunxi_irq_base + SUNXI_IRQ_NMI_CTRL_REG);
  102. sunxi_irq_domain = irq_domain_add_linear(node, 3 * 32,
  103. &sunxi_irq_ops, NULL);
  104. if (!sunxi_irq_domain)
  105. panic("%s: unable to create IRQ domain\n", node->full_name);
  106. return 0;
  107. }
  108. static struct of_device_id sunxi_irq_dt_ids[] __initconst = {
  109. { .compatible = "allwinner,sunxi-ic", .data = sunxi_of_init },
  110. { }
  111. };
  112. void __init sunxi_init_irq(void)
  113. {
  114. of_irq_init(sunxi_irq_dt_ids);
  115. }
  116. asmlinkage void __exception_irq_entry sunxi_handle_irq(struct pt_regs *regs)
  117. {
  118. u32 irq, hwirq;
  119. hwirq = readl(sunxi_irq_base + SUNXI_IRQ_VECTOR_REG) >> 2;
  120. while (hwirq != 0) {
  121. irq = irq_find_mapping(sunxi_irq_domain, hwirq);
  122. handle_IRQ(irq, regs);
  123. hwirq = readl(sunxi_irq_base + SUNXI_IRQ_VECTOR_REG) >> 2;
  124. }
  125. }