Sean Anderson | 019ef9a | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 1 | // 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> |
Damien Le Moal | fd426b3 | 2022-03-01 10:35:39 +0000 | [diff] [blame] | 10 | #include <k210/pll.h> |
Sean Anderson | 019ef9a | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 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 */ |
Heinrich Schuchardt | e77ef0b | 2022-10-16 18:12:32 +0200 | [diff] [blame] | 36 | error = abs64((error - BIT_ULL(32))) >> 16; |
Sean Anderson | 019ef9a | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 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)); |
Sean Anderson | 6e23c9f | 2021-09-11 13:20:02 -0400 | [diff] [blame] | 72 | ut_asserteq(-EINVAL, k210_pll_calc_config(1750000000, 13300000, |
| 73 | &theirs)); |
Sean Anderson | 019ef9a | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 74 | |
| 75 | /* Verify we get the same output with brute-force */ |
Sean Anderson | 6e23c9f | 2021-09-11 13:20:02 -0400 | [diff] [blame] | 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) |
Sean Anderson | 019ef9a | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 81 | |
Sean Anderson | 6e23c9f | 2021-09-11 13:20:02 -0400 | [diff] [blame] | 82 | compare(390000000, 26000000); |
| 83 | compare(26000000, 390000000); |
| 84 | compare(400000000, 26000000); |
| 85 | compare(27000000, 26000000); |
| 86 | compare(26000000, 27000000); |
Sean Anderson | 425c08f | 2021-09-11 13:20:03 -0400 | [diff] [blame] | 87 | compare(13300000 * 64, 13300000); |
| 88 | compare(21250000, 21250000 * 70); |
| 89 | compare(21250000, 1750000000); |
| 90 | compare(1750000000, 1750000000); |
Sean Anderson | 019ef9a | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | DM_TEST(dm_test_k210_pll, 0); |