cpuidle.c 4.9 KB

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