cpuidle34xx.c 8.9 KB

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