PageRenderTime 2260ms CodeModel.GetById 44ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm/mach-omap2/clock.c

https://bitbucket.org/sammyz/iscream_thunderc-2.6.35-rebase
C | 508 lines | 274 code | 85 blank | 149 comment | 45 complexity | 96a76e3900846fcd16ebbea6e06d0461 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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. /* Given a clock and a rate apply a clock specific rounding function */
  287. long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
  288. {
  289. if (clk->round_rate)
  290. return clk->round_rate(clk, rate);
  291. return clk->rate;
  292. }
  293. /* Set the clock rate for a clock source */
  294. int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
  295. {
  296. int ret = -EINVAL;
  297. pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
  298. /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
  299. if (clk->set_rate)
  300. ret = clk->set_rate(clk, rate);
  301. return ret;
  302. }
  303. int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
  304. {
  305. if (!clk->clksel)
  306. return -EINVAL;
  307. if (clk->parent == new_parent)
  308. return 0;
  309. return omap2_clksel_set_parent(clk, new_parent);
  310. }
  311. /* OMAP3/4 non-CORE DPLL clkops */
  312. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  313. const struct clkops clkops_omap3_noncore_dpll_ops = {
  314. .enable = omap3_noncore_dpll_enable,
  315. .disable = omap3_noncore_dpll_disable,
  316. };
  317. #endif
  318. /*
  319. * OMAP2+ clock reset and init functions
  320. */
  321. #ifdef CONFIG_OMAP_RESET_CLOCKS
  322. void omap2_clk_disable_unused(struct clk *clk)
  323. {
  324. u32 regval32, v;
  325. v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
  326. regval32 = __raw_readl(clk->enable_reg);
  327. if ((regval32 & (1 << clk->enable_bit)) == v)
  328. return;
  329. printk(KERN_DEBUG "Disabling unused clock \"%s\"\n", clk->name);
  330. if (cpu_is_omap34xx()) {
  331. omap2_clk_enable(clk);
  332. omap2_clk_disable(clk);
  333. } else {
  334. clk->ops->disable(clk);
  335. }
  336. if (clk->clkdm != NULL)
  337. pwrdm_clkdm_state_switch(clk->clkdm);
  338. }
  339. #endif
  340. /**
  341. * omap2_clk_switch_mpurate_at_boot - switch ARM MPU rate by boot-time argument
  342. * @mpurate_ck_name: clk name of the clock to change rate
  343. *
  344. * Change the ARM MPU clock rate to the rate specified on the command
  345. * line, if one was specified. @mpurate_ck_name should be
  346. * "virt_prcm_set" on OMAP2xxx and "dpll1_ck" on OMAP34xx/OMAP36xx.
  347. * XXX Does not handle voltage scaling - on OMAP2xxx this is currently
  348. * handled by the virt_prcm_set clock, but this should be handled by
  349. * the OPP layer. XXX This is intended to be handled by the OPP layer
  350. * code in the near future and should be removed from the clock code.
  351. * Returns -EINVAL if 'mpurate' is zero or if clk_set_rate() rejects
  352. * the rate, -ENOENT if the struct clk referred to by @mpurate_ck_name
  353. * cannot be found, or 0 upon success.
  354. */
  355. int __init omap2_clk_switch_mpurate_at_boot(const char *mpurate_ck_name)
  356. {
  357. struct clk *mpurate_ck;
  358. int r;
  359. if (!mpurate)
  360. return -EINVAL;
  361. mpurate_ck = clk_get(NULL, mpurate_ck_name);
  362. if (WARN(IS_ERR(mpurate_ck), "Failed to get %s.\n", mpurate_ck_name))
  363. return -ENOENT;
  364. r = clk_set_rate(mpurate_ck, mpurate);
  365. if (IS_ERR_VALUE(r)) {
  366. WARN(1, "clock: %s: unable to set MPU rate to %d: %d\n",
  367. mpurate_ck->name, mpurate, r);
  368. return -EINVAL;
  369. }
  370. calibrate_delay();
  371. recalculate_root_clocks();
  372. clk_put(mpurate_ck);
  373. return 0;
  374. }
  375. /**
  376. * omap2_clk_print_new_rates - print summary of current clock tree rates
  377. * @hfclkin_ck_name: clk name for the off-chip HF oscillator
  378. * @core_ck_name: clk name for the on-chip CORE_CLK
  379. * @mpu_ck_name: clk name for the ARM MPU clock
  380. *
  381. * Prints a short message to the console with the HFCLKIN oscillator
  382. * rate, the rate of the CORE clock, and the rate of the ARM MPU clock.
  383. * Called by the boot-time MPU rate switching code. XXX This is intended
  384. * to be handled by the OPP layer code in the near future and should be
  385. * removed from the clock code. No return value.
  386. */
  387. void __init omap2_clk_print_new_rates(const char *hfclkin_ck_name,
  388. const char *core_ck_name,
  389. const char *mpu_ck_name)
  390. {
  391. struct clk *hfclkin_ck, *core_ck, *mpu_ck;
  392. unsigned long hfclkin_rate;
  393. mpu_ck = clk_get(NULL, mpu_ck_name);
  394. if (WARN(IS_ERR(mpu_ck), "clock: failed to get %s.\n", mpu_ck_name))
  395. return;
  396. core_ck = clk_get(NULL, core_ck_name);
  397. if (WARN(IS_ERR(core_ck), "clock: failed to get %s.\n", core_ck_name))
  398. return;
  399. hfclkin_ck = clk_get(NULL, hfclkin_ck_name);
  400. if (WARN(IS_ERR(hfclkin_ck), "Failed to get %s.\n", hfclkin_ck_name))
  401. return;
  402. hfclkin_rate = clk_get_rate(hfclkin_ck);
  403. pr_info("Switched to new clocking rate (Crystal/Core/MPU): "
  404. "%ld.%01ld/%ld/%ld MHz\n",
  405. (hfclkin_rate / 1000000),
  406. ((hfclkin_rate / 100000) % 10),
  407. (clk_get_rate(core_ck) / 1000000),
  408. (clk_get_rate(mpu_ck) / 1000000));
  409. }
  410. /* Common data */
  411. struct clk_functions omap2_clk_functions = {
  412. .clk_enable = omap2_clk_enable,
  413. .clk_disable = omap2_clk_disable,
  414. .clk_round_rate = omap2_clk_round_rate,
  415. .clk_set_rate = omap2_clk_set_rate,
  416. .clk_set_parent = omap2_clk_set_parent,
  417. .clk_disable_unused = omap2_clk_disable_unused,
  418. #ifdef CONFIG_CPU_FREQ
  419. /* These will be removed when the OPP code is integrated */
  420. .clk_init_cpufreq_table = omap2_clk_init_cpufreq_table,
  421. .clk_exit_cpufreq_table = omap2_clk_exit_cpufreq_table,
  422. #endif
  423. };