clock.c 15 KB

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