clock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. 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->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 clkdm %s\n",
  91. 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. pr_err("clock: clk_disable called on independent clock %s which has no enable_reg\n", clk->name);
  200. return;
  201. }
  202. v = __raw_readl(clk->enable_reg);
  203. if (clk->flags & INVERT_ENABLE)
  204. v |= (1 << clk->enable_bit);
  205. else
  206. v &= ~(1 << clk->enable_bit);
  207. __raw_writel(v, clk->enable_reg);
  208. /* No OCP barrier needed here since it is a disable operation */
  209. }
  210. const struct clkops clkops_omap2_dflt_wait = {
  211. .enable = omap2_dflt_clk_enable,
  212. .disable = omap2_dflt_clk_disable,
  213. .find_companion = omap2_clk_dflt_find_companion,
  214. .find_idlest = omap2_clk_dflt_find_idlest,
  215. };
  216. const struct clkops clkops_omap2_dflt = {
  217. .enable = omap2_dflt_clk_enable,
  218. .disable = omap2_dflt_clk_disable,
  219. };
  220. /**
  221. * omap2_clk_disable - disable a clock, if the system is not using it
  222. * @clk: struct clk * to disable
  223. *
  224. * Decrements the usecount on struct clk @clk. If there are no users
  225. * left, call the clkops-specific clock disable function to disable it
  226. * in hardware. If the clock is part of a clockdomain (which they all
  227. * should be), request that the clockdomain be disabled. (It too has
  228. * a usecount, and so will not be disabled in the hardware until it no
  229. * longer has any users.) If the clock has a parent clock (most of
  230. * them do), then call ourselves, recursing on the parent clock. This
  231. * can cause an entire branch of the clock tree to be powered off by
  232. * simply disabling one clock. Intended to be called with the clockfw_lock
  233. * spinlock held. No return value.
  234. */
  235. void omap2_clk_disable(struct clk *clk)
  236. {
  237. if (clk->usecount == 0) {
  238. WARN(1, "clock: %s: omap2_clk_disable() called, but usecount already 0?", clk->name);
  239. return;
  240. }
  241. pr_debug("clock: %s: decrementing usecount\n", clk->name);
  242. clk->usecount--;
  243. if (clk->usecount > 0)
  244. return;
  245. pr_debug("clock: %s: disabling in hardware\n", clk->name);
  246. if (clk->ops && clk->ops->disable) {
  247. trace_clock_disable(clk->name, 0, smp_processor_id());
  248. clk->ops->disable(clk);
  249. }
  250. if (clkdm_control && clk->clkdm)
  251. clkdm_clk_disable(clk->clkdm, clk);
  252. if (clk->parent)
  253. omap2_clk_disable(clk->parent);
  254. }
  255. /**
  256. * omap2_clk_enable - request that the system enable a clock
  257. * @clk: struct clk * to enable
  258. *
  259. * Increments the usecount on struct clk @clk. If there were no users
  260. * previously, then recurse up the clock tree, enabling all of the
  261. * clock's parents and all of the parent clockdomains, and finally,
  262. * enabling @clk's clockdomain, and @clk itself. Intended to be
  263. * called with the clockfw_lock spinlock held. Returns 0 upon success
  264. * or a negative error code upon failure.
  265. */
  266. int omap2_clk_enable(struct clk *clk)
  267. {
  268. int ret;
  269. pr_debug("clock: %s: incrementing usecount\n", clk->name);
  270. clk->usecount++;
  271. if (clk->usecount > 1)
  272. return 0;
  273. pr_debug("clock: %s: enabling in hardware\n", clk->name);
  274. if (clk->parent) {
  275. ret = omap2_clk_enable(clk->parent);
  276. if (ret) {
  277. WARN(1, "clock: %s: could not enable parent %s: %d\n",
  278. clk->name, clk->parent->name, ret);
  279. goto oce_err1;
  280. }
  281. }
  282. if (clkdm_control && clk->clkdm) {
  283. ret = clkdm_clk_enable(clk->clkdm, clk);
  284. if (ret) {
  285. WARN(1, "clock: %s: could not enable clockdomain %s: %d\n",
  286. clk->name, clk->clkdm->name, ret);
  287. goto oce_err2;
  288. }
  289. }
  290. if (clk->ops && clk->ops->enable) {
  291. trace_clock_enable(clk->name, 1, smp_processor_id());
  292. ret = clk->ops->enable(clk);
  293. if (ret) {
  294. WARN(1, "clock: %s: could not enable: %d\n",
  295. clk->name, ret);
  296. goto oce_err3;
  297. }
  298. }
  299. return 0;
  300. oce_err3:
  301. if (clkdm_control && clk->clkdm)
  302. clkdm_clk_disable(clk->clkdm, clk);
  303. oce_err2:
  304. if (clk->parent)
  305. omap2_clk_disable(clk->parent);
  306. oce_err1:
  307. clk->usecount--;
  308. return ret;
  309. }
  310. /* Given a clock and a rate apply a clock specific rounding function */
  311. long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
  312. {
  313. if (clk->round_rate)
  314. return clk->round_rate(clk, rate);
  315. return clk->rate;
  316. }
  317. /* Set the clock rate for a clock source */
  318. int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
  319. {
  320. int ret = -EINVAL;
  321. pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
  322. /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
  323. if (clk->set_rate) {
  324. trace_clock_set_rate(clk->name, rate, smp_processor_id());
  325. ret = clk->set_rate(clk, rate);
  326. }
  327. return ret;
  328. }
  329. int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
  330. {
  331. if (!clk->clksel)
  332. return -EINVAL;
  333. if (clk->parent == new_parent)
  334. return 0;
  335. return omap2_clksel_set_parent(clk, new_parent);
  336. }
  337. /*
  338. * OMAP2+ clock reset and init functions
  339. */
  340. #ifdef CONFIG_OMAP_RESET_CLOCKS
  341. void omap2_clk_disable_unused(struct clk *clk)
  342. {
  343. u32 regval32, v;
  344. v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
  345. regval32 = __raw_readl(clk->enable_reg);
  346. if ((regval32 & (1 << clk->enable_bit)) == v)
  347. return;
  348. pr_debug("Disabling unused clock \"%s\"\n", clk->name);
  349. if (cpu_is_omap34xx()) {
  350. omap2_clk_enable(clk);
  351. omap2_clk_disable(clk);
  352. } else {
  353. clk->ops->disable(clk);
  354. }
  355. if (clk->clkdm != NULL)
  356. pwrdm_state_switch(clk->clkdm->pwrdm.ptr);
  357. }
  358. #endif
  359. /**
  360. * omap2_clk_switch_mpurate_at_boot - switch ARM MPU rate by boot-time argument
  361. * @mpurate_ck_name: clk name of the clock to change rate
  362. *
  363. * Change the ARM MPU clock rate to the rate specified on the command
  364. * line, if one was specified. @mpurate_ck_name should be
  365. * "virt_prcm_set" on OMAP2xxx and "dpll1_ck" on OMAP34xx/OMAP36xx.
  366. * XXX Does not handle voltage scaling - on OMAP2xxx this is currently
  367. * handled by the virt_prcm_set clock, but this should be handled by
  368. * the OPP layer. XXX This is intended to be handled by the OPP layer
  369. * code in the near future and should be removed from the clock code.
  370. * Returns -EINVAL if 'mpurate' is zero or if clk_set_rate() rejects
  371. * the rate, -ENOENT if the struct clk referred to by @mpurate_ck_name
  372. * cannot be found, or 0 upon success.
  373. */
  374. int __init omap2_clk_switch_mpurate_at_boot(const char *mpurate_ck_name)
  375. {
  376. struct clk *mpurate_ck;
  377. int r;
  378. if (!mpurate)
  379. return -EINVAL;
  380. mpurate_ck = clk_get(NULL, mpurate_ck_name);
  381. if (WARN(IS_ERR(mpurate_ck), "Failed to get %s.\n", mpurate_ck_name))
  382. return -ENOENT;
  383. r = clk_set_rate(mpurate_ck, mpurate);
  384. if (IS_ERR_VALUE(r)) {
  385. WARN(1, "clock: %s: unable to set MPU rate to %d: %d\n",
  386. mpurate_ck->name, mpurate, r);
  387. clk_put(mpurate_ck);
  388. return -EINVAL;
  389. }
  390. calibrate_delay();
  391. recalculate_root_clocks();
  392. clk_put(mpurate_ck);
  393. return 0;
  394. }
  395. /**
  396. * omap2_clk_print_new_rates - print summary of current clock tree rates
  397. * @hfclkin_ck_name: clk name for the off-chip HF oscillator
  398. * @core_ck_name: clk name for the on-chip CORE_CLK
  399. * @mpu_ck_name: clk name for the ARM MPU clock
  400. *
  401. * Prints a short message to the console with the HFCLKIN oscillator
  402. * rate, the rate of the CORE clock, and the rate of the ARM MPU clock.
  403. * Called by the boot-time MPU rate switching code. XXX This is intended
  404. * to be handled by the OPP layer code in the near future and should be
  405. * removed from the clock code. No return value.
  406. */
  407. void __init omap2_clk_print_new_rates(const char *hfclkin_ck_name,
  408. const char *core_ck_name,
  409. const char *mpu_ck_name)
  410. {
  411. struct clk *hfclkin_ck, *core_ck, *mpu_ck;
  412. unsigned long hfclkin_rate;
  413. mpu_ck = clk_get(NULL, mpu_ck_name);
  414. if (WARN(IS_ERR(mpu_ck), "clock: failed to get %s.\n", mpu_ck_name))
  415. return;
  416. core_ck = clk_get(NULL, core_ck_name);
  417. if (WARN(IS_ERR(core_ck), "clock: failed to get %s.\n", core_ck_name))
  418. return;
  419. hfclkin_ck = clk_get(NULL, hfclkin_ck_name);
  420. if (WARN(IS_ERR(hfclkin_ck), "Failed to get %s.\n", hfclkin_ck_name))
  421. return;
  422. hfclkin_rate = clk_get_rate(hfclkin_ck);
  423. pr_info("Switched to new clocking rate (Crystal/Core/MPU): %ld.%01ld/%ld/%ld MHz\n",
  424. (hfclkin_rate / 1000000), ((hfclkin_rate / 100000) % 10),
  425. (clk_get_rate(core_ck) / 1000000),
  426. (clk_get_rate(mpu_ck) / 1000000));
  427. }
  428. /* Common data */
  429. struct clk_functions omap2_clk_functions = {
  430. .clk_enable = omap2_clk_enable,
  431. .clk_disable = omap2_clk_disable,
  432. .clk_round_rate = omap2_clk_round_rate,
  433. .clk_set_rate = omap2_clk_set_rate,
  434. .clk_set_parent = omap2_clk_set_parent,
  435. .clk_disable_unused = omap2_clk_disable_unused,
  436. };