cpuidle.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <mach/regs-pmu.h>
  22. #include <mach/pmu.h>
  23. #include <plat/cpu.h>
  24. #define REG_DIRECTGO_ADDR (samsung_rev() == EXYNOS4210_REV_1_1 ? \
  25. S5P_INFORM7 : (samsung_rev() == EXYNOS4210_REV_1_0 ? \
  26. (S5P_VA_SYSRAM + 0x24) : S5P_INFORM0))
  27. #define REG_DIRECTGO_FLAG (samsung_rev() == EXYNOS4210_REV_1_1 ? \
  28. S5P_INFORM6 : (samsung_rev() == EXYNOS4210_REV_1_0 ? \
  29. (S5P_VA_SYSRAM + 0x20) : S5P_INFORM1))
  30. #define S5P_CHECK_AFTR 0xFCBA0D10
  31. static int exynos4_enter_idle(struct cpuidle_device *dev,
  32. struct cpuidle_driver *drv,
  33. int index);
  34. static int exynos4_enter_lowpower(struct cpuidle_device *dev,
  35. struct cpuidle_driver *drv,
  36. int index);
  37. static struct cpuidle_state exynos4_cpuidle_set[] __initdata = {
  38. [0] = {
  39. .enter = exynos4_enter_idle,
  40. .exit_latency = 1,
  41. .target_residency = 100000,
  42. .flags = CPUIDLE_FLAG_TIME_VALID,
  43. .name = "C0",
  44. .desc = "ARM clock gating(WFI)",
  45. },
  46. [1] = {
  47. .enter = exynos4_enter_lowpower,
  48. .exit_latency = 300,
  49. .target_residency = 100000,
  50. .flags = CPUIDLE_FLAG_TIME_VALID,
  51. .name = "C1",
  52. .desc = "ARM power down",
  53. },
  54. };
  55. static DEFINE_PER_CPU(struct cpuidle_device, exynos4_cpuidle_device);
  56. static struct cpuidle_driver exynos4_idle_driver = {
  57. .name = "exynos4_idle",
  58. .owner = THIS_MODULE,
  59. };
  60. /* Ext-GIC nIRQ/nFIQ is the only wakeup source in AFTR */
  61. static void exynos4_set_wakeupmask(void)
  62. {
  63. __raw_writel(0x0000ff3e, S5P_WAKEUP_MASK);
  64. }
  65. static unsigned int g_pwr_ctrl, g_diag_reg;
  66. static void save_cpu_arch_register(void)
  67. {
  68. /*read power control register*/
  69. asm("mrc p15, 0, %0, c15, c0, 0" : "=r"(g_pwr_ctrl) : : "cc");
  70. /*read diagnostic register*/
  71. asm("mrc p15, 0, %0, c15, c0, 1" : "=r"(g_diag_reg) : : "cc");
  72. return;
  73. }
  74. static void restore_cpu_arch_register(void)
  75. {
  76. /*write power control register*/
  77. asm("mcr p15, 0, %0, c15, c0, 0" : : "r"(g_pwr_ctrl) : "cc");
  78. /*write diagnostic register*/
  79. asm("mcr p15, 0, %0, c15, c0, 1" : : "r"(g_diag_reg) : "cc");
  80. return;
  81. }
  82. static int idle_finisher(unsigned long flags)
  83. {
  84. cpu_do_idle();
  85. return 1;
  86. }
  87. static int exynos4_enter_core0_aftr(struct cpuidle_device *dev,
  88. struct cpuidle_driver *drv,
  89. int index)
  90. {
  91. struct timeval before, after;
  92. int idle_time;
  93. unsigned long tmp;
  94. local_irq_disable();
  95. do_gettimeofday(&before);
  96. exynos4_set_wakeupmask();
  97. /* Set value of power down register for aftr mode */
  98. exynos4_sys_powerdown_conf(SYS_AFTR);
  99. __raw_writel(virt_to_phys(s3c_cpu_resume), REG_DIRECTGO_ADDR);
  100. __raw_writel(S5P_CHECK_AFTR, REG_DIRECTGO_FLAG);
  101. save_cpu_arch_register();
  102. /* Setting Central Sequence Register for power down mode */
  103. tmp = __raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
  104. tmp &= ~S5P_CENTRAL_LOWPWR_CFG;
  105. __raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
  106. cpu_pm_enter();
  107. cpu_suspend(0, idle_finisher);
  108. #ifdef CONFIG_SMP
  109. scu_enable(S5P_VA_SCU);
  110. #endif
  111. cpu_pm_exit();
  112. restore_cpu_arch_register();
  113. /*
  114. * If PMU failed while entering sleep mode, WFI will be
  115. * ignored by PMU and then exiting cpu_do_idle().
  116. * S5P_CENTRAL_LOWPWR_CFG bit will not be set automatically
  117. * in this situation.
  118. */
  119. tmp = __raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
  120. if (!(tmp & S5P_CENTRAL_LOWPWR_CFG)) {
  121. tmp |= S5P_CENTRAL_LOWPWR_CFG;
  122. __raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
  123. }
  124. /* Clear wakeup state register */
  125. __raw_writel(0x0, S5P_WAKEUP_STAT);
  126. do_gettimeofday(&after);
  127. local_irq_enable();
  128. idle_time = (after.tv_sec - before.tv_sec) * USEC_PER_SEC +
  129. (after.tv_usec - before.tv_usec);
  130. dev->last_residency = idle_time;
  131. return index;
  132. }
  133. static int exynos4_enter_idle(struct cpuidle_device *dev,
  134. struct cpuidle_driver *drv,
  135. int index)
  136. {
  137. struct timeval before, after;
  138. int idle_time;
  139. local_irq_disable();
  140. do_gettimeofday(&before);
  141. cpu_do_idle();
  142. do_gettimeofday(&after);
  143. local_irq_enable();
  144. idle_time = (after.tv_sec - before.tv_sec) * USEC_PER_SEC +
  145. (after.tv_usec - before.tv_usec);
  146. dev->last_residency = idle_time;
  147. return index;
  148. }
  149. static int exynos4_enter_lowpower(struct cpuidle_device *dev,
  150. struct cpuidle_driver *drv,
  151. int index)
  152. {
  153. int new_index = index;
  154. /* This mode only can be entered when other core's are offline */
  155. if (num_online_cpus() > 1)
  156. new_index = drv->safe_state_index;
  157. if (new_index == 0)
  158. return exynos4_enter_idle(dev, drv, new_index);
  159. else
  160. return exynos4_enter_core0_aftr(dev, drv, new_index);
  161. }
  162. static int __init exynos4_init_cpuidle(void)
  163. {
  164. int i, max_cpuidle_state, cpu_id;
  165. struct cpuidle_device *device;
  166. struct cpuidle_driver *drv = &exynos4_idle_driver;
  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);