cpuidle34xx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 "powerdomain.h"
  29. #include "clockdomain.h"
  30. #include "pm.h"
  31. #include "control.h"
  32. #include "common.h"
  33. /* Mach specific information to be recorded in the C-state driver_data */
  34. struct omap3_idle_statedata {
  35. u32 mpu_state;
  36. u32 core_state;
  37. };
  38. static struct powerdomain *mpu_pd, *core_pd, *per_pd, *cam_pd;
  39. static struct omap3_idle_statedata omap3_idle_data[] = {
  40. {
  41. .mpu_state = PWRDM_POWER_ON,
  42. .core_state = PWRDM_POWER_ON,
  43. },
  44. {
  45. .mpu_state = PWRDM_POWER_ON,
  46. .core_state = PWRDM_POWER_ON,
  47. },
  48. {
  49. .mpu_state = PWRDM_POWER_RET,
  50. .core_state = PWRDM_POWER_ON,
  51. },
  52. {
  53. .mpu_state = PWRDM_POWER_OFF,
  54. .core_state = PWRDM_POWER_ON,
  55. },
  56. {
  57. .mpu_state = PWRDM_POWER_RET,
  58. .core_state = PWRDM_POWER_RET,
  59. },
  60. {
  61. .mpu_state = PWRDM_POWER_OFF,
  62. .core_state = PWRDM_POWER_RET,
  63. },
  64. {
  65. .mpu_state = PWRDM_POWER_OFF,
  66. .core_state = PWRDM_POWER_OFF,
  67. },
  68. };
  69. /* Private functions */
  70. static int __omap3_enter_idle(struct cpuidle_device *dev,
  71. struct cpuidle_driver *drv,
  72. int index)
  73. {
  74. struct omap3_idle_statedata *cx = &omap3_idle_data[index];
  75. u32 mpu_state = cx->mpu_state, core_state = cx->core_state;
  76. local_fiq_disable();
  77. pwrdm_set_next_pwrst(mpu_pd, mpu_state);
  78. pwrdm_set_next_pwrst(core_pd, core_state);
  79. if (omap_irq_pending() || need_resched())
  80. goto return_sleep_time;
  81. /* Deny idle for C1 */
  82. if (index == 0) {
  83. clkdm_deny_idle(mpu_pd->pwrdm_clkdms[0]);
  84. clkdm_deny_idle(core_pd->pwrdm_clkdms[0]);
  85. }
  86. /*
  87. * Call idle CPU PM enter notifier chain so that
  88. * VFP context is saved.
  89. */
  90. if (mpu_state == PWRDM_POWER_OFF)
  91. cpu_pm_enter();
  92. /* Execute ARM wfi */
  93. omap_sram_idle();
  94. /*
  95. * Call idle CPU PM enter notifier chain to restore
  96. * VFP context.
  97. */
  98. if (pwrdm_read_prev_pwrst(mpu_pd) == PWRDM_POWER_OFF)
  99. cpu_pm_exit();
  100. /* Re-allow idle for C1 */
  101. if (index == 0) {
  102. clkdm_allow_idle(mpu_pd->pwrdm_clkdms[0]);
  103. clkdm_allow_idle(core_pd->pwrdm_clkdms[0]);
  104. }
  105. return_sleep_time:
  106. local_fiq_enable();
  107. return index;
  108. }
  109. /**
  110. * omap3_enter_idle - Programs OMAP3 to enter the specified state
  111. * @dev: cpuidle device
  112. * @drv: cpuidle driver
  113. * @index: the index of state to be entered
  114. *
  115. * Called from the CPUidle framework to program the device to the
  116. * specified target state selected by the governor.
  117. */
  118. static inline int omap3_enter_idle(struct cpuidle_device *dev,
  119. struct cpuidle_driver *drv,
  120. int index)
  121. {
  122. return cpuidle_wrap_enter(dev, drv, index, __omap3_enter_idle);
  123. }
  124. /**
  125. * next_valid_state - Find next valid C-state
  126. * @dev: cpuidle device
  127. * @drv: cpuidle driver
  128. * @index: Index of currently selected c-state
  129. *
  130. * If the state corresponding to index is valid, index is returned back
  131. * to the caller. Else, this function searches for a lower c-state which is
  132. * still valid (as defined in omap3_power_states[]) and returns its index.
  133. *
  134. * A state is valid if the 'valid' field is enabled and
  135. * if it satisfies the enable_off_mode condition.
  136. */
  137. static int next_valid_state(struct cpuidle_device *dev,
  138. struct cpuidle_driver *drv, int index)
  139. {
  140. struct omap3_idle_statedata *cx = &omap3_idle_data[index];
  141. u32 mpu_deepest_state = PWRDM_POWER_RET;
  142. u32 core_deepest_state = PWRDM_POWER_RET;
  143. int idx;
  144. int next_index = 0; /* C1 is the default value */
  145. if (enable_off_mode) {
  146. mpu_deepest_state = PWRDM_POWER_OFF;
  147. /*
  148. * Erratum i583: valable for ES rev < Es1.2 on 3630.
  149. * CORE OFF mode is not supported in a stable form, restrict
  150. * instead the CORE state to RET.
  151. */
  152. if (!IS_PM34XX_ERRATUM(PM_SDRC_WAKEUP_ERRATUM_i583))
  153. core_deepest_state = PWRDM_POWER_OFF;
  154. }
  155. /* Check if current state is valid */
  156. if ((cx->mpu_state >= mpu_deepest_state) &&
  157. (cx->core_state >= core_deepest_state))
  158. return index;
  159. /*
  160. * Drop to next valid state.
  161. * Start search from the next (lower) state.
  162. */
  163. for (idx = index - 1; idx >= 0; idx--) {
  164. cx = &omap3_idle_data[idx];
  165. if ((cx->mpu_state >= mpu_deepest_state) &&
  166. (cx->core_state >= core_deepest_state)) {
  167. next_index = idx;
  168. break;
  169. }
  170. }
  171. return next_index;
  172. }
  173. /**
  174. * omap3_enter_idle_bm - Checks for any bus activity
  175. * @dev: cpuidle device
  176. * @drv: cpuidle driver
  177. * @index: array index of target state to be programmed
  178. *
  179. * This function checks for any pending activity and then programs
  180. * the device to the specified or a safer state.
  181. */
  182. static int omap3_enter_idle_bm(struct cpuidle_device *dev,
  183. struct cpuidle_driver *drv,
  184. int index)
  185. {
  186. int new_state_idx;
  187. u32 core_next_state, per_next_state = 0, per_saved_state = 0;
  188. struct omap3_idle_statedata *cx;
  189. int ret;
  190. /*
  191. * Use only C1 if CAM is active.
  192. * CAM does not have wakeup capability in OMAP3.
  193. */
  194. if (pwrdm_read_pwrst(cam_pd) == PWRDM_POWER_ON)
  195. new_state_idx = drv->safe_state_index;
  196. else
  197. new_state_idx = next_valid_state(dev, drv, index);
  198. /*
  199. * FIXME: we currently manage device-specific idle states
  200. * for PER and CORE in combination with CPU-specific
  201. * idle states. This is wrong, and device-specific
  202. * idle management needs to be separated out into
  203. * its own code.
  204. */
  205. /* Program PER state */
  206. cx = &omap3_idle_data[new_state_idx];
  207. core_next_state = cx->core_state;
  208. per_next_state = per_saved_state = pwrdm_read_next_pwrst(per_pd);
  209. if (new_state_idx == 0) {
  210. /* In C1 do not allow PER state lower than CORE state */
  211. if (per_next_state < core_next_state)
  212. per_next_state = core_next_state;
  213. } else {
  214. /*
  215. * Prevent PER OFF if CORE is not in RETention or OFF as this
  216. * would disable PER wakeups completely.
  217. */
  218. if ((per_next_state == PWRDM_POWER_OFF) &&
  219. (core_next_state > PWRDM_POWER_RET))
  220. per_next_state = PWRDM_POWER_RET;
  221. }
  222. /* Are we changing PER target state? */
  223. if (per_next_state != per_saved_state)
  224. pwrdm_set_next_pwrst(per_pd, per_next_state);
  225. ret = omap3_enter_idle(dev, drv, new_state_idx);
  226. /* Restore original PER state if it was modified */
  227. if (per_next_state != per_saved_state)
  228. pwrdm_set_next_pwrst(per_pd, per_saved_state);
  229. return ret;
  230. }
  231. static DEFINE_PER_CPU(struct cpuidle_device, omap3_idle_dev);
  232. static struct cpuidle_driver omap3_idle_driver = {
  233. .name = "omap3_idle",
  234. .owner = THIS_MODULE,
  235. .states = {
  236. {
  237. .enter = omap3_enter_idle_bm,
  238. .exit_latency = 2 + 2,
  239. .target_residency = 5,
  240. .flags = CPUIDLE_FLAG_TIME_VALID,
  241. .name = "C1",
  242. .desc = "MPU ON + CORE ON",
  243. },
  244. {
  245. .enter = omap3_enter_idle_bm,
  246. .exit_latency = 10 + 10,
  247. .target_residency = 30,
  248. .flags = CPUIDLE_FLAG_TIME_VALID,
  249. .name = "C2",
  250. .desc = "MPU ON + CORE ON",
  251. },
  252. {
  253. .enter = omap3_enter_idle_bm,
  254. .exit_latency = 50 + 50,
  255. .target_residency = 300,
  256. .flags = CPUIDLE_FLAG_TIME_VALID,
  257. .name = "C3",
  258. .desc = "MPU RET + CORE ON",
  259. },
  260. {
  261. .enter = omap3_enter_idle_bm,
  262. .exit_latency = 1500 + 1800,
  263. .target_residency = 4000,
  264. .flags = CPUIDLE_FLAG_TIME_VALID,
  265. .name = "C4",
  266. .desc = "MPU OFF + CORE ON",
  267. },
  268. {
  269. .enter = omap3_enter_idle_bm,
  270. .exit_latency = 2500 + 7500,
  271. .target_residency = 12000,
  272. .flags = CPUIDLE_FLAG_TIME_VALID,
  273. .name = "C5",
  274. .desc = "MPU RET + CORE RET",
  275. },
  276. {
  277. .enter = omap3_enter_idle_bm,
  278. .exit_latency = 3000 + 8500,
  279. .target_residency = 15000,
  280. .flags = CPUIDLE_FLAG_TIME_VALID,
  281. .name = "C6",
  282. .desc = "MPU OFF + CORE RET",
  283. },
  284. {
  285. .enter = omap3_enter_idle_bm,
  286. .exit_latency = 10000 + 30000,
  287. .target_residency = 30000,
  288. .flags = CPUIDLE_FLAG_TIME_VALID,
  289. .name = "C7",
  290. .desc = "MPU OFF + CORE OFF",
  291. },
  292. },
  293. .state_count = ARRAY_SIZE(omap3_idle_data),
  294. .safe_state_index = 0,
  295. };
  296. /* Public functions */
  297. /**
  298. * omap3_idle_init - Init routine for OMAP3 idle
  299. *
  300. * Registers the OMAP3 specific cpuidle driver to the cpuidle
  301. * framework with the valid set of states.
  302. */
  303. int __init omap3_idle_init(void)
  304. {
  305. struct cpuidle_device *dev;
  306. mpu_pd = pwrdm_lookup("mpu_pwrdm");
  307. core_pd = pwrdm_lookup("core_pwrdm");
  308. per_pd = pwrdm_lookup("per_pwrdm");
  309. cam_pd = pwrdm_lookup("cam_pwrdm");
  310. if (!mpu_pd || !core_pd || !per_pd || !cam_pd)
  311. return -ENODEV;
  312. cpuidle_register_driver(&omap3_idle_driver);
  313. dev = &per_cpu(omap3_idle_dev, smp_processor_id());
  314. dev->cpu = 0;
  315. if (cpuidle_register_device(dev)) {
  316. printk(KERN_ERR "%s: CPUidle register device failed\n",
  317. __func__);
  318. return -EIO;
  319. }
  320. return 0;
  321. }