clock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * linux/arch/arm/mach-omap2/clock.c
  3. *
  4. * Copyright (C) 2005-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2004-2010 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/kernel.h>
  17. #include <linux/list.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/delay.h>
  21. #include <linux/clk.h>
  22. #include <linux/io.h>
  23. #include <linux/bitops.h>
  24. #include <plat/clock.h>
  25. #include <plat/clockdomain.h>
  26. #include <plat/cpu.h>
  27. #include <plat/prcm.h>
  28. #include "clock.h"
  29. #include "prm.h"
  30. #include "prm-regbits-24xx.h"
  31. #include "cm.h"
  32. #include "cm-regbits-24xx.h"
  33. #include "cm-regbits-34xx.h"
  34. u8 cpu_mask;
  35. /*
  36. * OMAP2+ specific clock functions
  37. */
  38. /* Private functions */
  39. /**
  40. * _omap2_module_wait_ready - wait for an OMAP module to leave IDLE
  41. * @clk: struct clk * belonging to the module
  42. *
  43. * If the necessary clocks for the OMAP hardware IP block that
  44. * corresponds to clock @clk are enabled, then wait for the module to
  45. * indicate readiness (i.e., to leave IDLE). This code does not
  46. * belong in the clock code and will be moved in the medium term to
  47. * module-dependent code. No return value.
  48. */
  49. static void _omap2_module_wait_ready(struct clk *clk)
  50. {
  51. void __iomem *companion_reg, *idlest_reg;
  52. u8 other_bit, idlest_bit, idlest_val;
  53. /* Not all modules have multiple clocks that their IDLEST depends on */
  54. if (clk->ops->find_companion) {
  55. clk->ops->find_companion(clk, &companion_reg, &other_bit);
  56. if (!(__raw_readl(companion_reg) & (1 << other_bit)))
  57. return;
  58. }
  59. clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit, &idlest_val);
  60. omap2_cm_wait_idlest(idlest_reg, (1 << idlest_bit), idlest_val,
  61. clk->name);
  62. }
  63. /* Public functions */
  64. /**
  65. * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
  66. * @clk: OMAP clock struct ptr to use
  67. *
  68. * Convert a clockdomain name stored in a struct clk 'clk' into a
  69. * clockdomain pointer, and save it into the struct clk. Intended to be
  70. * called during clk_register(). No return value.
  71. */
  72. void omap2_init_clk_clkdm(struct clk *clk)
  73. {
  74. struct clockdomain *clkdm;
  75. if (!clk->clkdm_name)
  76. return;
  77. clkdm = clkdm_lookup(clk->clkdm_name);
  78. if (clkdm) {
  79. pr_debug("clock: associated clk %s to clkdm %s\n",
  80. clk->name, clk->clkdm_name);
  81. clk->clkdm = clkdm;
  82. } else {
  83. pr_debug("clock: could not associate clk %s to "
  84. "clkdm %s\n", clk->name, clk->clkdm_name);
  85. }
  86. }
  87. /**
  88. * omap2_clk_dflt_find_companion - find companion clock to @clk
  89. * @clk: struct clk * to find the companion clock of
  90. * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
  91. * @other_bit: u8 ** to return the companion clock bit shift in
  92. *
  93. * Note: We don't need special code here for INVERT_ENABLE for the
  94. * time being since INVERT_ENABLE only applies to clocks enabled by
  95. * CM_CLKEN_PLL
  96. *
  97. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
  98. * just a matter of XORing the bits.
  99. *
  100. * Some clocks don't have companion clocks. For example, modules with
  101. * only an interface clock (such as MAILBOXES) don't have a companion
  102. * clock. Right now, this code relies on the hardware exporting a bit
  103. * in the correct companion register that indicates that the
  104. * nonexistent 'companion clock' is active. Future patches will
  105. * associate this type of code with per-module data structures to
  106. * avoid this issue, and remove the casts. No return value.
  107. */
  108. void omap2_clk_dflt_find_companion(struct clk *clk, void __iomem **other_reg,
  109. u8 *other_bit)
  110. {
  111. u32 r;
  112. /*
  113. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
  114. * it's just a matter of XORing the bits.
  115. */
  116. r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
  117. *other_reg = (__force void __iomem *)r;
  118. *other_bit = clk->enable_bit;
  119. }
  120. /**
  121. * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
  122. * @clk: struct clk * to find IDLEST info for
  123. * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
  124. * @idlest_bit: u8 * to return the CM_IDLEST bit shift in
  125. * @idlest_val: u8 * to return the idle status indicator
  126. *
  127. * Return the CM_IDLEST register address and bit shift corresponding
  128. * to the module that "owns" this clock. This default code assumes
  129. * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
  130. * the IDLEST register address ID corresponds to the CM_*CLKEN
  131. * register address ID (e.g., that CM_FCLKEN2 corresponds to
  132. * CM_IDLEST2). This is not true for all modules. No return value.
  133. */
  134. void omap2_clk_dflt_find_idlest(struct clk *clk, void __iomem **idlest_reg,
  135. u8 *idlest_bit, u8 *idlest_val)
  136. {
  137. u32 r;
  138. r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
  139. *idlest_reg = (__force void __iomem *)r;
  140. *idlest_bit = clk->enable_bit;
  141. /*
  142. * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
  143. * 34xx reverses this, just to keep us on our toes
  144. * AM35xx uses both, depending on the module.
  145. */
  146. if (cpu_is_omap24xx())
  147. *idlest_val = OMAP24XX_CM_IDLEST_VAL;
  148. else if (cpu_is_omap34xx())
  149. *idlest_val = OMAP34XX_CM_IDLEST_VAL;
  150. else
  151. BUG();
  152. }
  153. int omap2_dflt_clk_enable(struct clk *clk)
  154. {
  155. u32 v;
  156. if (unlikely(clk->enable_reg == NULL)) {
  157. pr_err("clock.c: Enable for %s without enable code\n",
  158. clk->name);
  159. return 0; /* REVISIT: -EINVAL */
  160. }
  161. v = __raw_readl(clk->enable_reg);
  162. if (clk->flags & INVERT_ENABLE)
  163. v &= ~(1 << clk->enable_bit);
  164. else
  165. v |= (1 << clk->enable_bit);
  166. __raw_writel(v, clk->enable_reg);
  167. v = __raw_readl(clk->enable_reg); /* OCP barrier */
  168. if (clk->ops->find_idlest)
  169. _omap2_module_wait_ready(clk);
  170. return 0;
  171. }
  172. void omap2_dflt_clk_disable(struct clk *clk)
  173. {
  174. u32 v;
  175. if (!clk->enable_reg) {
  176. /*
  177. * 'Independent' here refers to a clock which is not
  178. * controlled by its parent.
  179. */
  180. printk(KERN_ERR "clock: clk_disable called on independent "
  181. "clock %s which has no enable_reg\n", clk->name);
  182. return;
  183. }
  184. v = __raw_readl(clk->enable_reg);
  185. if (clk->flags & INVERT_ENABLE)
  186. v |= (1 << clk->enable_bit);
  187. else
  188. v &= ~(1 << clk->enable_bit);
  189. __raw_writel(v, clk->enable_reg);
  190. /* No OCP barrier needed here since it is a disable operation */
  191. }
  192. const struct clkops clkops_omap2_dflt_wait = {
  193. .enable = omap2_dflt_clk_enable,
  194. .disable = omap2_dflt_clk_disable,
  195. .find_companion = omap2_clk_dflt_find_companion,
  196. .find_idlest = omap2_clk_dflt_find_idlest,
  197. };
  198. const struct clkops clkops_omap2_dflt = {
  199. .enable = omap2_dflt_clk_enable,
  200. .disable = omap2_dflt_clk_disable,
  201. };
  202. /**
  203. * omap2_clk_disable - disable a clock, if the system is not using it
  204. * @clk: struct clk * to disable
  205. *
  206. * Decrements the usecount on struct clk @clk. If there are no users
  207. * left, call the clkops-specific clock disable function to disable it
  208. * in hardware. If the clock is part of a clockdomain (which they all
  209. * should be), request that the clockdomain be disabled. (It too has
  210. * a usecount, and so will not be disabled in the hardware until it no
  211. * longer has any users.) If the clock has a parent clock (most of
  212. * them do), then call ourselves, recursing on the parent clock. This
  213. * can cause an entire branch of the clock tree to be powered off by
  214. * simply disabling one clock. Intended to be called with the clockfw_lock
  215. * spinlock held. No return value.
  216. */
  217. void omap2_clk_disable(struct clk *clk)
  218. {
  219. if (clk->usecount == 0) {
  220. WARN(1, "clock: %s: omap2_clk_disable() called, but usecount "
  221. "already 0?", clk->name);
  222. return;
  223. }
  224. pr_debug("clock: %s: decrementing usecount\n", clk->name);
  225. clk->usecount--;
  226. if (clk->usecount > 0)
  227. return;
  228. pr_debug("clock: %s: disabling in hardware\n", clk->name);
  229. clk->ops->disable(clk);
  230. if (clk->clkdm)
  231. omap2_clkdm_clk_disable(clk->clkdm, clk);
  232. if (clk->parent)
  233. omap2_clk_disable(clk->parent);
  234. }
  235. /**
  236. * omap2_clk_enable - request that the system enable a clock
  237. * @clk: struct clk * to enable
  238. *
  239. * Increments the usecount on struct clk @clk. If there were no users
  240. * previously, then recurse up the clock tree, enabling all of the
  241. * clock's parents and all of the parent clockdomains, and finally,
  242. * enabling @clk's clockdomain, and @clk itself. Intended to be
  243. * called with the clockfw_lock spinlock held. Returns 0 upon success
  244. * or a negative error code upon failure.
  245. */
  246. int omap2_clk_enable(struct clk *clk)
  247. {
  248. int ret;
  249. pr_debug("clock: %s: incrementing usecount\n", clk->name);
  250. clk->usecount++;
  251. if (clk->usecount > 1)
  252. return 0;
  253. pr_debug("clock: %s: enabling in hardware\n", clk->name);
  254. if (clk->parent) {
  255. ret = omap2_clk_enable(clk->parent);
  256. if (ret) {
  257. WARN(1, "clock: %s: could not enable parent %s: %d\n",
  258. clk->name, clk->parent->name, ret);
  259. goto oce_err1;
  260. }
  261. }
  262. if (clk->clkdm) {
  263. ret = omap2_clkdm_clk_enable(clk->clkdm, clk);
  264. if (ret) {
  265. WARN(1, "clock: %s: could not enable clockdomain %s: "
  266. "%d\n", clk->name, clk->clkdm->name, ret);
  267. goto oce_err2;
  268. }
  269. }
  270. ret = clk->ops->enable(clk);
  271. if (ret) {
  272. WARN(1, "clock: %s: could not enable: %d\n", clk->name, ret);
  273. goto oce_err3;
  274. }
  275. return 0;
  276. oce_err3:
  277. if (clk->clkdm)
  278. omap2_clkdm_clk_disable(clk->clkdm, clk);
  279. oce_err2:
  280. if (clk->parent)
  281. omap2_clk_disable(clk->parent);
  282. oce_err1:
  283. clk->usecount--;
  284. return ret;
  285. }
  286. /* Set the clock rate for a clock source */
  287. int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
  288. {
  289. int ret = -EINVAL;
  290. pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
  291. /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
  292. if (clk->set_rate)
  293. ret = clk->set_rate(clk, rate);
  294. return ret;
  295. }
  296. int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
  297. {
  298. if (!clk->clksel)
  299. return -EINVAL;
  300. if (clk->parent == new_parent)
  301. return 0;
  302. return omap2_clksel_set_parent(clk, new_parent);
  303. }
  304. /* OMAP3/4 non-CORE DPLL clkops */
  305. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  306. const struct clkops clkops_omap3_noncore_dpll_ops = {
  307. .enable = omap3_noncore_dpll_enable,
  308. .disable = omap3_noncore_dpll_disable,
  309. };
  310. #endif
  311. /*
  312. * OMAP2+ clock reset and init functions
  313. */
  314. #ifdef CONFIG_OMAP_RESET_CLOCKS
  315. void omap2_clk_disable_unused(struct clk *clk)
  316. {
  317. u32 regval32, v;
  318. v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
  319. regval32 = __raw_readl(clk->enable_reg);
  320. if ((regval32 & (1 << clk->enable_bit)) == v)
  321. return;
  322. printk(KERN_DEBUG "Disabling unused clock \"%s\"\n", clk->name);
  323. if (cpu_is_omap34xx()) {
  324. omap2_clk_enable(clk);
  325. omap2_clk_disable(clk);
  326. } else {
  327. clk->ops->disable(clk);
  328. }
  329. if (clk->clkdm != NULL)
  330. pwrdm_clkdm_state_switch(clk->clkdm);
  331. }
  332. #endif
  333. /**
  334. * omap2_clk_switch_mpurate_at_boot - switch ARM MPU rate by boot-time argument
  335. * @mpurate_ck_name: clk name of the clock to change rate
  336. *
  337. * Change the ARM MPU clock rate to the rate specified on the command
  338. * line, if one was specified. @mpurate_ck_name should be
  339. * "virt_prcm_set" on OMAP2xxx and "dpll1_ck" on OMAP34xx/OMAP36xx.
  340. * XXX Does not handle voltage scaling - on OMAP2xxx this is currently
  341. * handled by the virt_prcm_set clock, but this should be handled by
  342. * the OPP layer. XXX This is intended to be handled by the OPP layer
  343. * code in the near future and should be removed from the clock code.
  344. * Returns -EINVAL if 'mpurate' is zero or if clk_set_rate() rejects
  345. * the rate, -ENOENT if the struct clk referred to by @mpurate_ck_name
  346. * cannot be found, or 0 upon success.
  347. */
  348. int __init omap2_clk_switch_mpurate_at_boot(const char *mpurate_ck_name)
  349. {
  350. struct clk *mpurate_ck;
  351. int r;
  352. if (!mpurate)
  353. return -EINVAL;
  354. mpurate_ck = clk_get(NULL, mpurate_ck_name);
  355. if (WARN(IS_ERR(mpurate_ck), "Failed to get %s.\n", mpurate_ck_name))
  356. return -ENOENT;
  357. r = clk_set_rate(mpurate_ck, mpurate);
  358. if (IS_ERR_VALUE(r)) {
  359. WARN(1, "clock: %s: unable to set MPU rate to %d: %d\n",
  360. mpurate_ck->name, mpurate, r);
  361. return -EINVAL;
  362. }
  363. calibrate_delay();
  364. recalculate_root_clocks();
  365. clk_put(mpurate_ck);
  366. return 0;
  367. }
  368. /**
  369. * omap2_clk_print_new_rates - print summary of current clock tree rates
  370. * @hfclkin_ck_name: clk name for the off-chip HF oscillator
  371. * @core_ck_name: clk name for the on-chip CORE_CLK
  372. * @mpu_ck_name: clk name for the ARM MPU clock
  373. *
  374. * Prints a short message to the console with the HFCLKIN oscillator
  375. * rate, the rate of the CORE clock, and the rate of the ARM MPU clock.
  376. * Called by the boot-time MPU rate switching code. XXX This is intended
  377. * to be handled by the OPP layer code in the near future and should be
  378. * removed from the clock code. No return value.
  379. */
  380. void __init omap2_clk_print_new_rates(const char *hfclkin_ck_name,
  381. const char *core_ck_name,
  382. const char *mpu_ck_name)
  383. {
  384. struct clk *hfclkin_ck, *core_ck, *mpu_ck;
  385. unsigned long hfclkin_rate;
  386. mpu_ck = clk_get(NULL, mpu_ck_name);
  387. if (WARN(IS_ERR(mpu_ck), "clock: failed to get %s.\n", mpu_ck_name))
  388. return;
  389. core_ck = clk_get(NULL, core_ck_name);
  390. if (WARN(IS_ERR(core_ck), "clock: failed to get %s.\n", core_ck_name))
  391. return;
  392. hfclkin_ck = clk_get(NULL, hfclkin_ck_name);
  393. if (WARN(IS_ERR(hfclkin_ck), "Failed to get %s.\n", hfclkin_ck_name))
  394. return;
  395. hfclkin_rate = clk_get_rate(hfclkin_ck);
  396. pr_info("Switched to new clocking rate (Crystal/Core/MPU): "
  397. "%ld.%01ld/%ld/%ld MHz\n",
  398. (hfclkin_rate / 1000000),
  399. ((hfclkin_rate / 100000) % 10),
  400. (clk_get_rate(core_ck) / 1000000),
  401. (clk_get_rate(mpu_ck) / 1000000));
  402. }
  403. /* Common data */
  404. struct clk_functions omap2_clk_functions = {
  405. .clk_enable = omap2_clk_enable,
  406. .clk_disable = omap2_clk_disable,
  407. .clk_round_rate = omap2_clk_round_rate,
  408. .clk_set_rate = omap2_clk_set_rate,
  409. .clk_set_parent = omap2_clk_set_parent,
  410. .clk_disable_unused = omap2_clk_disable_unused,
  411. #ifdef CONFIG_CPU_FREQ
  412. /* These will be removed when the OPP code is integrated */
  413. .clk_init_cpufreq_table = omap2_clk_init_cpufreq_table,
  414. .clk_exit_cpufreq_table = omap2_clk_exit_cpufreq_table,
  415. #endif
  416. };