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