linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements
@ 2025-08-26  6:22 peter.wang
  2025-08-26  6:22 ` [PATCH v2 01/10] ufs: host: mediatek: Enhance recovery on hibernation exit failure peter.wang
                   ` (9 more replies)
  0 siblings, 10 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Peter Wang <peter.wang@mediatek.com>

These patches collectively enhance the UFS host driver's reliability,
power management efficiency, and error recovery mechanisms on MediaTek
platforms. They address critical issues and introduce optimizations
that improve system stability and performance.

Changes since v1:
1. Export ufshcd_force_error_recovery instead use force_reset
  ufs: host: mediatek: Enhance recovery on hibernation exit failure

Peter Wang (7):
  ufs: host: mediatek: Enhance recovery on hibernation exit failure
  ufs: host: mediatek: Enhance recovery on resume failure
  ufs: host: mediatek: Correct system PM flow
  ufs: host: mediatek: Support UFS PHY runtime PM and correct sequence
  ufs: host: mediatek: Disable auto-hibern8 during power mode changes
  ufs: host: mediatek: Fix unbalanced IRQ enable issue
  ufs: host: mediatek: Fix device power control

Alice Chao (2):
  ufs: host: mediatek: Correct resume flow for LPM and MTCMOS
  ufs: host: mediatek: Fix adapt issue after PA_Init

Sanjeev Y (1):
  ufs: host: mediatek: Return error directly on idle wait timeout

 drivers/ufs/core/ufshcd.c       |   3 +-
 drivers/ufs/host/ufs-mediatek.c | 171 ++++++++++++++++++++++++++------
 drivers/ufs/host/ufs-mediatek.h |   1 +
 include/ufs/ufshcd.h            |   1 +
 4 files changed, 145 insertions(+), 31 deletions(-)

-- 
2.45.2


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v2 01/10] ufs: host: mediatek: Enhance recovery on hibernation exit failure
  2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
@ 2025-08-26  6:22 ` peter.wang
  2025-08-26 15:06   ` Bart Van Assche
  2025-08-31  1:35   ` Martin K. Petersen
  2025-08-26  6:22 ` [PATCH v2 02/10] ufs: host: mediatek: Enhance recovery on resume failure peter.wang
                   ` (8 subsequent siblings)
  9 siblings, 2 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Peter Wang <peter.wang@mediatek.com>

This patch improves the recovery process when exiting hibernation
mode fails. It triggers the error handler and breaks the suspend
operation, ensuring that the system can recover from hibernation
errors more effectively. The error handling mechanism is activated
by setting 'force_reset' and scheduling the error handler work.

Signed-off-by: Peter Wang <peter.wang@mediatek.com>
---
 drivers/ufs/core/ufshcd.c       |  3 ++-
 drivers/ufs/host/ufs-mediatek.c | 14 +++++++++++---
 include/ufs/ufshcd.h            |  1 +
 3 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 50adfb8b335b..a74aa8804caa 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -6410,13 +6410,14 @@ void ufshcd_schedule_eh_work(struct ufs_hba *hba)
 	}
 }
 
-static void ufshcd_force_error_recovery(struct ufs_hba *hba)
+void ufshcd_force_error_recovery(struct ufs_hba *hba)
 {
 	spin_lock_irq(hba->host->host_lock);
 	hba->force_reset = true;
 	ufshcd_schedule_eh_work(hba);
 	spin_unlock_irq(hba->host->host_lock);
 }
+EXPORT_SYMBOL_GPL(ufshcd_force_error_recovery);
 
 static void ufshcd_clk_scaling_allow(struct ufs_hba *hba, bool allow)
 {
diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index 5ef0ba4527e4..1aa14a8dc161 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -1686,7 +1686,7 @@ static void ufs_mtk_dev_vreg_set_lpm(struct ufs_hba *hba, bool lpm)
 	}
 }
 
-static void ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
+static int ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
 {
 	int ret;
 
@@ -1697,8 +1697,16 @@ static void ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
 	ufs_mtk_wait_idle_state(hba, 5);
 
 	ret = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
-	if (ret)
+	if (ret) {
 		dev_warn(hba->dev, "exit h8 state fail, ret=%d\n", ret);
+
+		ufshcd_force_error_recovery(hba);
+
+		/* trigger error handler and break suspend */
+		ret = -EBUSY;
+	}
+
+	return ret;
 }
 
 static int ufs_mtk_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
@@ -1709,7 +1717,7 @@ static int ufs_mtk_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
 
 	if (status == PRE_CHANGE) {
 		if (ufshcd_is_auto_hibern8_supported(hba))
-			ufs_mtk_auto_hibern8_disable(hba);
+			return ufs_mtk_auto_hibern8_disable(hba);
 		return 0;
 	}
 
diff --git a/include/ufs/ufshcd.h b/include/ufs/ufshcd.h
index 9b3515cee711..93f91b09589d 100644
--- a/include/ufs/ufshcd.h
+++ b/include/ufs/ufshcd.h
@@ -1507,5 +1507,6 @@ int __ufshcd_write_ee_control(struct ufs_hba *hba, u32 ee_ctrl_mask);
 int ufshcd_write_ee_control(struct ufs_hba *hba);
 int ufshcd_update_ee_control(struct ufs_hba *hba, u16 *mask,
 			     const u16 *other_mask, u16 set, u16 clr);
+void ufshcd_force_error_recovery(struct ufs_hba *hba);
 
 #endif /* End of Header */
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 02/10] ufs: host: mediatek: Enhance recovery on resume failure
  2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
  2025-08-26  6:22 ` [PATCH v2 01/10] ufs: host: mediatek: Enhance recovery on hibernation exit failure peter.wang
@ 2025-08-26  6:22 ` peter.wang
  2025-08-26  6:22 ` [PATCH v2 03/10] ufs: host: mediatek: Correct system PM flow peter.wang
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Peter Wang <peter.wang@mediatek.com>

This patch improves the recovery process when a resume operation
fails. If both the resume and recovery fail, it logs the device's
power status and returns 0 to prevent I/O hang..

Signed-off-by: Peter Wang <peter.wang@mediatek.com>
---
 drivers/ufs/host/ufs-mediatek.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index 1aa14a8dc161..6d25110c8cb8 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -1775,8 +1775,21 @@ static int ufs_mtk_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
 	}
 
 	return 0;
+
 fail:
-	return ufshcd_link_recovery(hba);
+	/*
+	 * Check if the platform (parent) device has resumed, and ensure that
+	 * power, clock, and MTCMOS are all turned on.
+	 */
+	err = ufshcd_link_recovery(hba);
+	if (err) {
+		dev_err(hba->dev, "Device PM: req=%d, status:%d, err:%d\n",
+			hba->dev->power.request,
+			hba->dev->power.runtime_status,
+			hba->dev->power.runtime_error);
+	}
+
+	return 0; /* Cannot return a failure, otherwise, the I/O will hang. */
 }
 
 static void ufs_mtk_dbg_register_dump(struct ufs_hba *hba)
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 03/10] ufs: host: mediatek: Correct system PM flow
  2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
  2025-08-26  6:22 ` [PATCH v2 01/10] ufs: host: mediatek: Enhance recovery on hibernation exit failure peter.wang
  2025-08-26  6:22 ` [PATCH v2 02/10] ufs: host: mediatek: Enhance recovery on resume failure peter.wang
@ 2025-08-26  6:22 ` peter.wang
  2025-08-26  6:22 ` [PATCH v2 04/10] ufs: host: mediatek: Correct resume flow for LPM and MTCMOS peter.wang
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Peter Wang <peter.wang@mediatek.com>

This patch refines the system power management (PM) flow by
skipping low power mode (LPM) and MTCMOS settings if runtime PM
has already been applied. This optimization prevents redundant
operations, ensuring a more efficient PM process.

Signed-off-by: Peter Wang <peter.wang@mediatek.com>
---
 drivers/ufs/host/ufs-mediatek.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index 6d25110c8cb8..d762f096e32e 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -2304,27 +2304,38 @@ static int ufs_mtk_system_suspend(struct device *dev)
 
 	ret = ufshcd_system_suspend(dev);
 	if (ret)
-		return ret;
+		goto out;
+
+	if (pm_runtime_suspended(hba->dev))
+		goto out;
 
 	ufs_mtk_dev_vreg_set_lpm(hba, true);
 
 	if (ufs_mtk_is_rtff_mtcmos(hba))
 		ufs_mtk_mtcmos_ctrl(false, res);
 
-	return 0;
+out:
+	return ret;
 }
 
 static int ufs_mtk_system_resume(struct device *dev)
 {
+	int ret = 0;
 	struct ufs_hba *hba = dev_get_drvdata(dev);
 	struct arm_smccc_res res;
 
+	if (pm_runtime_suspended(hba->dev))
+		goto out;
+
 	ufs_mtk_dev_vreg_set_lpm(hba, false);
 
 	if (ufs_mtk_is_rtff_mtcmos(hba))
 		ufs_mtk_mtcmos_ctrl(true, res);
 
-	return ufshcd_system_resume(dev);
+out:
+	ret = ufshcd_system_resume(dev);
+
+	return ret;
 }
 #endif
 
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 04/10] ufs: host: mediatek: Correct resume flow for LPM and MTCMOS
  2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
                   ` (2 preceding siblings ...)
  2025-08-26  6:22 ` [PATCH v2 03/10] ufs: host: mediatek: Correct system PM flow peter.wang
@ 2025-08-26  6:22 ` peter.wang
  2025-08-26  6:22 ` [PATCH v2 05/10] ufs: host: mediatek: Support UFS PHY runtime PM and correct sequence peter.wang
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Alice Chao <alice.chao@mediatek.com>

This patch corrects the system resume flow by ensuring that MTCMOS
is turned on before setting LPM to false.
During system suspend, LPM is set to true and MTCMOS is turned off.
The updated resume sequence ensures proper power management and
system stability.

Signed-off-by: Peter Wang <peter.wang@mediatek.com>
Signed-off-by: Alice Chao <alice.chao@mediatek.com>
Reviewed-by: Peter Wang <peter.wang@mediatek.com>
---
 drivers/ufs/host/ufs-mediatek.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index d762f096e32e..a47713a047c1 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -2327,11 +2327,11 @@ static int ufs_mtk_system_resume(struct device *dev)
 	if (pm_runtime_suspended(hba->dev))
 		goto out;
 
-	ufs_mtk_dev_vreg_set_lpm(hba, false);
-
 	if (ufs_mtk_is_rtff_mtcmos(hba))
 		ufs_mtk_mtcmos_ctrl(true, res);
 
+	ufs_mtk_dev_vreg_set_lpm(hba, false);
+
 out:
 	ret = ufshcd_system_resume(dev);
 
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 05/10] ufs: host: mediatek: Support UFS PHY runtime PM and correct sequence
  2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
                   ` (3 preceding siblings ...)
  2025-08-26  6:22 ` [PATCH v2 04/10] ufs: host: mediatek: Correct resume flow for LPM and MTCMOS peter.wang
@ 2025-08-26  6:22 ` peter.wang
  2025-08-26  6:22 ` [PATCH v2 06/10] ufs: host: mediatek: Disable auto-hibern8 during power mode changes peter.wang
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Peter Wang <peter.wang@mediatek.com>

This patch adds support for UFS PHY runtime power management by
probing the PHY device and enabling its runtime PM. It ensures the
correct sequence of operations during suspend and resume:
PHY suspend -> UFS suspend -> UFS resume -> PHY resume.
This enhancement improves power management efficiency and system
stability.

Signed-off-by: Peter Wang <peter.wang@mediatek.com>
---
 drivers/ufs/host/ufs-mediatek.c | 53 +++++++++++++++++++++++++++++----
 drivers/ufs/host/ufs-mediatek.h |  1 +
 2 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index a47713a047c1..5d2de4fc370b 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -2243,10 +2243,12 @@ static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
 static int ufs_mtk_probe(struct platform_device *pdev)
 {
 	int err;
-	struct device *dev = &pdev->dev;
-	struct device_node *reset_node;
-	struct platform_device *reset_pdev;
+	struct device *dev = &pdev->dev, *phy_dev = NULL;
+	struct device_node *reset_node, *phy_node = NULL;
+	struct platform_device *reset_pdev, *phy_pdev = NULL;
 	struct device_link *link;
+	struct ufs_hba *hba;
+	struct ufs_mtk_host *host;
 
 	reset_node = of_find_compatible_node(NULL, NULL,
 					     "ti,syscon-reset");
@@ -2273,13 +2275,44 @@ static int ufs_mtk_probe(struct platform_device *pdev)
 	}
 
 skip_reset:
+	/* find phy node */
+	phy_node = of_parse_phandle(dev->of_node, "phys", 0);
+
+	if (phy_node) {
+		phy_pdev = of_find_device_by_node(phy_node);
+		if (!phy_pdev)
+			goto skip_phy;
+		phy_dev = &phy_pdev->dev;
+
+		pm_runtime_set_active(phy_dev);
+		pm_runtime_enable(phy_dev);
+		pm_runtime_get_sync(phy_dev);
+
+		put_device(phy_dev);
+		dev_info(dev, "phys node found\n");
+	} else {
+		dev_notice(dev, "phys node not found\n");
+	}
+
+skip_phy:
 	/* perform generic probe */
 	err = ufshcd_pltfrm_init(pdev, &ufs_hba_mtk_vops);
-
-out:
-	if (err)
+	if (err) {
 		dev_err(dev, "probe failed %d\n", err);
+		goto out;
+	}
+
+	hba = platform_get_drvdata(pdev);
+	if (!hba)
+		goto out;
+
+	if (phy_node && phy_dev) {
+		host = ufshcd_get_variant(hba);
+		host->phy_dev = phy_dev;
+	}
 
+out:
+	of_node_put(phy_node);
 	of_node_put(reset_node);
 	return err;
 }
@@ -2343,6 +2376,7 @@ static int ufs_mtk_system_resume(struct device *dev)
 static int ufs_mtk_runtime_suspend(struct device *dev)
 {
 	struct ufs_hba *hba = dev_get_drvdata(dev);
+	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
 	struct arm_smccc_res res;
 	int ret = 0;
 
@@ -2355,17 +2389,24 @@ static int ufs_mtk_runtime_suspend(struct device *dev)
 	if (ufs_mtk_is_rtff_mtcmos(hba))
 		ufs_mtk_mtcmos_ctrl(false, res);
 
+	if (host->phy_dev)
+		pm_runtime_put_sync(host->phy_dev);
+
 	return 0;
 }
 
 static int ufs_mtk_runtime_resume(struct device *dev)
 {
 	struct ufs_hba *hba = dev_get_drvdata(dev);
+	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
 	struct arm_smccc_res res;
 
 	if (ufs_mtk_is_rtff_mtcmos(hba))
 		ufs_mtk_mtcmos_ctrl(true, res);
 
+	if (host->phy_dev)
+		pm_runtime_get_sync(host->phy_dev);
+
 	ufs_mtk_dev_vreg_set_lpm(hba, false);
 
 	return ufshcd_runtime_resume(dev);
diff --git a/drivers/ufs/host/ufs-mediatek.h b/drivers/ufs/host/ufs-mediatek.h
index e46dc5fa209d..dfbf78bd8664 100644
--- a/drivers/ufs/host/ufs-mediatek.h
+++ b/drivers/ufs/host/ufs-mediatek.h
@@ -193,6 +193,7 @@ struct ufs_mtk_host {
 	bool is_mcq_intr_enabled;
 	int mcq_nr_intr;
 	struct ufs_mtk_mcq_intr_info mcq_intr_info[UFSHCD_MAX_Q_NR];
+	struct device *phy_dev;
 };
 
 /* MTK delay of autosuspend: 500 ms */
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 06/10] ufs: host: mediatek: Disable auto-hibern8 during power mode changes
  2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
                   ` (4 preceding siblings ...)
  2025-08-26  6:22 ` [PATCH v2 05/10] ufs: host: mediatek: Support UFS PHY runtime PM and correct sequence peter.wang
@ 2025-08-26  6:22 ` peter.wang
  2025-08-26  6:22 ` [PATCH v2 07/10] ufs: host: mediatek: Return error directly on idle wait timeout peter.wang
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Peter Wang <peter.wang@mediatek.com>

This patch ensures that auto-hibern8 is disabled when changing power
modes to prevent unintended entry into auto-hibern8 during these
transitions. The original auto-hibern8 timer value is restored
after the power mode change is complete, maintaining system stability
and preventing potential issues during power state transitions.

Signed-off-by: Peter Wang <peter.wang@mediatek.com>
---
 drivers/ufs/host/ufs-mediatek.c | 53 +++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index 5d2de4fc370b..e42e9b97810c 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -1429,19 +1429,49 @@ static int ufs_mtk_pre_pwr_change(struct ufs_hba *hba,
 	return ret;
 }
 
+static int ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
+{
+	int ret;
+
+	/* disable auto-hibern8 */
+	ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER);
+
+	/* wait host return to idle state when auto-hibern8 off */
+	ufs_mtk_wait_idle_state(hba, 5);
+
+	ret = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
+	if (ret) {
+		dev_warn(hba->dev, "exit h8 state fail, ret=%d\n", ret);
+
+		ufshcd_force_error_recovery(hba);
+
+		/* trigger error handler and break suspend */
+		ret = -EBUSY;
+	}
+
+	return ret;
+}
+
 static int ufs_mtk_pwr_change_notify(struct ufs_hba *hba,
 				enum ufs_notify_change_status stage,
 				const struct ufs_pa_layer_attr *dev_max_params,
 				struct ufs_pa_layer_attr *dev_req_params)
 {
 	int ret = 0;
+	static u32 reg;
 
 	switch (stage) {
 	case PRE_CHANGE:
+		if (ufshcd_is_auto_hibern8_supported(hba)) {
+			reg = ufshcd_readl(hba, REG_AUTO_HIBERNATE_IDLE_TIMER);
+			ufs_mtk_auto_hibern8_disable(hba);
+		}
 		ret = ufs_mtk_pre_pwr_change(hba, dev_max_params,
 					     dev_req_params);
 		break;
 	case POST_CHANGE:
+		if (ufshcd_is_auto_hibern8_supported(hba))
+			ufshcd_writel(hba, reg, REG_AUTO_HIBERNATE_IDLE_TIMER);
 		break;
 	default:
 		ret = -EINVAL;
@@ -1686,29 +1716,6 @@ static void ufs_mtk_dev_vreg_set_lpm(struct ufs_hba *hba, bool lpm)
 	}
 }
 
-static int ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
-{
-	int ret;
-
-	/* disable auto-hibern8 */
-	ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER);
-
-	/* wait host return to idle state when auto-hibern8 off */
-	ufs_mtk_wait_idle_state(hba, 5);
-
-	ret = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
-	if (ret) {
-		dev_warn(hba->dev, "exit h8 state fail, ret=%d\n", ret);
-
-		ufshcd_force_error_recovery(hba);
-
-		/* trigger error handler and break suspend */
-		ret = -EBUSY;
-	}
-
-	return ret;
-}
-
 static int ufs_mtk_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
 	enum ufs_notify_change_status status)
 {
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 07/10] ufs: host: mediatek: Return error directly on idle wait timeout
  2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
                   ` (5 preceding siblings ...)
  2025-08-26  6:22 ` [PATCH v2 06/10] ufs: host: mediatek: Disable auto-hibern8 during power mode changes peter.wang
@ 2025-08-26  6:22 ` peter.wang
  2025-08-26  6:22 ` [PATCH v2 08/10] ufs: host: mediatek: Fix adapt issue after PA_Init peter.wang
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Sanjeev Y <sanjeev.y@mediatek.com>

This patch optimizes the recovery flow by returning an error code
immediately if a wait idle timeout occurs, rather than proceeding
to wait for the link to reach the up state. This change shortens
the recovery process and improves error handling efficiency when
idle state transitions fail.

Signed-off-by: Peter Wang <peter.wang@mediatek.com>
Signed-off-by: Sanjeev Y <sanjeev.y@mediatek.com>
Reviewed-by: Peter Wang <peter.wang@mediatek.com>
---
 drivers/ufs/host/ufs-mediatek.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index e42e9b97810c..a6f812713345 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -416,7 +416,7 @@ static void ufs_mtk_dbg_sel(struct ufs_hba *hba)
 	}
 }
 
-static void ufs_mtk_wait_idle_state(struct ufs_hba *hba,
+static int ufs_mtk_wait_idle_state(struct ufs_hba *hba,
 			    unsigned long retry_ms)
 {
 	u64 timeout, time_checked;
@@ -452,8 +452,12 @@ static void ufs_mtk_wait_idle_state(struct ufs_hba *hba,
 			break;
 	} while (time_checked < timeout);
 
-	if (wait_idle && sm != VS_HCE_BASE)
+	if (wait_idle && sm != VS_HCE_BASE) {
 		dev_info(hba->dev, "wait idle tmo: 0x%x\n", val);
+		return -ETIMEDOUT;
+	}
+
+	return 0;
 }
 
 static int ufs_mtk_wait_link_state(struct ufs_hba *hba, u32 state,
@@ -1437,9 +1441,13 @@ static int ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
 	ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER);
 
 	/* wait host return to idle state when auto-hibern8 off */
-	ufs_mtk_wait_idle_state(hba, 5);
+	ret = ufs_mtk_wait_idle_state(hba, 5);
+	if (ret)
+		goto out;
 
 	ret = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
+
+out:
 	if (ret) {
 		dev_warn(hba->dev, "exit h8 state fail, ret=%d\n", ret);
 
@@ -1614,7 +1622,11 @@ static int ufs_mtk_link_set_hpm(struct ufs_hba *hba)
 		return err;
 
 	/* Check link state to make sure exit h8 success */
-	ufs_mtk_wait_idle_state(hba, 5);
+	err = ufs_mtk_wait_idle_state(hba, 5);
+	if (err) {
+		dev_warn(hba->dev, "wait idle fail, err=%d\n", err);
+		return err;
+	}
 	err = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
 	if (err) {
 		dev_warn(hba->dev, "exit h8 state fail, err=%d\n", err);
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 08/10] ufs: host: mediatek: Fix adapt issue after PA_Init
  2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
                   ` (6 preceding siblings ...)
  2025-08-26  6:22 ` [PATCH v2 07/10] ufs: host: mediatek: Return error directly on idle wait timeout peter.wang
@ 2025-08-26  6:22 ` peter.wang
  2025-08-26  6:22 ` [PATCH v2 09/10] ufs: host: mediatek: Fix unbalanced IRQ enable issue peter.wang
  2025-08-26  6:22 ` [PATCH v2 10/10] ufs: host: mediatek: Fix device power control peter.wang
  9 siblings, 0 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Alice Chao <alice.chao@mediatek.com>

This patch addresses the issue where the host does not send adapt
to the device after PA_Init success.
It ensures that the adapt process is correctly initiated for devices
with IP version MT6899 and above, resolving communication issues
between the host and device.

Signed-off-by: Peter Wang <peter.wang@mediatek.com>
Signed-off-by: Alice Chao <alice.chao@mediatek.com>
Reviewed-by: Peter Wang <peter.wang@mediatek.com>
---
 drivers/ufs/host/ufs-mediatek.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index a6f812713345..1342fe7d8e2b 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -1551,8 +1551,19 @@ static int ufs_mtk_pre_link(struct ufs_hba *hba)
 
 	return ret;
 }
+
 static void ufs_mtk_post_link(struct ufs_hba *hba)
 {
+	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
+	u32 tmp;
+
+	/* fix device PA_INIT no adapt */
+	if (host->ip_ver >= IP_VER_MT6899) {
+		ufshcd_dme_get(hba, UIC_ARG_MIB(VS_DEBUGOMC), &tmp);
+		tmp |= 0x100;
+		ufshcd_dme_set(hba, UIC_ARG_MIB(VS_DEBUGOMC), tmp);
+	}
+
 	/* enable unipro clock gating feature */
 	ufs_mtk_cfg_unipro_cg(hba, true);
 }
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 09/10] ufs: host: mediatek: Fix unbalanced IRQ enable issue
  2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
                   ` (7 preceding siblings ...)
  2025-08-26  6:22 ` [PATCH v2 08/10] ufs: host: mediatek: Fix adapt issue after PA_Init peter.wang
@ 2025-08-26  6:22 ` peter.wang
  2025-08-26  6:22 ` [PATCH v2 10/10] ufs: host: mediatek: Fix device power control peter.wang
  9 siblings, 0 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Peter Wang <peter.wang@mediatek.com>

This patch resolves the issue of unbalanced IRQ enablement by setting
the 'is_mcq_intr_enabled' flag after the first successful IRQ
enablement. This ensures proper tracking of the IRQ state and prevents
potential mismatches in IRQ handling.

Signed-off-by: Peter Wang <peter.wang@mediatek.com>
---
 drivers/ufs/host/ufs-mediatek.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index 1342fe7d8e2b..af5574ac0b3c 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -2190,6 +2190,7 @@ static int ufs_mtk_config_mcq_irq(struct ufs_hba *hba)
 			return ret;
 		}
 	}
+	host->is_mcq_intr_enabled = true;
 
 	return 0;
 }
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 10/10] ufs: host: mediatek: Fix device power control
  2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
                   ` (8 preceding siblings ...)
  2025-08-26  6:22 ` [PATCH v2 09/10] ufs: host: mediatek: Fix unbalanced IRQ enable issue peter.wang
@ 2025-08-26  6:22 ` peter.wang
  9 siblings, 0 replies; 14+ messages in thread
From: peter.wang @ 2025-08-26  6:22 UTC (permalink / raw)
  To: linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, peter.wang, chun-hung.wu,
	alice.chao, cc.chou, chaotian.jing, jiajie.hao, yi-fan.peng,
	qilin.tan, lin.gui, tun-yu.yu, eddie.huang, naomi.chu, ed.tsai,
	sanjeev.y, bvanassche

From: Peter Wang <peter.wang@mediatek.com>

This patch adjusts the timing of device power control to ensure
that low power mode (LPM) is entered only after VCC is turned off.
This change prevents VCCQ/VCCQ2 from entering LPM prematurely,
ensuring proper power management and device stability.

Signed-off-by: Peter Wang <peter.wang@mediatek.com>
---
 drivers/ufs/host/ufs-mediatek.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index af5574ac0b3c..758a393a9de1 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -2342,6 +2342,13 @@ static int ufs_mtk_probe(struct platform_device *pdev)
 		host->phy_dev = phy_dev;
 	}
 
+	/*
+	 * Because the default power setting of VSx (the upper layer of
+	 * VCCQ/VCCQ2) is HWLP, we need to prevent VCCQ/VCCQ2 from
+	 * entering LPM.
+	 */
+	ufs_mtk_dev_vreg_set_lpm(hba, false);
+
 out:
 	of_node_put(phy_node);
 	of_node_put(reset_node);
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 01/10] ufs: host: mediatek: Enhance recovery on hibernation exit failure
  2025-08-26  6:22 ` [PATCH v2 01/10] ufs: host: mediatek: Enhance recovery on hibernation exit failure peter.wang
@ 2025-08-26 15:06   ` Bart Van Assche
  2025-08-31  1:35   ` Martin K. Petersen
  1 sibling, 0 replies; 14+ messages in thread
From: Bart Van Assche @ 2025-08-26 15:06 UTC (permalink / raw)
  To: peter.wang, linux-scsi, martin.petersen
  Cc: wsd_upstream, linux-mediatek, chun-hung.wu, alice.chao, cc.chou,
	chaotian.jing, jiajie.hao, yi-fan.peng, qilin.tan, lin.gui,
	tun-yu.yu, eddie.huang, naomi.chu, ed.tsai, sanjeev.y

On 8/25/25 11:22 PM, peter.wang@mediatek.com wrote:
> This patch improves the recovery process when exiting hibernation
> mode fails. It triggers the error handler and breaks the suspend
> operation, ensuring that the system can recover from hibernation
> errors more effectively. The error handling mechanism is activated
> by setting 'force_reset' and scheduling the error handler work.

Reviewed-by: Bart Van Assche <bvanassche@acm.org>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 01/10] ufs: host: mediatek: Enhance recovery on hibernation exit failure
  2025-08-26  6:22 ` [PATCH v2 01/10] ufs: host: mediatek: Enhance recovery on hibernation exit failure peter.wang
  2025-08-26 15:06   ` Bart Van Assche
@ 2025-08-31  1:35   ` Martin K. Petersen
  2025-09-03  2:42     ` Peter Wang (王信友)
  1 sibling, 1 reply; 14+ messages in thread
From: Martin K. Petersen @ 2025-08-31  1:35 UTC (permalink / raw)
  To: peter.wang
  Cc: linux-scsi, martin.petersen, wsd_upstream, linux-mediatek,
	chun-hung.wu, alice.chao, cc.chou, chaotian.jing, jiajie.hao,
	yi-fan.peng, qilin.tan, lin.gui, tun-yu.yu, eddie.huang,
	naomi.chu, ed.tsai, sanjeev.y, bvanassche


Peter,

> This patch improves the recovery process when exiting hibernation mode
> fails. It triggers the error handler and breaks the suspend operation,
> ensuring that the system can recover from hibernation errors more
> effectively.

Reminder: There is no "this patch" in a commit message. Please rewrite
your descriptions to use imperative mood per the instructions in
Documentation/process/submitting-patches.rst.

The above description should be something along the lines of:

  Improve the recovery process when exiting hibernation mode fails.
  Trigger the error handler and break the suspend operation, ensuring
  that the system can recover from hibernation errors more effectively.

Please fix and resubmit. Thanks!

-- 
Martin K. Petersen

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 01/10] ufs: host: mediatek: Enhance recovery on hibernation exit failure
  2025-08-31  1:35   ` Martin K. Petersen
@ 2025-09-03  2:42     ` Peter Wang (王信友)
  0 siblings, 0 replies; 14+ messages in thread
From: Peter Wang (王信友) @ 2025-09-03  2:42 UTC (permalink / raw)
  To: martin.petersen@oracle.com
  Cc: CC Chou (周志杰), bvanassche@acm.org, Sanjeev Y,
	linux-scsi@vger.kernel.org,
	Eddie Huang (黃智傑),
	linux-mediatek@lists.infradead.org,
	Chaotian Jing (井朝天),
	Qilin Tan (谭麒麟),
	Yi-fan Peng (彭羿凡),
	Lin Gui (桂林), Jiajie Hao (郝加节),
	Naomi Chu (朱詠田),
	Ed Tsai (蔡宗軒),
	Alice Chao (趙珮均), wsd_upstream,
	Chun-Hung Wu (巫駿宏),
	Tun-yu Yu (游敦聿)

On Sat, 2025-08-30 at 21:35 -0400, Martin K. Petersen wrote:
> Peter,
> 
> > This patch improves the recovery process when exiting hibernation
> > mode
> > fails. It triggers the error handler and breaks the suspend
> > operation,
> > ensuring that the system can recover from hibernation errors more
> > effectively.
> 
> Reminder: There is no "this patch" in a commit message. Please
> rewrite
> your descriptions to use imperative mood per the instructions in
> Documentation/process/submitting-patches.rst.
> 
> The above description should be something along the lines of:
> 
>   Improve the recovery process when exiting hibernation mode fails.
>   Trigger the error handler and break the suspend operation, ensuring
>   that the system can recover from hibernation errors more
> effectively.
> 
> Please fix and resubmit. Thanks!
> 
> --
> Martin K. Petersen

Hi Martin,

Thanks for reminder.
I'll update the commit description next version.

Thanks.
Peter


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2025-09-03  2:42 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-26  6:22 [PATCH v2 00/10] ufs: host: mediatek: Power Management and Stability Enhancements peter.wang
2025-08-26  6:22 ` [PATCH v2 01/10] ufs: host: mediatek: Enhance recovery on hibernation exit failure peter.wang
2025-08-26 15:06   ` Bart Van Assche
2025-08-31  1:35   ` Martin K. Petersen
2025-09-03  2:42     ` Peter Wang (王信友)
2025-08-26  6:22 ` [PATCH v2 02/10] ufs: host: mediatek: Enhance recovery on resume failure peter.wang
2025-08-26  6:22 ` [PATCH v2 03/10] ufs: host: mediatek: Correct system PM flow peter.wang
2025-08-26  6:22 ` [PATCH v2 04/10] ufs: host: mediatek: Correct resume flow for LPM and MTCMOS peter.wang
2025-08-26  6:22 ` [PATCH v2 05/10] ufs: host: mediatek: Support UFS PHY runtime PM and correct sequence peter.wang
2025-08-26  6:22 ` [PATCH v2 06/10] ufs: host: mediatek: Disable auto-hibern8 during power mode changes peter.wang
2025-08-26  6:22 ` [PATCH v2 07/10] ufs: host: mediatek: Return error directly on idle wait timeout peter.wang
2025-08-26  6:22 ` [PATCH v2 08/10] ufs: host: mediatek: Fix adapt issue after PA_Init peter.wang
2025-08-26  6:22 ` [PATCH v2 09/10] ufs: host: mediatek: Fix unbalanced IRQ enable issue peter.wang
2025-08-26  6:22 ` [PATCH v2 10/10] ufs: host: mediatek: Fix device power control peter.wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).