cpuidle-tegra20.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * CPU idle driver for Tegra CPUs
  3. *
  4. * Copyright (c) 2010-2012, NVIDIA Corporation.
  5. * Copyright (c) 2011 Google, Inc.
  6. * Author: Colin Cross <ccross@android.com>
  7. * Gary King <gking@nvidia.com>
  8. *
  9. * Rework for 3.3 by Peter De Schrijver <pdeschrijver@nvidia.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/cpuidle.h>
  24. #include <linux/cpu_pm.h>
  25. #include <linux/clockchips.h>
  26. #include <linux/clk/tegra.h>
  27. #include <asm/cpuidle.h>
  28. #include <asm/proc-fns.h>
  29. #include <asm/suspend.h>
  30. #include <asm/smp_plat.h>
  31. #include "pm.h"
  32. #include "sleep.h"
  33. #include "iomap.h"
  34. #include "irq.h"
  35. #include "flowctrl.h"
  36. #ifdef CONFIG_PM_SLEEP
  37. static bool abort_flag;
  38. static atomic_t abort_barrier;
  39. static int tegra20_idle_lp2_coupled(struct cpuidle_device *dev,
  40. struct cpuidle_driver *drv,
  41. int index);
  42. #define TEGRA20_MAX_STATES 2
  43. #else
  44. #define TEGRA20_MAX_STATES 1
  45. #endif
  46. static struct cpuidle_driver tegra_idle_driver = {
  47. .name = "tegra_idle",
  48. .owner = THIS_MODULE,
  49. .states = {
  50. ARM_CPUIDLE_WFI_STATE_PWR(600),
  51. #ifdef CONFIG_PM_SLEEP
  52. {
  53. .enter = tegra20_idle_lp2_coupled,
  54. .exit_latency = 5000,
  55. .target_residency = 10000,
  56. .power_usage = 0,
  57. .flags = CPUIDLE_FLAG_TIME_VALID |
  58. CPUIDLE_FLAG_COUPLED,
  59. .name = "powered-down",
  60. .desc = "CPU power gated",
  61. },
  62. #endif
  63. },
  64. .state_count = TEGRA20_MAX_STATES,
  65. .safe_state_index = 0,
  66. };
  67. #ifdef CONFIG_PM_SLEEP
  68. #ifdef CONFIG_SMP
  69. static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
  70. static int tegra20_reset_sleeping_cpu_1(void)
  71. {
  72. int ret = 0;
  73. tegra_pen_lock();
  74. if (readl(pmc + PMC_SCRATCH41) == CPU_RESETTABLE)
  75. tegra20_cpu_shutdown(1);
  76. else
  77. ret = -EINVAL;
  78. tegra_pen_unlock();
  79. return ret;
  80. }
  81. static void tegra20_wake_cpu1_from_reset(void)
  82. {
  83. tegra_pen_lock();
  84. tegra20_cpu_clear_resettable();
  85. /* enable cpu clock on cpu */
  86. tegra_enable_cpu_clock(1);
  87. /* take the CPU out of reset */
  88. tegra_cpu_out_of_reset(1);
  89. /* unhalt the cpu */
  90. flowctrl_write_cpu_halt(1, 0);
  91. tegra_pen_unlock();
  92. }
  93. static int tegra20_reset_cpu_1(void)
  94. {
  95. if (!cpu_online(1) || !tegra20_reset_sleeping_cpu_1())
  96. return 0;
  97. tegra20_wake_cpu1_from_reset();
  98. return -EBUSY;
  99. }
  100. #else
  101. static inline void tegra20_wake_cpu1_from_reset(void)
  102. {
  103. }
  104. static inline int tegra20_reset_cpu_1(void)
  105. {
  106. return 0;
  107. }
  108. #endif
  109. static bool tegra20_cpu_cluster_power_down(struct cpuidle_device *dev,
  110. struct cpuidle_driver *drv,
  111. int index)
  112. {
  113. while (tegra20_cpu_is_resettable_soon())
  114. cpu_relax();
  115. if (tegra20_reset_cpu_1() || !tegra_cpu_rail_off_ready())
  116. return false;
  117. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
  118. tegra_idle_lp2_last();
  119. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
  120. if (cpu_online(1))
  121. tegra20_wake_cpu1_from_reset();
  122. return true;
  123. }
  124. #ifdef CONFIG_SMP
  125. static bool tegra20_idle_enter_lp2_cpu_1(struct cpuidle_device *dev,
  126. struct cpuidle_driver *drv,
  127. int index)
  128. {
  129. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
  130. cpu_suspend(0, tegra20_sleep_cpu_secondary_finish);
  131. tegra20_cpu_clear_resettable();
  132. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
  133. return true;
  134. }
  135. #else
  136. static inline bool tegra20_idle_enter_lp2_cpu_1(struct cpuidle_device *dev,
  137. struct cpuidle_driver *drv,
  138. int index)
  139. {
  140. return true;
  141. }
  142. #endif
  143. static int tegra20_idle_lp2_coupled(struct cpuidle_device *dev,
  144. struct cpuidle_driver *drv,
  145. int index)
  146. {
  147. u32 cpu = is_smp() ? cpu_logical_map(dev->cpu) : dev->cpu;
  148. bool entered_lp2 = false;
  149. if (tegra_pending_sgi())
  150. ACCESS_ONCE(abort_flag) = true;
  151. cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
  152. if (abort_flag) {
  153. cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
  154. abort_flag = false; /* clean flag for next coming */
  155. return -EINTR;
  156. }
  157. local_fiq_disable();
  158. tegra_set_cpu_in_lp2(cpu);
  159. cpu_pm_enter();
  160. if (cpu == 0)
  161. entered_lp2 = tegra20_cpu_cluster_power_down(dev, drv, index);
  162. else
  163. entered_lp2 = tegra20_idle_enter_lp2_cpu_1(dev, drv, index);
  164. cpu_pm_exit();
  165. tegra_clear_cpu_in_lp2(cpu);
  166. local_fiq_enable();
  167. smp_rmb();
  168. return entered_lp2 ? index : 0;
  169. }
  170. #endif
  171. int __init tegra20_cpuidle_init(void)
  172. {
  173. #ifdef CONFIG_PM_SLEEP
  174. tegra_tear_down_cpu = tegra20_tear_down_cpu;
  175. #endif
  176. return cpuidle_register(&tegra_idle_driver, cpu_possible_mask);
  177. }