cm2xxx.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * OMAP2xxx CM module functions
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Copyright (C) 2008-2010, 2012 Texas Instruments, Inc.
  6. * Paul Walmsley
  7. * Rajendra Nayak <rnayak@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/delay.h>
  16. #include <linux/errno.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include "prm2xxx.h"
  20. #include "cm.h"
  21. #include "cm2xxx.h"
  22. #include "cm-regbits-24xx.h"
  23. #include "clockdomain.h"
  24. /* CM_AUTOIDLE_PLL.AUTO_* bit values for DPLLs */
  25. #define DPLL_AUTOIDLE_DISABLE 0x0
  26. #define OMAP2XXX_DPLL_AUTOIDLE_LOW_POWER_STOP 0x3
  27. /* CM_AUTOIDLE_PLL.AUTO_* bit values for APLLs (OMAP2xxx only) */
  28. #define OMAP2XXX_APLL_AUTOIDLE_DISABLE 0x0
  29. #define OMAP2XXX_APLL_AUTOIDLE_LOW_POWER_STOP 0x3
  30. /* CM_IDLEST_PLL bit value offset for APLLs (OMAP2xxx only) */
  31. #define EN_APLL_LOCKED 3
  32. static const u8 omap2xxx_cm_idlest_offs[] = {
  33. CM_IDLEST1, CM_IDLEST2, OMAP2430_CM_IDLEST3, OMAP24XX_CM_IDLEST4
  34. };
  35. /*
  36. *
  37. */
  38. static void _write_clktrctrl(u8 c, s16 module, u32 mask)
  39. {
  40. u32 v;
  41. v = omap2_cm_read_mod_reg(module, OMAP2_CM_CLKSTCTRL);
  42. v &= ~mask;
  43. v |= c << __ffs(mask);
  44. omap2_cm_write_mod_reg(v, module, OMAP2_CM_CLKSTCTRL);
  45. }
  46. static bool omap2xxx_cm_is_clkdm_in_hwsup(s16 module, u32 mask)
  47. {
  48. u32 v;
  49. v = omap2_cm_read_mod_reg(module, OMAP2_CM_CLKSTCTRL);
  50. v &= mask;
  51. v >>= __ffs(mask);
  52. return (v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO) ? 1 : 0;
  53. }
  54. static void omap2xxx_cm_clkdm_enable_hwsup(s16 module, u32 mask)
  55. {
  56. _write_clktrctrl(OMAP24XX_CLKSTCTRL_ENABLE_AUTO, module, mask);
  57. }
  58. static void omap2xxx_cm_clkdm_disable_hwsup(s16 module, u32 mask)
  59. {
  60. _write_clktrctrl(OMAP24XX_CLKSTCTRL_DISABLE_AUTO, module, mask);
  61. }
  62. /*
  63. * DPLL autoidle control
  64. */
  65. static void _omap2xxx_set_dpll_autoidle(u8 m)
  66. {
  67. u32 v;
  68. v = omap2_cm_read_mod_reg(PLL_MOD, CM_AUTOIDLE);
  69. v &= ~OMAP24XX_AUTO_DPLL_MASK;
  70. v |= m << OMAP24XX_AUTO_DPLL_SHIFT;
  71. omap2_cm_write_mod_reg(v, PLL_MOD, CM_AUTOIDLE);
  72. }
  73. void omap2xxx_cm_set_dpll_disable_autoidle(void)
  74. {
  75. _omap2xxx_set_dpll_autoidle(OMAP2XXX_DPLL_AUTOIDLE_LOW_POWER_STOP);
  76. }
  77. void omap2xxx_cm_set_dpll_auto_low_power_stop(void)
  78. {
  79. _omap2xxx_set_dpll_autoidle(DPLL_AUTOIDLE_DISABLE);
  80. }
  81. /*
  82. * APLL control
  83. */
  84. static void _omap2xxx_set_apll_autoidle(u8 m, u32 mask)
  85. {
  86. u32 v;
  87. v = omap2_cm_read_mod_reg(PLL_MOD, CM_AUTOIDLE);
  88. v &= ~mask;
  89. v |= m << __ffs(mask);
  90. omap2_cm_write_mod_reg(v, PLL_MOD, CM_AUTOIDLE);
  91. }
  92. void omap2xxx_cm_set_apll54_disable_autoidle(void)
  93. {
  94. _omap2xxx_set_apll_autoidle(OMAP2XXX_APLL_AUTOIDLE_LOW_POWER_STOP,
  95. OMAP24XX_AUTO_54M_MASK);
  96. }
  97. void omap2xxx_cm_set_apll54_auto_low_power_stop(void)
  98. {
  99. _omap2xxx_set_apll_autoidle(OMAP2XXX_APLL_AUTOIDLE_DISABLE,
  100. OMAP24XX_AUTO_54M_MASK);
  101. }
  102. void omap2xxx_cm_set_apll96_disable_autoidle(void)
  103. {
  104. _omap2xxx_set_apll_autoidle(OMAP2XXX_APLL_AUTOIDLE_LOW_POWER_STOP,
  105. OMAP24XX_AUTO_96M_MASK);
  106. }
  107. void omap2xxx_cm_set_apll96_auto_low_power_stop(void)
  108. {
  109. _omap2xxx_set_apll_autoidle(OMAP2XXX_APLL_AUTOIDLE_DISABLE,
  110. OMAP24XX_AUTO_96M_MASK);
  111. }
  112. /* Enable an APLL if off */
  113. static int _omap2xxx_apll_enable(u8 enable_bit, u8 status_bit)
  114. {
  115. u32 v, m;
  116. m = EN_APLL_LOCKED << enable_bit;
  117. v = omap2_cm_read_mod_reg(PLL_MOD, CM_CLKEN);
  118. if (v & m)
  119. return 0; /* apll already enabled */
  120. v |= m;
  121. omap2_cm_write_mod_reg(v, PLL_MOD, CM_CLKEN);
  122. omap2xxx_cm_wait_module_ready(0, PLL_MOD, 1, status_bit);
  123. /*
  124. * REVISIT: Should we return an error code if
  125. * omap2xxx_cm_wait_module_ready() fails?
  126. */
  127. return 0;
  128. }
  129. /* Stop APLL */
  130. static void _omap2xxx_apll_disable(u8 enable_bit)
  131. {
  132. u32 v;
  133. v = omap2_cm_read_mod_reg(PLL_MOD, CM_CLKEN);
  134. v &= ~(EN_APLL_LOCKED << enable_bit);
  135. omap2_cm_write_mod_reg(v, PLL_MOD, CM_CLKEN);
  136. }
  137. /* Enable an APLL if off */
  138. int omap2xxx_cm_apll54_enable(void)
  139. {
  140. return _omap2xxx_apll_enable(OMAP24XX_EN_54M_PLL_SHIFT,
  141. OMAP24XX_ST_54M_APLL_SHIFT);
  142. }
  143. /* Enable an APLL if off */
  144. int omap2xxx_cm_apll96_enable(void)
  145. {
  146. return _omap2xxx_apll_enable(OMAP24XX_EN_96M_PLL_SHIFT,
  147. OMAP24XX_ST_96M_APLL_SHIFT);
  148. }
  149. /* Stop APLL */
  150. void omap2xxx_cm_apll54_disable(void)
  151. {
  152. _omap2xxx_apll_disable(OMAP24XX_EN_54M_PLL_SHIFT);
  153. }
  154. /* Stop APLL */
  155. void omap2xxx_cm_apll96_disable(void)
  156. {
  157. _omap2xxx_apll_disable(OMAP24XX_EN_96M_PLL_SHIFT);
  158. }
  159. /**
  160. * omap2xxx_cm_split_idlest_reg - split CM_IDLEST reg addr into its components
  161. * @idlest_reg: CM_IDLEST* virtual address
  162. * @prcm_inst: pointer to an s16 to return the PRCM instance offset
  163. * @idlest_reg_id: pointer to a u8 to return the CM_IDLESTx register ID
  164. *
  165. * XXX This function is only needed until absolute register addresses are
  166. * removed from the OMAP struct clk records.
  167. */
  168. static int omap2xxx_cm_split_idlest_reg(void __iomem *idlest_reg,
  169. s16 *prcm_inst,
  170. u8 *idlest_reg_id)
  171. {
  172. unsigned long offs;
  173. u8 idlest_offs;
  174. int i;
  175. if (idlest_reg < cm_base || idlest_reg > (cm_base + 0x0fff))
  176. return -EINVAL;
  177. idlest_offs = (unsigned long)idlest_reg & 0xff;
  178. for (i = 0; i < ARRAY_SIZE(omap2xxx_cm_idlest_offs); i++) {
  179. if (idlest_offs == omap2xxx_cm_idlest_offs[i]) {
  180. *idlest_reg_id = i + 1;
  181. break;
  182. }
  183. }
  184. if (i == ARRAY_SIZE(omap2xxx_cm_idlest_offs))
  185. return -EINVAL;
  186. offs = idlest_reg - cm_base;
  187. offs &= 0xff00;
  188. *prcm_inst = offs;
  189. return 0;
  190. }
  191. /*
  192. *
  193. */
  194. /**
  195. * omap2xxx_cm_wait_module_ready - wait for a module to leave idle or standby
  196. * @part: PRCM partition, ignored for OMAP2
  197. * @prcm_mod: PRCM module offset
  198. * @idlest_id: CM_IDLESTx register ID (i.e., x = 1, 2, 3)
  199. * @idlest_shift: shift of the bit in the CM_IDLEST* register to check
  200. *
  201. * Wait for the PRCM to indicate that the module identified by
  202. * (@prcm_mod, @idlest_id, @idlest_shift) is clocked. Return 0 upon
  203. * success or -EBUSY if the module doesn't enable in time.
  204. */
  205. int omap2xxx_cm_wait_module_ready(u8 part, s16 prcm_mod, u16 idlest_id,
  206. u8 idlest_shift)
  207. {
  208. int ena = 0, i = 0;
  209. u8 cm_idlest_reg;
  210. u32 mask;
  211. if (!idlest_id || (idlest_id > ARRAY_SIZE(omap2xxx_cm_idlest_offs)))
  212. return -EINVAL;
  213. cm_idlest_reg = omap2xxx_cm_idlest_offs[idlest_id - 1];
  214. mask = 1 << idlest_shift;
  215. ena = mask;
  216. omap_test_timeout(((omap2_cm_read_mod_reg(prcm_mod, cm_idlest_reg) &
  217. mask) == ena), MAX_MODULE_READY_TIME, i);
  218. return (i < MAX_MODULE_READY_TIME) ? 0 : -EBUSY;
  219. }
  220. /* Clockdomain low-level functions */
  221. static void omap2xxx_clkdm_allow_idle(struct clockdomain *clkdm)
  222. {
  223. omap2xxx_cm_clkdm_enable_hwsup(clkdm->pwrdm.ptr->prcm_offs,
  224. clkdm->clktrctrl_mask);
  225. }
  226. static void omap2xxx_clkdm_deny_idle(struct clockdomain *clkdm)
  227. {
  228. omap2xxx_cm_clkdm_disable_hwsup(clkdm->pwrdm.ptr->prcm_offs,
  229. clkdm->clktrctrl_mask);
  230. }
  231. static int omap2xxx_clkdm_clk_enable(struct clockdomain *clkdm)
  232. {
  233. bool hwsup = false;
  234. if (!clkdm->clktrctrl_mask)
  235. return 0;
  236. hwsup = omap2xxx_cm_is_clkdm_in_hwsup(clkdm->pwrdm.ptr->prcm_offs,
  237. clkdm->clktrctrl_mask);
  238. if (!hwsup && clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
  239. omap2xxx_clkdm_wakeup(clkdm);
  240. return 0;
  241. }
  242. static int omap2xxx_clkdm_clk_disable(struct clockdomain *clkdm)
  243. {
  244. bool hwsup = false;
  245. if (!clkdm->clktrctrl_mask)
  246. return 0;
  247. hwsup = omap2xxx_cm_is_clkdm_in_hwsup(clkdm->pwrdm.ptr->prcm_offs,
  248. clkdm->clktrctrl_mask);
  249. if (!hwsup && clkdm->flags & CLKDM_CAN_FORCE_SLEEP)
  250. omap2xxx_clkdm_sleep(clkdm);
  251. return 0;
  252. }
  253. struct clkdm_ops omap2_clkdm_operations = {
  254. .clkdm_add_wkdep = omap2_clkdm_add_wkdep,
  255. .clkdm_del_wkdep = omap2_clkdm_del_wkdep,
  256. .clkdm_read_wkdep = omap2_clkdm_read_wkdep,
  257. .clkdm_clear_all_wkdeps = omap2_clkdm_clear_all_wkdeps,
  258. .clkdm_sleep = omap2xxx_clkdm_sleep,
  259. .clkdm_wakeup = omap2xxx_clkdm_wakeup,
  260. .clkdm_allow_idle = omap2xxx_clkdm_allow_idle,
  261. .clkdm_deny_idle = omap2xxx_clkdm_deny_idle,
  262. .clkdm_clk_enable = omap2xxx_clkdm_clk_enable,
  263. .clkdm_clk_disable = omap2xxx_clkdm_clk_disable,
  264. };
  265. int omap2xxx_cm_fclks_active(void)
  266. {
  267. u32 f1, f2;
  268. f1 = omap2_cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
  269. f2 = omap2_cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
  270. return (f1 | f2) ? 1 : 0;
  271. }
  272. int omap2xxx_cm_mpu_retention_allowed(void)
  273. {
  274. u32 l;
  275. /* Check for MMC, UART2, UART1, McSPI2, McSPI1 and DSS1. */
  276. l = omap2_cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
  277. if (l & (OMAP2420_EN_MMC_MASK | OMAP24XX_EN_UART2_MASK |
  278. OMAP24XX_EN_UART1_MASK | OMAP24XX_EN_MCSPI2_MASK |
  279. OMAP24XX_EN_MCSPI1_MASK | OMAP24XX_EN_DSS1_MASK))
  280. return 0;
  281. /* Check for UART3. */
  282. l = omap2_cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
  283. if (l & OMAP24XX_EN_UART3_MASK)
  284. return 0;
  285. return 1;
  286. }
  287. u32 omap2xxx_cm_get_core_clk_src(void)
  288. {
  289. u32 v;
  290. v = omap2_cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
  291. v &= OMAP24XX_CORE_CLK_SRC_MASK;
  292. return v;
  293. }
  294. u32 omap2xxx_cm_get_core_pll_config(void)
  295. {
  296. return omap2_cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
  297. }
  298. void omap2xxx_cm_set_mod_dividers(u32 mpu, u32 dsp, u32 gfx, u32 core, u32 mdm)
  299. {
  300. u32 tmp;
  301. omap2_cm_write_mod_reg(mpu, MPU_MOD, CM_CLKSEL);
  302. omap2_cm_write_mod_reg(dsp, OMAP24XX_DSP_MOD, CM_CLKSEL);
  303. omap2_cm_write_mod_reg(gfx, GFX_MOD, CM_CLKSEL);
  304. tmp = omap2_cm_read_mod_reg(CORE_MOD, CM_CLKSEL1) &
  305. OMAP24XX_CLKSEL_DSS2_MASK;
  306. omap2_cm_write_mod_reg(core | tmp, CORE_MOD, CM_CLKSEL1);
  307. if (mdm)
  308. omap2_cm_write_mod_reg(mdm, OMAP2430_MDM_MOD, CM_CLKSEL);
  309. }
  310. /*
  311. *
  312. */
  313. static struct cm_ll_data omap2xxx_cm_ll_data = {
  314. .split_idlest_reg = &omap2xxx_cm_split_idlest_reg,
  315. .wait_module_ready = &omap2xxx_cm_wait_module_ready,
  316. };
  317. int __init omap2xxx_cm_init(const struct omap_prcm_init_data *data)
  318. {
  319. return cm_register(&omap2xxx_cm_ll_data);
  320. }
  321. static void __exit omap2xxx_cm_exit(void)
  322. {
  323. cm_unregister(&omap2xxx_cm_ll_data);
  324. }
  325. __exitcall(omap2xxx_cm_exit);