pm.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/of.h>
  20. #include <linux/soc/samsung/exynos-regs-pmu.h>
  21. #include <linux/soc/samsung/exynos-pmu.h>
  22. #include <asm/firmware.h>
  23. #include <asm/smp_scu.h>
  24. #include <asm/suspend.h>
  25. #include <asm/cacheflush.h>
  26. #include <mach/map.h>
  27. #include "common.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. writel_relaxed(virt_to_phys(exynos_cpu_resume),
  111. exynos_boot_vector_addr());
  112. writel_relaxed(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. unsigned long boot_addr;
  170. /*
  171. * The other cpu may skip idle and boot back
  172. * up again
  173. */
  174. if (atomic_read(&cpu1_wakeup))
  175. goto abort;
  176. /*
  177. * The other cpu may bounce through idle and
  178. * boot back up again, getting stuck in the
  179. * boot rom code
  180. */
  181. ret = exynos_get_boot_addr(1, &boot_addr);
  182. if (ret)
  183. goto fail;
  184. ret = -1;
  185. if (boot_addr == 0)
  186. goto abort;
  187. cpu_relax();
  188. }
  189. }
  190. exynos_enter_aftr();
  191. ret = 0;
  192. abort:
  193. if (cpu_online(1)) {
  194. unsigned long boot_addr = virt_to_phys(exynos_cpu_resume);
  195. /*
  196. * Set the boot vector to something non-zero
  197. */
  198. ret = exynos_set_boot_addr(1, boot_addr);
  199. if (ret)
  200. goto fail;
  201. dsb();
  202. /*
  203. * Turn on cpu1 and wait for it to be on
  204. */
  205. exynos_cpu_power_up(1);
  206. while (exynos_cpu_power_state(1) != S5P_CORE_LOCAL_PWR_EN)
  207. cpu_relax();
  208. if (soc_is_exynos3250()) {
  209. while (!pmu_raw_readl(S5P_PMU_SPARE2) &&
  210. !atomic_read(&cpu1_wakeup))
  211. cpu_relax();
  212. if (!atomic_read(&cpu1_wakeup))
  213. exynos_core_restart(1);
  214. }
  215. while (!atomic_read(&cpu1_wakeup)) {
  216. smp_rmb();
  217. /*
  218. * Poke cpu1 out of the boot rom
  219. */
  220. ret = exynos_set_boot_addr(1, boot_addr);
  221. if (ret)
  222. goto fail;
  223. call_firmware_op(cpu_boot, 1);
  224. if (soc_is_exynos3250())
  225. dsb_sev();
  226. else
  227. arch_send_wakeup_ipi_mask(cpumask_of(1));
  228. }
  229. }
  230. fail:
  231. return ret;
  232. }
  233. static int exynos_wfi_finisher(unsigned long flags)
  234. {
  235. if (soc_is_exynos3250())
  236. flush_cache_all();
  237. cpu_do_idle();
  238. return -1;
  239. }
  240. static int exynos_cpu1_powerdown(void)
  241. {
  242. int ret = -1;
  243. /*
  244. * Idle sequence for cpu1
  245. */
  246. if (cpu_pm_enter())
  247. goto cpu1_aborted;
  248. /*
  249. * Turn off cpu 1
  250. */
  251. exynos_cpu_power_down(1);
  252. if (soc_is_exynos3250())
  253. pmu_raw_writel(0, S5P_PMU_SPARE2);
  254. ret = cpu_suspend(0, exynos_wfi_finisher);
  255. cpu_pm_exit();
  256. cpu1_aborted:
  257. dsb();
  258. /*
  259. * Notify cpu 0 that cpu 1 is awake
  260. */
  261. atomic_set(&cpu1_wakeup, 1);
  262. return ret;
  263. }
  264. static void exynos_pre_enter_aftr(void)
  265. {
  266. unsigned long boot_addr = virt_to_phys(exynos_cpu_resume);
  267. (void)exynos_set_boot_addr(1, boot_addr);
  268. }
  269. static void exynos_post_enter_aftr(void)
  270. {
  271. atomic_set(&cpu1_wakeup, 0);
  272. }
  273. struct cpuidle_exynos_data cpuidle_coupled_exynos_data = {
  274. .cpu0_enter_aftr = exynos_cpu0_enter_aftr,
  275. .cpu1_powerdown = exynos_cpu1_powerdown,
  276. .pre_enter_aftr = exynos_pre_enter_aftr,
  277. .post_enter_aftr = exynos_post_enter_aftr,
  278. };
  279. #endif /* CONFIG_SMP && CONFIG_ARM_EXYNOS_CPUIDLE */