From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (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 B53C73C2B80; Tue, 13 Oct 2026 14:14:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.85.4 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791900885; cv=none; b=UNooRdZysFxJOUQ+AnPVaXmogat92pnOmSQO2Mb7xiqZQfDFkBRdeIxoVCE/Y90xD9V5UCGO7bWAmxc0OtZF9U3d1b2Sj9qqpJOZgc2tgZz6ldttAfiHuOEezs9kkiaTEwTyZqf//mcJcqmlu+tIwHCXkmciXBhsL6I4MVi0cUI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791900885; c=relaxed/simple; bh=kpi4OqmK9zYQD8EfRtqF50euuZmKm4ovebnl2K4Bvo0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vg7QPS5hqYCVsufJyBR6mauJg6FWcLYnjiwiNhJG28MVYQ8XADcMqISRUoJesryLoHFTF/hkYsKiOviSx542K6lwrirQlzl01asb1jch6dWlBrMTAd0rRq0jeX/W8AHo9drY+/mNPtUJG6nBzr0IF4td3gU3sgphRsJPynhe6E4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=dIfI6zYR; arc=none smtp.client-ip=185.246.85.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="dIfI6zYR" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id 402E84E41209; Tue, 13 Oct 2026 14:14:42 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 14C7C60362; Tue, 13 Oct 2026 14:14:42 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 3AABC11D71027; Tue, 13 Oct 2026 16:14:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1791900877; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=LT6bzH7ij2G7iHBsS599MfmBcz4/Mb03S3bgNvQVnBc=; b=dIfI6zYRJRaiFANLMckonG3G2cKjzZfwbEjyFjxFpw+XLuDKqycktDUHuZLYdUOw/xj6Su 2m0XvMOf7b982RCQECaxwaoyxL8nGj0nzeOGh3utsEMIQSjVt4a5t3Ew/xXYEt1HOYIRgJ CDSOEeZqiwWje+jzBX4gpX/XzrNZEp0wAm6s/ejC647ggEjJJo0wOv50+pqcI+XhmCRy9w ViJ2I/hR8fiEX3pv/kEtUeFpwaAS12clz422LFCezMrp3tj6XKMJjD5rRHM7pq9LZQBWQV k9ocVuKftSuXs1/JG8XfqppYq2iAhkF6enAZw1diuk4nSbnzEIBAUS1FIZw9bA== From: Paul Cercueil To: Pratyush Yadav , Michael Walle , Takahiro Kuwano , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Mark Brown , Thomas Petazzoni Cc: Kees Cook , Tony Luck , "Guilherme G . Piccoli" , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, linux-spi@vger.kernel.org, Bharat Bhushan , Sunil Kovvuri Goutham , Tanmay Jagdale , Paul Cercueil Subject: [PATCH v2 2/7] mtd: spi-nor: Wait for NOR ready with poll_timeout_us() Date: Tue, 13 Oct 2026 16:13:45 +0200 Message-ID: <20261013141350.189996-3-paul.cercueil@bootlin.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261013141350.189996-1-paul.cercueil@bootlin.com> References: <20261013141350.189996-1-paul.cercueil@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Last-TLS-Session-Version: TLSv1.3 From: Tanmay Jagdale Instead of polling for the spi_nor_ready() with a custom loop, use poll_timeout_us(). The sleep period between two polls is set to 100 us which I think should be fine. This will make it simpler to support waiting for the SPI NOR in atomic context later. Signed-off-by: Tanmay Jagdale Co-developed-by: Paul Cercueil Signed-off-by: Paul Cercueil --- Notes: v2: New patch drivers/mtd/spi-nor/core.c | 39 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c index 8bc117b46e02..2509fb983d4f 100644 --- a/drivers/mtd/spi-nor/core.c +++ b/drivers/mtd/spi-nor/core.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -32,13 +33,13 @@ * For everything but full-chip erase; probably could be much smaller, but kept * around for safety for now */ -#define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ) +#define DEFAULT_READY_TIMEOUT_MS 40000UL /* * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up * for larger flash */ -#define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ) +#define CHIP_ERASE_2MB_READY_TIMEOUT_MS 40000UL #define SPI_NOR_MAX_ADDR_NBYTES 4 @@ -712,29 +713,21 @@ static int spi_nor_ready(struct spi_nor *nor) * Return: 0 on success, -errno otherwise. */ static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor, - unsigned long timeout_jiffies) + unsigned long timeout_ms) { - unsigned long deadline; - int timeout = 0, ret; + int ret, status; - deadline = jiffies + timeout_jiffies; - - while (!timeout) { - if (time_after_eq(jiffies, deadline)) - timeout = 1; - - ret = spi_nor_ready(nor); - if (ret < 0) - return ret; - if (ret) - return 0; - - cond_resched(); + ret = poll_timeout_us(status = spi_nor_ready(nor), status, + 100, timeout_ms * 1000, false); + if (ret < 0) { + dev_dbg(nor->dev, "flash operation timed out\n"); + return ret; } - dev_dbg(nor->dev, "flash operation timed out\n"); + if (status < 0) + return status; - return -ETIMEDOUT; + return 0; } /** @@ -747,7 +740,7 @@ static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor, int spi_nor_wait_till_ready(struct spi_nor *nor) { return spi_nor_wait_till_ready_with_timeout(nor, - DEFAULT_READY_WAIT_JIFFIES); + DEFAULT_READY_TIMEOUT_MS); } /** @@ -1781,8 +1774,8 @@ static int spi_nor_erase_dice(struct spi_nor *nor, loff_t addr, * pull these from CFI/SFDP, but these values should be good * enough for now. */ - timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES, - CHIP_ERASE_2MB_READY_WAIT_JIFFIES * + timeout = max(CHIP_ERASE_2MB_READY_TIMEOUT_MS, + CHIP_ERASE_2MB_READY_TIMEOUT_MS * (unsigned long)(nor->mtd.size / SZ_2M)); do { -- 2.47.3