powerdomain.c 27 KB

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