mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] scsi: ufs: spacemit: k3: change to FASTAUTO before the FAST power mode
@ 2026-11-24  8:11 Thomas Roos via B4 Relay
  0 siblings, 0 replies; only message in thread
From: Thomas Roos via B4 Relay @ 2026-11-24  8:11 UTC (permalink / raw)
  To: Yixun Lan
  Cc: linux-kernel, Martin K. Petersen, Avri Altman, Aurelien Jarno,
	linux-riscv, spacemit, James E.J. Bottomley, Alim Akhtar,
	Bart Van Assche, linux-scsi, Thomas Roos

From: Thomas Roos <thomas@exein.io>

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
Signed-off-by: Thomas Roos <thomas@exein.io>
---
This applies on top of v8 of "scsi: ufs: spacemit: k3: Add UFS Host
Controller driver":
https://lore.kernel.org/r/20261116-08-k3-ufs-support-v8-2-e3b256972dd4@kernel.org

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(+)

diff --git a/drivers/ufs/host/ufs-spacemit.c b/drivers/ufs/host/ufs-spacemit.c
index eb2ad50..04380a0 100644
--- a/drivers/ufs/host/ufs-spacemit.c
+++ b/drivers/ufs/host/ufs-spacemit.c
@@ -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: <20261116-08-k3-ufs-support-v8-0-e3b256972dd4@kernel.org>
prerequisite-patch-id: 70385741c42f21fbc79b3ef967e48cb8214e5cd0
prerequisite-patch-id: 5198841accdd660ed1597e92dcdef85a6accc8cd

Best regards,
--  
Thomas Roos <thomas@exein.io>



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-24  8:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24  8:11 [PATCH] scsi: ufs: spacemit: k3: change to FASTAUTO before the FAST power mode Thomas Roos via B4 Relay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®