cpuidle34xx.c 9.0 KB

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