powerdomain.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. * OMAP powerdomain control
  3. *
  4. * Copyright (C) 2007-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2007-2009 Nokia Corporation
  6. *
  7. * Written by Paul Walmsley
  8. *
  9. * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #undef DEBUG
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/delay.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/list.h>
  22. #include <linux/errno.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <asm/atomic.h>
  26. #include "cm.h"
  27. #include "cm-regbits-34xx.h"
  28. #include "cm-regbits-44xx.h"
  29. #include "prm.h"
  30. #include "prm-regbits-34xx.h"
  31. #include "prm-regbits-44xx.h"
  32. #include <plat/cpu.h>
  33. #include <plat/powerdomain.h>
  34. #include <plat/clockdomain.h>
  35. #include <plat/prcm.h>
  36. #include "pm.h"
  37. enum {
  38. PWRDM_STATE_NOW = 0,
  39. PWRDM_STATE_PREV,
  40. };
  41. /* Variable holding value of the CPU dependent PWRSTCTRL Register Offset */
  42. static u16 pwrstctrl_reg_offs;
  43. /* Variable holding value of the CPU dependent PWRSTST Register Offset */
  44. static u16 pwrstst_reg_offs;
  45. /* OMAP3 and OMAP4 specific register bit initialisations
  46. * Notice that the names here are not according to each power
  47. * domain but the bit mapping used applies to all of them
  48. */
  49. /* OMAP3 and OMAP4 Memory Onstate Masks (common across all power domains) */
  50. #define OMAP_MEM0_ONSTATE_MASK OMAP3430_SHAREDL1CACHEFLATONSTATE_MASK
  51. #define OMAP_MEM1_ONSTATE_MASK OMAP3430_L1FLATMEMONSTATE_MASK
  52. #define OMAP_MEM2_ONSTATE_MASK OMAP3430_SHAREDL2CACHEFLATONSTATE_MASK
  53. #define OMAP_MEM3_ONSTATE_MASK OMAP3430_L2FLATMEMONSTATE_MASK
  54. #define OMAP_MEM4_ONSTATE_MASK OMAP4430_OCP_NRET_BANK_ONSTATE_MASK
  55. /* OMAP3 and OMAP4 Memory Retstate Masks (common across all power domains) */
  56. #define OMAP_MEM0_RETSTATE_MASK OMAP3430_SHAREDL1CACHEFLATRETSTATE
  57. #define OMAP_MEM1_RETSTATE_MASK OMAP3430_L1FLATMEMRETSTATE
  58. #define OMAP_MEM2_RETSTATE_MASK OMAP3430_SHAREDL2CACHEFLATRETSTATE
  59. #define OMAP_MEM3_RETSTATE_MASK OMAP3430_L2FLATMEMRETSTATE
  60. #define OMAP_MEM4_RETSTATE_MASK OMAP4430_OCP_NRET_BANK_RETSTATE_MASK
  61. /* OMAP3 and OMAP4 Memory Status bits */
  62. #define OMAP_MEM0_STATEST_MASK OMAP3430_SHAREDL1CACHEFLATSTATEST_MASK
  63. #define OMAP_MEM1_STATEST_MASK OMAP3430_L1FLATMEMSTATEST_MASK
  64. #define OMAP_MEM2_STATEST_MASK OMAP3430_SHAREDL2CACHEFLATSTATEST_MASK
  65. #define OMAP_MEM3_STATEST_MASK OMAP3430_L2FLATMEMSTATEST_MASK
  66. #define OMAP_MEM4_STATEST_MASK OMAP4430_OCP_NRET_BANK_STATEST_MASK
  67. /* pwrdm_list contains all registered struct powerdomains */
  68. static LIST_HEAD(pwrdm_list);
  69. /* Private functions */
  70. static struct powerdomain *_pwrdm_lookup(const char *name)
  71. {
  72. struct powerdomain *pwrdm, *temp_pwrdm;
  73. pwrdm = NULL;
  74. list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
  75. if (!strcmp(name, temp_pwrdm->name)) {
  76. pwrdm = temp_pwrdm;
  77. break;
  78. }
  79. }
  80. return pwrdm;
  81. }
  82. /**
  83. * _pwrdm_register - register a powerdomain
  84. * @pwrdm: struct powerdomain * to register
  85. *
  86. * Adds a powerdomain to the internal powerdomain list. Returns
  87. * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
  88. * already registered by the provided name, or 0 upon success.
  89. */
  90. static int _pwrdm_register(struct powerdomain *pwrdm)
  91. {
  92. int i;
  93. if (!pwrdm)
  94. return -EINVAL;
  95. if (!omap_chip_is(pwrdm->omap_chip))
  96. return -EINVAL;
  97. if (_pwrdm_lookup(pwrdm->name))
  98. return -EEXIST;
  99. list_add(&pwrdm->node, &pwrdm_list);
  100. /* Initialize the powerdomain's state counter */
  101. for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
  102. pwrdm->state_counter[i] = 0;
  103. pwrdm_wait_transition(pwrdm);
  104. pwrdm->state = pwrdm_read_pwrst(pwrdm);
  105. pwrdm->state_counter[pwrdm->state] = 1;
  106. pr_debug("powerdomain: registered %s\n", pwrdm->name);
  107. return 0;
  108. }
  109. static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
  110. {
  111. int prev;
  112. int state;
  113. if (pwrdm == NULL)
  114. return -EINVAL;
  115. state = pwrdm_read_pwrst(pwrdm);
  116. switch (flag) {
  117. case PWRDM_STATE_NOW:
  118. prev = pwrdm->state;
  119. break;
  120. case PWRDM_STATE_PREV:
  121. prev = pwrdm_read_prev_pwrst(pwrdm);
  122. if (pwrdm->state != prev)
  123. pwrdm->state_counter[prev]++;
  124. break;
  125. default:
  126. return -EINVAL;
  127. }
  128. if (state != prev)
  129. pwrdm->state_counter[state]++;
  130. pm_dbg_update_time(pwrdm, prev);
  131. pwrdm->state = state;
  132. return 0;
  133. }
  134. static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
  135. {
  136. pwrdm_clear_all_prev_pwrst(pwrdm);
  137. _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
  138. return 0;
  139. }
  140. static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
  141. {
  142. _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
  143. return 0;
  144. }
  145. /* Public functions */
  146. /**
  147. * pwrdm_init - set up the powerdomain layer
  148. * @pwrdm_list: array of struct powerdomain pointers to register
  149. *
  150. * Loop through the array of powerdomains @pwrdm_list, registering all
  151. * that are available on the current CPU. If pwrdm_list is supplied
  152. * and not null, all of the referenced powerdomains will be
  153. * registered. No return value. XXX pwrdm_list is not really a
  154. * "list"; it is an array. Rename appropriately.
  155. */
  156. void pwrdm_init(struct powerdomain **pwrdm_list)
  157. {
  158. struct powerdomain **p = NULL;
  159. if (cpu_is_omap24xx() | cpu_is_omap34xx()) {
  160. pwrstctrl_reg_offs = OMAP2_PM_PWSTCTRL;
  161. pwrstst_reg_offs = OMAP2_PM_PWSTST;
  162. } else if (cpu_is_omap44xx()) {
  163. pwrstctrl_reg_offs = OMAP4_PM_PWSTCTRL;
  164. pwrstst_reg_offs = OMAP4_PM_PWSTST;
  165. } else {
  166. printk(KERN_ERR "Power Domain struct not supported for " \
  167. "this CPU\n");
  168. return;
  169. }
  170. if (pwrdm_list) {
  171. for (p = pwrdm_list; *p; p++)
  172. _pwrdm_register(*p);
  173. }
  174. }
  175. /**
  176. * pwrdm_lookup - look up a powerdomain by name, return a pointer
  177. * @name: name of powerdomain
  178. *
  179. * Find a registered powerdomain by its name @name. Returns a pointer
  180. * to the struct powerdomain if found, or NULL otherwise.
  181. */
  182. struct powerdomain *pwrdm_lookup(const char *name)
  183. {
  184. struct powerdomain *pwrdm;
  185. if (!name)
  186. return NULL;
  187. pwrdm = _pwrdm_lookup(name);
  188. return pwrdm;
  189. }
  190. /**
  191. * pwrdm_for_each - call function on each registered clockdomain
  192. * @fn: callback function *
  193. *
  194. * Call the supplied function @fn for each registered powerdomain.
  195. * The callback function @fn can return anything but 0 to bail out
  196. * early from the iterator. Returns the last return value of the
  197. * callback function, which should be 0 for success or anything else
  198. * to indicate failure; or -EINVAL if the function pointer is null.
  199. */
  200. int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
  201. void *user)
  202. {
  203. struct powerdomain *temp_pwrdm;
  204. int ret = 0;
  205. if (!fn)
  206. return -EINVAL;
  207. list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
  208. ret = (*fn)(temp_pwrdm, user);
  209. if (ret)
  210. break;
  211. }
  212. return ret;
  213. }
  214. /**
  215. * pwrdm_add_clkdm - add a clockdomain to a powerdomain
  216. * @pwrdm: struct powerdomain * to add the clockdomain to
  217. * @clkdm: struct clockdomain * to associate with a powerdomain
  218. *
  219. * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
  220. * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
  221. * presented with invalid pointers; -ENOMEM if memory could not be allocated;
  222. * or 0 upon success.
  223. */
  224. int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
  225. {
  226. int i;
  227. int ret = -EINVAL;
  228. if (!pwrdm || !clkdm)
  229. return -EINVAL;
  230. pr_debug("powerdomain: associating clockdomain %s with powerdomain "
  231. "%s\n", clkdm->name, pwrdm->name);
  232. for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
  233. if (!pwrdm->pwrdm_clkdms[i])
  234. break;
  235. #ifdef DEBUG
  236. if (pwrdm->pwrdm_clkdms[i] == clkdm) {
  237. ret = -EINVAL;
  238. goto pac_exit;
  239. }
  240. #endif
  241. }
  242. if (i == PWRDM_MAX_CLKDMS) {
  243. pr_debug("powerdomain: increase PWRDM_MAX_CLKDMS for "
  244. "pwrdm %s clkdm %s\n", pwrdm->name, clkdm->name);
  245. WARN_ON(1);
  246. ret = -ENOMEM;
  247. goto pac_exit;
  248. }
  249. pwrdm->pwrdm_clkdms[i] = clkdm;
  250. ret = 0;
  251. pac_exit:
  252. return ret;
  253. }
  254. /**
  255. * pwrdm_del_clkdm - remove a clockdomain from a powerdomain
  256. * @pwrdm: struct powerdomain * to add the clockdomain to
  257. * @clkdm: struct clockdomain * to associate with a powerdomain
  258. *
  259. * Dissociate the clockdomain @clkdm from the powerdomain
  260. * @pwrdm. Returns -EINVAL if presented with invalid pointers; -ENOENT
  261. * if @clkdm was not associated with the powerdomain, or 0 upon
  262. * success.
  263. */
  264. int pwrdm_del_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
  265. {
  266. int ret = -EINVAL;
  267. int i;
  268. if (!pwrdm || !clkdm)
  269. return -EINVAL;
  270. pr_debug("powerdomain: dissociating clockdomain %s from powerdomain "
  271. "%s\n", clkdm->name, pwrdm->name);
  272. for (i = 0; i < PWRDM_MAX_CLKDMS; i++)
  273. if (pwrdm->pwrdm_clkdms[i] == clkdm)
  274. break;
  275. if (i == PWRDM_MAX_CLKDMS) {
  276. pr_debug("powerdomain: clkdm %s not associated with pwrdm "
  277. "%s ?!\n", clkdm->name, pwrdm->name);
  278. ret = -ENOENT;
  279. goto pdc_exit;
  280. }
  281. pwrdm->pwrdm_clkdms[i] = NULL;
  282. ret = 0;
  283. pdc_exit:
  284. return ret;
  285. }
  286. /**
  287. * pwrdm_for_each_clkdm - call function on each clkdm in a pwrdm
  288. * @pwrdm: struct powerdomain * to iterate over
  289. * @fn: callback function *
  290. *
  291. * Call the supplied function @fn for each clockdomain in the powerdomain
  292. * @pwrdm. The callback function can return anything but 0 to bail
  293. * out early from the iterator. Returns -EINVAL if presented with
  294. * invalid pointers; or passes along the last return value of the
  295. * callback function, which should be 0 for success or anything else
  296. * to indicate failure.
  297. */
  298. int pwrdm_for_each_clkdm(struct powerdomain *pwrdm,
  299. int (*fn)(struct powerdomain *pwrdm,
  300. struct clockdomain *clkdm))
  301. {
  302. int ret = 0;
  303. int i;
  304. if (!fn)
  305. return -EINVAL;
  306. for (i = 0; i < PWRDM_MAX_CLKDMS && !ret; i++)
  307. ret = (*fn)(pwrdm, pwrdm->pwrdm_clkdms[i]);
  308. return ret;
  309. }
  310. /**
  311. * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
  312. * @pwrdm: struct powerdomain *
  313. *
  314. * Return the number of controllable memory banks in powerdomain @pwrdm,
  315. * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
  316. */
  317. int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
  318. {
  319. if (!pwrdm)
  320. return -EINVAL;
  321. return pwrdm->banks;
  322. }
  323. /**
  324. * pwrdm_set_next_pwrst - set next powerdomain power state
  325. * @pwrdm: struct powerdomain * to set
  326. * @pwrst: one of the PWRDM_POWER_* macros
  327. *
  328. * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
  329. * may not enter this state immediately if the preconditions for this state
  330. * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
  331. * null or if the power state is invalid for the powerdomin, or returns 0
  332. * upon success.
  333. */
  334. int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
  335. {
  336. if (!pwrdm)
  337. return -EINVAL;
  338. if (!(pwrdm->pwrsts & (1 << pwrst)))
  339. return -EINVAL;
  340. pr_debug("powerdomain: setting next powerstate for %s to %0x\n",
  341. pwrdm->name, pwrst);
  342. prm_rmw_mod_reg_bits(OMAP_POWERSTATE_MASK,
  343. (pwrst << OMAP_POWERSTATE_SHIFT),
  344. pwrdm->prcm_offs, pwrstctrl_reg_offs);
  345. return 0;
  346. }
  347. /**
  348. * pwrdm_read_next_pwrst - get next powerdomain power state
  349. * @pwrdm: struct powerdomain * to get power state
  350. *
  351. * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
  352. * if the powerdomain pointer is null or returns the next power state
  353. * upon success.
  354. */
  355. int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
  356. {
  357. if (!pwrdm)
  358. return -EINVAL;
  359. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  360. pwrstctrl_reg_offs, OMAP_POWERSTATE_MASK);
  361. }
  362. /**
  363. * pwrdm_read_pwrst - get current powerdomain power state
  364. * @pwrdm: struct powerdomain * to get power state
  365. *
  366. * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
  367. * if the powerdomain pointer is null or returns the current power state
  368. * upon success.
  369. */
  370. int pwrdm_read_pwrst(struct powerdomain *pwrdm)
  371. {
  372. if (!pwrdm)
  373. return -EINVAL;
  374. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  375. pwrstst_reg_offs, OMAP_POWERSTATEST_MASK);
  376. }
  377. /**
  378. * pwrdm_read_prev_pwrst - get previous powerdomain power state
  379. * @pwrdm: struct powerdomain * to get previous power state
  380. *
  381. * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
  382. * if the powerdomain pointer is null or returns the previous power state
  383. * upon success.
  384. */
  385. int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
  386. {
  387. if (!pwrdm)
  388. return -EINVAL;
  389. return prm_read_mod_bits_shift(pwrdm->prcm_offs, OMAP3430_PM_PREPWSTST,
  390. OMAP3430_LASTPOWERSTATEENTERED_MASK);
  391. }
  392. /**
  393. * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
  394. * @pwrdm: struct powerdomain * to set
  395. * @pwrst: one of the PWRDM_POWER_* macros
  396. *
  397. * Set the next power state @pwrst that the logic portion of the
  398. * powerdomain @pwrdm will enter when the powerdomain enters retention.
  399. * This will be either RETENTION or OFF, if supported. Returns
  400. * -EINVAL if the powerdomain pointer is null or the target power
  401. * state is not not supported, or returns 0 upon success.
  402. */
  403. int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
  404. {
  405. if (!pwrdm)
  406. return -EINVAL;
  407. if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
  408. return -EINVAL;
  409. pr_debug("powerdomain: setting next logic powerstate for %s to %0x\n",
  410. pwrdm->name, pwrst);
  411. /*
  412. * The register bit names below may not correspond to the
  413. * actual names of the bits in each powerdomain's register,
  414. * but the type of value returned is the same for each
  415. * powerdomain.
  416. */
  417. prm_rmw_mod_reg_bits(OMAP3430_LOGICL1CACHERETSTATE,
  418. (pwrst << __ffs(OMAP3430_LOGICL1CACHERETSTATE)),
  419. pwrdm->prcm_offs, pwrstctrl_reg_offs);
  420. return 0;
  421. }
  422. /**
  423. * pwrdm_set_mem_onst - set memory power state while powerdomain ON
  424. * @pwrdm: struct powerdomain * to set
  425. * @bank: memory bank number to set (0-3)
  426. * @pwrst: one of the PWRDM_POWER_* macros
  427. *
  428. * Set the next power state @pwrst that memory bank @bank of the
  429. * powerdomain @pwrdm will enter when the powerdomain enters the ON
  430. * state. @bank will be a number from 0 to 3, and represents different
  431. * types of memory, depending on the powerdomain. Returns -EINVAL if
  432. * the powerdomain pointer is null or the target power state is not
  433. * not supported for this memory bank, -EEXIST if the target memory
  434. * bank does not exist or is not controllable, or returns 0 upon
  435. * success.
  436. */
  437. int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
  438. {
  439. u32 m;
  440. if (!pwrdm)
  441. return -EINVAL;
  442. if (pwrdm->banks < (bank + 1))
  443. return -EEXIST;
  444. if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
  445. return -EINVAL;
  446. pr_debug("powerdomain: setting next memory powerstate for domain %s "
  447. "bank %0x while pwrdm-ON to %0x\n", pwrdm->name, bank, pwrst);
  448. /*
  449. * The register bit names below may not correspond to the
  450. * actual names of the bits in each powerdomain's register,
  451. * but the type of value returned is the same for each
  452. * powerdomain.
  453. */
  454. switch (bank) {
  455. case 0:
  456. m = OMAP_MEM0_ONSTATE_MASK;
  457. break;
  458. case 1:
  459. m = OMAP_MEM1_ONSTATE_MASK;
  460. break;
  461. case 2:
  462. m = OMAP_MEM2_ONSTATE_MASK;
  463. break;
  464. case 3:
  465. m = OMAP_MEM3_ONSTATE_MASK;
  466. break;
  467. case 4:
  468. m = OMAP_MEM4_ONSTATE_MASK;
  469. break;
  470. default:
  471. WARN_ON(1); /* should never happen */
  472. return -EEXIST;
  473. }
  474. prm_rmw_mod_reg_bits(m, (pwrst << __ffs(m)),
  475. pwrdm->prcm_offs, pwrstctrl_reg_offs);
  476. return 0;
  477. }
  478. /**
  479. * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
  480. * @pwrdm: struct powerdomain * to set
  481. * @bank: memory bank number to set (0-3)
  482. * @pwrst: one of the PWRDM_POWER_* macros
  483. *
  484. * Set the next power state @pwrst that memory bank @bank of the
  485. * powerdomain @pwrdm will enter when the powerdomain enters the
  486. * RETENTION state. Bank will be a number from 0 to 3, and represents
  487. * different types of memory, depending on the powerdomain. @pwrst
  488. * will be either RETENTION or OFF, if supported. Returns -EINVAL if
  489. * the powerdomain pointer is null or the target power state is not
  490. * not supported for this memory bank, -EEXIST if the target memory
  491. * bank does not exist or is not controllable, or returns 0 upon
  492. * success.
  493. */
  494. int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
  495. {
  496. u32 m;
  497. if (!pwrdm)
  498. return -EINVAL;
  499. if (pwrdm->banks < (bank + 1))
  500. return -EEXIST;
  501. if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
  502. return -EINVAL;
  503. pr_debug("powerdomain: setting next memory powerstate for domain %s "
  504. "bank %0x while pwrdm-RET to %0x\n", pwrdm->name, bank, pwrst);
  505. /*
  506. * The register bit names below may not correspond to the
  507. * actual names of the bits in each powerdomain's register,
  508. * but the type of value returned is the same for each
  509. * powerdomain.
  510. */
  511. switch (bank) {
  512. case 0:
  513. m = OMAP_MEM0_RETSTATE_MASK;
  514. break;
  515. case 1:
  516. m = OMAP_MEM1_RETSTATE_MASK;
  517. break;
  518. case 2:
  519. m = OMAP_MEM2_RETSTATE_MASK;
  520. break;
  521. case 3:
  522. m = OMAP_MEM3_RETSTATE_MASK;
  523. break;
  524. case 4:
  525. m = OMAP_MEM4_RETSTATE_MASK;
  526. break;
  527. default:
  528. WARN_ON(1); /* should never happen */
  529. return -EEXIST;
  530. }
  531. prm_rmw_mod_reg_bits(m, (pwrst << __ffs(m)), pwrdm->prcm_offs,
  532. pwrstctrl_reg_offs);
  533. return 0;
  534. }
  535. /**
  536. * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
  537. * @pwrdm: struct powerdomain * to get current logic retention power state
  538. *
  539. * Return the power state that the logic portion of powerdomain @pwrdm
  540. * will enter when the powerdomain enters retention. Returns -EINVAL
  541. * if the powerdomain pointer is null or returns the logic retention
  542. * power state upon success.
  543. */
  544. int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
  545. {
  546. if (!pwrdm)
  547. return -EINVAL;
  548. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  549. pwrstst_reg_offs, OMAP3430_LOGICSTATEST);
  550. }
  551. /**
  552. * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
  553. * @pwrdm: struct powerdomain * to get previous logic power state
  554. *
  555. * Return the powerdomain @pwrdm's previous logic power state. Returns
  556. * -EINVAL if the powerdomain pointer is null or returns the previous
  557. * logic power state upon success.
  558. */
  559. int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
  560. {
  561. if (!pwrdm)
  562. return -EINVAL;
  563. /*
  564. * The register bit names below may not correspond to the
  565. * actual names of the bits in each powerdomain's register,
  566. * but the type of value returned is the same for each
  567. * powerdomain.
  568. */
  569. return prm_read_mod_bits_shift(pwrdm->prcm_offs, OMAP3430_PM_PREPWSTST,
  570. OMAP3430_LASTLOGICSTATEENTERED);
  571. }
  572. /**
  573. * pwrdm_read_mem_pwrst - get current memory bank power state
  574. * @pwrdm: struct powerdomain * to get current memory bank power state
  575. * @bank: memory bank number (0-3)
  576. *
  577. * Return the powerdomain @pwrdm's current memory power state for bank
  578. * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
  579. * the target memory bank does not exist or is not controllable, or
  580. * returns the current memory power state upon success.
  581. */
  582. int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
  583. {
  584. u32 m;
  585. if (!pwrdm)
  586. return -EINVAL;
  587. if (pwrdm->banks < (bank + 1))
  588. return -EEXIST;
  589. if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
  590. bank = 1;
  591. /*
  592. * The register bit names below may not correspond to the
  593. * actual names of the bits in each powerdomain's register,
  594. * but the type of value returned is the same for each
  595. * powerdomain.
  596. */
  597. switch (bank) {
  598. case 0:
  599. m = OMAP_MEM0_STATEST_MASK;
  600. break;
  601. case 1:
  602. m = OMAP_MEM1_STATEST_MASK;
  603. break;
  604. case 2:
  605. m = OMAP_MEM2_STATEST_MASK;
  606. break;
  607. case 3:
  608. m = OMAP_MEM3_STATEST_MASK;
  609. break;
  610. case 4:
  611. m = OMAP_MEM4_STATEST_MASK;
  612. break;
  613. default:
  614. WARN_ON(1); /* should never happen */
  615. return -EEXIST;
  616. }
  617. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  618. pwrstst_reg_offs, m);
  619. }
  620. /**
  621. * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
  622. * @pwrdm: struct powerdomain * to get previous memory bank power state
  623. * @bank: memory bank number (0-3)
  624. *
  625. * Return the powerdomain @pwrdm's previous memory power state for
  626. * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
  627. * -EEXIST if the target memory bank does not exist or is not
  628. * controllable, or returns the previous memory power state upon
  629. * success.
  630. */
  631. int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
  632. {
  633. u32 m;
  634. if (!pwrdm)
  635. return -EINVAL;
  636. if (pwrdm->banks < (bank + 1))
  637. return -EEXIST;
  638. if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
  639. bank = 1;
  640. /*
  641. * The register bit names below may not correspond to the
  642. * actual names of the bits in each powerdomain's register,
  643. * but the type of value returned is the same for each
  644. * powerdomain.
  645. */
  646. switch (bank) {
  647. case 0:
  648. m = OMAP3430_LASTMEM1STATEENTERED_MASK;
  649. break;
  650. case 1:
  651. m = OMAP3430_LASTMEM2STATEENTERED_MASK;
  652. break;
  653. case 2:
  654. m = OMAP3430_LASTSHAREDL2CACHEFLATSTATEENTERED_MASK;
  655. break;
  656. case 3:
  657. m = OMAP3430_LASTL2FLATMEMSTATEENTERED_MASK;
  658. break;
  659. default:
  660. WARN_ON(1); /* should never happen */
  661. return -EEXIST;
  662. }
  663. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  664. OMAP3430_PM_PREPWSTST, m);
  665. }
  666. /**
  667. * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
  668. * @pwrdm: struct powerdomain * to clear
  669. *
  670. * Clear the powerdomain's previous power state register @pwrdm.
  671. * Clears the entire register, including logic and memory bank
  672. * previous power states. Returns -EINVAL if the powerdomain pointer
  673. * is null, or returns 0 upon success.
  674. */
  675. int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
  676. {
  677. if (!pwrdm)
  678. return -EINVAL;
  679. /*
  680. * XXX should get the powerdomain's current state here;
  681. * warn & fail if it is not ON.
  682. */
  683. pr_debug("powerdomain: clearing previous power state reg for %s\n",
  684. pwrdm->name);
  685. prm_write_mod_reg(0, pwrdm->prcm_offs, OMAP3430_PM_PREPWSTST);
  686. return 0;
  687. }
  688. /**
  689. * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
  690. * @pwrdm: struct powerdomain *
  691. *
  692. * Enable automatic context save-and-restore upon power state change
  693. * for some devices in the powerdomain @pwrdm. Warning: this only
  694. * affects a subset of devices in a powerdomain; check the TRM
  695. * closely. Returns -EINVAL if the powerdomain pointer is null or if
  696. * the powerdomain does not support automatic save-and-restore, or
  697. * returns 0 upon success.
  698. */
  699. int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
  700. {
  701. if (!pwrdm)
  702. return -EINVAL;
  703. if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
  704. return -EINVAL;
  705. pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n",
  706. pwrdm->name);
  707. prm_rmw_mod_reg_bits(0, 1 << OMAP3430ES2_SAVEANDRESTORE_SHIFT,
  708. pwrdm->prcm_offs, pwrstctrl_reg_offs);
  709. return 0;
  710. }
  711. /**
  712. * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
  713. * @pwrdm: struct powerdomain *
  714. *
  715. * Disable automatic context save-and-restore upon power state change
  716. * for some devices in the powerdomain @pwrdm. Warning: this only
  717. * affects a subset of devices in a powerdomain; check the TRM
  718. * closely. Returns -EINVAL if the powerdomain pointer is null or if
  719. * the powerdomain does not support automatic save-and-restore, or
  720. * returns 0 upon success.
  721. */
  722. int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
  723. {
  724. if (!pwrdm)
  725. return -EINVAL;
  726. if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
  727. return -EINVAL;
  728. pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n",
  729. pwrdm->name);
  730. prm_rmw_mod_reg_bits(1 << OMAP3430ES2_SAVEANDRESTORE_SHIFT, 0,
  731. pwrdm->prcm_offs, pwrstctrl_reg_offs);
  732. return 0;
  733. }
  734. /**
  735. * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
  736. * @pwrdm: struct powerdomain *
  737. *
  738. * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
  739. * for some devices, or 0 if it does not.
  740. */
  741. bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
  742. {
  743. return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
  744. }
  745. /**
  746. * pwrdm_wait_transition - wait for powerdomain power transition to finish
  747. * @pwrdm: struct powerdomain * to wait for
  748. *
  749. * If the powerdomain @pwrdm is in the process of a state transition,
  750. * spin until it completes the power transition, or until an iteration
  751. * bailout value is reached. Returns -EINVAL if the powerdomain
  752. * pointer is null, -EAGAIN if the bailout value was reached, or
  753. * returns 0 upon success.
  754. */
  755. int pwrdm_wait_transition(struct powerdomain *pwrdm)
  756. {
  757. u32 c = 0;
  758. if (!pwrdm)
  759. return -EINVAL;
  760. /*
  761. * REVISIT: pwrdm_wait_transition() may be better implemented
  762. * via a callback and a periodic timer check -- how long do we expect
  763. * powerdomain transitions to take?
  764. */
  765. /* XXX Is this udelay() value meaningful? */
  766. while ((prm_read_mod_reg(pwrdm->prcm_offs, pwrstst_reg_offs) &
  767. OMAP_INTRANSITION) &&
  768. (c++ < PWRDM_TRANSITION_BAILOUT))
  769. udelay(1);
  770. if (c > PWRDM_TRANSITION_BAILOUT) {
  771. printk(KERN_ERR "powerdomain: waited too long for "
  772. "powerdomain %s to complete transition\n", pwrdm->name);
  773. return -EAGAIN;
  774. }
  775. pr_debug("powerdomain: completed transition in %d loops\n", c);
  776. return 0;
  777. }
  778. int pwrdm_state_switch(struct powerdomain *pwrdm)
  779. {
  780. return _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
  781. }
  782. int pwrdm_clkdm_state_switch(struct clockdomain *clkdm)
  783. {
  784. if (clkdm != NULL && clkdm->pwrdm.ptr != NULL) {
  785. pwrdm_wait_transition(clkdm->pwrdm.ptr);
  786. return pwrdm_state_switch(clkdm->pwrdm.ptr);
  787. }
  788. return -EINVAL;
  789. }
  790. int pwrdm_pre_transition(void)
  791. {
  792. pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
  793. return 0;
  794. }
  795. int pwrdm_post_transition(void)
  796. {
  797. pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
  798. return 0;
  799. }