clock.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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 <mach/clock.h>
  26. #include <mach/clockdomain.h>
  27. #include <mach/cpu.h>
  28. #include <asm/div64.h>
  29. #include <mach/sdrc.h>
  30. #include "sdrc.h"
  31. #include "clock.h"
  32. #include "prm.h"
  33. #include "prm-regbits-24xx.h"
  34. #include "cm.h"
  35. #include "cm-regbits-24xx.h"
  36. #include "cm-regbits-34xx.h"
  37. #define MAX_CLOCK_ENABLE_WAIT 100000
  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. OMAP24XX_PRCM_CLKCFG_CTRL_OFFSET);
  81. /* OCP barrier */
  82. prm_read_mod_reg(OMAP24XX_GR_MOD, OMAP24XX_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. void omap2_init_clk_clkdm(struct clk *clk)
  129. {
  130. struct clockdomain *clkdm;
  131. if (!clk->clkdm_name)
  132. return;
  133. clkdm = clkdm_lookup(clk->clkdm_name);
  134. if (clkdm) {
  135. pr_debug("clock: associated clk %s to clkdm %s\n",
  136. clk->name, clk->clkdm_name);
  137. clk->clkdm = clkdm;
  138. } else {
  139. pr_debug("clock: could not associate clk %s to "
  140. "clkdm %s\n", clk->name, clk->clkdm_name);
  141. }
  142. }
  143. /**
  144. * omap2_init_clksel_parent - set a clksel clk's parent field from the hardware
  145. * @clk: OMAP clock struct ptr to use
  146. *
  147. * Given a pointer to a source-selectable struct clk, read the hardware
  148. * register and determine what its parent is currently set to. Update the
  149. * clk->parent field with the appropriate clk ptr.
  150. */
  151. void omap2_init_clksel_parent(struct clk *clk)
  152. {
  153. const struct clksel *clks;
  154. const struct clksel_rate *clkr;
  155. u32 r, found = 0;
  156. if (!clk->clksel)
  157. return;
  158. r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  159. r >>= __ffs(clk->clksel_mask);
  160. for (clks = clk->clksel; clks->parent && !found; clks++) {
  161. for (clkr = clks->rates; clkr->div && !found; clkr++) {
  162. if ((clkr->flags & cpu_mask) && (clkr->val == r)) {
  163. if (clk->parent != clks->parent) {
  164. pr_debug("clock: inited %s parent "
  165. "to %s (was %s)\n",
  166. clk->name, clks->parent->name,
  167. ((clk->parent) ?
  168. clk->parent->name : "NULL"));
  169. clk_reparent(clk, clks->parent);
  170. };
  171. found = 1;
  172. }
  173. }
  174. }
  175. if (!found)
  176. printk(KERN_ERR "clock: init parent: could not find "
  177. "regval %0x for clock %s\n", r, clk->name);
  178. return;
  179. }
  180. /**
  181. * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
  182. * @clk: struct clk * of a DPLL
  183. *
  184. * DPLLs can be locked or bypassed - basically, enabled or disabled.
  185. * When locked, the DPLL output depends on the M and N values. When
  186. * bypassed, on OMAP2xxx, the output rate is either the 32KiHz clock
  187. * or sys_clk. Bypass rates on OMAP3 depend on the DPLL: DPLLs 1 and
  188. * 2 are bypassed with dpll1_fclk and dpll2_fclk respectively
  189. * (generated by DPLL3), while DPLL 3, 4, and 5 bypass rates are sys_clk.
  190. * Returns the current DPLL CLKOUT rate (*not* CLKOUTX2) if the DPLL is
  191. * locked, or the appropriate bypass rate if the DPLL is bypassed, or 0
  192. * if the clock @clk is not a DPLL.
  193. */
  194. u32 omap2_get_dpll_rate(struct clk *clk)
  195. {
  196. long long dpll_clk;
  197. u32 dpll_mult, dpll_div, v;
  198. struct dpll_data *dd;
  199. dd = clk->dpll_data;
  200. if (!dd)
  201. return 0;
  202. /* Return bypass rate if DPLL is bypassed */
  203. v = __raw_readl(dd->control_reg);
  204. v &= dd->enable_mask;
  205. v >>= __ffs(dd->enable_mask);
  206. if (cpu_is_omap24xx()) {
  207. if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
  208. v == OMAP2XXX_EN_DPLL_FRBYPASS)
  209. return dd->clk_bypass->rate;
  210. } else if (cpu_is_omap34xx()) {
  211. if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
  212. v == OMAP3XXX_EN_DPLL_FRBYPASS)
  213. return dd->clk_bypass->rate;
  214. }
  215. v = __raw_readl(dd->mult_div1_reg);
  216. dpll_mult = v & dd->mult_mask;
  217. dpll_mult >>= __ffs(dd->mult_mask);
  218. dpll_div = v & dd->div1_mask;
  219. dpll_div >>= __ffs(dd->div1_mask);
  220. dpll_clk = (long long)dd->clk_ref->rate * dpll_mult;
  221. do_div(dpll_clk, dpll_div + 1);
  222. return dpll_clk;
  223. }
  224. /*
  225. * Used for clocks that have the same value as the parent clock,
  226. * divided by some factor
  227. */
  228. unsigned long omap2_fixed_divisor_recalc(struct clk *clk)
  229. {
  230. WARN_ON(!clk->fixed_div);
  231. return clk->parent->rate / clk->fixed_div;
  232. }
  233. /**
  234. * omap2_wait_clock_ready - wait for clock to enable
  235. * @reg: physical address of clock IDLEST register
  236. * @mask: value to mask against to determine if the clock is active
  237. * @name: name of the clock (for printk)
  238. *
  239. * Returns 1 if the clock enabled in time, or 0 if it failed to enable
  240. * in roughly MAX_CLOCK_ENABLE_WAIT microseconds.
  241. */
  242. int omap2_wait_clock_ready(void __iomem *reg, u32 mask, const char *name)
  243. {
  244. int i = 0;
  245. int ena = 0;
  246. /*
  247. * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
  248. * 34xx reverses this, just to keep us on our toes
  249. */
  250. if (cpu_mask & (RATE_IN_242X | RATE_IN_243X))
  251. ena = mask;
  252. else if (cpu_mask & RATE_IN_343X)
  253. ena = 0;
  254. /* Wait for lock */
  255. while (((__raw_readl(reg) & mask) != ena) &&
  256. (i++ < MAX_CLOCK_ENABLE_WAIT)) {
  257. udelay(1);
  258. }
  259. if (i < MAX_CLOCK_ENABLE_WAIT)
  260. pr_debug("Clock %s stable after %d loops\n", name, i);
  261. else
  262. printk(KERN_ERR "Clock %s didn't enable in %d tries\n",
  263. name, MAX_CLOCK_ENABLE_WAIT);
  264. return (i < MAX_CLOCK_ENABLE_WAIT) ? 1 : 0;
  265. };
  266. /*
  267. * Note: We don't need special code here for INVERT_ENABLE
  268. * for the time being since INVERT_ENABLE only applies to clocks enabled by
  269. * CM_CLKEN_PLL
  270. */
  271. static void omap2_clk_wait_ready(struct clk *clk)
  272. {
  273. void __iomem *reg, *other_reg, *st_reg;
  274. u32 bit;
  275. /*
  276. * REVISIT: This code is pretty ugly. It would be nice to generalize
  277. * it and pull it into struct clk itself somehow.
  278. */
  279. reg = clk->enable_reg;
  280. /*
  281. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
  282. * it's just a matter of XORing the bits.
  283. */
  284. other_reg = (void __iomem *)((u32)reg ^ (CM_FCLKEN ^ CM_ICLKEN));
  285. /* Check if both functional and interface clocks
  286. * are running. */
  287. bit = 1 << clk->enable_bit;
  288. if (!(__raw_readl(other_reg) & bit))
  289. return;
  290. st_reg = (void __iomem *)(((u32)other_reg & ~0xf0) | 0x20); /* CM_IDLEST* */
  291. omap2_wait_clock_ready(st_reg, bit, clk->name);
  292. }
  293. static int omap2_dflt_clk_enable(struct clk *clk)
  294. {
  295. u32 v;
  296. if (unlikely(clk->enable_reg == NULL)) {
  297. printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
  298. clk->name);
  299. return 0; /* REVISIT: -EINVAL */
  300. }
  301. v = __raw_readl(clk->enable_reg);
  302. if (clk->flags & INVERT_ENABLE)
  303. v &= ~(1 << clk->enable_bit);
  304. else
  305. v |= (1 << clk->enable_bit);
  306. __raw_writel(v, clk->enable_reg);
  307. v = __raw_readl(clk->enable_reg); /* OCP barrier */
  308. return 0;
  309. }
  310. static int omap2_dflt_clk_enable_wait(struct clk *clk)
  311. {
  312. int ret;
  313. if (!clk->enable_reg) {
  314. printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
  315. clk->name);
  316. return 0; /* REVISIT: -EINVAL */
  317. }
  318. ret = omap2_dflt_clk_enable(clk);
  319. if (ret == 0)
  320. omap2_clk_wait_ready(clk);
  321. return ret;
  322. }
  323. static void omap2_dflt_clk_disable(struct clk *clk)
  324. {
  325. u32 v;
  326. if (!clk->enable_reg) {
  327. /*
  328. * 'Independent' here refers to a clock which is not
  329. * controlled by its parent.
  330. */
  331. printk(KERN_ERR "clock: clk_disable called on independent "
  332. "clock %s which has no enable_reg\n", clk->name);
  333. return;
  334. }
  335. v = __raw_readl(clk->enable_reg);
  336. if (clk->flags & INVERT_ENABLE)
  337. v |= (1 << clk->enable_bit);
  338. else
  339. v &= ~(1 << clk->enable_bit);
  340. __raw_writel(v, clk->enable_reg);
  341. /* No OCP barrier needed here since it is a disable operation */
  342. }
  343. const struct clkops clkops_omap2_dflt_wait = {
  344. .enable = omap2_dflt_clk_enable_wait,
  345. .disable = omap2_dflt_clk_disable,
  346. };
  347. const struct clkops clkops_omap2_dflt = {
  348. .enable = omap2_dflt_clk_enable,
  349. .disable = omap2_dflt_clk_disable,
  350. };
  351. /* Enables clock without considering parent dependencies or use count
  352. * REVISIT: Maybe change this to use clk->enable like on omap1?
  353. */
  354. static int _omap2_clk_enable(struct clk *clk)
  355. {
  356. return clk->ops->enable(clk);
  357. }
  358. /* Disables clock without considering parent dependencies or use count */
  359. static void _omap2_clk_disable(struct clk *clk)
  360. {
  361. clk->ops->disable(clk);
  362. }
  363. void omap2_clk_disable(struct clk *clk)
  364. {
  365. if (clk->usecount > 0 && !(--clk->usecount)) {
  366. _omap2_clk_disable(clk);
  367. if (clk->parent)
  368. omap2_clk_disable(clk->parent);
  369. if (clk->clkdm)
  370. omap2_clkdm_clk_disable(clk->clkdm, clk);
  371. }
  372. }
  373. int omap2_clk_enable(struct clk *clk)
  374. {
  375. int ret = 0;
  376. if (clk->usecount++ == 0) {
  377. if (clk->clkdm)
  378. omap2_clkdm_clk_enable(clk->clkdm, clk);
  379. if (clk->parent) {
  380. ret = omap2_clk_enable(clk->parent);
  381. if (ret)
  382. goto err;
  383. }
  384. ret = _omap2_clk_enable(clk);
  385. if (ret) {
  386. if (clk->parent)
  387. omap2_clk_disable(clk->parent);
  388. goto err;
  389. }
  390. }
  391. return ret;
  392. err:
  393. if (clk->clkdm)
  394. omap2_clkdm_clk_disable(clk->clkdm, clk);
  395. clk->usecount--;
  396. return ret;
  397. }
  398. /*
  399. * Used for clocks that are part of CLKSEL_xyz governed clocks.
  400. * REVISIT: Maybe change to use clk->enable() functions like on omap1?
  401. */
  402. unsigned long omap2_clksel_recalc(struct clk *clk)
  403. {
  404. unsigned long rate;
  405. u32 div = 0;
  406. pr_debug("clock: recalc'ing clksel clk %s\n", clk->name);
  407. div = omap2_clksel_get_divisor(clk);
  408. if (div == 0)
  409. return clk->rate;
  410. rate = clk->parent->rate / div;
  411. pr_debug("clock: new clock rate is %ld (div %d)\n", rate, div);
  412. return rate;
  413. }
  414. /**
  415. * omap2_get_clksel_by_parent - return clksel struct for a given clk & parent
  416. * @clk: OMAP struct clk ptr to inspect
  417. * @src_clk: OMAP struct clk ptr of the parent clk to search for
  418. *
  419. * Scan the struct clksel array associated with the clock to find
  420. * the element associated with the supplied parent clock address.
  421. * Returns a pointer to the struct clksel on success or NULL on error.
  422. */
  423. static const struct clksel *omap2_get_clksel_by_parent(struct clk *clk,
  424. struct clk *src_clk)
  425. {
  426. const struct clksel *clks;
  427. if (!clk->clksel)
  428. return NULL;
  429. for (clks = clk->clksel; clks->parent; clks++) {
  430. if (clks->parent == src_clk)
  431. break; /* Found the requested parent */
  432. }
  433. if (!clks->parent) {
  434. printk(KERN_ERR "clock: Could not find parent clock %s in "
  435. "clksel array of clock %s\n", src_clk->name,
  436. clk->name);
  437. return NULL;
  438. }
  439. return clks;
  440. }
  441. /**
  442. * omap2_clksel_round_rate_div - find divisor for the given clock and rate
  443. * @clk: OMAP struct clk to use
  444. * @target_rate: desired clock rate
  445. * @new_div: ptr to where we should store the divisor
  446. *
  447. * Finds 'best' divider value in an array based on the source and target
  448. * rates. The divider array must be sorted with smallest divider first.
  449. * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
  450. * they are only settable as part of virtual_prcm set.
  451. *
  452. * Returns the rounded clock rate or returns 0xffffffff on error.
  453. */
  454. u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
  455. u32 *new_div)
  456. {
  457. unsigned long test_rate;
  458. const struct clksel *clks;
  459. const struct clksel_rate *clkr;
  460. u32 last_div = 0;
  461. printk(KERN_INFO "clock: clksel_round_rate_div: %s target_rate %ld\n",
  462. clk->name, target_rate);
  463. *new_div = 1;
  464. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  465. if (!clks)
  466. return ~0;
  467. for (clkr = clks->rates; clkr->div; clkr++) {
  468. if (!(clkr->flags & cpu_mask))
  469. continue;
  470. /* Sanity check */
  471. if (clkr->div <= last_div)
  472. printk(KERN_ERR "clock: clksel_rate table not sorted "
  473. "for clock %s", clk->name);
  474. last_div = clkr->div;
  475. test_rate = clk->parent->rate / clkr->div;
  476. if (test_rate <= target_rate)
  477. break; /* found it */
  478. }
  479. if (!clkr->div) {
  480. printk(KERN_ERR "clock: Could not find divisor for target "
  481. "rate %ld for clock %s parent %s\n", target_rate,
  482. clk->name, clk->parent->name);
  483. return ~0;
  484. }
  485. *new_div = clkr->div;
  486. printk(KERN_INFO "clock: new_div = %d, new_rate = %ld\n", *new_div,
  487. (clk->parent->rate / clkr->div));
  488. return (clk->parent->rate / clkr->div);
  489. }
  490. /**
  491. * omap2_clksel_round_rate - find rounded rate for the given clock and rate
  492. * @clk: OMAP struct clk to use
  493. * @target_rate: desired clock rate
  494. *
  495. * Compatibility wrapper for OMAP clock framework
  496. * Finds best target rate based on the source clock and possible dividers.
  497. * rates. The divider array must be sorted with smallest divider first.
  498. * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
  499. * they are only settable as part of virtual_prcm set.
  500. *
  501. * Returns the rounded clock rate or returns 0xffffffff on error.
  502. */
  503. long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
  504. {
  505. u32 new_div;
  506. return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
  507. }
  508. /* Given a clock and a rate apply a clock specific rounding function */
  509. long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
  510. {
  511. if (clk->round_rate)
  512. return clk->round_rate(clk, rate);
  513. if (clk->flags & RATE_FIXED)
  514. printk(KERN_ERR "clock: generic omap2_clk_round_rate called "
  515. "on fixed-rate clock %s\n", clk->name);
  516. return clk->rate;
  517. }
  518. /**
  519. * omap2_clksel_to_divisor() - turn clksel field value into integer divider
  520. * @clk: OMAP struct clk to use
  521. * @field_val: register field value to find
  522. *
  523. * Given a struct clk of a rate-selectable clksel clock, and a register field
  524. * value to search for, find the corresponding clock divisor. The register
  525. * field value should be pre-masked and shifted down so the LSB is at bit 0
  526. * before calling. Returns 0 on error
  527. */
  528. u32 omap2_clksel_to_divisor(struct clk *clk, u32 field_val)
  529. {
  530. const struct clksel *clks;
  531. const struct clksel_rate *clkr;
  532. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  533. if (!clks)
  534. return 0;
  535. for (clkr = clks->rates; clkr->div; clkr++) {
  536. if ((clkr->flags & cpu_mask) && (clkr->val == field_val))
  537. break;
  538. }
  539. if (!clkr->div) {
  540. printk(KERN_ERR "clock: Could not find fieldval %d for "
  541. "clock %s parent %s\n", field_val, clk->name,
  542. clk->parent->name);
  543. return 0;
  544. }
  545. return clkr->div;
  546. }
  547. /**
  548. * omap2_divisor_to_clksel() - turn clksel integer divisor into a field value
  549. * @clk: OMAP struct clk to use
  550. * @div: integer divisor to search for
  551. *
  552. * Given a struct clk of a rate-selectable clksel clock, and a clock divisor,
  553. * find the corresponding register field value. The return register value is
  554. * the value before left-shifting. Returns ~0 on error
  555. */
  556. u32 omap2_divisor_to_clksel(struct clk *clk, u32 div)
  557. {
  558. const struct clksel *clks;
  559. const struct clksel_rate *clkr;
  560. /* should never happen */
  561. WARN_ON(div == 0);
  562. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  563. if (!clks)
  564. return ~0;
  565. for (clkr = clks->rates; clkr->div; clkr++) {
  566. if ((clkr->flags & cpu_mask) && (clkr->div == div))
  567. break;
  568. }
  569. if (!clkr->div) {
  570. printk(KERN_ERR "clock: Could not find divisor %d for "
  571. "clock %s parent %s\n", div, clk->name,
  572. clk->parent->name);
  573. return ~0;
  574. }
  575. return clkr->val;
  576. }
  577. /**
  578. * omap2_clksel_get_divisor - get current divider applied to parent clock.
  579. * @clk: OMAP struct clk to use.
  580. *
  581. * Returns the integer divisor upon success or 0 on error.
  582. */
  583. u32 omap2_clksel_get_divisor(struct clk *clk)
  584. {
  585. u32 v;
  586. if (!clk->clksel_mask)
  587. return 0;
  588. v = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  589. v >>= __ffs(clk->clksel_mask);
  590. return omap2_clksel_to_divisor(clk, v);
  591. }
  592. int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
  593. {
  594. u32 v, field_val, validrate, new_div = 0;
  595. if (!clk->clksel_mask)
  596. return -EINVAL;
  597. validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
  598. if (validrate != rate)
  599. return -EINVAL;
  600. field_val = omap2_divisor_to_clksel(clk, new_div);
  601. if (field_val == ~0)
  602. return -EINVAL;
  603. v = __raw_readl(clk->clksel_reg);
  604. v &= ~clk->clksel_mask;
  605. v |= field_val << __ffs(clk->clksel_mask);
  606. __raw_writel(v, clk->clksel_reg);
  607. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  608. clk->rate = clk->parent->rate / new_div;
  609. _omap2xxx_clk_commit(clk);
  610. return 0;
  611. }
  612. /* Set the clock rate for a clock source */
  613. int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
  614. {
  615. int ret = -EINVAL;
  616. pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
  617. /* CONFIG_PARTICIPANT clocks are changed only in sets via the
  618. rate table mechanism, driven by mpu_speed */
  619. if (clk->flags & CONFIG_PARTICIPANT)
  620. return -EINVAL;
  621. /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
  622. if (clk->set_rate)
  623. ret = clk->set_rate(clk, rate);
  624. return ret;
  625. }
  626. /*
  627. * Converts encoded control register address into a full address
  628. * On error, the return value (parent_div) will be 0.
  629. */
  630. static u32 _omap2_clksel_get_src_field(struct clk *src_clk, struct clk *clk,
  631. u32 *field_val)
  632. {
  633. const struct clksel *clks;
  634. const struct clksel_rate *clkr;
  635. clks = omap2_get_clksel_by_parent(clk, src_clk);
  636. if (!clks)
  637. return 0;
  638. for (clkr = clks->rates; clkr->div; clkr++) {
  639. if (clkr->flags & cpu_mask && clkr->flags & DEFAULT_RATE)
  640. break; /* Found the default rate for this platform */
  641. }
  642. if (!clkr->div) {
  643. printk(KERN_ERR "clock: Could not find default rate for "
  644. "clock %s parent %s\n", clk->name,
  645. src_clk->parent->name);
  646. return 0;
  647. }
  648. /* Should never happen. Add a clksel mask to the struct clk. */
  649. WARN_ON(clk->clksel_mask == 0);
  650. *field_val = clkr->val;
  651. return clkr->div;
  652. }
  653. int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
  654. {
  655. u32 field_val, v, parent_div;
  656. if (clk->flags & CONFIG_PARTICIPANT)
  657. return -EINVAL;
  658. if (!clk->clksel)
  659. return -EINVAL;
  660. parent_div = _omap2_clksel_get_src_field(new_parent, clk, &field_val);
  661. if (!parent_div)
  662. return -EINVAL;
  663. /* Set new source value (previous dividers if any in effect) */
  664. v = __raw_readl(clk->clksel_reg);
  665. v &= ~clk->clksel_mask;
  666. v |= field_val << __ffs(clk->clksel_mask);
  667. __raw_writel(v, clk->clksel_reg);
  668. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  669. _omap2xxx_clk_commit(clk);
  670. clk_reparent(clk, new_parent);
  671. /* CLKSEL clocks follow their parents' rates, divided by a divisor */
  672. clk->rate = new_parent->rate;
  673. if (parent_div > 0)
  674. clk->rate /= parent_div;
  675. pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
  676. clk->name, clk->parent->name, clk->rate);
  677. return 0;
  678. }
  679. /* DPLL rate rounding code */
  680. /**
  681. * omap2_dpll_set_rate_tolerance: set the error tolerance during rate rounding
  682. * @clk: struct clk * of the DPLL
  683. * @tolerance: maximum rate error tolerance
  684. *
  685. * Set the maximum DPLL rate error tolerance for the rate rounding
  686. * algorithm. The rate tolerance is an attempt to balance DPLL power
  687. * saving (the least divider value "n") vs. rate fidelity (the least
  688. * difference between the desired DPLL target rate and the rounded
  689. * rate out of the algorithm). So, increasing the tolerance is likely
  690. * to decrease DPLL power consumption and increase DPLL rate error.
  691. * Returns -EINVAL if provided a null clock ptr or a clk that is not a
  692. * DPLL; or 0 upon success.
  693. */
  694. int omap2_dpll_set_rate_tolerance(struct clk *clk, unsigned int tolerance)
  695. {
  696. if (!clk || !clk->dpll_data)
  697. return -EINVAL;
  698. clk->dpll_data->rate_tolerance = tolerance;
  699. return 0;
  700. }
  701. static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
  702. unsigned int m, unsigned int n)
  703. {
  704. unsigned long long num;
  705. num = (unsigned long long)parent_rate * m;
  706. do_div(num, n);
  707. return num;
  708. }
  709. /*
  710. * _dpll_test_mult - test a DPLL multiplier value
  711. * @m: pointer to the DPLL m (multiplier) value under test
  712. * @n: current DPLL n (divider) value under test
  713. * @new_rate: pointer to storage for the resulting rounded rate
  714. * @target_rate: the desired DPLL rate
  715. * @parent_rate: the DPLL's parent clock rate
  716. *
  717. * This code tests a DPLL multiplier value, ensuring that the
  718. * resulting rate will not be higher than the target_rate, and that
  719. * the multiplier value itself is valid for the DPLL. Initially, the
  720. * integer pointed to by the m argument should be prescaled by
  721. * multiplying by DPLL_SCALE_FACTOR. The code will replace this with
  722. * a non-scaled m upon return. This non-scaled m will result in a
  723. * new_rate as close as possible to target_rate (but not greater than
  724. * target_rate) given the current (parent_rate, n, prescaled m)
  725. * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
  726. * non-scaled m attempted to underflow, which can allow the calling
  727. * function to bail out early; or 0 upon success.
  728. */
  729. static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
  730. unsigned long target_rate,
  731. unsigned long parent_rate)
  732. {
  733. int r = 0, carry = 0;
  734. /* Unscale m and round if necessary */
  735. if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
  736. carry = 1;
  737. *m = (*m / DPLL_SCALE_FACTOR) + carry;
  738. /*
  739. * The new rate must be <= the target rate to avoid programming
  740. * a rate that is impossible for the hardware to handle
  741. */
  742. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  743. if (*new_rate > target_rate) {
  744. (*m)--;
  745. *new_rate = 0;
  746. }
  747. /* Guard against m underflow */
  748. if (*m < DPLL_MIN_MULTIPLIER) {
  749. *m = DPLL_MIN_MULTIPLIER;
  750. *new_rate = 0;
  751. r = DPLL_MULT_UNDERFLOW;
  752. }
  753. if (*new_rate == 0)
  754. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  755. return r;
  756. }
  757. /**
  758. * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
  759. * @clk: struct clk * for a DPLL
  760. * @target_rate: desired DPLL clock rate
  761. *
  762. * Given a DPLL, a desired target rate, and a rate tolerance, round
  763. * the target rate to a possible, programmable rate for this DPLL.
  764. * Rate tolerance is assumed to be set by the caller before this
  765. * function is called. Attempts to select the minimum possible n
  766. * within the tolerance to reduce power consumption. Stores the
  767. * computed (m, n) in the DPLL's dpll_data structure so set_rate()
  768. * will not need to call this (expensive) function again. Returns ~0
  769. * if the target rate cannot be rounded, either because the rate is
  770. * too low or because the rate tolerance is set too tightly; or the
  771. * rounded rate upon success.
  772. */
  773. long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
  774. {
  775. int m, n, r, e, scaled_max_m;
  776. unsigned long scaled_rt_rp, new_rate;
  777. int min_e = -1, min_e_m = -1, min_e_n = -1;
  778. struct dpll_data *dd;
  779. if (!clk || !clk->dpll_data)
  780. return ~0;
  781. dd = clk->dpll_data;
  782. pr_debug("clock: starting DPLL round_rate for clock %s, target rate "
  783. "%ld\n", clk->name, target_rate);
  784. scaled_rt_rp = target_rate / (dd->clk_ref->rate / DPLL_SCALE_FACTOR);
  785. scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
  786. dd->last_rounded_rate = 0;
  787. for (n = dd->min_divider; n <= dd->max_divider; n++) {
  788. /* Is the (input clk, divider) pair valid for the DPLL? */
  789. r = _dpll_test_fint(clk, n);
  790. if (r == DPLL_FINT_UNDERFLOW)
  791. break;
  792. else if (r == DPLL_FINT_INVALID)
  793. continue;
  794. /* Compute the scaled DPLL multiplier, based on the divider */
  795. m = scaled_rt_rp * n;
  796. /*
  797. * Since we're counting n up, a m overflow means we
  798. * can bail out completely (since as n increases in
  799. * the next iteration, there's no way that m can
  800. * increase beyond the current m)
  801. */
  802. if (m > scaled_max_m)
  803. break;
  804. r = _dpll_test_mult(&m, n, &new_rate, target_rate,
  805. dd->clk_ref->rate);
  806. /* m can't be set low enough for this n - try with a larger n */
  807. if (r == DPLL_MULT_UNDERFLOW)
  808. continue;
  809. e = target_rate - new_rate;
  810. pr_debug("clock: n = %d: m = %d: rate error is %d "
  811. "(new_rate = %ld)\n", n, m, e, new_rate);
  812. if (min_e == -1 ||
  813. min_e >= (int)(abs(e) - dd->rate_tolerance)) {
  814. min_e = e;
  815. min_e_m = m;
  816. min_e_n = n;
  817. pr_debug("clock: found new least error %d\n", min_e);
  818. /* We found good settings -- bail out now */
  819. if (min_e <= dd->rate_tolerance)
  820. break;
  821. }
  822. }
  823. if (min_e < 0) {
  824. pr_debug("clock: error: target rate or tolerance too low\n");
  825. return ~0;
  826. }
  827. dd->last_rounded_m = min_e_m;
  828. dd->last_rounded_n = min_e_n;
  829. dd->last_rounded_rate = _dpll_compute_new_rate(dd->clk_ref->rate,
  830. min_e_m, min_e_n);
  831. pr_debug("clock: final least error: e = %d, m = %d, n = %d\n",
  832. min_e, min_e_m, min_e_n);
  833. pr_debug("clock: final rate: %ld (target rate: %ld)\n",
  834. dd->last_rounded_rate, target_rate);
  835. return dd->last_rounded_rate;
  836. }
  837. /*-------------------------------------------------------------------------
  838. * Omap2 clock reset and init functions
  839. *-------------------------------------------------------------------------*/
  840. #ifdef CONFIG_OMAP_RESET_CLOCKS
  841. void omap2_clk_disable_unused(struct clk *clk)
  842. {
  843. u32 regval32, v;
  844. v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
  845. regval32 = __raw_readl(clk->enable_reg);
  846. if ((regval32 & (1 << clk->enable_bit)) == v)
  847. return;
  848. printk(KERN_INFO "Disabling unused clock \"%s\"\n", clk->name);
  849. if (cpu_is_omap34xx()) {
  850. omap2_clk_enable(clk);
  851. omap2_clk_disable(clk);
  852. } else
  853. _omap2_clk_disable(clk);
  854. }
  855. #endif