Skip to contentSkip to conversation

mmc: sdhci: Disable SD card clock before changing parameters

PATCH In review Erick Shepherd 4 participants 7 messages Feb 11, 2025 – Jul 23, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc: sdhci: Disable SD card clock before changing parameters

Per the SD Host Controller Simplified Specification v4.20 §3.2.3, change the SD card clock parameters only after first disabling the external card clock. Doing this fixes a spurious clock pulse on Baytrail and Apollo Lake SD controllers which otherwise breaks voltage switching with a specific Swissbit SD card.

---
 drivers/mmc/host/sdhci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/mmc/host/sdhci.c +7 -2
index f4a7733a8ad2..5f91b44891f9 100644
@@ -2065,10 +2065,15 @@ void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
 
 	host->mmc->actual_clock = 0;
 
-	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
+	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
+	if (clk & SDHCI_CLOCK_CARD_EN)
+		sdhci_writew(host, clk & ~SDHCI_CLOCK_CARD_EN,
+			SDHCI_CLOCK_CONTROL);
 
-	if (clock == 0)
+	if (clock == 0) {
+		sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
 		return;
+	}
 
 	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
 	sdhci_enable_clk(host, clk);
Signature · 1 line
2.43.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Erick Shepherd

Per the SD Host Controller Simplified Specification v4.20 §3.2.3, change the SD card clock parameters only after first disabling the external card clock. Doing this fixes a spurious clock pulse on Baytrail and Apollo Lake SD controllers which otherwise breaks voltage switching with a specific Swissbit SD card.

Quoted 26 lines
---
 drivers/mmc/host/sdhci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/mmc/host/sdhci.c +7 -2
index f4a7733a8ad2..5f91b44891f9 100644
@@ -2065,10 +2065,15 @@ void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
 
 	host->mmc->actual_clock = 0;
 
-	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
+	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
+	if (clk & SDHCI_CLOCK_CARD_EN)
+		sdhci_writew(host, clk & ~SDHCI_CLOCK_CARD_EN,
+			SDHCI_CLOCK_CONTROL);
 
-	if (clock == 0)
+	if (clock == 0) {
+		sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
 		return;
+	}
 
 	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
 	sdhci_enable_clk(host, clk);
#2 lore raw
In reply to Erick Shepherd
Quoted 10 lines · Erick Shepherd

Per the SD Host Controller Simplified Specification v4.20 §3.2.3, change the SD card clock parameters only after first disabling the external card clock. Doing this fixes a spurious clock pulse on Baytrail and Apollo Lake SD controllers which otherwise breaks voltage switching with a specific Swissbit SD card.

Applied for next, thanks!

Kind regards
Uffe

Quoted 29 lines
---
 drivers/mmc/host/sdhci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/mmc/host/sdhci.c +7 -3
index f4a7733a8ad2..5f91b44891f9 100644
@@ -2065,10 +2065,15 @@ void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
 
        host->mmc->actual_clock = 0;
 
-       sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
+       clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
+       if (clk & SDHCI_CLOCK_CARD_EN)
+               sdhci_writew(host, clk & ~SDHCI_CLOCK_CARD_EN,
+                       SDHCI_CLOCK_CONTROL);
 
-       if (clock == 0)
+       if (clock == 0) {
+               sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
                return;
+       }
 
        clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
        sdhci_enable_clk(host, clk);
--

2.43.0

#3 lore raw
In reply to Erick Shepherd

Hi Erick,

Quoted 37 lines · Erick Shepherd

Per the SD Host Controller Simplified Specification v4.20 §3.2.3, change the SD card clock parameters only after first disabling the external card clock. Doing this fixes a spurious clock pulse on Baytrail and Apollo Lake SD controllers which otherwise breaks voltage switching with a specific Swissbit SD card.

---
 drivers/mmc/host/sdhci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/mmc/host/sdhci.c +7 -3
index f4a7733a8ad2..5f91b44891f9 100644
@@ -2065,10 +2065,15 @@ void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
 
        host->mmc->actual_clock = 0;
 
-       sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
+       clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
+       if (clk & SDHCI_CLOCK_CARD_EN)
+               sdhci_writew(host, clk & ~SDHCI_CLOCK_CARD_EN,
+                       SDHCI_CLOCK_CONTROL);
 
-       if (clock == 0)
+       if (clock == 0) {
+               sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
                return;
+       }
 
        clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
        sdhci_enable_clk(host, clk);
--
Quoted 1 line

2.43.0

This commit is causing eMMC to sometimes fail to detect on my RK3399 ARM64 board after updating from v5.10.237 to v5.10.238. Any ideas?

Regards,
Jonathan

maintainer
#4 lore raw
Quoted 37 lines · Jonathan Liu

Hi Erick,

Quoted 34 lines · Erick Shepherd

Per the SD Host Controller Simplified Specification v4.20 §3.2.3, change the SD card clock parameters only after first disabling the external card clock. Doing this fixes a spurious clock pulse on Baytrail and Apollo Lake SD controllers which otherwise breaks voltage switching with a specific Swissbit SD card.

---
 drivers/mmc/host/sdhci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/mmc/host/sdhci.c +7 -2
index f4a7733a8ad2..5f91b44891f9 100644
@@ -2065,10 +2065,15 @@ void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
 
        host->mmc->actual_clock = 0;
 
-       sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
+       clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
+       if (clk & SDHCI_CLOCK_CARD_EN)
+               sdhci_writew(host, clk & ~SDHCI_CLOCK_CARD_EN,
+                       SDHCI_CLOCK_CONTROL);
 
-       if (clock == 0)
+       if (clock == 0) {
+               sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
                return;
+       }
 
Quoted 8 lines
Quoted 4 lines
drivers/mmc/host/sdhci.c +0 -1
        clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
        sdhci_enable_clk(host, clk);
--

2.43.0

This commit is causing eMMC to sometimes fail to detect on my RK3399 ARM64 board after updating from v5.10.237 to v5.10.238. Any ideas?

Looks like this patch will get reverted.

After that, would something like the following work instead?

drivers/mmc/host/sdhci-pci-core.c +13 -2
index dd084c8750f9..ad95218fe3b3 100644
@@ -679,8 +679,19 @@ static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
 	return 0;
 }
 
+static void sdhci_intel_set_clock(struct sdhci_host *host, unsigned int clock)
+{
+	u16 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
+
+	/* Stop card clock separately to avoid glitches on clock line */
+	if (clk & SDHCI_CLOCK_CARD_EN)
+		sdhci_writew(host, clk & ~SDHCI_CLOCK_CARD_EN, SDHCI_CLOCK_CONTROL);
+
+	sdhci_set_clock(host, clock);
+}
+
 static const struct sdhci_ops sdhci_intel_byt_ops = {
-	.set_clock		= sdhci_set_clock,
+	.set_clock		= sdhci_intel_set_clock,
 	.set_power		= sdhci_intel_set_power,
 	.enable_dma		= sdhci_pci_enable_dma,
 	.set_bus_width		= sdhci_set_bus_width,
@@ -690,7 +701,7 @@ static const struct sdhci_ops sdhci_intel_byt_ops = {
 };
 
 static const struct sdhci_ops sdhci_intel_glk_ops = {
-	.set_clock		= sdhci_set_clock,
+	.set_clock		= sdhci_intel_set_clock,
 	.set_power		= sdhci_intel_set_power,
 	.enable_dma		= sdhci_pci_enable_dma,
 	.set_bus_width		= sdhci_set_bus_width,
#5 lore raw

Hi Adrian,

I believe this fix would work since it looks like it keeps the functionality of the previous change but only for intel SD controllers. I also tested your proposed change on one of our devices with a Baytrail SD controller and a Swissbit SD card and did not run into any issues.

Regards,
Erick

maintainer
#6 lore raw
Quoted 7 lines · Erick Shepherd

Hi Adrian,

I believe this fix would work since it looks like it keeps the functionality of the previous change but only for intel SD controllers. I also tested your proposed change on one of our devices with a Baytrail SD controller and a Swissbit SD card and did not run into any issues.

If you are OK with it, please submit it as a patch