blob: f55379f33659b469e82a8dddab26d41e4125be71 [file] [log] [blame]
Sean Anderson6e887ee2020-06-24 06:41:09 -04001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
4 */
5
6#include <common.h>
7/* For DIV_ROUND_DOWN_ULL, defined in linux/kernel.h */
8#include <div64.h>
9#include <dm/test.h>
10#include <kendryte/pll.h>
11#include <test/ut.h>
12
13static int dm_test_k210_pll_calc_config(u32 rate, u32 rate_in,
14 struct k210_pll_config *best)
15{
16 u64 f, r, od, max_r, inv_ratio;
17 s64 error, best_error;
18
19 best_error = S64_MAX;
20 error = best_error;
21 max_r = min(16ULL, DIV_ROUND_DOWN_ULL(rate_in, 13300000));
22 inv_ratio = DIV_ROUND_CLOSEST_ULL((u64)rate_in << 32, rate);
23
24 /* Brute force it */
25 for (r = 1; r <= max_r; r++) {
26 for (f = 1; f <= 64; f++) {
27 for (od = 1; od <= 16; od++) {
28 u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
29
30 if (vco > 1750000000 || vco < 340000000)
31 continue;
32
33 error = DIV_ROUND_CLOSEST_ULL(f * inv_ratio,
34 r * od);
35 /* The lower 16 bits are spurious */
36 error = abs((error - BIT(32))) >> 16;
37 if (error < best_error) {
38 best->r = r;
39 best->f = f;
40 best->od = od;
41 best_error = error;
42 }
43 }
44 }
45 }
46
47 if (best_error == S64_MAX)
48 return -EINVAL;
49 return 0;
50}
51
52static int dm_test_k210_pll_compare(struct k210_pll_config *ours,
53 struct k210_pll_config *theirs)
54{
55 return (u32)ours->f * theirs->r * theirs->od !=
56 (u32)theirs->f * ours->r * ours->od;
57}
58
59static int dm_test_k210_pll(struct unit_test_state *uts)
60{
61 struct k210_pll_config ours, theirs;
62
63 /* General range checks */
64 ut_asserteq(-EINVAL, k210_pll_calc_config(0, 26000000, &theirs));
65 ut_asserteq(-EINVAL, k210_pll_calc_config(390000000, 0, &theirs));
66 ut_asserteq(-EINVAL, k210_pll_calc_config(2000000000, 26000000,
67 &theirs));
68 ut_asserteq(-EINVAL, k210_pll_calc_config(390000000, 2000000000,
69 &theirs));
70 ut_asserteq(-EINVAL, k210_pll_calc_config(1500000000, 20000000,
71 &theirs));
Sean Andersone850b4c2021-09-11 13:20:02 -040072 ut_asserteq(-EINVAL, k210_pll_calc_config(1750000000, 13300000,
73 &theirs));
Sean Anderson6e887ee2020-06-24 06:41:09 -040074
75 /* Verify we get the same output with brute-force */
Sean Andersone850b4c2021-09-11 13:20:02 -040076#define compare(rate, rate_in) do { \
77 ut_assertok(dm_test_k210_pll_calc_config(rate, rate_in, &ours)); \
78 ut_assertok(k210_pll_calc_config(rate, rate_in, &theirs)); \
79 ut_assertok(dm_test_k210_pll_compare(&ours, &theirs)); \
80} while (0)
Sean Anderson6e887ee2020-06-24 06:41:09 -040081
Sean Andersone850b4c2021-09-11 13:20:02 -040082 compare(390000000, 26000000);
83 compare(26000000, 390000000);
84 compare(400000000, 26000000);
85 compare(27000000, 26000000);
86 compare(26000000, 27000000);
Sean Andersone72139e2021-09-11 13:20:03 -040087 compare(13300000 * 64, 13300000);
88 compare(21250000, 21250000 * 70);
89 compare(21250000, 1750000000);
90 compare(1750000000, 1750000000);
Sean Anderson6e887ee2020-06-24 06:41:09 -040091
92 return 0;
93}
94DM_TEST(dm_test_k210_pll, 0);