clock.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /*
  2. * linux/arch/arm/mach-omap2/clock.c
  3. *
  4. * Copyright (C) 2005-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2004-2008 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/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/list.h>
  20. #include <linux/errno.h>
  21. #include <linux/delay.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/bitops.h>
  25. #include <plat/clock.h>
  26. #include <plat/clockdomain.h>
  27. #include <plat/cpu.h>
  28. #include <plat/prcm.h>
  29. #include <asm/div64.h>
  30. #include <plat/sdrc.h>
  31. #include "sdrc.h"
  32. #include "clock.h"
  33. #include "prm.h"
  34. #include "prm-regbits-24xx.h"
  35. #include "cm.h"
  36. #include "cm-regbits-24xx.h"
  37. #include "cm-regbits-34xx.h"
  38. /* DPLL rate rounding: minimum DPLL multiplier, divider values */
  39. #define DPLL_MIN_MULTIPLIER 1
  40. #define DPLL_MIN_DIVIDER 1
  41. /* Possible error results from _dpll_test_mult */
  42. #define DPLL_MULT_UNDERFLOW -1
  43. /*
  44. * Scale factor to mitigate roundoff errors in DPLL rate rounding.
  45. * The higher the scale factor, the greater the risk of arithmetic overflow,
  46. * but the closer the rounded rate to the target rate. DPLL_SCALE_FACTOR
  47. * must be a power of DPLL_SCALE_BASE.
  48. */
  49. #define DPLL_SCALE_FACTOR 64
  50. #define DPLL_SCALE_BASE 2
  51. #define DPLL_ROUNDING_VAL ((DPLL_SCALE_BASE / 2) * \
  52. (DPLL_SCALE_FACTOR / DPLL_SCALE_BASE))
  53. /* DPLL valid Fint frequency band limits - from 34xx TRM Section 4.7.6.2 */
  54. #define DPLL_FINT_BAND1_MIN 750000
  55. #define DPLL_FINT_BAND1_MAX 2100000
  56. #define DPLL_FINT_BAND2_MIN 7500000
  57. #define DPLL_FINT_BAND2_MAX 21000000
  58. /* _dpll_test_fint() return codes */
  59. #define DPLL_FINT_UNDERFLOW -1
  60. #define DPLL_FINT_INVALID -2
  61. u8 cpu_mask;
  62. /*-------------------------------------------------------------------------
  63. * OMAP2/3 specific clock functions
  64. *-------------------------------------------------------------------------*/
  65. /**
  66. * _omap2xxx_clk_commit - commit clock parent/rate changes in hardware
  67. * @clk: struct clk *
  68. *
  69. * If @clk has the DELAYED_APP flag set, meaning that parent/rate changes
  70. * don't take effect until the VALID_CONFIG bit is written, write the
  71. * VALID_CONFIG bit and wait for the write to complete. No return value.
  72. */
  73. static void _omap2xxx_clk_commit(struct clk *clk)
  74. {
  75. if (!cpu_is_omap24xx())
  76. return;
  77. if (!(clk->flags & DELAYED_APP))
  78. return;
  79. prm_write_mod_reg(OMAP24XX_VALID_CONFIG, OMAP24XX_GR_MOD,
  80. OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
  81. /* OCP barrier */
  82. prm_read_mod_reg(OMAP24XX_GR_MOD, OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
  83. }
  84. /*
  85. * _dpll_test_fint - test whether an Fint value is valid for the DPLL
  86. * @clk: DPLL struct clk to test
  87. * @n: divider value (N) to test
  88. *
  89. * Tests whether a particular divider @n will result in a valid DPLL
  90. * internal clock frequency Fint. See the 34xx TRM 4.7.6.2 "DPLL Jitter
  91. * Correction". Returns 0 if OK, -1 if the enclosing loop can terminate
  92. * (assuming that it is counting N upwards), or -2 if the enclosing loop
  93. * should skip to the next iteration (again assuming N is increasing).
  94. */
  95. static int _dpll_test_fint(struct clk *clk, u8 n)
  96. {
  97. struct dpll_data *dd;
  98. long fint;
  99. int ret = 0;
  100. dd = clk->dpll_data;
  101. /* DPLL divider must result in a valid jitter correction val */
  102. fint = clk->parent->rate / (n + 1);
  103. if (fint < DPLL_FINT_BAND1_MIN) {
  104. pr_debug("rejecting n=%d due to Fint failure, "
  105. "lowering max_divider\n", n);
  106. dd->max_divider = n;
  107. ret = DPLL_FINT_UNDERFLOW;
  108. } else if (fint > DPLL_FINT_BAND1_MAX &&
  109. fint < DPLL_FINT_BAND2_MIN) {
  110. pr_debug("rejecting n=%d due to Fint failure\n", n);
  111. ret = DPLL_FINT_INVALID;
  112. } else if (fint > DPLL_FINT_BAND2_MAX) {
  113. pr_debug("rejecting n=%d due to Fint failure, "
  114. "boosting min_divider\n", n);
  115. dd->min_divider = n;
  116. ret = DPLL_FINT_INVALID;
  117. }
  118. return ret;
  119. }
  120. /**
  121. * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
  122. * @clk: OMAP clock struct ptr to use
  123. *
  124. * Convert a clockdomain name stored in a struct clk 'clk' into a
  125. * clockdomain pointer, and save it into the struct clk. Intended to be
  126. * called during clk_register(). No return value.
  127. */
  128. #ifndef CONFIG_ARCH_OMAP4 /* FIXME: Remove this once clkdm f/w is in place */
  129. void omap2_init_clk_clkdm(struct clk *clk)
  130. {
  131. struct clockdomain *clkdm;
  132. if (!clk->clkdm_name)
  133. return;
  134. clkdm = clkdm_lookup(clk->clkdm_name);
  135. if (clkdm) {
  136. pr_debug("clock: associated clk %s to clkdm %s\n",
  137. clk->name, clk->clkdm_name);
  138. clk->clkdm = clkdm;
  139. } else {
  140. pr_debug("clock: could not associate clk %s to "
  141. "clkdm %s\n", clk->name, clk->clkdm_name);
  142. }
  143. }
  144. #endif
  145. /**
  146. * omap2_init_clksel_parent - set a clksel clk's parent field from the hardware
  147. * @clk: OMAP clock struct ptr to use
  148. *
  149. * Given a pointer to a source-selectable struct clk, read the hardware
  150. * register and determine what its parent is currently set to. Update the
  151. * clk->parent field with the appropriate clk ptr.
  152. */
  153. void omap2_init_clksel_parent(struct clk *clk)
  154. {
  155. const struct clksel *clks;
  156. const struct clksel_rate *clkr;
  157. u32 r, found = 0;
  158. if (!clk->clksel)
  159. return;
  160. r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  161. r >>= __ffs(clk->clksel_mask);
  162. for (clks = clk->clksel; clks->parent && !found; clks++) {
  163. for (clkr = clks->rates; clkr->div && !found; clkr++) {
  164. if ((clkr->flags & cpu_mask) && (clkr->val == r)) {
  165. if (clk->parent != clks->parent) {
  166. pr_debug("clock: inited %s parent "
  167. "to %s (was %s)\n",
  168. clk->name, clks->parent->name,
  169. ((clk->parent) ?
  170. clk->parent->name : "NULL"));
  171. clk_reparent(clk, clks->parent);
  172. };
  173. found = 1;
  174. }
  175. }
  176. }
  177. if (!found)
  178. printk(KERN_ERR "clock: init parent: could not find "
  179. "regval %0x for clock %s\n", r, clk->name);
  180. return;
  181. }
  182. /**
  183. * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
  184. * @clk: struct clk * of a DPLL
  185. *
  186. * DPLLs can be locked or bypassed - basically, enabled or disabled.
  187. * When locked, the DPLL output depends on the M and N values. When
  188. * bypassed, on OMAP2xxx, the output rate is either the 32KiHz clock
  189. * or sys_clk. Bypass rates on OMAP3 depend on the DPLL: DPLLs 1 and
  190. * 2 are bypassed with dpll1_fclk and dpll2_fclk respectively
  191. * (generated by DPLL3), while DPLL 3, 4, and 5 bypass rates are sys_clk.
  192. * Returns the current DPLL CLKOUT rate (*not* CLKOUTX2) if the DPLL is
  193. * locked, or the appropriate bypass rate if the DPLL is bypassed, or 0
  194. * if the clock @clk is not a DPLL.
  195. */
  196. u32 omap2_get_dpll_rate(struct clk *clk)
  197. {
  198. long long dpll_clk;
  199. u32 dpll_mult, dpll_div, v;
  200. struct dpll_data *dd;
  201. dd = clk->dpll_data;
  202. if (!dd)
  203. return 0;
  204. /* Return bypass rate if DPLL is bypassed */
  205. v = __raw_readl(dd->control_reg);
  206. v &= dd->enable_mask;
  207. v >>= __ffs(dd->enable_mask);
  208. if (cpu_is_omap24xx()) {
  209. if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
  210. v == OMAP2XXX_EN_DPLL_FRBYPASS)
  211. return dd->clk_bypass->rate;
  212. } else if (cpu_is_omap34xx()) {
  213. if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
  214. v == OMAP3XXX_EN_DPLL_FRBYPASS)
  215. return dd->clk_bypass->rate;
  216. } else if (cpu_is_omap44xx()) {
  217. if (v == OMAP4XXX_EN_DPLL_LPBYPASS ||
  218. v == OMAP4XXX_EN_DPLL_FRBYPASS ||
  219. v == OMAP4XXX_EN_DPLL_MNBYPASS)
  220. return dd->clk_bypass->rate;
  221. }
  222. v = __raw_readl(dd->mult_div1_reg);
  223. dpll_mult = v & dd->mult_mask;
  224. dpll_mult >>= __ffs(dd->mult_mask);
  225. dpll_div = v & dd->div1_mask;
  226. dpll_div >>= __ffs(dd->div1_mask);
  227. dpll_clk = (long long)dd->clk_ref->rate * dpll_mult;
  228. do_div(dpll_clk, dpll_div + 1);
  229. return dpll_clk;
  230. }
  231. /*
  232. * Used for clocks that have the same value as the parent clock,
  233. * divided by some factor
  234. */
  235. unsigned long omap2_fixed_divisor_recalc(struct clk *clk)
  236. {
  237. WARN_ON(!clk->fixed_div);
  238. return clk->parent->rate / clk->fixed_div;
  239. }
  240. /**
  241. * omap2_clk_dflt_find_companion - find companion clock to @clk
  242. * @clk: struct clk * to find the companion clock of
  243. * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
  244. * @other_bit: u8 ** to return the companion clock bit shift in
  245. *
  246. * Note: We don't need special code here for INVERT_ENABLE for the
  247. * time being since INVERT_ENABLE only applies to clocks enabled by
  248. * CM_CLKEN_PLL
  249. *
  250. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
  251. * just a matter of XORing the bits.
  252. *
  253. * Some clocks don't have companion clocks. For example, modules with
  254. * only an interface clock (such as MAILBOXES) don't have a companion
  255. * clock. Right now, this code relies on the hardware exporting a bit
  256. * in the correct companion register that indicates that the
  257. * nonexistent 'companion clock' is active. Future patches will
  258. * associate this type of code with per-module data structures to
  259. * avoid this issue, and remove the casts. No return value.
  260. */
  261. void omap2_clk_dflt_find_companion(struct clk *clk, void __iomem **other_reg,
  262. u8 *other_bit)
  263. {
  264. u32 r;
  265. /*
  266. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
  267. * it's just a matter of XORing the bits.
  268. */
  269. r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
  270. *other_reg = (__force void __iomem *)r;
  271. *other_bit = clk->enable_bit;
  272. }
  273. /**
  274. * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
  275. * @clk: struct clk * to find IDLEST info for
  276. * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
  277. * @idlest_bit: u8 ** to return the CM_IDLEST bit shift in
  278. *
  279. * Return the CM_IDLEST register address and bit shift corresponding
  280. * to the module that "owns" this clock. This default code assumes
  281. * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
  282. * the IDLEST register address ID corresponds to the CM_*CLKEN
  283. * register address ID (e.g., that CM_FCLKEN2 corresponds to
  284. * CM_IDLEST2). This is not true for all modules. No return value.
  285. */
  286. void omap2_clk_dflt_find_idlest(struct clk *clk, void __iomem **idlest_reg,
  287. u8 *idlest_bit)
  288. {
  289. u32 r;
  290. r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
  291. *idlest_reg = (__force void __iomem *)r;
  292. *idlest_bit = clk->enable_bit;
  293. }
  294. /**
  295. * omap2_module_wait_ready - wait for an OMAP module to leave IDLE
  296. * @clk: struct clk * belonging to the module
  297. *
  298. * If the necessary clocks for the OMAP hardware IP block that
  299. * corresponds to clock @clk are enabled, then wait for the module to
  300. * indicate readiness (i.e., to leave IDLE). This code does not
  301. * belong in the clock code and will be moved in the medium term to
  302. * module-dependent code. No return value.
  303. */
  304. static void omap2_module_wait_ready(struct clk *clk)
  305. {
  306. void __iomem *companion_reg, *idlest_reg;
  307. u8 other_bit, idlest_bit;
  308. /* Not all modules have multiple clocks that their IDLEST depends on */
  309. if (clk->ops->find_companion) {
  310. clk->ops->find_companion(clk, &companion_reg, &other_bit);
  311. if (!(__raw_readl(companion_reg) & (1 << other_bit)))
  312. return;
  313. }
  314. clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit);
  315. omap2_cm_wait_idlest(idlest_reg, (1 << idlest_bit), clk->name);
  316. }
  317. int omap2_dflt_clk_enable(struct clk *clk)
  318. {
  319. u32 v;
  320. if (unlikely(clk->enable_reg == NULL)) {
  321. pr_err("clock.c: Enable for %s without enable code\n",
  322. clk->name);
  323. return 0; /* REVISIT: -EINVAL */
  324. }
  325. v = __raw_readl(clk->enable_reg);
  326. if (clk->flags & INVERT_ENABLE)
  327. v &= ~(1 << clk->enable_bit);
  328. else
  329. v |= (1 << clk->enable_bit);
  330. __raw_writel(v, clk->enable_reg);
  331. v = __raw_readl(clk->enable_reg); /* OCP barrier */
  332. if (clk->ops->find_idlest)
  333. omap2_module_wait_ready(clk);
  334. return 0;
  335. }
  336. void omap2_dflt_clk_disable(struct clk *clk)
  337. {
  338. u32 v;
  339. if (!clk->enable_reg) {
  340. /*
  341. * 'Independent' here refers to a clock which is not
  342. * controlled by its parent.
  343. */
  344. printk(KERN_ERR "clock: clk_disable called on independent "
  345. "clock %s which has no enable_reg\n", clk->name);
  346. return;
  347. }
  348. v = __raw_readl(clk->enable_reg);
  349. if (clk->flags & INVERT_ENABLE)
  350. v |= (1 << clk->enable_bit);
  351. else
  352. v &= ~(1 << clk->enable_bit);
  353. __raw_writel(v, clk->enable_reg);
  354. /* No OCP barrier needed here since it is a disable operation */
  355. }
  356. const struct clkops clkops_omap2_dflt_wait = {
  357. .enable = omap2_dflt_clk_enable,
  358. .disable = omap2_dflt_clk_disable,
  359. .find_companion = omap2_clk_dflt_find_companion,
  360. .find_idlest = omap2_clk_dflt_find_idlest,
  361. };
  362. const struct clkops clkops_omap2_dflt = {
  363. .enable = omap2_dflt_clk_enable,
  364. .disable = omap2_dflt_clk_disable,
  365. };
  366. /* Enables clock without considering parent dependencies or use count
  367. * REVISIT: Maybe change this to use clk->enable like on omap1?
  368. */
  369. static int _omap2_clk_enable(struct clk *clk)
  370. {
  371. return clk->ops->enable(clk);
  372. }
  373. /* Disables clock without considering parent dependencies or use count */
  374. static void _omap2_clk_disable(struct clk *clk)
  375. {
  376. clk->ops->disable(clk);
  377. }
  378. void omap2_clk_disable(struct clk *clk)
  379. {
  380. if (clk->usecount > 0 && !(--clk->usecount)) {
  381. _omap2_clk_disable(clk);
  382. if (clk->parent)
  383. omap2_clk_disable(clk->parent);
  384. #ifndef CONFIG_ARCH_OMAP4 /* FIXME: Remove this once clkdm f/w is in place */
  385. if (clk->clkdm)
  386. omap2_clkdm_clk_disable(clk->clkdm, clk);
  387. #endif
  388. }
  389. }
  390. int omap2_clk_enable(struct clk *clk)
  391. {
  392. int ret = 0;
  393. if (clk->usecount++ == 0) {
  394. #ifndef CONFIG_ARCH_OMAP4 /* FIXME: Remove this once clkdm f/w is in place */
  395. if (clk->clkdm)
  396. omap2_clkdm_clk_enable(clk->clkdm, clk);
  397. #endif
  398. if (clk->parent) {
  399. ret = omap2_clk_enable(clk->parent);
  400. if (ret)
  401. goto err;
  402. }
  403. ret = _omap2_clk_enable(clk);
  404. if (ret) {
  405. if (clk->parent)
  406. omap2_clk_disable(clk->parent);
  407. goto err;
  408. }
  409. }
  410. return ret;
  411. err:
  412. #ifndef CONFIG_ARCH_OMAP4 /* FIXME: Remove this once clkdm f/w is in place */
  413. if (clk->clkdm)
  414. omap2_clkdm_clk_disable(clk->clkdm, clk);
  415. #endif
  416. clk->usecount--;
  417. return ret;
  418. }
  419. /*
  420. * Used for clocks that are part of CLKSEL_xyz governed clocks.
  421. * REVISIT: Maybe change to use clk->enable() functions like on omap1?
  422. */
  423. unsigned long omap2_clksel_recalc(struct clk *clk)
  424. {
  425. unsigned long rate;
  426. u32 div = 0;
  427. pr_debug("clock: recalc'ing clksel clk %s\n", clk->name);
  428. div = omap2_clksel_get_divisor(clk);
  429. if (div == 0)
  430. return clk->rate;
  431. rate = clk->parent->rate / div;
  432. pr_debug("clock: new clock rate is %ld (div %d)\n", rate, div);
  433. return rate;
  434. }
  435. /**
  436. * omap2_get_clksel_by_parent - return clksel struct for a given clk & parent
  437. * @clk: OMAP struct clk ptr to inspect
  438. * @src_clk: OMAP struct clk ptr of the parent clk to search for
  439. *
  440. * Scan the struct clksel array associated with the clock to find
  441. * the element associated with the supplied parent clock address.
  442. * Returns a pointer to the struct clksel on success or NULL on error.
  443. */
  444. static const struct clksel *omap2_get_clksel_by_parent(struct clk *clk,
  445. struct clk *src_clk)
  446. {
  447. const struct clksel *clks;
  448. if (!clk->clksel)
  449. return NULL;
  450. for (clks = clk->clksel; clks->parent; clks++) {
  451. if (clks->parent == src_clk)
  452. break; /* Found the requested parent */
  453. }
  454. if (!clks->parent) {
  455. printk(KERN_ERR "clock: Could not find parent clock %s in "
  456. "clksel array of clock %s\n", src_clk->name,
  457. clk->name);
  458. return NULL;
  459. }
  460. return clks;
  461. }
  462. /**
  463. * omap2_clksel_round_rate_div - find divisor for the given clock and rate
  464. * @clk: OMAP struct clk to use
  465. * @target_rate: desired clock rate
  466. * @new_div: ptr to where we should store the divisor
  467. *
  468. * Finds 'best' divider value in an array based on the source and target
  469. * rates. The divider array must be sorted with smallest divider first.
  470. * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
  471. * they are only settable as part of virtual_prcm set.
  472. *
  473. * Returns the rounded clock rate or returns 0xffffffff on error.
  474. */
  475. u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
  476. u32 *new_div)
  477. {
  478. unsigned long test_rate;
  479. const struct clksel *clks;
  480. const struct clksel_rate *clkr;
  481. u32 last_div = 0;
  482. pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",
  483. clk->name, target_rate);
  484. *new_div = 1;
  485. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  486. if (!clks)
  487. return ~0;
  488. for (clkr = clks->rates; clkr->div; clkr++) {
  489. if (!(clkr->flags & cpu_mask))
  490. continue;
  491. /* Sanity check */
  492. if (clkr->div <= last_div)
  493. pr_err("clock: clksel_rate table not sorted "
  494. "for clock %s", clk->name);
  495. last_div = clkr->div;
  496. test_rate = clk->parent->rate / clkr->div;
  497. if (test_rate <= target_rate)
  498. break; /* found it */
  499. }
  500. if (!clkr->div) {
  501. pr_err("clock: Could not find divisor for target "
  502. "rate %ld for clock %s parent %s\n", target_rate,
  503. clk->name, clk->parent->name);
  504. return ~0;
  505. }
  506. *new_div = clkr->div;
  507. pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div,
  508. (clk->parent->rate / clkr->div));
  509. return (clk->parent->rate / clkr->div);
  510. }
  511. /**
  512. * omap2_clksel_round_rate - find rounded rate for the given clock and rate
  513. * @clk: OMAP struct clk to use
  514. * @target_rate: desired clock rate
  515. *
  516. * Compatibility wrapper for OMAP clock framework
  517. * Finds best target rate based on the source clock and possible dividers.
  518. * rates. The divider array must be sorted with smallest divider first.
  519. * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
  520. * they are only settable as part of virtual_prcm set.
  521. *
  522. * Returns the rounded clock rate or returns 0xffffffff on error.
  523. */
  524. long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
  525. {
  526. u32 new_div;
  527. return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
  528. }
  529. /* Given a clock and a rate apply a clock specific rounding function */
  530. long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
  531. {
  532. if (clk->round_rate)
  533. return clk->round_rate(clk, rate);
  534. if (clk->flags & RATE_FIXED)
  535. printk(KERN_ERR "clock: generic omap2_clk_round_rate called "
  536. "on fixed-rate clock %s\n", clk->name);
  537. return clk->rate;
  538. }
  539. /**
  540. * omap2_clksel_to_divisor() - turn clksel field value into integer divider
  541. * @clk: OMAP struct clk to use
  542. * @field_val: register field value to find
  543. *
  544. * Given a struct clk of a rate-selectable clksel clock, and a register field
  545. * value to search for, find the corresponding clock divisor. The register
  546. * field value should be pre-masked and shifted down so the LSB is at bit 0
  547. * before calling. Returns 0 on error
  548. */
  549. u32 omap2_clksel_to_divisor(struct clk *clk, u32 field_val)
  550. {
  551. const struct clksel *clks;
  552. const struct clksel_rate *clkr;
  553. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  554. if (!clks)
  555. return 0;
  556. for (clkr = clks->rates; clkr->div; clkr++) {
  557. if ((clkr->flags & cpu_mask) && (clkr->val == field_val))
  558. break;
  559. }
  560. if (!clkr->div) {
  561. printk(KERN_ERR "clock: Could not find fieldval %d for "
  562. "clock %s parent %s\n", field_val, clk->name,
  563. clk->parent->name);
  564. return 0;
  565. }
  566. return clkr->div;
  567. }
  568. /**
  569. * omap2_divisor_to_clksel() - turn clksel integer divisor into a field value
  570. * @clk: OMAP struct clk to use
  571. * @div: integer divisor to search for
  572. *
  573. * Given a struct clk of a rate-selectable clksel clock, and a clock divisor,
  574. * find the corresponding register field value. The return register value is
  575. * the value before left-shifting. Returns ~0 on error
  576. */
  577. u32 omap2_divisor_to_clksel(struct clk *clk, u32 div)
  578. {
  579. const struct clksel *clks;
  580. const struct clksel_rate *clkr;
  581. /* should never happen */
  582. WARN_ON(div == 0);
  583. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  584. if (!clks)
  585. return ~0;
  586. for (clkr = clks->rates; clkr->div; clkr++) {
  587. if ((clkr->flags & cpu_mask) && (clkr->div == div))
  588. break;
  589. }
  590. if (!clkr->div) {
  591. printk(KERN_ERR "clock: Could not find divisor %d for "
  592. "clock %s parent %s\n", div, clk->name,
  593. clk->parent->name);
  594. return ~0;
  595. }
  596. return clkr->val;
  597. }
  598. /**
  599. * omap2_clksel_get_divisor - get current divider applied to parent clock.
  600. * @clk: OMAP struct clk to use.
  601. *
  602. * Returns the integer divisor upon success or 0 on error.
  603. */
  604. u32 omap2_clksel_get_divisor(struct clk *clk)
  605. {
  606. u32 v;
  607. if (!clk->clksel_mask)
  608. return 0;
  609. v = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  610. v >>= __ffs(clk->clksel_mask);
  611. return omap2_clksel_to_divisor(clk, v);
  612. }
  613. int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
  614. {
  615. u32 v, field_val, validrate, new_div = 0;
  616. if (!clk->clksel_mask)
  617. return -EINVAL;
  618. validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
  619. if (validrate != rate)
  620. return -EINVAL;
  621. field_val = omap2_divisor_to_clksel(clk, new_div);
  622. if (field_val == ~0)
  623. return -EINVAL;
  624. v = __raw_readl(clk->clksel_reg);
  625. v &= ~clk->clksel_mask;
  626. v |= field_val << __ffs(clk->clksel_mask);
  627. __raw_writel(v, clk->clksel_reg);
  628. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  629. clk->rate = clk->parent->rate / new_div;
  630. _omap2xxx_clk_commit(clk);
  631. return 0;
  632. }
  633. /* Set the clock rate for a clock source */
  634. int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
  635. {
  636. int ret = -EINVAL;
  637. pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
  638. /* CONFIG_PARTICIPANT clocks are changed only in sets via the
  639. rate table mechanism, driven by mpu_speed */
  640. if (clk->flags & CONFIG_PARTICIPANT)
  641. return -EINVAL;
  642. /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
  643. if (clk->set_rate)
  644. ret = clk->set_rate(clk, rate);
  645. return ret;
  646. }
  647. /*
  648. * Converts encoded control register address into a full address
  649. * On error, the return value (parent_div) will be 0.
  650. */
  651. static u32 _omap2_clksel_get_src_field(struct clk *src_clk, struct clk *clk,
  652. u32 *field_val)
  653. {
  654. const struct clksel *clks;
  655. const struct clksel_rate *clkr;
  656. clks = omap2_get_clksel_by_parent(clk, src_clk);
  657. if (!clks)
  658. return 0;
  659. for (clkr = clks->rates; clkr->div; clkr++) {
  660. if (clkr->flags & cpu_mask && clkr->flags & DEFAULT_RATE)
  661. break; /* Found the default rate for this platform */
  662. }
  663. if (!clkr->div) {
  664. printk(KERN_ERR "clock: Could not find default rate for "
  665. "clock %s parent %s\n", clk->name,
  666. src_clk->parent->name);
  667. return 0;
  668. }
  669. /* Should never happen. Add a clksel mask to the struct clk. */
  670. WARN_ON(clk->clksel_mask == 0);
  671. *field_val = clkr->val;
  672. return clkr->div;
  673. }
  674. int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
  675. {
  676. u32 field_val, v, parent_div;
  677. if (clk->flags & CONFIG_PARTICIPANT)
  678. return -EINVAL;
  679. if (!clk->clksel)
  680. return -EINVAL;
  681. parent_div = _omap2_clksel_get_src_field(new_parent, clk, &field_val);
  682. if (!parent_div)
  683. return -EINVAL;
  684. /* Set new source value (previous dividers if any in effect) */
  685. v = __raw_readl(clk->clksel_reg);
  686. v &= ~clk->clksel_mask;
  687. v |= field_val << __ffs(clk->clksel_mask);
  688. __raw_writel(v, clk->clksel_reg);
  689. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  690. _omap2xxx_clk_commit(clk);
  691. clk_reparent(clk, new_parent);
  692. /* CLKSEL clocks follow their parents' rates, divided by a divisor */
  693. clk->rate = new_parent->rate;
  694. if (parent_div > 0)
  695. clk->rate /= parent_div;
  696. pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
  697. clk->name, clk->parent->name, clk->rate);
  698. return 0;
  699. }
  700. /* DPLL rate rounding code */
  701. /**
  702. * omap2_dpll_set_rate_tolerance: set the error tolerance during rate rounding
  703. * @clk: struct clk * of the DPLL
  704. * @tolerance: maximum rate error tolerance
  705. *
  706. * Set the maximum DPLL rate error tolerance for the rate rounding
  707. * algorithm. The rate tolerance is an attempt to balance DPLL power
  708. * saving (the least divider value "n") vs. rate fidelity (the least
  709. * difference between the desired DPLL target rate and the rounded
  710. * rate out of the algorithm). So, increasing the tolerance is likely
  711. * to decrease DPLL power consumption and increase DPLL rate error.
  712. * Returns -EINVAL if provided a null clock ptr or a clk that is not a
  713. * DPLL; or 0 upon success.
  714. */
  715. int omap2_dpll_set_rate_tolerance(struct clk *clk, unsigned int tolerance)
  716. {
  717. if (!clk || !clk->dpll_data)
  718. return -EINVAL;
  719. clk->dpll_data->rate_tolerance = tolerance;
  720. return 0;
  721. }
  722. static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
  723. unsigned int m, unsigned int n)
  724. {
  725. unsigned long long num;
  726. num = (unsigned long long)parent_rate * m;
  727. do_div(num, n);
  728. return num;
  729. }
  730. /*
  731. * _dpll_test_mult - test a DPLL multiplier value
  732. * @m: pointer to the DPLL m (multiplier) value under test
  733. * @n: current DPLL n (divider) value under test
  734. * @new_rate: pointer to storage for the resulting rounded rate
  735. * @target_rate: the desired DPLL rate
  736. * @parent_rate: the DPLL's parent clock rate
  737. *
  738. * This code tests a DPLL multiplier value, ensuring that the
  739. * resulting rate will not be higher than the target_rate, and that
  740. * the multiplier value itself is valid for the DPLL. Initially, the
  741. * integer pointed to by the m argument should be prescaled by
  742. * multiplying by DPLL_SCALE_FACTOR. The code will replace this with
  743. * a non-scaled m upon return. This non-scaled m will result in a
  744. * new_rate as close as possible to target_rate (but not greater than
  745. * target_rate) given the current (parent_rate, n, prescaled m)
  746. * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
  747. * non-scaled m attempted to underflow, which can allow the calling
  748. * function to bail out early; or 0 upon success.
  749. */
  750. static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
  751. unsigned long target_rate,
  752. unsigned long parent_rate)
  753. {
  754. int r = 0, carry = 0;
  755. /* Unscale m and round if necessary */
  756. if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
  757. carry = 1;
  758. *m = (*m / DPLL_SCALE_FACTOR) + carry;
  759. /*
  760. * The new rate must be <= the target rate to avoid programming
  761. * a rate that is impossible for the hardware to handle
  762. */
  763. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  764. if (*new_rate > target_rate) {
  765. (*m)--;
  766. *new_rate = 0;
  767. }
  768. /* Guard against m underflow */
  769. if (*m < DPLL_MIN_MULTIPLIER) {
  770. *m = DPLL_MIN_MULTIPLIER;
  771. *new_rate = 0;
  772. r = DPLL_MULT_UNDERFLOW;
  773. }
  774. if (*new_rate == 0)
  775. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  776. return r;
  777. }
  778. /**
  779. * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
  780. * @clk: struct clk * for a DPLL
  781. * @target_rate: desired DPLL clock rate
  782. *
  783. * Given a DPLL, a desired target rate, and a rate tolerance, round
  784. * the target rate to a possible, programmable rate for this DPLL.
  785. * Rate tolerance is assumed to be set by the caller before this
  786. * function is called. Attempts to select the minimum possible n
  787. * within the tolerance to reduce power consumption. Stores the
  788. * computed (m, n) in the DPLL's dpll_data structure so set_rate()
  789. * will not need to call this (expensive) function again. Returns ~0
  790. * if the target rate cannot be rounded, either because the rate is
  791. * too low or because the rate tolerance is set too tightly; or the
  792. * rounded rate upon success.
  793. */
  794. long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
  795. {
  796. int m, n, r, e, scaled_max_m;
  797. unsigned long scaled_rt_rp, new_rate;
  798. int min_e = -1, min_e_m = -1, min_e_n = -1;
  799. struct dpll_data *dd;
  800. if (!clk || !clk->dpll_data)
  801. return ~0;
  802. dd = clk->dpll_data;
  803. pr_debug("clock: starting DPLL round_rate for clock %s, target rate "
  804. "%ld\n", clk->name, target_rate);
  805. scaled_rt_rp = target_rate / (dd->clk_ref->rate / DPLL_SCALE_FACTOR);
  806. scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
  807. dd->last_rounded_rate = 0;
  808. for (n = dd->min_divider; n <= dd->max_divider; n++) {
  809. /* Is the (input clk, divider) pair valid for the DPLL? */
  810. r = _dpll_test_fint(clk, n);
  811. if (r == DPLL_FINT_UNDERFLOW)
  812. break;
  813. else if (r == DPLL_FINT_INVALID)
  814. continue;
  815. /* Compute the scaled DPLL multiplier, based on the divider */
  816. m = scaled_rt_rp * n;
  817. /*
  818. * Since we're counting n up, a m overflow means we
  819. * can bail out completely (since as n increases in
  820. * the next iteration, there's no way that m can
  821. * increase beyond the current m)
  822. */
  823. if (m > scaled_max_m)
  824. break;
  825. r = _dpll_test_mult(&m, n, &new_rate, target_rate,
  826. dd->clk_ref->rate);
  827. /* m can't be set low enough for this n - try with a larger n */
  828. if (r == DPLL_MULT_UNDERFLOW)
  829. continue;
  830. e = target_rate - new_rate;
  831. pr_debug("clock: n = %d: m = %d: rate error is %d "
  832. "(new_rate = %ld)\n", n, m, e, new_rate);
  833. if (min_e == -1 ||
  834. min_e >= (int)(abs(e) - dd->rate_tolerance)) {
  835. min_e = e;
  836. min_e_m = m;
  837. min_e_n = n;
  838. pr_debug("clock: found new least error %d\n", min_e);
  839. /* We found good settings -- bail out now */
  840. if (min_e <= dd->rate_tolerance)
  841. break;
  842. }
  843. }
  844. if (min_e < 0) {
  845. pr_debug("clock: error: target rate or tolerance too low\n");
  846. return ~0;
  847. }
  848. dd->last_rounded_m = min_e_m;
  849. dd->last_rounded_n = min_e_n;
  850. dd->last_rounded_rate = _dpll_compute_new_rate(dd->clk_ref->rate,
  851. min_e_m, min_e_n);
  852. pr_debug("clock: final least error: e = %d, m = %d, n = %d\n",
  853. min_e, min_e_m, min_e_n);
  854. pr_debug("clock: final rate: %ld (target rate: %ld)\n",
  855. dd->last_rounded_rate, target_rate);
  856. return dd->last_rounded_rate;
  857. }
  858. /*-------------------------------------------------------------------------
  859. * Omap2 clock reset and init functions
  860. *-------------------------------------------------------------------------*/
  861. #ifdef CONFIG_OMAP_RESET_CLOCKS
  862. void omap2_clk_disable_unused(struct clk *clk)
  863. {
  864. u32 regval32, v;
  865. v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
  866. regval32 = __raw_readl(clk->enable_reg);
  867. if ((regval32 & (1 << clk->enable_bit)) == v)
  868. return;
  869. printk(KERN_DEBUG "Disabling unused clock \"%s\"\n", clk->name);
  870. if (cpu_is_omap34xx()) {
  871. omap2_clk_enable(clk);
  872. omap2_clk_disable(clk);
  873. } else
  874. _omap2_clk_disable(clk);
  875. if (clk->clkdm != NULL)
  876. pwrdm_clkdm_state_switch(clk->clkdm);
  877. }
  878. #endif