dpll3xxx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * OMAP3/4 - specific DPLL control functions
  3. *
  4. * Copyright (C) 2009-2010 Texas Instruments, Inc.
  5. * Copyright (C) 2009-2010 Nokia Corporation
  6. *
  7. * Written by Paul Walmsley
  8. * Testing and integration fixes by Jouni Högander
  9. *
  10. * 36xx support added by Vishwanath BS, Richard Woodruff, and Nishanth
  11. * Menon
  12. *
  13. * Parts of this code are based on code written by
  14. * Richard Woodruff, Tony Lindgren, Tuukka Tikkanen, Karthik Dasu
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as
  18. * published by the Free Software Foundation.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/device.h>
  22. #include <linux/list.h>
  23. #include <linux/errno.h>
  24. #include <linux/delay.h>
  25. #include <linux/clk.h>
  26. #include <linux/io.h>
  27. #include <linux/bitops.h>
  28. #include <plat/cpu.h>
  29. #include <plat/clock.h>
  30. #include <asm/clkdev.h>
  31. #include "clock.h"
  32. #include "cm2xxx_3xxx.h"
  33. #include "cm-regbits-34xx.h"
  34. /* CM_AUTOIDLE_PLL*.AUTO_* bit values */
  35. #define DPLL_AUTOIDLE_DISABLE 0x0
  36. #define DPLL_AUTOIDLE_LOW_POWER_STOP 0x1
  37. #define MAX_DPLL_WAIT_TRIES 1000000
  38. /* Private functions */
  39. /* _omap3_dpll_write_clken - write clken_bits arg to a DPLL's enable bits */
  40. static void _omap3_dpll_write_clken(struct clk *clk, u8 clken_bits)
  41. {
  42. const struct dpll_data *dd;
  43. u32 v;
  44. dd = clk->dpll_data;
  45. v = __raw_readl(dd->control_reg);
  46. v &= ~dd->enable_mask;
  47. v |= clken_bits << __ffs(dd->enable_mask);
  48. __raw_writel(v, dd->control_reg);
  49. }
  50. /* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */
  51. static int _omap3_wait_dpll_status(struct clk *clk, u8 state)
  52. {
  53. const struct dpll_data *dd;
  54. int i = 0;
  55. int ret = -EINVAL;
  56. dd = clk->dpll_data;
  57. state <<= __ffs(dd->idlest_mask);
  58. while (((__raw_readl(dd->idlest_reg) & dd->idlest_mask) != state) &&
  59. i < MAX_DPLL_WAIT_TRIES) {
  60. i++;
  61. udelay(1);
  62. }
  63. if (i == MAX_DPLL_WAIT_TRIES) {
  64. printk(KERN_ERR "clock: %s failed transition to '%s'\n",
  65. clk->name, (state) ? "locked" : "bypassed");
  66. } else {
  67. pr_debug("clock: %s transition to '%s' in %d loops\n",
  68. clk->name, (state) ? "locked" : "bypassed", i);
  69. ret = 0;
  70. }
  71. return ret;
  72. }
  73. /* From 3430 TRM ES2 4.7.6.2 */
  74. static u16 _omap3_dpll_compute_freqsel(struct clk *clk, u8 n)
  75. {
  76. unsigned long fint;
  77. u16 f = 0;
  78. fint = clk->dpll_data->clk_ref->rate / n;
  79. pr_debug("clock: fint is %lu\n", fint);
  80. if (fint >= 750000 && fint <= 1000000)
  81. f = 0x3;
  82. else if (fint > 1000000 && fint <= 1250000)
  83. f = 0x4;
  84. else if (fint > 1250000 && fint <= 1500000)
  85. f = 0x5;
  86. else if (fint > 1500000 && fint <= 1750000)
  87. f = 0x6;
  88. else if (fint > 1750000 && fint <= 2100000)
  89. f = 0x7;
  90. else if (fint > 7500000 && fint <= 10000000)
  91. f = 0xB;
  92. else if (fint > 10000000 && fint <= 12500000)
  93. f = 0xC;
  94. else if (fint > 12500000 && fint <= 15000000)
  95. f = 0xD;
  96. else if (fint > 15000000 && fint <= 17500000)
  97. f = 0xE;
  98. else if (fint > 17500000 && fint <= 21000000)
  99. f = 0xF;
  100. else
  101. pr_debug("clock: unknown freqsel setting for %d\n", n);
  102. return f;
  103. }
  104. /*
  105. * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
  106. * @clk: pointer to a DPLL struct clk
  107. *
  108. * Instructs a non-CORE DPLL to lock. Waits for the DPLL to report
  109. * readiness before returning. Will save and restore the DPLL's
  110. * autoidle state across the enable, per the CDP code. If the DPLL
  111. * locked successfully, return 0; if the DPLL did not lock in the time
  112. * allotted, or DPLL3 was passed in, return -EINVAL.
  113. */
  114. static int _omap3_noncore_dpll_lock(struct clk *clk)
  115. {
  116. u8 ai;
  117. int r;
  118. pr_debug("clock: locking DPLL %s\n", clk->name);
  119. ai = omap3_dpll_autoidle_read(clk);
  120. omap3_dpll_deny_idle(clk);
  121. _omap3_dpll_write_clken(clk, DPLL_LOCKED);
  122. r = _omap3_wait_dpll_status(clk, 1);
  123. if (ai)
  124. omap3_dpll_allow_idle(clk);
  125. return r;
  126. }
  127. /*
  128. * _omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
  129. * @clk: pointer to a DPLL struct clk
  130. *
  131. * Instructs a non-CORE DPLL to enter low-power bypass mode. In
  132. * bypass mode, the DPLL's rate is set equal to its parent clock's
  133. * rate. Waits for the DPLL to report readiness before returning.
  134. * Will save and restore the DPLL's autoidle state across the enable,
  135. * per the CDP code. If the DPLL entered bypass mode successfully,
  136. * return 0; if the DPLL did not enter bypass in the time allotted, or
  137. * DPLL3 was passed in, or the DPLL does not support low-power bypass,
  138. * return -EINVAL.
  139. */
  140. static int _omap3_noncore_dpll_bypass(struct clk *clk)
  141. {
  142. int r;
  143. u8 ai;
  144. if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
  145. return -EINVAL;
  146. pr_debug("clock: configuring DPLL %s for low-power bypass\n",
  147. clk->name);
  148. ai = omap3_dpll_autoidle_read(clk);
  149. _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
  150. r = _omap3_wait_dpll_status(clk, 0);
  151. if (ai)
  152. omap3_dpll_allow_idle(clk);
  153. else
  154. omap3_dpll_deny_idle(clk);
  155. return r;
  156. }
  157. /*
  158. * _omap3_noncore_dpll_stop - instruct a DPLL to stop
  159. * @clk: pointer to a DPLL struct clk
  160. *
  161. * Instructs a non-CORE DPLL to enter low-power stop. Will save and
  162. * restore the DPLL's autoidle state across the stop, per the CDP
  163. * code. If DPLL3 was passed in, or the DPLL does not support
  164. * low-power stop, return -EINVAL; otherwise, return 0.
  165. */
  166. static int _omap3_noncore_dpll_stop(struct clk *clk)
  167. {
  168. u8 ai;
  169. if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
  170. return -EINVAL;
  171. pr_debug("clock: stopping DPLL %s\n", clk->name);
  172. ai = omap3_dpll_autoidle_read(clk);
  173. _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
  174. if (ai)
  175. omap3_dpll_allow_idle(clk);
  176. else
  177. omap3_dpll_deny_idle(clk);
  178. return 0;
  179. }
  180. /**
  181. * lookup_dco_sddiv - Set j-type DPLL4 compensation variables
  182. * @clk: pointer to a DPLL struct clk
  183. * @dco: digital control oscillator selector
  184. * @sd_div: target sigma-delta divider
  185. * @m: DPLL multiplier to set
  186. * @n: DPLL divider to set
  187. *
  188. * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)"
  189. *
  190. * XXX This code is not needed for 3430/AM35xx; can it be optimized
  191. * out in non-multi-OMAP builds for those chips?
  192. */
  193. static void lookup_dco_sddiv(struct clk *clk, u8 *dco, u8 *sd_div, u16 m,
  194. u8 n)
  195. {
  196. unsigned long fint, clkinp, sd; /* watch out for overflow */
  197. int mod1, mod2;
  198. clkinp = clk->parent->rate;
  199. fint = (clkinp / n) * m;
  200. if (fint < 1000000000)
  201. *dco = 2;
  202. else
  203. *dco = 4;
  204. /*
  205. * target sigma-delta to near 250MHz
  206. * sd = ceil[(m/(n+1)) * (clkinp_MHz / 250)]
  207. */
  208. clkinp /= 100000; /* shift from MHz to 10*Hz for 38.4 and 19.2 */
  209. mod1 = (clkinp * m) % (250 * n);
  210. sd = (clkinp * m) / (250 * n);
  211. mod2 = sd % 10;
  212. sd /= 10;
  213. if (mod1 || mod2)
  214. sd++;
  215. *sd_div = sd;
  216. }
  217. /*
  218. * _omap3_noncore_dpll_program - set non-core DPLL M,N values directly
  219. * @clk: struct clk * of DPLL to set
  220. * @m: DPLL multiplier to set
  221. * @n: DPLL divider to set
  222. * @freqsel: FREQSEL value to set
  223. *
  224. * Program the DPLL with the supplied M, N values, and wait for the DPLL to
  225. * lock.. Returns -EINVAL upon error, or 0 upon success.
  226. */
  227. static int omap3_noncore_dpll_program(struct clk *clk, u16 m, u8 n, u16 freqsel)
  228. {
  229. struct dpll_data *dd = clk->dpll_data;
  230. u32 v;
  231. /* 3430 ES2 TRM: 4.7.6.9 DPLL Programming Sequence */
  232. _omap3_noncore_dpll_bypass(clk);
  233. /*
  234. * Set jitter correction. No jitter correction for OMAP4 and 3630
  235. * since freqsel field is no longer present
  236. */
  237. if (!cpu_is_omap44xx() && !cpu_is_omap3630()) {
  238. v = __raw_readl(dd->control_reg);
  239. v &= ~dd->freqsel_mask;
  240. v |= freqsel << __ffs(dd->freqsel_mask);
  241. __raw_writel(v, dd->control_reg);
  242. }
  243. /* Set DPLL multiplier, divider */
  244. v = __raw_readl(dd->mult_div1_reg);
  245. v &= ~(dd->mult_mask | dd->div1_mask);
  246. v |= m << __ffs(dd->mult_mask);
  247. v |= (n - 1) << __ffs(dd->div1_mask);
  248. /*
  249. * XXX This code is not needed for 3430/AM35XX; can it be optimized
  250. * out in non-multi-OMAP builds for those chips?
  251. */
  252. if ((dd->flags & DPLL_J_TYPE) && !(dd->flags & DPLL_NO_DCO_SEL)) {
  253. u8 dco, sd_div;
  254. lookup_dco_sddiv(clk, &dco, &sd_div, m, n);
  255. /* XXX This probably will need revision for OMAP4 */
  256. v &= ~(OMAP3630_PERIPH_DPLL_DCO_SEL_MASK
  257. | OMAP3630_PERIPH_DPLL_SD_DIV_MASK);
  258. v |= dco << __ffs(OMAP3630_PERIPH_DPLL_DCO_SEL_MASK);
  259. v |= sd_div << __ffs(OMAP3630_PERIPH_DPLL_SD_DIV_MASK);
  260. }
  261. __raw_writel(v, dd->mult_div1_reg);
  262. /* We let the clock framework set the other output dividers later */
  263. /* REVISIT: Set ramp-up delay? */
  264. _omap3_noncore_dpll_lock(clk);
  265. return 0;
  266. }
  267. /* Public functions */
  268. /**
  269. * omap3_dpll_recalc - recalculate DPLL rate
  270. * @clk: DPLL struct clk
  271. *
  272. * Recalculate and propagate the DPLL rate.
  273. */
  274. unsigned long omap3_dpll_recalc(struct clk *clk)
  275. {
  276. return omap2_get_dpll_rate(clk);
  277. }
  278. /* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
  279. /**
  280. * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
  281. * @clk: pointer to a DPLL struct clk
  282. *
  283. * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
  284. * The choice of modes depends on the DPLL's programmed rate: if it is
  285. * the same as the DPLL's parent clock, it will enter bypass;
  286. * otherwise, it will enter lock. This code will wait for the DPLL to
  287. * indicate readiness before returning, unless the DPLL takes too long
  288. * to enter the target state. Intended to be used as the struct clk's
  289. * enable function. If DPLL3 was passed in, or the DPLL does not
  290. * support low-power stop, or if the DPLL took too long to enter
  291. * bypass or lock, return -EINVAL; otherwise, return 0.
  292. */
  293. int omap3_noncore_dpll_enable(struct clk *clk)
  294. {
  295. int r;
  296. struct dpll_data *dd;
  297. dd = clk->dpll_data;
  298. if (!dd)
  299. return -EINVAL;
  300. if (clk->rate == dd->clk_bypass->rate) {
  301. WARN_ON(clk->parent != dd->clk_bypass);
  302. r = _omap3_noncore_dpll_bypass(clk);
  303. } else {
  304. WARN_ON(clk->parent != dd->clk_ref);
  305. r = _omap3_noncore_dpll_lock(clk);
  306. }
  307. /*
  308. *FIXME: this is dubious - if clk->rate has changed, what about
  309. * propagating?
  310. */
  311. if (!r)
  312. clk->rate = omap2_get_dpll_rate(clk);
  313. return r;
  314. }
  315. /**
  316. * omap3_noncore_dpll_disable - instruct a DPLL to enter low-power stop
  317. * @clk: pointer to a DPLL struct clk
  318. *
  319. * Instructs a non-CORE DPLL to enter low-power stop. This function is
  320. * intended for use in struct clkops. No return value.
  321. */
  322. void omap3_noncore_dpll_disable(struct clk *clk)
  323. {
  324. _omap3_noncore_dpll_stop(clk);
  325. }
  326. /* Non-CORE DPLL rate set code */
  327. /**
  328. * omap3_noncore_dpll_set_rate - set non-core DPLL rate
  329. * @clk: struct clk * of DPLL to set
  330. * @rate: rounded target rate
  331. *
  332. * Set the DPLL CLKOUT to the target rate. If the DPLL can enter
  333. * low-power bypass, and the target rate is the bypass source clock
  334. * rate, then configure the DPLL for bypass. Otherwise, round the
  335. * target rate if it hasn't been done already, then program and lock
  336. * the DPLL. Returns -EINVAL upon error, or 0 upon success.
  337. */
  338. int omap3_noncore_dpll_set_rate(struct clk *clk, unsigned long rate)
  339. {
  340. struct clk *new_parent = NULL;
  341. u16 freqsel = 0;
  342. struct dpll_data *dd;
  343. int ret;
  344. if (!clk || !rate)
  345. return -EINVAL;
  346. dd = clk->dpll_data;
  347. if (!dd)
  348. return -EINVAL;
  349. if (rate == omap2_get_dpll_rate(clk))
  350. return 0;
  351. /*
  352. * Ensure both the bypass and ref clocks are enabled prior to
  353. * doing anything; we need the bypass clock running to reprogram
  354. * the DPLL.
  355. */
  356. omap2_clk_enable(dd->clk_bypass);
  357. omap2_clk_enable(dd->clk_ref);
  358. if (dd->clk_bypass->rate == rate &&
  359. (clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS))) {
  360. pr_debug("clock: %s: set rate: entering bypass.\n", clk->name);
  361. ret = _omap3_noncore_dpll_bypass(clk);
  362. if (!ret)
  363. new_parent = dd->clk_bypass;
  364. } else {
  365. if (dd->last_rounded_rate != rate)
  366. omap2_dpll_round_rate(clk, rate);
  367. if (dd->last_rounded_rate == 0)
  368. return -EINVAL;
  369. /* No freqsel on OMAP4 and OMAP3630 */
  370. if (!cpu_is_omap44xx() && !cpu_is_omap3630()) {
  371. freqsel = _omap3_dpll_compute_freqsel(clk,
  372. dd->last_rounded_n);
  373. if (!freqsel)
  374. WARN_ON(1);
  375. }
  376. pr_debug("clock: %s: set rate: locking rate to %lu.\n",
  377. clk->name, rate);
  378. ret = omap3_noncore_dpll_program(clk, dd->last_rounded_m,
  379. dd->last_rounded_n, freqsel);
  380. if (!ret)
  381. new_parent = dd->clk_ref;
  382. }
  383. if (!ret) {
  384. /*
  385. * Switch the parent clock in the hierarchy, and make sure
  386. * that the new parent's usecount is correct. Note: we
  387. * enable the new parent before disabling the old to avoid
  388. * any unnecessary hardware disable->enable transitions.
  389. */
  390. if (clk->usecount) {
  391. omap2_clk_enable(new_parent);
  392. omap2_clk_disable(clk->parent);
  393. }
  394. clk_reparent(clk, new_parent);
  395. clk->rate = rate;
  396. }
  397. omap2_clk_disable(dd->clk_ref);
  398. omap2_clk_disable(dd->clk_bypass);
  399. return 0;
  400. }
  401. /* DPLL autoidle read/set code */
  402. /**
  403. * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
  404. * @clk: struct clk * of the DPLL to read
  405. *
  406. * Return the DPLL's autoidle bits, shifted down to bit 0. Returns
  407. * -EINVAL if passed a null pointer or if the struct clk does not
  408. * appear to refer to a DPLL.
  409. */
  410. u32 omap3_dpll_autoidle_read(struct clk *clk)
  411. {
  412. const struct dpll_data *dd;
  413. u32 v;
  414. if (!clk || !clk->dpll_data)
  415. return -EINVAL;
  416. dd = clk->dpll_data;
  417. v = __raw_readl(dd->autoidle_reg);
  418. v &= dd->autoidle_mask;
  419. v >>= __ffs(dd->autoidle_mask);
  420. return v;
  421. }
  422. /**
  423. * omap3_dpll_allow_idle - enable DPLL autoidle bits
  424. * @clk: struct clk * of the DPLL to operate on
  425. *
  426. * Enable DPLL automatic idle control. This automatic idle mode
  427. * switching takes effect only when the DPLL is locked, at least on
  428. * OMAP3430. The DPLL will enter low-power stop when its downstream
  429. * clocks are gated. No return value.
  430. */
  431. void omap3_dpll_allow_idle(struct clk *clk)
  432. {
  433. const struct dpll_data *dd;
  434. u32 v;
  435. if (!clk || !clk->dpll_data)
  436. return;
  437. dd = clk->dpll_data;
  438. /*
  439. * REVISIT: CORE DPLL can optionally enter low-power bypass
  440. * by writing 0x5 instead of 0x1. Add some mechanism to
  441. * optionally enter this mode.
  442. */
  443. v = __raw_readl(dd->autoidle_reg);
  444. v &= ~dd->autoidle_mask;
  445. v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask);
  446. __raw_writel(v, dd->autoidle_reg);
  447. }
  448. /**
  449. * omap3_dpll_deny_idle - prevent DPLL from automatically idling
  450. * @clk: struct clk * of the DPLL to operate on
  451. *
  452. * Disable DPLL automatic idle control. No return value.
  453. */
  454. void omap3_dpll_deny_idle(struct clk *clk)
  455. {
  456. const struct dpll_data *dd;
  457. u32 v;
  458. if (!clk || !clk->dpll_data)
  459. return;
  460. dd = clk->dpll_data;
  461. v = __raw_readl(dd->autoidle_reg);
  462. v &= ~dd->autoidle_mask;
  463. v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask);
  464. __raw_writel(v, dd->autoidle_reg);
  465. }
  466. /* Clock control for DPLL outputs */
  467. /**
  468. * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
  469. * @clk: DPLL output struct clk
  470. *
  471. * Using parent clock DPLL data, look up DPLL state. If locked, set our
  472. * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
  473. */
  474. unsigned long omap3_clkoutx2_recalc(struct clk *clk)
  475. {
  476. const struct dpll_data *dd;
  477. unsigned long rate;
  478. u32 v;
  479. struct clk *pclk;
  480. /* Walk up the parents of clk, looking for a DPLL */
  481. pclk = clk->parent;
  482. while (pclk && !pclk->dpll_data)
  483. pclk = pclk->parent;
  484. /* clk does not have a DPLL as a parent? */
  485. WARN_ON(!pclk);
  486. dd = pclk->dpll_data;
  487. WARN_ON(!dd->enable_mask);
  488. v = __raw_readl(dd->control_reg) & dd->enable_mask;
  489. v >>= __ffs(dd->enable_mask);
  490. if ((v != OMAP3XXX_EN_DPLL_LOCKED) || (dd->flags & DPLL_J_TYPE))
  491. rate = clk->parent->rate;
  492. else
  493. rate = clk->parent->rate * 2;
  494. return rate;
  495. }