clock.c 30 KB

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