clock.c 26 KB

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