cpuidle34xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. #define OMAP3_STATE_MAX OMAP3_STATE_C7
  43. struct omap3_processor_cx {
  44. u8 valid;
  45. u8 type;
  46. u32 sleep_latency;
  47. u32 wakeup_latency;
  48. u32 mpu_state;
  49. u32 core_state;
  50. u32 threshold;
  51. u32 flags;
  52. };
  53. struct omap3_processor_cx omap3_power_states[OMAP3_MAX_STATES];
  54. struct omap3_processor_cx current_cx_state;
  55. struct powerdomain *mpu_pd, *core_pd;
  56. static int omap3_idle_bm_check(void)
  57. {
  58. if (!omap3_can_sleep())
  59. return 1;
  60. return 0;
  61. }
  62. static int _cpuidle_allow_idle(struct powerdomain *pwrdm,
  63. struct clockdomain *clkdm)
  64. {
  65. omap2_clkdm_allow_idle(clkdm);
  66. return 0;
  67. }
  68. static int _cpuidle_deny_idle(struct powerdomain *pwrdm,
  69. struct clockdomain *clkdm)
  70. {
  71. omap2_clkdm_deny_idle(clkdm);
  72. return 0;
  73. }
  74. /**
  75. * omap3_enter_idle - Programs OMAP3 to enter the specified state
  76. * @dev: cpuidle device
  77. * @state: The target state to be programmed
  78. *
  79. * Called from the CPUidle framework to program the device to the
  80. * specified target state selected by the governor.
  81. */
  82. static int omap3_enter_idle(struct cpuidle_device *dev,
  83. struct cpuidle_state *state)
  84. {
  85. struct omap3_processor_cx *cx = cpuidle_get_statedata(state);
  86. struct timespec ts_preidle, ts_postidle, ts_idle;
  87. u32 mpu_state = cx->mpu_state, core_state = cx->core_state;
  88. current_cx_state = *cx;
  89. /* Used to keep track of the total time in idle */
  90. getnstimeofday(&ts_preidle);
  91. local_irq_disable();
  92. local_fiq_disable();
  93. pwrdm_set_next_pwrst(mpu_pd, mpu_state);
  94. pwrdm_set_next_pwrst(core_pd, core_state);
  95. if (omap_irq_pending() || need_resched())
  96. goto return_sleep_time;
  97. if (cx->type == OMAP3_STATE_C1) {
  98. pwrdm_for_each_clkdm(mpu_pd, _cpuidle_deny_idle);
  99. pwrdm_for_each_clkdm(core_pd, _cpuidle_deny_idle);
  100. }
  101. /* Execute ARM wfi */
  102. omap_sram_idle();
  103. if (cx->type == OMAP3_STATE_C1) {
  104. pwrdm_for_each_clkdm(mpu_pd, _cpuidle_allow_idle);
  105. pwrdm_for_each_clkdm(core_pd, _cpuidle_allow_idle);
  106. }
  107. return_sleep_time:
  108. getnstimeofday(&ts_postidle);
  109. ts_idle = timespec_sub(ts_postidle, ts_preidle);
  110. local_irq_enable();
  111. local_fiq_enable();
  112. return ts_idle.tv_nsec / NSEC_PER_USEC + ts_idle.tv_sec * USEC_PER_SEC;
  113. }
  114. /**
  115. * next_valid_state - Find next valid c-state
  116. * @dev: cpuidle device
  117. * @state: Currently selected c-state
  118. *
  119. * If the current state is valid, it is returned back to the caller.
  120. * Else, this function searches for a lower c-state which is still
  121. * valid (as defined in omap3_power_states[]).
  122. */
  123. static struct cpuidle_state *next_valid_state(struct cpuidle_device *dev,
  124. struct cpuidle_state *curr)
  125. {
  126. struct cpuidle_state *next = NULL;
  127. struct omap3_processor_cx *cx;
  128. cx = (struct omap3_processor_cx *)cpuidle_get_statedata(curr);
  129. /* Check if current state is valid */
  130. if (cx->valid) {
  131. return curr;
  132. } else {
  133. u8 idx = OMAP3_STATE_MAX;
  134. /*
  135. * Reach the current state starting at highest C-state
  136. */
  137. for (; idx >= OMAP3_STATE_C1; idx--) {
  138. if (&dev->states[idx] == curr) {
  139. next = &dev->states[idx];
  140. break;
  141. }
  142. }
  143. /*
  144. * Should never hit this condition.
  145. */
  146. WARN_ON(next == NULL);
  147. /*
  148. * Drop to next valid state.
  149. * Start search from the next (lower) state.
  150. */
  151. idx--;
  152. for (; idx >= OMAP3_STATE_C1; idx--) {
  153. struct omap3_processor_cx *cx;
  154. cx = cpuidle_get_statedata(&dev->states[idx]);
  155. if (cx->valid) {
  156. next = &dev->states[idx];
  157. break;
  158. }
  159. }
  160. /*
  161. * C1 and C2 are always valid.
  162. * So, no need to check for 'next==NULL' outside this loop.
  163. */
  164. }
  165. return next;
  166. }
  167. /**
  168. * omap3_enter_idle_bm - Checks for any bus activity
  169. * @dev: cpuidle device
  170. * @state: The target state to be programmed
  171. *
  172. * Used for C states with CPUIDLE_FLAG_CHECK_BM flag set. This
  173. * function checks for any pending activity and then programs the
  174. * device to the specified or a safer state.
  175. */
  176. static int omap3_enter_idle_bm(struct cpuidle_device *dev,
  177. struct cpuidle_state *state)
  178. {
  179. struct cpuidle_state *new_state = next_valid_state(dev, state);
  180. if ((state->flags & CPUIDLE_FLAG_CHECK_BM) && omap3_idle_bm_check()) {
  181. BUG_ON(!dev->safe_state);
  182. new_state = dev->safe_state;
  183. }
  184. dev->last_state = new_state;
  185. return omap3_enter_idle(dev, new_state);
  186. }
  187. DEFINE_PER_CPU(struct cpuidle_device, omap3_idle_dev);
  188. /**
  189. * omap3_cpuidle_update_states - Update the cpuidle states.
  190. *
  191. * Currently, this function toggles the validity of idle states based upon
  192. * the flag 'enable_off_mode'. When the flag is set all states are valid.
  193. * Else, states leading to OFF state set to be invalid.
  194. */
  195. void omap3_cpuidle_update_states(void)
  196. {
  197. int i;
  198. for (i = OMAP3_STATE_C1; i < OMAP3_MAX_STATES; i++) {
  199. struct omap3_processor_cx *cx = &omap3_power_states[i];
  200. if (enable_off_mode) {
  201. cx->valid = 1;
  202. } else {
  203. if ((cx->mpu_state == PWRDM_POWER_OFF) ||
  204. (cx->core_state == PWRDM_POWER_OFF))
  205. cx->valid = 0;
  206. }
  207. }
  208. }
  209. /* omap3_init_power_states - Initialises the OMAP3 specific C states.
  210. *
  211. * Below is the desciption of each C state.
  212. * C1 . MPU WFI + Core active
  213. * C2 . MPU WFI + Core inactive
  214. * C3 . MPU CSWR + Core inactive
  215. * C4 . MPU OFF + Core inactive
  216. * C5 . MPU CSWR + Core CSWR
  217. * C6 . MPU OFF + Core CSWR
  218. * C7 . MPU OFF + Core OFF
  219. */
  220. void omap_init_power_states(void)
  221. {
  222. /* C1 . MPU WFI + Core active */
  223. omap3_power_states[OMAP3_STATE_C1].valid = 1;
  224. omap3_power_states[OMAP3_STATE_C1].type = OMAP3_STATE_C1;
  225. omap3_power_states[OMAP3_STATE_C1].sleep_latency = 2;
  226. omap3_power_states[OMAP3_STATE_C1].wakeup_latency = 2;
  227. omap3_power_states[OMAP3_STATE_C1].threshold = 5;
  228. omap3_power_states[OMAP3_STATE_C1].mpu_state = PWRDM_POWER_ON;
  229. omap3_power_states[OMAP3_STATE_C1].core_state = PWRDM_POWER_ON;
  230. omap3_power_states[OMAP3_STATE_C1].flags = CPUIDLE_FLAG_TIME_VALID;
  231. /* C2 . MPU WFI + Core inactive */
  232. omap3_power_states[OMAP3_STATE_C2].valid = 1;
  233. omap3_power_states[OMAP3_STATE_C2].type = OMAP3_STATE_C2;
  234. omap3_power_states[OMAP3_STATE_C2].sleep_latency = 10;
  235. omap3_power_states[OMAP3_STATE_C2].wakeup_latency = 10;
  236. omap3_power_states[OMAP3_STATE_C2].threshold = 30;
  237. omap3_power_states[OMAP3_STATE_C2].mpu_state = PWRDM_POWER_ON;
  238. omap3_power_states[OMAP3_STATE_C2].core_state = PWRDM_POWER_ON;
  239. omap3_power_states[OMAP3_STATE_C2].flags = CPUIDLE_FLAG_TIME_VALID;
  240. /* C3 . MPU CSWR + Core inactive */
  241. omap3_power_states[OMAP3_STATE_C3].valid = 1;
  242. omap3_power_states[OMAP3_STATE_C3].type = OMAP3_STATE_C3;
  243. omap3_power_states[OMAP3_STATE_C3].sleep_latency = 50;
  244. omap3_power_states[OMAP3_STATE_C3].wakeup_latency = 50;
  245. omap3_power_states[OMAP3_STATE_C3].threshold = 300;
  246. omap3_power_states[OMAP3_STATE_C3].mpu_state = PWRDM_POWER_RET;
  247. omap3_power_states[OMAP3_STATE_C3].core_state = PWRDM_POWER_ON;
  248. omap3_power_states[OMAP3_STATE_C3].flags = CPUIDLE_FLAG_TIME_VALID |
  249. CPUIDLE_FLAG_CHECK_BM;
  250. /* C4 . MPU OFF + Core inactive */
  251. omap3_power_states[OMAP3_STATE_C4].valid = 1;
  252. omap3_power_states[OMAP3_STATE_C4].type = OMAP3_STATE_C4;
  253. omap3_power_states[OMAP3_STATE_C4].sleep_latency = 1500;
  254. omap3_power_states[OMAP3_STATE_C4].wakeup_latency = 1800;
  255. omap3_power_states[OMAP3_STATE_C4].threshold = 4000;
  256. omap3_power_states[OMAP3_STATE_C4].mpu_state = PWRDM_POWER_OFF;
  257. omap3_power_states[OMAP3_STATE_C4].core_state = PWRDM_POWER_ON;
  258. omap3_power_states[OMAP3_STATE_C4].flags = CPUIDLE_FLAG_TIME_VALID |
  259. CPUIDLE_FLAG_CHECK_BM;
  260. /* C5 . MPU CSWR + Core CSWR*/
  261. omap3_power_states[OMAP3_STATE_C5].valid = 1;
  262. omap3_power_states[OMAP3_STATE_C5].type = OMAP3_STATE_C5;
  263. omap3_power_states[OMAP3_STATE_C5].sleep_latency = 2500;
  264. omap3_power_states[OMAP3_STATE_C5].wakeup_latency = 7500;
  265. omap3_power_states[OMAP3_STATE_C5].threshold = 12000;
  266. omap3_power_states[OMAP3_STATE_C5].mpu_state = PWRDM_POWER_RET;
  267. omap3_power_states[OMAP3_STATE_C5].core_state = PWRDM_POWER_RET;
  268. omap3_power_states[OMAP3_STATE_C5].flags = CPUIDLE_FLAG_TIME_VALID |
  269. CPUIDLE_FLAG_CHECK_BM;
  270. /* C6 . MPU OFF + Core CSWR */
  271. omap3_power_states[OMAP3_STATE_C6].valid = 1;
  272. omap3_power_states[OMAP3_STATE_C6].type = OMAP3_STATE_C6;
  273. omap3_power_states[OMAP3_STATE_C6].sleep_latency = 3000;
  274. omap3_power_states[OMAP3_STATE_C6].wakeup_latency = 8500;
  275. omap3_power_states[OMAP3_STATE_C6].threshold = 15000;
  276. omap3_power_states[OMAP3_STATE_C6].mpu_state = PWRDM_POWER_OFF;
  277. omap3_power_states[OMAP3_STATE_C6].core_state = PWRDM_POWER_RET;
  278. omap3_power_states[OMAP3_STATE_C6].flags = CPUIDLE_FLAG_TIME_VALID |
  279. CPUIDLE_FLAG_CHECK_BM;
  280. /* C7 . MPU OFF + Core OFF */
  281. omap3_power_states[OMAP3_STATE_C7].valid = 1;
  282. omap3_power_states[OMAP3_STATE_C7].type = OMAP3_STATE_C7;
  283. omap3_power_states[OMAP3_STATE_C7].sleep_latency = 10000;
  284. omap3_power_states[OMAP3_STATE_C7].wakeup_latency = 30000;
  285. omap3_power_states[OMAP3_STATE_C7].threshold = 300000;
  286. omap3_power_states[OMAP3_STATE_C7].mpu_state = PWRDM_POWER_OFF;
  287. omap3_power_states[OMAP3_STATE_C7].core_state = PWRDM_POWER_OFF;
  288. omap3_power_states[OMAP3_STATE_C7].flags = CPUIDLE_FLAG_TIME_VALID |
  289. CPUIDLE_FLAG_CHECK_BM;
  290. }
  291. struct cpuidle_driver omap3_idle_driver = {
  292. .name = "omap3_idle",
  293. .owner = THIS_MODULE,
  294. };
  295. /**
  296. * omap3_idle_init - Init routine for OMAP3 idle
  297. *
  298. * Registers the OMAP3 specific cpuidle driver with the cpuidle
  299. * framework with the valid set of states.
  300. */
  301. int __init omap3_idle_init(void)
  302. {
  303. int i, count = 0;
  304. struct omap3_processor_cx *cx;
  305. struct cpuidle_state *state;
  306. struct cpuidle_device *dev;
  307. mpu_pd = pwrdm_lookup("mpu_pwrdm");
  308. core_pd = pwrdm_lookup("core_pwrdm");
  309. omap_init_power_states();
  310. cpuidle_register_driver(&omap3_idle_driver);
  311. dev = &per_cpu(omap3_idle_dev, smp_processor_id());
  312. for (i = OMAP3_STATE_C1; i < OMAP3_MAX_STATES; i++) {
  313. cx = &omap3_power_states[i];
  314. state = &dev->states[count];
  315. if (!cx->valid)
  316. continue;
  317. cpuidle_set_statedata(state, cx);
  318. state->exit_latency = cx->sleep_latency + cx->wakeup_latency;
  319. state->target_residency = cx->threshold;
  320. state->flags = cx->flags;
  321. state->enter = (state->flags & CPUIDLE_FLAG_CHECK_BM) ?
  322. omap3_enter_idle_bm : omap3_enter_idle;
  323. if (cx->type == OMAP3_STATE_C1)
  324. dev->safe_state = state;
  325. sprintf(state->name, "C%d", count+1);
  326. count++;
  327. }
  328. if (!count)
  329. return -EINVAL;
  330. dev->state_count = count;
  331. omap3_cpuidle_update_states();
  332. if (cpuidle_register_device(dev)) {
  333. printk(KERN_ERR "%s: CPUidle register device failed\n",
  334. __func__);
  335. return -EIO;
  336. }
  337. return 0;
  338. }
  339. #else
  340. int __init omap3_idle_init(void)
  341. {
  342. return 0;
  343. }
  344. #endif /* CONFIG_CPU_IDLE */