clock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 <asm/cpu.h>
  25. #include <plat/clock.h>
  26. #include <plat/prcm.h>
  27. #include <trace/events/power.h>
  28. #include "soc.h"
  29. #include "clockdomain.h"
  30. #include "clock.h"
  31. #include "cm2xxx_3xxx.h"
  32. #include "cm-regbits-24xx.h"
  33. #include "cm-regbits-34xx.h"
  34. u16 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_get_name(clk));
  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. const char *clk_name;
  83. if (!clk->clkdm_name)
  84. return;
  85. clk_name = __clk_get_name(clk);
  86. clkdm = clkdm_lookup(clk->clkdm_name);
  87. if (clkdm) {
  88. pr_debug("clock: associated clk %s to clkdm %s\n",
  89. clk_name, clk->clkdm_name);
  90. clk->clkdm = clkdm;
  91. } else {
  92. pr_debug("clock: could not associate clk %s to clkdm %s\n",
  93. clk_name, clk->clkdm_name);
  94. }
  95. }
  96. /**
  97. * omap2_clk_disable_clkdm_control - disable clkdm control on clk enable/disable
  98. *
  99. * Prevent the OMAP clock code from calling into the clockdomain code
  100. * when a hardware clock in that clockdomain is enabled or disabled.
  101. * Intended to be called at init time from omap*_clk_init(). No
  102. * return value.
  103. */
  104. void __init omap2_clk_disable_clkdm_control(void)
  105. {
  106. clkdm_control = false;
  107. }
  108. /**
  109. * omap2_clk_dflt_find_companion - find companion clock to @clk
  110. * @clk: struct clk * to find the companion clock of
  111. * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
  112. * @other_bit: u8 ** to return the companion clock bit shift in
  113. *
  114. * Note: We don't need special code here for INVERT_ENABLE for the
  115. * time being since INVERT_ENABLE only applies to clocks enabled by
  116. * CM_CLKEN_PLL
  117. *
  118. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
  119. * just a matter of XORing the bits.
  120. *
  121. * Some clocks don't have companion clocks. For example, modules with
  122. * only an interface clock (such as MAILBOXES) don't have a companion
  123. * clock. Right now, this code relies on the hardware exporting a bit
  124. * in the correct companion register that indicates that the
  125. * nonexistent 'companion clock' is active. Future patches will
  126. * associate this type of code with per-module data structures to
  127. * avoid this issue, and remove the casts. No return value.
  128. */
  129. void omap2_clk_dflt_find_companion(struct clk *clk, void __iomem **other_reg,
  130. u8 *other_bit)
  131. {
  132. u32 r;
  133. /*
  134. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
  135. * it's just a matter of XORing the bits.
  136. */
  137. r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
  138. *other_reg = (__force void __iomem *)r;
  139. *other_bit = clk->enable_bit;
  140. }
  141. /**
  142. * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
  143. * @clk: struct clk * to find IDLEST info for
  144. * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
  145. * @idlest_bit: u8 * to return the CM_IDLEST bit shift in
  146. * @idlest_val: u8 * to return the idle status indicator
  147. *
  148. * Return the CM_IDLEST register address and bit shift corresponding
  149. * to the module that "owns" this clock. This default code assumes
  150. * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
  151. * the IDLEST register address ID corresponds to the CM_*CLKEN
  152. * register address ID (e.g., that CM_FCLKEN2 corresponds to
  153. * CM_IDLEST2). This is not true for all modules. No return value.
  154. */
  155. void omap2_clk_dflt_find_idlest(struct clk *clk, void __iomem **idlest_reg,
  156. u8 *idlest_bit, u8 *idlest_val)
  157. {
  158. u32 r;
  159. r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
  160. *idlest_reg = (__force void __iomem *)r;
  161. *idlest_bit = clk->enable_bit;
  162. /*
  163. * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
  164. * 34xx reverses this, just to keep us on our toes
  165. * AM35xx uses both, depending on the module.
  166. */
  167. if (cpu_is_omap24xx())
  168. *idlest_val = OMAP24XX_CM_IDLEST_VAL;
  169. else if (cpu_is_omap34xx())
  170. *idlest_val = OMAP34XX_CM_IDLEST_VAL;
  171. else
  172. BUG();
  173. }
  174. int omap2_dflt_clk_enable(struct clk *clk)
  175. {
  176. u32 v;
  177. if (unlikely(clk->enable_reg == NULL)) {
  178. pr_err("clock.c: Enable for %s without enable code\n",
  179. clk->name);
  180. return 0; /* REVISIT: -EINVAL */
  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. v = __raw_readl(clk->enable_reg); /* OCP barrier */
  189. if (clk->ops->find_idlest)
  190. _omap2_module_wait_ready(clk);
  191. return 0;
  192. }
  193. void omap2_dflt_clk_disable(struct clk *clk)
  194. {
  195. u32 v;
  196. if (!clk->enable_reg) {
  197. /*
  198. * 'Independent' here refers to a clock which is not
  199. * controlled by its parent.
  200. */
  201. pr_err("clock: clk_disable called on independent clock %s which has no enable_reg\n", clk->name);
  202. return;
  203. }
  204. v = __raw_readl(clk->enable_reg);
  205. if (clk->flags & INVERT_ENABLE)
  206. v |= (1 << clk->enable_bit);
  207. else
  208. v &= ~(1 << clk->enable_bit);
  209. __raw_writel(v, clk->enable_reg);
  210. /* No OCP barrier needed here since it is a disable operation */
  211. }
  212. const struct clkops clkops_omap2_dflt_wait = {
  213. .enable = omap2_dflt_clk_enable,
  214. .disable = omap2_dflt_clk_disable,
  215. .find_companion = omap2_clk_dflt_find_companion,
  216. .find_idlest = omap2_clk_dflt_find_idlest,
  217. };
  218. const struct clkops clkops_omap2_dflt = {
  219. .enable = omap2_dflt_clk_enable,
  220. .disable = omap2_dflt_clk_disable,
  221. };
  222. /**
  223. * omap2_clk_disable - disable a clock, if the system is not using it
  224. * @clk: struct clk * to disable
  225. *
  226. * Decrements the usecount on struct clk @clk. If there are no users
  227. * left, call the clkops-specific clock disable function to disable it
  228. * in hardware. If the clock is part of a clockdomain (which they all
  229. * should be), request that the clockdomain be disabled. (It too has
  230. * a usecount, and so will not be disabled in the hardware until it no
  231. * longer has any users.) If the clock has a parent clock (most of
  232. * them do), then call ourselves, recursing on the parent clock. This
  233. * can cause an entire branch of the clock tree to be powered off by
  234. * simply disabling one clock. Intended to be called with the clockfw_lock
  235. * spinlock held. No return value.
  236. */
  237. void omap2_clk_disable(struct clk *clk)
  238. {
  239. if (clk->usecount == 0) {
  240. WARN(1, "clock: %s: omap2_clk_disable() called, but usecount 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: %d\n",
  288. 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. /*
  340. * OMAP2+ clock reset and init functions
  341. */
  342. #ifdef CONFIG_OMAP_RESET_CLOCKS
  343. void omap2_clk_disable_unused(struct clk *clk)
  344. {
  345. u32 regval32, v;
  346. v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
  347. regval32 = __raw_readl(clk->enable_reg);
  348. if ((regval32 & (1 << clk->enable_bit)) == v)
  349. return;
  350. pr_debug("Disabling unused clock \"%s\"\n", clk->name);
  351. if (cpu_is_omap34xx()) {
  352. omap2_clk_enable(clk);
  353. omap2_clk_disable(clk);
  354. } else {
  355. clk->ops->disable(clk);
  356. }
  357. if (clk->clkdm != NULL)
  358. pwrdm_state_switch(clk->clkdm->pwrdm.ptr);
  359. }
  360. #endif
  361. /**
  362. * omap2_clk_switch_mpurate_at_boot - switch ARM MPU rate by boot-time argument
  363. * @mpurate_ck_name: clk name of the clock to change rate
  364. *
  365. * Change the ARM MPU clock rate to the rate specified on the command
  366. * line, if one was specified. @mpurate_ck_name should be
  367. * "virt_prcm_set" on OMAP2xxx and "dpll1_ck" on OMAP34xx/OMAP36xx.
  368. * XXX Does not handle voltage scaling - on OMAP2xxx this is currently
  369. * handled by the virt_prcm_set clock, but this should be handled by
  370. * the OPP layer. XXX This is intended to be handled by the OPP layer
  371. * code in the near future and should be removed from the clock code.
  372. * Returns -EINVAL if 'mpurate' is zero or if clk_set_rate() rejects
  373. * the rate, -ENOENT if the struct clk referred to by @mpurate_ck_name
  374. * cannot be found, or 0 upon success.
  375. */
  376. int __init omap2_clk_switch_mpurate_at_boot(const char *mpurate_ck_name)
  377. {
  378. struct clk *mpurate_ck;
  379. int r;
  380. if (!mpurate)
  381. return -EINVAL;
  382. mpurate_ck = clk_get(NULL, mpurate_ck_name);
  383. if (WARN(IS_ERR(mpurate_ck), "Failed to get %s.\n", mpurate_ck_name))
  384. return -ENOENT;
  385. r = clk_set_rate(mpurate_ck, mpurate);
  386. if (IS_ERR_VALUE(r)) {
  387. WARN(1, "clock: %s: unable to set MPU rate to %d: %d\n",
  388. mpurate_ck->name, mpurate, r);
  389. clk_put(mpurate_ck);
  390. return -EINVAL;
  391. }
  392. calibrate_delay();
  393. recalculate_root_clocks();
  394. clk_put(mpurate_ck);
  395. return 0;
  396. }
  397. /**
  398. * omap2_clk_print_new_rates - print summary of current clock tree rates
  399. * @hfclkin_ck_name: clk name for the off-chip HF oscillator
  400. * @core_ck_name: clk name for the on-chip CORE_CLK
  401. * @mpu_ck_name: clk name for the ARM MPU clock
  402. *
  403. * Prints a short message to the console with the HFCLKIN oscillator
  404. * rate, the rate of the CORE clock, and the rate of the ARM MPU clock.
  405. * Called by the boot-time MPU rate switching code. XXX This is intended
  406. * to be handled by the OPP layer code in the near future and should be
  407. * removed from the clock code. No return value.
  408. */
  409. void __init omap2_clk_print_new_rates(const char *hfclkin_ck_name,
  410. const char *core_ck_name,
  411. const char *mpu_ck_name)
  412. {
  413. struct clk *hfclkin_ck, *core_ck, *mpu_ck;
  414. unsigned long hfclkin_rate;
  415. mpu_ck = clk_get(NULL, mpu_ck_name);
  416. if (WARN(IS_ERR(mpu_ck), "clock: failed to get %s.\n", mpu_ck_name))
  417. return;
  418. core_ck = clk_get(NULL, core_ck_name);
  419. if (WARN(IS_ERR(core_ck), "clock: failed to get %s.\n", core_ck_name))
  420. return;
  421. hfclkin_ck = clk_get(NULL, hfclkin_ck_name);
  422. if (WARN(IS_ERR(hfclkin_ck), "Failed to get %s.\n", hfclkin_ck_name))
  423. return;
  424. hfclkin_rate = clk_get_rate(hfclkin_ck);
  425. pr_info("Switched to new clocking rate (Crystal/Core/MPU): %ld.%01ld/%ld/%ld MHz\n",
  426. (hfclkin_rate / 1000000), ((hfclkin_rate / 100000) % 10),
  427. (clk_get_rate(core_ck) / 1000000),
  428. (clk_get_rate(mpu_ck) / 1000000));
  429. }
  430. /* Common data */
  431. struct clk_functions omap2_clk_functions = {
  432. .clk_enable = omap2_clk_enable,
  433. .clk_disable = omap2_clk_disable,
  434. .clk_round_rate = omap2_clk_round_rate,
  435. .clk_set_rate = omap2_clk_set_rate,
  436. .clk_set_parent = omap2_clk_set_parent,
  437. .clk_disable_unused = omap2_clk_disable_unused,
  438. };