prm33xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AM33XX PRM functions
  4. *
  5. * Copyright (C) 2011-2012 Texas Instruments Incorporated - https://www.ti.com/
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/errno.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include "powerdomain.h"
  13. #include "prm33xx.h"
  14. #include "prm-regbits-33xx.h"
  15. #define AM33XX_PRM_RSTCTRL_OFFSET 0x0000
  16. #define AM33XX_RST_GLOBAL_WARM_SW_MASK (1 << 0)
  17. /* Read a register in a PRM instance */
  18. static u32 am33xx_prm_read_reg(s16 inst, u16 idx)
  19. {
  20. return readl_relaxed(prm_base.va + inst + idx);
  21. }
  22. /* Write into a register in a PRM instance */
  23. static void am33xx_prm_write_reg(u32 val, s16 inst, u16 idx)
  24. {
  25. writel_relaxed(val, prm_base.va + inst + idx);
  26. }
  27. /* Read-modify-write a register in PRM. Caller must lock */
  28. static u32 am33xx_prm_rmw_reg_bits(u32 mask, u32 bits, s16 inst, s16 idx)
  29. {
  30. u32 v;
  31. v = am33xx_prm_read_reg(inst, idx);
  32. v &= ~mask;
  33. v |= bits;
  34. am33xx_prm_write_reg(v, inst, idx);
  35. return v;
  36. }
  37. /**
  38. * am33xx_prm_is_hardreset_asserted - read the HW reset line state of
  39. * submodules contained in the hwmod module
  40. * @shift: register bit shift corresponding to the reset line to check
  41. * @part: PRM partition, ignored for AM33xx
  42. * @inst: CM instance register offset (*_INST macro)
  43. * @rstctrl_offs: RM_RSTCTRL register address offset for this module
  44. *
  45. * Returns 1 if the (sub)module hardreset line is currently asserted,
  46. * 0 if the (sub)module hardreset line is not currently asserted, or
  47. * -EINVAL upon parameter error.
  48. */
  49. static int am33xx_prm_is_hardreset_asserted(u8 shift, u8 part, s16 inst,
  50. u16 rstctrl_offs)
  51. {
  52. u32 v;
  53. v = am33xx_prm_read_reg(inst, rstctrl_offs);
  54. v &= 1 << shift;
  55. v >>= shift;
  56. return v;
  57. }
  58. /**
  59. * am33xx_prm_assert_hardreset - assert the HW reset line of a submodule
  60. * @shift: register bit shift corresponding to the reset line to assert
  61. * @part: CM partition, ignored for AM33xx
  62. * @inst: CM instance register offset (*_INST macro)
  63. * @rstctrl_reg: RM_RSTCTRL register address for this module
  64. *
  65. * Some IPs like dsp, ipu or iva contain processors that require an HW
  66. * reset line to be asserted / deasserted in order to fully enable the
  67. * IP. These modules may have multiple hard-reset lines that reset
  68. * different 'submodules' inside the IP block. This function will
  69. * place the submodule into reset. Returns 0 upon success or -EINVAL
  70. * upon an argument error.
  71. */
  72. static int am33xx_prm_assert_hardreset(u8 shift, u8 part, s16 inst,
  73. u16 rstctrl_offs)
  74. {
  75. u32 mask = 1 << shift;
  76. am33xx_prm_rmw_reg_bits(mask, mask, inst, rstctrl_offs);
  77. return 0;
  78. }
  79. /**
  80. * am33xx_prm_deassert_hardreset - deassert a submodule hardreset line and
  81. * wait
  82. * @shift: register bit shift corresponding to the reset line to deassert
  83. * @st_shift: reset status register bit shift corresponding to the reset line
  84. * @part: PRM partition, not used for AM33xx
  85. * @inst: CM instance register offset (*_INST macro)
  86. * @rstctrl_reg: RM_RSTCTRL register address for this module
  87. * @rstst_reg: RM_RSTST register address for this module
  88. *
  89. * Some IPs like dsp, ipu or iva contain processors that require an HW
  90. * reset line to be asserted / deasserted in order to fully enable the
  91. * IP. These modules may have multiple hard-reset lines that reset
  92. * different 'submodules' inside the IP block. This function will
  93. * take the submodule out of reset and wait until the PRCM indicates
  94. * that the reset has completed before returning. Returns 0 upon success or
  95. * -EINVAL upon an argument error, -EEXIST if the submodule was already out
  96. * of reset, or -EBUSY if the submodule did not exit reset promptly.
  97. */
  98. static int am33xx_prm_deassert_hardreset(u8 shift, u8 st_shift, u8 part,
  99. s16 inst, u16 rstctrl_offs,
  100. u16 rstst_offs)
  101. {
  102. int c;
  103. u32 mask = 1 << st_shift;
  104. /* Check the current status to avoid de-asserting the line twice */
  105. if (am33xx_prm_is_hardreset_asserted(shift, 0, inst, rstctrl_offs) == 0)
  106. return -EEXIST;
  107. /* Clear the reset status by writing 1 to the status bit */
  108. am33xx_prm_rmw_reg_bits(0xffffffff, mask, inst, rstst_offs);
  109. /* de-assert the reset control line */
  110. mask = 1 << shift;
  111. am33xx_prm_rmw_reg_bits(mask, 0, inst, rstctrl_offs);
  112. /* wait the status to be set */
  113. omap_test_timeout(am33xx_prm_is_hardreset_asserted(st_shift, 0, inst,
  114. rstst_offs),
  115. MAX_MODULE_HARDRESET_WAIT, c);
  116. return (c == MAX_MODULE_HARDRESET_WAIT) ? -EBUSY : 0;
  117. }
  118. static int am33xx_pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
  119. {
  120. am33xx_prm_rmw_reg_bits(OMAP_POWERSTATE_MASK,
  121. (pwrst << OMAP_POWERSTATE_SHIFT),
  122. pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
  123. return 0;
  124. }
  125. static int am33xx_pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
  126. {
  127. u32 v;
  128. v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
  129. v &= OMAP_POWERSTATE_MASK;
  130. v >>= OMAP_POWERSTATE_SHIFT;
  131. return v;
  132. }
  133. static int am33xx_pwrdm_read_pwrst(struct powerdomain *pwrdm)
  134. {
  135. u32 v;
  136. v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstst_offs);
  137. v &= OMAP_POWERSTATEST_MASK;
  138. v >>= OMAP_POWERSTATEST_SHIFT;
  139. return v;
  140. }
  141. static int am33xx_pwrdm_set_lowpwrstchange(struct powerdomain *pwrdm)
  142. {
  143. am33xx_prm_rmw_reg_bits(AM33XX_LOWPOWERSTATECHANGE_MASK,
  144. (1 << AM33XX_LOWPOWERSTATECHANGE_SHIFT),
  145. pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
  146. return 0;
  147. }
  148. static int am33xx_pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
  149. {
  150. am33xx_prm_rmw_reg_bits(AM33XX_LASTPOWERSTATEENTERED_MASK,
  151. AM33XX_LASTPOWERSTATEENTERED_MASK,
  152. pwrdm->prcm_offs, pwrdm->pwrstst_offs);
  153. return 0;
  154. }
  155. static int am33xx_pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
  156. {
  157. u32 m;
  158. m = pwrdm->logicretstate_mask;
  159. if (!m)
  160. return -EINVAL;
  161. am33xx_prm_rmw_reg_bits(m, (pwrst << __ffs(m)),
  162. pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
  163. return 0;
  164. }
  165. static int am33xx_pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
  166. {
  167. u32 v;
  168. v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstst_offs);
  169. v &= AM33XX_LOGICSTATEST_MASK;
  170. v >>= AM33XX_LOGICSTATEST_SHIFT;
  171. return v;
  172. }
  173. static int am33xx_pwrdm_read_logic_retst(struct powerdomain *pwrdm)
  174. {
  175. u32 v, m;
  176. m = pwrdm->logicretstate_mask;
  177. if (!m)
  178. return -EINVAL;
  179. v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
  180. v &= m;
  181. v >>= __ffs(m);
  182. return v;
  183. }
  184. static int am33xx_pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank,
  185. u8 pwrst)
  186. {
  187. u32 m;
  188. m = pwrdm->mem_on_mask[bank];
  189. if (!m)
  190. return -EINVAL;
  191. am33xx_prm_rmw_reg_bits(m, (pwrst << __ffs(m)),
  192. pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
  193. return 0;
  194. }
  195. static int am33xx_pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank,
  196. u8 pwrst)
  197. {
  198. u32 m;
  199. m = pwrdm->mem_ret_mask[bank];
  200. if (!m)
  201. return -EINVAL;
  202. am33xx_prm_rmw_reg_bits(m, (pwrst << __ffs(m)),
  203. pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
  204. return 0;
  205. }
  206. static int am33xx_pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
  207. {
  208. u32 m, v;
  209. m = pwrdm->mem_pwrst_mask[bank];
  210. if (!m)
  211. return -EINVAL;
  212. v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstst_offs);
  213. v &= m;
  214. v >>= __ffs(m);
  215. return v;
  216. }
  217. static int am33xx_pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
  218. {
  219. u32 m, v;
  220. m = pwrdm->mem_retst_mask[bank];
  221. if (!m)
  222. return -EINVAL;
  223. v = am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstctrl_offs);
  224. v &= m;
  225. v >>= __ffs(m);
  226. return v;
  227. }
  228. static int am33xx_pwrdm_wait_transition(struct powerdomain *pwrdm)
  229. {
  230. u32 c = 0;
  231. /*
  232. * REVISIT: pwrdm_wait_transition() may be better implemented
  233. * via a callback and a periodic timer check -- how long do we expect
  234. * powerdomain transitions to take?
  235. */
  236. /* XXX Is this udelay() value meaningful? */
  237. while ((am33xx_prm_read_reg(pwrdm->prcm_offs, pwrdm->pwrstst_offs)
  238. & OMAP_INTRANSITION_MASK) &&
  239. (c++ < PWRDM_TRANSITION_BAILOUT))
  240. udelay(1);
  241. if (c > PWRDM_TRANSITION_BAILOUT) {
  242. pr_err("powerdomain: %s: waited too long to complete transition\n",
  243. pwrdm->name);
  244. return -EAGAIN;
  245. }
  246. pr_debug("powerdomain: completed transition in %d loops\n", c);
  247. return 0;
  248. }
  249. static int am33xx_check_vcvp(void)
  250. {
  251. /* No VC/VP on am33xx devices */
  252. return 0;
  253. }
  254. /**
  255. * am33xx_prm_global_warm_sw_reset - reboot the device via warm reset
  256. *
  257. * Immediately reboots the device through warm reset.
  258. */
  259. static void am33xx_prm_global_warm_sw_reset(void)
  260. {
  261. am33xx_prm_rmw_reg_bits(AM33XX_RST_GLOBAL_WARM_SW_MASK,
  262. AM33XX_RST_GLOBAL_WARM_SW_MASK,
  263. AM33XX_PRM_DEVICE_MOD,
  264. AM33XX_PRM_RSTCTRL_OFFSET);
  265. /* OCP barrier */
  266. (void)am33xx_prm_read_reg(AM33XX_PRM_DEVICE_MOD,
  267. AM33XX_PRM_RSTCTRL_OFFSET);
  268. }
  269. static void am33xx_pwrdm_save_context(struct powerdomain *pwrdm)
  270. {
  271. pwrdm->context = am33xx_prm_read_reg(pwrdm->prcm_offs,
  272. pwrdm->pwrstctrl_offs);
  273. /*
  274. * Do not save LOWPOWERSTATECHANGE, writing a 1 indicates a request,
  275. * reading back a 1 indicates a request in progress.
  276. */
  277. pwrdm->context &= ~AM33XX_LOWPOWERSTATECHANGE_MASK;
  278. }
  279. static void am33xx_pwrdm_restore_context(struct powerdomain *pwrdm)
  280. {
  281. int st, ctrl;
  282. st = am33xx_prm_read_reg(pwrdm->prcm_offs,
  283. pwrdm->pwrstst_offs);
  284. am33xx_prm_write_reg(pwrdm->context, pwrdm->prcm_offs,
  285. pwrdm->pwrstctrl_offs);
  286. /* Make sure we only wait for a transition if there is one */
  287. st &= OMAP_POWERSTATEST_MASK;
  288. ctrl = OMAP_POWERSTATEST_MASK & pwrdm->context;
  289. if (st != ctrl)
  290. am33xx_pwrdm_wait_transition(pwrdm);
  291. }
  292. struct pwrdm_ops am33xx_pwrdm_operations = {
  293. .pwrdm_set_next_pwrst = am33xx_pwrdm_set_next_pwrst,
  294. .pwrdm_read_next_pwrst = am33xx_pwrdm_read_next_pwrst,
  295. .pwrdm_read_pwrst = am33xx_pwrdm_read_pwrst,
  296. .pwrdm_set_logic_retst = am33xx_pwrdm_set_logic_retst,
  297. .pwrdm_read_logic_pwrst = am33xx_pwrdm_read_logic_pwrst,
  298. .pwrdm_read_logic_retst = am33xx_pwrdm_read_logic_retst,
  299. .pwrdm_clear_all_prev_pwrst = am33xx_pwrdm_clear_all_prev_pwrst,
  300. .pwrdm_set_lowpwrstchange = am33xx_pwrdm_set_lowpwrstchange,
  301. .pwrdm_read_mem_pwrst = am33xx_pwrdm_read_mem_pwrst,
  302. .pwrdm_read_mem_retst = am33xx_pwrdm_read_mem_retst,
  303. .pwrdm_set_mem_onst = am33xx_pwrdm_set_mem_onst,
  304. .pwrdm_set_mem_retst = am33xx_pwrdm_set_mem_retst,
  305. .pwrdm_wait_transition = am33xx_pwrdm_wait_transition,
  306. .pwrdm_has_voltdm = am33xx_check_vcvp,
  307. .pwrdm_save_context = am33xx_pwrdm_save_context,
  308. .pwrdm_restore_context = am33xx_pwrdm_restore_context,
  309. };
  310. static struct prm_ll_data am33xx_prm_ll_data = {
  311. .assert_hardreset = am33xx_prm_assert_hardreset,
  312. .deassert_hardreset = am33xx_prm_deassert_hardreset,
  313. .is_hardreset_asserted = am33xx_prm_is_hardreset_asserted,
  314. .reset_system = am33xx_prm_global_warm_sw_reset,
  315. };
  316. int __init am33xx_prm_init(const struct omap_prcm_init_data *data)
  317. {
  318. return prm_register(&am33xx_prm_ll_data);
  319. }
  320. static void __exit am33xx_prm_exit(void)
  321. {
  322. prm_unregister(&am33xx_prm_ll_data);
  323. }
  324. __exitcall(am33xx_prm_exit);