suspend.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * Copyright (c) 2011-2014 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * EXYNOS - Suspend support
  6. *
  7. * Based on arch/arm/mach-s3c2410/pm.c
  8. * Copyright (c) 2006 Simtec Electronics
  9. * Ben Dooks <ben@simtec.co.uk>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/suspend.h>
  17. #include <linux/syscore_ops.h>
  18. #include <linux/cpu_pm.h>
  19. #include <linux/io.h>
  20. #include <linux/irqchip/arm-gic.h>
  21. #include <linux/err.h>
  22. #include <linux/regulator/machine.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/hardware/cache-l2x0.h>
  25. #include <asm/firmware.h>
  26. #include <asm/mcpm.h>
  27. #include <asm/smp_scu.h>
  28. #include <asm/suspend.h>
  29. #include <plat/pm-common.h>
  30. #include <plat/regs-srom.h>
  31. #include "common.h"
  32. #include "regs-pmu.h"
  33. #include "regs-sys.h"
  34. #include "exynos-pmu.h"
  35. #define S5P_CHECK_SLEEP 0x00000BAD
  36. #define REG_TABLE_END (-1U)
  37. #define EXYNOS5420_CPU_STATE 0x28
  38. /**
  39. * struct exynos_wkup_irq - Exynos GIC to PMU IRQ mapping
  40. * @hwirq: Hardware IRQ signal of the GIC
  41. * @mask: Mask in PMU wake-up mask register
  42. */
  43. struct exynos_wkup_irq {
  44. unsigned int hwirq;
  45. u32 mask;
  46. };
  47. static struct sleep_save exynos5_sys_save[] = {
  48. SAVE_ITEM(EXYNOS5_SYS_I2C_CFG),
  49. };
  50. static struct sleep_save exynos_core_save[] = {
  51. /* SROM side */
  52. SAVE_ITEM(S5P_SROM_BW),
  53. SAVE_ITEM(S5P_SROM_BC0),
  54. SAVE_ITEM(S5P_SROM_BC1),
  55. SAVE_ITEM(S5P_SROM_BC2),
  56. SAVE_ITEM(S5P_SROM_BC3),
  57. };
  58. struct exynos_pm_data {
  59. const struct exynos_wkup_irq *wkup_irq;
  60. struct sleep_save *extra_save;
  61. int num_extra_save;
  62. unsigned int wake_disable_mask;
  63. unsigned int *release_ret_regs;
  64. void (*pm_prepare)(void);
  65. void (*pm_resume_prepare)(void);
  66. void (*pm_resume)(void);
  67. int (*pm_suspend)(void);
  68. int (*cpu_suspend)(unsigned long);
  69. };
  70. struct exynos_pm_data *pm_data;
  71. static int exynos5420_cpu_state;
  72. static unsigned int exynos_pmu_spare3;
  73. /*
  74. * GIC wake-up support
  75. */
  76. static u32 exynos_irqwake_intmask = 0xffffffff;
  77. static const struct exynos_wkup_irq exynos4_wkup_irq[] = {
  78. { 76, BIT(1) }, /* RTC alarm */
  79. { 77, BIT(2) }, /* RTC tick */
  80. { /* sentinel */ },
  81. };
  82. static const struct exynos_wkup_irq exynos5250_wkup_irq[] = {
  83. { 75, BIT(1) }, /* RTC alarm */
  84. { 76, BIT(2) }, /* RTC tick */
  85. { /* sentinel */ },
  86. };
  87. unsigned int exynos_release_ret_regs[] = {
  88. S5P_PAD_RET_MAUDIO_OPTION,
  89. S5P_PAD_RET_GPIO_OPTION,
  90. S5P_PAD_RET_UART_OPTION,
  91. S5P_PAD_RET_MMCA_OPTION,
  92. S5P_PAD_RET_MMCB_OPTION,
  93. S5P_PAD_RET_EBIA_OPTION,
  94. S5P_PAD_RET_EBIB_OPTION,
  95. REG_TABLE_END,
  96. };
  97. unsigned int exynos5420_release_ret_regs[] = {
  98. EXYNOS_PAD_RET_DRAM_OPTION,
  99. EXYNOS_PAD_RET_MAUDIO_OPTION,
  100. EXYNOS_PAD_RET_JTAG_OPTION,
  101. EXYNOS5420_PAD_RET_GPIO_OPTION,
  102. EXYNOS5420_PAD_RET_UART_OPTION,
  103. EXYNOS5420_PAD_RET_MMCA_OPTION,
  104. EXYNOS5420_PAD_RET_MMCB_OPTION,
  105. EXYNOS5420_PAD_RET_MMCC_OPTION,
  106. EXYNOS5420_PAD_RET_HSI_OPTION,
  107. EXYNOS_PAD_RET_EBIA_OPTION,
  108. EXYNOS_PAD_RET_EBIB_OPTION,
  109. EXYNOS5420_PAD_RET_SPI_OPTION,
  110. EXYNOS5420_PAD_RET_DRAM_COREBLK_OPTION,
  111. REG_TABLE_END,
  112. };
  113. static int exynos_irq_set_wake(struct irq_data *data, unsigned int state)
  114. {
  115. const struct exynos_wkup_irq *wkup_irq;
  116. if (!pm_data->wkup_irq)
  117. return -ENOENT;
  118. wkup_irq = pm_data->wkup_irq;
  119. while (wkup_irq->mask) {
  120. if (wkup_irq->hwirq == data->hwirq) {
  121. if (!state)
  122. exynos_irqwake_intmask |= wkup_irq->mask;
  123. else
  124. exynos_irqwake_intmask &= ~wkup_irq->mask;
  125. return 0;
  126. }
  127. ++wkup_irq;
  128. }
  129. return -ENOENT;
  130. }
  131. static int exynos_cpu_do_idle(void)
  132. {
  133. /* issue the standby signal into the pm unit. */
  134. cpu_do_idle();
  135. pr_info("Failed to suspend the system\n");
  136. return 1; /* Aborting suspend */
  137. }
  138. static void exynos_flush_cache_all(void)
  139. {
  140. flush_cache_all();
  141. outer_flush_all();
  142. }
  143. static int exynos_cpu_suspend(unsigned long arg)
  144. {
  145. exynos_flush_cache_all();
  146. return exynos_cpu_do_idle();
  147. }
  148. static int exynos5420_cpu_suspend(unsigned long arg)
  149. {
  150. /* MCPM works with HW CPU identifiers */
  151. unsigned int mpidr = read_cpuid_mpidr();
  152. unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  153. unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  154. __raw_writel(0x0, sysram_base_addr + EXYNOS5420_CPU_STATE);
  155. if (IS_ENABLED(CONFIG_EXYNOS5420_MCPM)) {
  156. mcpm_set_entry_vector(cpu, cluster, exynos_cpu_resume);
  157. /*
  158. * Residency value passed to mcpm_cpu_suspend back-end
  159. * has to be given clear semantics. Set to 0 as a
  160. * temporary value.
  161. */
  162. mcpm_cpu_suspend(0);
  163. }
  164. pr_info("Failed to suspend the system\n");
  165. /* return value != 0 means failure */
  166. return 1;
  167. }
  168. static void exynos_pm_set_wakeup_mask(void)
  169. {
  170. /* Set wake-up mask registers */
  171. pmu_raw_writel(exynos_get_eint_wake_mask(), S5P_EINT_WAKEUP_MASK);
  172. pmu_raw_writel(exynos_irqwake_intmask & ~(1 << 31), S5P_WAKEUP_MASK);
  173. }
  174. static void exynos_pm_enter_sleep_mode(void)
  175. {
  176. /* Set value of power down register for sleep mode */
  177. exynos_sys_powerdown_conf(SYS_SLEEP);
  178. pmu_raw_writel(S5P_CHECK_SLEEP, S5P_INFORM1);
  179. }
  180. static void exynos_pm_prepare(void)
  181. {
  182. /* Set wake-up mask registers */
  183. exynos_pm_set_wakeup_mask();
  184. s3c_pm_do_save(exynos_core_save, ARRAY_SIZE(exynos_core_save));
  185. if (pm_data->extra_save)
  186. s3c_pm_do_save(pm_data->extra_save,
  187. pm_data->num_extra_save);
  188. exynos_pm_enter_sleep_mode();
  189. /* ensure at least INFORM0 has the resume address */
  190. pmu_raw_writel(virt_to_phys(exynos_cpu_resume), S5P_INFORM0);
  191. }
  192. static void exynos5420_pm_prepare(void)
  193. {
  194. unsigned int tmp;
  195. /* Set wake-up mask registers */
  196. exynos_pm_set_wakeup_mask();
  197. s3c_pm_do_save(exynos_core_save, ARRAY_SIZE(exynos_core_save));
  198. exynos_pmu_spare3 = pmu_raw_readl(S5P_PMU_SPARE3);
  199. /*
  200. * The cpu state needs to be saved and restored so that the
  201. * secondary CPUs will enter low power start. Though the U-Boot
  202. * is setting the cpu state with low power flag, the kernel
  203. * needs to restore it back in case, the primary cpu fails to
  204. * suspend for any reason.
  205. */
  206. exynos5420_cpu_state = __raw_readl(sysram_base_addr +
  207. EXYNOS5420_CPU_STATE);
  208. exynos_pm_enter_sleep_mode();
  209. /* ensure at least INFORM0 has the resume address */
  210. if (IS_ENABLED(CONFIG_EXYNOS5420_MCPM))
  211. pmu_raw_writel(virt_to_phys(mcpm_entry_point), S5P_INFORM0);
  212. tmp = pmu_raw_readl(EXYNOS5_ARM_L2_OPTION);
  213. tmp &= ~EXYNOS5_USE_RETENTION;
  214. pmu_raw_writel(tmp, EXYNOS5_ARM_L2_OPTION);
  215. tmp = pmu_raw_readl(EXYNOS5420_SFR_AXI_CGDIS1);
  216. tmp |= EXYNOS5420_UFS;
  217. pmu_raw_writel(tmp, EXYNOS5420_SFR_AXI_CGDIS1);
  218. tmp = pmu_raw_readl(EXYNOS5420_ARM_COMMON_OPTION);
  219. tmp &= ~EXYNOS5420_L2RSTDISABLE_VALUE;
  220. pmu_raw_writel(tmp, EXYNOS5420_ARM_COMMON_OPTION);
  221. tmp = pmu_raw_readl(EXYNOS5420_FSYS2_OPTION);
  222. tmp |= EXYNOS5420_EMULATION;
  223. pmu_raw_writel(tmp, EXYNOS5420_FSYS2_OPTION);
  224. tmp = pmu_raw_readl(EXYNOS5420_PSGEN_OPTION);
  225. tmp |= EXYNOS5420_EMULATION;
  226. pmu_raw_writel(tmp, EXYNOS5420_PSGEN_OPTION);
  227. }
  228. static int exynos_pm_suspend(void)
  229. {
  230. exynos_pm_central_suspend();
  231. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  232. exynos_cpu_save_register();
  233. return 0;
  234. }
  235. static int exynos5420_pm_suspend(void)
  236. {
  237. u32 this_cluster;
  238. exynos_pm_central_suspend();
  239. /* Setting SEQ_OPTION register */
  240. this_cluster = MPIDR_AFFINITY_LEVEL(read_cpuid_mpidr(), 1);
  241. if (!this_cluster)
  242. pmu_raw_writel(EXYNOS5420_ARM_USE_STANDBY_WFI0,
  243. S5P_CENTRAL_SEQ_OPTION);
  244. else
  245. pmu_raw_writel(EXYNOS5420_KFC_USE_STANDBY_WFI0,
  246. S5P_CENTRAL_SEQ_OPTION);
  247. return 0;
  248. }
  249. static void exynos_pm_release_retention(void)
  250. {
  251. unsigned int i;
  252. for (i = 0; (pm_data->release_ret_regs[i] != REG_TABLE_END); i++)
  253. pmu_raw_writel(EXYNOS_WAKEUP_FROM_LOWPWR,
  254. pm_data->release_ret_regs[i]);
  255. }
  256. static void exynos_pm_resume(void)
  257. {
  258. u32 cpuid = read_cpuid_part();
  259. if (exynos_pm_central_resume())
  260. goto early_wakeup;
  261. /* For release retention */
  262. exynos_pm_release_retention();
  263. if (pm_data->extra_save)
  264. s3c_pm_do_restore_core(pm_data->extra_save,
  265. pm_data->num_extra_save);
  266. s3c_pm_do_restore_core(exynos_core_save, ARRAY_SIZE(exynos_core_save));
  267. if (cpuid == ARM_CPU_PART_CORTEX_A9)
  268. scu_enable(S5P_VA_SCU);
  269. if (call_firmware_op(resume) == -ENOSYS
  270. && cpuid == ARM_CPU_PART_CORTEX_A9)
  271. exynos_cpu_restore_register();
  272. early_wakeup:
  273. /* Clear SLEEP mode set in INFORM1 */
  274. pmu_raw_writel(0x0, S5P_INFORM1);
  275. }
  276. static void exynos5420_prepare_pm_resume(void)
  277. {
  278. if (IS_ENABLED(CONFIG_EXYNOS5420_MCPM))
  279. WARN_ON(mcpm_cpu_powered_up());
  280. }
  281. static void exynos5420_pm_resume(void)
  282. {
  283. unsigned long tmp;
  284. /* Restore the CPU0 low power state register */
  285. tmp = pmu_raw_readl(EXYNOS5_ARM_CORE0_SYS_PWR_REG);
  286. pmu_raw_writel(tmp | S5P_CORE_LOCAL_PWR_EN,
  287. EXYNOS5_ARM_CORE0_SYS_PWR_REG);
  288. /* Restore the sysram cpu state register */
  289. __raw_writel(exynos5420_cpu_state,
  290. sysram_base_addr + EXYNOS5420_CPU_STATE);
  291. pmu_raw_writel(EXYNOS5420_USE_STANDBY_WFI_ALL,
  292. S5P_CENTRAL_SEQ_OPTION);
  293. if (exynos_pm_central_resume())
  294. goto early_wakeup;
  295. /* For release retention */
  296. exynos_pm_release_retention();
  297. pmu_raw_writel(exynos_pmu_spare3, S5P_PMU_SPARE3);
  298. s3c_pm_do_restore_core(exynos_core_save, ARRAY_SIZE(exynos_core_save));
  299. early_wakeup:
  300. tmp = pmu_raw_readl(EXYNOS5420_SFR_AXI_CGDIS1);
  301. tmp &= ~EXYNOS5420_UFS;
  302. pmu_raw_writel(tmp, EXYNOS5420_SFR_AXI_CGDIS1);
  303. tmp = pmu_raw_readl(EXYNOS5420_FSYS2_OPTION);
  304. tmp &= ~EXYNOS5420_EMULATION;
  305. pmu_raw_writel(tmp, EXYNOS5420_FSYS2_OPTION);
  306. tmp = pmu_raw_readl(EXYNOS5420_PSGEN_OPTION);
  307. tmp &= ~EXYNOS5420_EMULATION;
  308. pmu_raw_writel(tmp, EXYNOS5420_PSGEN_OPTION);
  309. /* Clear SLEEP mode set in INFORM1 */
  310. pmu_raw_writel(0x0, S5P_INFORM1);
  311. }
  312. /*
  313. * Suspend Ops
  314. */
  315. static int exynos_suspend_enter(suspend_state_t state)
  316. {
  317. int ret;
  318. s3c_pm_debug_init();
  319. S3C_PMDBG("%s: suspending the system...\n", __func__);
  320. S3C_PMDBG("%s: wakeup masks: %08x,%08x\n", __func__,
  321. exynos_irqwake_intmask, exynos_get_eint_wake_mask());
  322. if (exynos_irqwake_intmask == -1U
  323. && exynos_get_eint_wake_mask() == -1U) {
  324. pr_err("%s: No wake-up sources!\n", __func__);
  325. pr_err("%s: Aborting sleep\n", __func__);
  326. return -EINVAL;
  327. }
  328. s3c_pm_save_uarts();
  329. if (pm_data->pm_prepare)
  330. pm_data->pm_prepare();
  331. flush_cache_all();
  332. s3c_pm_check_store();
  333. ret = call_firmware_op(suspend);
  334. if (ret == -ENOSYS)
  335. ret = cpu_suspend(0, pm_data->cpu_suspend);
  336. if (ret)
  337. return ret;
  338. if (pm_data->pm_resume_prepare)
  339. pm_data->pm_resume_prepare();
  340. s3c_pm_restore_uarts();
  341. S3C_PMDBG("%s: wakeup stat: %08x\n", __func__,
  342. pmu_raw_readl(S5P_WAKEUP_STAT));
  343. s3c_pm_check_restore();
  344. S3C_PMDBG("%s: resuming the system...\n", __func__);
  345. return 0;
  346. }
  347. static int exynos_suspend_prepare(void)
  348. {
  349. int ret;
  350. /*
  351. * REVISIT: It would be better if struct platform_suspend_ops
  352. * .prepare handler get the suspend_state_t as a parameter to
  353. * avoid hard-coding the suspend to mem state. It's safe to do
  354. * it now only because the suspend_valid_only_mem function is
  355. * used as the .valid callback used to check if a given state
  356. * is supported by the platform anyways.
  357. */
  358. ret = regulator_suspend_prepare(PM_SUSPEND_MEM);
  359. if (ret) {
  360. pr_err("Failed to prepare regulators for suspend (%d)\n", ret);
  361. return ret;
  362. }
  363. s3c_pm_check_prepare();
  364. return 0;
  365. }
  366. static void exynos_suspend_finish(void)
  367. {
  368. int ret;
  369. s3c_pm_check_cleanup();
  370. ret = regulator_suspend_finish();
  371. if (ret)
  372. pr_warn("Failed to resume regulators from suspend (%d)\n", ret);
  373. }
  374. static const struct platform_suspend_ops exynos_suspend_ops = {
  375. .enter = exynos_suspend_enter,
  376. .prepare = exynos_suspend_prepare,
  377. .finish = exynos_suspend_finish,
  378. .valid = suspend_valid_only_mem,
  379. };
  380. static const struct exynos_pm_data exynos4_pm_data = {
  381. .wkup_irq = exynos4_wkup_irq,
  382. .wake_disable_mask = ((0xFF << 8) | (0x1F << 1)),
  383. .release_ret_regs = exynos_release_ret_regs,
  384. .pm_suspend = exynos_pm_suspend,
  385. .pm_resume = exynos_pm_resume,
  386. .pm_prepare = exynos_pm_prepare,
  387. .cpu_suspend = exynos_cpu_suspend,
  388. };
  389. static const struct exynos_pm_data exynos5250_pm_data = {
  390. .wkup_irq = exynos5250_wkup_irq,
  391. .wake_disable_mask = ((0xFF << 8) | (0x1F << 1)),
  392. .release_ret_regs = exynos_release_ret_regs,
  393. .extra_save = exynos5_sys_save,
  394. .num_extra_save = ARRAY_SIZE(exynos5_sys_save),
  395. .pm_suspend = exynos_pm_suspend,
  396. .pm_resume = exynos_pm_resume,
  397. .pm_prepare = exynos_pm_prepare,
  398. .cpu_suspend = exynos_cpu_suspend,
  399. };
  400. static struct exynos_pm_data exynos5420_pm_data = {
  401. .wkup_irq = exynos5250_wkup_irq,
  402. .wake_disable_mask = (0x7F << 7) | (0x1F << 1),
  403. .release_ret_regs = exynos5420_release_ret_regs,
  404. .pm_resume_prepare = exynos5420_prepare_pm_resume,
  405. .pm_resume = exynos5420_pm_resume,
  406. .pm_suspend = exynos5420_pm_suspend,
  407. .pm_prepare = exynos5420_pm_prepare,
  408. .cpu_suspend = exynos5420_cpu_suspend,
  409. };
  410. static struct of_device_id exynos_pmu_of_device_ids[] = {
  411. {
  412. .compatible = "samsung,exynos4210-pmu",
  413. .data = &exynos4_pm_data,
  414. }, {
  415. .compatible = "samsung,exynos4212-pmu",
  416. .data = &exynos4_pm_data,
  417. }, {
  418. .compatible = "samsung,exynos4412-pmu",
  419. .data = &exynos4_pm_data,
  420. }, {
  421. .compatible = "samsung,exynos5250-pmu",
  422. .data = &exynos5250_pm_data,
  423. }, {
  424. .compatible = "samsung,exynos5420-pmu",
  425. .data = &exynos5420_pm_data,
  426. },
  427. { /*sentinel*/ },
  428. };
  429. static struct syscore_ops exynos_pm_syscore_ops;
  430. void __init exynos_pm_init(void)
  431. {
  432. const struct of_device_id *match;
  433. u32 tmp;
  434. of_find_matching_node_and_match(NULL, exynos_pmu_of_device_ids, &match);
  435. if (!match) {
  436. pr_err("Failed to find PMU node\n");
  437. return;
  438. }
  439. pm_data = (struct exynos_pm_data *) match->data;
  440. /* Platform-specific GIC callback */
  441. gic_arch_extn.irq_set_wake = exynos_irq_set_wake;
  442. /* All wakeup disable */
  443. tmp = pmu_raw_readl(S5P_WAKEUP_MASK);
  444. tmp |= pm_data->wake_disable_mask;
  445. pmu_raw_writel(tmp, S5P_WAKEUP_MASK);
  446. exynos_pm_syscore_ops.suspend = pm_data->pm_suspend;
  447. exynos_pm_syscore_ops.resume = pm_data->pm_resume;
  448. register_syscore_ops(&exynos_pm_syscore_ops);
  449. suspend_set_ops(&exynos_suspend_ops);
  450. }