dpll3xxx.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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_mask)
  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_mask)
  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_mask)
  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. /**
  646. * omap3_core_dpll_save_context - Save the m and n values of the divider
  647. * @hw: pointer struct clk_hw
  648. *
  649. * Before the dpll registers are lost save the last rounded rate m and n
  650. * and the enable mask.
  651. */
  652. int omap3_core_dpll_save_context(struct clk_hw *hw)
  653. {
  654. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  655. struct dpll_data *dd;
  656. u32 v;
  657. dd = clk->dpll_data;
  658. v = ti_clk_ll_ops->clk_readl(&dd->control_reg);
  659. clk->context = (v & dd->enable_mask) >> __ffs(dd->enable_mask);
  660. if (clk->context == DPLL_LOCKED) {
  661. v = ti_clk_ll_ops->clk_readl(&dd->mult_div1_reg);
  662. dd->last_rounded_m = (v & dd->mult_mask) >>
  663. __ffs(dd->mult_mask);
  664. dd->last_rounded_n = ((v & dd->div1_mask) >>
  665. __ffs(dd->div1_mask)) + 1;
  666. }
  667. return 0;
  668. }
  669. /**
  670. * omap3_core_dpll_restore_context - restore the m and n values of the divider
  671. * @hw: pointer struct clk_hw
  672. *
  673. * Restore the last rounded rate m and n
  674. * and the enable mask.
  675. */
  676. void omap3_core_dpll_restore_context(struct clk_hw *hw)
  677. {
  678. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  679. const struct dpll_data *dd;
  680. u32 v;
  681. dd = clk->dpll_data;
  682. if (clk->context == DPLL_LOCKED) {
  683. _omap3_dpll_write_clken(clk, 0x4);
  684. _omap3_wait_dpll_status(clk, 0);
  685. v = ti_clk_ll_ops->clk_readl(&dd->mult_div1_reg);
  686. v &= ~(dd->mult_mask | dd->div1_mask);
  687. v |= dd->last_rounded_m << __ffs(dd->mult_mask);
  688. v |= (dd->last_rounded_n - 1) << __ffs(dd->div1_mask);
  689. ti_clk_ll_ops->clk_writel(v, &dd->mult_div1_reg);
  690. _omap3_dpll_write_clken(clk, DPLL_LOCKED);
  691. _omap3_wait_dpll_status(clk, 1);
  692. } else {
  693. _omap3_dpll_write_clken(clk, clk->context);
  694. }
  695. }
  696. /**
  697. * omap3_non_core_dpll_save_context - Save the m and n values of the divider
  698. * @hw: pointer struct clk_hw
  699. *
  700. * Before the dpll registers are lost save the last rounded rate m and n
  701. * and the enable mask.
  702. */
  703. int omap3_noncore_dpll_save_context(struct clk_hw *hw)
  704. {
  705. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  706. struct dpll_data *dd;
  707. u32 v;
  708. dd = clk->dpll_data;
  709. v = ti_clk_ll_ops->clk_readl(&dd->control_reg);
  710. clk->context = (v & dd->enable_mask) >> __ffs(dd->enable_mask);
  711. if (clk->context == DPLL_LOCKED) {
  712. v = ti_clk_ll_ops->clk_readl(&dd->mult_div1_reg);
  713. dd->last_rounded_m = (v & dd->mult_mask) >>
  714. __ffs(dd->mult_mask);
  715. dd->last_rounded_n = ((v & dd->div1_mask) >>
  716. __ffs(dd->div1_mask)) + 1;
  717. }
  718. return 0;
  719. }
  720. /**
  721. * omap3_core_dpll_restore_context - restore the m and n values of the divider
  722. * @hw: pointer struct clk_hw
  723. *
  724. * Restore the last rounded rate m and n
  725. * and the enable mask.
  726. */
  727. void omap3_noncore_dpll_restore_context(struct clk_hw *hw)
  728. {
  729. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  730. const struct dpll_data *dd;
  731. u32 ctrl, mult_div1;
  732. dd = clk->dpll_data;
  733. ctrl = ti_clk_ll_ops->clk_readl(&dd->control_reg);
  734. mult_div1 = ti_clk_ll_ops->clk_readl(&dd->mult_div1_reg);
  735. if (clk->context == ((ctrl & dd->enable_mask) >>
  736. __ffs(dd->enable_mask)) &&
  737. dd->last_rounded_m == ((mult_div1 & dd->mult_mask) >>
  738. __ffs(dd->mult_mask)) &&
  739. dd->last_rounded_n == ((mult_div1 & dd->div1_mask) >>
  740. __ffs(dd->div1_mask)) + 1) {
  741. /* nothing to be done */
  742. return;
  743. }
  744. if (clk->context == DPLL_LOCKED)
  745. omap3_noncore_dpll_program(clk, 0);
  746. else
  747. _omap3_dpll_write_clken(clk, clk->context);
  748. }
  749. /* OMAP3/4 non-CORE DPLL clkops */
  750. const struct clk_hw_omap_ops clkhwops_omap3_dpll = {
  751. .allow_idle = omap3_dpll_allow_idle,
  752. .deny_idle = omap3_dpll_deny_idle,
  753. };
  754. /**
  755. * omap3_dpll4_set_rate - set rate for omap3 per-dpll
  756. * @hw: clock to change
  757. * @rate: target rate for clock
  758. * @parent_rate: rate of the parent clock
  759. *
  760. * Check if the current SoC supports the per-dpll reprogram operation
  761. * or not, and then do the rate change if supported. Returns -EINVAL
  762. * if not supported, 0 for success, and potential error codes from the
  763. * clock rate change.
  764. */
  765. int omap3_dpll4_set_rate(struct clk_hw *hw, unsigned long rate,
  766. unsigned long parent_rate)
  767. {
  768. /*
  769. * According to the 12-5 CDP code from TI, "Limitation 2.5"
  770. * on 3430ES1 prevents us from changing DPLL multipliers or dividers
  771. * on DPLL4.
  772. */
  773. if (ti_clk_get_features()->flags & TI_CLK_DPLL4_DENY_REPROGRAM) {
  774. pr_err("clock: DPLL4 cannot change rate due to silicon 'Limitation 2.5' on 3430ES1.\n");
  775. return -EINVAL;
  776. }
  777. return omap3_noncore_dpll_set_rate(hw, rate, parent_rate);
  778. }
  779. /**
  780. * omap3_dpll4_set_rate_and_parent - set rate and parent for omap3 per-dpll
  781. * @hw: clock to change
  782. * @rate: target rate for clock
  783. * @parent_rate: rate of the parent clock
  784. * @index: parent index, 0 - reference clock, 1 - bypass clock
  785. *
  786. * Check if the current SoC support the per-dpll reprogram operation
  787. * or not, and then do the rate + parent change if supported. Returns
  788. * -EINVAL if not supported, 0 for success, and potential error codes
  789. * from the clock rate change.
  790. */
  791. int omap3_dpll4_set_rate_and_parent(struct clk_hw *hw, unsigned long rate,
  792. unsigned long parent_rate, u8 index)
  793. {
  794. if (ti_clk_get_features()->flags & TI_CLK_DPLL4_DENY_REPROGRAM) {
  795. pr_err("clock: DPLL4 cannot change rate due to silicon 'Limitation 2.5' on 3430ES1.\n");
  796. return -EINVAL;
  797. }
  798. return omap3_noncore_dpll_set_rate_and_parent(hw, rate, parent_rate,
  799. index);
  800. }
  801. /* Apply DM3730 errata sprz319 advisory 2.1. */
  802. static bool omap3_dpll5_apply_errata(struct clk_hw *hw,
  803. unsigned long parent_rate)
  804. {
  805. struct omap3_dpll5_settings {
  806. unsigned int rate, m, n;
  807. };
  808. static const struct omap3_dpll5_settings precomputed[] = {
  809. /*
  810. * From DM3730 errata advisory 2.1, table 35 and 36.
  811. * The N value is increased by 1 compared to the tables as the
  812. * errata lists register values while last_rounded_field is the
  813. * real divider value.
  814. */
  815. { 12000000, 80, 0 + 1 },
  816. { 13000000, 443, 5 + 1 },
  817. { 19200000, 50, 0 + 1 },
  818. { 26000000, 443, 11 + 1 },
  819. { 38400000, 25, 0 + 1 }
  820. };
  821. const struct omap3_dpll5_settings *d;
  822. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  823. struct dpll_data *dd;
  824. unsigned int i;
  825. for (i = 0; i < ARRAY_SIZE(precomputed); ++i) {
  826. if (parent_rate == precomputed[i].rate)
  827. break;
  828. }
  829. if (i == ARRAY_SIZE(precomputed))
  830. return false;
  831. d = &precomputed[i];
  832. /* Update the M, N and rounded rate values and program the DPLL. */
  833. dd = clk->dpll_data;
  834. dd->last_rounded_m = d->m;
  835. dd->last_rounded_n = d->n;
  836. dd->last_rounded_rate = div_u64((u64)parent_rate * d->m, d->n);
  837. omap3_noncore_dpll_program(clk, 0);
  838. return true;
  839. }
  840. /**
  841. * omap3_dpll5_set_rate - set rate for omap3 dpll5
  842. * @hw: clock to change
  843. * @rate: target rate for clock
  844. * @parent_rate: rate of the parent clock
  845. *
  846. * Set rate for the DPLL5 clock. Apply the sprz319 advisory 2.1 on OMAP36xx if
  847. * the DPLL is used for USB host (detected through the requested rate).
  848. */
  849. int omap3_dpll5_set_rate(struct clk_hw *hw, unsigned long rate,
  850. unsigned long parent_rate)
  851. {
  852. if (rate == OMAP3_DPLL5_FREQ_FOR_USBHOST * 8) {
  853. if (omap3_dpll5_apply_errata(hw, parent_rate))
  854. return 0;
  855. }
  856. return omap3_noncore_dpll_set_rate(hw, rate, parent_rate);
  857. }