From: Florent Kermarrec Date: Thu, 9 Jul 2020 11:09:36 +0000 (+0200) Subject: liblitesdcard/sdcard: clamp divider value. X-Git-Tag: 24jan2021_ls180~91 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=5ebdfd9307060a2548c896fef03d278b994d12f6;p=litex.git liblitesdcard/sdcard: clamp divider value. --- diff --git a/litex/soc/software/liblitesdcard/sdcard.c b/litex/soc/software/liblitesdcard/sdcard.c index 6b6d838a..5739809c 100644 --- a/litex/soc/software/liblitesdcard/sdcard.c +++ b/litex/soc/software/liblitesdcard/sdcard.c @@ -31,6 +31,13 @@ unsigned int sdcard_response[SD_CMD_RESPONSE_SIZE/4]; +/*-----------------------------------------------------------------------*/ +/* Helpers */ +/*-----------------------------------------------------------------------*/ + +#define max(x, y) (((x) > (y)) ? (x) : (y)) +#define min(x, y) (((x) < (y)) ? (x) : (y)) + /*-----------------------------------------------------------------------*/ /* SDCard command helpers */ /*-----------------------------------------------------------------------*/ @@ -124,6 +131,8 @@ static void sdcard_set_clk_freq(uint32_t clk_freq) { uint32_t divider; divider = CONFIG_CLOCK_FREQUENCY/clk_freq + 1; divider = (1 << log2(divider)); + divider = max(divider, 2); + divider = min(divider, 128); #ifdef SDCARD_DEBUG printf("Setting SDCard clk freq to "); if (clk_freq > 1000000)