irq.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (C) 2011 Google, Inc.
  3. *
  4. * Author:
  5. * Colin Cross <ccross@android.com>
  6. *
  7. * Copyright (C) 2010,2013, NVIDIA Corporation
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/io.h>
  23. #include <linux/of.h>
  24. #include <linux/irqchip/arm-gic.h>
  25. #include <linux/syscore_ops.h>
  26. #include "board.h"
  27. #include "iomap.h"
  28. #define ICTLR_CPU_IEP_VFIQ 0x08
  29. #define ICTLR_CPU_IEP_FIR 0x14
  30. #define ICTLR_CPU_IEP_FIR_SET 0x18
  31. #define ICTLR_CPU_IEP_FIR_CLR 0x1c
  32. #define ICTLR_CPU_IER 0x20
  33. #define ICTLR_CPU_IER_SET 0x24
  34. #define ICTLR_CPU_IER_CLR 0x28
  35. #define ICTLR_CPU_IEP_CLASS 0x2C
  36. #define ICTLR_COP_IER 0x30
  37. #define ICTLR_COP_IER_SET 0x34
  38. #define ICTLR_COP_IER_CLR 0x38
  39. #define ICTLR_COP_IEP_CLASS 0x3c
  40. #define FIRST_LEGACY_IRQ 32
  41. #define TEGRA_MAX_NUM_ICTLRS 5
  42. #define SGI_MASK 0xFFFF
  43. static int num_ictlrs;
  44. static void __iomem *ictlr_reg_base[] = {
  45. IO_ADDRESS(TEGRA_PRIMARY_ICTLR_BASE),
  46. IO_ADDRESS(TEGRA_SECONDARY_ICTLR_BASE),
  47. IO_ADDRESS(TEGRA_TERTIARY_ICTLR_BASE),
  48. IO_ADDRESS(TEGRA_QUATERNARY_ICTLR_BASE),
  49. IO_ADDRESS(TEGRA_QUINARY_ICTLR_BASE),
  50. };
  51. #ifdef CONFIG_PM_SLEEP
  52. static u32 cop_ier[TEGRA_MAX_NUM_ICTLRS];
  53. static u32 cop_iep[TEGRA_MAX_NUM_ICTLRS];
  54. static u32 cpu_ier[TEGRA_MAX_NUM_ICTLRS];
  55. static u32 cpu_iep[TEGRA_MAX_NUM_ICTLRS];
  56. static u32 ictlr_wake_mask[TEGRA_MAX_NUM_ICTLRS];
  57. #endif
  58. bool tegra_pending_sgi(void)
  59. {
  60. u32 pending_set;
  61. void __iomem *distbase = IO_ADDRESS(TEGRA_ARM_INT_DIST_BASE);
  62. pending_set = readl_relaxed(distbase + GIC_DIST_PENDING_SET);
  63. if (pending_set & SGI_MASK)
  64. return true;
  65. return false;
  66. }
  67. static inline void tegra_irq_write_mask(unsigned int irq, unsigned long reg)
  68. {
  69. void __iomem *base;
  70. u32 mask;
  71. BUG_ON(irq < FIRST_LEGACY_IRQ ||
  72. irq >= FIRST_LEGACY_IRQ + num_ictlrs * 32);
  73. base = ictlr_reg_base[(irq - FIRST_LEGACY_IRQ) / 32];
  74. mask = BIT((irq - FIRST_LEGACY_IRQ) % 32);
  75. __raw_writel(mask, base + reg);
  76. }
  77. static void tegra_mask(struct irq_data *d)
  78. {
  79. if (d->irq < FIRST_LEGACY_IRQ)
  80. return;
  81. tegra_irq_write_mask(d->irq, ICTLR_CPU_IER_CLR);
  82. }
  83. static void tegra_unmask(struct irq_data *d)
  84. {
  85. if (d->irq < FIRST_LEGACY_IRQ)
  86. return;
  87. tegra_irq_write_mask(d->irq, ICTLR_CPU_IER_SET);
  88. }
  89. static void tegra_ack(struct irq_data *d)
  90. {
  91. if (d->irq < FIRST_LEGACY_IRQ)
  92. return;
  93. tegra_irq_write_mask(d->irq, ICTLR_CPU_IEP_FIR_CLR);
  94. }
  95. static void tegra_eoi(struct irq_data *d)
  96. {
  97. if (d->irq < FIRST_LEGACY_IRQ)
  98. return;
  99. tegra_irq_write_mask(d->irq, ICTLR_CPU_IEP_FIR_CLR);
  100. }
  101. static int tegra_retrigger(struct irq_data *d)
  102. {
  103. if (d->irq < FIRST_LEGACY_IRQ)
  104. return 0;
  105. tegra_irq_write_mask(d->irq, ICTLR_CPU_IEP_FIR_SET);
  106. return 1;
  107. }
  108. #ifdef CONFIG_PM_SLEEP
  109. static int tegra_set_wake(struct irq_data *d, unsigned int enable)
  110. {
  111. u32 irq = d->irq;
  112. u32 index, mask;
  113. if (irq < FIRST_LEGACY_IRQ ||
  114. irq >= FIRST_LEGACY_IRQ + num_ictlrs * 32)
  115. return -EINVAL;
  116. index = ((irq - FIRST_LEGACY_IRQ) / 32);
  117. mask = BIT((irq - FIRST_LEGACY_IRQ) % 32);
  118. if (enable)
  119. ictlr_wake_mask[index] |= mask;
  120. else
  121. ictlr_wake_mask[index] &= ~mask;
  122. return 0;
  123. }
  124. static int tegra_legacy_irq_suspend(void)
  125. {
  126. unsigned long flags;
  127. int i;
  128. local_irq_save(flags);
  129. for (i = 0; i < num_ictlrs; i++) {
  130. void __iomem *ictlr = ictlr_reg_base[i];
  131. /* Save interrupt state */
  132. cpu_ier[i] = readl_relaxed(ictlr + ICTLR_CPU_IER);
  133. cpu_iep[i] = readl_relaxed(ictlr + ICTLR_CPU_IEP_CLASS);
  134. cop_ier[i] = readl_relaxed(ictlr + ICTLR_COP_IER);
  135. cop_iep[i] = readl_relaxed(ictlr + ICTLR_COP_IEP_CLASS);
  136. /* Disable COP interrupts */
  137. writel_relaxed(~0ul, ictlr + ICTLR_COP_IER_CLR);
  138. /* Disable CPU interrupts */
  139. writel_relaxed(~0ul, ictlr + ICTLR_CPU_IER_CLR);
  140. /* Enable the wakeup sources of ictlr */
  141. writel_relaxed(ictlr_wake_mask[i], ictlr + ICTLR_CPU_IER_SET);
  142. }
  143. local_irq_restore(flags);
  144. return 0;
  145. }
  146. static void tegra_legacy_irq_resume(void)
  147. {
  148. unsigned long flags;
  149. int i;
  150. local_irq_save(flags);
  151. for (i = 0; i < num_ictlrs; i++) {
  152. void __iomem *ictlr = ictlr_reg_base[i];
  153. writel_relaxed(cpu_iep[i], ictlr + ICTLR_CPU_IEP_CLASS);
  154. writel_relaxed(~0ul, ictlr + ICTLR_CPU_IER_CLR);
  155. writel_relaxed(cpu_ier[i], ictlr + ICTLR_CPU_IER_SET);
  156. writel_relaxed(cop_iep[i], ictlr + ICTLR_COP_IEP_CLASS);
  157. writel_relaxed(~0ul, ictlr + ICTLR_COP_IER_CLR);
  158. writel_relaxed(cop_ier[i], ictlr + ICTLR_COP_IER_SET);
  159. }
  160. local_irq_restore(flags);
  161. }
  162. static struct syscore_ops tegra_legacy_irq_syscore_ops = {
  163. .suspend = tegra_legacy_irq_suspend,
  164. .resume = tegra_legacy_irq_resume,
  165. };
  166. int tegra_legacy_irq_syscore_init(void)
  167. {
  168. register_syscore_ops(&tegra_legacy_irq_syscore_ops);
  169. return 0;
  170. }
  171. #else
  172. #define tegra_set_wake NULL
  173. #endif
  174. void __init tegra_init_irq(void)
  175. {
  176. int i;
  177. void __iomem *distbase;
  178. distbase = IO_ADDRESS(TEGRA_ARM_INT_DIST_BASE);
  179. num_ictlrs = readl_relaxed(distbase + GIC_DIST_CTR) & 0x1f;
  180. if (num_ictlrs > ARRAY_SIZE(ictlr_reg_base)) {
  181. WARN(1, "Too many (%d) interrupt controllers found. Maximum is %d.",
  182. num_ictlrs, ARRAY_SIZE(ictlr_reg_base));
  183. num_ictlrs = ARRAY_SIZE(ictlr_reg_base);
  184. }
  185. for (i = 0; i < num_ictlrs; i++) {
  186. void __iomem *ictlr = ictlr_reg_base[i];
  187. writel(~0, ictlr + ICTLR_CPU_IER_CLR);
  188. writel(0, ictlr + ICTLR_CPU_IEP_CLASS);
  189. }
  190. gic_arch_extn.irq_ack = tegra_ack;
  191. gic_arch_extn.irq_eoi = tegra_eoi;
  192. gic_arch_extn.irq_mask = tegra_mask;
  193. gic_arch_extn.irq_unmask = tegra_unmask;
  194. gic_arch_extn.irq_retrigger = tegra_retrigger;
  195. gic_arch_extn.irq_set_wake = tegra_set_wake;
  196. gic_arch_extn.flags = IRQCHIP_MASK_ON_SUSPEND;
  197. /*
  198. * Check if there is a devicetree present, since the GIC will be
  199. * initialized elsewhere under DT.
  200. */
  201. if (!of_have_populated_dt())
  202. gic_init(0, 29, distbase,
  203. IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x100));
  204. }