k210_pll.c 2.9 KB

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