pm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/clk/tegra.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/cpu_pm.h>
  21. #include <linux/delay.h>
  22. #include <linux/err.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/suspend.h>
  28. #include <soc/tegra/flowctrl.h>
  29. #include <soc/tegra/fuse.h>
  30. #include <soc/tegra/pm.h>
  31. #include <soc/tegra/pmc.h>
  32. #include <asm/cacheflush.h>
  33. #include <asm/idmap.h>
  34. #include <asm/proc-fns.h>
  35. #include <asm/smp_plat.h>
  36. #include <asm/suspend.h>
  37. #include <asm/tlbflush.h>
  38. #include "iomap.h"
  39. #include "pm.h"
  40. #include "reset.h"
  41. #include "sleep.h"
  42. #ifdef CONFIG_PM_SLEEP
  43. static DEFINE_SPINLOCK(tegra_lp2_lock);
  44. static u32 iram_save_size;
  45. static void *iram_save_addr;
  46. struct tegra_lp1_iram tegra_lp1_iram;
  47. void (*tegra_tear_down_cpu)(void);
  48. void (*tegra_sleep_core_finish)(unsigned long v2p);
  49. static int (*tegra_sleep_func)(unsigned long v2p);
  50. static void tegra_tear_down_cpu_init(void)
  51. {
  52. switch (tegra_get_chip_id()) {
  53. case TEGRA20:
  54. if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
  55. tegra_tear_down_cpu = tegra20_tear_down_cpu;
  56. break;
  57. case TEGRA30:
  58. case TEGRA114:
  59. case TEGRA124:
  60. if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
  61. IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
  62. IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
  63. tegra_tear_down_cpu = tegra30_tear_down_cpu;
  64. break;
  65. }
  66. }
  67. /*
  68. * restore_cpu_complex
  69. *
  70. * restores cpu clock setting, clears flow controller
  71. *
  72. * Always called on CPU 0.
  73. */
  74. static void restore_cpu_complex(void)
  75. {
  76. int cpu = smp_processor_id();
  77. BUG_ON(cpu != 0);
  78. #ifdef CONFIG_SMP
  79. cpu = cpu_logical_map(cpu);
  80. #endif
  81. /* Restore the CPU clock settings */
  82. tegra_cpu_clock_resume();
  83. flowctrl_cpu_suspend_exit(cpu);
  84. }
  85. /*
  86. * suspend_cpu_complex
  87. *
  88. * saves pll state for use by restart_plls, prepares flow controller for
  89. * transition to suspend state
  90. *
  91. * Must always be called on cpu 0.
  92. */
  93. static void suspend_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. /* Save the CPU clock settings */
  101. tegra_cpu_clock_suspend();
  102. flowctrl_cpu_suspend_enter(cpu);
  103. }
  104. void tegra_clear_cpu_in_lp2(void)
  105. {
  106. int phy_cpu_id = cpu_logical_map(smp_processor_id());
  107. u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
  108. spin_lock(&tegra_lp2_lock);
  109. BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
  110. *cpu_in_lp2 &= ~BIT(phy_cpu_id);
  111. spin_unlock(&tegra_lp2_lock);
  112. }
  113. bool tegra_set_cpu_in_lp2(void)
  114. {
  115. int phy_cpu_id = cpu_logical_map(smp_processor_id());
  116. bool last_cpu = false;
  117. cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
  118. u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
  119. spin_lock(&tegra_lp2_lock);
  120. BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
  121. *cpu_in_lp2 |= BIT(phy_cpu_id);
  122. if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
  123. last_cpu = true;
  124. else if (tegra_get_chip_id() == TEGRA20 && phy_cpu_id == 1)
  125. tegra20_cpu_set_resettable_soon();
  126. spin_unlock(&tegra_lp2_lock);
  127. return last_cpu;
  128. }
  129. int tegra_cpu_do_idle(void)
  130. {
  131. return cpu_do_idle();
  132. }
  133. static int tegra_sleep_cpu(unsigned long v2p)
  134. {
  135. setup_mm_for_reboot();
  136. tegra_sleep_cpu_finish(v2p);
  137. /* should never here */
  138. BUG();
  139. return 0;
  140. }
  141. static void tegra_pm_set(enum tegra_suspend_mode mode)
  142. {
  143. u32 value;
  144. switch (tegra_get_chip_id()) {
  145. case TEGRA20:
  146. case TEGRA30:
  147. break;
  148. default:
  149. /* Turn off CRAIL */
  150. value = flowctrl_read_cpu_csr(0);
  151. value &= ~FLOW_CTRL_CSR_ENABLE_EXT_MASK;
  152. value |= FLOW_CTRL_CSR_ENABLE_EXT_CRAIL;
  153. flowctrl_write_cpu_csr(0, value);
  154. break;
  155. }
  156. tegra_pmc_enter_suspend_mode(mode);
  157. }
  158. void tegra_idle_lp2_last(void)
  159. {
  160. tegra_pm_set(TEGRA_SUSPEND_LP2);
  161. cpu_cluster_pm_enter();
  162. suspend_cpu_complex();
  163. cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
  164. restore_cpu_complex();
  165. cpu_cluster_pm_exit();
  166. }
  167. enum tegra_suspend_mode tegra_pm_validate_suspend_mode(
  168. enum tegra_suspend_mode mode)
  169. {
  170. /*
  171. * The Tegra devices support suspending to LP1 or lower currently.
  172. */
  173. if (mode > TEGRA_SUSPEND_LP1)
  174. return TEGRA_SUSPEND_LP1;
  175. return mode;
  176. }
  177. static int tegra_sleep_core(unsigned long v2p)
  178. {
  179. setup_mm_for_reboot();
  180. tegra_sleep_core_finish(v2p);
  181. /* should never here */
  182. BUG();
  183. return 0;
  184. }
  185. /*
  186. * tegra_lp1_iram_hook
  187. *
  188. * Hooking the address of LP1 reset vector and SDRAM self-refresh code in
  189. * SDRAM. These codes not be copied to IRAM in this fuction. We need to
  190. * copy these code to IRAM before LP0/LP1 suspend and restore the content
  191. * of IRAM after resume.
  192. */
  193. static bool tegra_lp1_iram_hook(void)
  194. {
  195. switch (tegra_get_chip_id()) {
  196. case TEGRA20:
  197. if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
  198. tegra20_lp1_iram_hook();
  199. break;
  200. case TEGRA30:
  201. case TEGRA114:
  202. case TEGRA124:
  203. if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
  204. IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
  205. IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
  206. tegra30_lp1_iram_hook();
  207. break;
  208. default:
  209. break;
  210. }
  211. if (!tegra_lp1_iram.start_addr || !tegra_lp1_iram.end_addr)
  212. return false;
  213. iram_save_size = tegra_lp1_iram.end_addr - tegra_lp1_iram.start_addr;
  214. iram_save_addr = kmalloc(iram_save_size, GFP_KERNEL);
  215. if (!iram_save_addr)
  216. return false;
  217. return true;
  218. }
  219. static bool tegra_sleep_core_init(void)
  220. {
  221. switch (tegra_get_chip_id()) {
  222. case TEGRA20:
  223. if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
  224. tegra20_sleep_core_init();
  225. break;
  226. case TEGRA30:
  227. case TEGRA114:
  228. case TEGRA124:
  229. if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
  230. IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
  231. IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
  232. tegra30_sleep_core_init();
  233. break;
  234. default:
  235. break;
  236. }
  237. if (!tegra_sleep_core_finish)
  238. return false;
  239. return true;
  240. }
  241. static void tegra_suspend_enter_lp1(void)
  242. {
  243. /* copy the reset vector & SDRAM shutdown code into IRAM */
  244. memcpy(iram_save_addr, IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA),
  245. iram_save_size);
  246. memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA),
  247. tegra_lp1_iram.start_addr, iram_save_size);
  248. *((u32 *)tegra_cpu_lp1_mask) = 1;
  249. }
  250. static void tegra_suspend_exit_lp1(void)
  251. {
  252. /* restore IRAM */
  253. memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA), iram_save_addr,
  254. iram_save_size);
  255. *(u32 *)tegra_cpu_lp1_mask = 0;
  256. }
  257. static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = {
  258. [TEGRA_SUSPEND_NONE] = "none",
  259. [TEGRA_SUSPEND_LP2] = "LP2",
  260. [TEGRA_SUSPEND_LP1] = "LP1",
  261. [TEGRA_SUSPEND_LP0] = "LP0",
  262. };
  263. static int tegra_suspend_enter(suspend_state_t state)
  264. {
  265. enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
  266. if (WARN_ON(mode < TEGRA_SUSPEND_NONE ||
  267. mode >= TEGRA_MAX_SUSPEND_MODE))
  268. return -EINVAL;
  269. pr_info("Entering suspend state %s\n", lp_state[mode]);
  270. tegra_pm_set(mode);
  271. local_fiq_disable();
  272. suspend_cpu_complex();
  273. switch (mode) {
  274. case TEGRA_SUSPEND_LP1:
  275. tegra_suspend_enter_lp1();
  276. break;
  277. case TEGRA_SUSPEND_LP2:
  278. tegra_set_cpu_in_lp2();
  279. break;
  280. default:
  281. break;
  282. }
  283. cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, tegra_sleep_func);
  284. switch (mode) {
  285. case TEGRA_SUSPEND_LP1:
  286. tegra_suspend_exit_lp1();
  287. break;
  288. case TEGRA_SUSPEND_LP2:
  289. tegra_clear_cpu_in_lp2();
  290. break;
  291. default:
  292. break;
  293. }
  294. restore_cpu_complex();
  295. local_fiq_enable();
  296. return 0;
  297. }
  298. static const struct platform_suspend_ops tegra_suspend_ops = {
  299. .valid = suspend_valid_only_mem,
  300. .enter = tegra_suspend_enter,
  301. };
  302. void __init tegra_init_suspend(void)
  303. {
  304. enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
  305. if (mode == TEGRA_SUSPEND_NONE)
  306. return;
  307. tegra_tear_down_cpu_init();
  308. if (mode >= TEGRA_SUSPEND_LP1) {
  309. if (!tegra_lp1_iram_hook() || !tegra_sleep_core_init()) {
  310. pr_err("%s: unable to allocate memory for SDRAM"
  311. "self-refresh -- LP0/LP1 unavailable\n",
  312. __func__);
  313. tegra_pmc_set_suspend_mode(TEGRA_SUSPEND_LP2);
  314. mode = TEGRA_SUSPEND_LP2;
  315. }
  316. }
  317. /* set up sleep function for cpu_suspend */
  318. switch (mode) {
  319. case TEGRA_SUSPEND_LP1:
  320. tegra_sleep_func = tegra_sleep_core;
  321. break;
  322. case TEGRA_SUSPEND_LP2:
  323. tegra_sleep_func = tegra_sleep_cpu;
  324. break;
  325. default:
  326. break;
  327. }
  328. suspend_set_ops(&tegra_suspend_ops);
  329. }
  330. #endif