irq.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/cpu_pm.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/io.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/irqchip/arm-gic.h>
  27. #include <linux/syscore_ops.h>
  28. #include "board.h"
  29. #include "iomap.h"
  30. #define ICTLR_CPU_IEP_VFIQ 0x08
  31. #define ICTLR_CPU_IEP_FIR 0x14
  32. #define ICTLR_CPU_IEP_FIR_SET 0x18
  33. #define ICTLR_CPU_IEP_FIR_CLR 0x1c
  34. #define ICTLR_CPU_IER 0x20
  35. #define ICTLR_CPU_IER_SET 0x24
  36. #define ICTLR_CPU_IER_CLR 0x28
  37. #define ICTLR_CPU_IEP_CLASS 0x2C
  38. #define ICTLR_COP_IER 0x30
  39. #define ICTLR_COP_IER_SET 0x34
  40. #define ICTLR_COP_IER_CLR 0x38
  41. #define ICTLR_COP_IEP_CLASS 0x3c
  42. #define FIRST_LEGACY_IRQ 32
  43. #define TEGRA_MAX_NUM_ICTLRS 5
  44. #define SGI_MASK 0xFFFF
  45. static int num_ictlrs;
  46. static void __iomem *ictlr_reg_base[] = {
  47. IO_ADDRESS(TEGRA_PRIMARY_ICTLR_BASE),
  48. IO_ADDRESS(TEGRA_SECONDARY_ICTLR_BASE),
  49. IO_ADDRESS(TEGRA_TERTIARY_ICTLR_BASE),
  50. IO_ADDRESS(TEGRA_QUATERNARY_ICTLR_BASE),
  51. IO_ADDRESS(TEGRA_QUINARY_ICTLR_BASE),
  52. };
  53. #ifdef CONFIG_PM_SLEEP
  54. static u32 cop_ier[TEGRA_MAX_NUM_ICTLRS];
  55. static u32 cop_iep[TEGRA_MAX_NUM_ICTLRS];
  56. static u32 cpu_ier[TEGRA_MAX_NUM_ICTLRS];
  57. static u32 cpu_iep[TEGRA_MAX_NUM_ICTLRS];
  58. static u32 ictlr_wake_mask[TEGRA_MAX_NUM_ICTLRS];
  59. static void __iomem *tegra_gic_cpu_base;
  60. #endif
  61. bool tegra_pending_sgi(void)
  62. {
  63. u32 pending_set;
  64. void __iomem *distbase = IO_ADDRESS(TEGRA_ARM_INT_DIST_BASE);
  65. pending_set = readl_relaxed(distbase + GIC_DIST_PENDING_SET);
  66. if (pending_set & SGI_MASK)
  67. return true;
  68. return false;
  69. }
  70. static inline void tegra_irq_write_mask(unsigned int irq, unsigned long reg)
  71. {
  72. void __iomem *base;
  73. u32 mask;
  74. BUG_ON(irq < FIRST_LEGACY_IRQ ||
  75. irq >= FIRST_LEGACY_IRQ + num_ictlrs * 32);
  76. base = ictlr_reg_base[(irq - FIRST_LEGACY_IRQ) / 32];
  77. mask = BIT((irq - FIRST_LEGACY_IRQ) % 32);
  78. __raw_writel(mask, base + reg);
  79. }
  80. static void tegra_mask(struct irq_data *d)
  81. {
  82. if (d->irq < FIRST_LEGACY_IRQ)
  83. return;
  84. tegra_irq_write_mask(d->irq, ICTLR_CPU_IER_CLR);
  85. }
  86. static void tegra_unmask(struct irq_data *d)
  87. {
  88. if (d->irq < FIRST_LEGACY_IRQ)
  89. return;
  90. tegra_irq_write_mask(d->irq, ICTLR_CPU_IER_SET);
  91. }
  92. static void tegra_ack(struct irq_data *d)
  93. {
  94. if (d->irq < FIRST_LEGACY_IRQ)
  95. return;
  96. tegra_irq_write_mask(d->irq, ICTLR_CPU_IEP_FIR_CLR);
  97. }
  98. static void tegra_eoi(struct irq_data *d)
  99. {
  100. if (d->irq < FIRST_LEGACY_IRQ)
  101. return;
  102. tegra_irq_write_mask(d->irq, ICTLR_CPU_IEP_FIR_CLR);
  103. }
  104. static int tegra_retrigger(struct irq_data *d)
  105. {
  106. if (d->irq < FIRST_LEGACY_IRQ)
  107. return 0;
  108. tegra_irq_write_mask(d->irq, ICTLR_CPU_IEP_FIR_SET);
  109. return 1;
  110. }
  111. #ifdef CONFIG_PM_SLEEP
  112. static int tegra_set_wake(struct irq_data *d, unsigned int enable)
  113. {
  114. u32 irq = d->irq;
  115. u32 index, mask;
  116. if (irq < FIRST_LEGACY_IRQ ||
  117. irq >= FIRST_LEGACY_IRQ + num_ictlrs * 32)
  118. return -EINVAL;
  119. index = ((irq - FIRST_LEGACY_IRQ) / 32);
  120. mask = BIT((irq - FIRST_LEGACY_IRQ) % 32);
  121. if (enable)
  122. ictlr_wake_mask[index] |= mask;
  123. else
  124. ictlr_wake_mask[index] &= ~mask;
  125. return 0;
  126. }
  127. static int tegra_legacy_irq_suspend(void)
  128. {
  129. unsigned long flags;
  130. int i;
  131. local_irq_save(flags);
  132. for (i = 0; i < num_ictlrs; i++) {
  133. void __iomem *ictlr = ictlr_reg_base[i];
  134. /* Save interrupt state */
  135. cpu_ier[i] = readl_relaxed(ictlr + ICTLR_CPU_IER);
  136. cpu_iep[i] = readl_relaxed(ictlr + ICTLR_CPU_IEP_CLASS);
  137. cop_ier[i] = readl_relaxed(ictlr + ICTLR_COP_IER);
  138. cop_iep[i] = readl_relaxed(ictlr + ICTLR_COP_IEP_CLASS);
  139. /* Disable COP interrupts */
  140. writel_relaxed(~0ul, ictlr + ICTLR_COP_IER_CLR);
  141. /* Disable CPU interrupts */
  142. writel_relaxed(~0ul, ictlr + ICTLR_CPU_IER_CLR);
  143. /* Enable the wakeup sources of ictlr */
  144. writel_relaxed(ictlr_wake_mask[i], ictlr + ICTLR_CPU_IER_SET);
  145. }
  146. local_irq_restore(flags);
  147. return 0;
  148. }
  149. static void tegra_legacy_irq_resume(void)
  150. {
  151. unsigned long flags;
  152. int i;
  153. local_irq_save(flags);
  154. for (i = 0; i < num_ictlrs; i++) {
  155. void __iomem *ictlr = ictlr_reg_base[i];
  156. writel_relaxed(cpu_iep[i], ictlr + ICTLR_CPU_IEP_CLASS);
  157. writel_relaxed(~0ul, ictlr + ICTLR_CPU_IER_CLR);
  158. writel_relaxed(cpu_ier[i], ictlr + ICTLR_CPU_IER_SET);
  159. writel_relaxed(cop_iep[i], ictlr + ICTLR_COP_IEP_CLASS);
  160. writel_relaxed(~0ul, ictlr + ICTLR_COP_IER_CLR);
  161. writel_relaxed(cop_ier[i], ictlr + ICTLR_COP_IER_SET);
  162. }
  163. local_irq_restore(flags);
  164. }
  165. static struct syscore_ops tegra_legacy_irq_syscore_ops = {
  166. .suspend = tegra_legacy_irq_suspend,
  167. .resume = tegra_legacy_irq_resume,
  168. };
  169. int tegra_legacy_irq_syscore_init(void)
  170. {
  171. register_syscore_ops(&tegra_legacy_irq_syscore_ops);
  172. return 0;
  173. }
  174. static int tegra_gic_notifier(struct notifier_block *self,
  175. unsigned long cmd, void *v)
  176. {
  177. switch (cmd) {
  178. case CPU_PM_ENTER:
  179. writel_relaxed(0x1E0, tegra_gic_cpu_base + GIC_CPU_CTRL);
  180. break;
  181. }
  182. return NOTIFY_OK;
  183. }
  184. static struct notifier_block tegra_gic_notifier_block = {
  185. .notifier_call = tegra_gic_notifier,
  186. };
  187. static const struct of_device_id tegra114_dt_gic_match[] __initconst = {
  188. { .compatible = "arm,cortex-a15-gic" },
  189. { }
  190. };
  191. static void tegra114_gic_cpu_pm_registration(void)
  192. {
  193. struct device_node *dn;
  194. dn = of_find_matching_node(NULL, tegra114_dt_gic_match);
  195. if (!dn)
  196. return;
  197. tegra_gic_cpu_base = of_iomap(dn, 1);
  198. cpu_pm_register_notifier(&tegra_gic_notifier_block);
  199. }
  200. #else
  201. #define tegra_set_wake NULL
  202. static void tegra114_gic_cpu_pm_registration(void) { }
  203. #endif
  204. void __init tegra_init_irq(void)
  205. {
  206. int i;
  207. void __iomem *distbase;
  208. distbase = IO_ADDRESS(TEGRA_ARM_INT_DIST_BASE);
  209. num_ictlrs = readl_relaxed(distbase + GIC_DIST_CTR) & 0x1f;
  210. if (num_ictlrs > ARRAY_SIZE(ictlr_reg_base)) {
  211. WARN(1, "Too many (%d) interrupt controllers found. Maximum is %d.",
  212. num_ictlrs, ARRAY_SIZE(ictlr_reg_base));
  213. num_ictlrs = ARRAY_SIZE(ictlr_reg_base);
  214. }
  215. for (i = 0; i < num_ictlrs; i++) {
  216. void __iomem *ictlr = ictlr_reg_base[i];
  217. writel(~0, ictlr + ICTLR_CPU_IER_CLR);
  218. writel(0, ictlr + ICTLR_CPU_IEP_CLASS);
  219. }
  220. gic_arch_extn.irq_ack = tegra_ack;
  221. gic_arch_extn.irq_eoi = tegra_eoi;
  222. gic_arch_extn.irq_mask = tegra_mask;
  223. gic_arch_extn.irq_unmask = tegra_unmask;
  224. gic_arch_extn.irq_retrigger = tegra_retrigger;
  225. gic_arch_extn.irq_set_wake = tegra_set_wake;
  226. gic_arch_extn.flags = IRQCHIP_MASK_ON_SUSPEND;
  227. /*
  228. * Check if there is a devicetree present, since the GIC will be
  229. * initialized elsewhere under DT.
  230. */
  231. if (!of_have_populated_dt())
  232. gic_init(0, 29, distbase,
  233. IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x100));
  234. tegra114_gic_cpu_pm_registration();
  235. }