cpuidle.c 5.5 KB

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