clock.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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/cpu.h>
  28. #include <mach/prcm.h>
  29. #include <asm/div64.h>
  30. #include <mach/sdrc.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. /* 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
  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. /* DPLL valid Fint frequency band limits - from 34xx TRM Section 4.7.6.2 */
  54. #define DPLL_FINT_BAND1_MIN 750000
  55. #define DPLL_FINT_BAND1_MAX 2100000
  56. #define DPLL_FINT_BAND2_MIN 7500000
  57. #define DPLL_FINT_BAND2_MAX 21000000
  58. /* _dpll_test_fint() return codes */
  59. #define DPLL_FINT_UNDERFLOW -1
  60. #define DPLL_FINT_INVALID -2
  61. u8 cpu_mask;
  62. /*-------------------------------------------------------------------------
  63. * OMAP2/3 specific clock functions
  64. *-------------------------------------------------------------------------*/
  65. /**
  66. * _omap2xxx_clk_commit - commit clock parent/rate changes in hardware
  67. * @clk: struct clk *
  68. *
  69. * If @clk has the DELAYED_APP flag set, meaning that parent/rate changes
  70. * don't take effect until the VALID_CONFIG bit is written, write the
  71. * VALID_CONFIG bit and wait for the write to complete. No return value.
  72. */
  73. static void _omap2xxx_clk_commit(struct clk *clk)
  74. {
  75. if (!cpu_is_omap24xx())
  76. return;
  77. if (!(clk->flags & DELAYED_APP))
  78. return;
  79. prm_write_mod_reg(OMAP24XX_VALID_CONFIG, OMAP24XX_GR_MOD,
  80. OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
  81. /* OCP barrier */
  82. prm_read_mod_reg(OMAP24XX_GR_MOD, OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
  83. }
  84. /*
  85. * _dpll_test_fint - test whether an Fint value is valid for the DPLL
  86. * @clk: DPLL struct clk to test
  87. * @n: divider value (N) to test
  88. *
  89. * Tests whether a particular divider @n will result in a valid DPLL
  90. * internal clock frequency Fint. See the 34xx TRM 4.7.6.2 "DPLL Jitter
  91. * Correction". Returns 0 if OK, -1 if the enclosing loop can terminate
  92. * (assuming that it is counting N upwards), or -2 if the enclosing loop
  93. * should skip to the next iteration (again assuming N is increasing).
  94. */
  95. static int _dpll_test_fint(struct clk *clk, u8 n)
  96. {
  97. struct dpll_data *dd;
  98. long fint;
  99. int ret = 0;
  100. dd = clk->dpll_data;
  101. /* DPLL divider must result in a valid jitter correction val */
  102. fint = clk->parent->rate / (n + 1);
  103. if (fint < DPLL_FINT_BAND1_MIN) {
  104. pr_debug("rejecting n=%d due to Fint failure, "
  105. "lowering max_divider\n", n);
  106. dd->max_divider = n;
  107. ret = DPLL_FINT_UNDERFLOW;
  108. } else if (fint > DPLL_FINT_BAND1_MAX &&
  109. fint < DPLL_FINT_BAND2_MIN) {
  110. pr_debug("rejecting n=%d due to Fint failure\n", n);
  111. ret = DPLL_FINT_INVALID;
  112. } else if (fint > DPLL_FINT_BAND2_MAX) {
  113. pr_debug("rejecting n=%d due to Fint failure, "
  114. "boosting min_divider\n", n);
  115. dd->min_divider = n;
  116. ret = DPLL_FINT_INVALID;
  117. }
  118. return ret;
  119. }
  120. /**
  121. * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
  122. * @clk: OMAP clock struct ptr to use
  123. *
  124. * Convert a clockdomain name stored in a struct clk 'clk' into a
  125. * clockdomain pointer, and save it into the struct clk. Intended to be
  126. * called during clk_register(). No return value.
  127. */
  128. void omap2_init_clk_clkdm(struct clk *clk)
  129. {
  130. struct clockdomain *clkdm;
  131. if (!clk->clkdm_name)
  132. return;
  133. clkdm = clkdm_lookup(clk->clkdm_name);
  134. if (clkdm) {
  135. pr_debug("clock: associated clk %s to clkdm %s\n",
  136. clk->name, clk->clkdm_name);
  137. clk->clkdm = clkdm;
  138. } else {
  139. pr_debug("clock: could not associate clk %s to "
  140. "clkdm %s\n", clk->name, clk->clkdm_name);
  141. }
  142. }
  143. /**
  144. * omap2_init_clksel_parent - set a clksel clk's parent field from the hardware
  145. * @clk: OMAP clock struct ptr to use
  146. *
  147. * Given a pointer to a source-selectable struct clk, read the hardware
  148. * register and determine what its parent is currently set to. Update the
  149. * clk->parent field with the appropriate clk ptr.
  150. */
  151. void omap2_init_clksel_parent(struct clk *clk)
  152. {
  153. const struct clksel *clks;
  154. const struct clksel_rate *clkr;
  155. u32 r, found = 0;
  156. if (!clk->clksel)
  157. return;
  158. r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  159. r >>= __ffs(clk->clksel_mask);
  160. for (clks = clk->clksel; clks->parent && !found; clks++) {
  161. for (clkr = clks->rates; clkr->div && !found; clkr++) {
  162. if ((clkr->flags & cpu_mask) && (clkr->val == r)) {
  163. if (clk->parent != clks->parent) {
  164. pr_debug("clock: inited %s parent "
  165. "to %s (was %s)\n",
  166. clk->name, clks->parent->name,
  167. ((clk->parent) ?
  168. clk->parent->name : "NULL"));
  169. clk_reparent(clk, clks->parent);
  170. };
  171. found = 1;
  172. }
  173. }
  174. }
  175. if (!found)
  176. printk(KERN_ERR "clock: init parent: could not find "
  177. "regval %0x for clock %s\n", r, clk->name);
  178. return;
  179. }
  180. /**
  181. * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
  182. * @clk: struct clk * of a DPLL
  183. *
  184. * DPLLs can be locked or bypassed - basically, enabled or disabled.
  185. * When locked, the DPLL output depends on the M and N values. When
  186. * bypassed, on OMAP2xxx, the output rate is either the 32KiHz clock
  187. * or sys_clk. Bypass rates on OMAP3 depend on the DPLL: DPLLs 1 and
  188. * 2 are bypassed with dpll1_fclk and dpll2_fclk respectively
  189. * (generated by DPLL3), while DPLL 3, 4, and 5 bypass rates are sys_clk.
  190. * Returns the current DPLL CLKOUT rate (*not* CLKOUTX2) if the DPLL is
  191. * locked, or the appropriate bypass rate if the DPLL is bypassed, or 0
  192. * if the clock @clk is not a DPLL.
  193. */
  194. u32 omap2_get_dpll_rate(struct clk *clk)
  195. {
  196. long long dpll_clk;
  197. u32 dpll_mult, dpll_div, v;
  198. struct dpll_data *dd;
  199. dd = clk->dpll_data;
  200. if (!dd)
  201. return 0;
  202. /* Return bypass rate if DPLL is bypassed */
  203. v = __raw_readl(dd->control_reg);
  204. v &= dd->enable_mask;
  205. v >>= __ffs(dd->enable_mask);
  206. if (cpu_is_omap24xx()) {
  207. if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
  208. v == OMAP2XXX_EN_DPLL_FRBYPASS)
  209. return dd->clk_bypass->rate;
  210. } else if (cpu_is_omap34xx()) {
  211. if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
  212. v == OMAP3XXX_EN_DPLL_FRBYPASS)
  213. return dd->clk_bypass->rate;
  214. }
  215. v = __raw_readl(dd->mult_div1_reg);
  216. dpll_mult = v & dd->mult_mask;
  217. dpll_mult >>= __ffs(dd->mult_mask);
  218. dpll_div = v & dd->div1_mask;
  219. dpll_div >>= __ffs(dd->div1_mask);
  220. dpll_clk = (long long)dd->clk_ref->rate * dpll_mult;
  221. do_div(dpll_clk, dpll_div + 1);
  222. return dpll_clk;
  223. }
  224. /*
  225. * Used for clocks that have the same value as the parent clock,
  226. * divided by some factor
  227. */
  228. unsigned long omap2_fixed_divisor_recalc(struct clk *clk)
  229. {
  230. WARN_ON(!clk->fixed_div);
  231. return clk->parent->rate / clk->fixed_div;
  232. }
  233. /**
  234. * omap2_clk_dflt_find_companion - find companion clock to @clk
  235. * @clk: struct clk * to find the companion clock of
  236. * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
  237. * @other_bit: u8 ** to return the companion clock bit shift in
  238. *
  239. * Note: We don't need special code here for INVERT_ENABLE for the
  240. * time being since INVERT_ENABLE only applies to clocks enabled by
  241. * CM_CLKEN_PLL
  242. *
  243. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
  244. * just a matter of XORing the bits.
  245. *
  246. * Some clocks don't have companion clocks. For example, modules with
  247. * only an interface clock (such as MAILBOXES) don't have a companion
  248. * clock. Right now, this code relies on the hardware exporting a bit
  249. * in the correct companion register that indicates that the
  250. * nonexistent 'companion clock' is active. Future patches will
  251. * associate this type of code with per-module data structures to
  252. * avoid this issue, and remove the casts. No return value.
  253. */
  254. void omap2_clk_dflt_find_companion(struct clk *clk, void __iomem **other_reg,
  255. u8 *other_bit)
  256. {
  257. u32 r;
  258. /*
  259. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
  260. * it's just a matter of XORing the bits.
  261. */
  262. r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
  263. *other_reg = (__force void __iomem *)r;
  264. *other_bit = clk->enable_bit;
  265. }
  266. /**
  267. * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
  268. * @clk: struct clk * to find IDLEST info for
  269. * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
  270. * @idlest_bit: u8 ** to return the CM_IDLEST bit shift in
  271. *
  272. * Return the CM_IDLEST register address and bit shift corresponding
  273. * to the module that "owns" this clock. This default code assumes
  274. * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
  275. * the IDLEST register address ID corresponds to the CM_*CLKEN
  276. * register address ID (e.g., that CM_FCLKEN2 corresponds to
  277. * CM_IDLEST2). This is not true for all modules. No return value.
  278. */
  279. void omap2_clk_dflt_find_idlest(struct clk *clk, void __iomem **idlest_reg,
  280. u8 *idlest_bit)
  281. {
  282. u32 r;
  283. r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
  284. *idlest_reg = (__force void __iomem *)r;
  285. *idlest_bit = clk->enable_bit;
  286. }
  287. /**
  288. * omap2_module_wait_ready - wait for an OMAP module to leave IDLE
  289. * @clk: struct clk * belonging to the module
  290. *
  291. * If the necessary clocks for the OMAP hardware IP block that
  292. * corresponds to clock @clk are enabled, then wait for the module to
  293. * indicate readiness (i.e., to leave IDLE). This code does not
  294. * belong in the clock code and will be moved in the medium term to
  295. * module-dependent code. No return value.
  296. */
  297. static void omap2_module_wait_ready(struct clk *clk)
  298. {
  299. void __iomem *companion_reg, *idlest_reg;
  300. u8 other_bit, idlest_bit;
  301. /* Not all modules have multiple clocks that their IDLEST depends on */
  302. if (clk->ops->find_companion) {
  303. clk->ops->find_companion(clk, &companion_reg, &other_bit);
  304. if (!(__raw_readl(companion_reg) & (1 << other_bit)))
  305. return;
  306. }
  307. clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit);
  308. omap2_cm_wait_idlest(idlest_reg, (1 << idlest_bit), clk->name);
  309. }
  310. int omap2_dflt_clk_enable(struct clk *clk)
  311. {
  312. u32 v;
  313. if (unlikely(clk->enable_reg == NULL)) {
  314. pr_err("clock.c: Enable for %s without enable code\n",
  315. clk->name);
  316. return 0; /* REVISIT: -EINVAL */
  317. }
  318. v = __raw_readl(clk->enable_reg);
  319. if (clk->flags & INVERT_ENABLE)
  320. v &= ~(1 << clk->enable_bit);
  321. else
  322. v |= (1 << clk->enable_bit);
  323. __raw_writel(v, clk->enable_reg);
  324. v = __raw_readl(clk->enable_reg); /* OCP barrier */
  325. if (clk->ops->find_idlest)
  326. omap2_module_wait_ready(clk);
  327. return 0;
  328. }
  329. void omap2_dflt_clk_disable(struct clk *clk)
  330. {
  331. u32 v;
  332. if (!clk->enable_reg) {
  333. /*
  334. * 'Independent' here refers to a clock which is not
  335. * controlled by its parent.
  336. */
  337. printk(KERN_ERR "clock: clk_disable called on independent "
  338. "clock %s which has no enable_reg\n", clk->name);
  339. return;
  340. }
  341. v = __raw_readl(clk->enable_reg);
  342. if (clk->flags & INVERT_ENABLE)
  343. v |= (1 << clk->enable_bit);
  344. else
  345. v &= ~(1 << clk->enable_bit);
  346. __raw_writel(v, clk->enable_reg);
  347. /* No OCP barrier needed here since it is a disable operation */
  348. }
  349. const struct clkops clkops_omap2_dflt_wait = {
  350. .enable = omap2_dflt_clk_enable,
  351. .disable = omap2_dflt_clk_disable,
  352. .find_companion = omap2_clk_dflt_find_companion,
  353. .find_idlest = omap2_clk_dflt_find_idlest,
  354. };
  355. const struct clkops clkops_omap2_dflt = {
  356. .enable = omap2_dflt_clk_enable,
  357. .disable = omap2_dflt_clk_disable,
  358. };
  359. /* Enables clock without considering parent dependencies or use count
  360. * REVISIT: Maybe change this to use clk->enable like on omap1?
  361. */
  362. static int _omap2_clk_enable(struct clk *clk)
  363. {
  364. return clk->ops->enable(clk);
  365. }
  366. /* Disables clock without considering parent dependencies or use count */
  367. static void _omap2_clk_disable(struct clk *clk)
  368. {
  369. clk->ops->disable(clk);
  370. }
  371. void omap2_clk_disable(struct clk *clk)
  372. {
  373. if (clk->usecount > 0 && !(--clk->usecount)) {
  374. _omap2_clk_disable(clk);
  375. if (clk->parent)
  376. omap2_clk_disable(clk->parent);
  377. if (clk->clkdm)
  378. omap2_clkdm_clk_disable(clk->clkdm, clk);
  379. }
  380. }
  381. int omap2_clk_enable(struct clk *clk)
  382. {
  383. int ret = 0;
  384. if (clk->usecount++ == 0) {
  385. if (clk->clkdm)
  386. omap2_clkdm_clk_enable(clk->clkdm, clk);
  387. if (clk->parent) {
  388. ret = omap2_clk_enable(clk->parent);
  389. if (ret)
  390. goto err;
  391. }
  392. ret = _omap2_clk_enable(clk);
  393. if (ret) {
  394. if (clk->parent)
  395. omap2_clk_disable(clk->parent);
  396. goto err;
  397. }
  398. }
  399. return ret;
  400. err:
  401. if (clk->clkdm)
  402. omap2_clkdm_clk_disable(clk->clkdm, clk);
  403. clk->usecount--;
  404. return ret;
  405. }
  406. /*
  407. * Used for clocks that are part of CLKSEL_xyz governed clocks.
  408. * REVISIT: Maybe change to use clk->enable() functions like on omap1?
  409. */
  410. unsigned long omap2_clksel_recalc(struct clk *clk)
  411. {
  412. unsigned long rate;
  413. u32 div = 0;
  414. pr_debug("clock: recalc'ing clksel clk %s\n", clk->name);
  415. div = omap2_clksel_get_divisor(clk);
  416. if (div == 0)
  417. return clk->rate;
  418. rate = clk->parent->rate / div;
  419. pr_debug("clock: new clock rate is %ld (div %d)\n", rate, div);
  420. return rate;
  421. }
  422. /**
  423. * omap2_get_clksel_by_parent - return clksel struct for a given clk & parent
  424. * @clk: OMAP struct clk ptr to inspect
  425. * @src_clk: OMAP struct clk ptr of the parent clk to search for
  426. *
  427. * Scan the struct clksel array associated with the clock to find
  428. * the element associated with the supplied parent clock address.
  429. * Returns a pointer to the struct clksel on success or NULL on error.
  430. */
  431. static const struct clksel *omap2_get_clksel_by_parent(struct clk *clk,
  432. struct clk *src_clk)
  433. {
  434. const struct clksel *clks;
  435. if (!clk->clksel)
  436. return NULL;
  437. for (clks = clk->clksel; clks->parent; clks++) {
  438. if (clks->parent == src_clk)
  439. break; /* Found the requested parent */
  440. }
  441. if (!clks->parent) {
  442. printk(KERN_ERR "clock: Could not find parent clock %s in "
  443. "clksel array of clock %s\n", src_clk->name,
  444. clk->name);
  445. return NULL;
  446. }
  447. return clks;
  448. }
  449. /**
  450. * omap2_clksel_round_rate_div - find divisor for the given clock and rate
  451. * @clk: OMAP struct clk to use
  452. * @target_rate: desired clock rate
  453. * @new_div: ptr to where we should store the divisor
  454. *
  455. * Finds 'best' divider value in an array based on the source and target
  456. * rates. The divider array must be sorted with smallest divider first.
  457. * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
  458. * they are only settable as part of virtual_prcm set.
  459. *
  460. * Returns the rounded clock rate or returns 0xffffffff on error.
  461. */
  462. u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
  463. u32 *new_div)
  464. {
  465. unsigned long test_rate;
  466. const struct clksel *clks;
  467. const struct clksel_rate *clkr;
  468. u32 last_div = 0;
  469. pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",
  470. clk->name, target_rate);
  471. *new_div = 1;
  472. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  473. if (!clks)
  474. return ~0;
  475. for (clkr = clks->rates; clkr->div; clkr++) {
  476. if (!(clkr->flags & cpu_mask))
  477. continue;
  478. /* Sanity check */
  479. if (clkr->div <= last_div)
  480. pr_err("clock: clksel_rate table not sorted "
  481. "for clock %s", clk->name);
  482. last_div = clkr->div;
  483. test_rate = clk->parent->rate / clkr->div;
  484. if (test_rate <= target_rate)
  485. break; /* found it */
  486. }
  487. if (!clkr->div) {
  488. pr_err("clock: Could not find divisor for target "
  489. "rate %ld for clock %s parent %s\n", target_rate,
  490. clk->name, clk->parent->name);
  491. return ~0;
  492. }
  493. *new_div = clkr->div;
  494. pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div,
  495. (clk->parent->rate / clkr->div));
  496. return (clk->parent->rate / clkr->div);
  497. }
  498. /**
  499. * omap2_clksel_round_rate - find rounded rate for the given clock and rate
  500. * @clk: OMAP struct clk to use
  501. * @target_rate: desired clock rate
  502. *
  503. * Compatibility wrapper for OMAP clock framework
  504. * Finds best target rate based on the source clock and possible dividers.
  505. * rates. The divider array must be sorted with smallest divider first.
  506. * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
  507. * they are only settable as part of virtual_prcm set.
  508. *
  509. * Returns the rounded clock rate or returns 0xffffffff on error.
  510. */
  511. long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
  512. {
  513. u32 new_div;
  514. return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
  515. }
  516. /* Given a clock and a rate apply a clock specific rounding function */
  517. long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
  518. {
  519. if (clk->round_rate)
  520. return clk->round_rate(clk, rate);
  521. if (clk->flags & RATE_FIXED)
  522. printk(KERN_ERR "clock: generic omap2_clk_round_rate called "
  523. "on fixed-rate clock %s\n", clk->name);
  524. return clk->rate;
  525. }
  526. /**
  527. * omap2_clksel_to_divisor() - turn clksel field value into integer divider
  528. * @clk: OMAP struct clk to use
  529. * @field_val: register field value to find
  530. *
  531. * Given a struct clk of a rate-selectable clksel clock, and a register field
  532. * value to search for, find the corresponding clock divisor. The register
  533. * field value should be pre-masked and shifted down so the LSB is at bit 0
  534. * before calling. Returns 0 on error
  535. */
  536. u32 omap2_clksel_to_divisor(struct clk *clk, u32 field_val)
  537. {
  538. const struct clksel *clks;
  539. const struct clksel_rate *clkr;
  540. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  541. if (!clks)
  542. return 0;
  543. for (clkr = clks->rates; clkr->div; clkr++) {
  544. if ((clkr->flags & cpu_mask) && (clkr->val == field_val))
  545. break;
  546. }
  547. if (!clkr->div) {
  548. printk(KERN_ERR "clock: Could not find fieldval %d for "
  549. "clock %s parent %s\n", field_val, clk->name,
  550. clk->parent->name);
  551. return 0;
  552. }
  553. return clkr->div;
  554. }
  555. /**
  556. * omap2_divisor_to_clksel() - turn clksel integer divisor into a field value
  557. * @clk: OMAP struct clk to use
  558. * @div: integer divisor to search for
  559. *
  560. * Given a struct clk of a rate-selectable clksel clock, and a clock divisor,
  561. * find the corresponding register field value. The return register value is
  562. * the value before left-shifting. Returns ~0 on error
  563. */
  564. u32 omap2_divisor_to_clksel(struct clk *clk, u32 div)
  565. {
  566. const struct clksel *clks;
  567. const struct clksel_rate *clkr;
  568. /* should never happen */
  569. WARN_ON(div == 0);
  570. clks = omap2_get_clksel_by_parent(clk, clk->parent);
  571. if (!clks)
  572. return ~0;
  573. for (clkr = clks->rates; clkr->div; clkr++) {
  574. if ((clkr->flags & cpu_mask) && (clkr->div == div))
  575. break;
  576. }
  577. if (!clkr->div) {
  578. printk(KERN_ERR "clock: Could not find divisor %d for "
  579. "clock %s parent %s\n", div, clk->name,
  580. clk->parent->name);
  581. return ~0;
  582. }
  583. return clkr->val;
  584. }
  585. /**
  586. * omap2_clksel_get_divisor - get current divider applied to parent clock.
  587. * @clk: OMAP struct clk to use.
  588. *
  589. * Returns the integer divisor upon success or 0 on error.
  590. */
  591. u32 omap2_clksel_get_divisor(struct clk *clk)
  592. {
  593. u32 v;
  594. if (!clk->clksel_mask)
  595. return 0;
  596. v = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  597. v >>= __ffs(clk->clksel_mask);
  598. return omap2_clksel_to_divisor(clk, v);
  599. }
  600. int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
  601. {
  602. u32 v, field_val, validrate, new_div = 0;
  603. if (!clk->clksel_mask)
  604. return -EINVAL;
  605. validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
  606. if (validrate != rate)
  607. return -EINVAL;
  608. field_val = omap2_divisor_to_clksel(clk, new_div);
  609. if (field_val == ~0)
  610. return -EINVAL;
  611. v = __raw_readl(clk->clksel_reg);
  612. v &= ~clk->clksel_mask;
  613. v |= field_val << __ffs(clk->clksel_mask);
  614. __raw_writel(v, clk->clksel_reg);
  615. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  616. clk->rate = clk->parent->rate / new_div;
  617. _omap2xxx_clk_commit(clk);
  618. return 0;
  619. }
  620. /* Set the clock rate for a clock source */
  621. int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
  622. {
  623. int ret = -EINVAL;
  624. pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
  625. /* CONFIG_PARTICIPANT clocks are changed only in sets via the
  626. rate table mechanism, driven by mpu_speed */
  627. if (clk->flags & CONFIG_PARTICIPANT)
  628. return -EINVAL;
  629. /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
  630. if (clk->set_rate)
  631. ret = clk->set_rate(clk, rate);
  632. return ret;
  633. }
  634. /*
  635. * Converts encoded control register address into a full address
  636. * On error, the return value (parent_div) will be 0.
  637. */
  638. static u32 _omap2_clksel_get_src_field(struct clk *src_clk, struct clk *clk,
  639. u32 *field_val)
  640. {
  641. const struct clksel *clks;
  642. const struct clksel_rate *clkr;
  643. clks = omap2_get_clksel_by_parent(clk, src_clk);
  644. if (!clks)
  645. return 0;
  646. for (clkr = clks->rates; clkr->div; clkr++) {
  647. if (clkr->flags & cpu_mask && clkr->flags & DEFAULT_RATE)
  648. break; /* Found the default rate for this platform */
  649. }
  650. if (!clkr->div) {
  651. printk(KERN_ERR "clock: Could not find default rate for "
  652. "clock %s parent %s\n", clk->name,
  653. src_clk->parent->name);
  654. return 0;
  655. }
  656. /* Should never happen. Add a clksel mask to the struct clk. */
  657. WARN_ON(clk->clksel_mask == 0);
  658. *field_val = clkr->val;
  659. return clkr->div;
  660. }
  661. int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
  662. {
  663. u32 field_val, v, parent_div;
  664. if (clk->flags & CONFIG_PARTICIPANT)
  665. return -EINVAL;
  666. if (!clk->clksel)
  667. return -EINVAL;
  668. parent_div = _omap2_clksel_get_src_field(new_parent, clk, &field_val);
  669. if (!parent_div)
  670. return -EINVAL;
  671. /* Set new source value (previous dividers if any in effect) */
  672. v = __raw_readl(clk->clksel_reg);
  673. v &= ~clk->clksel_mask;
  674. v |= field_val << __ffs(clk->clksel_mask);
  675. __raw_writel(v, clk->clksel_reg);
  676. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  677. _omap2xxx_clk_commit(clk);
  678. clk_reparent(clk, new_parent);
  679. /* CLKSEL clocks follow their parents' rates, divided by a divisor */
  680. clk->rate = new_parent->rate;
  681. if (parent_div > 0)
  682. clk->rate /= parent_div;
  683. pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
  684. clk->name, clk->parent->name, clk->rate);
  685. return 0;
  686. }
  687. /* DPLL rate rounding code */
  688. /**
  689. * omap2_dpll_set_rate_tolerance: set the error tolerance during rate rounding
  690. * @clk: struct clk * of the DPLL
  691. * @tolerance: maximum rate error tolerance
  692. *
  693. * Set the maximum DPLL rate error tolerance for the rate rounding
  694. * algorithm. The rate tolerance is an attempt to balance DPLL power
  695. * saving (the least divider value "n") vs. rate fidelity (the least
  696. * difference between the desired DPLL target rate and the rounded
  697. * rate out of the algorithm). So, increasing the tolerance is likely
  698. * to decrease DPLL power consumption and increase DPLL rate error.
  699. * Returns -EINVAL if provided a null clock ptr or a clk that is not a
  700. * DPLL; or 0 upon success.
  701. */
  702. int omap2_dpll_set_rate_tolerance(struct clk *clk, unsigned int tolerance)
  703. {
  704. if (!clk || !clk->dpll_data)
  705. return -EINVAL;
  706. clk->dpll_data->rate_tolerance = tolerance;
  707. return 0;
  708. }
  709. static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
  710. unsigned int m, unsigned int n)
  711. {
  712. unsigned long long num;
  713. num = (unsigned long long)parent_rate * m;
  714. do_div(num, n);
  715. return num;
  716. }
  717. /*
  718. * _dpll_test_mult - test a DPLL multiplier value
  719. * @m: pointer to the DPLL m (multiplier) value under test
  720. * @n: current DPLL n (divider) value under test
  721. * @new_rate: pointer to storage for the resulting rounded rate
  722. * @target_rate: the desired DPLL rate
  723. * @parent_rate: the DPLL's parent clock rate
  724. *
  725. * This code tests a DPLL multiplier value, ensuring that the
  726. * resulting rate will not be higher than the target_rate, and that
  727. * the multiplier value itself is valid for the DPLL. Initially, the
  728. * integer pointed to by the m argument should be prescaled by
  729. * multiplying by DPLL_SCALE_FACTOR. The code will replace this with
  730. * a non-scaled m upon return. This non-scaled m will result in a
  731. * new_rate as close as possible to target_rate (but not greater than
  732. * target_rate) given the current (parent_rate, n, prescaled m)
  733. * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
  734. * non-scaled m attempted to underflow, which can allow the calling
  735. * function to bail out early; or 0 upon success.
  736. */
  737. static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
  738. unsigned long target_rate,
  739. unsigned long parent_rate)
  740. {
  741. int r = 0, carry = 0;
  742. /* Unscale m and round if necessary */
  743. if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
  744. carry = 1;
  745. *m = (*m / DPLL_SCALE_FACTOR) + carry;
  746. /*
  747. * The new rate must be <= the target rate to avoid programming
  748. * a rate that is impossible for the hardware to handle
  749. */
  750. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  751. if (*new_rate > target_rate) {
  752. (*m)--;
  753. *new_rate = 0;
  754. }
  755. /* Guard against m underflow */
  756. if (*m < DPLL_MIN_MULTIPLIER) {
  757. *m = DPLL_MIN_MULTIPLIER;
  758. *new_rate = 0;
  759. r = DPLL_MULT_UNDERFLOW;
  760. }
  761. if (*new_rate == 0)
  762. *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
  763. return r;
  764. }
  765. /**
  766. * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
  767. * @clk: struct clk * for a DPLL
  768. * @target_rate: desired DPLL clock rate
  769. *
  770. * Given a DPLL, a desired target rate, and a rate tolerance, round
  771. * the target rate to a possible, programmable rate for this DPLL.
  772. * Rate tolerance is assumed to be set by the caller before this
  773. * function is called. Attempts to select the minimum possible n
  774. * within the tolerance to reduce power consumption. Stores the
  775. * computed (m, n) in the DPLL's dpll_data structure so set_rate()
  776. * will not need to call this (expensive) function again. Returns ~0
  777. * if the target rate cannot be rounded, either because the rate is
  778. * too low or because the rate tolerance is set too tightly; or the
  779. * rounded rate upon success.
  780. */
  781. long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
  782. {
  783. int m, n, r, e, scaled_max_m;
  784. unsigned long scaled_rt_rp, new_rate;
  785. int min_e = -1, min_e_m = -1, min_e_n = -1;
  786. struct dpll_data *dd;
  787. if (!clk || !clk->dpll_data)
  788. return ~0;
  789. dd = clk->dpll_data;
  790. pr_debug("clock: starting DPLL round_rate for clock %s, target rate "
  791. "%ld\n", clk->name, target_rate);
  792. scaled_rt_rp = target_rate / (dd->clk_ref->rate / DPLL_SCALE_FACTOR);
  793. scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
  794. dd->last_rounded_rate = 0;
  795. for (n = dd->min_divider; n <= dd->max_divider; n++) {
  796. /* Is the (input clk, divider) pair valid for the DPLL? */
  797. r = _dpll_test_fint(clk, n);
  798. if (r == DPLL_FINT_UNDERFLOW)
  799. break;
  800. else if (r == DPLL_FINT_INVALID)
  801. continue;
  802. /* Compute the scaled DPLL multiplier, based on the divider */
  803. m = scaled_rt_rp * n;
  804. /*
  805. * Since we're counting n up, a m overflow means we
  806. * can bail out completely (since as n increases in
  807. * the next iteration, there's no way that m can
  808. * increase beyond the current m)
  809. */
  810. if (m > scaled_max_m)
  811. break;
  812. r = _dpll_test_mult(&m, n, &new_rate, target_rate,
  813. dd->clk_ref->rate);
  814. /* m can't be set low enough for this n - try with a larger n */
  815. if (r == DPLL_MULT_UNDERFLOW)
  816. continue;
  817. e = target_rate - new_rate;
  818. pr_debug("clock: n = %d: m = %d: rate error is %d "
  819. "(new_rate = %ld)\n", n, m, e, new_rate);
  820. if (min_e == -1 ||
  821. min_e >= (int)(abs(e) - dd->rate_tolerance)) {
  822. min_e = e;
  823. min_e_m = m;
  824. min_e_n = n;
  825. pr_debug("clock: found new least error %d\n", min_e);
  826. /* We found good settings -- bail out now */
  827. if (min_e <= dd->rate_tolerance)
  828. break;
  829. }
  830. }
  831. if (min_e < 0) {
  832. pr_debug("clock: error: target rate or tolerance too low\n");
  833. return ~0;
  834. }
  835. dd->last_rounded_m = min_e_m;
  836. dd->last_rounded_n = min_e_n;
  837. dd->last_rounded_rate = _dpll_compute_new_rate(dd->clk_ref->rate,
  838. min_e_m, min_e_n);
  839. pr_debug("clock: final least error: e = %d, m = %d, n = %d\n",
  840. min_e, min_e_m, min_e_n);
  841. pr_debug("clock: final rate: %ld (target rate: %ld)\n",
  842. dd->last_rounded_rate, target_rate);
  843. return dd->last_rounded_rate;
  844. }
  845. /*-------------------------------------------------------------------------
  846. * Omap2 clock reset and init functions
  847. *-------------------------------------------------------------------------*/
  848. #ifdef CONFIG_OMAP_RESET_CLOCKS
  849. void omap2_clk_disable_unused(struct clk *clk)
  850. {
  851. u32 regval32, v;
  852. v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
  853. regval32 = __raw_readl(clk->enable_reg);
  854. if ((regval32 & (1 << clk->enable_bit)) == v)
  855. return;
  856. printk(KERN_DEBUG "Disabling unused clock \"%s\"\n", clk->name);
  857. if (cpu_is_omap34xx()) {
  858. omap2_clk_enable(clk);
  859. omap2_clk_disable(clk);
  860. } else
  861. _omap2_clk_disable(clk);
  862. if (clk->clkdm != NULL)
  863. pwrdm_clkdm_state_switch(clk->clkdm);
  864. }
  865. #endif