From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EA858C624D5 for ; Tue, 1 Sep 2026 15:39:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jh4SFRZq0mOHG032X4Vw1oUdVU04SIZwL26SLmED5hI=; b=gUh7/KJ25YCeYD ZOYyr8PD4aeKMY4MdwdG9nwKOXbygDBHnBXsWxY/kXPOQI446AAXXTpwdosiE2hMKyu4pCR3LrEU0 6QzjhleIW4gAOuxzzVTG/0FCfE4/XvIRPO6hTPO+wuquO33/KN82F6kDxy0ecZOJQobQKNNsowyNN TpKsV1Coc+k1jPReTEdwKYmDeApOGXM1Ix6eakQP5UhLATWAeHbFtZiS82+MVEiES3fV1SloOBG8x Q0wBlxElnRRv9vU6wIPLC9sjicMQ8pPzXzvYkuuZk4iqmoVntQ6rHuKPHtsjMmp3xhb5LzmyNCdxQ xoQ1mVkreca4iYbxRI/w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1x1Qad-0000000CY2V-1qV6; Tue, 01 Sep 2026 15:39:59 +0000 Received: from bali.collaboradmins.com ([2a01:4f8:201:9162::2]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1x1QaQ-0000000CXLn-2pvg; Tue, 01 Sep 2026 15:39:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1788277184; bh=ymebz7Y5/uUSOQIwsdu/iJB0xq+Mye+6P9M3cZf4mWs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=PAiuNIXQyXvr/7ae2v0buwqflSO0GeHPUB3kIe5NCyWoF4iy6VzWokrzS9M0lIVkR 5LxIExPW1SL5ALX2QoKOISLqw2gBrt0XnI5eEIFZcXdZzZudoSauA1qC0SH62C008t y1Mf3O6Ini9hncn/lE9ElGaRSMVdwVgC0TUhwrP9fT1IClsPlBe6WJYsdyJQYlKojq s4AbL8ZiZ6927iCimHSyUwVzGdTdSH48fUSNg28q0IOoK8epitSkfdcvx9PWGK5eEF HLJ64YXtEmyeb0iDnCkS9pjNv4GgVrmIXNWih9Dm9wJQsbG9gkDMFYly4uau2d0Uhk ZgcNBCHJ+ndwQ== Received: from yukiji.home (unknown [100.64.0.131]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: laeyraud) by bali.collaboradmins.com (Postfix) with ESMTPSA id B0F3717E0FC9; Tue, 01 Sep 2026 17:39:43 +0200 (CEST) From: Louis-Alexis Eyraud Date: Tue, 01 Sep 2026 17:39:06 +0200 Subject: [PATCH v11 18/24] scsi: ufs: mediatek: Rework ufs_mtk_wait_idle_state MIME-Version: 1.0 Message-Id: <20260901-mt8196-ufs-v11-18-dbd007704490@collabora.com> References: <20260901-mt8196-ufs-v11-0-dbd007704490@collabora.com> In-Reply-To: <20260901-mt8196-ufs-v11-0-dbd007704490@collabora.com> To: Chunfeng Yun , Vinod Koul , Neil Armstrong , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Matthias Brugger , AngeloGioacchino Del Regno , Peter Wang , Chaotian Jing , Alim Akhtar , Avri Altman , Bart Van Assche , Stanley Jhu , "James E.J. Bottomley" , Philipp Zabel , Liam Girdwood , Mark Brown , Manivannan Sadhasivam , "Martin K. Petersen" , "Martin K. Petersen" Cc: kernel@collabora.com, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-phy@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, Nicolas Frattaroli , Louis-Alexis Eyraud X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788277163; l=4343; i=louisalexis.eyraud@collabora.com; s=20250113; h=from:subject:message-id; bh=jrB3JYKQQN/6KhtGIU+klNZ7FPFTnMtd1WM+alH9PAY=; b=FFZ7/CmSxu9GhiiQl9N5QfuixQ50pMed/2rWuxM50vBznjTleZhs2b2+loBUheqHcpm7ZY4T9 GI30iDJ6JudBWQeflD+vsFhnYhcwG9kux2MQU4ZOJW/+W4DDLP08ud0 X-Developer-Key: i=louisalexis.eyraud@collabora.com; a=ed25519; pk=CHFBDB2Kqh4EHc6JIqFn69GhxJJAzc0Zr4e8QxtumuM= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260901_083946_922820_1BEAFFED X-CRM114-Status: GOOD ( 19.42 ) X-BeenThere: linux-phy@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux Phy Mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-phy" Errors-To: linux-phy-bounces+linux-phy=archiver.kernel.org@lists.infradead.org From: Nicolas Frattaroli While ufs_mtk_wait_idle state has some code smells for me (the VS_HCE_BASE early exit seems racey at best), it can still benefit from some general cleanup to make the code flow less convoluted. Use the iopoll helpers, for one, and specifically the one that sleeps and does not busy delay, as it's being done for up to 5ms. The register read is split out to a helper function that branches between new and old style flow. Every called uses the same 5ms timeout value, so there is no point in making this a parameter. Just assume a 5ms timeout in the function. Reviewed-by: Peter Wang Reviewed-by: AngeloGioacchino Del Regno Signed-off-by: Nicolas Frattaroli Signed-off-by: Louis-Alexis Eyraud --- drivers/ufs/host/ufs-mediatek.c | 71 +++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c index a84bffe069eb..c71a4034127e 100644 --- a/drivers/ufs/host/ufs-mediatek.c +++ b/drivers/ufs/host/ufs-mediatek.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -383,51 +384,39 @@ static void ufs_mtk_dbg_sel(struct ufs_hba *hba) } } -static int ufs_mtk_wait_idle_state(struct ufs_hba *hba, - unsigned long retry_ms) +static u32 ufs_mtk_read_state(struct ufs_hba *hba, bool old_style) { - u64 timeout, time_checked; - u32 val, sm; - bool wait_idle; - struct ufs_mtk_host *host = ufshcd_get_variant(hba); - - /* cannot use plain ktime_get() in suspend */ - timeout = ktime_get_mono_fast_ns() + retry_ms * 1000000UL; - - /* wait a specific time after check base */ - udelay(10); - wait_idle = false; + u32 val; - do { - time_checked = ktime_get_mono_fast_ns(); - if (host->legacy_ip_ver || host->ip_ver < IP_VER_MT6899) { - ufs_mtk_dbg_sel(hba); - val = ufshcd_readl(hba, REG_UFS_PROBE); - } else { - val = ufshcd_readl(hba, REG_UFS_UFS_MMIO_OTSD_CTRL); - val = val >> 16; - } + if (old_style) { + ufs_mtk_dbg_sel(hba); + val = ufshcd_readl(hba, REG_UFS_PROBE); + } else { + val = ufshcd_readl(hba, REG_UFS_UFS_MMIO_OTSD_CTRL) >> 16; + } - sm = val & 0x1f; + return FIELD_GET(0x1f, val); +} - /* - * if state is in H8 enter and H8 enter confirm - * wait until return to idle state. - */ - if ((sm >= VS_HIB_ENTER) && (sm <= VS_HIB_EXIT)) { - wait_idle = true; - udelay(50); - continue; - } else if (!wait_idle) - break; +static int ufs_mtk_wait_idle_state(struct ufs_hba *hba) +{ + struct ufs_mtk_host *host = ufshcd_get_variant(hba); + bool old_style = (host->legacy_ip_ver || host->ip_ver < IP_VER_MT6899); + u32 val; + int ret; - if (wait_idle && (sm == VS_HCE_BASE)) - break; - } while (time_checked < timeout); + /* If the device isn't in a hibernate state after 10us, don't wait. */ + udelay(10); + val = ufs_mtk_read_state(hba, old_style); + if (val < VS_HIB_ENTER || val > VS_HIB_EXIT) + return 0; - if (wait_idle && sm != VS_HCE_BASE) { - dev_info(hba->dev, "wait idle tmo: 0x%x\n", val); - return -ETIMEDOUT; + /* Poll to wait for idle */ + ret = read_poll_timeout(ufs_mtk_read_state, val, (val == VS_HCE_BASE), + 50, 5 * USEC_PER_MSEC, false, hba, old_style); + if (ret) { + dev_err(hba->dev, "Timed out waiting for idle state, val = 0x%x\n", val); + return ret; } return 0; @@ -1432,7 +1421,7 @@ 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 */ - ret = ufs_mtk_wait_idle_state(hba, 5); + ret = ufs_mtk_wait_idle_state(hba); if (ret) goto out; @@ -1634,7 +1623,7 @@ static int ufs_mtk_link_set_hpm(struct ufs_hba *hba) return err; /* Check link state to make sure exit h8 success */ - err = ufs_mtk_wait_idle_state(hba, 5); + err = ufs_mtk_wait_idle_state(hba); if (err) { dev_err(hba->dev, "Failed to wait for idle: %pe\n", ERR_PTR(err)); return err; -- 2.55.0 -- linux-phy mailing list linux-phy@lists.infradead.org https://lists.infradead.org/mailman/listinfo/linux-phy