From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bali.collaboradmins.com (bali.collaboradmins.com [148.251.105.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F3184457AF; Mon, 14 Sep 2026 11:39:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.251.105.195 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789385985; cv=none; b=HC5WusAnKXNDJgvnuYciuuTxFJ8jlzJ7NpB4FacpZZ25iqTSva7L41MerBcXcMqwTKQub60BR9es5N+Ww/iQukxcTDblQC0EJW42UO/9asBxoNmEfB+UWr7bc0DzeDzC9Y6y+ForhNyg8eUVZ/OvGqyUl8VagxW1n/8xBcGCIH0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789385985; c=relaxed/simple; bh=CDdzCMsyY0hbLRq251sYGR1yW3tb8Oi9E3rqzOxvS3A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fyJ981EG8etYDbNTVnu8OBujPy5nVFaCrh71KnjVL5eEeklzeGm0xdZ618F9cHeuHn6XoEf6U2ifofVw7yBqjtrBkZKaHhMj4bWtbr6gy/F6wnsBpdAQl65B9XKlZjbho1qaAVZ1p3DAu1gj7z8JKphjdeD3Klx837Y6+WIz0ZQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=aMF6nVju; arc=none smtp.client-ip=148.251.105.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="aMF6nVju" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1789385981; bh=CDdzCMsyY0hbLRq251sYGR1yW3tb8Oi9E3rqzOxvS3A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=aMF6nVjuU0ISblQWTRlfacD+rzFKC8bU7/9oJCxH+XRO8/q3hMM5Vt+pz+QVn1M4K vZl6SdX4XjaGAKLy28QAXuxrlLpzVSq8/7P2Tr8bAd2lGlYQWlU5HP4dVassEGmEVQ CFu5tyjzUMvOd5oXiAciaPO6MCsHWFz+Fe2iZWYr/uszAsjoDur1qPsGTZxnnWIXQF fusYLo2xpeHH7J854WO/kVMgVHucvI3TCxSJx/eIwHz6jdtQeok9c6xOIXGQWeOUVZ kyHXz0ZHKFyO4aBGVbF5mAuT4nVLyBOsSRnqYcW8FO+50fLZdayed6k9Zu0y3qVsYa g+Si73EzsOELw== 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 0F33717E07DC; Mon, 14 Sep 2026 13:39:40 +0200 (CEST) From: Louis-Alexis Eyraud Date: Mon, 14 Sep 2026 13:39:04 +0200 Subject: [PATCH v12 18/24] scsi: ufs: mediatek: Rework ufs_mtk_wait_idle_state Precedence: bulk X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260914-mt8196-ufs-v12-18-9279d7ef814d@collabora.com> References: <20260914-mt8196-ufs-v12-0-9279d7ef814d@collabora.com> In-Reply-To: <20260914-mt8196-ufs-v12-0-9279d7ef814d@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" 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=1789385943; l=4343; i=louisalexis.eyraud@collabora.com; s=20250113; h=from:subject:message-id; bh=ynxLXnChHF4gNItLESWr8U8gDqusMdmhPDTxRufgRJ4=; b=+ytchkoDIpFr422hk3zKxa2FvMRiLRXCzeAOig0GtFRhfBOG2aE40JA4b9ido2MiboboTF1V2 /8fW+NqrnQUAwd31zCiPX0kk1jJaVJ9ZopRKXJcucRcqMB+Fonhr1qC X-Developer-Key: i=louisalexis.eyraud@collabora.com; a=ed25519; pk=CHFBDB2Kqh4EHc6JIqFn69GhxJJAzc0Zr4e8QxtumuM= 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 071e81357537..4a8f13e67f3f 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 @@ -384,51 +385,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; @@ -1442,7 +1431,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; @@ -1644,7 +1633,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