cpuidle.c 5.9 KB

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