cpuidle34xx.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/cpuidle.h>
  25. #include <plat/prcm.h>
  26. #include <plat/irqs.h>
  27. #include <plat/powerdomain.h>
  28. #include <plat/clockdomain.h>
  29. #include <plat/control.h>
  30. #include <plat/serial.h>
  31. #include "pm.h"
  32. #ifdef CONFIG_CPU_IDLE
  33. #define OMAP3_MAX_STATES 8
  34. #define OMAP3_STATE_C1 1 /* C1 - MPU WFI + Core active */
  35. #define OMAP3_STATE_C2 2 /* C2 - MPU WFI + Core inactive */
  36. #define OMAP3_STATE_C3 3 /* C3 - MPU CSWR + Core inactive */
  37. #define OMAP3_STATE_C4 4 /* C4 - MPU OFF + Core iactive */
  38. #define OMAP3_STATE_C5 5 /* C5 - MPU RET + Core RET */
  39. #define OMAP3_STATE_C6 6 /* C6 - MPU OFF + Core RET */
  40. #define OMAP3_STATE_C7 7 /* C7 - MPU OFF + Core OFF */
  41. struct omap3_processor_cx {
  42. u8 valid;
  43. u8 type;
  44. u32 sleep_latency;
  45. u32 wakeup_latency;
  46. u32 mpu_state;
  47. u32 core_state;
  48. u32 threshold;
  49. u32 flags;
  50. };
  51. struct omap3_processor_cx omap3_power_states[OMAP3_MAX_STATES];
  52. struct omap3_processor_cx current_cx_state;
  53. struct powerdomain *mpu_pd, *core_pd;
  54. static int omap3_idle_bm_check(void)
  55. {
  56. if (!omap3_can_sleep())
  57. return 1;
  58. return 0;
  59. }
  60. static int _cpuidle_allow_idle(struct powerdomain *pwrdm,
  61. struct clockdomain *clkdm)
  62. {
  63. omap2_clkdm_allow_idle(clkdm);
  64. return 0;
  65. }
  66. static int _cpuidle_deny_idle(struct powerdomain *pwrdm,
  67. struct clockdomain *clkdm)
  68. {
  69. omap2_clkdm_deny_idle(clkdm);
  70. return 0;
  71. }
  72. /**
  73. * omap3_enter_idle - Programs OMAP3 to enter the specified state
  74. * @dev: cpuidle device
  75. * @state: The target state to be programmed
  76. *
  77. * Called from the CPUidle framework to program the device to the
  78. * specified target state selected by the governor.
  79. */
  80. static int omap3_enter_idle(struct cpuidle_device *dev,
  81. struct cpuidle_state *state)
  82. {
  83. struct omap3_processor_cx *cx = cpuidle_get_statedata(state);
  84. struct timespec ts_preidle, ts_postidle, ts_idle;
  85. u32 mpu_state = cx->mpu_state, core_state = cx->core_state;
  86. current_cx_state = *cx;
  87. /* Used to keep track of the total time in idle */
  88. getnstimeofday(&ts_preidle);
  89. local_irq_disable();
  90. local_fiq_disable();
  91. if (!enable_off_mode) {
  92. if (mpu_state < PWRDM_POWER_RET)
  93. mpu_state = PWRDM_POWER_RET;
  94. if (core_state < PWRDM_POWER_RET)
  95. core_state = PWRDM_POWER_RET;
  96. }
  97. set_pwrdm_state(mpu_pd, mpu_state);
  98. set_pwrdm_state(core_pd, core_state);
  99. if (omap_irq_pending())
  100. goto return_sleep_time;
  101. if (cx->type == OMAP3_STATE_C1) {
  102. pwrdm_for_each_clkdm(mpu_pd, _cpuidle_deny_idle);
  103. pwrdm_for_each_clkdm(core_pd, _cpuidle_deny_idle);
  104. }
  105. /* Execute ARM wfi */
  106. omap_sram_idle();
  107. if (cx->type == OMAP3_STATE_C1) {
  108. pwrdm_for_each_clkdm(mpu_pd, _cpuidle_allow_idle);
  109. pwrdm_for_each_clkdm(core_pd, _cpuidle_allow_idle);
  110. }
  111. return_sleep_time:
  112. getnstimeofday(&ts_postidle);
  113. ts_idle = timespec_sub(ts_postidle, ts_preidle);
  114. local_irq_enable();
  115. local_fiq_enable();
  116. return (u32)timespec_to_ns(&ts_idle)/1000;
  117. }
  118. /**
  119. * omap3_enter_idle_bm - Checks for any bus activity
  120. * @dev: cpuidle device
  121. * @state: The target state to be programmed
  122. *
  123. * Used for C states with CPUIDLE_FLAG_CHECK_BM flag set. This
  124. * function checks for any pending activity and then programs the
  125. * device to the specified or a safer state.
  126. */
  127. static int omap3_enter_idle_bm(struct cpuidle_device *dev,
  128. struct cpuidle_state *state)
  129. {
  130. struct cpuidle_state *new_state = state;
  131. if ((state->flags & CPUIDLE_FLAG_CHECK_BM) && omap3_idle_bm_check()) {
  132. BUG_ON(!dev->safe_state);
  133. new_state = dev->safe_state;
  134. }
  135. dev->last_state = new_state;
  136. return omap3_enter_idle(dev, new_state);
  137. }
  138. DEFINE_PER_CPU(struct cpuidle_device, omap3_idle_dev);
  139. /* omap3_init_power_states - Initialises the OMAP3 specific C states.
  140. *
  141. * Below is the desciption of each C state.
  142. * C1 . MPU WFI + Core active
  143. * C2 . MPU WFI + Core inactive
  144. * C3 . MPU CSWR + Core inactive
  145. * C4 . MPU OFF + Core inactive
  146. * C5 . MPU CSWR + Core CSWR
  147. * C6 . MPU OFF + Core CSWR
  148. * C7 . MPU OFF + Core OFF
  149. */
  150. void omap_init_power_states(void)
  151. {
  152. /* C1 . MPU WFI + Core active */
  153. omap3_power_states[OMAP3_STATE_C1].valid = 1;
  154. omap3_power_states[OMAP3_STATE_C1].type = OMAP3_STATE_C1;
  155. omap3_power_states[OMAP3_STATE_C1].sleep_latency = 2;
  156. omap3_power_states[OMAP3_STATE_C1].wakeup_latency = 2;
  157. omap3_power_states[OMAP3_STATE_C1].threshold = 5;
  158. omap3_power_states[OMAP3_STATE_C1].mpu_state = PWRDM_POWER_ON;
  159. omap3_power_states[OMAP3_STATE_C1].core_state = PWRDM_POWER_ON;
  160. omap3_power_states[OMAP3_STATE_C1].flags = CPUIDLE_FLAG_TIME_VALID;
  161. /* C2 . MPU WFI + Core inactive */
  162. omap3_power_states[OMAP3_STATE_C2].valid = 1;
  163. omap3_power_states[OMAP3_STATE_C2].type = OMAP3_STATE_C2;
  164. omap3_power_states[OMAP3_STATE_C2].sleep_latency = 10;
  165. omap3_power_states[OMAP3_STATE_C2].wakeup_latency = 10;
  166. omap3_power_states[OMAP3_STATE_C2].threshold = 30;
  167. omap3_power_states[OMAP3_STATE_C2].mpu_state = PWRDM_POWER_ON;
  168. omap3_power_states[OMAP3_STATE_C2].core_state = PWRDM_POWER_ON;
  169. omap3_power_states[OMAP3_STATE_C2].flags = CPUIDLE_FLAG_TIME_VALID;
  170. /* C3 . MPU CSWR + Core inactive */
  171. omap3_power_states[OMAP3_STATE_C3].valid = 1;
  172. omap3_power_states[OMAP3_STATE_C3].type = OMAP3_STATE_C3;
  173. omap3_power_states[OMAP3_STATE_C3].sleep_latency = 50;
  174. omap3_power_states[OMAP3_STATE_C3].wakeup_latency = 50;
  175. omap3_power_states[OMAP3_STATE_C3].threshold = 300;
  176. omap3_power_states[OMAP3_STATE_C3].mpu_state = PWRDM_POWER_RET;
  177. omap3_power_states[OMAP3_STATE_C3].core_state = PWRDM_POWER_ON;
  178. omap3_power_states[OMAP3_STATE_C3].flags = CPUIDLE_FLAG_TIME_VALID |
  179. CPUIDLE_FLAG_CHECK_BM;
  180. /* C4 . MPU OFF + Core inactive */
  181. omap3_power_states[OMAP3_STATE_C4].valid = 1;
  182. omap3_power_states[OMAP3_STATE_C4].type = OMAP3_STATE_C4;
  183. omap3_power_states[OMAP3_STATE_C4].sleep_latency = 1500;
  184. omap3_power_states[OMAP3_STATE_C4].wakeup_latency = 1800;
  185. omap3_power_states[OMAP3_STATE_C4].threshold = 4000;
  186. omap3_power_states[OMAP3_STATE_C4].mpu_state = PWRDM_POWER_OFF;
  187. omap3_power_states[OMAP3_STATE_C4].core_state = PWRDM_POWER_ON;
  188. omap3_power_states[OMAP3_STATE_C4].flags = CPUIDLE_FLAG_TIME_VALID |
  189. CPUIDLE_FLAG_CHECK_BM;
  190. /* C5 . MPU CSWR + Core CSWR*/
  191. omap3_power_states[OMAP3_STATE_C5].valid = 1;
  192. omap3_power_states[OMAP3_STATE_C5].type = OMAP3_STATE_C5;
  193. omap3_power_states[OMAP3_STATE_C5].sleep_latency = 2500;
  194. omap3_power_states[OMAP3_STATE_C5].wakeup_latency = 7500;
  195. omap3_power_states[OMAP3_STATE_C5].threshold = 12000;
  196. omap3_power_states[OMAP3_STATE_C5].mpu_state = PWRDM_POWER_RET;
  197. omap3_power_states[OMAP3_STATE_C5].core_state = PWRDM_POWER_RET;
  198. omap3_power_states[OMAP3_STATE_C5].flags = CPUIDLE_FLAG_TIME_VALID |
  199. CPUIDLE_FLAG_CHECK_BM;
  200. /* C6 . MPU OFF + Core CSWR */
  201. omap3_power_states[OMAP3_STATE_C6].valid = 1;
  202. omap3_power_states[OMAP3_STATE_C6].type = OMAP3_STATE_C6;
  203. omap3_power_states[OMAP3_STATE_C6].sleep_latency = 3000;
  204. omap3_power_states[OMAP3_STATE_C6].wakeup_latency = 8500;
  205. omap3_power_states[OMAP3_STATE_C6].threshold = 15000;
  206. omap3_power_states[OMAP3_STATE_C6].mpu_state = PWRDM_POWER_OFF;
  207. omap3_power_states[OMAP3_STATE_C6].core_state = PWRDM_POWER_RET;
  208. omap3_power_states[OMAP3_STATE_C6].flags = CPUIDLE_FLAG_TIME_VALID |
  209. CPUIDLE_FLAG_CHECK_BM;
  210. /* C7 . MPU OFF + Core OFF */
  211. omap3_power_states[OMAP3_STATE_C7].valid = 1;
  212. omap3_power_states[OMAP3_STATE_C7].type = OMAP3_STATE_C7;
  213. omap3_power_states[OMAP3_STATE_C7].sleep_latency = 10000;
  214. omap3_power_states[OMAP3_STATE_C7].wakeup_latency = 30000;
  215. omap3_power_states[OMAP3_STATE_C7].threshold = 300000;
  216. omap3_power_states[OMAP3_STATE_C7].mpu_state = PWRDM_POWER_OFF;
  217. omap3_power_states[OMAP3_STATE_C7].core_state = PWRDM_POWER_OFF;
  218. omap3_power_states[OMAP3_STATE_C7].flags = CPUIDLE_FLAG_TIME_VALID |
  219. CPUIDLE_FLAG_CHECK_BM;
  220. }
  221. struct cpuidle_driver omap3_idle_driver = {
  222. .name = "omap3_idle",
  223. .owner = THIS_MODULE,
  224. };
  225. /**
  226. * omap3_idle_init - Init routine for OMAP3 idle
  227. *
  228. * Registers the OMAP3 specific cpuidle driver with the cpuidle
  229. * framework with the valid set of states.
  230. */
  231. int __init omap3_idle_init(void)
  232. {
  233. int i, count = 0;
  234. struct omap3_processor_cx *cx;
  235. struct cpuidle_state *state;
  236. struct cpuidle_device *dev;
  237. mpu_pd = pwrdm_lookup("mpu_pwrdm");
  238. core_pd = pwrdm_lookup("core_pwrdm");
  239. omap_init_power_states();
  240. cpuidle_register_driver(&omap3_idle_driver);
  241. dev = &per_cpu(omap3_idle_dev, smp_processor_id());
  242. for (i = 1; i < OMAP3_MAX_STATES; i++) {
  243. cx = &omap3_power_states[i];
  244. state = &dev->states[count];
  245. if (!cx->valid)
  246. continue;
  247. cpuidle_set_statedata(state, cx);
  248. state->exit_latency = cx->sleep_latency + cx->wakeup_latency;
  249. state->target_residency = cx->threshold;
  250. state->flags = cx->flags;
  251. state->enter = (state->flags & CPUIDLE_FLAG_CHECK_BM) ?
  252. omap3_enter_idle_bm : omap3_enter_idle;
  253. if (cx->type == OMAP3_STATE_C1)
  254. dev->safe_state = state;
  255. sprintf(state->name, "C%d", count+1);
  256. count++;
  257. }
  258. if (!count)
  259. return -EINVAL;
  260. dev->state_count = count;
  261. if (cpuidle_register_device(dev)) {
  262. printk(KERN_ERR "%s: CPUidle register device failed\n",
  263. __func__);
  264. return -EIO;
  265. }
  266. return 0;
  267. }
  268. #else
  269. int __init omap3_idle_init(void)
  270. {
  271. return 0;
  272. }
  273. #endif /* CONFIG_CPU_IDLE */