]>
Commit | Line | Data |
---|---|---|
019ef9a3 SA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Copyright (C) 2020 Sean Anderson <[email protected]> | |
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> | |
fd426b31 | 10 | #include <k210/pll.h> |
019ef9a3 SA |
11 | #include <test/ut.h> |
12 | ||
13 | static 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 | ||
52 | static 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 | ||
59 | static 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)); | |
6e23c9f0 SA |
72 | ut_asserteq(-EINVAL, k210_pll_calc_config(1750000000, 13300000, |
73 | &theirs)); | |
019ef9a3 SA |
74 | |
75 | /* Verify we get the same output with brute-force */ | |
6e23c9f0 SA |
76 | #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) | |
81 | ||
82 | compare(390000000, 26000000); | |
83 | compare(26000000, 390000000); | |
84 | compare(400000000, 26000000); | |
85 | compare(27000000, 26000000); | |
86 | compare(26000000, 27000000); | |
425c08fa SA |
87 | compare(13300000 * 64, 13300000); |
88 | compare(21250000, 21250000 * 70); | |
89 | compare(21250000, 1750000000); | |
90 | compare(1750000000, 1750000000); | |
019ef9a3 SA |
91 | |
92 | return 0; | |
93 | } | |
94 | DM_TEST(dm_test_k210_pll, 0); |