clock.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * linux/arch/arm/mach-omap2/clock.c
  3. *
  4. * Copyright (C) 2005-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2004-2008 Nokia Corporation
  6. *
  7. * Contacts:
  8. * Richard Woodruff <r-woodruff2@ti.com>
  9. * Paul Walmsley
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #undef DEBUG
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/list.h>
  20. #include <linux/errno.h>
  21. #include <linux/delay.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/bitops.h>
  25. #include <plat/clock.h>
  26. #include <plat/clockdomain.h>
  27. #include <plat/cpu.h>
  28. #include <plat/prcm.h>
  29. #include "clock.h"
  30. #include "prm.h"
  31. #include "prm-regbits-24xx.h"
  32. #include "cm.h"
  33. #include "cm-regbits-24xx.h"
  34. #include "cm-regbits-34xx.h"
  35. u8 cpu_mask;
  36. /*-------------------------------------------------------------------------
  37. * OMAP2/3/4 specific clock functions
  38. *-------------------------------------------------------------------------*/
  39. /* Private functions */
  40. /**
  41. * _omap2_module_wait_ready - wait for an OMAP module to leave IDLE
  42. * @clk: struct clk * belonging to the module
  43. *
  44. * If the necessary clocks for the OMAP hardware IP block that
  45. * corresponds to clock @clk are enabled, then wait for the module to
  46. * indicate readiness (i.e., to leave IDLE). This code does not
  47. * belong in the clock code and will be moved in the medium term to
  48. * module-dependent code. No return value.
  49. */
  50. static void _omap2_module_wait_ready(struct clk *clk)
  51. {
  52. void __iomem *companion_reg, *idlest_reg;
  53. u8 other_bit, idlest_bit;
  54. /* Not all modules have multiple clocks that their IDLEST depends on */
  55. if (clk->ops->find_companion) {
  56. clk->ops->find_companion(clk, &companion_reg, &other_bit);
  57. if (!(__raw_readl(companion_reg) & (1 << other_bit)))
  58. return;
  59. }
  60. clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit);
  61. omap2_cm_wait_idlest(idlest_reg, (1 << idlest_bit), clk->name);
  62. }
  63. /* Enables clock without considering parent dependencies or use count
  64. * REVISIT: Maybe change this to use clk->enable like on omap1?
  65. */
  66. static int _omap2_clk_enable(struct clk *clk)
  67. {
  68. return clk->ops->enable(clk);
  69. }
  70. /* Disables clock without considering parent dependencies or use count */
  71. static void _omap2_clk_disable(struct clk *clk)
  72. {
  73. clk->ops->disable(clk);
  74. }
  75. /* Public functions */
  76. /**
  77. * omap2xxx_clk_commit - commit clock parent/rate changes in hardware
  78. * @clk: struct clk *
  79. *
  80. * If @clk has the DELAYED_APP flag set, meaning that parent/rate changes
  81. * don't take effect until the VALID_CONFIG bit is written, write the
  82. * VALID_CONFIG bit and wait for the write to complete. No return value.
  83. */
  84. void omap2xxx_clk_commit(struct clk *clk)
  85. {
  86. if (!cpu_is_omap24xx())
  87. return;
  88. if (!(clk->flags & DELAYED_APP))
  89. return;
  90. prm_write_mod_reg(OMAP24XX_VALID_CONFIG, OMAP24XX_GR_MOD,
  91. OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
  92. /* OCP barrier */
  93. prm_read_mod_reg(OMAP24XX_GR_MOD, OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
  94. }
  95. /**
  96. * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
  97. * @clk: OMAP clock struct ptr to use
  98. *
  99. * Convert a clockdomain name stored in a struct clk 'clk' into a
  100. * clockdomain pointer, and save it into the struct clk. Intended to be
  101. * called during clk_register(). No return value.
  102. */
  103. void omap2_init_clk_clkdm(struct clk *clk)
  104. {
  105. struct clockdomain *clkdm;
  106. if (!clk->clkdm_name)
  107. return;
  108. clkdm = clkdm_lookup(clk->clkdm_name);
  109. if (clkdm) {
  110. pr_debug("clock: associated clk %s to clkdm %s\n",
  111. clk->name, clk->clkdm_name);
  112. clk->clkdm = clkdm;
  113. } else {
  114. pr_debug("clock: could not associate clk %s to "
  115. "clkdm %s\n", clk->name, clk->clkdm_name);
  116. }
  117. }
  118. /**
  119. * omap2_clk_dflt_find_companion - find companion clock to @clk
  120. * @clk: struct clk * to find the companion clock of
  121. * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
  122. * @other_bit: u8 ** to return the companion clock bit shift in
  123. *
  124. * Note: We don't need special code here for INVERT_ENABLE for the
  125. * time being since INVERT_ENABLE only applies to clocks enabled by
  126. * CM_CLKEN_PLL
  127. *
  128. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
  129. * just a matter of XORing the bits.
  130. *
  131. * Some clocks don't have companion clocks. For example, modules with
  132. * only an interface clock (such as MAILBOXES) don't have a companion
  133. * clock. Right now, this code relies on the hardware exporting a bit
  134. * in the correct companion register that indicates that the
  135. * nonexistent 'companion clock' is active. Future patches will
  136. * associate this type of code with per-module data structures to
  137. * avoid this issue, and remove the casts. No return value.
  138. */
  139. void omap2_clk_dflt_find_companion(struct clk *clk, void __iomem **other_reg,
  140. u8 *other_bit)
  141. {
  142. u32 r;
  143. /*
  144. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
  145. * it's just a matter of XORing the bits.
  146. */
  147. r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
  148. *other_reg = (__force void __iomem *)r;
  149. *other_bit = clk->enable_bit;
  150. }
  151. /**
  152. * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
  153. * @clk: struct clk * to find IDLEST info for
  154. * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
  155. * @idlest_bit: u8 ** to return the CM_IDLEST bit shift in
  156. *
  157. * Return the CM_IDLEST register address and bit shift corresponding
  158. * to the module that "owns" this clock. This default code assumes
  159. * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
  160. * the IDLEST register address ID corresponds to the CM_*CLKEN
  161. * register address ID (e.g., that CM_FCLKEN2 corresponds to
  162. * CM_IDLEST2). This is not true for all modules. No return value.
  163. */
  164. void omap2_clk_dflt_find_idlest(struct clk *clk, void __iomem **idlest_reg,
  165. u8 *idlest_bit)
  166. {
  167. u32 r;
  168. r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
  169. *idlest_reg = (__force void __iomem *)r;
  170. *idlest_bit = clk->enable_bit;
  171. }
  172. int omap2_dflt_clk_enable(struct clk *clk)
  173. {
  174. u32 v;
  175. if (unlikely(clk->enable_reg == NULL)) {
  176. pr_err("clock.c: Enable for %s without enable code\n",
  177. clk->name);
  178. return 0; /* REVISIT: -EINVAL */
  179. }
  180. v = __raw_readl(clk->enable_reg);
  181. if (clk->flags & INVERT_ENABLE)
  182. v &= ~(1 << clk->enable_bit);
  183. else
  184. v |= (1 << clk->enable_bit);
  185. __raw_writel(v, clk->enable_reg);
  186. v = __raw_readl(clk->enable_reg); /* OCP barrier */
  187. if (clk->ops->find_idlest)
  188. _omap2_module_wait_ready(clk);
  189. return 0;
  190. }
  191. void omap2_dflt_clk_disable(struct clk *clk)
  192. {
  193. u32 v;
  194. if (!clk->enable_reg) {
  195. /*
  196. * 'Independent' here refers to a clock which is not
  197. * controlled by its parent.
  198. */
  199. printk(KERN_ERR "clock: clk_disable called on independent "
  200. "clock %s which has no enable_reg\n", clk->name);
  201. return;
  202. }
  203. v = __raw_readl(clk->enable_reg);
  204. if (clk->flags & INVERT_ENABLE)
  205. v |= (1 << clk->enable_bit);
  206. else
  207. v &= ~(1 << clk->enable_bit);
  208. __raw_writel(v, clk->enable_reg);
  209. /* No OCP barrier needed here since it is a disable operation */
  210. }
  211. const struct clkops clkops_omap2_dflt_wait = {
  212. .enable = omap2_dflt_clk_enable,
  213. .disable = omap2_dflt_clk_disable,
  214. .find_companion = omap2_clk_dflt_find_companion,
  215. .find_idlest = omap2_clk_dflt_find_idlest,
  216. };
  217. const struct clkops clkops_omap2_dflt = {
  218. .enable = omap2_dflt_clk_enable,
  219. .disable = omap2_dflt_clk_disable,
  220. };
  221. void omap2_clk_disable(struct clk *clk)
  222. {
  223. if (clk->usecount > 0 && !(--clk->usecount)) {
  224. _omap2_clk_disable(clk);
  225. if (clk->parent)
  226. omap2_clk_disable(clk->parent);
  227. if (clk->clkdm)
  228. omap2_clkdm_clk_disable(clk->clkdm, clk);
  229. }
  230. }
  231. int omap2_clk_enable(struct clk *clk)
  232. {
  233. int ret = 0;
  234. if (clk->usecount++ == 0) {
  235. if (clk->clkdm)
  236. omap2_clkdm_clk_enable(clk->clkdm, clk);
  237. if (clk->parent) {
  238. ret = omap2_clk_enable(clk->parent);
  239. if (ret)
  240. goto err;
  241. }
  242. ret = _omap2_clk_enable(clk);
  243. if (ret) {
  244. if (clk->parent)
  245. omap2_clk_disable(clk->parent);
  246. goto err;
  247. }
  248. }
  249. return ret;
  250. err:
  251. if (clk->clkdm)
  252. omap2_clkdm_clk_disable(clk->clkdm, clk);
  253. clk->usecount--;
  254. return ret;
  255. }
  256. /* Set the clock rate for a clock source */
  257. int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
  258. {
  259. int ret = -EINVAL;
  260. pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
  261. /* CONFIG_PARTICIPANT clocks are changed only in sets via the
  262. rate table mechanism, driven by mpu_speed */
  263. if (clk->flags & CONFIG_PARTICIPANT)
  264. return -EINVAL;
  265. /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
  266. if (clk->set_rate)
  267. ret = clk->set_rate(clk, rate);
  268. return ret;
  269. }
  270. int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
  271. {
  272. if (clk->flags & CONFIG_PARTICIPANT)
  273. return -EINVAL;
  274. if (!clk->clksel)
  275. return -EINVAL;
  276. return omap2_clksel_set_parent(clk, new_parent);
  277. }
  278. /*-------------------------------------------------------------------------
  279. * Omap2 clock reset and init functions
  280. *-------------------------------------------------------------------------*/
  281. #ifdef CONFIG_OMAP_RESET_CLOCKS
  282. void omap2_clk_disable_unused(struct clk *clk)
  283. {
  284. u32 regval32, v;
  285. v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
  286. regval32 = __raw_readl(clk->enable_reg);
  287. if ((regval32 & (1 << clk->enable_bit)) == v)
  288. return;
  289. printk(KERN_DEBUG "Disabling unused clock \"%s\"\n", clk->name);
  290. if (cpu_is_omap34xx()) {
  291. omap2_clk_enable(clk);
  292. omap2_clk_disable(clk);
  293. } else
  294. _omap2_clk_disable(clk);
  295. if (clk->clkdm != NULL)
  296. pwrdm_clkdm_state_switch(clk->clkdm);
  297. }
  298. #endif
  299. /* Common data */
  300. struct clk_functions omap2_clk_functions = {
  301. .clk_enable = omap2_clk_enable,
  302. .clk_disable = omap2_clk_disable,
  303. .clk_round_rate = omap2_clk_round_rate,
  304. .clk_set_rate = omap2_clk_set_rate,
  305. .clk_set_parent = omap2_clk_set_parent,
  306. .clk_disable_unused = omap2_clk_disable_unused,
  307. #ifdef CONFIG_CPU_FREQ
  308. /* These will be removed when the OPP code is integrated */
  309. .clk_init_cpufreq_table = omap2_clk_init_cpufreq_table,
  310. .clk_exit_cpufreq_table = omap2_clk_exit_cpufreq_table,
  311. #endif
  312. };