clock.c 26 KB

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