Skip to contentSkip to conversation

mtd: spi-nor: Fix chip erase timeout issue

PATCH Superseded Ye Li 3 participants 4 messages Sep 11, 2024 – Sep 12, 2024 Memory technology devices (MTD)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ye Li

author
#0 lore raw

PATCH 1/1 mtd: spi-nor: Fix chip erase timeout issue

Chip erase support was added to spi_nor_erase, but the timeout for polling SR ready is not updated and still for sector erase. So the timeout value is not enough for chip erase on some NOR flash. Follow kernel implementation to set new timeout for chip erase.

Fixes b91a0822d752 ("mtd: spi-nor: Add CHIP_ERASE optimization")
---
 drivers/mtd/spi/spi-nor-core.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/mtd/spi/spi-nor-core.c +20 -1
index aea611fef523..338e5856337e 100644
@@ -44,6 +44,12 @@
 
 #define DEFAULT_READY_WAIT_JIFFIES		(40UL * HZ)
 
+/*
+ * 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 ROUND_UP_TO(x, y)	(((x) + (y) - 1) / (y) * (y))
 
 struct sfdp_parameter_header {
@@ -991,6 +997,7 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
 	bool addr_known = false;
 	u32 addr, len, rem;
 	int ret, err;
+	unsigned long timeout = 0;
 
 	dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
 		(long long)instr->len);
@@ -1026,6 +1033,15 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
 		if (len == mtd->size &&
 		    !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
 			ret = spi_nor_erase_chip(nor);
+			/*
+			 * Scale the timeout linearly with the size of the flash, with
+			 * a minimum calibrated to an old 2MB flash. We could try to
+			 * 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 *
+				      (unsigned long)(mtd->size / SZ_2M));
 		} else {
 			ret = spi_nor_erase_sector(nor, addr);
 		}
@@ -1035,7 +1051,10 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
 		addr += ret;
 		len -= ret;
 
-		ret = spi_nor_wait_till_ready(nor);
+		if (timeout)
+			ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
+		else
+			ret = spi_nor_wait_till_ready(nor);
 		if (ret)
 			goto erase_err;
 	}
Signature · 1 line
2.7.4
#1 lore raw
Quoted 4 lines · Ye Li

Chip erase support was added to spi_nor_erase, but the timeout for polling SR ready is not updated and still for sector erase. So the timeout value is not enough for chip erase on some NOR flash. Follow kernel implementation to set new timeout for chip erase.

Is there a matching Linux kernel commit you could point to, from which this was ported over ?

Quoted 25 lines
Fixes b91a0822d752 ("mtd: spi-nor: Add CHIP_ERASE optimization")
---
  drivers/mtd/spi/spi-nor-core.c | 21 ++++++++++++++++++++-
  1 file changed, 20 insertions(+), 1 deletion(-)
drivers/mtd/spi/spi-nor-core.c +6 -0
index aea611fef523..338e5856337e 100644
@@ -44,6 +44,12 @@
 
  #define DEFAULT_READY_WAIT_JIFFIES		(40UL * HZ)
 
+/*
+ * 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 ROUND_UP_TO(x, y)	(((x) + (y) - 1) / (y) * (y))
 
  struct sfdp_parameter_header {

drivers/mtd/spi/spi-nor-core.c static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr) · line 1000

Quoted 4 lines
drivers/mtd/spi/spi-nor-core.c +1 -0
@@ -991,6 +997,7 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
  	bool addr_known = false;
  	u32 addr, len, rem;
  	int ret, err;
+	unsigned long timeout = 0;

Keep the variables in reverse xmas-tree please.

Quoted 5 lines
drivers/mtd/spi/spi-nor-core.c +0 -0
  	dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
  		(long long)instr->len);
@@ -1026,6 +1033,15 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
  		if (len == mtd->size &&
  		    !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {

drivers/mtd/spi/spi-nor-core.c static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr) · line 1044

Quoted 10 lines
drivers/mtd/spi/spi-nor-core.c +9 -0
  			ret = spi_nor_erase_chip(nor);
+			/*
+			 * Scale the timeout linearly with the size of the flash, with
+			 * a minimum calibrated to an old 2MB flash. We could try to
+			 * 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 *
+				      (unsigned long)(mtd->size / SZ_2M));

Is the type cast needed ?

Thanks

#2 lore raw
Quoted 7 lines · Marek Vasut
Quoted 4 lines · Ye Li

Chip erase support was added to spi_nor_erase, but the timeout for polling SR ready is not updated and still for sector erase. So the timeout value is not enough for chip erase on some NOR flash. Follow kernel implementation to set new timeout for chip erase.

Is there a matching Linux kernel commit you could point to, from which this was ported over ?

Yes.   09b6a377687b mtd: spi-nor: scale up timeout for full-chip erase

Quoted 23 lines
Quoted 23 lines
Fixes b91a0822d752 ("mtd: spi-nor: Add CHIP_ERASE optimization")

---
  drivers/mtd/spi/spi-nor-core.c | 21 ++++++++++++++++++++-

  1 file changed, 20 insertions(+), 1 deletion(-)
drivers/mtd/spi/spi-nor-core.c +0 -0
index aea611fef523..338e5856337e 100644
@@ -44,6 +44,12 @@
     #define DEFAULT_READY_WAIT_JIFFIES        (40UL * HZ)

  +/*
+ * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
+ * for larger flash
+ */

drivers/mtd/spi/spi-nor-core.c +2 -0
+#define CHIP_ERASE_2MB_READY_WAIT_JIFFIES    (40UL * HZ)
+

  #define ROUND_UP_TO(x, y)    (((x) + (y) - 1) / (y) * (y)) struct sfdp_parameter_header {

Quoted 8 lines

drivers/mtd/spi/spi-nor-core.c static int spi_nor_erase(struct mtd_info *mtd, · line 1001

Quoted 6 lines
drivers/mtd/spi/spi-nor-core.c +1 -0
@@ -991,6 +997,7 @@ static int spi_nor_erase(struct mtd_info *mtd, 
 struct erase_info *instr)
       bool addr_known = false;
       u32 addr, len, rem;
       int ret, err;
+    unsigned long timeout = 0;

Keep the variables in reverse xmas-tree please.

Will update it in v2.

Quoted 13 lines
Quoted 13 lines
drivers/mtd/spi/spi-nor-core.c +0 -0
       dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
           (long long)instr->len);
@@ -1026,6 +1033,15 @@ static int spi_nor_erase(struct mtd_info *mtd, 
 struct erase_info *instr)

          if (len == mtd->size &&
              !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) { ret = spi_nor_erase_chip(nor);
+            /*
+             * Scale the timeout linearly with the size of the flash, with
+             * a minimum calibrated to an old 2MB flash. We could try to

Quoted 8 lines
Quoted 7 lines

+             * 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 * +                      (unsigned long)(mtd->size / SZ_2M));

Is the type cast needed ?

Yes. otherwise get below warning

In file included from include/linux/bitops.h:22,
                 from include/log.h:15,
                 from drivers/mtd/spi/spi-nor-core.c:13:
drivers/mtd/spi/spi-nor-core.c: In function ‘spi_nor_erase’:
include/linux/kernel.h:190:24: warning: comparison of distinct pointer types lacks a cast 190 |         (void) (&_max1 == &_max2);              \
      |                        ^~
drivers/mtd/spi/spi-nor-core.c:1042:35: note: in expansion of macro ‘max’ 1042 |                         timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES, |

Best regards,

Ye Li

Quoted 2 lines

Thanks

#3 lore raw

On 9/12/24 10:48 AM, Ye Li wrote:

[...]

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

+            timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES, +                      CHIP_ERASE_2MB_READY_WAIT_JIFFIES * +                      (unsigned long)(mtd->size / SZ_2M));

Is the type cast needed ?

Yes. otherwise get below warning

Quoted 11 lines

In file included from include/linux/bitops.h:22,
                 from include/log.h:15,
                 from drivers/mtd/spi/spi-nor-core.c:13:
drivers/mtd/spi/spi-nor-core.c: In function ‘spi_nor_erase’:
include/linux/kernel.h:190:24: warning: comparison of distinct pointer types lacks a cast
  190 |         (void) (&_max1 == &_max2);              \
      |                        ^~
drivers/mtd/spi/spi-nor-core.c:1042:35: note: in expansion of macro ‘max’
 1042 |                         timeout =
max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
      |

mtd->size is uint64 , so you need to use do_div() :

u64 sz = mtd->size;
do_div(sz, SZ_2M);
timeout = CHIP_ERASE_2MB_READY_WAIT_JIFFIES * max(1ULL, sz);