Skip to contentSkip to conversation

scsi: ufs: spacemit: k3: change to FASTAUTO before the FAST power mode

PATCH In review Thomas Roos 2 participants 2 messages Nov 24 – Nov 28

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

PATCH 1/1 scsi: ufs: spacemit: k3: change to FASTAUTO before the FAST power mode

The SpacemiT vendor kernel and U-Boot never change the link straight to
FAST: they change to FASTAUTO at the target gear/rate first, wait 100 ms, and only then to FAST. A direct FAST change kills the link at the M-PHY PLL rate change: on the K3 CoM260 IFX (YMTC YMUS8B5TH1A1C1) every mode except HS-G1 rate A fails with DL errors 0x80000002 / 0x80000001 / 0x80002000 and PA_ERR 0x80000010, and the disk is lost.

Do the FASTAUTO step in the PRE_CHANGE hook; the core then sends the FAST request. With this the link runs at HS-G3 rate B on two lanes.

Skip the step only when the link already runs in FAST with the requested parameters, judged by PA_PWRMODE and not by hba->pwr_info alone. After a LINERESET, ufshcd_err_handler() restores the power mode with ufshcd_config_pwr_mode(hba, &hba->pwr_info, UFSHCD_PMC_POLICY_FORCE):
the requested parameters equal hba->pwr_info, but the link has fallen back to a slow mode, and the forced FAST request would kill it again.

The step also writes ANA_HSGEAR_CTRL_ATTR = 0x25 and waits for the PLL lock, as ufs_spacemit_apply_dev_quirks() does at probe. Every POST_CHANGE clears the attribute, so without this a LINERESET recovery would run the FASTAUTO and FAST changes with a different analog setting than probe.

Assisted-by claude-opus-5-5

---
This applies on top of v8 of "scsi: ufs: spacemit: k3: Add UFS Host Controller driver":
https://lore.kernel.org/r/[email protected]

Yixun, feel free to fold it into the next version of the series.

Tested on a K3 CoM260 IFX (YMTC YMUS8B5TH1A1C1) on 7.3.4 with the equivalent v7-based driver: without the change only HS-G1 rate A works; with it the link runs at HS-G3 rate B x2 and the rootfs on UFS is stable. Builds without warnings at W=1 on top of v8.

The restore path was exercised with this patch: a test-only debugfs hook drops the link to PWM-G1 SLOWAUTO behind the core (raw reads fall from ~900 MB/s to 1 MB/s), then a PA generic error is injected through the ufshcd debugfs saved_uic_err. In 6 of 6 runs the restore went through FASTAUTO back to G3B FAST, with reads at 850-980 MB/s again and a matching write/read-back. That setup keeps the PHY on rate B, so it does not reproduce a LINERESET that also resets the PLL rate.

The FASTAUTO step open-codes the attribute writes of the core's ufshcd_dme_change_power_mode(). A core helper that takes an explicit PA_PWRMODE value and leaves hba->pwr_info alone would avoid that; I kept it in the driver for now.

---
 drivers/ufs/host/ufs-spacemit.c | 96 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
drivers/ufs/host/ufs-spacemit.c +96 -0
index eb2ad50..04380a0 100644
@@ -343,6 +343,87 @@ static int ufs_spacemit_negotiate_pwr_mode(struct ufs_hba *hba,
 	return ret;
 }
 
+/*
+ * As in the vendor kernel and U-Boot: change to FASTAUTO at the target
+ * gear/rate first and only then (from the core) to FAST. A direct FAST
+ * change kills the link after the M-PHY PLL rate change, so only HS-G1A
+ * would work.
+ */
+static int ufs_spacemit_fastauto_step(struct ufs_hba *hba,
+				      struct ufs_pa_layer_attr *p)
+{
+	int ret;
+
+	/*
+	 * Restore the value and PLL lock wait of ufs_spacemit_apply_dev_quirks()
+	 * at probe: every POST_CHANGE resets it, which matters for a LINERESET
+	 * recovery.
+	 */
+	ufshcd_dme_set(hba, UIC_ARG_MIB(ANA_HSGEAR_CTRL_ATTR), 0x25);
+	ret = ufs_spacemit_wait_mphy_pll_lock(hba);
+	if (ret)
+		return ret;
+
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), p->gear_rx);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), p->lane_rx);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), 1);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), p->gear_tx);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), p->lane_tx);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), 1);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), p->hs_rate);
+	/* as in ufshcd_dme_change_power_mode() */
+	if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) {
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
+			       DL_FC0ProtectionTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
+			       DL_TC0ReplayTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
+			       DL_AFC0ReqTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
+			       DL_FC1ProtectionTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
+			       DL_TC1ReplayTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
+			       DL_AFC1ReqTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
+			       DL_FC0ProtectionTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
+			       DL_TC0ReplayTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
+			       DL_AFC0ReqTimeOutVal_Default);
+	}
+
+	ret = ufshcd_uic_change_pwr_mode(hba, FASTAUTO_MODE << 4 | FASTAUTO_MODE);
+	if (ret)
+		return ret;
+	msleep(100);
+
+	return ufs_spacemit_wait_mphy_pll_lock(hba);
+}
+
+/*
+ * True if the link already runs in FAST with the gear, lanes and rate of
+ * @p, so no FASTAUTO step is needed. Read PA_PWRMODE rather than trust
+ * hba->pwr_info alone: after a LINERESET the error handler restores the
+ * mode with UFSHCD_PMC_POLICY_FORCE and parameters equal to hba->pwr_info
+ * while the link has fallen back to a slow mode, and the forced FAST
+ * request from there kills the link.
+ */
+static bool ufs_spacemit_link_in_fast(struct ufs_hba *hba,
+				      struct ufs_pa_layer_attr *p)
+{
+	u32 mode;
+
+	if (memcmp(p, &hba->pwr_info, sizeof(*p)))
+		return false;
+
+	if (ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &mode))
+		return false;
+
+	return ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK) == FAST_MODE &&
+	       (mode & PWRMODE_MASK) == FAST_MODE;
+}
+
 static int ufs_spacemit_pwr_change_notify(struct ufs_hba *hba,
 					  enum ufs_notify_change_status status,
 					  struct ufs_pa_layer_attr *dev_req_params)
@@ -356,6 +437,21 @@ static int ufs_spacemit_pwr_change_notify(struct ufs_hba *hba,
 
 	switch (status) {
 	case PRE_CHANGE:
+		/* the core sends the FAST request after this hook */
+		if (dev_req_params->pwr_rx == FAST_MODE &&
+		    dev_req_params->pwr_tx == FAST_MODE &&
+		    !ufs_spacemit_link_in_fast(hba, dev_req_params)) {
+			/*
+			 * The core ignores the result. If the FASTAUTO PMC
+			 * failed outside a PM operation, the link is marked
+			 * broken and the FAST request is not sent; if only the
+			 * PLL lock wait timed out, the link already runs at
+			 * the target rate.
+			 */
+			ret = ufs_spacemit_fastauto_step(hba, dev_req_params);
+			if (ret)
+				dev_warn(hba->dev, "FASTAUTO step failed: %d\n", ret);
+		}
 		break;
 	case POST_CHANGE:
 		ret = ufs_spacemit_wait_mphy_pll_lock(hba);

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261123-fastauto-306d20d17590
prerequisite-message-id: <[email protected]>
prerequisite-patch-id: 70385741c42f21fbc79b3ef967e48cb8214e5cd0
prerequisite-patch-id: 5198841accdd660ed1597e92dcdef85a6accc8cd

Best regards,

Signature · 1 line
Thomas Roos <[email protected]>
maintainer
#1 lore raw

Hi Thomas Roos,

Thanks for bringing up of this, and sending the patch.

Quoted 9 lines · On 09:11 Tue 24 Nov , Thomas Roos via B4 Relay wrote:

From: Thomas Roos <[email protected]>

The SpacemiT vendor kernel and U-Boot never change the link straight to
FAST: they change to FASTAUTO at the target gear/rate first, wait 100 ms, and only then to FAST. A direct FAST change kills the link at the M-PHY PLL rate change: on the K3 CoM260 IFX (YMTC YMUS8B5TH1A1C1) every mode except HS-G1 rate A fails with DL errors 0x80000002 / 0x80000001 / 0x80002000 and PA_ERR 0x80000010, and the disk is lost.

I actually do get report of this issue on YMTC UFS chip, but haven't seen
problem with Kingston UFS chip

Quoted 10 lines

Do the FASTAUTO step in the PRE_CHANGE hook; the core then sends the FAST request. With this the link runs at HS-G3 rate B on two lanes.

Skip the step only when the link already runs in FAST with the requested parameters, judged by PA_PWRMODE and not by hba->pwr_info alone. After a LINERESET, ufshcd_err_handler() restores the power mode with ufshcd_config_pwr_mode(hba, &hba->pwr_info, UFSHCD_PMC_POLICY_FORCE):
the requested parameters equal hba->pwr_info, but the link has fallen back to a slow mode, and the forced FAST request would kill it again.

Quoted 5 lines

The step also writes ANA_HSGEAR_CTRL_ATTR = 0x25 and waits for the PLL lock, as ufs_spacemit_apply_dev_quirks() does at probe. Every POST_CHANGE clears the attribute, so without this a LINERESET recovery would run the FASTAUTO and FAST changes with a different analog setting than probe.

Vendor provide similar patch for this, but I'm rather skeptical on it as a proper fix and ask them to clarify with IP/UFS chip vendor..

FYI, I'd expect to see basic UFS patch merged first, then we can address this issue separately. Besides, I do have several boards on hand, but none of them shipped with YMTC UFS, so need someone to help reviewing and
testing the patch.

Quoted 9 lines
Assisted-by claude-opus-5-5

---
This applies on top of v8 of "scsi: ufs: spacemit: k3: Add UFS Host Controller driver":
https://lore.kernel.org/r/[email protected]

Yixun, feel free to fold it into the next version of the series.

I'd prefer it iterated as a separate patch on top of mine, thanks

Quoted 2 lines

Tested on a K3 CoM260 IFX (YMTC YMUS8B5TH1A1C1) on 7.3.4 with the equivalent v7-based driver: without the change only HS-G1 rate A works;

Just curious, is "HS-G1 rate A" mode stable without this patch? or any specific change needed to change to this mode?

Quoted 146 lines

with it the link runs at HS-G3 rate B x2 and the rootfs on UFS is stable. Builds without warnings at W=1 on top of v8.

The restore path was exercised with this patch: a test-only debugfs hook drops the link to PWM-G1 SLOWAUTO behind the core (raw reads fall from ~900 MB/s to 1 MB/s), then a PA generic error is injected through the ufshcd debugfs saved_uic_err. In 6 of 6 runs the restore went through FASTAUTO back to G3B FAST, with reads at 850-980 MB/s again and a matching write/read-back. That setup keeps the PHY on rate B, so it does not reproduce a LINERESET that also resets the PLL rate.

The FASTAUTO step open-codes the attribute writes of the core's ufshcd_dme_change_power_mode(). A core helper that takes an explicit PA_PWRMODE value and leaves hba->pwr_info alone would avoid that; I kept it in the driver for now.

---
 drivers/ufs/host/ufs-spacemit.c | 96 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
drivers/ufs/host/ufs-spacemit.c +96 -1
index eb2ad50..04380a0 100644
@@ -343,6 +343,87 @@ static int ufs_spacemit_negotiate_pwr_mode(struct ufs_hba *hba,
 	return ret;
 }
 
+/*
+ * As in the vendor kernel and U-Boot: change to FASTAUTO at the target
+ * gear/rate first and only then (from the core) to FAST. A direct FAST
+ * change kills the link after the M-PHY PLL rate change, so only HS-G1A
+ * would work.
+ */
+static int ufs_spacemit_fastauto_step(struct ufs_hba *hba,
+				      struct ufs_pa_layer_attr *p)
+{
+	int ret;
+
+	/*
+	 * Restore the value and PLL lock wait of ufs_spacemit_apply_dev_quirks()
+	 * at probe: every POST_CHANGE resets it, which matters for a LINERESET
+	 * recovery.
+	 */
+	ufshcd_dme_set(hba, UIC_ARG_MIB(ANA_HSGEAR_CTRL_ATTR), 0x25);
+	ret = ufs_spacemit_wait_mphy_pll_lock(hba);
+	if (ret)
+		return ret;
+
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), p->gear_rx);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), p->lane_rx);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), 1);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), p->gear_tx);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), p->lane_tx);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), 1);
+	ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), p->hs_rate);
+	/* as in ufshcd_dme_change_power_mode() */
+	if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) {
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
+			       DL_FC0ProtectionTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
+			       DL_TC0ReplayTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
+			       DL_AFC0ReqTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
+			       DL_FC1ProtectionTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
+			       DL_TC1ReplayTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
+			       DL_AFC1ReqTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
+			       DL_FC0ProtectionTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
+			       DL_TC0ReplayTimeOutVal_Default);
+		ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
+			       DL_AFC0ReqTimeOutVal_Default);
+	}
+
+	ret = ufshcd_uic_change_pwr_mode(hba, FASTAUTO_MODE << 4 | FASTAUTO_MODE);
+	if (ret)
+		return ret;
+	msleep(100);
+
+	return ufs_spacemit_wait_mphy_pll_lock(hba);
+}
+
+/*
+ * True if the link already runs in FAST with the gear, lanes and rate of
+ * @p, so no FASTAUTO step is needed. Read PA_PWRMODE rather than trust
+ * hba->pwr_info alone: after a LINERESET the error handler restores the
+ * mode with UFSHCD_PMC_POLICY_FORCE and parameters equal to hba->pwr_info
+ * while the link has fallen back to a slow mode, and the forced FAST
+ * request from there kills the link.
+ */
+static bool ufs_spacemit_link_in_fast(struct ufs_hba *hba,
+				      struct ufs_pa_layer_attr *p)
+{
+	u32 mode;
+
+	if (memcmp(p, &hba->pwr_info, sizeof(*p)))
+		return false;
+
+	if (ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &mode))
+		return false;
+
+	return ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK) == FAST_MODE &&
+	       (mode & PWRMODE_MASK) == FAST_MODE;
+}
+
 static int ufs_spacemit_pwr_change_notify(struct ufs_hba *hba,
 					  enum ufs_notify_change_status status,
 					  struct ufs_pa_layer_attr *dev_req_params)
@@ -356,6 +437,21 @@ static int ufs_spacemit_pwr_change_notify(struct ufs_hba *hba,
 
 	switch (status) {
 	case PRE_CHANGE:
+		/* the core sends the FAST request after this hook */
+		if (dev_req_params->pwr_rx == FAST_MODE &&
+		    dev_req_params->pwr_tx == FAST_MODE &&
+		    !ufs_spacemit_link_in_fast(hba, dev_req_params)) {
+			/*
+			 * The core ignores the result. If the FASTAUTO PMC
+			 * failed outside a PM operation, the link is marked
+			 * broken and the FAST request is not sent; if only the
+			 * PLL lock wait timed out, the link already runs at
+			 * the target rate.
+			 */
+			ret = ufs_spacemit_fastauto_step(hba, dev_req_params);
+			if (ret)
+				dev_warn(hba->dev, "FASTAUTO step failed: %d\n", ret);
+		}
 		break;
 	case POST_CHANGE:
 		ret = ufs_spacemit_wait_mphy_pll_lock(hba);
 
---

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261123-fastauto-306d20d17590
prerequisite-message-id: <[email protected]>
prerequisite-patch-id: 70385741c42f21fbc79b3ef967e48cb8214e5cd0
prerequisite-patch-id: 5198841accdd660ed1597e92dcdef85a6accc8cd

Best regards,

Signature · 1 line
Thomas Roos <[email protected]>
Signature · 1 line
Yixun Lan (dlan)