cpuidle34xx.c 8.9 KB

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