wrpll-cln28hpc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018-2019 SiFive, Inc.
  4. * Wesley Terpstra
  5. * Paul Walmsley
  6. *
  7. * This library supports configuration parsing and reprogramming of
  8. * the CLN28HPC variant of the Analog Bits Wide Range PLL. The
  9. * intention is for this library to be reusable for any device that
  10. * integrates this PLL; thus the register structure and programming
  11. * details are expected to be provided by a separate IP block driver.
  12. *
  13. * The bulk of this code is primarily useful for clock configurations
  14. * that must operate at arbitrary rates, as opposed to clock configurations
  15. * that are restricted by software or manufacturer guidance to a small,
  16. * pre-determined set of performance points.
  17. *
  18. * References:
  19. * - Analog Bits "Wide Range PLL Datasheet", version 2015.10.01
  20. * - SiFive FU540-C000 Manual v1p0, Chapter 7 "Clocking and Reset"
  21. * https://static.dev.sifive.com/FU540-C000-v1.0.pdf
  22. */
  23. #include <linux/bug.h>
  24. #include <linux/err.h>
  25. #include <linux/limits.h>
  26. #include <linux/log2.h>
  27. #include <linux/math64.h>
  28. #include <linux/math.h>
  29. #include <linux/minmax.h>
  30. #include <linux/module.h>
  31. #include <linux/clk/analogbits-wrpll-cln28hpc.h>
  32. /* MIN_INPUT_FREQ: minimum input clock frequency, in Hz (Fref_min) */
  33. #define MIN_INPUT_FREQ 7000000
  34. /* MAX_INPUT_FREQ: maximum input clock frequency, in Hz (Fref_max) */
  35. #define MAX_INPUT_FREQ 600000000
  36. /* MIN_POST_DIVIDE_REF_FREQ: minimum post-divider reference frequency, in Hz */
  37. #define MIN_POST_DIVR_FREQ 7000000
  38. /* MAX_POST_DIVIDE_REF_FREQ: maximum post-divider reference frequency, in Hz */
  39. #define MAX_POST_DIVR_FREQ 200000000
  40. /* MIN_VCO_FREQ: minimum VCO frequency, in Hz (Fvco_min) */
  41. #define MIN_VCO_FREQ 2400000000UL
  42. /* MAX_VCO_FREQ: maximum VCO frequency, in Hz (Fvco_max) */
  43. #define MAX_VCO_FREQ 4800000000ULL
  44. /* MAX_DIVQ_DIVISOR: maximum output divisor. Selected by DIVQ = 6 */
  45. #define MAX_DIVQ_DIVISOR 64
  46. /* MAX_DIVR_DIVISOR: maximum reference divisor. Selected by DIVR = 63 */
  47. #define MAX_DIVR_DIVISOR 64
  48. /* MAX_LOCK_US: maximum PLL lock time, in microseconds (tLOCK_max) */
  49. #define MAX_LOCK_US 70
  50. /*
  51. * ROUND_SHIFT: number of bits to shift to avoid precision loss in the rounding
  52. * algorithm
  53. */
  54. #define ROUND_SHIFT 20
  55. /*
  56. * Private functions
  57. */
  58. /**
  59. * __wrpll_calc_filter_range() - determine PLL loop filter bandwidth
  60. * @post_divr_freq: input clock rate after the R divider
  61. *
  62. * Select the value to be presented to the PLL RANGE input signals, based
  63. * on the input clock frequency after the post-R-divider @post_divr_freq.
  64. * This code follows the recommendations in the PLL datasheet for filter
  65. * range selection.
  66. *
  67. * Return: The RANGE value to be presented to the PLL configuration inputs,
  68. * or a negative return code upon error.
  69. */
  70. static int __wrpll_calc_filter_range(unsigned long post_divr_freq)
  71. {
  72. if (post_divr_freq < MIN_POST_DIVR_FREQ ||
  73. post_divr_freq > MAX_POST_DIVR_FREQ) {
  74. WARN(1, "%s: post-divider reference freq out of range: %lu",
  75. __func__, post_divr_freq);
  76. return -ERANGE;
  77. }
  78. switch (post_divr_freq) {
  79. case 0 ... 10999999:
  80. return 1;
  81. case 11000000 ... 17999999:
  82. return 2;
  83. case 18000000 ... 29999999:
  84. return 3;
  85. case 30000000 ... 49999999:
  86. return 4;
  87. case 50000000 ... 79999999:
  88. return 5;
  89. case 80000000 ... 129999999:
  90. return 6;
  91. }
  92. return 7;
  93. }
  94. /**
  95. * __wrpll_calc_fbdiv() - return feedback fixed divide value
  96. * @c: ptr to a struct wrpll_cfg record to read from
  97. *
  98. * The internal feedback path includes a fixed by-two divider; the
  99. * external feedback path does not. Return the appropriate divider
  100. * value (2 or 1) depending on whether internal or external feedback
  101. * is enabled. This code doesn't test for invalid configurations
  102. * (e.g. both or neither of WRPLL_FLAGS_*_FEEDBACK are set); it relies
  103. * on the caller to do so.
  104. *
  105. * Context: Any context. Caller must protect the memory pointed to by
  106. * @c from simultaneous modification.
  107. *
  108. * Return: 2 if internal feedback is enabled or 1 if external feedback
  109. * is enabled.
  110. */
  111. static u8 __wrpll_calc_fbdiv(const struct wrpll_cfg *c)
  112. {
  113. return (c->flags & WRPLL_FLAGS_INT_FEEDBACK_MASK) ? 2 : 1;
  114. }
  115. /**
  116. * __wrpll_calc_divq() - determine DIVQ based on target PLL output clock rate
  117. * @target_rate: target PLL output clock rate
  118. * @vco_rate: pointer to a u64 to store the computed VCO rate into
  119. *
  120. * Determine a reasonable value for the PLL Q post-divider, based on the
  121. * target output rate @target_rate for the PLL. Along with returning the
  122. * computed Q divider value as the return value, this function stores the
  123. * desired target VCO rate into the variable pointed to by @vco_rate.
  124. *
  125. * Context: Any context. Caller must protect the memory pointed to by
  126. * @vco_rate from simultaneous access or modification.
  127. *
  128. * Return: a positive integer DIVQ value to be programmed into the hardware
  129. * upon success, or 0 upon error (since 0 is an invalid DIVQ value)
  130. */
  131. static u8 __wrpll_calc_divq(u32 target_rate, u64 *vco_rate)
  132. {
  133. u64 s;
  134. u8 divq = 0;
  135. if (!vco_rate) {
  136. WARN_ON(1);
  137. goto wcd_out;
  138. }
  139. s = div_u64(MAX_VCO_FREQ, target_rate);
  140. if (s <= 1) {
  141. divq = 1;
  142. *vco_rate = MAX_VCO_FREQ;
  143. } else if (s > MAX_DIVQ_DIVISOR) {
  144. divq = ilog2(MAX_DIVQ_DIVISOR);
  145. *vco_rate = MIN_VCO_FREQ;
  146. } else {
  147. divq = ilog2(s);
  148. *vco_rate = (u64)target_rate << divq;
  149. }
  150. wcd_out:
  151. return divq;
  152. }
  153. /**
  154. * __wrpll_update_parent_rate() - update PLL data when parent rate changes
  155. * @c: ptr to a struct wrpll_cfg record to write PLL data to
  156. * @parent_rate: PLL input refclk rate (pre-R-divider)
  157. *
  158. * Pre-compute some data used by the PLL configuration algorithm when
  159. * the PLL's reference clock rate changes. The intention is to avoid
  160. * computation when the parent rate remains constant - expected to be
  161. * the common case.
  162. *
  163. * Returns: 0 upon success or -ERANGE if the reference clock rate is
  164. * out of range.
  165. */
  166. static int __wrpll_update_parent_rate(struct wrpll_cfg *c,
  167. unsigned long parent_rate)
  168. {
  169. u8 max_r_for_parent;
  170. if (parent_rate > MAX_INPUT_FREQ || parent_rate < MIN_POST_DIVR_FREQ)
  171. return -ERANGE;
  172. c->parent_rate = parent_rate;
  173. max_r_for_parent = div_u64(parent_rate, MIN_POST_DIVR_FREQ);
  174. c->max_r = min_t(u8, MAX_DIVR_DIVISOR, max_r_for_parent);
  175. c->init_r = DIV_ROUND_UP_ULL(parent_rate, MAX_POST_DIVR_FREQ);
  176. return 0;
  177. }
  178. /**
  179. * wrpll_configure_for_rate() - compute PLL configuration for a target rate
  180. * @c: ptr to a struct wrpll_cfg record to write into
  181. * @target_rate: target PLL output clock rate (post-Q-divider)
  182. * @parent_rate: PLL input refclk rate (pre-R-divider)
  183. *
  184. * Compute the appropriate PLL signal configuration values and store
  185. * in PLL context @c. PLL reprogramming is not glitchless, so the
  186. * caller should switch any downstream logic to a different clock
  187. * source or clock-gate it before presenting these values to the PLL
  188. * configuration signals.
  189. *
  190. * The caller must pass this function a pre-initialized struct
  191. * wrpll_cfg record: either initialized to zero (with the
  192. * exception of the .name and .flags fields) or read from the PLL.
  193. *
  194. * Context: Any context. Caller must protect the memory pointed to by @c
  195. * from simultaneous access or modification.
  196. *
  197. * Return: 0 upon success; anything else upon failure.
  198. */
  199. int wrpll_configure_for_rate(struct wrpll_cfg *c, u32 target_rate,
  200. unsigned long parent_rate)
  201. {
  202. unsigned long ratio;
  203. u64 target_vco_rate, delta, best_delta, f_pre_div, vco, vco_pre;
  204. u32 best_f, f, post_divr_freq;
  205. u8 fbdiv, divq, best_r, r;
  206. int range;
  207. if (c->flags == 0) {
  208. WARN(1, "%s called with uninitialized PLL config", __func__);
  209. return -EINVAL;
  210. }
  211. /* Initialize rounding data if it hasn't been initialized already */
  212. if (parent_rate != c->parent_rate) {
  213. if (__wrpll_update_parent_rate(c, parent_rate)) {
  214. pr_err("%s: PLL input rate is out of range\n",
  215. __func__);
  216. return -ERANGE;
  217. }
  218. }
  219. c->flags &= ~WRPLL_FLAGS_RESET_MASK;
  220. /* Put the PLL into bypass if the user requests the parent clock rate */
  221. if (target_rate == parent_rate) {
  222. c->flags |= WRPLL_FLAGS_BYPASS_MASK;
  223. return 0;
  224. }
  225. c->flags &= ~WRPLL_FLAGS_BYPASS_MASK;
  226. /* Calculate the Q shift and target VCO rate */
  227. divq = __wrpll_calc_divq(target_rate, &target_vco_rate);
  228. if (!divq)
  229. return -1;
  230. c->divq = divq;
  231. /* Precalculate the pre-Q divider target ratio */
  232. ratio = div64_u64((target_vco_rate << ROUND_SHIFT), parent_rate);
  233. fbdiv = __wrpll_calc_fbdiv(c);
  234. best_r = 0;
  235. best_f = 0;
  236. best_delta = MAX_VCO_FREQ;
  237. /*
  238. * Consider all values for R which land within
  239. * [MIN_POST_DIVR_FREQ, MAX_POST_DIVR_FREQ]; prefer smaller R
  240. */
  241. for (r = c->init_r; r <= c->max_r; ++r) {
  242. f_pre_div = ratio * r;
  243. f = (f_pre_div + (1 << ROUND_SHIFT)) >> ROUND_SHIFT;
  244. f >>= (fbdiv - 1);
  245. post_divr_freq = div_u64(parent_rate, r);
  246. vco_pre = fbdiv * post_divr_freq;
  247. vco = vco_pre * f;
  248. /* Ensure rounding didn't take us out of range */
  249. if (vco > target_vco_rate) {
  250. --f;
  251. vco = vco_pre * f;
  252. } else if (vco < MIN_VCO_FREQ) {
  253. ++f;
  254. vco = vco_pre * f;
  255. }
  256. delta = abs(target_vco_rate - vco);
  257. if (delta < best_delta) {
  258. best_delta = delta;
  259. best_r = r;
  260. best_f = f;
  261. }
  262. }
  263. c->divr = best_r - 1;
  264. c->divf = best_f - 1;
  265. post_divr_freq = div_u64(parent_rate, best_r);
  266. /* Pick the best PLL jitter filter */
  267. range = __wrpll_calc_filter_range(post_divr_freq);
  268. if (range < 0)
  269. return range;
  270. c->range = range;
  271. return 0;
  272. }
  273. EXPORT_SYMBOL_GPL(wrpll_configure_for_rate);
  274. /**
  275. * wrpll_calc_output_rate() - calculate the PLL's target output rate
  276. * @c: ptr to a struct wrpll_cfg record to read from
  277. * @parent_rate: PLL refclk rate
  278. *
  279. * Given a pointer to the PLL's current input configuration @c and the
  280. * PLL's input reference clock rate @parent_rate (before the R
  281. * pre-divider), calculate the PLL's output clock rate (after the Q
  282. * post-divider).
  283. *
  284. * Context: Any context. Caller must protect the memory pointed to by @c
  285. * from simultaneous modification.
  286. *
  287. * Return: the PLL's output clock rate, in Hz. The return value from
  288. * this function is intended to be convenient to pass directly
  289. * to the Linux clock framework; thus there is no explicit
  290. * error return value.
  291. */
  292. unsigned long wrpll_calc_output_rate(const struct wrpll_cfg *c,
  293. unsigned long parent_rate)
  294. {
  295. u8 fbdiv;
  296. u64 n;
  297. if (c->flags & WRPLL_FLAGS_EXT_FEEDBACK_MASK) {
  298. WARN(1, "external feedback mode not yet supported");
  299. return ULONG_MAX;
  300. }
  301. fbdiv = __wrpll_calc_fbdiv(c);
  302. n = parent_rate * fbdiv * (c->divf + 1);
  303. n = div_u64(n, c->divr + 1);
  304. n >>= c->divq;
  305. return n;
  306. }
  307. EXPORT_SYMBOL_GPL(wrpll_calc_output_rate);
  308. /**
  309. * wrpll_calc_max_lock_us() - return the time for the PLL to lock
  310. * @c: ptr to a struct wrpll_cfg record to read from
  311. *
  312. * Return the minimum amount of time (in microseconds) that the caller
  313. * must wait after reprogramming the PLL to ensure that it is locked
  314. * to the input frequency and stable. This is likely to depend on the DIVR
  315. * value; this is under discussion with the manufacturer.
  316. *
  317. * Return: the minimum amount of time the caller must wait for the PLL
  318. * to lock (in microseconds)
  319. */
  320. unsigned int wrpll_calc_max_lock_us(const struct wrpll_cfg *c)
  321. {
  322. return MAX_LOCK_US;
  323. }
  324. EXPORT_SYMBOL_GPL(wrpll_calc_max_lock_us);
  325. MODULE_AUTHOR("Paul Walmsley <paul.walmsley@sifive.com>");
  326. MODULE_DESCRIPTION("Analog Bits Wide-Range PLL library");
  327. MODULE_LICENSE("GPL");