* [PATCH v5 0/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0
@ 2026-05-21 7:21 Inochi Amaoto
2026-05-21 7:21 ` [PATCH v5 1/2] mmc: litex_mmc: Use DIV_ROUND_UP for more accurate clock calculation Inochi Amaoto
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Inochi Amaoto @ 2026-05-21 7:21 UTC (permalink / raw)
To: Ulf Hansson, Karol Gugala, Mateusz Holenko, Gabriel Somlo,
Joel Stanley, Maciej Dudek, Paul Mackerras
Cc: Inochi Amaoto, linux-mmc, linux-kernel, Yixun Lan, Longbin Li
The litex_mmc driver assumes the card is already probed in the BIOS
and skip the phy initialization. This will cause the command fail
like the following when the old card is unplugged and then insert
a new card:
[ 62.923593] litex-mmc f0004000.mmc: Command (cmd 8) error, status -110
[ 62.949717] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
[ 62.976606] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
[ 63.002516] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
[ 63.028442] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
Firstly, fix the clock divider calculation so we can get the right
clock frequency, then add required clock settings and initialization
for the CMD 0, so it can probe the new card.
Test Results:
Rocket:
# dmesg | grep mmc
[ 0.109176] litex-mmc 12003000.mmc: LiteX MMC controller initialized.
[ 0.256926] mmc0: new SDHC card at address aaaa
[ 0.257058] mmcblk0: mmc0:aaaa WC32G 29.7 GiB
Note: it seems like the rocket give a wrong DMA result when the target
address is in main memory.
See issue: https://github.com/enjoy-digital/litex/issues/2464
VexiiRiscv:
# dmesg | grep mmc
[ 2.368365] litex-mmc f0004000.mmc: LiteX MMC controller initialized.
[ 2.576993] mmc0: new SDHC card at address aaaa
[ 2.588966] mmcblk0: mmc0:aaaa WC32G 29.7 GiB
[ 2.620197] mmcblk0: p1
Changed from v4:
- https://lore.kernel.org/linux-mmc/20260517015323.264179-1-inochiama@gmail.com/
1. Add a new patch for fixing clock divider calculation
2. Move everything in the set_ios() callback.
Changed from v3:
- https://lore.kernel.org/linux-mmc/20260426112016.1370929-1-inochiama@gmail.com/
1. Remove patch 1: mmc: litex_mmc: Move litex_mmc_setclk() to bottom for reuse
2. Use set_ios() callback to apply the clock change.
Changed from v2:
- https://lore.kernel.org/linux-mmc/20260424013615.470325-1-inochiama@gmail.com/
1. Remove the added function forward reference and add a new patch
for moving litex_mmc_setclk() function
Change from v1:
- https://lore.kernel.org/linux-mmc/20260421025052.755471-1-inochiama@gmail.com/
1. use fsleep to replace udelay
Inochi Amaoto (2):
mmc: litex_mmc: Use DIV_ROUND_UP for more accurate clock calculation
mmc: litex_mmc: Set mandatory idle clocks before CMD0
drivers/mmc/host/litex_mmc.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
--
2.54.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v5 1/2] mmc: litex_mmc: Use DIV_ROUND_UP for more accurate clock calculation
2026-05-21 7:21 [PATCH v5 0/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0 Inochi Amaoto
@ 2026-05-21 7:21 ` Inochi Amaoto
2026-05-21 7:21 ` [PATCH v5 2/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0 Inochi Amaoto
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Inochi Amaoto @ 2026-05-21 7:21 UTC (permalink / raw)
To: Ulf Hansson, Karol Gugala, Mateusz Holenko, Gabriel Somlo,
Joel Stanley, Maciej Dudek, Paul Mackerras
Cc: Inochi Amaoto, linux-mmc, linux-kernel, Yixun Lan, Longbin Li
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>
---
drivers/mmc/host/litex_mmc.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/mmc/host/litex_mmc.c b/drivers/mmc/host/litex_mmc.c
index d2f19c2dc673..52571bb17c61 100644
--- a/drivers/mmc/host/litex_mmc.c
+++ b/drivers/mmc/host/litex_mmc.c
@@ -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;
}
--
2.54.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v5 2/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0
2026-05-21 7:21 [PATCH v5 0/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0 Inochi Amaoto
2026-05-21 7:21 ` [PATCH v5 1/2] mmc: litex_mmc: Use DIV_ROUND_UP for more accurate clock calculation Inochi Amaoto
@ 2026-05-21 7:21 ` Inochi Amaoto
2026-05-21 14:39 ` [PATCH v5 0/2] " Gabriel L. Somlo
2026-05-29 14:45 ` Ulf Hansson
3 siblings, 0 replies; 5+ messages in thread
From: Inochi Amaoto @ 2026-05-21 7:21 UTC (permalink / raw)
To: Ulf Hansson, Karol Gugala, Mateusz Holenko, Gabriel Somlo,
Joel Stanley, Maciej Dudek, Paul Mackerras
Cc: Inochi Amaoto, linux-mmc, linux-kernel, Yixun Lan, Longbin Li
The litex_mmc driver assumes the card is already probed in the BIOS
and skip the phy initialization. This will cause the command fail
like the following when the old card is unplugged and then insert
a new card:
[ 62.923593] litex-mmc f0004000.mmc: Command (cmd 8) error, status -110
[ 62.949717] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
[ 62.976606] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
[ 63.002516] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
[ 63.028442] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
Add required clock settings and initialization for the CMD 0, so it can
probe the new card.
Fixes: 92e099104729 ("mmc: Add driver for LiteX's LiteSDCard interface")
Signed-off-by: Inochi Amaoto <inochiama@gmail.com>
---
drivers/mmc/host/litex_mmc.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/drivers/mmc/host/litex_mmc.c b/drivers/mmc/host/litex_mmc.c
index 52571bb17c61..3655542ca998 100644
--- a/drivers/mmc/host/litex_mmc.c
+++ b/drivers/mmc/host/litex_mmc.c
@@ -69,6 +69,9 @@
#define SD_SLEEP_US 5
#define SD_TIMEOUT_US 20000
+#define SD_INIT_DELAY_US 1000
+#define SD_INIT_CLK_HZ 400000
+
#define SDIRQ_CARD_DETECT 1
#define SDIRQ_SD_TO_MEM_DONE 2
#define SDIRQ_MEM_TO_SD_DONE 4
@@ -449,6 +452,17 @@ static void litex_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
{
struct litex_mmc_host *host = mmc_priv(mmc);
+ /*
+ * The SD specification requires at least 74 idle clocks before CMD0.
+ * These dummy cycles is generated by writing LITEX_PHY_INITIALIZE.
+ */
+ if (ios->chip_select == MMC_CS_HIGH) {
+ litex_mmc_setclk(host, SD_INIT_CLK_HZ);
+ litex_write8(host->sdphy + LITEX_PHY_INITIALIZE, 1);
+ fsleep(SD_INIT_DELAY_US);
+ return;
+ }
+
/*
* NOTE: Ignore any ios->bus_width updates; they occur right after
* the mmc core sends its own acmd6 bus-width change notification,
--
2.54.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v5 0/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0
2026-05-21 7:21 [PATCH v5 0/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0 Inochi Amaoto
2026-05-21 7:21 ` [PATCH v5 1/2] mmc: litex_mmc: Use DIV_ROUND_UP for more accurate clock calculation Inochi Amaoto
2026-05-21 7:21 ` [PATCH v5 2/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0 Inochi Amaoto
@ 2026-05-21 14:39 ` Gabriel L. Somlo
2026-05-29 14:45 ` Ulf Hansson
3 siblings, 0 replies; 5+ messages in thread
From: Gabriel L. Somlo @ 2026-05-21 14:39 UTC (permalink / raw)
To: Inochi Amaoto
Cc: Ulf Hansson, Karol Gugala, Mateusz Holenko, Joel Stanley,
Maciej Dudek, Paul Mackerras, linux-mmc, linux-kernel, Yixun Lan,
Longbin Li
On Thu, May 21, 2026 at 03:21:19PM +0800, Inochi Amaoto wrote:
> The litex_mmc driver assumes the card is already probed in the BIOS
> and skip the phy initialization. This will cause the command fail
> like the following when the old card is unplugged and then insert
> a new card:
>
> [ 62.923593] litex-mmc f0004000.mmc: Command (cmd 8) error, status -110
> [ 62.949717] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
> [ 62.976606] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
> [ 63.002516] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
> [ 63.028442] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
>
> Firstly, fix the clock divider calculation so we can get the right
> clock frequency, then add required clock settings and initialization
> for the CMD 0, so it can probe the new card.
>
> Test Results:
> Rocket:
> # dmesg | grep mmc
> [ 0.109176] litex-mmc 12003000.mmc: LiteX MMC controller initialized.
> [ 0.256926] mmc0: new SDHC card at address aaaa
> [ 0.257058] mmcblk0: mmc0:aaaa WC32G 29.7 GiB
>
> Note: it seems like the rocket give a wrong DMA result when the target
> address is in main memory.
> See issue: https://github.com/enjoy-digital/litex/issues/2464
>
> VexiiRiscv:
> # dmesg | grep mmc
> [ 2.368365] litex-mmc f0004000.mmc: LiteX MMC controller initialized.
> [ 2.576993] mmc0: new SDHC card at address aaaa
> [ 2.588966] mmcblk0: mmc0:aaaa WC32G 29.7 GiB
> [ 2.620197] mmcblk0: p1
For the whole series:
Reviewed-by: Gabriel Somlo <gsomlo@gmail.com>
Thanks,
--Gabriel
> Changed from v4:
> - https://lore.kernel.org/linux-mmc/20260517015323.264179-1-inochiama@gmail.com/
> 1. Add a new patch for fixing clock divider calculation
> 2. Move everything in the set_ios() callback.
>
> Changed from v3:
> - https://lore.kernel.org/linux-mmc/20260426112016.1370929-1-inochiama@gmail.com/
> 1. Remove patch 1: mmc: litex_mmc: Move litex_mmc_setclk() to bottom for reuse
> 2. Use set_ios() callback to apply the clock change.
>
> Changed from v2:
> - https://lore.kernel.org/linux-mmc/20260424013615.470325-1-inochiama@gmail.com/
> 1. Remove the added function forward reference and add a new patch
> for moving litex_mmc_setclk() function
>
> Change from v1:
> - https://lore.kernel.org/linux-mmc/20260421025052.755471-1-inochiama@gmail.com/
> 1. use fsleep to replace udelay
>
> Inochi Amaoto (2):
> mmc: litex_mmc: Use DIV_ROUND_UP for more accurate clock calculation
> mmc: litex_mmc: Set mandatory idle clocks before CMD0
>
> drivers/mmc/host/litex_mmc.c | 20 +++++++++++++++++---
> 1 file changed, 17 insertions(+), 3 deletions(-)
>
> --
> 2.54.0
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v5 0/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0
2026-05-21 7:21 [PATCH v5 0/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0 Inochi Amaoto
` (2 preceding siblings ...)
2026-05-21 14:39 ` [PATCH v5 0/2] " Gabriel L. Somlo
@ 2026-05-29 14:45 ` Ulf Hansson
3 siblings, 0 replies; 5+ messages in thread
From: Ulf Hansson @ 2026-05-29 14:45 UTC (permalink / raw)
To: Inochi Amaoto
Cc: Ulf Hansson, Karol Gugala, Mateusz Holenko, Gabriel Somlo,
Joel Stanley, Maciej Dudek, Paul Mackerras, linux-mmc,
linux-kernel, Yixun Lan, Longbin Li
On Thu, May 21, 2026 at 9:21 AM Inochi Amaoto <inochiama@gmail.com> wrote:
>
> The litex_mmc driver assumes the card is already probed in the BIOS
> and skip the phy initialization. This will cause the command fail
> like the following when the old card is unplugged and then insert
> a new card:
>
> [ 62.923593] litex-mmc f0004000.mmc: Command (cmd 8) error, status -110
> [ 62.949717] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
> [ 62.976606] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
> [ 63.002516] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
> [ 63.028442] litex-mmc f0004000.mmc: Command (cmd 55) error, status -110
>
> Firstly, fix the clock divider calculation so we can get the right
> clock frequency, then add required clock settings and initialization
> for the CMD 0, so it can probe the new card.
>
> Test Results:
> Rocket:
> # dmesg | grep mmc
> [ 0.109176] litex-mmc 12003000.mmc: LiteX MMC controller initialized.
> [ 0.256926] mmc0: new SDHC card at address aaaa
> [ 0.257058] mmcblk0: mmc0:aaaa WC32G 29.7 GiB
>
> Note: it seems like the rocket give a wrong DMA result when the target
> address is in main memory.
> See issue: https://github.com/enjoy-digital/litex/issues/2464
>
> VexiiRiscv:
> # dmesg | grep mmc
> [ 2.368365] litex-mmc f0004000.mmc: LiteX MMC controller initialized.
> [ 2.576993] mmc0: new SDHC card at address aaaa
> [ 2.588966] mmcblk0: mmc0:aaaa WC32G 29.7 GiB
> [ 2.620197] mmcblk0: p1
>
> Changed from v4:
> - https://lore.kernel.org/linux-mmc/20260517015323.264179-1-inochiama@gmail.com/
> 1. Add a new patch for fixing clock divider calculation
> 2. Move everything in the set_ios() callback.
>
> Changed from v3:
> - https://lore.kernel.org/linux-mmc/20260426112016.1370929-1-inochiama@gmail.com/
> 1. Remove patch 1: mmc: litex_mmc: Move litex_mmc_setclk() to bottom for reuse
> 2. Use set_ios() callback to apply the clock change.
>
> Changed from v2:
> - https://lore.kernel.org/linux-mmc/20260424013615.470325-1-inochiama@gmail.com/
> 1. Remove the added function forward reference and add a new patch
> for moving litex_mmc_setclk() function
>
> Change from v1:
> - https://lore.kernel.org/linux-mmc/20260421025052.755471-1-inochiama@gmail.com/
> 1. use fsleep to replace udelay
>
> Inochi Amaoto (2):
> mmc: litex_mmc: Use DIV_ROUND_UP for more accurate clock calculation
> mmc: litex_mmc: Set mandatory idle clocks before CMD0
>
> drivers/mmc/host/litex_mmc.c | 20 +++++++++++++++++---
> 1 file changed, 17 insertions(+), 3 deletions(-)
>
> --
> 2.54.0
The series applied for fixes and by adding stable tags to both patches, thanks!
Kind regards
Uffe
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-05-29 14:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-21 7:21 [PATCH v5 0/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0 Inochi Amaoto
2026-05-21 7:21 ` [PATCH v5 1/2] mmc: litex_mmc: Use DIV_ROUND_UP for more accurate clock calculation Inochi Amaoto
2026-05-21 7:21 ` [PATCH v5 2/2] mmc: litex_mmc: Set mandatory idle clocks before CMD0 Inochi Amaoto
2026-05-21 14:39 ` [PATCH v5 0/2] " Gabriel L. Somlo
2026-05-29 14:45 ` Ulf Hansson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox