]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
mmc: litex_mmc: Use DIV_ROUND_UP for more accurate clock calculation
authorInochi Amaoto <inochiama@gmail.com>
Thu, 21 May 2026 07:21:20 +0000 (15:21 +0800)
committerUlf Hansson <ulfh@kernel.org>
Fri, 29 May 2026 12:43:42 +0000 (14:43 +0200)
The previous clock uses roundup_pow_of_two() to calculate the core
clock frequency. It does not meet the actual hardware meaning.
The actual frequency is calculated by "ref_clk / ((div >> 1) << 1)".

Fix the clock divider calculation.

Fixes: 92e099104729 ("mmc: Add driver for LiteX's LiteSDCard interface")
Signed-off-by: Inochi Amaoto <inochiama@gmail.com>
Reviewed-by: Gabriel Somlo <gsomlo@gmail.com>
Cc: stable@vger.kernel.org
Signed-off-by: Ulf Hansson <ulfh@kernel.org>
drivers/mmc/host/litex_mmc.c

index d2f19c2dc6738c8cc051e3dfc5ab81f3ec799aa1..52571bb17c61ab2f240af4e45f4ba65afddab22b 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/interrupt.h>
 #include <linux/iopoll.h>
 #include <linux/litex.h>
+#include <linux/math.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
@@ -436,11 +437,10 @@ static void litex_mmc_setclk(struct litex_mmc_host *host, unsigned int freq)
        struct device *dev = mmc_dev(host->mmc);
        u32 div;
 
-       div = freq ? host->ref_clk / freq : 256U;
-       div = roundup_pow_of_two(div);
+       div = freq ? DIV_ROUND_UP(host->ref_clk, freq) : 256U;
        div = clamp(div, 2U, 256U);
        dev_dbg(dev, "sd_clk_freq=%d: set to %d via div=%d\n",
-               freq, host->ref_clk / div, div);
+               freq, host->ref_clk / ((div + 1) & ~1U), div);
        litex_write16(host->sdphy + LITEX_PHY_CLOCKERDIV, div);
        host->sd_clk = freq;
 }