summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Boichat <drinkcat@chromium.org>2019-06-23 20:47:22 +0800
committerCommit Bot <commit-bot@chromium.org>2019-06-24 06:45:17 +0000
commit81d5f99910334d09768593519767f571392c5238 (patch)
treed8da9a36959e5b8ac20fb791f15e890e946a8485
parent5d4f631acf5ef95a8893d3f91512ed549f105ec0 (diff)
downloadchrome-ec-81d5f99910334d09768593519767f571392c5238.tar.gz
chip/mt_scp/clock: Prefer closest frequency when calibrating
It's better to chose the closest possible frequency, rather than the one above the target, as the system clock is now sourced from ULPOSC1 and we want to minimize errors. BRANCH=none BUG=b:120176040 TEST=Load kukui_scp, see that ULPOSC is calibrated to 247558 kHz, instead of 250148 kHz (0.18% error, instead of 0.87%) Change-Id: I068fa09b5fedaf795ee6171f0b88ab658d5792a9 Signed-off-by: Nicolas Boichat <drinkcat@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1672655 Reviewed-by: Yilun Lin <yllin@chromium.org>
-rw-r--r--chip/mt_scp/clock.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/chip/mt_scp/clock.c b/chip/mt_scp/clock.c
index 8214f5ae15..edc50d5939 100644
--- a/chip/mt_scp/clock.c
+++ b/chip/mt_scp/clock.c
@@ -198,8 +198,10 @@ static int scp_calibrate_ulposc(int osc, int target_mhz)
/*
* In the loop below, linear search closest div value to get desired
* frequency counter value. Then adjust cali to get a better result.
- * Note that this doesn't give optimal output frequency, and we prefer
- * a frequency slightly above our target.
+ * Note that this doesn't give optimal output frequency, but it's
+ * usually close enough.
+ * TODO(b:120176040): See if we can efficiently calibrate the clock with
+ * more precision by exploring more of the cali/div space.
*
* The frequency function follows. Note that f is positively correlated
* with both div and cali:
@@ -215,11 +217,12 @@ static int scp_calibrate_ulposc(int osc, int target_mhz)
/*
* If previous and current are on either side of the desired
- * frequency, pick the one above the desired frequency.
+ * frequency, pick the closest one.
*/
if (prev.freq && signum(target_freq - curr.freq) !=
signum(target_freq - prev.freq)) {
- if (prev.freq > curr.freq)
+ if (abs(target_freq - prev.freq) <
+ abs(target_freq - curr.freq))
curr = prev;
if (stage == STAGE_CALI)