clock.c 14 KB

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