Sean Anderson | 6e887ee | 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 | |
Sean Anderson | 6e887ee | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 6 | /* For DIV_ROUND_DOWN_ULL, defined in linux/kernel.h */ |
| 7 | #include <div64.h> |
| 8 | #include <dm/test.h> |
Damien Le Moal | 6e5a8b7 | 2022-03-01 10:35:39 +0000 | [diff] [blame] | 9 | #include <k210/pll.h> |
Sean Anderson | 6e887ee | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 10 | #include <test/ut.h> |
| 11 | |
| 12 | static int dm_test_k210_pll_calc_config(u32 rate, u32 rate_in, |
| 13 | struct k210_pll_config *best) |
| 14 | { |
| 15 | u64 f, r, od, max_r, inv_ratio; |
| 16 | s64 error, best_error; |
| 17 | |
| 18 | best_error = S64_MAX; |
| 19 | error = best_error; |
| 20 | max_r = min(16ULL, DIV_ROUND_DOWN_ULL(rate_in, 13300000)); |
| 21 | inv_ratio = DIV_ROUND_CLOSEST_ULL((u64)rate_in << 32, rate); |
| 22 | |
| 23 | /* Brute force it */ |
| 24 | for (r = 1; r <= max_r; r++) { |
| 25 | for (f = 1; f <= 64; f++) { |
| 26 | for (od = 1; od <= 16; od++) { |
| 27 | u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r); |
| 28 | |
| 29 | if (vco > 1750000000 || vco < 340000000) |
| 30 | continue; |
| 31 | |
| 32 | error = DIV_ROUND_CLOSEST_ULL(f * inv_ratio, |
| 33 | r * od); |
| 34 | /* The lower 16 bits are spurious */ |
Heinrich Schuchardt | 135e5f6 | 2022-10-16 18:12:32 +0200 | [diff] [blame] | 35 | error = abs64((error - BIT_ULL(32))) >> 16; |
Sean Anderson | 6e887ee | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 36 | if (error < best_error) { |
| 37 | best->r = r; |
| 38 | best->f = f; |
| 39 | best->od = od; |
| 40 | best_error = error; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if (best_error == S64_MAX) |
| 47 | return -EINVAL; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int dm_test_k210_pll_compare(struct k210_pll_config *ours, |
| 52 | struct k210_pll_config *theirs) |
| 53 | { |
| 54 | return (u32)ours->f * theirs->r * theirs->od != |
| 55 | (u32)theirs->f * ours->r * ours->od; |
| 56 | } |
| 57 | |
| 58 | static int dm_test_k210_pll(struct unit_test_state *uts) |
| 59 | { |
| 60 | struct k210_pll_config ours, theirs; |
| 61 | |
| 62 | /* General range checks */ |
| 63 | ut_asserteq(-EINVAL, k210_pll_calc_config(0, 26000000, &theirs)); |
| 64 | ut_asserteq(-EINVAL, k210_pll_calc_config(390000000, 0, &theirs)); |
| 65 | ut_asserteq(-EINVAL, k210_pll_calc_config(2000000000, 26000000, |
| 66 | &theirs)); |
| 67 | ut_asserteq(-EINVAL, k210_pll_calc_config(390000000, 2000000000, |
| 68 | &theirs)); |
| 69 | ut_asserteq(-EINVAL, k210_pll_calc_config(1500000000, 20000000, |
| 70 | &theirs)); |
Sean Anderson | e850b4c | 2021-09-11 13:20:02 -0400 | [diff] [blame] | 71 | ut_asserteq(-EINVAL, k210_pll_calc_config(1750000000, 13300000, |
| 72 | &theirs)); |
Sean Anderson | 6e887ee | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 73 | |
| 74 | /* Verify we get the same output with brute-force */ |
Sean Anderson | e850b4c | 2021-09-11 13:20:02 -0400 | [diff] [blame] | 75 | #define compare(rate, rate_in) do { \ |
| 76 | ut_assertok(dm_test_k210_pll_calc_config(rate, rate_in, &ours)); \ |
| 77 | ut_assertok(k210_pll_calc_config(rate, rate_in, &theirs)); \ |
| 78 | ut_assertok(dm_test_k210_pll_compare(&ours, &theirs)); \ |
| 79 | } while (0) |
Sean Anderson | 6e887ee | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 80 | |
Sean Anderson | e850b4c | 2021-09-11 13:20:02 -0400 | [diff] [blame] | 81 | compare(390000000, 26000000); |
| 82 | compare(26000000, 390000000); |
| 83 | compare(400000000, 26000000); |
| 84 | compare(27000000, 26000000); |
| 85 | compare(26000000, 27000000); |
Sean Anderson | e72139e | 2021-09-11 13:20:03 -0400 | [diff] [blame] | 86 | compare(13300000 * 64, 13300000); |
| 87 | compare(21250000, 21250000 * 70); |
| 88 | compare(21250000, 1750000000); |
| 89 | compare(1750000000, 1750000000); |
Sean Anderson | 6e887ee | 2020-06-24 06:41:09 -0400 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | DM_TEST(dm_test_k210_pll, 0); |