clk-pll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015 Endless Mobile, Inc.
  4. * Author: Carlo Caione <[email protected]>
  5. *
  6. * Copyright (c) 2018 Baylibre, SAS.
  7. * Author: Jerome Brunet <[email protected]>
  8. */
  9. /*
  10. * In the most basic form, a Meson PLL is composed as follows:
  11. *
  12. * PLL
  13. * +--------------------------------+
  14. * | |
  15. * | +--+ |
  16. * in >>-----[ /N ]--->| | +-----+ |
  17. * | | |------| DCO |---->> out
  18. * | +--------->| | +--v--+ |
  19. * | | +--+ | |
  20. * | | | |
  21. * | +--[ *(M + (F/Fmax) ]<--+ |
  22. * | |
  23. * +--------------------------------+
  24. *
  25. * out = in * (m + frac / frac_max) / n
  26. */
  27. #include <linux/clk-provider.h>
  28. #include <linux/delay.h>
  29. #include <linux/err.h>
  30. #include <linux/io.h>
  31. #include <linux/math64.h>
  32. #include <linux/module.h>
  33. #include <linux/rational.h>
  34. #include "clk-regmap.h"
  35. #include "clk-pll.h"
  36. static inline struct meson_clk_pll_data *
  37. meson_clk_pll_data(struct clk_regmap *clk)
  38. {
  39. return (struct meson_clk_pll_data *)clk->data;
  40. }
  41. static int __pll_round_closest_mult(struct meson_clk_pll_data *pll)
  42. {
  43. if ((pll->flags & CLK_MESON_PLL_ROUND_CLOSEST) &&
  44. !MESON_PARM_APPLICABLE(&pll->frac))
  45. return 1;
  46. return 0;
  47. }
  48. static unsigned long __pll_params_to_rate(unsigned long parent_rate,
  49. unsigned int m, unsigned int n,
  50. unsigned int frac,
  51. struct meson_clk_pll_data *pll)
  52. {
  53. u64 rate = (u64)parent_rate * m;
  54. if (frac && MESON_PARM_APPLICABLE(&pll->frac)) {
  55. u64 frac_rate = (u64)parent_rate * frac;
  56. rate += DIV_ROUND_UP_ULL(frac_rate,
  57. (1 << pll->frac.width));
  58. }
  59. return DIV_ROUND_UP_ULL(rate, n);
  60. }
  61. static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
  62. unsigned long parent_rate)
  63. {
  64. struct clk_regmap *clk = to_clk_regmap(hw);
  65. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  66. unsigned int m, n, frac;
  67. n = meson_parm_read(clk->map, &pll->n);
  68. /*
  69. * On some HW, N is set to zero on init. This value is invalid as
  70. * it would result in a division by zero. The rate can't be
  71. * calculated in this case
  72. */
  73. if (n == 0)
  74. return 0;
  75. m = meson_parm_read(clk->map, &pll->m);
  76. frac = MESON_PARM_APPLICABLE(&pll->frac) ?
  77. meson_parm_read(clk->map, &pll->frac) :
  78. 0;
  79. return __pll_params_to_rate(parent_rate, m, n, frac, pll);
  80. }
  81. static unsigned int __pll_params_with_frac(unsigned long rate,
  82. unsigned long parent_rate,
  83. unsigned int m,
  84. unsigned int n,
  85. struct meson_clk_pll_data *pll)
  86. {
  87. unsigned int frac_max = (1 << pll->frac.width);
  88. u64 val = (u64)rate * n;
  89. /* Bail out if we are already over the requested rate */
  90. if (rate < parent_rate * m / n)
  91. return 0;
  92. if (pll->flags & CLK_MESON_PLL_ROUND_CLOSEST)
  93. val = DIV_ROUND_CLOSEST_ULL(val * frac_max, parent_rate);
  94. else
  95. val = div_u64(val * frac_max, parent_rate);
  96. val -= m * frac_max;
  97. return min((unsigned int)val, (frac_max - 1));
  98. }
  99. static bool meson_clk_pll_is_better(unsigned long rate,
  100. unsigned long best,
  101. unsigned long now,
  102. struct meson_clk_pll_data *pll)
  103. {
  104. if (__pll_round_closest_mult(pll)) {
  105. /* Round Closest */
  106. if (abs(now - rate) < abs(best - rate))
  107. return true;
  108. } else {
  109. /* Round down */
  110. if (now <= rate && best < now)
  111. return true;
  112. }
  113. return false;
  114. }
  115. static int meson_clk_get_pll_table_index(unsigned int index,
  116. unsigned int *m,
  117. unsigned int *n,
  118. struct meson_clk_pll_data *pll)
  119. {
  120. if (!pll->table[index].n)
  121. return -EINVAL;
  122. *m = pll->table[index].m;
  123. *n = pll->table[index].n;
  124. return 0;
  125. }
  126. static unsigned int meson_clk_get_pll_range_m(unsigned long rate,
  127. unsigned long parent_rate,
  128. unsigned int n,
  129. struct meson_clk_pll_data *pll)
  130. {
  131. u64 val = (u64)rate * n;
  132. if (__pll_round_closest_mult(pll))
  133. return DIV_ROUND_CLOSEST_ULL(val, parent_rate);
  134. return div_u64(val, parent_rate);
  135. }
  136. static int meson_clk_get_pll_range_index(unsigned long rate,
  137. unsigned long parent_rate,
  138. unsigned int index,
  139. unsigned int *m,
  140. unsigned int *n,
  141. struct meson_clk_pll_data *pll)
  142. {
  143. *n = index + 1;
  144. /* Check the predivider range */
  145. if (*n >= (1 << pll->n.width))
  146. return -EINVAL;
  147. if (*n == 1) {
  148. /* Get the boundaries out the way */
  149. if (rate <= pll->range->min * parent_rate) {
  150. *m = pll->range->min;
  151. return -ENODATA;
  152. } else if (rate >= pll->range->max * parent_rate) {
  153. *m = pll->range->max;
  154. return -ENODATA;
  155. }
  156. }
  157. *m = meson_clk_get_pll_range_m(rate, parent_rate, *n, pll);
  158. /* the pre-divider gives a multiplier too big - stop */
  159. if (*m >= (1 << pll->m.width))
  160. return -EINVAL;
  161. return 0;
  162. }
  163. static int meson_clk_get_pll_get_index(unsigned long rate,
  164. unsigned long parent_rate,
  165. unsigned int index,
  166. unsigned int *m,
  167. unsigned int *n,
  168. struct meson_clk_pll_data *pll)
  169. {
  170. if (pll->range)
  171. return meson_clk_get_pll_range_index(rate, parent_rate,
  172. index, m, n, pll);
  173. else if (pll->table)
  174. return meson_clk_get_pll_table_index(index, m, n, pll);
  175. return -EINVAL;
  176. }
  177. static int meson_clk_get_pll_settings(unsigned long rate,
  178. unsigned long parent_rate,
  179. unsigned int *best_m,
  180. unsigned int *best_n,
  181. struct meson_clk_pll_data *pll)
  182. {
  183. unsigned long best = 0, now = 0;
  184. unsigned int i, m, n;
  185. int ret;
  186. for (i = 0, ret = 0; !ret; i++) {
  187. ret = meson_clk_get_pll_get_index(rate, parent_rate,
  188. i, &m, &n, pll);
  189. if (ret == -EINVAL)
  190. break;
  191. now = __pll_params_to_rate(parent_rate, m, n, 0, pll);
  192. if (meson_clk_pll_is_better(rate, best, now, pll)) {
  193. best = now;
  194. *best_m = m;
  195. *best_n = n;
  196. if (now == rate)
  197. break;
  198. }
  199. }
  200. return best ? 0 : -EINVAL;
  201. }
  202. static int meson_clk_pll_determine_rate(struct clk_hw *hw,
  203. struct clk_rate_request *req)
  204. {
  205. struct clk_regmap *clk = to_clk_regmap(hw);
  206. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  207. unsigned int m, n, frac;
  208. unsigned long round;
  209. int ret;
  210. ret = meson_clk_get_pll_settings(req->rate, req->best_parent_rate,
  211. &m, &n, pll);
  212. if (ret)
  213. return ret;
  214. round = __pll_params_to_rate(req->best_parent_rate, m, n, 0, pll);
  215. if (!MESON_PARM_APPLICABLE(&pll->frac) || req->rate == round) {
  216. req->rate = round;
  217. return 0;
  218. }
  219. /*
  220. * The rate provided by the setting is not an exact match, let's
  221. * try to improve the result using the fractional parameter
  222. */
  223. frac = __pll_params_with_frac(req->rate, req->best_parent_rate, m, n, pll);
  224. req->rate = __pll_params_to_rate(req->best_parent_rate, m, n, frac, pll);
  225. return 0;
  226. }
  227. static int meson_clk_pll_wait_lock(struct clk_hw *hw)
  228. {
  229. struct clk_regmap *clk = to_clk_regmap(hw);
  230. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  231. int delay = 24000000;
  232. do {
  233. /* Is the clock locked now ? */
  234. if (meson_parm_read(clk->map, &pll->l))
  235. return 0;
  236. delay--;
  237. } while (delay > 0);
  238. return -ETIMEDOUT;
  239. }
  240. static int meson_clk_pll_init(struct clk_hw *hw)
  241. {
  242. struct clk_regmap *clk = to_clk_regmap(hw);
  243. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  244. if (pll->init_count) {
  245. meson_parm_write(clk->map, &pll->rst, 1);
  246. regmap_multi_reg_write(clk->map, pll->init_regs,
  247. pll->init_count);
  248. meson_parm_write(clk->map, &pll->rst, 0);
  249. }
  250. return 0;
  251. }
  252. static int meson_clk_pll_is_enabled(struct clk_hw *hw)
  253. {
  254. struct clk_regmap *clk = to_clk_regmap(hw);
  255. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  256. if (meson_parm_read(clk->map, &pll->rst) ||
  257. !meson_parm_read(clk->map, &pll->en) ||
  258. !meson_parm_read(clk->map, &pll->l))
  259. return 0;
  260. return 1;
  261. }
  262. static int meson_clk_pcie_pll_enable(struct clk_hw *hw)
  263. {
  264. meson_clk_pll_init(hw);
  265. if (meson_clk_pll_wait_lock(hw))
  266. return -EIO;
  267. return 0;
  268. }
  269. static int meson_clk_pll_enable(struct clk_hw *hw)
  270. {
  271. struct clk_regmap *clk = to_clk_regmap(hw);
  272. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  273. /* do nothing if the PLL is already enabled */
  274. if (clk_hw_is_enabled(hw))
  275. return 0;
  276. /* Make sure the pll is in reset */
  277. meson_parm_write(clk->map, &pll->rst, 1);
  278. /* Enable the pll */
  279. meson_parm_write(clk->map, &pll->en, 1);
  280. /* Take the pll out reset */
  281. meson_parm_write(clk->map, &pll->rst, 0);
  282. if (meson_clk_pll_wait_lock(hw))
  283. return -EIO;
  284. return 0;
  285. }
  286. static void meson_clk_pll_disable(struct clk_hw *hw)
  287. {
  288. struct clk_regmap *clk = to_clk_regmap(hw);
  289. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  290. /* Put the pll is in reset */
  291. meson_parm_write(clk->map, &pll->rst, 1);
  292. /* Disable the pll */
  293. meson_parm_write(clk->map, &pll->en, 0);
  294. }
  295. static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  296. unsigned long parent_rate)
  297. {
  298. struct clk_regmap *clk = to_clk_regmap(hw);
  299. struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
  300. unsigned int enabled, m, n, frac = 0;
  301. unsigned long old_rate;
  302. int ret;
  303. if (parent_rate == 0 || rate == 0)
  304. return -EINVAL;
  305. old_rate = clk_hw_get_rate(hw);
  306. ret = meson_clk_get_pll_settings(rate, parent_rate, &m, &n, pll);
  307. if (ret)
  308. return ret;
  309. enabled = meson_parm_read(clk->map, &pll->en);
  310. if (enabled)
  311. meson_clk_pll_disable(hw);
  312. meson_parm_write(clk->map, &pll->n, n);
  313. meson_parm_write(clk->map, &pll->m, m);
  314. if (MESON_PARM_APPLICABLE(&pll->frac)) {
  315. frac = __pll_params_with_frac(rate, parent_rate, m, n, pll);
  316. meson_parm_write(clk->map, &pll->frac, frac);
  317. }
  318. /* If the pll is stopped, bail out now */
  319. if (!enabled)
  320. return 0;
  321. ret = meson_clk_pll_enable(hw);
  322. if (ret) {
  323. pr_warn("%s: pll did not lock, trying to restore old rate %lu\n",
  324. __func__, old_rate);
  325. /*
  326. * FIXME: Do we really need/want this HACK ?
  327. * It looks unsafe. what happens if the clock gets into a
  328. * broken state and we can't lock back on the old_rate ? Looks
  329. * like an infinite recursion is possible
  330. */
  331. meson_clk_pll_set_rate(hw, old_rate, parent_rate);
  332. }
  333. return ret;
  334. }
  335. /*
  336. * The Meson G12A PCIE PLL is fined tuned to deliver a very precise
  337. * 100MHz reference clock for the PCIe Analog PHY, and thus requires
  338. * a strict register sequence to enable the PLL.
  339. * To simplify, re-use the _init() op to enable the PLL and keep
  340. * the other ops except set_rate since the rate is fixed.
  341. */
  342. const struct clk_ops meson_clk_pcie_pll_ops = {
  343. .recalc_rate = meson_clk_pll_recalc_rate,
  344. .determine_rate = meson_clk_pll_determine_rate,
  345. .is_enabled = meson_clk_pll_is_enabled,
  346. .enable = meson_clk_pcie_pll_enable,
  347. .disable = meson_clk_pll_disable
  348. };
  349. EXPORT_SYMBOL_GPL(meson_clk_pcie_pll_ops);
  350. const struct clk_ops meson_clk_pll_ops = {
  351. .init = meson_clk_pll_init,
  352. .recalc_rate = meson_clk_pll_recalc_rate,
  353. .determine_rate = meson_clk_pll_determine_rate,
  354. .set_rate = meson_clk_pll_set_rate,
  355. .is_enabled = meson_clk_pll_is_enabled,
  356. .enable = meson_clk_pll_enable,
  357. .disable = meson_clk_pll_disable
  358. };
  359. EXPORT_SYMBOL_GPL(meson_clk_pll_ops);
  360. const struct clk_ops meson_clk_pll_ro_ops = {
  361. .recalc_rate = meson_clk_pll_recalc_rate,
  362. .is_enabled = meson_clk_pll_is_enabled,
  363. };
  364. EXPORT_SYMBOL_GPL(meson_clk_pll_ro_ops);
  365. MODULE_DESCRIPTION("Amlogic PLL driver");
  366. MODULE_AUTHOR("Carlo Caione <[email protected]>");
  367. MODULE_AUTHOR("Jerome Brunet <[email protected]>");
  368. MODULE_LICENSE("GPL v2");