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 != 0) {
  355. clk->usecount--;
  356. return ret;
  357. }
  358. if (clk->clkdm)
  359. omap2_clkdm_clk_enable(clk->clkdm, clk);
  360. ret = _omap2_clk_enable(clk);
  361. if (ret != 0) {
  362. if (clk->clkdm)
  363. omap2_clkdm_clk_disable(clk->clkdm, clk);
  364. if (clk->parent) {
  365. omap2_clk_disable(clk->parent);
  366. clk->usecount--;
  367. }
  368. }
  369. }
  370. return ret;
  371. }
  372. /*
  373. * Used for clocks that are part of CLKSEL_xyz governed clocks.
  374. * REVISIT: Maybe change to use clk->enable() functions like on omap1?
  375. */
  376. void omap2_clksel_recalc(struct clk *clk)
  377. {
  378. u32 div = 0;
  379. pr_debug("clock: recalc'ing clksel clk %s\n", clk->name);
  380. div = omap2_clksel_get_divisor(clk);
  381. if (div == 0)
  382. return;
  383. if (clk->rate == (clk->parent->rate / div))
  384. return;
  385. clk->rate = clk->parent->rate / div;
  386. pr_debug("clock: new clock rate is %ld (div %d)\n", clk->rate, div);
  387. }
  388. /**
  389. * omap2_get_clksel_by_parent - return clksel struct for a given clk & parent
  390. * @clk: OMAP struct clk ptr to inspect
  391. * @src_clk: OMAP struct clk ptr of the parent clk to search for
  392. *
  393. * Scan the struct clksel array associated with the clock to find
  394. * the element associated with the supplied parent clock address.
  395. * Returns a pointer to the struct clksel on success or NULL on error.
  396. */
  397. static const struct clksel *omap2_get_clksel_by_parent(struct clk *clk,
  398. struct clk *src_clk)
  399. {
  400. const struct clksel *clks;
  401. if (!clk->clksel)
  402. return NULL;
  403. for (clks = clk->clksel; clks->parent; clks++) {
  404. if (clks->parent == src_clk)
  405. break; /* Found the requested parent */
  406. }
  407. if (!clks->parent) {
  408. printk(KERN_ERR "clock: Could not find parent clock %s in "
  409. "clksel array of clock %s\n", src_clk->name,
  410. clk->name);
  411. return NULL;
  412. }
  413. return clks;
  414. }
  415. /**
  416. * omap2_clksel_round_rate_div - find divisor for the given clock and rate
  417. * @clk: OMAP struct clk to use
  418. * @target_rate: desired clock rate
  419. * @new_div: ptr to where we should store the divisor
  420. *
  421. * Finds 'best' divider value in an array based on the source and target
  422. * rates. The divider array must be sorted with smallest divider first.
  423. * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
  424. * they are only settable as part of virtual_prcm set.
  425. *
  426. * Returns the rounded clock rate or returns 0xffffffff on error.
  427. */
  428. u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
  429. u32 *new_div)
  430. {
  431. unsigned long test_rate;
  432. const struct clksel *clks;
  433. const struct clksel_rate *clkr;
  434. u32 last_div = 0;
  435. printk(KERN_INFO "clock: clksel_round_rate_div: %s target_rate %ld\n",
  436. clk->name, target_rate);
  437. *new_div = 1;
  438. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  439. if (!clks)
  440. return ~0;
  441. for (clkr = clks->rates; clkr->div; clkr++) {
  442. if (!(clkr->flags & cpu_mask))
  443. continue;
  444. /* Sanity check */
  445. if (clkr->div <= last_div)
  446. printk(KERN_ERR "clock: clksel_rate table not sorted "
  447. "for clock %s", clk->name);
  448. last_div = clkr->div;
  449. test_rate = clk->parent->rate / clkr->div;
  450. if (test_rate <= target_rate)
  451. break; /* found it */
  452. }
  453. if (!clkr->div) {
  454. printk(KERN_ERR "clock: Could not find divisor for target "
  455. "rate %ld for clock %s parent %s\n", target_rate,
  456. clk->name, clk->parent->name);
  457. return ~0;
  458. }
  459. *new_div = clkr->div;
  460. printk(KERN_INFO "clock: new_div = %d, new_rate = %ld\n", *new_div,
  461. (clk->parent->rate / clkr->div));
  462. return (clk->parent->rate / clkr->div);
  463. }
  464. /**
  465. * omap2_clksel_round_rate - find rounded rate for the given clock and rate
  466. * @clk: OMAP struct clk to use
  467. * @target_rate: desired clock rate
  468. *
  469. * Compatibility wrapper for OMAP clock framework
  470. * Finds best target rate based on the source clock and possible dividers.
  471. * rates. The divider array must be sorted with smallest divider first.
  472. * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
  473. * they are only settable as part of virtual_prcm set.
  474. *
  475. * Returns the rounded clock rate or returns 0xffffffff on error.
  476. */
  477. long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
  478. {
  479. u32 new_div;
  480. return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
  481. }
  482. /* Given a clock and a rate apply a clock specific rounding function */
  483. long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
  484. {
  485. if (clk->round_rate)
  486. return clk->round_rate(clk, rate);
  487. if (clk->flags & RATE_FIXED)
  488. printk(KERN_ERR "clock: generic omap2_clk_round_rate called "
  489. "on fixed-rate clock %s\n", clk->name);
  490. return clk->rate;
  491. }
  492. /**
  493. * omap2_clksel_to_divisor() - turn clksel field value into integer divider
  494. * @clk: OMAP struct clk to use
  495. * @field_val: register field value to find
  496. *
  497. * Given a struct clk of a rate-selectable clksel clock, and a register field
  498. * value to search for, find the corresponding clock divisor. The register
  499. * field value should be pre-masked and shifted down so the LSB is at bit 0
  500. * before calling. Returns 0 on error
  501. */
  502. u32 omap2_clksel_to_divisor(struct clk *clk, u32 field_val)
  503. {
  504. const struct clksel *clks;
  505. const struct clksel_rate *clkr;
  506. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  507. if (!clks)
  508. return 0;
  509. for (clkr = clks->rates; clkr->div; clkr++) {
  510. if ((clkr->flags & cpu_mask) && (clkr->val == field_val))
  511. break;
  512. }
  513. if (!clkr->div) {
  514. printk(KERN_ERR "clock: Could not find fieldval %d for "
  515. "clock %s parent %s\n", field_val, clk->name,
  516. clk->parent->name);
  517. return 0;
  518. }
  519. return clkr->div;
  520. }
  521. /**
  522. * omap2_divisor_to_clksel() - turn clksel integer divisor into a field value
  523. * @clk: OMAP struct clk to use
  524. * @div: integer divisor to search for
  525. *
  526. * Given a struct clk of a rate-selectable clksel clock, and a clock divisor,
  527. * find the corresponding register field value. The return register value is
  528. * the value before left-shifting. Returns 0xffffffff on error
  529. */
  530. u32 omap2_divisor_to_clksel(struct clk *clk, u32 div)
  531. {
  532. const struct clksel *clks;
  533. const struct clksel_rate *clkr;
  534. /* should never happen */
  535. WARN_ON(div == 0);
  536. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  537. if (!clks)
  538. return 0;
  539. for (clkr = clks->rates; clkr->div; clkr++) {
  540. if ((clkr->flags & cpu_mask) && (clkr->div == div))
  541. break;
  542. }
  543. if (!clkr->div) {
  544. printk(KERN_ERR "clock: Could not find divisor %d for "
  545. "clock %s parent %s\n", div, clk->name,
  546. clk->parent->name);
  547. return 0;
  548. }
  549. return clkr->val;
  550. }
  551. /**
  552. * omap2_clksel_get_divisor - get current divider applied to parent clock.
  553. * @clk: OMAP struct clk to use.
  554. *
  555. * Returns the integer divisor upon success or 0 on error.
  556. */
  557. u32 omap2_clksel_get_divisor(struct clk *clk)
  558. {
  559. u32 v;
  560. if (!clk->clksel_mask)
  561. return 0;
  562. v = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  563. v >>= __ffs(clk->clksel_mask);
  564. return omap2_clksel_to_divisor(clk, v);
  565. }
  566. int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
  567. {
  568. u32 v, field_val, validrate, new_div = 0;
  569. if (!clk->clksel_mask)
  570. return -EINVAL;
  571. validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
  572. if (validrate != rate)
  573. return -EINVAL;
  574. field_val = omap2_divisor_to_clksel(clk, new_div);
  575. if (field_val == ~0)
  576. return -EINVAL;
  577. v = __raw_readl(clk->clksel_reg);
  578. v &= ~clk->clksel_mask;
  579. v |= field_val << __ffs(clk->clksel_mask);
  580. __raw_writel(v, clk->clksel_reg);
  581. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  582. clk->rate = clk->parent->rate / new_div;
  583. _omap2xxx_clk_commit(clk);
  584. return 0;
  585. }
  586. /* Set the clock rate for a clock source */
  587. int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
  588. {
  589. int ret = -EINVAL;
  590. pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
  591. /* CONFIG_PARTICIPANT clocks are changed only in sets via the
  592. rate table mechanism, driven by mpu_speed */
  593. if (clk->flags & CONFIG_PARTICIPANT)
  594. return -EINVAL;
  595. /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
  596. if (clk->set_rate)
  597. ret = clk->set_rate(clk, rate);
  598. return ret;
  599. }
  600. /*
  601. * Converts encoded control register address into a full address
  602. * On error, the return value (parent_div) will be 0.
  603. */
  604. static u32 _omap2_clksel_get_src_field(struct clk *src_clk, struct clk *clk,
  605. u32 *field_val)
  606. {
  607. const struct clksel *clks;
  608. const struct clksel_rate *clkr;
  609. clks = omap2_get_clksel_by_parent(clk, src_clk);
  610. if (!clks)
  611. return 0;
  612. for (clkr = clks->rates; clkr->div; clkr++) {
  613. if (clkr->flags & (cpu_mask | DEFAULT_RATE))
  614. break; /* Found the default rate for this platform */
  615. }
  616. if (!clkr->div) {
  617. printk(KERN_ERR "clock: Could not find default rate for "
  618. "clock %s parent %s\n", clk->name,
  619. src_clk->parent->name);
  620. return 0;
  621. }
  622. /* Should never happen. Add a clksel mask to the struct clk. */
  623. WARN_ON(clk->clksel_mask == 0);
  624. *field_val = clkr->val;
  625. return clkr->div;
  626. }
  627. int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
  628. {
  629. u32 field_val, v, parent_div;
  630. if (clk->flags & CONFIG_PARTICIPANT)
  631. return -EINVAL;
  632. if (!clk->clksel)
  633. return -EINVAL;
  634. parent_div = _omap2_clksel_get_src_field(new_parent, clk, &field_val);
  635. if (!parent_div)
  636. return -EINVAL;
  637. if (clk->usecount > 0)
  638. _omap2_clk_disable(clk);
  639. /* Set new source value (previous dividers if any in effect) */
  640. v = __raw_readl(clk->clksel_reg);
  641. v &= ~clk->clksel_mask;
  642. v |= field_val << __ffs(clk->clksel_mask);
  643. __raw_writel(v, clk->clksel_reg);
  644. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  645. _omap2xxx_clk_commit(clk);
  646. if (clk->usecount > 0)
  647. _omap2_clk_enable(clk);
  648. clk_reparent(clk, new_parent);
  649. /* CLKSEL clocks follow their parents' rates, divided by a divisor */
  650. clk->rate = new_parent->rate;
  651. if (parent_div > 0)
  652. clk->rate /= parent_div;
  653. pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
  654. clk->name, clk->parent->name, clk->rate);
  655. return 0;
  656. }
  657. /* DPLL rate rounding code */
  658. /**
  659. * omap2_dpll_set_rate_tolerance: set the error tolerance during rate rounding
  660. * @clk: struct clk * of the DPLL
  661. * @tolerance: maximum rate error tolerance
  662. *
  663. * Set the maximum DPLL rate error tolerance for the rate rounding
  664. * algorithm. The rate tolerance is an attempt to balance DPLL power
  665. * saving (the least divider value "n") vs. rate fidelity (the least
  666. * difference between the desired DPLL target rate and the rounded
  667. * rate out of the algorithm). So, increasing the tolerance is likely
  668. * to decrease DPLL power consumption and increase DPLL rate error.
  669. * Returns -EINVAL if provided a null clock ptr or a clk that is not a
  670. * DPLL; or 0 upon success.
  671. */
  672. int omap2_dpll_set_rate_tolerance(struct clk *clk, unsigned int tolerance)
  673. {
  674. if (!clk || !clk->dpll_data)
  675. return -EINVAL;
  676. clk->dpll_data->rate_tolerance = tolerance;
  677. return 0;
  678. }
  679. static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
  680. unsigned int m, unsigned int n)
  681. {
  682. unsigned long long num;
  683. num = (unsigned long long)parent_rate * m;
  684. do_div(num, n);
  685. return num;
  686. }
  687. /*
  688. * _dpll_test_mult - test a DPLL multiplier value
  689. * @m: pointer to the DPLL m (multiplier) value under test
  690. * @n: current DPLL n (divider) value under test
  691. * @new_rate: pointer to storage for the resulting rounded rate
  692. * @target_rate: the desired DPLL rate
  693. * @parent_rate: the DPLL's parent clock rate
  694. *
  695. * This code tests a DPLL multiplier value, ensuring that the
  696. * resulting rate will not be higher than the target_rate, and that
  697. * the multiplier value itself is valid for the DPLL. Initially, the
  698. * integer pointed to by the m argument should be prescaled by
  699. * multiplying by DPLL_SCALE_FACTOR. The code will replace this with
  700. * a non-scaled m upon return. This non-scaled m will result in a
  701. * new_rate as close as possible to target_rate (but not greater than
  702. * target_rate) given the current (parent_rate, n, prescaled m)
  703. * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
  704. * non-scaled m attempted to underflow, which can allow the calling
  705. * function to bail out early; or 0 upon success.
  706. */
  707. static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
  708. unsigned long target_rate,
  709. unsigned long parent_rate)
  710. {
  711. int r = 0, carry = 0;
  712. /* Unscale m and round if necessary */
  713. if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
  714. carry = 1;
  715. *m = (*m / DPLL_SCALE_FACTOR) + carry;
  716. /*
  717. * The new rate must be <= the target rate to avoid programming
  718. * a rate that is impossible for the hardware to handle
  719. */
  720. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  721. if (*new_rate > target_rate) {
  722. (*m)--;
  723. *new_rate = 0;
  724. }
  725. /* Guard against m underflow */
  726. if (*m < DPLL_MIN_MULTIPLIER) {
  727. *m = DPLL_MIN_MULTIPLIER;
  728. *new_rate = 0;
  729. r = DPLL_MULT_UNDERFLOW;
  730. }
  731. if (*new_rate == 0)
  732. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  733. return r;
  734. }
  735. /**
  736. * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
  737. * @clk: struct clk * for a DPLL
  738. * @target_rate: desired DPLL clock rate
  739. *
  740. * Given a DPLL, a desired target rate, and a rate tolerance, round
  741. * the target rate to a possible, programmable rate for this DPLL.
  742. * Rate tolerance is assumed to be set by the caller before this
  743. * function is called. Attempts to select the minimum possible n
  744. * within the tolerance to reduce power consumption. Stores the
  745. * computed (m, n) in the DPLL's dpll_data structure so set_rate()
  746. * will not need to call this (expensive) function again. Returns ~0
  747. * if the target rate cannot be rounded, either because the rate is
  748. * too low or because the rate tolerance is set too tightly; or the
  749. * rounded rate upon success.
  750. */
  751. long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
  752. {
  753. int m, n, r, e, scaled_max_m;
  754. unsigned long scaled_rt_rp, new_rate;
  755. int min_e = -1, min_e_m = -1, min_e_n = -1;
  756. struct dpll_data *dd;
  757. if (!clk || !clk->dpll_data)
  758. return ~0;
  759. dd = clk->dpll_data;
  760. pr_debug("clock: starting DPLL round_rate for clock %s, target rate "
  761. "%ld\n", clk->name, target_rate);
  762. scaled_rt_rp = target_rate / (clk->parent->rate / DPLL_SCALE_FACTOR);
  763. scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
  764. dd->last_rounded_rate = 0;
  765. for (n = dd->min_divider; n <= dd->max_divider; n++) {
  766. /* Is the (input clk, divider) pair valid for the DPLL? */
  767. r = _dpll_test_fint(clk, n);
  768. if (r == DPLL_FINT_UNDERFLOW)
  769. break;
  770. else if (r == DPLL_FINT_INVALID)
  771. continue;
  772. /* Compute the scaled DPLL multiplier, based on the divider */
  773. m = scaled_rt_rp * n;
  774. /*
  775. * Since we're counting n up, a m overflow means we
  776. * can bail out completely (since as n increases in
  777. * the next iteration, there's no way that m can
  778. * increase beyond the current m)
  779. */
  780. if (m > scaled_max_m)
  781. break;
  782. r = _dpll_test_mult(&m, n, &new_rate, target_rate,
  783. clk->parent->rate);
  784. /* m can't be set low enough for this n - try with a larger n */
  785. if (r == DPLL_MULT_UNDERFLOW)
  786. continue;
  787. e = target_rate - new_rate;
  788. pr_debug("clock: n = %d: m = %d: rate error is %d "
  789. "(new_rate = %ld)\n", n, m, e, new_rate);
  790. if (min_e == -1 ||
  791. min_e >= (int)(abs(e) - dd->rate_tolerance)) {
  792. min_e = e;
  793. min_e_m = m;
  794. min_e_n = n;
  795. pr_debug("clock: found new least error %d\n", min_e);
  796. /* We found good settings -- bail out now */
  797. if (min_e <= dd->rate_tolerance)
  798. break;
  799. }
  800. }
  801. if (min_e < 0) {
  802. pr_debug("clock: error: target rate or tolerance too low\n");
  803. return ~0;
  804. }
  805. dd->last_rounded_m = min_e_m;
  806. dd->last_rounded_n = min_e_n;
  807. dd->last_rounded_rate = _dpll_compute_new_rate(clk->parent->rate,
  808. min_e_m, min_e_n);
  809. pr_debug("clock: final least error: e = %d, m = %d, n = %d\n",
  810. min_e, min_e_m, min_e_n);
  811. pr_debug("clock: final rate: %ld (target rate: %ld)\n",
  812. dd->last_rounded_rate, target_rate);
  813. return dd->last_rounded_rate;
  814. }
  815. /*-------------------------------------------------------------------------
  816. * Omap2 clock reset and init functions
  817. *-------------------------------------------------------------------------*/
  818. #ifdef CONFIG_OMAP_RESET_CLOCKS
  819. void omap2_clk_disable_unused(struct clk *clk)
  820. {
  821. u32 regval32, v;
  822. v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
  823. regval32 = __raw_readl(clk->enable_reg);
  824. if ((regval32 & (1 << clk->enable_bit)) == v)
  825. return;
  826. printk(KERN_INFO "Disabling unused clock \"%s\"\n", clk->name);
  827. if (cpu_is_omap34xx()) {
  828. omap2_clk_enable(clk);
  829. omap2_clk_disable(clk);
  830. } else
  831. _omap2_clk_disable(clk);
  832. }
  833. #endif