clock.c 26 KB

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