pm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * CPU complex suspend & resume functions for Tegra SoCs
  3. *
  4. * Copyright (c) 2009-2012, NVIDIA Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/io.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/delay.h>
  23. #include <linux/cpu_pm.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <linux/clk/tegra.h>
  27. #include <asm/smp_plat.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/suspend.h>
  30. #include <asm/idmap.h>
  31. #include <asm/proc-fns.h>
  32. #include <asm/tlbflush.h>
  33. #include "iomap.h"
  34. #include "reset.h"
  35. #include "flowctrl.h"
  36. #include "fuse.h"
  37. #include "sleep.h"
  38. #define TEGRA_POWER_CPU_PWRREQ_OE (1 << 16) /* CPU pwr req enable */
  39. #define PMC_CTRL 0x0
  40. #define PMC_CPUPWRGOOD_TIMER 0xc8
  41. #define PMC_CPUPWROFF_TIMER 0xcc
  42. #ifdef CONFIG_PM_SLEEP
  43. static unsigned int g_diag_reg;
  44. static DEFINE_SPINLOCK(tegra_lp2_lock);
  45. static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
  46. static struct clk *tegra_pclk;
  47. void (*tegra_tear_down_cpu)(void);
  48. void save_cpu_arch_register(void)
  49. {
  50. /* read diagnostic register */
  51. asm("mrc p15, 0, %0, c15, c0, 1" : "=r"(g_diag_reg) : : "cc");
  52. return;
  53. }
  54. void restore_cpu_arch_register(void)
  55. {
  56. /* write diagnostic register */
  57. asm("mcr p15, 0, %0, c15, c0, 1" : : "r"(g_diag_reg) : "cc");
  58. return;
  59. }
  60. static void set_power_timers(unsigned long us_on, unsigned long us_off)
  61. {
  62. unsigned long long ticks;
  63. unsigned long long pclk;
  64. unsigned long rate;
  65. static unsigned long tegra_last_pclk;
  66. if (tegra_pclk == NULL) {
  67. tegra_pclk = clk_get_sys(NULL, "pclk");
  68. WARN_ON(IS_ERR(tegra_pclk));
  69. }
  70. rate = clk_get_rate(tegra_pclk);
  71. if (WARN_ON_ONCE(rate <= 0))
  72. pclk = 100000000;
  73. else
  74. pclk = rate;
  75. if ((rate != tegra_last_pclk)) {
  76. ticks = (us_on * pclk) + 999999ull;
  77. do_div(ticks, 1000000);
  78. writel((unsigned long)ticks, pmc + PMC_CPUPWRGOOD_TIMER);
  79. ticks = (us_off * pclk) + 999999ull;
  80. do_div(ticks, 1000000);
  81. writel((unsigned long)ticks, pmc + PMC_CPUPWROFF_TIMER);
  82. wmb();
  83. }
  84. tegra_last_pclk = pclk;
  85. }
  86. /*
  87. * restore_cpu_complex
  88. *
  89. * restores cpu clock setting, clears flow controller
  90. *
  91. * Always called on CPU 0.
  92. */
  93. static void restore_cpu_complex(void)
  94. {
  95. int cpu = smp_processor_id();
  96. BUG_ON(cpu != 0);
  97. #ifdef CONFIG_SMP
  98. cpu = cpu_logical_map(cpu);
  99. #endif
  100. /* Restore the CPU clock settings */
  101. tegra_cpu_clock_resume();
  102. flowctrl_cpu_suspend_exit(cpu);
  103. restore_cpu_arch_register();
  104. }
  105. /*
  106. * suspend_cpu_complex
  107. *
  108. * saves pll state for use by restart_plls, prepares flow controller for
  109. * transition to suspend state
  110. *
  111. * Must always be called on cpu 0.
  112. */
  113. static void suspend_cpu_complex(void)
  114. {
  115. int cpu = smp_processor_id();
  116. BUG_ON(cpu != 0);
  117. #ifdef CONFIG_SMP
  118. cpu = cpu_logical_map(cpu);
  119. #endif
  120. /* Save the CPU clock settings */
  121. tegra_cpu_clock_suspend();
  122. flowctrl_cpu_suspend_enter(cpu);
  123. save_cpu_arch_register();
  124. }
  125. void tegra_clear_cpu_in_lp2(int phy_cpu_id)
  126. {
  127. u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
  128. spin_lock(&tegra_lp2_lock);
  129. BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
  130. *cpu_in_lp2 &= ~BIT(phy_cpu_id);
  131. spin_unlock(&tegra_lp2_lock);
  132. }
  133. bool tegra_set_cpu_in_lp2(int phy_cpu_id)
  134. {
  135. bool last_cpu = false;
  136. cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
  137. u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
  138. spin_lock(&tegra_lp2_lock);
  139. BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
  140. *cpu_in_lp2 |= BIT(phy_cpu_id);
  141. if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
  142. last_cpu = true;
  143. else if (tegra_chip_id == TEGRA20 && phy_cpu_id == 1)
  144. tegra20_cpu_set_resettable_soon();
  145. spin_unlock(&tegra_lp2_lock);
  146. return last_cpu;
  147. }
  148. static int tegra_sleep_cpu(unsigned long v2p)
  149. {
  150. /* Switch to the identity mapping. */
  151. cpu_switch_mm(idmap_pgd, &init_mm);
  152. /* Flush the TLB. */
  153. local_flush_tlb_all();
  154. tegra_sleep_cpu_finish(v2p);
  155. /* should never here */
  156. BUG();
  157. return 0;
  158. }
  159. void tegra_idle_lp2_last(u32 cpu_on_time, u32 cpu_off_time)
  160. {
  161. u32 mode;
  162. /* Only the last cpu down does the final suspend steps */
  163. mode = readl(pmc + PMC_CTRL);
  164. mode |= TEGRA_POWER_CPU_PWRREQ_OE;
  165. writel(mode, pmc + PMC_CTRL);
  166. set_power_timers(cpu_on_time, cpu_off_time);
  167. cpu_cluster_pm_enter();
  168. suspend_cpu_complex();
  169. cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
  170. restore_cpu_complex();
  171. cpu_cluster_pm_exit();
  172. }
  173. #endif