psc.c 9.6 KB

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