dpll3xxx.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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_hw_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, ai = 0;
  254. u32 v;
  255. bool errata_i810;
  256. /* 3430 ES2 TRM: 4.7.6.9 DPLL Programming Sequence */
  257. _omap3_noncore_dpll_bypass(clk);
  258. /*
  259. * Set jitter correction. Jitter correction applicable for OMAP343X
  260. * only since freqsel field is no longer present on other devices.
  261. */
  262. if (ti_clk_get_features()->flags & TI_CLK_DPLL_HAS_FREQSEL) {
  263. v = ti_clk_ll_ops->clk_readl(dd->control_reg);
  264. v &= ~dd->freqsel_mask;
  265. v |= freqsel << __ffs(dd->freqsel_mask);
  266. ti_clk_ll_ops->clk_writel(v, dd->control_reg);
  267. }
  268. /* Set DPLL multiplier, divider */
  269. v = ti_clk_ll_ops->clk_readl(dd->mult_div1_reg);
  270. /* Handle Duty Cycle Correction */
  271. if (dd->dcc_mask) {
  272. if (dd->last_rounded_rate >= dd->dcc_rate)
  273. v |= dd->dcc_mask; /* Enable DCC */
  274. else
  275. v &= ~dd->dcc_mask; /* Disable DCC */
  276. }
  277. v &= ~(dd->mult_mask | dd->div1_mask);
  278. v |= dd->last_rounded_m << __ffs(dd->mult_mask);
  279. v |= (dd->last_rounded_n - 1) << __ffs(dd->div1_mask);
  280. /* Configure dco and sd_div for dplls that have these fields */
  281. if (dd->dco_mask) {
  282. _lookup_dco(clk, &dco, dd->last_rounded_m, dd->last_rounded_n);
  283. v &= ~(dd->dco_mask);
  284. v |= dco << __ffs(dd->dco_mask);
  285. }
  286. if (dd->sddiv_mask) {
  287. _lookup_sddiv(clk, &sd_div, dd->last_rounded_m,
  288. dd->last_rounded_n);
  289. v &= ~(dd->sddiv_mask);
  290. v |= sd_div << __ffs(dd->sddiv_mask);
  291. }
  292. /*
  293. * Errata i810 - DPLL controller can get stuck while transitioning
  294. * to a power saving state. Software must ensure the DPLL can not
  295. * transition to a low power state while changing M/N values.
  296. * Easiest way to accomplish this is to prevent DPLL autoidle
  297. * before doing the M/N re-program.
  298. */
  299. errata_i810 = ti_clk_get_features()->flags & TI_CLK_ERRATA_I810;
  300. if (errata_i810) {
  301. ai = omap3_dpll_autoidle_read(clk);
  302. if (ai) {
  303. omap3_dpll_deny_idle(clk);
  304. /* OCP barrier */
  305. omap3_dpll_autoidle_read(clk);
  306. }
  307. }
  308. ti_clk_ll_ops->clk_writel(v, dd->mult_div1_reg);
  309. /* Set 4X multiplier and low-power mode */
  310. if (dd->m4xen_mask || dd->lpmode_mask) {
  311. v = ti_clk_ll_ops->clk_readl(dd->control_reg);
  312. if (dd->m4xen_mask) {
  313. if (dd->last_rounded_m4xen)
  314. v |= dd->m4xen_mask;
  315. else
  316. v &= ~dd->m4xen_mask;
  317. }
  318. if (dd->lpmode_mask) {
  319. if (dd->last_rounded_lpmode)
  320. v |= dd->lpmode_mask;
  321. else
  322. v &= ~dd->lpmode_mask;
  323. }
  324. ti_clk_ll_ops->clk_writel(v, dd->control_reg);
  325. }
  326. /* We let the clock framework set the other output dividers later */
  327. /* REVISIT: Set ramp-up delay? */
  328. _omap3_noncore_dpll_lock(clk);
  329. if (errata_i810 && ai)
  330. omap3_dpll_allow_idle(clk);
  331. return 0;
  332. }
  333. /* Public functions */
  334. /**
  335. * omap3_dpll_recalc - recalculate DPLL rate
  336. * @clk: DPLL struct clk
  337. *
  338. * Recalculate and propagate the DPLL rate.
  339. */
  340. unsigned long omap3_dpll_recalc(struct clk_hw *hw, unsigned long parent_rate)
  341. {
  342. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  343. return omap2_get_dpll_rate(clk);
  344. }
  345. /* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
  346. /**
  347. * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
  348. * @clk: pointer to a DPLL struct clk
  349. *
  350. * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
  351. * The choice of modes depends on the DPLL's programmed rate: if it is
  352. * the same as the DPLL's parent clock, it will enter bypass;
  353. * otherwise, it will enter lock. This code will wait for the DPLL to
  354. * indicate readiness before returning, unless the DPLL takes too long
  355. * to enter the target state. Intended to be used as the struct clk's
  356. * enable function. If DPLL3 was passed in, or the DPLL does not
  357. * support low-power stop, or if the DPLL took too long to enter
  358. * bypass or lock, return -EINVAL; otherwise, return 0.
  359. */
  360. int omap3_noncore_dpll_enable(struct clk_hw *hw)
  361. {
  362. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  363. int r;
  364. struct dpll_data *dd;
  365. struct clk_hw *parent;
  366. dd = clk->dpll_data;
  367. if (!dd)
  368. return -EINVAL;
  369. if (clk->clkdm) {
  370. r = ti_clk_ll_ops->clkdm_clk_enable(clk->clkdm, hw->clk);
  371. if (r) {
  372. WARN(1,
  373. "%s: could not enable %s's clockdomain %s: %d\n",
  374. __func__, clk_hw_get_name(hw),
  375. clk->clkdm_name, r);
  376. return r;
  377. }
  378. }
  379. parent = clk_hw_get_parent(hw);
  380. if (clk_hw_get_rate(hw) == clk_hw_get_rate(dd->clk_bypass)) {
  381. WARN_ON(parent != dd->clk_bypass);
  382. r = _omap3_noncore_dpll_bypass(clk);
  383. } else {
  384. WARN_ON(parent != dd->clk_ref);
  385. r = _omap3_noncore_dpll_lock(clk);
  386. }
  387. return r;
  388. }
  389. /**
  390. * omap3_noncore_dpll_disable - instruct a DPLL to enter low-power stop
  391. * @clk: pointer to a DPLL struct clk
  392. *
  393. * Instructs a non-CORE DPLL to enter low-power stop. This function is
  394. * intended for use in struct clkops. No return value.
  395. */
  396. void omap3_noncore_dpll_disable(struct clk_hw *hw)
  397. {
  398. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  399. _omap3_noncore_dpll_stop(clk);
  400. if (clk->clkdm)
  401. ti_clk_ll_ops->clkdm_clk_disable(clk->clkdm, hw->clk);
  402. }
  403. /* Non-CORE DPLL rate set code */
  404. /**
  405. * omap3_noncore_dpll_determine_rate - determine rate for a DPLL
  406. * @hw: pointer to the clock to determine rate for
  407. * @req: target rate request
  408. *
  409. * Determines which DPLL mode to use for reaching a desired target rate.
  410. * Checks whether the DPLL shall be in bypass or locked mode, and if
  411. * locked, calculates the M,N values for the DPLL via round-rate.
  412. * Returns a 0 on success, negative error value in failure.
  413. */
  414. int omap3_noncore_dpll_determine_rate(struct clk_hw *hw,
  415. struct clk_rate_request *req)
  416. {
  417. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  418. struct dpll_data *dd;
  419. if (!req->rate)
  420. return -EINVAL;
  421. dd = clk->dpll_data;
  422. if (!dd)
  423. return -EINVAL;
  424. if (clk_hw_get_rate(dd->clk_bypass) == req->rate &&
  425. (dd->modes & (1 << DPLL_LOW_POWER_BYPASS))) {
  426. req->best_parent_hw = dd->clk_bypass;
  427. } else {
  428. req->rate = omap2_dpll_round_rate(hw, req->rate,
  429. &req->best_parent_rate);
  430. req->best_parent_hw = dd->clk_ref;
  431. }
  432. req->best_parent_rate = req->rate;
  433. return 0;
  434. }
  435. /**
  436. * omap3_noncore_dpll_set_parent - set parent for a DPLL clock
  437. * @hw: pointer to the clock to set parent for
  438. * @index: parent index to select
  439. *
  440. * Sets parent for a DPLL clock. This sets the DPLL into bypass or
  441. * locked mode. Returns 0 with success, negative error value otherwise.
  442. */
  443. int omap3_noncore_dpll_set_parent(struct clk_hw *hw, u8 index)
  444. {
  445. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  446. int ret;
  447. if (!hw)
  448. return -EINVAL;
  449. if (index)
  450. ret = _omap3_noncore_dpll_bypass(clk);
  451. else
  452. ret = _omap3_noncore_dpll_lock(clk);
  453. return ret;
  454. }
  455. /**
  456. * omap3_noncore_dpll_set_rate - set rate for a DPLL clock
  457. * @hw: pointer to the clock to set parent for
  458. * @rate: target rate for the clock
  459. * @parent_rate: rate of the parent clock
  460. *
  461. * Sets rate for a DPLL clock. First checks if the clock parent is
  462. * reference clock (in bypass mode, the rate of the clock can't be
  463. * changed) and proceeds with the rate change operation. Returns 0
  464. * with success, negative error value otherwise.
  465. */
  466. int omap3_noncore_dpll_set_rate(struct clk_hw *hw, unsigned long rate,
  467. unsigned long parent_rate)
  468. {
  469. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  470. struct dpll_data *dd;
  471. u16 freqsel = 0;
  472. int ret;
  473. if (!hw || !rate)
  474. return -EINVAL;
  475. dd = clk->dpll_data;
  476. if (!dd)
  477. return -EINVAL;
  478. if (clk_hw_get_parent(hw) != dd->clk_ref)
  479. return -EINVAL;
  480. if (dd->last_rounded_rate == 0)
  481. return -EINVAL;
  482. /* Freqsel is available only on OMAP343X devices */
  483. if (ti_clk_get_features()->flags & TI_CLK_DPLL_HAS_FREQSEL) {
  484. freqsel = _omap3_dpll_compute_freqsel(clk, dd->last_rounded_n);
  485. WARN_ON(!freqsel);
  486. }
  487. pr_debug("%s: %s: set rate: locking rate to %lu.\n", __func__,
  488. clk_hw_get_name(hw), rate);
  489. ret = omap3_noncore_dpll_program(clk, freqsel);
  490. return ret;
  491. }
  492. /**
  493. * omap3_noncore_dpll_set_rate_and_parent - set rate and parent for a DPLL clock
  494. * @hw: pointer to the clock to set rate and parent for
  495. * @rate: target rate for the DPLL
  496. * @parent_rate: clock rate of the DPLL parent
  497. * @index: new parent index for the DPLL, 0 - reference, 1 - bypass
  498. *
  499. * Sets rate and parent for a DPLL clock. If new parent is the bypass
  500. * clock, only selects the parent. Otherwise proceeds with a rate
  501. * change, as this will effectively also change the parent as the
  502. * DPLL is put into locked mode. Returns 0 with success, negative error
  503. * value otherwise.
  504. */
  505. int omap3_noncore_dpll_set_rate_and_parent(struct clk_hw *hw,
  506. unsigned long rate,
  507. unsigned long parent_rate,
  508. u8 index)
  509. {
  510. int ret;
  511. if (!hw || !rate)
  512. return -EINVAL;
  513. /*
  514. * clk-ref at index[0], in which case we only need to set rate,
  515. * the parent will be changed automatically with the lock sequence.
  516. * With clk-bypass case we only need to change parent.
  517. */
  518. if (index)
  519. ret = omap3_noncore_dpll_set_parent(hw, index);
  520. else
  521. ret = omap3_noncore_dpll_set_rate(hw, rate, parent_rate);
  522. return ret;
  523. }
  524. /* DPLL autoidle read/set code */
  525. /**
  526. * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
  527. * @clk: struct clk * of the DPLL to read
  528. *
  529. * Return the DPLL's autoidle bits, shifted down to bit 0. Returns
  530. * -EINVAL if passed a null pointer or if the struct clk does not
  531. * appear to refer to a DPLL.
  532. */
  533. static u32 omap3_dpll_autoidle_read(struct clk_hw_omap *clk)
  534. {
  535. const struct dpll_data *dd;
  536. u32 v;
  537. if (!clk || !clk->dpll_data)
  538. return -EINVAL;
  539. dd = clk->dpll_data;
  540. if (!dd->autoidle_reg)
  541. return -EINVAL;
  542. v = ti_clk_ll_ops->clk_readl(dd->autoidle_reg);
  543. v &= dd->autoidle_mask;
  544. v >>= __ffs(dd->autoidle_mask);
  545. return v;
  546. }
  547. /**
  548. * omap3_dpll_allow_idle - enable DPLL autoidle bits
  549. * @clk: struct clk * of the DPLL to operate on
  550. *
  551. * Enable DPLL automatic idle control. This automatic idle mode
  552. * switching takes effect only when the DPLL is locked, at least on
  553. * OMAP3430. The DPLL will enter low-power stop when its downstream
  554. * clocks are gated. No return value.
  555. */
  556. static void omap3_dpll_allow_idle(struct clk_hw_omap *clk)
  557. {
  558. const struct dpll_data *dd;
  559. u32 v;
  560. if (!clk || !clk->dpll_data)
  561. return;
  562. dd = clk->dpll_data;
  563. if (!dd->autoidle_reg)
  564. return;
  565. /*
  566. * REVISIT: CORE DPLL can optionally enter low-power bypass
  567. * by writing 0x5 instead of 0x1. Add some mechanism to
  568. * optionally enter this mode.
  569. */
  570. v = ti_clk_ll_ops->clk_readl(dd->autoidle_reg);
  571. v &= ~dd->autoidle_mask;
  572. v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask);
  573. ti_clk_ll_ops->clk_writel(v, dd->autoidle_reg);
  574. }
  575. /**
  576. * omap3_dpll_deny_idle - prevent DPLL from automatically idling
  577. * @clk: struct clk * of the DPLL to operate on
  578. *
  579. * Disable DPLL automatic idle control. No return value.
  580. */
  581. static void omap3_dpll_deny_idle(struct clk_hw_omap *clk)
  582. {
  583. const struct dpll_data *dd;
  584. u32 v;
  585. if (!clk || !clk->dpll_data)
  586. return;
  587. dd = clk->dpll_data;
  588. if (!dd->autoidle_reg)
  589. return;
  590. v = ti_clk_ll_ops->clk_readl(dd->autoidle_reg);
  591. v &= ~dd->autoidle_mask;
  592. v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask);
  593. ti_clk_ll_ops->clk_writel(v, dd->autoidle_reg);
  594. }
  595. /* Clock control for DPLL outputs */
  596. /* Find the parent DPLL for the given clkoutx2 clock */
  597. static struct clk_hw_omap *omap3_find_clkoutx2_dpll(struct clk_hw *hw)
  598. {
  599. struct clk_hw_omap *pclk = NULL;
  600. /* Walk up the parents of clk, looking for a DPLL */
  601. do {
  602. do {
  603. hw = clk_hw_get_parent(hw);
  604. } while (hw && (clk_hw_get_flags(hw) & CLK_IS_BASIC));
  605. if (!hw)
  606. break;
  607. pclk = to_clk_hw_omap(hw);
  608. } while (pclk && !pclk->dpll_data);
  609. /* clk does not have a DPLL as a parent? error in the clock data */
  610. if (!pclk) {
  611. WARN_ON(1);
  612. return NULL;
  613. }
  614. return pclk;
  615. }
  616. /**
  617. * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
  618. * @clk: DPLL output struct clk
  619. *
  620. * Using parent clock DPLL data, look up DPLL state. If locked, set our
  621. * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
  622. */
  623. unsigned long omap3_clkoutx2_recalc(struct clk_hw *hw,
  624. unsigned long parent_rate)
  625. {
  626. const struct dpll_data *dd;
  627. unsigned long rate;
  628. u32 v;
  629. struct clk_hw_omap *pclk = NULL;
  630. if (!parent_rate)
  631. return 0;
  632. pclk = omap3_find_clkoutx2_dpll(hw);
  633. if (!pclk)
  634. return 0;
  635. dd = pclk->dpll_data;
  636. WARN_ON(!dd->enable_mask);
  637. v = ti_clk_ll_ops->clk_readl(dd->control_reg) & dd->enable_mask;
  638. v >>= __ffs(dd->enable_mask);
  639. if ((v != OMAP3XXX_EN_DPLL_LOCKED) || (dd->flags & DPLL_J_TYPE))
  640. rate = parent_rate;
  641. else
  642. rate = parent_rate * 2;
  643. return rate;
  644. }
  645. /* OMAP3/4 non-CORE DPLL clkops */
  646. const struct clk_hw_omap_ops clkhwops_omap3_dpll = {
  647. .allow_idle = omap3_dpll_allow_idle,
  648. .deny_idle = omap3_dpll_deny_idle,
  649. };
  650. /**
  651. * omap3_dpll4_set_rate - set rate for omap3 per-dpll
  652. * @hw: clock to change
  653. * @rate: target rate for clock
  654. * @parent_rate: rate of the parent clock
  655. *
  656. * Check if the current SoC supports the per-dpll reprogram operation
  657. * or not, and then do the rate change if supported. Returns -EINVAL
  658. * if not supported, 0 for success, and potential error codes from the
  659. * clock rate change.
  660. */
  661. int omap3_dpll4_set_rate(struct clk_hw *hw, unsigned long rate,
  662. unsigned long parent_rate)
  663. {
  664. /*
  665. * According to the 12-5 CDP code from TI, "Limitation 2.5"
  666. * on 3430ES1 prevents us from changing DPLL multipliers or dividers
  667. * on DPLL4.
  668. */
  669. if (ti_clk_get_features()->flags & TI_CLK_DPLL4_DENY_REPROGRAM) {
  670. pr_err("clock: DPLL4 cannot change rate due to silicon 'Limitation 2.5' on 3430ES1.\n");
  671. return -EINVAL;
  672. }
  673. return omap3_noncore_dpll_set_rate(hw, rate, parent_rate);
  674. }
  675. /**
  676. * omap3_dpll4_set_rate_and_parent - set rate and parent for omap3 per-dpll
  677. * @hw: clock to change
  678. * @rate: target rate for clock
  679. * @parent_rate: rate of the parent clock
  680. * @index: parent index, 0 - reference clock, 1 - bypass clock
  681. *
  682. * Check if the current SoC support the per-dpll reprogram operation
  683. * or not, and then do the rate + parent change if supported. Returns
  684. * -EINVAL if not supported, 0 for success, and potential error codes
  685. * from the clock rate change.
  686. */
  687. int omap3_dpll4_set_rate_and_parent(struct clk_hw *hw, unsigned long rate,
  688. unsigned long parent_rate, u8 index)
  689. {
  690. if (ti_clk_get_features()->flags & TI_CLK_DPLL4_DENY_REPROGRAM) {
  691. pr_err("clock: DPLL4 cannot change rate due to silicon 'Limitation 2.5' on 3430ES1.\n");
  692. return -EINVAL;
  693. }
  694. return omap3_noncore_dpll_set_rate_and_parent(hw, rate, parent_rate,
  695. index);
  696. }
  697. /* Apply DM3730 errata sprz319 advisory 2.1. */
  698. static bool omap3_dpll5_apply_errata(struct clk_hw *hw,
  699. unsigned long parent_rate)
  700. {
  701. struct omap3_dpll5_settings {
  702. unsigned int rate, m, n;
  703. };
  704. static const struct omap3_dpll5_settings precomputed[] = {
  705. /*
  706. * From DM3730 errata advisory 2.1, table 35 and 36.
  707. * The N value is increased by 1 compared to the tables as the
  708. * errata lists register values while last_rounded_field is the
  709. * real divider value.
  710. */
  711. { 12000000, 80, 0 + 1 },
  712. { 13000000, 443, 5 + 1 },
  713. { 19200000, 50, 0 + 1 },
  714. { 26000000, 443, 11 + 1 },
  715. { 38400000, 25, 0 + 1 }
  716. };
  717. const struct omap3_dpll5_settings *d;
  718. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  719. struct dpll_data *dd;
  720. unsigned int i;
  721. for (i = 0; i < ARRAY_SIZE(precomputed); ++i) {
  722. if (parent_rate == precomputed[i].rate)
  723. break;
  724. }
  725. if (i == ARRAY_SIZE(precomputed))
  726. return false;
  727. d = &precomputed[i];
  728. /* Update the M, N and rounded rate values and program the DPLL. */
  729. dd = clk->dpll_data;
  730. dd->last_rounded_m = d->m;
  731. dd->last_rounded_n = d->n;
  732. dd->last_rounded_rate = div_u64((u64)parent_rate * d->m, d->n);
  733. omap3_noncore_dpll_program(clk, 0);
  734. return true;
  735. }
  736. /**
  737. * omap3_dpll5_set_rate - set rate for omap3 dpll5
  738. * @hw: clock to change
  739. * @rate: target rate for clock
  740. * @parent_rate: rate of the parent clock
  741. *
  742. * Set rate for the DPLL5 clock. Apply the sprz319 advisory 2.1 on OMAP36xx if
  743. * the DPLL is used for USB host (detected through the requested rate).
  744. */
  745. int omap3_dpll5_set_rate(struct clk_hw *hw, unsigned long rate,
  746. unsigned long parent_rate)
  747. {
  748. if (rate == OMAP3_DPLL5_FREQ_FOR_USBHOST * 8) {
  749. if (omap3_dpll5_apply_errata(hw, parent_rate))
  750. return 0;
  751. }
  752. return omap3_noncore_dpll_set_rate(hw, rate, parent_rate);
  753. }