clock.c 26 KB

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