cpuidle.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* linux/arch/arm/mach-exynos4/cpuidle.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/cpuidle.h>
  13. #include <linux/cpu_pm.h>
  14. #include <linux/io.h>
  15. #include <linux/export.h>
  16. #include <linux/time.h>
  17. #include <linux/platform_device.h>
  18. #include <asm/proc-fns.h>
  19. #include <asm/smp_scu.h>
  20. #include <asm/suspend.h>
  21. #include <asm/unified.h>
  22. #include <asm/cpuidle.h>
  23. #include <mach/regs-clock.h>
  24. #include <mach/regs-pmu.h>
  25. #include <plat/cpu.h>
  26. #include <plat/pm.h>
  27. #include "common.h"
  28. #define REG_DIRECTGO_ADDR (samsung_rev() == EXYNOS4210_REV_1_1 ? \
  29. S5P_INFORM7 : (samsung_rev() == EXYNOS4210_REV_1_0 ? \
  30. (S5P_VA_SYSRAM + 0x24) : S5P_INFORM0))
  31. #define REG_DIRECTGO_FLAG (samsung_rev() == EXYNOS4210_REV_1_1 ? \
  32. S5P_INFORM6 : (samsung_rev() == EXYNOS4210_REV_1_0 ? \
  33. (S5P_VA_SYSRAM + 0x20) : S5P_INFORM1))
  34. #define S5P_CHECK_AFTR 0xFCBA0D10
  35. static int exynos4_enter_lowpower(struct cpuidle_device *dev,
  36. struct cpuidle_driver *drv,
  37. int index);
  38. static DEFINE_PER_CPU(struct cpuidle_device, exynos4_cpuidle_device);
  39. static struct cpuidle_driver exynos4_idle_driver = {
  40. .name = "exynos4_idle",
  41. .owner = THIS_MODULE,
  42. .states = {
  43. [0] = ARM_CPUIDLE_WFI_STATE,
  44. [1] = {
  45. .enter = exynos4_enter_lowpower,
  46. .exit_latency = 300,
  47. .target_residency = 100000,
  48. .flags = CPUIDLE_FLAG_TIME_VALID,
  49. .name = "C1",
  50. .desc = "ARM power down",
  51. },
  52. },
  53. .state_count = 2,
  54. .safe_state_index = 0,
  55. };
  56. /* Ext-GIC nIRQ/nFIQ is the only wakeup source in AFTR */
  57. static void exynos4_set_wakeupmask(void)
  58. {
  59. __raw_writel(0x0000ff3e, S5P_WAKEUP_MASK);
  60. }
  61. static unsigned int g_pwr_ctrl, g_diag_reg;
  62. static void save_cpu_arch_register(void)
  63. {
  64. /*read power control register*/
  65. asm("mrc p15, 0, %0, c15, c0, 0" : "=r"(g_pwr_ctrl) : : "cc");
  66. /*read diagnostic register*/
  67. asm("mrc p15, 0, %0, c15, c0, 1" : "=r"(g_diag_reg) : : "cc");
  68. return;
  69. }
  70. static void restore_cpu_arch_register(void)
  71. {
  72. /*write power control register*/
  73. asm("mcr p15, 0, %0, c15, c0, 0" : : "r"(g_pwr_ctrl) : "cc");
  74. /*write diagnostic register*/
  75. asm("mcr p15, 0, %0, c15, c0, 1" : : "r"(g_diag_reg) : "cc");
  76. return;
  77. }
  78. static int idle_finisher(unsigned long flags)
  79. {
  80. cpu_do_idle();
  81. return 1;
  82. }
  83. static int exynos4_enter_core0_aftr(struct cpuidle_device *dev,
  84. struct cpuidle_driver *drv,
  85. int index)
  86. {
  87. unsigned long tmp;
  88. exynos4_set_wakeupmask();
  89. /* Set value of power down register for aftr mode */
  90. exynos_sys_powerdown_conf(SYS_AFTR);
  91. __raw_writel(virt_to_phys(s3c_cpu_resume), REG_DIRECTGO_ADDR);
  92. __raw_writel(S5P_CHECK_AFTR, REG_DIRECTGO_FLAG);
  93. save_cpu_arch_register();
  94. /* Setting Central Sequence Register for power down mode */
  95. tmp = __raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
  96. tmp &= ~S5P_CENTRAL_LOWPWR_CFG;
  97. __raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
  98. cpu_pm_enter();
  99. cpu_suspend(0, idle_finisher);
  100. #ifdef CONFIG_SMP
  101. if (!soc_is_exynos5250())
  102. scu_enable(S5P_VA_SCU);
  103. #endif
  104. cpu_pm_exit();
  105. restore_cpu_arch_register();
  106. /*
  107. * If PMU failed while entering sleep mode, WFI will be
  108. * ignored by PMU and then exiting cpu_do_idle().
  109. * S5P_CENTRAL_LOWPWR_CFG bit will not be set automatically
  110. * in this situation.
  111. */
  112. tmp = __raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
  113. if (!(tmp & S5P_CENTRAL_LOWPWR_CFG)) {
  114. tmp |= S5P_CENTRAL_LOWPWR_CFG;
  115. __raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
  116. }
  117. /* Clear wakeup state register */
  118. __raw_writel(0x0, S5P_WAKEUP_STAT);
  119. return index;
  120. }
  121. static int exynos4_enter_lowpower(struct cpuidle_device *dev,
  122. struct cpuidle_driver *drv,
  123. int index)
  124. {
  125. int new_index = index;
  126. /* This mode only can be entered when other core's are offline */
  127. if (num_online_cpus() > 1)
  128. new_index = drv->safe_state_index;
  129. if (new_index == 0)
  130. return arm_cpuidle_simple_enter(dev, drv, new_index);
  131. else
  132. return exynos4_enter_core0_aftr(dev, drv, new_index);
  133. }
  134. static void __init exynos5_core_down_clk(void)
  135. {
  136. unsigned int tmp;
  137. /*
  138. * Enable arm clock down (in idle) and set arm divider
  139. * ratios in WFI/WFE state.
  140. */
  141. tmp = PWR_CTRL1_CORE2_DOWN_RATIO | \
  142. PWR_CTRL1_CORE1_DOWN_RATIO | \
  143. PWR_CTRL1_DIV2_DOWN_EN | \
  144. PWR_CTRL1_DIV1_DOWN_EN | \
  145. PWR_CTRL1_USE_CORE1_WFE | \
  146. PWR_CTRL1_USE_CORE0_WFE | \
  147. PWR_CTRL1_USE_CORE1_WFI | \
  148. PWR_CTRL1_USE_CORE0_WFI;
  149. __raw_writel(tmp, EXYNOS5_PWR_CTRL1);
  150. /*
  151. * Enable arm clock up (on exiting idle). Set arm divider
  152. * ratios when not in idle along with the standby duration
  153. * ratios.
  154. */
  155. tmp = PWR_CTRL2_DIV2_UP_EN | \
  156. PWR_CTRL2_DIV1_UP_EN | \
  157. PWR_CTRL2_DUR_STANDBY2_VAL | \
  158. PWR_CTRL2_DUR_STANDBY1_VAL | \
  159. PWR_CTRL2_CORE2_UP_RATIO | \
  160. PWR_CTRL2_CORE1_UP_RATIO;
  161. __raw_writel(tmp, EXYNOS5_PWR_CTRL2);
  162. }
  163. static int exynos_cpuidle_probe(struct platform_device *pdev)
  164. {
  165. int cpu_id, ret;
  166. struct cpuidle_device *device;
  167. if (soc_is_exynos5250())
  168. exynos5_core_down_clk();
  169. if (soc_is_exynos5440())
  170. exynos4_idle_driver.state_count = 1;
  171. ret = cpuidle_register_driver(&exynos4_idle_driver);
  172. if (ret) {
  173. dev_err(&pdev->dev, "failed to register cpuidle driver\n");
  174. return ret;
  175. }
  176. for_each_online_cpu(cpu_id) {
  177. device = &per_cpu(exynos4_cpuidle_device, cpu_id);
  178. device->cpu = cpu_id;
  179. /* Support IDLE only */
  180. if (cpu_id != 0)
  181. device->state_count = 1;
  182. ret = cpuidle_register_device(device);
  183. if (ret) {
  184. dev_err(&pdev->dev, "failed to register cpuidle device\n");
  185. return ret;
  186. }
  187. }
  188. return 0;
  189. }
  190. static struct platform_driver exynos_cpuidle_driver = {
  191. .probe = exynos_cpuidle_probe,
  192. .driver = {
  193. .name = "exynos_cpuidle",
  194. .owner = THIS_MODULE,
  195. },
  196. };
  197. module_platform_driver(exynos_cpuidle_driver);