git: 4b8553c3700b - main - aw_clk: Take the first candidate in the nmm and np find_best functions

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Thu, 12 Nov 2026 09:51:24 UTC
The branch main has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=4b8553c3700bbc44f89882daeabed92ec33709b0

commit 4b8553c3700bbc44f89882daeabed92ec33709b0
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-11-10 15:16:47 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-12 09:47:28 +0000

    aw_clk: Take the first candidate in the nmm and np find_best functions
    
    The search starts with a best value of 0 and only takes a candidate that
    is closer to the requested frequency than the best one.  When every
    candidate is more than twice the requested frequency, which happens for
    a clock that has a minimum for its N factor, none of them is closer than
    0 and the search returns the factors 0.  set_freq then writes them,
    which gives a wrong value to the register.
    
    Always take the first candidate, so that the result is the closest one
    that the clock can do.
    
    Tested on: H6
---
 sys/dev/clk/allwinner/aw_clk_nmm.c | 2 +-
 sys/dev/clk/allwinner/aw_clk_np.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/sys/dev/clk/allwinner/aw_clk_nmm.c b/sys/dev/clk/allwinner/aw_clk_nmm.c
index cb41a5fd2fc8..9f0e7d9fd2f7 100644
--- a/sys/dev/clk/allwinner/aw_clk_nmm.c
+++ b/sys/dev/clk/allwinner/aw_clk_nmm.c
@@ -119,7 +119,7 @@ aw_clk_nmm_find_best(struct aw_clk_nmm_sc *sc, uint64_t fparent, uint64_t *fout,
 		for (m1 = min_m1; m1 <= max_m1; ) {
 			for (n = min_n; n <= max_n; ) {
 				cur = fparent * n / m0 / m1;
-				if (clk_freq_diff(*fout, cur) <
+				if (best == 0 || clk_freq_diff(*fout, cur) <
 				    clk_freq_diff(*fout, best)) {
 					best = cur;
 					*factor_n = n;
diff --git a/sys/dev/clk/allwinner/aw_clk_np.c b/sys/dev/clk/allwinner/aw_clk_np.c
index 7d9430e74a22..3b520cdc5272 100644
--- a/sys/dev/clk/allwinner/aw_clk_np.c
+++ b/sys/dev/clk/allwinner/aw_clk_np.c
@@ -113,7 +113,7 @@ aw_clk_np_find_best(struct aw_clk_np_sc *sc, uint64_t fparent, uint64_t *fout,
 	for (p = min_p; p <= max_p; ) {
 		for (n = min_n; n <= max_n; ) {
 			cur = fparent * n / p;
-			if (clk_freq_diff(*fout, cur) <
+			if (best == 0 || clk_freq_diff(*fout, cur) <
 			    clk_freq_diff(*fout, best)) {
 				best = cur;
 				*factor_n = n;