scsi: ufs: mediatek: pre-UniPro-1.8 adapt and clock scaling fixes
v2 cover scsi: ufs: mediatek: pre-UniPro-1.8 adapt and clock scaling fixes
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(-)
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH v2 1/3 scsi: ufs: mediatek: skip PA_TXHSADAPTTYPE on pre-UniPro-1.8 hosts
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.
---
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(-)
drivers/ufs/host/ufs-mediatek.c +8 -11
@@ -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;
Signature · 1 line
2.54.0 (Apple Git-157)
Re: [PATCH v2 1/3] scsi: ufs: mediatek: skip PA_TXHSADAPTTYPE on pre-UniPro-1.8 hosts
Quoted 27 lines · On Thu, 2026-11-26 at 03:10 +0000, Ilia Snezhkov via B4 Relay wrote:
From: Ilia Snezhkov <[email protected]>
ufs_mtk_pre_pwr_change()callsufshcd_dme_configure_adapt()on every power mode change, which writesPA_TXHSADAPTTYPE(0x15d4) even when passingPA_NO_ADAPT.PA_TXHSADAPTTYPEwas introduced in UniPro 1.8, andufs_mtk_get_controller_version()setshost->hw_ver.major= 3 only whenPA_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-id0x15d4val0x3failed 0 retries". While mainlineufshcd_change_power_mode()ignores thePRE_CHANGEreturn value after commitd3eba21c7170("scsi: ufs: core: Introduce a new ufshcd vopsnegotiate_pwr_mode()"), older stable kernels still discard the negotiated parameters onPRE_CHANGEfailure, and mainline still emits spurious DME-SET errors on every power-mode transition.Return early from
ufs_mtk_pre_pwr_change()whenhost->hw_ver.major< 3
so pre-UniPro-1.8 hosts do not writePA_TXHSADAPTTYPE, and simplify the
remaining major >= 3 branch.
Quoted 4 lines
Fixes 7212d624f863 ("scsi: ufs: host: mediatek: Fix PWM mode switchissue")
Signed-off-by Ilia Snezhkov <[email protected]>---
Hi Ilia,
Thank you for fixing this bug.
PATCH v2 2/3 scsi: ufs: mediatek: allow clock scaling when ufs_fde is absent
In reply to Ilia Snezhkovufs_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.
---
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(-)
drivers/ufs/host/ufs-mediatek.c +2 -2
@@ -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);
Signature · 1 line
2.54.0 (Apple Git-157)
Peter Wang
maintainerRe: [PATCH v2 2/3] scsi: ufs: mediatek: allow clock scaling when ufs_fde is absent
Quoted 12 lines · On Thu, 2026-11-26 at 03:10 +0000, Ilia Snezhkov via B4 Relay wrote:
From: Ilia Snezhkov <[email protected]>
ufs_mtk_is_clk_scale_ready()enablesUFSHCD_CAP_CLK_SCALINGwheneverufs_sel,ufs_sel_max_src, andufs_sel_min_srcare present, treating the
FDE (AES) clock as optional. However,_ufs_mtk_clk_scale()bails out early on !fde_clki, so on SoCs without aufs_fdeclock (such as MT6789)
ufshcd believes scaling succeeded and updatesclki->curr_freqwhile theufs_selmux never switches parents.
Quoted 3 lines
Only require clki (
ufs_sel) in_ufs_mtk_clk_scale(), and gateclk_fde_scaleonfde_clkibeing 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
PATCH v2 3/3 scsi: ufs: mediatek: round scalable clock bounds to clk_round_rate()
In reply to Ilia Snezhkovufshcd_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.
---
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(+)
drivers/ufs/host/ufs-mediatek.c +35 -0
@@ -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)
Signature · 1 line
2.54.0 (Apple Git-157)
Peter Wang
maintainerRe: [PATCH v2 3/3] scsi: ufs: mediatek: round scalable clock bounds to clk_round_rate()
Quoted 10 lines · On Thu, 2026-11-26 at 03:10 +0000, Ilia Snezhkov via B4 Relay wrote:
From: Ilia Snezhkov <[email protected]>
ufshcd_devfreq_target()determinesscale_upvia an exact equality check
againstclki->max_freqafter 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_d2on 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