* [PATCH v2 0/3] scsi: ufs: mediatek: pre-UniPro-1.8 adapt and clock scaling fixes
@ 2026-11-26 3:10 Ilia Snezhkov via B4 Relay
2026-11-26 3:10 ` [PATCH v2 1/3] scsi: ufs: mediatek: skip PA_TXHSADAPTTYPE on pre-UniPro-1.8 hosts Ilia Snezhkov via B4 Relay
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26 3:10 UTC (permalink / raw)
To: peter.wang, stanley.chu, martin.petersen, James.Bottomley
Cc: matthias.bgg, angelogioacchino.delregno, louisalexis.eyraud,
linux-scsi, linux-mediatek, linux-kernel
This series fixes three bugs in the MediaTek UFS host driver observed on
MT6789 (UFS 2.2, pre-UniPro-1.8, HS-G3 rate B, no ufs_fde clock):
1. Skip ufshcd_dme_configure_adapt() when host->hw_ver.major < 3 so
pre-UniPro-1.8 hosts do not fail DME-SET on PA_TXHSADAPTTYPE (0x15d4)
on every power-mode change.
2. Allow _ufs_mtk_clk_scale() to switch ufs_sel when the optional
ufs_fde clock is absent, matching ufs_mtk_is_clk_scale_ready().
3. Round clki->max_freq and clki->min_freq with clk_round_rate() during
ufs_mtk_init_clocks() so PLL-derived clocks whose rounded rate
differs from the nominal DT frequency can scale back up in
ufshcd_devfreq_target().
Changes since v1:
- Patch 1: corrected Fixes tag to 7212d624f863 ("scsi: ufs: host:
mediatek: Fix PWM mode switch issue") and clarified mainline behavior
after d3eba21c7170 vs older stable kernels.
- Patch 3: use IS_ERR_OR_NULL(clki->clk) and cast max_rounded to u64
before comparing with U32_MAX to avoid 32-bit -Wtype-limits warnings.
- Moved AI assistant disclosure out of commit logs (below --- and in
this cover letter).
Found on hardware and drafted with an AI coding assistant.
Ilia Snezhkov (3):
scsi: ufs: mediatek: skip PA_TXHSADAPTTYPE on pre-UniPro-1.8 hosts
scsi: ufs: mediatek: allow clock scaling when ufs_fde is absent
scsi: ufs: mediatek: round scalable clock bounds to clk_round_rate()
drivers/ufs/host/ufs-mediatek.c | 58 +++++++++++++++++++++++++--------
1 file changed, 45 insertions(+), 13 deletions(-)
--
2.54.0 (Apple Git-157)
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/3] scsi: ufs: mediatek: skip PA_TXHSADAPTTYPE on pre-UniPro-1.8 hosts
2026-11-26 3:10 [PATCH v2 0/3] scsi: ufs: mediatek: pre-UniPro-1.8 adapt and clock scaling fixes Ilia Snezhkov via B4 Relay
@ 2026-11-26 3:10 ` Ilia Snezhkov via B4 Relay
2026-11-26 13:01 ` Peter Wang
2026-11-26 3:10 ` [PATCH v2 2/3] scsi: ufs: mediatek: allow clock scaling when ufs_fde is absent Ilia Snezhkov via B4 Relay
2026-11-26 3:10 ` [PATCH v2 3/3] scsi: ufs: mediatek: round scalable clock bounds to clk_round_rate() Ilia Snezhkov via B4 Relay
2 siblings, 1 reply; 7+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26 3:10 UTC (permalink / raw)
To: peter.wang, stanley.chu, martin.petersen, James.Bottomley
Cc: matthias.bgg, angelogioacchino.delregno, louisalexis.eyraud,
linux-scsi, linux-mediatek, linux-kernel
From: Ilia Snezhkov <iasnezhkov@gmail.com>
ufs_mtk_pre_pwr_change() calls ufshcd_dme_configure_adapt() on every
power mode change, which writes PA_TXHSADAPTTYPE (0x15d4) even when
passing PA_NO_ADAPT. PA_TXHSADAPTTYPE was introduced in UniPro 1.8, and
ufs_mtk_get_controller_version() sets host->hw_ver.major = 3 only when
PA_LOCALVERINFO >= UFS_UNIPRO_VER_1_8 (leaving older hosts at major = 2).
On pre-UniPro-1.8 MediaTek hosts such as MT6789 (UFS 2.2, HS-G3 rate B),
the DME write fails on every gear change with
"dme-set: attr-id 0x15d4 val 0x3 failed 0 retries". While mainline
ufshcd_change_power_mode() ignores the PRE_CHANGE return value after
commit d3eba21c7170 ("scsi: ufs: core: Introduce a new ufshcd vops
negotiate_pwr_mode()"), older stable kernels still discard the
negotiated parameters on PRE_CHANGE failure, and mainline still emits
spurious DME-SET errors on every power-mode transition.
Return early from ufs_mtk_pre_pwr_change() when host->hw_ver.major < 3
so pre-UniPro-1.8 hosts do not write PA_TXHSADAPTTYPE, and simplify the
remaining major >= 3 branch.
Fixes: 7212d624f863 ("scsi: ufs: host: mediatek: Fix PWM mode switch issue")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes since v1:
- Corrected Fixes tag from e1e25d1b8996 to 7212d624f863.
- Updated commit description for mainline post-d3eba21c7170 behavior.
- Found on hardware and drafted with an AI coding assistant.
drivers/ufs/host/ufs-mediatek.c | 19 ++++++++-----------
1 file changed, 8 insertions(+), 11 deletions(-)
diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index 7691349..38d05ac 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -1468,21 +1468,18 @@ static int ufs_mtk_pre_pwr_change(struct ufs_hba *hba,
return ret;
}
+ if (host->hw_ver.major < 3)
+ return ret;
+
if (dev_req_params->pwr_rx == FAST_MODE ||
dev_req_params->pwr_rx == FASTAUTO_MODE) {
- if (host->hw_ver.major >= 3) {
- ret = ufshcd_dme_configure_adapt(hba,
- dev_req_params->gear_tx,
- PA_INITIAL_ADAPT);
- } else {
- ret = ufshcd_dme_configure_adapt(hba,
- dev_req_params->gear_tx,
- PA_NO_ADAPT);
- }
+ ret = ufshcd_dme_configure_adapt(hba,
+ dev_req_params->gear_tx,
+ PA_INITIAL_ADAPT);
} else {
ret = ufshcd_dme_configure_adapt(hba,
- dev_req_params->gear_tx,
- PA_NO_ADAPT);
+ dev_req_params->gear_tx,
+ PA_NO_ADAPT);
}
return ret;
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/3] scsi: ufs: mediatek: allow clock scaling when ufs_fde is absent
2026-11-26 3:10 [PATCH v2 0/3] scsi: ufs: mediatek: pre-UniPro-1.8 adapt and clock scaling fixes Ilia Snezhkov via B4 Relay
2026-11-26 3:10 ` [PATCH v2 1/3] scsi: ufs: mediatek: skip PA_TXHSADAPTTYPE on pre-UniPro-1.8 hosts Ilia Snezhkov via B4 Relay
@ 2026-11-26 3:10 ` Ilia Snezhkov via B4 Relay
2026-11-26 13:03 ` Peter Wang
2026-11-26 3:10 ` [PATCH v2 3/3] scsi: ufs: mediatek: round scalable clock bounds to clk_round_rate() Ilia Snezhkov via B4 Relay
2 siblings, 1 reply; 7+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26 3:10 UTC (permalink / raw)
To: peter.wang, stanley.chu, martin.petersen, James.Bottomley
Cc: matthias.bgg, angelogioacchino.delregno, louisalexis.eyraud,
linux-scsi, linux-mediatek, linux-kernel
From: Ilia Snezhkov <iasnezhkov@gmail.com>
ufs_mtk_is_clk_scale_ready() enables UFSHCD_CAP_CLK_SCALING whenever
ufs_sel, ufs_sel_max_src, and ufs_sel_min_src are present, treating the
FDE (AES) clock as optional. However, _ufs_mtk_clk_scale() bails out
early on !fde_clki, so on SoCs without a ufs_fde clock (such as MT6789)
ufshcd believes scaling succeeded and updates clki->curr_freq while the
ufs_sel mux never switches parents.
Only require clki (ufs_sel) in _ufs_mtk_clk_scale(), and gate
clk_fde_scale on fde_clki being non-NULL alongside its min/max parents.
Fixes: 5e5976f5242d ("scsi: ufs: host: mediatek: Support FDE (AES) clock scaling")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes since v1:
- Moved AI coding assistant note below ---.
- Found on hardware and drafted with an AI coding assistant.
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 38d05ac..09b7993 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -2059,7 +2059,7 @@ static void _ufs_mtk_clk_scale(struct ufs_hba *hba, bool scale_up)
if (!hba->clk_scaling.is_initialized)
return;
- if (!clki || !fde_clki)
+ if (!clki)
return;
reg = host->mclk.reg_vcore;
@@ -2067,7 +2067,7 @@ static void _ufs_mtk_clk_scale(struct ufs_hba *hba, bool scale_up)
if (reg && volt != 0)
clk_bind_vcore = true;
- if (mclk->ufs_fde_max_clki && mclk->ufs_fde_min_clki)
+ if (fde_clki && mclk->ufs_fde_max_clki && mclk->ufs_fde_min_clki)
clk_fde_scale = true;
ret = clk_prepare_enable(clki->clk);
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 3/3] scsi: ufs: mediatek: round scalable clock bounds to clk_round_rate()
2026-11-26 3:10 [PATCH v2 0/3] scsi: ufs: mediatek: pre-UniPro-1.8 adapt and clock scaling fixes Ilia Snezhkov via B4 Relay
2026-11-26 3:10 ` [PATCH v2 1/3] scsi: ufs: mediatek: skip PA_TXHSADAPTTYPE on pre-UniPro-1.8 hosts Ilia Snezhkov via B4 Relay
2026-11-26 3:10 ` [PATCH v2 2/3] scsi: ufs: mediatek: allow clock scaling when ufs_fde is absent Ilia Snezhkov via B4 Relay
@ 2026-11-26 3:10 ` Ilia Snezhkov via B4 Relay
2026-11-26 13:04 ` Peter Wang
2 siblings, 1 reply; 7+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26 3:10 UTC (permalink / raw)
To: peter.wang, stanley.chu, martin.petersen, James.Bottomley
Cc: matthias.bgg, angelogioacchino.delregno, louisalexis.eyraud,
linux-scsi, linux-mediatek, linux-kernel
From: Ilia Snezhkov <iasnezhkov@gmail.com>
ufshcd_devfreq_target() determines scale_up via an exact equality check
against clki->max_freq after rounding the target frequency. When a DT
freq-table-hz lists a nominal value that a PLL-derived clock cannot hit
exactly (for example, msdcpll_d2 on MT6789 delivers 191999939 Hz for a
192000000 Hz DT maximum), clk_round_rate() produces a rate that never
equals clki->max_freq, so devfreq scales down once and can never scale
back up.
Round clki->max_freq and clki->min_freq with clk_round_rate() in
ufs_mtk_init_clocks() for ufs_sel (and ufs_fde when present) before
ufshcd_devfreq_init() builds the OPP table from those bounds, keeping
clki->curr_freq synchronized with the rounded bound and skipping the
adjustment if rounding fails, overflows u32, or collapses min >= max.
Fixes: b7dbc686f60b ("scsi: ufs: ufs-mediatek: Support clk-scaling to optimize power consumption")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes since v1:
- Use IS_ERR_OR_NULL(clki->clk) and cast max_rounded to u64 before
comparing against U32_MAX for 32-bit cleanliness.
- Found on hardware and drafted with an AI coding assistant.
drivers/ufs/host/ufs-mediatek.c | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index 09b7993..4de72ae 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -967,6 +967,37 @@ static u32 ufs_mtk_get_ufs_hci_version(struct ufs_hba *hba)
return hba->ufs_version;
}
+static void ufs_mtk_round_clk_freq(struct ufs_hba *hba,
+ struct ufs_clk_info *clki)
+{
+ long max_rounded, min_rounded;
+
+ if (IS_ERR_OR_NULL(clki->clk) || !clki->max_freq || !clki->min_freq)
+ return;
+
+ max_rounded = clk_round_rate(clki->clk, clki->max_freq);
+ min_rounded = clk_round_rate(clki->clk, clki->min_freq);
+ if (max_rounded <= 0 || min_rounded <= 0 ||
+ min_rounded >= max_rounded || (u64)max_rounded > U32_MAX)
+ return;
+
+ if (max_rounded != clki->max_freq) {
+ dev_dbg(hba->dev, "%s: max_freq %u rounds to %ld\n",
+ clki->name, clki->max_freq, max_rounded);
+ if (clki->curr_freq == clki->max_freq)
+ clki->curr_freq = max_rounded;
+ clki->max_freq = max_rounded;
+ }
+
+ if (min_rounded != clki->min_freq) {
+ dev_dbg(hba->dev, "%s: min_freq %u rounds to %ld\n",
+ clki->name, clki->min_freq, min_rounded);
+ if (clki->curr_freq == clki->min_freq)
+ clki->curr_freq = min_rounded;
+ clki->min_freq = min_rounded;
+ }
+}
+
/**
* ufs_mtk_init_clocks - Init mtk driver private clocks
*
@@ -1022,6 +1053,10 @@ static void ufs_mtk_init_clocks(struct ufs_hba *hba)
return;
}
+ ufs_mtk_round_clk_freq(hba, host->mclk.ufs_sel_clki);
+ if (host->mclk.ufs_fde_clki)
+ ufs_mtk_round_clk_freq(hba, host->mclk.ufs_fde_clki);
+
/*
* Default get vcore if dts have these settings.
* No matter clock scaling support or not. (may disable by customer)
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/3] scsi: ufs: mediatek: skip PA_TXHSADAPTTYPE on pre-UniPro-1.8 hosts
2026-11-26 3:10 ` [PATCH v2 1/3] scsi: ufs: mediatek: skip PA_TXHSADAPTTYPE on pre-UniPro-1.8 hosts Ilia Snezhkov via B4 Relay
@ 2026-11-26 13:01 ` Peter Wang
0 siblings, 0 replies; 7+ messages in thread
From: Peter Wang @ 2026-11-26 13:01 UTC (permalink / raw)
To: iasnezhkov, stanley.chu, martin.petersen, James.Bottomley
Cc: matthias.bgg, angelogioacchino.delregno, louisalexis.eyraud,
linux-scsi, linux-mediatek, linux-kernel
On Thu, 2026-11-26 at 03:10 +0000, Ilia Snezhkov via B4 Relay wrote:
> From: Ilia Snezhkov <iasnezhkov@gmail.com>
>
> ufs_mtk_pre_pwr_change() calls ufshcd_dme_configure_adapt() on every
> power mode change, which writes PA_TXHSADAPTTYPE (0x15d4) even when
> passing PA_NO_ADAPT. PA_TXHSADAPTTYPE was introduced in UniPro 1.8,
> and
> ufs_mtk_get_controller_version() sets host->hw_ver.major = 3 only
> when
> PA_LOCALVERINFO >= UFS_UNIPRO_VER_1_8 (leaving older hosts at major =
> 2).
>
> On pre-UniPro-1.8 MediaTek hosts such as MT6789 (UFS 2.2, HS-G3 rate
> B),
> the DME write fails on every gear change with
> "dme-set: attr-id 0x15d4 val 0x3 failed 0 retries". While mainline
> ufshcd_change_power_mode() ignores the PRE_CHANGE return value after
> commit d3eba21c7170 ("scsi: ufs: core: Introduce a new ufshcd vops
> negotiate_pwr_mode()"), older stable kernels still discard the
> negotiated parameters on PRE_CHANGE failure, and mainline still emits
> spurious DME-SET errors on every power-mode transition.
>
> Return early from ufs_mtk_pre_pwr_change() when host->hw_ver.major <
> 3
> so pre-UniPro-1.8 hosts do not write PA_TXHSADAPTTYPE, and simplify
> the
> remaining major >= 3 branch.
>
> Fixes: 7212d624f863 ("scsi: ufs: host: mediatek: Fix PWM mode switch
> issue")
> Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
> ---
Hi Ilia,
Thank you for fixing this bug.
Reviewed-by: Peter Wang <peter.wang@mediatek.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/3] scsi: ufs: mediatek: allow clock scaling when ufs_fde is absent
2026-11-26 3:10 ` [PATCH v2 2/3] scsi: ufs: mediatek: allow clock scaling when ufs_fde is absent Ilia Snezhkov via B4 Relay
@ 2026-11-26 13:03 ` Peter Wang
0 siblings, 0 replies; 7+ messages in thread
From: Peter Wang @ 2026-11-26 13:03 UTC (permalink / raw)
To: iasnezhkov, stanley.chu, martin.petersen, James.Bottomley
Cc: matthias.bgg, angelogioacchino.delregno, louisalexis.eyraud,
linux-scsi, linux-mediatek, linux-kernel
On Thu, 2026-11-26 at 03:10 +0000, Ilia Snezhkov via B4 Relay wrote:
> From: Ilia Snezhkov <iasnezhkov@gmail.com>
>
> ufs_mtk_is_clk_scale_ready() enables UFSHCD_CAP_CLK_SCALING whenever
> ufs_sel, ufs_sel_max_src, and ufs_sel_min_src are present, treating
> the
> FDE (AES) clock as optional. However, _ufs_mtk_clk_scale() bails out
> early on !fde_clki, so on SoCs without a ufs_fde clock (such as
> MT6789)
> ufshcd believes scaling succeeded and updates clki->curr_freq while
> the
> ufs_sel mux never switches parents.
>
> Only require clki (ufs_sel) in _ufs_mtk_clk_scale(), and gate
> clk_fde_scale on fde_clki being non-NULL alongside its min/max
> parents.
Hi Ilia,
The logic is basically fine, but I'm a bit curious:
would this change cause MT6789 to enable UFSHCD_CAP_CLK_SCALING?
Thanks
Peter
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 3/3] scsi: ufs: mediatek: round scalable clock bounds to clk_round_rate()
2026-11-26 3:10 ` [PATCH v2 3/3] scsi: ufs: mediatek: round scalable clock bounds to clk_round_rate() Ilia Snezhkov via B4 Relay
@ 2026-11-26 13:04 ` Peter Wang
0 siblings, 0 replies; 7+ messages in thread
From: Peter Wang @ 2026-11-26 13:04 UTC (permalink / raw)
To: iasnezhkov, stanley.chu, martin.petersen, James.Bottomley
Cc: matthias.bgg, angelogioacchino.delregno, louisalexis.eyraud,
linux-scsi, linux-mediatek, linux-kernel
On Thu, 2026-11-26 at 03:10 +0000, Ilia Snezhkov via B4 Relay wrote:
> From: Ilia Snezhkov <iasnezhkov@gmail.com>
>
> ufshcd_devfreq_target() determines scale_up via an exact equality
> check
> against clki->max_freq after rounding the target frequency. When a DT
> freq-table-hz lists a nominal value that a PLL-derived clock cannot
> hit
> exactly (for example, msdcpll_d2 on MT6789 delivers 191999939 Hz for
> a
> 192000000 Hz DT maximum), clk_round_rate() produces a rate that never
Hi Ilia,
I am curious why the HW generates this value of 191999939 Hz.
Should the HW need a calibration to generate exactly 192000000 Hz?
Thanks.
Peter
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-26 13:04 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 3:10 [PATCH v2 0/3] scsi: ufs: mediatek: pre-UniPro-1.8 adapt and clock scaling fixes Ilia Snezhkov via B4 Relay
2026-11-26 3:10 ` [PATCH v2 1/3] scsi: ufs: mediatek: skip PA_TXHSADAPTTYPE on pre-UniPro-1.8 hosts Ilia Snezhkov via B4 Relay
2026-11-26 13:01 ` Peter Wang
2026-11-26 3:10 ` [PATCH v2 2/3] scsi: ufs: mediatek: allow clock scaling when ufs_fde is absent Ilia Snezhkov via B4 Relay
2026-11-26 13:03 ` Peter Wang
2026-11-26 3:10 ` [PATCH v2 3/3] scsi: ufs: mediatek: round scalable clock bounds to clk_round_rate() Ilia Snezhkov via B4 Relay
2026-11-26 13:04 ` Peter Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox