From 8a74eba4d5ee0184eee0c68b9396f31088e05b24 Mon Sep 17 00:00:00 2001 From: Icenowy Zheng Date: Wed, 9 Nov 2022 17:52:38 +0800 Subject: [PATCH] software/liblitedram: scale up values when finding CMD delay The delay finding code is based on integers, and when divided by the count, rounding error will be quite easy to happen. Scale up all delay value by 256x to reduce rounding error. Signed-off-by: Icenowy Zheng --- litex/soc/software/liblitedram/sdram.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/litex/soc/software/liblitedram/sdram.c b/litex/soc/software/liblitedram/sdram.c index 47f20944d..e824d01a8 100644 --- a/litex/soc/software/liblitedram/sdram.c +++ b/litex/soc/software/liblitedram/sdram.c @@ -773,7 +773,7 @@ static void sdram_write_leveling_find_cmd_delay(unsigned int *best_error, unsign int delay_count = 0; for (int i=0; i < SDRAM_PHY_MODULES; ++i) { if (delays[i] != -1) { - delay_mean += delays[i] + _sdram_tck_taps/4; + delay_mean += delays[i]*256 + _sdram_tck_taps*64; delay_count += 1; } } @@ -781,7 +781,7 @@ static void sdram_write_leveling_find_cmd_delay(unsigned int *best_error, unsign delay_mean /= delay_count; /* We want the higher number of valid modules and delay to be centered */ - int ideal_delay = (SDRAM_PHY_DELAYS - _sdram_tck_taps/4)/2; + int ideal_delay = SDRAM_PHY_DELAYS*128 - _sdram_tck_taps*32; int error = ideal_delay - delay_mean; if (error < 0) error *= -1; @@ -794,7 +794,7 @@ static void sdram_write_leveling_find_cmd_delay(unsigned int *best_error, unsign } } #ifdef SDRAM_WRITE_LEVELING_CMD_DELAY_DEBUG - printf("Delay mean: %d, ideal: %d\n", delay_mean, ideal_delay); + printf("Delay mean: %d/256, ideal: %d/256\n", delay_mean, ideal_delay); #else printf("%d", ok); #endif