irq-ark.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * arkmicro interrupt controller driver
  3. *
  4. * Licensed under GPLv2 or later.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/list.h>
  11. #include <linux/smp.h>
  12. #include <linux/cpu.h>
  13. #include <linux/cpu_pm.h>
  14. #include <linux/cpumask.h>
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/slab.h>
  22. #include <linux/irqchip.h>
  23. #include <linux/irqchip/arm-gic.h>
  24. #include <linux/delay.h>
  25. #include <asm/irq.h>
  26. #include <asm/exception.h>
  27. #include "irq-gic-common.h"
  28. #define ARK_MAX_IRQS 32
  29. #define ICSET 0x00
  30. #define ICPEND 0x04
  31. #define ICMODE 0x08
  32. #define ICMASK 0x0c
  33. #define ICLEVEL 0x10
  34. #define IRQISPR 0x3c
  35. #define IRQISPC 0x40
  36. #define IVEC_ADDR 0x78
  37. struct ark_irq_data {
  38. void __iomem *base;
  39. struct irq_domain *domain;
  40. unsigned int nr_irqs;
  41. };
  42. static DEFINE_RAW_SPINLOCK(irq_controller_lock);
  43. /*
  44. * The GIC mapping of CPU interfaces does not necessarily match
  45. * the logical CPU numbering. Let's use a mapping as returned
  46. * by the GIC itself.
  47. */
  48. static struct ark_irq_data ark_data __read_mostly;
  49. static inline void __iomem *ark_base(struct irq_data *d)
  50. {
  51. struct ark_irq_data *ark_data = irq_data_get_irq_chip_data(d);
  52. return ark_data->base;
  53. }
  54. static inline unsigned int ark_irq(struct irq_data *d)
  55. {
  56. return d->hwirq;
  57. }
  58. static asmlinkage void __exception_irq_entry
  59. ark_intc_handle(struct pt_regs *regs)
  60. {
  61. u32 pnd;
  62. u32 offset;
  63. pnd = readl_relaxed(ark_data.base + ICPEND);
  64. if (!pnd)
  65. return;
  66. offset = readl_relaxed(ark_data.base + IVEC_ADDR);
  67. offset >>= 2;
  68. handle_domain_irq(ark_data.domain, offset, regs);
  69. }
  70. /*
  71. * Routines to acknowledge, disable and enable interrupts
  72. */
  73. static void ark_mask_irq(struct irq_data *d)
  74. {
  75. u32 mask = 1 << ark_irq(d);
  76. raw_spin_lock(&irq_controller_lock);
  77. writel_relaxed(readl_relaxed(ark_base(d) + ICMASK) | mask, ark_base(d) + ICMASK);
  78. raw_spin_unlock(&irq_controller_lock);
  79. }
  80. static void ark_unmask_irq(struct irq_data *d)
  81. {
  82. u32 mask = 1 << ark_irq(d);
  83. raw_spin_lock(&irq_controller_lock);
  84. writel_relaxed(readl_relaxed(ark_base(d) + ICMASK) & ~mask, ark_base(d) + ICMASK);
  85. raw_spin_unlock(&irq_controller_lock);
  86. }
  87. static void ark_ack_irq(struct irq_data *d)
  88. {
  89. u32 mask = 1 << ark_irq(d);
  90. raw_spin_lock(&irq_controller_lock);
  91. writel_relaxed(mask, ark_base(d) + IRQISPC);
  92. raw_spin_unlock(&irq_controller_lock);
  93. }
  94. static struct irq_chip ark_irq_chip = {
  95. .name = "ark-intc",
  96. .irq_mask = ark_mask_irq,
  97. .irq_unmask = ark_unmask_irq,
  98. .irq_ack = ark_ack_irq,
  99. };
  100. static void __init ark_irq_hw_init(struct ark_irq_data *intc)
  101. {
  102. writel_relaxed(0x08, intc->base + ICSET);
  103. udelay(10);
  104. writel_relaxed(0x05, intc->base + ICSET); /* enabel irq disable fiq */
  105. writel_relaxed(0x00, intc->base + ICMODE); /* 0--> irq type, 1 --> fiq type */
  106. writel_relaxed(0xffffffff, intc->base + ICMASK);
  107. writel_relaxed(0xffffffff, intc->base + ICLEVEL);
  108. writel_relaxed(0xffffffff, intc->base + IRQISPC);
  109. }
  110. static int ark_irq_domain_map(struct irq_domain *d, unsigned int irq,
  111. irq_hw_number_t hw)
  112. {
  113. irq_set_chip_and_handler(irq, &ark_irq_chip,
  114. handle_level_irq);
  115. irq_set_chip_data(irq, d->host_data);
  116. return 0;
  117. }
  118. static int ark_irq_domain_xlate(struct irq_domain *d,
  119. struct device_node *controller,
  120. const u32 *intspec, unsigned int intsize,
  121. unsigned long *out_hwirq,
  122. unsigned int *out_type)
  123. {
  124. unsigned long ret = 0;
  125. if (irq_domain_get_of_node(d) != controller)
  126. return -EINVAL;
  127. if (intsize < 1)
  128. return -EINVAL;
  129. *out_hwirq = intspec[0];
  130. *out_type = IRQ_TYPE_LEVEL_HIGH;
  131. return ret;
  132. }
  133. static const struct irq_domain_ops ark_irq_domain_ops = {
  134. .map = ark_irq_domain_map,
  135. .xlate = ark_irq_domain_xlate,
  136. };
  137. static int __init
  138. ark_irq_of_init(struct device_node *node, struct device_node *parent)
  139. {
  140. irq_hw_number_t hwirq_base = 0;
  141. int nr_irqs, irq_base;
  142. if (WARN_ON(!node))
  143. return -ENODEV;
  144. ark_data.base = of_iomap(node, 0);
  145. WARN(!ark_data.base, "fail to map ark intc dist registers\n");
  146. ark_data.nr_irqs = nr_irqs = ARK_MAX_IRQS;
  147. irq_base = irq_alloc_descs(-1, hwirq_base, nr_irqs, numa_node_id());
  148. if (irq_base < 0) {
  149. pr_err("failed to allocate IRQ numbers\n");
  150. return -EINVAL;
  151. }
  152. ark_data.domain = irq_domain_add_legacy(node, nr_irqs, irq_base,
  153. hwirq_base,
  154. &ark_irq_domain_ops,
  155. &ark_data);
  156. if (WARN_ON(!ark_data.domain))
  157. return -EINVAL;
  158. ark_irq_hw_init(&ark_data);
  159. set_handle_irq(ark_intc_handle);
  160. return 0;
  161. }
  162. IRQCHIP_DECLARE(ark_intc, "arkmicro,ark-intc", ark_irq_of_init);
  163. MODULE_AUTHOR("Sim");
  164. MODULE_DESCRIPTION("Arkmicro irq controller driver");
  165. MODULE_LICENSE("GPL v2");