clock.c 25 KB

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