blob: a3cb109d357581fca6103467e195359ff5ba359e [file] [log] [blame]
Anup Patel42fdf082019-02-25 08:14:49 +00001// SPDX-License-Identifier: GPL-2.0
2/*
Anup Patel6f7b5a22019-06-25 06:31:08 +00003 * Copyright (C) 2018-2019 SiFive, Inc.
Anup Patel42fdf082019-02-25 08:14:49 +00004 * Wesley Terpstra
5 * Paul Walmsley
6 *
Anup Patel42fdf082019-02-25 08:14:49 +00007 * 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"
Anup Patel6f7b5a22019-06-25 06:31:08 +000021 * https://static.dev.sifive.com/FU540-C000-v1.0.pdf
Anup Patel42fdf082019-02-25 08:14:49 +000022 */
23
24#include <linux/bug.h>
25#include <linux/err.h>
26#include <linux/log2.h>
27#include <linux/math64.h>
Anup Patel00a156d2019-06-25 06:31:02 +000028#include <linux/clk/analogbits-wrpll-cln28hpc.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060029#include <linux/printk.h>
Anup Patel42fdf082019-02-25 08:14:49 +000030
31/* MIN_INPUT_FREQ: minimum input clock frequency, in Hz (Fref_min) */
32#define MIN_INPUT_FREQ 7000000
33
34/* MAX_INPUT_FREQ: maximum input clock frequency, in Hz (Fref_max) */
35#define MAX_INPUT_FREQ 600000000
36
37/* MIN_POST_DIVIDE_REF_FREQ: minimum post-divider reference frequency, in Hz */
38#define MIN_POST_DIVR_FREQ 7000000
39
40/* MAX_POST_DIVIDE_REF_FREQ: maximum post-divider reference frequency, in Hz */
41#define MAX_POST_DIVR_FREQ 200000000
42
43/* MIN_VCO_FREQ: minimum VCO frequency, in Hz (Fvco_min) */
44#define MIN_VCO_FREQ 2400000000UL
45
46/* MAX_VCO_FREQ: maximum VCO frequency, in Hz (Fvco_max) */
47#define MAX_VCO_FREQ 4800000000ULL
48
49/* MAX_DIVQ_DIVISOR: maximum output divisor. Selected by DIVQ = 6 */
50#define MAX_DIVQ_DIVISOR 64
51
52/* MAX_DIVR_DIVISOR: maximum reference divisor. Selected by DIVR = 63 */
53#define MAX_DIVR_DIVISOR 64
54
55/* MAX_LOCK_US: maximum PLL lock time, in microseconds (tLOCK_max) */
56#define MAX_LOCK_US 70
57
58/*
59 * ROUND_SHIFT: number of bits to shift to avoid precision loss in the rounding
60 * algorithm
61 */
62#define ROUND_SHIFT 20
63
64/*
65 * Private functions
66 */
67
68/**
69 * __wrpll_calc_filter_range() - determine PLL loop filter bandwidth
70 * @post_divr_freq: input clock rate after the R divider
71 *
72 * Select the value to be presented to the PLL RANGE input signals, based
73 * on the input clock frequency after the post-R-divider @post_divr_freq.
74 * This code follows the recommendations in the PLL datasheet for filter
75 * range selection.
76 *
77 * Return: The RANGE value to be presented to the PLL configuration inputs,
Anup Patel6f7b5a22019-06-25 06:31:08 +000078 * or a negative return code upon error.
Anup Patel42fdf082019-02-25 08:14:49 +000079 */
80static int __wrpll_calc_filter_range(unsigned long post_divr_freq)
81{
Anup Patel42fdf082019-02-25 08:14:49 +000082 if (post_divr_freq < MIN_POST_DIVR_FREQ ||
83 post_divr_freq > MAX_POST_DIVR_FREQ) {
84 WARN(1, "%s: post-divider reference freq out of range: %lu",
85 __func__, post_divr_freq);
Anup Patel6f7b5a22019-06-25 06:31:08 +000086 return -ERANGE;
Anup Patel42fdf082019-02-25 08:14:49 +000087 }
88
Anup Patel6f7b5a22019-06-25 06:31:08 +000089 switch (post_divr_freq) {
90 case 0 ... 10999999:
91 return 1;
92 case 11000000 ... 17999999:
93 return 2;
94 case 18000000 ... 29999999:
95 return 3;
96 case 30000000 ... 49999999:
97 return 4;
98 case 50000000 ... 79999999:
99 return 5;
100 case 80000000 ... 129999999:
101 return 6;
102 }
Anup Patel42fdf082019-02-25 08:14:49 +0000103
Anup Patel6f7b5a22019-06-25 06:31:08 +0000104 return 7;
Anup Patel42fdf082019-02-25 08:14:49 +0000105}
106
107/**
108 * __wrpll_calc_fbdiv() - return feedback fixed divide value
Anup Patel6f7b5a22019-06-25 06:31:08 +0000109 * @c: ptr to a struct wrpll_cfg record to read from
Anup Patel42fdf082019-02-25 08:14:49 +0000110 *
111 * The internal feedback path includes a fixed by-two divider; the
112 * external feedback path does not. Return the appropriate divider
113 * value (2 or 1) depending on whether internal or external feedback
114 * is enabled. This code doesn't test for invalid configurations
115 * (e.g. both or neither of WRPLL_FLAGS_*_FEEDBACK are set); it relies
116 * on the caller to do so.
117 *
118 * Context: Any context. Caller must protect the memory pointed to by
119 * @c from simultaneous modification.
120 *
121 * Return: 2 if internal feedback is enabled or 1 if external feedback
122 * is enabled.
123 */
Anup Patel6f7b5a22019-06-25 06:31:08 +0000124static u8 __wrpll_calc_fbdiv(const struct wrpll_cfg *c)
Anup Patel42fdf082019-02-25 08:14:49 +0000125{
126 return (c->flags & WRPLL_FLAGS_INT_FEEDBACK_MASK) ? 2 : 1;
127}
128
129/**
130 * __wrpll_calc_divq() - determine DIVQ based on target PLL output clock rate
131 * @target_rate: target PLL output clock rate
132 * @vco_rate: pointer to a u64 to store the computed VCO rate into
133 *
134 * Determine a reasonable value for the PLL Q post-divider, based on the
135 * target output rate @target_rate for the PLL. Along with returning the
136 * computed Q divider value as the return value, this function stores the
137 * desired target VCO rate into the variable pointed to by @vco_rate.
138 *
139 * Context: Any context. Caller must protect the memory pointed to by
140 * @vco_rate from simultaneous access or modification.
141 *
142 * Return: a positive integer DIVQ value to be programmed into the hardware
143 * upon success, or 0 upon error (since 0 is an invalid DIVQ value)
144 */
145static u8 __wrpll_calc_divq(u32 target_rate, u64 *vco_rate)
146{
147 u64 s;
148 u8 divq = 0;
149
150 if (!vco_rate) {
151 WARN_ON(1);
152 goto wcd_out;
153 }
154
155 s = div_u64(MAX_VCO_FREQ, target_rate);
156 if (s <= 1) {
157 divq = 1;
158 *vco_rate = MAX_VCO_FREQ;
159 } else if (s > MAX_DIVQ_DIVISOR) {
160 divq = ilog2(MAX_DIVQ_DIVISOR);
161 *vco_rate = MIN_VCO_FREQ;
162 } else {
163 divq = ilog2(s);
Anup Patel6f7b5a22019-06-25 06:31:08 +0000164 *vco_rate = (u64)target_rate << divq;
Anup Patel42fdf082019-02-25 08:14:49 +0000165 }
166
167wcd_out:
168 return divq;
169}
170
171/**
172 * __wrpll_update_parent_rate() - update PLL data when parent rate changes
Anup Patel6f7b5a22019-06-25 06:31:08 +0000173 * @c: ptr to a struct wrpll_cfg record to write PLL data to
Anup Patel42fdf082019-02-25 08:14:49 +0000174 * @parent_rate: PLL input refclk rate (pre-R-divider)
175 *
176 * Pre-compute some data used by the PLL configuration algorithm when
177 * the PLL's reference clock rate changes. The intention is to avoid
178 * computation when the parent rate remains constant - expected to be
179 * the common case.
180 *
Anup Patel6f7b5a22019-06-25 06:31:08 +0000181 * Returns: 0 upon success or -ERANGE if the reference clock rate is
182 * out of range.
Anup Patel42fdf082019-02-25 08:14:49 +0000183 */
Anup Patel6f7b5a22019-06-25 06:31:08 +0000184static int __wrpll_update_parent_rate(struct wrpll_cfg *c,
Anup Patel42fdf082019-02-25 08:14:49 +0000185 unsigned long parent_rate)
186{
187 u8 max_r_for_parent;
188
189 if (parent_rate > MAX_INPUT_FREQ || parent_rate < MIN_POST_DIVR_FREQ)
Anup Patel6f7b5a22019-06-25 06:31:08 +0000190 return -ERANGE;
Anup Patel42fdf082019-02-25 08:14:49 +0000191
Anup Patel6f7b5a22019-06-25 06:31:08 +0000192 c->parent_rate = parent_rate;
Anup Patel42fdf082019-02-25 08:14:49 +0000193 max_r_for_parent = div_u64(parent_rate, MIN_POST_DIVR_FREQ);
Anup Patel6f7b5a22019-06-25 06:31:08 +0000194 c->max_r = min_t(u8, MAX_DIVR_DIVISOR, max_r_for_parent);
Anup Patel42fdf082019-02-25 08:14:49 +0000195
Anup Patel6f7b5a22019-06-25 06:31:08 +0000196 c->init_r = DIV_ROUND_UP_ULL(parent_rate, MAX_POST_DIVR_FREQ);
Anup Patel42fdf082019-02-25 08:14:49 +0000197
198 return 0;
199}
200
Anup Patel42fdf082019-02-25 08:14:49 +0000201/**
Anup Patel6f7b5a22019-06-25 06:31:08 +0000202 * wrpll_configure() - compute PLL configuration for a target rate
203 * @c: ptr to a struct wrpll_cfg record to write into
Anup Patel42fdf082019-02-25 08:14:49 +0000204 * @target_rate: target PLL output clock rate (post-Q-divider)
205 * @parent_rate: PLL input refclk rate (pre-R-divider)
206 *
Anup Patel6f7b5a22019-06-25 06:31:08 +0000207 * Compute the appropriate PLL signal configuration values and store
208 * in PLL context @c. PLL reprogramming is not glitchless, so the
209 * caller should switch any downstream logic to a different clock
210 * source or clock-gate it before presenting these values to the PLL
211 * configuration signals.
Anup Patel42fdf082019-02-25 08:14:49 +0000212 *
213 * The caller must pass this function a pre-initialized struct
Anup Patel6f7b5a22019-06-25 06:31:08 +0000214 * wrpll_cfg record: either initialized to zero (with the
Anup Patel42fdf082019-02-25 08:14:49 +0000215 * exception of the .name and .flags fields) or read from the PLL.
216 *
217 * Context: Any context. Caller must protect the memory pointed to by @c
218 * from simultaneous access or modification.
219 *
220 * Return: 0 upon success; anything else upon failure.
221 */
Anup Patel6f7b5a22019-06-25 06:31:08 +0000222int wrpll_configure_for_rate(struct wrpll_cfg *c, u32 target_rate,
223 unsigned long parent_rate)
Anup Patel42fdf082019-02-25 08:14:49 +0000224{
225 unsigned long ratio;
226 u64 target_vco_rate, delta, best_delta, f_pre_div, vco, vco_pre;
Anup Patel6f7b5a22019-06-25 06:31:08 +0000227 u32 best_f, f, post_divr_freq;
Anup Patel42fdf082019-02-25 08:14:49 +0000228 u8 fbdiv, divq, best_r, r;
Anup Patel6f7b5a22019-06-25 06:31:08 +0000229 int range;
Anup Patel42fdf082019-02-25 08:14:49 +0000230
231 if (c->flags == 0) {
232 WARN(1, "%s called with uninitialized PLL config", __func__);
Anup Patel6f7b5a22019-06-25 06:31:08 +0000233 return -EINVAL;
Anup Patel42fdf082019-02-25 08:14:49 +0000234 }
235
Anup Patel42fdf082019-02-25 08:14:49 +0000236 /* Initialize rounding data if it hasn't been initialized already */
Anup Patel6f7b5a22019-06-25 06:31:08 +0000237 if (parent_rate != c->parent_rate) {
Anup Patel42fdf082019-02-25 08:14:49 +0000238 if (__wrpll_update_parent_rate(c, parent_rate)) {
239 pr_err("%s: PLL input rate is out of range\n",
240 __func__);
Anup Patel6f7b5a22019-06-25 06:31:08 +0000241 return -ERANGE;
Anup Patel42fdf082019-02-25 08:14:49 +0000242 }
243 }
244
245 c->flags &= ~WRPLL_FLAGS_RESET_MASK;
246
247 /* Put the PLL into bypass if the user requests the parent clock rate */
248 if (target_rate == parent_rate) {
249 c->flags |= WRPLL_FLAGS_BYPASS_MASK;
250 return 0;
251 }
Anup Patel6f7b5a22019-06-25 06:31:08 +0000252
Anup Patel42fdf082019-02-25 08:14:49 +0000253 c->flags &= ~WRPLL_FLAGS_BYPASS_MASK;
254
255 /* Calculate the Q shift and target VCO rate */
256 divq = __wrpll_calc_divq(target_rate, &target_vco_rate);
Anup Patel6f7b5a22019-06-25 06:31:08 +0000257 if (!divq)
Anup Patel42fdf082019-02-25 08:14:49 +0000258 return -1;
259 c->divq = divq;
260
261 /* Precalculate the pre-Q divider target ratio */
262 ratio = div64_u64((target_vco_rate << ROUND_SHIFT), parent_rate);
263
264 fbdiv = __wrpll_calc_fbdiv(c);
265 best_r = 0;
266 best_f = 0;
267 best_delta = MAX_VCO_FREQ;
268
269 /*
270 * Consider all values for R which land within
271 * [MIN_POST_DIVR_FREQ, MAX_POST_DIVR_FREQ]; prefer smaller R
272 */
Anup Patel6f7b5a22019-06-25 06:31:08 +0000273 for (r = c->init_r; r <= c->max_r; ++r) {
Anup Patel42fdf082019-02-25 08:14:49 +0000274 f_pre_div = ratio * r;
275 f = (f_pre_div + (1 << ROUND_SHIFT)) >> ROUND_SHIFT;
276 f >>= (fbdiv - 1);
277
278 post_divr_freq = div_u64(parent_rate, r);
279 vco_pre = fbdiv * post_divr_freq;
280 vco = vco_pre * f;
281
282 /* Ensure rounding didn't take us out of range */
283 if (vco > target_vco_rate) {
284 --f;
285 vco = vco_pre * f;
286 } else if (vco < MIN_VCO_FREQ) {
287 ++f;
288 vco = vco_pre * f;
289 }
290
291 delta = abs(target_rate - vco);
292 if (delta < best_delta) {
293 best_delta = delta;
294 best_r = r;
295 best_f = f;
296 }
297 }
298
299 c->divr = best_r - 1;
300 c->divf = best_f - 1;
301
302 post_divr_freq = div_u64(parent_rate, best_r);
303
304 /* Pick the best PLL jitter filter */
Anup Patel6f7b5a22019-06-25 06:31:08 +0000305 range = __wrpll_calc_filter_range(post_divr_freq);
306 if (range < 0)
307 return range;
308 c->range = range;
Anup Patel42fdf082019-02-25 08:14:49 +0000309
310 return 0;
311}
312
313/**
Anup Patel6f7b5a22019-06-25 06:31:08 +0000314 * wrpll_calc_output_rate() - calculate the PLL's target output rate
315 * @c: ptr to a struct wrpll_cfg record to read from
Anup Patel42fdf082019-02-25 08:14:49 +0000316 * @parent_rate: PLL refclk rate
317 *
318 * Given a pointer to the PLL's current input configuration @c and the
319 * PLL's input reference clock rate @parent_rate (before the R
320 * pre-divider), calculate the PLL's output clock rate (after the Q
Anup Patel6f7b5a22019-06-25 06:31:08 +0000321 * post-divider).
Anup Patel42fdf082019-02-25 08:14:49 +0000322 *
323 * Context: Any context. Caller must protect the memory pointed to by @c
324 * from simultaneous modification.
325 *
Anup Patel6f7b5a22019-06-25 06:31:08 +0000326 * Return: the PLL's output clock rate, in Hz. The return value from
327 * this function is intended to be convenient to pass directly
328 * to the Linux clock framework; thus there is no explicit
329 * error return value.
Anup Patel42fdf082019-02-25 08:14:49 +0000330 */
Anup Patel6f7b5a22019-06-25 06:31:08 +0000331unsigned long wrpll_calc_output_rate(const struct wrpll_cfg *c,
332 unsigned long parent_rate)
Anup Patel42fdf082019-02-25 08:14:49 +0000333{
334 u8 fbdiv;
335 u64 n;
336
Anup Patel6f7b5a22019-06-25 06:31:08 +0000337 if (c->flags & WRPLL_FLAGS_EXT_FEEDBACK_MASK) {
338 WARN(1, "external feedback mode not yet supported");
339 return ULONG_MAX;
340 }
Anup Patel42fdf082019-02-25 08:14:49 +0000341
342 fbdiv = __wrpll_calc_fbdiv(c);
343 n = parent_rate * fbdiv * (c->divf + 1);
Anup Patel6f7b5a22019-06-25 06:31:08 +0000344 n = div_u64(n, c->divr + 1);
Anup Patel42fdf082019-02-25 08:14:49 +0000345 n >>= c->divq;
346
347 return n;
348}
349
350/**
Anup Patel6f7b5a22019-06-25 06:31:08 +0000351 * wrpll_calc_max_lock_us() - return the time for the PLL to lock
352 * @c: ptr to a struct wrpll_cfg record to read from
Anup Patel42fdf082019-02-25 08:14:49 +0000353 *
354 * Return the minimum amount of time (in microseconds) that the caller
355 * must wait after reprogramming the PLL to ensure that it is locked
356 * to the input frequency and stable. This is likely to depend on the DIVR
357 * value; this is under discussion with the manufacturer.
358 *
359 * Return: the minimum amount of time the caller must wait for the PLL
360 * to lock (in microseconds)
361 */
Anup Patel6f7b5a22019-06-25 06:31:08 +0000362unsigned int wrpll_calc_max_lock_us(const struct wrpll_cfg *c)
Anup Patel42fdf082019-02-25 08:14:49 +0000363{
364 return MAX_LOCK_US;
365}