dpll3xxx.c 23 KB

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