cpuidle34xx.c 9.5 KB

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