cpuidle34xx.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * linux/arch/arm/mach-omap2/cpuidle34xx.c
  3. *
  4. * OMAP3 CPU IDLE Routines
  5. *
  6. * Copyright (C) 2008 Texas Instruments, Inc.
  7. * Rajendra Nayak <rnayak@ti.com>
  8. *
  9. * Copyright (C) 2007 Texas Instruments, Inc.
  10. * Karthik Dasu <karthik-dp@ti.com>
  11. *
  12. * Copyright (C) 2006 Nokia Corporation
  13. * Tony Lindgren <tony@atomide.com>
  14. *
  15. * Copyright (C) 2005 Texas Instruments, Inc.
  16. * Richard Woodruff <r-woodruff2@ti.com>
  17. *
  18. * Based on pm.c for omap2
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License version 2 as
  22. * published by the Free Software Foundation.
  23. */
  24. #include <linux/sched.h>
  25. #include <linux/cpuidle.h>
  26. #include <linux/export.h>
  27. #include <linux/cpu_pm.h>
  28. #include <asm/cpuidle.h>
  29. #include "powerdomain.h"
  30. #include "clockdomain.h"
  31. #include "pm.h"
  32. #include "control.h"
  33. #include "common.h"
  34. /* Mach specific information to be recorded in the C-state driver_data */
  35. struct omap3_idle_statedata {
  36. u8 mpu_state;
  37. u8 core_state;
  38. u8 per_min_state;
  39. u8 flags;
  40. };
  41. static struct powerdomain *mpu_pd, *core_pd, *per_pd, *cam_pd;
  42. /*
  43. * Possible flag bits for struct omap3_idle_statedata.flags:
  44. *
  45. * OMAP_CPUIDLE_CX_NO_CLKDM_IDLE: don't allow the MPU clockdomain to go
  46. * inactive. This in turn prevents the MPU DPLL from entering autoidle
  47. * mode, so wakeup latency is greatly reduced, at the cost of additional
  48. * energy consumption. This also prevents the CORE clockdomain from
  49. * entering idle.
  50. */
  51. #define OMAP_CPUIDLE_CX_NO_CLKDM_IDLE BIT(0)
  52. /*
  53. * Prevent PER OFF if CORE is not in RETention or OFF as this would
  54. * disable PER wakeups completely.
  55. */
  56. static struct omap3_idle_statedata omap3_idle_data[] = {
  57. {
  58. .mpu_state = PWRDM_POWER_ON,
  59. .core_state = PWRDM_POWER_ON,
  60. /* In C1 do not allow PER state lower than CORE state */
  61. .per_min_state = PWRDM_POWER_ON,
  62. .flags = OMAP_CPUIDLE_CX_NO_CLKDM_IDLE,
  63. },
  64. {
  65. .mpu_state = PWRDM_POWER_ON,
  66. .core_state = PWRDM_POWER_ON,
  67. .per_min_state = PWRDM_POWER_RET,
  68. },
  69. {
  70. .mpu_state = PWRDM_POWER_RET,
  71. .core_state = PWRDM_POWER_ON,
  72. .per_min_state = PWRDM_POWER_RET,
  73. },
  74. {
  75. .mpu_state = PWRDM_POWER_OFF,
  76. .core_state = PWRDM_POWER_ON,
  77. .per_min_state = PWRDM_POWER_RET,
  78. },
  79. {
  80. .mpu_state = PWRDM_POWER_RET,
  81. .core_state = PWRDM_POWER_RET,
  82. .per_min_state = PWRDM_POWER_OFF,
  83. },
  84. {
  85. .mpu_state = PWRDM_POWER_OFF,
  86. .core_state = PWRDM_POWER_RET,
  87. .per_min_state = PWRDM_POWER_OFF,
  88. },
  89. {
  90. .mpu_state = PWRDM_POWER_OFF,
  91. .core_state = PWRDM_POWER_OFF,
  92. .per_min_state = PWRDM_POWER_OFF,
  93. },
  94. };
  95. /**
  96. * omap3_enter_idle - Programs OMAP3 to enter the specified state
  97. * @dev: cpuidle device
  98. * @drv: cpuidle driver
  99. * @index: the index of state to be entered
  100. */
  101. static int omap3_enter_idle(struct cpuidle_device *dev,
  102. struct cpuidle_driver *drv,
  103. int index)
  104. {
  105. struct omap3_idle_statedata *cx = &omap3_idle_data[index];
  106. if (omap_irq_pending() || need_resched())
  107. goto return_sleep_time;
  108. /* Deny idle for C1 */
  109. if (cx->flags & OMAP_CPUIDLE_CX_NO_CLKDM_IDLE) {
  110. clkdm_deny_idle(mpu_pd->pwrdm_clkdms[0]);
  111. } else {
  112. pwrdm_set_next_pwrst(mpu_pd, cx->mpu_state);
  113. pwrdm_set_next_pwrst(core_pd, cx->core_state);
  114. }
  115. /*
  116. * Call idle CPU PM enter notifier chain so that
  117. * VFP context is saved.
  118. */
  119. if (cx->mpu_state == PWRDM_POWER_OFF)
  120. cpu_pm_enter();
  121. /* Execute ARM wfi */
  122. omap_sram_idle();
  123. /*
  124. * Call idle CPU PM enter notifier chain to restore
  125. * VFP context.
  126. */
  127. if (cx->mpu_state == PWRDM_POWER_OFF &&
  128. pwrdm_read_prev_pwrst(mpu_pd) == PWRDM_POWER_OFF)
  129. cpu_pm_exit();
  130. /* Re-allow idle for C1 */
  131. if (cx->flags & OMAP_CPUIDLE_CX_NO_CLKDM_IDLE)
  132. clkdm_allow_idle(mpu_pd->pwrdm_clkdms[0]);
  133. return_sleep_time:
  134. return index;
  135. }
  136. /**
  137. * next_valid_state - Find next valid C-state
  138. * @dev: cpuidle device
  139. * @drv: cpuidle driver
  140. * @index: Index of currently selected c-state
  141. *
  142. * If the state corresponding to index is valid, index is returned back
  143. * to the caller. Else, this function searches for a lower c-state which is
  144. * still valid (as defined in omap3_power_states[]) and returns its index.
  145. *
  146. * A state is valid if the 'valid' field is enabled and
  147. * if it satisfies the enable_off_mode condition.
  148. */
  149. static int next_valid_state(struct cpuidle_device *dev,
  150. struct cpuidle_driver *drv, int index)
  151. {
  152. struct omap3_idle_statedata *cx = &omap3_idle_data[index];
  153. u32 mpu_deepest_state = PWRDM_POWER_RET;
  154. u32 core_deepest_state = PWRDM_POWER_RET;
  155. int idx;
  156. int next_index = 0; /* C1 is the default value */
  157. if (enable_off_mode) {
  158. mpu_deepest_state = PWRDM_POWER_OFF;
  159. /*
  160. * Erratum i583: valable for ES rev < Es1.2 on 3630.
  161. * CORE OFF mode is not supported in a stable form, restrict
  162. * instead the CORE state to RET.
  163. */
  164. if (!IS_PM34XX_ERRATUM(PM_SDRC_WAKEUP_ERRATUM_i583))
  165. core_deepest_state = PWRDM_POWER_OFF;
  166. }
  167. /* Check if current state is valid */
  168. if ((cx->mpu_state >= mpu_deepest_state) &&
  169. (cx->core_state >= core_deepest_state))
  170. return index;
  171. /*
  172. * Drop to next valid state.
  173. * Start search from the next (lower) state.
  174. */
  175. for (idx = index - 1; idx >= 0; idx--) {
  176. cx = &omap3_idle_data[idx];
  177. if ((cx->mpu_state >= mpu_deepest_state) &&
  178. (cx->core_state >= core_deepest_state)) {
  179. next_index = idx;
  180. break;
  181. }
  182. }
  183. return next_index;
  184. }
  185. /**
  186. * omap3_enter_idle_bm - Checks for any bus activity
  187. * @dev: cpuidle device
  188. * @drv: cpuidle driver
  189. * @index: array index of target state to be programmed
  190. *
  191. * This function checks for any pending activity and then programs
  192. * the device to the specified or a safer state.
  193. */
  194. static int omap3_enter_idle_bm(struct cpuidle_device *dev,
  195. struct cpuidle_driver *drv,
  196. int index)
  197. {
  198. int new_state_idx, ret;
  199. u8 per_next_state, per_saved_state;
  200. struct omap3_idle_statedata *cx;
  201. /*
  202. * Use only C1 if CAM is active.
  203. * CAM does not have wakeup capability in OMAP3.
  204. */
  205. if (pwrdm_read_pwrst(cam_pd) == PWRDM_POWER_ON)
  206. new_state_idx = drv->safe_state_index;
  207. else
  208. new_state_idx = next_valid_state(dev, drv, index);
  209. /*
  210. * FIXME: we currently manage device-specific idle states
  211. * for PER and CORE in combination with CPU-specific
  212. * idle states. This is wrong, and device-specific
  213. * idle management needs to be separated out into
  214. * its own code.
  215. */
  216. /* Program PER state */
  217. cx = &omap3_idle_data[new_state_idx];
  218. per_next_state = pwrdm_read_next_pwrst(per_pd);
  219. per_saved_state = per_next_state;
  220. if (per_next_state < cx->per_min_state) {
  221. per_next_state = cx->per_min_state;
  222. pwrdm_set_next_pwrst(per_pd, per_next_state);
  223. }
  224. ret = omap3_enter_idle(dev, drv, new_state_idx);
  225. /* Restore original PER state if it was modified */
  226. if (per_next_state != per_saved_state)
  227. pwrdm_set_next_pwrst(per_pd, per_saved_state);
  228. return ret;
  229. }
  230. static struct cpuidle_driver omap3_idle_driver = {
  231. .name = "omap3_idle",
  232. .owner = THIS_MODULE,
  233. .states = {
  234. {
  235. .enter = omap3_enter_idle_bm,
  236. .exit_latency = 2 + 2,
  237. .target_residency = 5,
  238. .flags = CPUIDLE_FLAG_TIME_VALID,
  239. .name = "C1",
  240. .desc = "MPU ON + CORE ON",
  241. },
  242. {
  243. .enter = omap3_enter_idle_bm,
  244. .exit_latency = 10 + 10,
  245. .target_residency = 30,
  246. .flags = CPUIDLE_FLAG_TIME_VALID,
  247. .name = "C2",
  248. .desc = "MPU ON + CORE ON",
  249. },
  250. {
  251. .enter = omap3_enter_idle_bm,
  252. .exit_latency = 50 + 50,
  253. .target_residency = 300,
  254. .flags = CPUIDLE_FLAG_TIME_VALID,
  255. .name = "C3",
  256. .desc = "MPU RET + CORE ON",
  257. },
  258. {
  259. .enter = omap3_enter_idle_bm,
  260. .exit_latency = 1500 + 1800,
  261. .target_residency = 4000,
  262. .flags = CPUIDLE_FLAG_TIME_VALID,
  263. .name = "C4",
  264. .desc = "MPU OFF + CORE ON",
  265. },
  266. {
  267. .enter = omap3_enter_idle_bm,
  268. .exit_latency = 2500 + 7500,
  269. .target_residency = 12000,
  270. .flags = CPUIDLE_FLAG_TIME_VALID,
  271. .name = "C5",
  272. .desc = "MPU RET + CORE RET",
  273. },
  274. {
  275. .enter = omap3_enter_idle_bm,
  276. .exit_latency = 3000 + 8500,
  277. .target_residency = 15000,
  278. .flags = CPUIDLE_FLAG_TIME_VALID,
  279. .name = "C6",
  280. .desc = "MPU OFF + CORE RET",
  281. },
  282. {
  283. .enter = omap3_enter_idle_bm,
  284. .exit_latency = 10000 + 30000,
  285. .target_residency = 30000,
  286. .flags = CPUIDLE_FLAG_TIME_VALID,
  287. .name = "C7",
  288. .desc = "MPU OFF + CORE OFF",
  289. },
  290. },
  291. .state_count = ARRAY_SIZE(omap3_idle_data),
  292. .safe_state_index = 0,
  293. };
  294. /* Public functions */
  295. /**
  296. * omap3_idle_init - Init routine for OMAP3 idle
  297. *
  298. * Registers the OMAP3 specific cpuidle driver to the cpuidle
  299. * framework with the valid set of states.
  300. */
  301. int __init omap3_idle_init(void)
  302. {
  303. mpu_pd = pwrdm_lookup("mpu_pwrdm");
  304. core_pd = pwrdm_lookup("core_pwrdm");
  305. per_pd = pwrdm_lookup("per_pwrdm");
  306. cam_pd = pwrdm_lookup("cam_pwrdm");
  307. if (!mpu_pd || !core_pd || !per_pd || !cam_pd)
  308. return -ENODEV;
  309. return cpuidle_register(&omap3_idle_driver, NULL);
  310. }