cm33xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 "clockdomain.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(u16 inst, u16 idx)
  47. {
  48. return readl_relaxed(cm_base + inst + idx);
  49. }
  50. /* Write into a register in a CM */
  51. static inline void am33xx_cm_write_reg(u32 val, u16 inst, u16 idx)
  52. {
  53. writel_relaxed(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_read_reg_bits(u16 inst, s16 idx, u32 mask)
  66. {
  67. u32 v;
  68. v = am33xx_cm_read_reg(inst, idx);
  69. v &= mask;
  70. v >>= __ffs(mask);
  71. return v;
  72. }
  73. /**
  74. * _clkctrl_idlest - read a CM_*_CLKCTRL register; mask & shift IDLEST bitfield
  75. * @inst: CM instance register offset (*_INST macro)
  76. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  77. *
  78. * Return the IDLEST bitfield of a CM_*_CLKCTRL register, shifted down to
  79. * bit 0.
  80. */
  81. static u32 _clkctrl_idlest(u16 inst, u16 clkctrl_offs)
  82. {
  83. u32 v = am33xx_cm_read_reg(inst, clkctrl_offs);
  84. v &= AM33XX_IDLEST_MASK;
  85. v >>= AM33XX_IDLEST_SHIFT;
  86. return v;
  87. }
  88. /**
  89. * _is_module_ready - can module registers be accessed without causing an abort?
  90. * @inst: CM instance register offset (*_INST macro)
  91. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  92. *
  93. * Returns true if the module's CM_*_CLKCTRL.IDLEST bitfield is either
  94. * *FUNCTIONAL or *INTERFACE_IDLE; false otherwise.
  95. */
  96. static bool _is_module_ready(u16 inst, u16 clkctrl_offs)
  97. {
  98. u32 v;
  99. v = _clkctrl_idlest(inst, clkctrl_offs);
  100. return (v == CLKCTRL_IDLEST_FUNCTIONAL ||
  101. v == CLKCTRL_IDLEST_INTERFACE_IDLE) ? true : false;
  102. }
  103. /**
  104. * _clktrctrl_write - write @c to a CM_CLKSTCTRL.CLKTRCTRL register bitfield
  105. * @c: CLKTRCTRL register bitfield (LSB = bit 0, i.e., unshifted)
  106. * @inst: CM instance register offset (*_INST macro)
  107. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  108. *
  109. * @c must be the unshifted value for CLKTRCTRL - i.e., this function
  110. * will handle the shift itself.
  111. */
  112. static void _clktrctrl_write(u8 c, u16 inst, u16 cdoffs)
  113. {
  114. u32 v;
  115. v = am33xx_cm_read_reg(inst, cdoffs);
  116. v &= ~AM33XX_CLKTRCTRL_MASK;
  117. v |= c << AM33XX_CLKTRCTRL_SHIFT;
  118. am33xx_cm_write_reg(v, inst, cdoffs);
  119. }
  120. /* Public functions */
  121. /**
  122. * am33xx_cm_is_clkdm_in_hwsup - is a clockdomain in hwsup idle mode?
  123. * @inst: CM instance register offset (*_INST macro)
  124. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  125. *
  126. * Returns true if the clockdomain referred to by (@inst, @cdoffs)
  127. * is in hardware-supervised idle mode, or 0 otherwise.
  128. */
  129. static bool am33xx_cm_is_clkdm_in_hwsup(u16 inst, u16 cdoffs)
  130. {
  131. u32 v;
  132. v = am33xx_cm_read_reg(inst, cdoffs);
  133. v &= AM33XX_CLKTRCTRL_MASK;
  134. v >>= AM33XX_CLKTRCTRL_SHIFT;
  135. return (v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ? true : false;
  136. }
  137. /**
  138. * am33xx_cm_clkdm_enable_hwsup - put a clockdomain in hwsup-idle mode
  139. * @inst: CM instance register offset (*_INST macro)
  140. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  141. *
  142. * Put a clockdomain referred to by (@inst, @cdoffs) into
  143. * hardware-supervised idle mode. No return value.
  144. */
  145. static void am33xx_cm_clkdm_enable_hwsup(u16 inst, u16 cdoffs)
  146. {
  147. _clktrctrl_write(OMAP34XX_CLKSTCTRL_ENABLE_AUTO, inst, cdoffs);
  148. }
  149. /**
  150. * am33xx_cm_clkdm_disable_hwsup - put a clockdomain in swsup-idle mode
  151. * @inst: CM instance register offset (*_INST macro)
  152. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  153. *
  154. * Put a clockdomain referred to by (@inst, @cdoffs) into
  155. * software-supervised idle mode, i.e., controlled manually by the
  156. * Linux OMAP clockdomain code. No return value.
  157. */
  158. static void am33xx_cm_clkdm_disable_hwsup(u16 inst, u16 cdoffs)
  159. {
  160. _clktrctrl_write(OMAP34XX_CLKSTCTRL_DISABLE_AUTO, inst, cdoffs);
  161. }
  162. /**
  163. * am33xx_cm_clkdm_force_sleep - try to put a clockdomain into idle
  164. * @inst: CM instance register offset (*_INST macro)
  165. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  166. *
  167. * Put a clockdomain referred to by (@inst, @cdoffs) into idle
  168. * No return value.
  169. */
  170. static void am33xx_cm_clkdm_force_sleep(u16 inst, u16 cdoffs)
  171. {
  172. _clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_SLEEP, inst, cdoffs);
  173. }
  174. /**
  175. * am33xx_cm_clkdm_force_wakeup - try to take a clockdomain out of idle
  176. * @inst: CM instance register offset (*_INST macro)
  177. * @cdoffs: Clockdomain register offset (*_CDOFFS macro)
  178. *
  179. * Take a clockdomain referred to by (@inst, @cdoffs) out of idle,
  180. * waking it up. No return value.
  181. */
  182. static void am33xx_cm_clkdm_force_wakeup(u16 inst, u16 cdoffs)
  183. {
  184. _clktrctrl_write(OMAP34XX_CLKSTCTRL_FORCE_WAKEUP, inst, cdoffs);
  185. }
  186. /*
  187. *
  188. */
  189. /**
  190. * am33xx_cm_wait_module_ready - wait for a module to be in 'func' state
  191. * @part: PRCM partition, ignored for AM33xx
  192. * @inst: CM instance register offset (*_INST macro)
  193. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  194. * @bit_shift: bit shift for the register, ignored for AM33xx
  195. *
  196. * Wait for the module IDLEST to be functional. If the idle state is in any
  197. * the non functional state (trans, idle or disabled), module and thus the
  198. * sysconfig cannot be accessed and will probably lead to an "imprecise
  199. * external abort"
  200. */
  201. static int am33xx_cm_wait_module_ready(u8 part, s16 inst, u16 clkctrl_offs,
  202. u8 bit_shift)
  203. {
  204. int i = 0;
  205. omap_test_timeout(_is_module_ready(inst, clkctrl_offs),
  206. MAX_MODULE_READY_TIME, i);
  207. return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;
  208. }
  209. /**
  210. * am33xx_cm_wait_module_idle - wait for a module to be in 'disabled'
  211. * state
  212. * @part: CM partition, ignored for AM33xx
  213. * @inst: CM instance register offset (*_INST macro)
  214. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  215. * @bit_shift: bit shift for the register, ignored for AM33xx
  216. *
  217. * Wait for the module IDLEST to be disabled. Some PRCM transition,
  218. * like reset assertion or parent clock de-activation must wait the
  219. * module to be fully disabled.
  220. */
  221. static int am33xx_cm_wait_module_idle(u8 part, s16 inst, u16 clkctrl_offs,
  222. u8 bit_shift)
  223. {
  224. int i = 0;
  225. omap_test_timeout((_clkctrl_idlest(inst, clkctrl_offs) ==
  226. CLKCTRL_IDLEST_DISABLED),
  227. MAX_MODULE_READY_TIME, i);
  228. return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;
  229. }
  230. /**
  231. * am33xx_cm_module_enable - Enable the modulemode inside CLKCTRL
  232. * @mode: Module mode (SW or HW)
  233. * @part: CM partition, ignored for AM33xx
  234. * @inst: CM instance register offset (*_INST macro)
  235. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  236. *
  237. * No return value.
  238. */
  239. static void am33xx_cm_module_enable(u8 mode, u8 part, u16 inst,
  240. u16 clkctrl_offs)
  241. {
  242. u32 v;
  243. v = am33xx_cm_read_reg(inst, clkctrl_offs);
  244. v &= ~AM33XX_MODULEMODE_MASK;
  245. v |= mode << AM33XX_MODULEMODE_SHIFT;
  246. am33xx_cm_write_reg(v, inst, clkctrl_offs);
  247. }
  248. /**
  249. * am33xx_cm_module_disable - Disable the module inside CLKCTRL
  250. * @part: CM partition, ignored for AM33xx
  251. * @inst: CM instance register offset (*_INST macro)
  252. * @clkctrl_offs: Module clock control register offset (*_CLKCTRL macro)
  253. *
  254. * No return value.
  255. */
  256. static void am33xx_cm_module_disable(u8 part, u16 inst, u16 clkctrl_offs)
  257. {
  258. u32 v;
  259. v = am33xx_cm_read_reg(inst, clkctrl_offs);
  260. v &= ~AM33XX_MODULEMODE_MASK;
  261. am33xx_cm_write_reg(v, inst, clkctrl_offs);
  262. }
  263. /*
  264. * Clockdomain low-level functions
  265. */
  266. static int am33xx_clkdm_sleep(struct clockdomain *clkdm)
  267. {
  268. am33xx_cm_clkdm_force_sleep(clkdm->cm_inst, clkdm->clkdm_offs);
  269. return 0;
  270. }
  271. static int am33xx_clkdm_wakeup(struct clockdomain *clkdm)
  272. {
  273. am33xx_cm_clkdm_force_wakeup(clkdm->cm_inst, clkdm->clkdm_offs);
  274. return 0;
  275. }
  276. static void am33xx_clkdm_allow_idle(struct clockdomain *clkdm)
  277. {
  278. am33xx_cm_clkdm_enable_hwsup(clkdm->cm_inst, clkdm->clkdm_offs);
  279. }
  280. static void am33xx_clkdm_deny_idle(struct clockdomain *clkdm)
  281. {
  282. am33xx_cm_clkdm_disable_hwsup(clkdm->cm_inst, clkdm->clkdm_offs);
  283. }
  284. static int am33xx_clkdm_clk_enable(struct clockdomain *clkdm)
  285. {
  286. if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
  287. return am33xx_clkdm_wakeup(clkdm);
  288. return 0;
  289. }
  290. static int am33xx_clkdm_clk_disable(struct clockdomain *clkdm)
  291. {
  292. bool hwsup = false;
  293. hwsup = am33xx_cm_is_clkdm_in_hwsup(clkdm->cm_inst, clkdm->clkdm_offs);
  294. if (!hwsup && (clkdm->flags & CLKDM_CAN_FORCE_SLEEP))
  295. am33xx_clkdm_sleep(clkdm);
  296. return 0;
  297. }
  298. /**
  299. * am33xx_clkdm_save_context - Save the clockdomain transition context
  300. * @clkdm: The clockdomain pointer whose context needs to be saved
  301. *
  302. * Save the clockdomain transition context.
  303. */
  304. static int am33xx_clkdm_save_context(struct clockdomain *clkdm)
  305. {
  306. clkdm->context = am33xx_cm_read_reg_bits(clkdm->cm_inst,
  307. clkdm->clkdm_offs, AM33XX_CLKTRCTRL_MASK);
  308. return 0;
  309. }
  310. /**
  311. * am33xx_restore_save_context - Restore the clockdomain transition context
  312. * @clkdm: The clockdomain pointer whose context needs to be restored
  313. *
  314. * Restore the clockdomain transition context.
  315. */
  316. static int am33xx_clkdm_restore_context(struct clockdomain *clkdm)
  317. {
  318. switch (clkdm->context) {
  319. case OMAP34XX_CLKSTCTRL_DISABLE_AUTO:
  320. am33xx_clkdm_deny_idle(clkdm);
  321. break;
  322. case OMAP34XX_CLKSTCTRL_FORCE_SLEEP:
  323. am33xx_clkdm_sleep(clkdm);
  324. break;
  325. case OMAP34XX_CLKSTCTRL_FORCE_WAKEUP:
  326. am33xx_clkdm_wakeup(clkdm);
  327. break;
  328. case OMAP34XX_CLKSTCTRL_ENABLE_AUTO:
  329. am33xx_clkdm_allow_idle(clkdm);
  330. break;
  331. }
  332. return 0;
  333. }
  334. struct clkdm_ops am33xx_clkdm_operations = {
  335. .clkdm_sleep = am33xx_clkdm_sleep,
  336. .clkdm_wakeup = am33xx_clkdm_wakeup,
  337. .clkdm_allow_idle = am33xx_clkdm_allow_idle,
  338. .clkdm_deny_idle = am33xx_clkdm_deny_idle,
  339. .clkdm_clk_enable = am33xx_clkdm_clk_enable,
  340. .clkdm_clk_disable = am33xx_clkdm_clk_disable,
  341. .clkdm_save_context = am33xx_clkdm_save_context,
  342. .clkdm_restore_context = am33xx_clkdm_restore_context,
  343. };
  344. static struct cm_ll_data am33xx_cm_ll_data = {
  345. .wait_module_ready = &am33xx_cm_wait_module_ready,
  346. .wait_module_idle = &am33xx_cm_wait_module_idle,
  347. .module_enable = &am33xx_cm_module_enable,
  348. .module_disable = &am33xx_cm_module_disable,
  349. };
  350. int __init am33xx_cm_init(const struct omap_prcm_init_data *data)
  351. {
  352. return cm_register(&am33xx_cm_ll_data);
  353. }
  354. static void __exit am33xx_cm_exit(void)
  355. {
  356. cm_unregister(&am33xx_cm_ll_data);
  357. }
  358. __exitcall(am33xx_cm_exit);