psc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Keystone: PSC configuration module
  3. *
  4. * (C) Copyright 2012-2014
  5. * Texas Instruments Incorporated, <www.ti.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <linux/errno.h>
  11. #include <asm/io.h>
  12. #include <asm/processor.h>
  13. #include <asm/arch/psc_defs.h>
  14. /**
  15. * psc_delay() - delay for psc
  16. *
  17. * Return: 10
  18. */
  19. int psc_delay(void)
  20. {
  21. udelay(10);
  22. return 10;
  23. }
  24. /**
  25. * psc_wait() - Wait for end of transitional state
  26. * @domain_num: GPSC domain number
  27. *
  28. * Polls pstat for the selected domain and waits for transitions to be complete.
  29. * Since this is boot loader code it is *ASSUMED* that interrupts are disabled
  30. * and no other core is mucking around with the psc at the same time.
  31. *
  32. * Return: 0 when the domain is free. Returns -1 if a timeout occurred waiting
  33. * for the completion.
  34. */
  35. int psc_wait(u32 domain_num)
  36. {
  37. u32 retry;
  38. u32 ptstat;
  39. /*
  40. * Do nothing if the power domain is in transition. This should never
  41. * happen since the boot code is the only software accesses psc.
  42. * It's still remotely possible that the hardware state machines
  43. * initiate transitions.
  44. * Don't trap if the domain (or a module in this domain) is
  45. * stuck in transition.
  46. */
  47. retry = 0;
  48. do {
  49. ptstat = __raw_readl(KS2_PSC_BASE + PSC_REG_PSTAT);
  50. ptstat = ptstat & (1 << domain_num);
  51. } while ((ptstat != 0) && ((retry += psc_delay()) <
  52. PSC_PTSTAT_TIMEOUT_LIMIT));
  53. if (retry >= PSC_PTSTAT_TIMEOUT_LIMIT)
  54. return -1;
  55. return 0;
  56. }
  57. /**
  58. * psc_get_domain_num() - Get the domain number
  59. * @mod_num: LPSC module number
  60. */
  61. u32 psc_get_domain_num(u32 mod_num)
  62. {
  63. u32 domain_num;
  64. /* Get the power domain associated with the module number */
  65. domain_num = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
  66. domain_num = PSC_REG_MDCFG_GET_PD(domain_num);
  67. return domain_num;
  68. }
  69. /**
  70. * psc_set_state() - powers up/down a module
  71. * @mod_num: LPSC module number
  72. * @state: 1 to enable, 0 to disable.
  73. *
  74. * Powers up/down the requested module and the associated power domain if
  75. * required. No action is taken it the module is already powered up/down.
  76. * This only controls modules. The domain in which the module resides will
  77. * be left in the power on state. Multiple modules can exist in a power
  78. * domain, so powering down the domain based on a single module is not done.
  79. *
  80. * Return: 0 on success, -1 if the module can't be powered up, or if there is a
  81. * timeout waiting for the transition.
  82. */
  83. int psc_set_state(u32 mod_num, u32 state)
  84. {
  85. u32 domain_num;
  86. u32 pdctl;
  87. u32 mdctl;
  88. u32 ptcmd;
  89. u32 reset_iso;
  90. u32 v;
  91. /*
  92. * Get the power domain associated with the module number, and reset
  93. * isolation functionality
  94. */
  95. v = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
  96. domain_num = PSC_REG_MDCFG_GET_PD(v);
  97. reset_iso = PSC_REG_MDCFG_GET_RESET_ISO(v);
  98. /* Wait for the status of the domain/module to be non-transitional */
  99. if (psc_wait(domain_num) != 0)
  100. return -1;
  101. /*
  102. * Perform configuration even if the current status matches the
  103. * existing state
  104. *
  105. * Set the next state of the power domain to on. It's OK if the domain
  106. * is always on. This code will not ever power down a domain, so no
  107. * change is made if the new state is power down.
  108. */
  109. if (state == PSC_REG_VAL_MDCTL_NEXT_ON) {
  110. pdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
  111. pdctl = PSC_REG_PDCTL_SET_NEXT(pdctl,
  112. PSC_REG_VAL_PDCTL_NEXT_ON);
  113. __raw_writel(pdctl, KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
  114. }
  115. /* Set the next state for the module to enabled/disabled */
  116. mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  117. mdctl = PSC_REG_MDCTL_SET_NEXT(mdctl, state);
  118. mdctl = PSC_REG_MDCTL_SET_RESET_ISO(mdctl, reset_iso);
  119. __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  120. /* Trigger the enable */
  121. ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
  122. ptcmd |= (u32)(1<<domain_num);
  123. __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
  124. /* Wait on the complete */
  125. return psc_wait(domain_num);
  126. }
  127. /**
  128. * psc_enable_module() - power up a module
  129. * @mod_num: LPSC module number
  130. *
  131. * Powers up the requested module and the associated power domain
  132. * if required. No action is taken it the module is already powered up.
  133. *
  134. * Return: 0 on success, -1 if the module can't be powered up, or
  135. * if there is a timeout waiting for the transition.
  136. *
  137. */
  138. int psc_enable_module(u32 mod_num)
  139. {
  140. u32 mdctl;
  141. /* Set the bit to apply reset */
  142. mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  143. if ((mdctl & 0x3f) == PSC_REG_VAL_MDSTAT_STATE_ON)
  144. return 0;
  145. return psc_set_state(mod_num, PSC_REG_VAL_MDCTL_NEXT_ON);
  146. }
  147. /**
  148. * psc_disable_module() - Power down a module
  149. * @mod_num: LPSC module number
  150. *
  151. * Return: 0 on success, -1 on failure or timeout.
  152. */
  153. int psc_disable_module(u32 mod_num)
  154. {
  155. u32 mdctl;
  156. /* Set the bit to apply reset */
  157. mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  158. if ((mdctl & 0x3f) == 0)
  159. return 0;
  160. mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0);
  161. __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  162. return psc_set_state(mod_num, PSC_REG_VAL_MDCTL_NEXT_SWRSTDISABLE);
  163. }
  164. /**
  165. * psc_set_reset_iso() - Set the reset isolation bit in mdctl
  166. * @mod_num: LPSC module number
  167. *
  168. * The reset isolation enable bit is set. The state of the module is not
  169. * changed.
  170. *
  171. * Return: 0 if the module config showed that reset isolation is supported.
  172. * Returns 1 otherwise. This is not an error, but setting the bit in mdctl
  173. * has no effect.
  174. */
  175. int psc_set_reset_iso(u32 mod_num)
  176. {
  177. u32 v;
  178. u32 mdctl;
  179. /* Set the reset isolation bit */
  180. mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  181. mdctl = PSC_REG_MDCTL_SET_RESET_ISO(mdctl, 1);
  182. __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  183. v = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCFG(mod_num));
  184. if (PSC_REG_MDCFG_GET_RESET_ISO(v) == 1)
  185. return 0;
  186. return 1;
  187. }
  188. /**
  189. * psc_disable_domain() - Disable a power domain
  190. * @domain_num: GPSC domain number
  191. */
  192. int psc_disable_domain(u32 domain_num)
  193. {
  194. u32 pdctl;
  195. u32 ptcmd;
  196. pdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
  197. pdctl = PSC_REG_PDCTL_SET_NEXT(pdctl, PSC_REG_VAL_PDCTL_NEXT_OFF);
  198. pdctl = PSC_REG_PDCTL_SET_PDMODE(pdctl, PSC_REG_VAL_PDCTL_PDMODE_SLEEP);
  199. __raw_writel(pdctl, KS2_PSC_BASE + PSC_REG_PDCTL(domain_num));
  200. ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
  201. ptcmd |= (u32)(1 << domain_num);
  202. __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
  203. return psc_wait(domain_num);
  204. }
  205. /**
  206. * psc_module_keep_in_reset_enabled() - Keep module in enabled,in-reset state
  207. * @mod_num: LPSC module number
  208. * @gate_clocks: Can the clocks be gated on this module?
  209. *
  210. * Enable the module, but do not release the module from local reset. This is
  211. * necessary for many processor systems on keystone SoCs to allow for system
  212. * initialization from a master processor prior to releasing the processor
  213. * from reset.
  214. */
  215. int psc_module_keep_in_reset_enabled(u32 mod_num, bool gate_clocks)
  216. {
  217. u32 mdctl, ptcmd, mdstat;
  218. u32 next_state;
  219. int domain_num = psc_get_domain_num(mod_num);
  220. int timeout = 100000;
  221. /* Wait for any previous transitions to complete */
  222. psc_wait(domain_num);
  223. mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  224. /* Should be set 0 to assert Local reset */
  225. if ((mdctl & PSC_REG_MDCTL_SET_LRSTZ(mdctl, 1))) {
  226. mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0);
  227. __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  228. /* Wait for transition to take place */
  229. psc_wait(domain_num);
  230. }
  231. /* Clear Module reset */
  232. mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  233. next_state = gate_clocks ? PSC_REG_VAL_MDCTL_NEXT_OFF :
  234. PSC_REG_VAL_MDCTL_NEXT_ON;
  235. mdctl = PSC_REG_MDCTL_SET_NEXT(mdctl, next_state);
  236. __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  237. /* Trigger PD transition */
  238. ptcmd = __raw_readl(KS2_PSC_BASE + PSC_REG_PTCMD);
  239. ptcmd |= (u32)(1 << domain_num);
  240. __raw_writel(ptcmd, KS2_PSC_BASE + PSC_REG_PTCMD);
  241. psc_wait(domain_num);
  242. mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
  243. while (timeout) {
  244. mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
  245. if (!(PSC_REG_MDSTAT_GET_STATUS(mdstat) & 0x30) &&
  246. PSC_REG_MDSTAT_GET_MRSTDONE(mdstat) &&
  247. PSC_REG_MDSTAT_GET_LRSTDONE(mdstat))
  248. break;
  249. timeout--;
  250. }
  251. if (!timeout) {
  252. printf("%s: Timedout waiting for mdstat(0x%08x) to change\n",
  253. __func__, mdstat);
  254. return -ETIMEDOUT;
  255. }
  256. return 0;
  257. }
  258. /**
  259. * psc_module_release_from_reset() - Release the module from reset
  260. * @mod_num: LPSC module number
  261. *
  262. * This is the follow through for the command 'psc_module_keep_in_reset_enabled'
  263. * Allowing the module to be released from reset once all required inits are
  264. * complete for the module. Typically, this allows the processor module to start
  265. * execution.
  266. */
  267. int psc_module_release_from_reset(u32 mod_num)
  268. {
  269. u32 mdctl, mdstat;
  270. int domain_num = psc_get_domain_num(mod_num);
  271. int timeout = 100000;
  272. /* Wait for any previous transitions to complete */
  273. psc_wait(domain_num);
  274. mdctl = __raw_readl(KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  275. /* Should be set to 1 to de-assert Local reset */
  276. if ((mdctl & PSC_REG_MDCTL_SET_LRSTZ(mdctl, 0))) {
  277. mdctl = PSC_REG_MDCTL_SET_LRSTZ(mdctl, 1);
  278. __raw_writel(mdctl, KS2_PSC_BASE + PSC_REG_MDCTL(mod_num));
  279. /* Wait for transition to take place */
  280. psc_wait(domain_num);
  281. }
  282. mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
  283. while (timeout) {
  284. mdstat = __raw_readl(KS2_PSC_BASE + PSC_REG_MDSTAT(mod_num));
  285. if (!(PSC_REG_MDSTAT_GET_STATUS(mdstat) & 0x30) &&
  286. PSC_REG_MDSTAT_GET_MRSTDONE(mdstat) &&
  287. PSC_REG_MDSTAT_GET_LRSTDONE(mdstat))
  288. break;
  289. timeout--;
  290. }
  291. if (!timeout) {
  292. printf("%s: Timedout waiting for mdstat(0x%08x) to change\n",
  293. __func__, mdstat);
  294. return -ETIMEDOUT;
  295. }
  296. return 0;
  297. }