pm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) 2011-2014 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * EXYNOS - Power Management 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/cpu_pm.h>
  18. #include <linux/io.h>
  19. #include <linux/err.h>
  20. #include <asm/firmware.h>
  21. #include <asm/smp_scu.h>
  22. #include <asm/suspend.h>
  23. #include <mach/map.h>
  24. #include <plat/pm-common.h>
  25. #include "common.h"
  26. #include "exynos-pmu.h"
  27. #include "regs-pmu.h"
  28. static inline void __iomem *exynos_boot_vector_addr(void)
  29. {
  30. if (samsung_rev() == EXYNOS4210_REV_1_1)
  31. return pmu_base_addr + S5P_INFORM7;
  32. else if (samsung_rev() == EXYNOS4210_REV_1_0)
  33. return sysram_base_addr + 0x24;
  34. return pmu_base_addr + S5P_INFORM0;
  35. }
  36. static inline void __iomem *exynos_boot_vector_flag(void)
  37. {
  38. if (samsung_rev() == EXYNOS4210_REV_1_1)
  39. return pmu_base_addr + S5P_INFORM6;
  40. else if (samsung_rev() == EXYNOS4210_REV_1_0)
  41. return sysram_base_addr + 0x20;
  42. return pmu_base_addr + S5P_INFORM1;
  43. }
  44. #define S5P_CHECK_AFTR 0xFCBA0D10
  45. /* For Cortex-A9 Diagnostic and Power control register */
  46. static unsigned int save_arm_register[2];
  47. void exynos_cpu_save_register(void)
  48. {
  49. unsigned long tmp;
  50. /* Save Power control register */
  51. asm ("mrc p15, 0, %0, c15, c0, 0"
  52. : "=r" (tmp) : : "cc");
  53. save_arm_register[0] = tmp;
  54. /* Save Diagnostic register */
  55. asm ("mrc p15, 0, %0, c15, c0, 1"
  56. : "=r" (tmp) : : "cc");
  57. save_arm_register[1] = tmp;
  58. }
  59. void exynos_cpu_restore_register(void)
  60. {
  61. unsigned long tmp;
  62. /* Restore Power control register */
  63. tmp = save_arm_register[0];
  64. asm volatile ("mcr p15, 0, %0, c15, c0, 0"
  65. : : "r" (tmp)
  66. : "cc");
  67. /* Restore Diagnostic register */
  68. tmp = save_arm_register[1];
  69. asm volatile ("mcr p15, 0, %0, c15, c0, 1"
  70. : : "r" (tmp)
  71. : "cc");
  72. }
  73. void exynos_pm_central_suspend(void)
  74. {
  75. unsigned long tmp;
  76. /* Setting Central Sequence Register for power down mode */
  77. tmp = pmu_raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
  78. tmp &= ~S5P_CENTRAL_LOWPWR_CFG;
  79. pmu_raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
  80. }
  81. int exynos_pm_central_resume(void)
  82. {
  83. unsigned long tmp;
  84. /*
  85. * If PMU failed while entering sleep mode, WFI will be
  86. * ignored by PMU and then exiting cpu_do_idle().
  87. * S5P_CENTRAL_LOWPWR_CFG bit will not be set automatically
  88. * in this situation.
  89. */
  90. tmp = pmu_raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
  91. if (!(tmp & S5P_CENTRAL_LOWPWR_CFG)) {
  92. tmp |= S5P_CENTRAL_LOWPWR_CFG;
  93. pmu_raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
  94. /* clear the wakeup state register */
  95. pmu_raw_writel(0x0, S5P_WAKEUP_STAT);
  96. /* No need to perform below restore code */
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. /* Ext-GIC nIRQ/nFIQ is the only wakeup source in AFTR */
  102. static void exynos_set_wakeupmask(long mask)
  103. {
  104. pmu_raw_writel(mask, S5P_WAKEUP_MASK);
  105. if (soc_is_exynos3250())
  106. pmu_raw_writel(0x0, S5P_WAKEUP_MASK2);
  107. }
  108. static void exynos_cpu_set_boot_vector(long flags)
  109. {
  110. __raw_writel(virt_to_phys(exynos_cpu_resume),
  111. exynos_boot_vector_addr());
  112. __raw_writel(flags, exynos_boot_vector_flag());
  113. }
  114. static int exynos_aftr_finisher(unsigned long flags)
  115. {
  116. int ret;
  117. exynos_set_wakeupmask(soc_is_exynos3250() ? 0x40003ffe : 0x0000ff3e);
  118. /* Set value of power down register for aftr mode */
  119. exynos_sys_powerdown_conf(SYS_AFTR);
  120. ret = call_firmware_op(do_idle, FW_DO_IDLE_AFTR);
  121. if (ret == -ENOSYS) {
  122. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  123. exynos_cpu_save_register();
  124. exynos_cpu_set_boot_vector(S5P_CHECK_AFTR);
  125. cpu_do_idle();
  126. }
  127. return 1;
  128. }
  129. void exynos_enter_aftr(void)
  130. {
  131. unsigned int cpuid = smp_processor_id();
  132. cpu_pm_enter();
  133. if (soc_is_exynos3250())
  134. exynos_set_boot_flag(cpuid, C2_STATE);
  135. exynos_pm_central_suspend();
  136. if (of_machine_is_compatible("samsung,exynos4212") ||
  137. of_machine_is_compatible("samsung,exynos4412")) {
  138. /* Setting SEQ_OPTION register */
  139. pmu_raw_writel(S5P_USE_STANDBY_WFI0 | S5P_USE_STANDBY_WFE0,
  140. S5P_CENTRAL_SEQ_OPTION);
  141. }
  142. cpu_suspend(0, exynos_aftr_finisher);
  143. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) {
  144. scu_enable(S5P_VA_SCU);
  145. if (call_firmware_op(resume) == -ENOSYS)
  146. exynos_cpu_restore_register();
  147. }
  148. exynos_pm_central_resume();
  149. if (soc_is_exynos3250())
  150. exynos_clear_boot_flag(cpuid, C2_STATE);
  151. cpu_pm_exit();
  152. }
  153. #if defined(CONFIG_SMP) && defined(CONFIG_ARM_EXYNOS_CPUIDLE)
  154. static atomic_t cpu1_wakeup = ATOMIC_INIT(0);
  155. static int exynos_cpu0_enter_aftr(void)
  156. {
  157. int ret = -1;
  158. /*
  159. * If the other cpu is powered on, we have to power it off, because
  160. * the AFTR state won't work otherwise
  161. */
  162. if (cpu_online(1)) {
  163. /*
  164. * We reach a sync point with the coupled idle state, we know
  165. * the other cpu will power down itself or will abort the
  166. * sequence, let's wait for one of these to happen
  167. */
  168. while (exynos_cpu_power_state(1)) {
  169. /*
  170. * The other cpu may skip idle and boot back
  171. * up again
  172. */
  173. if (atomic_read(&cpu1_wakeup))
  174. goto abort;
  175. /*
  176. * The other cpu may bounce through idle and
  177. * boot back up again, getting stuck in the
  178. * boot rom code
  179. */
  180. if (__raw_readl(cpu_boot_reg_base()) == 0)
  181. goto abort;
  182. cpu_relax();
  183. }
  184. }
  185. exynos_enter_aftr();
  186. ret = 0;
  187. abort:
  188. if (cpu_online(1)) {
  189. /*
  190. * Set the boot vector to something non-zero
  191. */
  192. __raw_writel(virt_to_phys(exynos_cpu_resume),
  193. cpu_boot_reg_base());
  194. dsb();
  195. /*
  196. * Turn on cpu1 and wait for it to be on
  197. */
  198. exynos_cpu_power_up(1);
  199. while (exynos_cpu_power_state(1) != S5P_CORE_LOCAL_PWR_EN)
  200. cpu_relax();
  201. while (!atomic_read(&cpu1_wakeup)) {
  202. /*
  203. * Poke cpu1 out of the boot rom
  204. */
  205. __raw_writel(virt_to_phys(exynos_cpu_resume),
  206. cpu_boot_reg_base());
  207. arch_send_wakeup_ipi_mask(cpumask_of(1));
  208. }
  209. }
  210. return ret;
  211. }
  212. static int exynos_wfi_finisher(unsigned long flags)
  213. {
  214. cpu_do_idle();
  215. return -1;
  216. }
  217. static int exynos_cpu1_powerdown(void)
  218. {
  219. int ret = -1;
  220. /*
  221. * Idle sequence for cpu1
  222. */
  223. if (cpu_pm_enter())
  224. goto cpu1_aborted;
  225. /*
  226. * Turn off cpu 1
  227. */
  228. exynos_cpu_power_down(1);
  229. ret = cpu_suspend(0, exynos_wfi_finisher);
  230. cpu_pm_exit();
  231. cpu1_aborted:
  232. dsb();
  233. /*
  234. * Notify cpu 0 that cpu 1 is awake
  235. */
  236. atomic_set(&cpu1_wakeup, 1);
  237. return ret;
  238. }
  239. static void exynos_pre_enter_aftr(void)
  240. {
  241. __raw_writel(virt_to_phys(exynos_cpu_resume), cpu_boot_reg_base());
  242. }
  243. static void exynos_post_enter_aftr(void)
  244. {
  245. atomic_set(&cpu1_wakeup, 0);
  246. }
  247. struct cpuidle_exynos_data cpuidle_coupled_exynos_data = {
  248. .cpu0_enter_aftr = exynos_cpu0_enter_aftr,
  249. .cpu1_powerdown = exynos_cpu1_powerdown,
  250. .pre_enter_aftr = exynos_pre_enter_aftr,
  251. .post_enter_aftr = exynos_post_enter_aftr,
  252. };
  253. #endif /* CONFIG_SMP && CONFIG_ARM_EXYNOS_CPUIDLE */