Skip to contentSkip to conversation

mmc: spacemit: Improve clock and tuning logic

PATCH RESEND In review Yixun Lan 3 participants 19 messages Dec 1 – Dec 3

Subsystems (4)
lore
Share & tools
lore

Yixun Lan

author maintainer
#0 lore raw

v2 cover mmc: spacemit: Improve clock and tuning logic

This series includes some bug fixes and improvement to SpacemiT SDHCI driver, which relate to pinctrl, PHY, clock, tuning logic. Most of them are quite independent, but put them together here for people to easy review.

Reason for the RESEND:
Rebasing on top of "Introduce tx delay DT property" [1],
it would make sashiko bot fail to apply [8/8], so decouple the dependency and resend base on tag v7.4-rc1.

---
Changes in v2:
- Fix clock defer probe error
- Fix pinctrl state check
- Add macro for tx_tuning window limit
- Link to v1: https://patch.msgid.link/[email protected]

---
Long Wan (6):
mmc: sdhci-of-k1: No pinctrl state switching
mmc: sdhci-of-k1: Improve power control
mmc: sdhci-of-k1: Configure PHY mode
mmc: sdhci-of-k1: Use finest delay granularity for RX tuning
mmc: sdhci-of-k1: Improve RX tuning window
mmc: sdhci-of-k1: Implement SD clock gating for voltage switch

Yixun Lan (2):
mmc: sdhci-of-k1: Handle clock probe deferral
mmc: sdhci-of-k1: Drop duplicated voltage settings

 drivers/mmc/host/sdhci-of-k1.c | 84 +++++++++++++++++++++++++++++++++---------
 1 file changed, 66 insertions(+), 18 deletions(-)

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260901-07-k3-sdhci-fix-ce577d1db7b6

Best regards,

Signature · 1 line
Yixun Lan <[email protected]>
author maintainer
#1 lore raw

PATCH RESEND v2 1/8 mmc: sdhci-of-k1: No pinctrl state switching

From: Long Wan <[email protected]>

No need to switch pinctrl state if the SDHCI controller only support 1.8v IO voltage which DT will only provide a "default" pinctrl state, otherwise function pinctrl_select_state() will trigger a NULL pointer error.

Fixes 00a97fc57c09 ("mmc: sdhci-of-k1: add regulator and pinctrl voltage switching support")
---
 drivers/mmc/host/sdhci-of-k1.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/mmc/host/sdhci-of-k1.c +4 -0
index 37b0911e7cf2..9596d79fa97f 100644
@@ -407,8 +407,12 @@ static int spacemit_sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
 	if (ret)
 		return ret;
 
+	/* No pinctrl */
 	if (!sdhst->pinctrl)
 		return 0;
+	/* No "uhs" state */
+	if (!sdhst->pinctrl_uhs)
+		return 0;
 
 	/* Select appropriate pinctrl state based on signal voltage */
 	switch (ios->signal_voltage) {
Signature · 1 line
2.56.0
maintainer Acked-by on v2 1/8
#2 lore raw

Re: [PATCH RESEND v2 1/8] mmc: sdhci-of-k1: No pinctrl state switching

Quoted 9 lines · Yixun Lan

From: Long Wan <[email protected]>

No need to switch pinctrl state if the SDHCI controller only support 1.8v IO voltage which DT will only provide a "default" pinctrl state, otherwise function pinctrl_select_state() will trigger a NULL pointer error.

Fixes 00a97fc57c09 ("mmc: sdhci-of-k1: add regulator and pinctrl voltage switching support")
Quoted 22 lines
---
 drivers/mmc/host/sdhci-of-k1.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/mmc/host/sdhci-of-k1.c +4 -0
index 37b0911e7cf2..9596d79fa97f 100644
@@ -407,8 +407,12 @@ static int spacemit_sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
 	if (ret)
 		return ret;
 
+	/* No pinctrl */
 	if (!sdhst->pinctrl)
 		return 0;
+	/* No "uhs" state */
+	if (!sdhst->pinctrl_uhs)
+		return 0;
 
 	/* Select appropriate pinctrl state based on signal voltage */
 	switch (ios->signal_voltage) {
author maintainer
#3 lore raw

PATCH RESEND v2 2/8 mmc: sdhci-of-k1: Handle clock probe deferral

In reply to Yixun Lan

Propagate the error code when clock subsystem isn't ready, so it will fallback to proper probe deferral mechanism.

Fixes e5502d15b0f3 ("mmc: sdhci-of-k1: add support for SpacemiT K1 SoC")
---
 drivers/mmc/host/sdhci-of-k1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/mmc/host/sdhci-of-k1.c +2 -2
index 9596d79fa97f..68b9958f8683 100644
@@ -445,11 +445,11 @@ static inline int spacemit_sdhci_get_clocks(struct device *dev,
 
 	sdhst->clk_core = devm_clk_get_enabled(dev, "core");
 	if (IS_ERR(sdhst->clk_core))
-		return -EINVAL;
+		return PTR_ERR(sdhst->clk_core);
 
 	sdhst->clk_io = devm_clk_get_enabled(dev, "io");
 	if (IS_ERR(sdhst->clk_io))
-		return -EINVAL;
+		return PTR_ERR(sdhst->clk_io);
 
 	pltfm_host->clk = sdhst->clk_io;
 
Signature · 1 line
2.56.0
maintainer Acked-by on v2 2/8
#4 lore raw

Re: [PATCH RESEND v2 2/8] mmc: sdhci-of-k1: Handle clock probe deferral

Quoted 5 lines · Yixun Lan

Propagate the error code when clock subsystem isn't ready, so it will fallback to proper probe deferral mechanism.

Fixes e5502d15b0f3 ("mmc: sdhci-of-k1: add support for SpacemiT K1 SoC")
Quoted 23 lines
---
 drivers/mmc/host/sdhci-of-k1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/mmc/host/sdhci-of-k1.c +2 -2
index 9596d79fa97f..68b9958f8683 100644
@@ -445,11 +445,11 @@ static inline int spacemit_sdhci_get_clocks(struct device *dev,
 
 	sdhst->clk_core = devm_clk_get_enabled(dev, "core");
 	if (IS_ERR(sdhst->clk_core))
-		return -EINVAL;
+		return PTR_ERR(sdhst->clk_core);
 
 	sdhst->clk_io = devm_clk_get_enabled(dev, "io");
 	if (IS_ERR(sdhst->clk_io))
-		return -EINVAL;
+		return PTR_ERR(sdhst->clk_io);
 
 	pltfm_host->clk = sdhst->clk_io;
 
author maintainer
#5 lore raw

PATCH RESEND v2 3/8 mmc: sdhci-of-k1: Drop duplicated voltage settings

In reply to Yixun Lan

No need to set 1.8V Signal for non-SDIO hosts, it will duplicate what sdhci_start_signal_voltage_switch() already handled there.

Fixes e5502d15b0f3 ("mmc: sdhci-of-k1: add support for SpacemiT K1 SoC")
---
 drivers/mmc/host/sdhci-of-k1.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/mmc/host/sdhci-of-k1.c +0 -3
index 68b9958f8683..f86f3636f5a9 100644
@@ -195,9 +195,6 @@ static void spacemit_sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned i
 		spacemit_sdhci_setbits(host, SDHC_MMC_HS400, SPACEMIT_SDHC_MMC_CTRL_REG);
 
 	sdhci_set_uhs_signaling(host, timing);
-
-	if (!(host->mmc->caps2 & MMC_CAP2_NO_SDIO))
-		spacemit_sdhci_setbits(host, SDHCI_CTRL_VDD_180, SDHCI_HOST_CONTROL2);
 }
 
 static void spacemit_sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Signature · 1 line
2.56.0
maintainer Acked-by on v2 3/8
#6 lore raw

Re: [PATCH RESEND v2 3/8] mmc: sdhci-of-k1: Drop duplicated voltage settings

Quoted 1 line · Yixun Lan

No need to set 1.8V Signal for non-SDIO hosts, it will duplicate

"!(host->mmc->caps2 & MMC_CAP2_NO_SDIO)" is a double negative so it is aimed at SDIO. Please clarify the commit message.

spacemit_sdhci_setbits() is for 32-bit registers but SDHCI_HOST_CONTROL2 is 16-bit, so it was wrong in that way also, which is worth a mention.

Otherwise:

Quoted 23 lines

what sdhci_start_signal_voltage_switch() already handled there.

Fixes e5502d15b0f3 ("mmc: sdhci-of-k1: add support for SpacemiT K1 SoC")
---
 drivers/mmc/host/sdhci-of-k1.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/mmc/host/sdhci-of-k1.c +0 -3
index 68b9958f8683..f86f3636f5a9 100644
@@ -195,9 +195,6 @@ static void spacemit_sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned i
 		spacemit_sdhci_setbits(host, SDHC_MMC_HS400, SPACEMIT_SDHC_MMC_CTRL_REG);
 
 	sdhci_set_uhs_signaling(host, timing);
-
-	if (!(host->mmc->caps2 & MMC_CAP2_NO_SDIO))
-		spacemit_sdhci_setbits(host, SDHCI_CTRL_VDD_180, SDHCI_HOST_CONTROL2);
 }
 
 static void spacemit_sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
author maintainer
#7 lore raw

PATCH RESEND v2 4/8 mmc: sdhci-of-k1: Improve power control

In reply to Yixun Lan

From: Long Wan <[email protected]>

The default sdhci_set_power() skips the SD bus voltage select field entirely when a vmmc regulator is present, leaving SD Bus Power in off state of the power control register. To fix this, configure the bus voltage explicitly.

Fixes e5502d15b0f3 ("mmc: sdhci-of-k1: add support for SpacemiT K1 SoC")
---
 drivers/mmc/host/sdhci-of-k1.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mmc/host/sdhci-of-k1.c +1 -0
index f86f3636f5a9..5fb4692b58cd 100644
@@ -497,6 +497,7 @@ static const struct sdhci_ops spacemit_sdhci_ops = {
 	.reset			= spacemit_sdhci_reset,
 	.set_bus_width		= sdhci_set_bus_width,
 	.set_clock		= spacemit_sdhci_set_clock,
+	.set_power		= sdhci_set_power_and_bus_voltage,
 	.set_uhs_signaling	= spacemit_sdhci_set_uhs_signaling,
 	.platform_execute_tuning = spacemit_sdhci_execute_tuning,
 };
Signature · 1 line
2.56.0
maintainer Acked-by on v2 4/8
#8 lore raw

Re: [PATCH RESEND v2 4/8] mmc: sdhci-of-k1: Improve power control

Quoted 10 lines · Yixun Lan

From: Long Wan <[email protected]>

The default sdhci_set_power() skips the SD bus voltage select field entirely when a vmmc regulator is present, leaving SD Bus Power in off state of the power control register. To fix this, configure the bus voltage explicitly.

Fixes e5502d15b0f3 ("mmc: sdhci-of-k1: add support for SpacemiT K1 SoC")
Quoted 17 lines
---
 drivers/mmc/host/sdhci-of-k1.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mmc/host/sdhci-of-k1.c +1 -0
index f86f3636f5a9..5fb4692b58cd 100644
@@ -497,6 +497,7 @@ static const struct sdhci_ops spacemit_sdhci_ops = {
 	.reset			= spacemit_sdhci_reset,
 	.set_bus_width		= sdhci_set_bus_width,
 	.set_clock		= spacemit_sdhci_set_clock,
+	.set_power		= sdhci_set_power_and_bus_voltage,
 	.set_uhs_signaling	= spacemit_sdhci_set_uhs_signaling,
 	.platform_execute_tuning = spacemit_sdhci_execute_tuning,
 };
maintainer
#9 lore raw

Re: [PATCH RESEND v2 4/8] mmc: sdhci-of-k1: Improve power control

Quoted 8 lines · Adrian Hunter
Quoted 7 lines · Yixun Lan

From: Long Wan <[email protected]>

The default sdhci_set_power() skips the SD bus voltage select field entirely when a vmmc regulator is present, leaving SD Bus Power in off state of the power control register. To fix this, configure the bus voltage explicitly.

Quoted 5 lines
Quoted 3 lines
Fixes e5502d15b0f3 ("mmc: sdhci-of-k1: add support for SpacemiT K1 SoC")

Although it would be worth explaining in the commit message how the issue manifests i.e. what happens without this fix?

Quoted 19 lines
Quoted 18 lines
---
 drivers/mmc/host/sdhci-of-k1.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mmc/host/sdhci-of-k1.c +1 -0
index f86f3636f5a9..5fb4692b58cd 100644
@@ -497,6 +497,7 @@ static const struct sdhci_ops spacemit_sdhci_ops = {
 	.reset			= spacemit_sdhci_reset,
 	.set_bus_width		= sdhci_set_bus_width,
 	.set_clock		= spacemit_sdhci_set_clock,
+	.set_power		= sdhci_set_power_and_bus_voltage,
 	.set_uhs_signaling	= spacemit_sdhci_set_uhs_signaling,
 	.platform_execute_tuning = spacemit_sdhci_execute_tuning,
 };
author maintainer
#10 lore raw

PATCH RESEND v2 5/8 mmc: sdhci-of-k1: Configure PHY mode

In reply to Yixun Lan

From: Long Wan <[email protected]>

Apply PHY configuration only for eMMC hosts which enable PHY function mode, and configure drive strength and bias. While for SD/SDIO hosts mode, bypass the PHY mode and select TX internal clock instead.

Fixes f87b273e4b6d ("mmc: sdhci-of-k1: enable essential clock infrastructure for SD operation")
---
 drivers/mmc/host/sdhci-of-k1.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/mmc/host/sdhci-of-k1.c +14 -10
index 5fb4692b58cd..01bc0f4bd6de 100644
@@ -169,21 +169,25 @@ static void spacemit_sdhci_reset(struct sdhci_host *host, u8 mask)
 	if (mask != SDHCI_RESET_ALL)
 		return;
 
-	spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK,
-			       SPACEMIT_SDHC_PHY_CTRL_REG);
+	if (host->mmc->caps2 & MMC_CAP2_NO_MMC) {
+		/* SD/SDIO: bypass PHY, use TX internal clock */
+		spacemit_sdhci_setbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG);
+
+		spacemit_sdhci_setbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
+				       SPACEMIT_SDHC_OP_EXT_REG);
+	} else {
+		/* eMMC: use PHY function mode */
+		spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK,
+				       SPACEMIT_SDHC_PHY_CTRL_REG);
 
-	spacemit_sdhci_clrsetbits(host, SDHC_PHY_DRIVE_SEL,
-				  SDHC_RX_BIAS_CTRL | FIELD_PREP(SDHC_PHY_DRIVE_SEL, 4),
-				  SPACEMIT_SDHC_PHY_PADCFG_REG);
+		spacemit_sdhci_clrsetbits(host, SDHC_PHY_DRIVE_SEL,
+					  SDHC_RX_BIAS_CTRL | FIELD_PREP(SDHC_PHY_DRIVE_SEL, 4),
+					  SPACEMIT_SDHC_PHY_PADCFG_REG);
 
-	if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC))
 		spacemit_sdhci_setbits(host, SDHC_MMC_CARD_MODE, SPACEMIT_SDHC_MMC_CTRL_REG);
+	}
 
 	spacemit_sdhci_setbits(host, SDHC_GEN_PAD_CLK_ON, SPACEMIT_SDHC_LEGACY_CTRL_REG);
-
-	if (host->mmc->caps2 & MMC_CAP2_NO_MMC)
-		spacemit_sdhci_setbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
-				       SPACEMIT_SDHC_OP_EXT_REG);
 }
 
 static void spacemit_sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned int timing)
Signature · 1 line
2.56.0
maintainer Acked-by on v2 5/8
#11 lore raw

Re: [PATCH RESEND v2 5/8] mmc: sdhci-of-k1: Configure PHY mode

Quoted 5 lines · Yixun Lan

From: Long Wan <[email protected]>

Apply PHY configuration only for eMMC hosts which enable PHY function mode, and configure drive strength and bias. While for SD/SDIO hosts mode, bypass the PHY mode and select TX internal clock instead.

Could use an explanation of what happens without this fix.

Quoted 4 lines
Fixes f87b273e4b6d ("mmc: sdhci-of-k1: enable essential clock infrastructure for SD operation")

Otherwise:

Quoted 45 lines
---
 drivers/mmc/host/sdhci-of-k1.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/mmc/host/sdhci-of-k1.c +14 -10
index 5fb4692b58cd..01bc0f4bd6de 100644
@@ -169,21 +169,25 @@ static void spacemit_sdhci_reset(struct sdhci_host *host, u8 mask)
 	if (mask != SDHCI_RESET_ALL)
 		return;
 
-	spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK,
-			       SPACEMIT_SDHC_PHY_CTRL_REG);
+	if (host->mmc->caps2 & MMC_CAP2_NO_MMC) {
+		/* SD/SDIO: bypass PHY, use TX internal clock */
+		spacemit_sdhci_setbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG);
+
+		spacemit_sdhci_setbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
+				       SPACEMIT_SDHC_OP_EXT_REG);
+	} else {
+		/* eMMC: use PHY function mode */
+		spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK,
+				       SPACEMIT_SDHC_PHY_CTRL_REG);
 
-	spacemit_sdhci_clrsetbits(host, SDHC_PHY_DRIVE_SEL,
-				  SDHC_RX_BIAS_CTRL | FIELD_PREP(SDHC_PHY_DRIVE_SEL, 4),
-				  SPACEMIT_SDHC_PHY_PADCFG_REG);
+		spacemit_sdhci_clrsetbits(host, SDHC_PHY_DRIVE_SEL,
+					  SDHC_RX_BIAS_CTRL | FIELD_PREP(SDHC_PHY_DRIVE_SEL, 4),
+					  SPACEMIT_SDHC_PHY_PADCFG_REG);
 
-	if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC))
 		spacemit_sdhci_setbits(host, SDHC_MMC_CARD_MODE, SPACEMIT_SDHC_MMC_CTRL_REG);
+	}
 
 	spacemit_sdhci_setbits(host, SDHC_GEN_PAD_CLK_ON, SPACEMIT_SDHC_LEGACY_CTRL_REG);
-
-	if (host->mmc->caps2 & MMC_CAP2_NO_MMC)
-		spacemit_sdhci_setbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
-				       SPACEMIT_SDHC_OP_EXT_REG);
 }
 
 static void spacemit_sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned int timing)
author maintainer
#12 lore raw

PATCH RESEND v2 6/8 mmc: sdhci-of-k1: Use finest delay granularity for RX tuning

In reply to Yixun Lan

From: Long Wan <[email protected]>

Initialize the RX delay line to 0 (finest step size) instead of 9. This improves tuning precision by allowing smaller delay increments when searching for the optimal sampling point.

Fixes e9cb83c10071 ("mmc: sdhci-of-k1: add comprehensive SDR tuning support")
---
 drivers/mmc/host/sdhci-of-k1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/host/sdhci-of-k1.c +1 -1
index 01bc0f4bd6de..ba01434284eb 100644
@@ -84,7 +84,7 @@
 #define  SDHC_RX_DLINE_GAIN             BIT(8)
 #define  SDHC_TX_DLINE_REG_MASK         GENMASK(23, 16)
 
-#define SPACEMIT_RX_DLINE_REG		9
+#define SPACEMIT_RX_DLINE_REG		0
 #define SPACEMIT_RX_TUNE_DELAY_MIN	0x0
 #define SPACEMIT_RX_TUNE_DELAY_MAX	0xFF
 
Signature · 1 line
2.56.0
maintainer
#13 lore raw

Re: [PATCH RESEND v2 6/8] mmc: sdhci-of-k1: Use finest delay granularity for RX tuning

Quoted 5 lines · Yixun Lan

From: Long Wan <[email protected]>

Initialize the RX delay line to 0 (finest step size) instead of 9. This improves tuning precision by allowing smaller delay increments when searching for the optimal sampling point.

If the step size is smaller, does there also need to be more steps somewhere e.g. the next patch?

Also what happens without this fix?

Quoted 22 lines
Fixes e9cb83c10071 ("mmc: sdhci-of-k1: add comprehensive SDR tuning support")
---
 drivers/mmc/host/sdhci-of-k1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/host/sdhci-of-k1.c +1 -1
index 01bc0f4bd6de..ba01434284eb 100644
@@ -84,7 +84,7 @@
 #define  SDHC_RX_DLINE_GAIN             BIT(8)
 #define  SDHC_TX_DLINE_REG_MASK         GENMASK(23, 16)
 
-#define SPACEMIT_RX_DLINE_REG		9
+#define SPACEMIT_RX_DLINE_REG		0
 #define SPACEMIT_RX_TUNE_DELAY_MIN	0x0
 #define SPACEMIT_RX_TUNE_DELAY_MAX	0xFF
 
author maintainer
#14 lore raw

PATCH RESEND v2 7/8 mmc: sdhci-of-k1: Improve RX tuning window

In reply to Yixun Lan

From: Long Wan <[email protected]>

Raise the minimum delay codes of RX tuning window from 3 to 50, to more accurately retrieve a valid configuration.

A window of 3 codes wide leaves no sampling margin, which will result tuning tests reporting success on a configuration that drifts out of the window under thermal or power variation.

Fixes e9cb83c10071 ("mmc: sdhci-of-k1: add comprehensive SDR tuning support")
---
 drivers/mmc/host/sdhci-of-k1.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mmc/host/sdhci-of-k1.c +2 -1
index ba01434284eb..9f15cc6a4a8c 100644
@@ -87,6 +87,7 @@
 #define SPACEMIT_RX_DLINE_REG		0
 #define SPACEMIT_RX_TUNE_DELAY_MIN	0x0
 #define SPACEMIT_RX_TUNE_DELAY_MAX	0xFF
+#define SPACEMIT_RX_TUNE_WINDOW_LIMIT	50
 
 #define SPACEMIT_TX_TUNING_DLINE_REG	0x00
 #define SPACEMIT_TX_TUNING_DELAYCODE	127
@@ -316,7 +317,7 @@ static int spacemit_sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
 		max_pass_start = current_start;
 	}
 
-	if (max_pass_len < 3) {
+	if (max_pass_len < SPACEMIT_RX_TUNE_WINDOW_LIMIT) {
 		dev_err(mmc_dev(host->mmc), "Tuning failed: no stable window found\n");
 		return -EIO;
 	}
Signature · 1 line
2.56.0
maintainer
#15 lore raw

Re: [PATCH RESEND v2 7/8] mmc: sdhci-of-k1: Improve RX tuning window

Quoted 8 lines · Yixun Lan

From: Long Wan <[email protected]>

Raise the minimum delay codes of RX tuning window from 3 to 50, to more accurately retrieve a valid configuration.

A window of 3 codes wide leaves no sampling margin, which will result tuning tests reporting success on a configuration that drifts out of the window under thermal or power variation.

How does more readily returning an error help?

Quoted 30 lines
Fixes e9cb83c10071 ("mmc: sdhci-of-k1: add comprehensive SDR tuning support")
---
 drivers/mmc/host/sdhci-of-k1.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mmc/host/sdhci-of-k1.c +2 -1
index ba01434284eb..9f15cc6a4a8c 100644
@@ -87,6 +87,7 @@
 #define SPACEMIT_RX_DLINE_REG		0
 #define SPACEMIT_RX_TUNE_DELAY_MIN	0x0
 #define SPACEMIT_RX_TUNE_DELAY_MAX	0xFF
+#define SPACEMIT_RX_TUNE_WINDOW_LIMIT	50
 
 #define SPACEMIT_TX_TUNING_DLINE_REG	0x00
 #define SPACEMIT_TX_TUNING_DELAYCODE	127
@@ -316,7 +317,7 @@ static int spacemit_sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
 		max_pass_start = current_start;
 	}
 
-	if (max_pass_len < 3) {
+	if (max_pass_len < SPACEMIT_RX_TUNE_WINDOW_LIMIT) {
 		dev_err(mmc_dev(host->mmc), "Tuning failed: no stable window found\n");
 		return -EIO;
 	}
author maintainer
#16 lore raw

PATCH RESEND v2 8/8 mmc: sdhci-of-k1: Implement SD clock gating for voltage switch

In reply to Yixun Lan

From: Long Wan <[email protected]>

reset() permanently set OVRRD_CLK_OEN and FORCE_CLK_ON for SD/SDIO hosts and never cleared them, so the SD clock was forced on for the lifetime of the controller and hardware auto gating never took effect.

These bits are only needed during the CMD11 1.8V voltage switch, where the clock has to keep running so DAT[3:0] can be sampled after the switch. Add a set_clk_gate() helper, force the clock on from set_clock() when CMD11 is in flight, and restore auto gating from card_busy() once the switch completes.

card_busy() is registered for all card types since the CMD11 sequence is an SD/SDIO path and the DAT[0] level check is generic.

---
 drivers/mmc/host/sdhci-of-k1.c | 49 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 4 deletions(-)
drivers/mmc/host/sdhci-of-k1.c +45 -4
index 9f15cc6a4a8c..e99e972f006b 100644
@@ -12,6 +12,7 @@
 #include <linux/mmc/card.h>
 #include <linux/mmc/host.h>
 #include <linux/mmc/mmc.h>
+#include <linux/mmc/sd.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
@@ -163,6 +164,16 @@ static void spacemit_sdhci_prepare_tuning(struct sdhci_host *host)
 		spacemit_sdhci_setbits(host, SDHC_HS200_USE_RFIFO, SPACEMIT_SDHC_PHY_FUNC_REG);
 }
 
+static void spacemit_sdhci_set_clk_gate(struct sdhci_host *host, unsigned int auto_gate)
+{
+	if (auto_gate)
+		spacemit_sdhci_clrbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
+				       SPACEMIT_SDHC_OP_EXT_REG);
+	else
+		spacemit_sdhci_setbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
+				       SPACEMIT_SDHC_OP_EXT_REG);
+}
+
 static void spacemit_sdhci_reset(struct sdhci_host *host, u8 mask)
 {
 	sdhci_reset(host, mask);
@@ -173,9 +184,6 @@ static void spacemit_sdhci_reset(struct sdhci_host *host, u8 mask)
 	if (host->mmc->caps2 & MMC_CAP2_NO_MMC) {
 		/* SD/SDIO: bypass PHY, use TX internal clock */
 		spacemit_sdhci_setbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG);
-
-		spacemit_sdhci_setbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
-				       SPACEMIT_SDHC_OP_EXT_REG);
 	} else {
 		/* eMMC: use PHY function mode */
 		spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK,
@@ -212,6 +220,20 @@ static void spacemit_sdhci_set_clock(struct sdhci_host *host, unsigned int clock
 		spacemit_sdhci_clrbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG);
 
 	sdhci_set_clock(host, clock);
+
+	if (host->mmc->caps2 & MMC_CAP2_NO_MMC) {
+		/*
+		 * During CMD11 voltage switch to 1.8V, temporarily force the
+		 * clock on to ensure DAT[3:0] can be sampled correctly after
+		 * the switch. Auto gating is restored in card_busy() once the
+		 * switch completes.
+		 */
+		if ((SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) == SD_SWITCH_VOLTAGE) &&
+		    host->mmc->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
+			if (clock)
+				spacemit_sdhci_set_clk_gate(host, 0);
+		}
+	}
 };
 
 static void spacemit_sdhci_phy_dll_init(struct sdhci_host *host)
@@ -497,6 +519,23 @@ static inline void spacemit_sdhci_get_pins(struct device *dev,
 		sdhst->pinctrl_default, sdhst->pinctrl_uhs);
 }
 
+static int spacemit_sdhci_card_busy(struct mmc_host *mmc)
+{
+	struct sdhci_host *host = mmc_priv(mmc);
+	u32 present_state;
+
+	present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
+
+	if (host->mmc->caps2 & MMC_CAP2_NO_MMC) {
+		if ((SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) == SD_SWITCH_VOLTAGE) &&
+		    host->mmc->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_180)
+			/* Recover auto clock after voltage switch */
+			spacemit_sdhci_set_clk_gate(host, 1);
+	}
+
+	return !(present_state & SDHCI_DATA_0_LVL_MASK);
+}
+
 static const struct sdhci_ops spacemit_sdhci_ops = {
 	.get_max_clock		= spacemit_sdhci_clk_get_max_clock,
 	.reset			= spacemit_sdhci_reset,
@@ -561,8 +600,10 @@ static int spacemit_sdhci_probe(struct platform_device *pdev)
 
 	sdhci_get_of_property(pdev);
 
+	mops = &host->mmc_host_ops;
+	mops->card_busy = spacemit_sdhci_card_busy;
+
 	if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC)) {
-		mops = &host->mmc_host_ops;
 		mops->hs400_prepare_ddr	= spacemit_sdhci_pre_select_hs400;
 		mops->hs400_complete	= spacemit_sdhci_post_select_hs400;
 		mops->hs400_downgrade	= spacemit_sdhci_pre_hs400_to_hs200;
Signature · 1 line
2.56.0
maintainer
#17 lore raw

Re: [PATCH RESEND v2 8/8] mmc: sdhci-of-k1: Implement SD clock gating for voltage switch

Quoted 6 lines · Yixun Lan

From: Long Wan <[email protected]>

reset() permanently set OVRRD_CLK_OEN and FORCE_CLK_ON for SD/SDIO hosts and never cleared them, so the SD clock was forced on for the lifetime of the controller and hardware auto gating never took effect.

Quoted 8 lines

These bits are only needed during the CMD11 1.8V voltage switch, where the clock has to keep running so DAT[3:0] can be sampled after the switch. Add a set_clk_gate() helper, force the clock on from set_clock() when CMD11 is in flight, and restore auto gating from card_busy() once the switch completes.

card_busy() is registered for all card types since the CMD11 sequence is an SD/SDIO path and the DAT[0] level check is generic.

That is a bit of a misuse of ->set_clock() and ->card_busy()

spacemit_sdhci_start_signal_voltage_switch() seems like a better place to turn off auto-clock-gating, and set a new flag in struct spacemit_sdhci_host so the driver knows it needs to turn it back on.

I'd suggest implementing ->request_done() and re-enable it there based on the new flag.

That way there is no need to look at SDHCI_COMMAND.

Quoted 104 lines
---
 drivers/mmc/host/sdhci-of-k1.c | 49 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 4 deletions(-)
drivers/mmc/host/sdhci-of-k1.c +45 -4
index 9f15cc6a4a8c..e99e972f006b 100644
@@ -12,6 +12,7 @@
 #include <linux/mmc/card.h>
 #include <linux/mmc/host.h>
 #include <linux/mmc/mmc.h>
+#include <linux/mmc/sd.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
@@ -163,6 +164,16 @@ static void spacemit_sdhci_prepare_tuning(struct sdhci_host *host)
 		spacemit_sdhci_setbits(host, SDHC_HS200_USE_RFIFO, SPACEMIT_SDHC_PHY_FUNC_REG);
 }
 
+static void spacemit_sdhci_set_clk_gate(struct sdhci_host *host, unsigned int auto_gate)
+{
+	if (auto_gate)
+		spacemit_sdhci_clrbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
+				       SPACEMIT_SDHC_OP_EXT_REG);
+	else
+		spacemit_sdhci_setbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
+				       SPACEMIT_SDHC_OP_EXT_REG);
+}
+
 static void spacemit_sdhci_reset(struct sdhci_host *host, u8 mask)
 {
 	sdhci_reset(host, mask);
@@ -173,9 +184,6 @@ static void spacemit_sdhci_reset(struct sdhci_host *host, u8 mask)
 	if (host->mmc->caps2 & MMC_CAP2_NO_MMC) {
 		/* SD/SDIO: bypass PHY, use TX internal clock */
 		spacemit_sdhci_setbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG);
-
-		spacemit_sdhci_setbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
-				       SPACEMIT_SDHC_OP_EXT_REG);
 	} else {
 		/* eMMC: use PHY function mode */
 		spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK,
@@ -212,6 +220,20 @@ static void spacemit_sdhci_set_clock(struct sdhci_host *host, unsigned int clock
 		spacemit_sdhci_clrbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG);
 
 	sdhci_set_clock(host, clock);
+
+	if (host->mmc->caps2 & MMC_CAP2_NO_MMC) {
+		/*
+		 * During CMD11 voltage switch to 1.8V, temporarily force the
+		 * clock on to ensure DAT[3:0] can be sampled correctly after
+		 * the switch. Auto gating is restored in card_busy() once the
+		 * switch completes.
+		 */
+		if ((SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) == SD_SWITCH_VOLTAGE) &&
+		    host->mmc->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
+			if (clock)
+				spacemit_sdhci_set_clk_gate(host, 0);
+		}
+	}
 };
 
 static void spacemit_sdhci_phy_dll_init(struct sdhci_host *host)
@@ -497,6 +519,23 @@ static inline void spacemit_sdhci_get_pins(struct device *dev,
 		sdhst->pinctrl_default, sdhst->pinctrl_uhs);
 }
 
+static int spacemit_sdhci_card_busy(struct mmc_host *mmc)
+{
+	struct sdhci_host *host = mmc_priv(mmc);
+	u32 present_state;
+
+	present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
+
+	if (host->mmc->caps2 & MMC_CAP2_NO_MMC) {
+		if ((SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) == SD_SWITCH_VOLTAGE) &&
+		    host->mmc->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_180)
+			/* Recover auto clock after voltage switch */
+			spacemit_sdhci_set_clk_gate(host, 1);
+	}
+
+	return !(present_state & SDHCI_DATA_0_LVL_MASK);
+}
+
 static const struct sdhci_ops spacemit_sdhci_ops = {
 	.get_max_clock		= spacemit_sdhci_clk_get_max_clock,
 	.reset			= spacemit_sdhci_reset,
@@ -561,8 +600,10 @@ static int spacemit_sdhci_probe(struct platform_device *pdev)
 
 	sdhci_get_of_property(pdev);
 
+	mops = &host->mmc_host_ops;
+	mops->card_busy = spacemit_sdhci_card_busy;
+
 	if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC)) {
-		mops = &host->mmc_host_ops;
 		mops->hs400_prepare_ddr	= spacemit_sdhci_pre_select_hs400;
 		mops->hs400_complete	= spacemit_sdhci_post_select_hs400;
 		mops->hs400_downgrade	= spacemit_sdhci_pre_hs400_to_hs200;
Tested-by on the v2 series
#18 lore raw
In reply to Yixun Lan

Hi Yixun,

Tested v2 (on top of the tx delay v2 series) on a Banana Pi BPI-F3, v7.4-rc5: no regression. The eMMC runs in HS400 enhanced strobe (1 GiB direct read at 200 MB/s, 8.7k IOPS 4k random), the SD card in high speed at 15.6 MB/s, and 5 unbind/bind cycles of the SD controller are clean. The tuning paths are not exercised on this board (HS400ES needs no tuning, and the SD card stays at 3.3 V). W=1 build is clean. Testing done with the help of Claude (AI assistant).

Frédéric