clock.c 27 KB

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