pm.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <asm/smp_plat.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/suspend.h>
  29. #include <asm/idmap.h>
  30. #include <asm/proc-fns.h>
  31. #include <asm/tlbflush.h>
  32. #include "iomap.h"
  33. #include "reset.h"
  34. #include "flowctrl.h"
  35. #include "sleep.h"
  36. #include "tegra_cpu_car.h"
  37. #define TEGRA_POWER_CPU_PWRREQ_OE (1 << 16) /* CPU pwr req enable */
  38. #define PMC_CTRL 0x0
  39. #define PMC_CPUPWRGOOD_TIMER 0xc8
  40. #define PMC_CPUPWROFF_TIMER 0xcc
  41. #ifdef CONFIG_PM_SLEEP
  42. static unsigned int g_diag_reg;
  43. static DEFINE_SPINLOCK(tegra_lp2_lock);
  44. static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
  45. static struct clk *tegra_pclk;
  46. void (*tegra_tear_down_cpu)(void);
  47. void save_cpu_arch_register(void)
  48. {
  49. /* read diagnostic register */
  50. asm("mrc p15, 0, %0, c15, c0, 1" : "=r"(g_diag_reg) : : "cc");
  51. return;
  52. }
  53. void restore_cpu_arch_register(void)
  54. {
  55. /* write diagnostic register */
  56. asm("mcr p15, 0, %0, c15, c0, 1" : : "r"(g_diag_reg) : "cc");
  57. return;
  58. }
  59. static void set_power_timers(unsigned long us_on, unsigned long us_off)
  60. {
  61. unsigned long long ticks;
  62. unsigned long long pclk;
  63. unsigned long rate;
  64. static unsigned long tegra_last_pclk;
  65. if (tegra_pclk == NULL) {
  66. tegra_pclk = clk_get_sys(NULL, "pclk");
  67. WARN_ON(IS_ERR(tegra_pclk));
  68. }
  69. rate = clk_get_rate(tegra_pclk);
  70. if (WARN_ON_ONCE(rate <= 0))
  71. pclk = 100000000;
  72. else
  73. pclk = rate;
  74. if ((rate != tegra_last_pclk)) {
  75. ticks = (us_on * pclk) + 999999ull;
  76. do_div(ticks, 1000000);
  77. writel((unsigned long)ticks, pmc + PMC_CPUPWRGOOD_TIMER);
  78. ticks = (us_off * pclk) + 999999ull;
  79. do_div(ticks, 1000000);
  80. writel((unsigned long)ticks, pmc + PMC_CPUPWROFF_TIMER);
  81. wmb();
  82. }
  83. tegra_last_pclk = pclk;
  84. }
  85. /*
  86. * restore_cpu_complex
  87. *
  88. * restores cpu clock setting, clears flow controller
  89. *
  90. * Always called on CPU 0.
  91. */
  92. static void restore_cpu_complex(void)
  93. {
  94. int cpu = smp_processor_id();
  95. BUG_ON(cpu != 0);
  96. #ifdef CONFIG_SMP
  97. cpu = cpu_logical_map(cpu);
  98. #endif
  99. /* Restore the CPU clock settings */
  100. tegra_cpu_clock_resume();
  101. flowctrl_cpu_suspend_exit(cpu);
  102. restore_cpu_arch_register();
  103. }
  104. /*
  105. * suspend_cpu_complex
  106. *
  107. * saves pll state for use by restart_plls, prepares flow controller for
  108. * transition to suspend state
  109. *
  110. * Must always be called on cpu 0.
  111. */
  112. static void suspend_cpu_complex(void)
  113. {
  114. int cpu = smp_processor_id();
  115. BUG_ON(cpu != 0);
  116. #ifdef CONFIG_SMP
  117. cpu = cpu_logical_map(cpu);
  118. #endif
  119. /* Save the CPU clock settings */
  120. tegra_cpu_clock_suspend();
  121. flowctrl_cpu_suspend_enter(cpu);
  122. save_cpu_arch_register();
  123. }
  124. void __cpuinit tegra_clear_cpu_in_lp2(int phy_cpu_id)
  125. {
  126. u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
  127. spin_lock(&tegra_lp2_lock);
  128. BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
  129. *cpu_in_lp2 &= ~BIT(phy_cpu_id);
  130. spin_unlock(&tegra_lp2_lock);
  131. }
  132. bool __cpuinit tegra_set_cpu_in_lp2(int phy_cpu_id)
  133. {
  134. bool last_cpu = false;
  135. cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
  136. u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
  137. spin_lock(&tegra_lp2_lock);
  138. BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
  139. *cpu_in_lp2 |= BIT(phy_cpu_id);
  140. if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
  141. last_cpu = true;
  142. spin_unlock(&tegra_lp2_lock);
  143. return last_cpu;
  144. }
  145. static int tegra_sleep_cpu(unsigned long v2p)
  146. {
  147. /* Switch to the identity mapping. */
  148. cpu_switch_mm(idmap_pgd, &init_mm);
  149. /* Flush the TLB. */
  150. local_flush_tlb_all();
  151. tegra_sleep_cpu_finish(v2p);
  152. /* should never here */
  153. BUG();
  154. return 0;
  155. }
  156. void tegra_idle_lp2_last(u32 cpu_on_time, u32 cpu_off_time)
  157. {
  158. u32 mode;
  159. /* Only the last cpu down does the final suspend steps */
  160. mode = readl(pmc + PMC_CTRL);
  161. mode |= TEGRA_POWER_CPU_PWRREQ_OE;
  162. writel(mode, pmc + PMC_CTRL);
  163. set_power_timers(cpu_on_time, cpu_off_time);
  164. cpu_cluster_pm_enter();
  165. suspend_cpu_complex();
  166. cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
  167. restore_cpu_complex();
  168. cpu_cluster_pm_exit();
  169. }
  170. #endif