cm33xx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * AM33XX CM functions
  3. *
  4. * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
  5. * Vaibhav Hiremath <hvaibhav@ti.com>
  6. *
  7. * Reference taken from from OMAP4 cminst44xx.c
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation version 2.
  12. *
  13. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  14. * kind, whether express or implied; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/io.h>
  23. #include <plat/common.h>
  24. #include "cm.h"
  25. #include "cm33xx.h"
  26. #include "cm-regbits-34xx.h"
  27. #include "cm-regbits-33xx.h"
  28. #include "prm33xx.h"
  29. /*
  30. * CLKCTRL_IDLEST_*: possible values for the CM_*_CLKCTRL.IDLEST bitfield:
  31. *
  32. * 0x0 func: Module is fully functional, including OCP
  33. * 0x1 trans: Module is performing transition: wakeup, or sleep, or sleep
  34. * abortion
  35. * 0x2 idle: Module is in Idle mode (only OCP part). It is functional if
  36. * using separate functional clock
  37. * 0x3 disabled: Module is disabled and cannot be accessed
  38. *
  39. */
  40. #define CLKCTRL_IDLEST_FUNCTIONAL 0x0
  41. #define CLKCTRL_IDLEST_INTRANSITION 0x1
  42. #define CLKCTRL_IDLEST_INTERFACE_IDLE 0x2
  43. #define CLKCTRL_IDLEST_DISABLED 0x3
  44. /* Private functions */
  45. /* Read a register in a CM instance */
  46. static inline u32 am33xx_cm_read_reg(s16 inst, u16 idx)
  47. {
  48. return __raw_readl(cm_base + inst + idx);
  49. }
  50. /* Write into a register in a CM */
  51. static inline void am33xx_cm_write_reg(u32 val, s16 inst, u16 idx)
  52. {
  53. __raw_writel(val, cm_base + inst + idx);
  54. }
  55. /* Read-modify-write a register in CM */
  56. static inline u32 am33xx_cm_rmw_reg_bits(u32 mask, u32 bits, s16 inst, s16 idx)
  57. {
  58. u32 v;
  59. v = am33xx_cm_read_reg(inst, idx);
  60. v &= ~mask;
  61. v |= bits;
  62. am33xx_cm_write_reg(v, inst, idx);
  63. return v;
  64. }
  65. static inline u32 am33xx_cm_set_reg_bits(u32 bits, s16 inst, s16 idx)
  66. {
  67. return am33xx_cm_rmw_reg_bits(bits, bits, inst, idx);
  68. }
  69. static inline u32 am33xx_cm_clear_reg_bits(u32 bits, s16 inst, s16 idx)
  70. {
  71. return am33xx_cm_rmw_reg_bits(bits, 0x0, inst, idx);
  72. }
  73. static inline u32 am33xx_cm_read_reg_bits(u16 inst, s16 idx, u32 mask)
  74. {
  75. u32 v;
  76. v = am33xx_cm_read_reg(inst, idx);
  77. v &= mask;
  78. v >>= __ffs(mask);
  79. return v;
  80. }
  81. /**
  82. * _clkctrl_idlest - read a CM_*_CLKCTRL register; mask & shift IDLEST bitfield
  83. * @inst: CM instance register offset (*_INST macro)
  84. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  85. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  86. *
  87. * Return the IDLEST bitfield of a CM_*_CLKCTRL register, shifted down to
  88. * bit 0.
  89. */
  90. static u32 _clkctrl_idlest(u16 inst, s16 cdoffs, u16 clkctrl_offs)
  91. {
  92. u32 v = am33xx_cm_read_reg(inst, clkctrl_offs);
  93. v &= AM33XX_IDLEST_MASK;
  94. v >>= AM33XX_IDLEST_SHIFT;
  95. return v;
  96. }
  97. /**
  98. * _is_module_ready - can module registers be accessed without causing an abort?
  99. * @inst: CM instance register offset (*_INST macro)
  100. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  101. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  102. *
  103. * Returns true if the module's CM_*_CLKCTRL.IDLEST bitfield is either
  104. * *FUNCTIONAL or *INTERFACE_IDLE; false otherwise.
  105. */
  106. static bool _is_module_ready(u16 inst, s16 cdoffs, u16 clkctrl_offs)
  107. {
  108. u32 v;
  109. v = _clkctrl_idlest(inst, cdoffs, clkctrl_offs);
  110. return (v == CLKCTRL_IDLEST_FUNCTIONAL ||
  111. v == CLKCTRL_IDLEST_INTERFACE_IDLE) ? true : false;
  112. }
  113. /**
  114. * _clktrctrl_write - write @c to a CM_CLKSTCTRL.CLKTRCTRL register bitfield
  115. * @c: CLKTRCTRL register bitfield (LSB = bit 0, i.e., unshifted)
  116. * @inst: CM instance register offset (*_INST macro)
  117. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  118. *
  119. * @c must be the unshifted value for CLKTRCTRL - i.e., this function
  120. * will handle the shift itself.
  121. */
  122. static void _clktrctrl_write(u8 c, s16 inst, u16 cdoffs)
  123. {
  124. u32 v;
  125. v = am33xx_cm_read_reg(inst, cdoffs);
  126. v &= ~AM33XX_CLKTRCTRL_MASK;
  127. v |= c << AM33XX_CLKTRCTRL_SHIFT;
  128. am33xx_cm_write_reg(v, inst, cdoffs);
  129. }
  130. /* Public functions */
  131. /**
  132. * am33xx_cm_is_clkdm_in_hwsup - is a clockdomain in hwsup idle mode?
  133. * @inst: CM instance register offset (*_INST macro)
  134. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  135. *
  136. * Returns true if the clockdomain referred to by (@inst, @cdoffs)
  137. * is in hardware-supervised idle mode, or 0 otherwise.
  138. */
  139. bool am33xx_cm_is_clkdm_in_hwsup(s16 inst, u16 cdoffs)
  140. {
  141. u32 v;
  142. v = am33xx_cm_read_reg(inst, cdoffs);
  143. v &= AM33XX_CLKTRCTRL_MASK;
  144. v >>= AM33XX_CLKTRCTRL_SHIFT;
  145. return (v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ? true : false;
  146. }
  147. /**
  148. * am33xx_cm_clkdm_enable_hwsup - put a clockdomain in hwsup-idle mode
  149. * @inst: CM instance register offset (*_INST macro)
  150. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  151. *
  152. * Put a clockdomain referred to by (@inst, @cdoffs) into
  153. * hardware-supervised idle mode. No return value.
  154. */
  155. void am33xx_cm_clkdm_enable_hwsup(s16 inst, u16 cdoffs)
  156. {
  157. _clktrctrl_write(OMAP34XX_CLKSTCTRL_ENABLE_AUTO, inst, cdoffs);
  158. }
  159. /**
  160. * am33xx_cm_clkdm_disable_hwsup - put a clockdomain in swsup-idle mode
  161. * @inst: CM instance register offset (*_INST macro)
  162. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  163. *
  164. * Put a clockdomain referred to by (@inst, @cdoffs) into
  165. * software-supervised idle mode, i.e., controlled manually by the
  166. * Linux OMAP clockdomain code. No return value.
  167. */
  168. void am33xx_cm_clkdm_disable_hwsup(s16 inst, u16 cdoffs)
  169. {
  170. _clktrctrl_write(OMAP34XX_CLKSTCTRL_DISABLE_AUTO, inst, cdoffs);
  171. }
  172. /**
  173. * am33xx_cm_clkdm_force_sleep - try to put a clockdomain into idle
  174. * @inst: CM instance register offset (*_INST macro)
  175. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  176. *
  177. * Put a clockdomain referred to by (@inst, @cdoffs) into idle
  178. * No return value.
  179. */
  180. void am33xx_cm_clkdm_force_sleep(s16 inst, u16 cdoffs)
  181. {
  182. _clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_SLEEP, inst, cdoffs);
  183. }
  184. /**
  185. * am33xx_cm_clkdm_force_wakeup - try to take a clockdomain out of idle
  186. * @inst: CM instance register offset (*_INST macro)
  187. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  188. *
  189. * Take a clockdomain referred to by (@inst, @cdoffs) out of idle,
  190. * waking it up. No return value.
  191. */
  192. void am33xx_cm_clkdm_force_wakeup(s16 inst, u16 cdoffs)
  193. {
  194. _clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_WAKEUP, inst, cdoffs);
  195. }
  196. /*
  197. *
  198. */
  199. /**
  200. * am33xx_cm_wait_module_ready - wait for a module to be in 'func' state
  201. * @inst: CM instance register offset (*_INST macro)
  202. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  203. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  204. *
  205. * Wait for the module IDLEST to be functional. If the idle state is in any
  206. * the non functional state (trans, idle or disabled), module and thus the
  207. * sysconfig cannot be accessed and will probably lead to an "imprecise
  208. * external abort"
  209. */
  210. int am33xx_cm_wait_module_ready(u16 inst, s16 cdoffs, u16 clkctrl_offs)
  211. {
  212. int i = 0;
  213. if (!clkctrl_offs)
  214. return 0;
  215. omap_test_timeout(_is_module_ready(inst, cdoffs, clkctrl_offs),
  216. MAX_MODULE_READY_TIME, i);
  217. return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;
  218. }
  219. /**
  220. * am33xx_cm_wait_module_idle - wait for a module to be in 'disabled'
  221. * state
  222. * @inst: CM instance register offset (*_INST macro)
  223. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  224. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  225. *
  226. * Wait for the module IDLEST to be disabled. Some PRCM transition,
  227. * like reset assertion or parent clock de-activation must wait the
  228. * module to be fully disabled.
  229. */
  230. int am33xx_cm_wait_module_idle(u16 inst, s16 cdoffs, u16 clkctrl_offs)
  231. {
  232. int i = 0;
  233. if (!clkctrl_offs)
  234. return 0;
  235. omap_test_timeout((_clkctrl_idlest(inst, cdoffs, clkctrl_offs) ==
  236. CLKCTRL_IDLEST_DISABLED),
  237. MAX_MODULE_READY_TIME, i);
  238. return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;
  239. }
  240. /**
  241. * am33xx_cm_module_enable - Enable the modulemode inside CLKCTRL
  242. * @mode: Module mode (SW or HW)
  243. * @inst: CM instance register offset (*_INST macro)
  244. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  245. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  246. *
  247. * No return value.
  248. */
  249. void am33xx_cm_module_enable(u8 mode, u16 inst, s16 cdoffs, u16 clkctrl_offs)
  250. {
  251. u32 v;
  252. v = am33xx_cm_read_reg(inst, clkctrl_offs);
  253. v &= ~AM33XX_MODULEMODE_MASK;
  254. v |= mode << AM33XX_MODULEMODE_SHIFT;
  255. am33xx_cm_write_reg(v, inst, clkctrl_offs);
  256. }
  257. /**
  258. * am33xx_cm_module_disable - Disable the module inside CLKCTRL
  259. * @inst: CM instance register offset (*_INST macro)
  260. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  261. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  262. *
  263. * No return value.
  264. */
  265. void am33xx_cm_module_disable(u16 inst, s16 cdoffs, u16 clkctrl_offs)
  266. {
  267. u32 v;
  268. v = am33xx_cm_read_reg(inst, clkctrl_offs);
  269. v &= ~AM33XX_MODULEMODE_MASK;
  270. am33xx_cm_write_reg(v, inst, clkctrl_offs);
  271. }