clock.c 26 KB

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