cpuidle34xx.c 9.2 KB

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