Skip to contentSkip to conversation

mmc: spacemit: Improve clock and tuning logic

PATCH Superseded Yixun Lan 1 participant 9 messages Dec 1

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.

I've rebased this series on top of "Introduce tx delay DT property" [1], otherwise there is code conflicts when apply both.

---
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: cafaabe9e6fd91d15c6265bf5858272aab8431b3
change-id: 20260901-07-k3-sdhci-fix-ce577d1db7b6

Best regards,

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

PATCH 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 ee613e1417fa..010a871c7f25 100644
@@ -411,8 +411,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
author maintainer
#2 lore raw

PATCH 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 010a871c7f25..3169f360399e 100644
@@ -449,11 +449,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
author maintainer
#3 lore raw

PATCH 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 3169f360399e..9baa82ced321 100644
@@ -196,9 +196,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
author maintainer
#4 lore raw

PATCH 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 9baa82ced321..ce0c58e0b3c2 100644
@@ -512,6 +512,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
author maintainer
#5 lore raw

PATCH 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 ce0c58e0b3c2..6e8d7cc381e4 100644
@@ -170,21 +170,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
author maintainer
#6 lore raw

PATCH 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 6e8d7cc381e4..3e62c6e708ca 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
author maintainer
#7 lore raw

PATCH 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 3e62c6e708ca..e573d739f350 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
@@ -320,7 +321,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
author maintainer
#8 lore raw

PATCH 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 e573d739f350..d8d70a0d4857 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>
@@ -164,6 +165,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);
@@ -174,9 +185,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,
@@ -213,6 +221,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)
@@ -512,6 +534,23 @@ static void spacemit_sdhci_get_tuning_params(struct device *dev,
 	device_property_read_u8(dev, "spacemit,tx-delay", &sdhst->tx_delaycode);
 }
 
+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,
@@ -576,8 +615,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