dpll3xxx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 <linux/clkdev.h>
  29. #include "soc.h"
  30. #include "clockdomain.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. #ifdef CONFIG_COMMON_CLK
  41. static void _omap3_dpll_write_clken(struct clk_hw_omap *clk, u8 clken_bits)
  42. #else
  43. static void _omap3_dpll_write_clken(struct clk *clk, u8 clken_bits)
  44. #endif
  45. {
  46. const struct dpll_data *dd;
  47. u32 v;
  48. dd = clk->dpll_data;
  49. v = __raw_readl(dd->control_reg);
  50. v &= ~dd->enable_mask;
  51. v |= clken_bits << __ffs(dd->enable_mask);
  52. __raw_writel(v, dd->control_reg);
  53. }
  54. /* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */
  55. #ifdef CONFIG_COMMON_CLK
  56. static int _omap3_wait_dpll_status(struct clk_hw_omap *clk, u8 state)
  57. #else
  58. static int _omap3_wait_dpll_status(struct clk *clk, u8 state)
  59. #endif
  60. {
  61. const struct dpll_data *dd;
  62. int i = 0;
  63. int ret = -EINVAL;
  64. const char *clk_name;
  65. dd = clk->dpll_data;
  66. #ifdef CONFIG_COMMON_CLK
  67. clk_name = __clk_get_name(clk->hw.clk);
  68. #else
  69. clk_name = __clk_get_name(clk);
  70. #endif
  71. state <<= __ffs(dd->idlest_mask);
  72. while (((__raw_readl(dd->idlest_reg) & dd->idlest_mask) != state) &&
  73. i < MAX_DPLL_WAIT_TRIES) {
  74. i++;
  75. udelay(1);
  76. }
  77. if (i == MAX_DPLL_WAIT_TRIES) {
  78. printk(KERN_ERR "clock: %s failed transition to '%s'\n",
  79. clk_name, (state) ? "locked" : "bypassed");
  80. } else {
  81. pr_debug("clock: %s transition to '%s' in %d loops\n",
  82. clk_name, (state) ? "locked" : "bypassed", i);
  83. ret = 0;
  84. }
  85. return ret;
  86. }
  87. /* From 3430 TRM ES2 4.7.6.2 */
  88. #ifdef CONFIG_COMMON_CLK
  89. static u16 _omap3_dpll_compute_freqsel(struct clk_hw_omap *clk, u8 n)
  90. #else
  91. static u16 _omap3_dpll_compute_freqsel(struct clk *clk, u8 n)
  92. #endif
  93. {
  94. unsigned long fint;
  95. u16 f = 0;
  96. fint = __clk_get_rate(clk->dpll_data->clk_ref) / n;
  97. pr_debug("clock: fint is %lu\n", fint);
  98. if (fint >= 750000 && fint <= 1000000)
  99. f = 0x3;
  100. else if (fint > 1000000 && fint <= 1250000)
  101. f = 0x4;
  102. else if (fint > 1250000 && fint <= 1500000)
  103. f = 0x5;
  104. else if (fint > 1500000 && fint <= 1750000)
  105. f = 0x6;
  106. else if (fint > 1750000 && fint <= 2100000)
  107. f = 0x7;
  108. else if (fint > 7500000 && fint <= 10000000)
  109. f = 0xB;
  110. else if (fint > 10000000 && fint <= 12500000)
  111. f = 0xC;
  112. else if (fint > 12500000 && fint <= 15000000)
  113. f = 0xD;
  114. else if (fint > 15000000 && fint <= 17500000)
  115. f = 0xE;
  116. else if (fint > 17500000 && fint <= 21000000)
  117. f = 0xF;
  118. else
  119. pr_debug("clock: unknown freqsel setting for %d\n", n);
  120. return f;
  121. }
  122. /*
  123. * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
  124. * @clk: pointer to a DPLL struct clk
  125. *
  126. * Instructs a non-CORE DPLL to lock. Waits for the DPLL to report
  127. * readiness before returning. Will save and restore the DPLL's
  128. * autoidle state across the enable, per the CDP code. If the DPLL
  129. * locked successfully, return 0; if the DPLL did not lock in the time
  130. * allotted, or DPLL3 was passed in, return -EINVAL.
  131. */
  132. #ifdef CONFIG_COMMON_CLK
  133. static int _omap3_noncore_dpll_lock(struct clk_hw_omap *clk)
  134. #else
  135. static int _omap3_noncore_dpll_lock(struct clk *clk)
  136. #endif
  137. {
  138. const struct dpll_data *dd;
  139. u8 ai;
  140. u8 state = 1;
  141. int r = 0;
  142. #ifdef CONFIG_COMMON_CLK
  143. pr_debug("clock: locking DPLL %s\n", __clk_get_name(clk->hw.clk));
  144. #else
  145. pr_debug("clock: locking DPLL %s\n", __clk_get_name(clk));
  146. #endif
  147. dd = clk->dpll_data;
  148. state <<= __ffs(dd->idlest_mask);
  149. /* Check if already locked */
  150. if ((__raw_readl(dd->idlest_reg) & dd->idlest_mask) == state)
  151. goto done;
  152. ai = omap3_dpll_autoidle_read(clk);
  153. if (ai)
  154. omap3_dpll_deny_idle(clk);
  155. _omap3_dpll_write_clken(clk, DPLL_LOCKED);
  156. r = _omap3_wait_dpll_status(clk, 1);
  157. if (ai)
  158. omap3_dpll_allow_idle(clk);
  159. done:
  160. return r;
  161. }
  162. /*
  163. * _omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
  164. * @clk: pointer to a DPLL struct clk
  165. *
  166. * Instructs a non-CORE DPLL to enter low-power bypass mode. In
  167. * bypass mode, the DPLL's rate is set equal to its parent clock's
  168. * rate. Waits for the DPLL to report readiness before returning.
  169. * Will save and restore the DPLL's autoidle state across the enable,
  170. * per the CDP code. If the DPLL entered bypass mode successfully,
  171. * return 0; if the DPLL did not enter bypass in the time allotted, or
  172. * DPLL3 was passed in, or the DPLL does not support low-power bypass,
  173. * return -EINVAL.
  174. */
  175. #ifdef CONFIG_COMMON_CLK
  176. static int _omap3_noncore_dpll_bypass(struct clk_hw_omap *clk)
  177. #else
  178. static int _omap3_noncore_dpll_bypass(struct clk *clk)
  179. #endif
  180. {
  181. int r;
  182. u8 ai;
  183. if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
  184. return -EINVAL;
  185. pr_debug("clock: configuring DPLL %s for low-power bypass\n",
  186. #ifdef CONFIG_COMMON_CLK
  187. __clk_get_name(clk->hw.clk));
  188. #else
  189. __clk_get_name(clk));
  190. #endif
  191. ai = omap3_dpll_autoidle_read(clk);
  192. _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
  193. r = _omap3_wait_dpll_status(clk, 0);
  194. if (ai)
  195. omap3_dpll_allow_idle(clk);
  196. return r;
  197. }
  198. /*
  199. * _omap3_noncore_dpll_stop - instruct a DPLL to stop
  200. * @clk: pointer to a DPLL struct clk
  201. *
  202. * Instructs a non-CORE DPLL to enter low-power stop. Will save and
  203. * restore the DPLL's autoidle state across the stop, per the CDP
  204. * code. If DPLL3 was passed in, or the DPLL does not support
  205. * low-power stop, return -EINVAL; otherwise, return 0.
  206. */
  207. #ifdef CONFIG_COMMON_CLK
  208. static int _omap3_noncore_dpll_stop(struct clk_hw_omap *clk)
  209. #else
  210. static int _omap3_noncore_dpll_stop(struct clk *clk)
  211. #endif
  212. {
  213. u8 ai;
  214. if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
  215. return -EINVAL;
  216. #ifdef CONFIG_COMMON_CLK
  217. pr_debug("clock: stopping DPLL %s\n", __clk_get_name(clk->hw.clk));
  218. #else
  219. pr_debug("clock: stopping DPLL %s\n", __clk_get_name(clk));
  220. #endif
  221. ai = omap3_dpll_autoidle_read(clk);
  222. _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
  223. if (ai)
  224. omap3_dpll_allow_idle(clk);
  225. return 0;
  226. }
  227. /**
  228. * _lookup_dco - Lookup DCO used by j-type DPLL
  229. * @clk: pointer to a DPLL struct clk
  230. * @dco: digital control oscillator selector
  231. * @m: DPLL multiplier to set
  232. * @n: DPLL divider to set
  233. *
  234. * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)"
  235. *
  236. * XXX This code is not needed for 3430/AM35xx; can it be optimized
  237. * out in non-multi-OMAP builds for those chips?
  238. */
  239. #ifdef CONFIG_COMMON_CLK
  240. static void _lookup_dco(struct clk_hw_omap *clk, u8 *dco, u16 m, u8 n)
  241. #else
  242. static void _lookup_dco(struct clk *clk, u8 *dco, u16 m, u8 n)
  243. #endif
  244. {
  245. unsigned long fint, clkinp; /* watch out for overflow */
  246. #ifdef CONFIG_COMMON_CLK
  247. clkinp = __clk_get_rate(__clk_get_parent(clk->hw.clk));
  248. #else
  249. clkinp = __clk_get_rate(__clk_get_parent(clk));
  250. #endif
  251. fint = (clkinp / n) * m;
  252. if (fint < 1000000000)
  253. *dco = 2;
  254. else
  255. *dco = 4;
  256. }
  257. /**
  258. * _lookup_sddiv - Calculate sigma delta divider for j-type DPLL
  259. * @clk: pointer to a DPLL struct clk
  260. * @sd_div: target sigma-delta divider
  261. * @m: DPLL multiplier to set
  262. * @n: DPLL divider to set
  263. *
  264. * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)"
  265. *
  266. * XXX This code is not needed for 3430/AM35xx; can it be optimized
  267. * out in non-multi-OMAP builds for those chips?
  268. */
  269. #ifdef CONFIG_COMMON_CLK
  270. static void _lookup_sddiv(struct clk_hw_omap *clk, u8 *sd_div, u16 m, u8 n)
  271. #else
  272. static void _lookup_sddiv(struct clk *clk, u8 *sd_div, u16 m, u8 n)
  273. #endif
  274. {
  275. unsigned long clkinp, sd; /* watch out for overflow */
  276. int mod1, mod2;
  277. #ifdef CONFIG_COMMON_CLK
  278. clkinp = __clk_get_rate(__clk_get_parent(clk->hw.clk));
  279. #else
  280. clkinp = __clk_get_rate(__clk_get_parent(clk));
  281. #endif
  282. /*
  283. * target sigma-delta to near 250MHz
  284. * sd = ceil[(m/(n+1)) * (clkinp_MHz / 250)]
  285. */
  286. clkinp /= 100000; /* shift from MHz to 10*Hz for 38.4 and 19.2 */
  287. mod1 = (clkinp * m) % (250 * n);
  288. sd = (clkinp * m) / (250 * n);
  289. mod2 = sd % 10;
  290. sd /= 10;
  291. if (mod1 || mod2)
  292. sd++;
  293. *sd_div = sd;
  294. }
  295. /*
  296. * _omap3_noncore_dpll_program - set non-core DPLL M,N values directly
  297. * @clk: struct clk * of DPLL to set
  298. * @m: DPLL multiplier to set
  299. * @n: DPLL divider to set
  300. * @freqsel: FREQSEL value to set
  301. *
  302. * Program the DPLL with the supplied M, N values, and wait for the DPLL to
  303. * lock.. Returns -EINVAL upon error, or 0 upon success.
  304. */
  305. #ifdef CONFIG_COMMON_CLK
  306. static int omap3_noncore_dpll_program(struct clk_hw_omap *clk, u16 m, u8 n,
  307. u16 freqsel)
  308. #else
  309. static int omap3_noncore_dpll_program(struct clk *clk, u16 m, u8 n, u16 freqsel)
  310. #endif
  311. {
  312. struct dpll_data *dd = clk->dpll_data;
  313. u8 dco, sd_div;
  314. u32 v;
  315. /* 3430 ES2 TRM: 4.7.6.9 DPLL Programming Sequence */
  316. _omap3_noncore_dpll_bypass(clk);
  317. /*
  318. * Set jitter correction. No jitter correction for OMAP4 and 3630
  319. * since freqsel field is no longer present
  320. */
  321. if (!soc_is_am33xx() && !cpu_is_omap44xx() && !cpu_is_omap3630()) {
  322. v = __raw_readl(dd->control_reg);
  323. v &= ~dd->freqsel_mask;
  324. v |= freqsel << __ffs(dd->freqsel_mask);
  325. __raw_writel(v, dd->control_reg);
  326. }
  327. /* Set DPLL multiplier, divider */
  328. v = __raw_readl(dd->mult_div1_reg);
  329. v &= ~(dd->mult_mask | dd->div1_mask);
  330. v |= m << __ffs(dd->mult_mask);
  331. v |= (n - 1) << __ffs(dd->div1_mask);
  332. /* Configure dco and sd_div for dplls that have these fields */
  333. if (dd->dco_mask) {
  334. _lookup_dco(clk, &dco, m, n);
  335. v &= ~(dd->dco_mask);
  336. v |= dco << __ffs(dd->dco_mask);
  337. }
  338. if (dd->sddiv_mask) {
  339. _lookup_sddiv(clk, &sd_div, m, n);
  340. v &= ~(dd->sddiv_mask);
  341. v |= sd_div << __ffs(dd->sddiv_mask);
  342. }
  343. __raw_writel(v, dd->mult_div1_reg);
  344. /* We let the clock framework set the other output dividers later */
  345. /* REVISIT: Set ramp-up delay? */
  346. _omap3_noncore_dpll_lock(clk);
  347. return 0;
  348. }
  349. /* Public functions */
  350. /**
  351. * omap3_dpll_recalc - recalculate DPLL rate
  352. * @clk: DPLL struct clk
  353. *
  354. * Recalculate and propagate the DPLL rate.
  355. */
  356. #ifdef CONFIG_COMMON_CLK
  357. unsigned long omap3_dpll_recalc(struct clk_hw *hw, unsigned long parent_rate)
  358. {
  359. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  360. #else
  361. unsigned long omap3_dpll_recalc(struct clk *clk)
  362. {
  363. #endif
  364. return omap2_get_dpll_rate(clk);
  365. }
  366. /* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
  367. /**
  368. * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
  369. * @clk: pointer to a DPLL struct clk
  370. *
  371. * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
  372. * The choice of modes depends on the DPLL's programmed rate: if it is
  373. * the same as the DPLL's parent clock, it will enter bypass;
  374. * otherwise, it will enter lock. This code will wait for the DPLL to
  375. * indicate readiness before returning, unless the DPLL takes too long
  376. * to enter the target state. Intended to be used as the struct clk's
  377. * enable function. If DPLL3 was passed in, or the DPLL does not
  378. * support low-power stop, or if the DPLL took too long to enter
  379. * bypass or lock, return -EINVAL; otherwise, return 0.
  380. */
  381. #ifdef CONFIG_COMMON_CLK
  382. int omap3_noncore_dpll_enable(struct clk_hw *hw)
  383. {
  384. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  385. #else
  386. int omap3_noncore_dpll_enable(struct clk *clk)
  387. {
  388. #endif
  389. int r;
  390. struct dpll_data *dd;
  391. struct clk *parent;
  392. dd = clk->dpll_data;
  393. if (!dd)
  394. return -EINVAL;
  395. #ifdef CONFIG_COMMON_CLK
  396. if (clk->clkdm) {
  397. r = clkdm_clk_enable(clk->clkdm, hw->clk);
  398. if (r) {
  399. WARN(1,
  400. "%s: could not enable %s's clockdomain %s: %d\n",
  401. __func__, __clk_get_name(hw->clk),
  402. clk->clkdm->name, r);
  403. return r;
  404. }
  405. }
  406. parent = __clk_get_parent(hw->clk);
  407. if (__clk_get_rate(hw->clk) == __clk_get_rate(dd->clk_bypass)) {
  408. #else
  409. parent = __clk_get_parent(clk);
  410. if (__clk_get_rate(clk) == __clk_get_rate(dd->clk_bypass)) {
  411. #endif
  412. WARN_ON(parent != dd->clk_bypass);
  413. r = _omap3_noncore_dpll_bypass(clk);
  414. } else {
  415. WARN_ON(parent != dd->clk_ref);
  416. r = _omap3_noncore_dpll_lock(clk);
  417. }
  418. #ifndef CONFIG_COMMON_CLK
  419. /*
  420. *FIXME: this is dubious - if clk->rate has changed, what about
  421. * propagating?
  422. */
  423. if (!r)
  424. clk->rate = (clk->recalc) ? clk->recalc(clk) :
  425. omap2_get_dpll_rate(clk);
  426. #endif
  427. return r;
  428. }
  429. /**
  430. * omap3_noncore_dpll_disable - instruct a DPLL to enter low-power stop
  431. * @clk: pointer to a DPLL struct clk
  432. *
  433. * Instructs a non-CORE DPLL to enter low-power stop. This function is
  434. * intended for use in struct clkops. No return value.
  435. */
  436. #ifdef CONFIG_COMMON_CLK
  437. void omap3_noncore_dpll_disable(struct clk_hw *hw)
  438. {
  439. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  440. _omap3_noncore_dpll_stop(clk);
  441. if (clk->clkdm)
  442. clkdm_clk_disable(clk->clkdm, hw->clk);
  443. #else
  444. void omap3_noncore_dpll_disable(struct clk *clk)
  445. {
  446. _omap3_noncore_dpll_stop(clk);
  447. if (clk->clkdm)
  448. clkdm_clk_disable(clk->clkdm, clk);
  449. #endif
  450. }
  451. /* Non-CORE DPLL rate set code */
  452. /**
  453. * omap3_noncore_dpll_set_rate - set non-core DPLL rate
  454. * @clk: struct clk * of DPLL to set
  455. * @rate: rounded target rate
  456. *
  457. * Set the DPLL CLKOUT to the target rate. If the DPLL can enter
  458. * low-power bypass, and the target rate is the bypass source clock
  459. * rate, then configure the DPLL for bypass. Otherwise, round the
  460. * target rate if it hasn't been done already, then program and lock
  461. * the DPLL. Returns -EINVAL upon error, or 0 upon success.
  462. */
  463. #ifdef CONFIG_COMMON_CLK
  464. int omap3_noncore_dpll_set_rate(struct clk_hw *hw, unsigned long rate,
  465. unsigned long parent_rate)
  466. {
  467. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  468. struct clk *new_parent = NULL;
  469. u16 freqsel = 0;
  470. struct dpll_data *dd;
  471. int ret;
  472. if (!hw || !rate)
  473. return -EINVAL;
  474. dd = clk->dpll_data;
  475. if (!dd)
  476. return -EINVAL;
  477. __clk_prepare(dd->clk_bypass);
  478. clk_enable(dd->clk_bypass);
  479. __clk_prepare(dd->clk_ref);
  480. clk_enable(dd->clk_ref);
  481. if (__clk_get_rate(dd->clk_bypass) == rate &&
  482. (dd->modes & (1 << DPLL_LOW_POWER_BYPASS))) {
  483. pr_debug("%s: %s: set rate: entering bypass.\n",
  484. __func__, __clk_get_name(hw->clk));
  485. ret = _omap3_noncore_dpll_bypass(clk);
  486. if (!ret)
  487. new_parent = dd->clk_bypass;
  488. } else {
  489. if (dd->last_rounded_rate != rate)
  490. rate = __clk_round_rate(hw->clk, rate);
  491. if (dd->last_rounded_rate == 0)
  492. return -EINVAL;
  493. /* No freqsel on OMAP4 and OMAP3630 */
  494. if (!cpu_is_omap44xx() && !cpu_is_omap3630()) {
  495. freqsel = _omap3_dpll_compute_freqsel(clk,
  496. dd->last_rounded_n);
  497. if (!freqsel)
  498. WARN_ON(1);
  499. }
  500. pr_debug("%s: %s: set rate: locking rate to %lu.\n",
  501. __func__, __clk_get_name(hw->clk), rate);
  502. ret = omap3_noncore_dpll_program(clk, dd->last_rounded_m,
  503. dd->last_rounded_n, freqsel);
  504. if (!ret)
  505. new_parent = dd->clk_ref;
  506. }
  507. /*
  508. * FIXME - this is all wrong. common code handles reparenting and
  509. * migrating prepare/enable counts. dplls should be a multiplexer
  510. * clock and this should be a set_parent operation so that all of that
  511. * stuff is inherited for free
  512. */
  513. if (!ret)
  514. __clk_reparent(hw->clk, new_parent);
  515. clk_disable(dd->clk_ref);
  516. __clk_unprepare(dd->clk_ref);
  517. clk_disable(dd->clk_bypass);
  518. __clk_unprepare(dd->clk_bypass);
  519. return 0;
  520. }
  521. #else
  522. int omap3_noncore_dpll_set_rate(struct clk *clk, unsigned long rate)
  523. {
  524. struct clk *new_parent = NULL;
  525. unsigned long hw_rate, bypass_rate;
  526. u16 freqsel = 0;
  527. struct dpll_data *dd;
  528. int ret;
  529. if (!clk || !rate)
  530. return -EINVAL;
  531. dd = clk->dpll_data;
  532. if (!dd)
  533. return -EINVAL;
  534. hw_rate = (clk->recalc) ? clk->recalc(clk) : omap2_get_dpll_rate(clk);
  535. if (rate == hw_rate)
  536. return 0;
  537. /*
  538. * Ensure both the bypass and ref clocks are enabled prior to
  539. * doing anything; we need the bypass clock running to reprogram
  540. * the DPLL.
  541. */
  542. omap2_clk_enable(dd->clk_bypass);
  543. omap2_clk_enable(dd->clk_ref);
  544. bypass_rate = __clk_get_rate(dd->clk_bypass);
  545. if (bypass_rate == rate &&
  546. (clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS))) {
  547. pr_debug("clock: %s: set rate: entering bypass.\n", clk->name);
  548. ret = _omap3_noncore_dpll_bypass(clk);
  549. if (!ret)
  550. new_parent = dd->clk_bypass;
  551. } else {
  552. if (dd->last_rounded_rate != rate)
  553. rate = clk->round_rate(clk, rate);
  554. if (dd->last_rounded_rate == 0)
  555. return -EINVAL;
  556. /* No freqsel on OMAP4 and OMAP3630 */
  557. if (!soc_is_am33xx() && !cpu_is_omap44xx() && !cpu_is_omap3630()) {
  558. freqsel = _omap3_dpll_compute_freqsel(clk,
  559. dd->last_rounded_n);
  560. if (!freqsel)
  561. WARN_ON(1);
  562. }
  563. pr_debug("clock: %s: set rate: locking rate to %lu.\n",
  564. __clk_get_name(clk), rate);
  565. ret = omap3_noncore_dpll_program(clk, dd->last_rounded_m,
  566. dd->last_rounded_n, freqsel);
  567. if (!ret)
  568. new_parent = dd->clk_ref;
  569. }
  570. if (!ret) {
  571. /*
  572. * Switch the parent clock in the hierarchy, and make sure
  573. * that the new parent's usecount is correct. Note: we
  574. * enable the new parent before disabling the old to avoid
  575. * any unnecessary hardware disable->enable transitions.
  576. */
  577. if (clk->usecount) {
  578. omap2_clk_enable(new_parent);
  579. omap2_clk_disable(clk->parent);
  580. }
  581. clk_reparent(clk, new_parent);
  582. clk->rate = rate;
  583. }
  584. omap2_clk_disable(dd->clk_ref);
  585. omap2_clk_disable(dd->clk_bypass);
  586. return 0;
  587. }
  588. #endif
  589. /* DPLL autoidle read/set code */
  590. /**
  591. * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
  592. * @clk: struct clk * of the DPLL to read
  593. *
  594. * Return the DPLL's autoidle bits, shifted down to bit 0. Returns
  595. * -EINVAL if passed a null pointer or if the struct clk does not
  596. * appear to refer to a DPLL.
  597. */
  598. #ifdef CONFIG_COMMON_CLK
  599. u32 omap3_dpll_autoidle_read(struct clk_hw_omap *clk)
  600. #else
  601. u32 omap3_dpll_autoidle_read(struct clk *clk)
  602. #endif
  603. {
  604. const struct dpll_data *dd;
  605. u32 v;
  606. if (!clk || !clk->dpll_data)
  607. return -EINVAL;
  608. dd = clk->dpll_data;
  609. if (!dd->autoidle_reg)
  610. return -EINVAL;
  611. v = __raw_readl(dd->autoidle_reg);
  612. v &= dd->autoidle_mask;
  613. v >>= __ffs(dd->autoidle_mask);
  614. return v;
  615. }
  616. /**
  617. * omap3_dpll_allow_idle - enable DPLL autoidle bits
  618. * @clk: struct clk * of the DPLL to operate on
  619. *
  620. * Enable DPLL automatic idle control. This automatic idle mode
  621. * switching takes effect only when the DPLL is locked, at least on
  622. * OMAP3430. The DPLL will enter low-power stop when its downstream
  623. * clocks are gated. No return value.
  624. */
  625. #ifdef CONFIG_COMMON_CLK
  626. void omap3_dpll_allow_idle(struct clk_hw_omap *clk)
  627. #else
  628. void omap3_dpll_allow_idle(struct clk *clk)
  629. #endif
  630. {
  631. const struct dpll_data *dd;
  632. u32 v;
  633. if (!clk || !clk->dpll_data)
  634. return;
  635. dd = clk->dpll_data;
  636. if (!dd->autoidle_reg) {
  637. #ifndef CONFIG_COMMON_CLK
  638. pr_debug("clock: DPLL %s: autoidle not supported\n",
  639. __clk_get_name(clk));
  640. #endif
  641. return;
  642. }
  643. /*
  644. * REVISIT: CORE DPLL can optionally enter low-power bypass
  645. * by writing 0x5 instead of 0x1. Add some mechanism to
  646. * optionally enter this mode.
  647. */
  648. v = __raw_readl(dd->autoidle_reg);
  649. v &= ~dd->autoidle_mask;
  650. v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask);
  651. __raw_writel(v, dd->autoidle_reg);
  652. }
  653. /**
  654. * omap3_dpll_deny_idle - prevent DPLL from automatically idling
  655. * @clk: struct clk * of the DPLL to operate on
  656. *
  657. * Disable DPLL automatic idle control. No return value.
  658. */
  659. #ifdef CONFIG_COMMON_CLK
  660. void omap3_dpll_deny_idle(struct clk_hw_omap *clk)
  661. #else
  662. void omap3_dpll_deny_idle(struct clk *clk)
  663. #endif
  664. {
  665. const struct dpll_data *dd;
  666. u32 v;
  667. if (!clk || !clk->dpll_data)
  668. return;
  669. dd = clk->dpll_data;
  670. if (!dd->autoidle_reg) {
  671. #ifndef CONFIG_COMMON_CLK
  672. pr_debug("clock: DPLL %s: autoidle not supported\n",
  673. __clk_get_name(clk));
  674. #endif
  675. return;
  676. }
  677. v = __raw_readl(dd->autoidle_reg);
  678. v &= ~dd->autoidle_mask;
  679. v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask);
  680. __raw_writel(v, dd->autoidle_reg);
  681. }
  682. /* Clock control for DPLL outputs */
  683. /**
  684. * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
  685. * @clk: DPLL output struct clk
  686. *
  687. * Using parent clock DPLL data, look up DPLL state. If locked, set our
  688. * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
  689. */
  690. #ifdef CONFIG_COMMON_CLK
  691. unsigned long omap3_clkoutx2_recalc(struct clk_hw *hw,
  692. unsigned long parent_rate)
  693. {
  694. const struct dpll_data *dd;
  695. unsigned long rate;
  696. u32 v;
  697. struct clk_hw_omap *pclk = NULL;
  698. struct clk *parent;
  699. /* Walk up the parents of clk, looking for a DPLL */
  700. do {
  701. do {
  702. parent = __clk_get_parent(hw->clk);
  703. hw = __clk_get_hw(parent);
  704. } while (hw && (__clk_get_flags(hw->clk) & CLK_IS_BASIC));
  705. if (!hw)
  706. break;
  707. pclk = to_clk_hw_omap(hw);
  708. } while (pclk && !pclk->dpll_data);
  709. #else
  710. unsigned long omap3_clkoutx2_recalc(struct clk *clk)
  711. {
  712. const struct dpll_data *dd;
  713. unsigned long rate;
  714. u32 v;
  715. struct clk *pclk;
  716. unsigned long parent_rate;
  717. /* Walk up the parents of clk, looking for a DPLL */
  718. pclk = __clk_get_parent(clk);
  719. while (pclk && !pclk->dpll_data)
  720. pclk = __clk_get_parent(pclk);
  721. parent_rate = __clk_get_rate(__clk_get_parent(clk));
  722. #endif
  723. /* clk does not have a DPLL as a parent? error in the clock data */
  724. if (!pclk) {
  725. WARN_ON(1);
  726. return 0;
  727. }
  728. dd = pclk->dpll_data;
  729. WARN_ON(!dd->enable_mask);
  730. v = __raw_readl(dd->control_reg) & dd->enable_mask;
  731. v >>= __ffs(dd->enable_mask);
  732. if ((v != OMAP3XXX_EN_DPLL_LOCKED) || (dd->flags & DPLL_J_TYPE))
  733. rate = parent_rate;
  734. else
  735. rate = parent_rate * 2;
  736. return rate;
  737. }
  738. /* OMAP3/4 non-CORE DPLL clkops */
  739. #ifdef CONFIG_COMMON_CLK
  740. const struct clk_hw_omap_ops clkhwops_omap3_dpll = {
  741. .allow_idle = omap3_dpll_allow_idle,
  742. .deny_idle = omap3_dpll_deny_idle,
  743. };
  744. #else
  745. const struct clkops clkops_omap3_noncore_dpll_ops = {
  746. .enable = omap3_noncore_dpll_enable,
  747. .disable = omap3_noncore_dpll_disable,
  748. .allow_idle = omap3_dpll_allow_idle,
  749. .deny_idle = omap3_dpll_deny_idle,
  750. };
  751. const struct clkops clkops_omap3_core_dpll_ops = {
  752. .allow_idle = omap3_dpll_allow_idle,
  753. .deny_idle = omap3_dpll_deny_idle,
  754. };
  755. #endif