dpll3xxx.c 15 KB

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