Re: [PATCH] mmc: mtk-sd: roll back clocks when msdc_ungate_clock() fails

From: Ulf Hansson

Date: Mon Nov 30 2026 - 11:15:50 EST


On Tue, Nov 17, 2026 at 3:01 AM Luo Yifan <luoyifan@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> msdc_ungate_clock() enables h_clk, bus_clk, src_clk, src_clk_cg and
> crypto_clk before enabling the bulk clocks, but ignores the return values
> of those clk_prepare_enable() calls. If one of them fails, the function
> keeps going and returns the result of clk_bulk_prepare_enable() or
> readl_poll_timeout() instead, leaving the clocks enabled so far on.
>
> The probe error path then calls msdc_gate_clock(), which also disables the
> clocks that were never enabled. The runtime resume path only propagates
> the error, so the clocks stay on and the next suspend or resume works on
> an inconsistent clock state.
>
> Check every clk_prepare_enable() and unwind the clocks in reverse order
> when one fails, including the controller clock stabilization timeout.
> Since msdc_ungate_clock() now cleans up after itself, a failure from it in
> the probe path can go directly to release_mem, without disabling the
> clocks a second time. The other probe error paths still gate the clocks
> through release.
>
> Signed-off-by: Luo Yifan <luoyifan@xxxxxxxxxxxxxxxxxxxx>

Applied for next, thanks!

Kind regards
Uffe


> ---
> drivers/mmc/host/mtk-sd.c | 54 +++++++++++++++++++++++++++++++--------
> 1 file changed, 44 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
> index c9c6367553..49238acedb 100644
> --- a/drivers/mmc/host/mtk-sd.c
> +++ b/drivers/mmc/host/mtk-sd.c
> @@ -957,19 +957,53 @@ static int msdc_ungate_clock(struct msdc_host *host)
> u32 val;
> int ret;
>
> - clk_prepare_enable(host->h_clk);
> - clk_prepare_enable(host->bus_clk);
> - clk_prepare_enable(host->src_clk);
> - clk_prepare_enable(host->src_clk_cg);
> - clk_prepare_enable(host->crypto_clk);
> + ret = clk_prepare_enable(host->h_clk);
> + if (ret)
> + return ret;
> +
> + ret = clk_prepare_enable(host->bus_clk);
> + if (ret)
> + goto err_h_clk;
> +
> + ret = clk_prepare_enable(host->src_clk);
> + if (ret)
> + goto err_bus_clk;
> +
> + ret = clk_prepare_enable(host->src_clk_cg);
> + if (ret)
> + goto err_src_clk;
> +
> + ret = clk_prepare_enable(host->crypto_clk);
> + if (ret)
> + goto err_src_clk_cg;
> +
> ret = clk_bulk_prepare_enable(MSDC_NR_CLOCKS, host->bulk_clks);
> if (ret) {
> dev_err(host->dev, "Cannot enable pclk/axi/ahb clock gates\n");
> - return ret;
> + goto err_crypto_clk;
> }
>
> - return readl_poll_timeout(host->base + MSDC_CFG, val,
> - (val & MSDC_CFG_CKSTB), 1, 20000);
> + ret = readl_poll_timeout(host->base + MSDC_CFG, val,
> + (val & MSDC_CFG_CKSTB), 1, 20000);
> + if (ret)
> + goto err_bulk_clks;
> +
> + return 0;
> +
> +err_bulk_clks:
> + clk_bulk_disable_unprepare(MSDC_NR_CLOCKS, host->bulk_clks);
> +err_crypto_clk:
> + clk_disable_unprepare(host->crypto_clk);
> +err_src_clk_cg:
> + clk_disable_unprepare(host->src_clk_cg);
> +err_src_clk:
> + clk_disable_unprepare(host->src_clk);
> +err_bus_clk:
> + clk_disable_unprepare(host->bus_clk);
> +err_h_clk:
> + clk_disable_unprepare(host->h_clk);
> +
> + return ret;
> }
>
> static void msdc_new_tx_setting(struct msdc_host *host)
> @@ -3130,7 +3164,8 @@ static int msdc_drv_probe(struct platform_device *pdev)
> ret = msdc_ungate_clock(host);
> if (ret) {
> dev_err(&pdev->dev, "Cannot ungate clocks!\n");
> - goto release_clk;
> + platform_set_drvdata(pdev, NULL);
> + goto release_mem;
> }
> msdc_init_hw(host);
>
> @@ -3186,7 +3221,6 @@ static int msdc_drv_probe(struct platform_device *pdev)
> pm_runtime_disable(host->dev);
> release:
> msdc_deinit_hw(host);
> -release_clk:
> msdc_gate_clock(host);
> platform_set_drvdata(pdev, NULL);
> release_mem:
> --
> 2.24.4
>
>
>