powerdomain.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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->ret_logic_off_counter = 0;
  104. for (i = 0; i < pwrdm->banks; i++)
  105. pwrdm->ret_mem_off_counter[i] = 0;
  106. pwrdm_wait_transition(pwrdm);
  107. pwrdm->state = pwrdm_read_pwrst(pwrdm);
  108. pwrdm->state_counter[pwrdm->state] = 1;
  109. pr_debug("powerdomain: registered %s\n", pwrdm->name);
  110. return 0;
  111. }
  112. static void _update_logic_membank_counters(struct powerdomain *pwrdm)
  113. {
  114. int i;
  115. u8 prev_logic_pwrst, prev_mem_pwrst;
  116. prev_logic_pwrst = pwrdm_read_prev_logic_pwrst(pwrdm);
  117. if ((pwrdm->pwrsts_logic_ret == PWRSTS_OFF_RET) &&
  118. (prev_logic_pwrst == PWRDM_POWER_OFF))
  119. pwrdm->ret_logic_off_counter++;
  120. for (i = 0; i < pwrdm->banks; i++) {
  121. prev_mem_pwrst = pwrdm_read_prev_mem_pwrst(pwrdm, i);
  122. if ((pwrdm->pwrsts_mem_ret[i] == PWRSTS_OFF_RET) &&
  123. (prev_mem_pwrst == PWRDM_POWER_OFF))
  124. pwrdm->ret_mem_off_counter[i]++;
  125. }
  126. }
  127. static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
  128. {
  129. int prev;
  130. int state;
  131. if (pwrdm == NULL)
  132. return -EINVAL;
  133. state = pwrdm_read_pwrst(pwrdm);
  134. switch (flag) {
  135. case PWRDM_STATE_NOW:
  136. prev = pwrdm->state;
  137. break;
  138. case PWRDM_STATE_PREV:
  139. prev = pwrdm_read_prev_pwrst(pwrdm);
  140. if (pwrdm->state != prev)
  141. pwrdm->state_counter[prev]++;
  142. if (prev == PWRDM_POWER_RET)
  143. _update_logic_membank_counters(pwrdm);
  144. break;
  145. default:
  146. return -EINVAL;
  147. }
  148. if (state != prev)
  149. pwrdm->state_counter[state]++;
  150. pm_dbg_update_time(pwrdm, prev);
  151. pwrdm->state = state;
  152. return 0;
  153. }
  154. static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
  155. {
  156. pwrdm_clear_all_prev_pwrst(pwrdm);
  157. _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
  158. return 0;
  159. }
  160. static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
  161. {
  162. _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
  163. return 0;
  164. }
  165. /* Public functions */
  166. /**
  167. * pwrdm_init - set up the powerdomain layer
  168. * @pwrdm_list: array of struct powerdomain pointers to register
  169. *
  170. * Loop through the array of powerdomains @pwrdm_list, registering all
  171. * that are available on the current CPU. If pwrdm_list is supplied
  172. * and not null, all of the referenced powerdomains will be
  173. * registered. No return value. XXX pwrdm_list is not really a
  174. * "list"; it is an array. Rename appropriately.
  175. */
  176. void pwrdm_init(struct powerdomain **pwrdm_list)
  177. {
  178. struct powerdomain **p = NULL;
  179. if (cpu_is_omap24xx() | cpu_is_omap34xx()) {
  180. pwrstctrl_reg_offs = OMAP2_PM_PWSTCTRL;
  181. pwrstst_reg_offs = OMAP2_PM_PWSTST;
  182. } else if (cpu_is_omap44xx()) {
  183. pwrstctrl_reg_offs = OMAP4_PM_PWSTCTRL;
  184. pwrstst_reg_offs = OMAP4_PM_PWSTST;
  185. } else {
  186. printk(KERN_ERR "Power Domain struct not supported for " \
  187. "this CPU\n");
  188. return;
  189. }
  190. if (pwrdm_list) {
  191. for (p = pwrdm_list; *p; p++)
  192. _pwrdm_register(*p);
  193. }
  194. }
  195. /**
  196. * pwrdm_lookup - look up a powerdomain by name, return a pointer
  197. * @name: name of powerdomain
  198. *
  199. * Find a registered powerdomain by its name @name. Returns a pointer
  200. * to the struct powerdomain if found, or NULL otherwise.
  201. */
  202. struct powerdomain *pwrdm_lookup(const char *name)
  203. {
  204. struct powerdomain *pwrdm;
  205. if (!name)
  206. return NULL;
  207. pwrdm = _pwrdm_lookup(name);
  208. return pwrdm;
  209. }
  210. /**
  211. * pwrdm_for_each - call function on each registered clockdomain
  212. * @fn: callback function *
  213. *
  214. * Call the supplied function @fn for each registered powerdomain.
  215. * The callback function @fn can return anything but 0 to bail out
  216. * early from the iterator. Returns the last return value of the
  217. * callback function, which should be 0 for success or anything else
  218. * to indicate failure; or -EINVAL if the function pointer is null.
  219. */
  220. int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
  221. void *user)
  222. {
  223. struct powerdomain *temp_pwrdm;
  224. int ret = 0;
  225. if (!fn)
  226. return -EINVAL;
  227. list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
  228. ret = (*fn)(temp_pwrdm, user);
  229. if (ret)
  230. break;
  231. }
  232. return ret;
  233. }
  234. /**
  235. * pwrdm_add_clkdm - add a clockdomain to a powerdomain
  236. * @pwrdm: struct powerdomain * to add the clockdomain to
  237. * @clkdm: struct clockdomain * to associate with a powerdomain
  238. *
  239. * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
  240. * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
  241. * presented with invalid pointers; -ENOMEM if memory could not be allocated;
  242. * or 0 upon success.
  243. */
  244. int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
  245. {
  246. int i;
  247. int ret = -EINVAL;
  248. if (!pwrdm || !clkdm)
  249. return -EINVAL;
  250. pr_debug("powerdomain: associating clockdomain %s with powerdomain "
  251. "%s\n", clkdm->name, pwrdm->name);
  252. for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
  253. if (!pwrdm->pwrdm_clkdms[i])
  254. break;
  255. #ifdef DEBUG
  256. if (pwrdm->pwrdm_clkdms[i] == clkdm) {
  257. ret = -EINVAL;
  258. goto pac_exit;
  259. }
  260. #endif
  261. }
  262. if (i == PWRDM_MAX_CLKDMS) {
  263. pr_debug("powerdomain: increase PWRDM_MAX_CLKDMS for "
  264. "pwrdm %s clkdm %s\n", pwrdm->name, clkdm->name);
  265. WARN_ON(1);
  266. ret = -ENOMEM;
  267. goto pac_exit;
  268. }
  269. pwrdm->pwrdm_clkdms[i] = clkdm;
  270. ret = 0;
  271. pac_exit:
  272. return ret;
  273. }
  274. /**
  275. * pwrdm_del_clkdm - remove a clockdomain from a powerdomain
  276. * @pwrdm: struct powerdomain * to add the clockdomain to
  277. * @clkdm: struct clockdomain * to associate with a powerdomain
  278. *
  279. * Dissociate the clockdomain @clkdm from the powerdomain
  280. * @pwrdm. Returns -EINVAL if presented with invalid pointers; -ENOENT
  281. * if @clkdm was not associated with the powerdomain, or 0 upon
  282. * success.
  283. */
  284. int pwrdm_del_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
  285. {
  286. int ret = -EINVAL;
  287. int i;
  288. if (!pwrdm || !clkdm)
  289. return -EINVAL;
  290. pr_debug("powerdomain: dissociating clockdomain %s from powerdomain "
  291. "%s\n", clkdm->name, pwrdm->name);
  292. for (i = 0; i < PWRDM_MAX_CLKDMS; i++)
  293. if (pwrdm->pwrdm_clkdms[i] == clkdm)
  294. break;
  295. if (i == PWRDM_MAX_CLKDMS) {
  296. pr_debug("powerdomain: clkdm %s not associated with pwrdm "
  297. "%s ?!\n", clkdm->name, pwrdm->name);
  298. ret = -ENOENT;
  299. goto pdc_exit;
  300. }
  301. pwrdm->pwrdm_clkdms[i] = NULL;
  302. ret = 0;
  303. pdc_exit:
  304. return ret;
  305. }
  306. /**
  307. * pwrdm_for_each_clkdm - call function on each clkdm in a pwrdm
  308. * @pwrdm: struct powerdomain * to iterate over
  309. * @fn: callback function *
  310. *
  311. * Call the supplied function @fn for each clockdomain in the powerdomain
  312. * @pwrdm. The callback function can return anything but 0 to bail
  313. * out early from the iterator. Returns -EINVAL if presented with
  314. * invalid pointers; or passes along the last return value of the
  315. * callback function, which should be 0 for success or anything else
  316. * to indicate failure.
  317. */
  318. int pwrdm_for_each_clkdm(struct powerdomain *pwrdm,
  319. int (*fn)(struct powerdomain *pwrdm,
  320. struct clockdomain *clkdm))
  321. {
  322. int ret = 0;
  323. int i;
  324. if (!fn)
  325. return -EINVAL;
  326. for (i = 0; i < PWRDM_MAX_CLKDMS && !ret; i++)
  327. ret = (*fn)(pwrdm, pwrdm->pwrdm_clkdms[i]);
  328. return ret;
  329. }
  330. /**
  331. * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
  332. * @pwrdm: struct powerdomain *
  333. *
  334. * Return the number of controllable memory banks in powerdomain @pwrdm,
  335. * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
  336. */
  337. int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
  338. {
  339. if (!pwrdm)
  340. return -EINVAL;
  341. return pwrdm->banks;
  342. }
  343. /**
  344. * pwrdm_set_next_pwrst - set next powerdomain power state
  345. * @pwrdm: struct powerdomain * to set
  346. * @pwrst: one of the PWRDM_POWER_* macros
  347. *
  348. * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
  349. * may not enter this state immediately if the preconditions for this state
  350. * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
  351. * null or if the power state is invalid for the powerdomin, or returns 0
  352. * upon success.
  353. */
  354. int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
  355. {
  356. if (!pwrdm)
  357. return -EINVAL;
  358. if (!(pwrdm->pwrsts & (1 << pwrst)))
  359. return -EINVAL;
  360. pr_debug("powerdomain: setting next powerstate for %s to %0x\n",
  361. pwrdm->name, pwrst);
  362. prm_rmw_mod_reg_bits(OMAP_POWERSTATE_MASK,
  363. (pwrst << OMAP_POWERSTATE_SHIFT),
  364. pwrdm->prcm_offs, pwrstctrl_reg_offs);
  365. return 0;
  366. }
  367. /**
  368. * pwrdm_read_next_pwrst - get next powerdomain power state
  369. * @pwrdm: struct powerdomain * to get power state
  370. *
  371. * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
  372. * if the powerdomain pointer is null or returns the next power state
  373. * upon success.
  374. */
  375. int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
  376. {
  377. if (!pwrdm)
  378. return -EINVAL;
  379. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  380. pwrstctrl_reg_offs, OMAP_POWERSTATE_MASK);
  381. }
  382. /**
  383. * pwrdm_read_pwrst - get current powerdomain power state
  384. * @pwrdm: struct powerdomain * to get power state
  385. *
  386. * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
  387. * if the powerdomain pointer is null or returns the current power state
  388. * upon success.
  389. */
  390. int pwrdm_read_pwrst(struct powerdomain *pwrdm)
  391. {
  392. if (!pwrdm)
  393. return -EINVAL;
  394. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  395. pwrstst_reg_offs, OMAP_POWERSTATEST_MASK);
  396. }
  397. /**
  398. * pwrdm_read_prev_pwrst - get previous powerdomain power state
  399. * @pwrdm: struct powerdomain * to get previous power state
  400. *
  401. * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
  402. * if the powerdomain pointer is null or returns the previous power state
  403. * upon success.
  404. */
  405. int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
  406. {
  407. if (!pwrdm)
  408. return -EINVAL;
  409. return prm_read_mod_bits_shift(pwrdm->prcm_offs, OMAP3430_PM_PREPWSTST,
  410. OMAP3430_LASTPOWERSTATEENTERED_MASK);
  411. }
  412. /**
  413. * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
  414. * @pwrdm: struct powerdomain * to set
  415. * @pwrst: one of the PWRDM_POWER_* macros
  416. *
  417. * Set the next power state @pwrst that the logic portion of the
  418. * powerdomain @pwrdm will enter when the powerdomain enters retention.
  419. * This will be either RETENTION or OFF, if supported. Returns
  420. * -EINVAL if the powerdomain pointer is null or the target power
  421. * state is not not supported, or returns 0 upon success.
  422. */
  423. int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
  424. {
  425. if (!pwrdm)
  426. return -EINVAL;
  427. if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
  428. return -EINVAL;
  429. pr_debug("powerdomain: setting next logic powerstate for %s to %0x\n",
  430. pwrdm->name, pwrst);
  431. /*
  432. * The register bit names below may not correspond to the
  433. * actual names of the bits in each powerdomain's register,
  434. * but the type of value returned is the same for each
  435. * powerdomain.
  436. */
  437. prm_rmw_mod_reg_bits(OMAP3430_LOGICL1CACHERETSTATE,
  438. (pwrst << __ffs(OMAP3430_LOGICL1CACHERETSTATE)),
  439. pwrdm->prcm_offs, pwrstctrl_reg_offs);
  440. return 0;
  441. }
  442. /**
  443. * pwrdm_set_mem_onst - set memory power state while powerdomain ON
  444. * @pwrdm: struct powerdomain * to set
  445. * @bank: memory bank number to set (0-3)
  446. * @pwrst: one of the PWRDM_POWER_* macros
  447. *
  448. * Set the next power state @pwrst that memory bank @bank of the
  449. * powerdomain @pwrdm will enter when the powerdomain enters the ON
  450. * state. @bank will be a number from 0 to 3, and represents different
  451. * types of memory, depending on the powerdomain. Returns -EINVAL if
  452. * the powerdomain pointer is null or the target power state is not
  453. * not supported for this memory bank, -EEXIST if the target memory
  454. * bank does not exist or is not controllable, or returns 0 upon
  455. * success.
  456. */
  457. int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
  458. {
  459. u32 m;
  460. if (!pwrdm)
  461. return -EINVAL;
  462. if (pwrdm->banks < (bank + 1))
  463. return -EEXIST;
  464. if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
  465. return -EINVAL;
  466. pr_debug("powerdomain: setting next memory powerstate for domain %s "
  467. "bank %0x while pwrdm-ON to %0x\n", pwrdm->name, bank, pwrst);
  468. /*
  469. * The register bit names below may not correspond to the
  470. * actual names of the bits in each powerdomain's register,
  471. * but the type of value returned is the same for each
  472. * powerdomain.
  473. */
  474. switch (bank) {
  475. case 0:
  476. m = OMAP_MEM0_ONSTATE_MASK;
  477. break;
  478. case 1:
  479. m = OMAP_MEM1_ONSTATE_MASK;
  480. break;
  481. case 2:
  482. m = OMAP_MEM2_ONSTATE_MASK;
  483. break;
  484. case 3:
  485. m = OMAP_MEM3_ONSTATE_MASK;
  486. break;
  487. case 4:
  488. m = OMAP_MEM4_ONSTATE_MASK;
  489. break;
  490. default:
  491. WARN_ON(1); /* should never happen */
  492. return -EEXIST;
  493. }
  494. prm_rmw_mod_reg_bits(m, (pwrst << __ffs(m)),
  495. pwrdm->prcm_offs, pwrstctrl_reg_offs);
  496. return 0;
  497. }
  498. /**
  499. * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
  500. * @pwrdm: struct powerdomain * to set
  501. * @bank: memory bank number to set (0-3)
  502. * @pwrst: one of the PWRDM_POWER_* macros
  503. *
  504. * Set the next power state @pwrst that memory bank @bank of the
  505. * powerdomain @pwrdm will enter when the powerdomain enters the
  506. * RETENTION state. Bank will be a number from 0 to 3, and represents
  507. * different types of memory, depending on the powerdomain. @pwrst
  508. * will be either RETENTION or OFF, if supported. Returns -EINVAL if
  509. * the powerdomain pointer is null or the target power state is not
  510. * not supported for this memory bank, -EEXIST if the target memory
  511. * bank does not exist or is not controllable, or returns 0 upon
  512. * success.
  513. */
  514. int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
  515. {
  516. u32 m;
  517. if (!pwrdm)
  518. return -EINVAL;
  519. if (pwrdm->banks < (bank + 1))
  520. return -EEXIST;
  521. if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
  522. return -EINVAL;
  523. pr_debug("powerdomain: setting next memory powerstate for domain %s "
  524. "bank %0x while pwrdm-RET to %0x\n", pwrdm->name, bank, pwrst);
  525. /*
  526. * The register bit names below may not correspond to the
  527. * actual names of the bits in each powerdomain's register,
  528. * but the type of value returned is the same for each
  529. * powerdomain.
  530. */
  531. switch (bank) {
  532. case 0:
  533. m = OMAP_MEM0_RETSTATE_MASK;
  534. break;
  535. case 1:
  536. m = OMAP_MEM1_RETSTATE_MASK;
  537. break;
  538. case 2:
  539. m = OMAP_MEM2_RETSTATE_MASK;
  540. break;
  541. case 3:
  542. m = OMAP_MEM3_RETSTATE_MASK;
  543. break;
  544. case 4:
  545. m = OMAP_MEM4_RETSTATE_MASK;
  546. break;
  547. default:
  548. WARN_ON(1); /* should never happen */
  549. return -EEXIST;
  550. }
  551. prm_rmw_mod_reg_bits(m, (pwrst << __ffs(m)), pwrdm->prcm_offs,
  552. pwrstctrl_reg_offs);
  553. return 0;
  554. }
  555. /**
  556. * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
  557. * @pwrdm: struct powerdomain * to get current logic retention power state
  558. *
  559. * Return the power state that the logic portion of powerdomain @pwrdm
  560. * will enter when the powerdomain enters retention. Returns -EINVAL
  561. * if the powerdomain pointer is null or returns the logic retention
  562. * power state upon success.
  563. */
  564. int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
  565. {
  566. if (!pwrdm)
  567. return -EINVAL;
  568. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  569. pwrstst_reg_offs, OMAP3430_LOGICSTATEST);
  570. }
  571. /**
  572. * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
  573. * @pwrdm: struct powerdomain * to get previous logic power state
  574. *
  575. * Return the powerdomain @pwrdm's previous logic power state. Returns
  576. * -EINVAL if the powerdomain pointer is null or returns the previous
  577. * logic power state upon success.
  578. */
  579. int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
  580. {
  581. if (!pwrdm)
  582. return -EINVAL;
  583. /*
  584. * The register bit names below may not correspond to the
  585. * actual names of the bits in each powerdomain's register,
  586. * but the type of value returned is the same for each
  587. * powerdomain.
  588. */
  589. return prm_read_mod_bits_shift(pwrdm->prcm_offs, OMAP3430_PM_PREPWSTST,
  590. OMAP3430_LASTLOGICSTATEENTERED);
  591. }
  592. /**
  593. * pwrdm_read_logic_retst - get next powerdomain logic power state
  594. * @pwrdm: struct powerdomain * to get next logic power state
  595. *
  596. * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
  597. * if the powerdomain pointer is null or returns the next logic
  598. * power state upon success.
  599. */
  600. int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
  601. {
  602. if (!pwrdm)
  603. return -EINVAL;
  604. /*
  605. * The register bit names below may not correspond to the
  606. * actual names of the bits in each powerdomain's register,
  607. * but the type of value returned is the same for each
  608. * powerdomain.
  609. */
  610. return prm_read_mod_bits_shift(pwrdm->prcm_offs, pwrstctrl_reg_offs,
  611. OMAP3430_LOGICSTATEST);
  612. }
  613. /**
  614. * pwrdm_read_mem_pwrst - get current memory bank power state
  615. * @pwrdm: struct powerdomain * to get current memory bank power state
  616. * @bank: memory bank number (0-3)
  617. *
  618. * Return the powerdomain @pwrdm's current memory power state for bank
  619. * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
  620. * the target memory bank does not exist or is not controllable, or
  621. * returns the current memory power state upon success.
  622. */
  623. int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
  624. {
  625. u32 m;
  626. if (!pwrdm)
  627. return -EINVAL;
  628. if (pwrdm->banks < (bank + 1))
  629. return -EEXIST;
  630. if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
  631. bank = 1;
  632. /*
  633. * The register bit names below may not correspond to the
  634. * actual names of the bits in each powerdomain's register,
  635. * but the type of value returned is the same for each
  636. * powerdomain.
  637. */
  638. switch (bank) {
  639. case 0:
  640. m = OMAP_MEM0_STATEST_MASK;
  641. break;
  642. case 1:
  643. m = OMAP_MEM1_STATEST_MASK;
  644. break;
  645. case 2:
  646. m = OMAP_MEM2_STATEST_MASK;
  647. break;
  648. case 3:
  649. m = OMAP_MEM3_STATEST_MASK;
  650. break;
  651. case 4:
  652. m = OMAP_MEM4_STATEST_MASK;
  653. break;
  654. default:
  655. WARN_ON(1); /* should never happen */
  656. return -EEXIST;
  657. }
  658. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  659. pwrstst_reg_offs, m);
  660. }
  661. /**
  662. * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
  663. * @pwrdm: struct powerdomain * to get previous memory bank power state
  664. * @bank: memory bank number (0-3)
  665. *
  666. * Return the powerdomain @pwrdm's previous memory power state for
  667. * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
  668. * -EEXIST if the target memory bank does not exist or is not
  669. * controllable, or returns the previous memory power state upon
  670. * success.
  671. */
  672. int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
  673. {
  674. u32 m;
  675. if (!pwrdm)
  676. return -EINVAL;
  677. if (pwrdm->banks < (bank + 1))
  678. return -EEXIST;
  679. if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
  680. bank = 1;
  681. /*
  682. * The register bit names below may not correspond to the
  683. * actual names of the bits in each powerdomain's register,
  684. * but the type of value returned is the same for each
  685. * powerdomain.
  686. */
  687. switch (bank) {
  688. case 0:
  689. m = OMAP3430_LASTMEM1STATEENTERED_MASK;
  690. break;
  691. case 1:
  692. m = OMAP3430_LASTMEM2STATEENTERED_MASK;
  693. break;
  694. case 2:
  695. m = OMAP3430_LASTSHAREDL2CACHEFLATSTATEENTERED_MASK;
  696. break;
  697. case 3:
  698. m = OMAP3430_LASTL2FLATMEMSTATEENTERED_MASK;
  699. break;
  700. default:
  701. WARN_ON(1); /* should never happen */
  702. return -EEXIST;
  703. }
  704. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  705. OMAP3430_PM_PREPWSTST, m);
  706. }
  707. /**
  708. * pwrdm_read_mem_retst - get next memory bank power state
  709. * @pwrdm: struct powerdomain * to get mext memory bank power state
  710. * @bank: memory bank number (0-3)
  711. *
  712. * Return the powerdomain pwrdm's next memory power state for bank
  713. * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
  714. * the target memory bank does not exist or is not controllable, or
  715. * returns the next memory power state upon success.
  716. */
  717. int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
  718. {
  719. u32 m;
  720. if (!pwrdm)
  721. return -EINVAL;
  722. if (pwrdm->banks < (bank + 1))
  723. return -EEXIST;
  724. /*
  725. * The register bit names below may not correspond to the
  726. * actual names of the bits in each powerdomain's register,
  727. * but the type of value returned is the same for each
  728. * powerdomain.
  729. */
  730. switch (bank) {
  731. case 0:
  732. m = OMAP_MEM0_RETSTATE_MASK;
  733. break;
  734. case 1:
  735. m = OMAP_MEM1_RETSTATE_MASK;
  736. break;
  737. case 2:
  738. m = OMAP_MEM2_RETSTATE_MASK;
  739. break;
  740. case 3:
  741. m = OMAP_MEM3_RETSTATE_MASK;
  742. break;
  743. case 4:
  744. m = OMAP_MEM4_RETSTATE_MASK;
  745. default:
  746. WARN_ON(1); /* should never happen */
  747. return -EEXIST;
  748. }
  749. return prm_read_mod_bits_shift(pwrdm->prcm_offs,
  750. pwrstctrl_reg_offs, m);
  751. }
  752. /**
  753. * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
  754. * @pwrdm: struct powerdomain * to clear
  755. *
  756. * Clear the powerdomain's previous power state register @pwrdm.
  757. * Clears the entire register, including logic and memory bank
  758. * previous power states. Returns -EINVAL if the powerdomain pointer
  759. * is null, or returns 0 upon success.
  760. */
  761. int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
  762. {
  763. if (!pwrdm)
  764. return -EINVAL;
  765. /*
  766. * XXX should get the powerdomain's current state here;
  767. * warn & fail if it is not ON.
  768. */
  769. pr_debug("powerdomain: clearing previous power state reg for %s\n",
  770. pwrdm->name);
  771. prm_write_mod_reg(0, pwrdm->prcm_offs, OMAP3430_PM_PREPWSTST);
  772. return 0;
  773. }
  774. /**
  775. * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
  776. * @pwrdm: struct powerdomain *
  777. *
  778. * Enable automatic context save-and-restore upon power state change
  779. * for some devices in the powerdomain @pwrdm. Warning: this only
  780. * affects a subset of devices in a powerdomain; check the TRM
  781. * closely. Returns -EINVAL if the powerdomain pointer is null or if
  782. * the powerdomain does not support automatic save-and-restore, or
  783. * returns 0 upon success.
  784. */
  785. int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
  786. {
  787. if (!pwrdm)
  788. return -EINVAL;
  789. if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
  790. return -EINVAL;
  791. pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n",
  792. pwrdm->name);
  793. prm_rmw_mod_reg_bits(0, 1 << OMAP3430ES2_SAVEANDRESTORE_SHIFT,
  794. pwrdm->prcm_offs, pwrstctrl_reg_offs);
  795. return 0;
  796. }
  797. /**
  798. * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
  799. * @pwrdm: struct powerdomain *
  800. *
  801. * Disable automatic context save-and-restore upon power state change
  802. * for some devices in the powerdomain @pwrdm. Warning: this only
  803. * affects a subset of devices in a powerdomain; check the TRM
  804. * closely. Returns -EINVAL if the powerdomain pointer is null or if
  805. * the powerdomain does not support automatic save-and-restore, or
  806. * returns 0 upon success.
  807. */
  808. int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
  809. {
  810. if (!pwrdm)
  811. return -EINVAL;
  812. if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
  813. return -EINVAL;
  814. pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n",
  815. pwrdm->name);
  816. prm_rmw_mod_reg_bits(1 << OMAP3430ES2_SAVEANDRESTORE_SHIFT, 0,
  817. pwrdm->prcm_offs, pwrstctrl_reg_offs);
  818. return 0;
  819. }
  820. /**
  821. * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
  822. * @pwrdm: struct powerdomain *
  823. *
  824. * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
  825. * for some devices, or 0 if it does not.
  826. */
  827. bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
  828. {
  829. return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
  830. }
  831. /**
  832. * pwrdm_wait_transition - wait for powerdomain power transition to finish
  833. * @pwrdm: struct powerdomain * to wait for
  834. *
  835. * If the powerdomain @pwrdm is in the process of a state transition,
  836. * spin until it completes the power transition, or until an iteration
  837. * bailout value is reached. Returns -EINVAL if the powerdomain
  838. * pointer is null, -EAGAIN if the bailout value was reached, or
  839. * returns 0 upon success.
  840. */
  841. int pwrdm_wait_transition(struct powerdomain *pwrdm)
  842. {
  843. u32 c = 0;
  844. if (!pwrdm)
  845. return -EINVAL;
  846. /*
  847. * REVISIT: pwrdm_wait_transition() may be better implemented
  848. * via a callback and a periodic timer check -- how long do we expect
  849. * powerdomain transitions to take?
  850. */
  851. /* XXX Is this udelay() value meaningful? */
  852. while ((prm_read_mod_reg(pwrdm->prcm_offs, pwrstst_reg_offs) &
  853. OMAP_INTRANSITION) &&
  854. (c++ < PWRDM_TRANSITION_BAILOUT))
  855. udelay(1);
  856. if (c > PWRDM_TRANSITION_BAILOUT) {
  857. printk(KERN_ERR "powerdomain: waited too long for "
  858. "powerdomain %s to complete transition\n", pwrdm->name);
  859. return -EAGAIN;
  860. }
  861. pr_debug("powerdomain: completed transition in %d loops\n", c);
  862. return 0;
  863. }
  864. int pwrdm_state_switch(struct powerdomain *pwrdm)
  865. {
  866. return _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
  867. }
  868. int pwrdm_clkdm_state_switch(struct clockdomain *clkdm)
  869. {
  870. if (clkdm != NULL && clkdm->pwrdm.ptr != NULL) {
  871. pwrdm_wait_transition(clkdm->pwrdm.ptr);
  872. return pwrdm_state_switch(clkdm->pwrdm.ptr);
  873. }
  874. return -EINVAL;
  875. }
  876. int pwrdm_pre_transition(void)
  877. {
  878. pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
  879. return 0;
  880. }
  881. int pwrdm_post_transition(void)
  882. {
  883. pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
  884. return 0;
  885. }