Skip to contentSkip to conversation

mtd: nand: spi: Use write_cache first and then update_cache in write operation

PATCH In review Sky Huang 3 participants 6 messages Nov 19, 2024 – May 26, 2025

Subsystems (3)
lore
Share & tools
lore

Sky Huang

author
#0 lore raw

PATCH 1/1 mtd: nand: spi: Use write_cache first and then update_cache in write operation

From: Sky Huang <[email protected]>

According to discussion with Miquel Raynal <[email protected]> and Chuanhong Guo <[email protected]> on FORESEE F35SQA002G patch, Chuanhong recommmends that we can use the following sequence in spinand_write_to_cache_op():

x1 mode:
02H(program data load) -> 84H(random program data load) -> 84H ...

x4 mode:
32H(program data load x4) -> 34H(random program data load x4) -> 34H ...

02H or 32H commands will clear cache buffer on SPI-NAND and load data to it. For those SPI controllers which can't finish transmission in single step, 84H or 34H will be triggered for the rest data.

We observe that some current SPI-NANDs, including FORESEE F35SQA001G and F35SQA002G, must use 02H or 32H to reset cache buffer in flash before using 84H or 34H. Or users may encounter program failure issue. This issue is not always reproducible, but it may occur and cause system instability.

This sequence should work on all SPI-NANDs nowadays. I also check with Foresee that the sequence can solve the above program failure issue.

On my test platform (MT7988), SPI driver is drivers/spi/spi-mt65xx.c. And I limit MTK_SPI_IPM_PACKET_SIZE to SZ_1K to simulate lightweight SPI controller which can only transmit 1024 bytes.

The test step is the following:
- mtd erase /dev/mtd2
- dd if=/dev/zero bs=2048 count=1 | tr '\0' '\xA5' > output.bin
- mtd write output.bin /dev/mtd2

Before applying this patch, write operation uses only 34H(update_cache):

[78.937720] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xa5
[78.945297] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xa5
[78.954251] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xa5
[78.962966] OP code: 0x10, addr val: 0x300
[78.968816] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[78.977233] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[78.985124] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[78.992527] OP code: 0x10, addr val: 0x301
[78.996981] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[79.004416] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[79.012031] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[79.019435] OP code: 0x10, addr val: 0x302

...

After applying this patch, write operation will use 32H and then 34H:

[ 5380.911269] OP code: 0x32, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xa5
[ 5380.918889] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xa5
[ 5380.927836] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xa5
[ 5380.936558] OP code: 0x10, addr val: 0x300
[ 5380.942411] OP code: 0x32, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[ 5380.950831] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[ 5380.958722] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[ 5380.966129] OP code: 0x10, addr val: 0x301
[ 5380.970376] OP code: 0x32, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[ 5380.977810] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[ 5380.985424] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[ 5380.992826] OP code: 0x10, addr val: 0x302

...

---
 drivers/mtd/nand/spi/core.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/mtd/nand/spi/core.c +5 -2
index b1df7f627161..351bd14d8e06 100644
@@ -506,7 +506,10 @@ static int spinand_write_to_cache_op(struct spinand_device *spinand,
 		nbytes -= ret;
 		column += ret;
 		buf += ret;
+
+		wdesc->info.op_tmpl = *spinand->op_templates.update_cache;
 	}
+	wdesc->info.op_tmpl = *spinand->op_templates.write_cache;
 
 	return 0;
 }
@@ -1037,7 +1040,7 @@ static int spinand_create_dirmap(struct spinand_device *spinand,
 	/* The plane number is passed in MSB just above the column address */
 	info.offset = plane << fls(nand->memorg.pagesize);
 
-	info.op_tmpl = *spinand->op_templates.update_cache;
+	info.op_tmpl = *spinand->op_templates.write_cache;
 	desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
 					  spinand->spimem, &info);
 	if (IS_ERR(desc))
@@ -1060,7 +1063,7 @@ static int spinand_create_dirmap(struct spinand_device *spinand,
 		return 0;
 	}
 
-	info.op_tmpl = *spinand->op_templates.update_cache;
+	info.op_tmpl = *spinand->op_templates.write_cache;
 	info.op_tmpl.data.ecc = true;
 	desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
 					  spinand->spimem, &info);
Signature · 1 line
2.45.2
maintainer
#1 lore raw

Hello,

Quoted 35 lines · Sky Huang

From: Sky Huang <[email protected]>

According to discussion with Miquel Raynal <[email protected]> and Chuanhong Guo <[email protected]> on FORESEE F35SQA002G patch, Chuanhong recommmends that we can use the following sequence in spinand_write_to_cache_op():

x1 mode:
02H(program data load) -> 84H(random program data load) -> 84H ...

x4 mode:
32H(program data load x4) -> 34H(random program data load x4) -> 34H ...

02H or 32H commands will clear cache buffer on SPI-NAND and load data to it. For those SPI controllers which can't finish transmission in single step, 84H or 34H will be triggered for the rest data.

We observe that some current SPI-NANDs, including FORESEE F35SQA001G and F35SQA002G, must use 02H or 32H to reset cache buffer in flash before using 84H or 34H. Or users may encounter program failure issue. This issue is not always reproducible, but it may occur and cause system instability.

This sequence should work on all SPI-NANDs nowadays. I also check with Foresee that the sequence can solve the above program failure issue.

On my test platform (MT7988), SPI driver is drivers/spi/spi-mt65xx.c. And I limit MTK_SPI_IPM_PACKET_SIZE to SZ_1K to simulate lightweight SPI controller which can only transmit 1024 bytes.

The test step is the following:
- mtd erase /dev/mtd2
- dd if=/dev/zero bs=2048 count=1 | tr '\0' '\xA5' > output.bin
- mtd write output.bin /dev/mtd2

Before applying this patch, write operation uses only 34H(update_cache):

Quoted 12 lines
[78.937720] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xa5
[78.945297] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xa5
[78.954251] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xa5
[78.962966] OP code: 0x10, addr val: 0x300
[78.968816] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[78.977233] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[78.985124] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[78.992527] OP code: 0x10, addr val: 0x301
[78.996981] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[79.004416] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[79.012031] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[79.019435] OP code: 0x10, addr val: 0x302

I am sorry but above you said that we should not perform:

    0x32, 0x32, 0x32...

because the second time it would clear the cache again. And here you tell us that actually the core already handles that by performing instead:

    0x34, 0x34, 0x34...

So what is the problem?

Or maybe I misunderstood the issue, but I think Chuanhong raised an issue that is already solved? Isn't it?

Thanks,
Miquèl

#2 lore raw

Hello!

Quoted 60 lines · Miquel Raynal

Hello,

Quoted 35 lines · Sky Huang

From: Sky Huang <[email protected]>

According to discussion with Miquel Raynal <[email protected]> and Chuanhong Guo <[email protected]> on FORESEE F35SQA002G patch, Chuanhong recommmends that we can use the following sequence in spinand_write_to_cache_op():

x1 mode:
02H(program data load) -> 84H(random program data load) -> 84H ...

x4 mode:
32H(program data load x4) -> 34H(random program data load x4) -> 34H ...

02H or 32H commands will clear cache buffer on SPI-NAND and load data to it. For those SPI controllers which can't finish transmission in single step, 84H or 34H will be triggered for the rest data.

We observe that some current SPI-NANDs, including FORESEE F35SQA001G and F35SQA002G, must use 02H or 32H to reset cache buffer in flash before using 84H or 34H. Or users may encounter program failure issue. This issue is not always reproducible, but it may occur and cause system instability.

This sequence should work on all SPI-NANDs nowadays. I also check with Foresee that the sequence can solve the above program failure issue.

On my test platform (MT7988), SPI driver is drivers/spi/spi-mt65xx.c. And I limit MTK_SPI_IPM_PACKET_SIZE to SZ_1K to simulate lightweight SPI controller which can only transmit 1024 bytes.

The test step is the following:
- mtd erase /dev/mtd2
- dd if=/dev/zero bs=2048 count=1 | tr '\0' '\xA5' > output.bin
- mtd write output.bin /dev/mtd2

Before applying this patch, write operation uses only 34H(update_cache):

Quoted 12 lines
[78.937720] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xa5
[78.945297] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xa5
[78.954251] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xa5
[78.962966] OP code: 0x10, addr val: 0x300
[78.968816] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[78.977233] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[78.985124] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[78.992527] OP code: 0x10, addr val: 0x301
[78.996981] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[79.004416] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[79.012031] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[79.019435] OP code: 0x10, addr val: 0x302

I am sorry but above you said that we should not perform:

     0x32, 0x32, 0x32...

because the second time it would clear the cache again. And here you tell us that actually the core already handles that by performing instead:

Quoted 7 lines
     0x34, 0x34, 0x34...

So what is the problem?

Or maybe I misunderstood the issue, but I think Chuanhong raised an issue that is already solved? Isn't it?

The issue is that the FORESEE NANDs require the first cache writing instruction to be WRITE_CACHE instead of UPDATE_CACHE. i.e. it needs a command sequence of:

    0x32, 0x34, 0x34, 0x34...

This patch does exactly that, making the first instruction issued 0x32. It should be applied to fix the issue above.

---
Regards,
Chuanhong Guo

maintainer
#3 lore raw

Hello,

Quoted 64 lines · Chuanhong Guo

Hello!

Quoted 52 lines · Miquel Raynal

Hello,

Quoted 35 lines · Sky Huang

From: Sky Huang <[email protected]>

According to discussion with Miquel Raynal <[email protected]> and Chuanhong Guo <[email protected]> on FORESEE F35SQA002G patch, Chuanhong recommmends that we can use the following sequence in spinand_write_to_cache_op():

x1 mode:
02H(program data load) -> 84H(random program data load) -> 84H ...

x4 mode:
32H(program data load x4) -> 34H(random program data load x4) -> 34H ...

02H or 32H commands will clear cache buffer on SPI-NAND and load data to it. For those SPI controllers which can't finish transmission in single step, 84H or 34H will be triggered for the rest data.

We observe that some current SPI-NANDs, including FORESEE F35SQA001G and F35SQA002G, must use 02H or 32H to reset cache buffer in flash before using 84H or 34H. Or users may encounter program failure issue. This issue is not always reproducible, but it may occur and cause system instability.

This sequence should work on all SPI-NANDs nowadays. I also check with Foresee that the sequence can solve the above program failure issue.

On my test platform (MT7988), SPI driver is drivers/spi/spi-mt65xx.c. And I limit MTK_SPI_IPM_PACKET_SIZE to SZ_1K to simulate lightweight SPI controller which can only transmit 1024 bytes.

The test step is the following:
- mtd erase /dev/mtd2
- dd if=/dev/zero bs=2048 count=1 | tr '\0' '\xA5' > output.bin
- mtd write output.bin /dev/mtd2

Before applying this patch, write operation uses only 34H(update_cache):

Quoted 12 lines
[78.937720] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xa5
[78.945297] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xa5
[78.954251] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xa5
[78.962966] OP code: 0x10, addr val: 0x300
[78.968816] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[78.977233] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[78.985124] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[78.992527] OP code: 0x10, addr val: 0x301
[78.996981] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[79.004416] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[79.012031] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[79.019435] OP code: 0x10, addr val: 0x302

I am sorry but above you said that we should not perform:

Quoted 9 lines
     0x32, 0x32, 0x32...
because the second time it would clear the cache again. And here
you tell us that actually the core already handles that by performing
instead:
     0x34, 0x34, 0x34...

So what is the problem?
Or maybe I misunderstood the issue, but I think Chuanhong raised an issue that is already solved? Isn't it?

Quoted 4 lines

The issue is that the FORESEE NANDs require the first cache writing instruction to be WRITE_CACHE instead of UPDATE_CACHE. i.e. it needs a command sequence of:

    0x32, 0x34, 0x34, 0x34...

So Foresee NANDs do not support update_cache, why are they advertised in the first place? Could you we have a less impacting solution for the other NANDs?

Quoted 2 lines

This patch does exactly that, making the first instruction issued 0x32. It should be applied to fix the issue above.

My understanding is that this is very specific to FORESEE NANDs and you are changing this for all NANDs. I have fears that it will break everywhere else.

Overall I understand the problem, but I disagree with the fix. Could you propose something less impacting as hinted above?

Thanks and sorry for the delay.
Miquèl

#4 lore raw

Hello, Miquel!

Quoted 78 lines · Miquel Raynal

Hello,

Quoted 64 lines · Chuanhong Guo

Hello!

Quoted 52 lines · Miquel Raynal

Hello,

Quoted 35 lines · Sky Huang

From: Sky Huang <[email protected]>

According to discussion with Miquel Raynal <[email protected]> and Chuanhong Guo <[email protected]> on FORESEE F35SQA002G patch, Chuanhong recommmends that we can use the following sequence in spinand_write_to_cache_op():

x1 mode:
02H(program data load) -> 84H(random program data load) -> 84H ...

x4 mode:
32H(program data load x4) -> 34H(random program data load x4) -> 34H ...

02H or 32H commands will clear cache buffer on SPI-NAND and load data to it. For those SPI controllers which can't finish transmission in single step, 84H or 34H will be triggered for the rest data.

We observe that some current SPI-NANDs, including FORESEE F35SQA001G and F35SQA002G, must use 02H or 32H to reset cache buffer in flash before using 84H or 34H. Or users may encounter program failure issue. This issue is not always reproducible, but it may occur and cause system instability.

This sequence should work on all SPI-NANDs nowadays. I also check with Foresee that the sequence can solve the above program failure issue.

On my test platform (MT7988), SPI driver is drivers/spi/spi-mt65xx.c. And I limit MTK_SPI_IPM_PACKET_SIZE to SZ_1K to simulate lightweight SPI controller which can only transmit 1024 bytes.

The test step is the following:
- mtd erase /dev/mtd2
- dd if=/dev/zero bs=2048 count=1 | tr '\0' '\xA5' > output.bin
- mtd write output.bin /dev/mtd2

Before applying this patch, write operation uses only 34H(update_cache):

Quoted 12 lines
[78.937720] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xa5
[78.945297] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xa5
[78.954251] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xa5
[78.962966] OP code: 0x10, addr val: 0x300
[78.968816] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[78.977233] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[78.985124] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[78.992527] OP code: 0x10, addr val: 0x301
[78.996981] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[79.004416] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[79.012031] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[79.019435] OP code: 0x10, addr val: 0x302

I am sorry but above you said that we should not perform:

Quoted 9 lines
     0x32, 0x32, 0x32...
because the second time it would clear the cache again. And here
you tell us that actually the core already handles that by performing
instead:
     0x34, 0x34, 0x34...

So what is the problem?
Or maybe I misunderstood the issue, but I think Chuanhong raised an issue that is already solved? Isn't it?

Quoted 4 lines

The issue is that the FORESEE NANDs require the first cache writing instruction to be WRITE_CACHE instead of UPDATE_CACHE. i.e. it needs a command sequence of:

    0x32, 0x34, 0x34, 0x34...

So Foresee NANDs do not support update_cache, why are they advertised in the first place? Could you we have a less impacting solution for the other NANDs?

Quoted 6 lines
Quoted 2 lines

This patch does exactly that, making the first instruction issued 0x32. It should be applied to fix the issue above.

My understanding is that this is very specific to FORESEE NANDs and you are changing this for all NANDs. I have fears that it will break everywhere else.

I just checked a few datasheets of SPI-NANDs from Toshiba/Winbond/Etron/ESMT/GigaDevice/Macronix.
All of them specify the programming sequence to be:
write_enable->write_cache->update_cache if needed-> program_execute->poll status.
Some of them mentions that the only difference between write_cache and update_cache is whether the page cache is cleared before writing (Winbond), while others don't specifically say that.

The original sequence doesn't seem to be following any manufacturers' instructions. We just haven't run into any problems until this FORESEE one.

Quoted 6 lines

Overall I understand the problem, but I disagree with the fix. Could you propose something less impacting as hinted above?

Thanks and sorry for the delay.
Miquèl

Signature · 2 lines
Regards,
Chuanhong Guo
maintainer
#5 lore raw

Hello Chuanhong,

Quoted 49 lines
Quoted 33 lines
Quoted 24 lines
Quoted 14 lines
Quoted 13 lines

Before applying this patch, write operation uses only 34H(update_cache):

[78.937720] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xa5
[78.945297] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xa5
[78.954251] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xa5
[78.962966] OP code: 0x10, addr val: 0x300
[78.968816] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[78.977233] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[78.985124] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[78.992527] OP code: 0x10, addr val: 0x301
[78.996981] OP code: 0x34, addr val: 0x0, data nbytes: 1020, data 1st byte: 0xff
[79.004416] OP code: 0x34, addr val: 0x3fc, data nbytes: 1020, data 1st byte: 0xff
[79.012031] OP code: 0x34, addr val: 0x7f8, data nbytes: 72, data 1st byte: 0xff
[79.019435] OP code: 0x10, addr val: 0x302

I am sorry but above you said that we should not perform:

Quoted 9 lines
     0x32, 0x32, 0x32...
because the second time it would clear the cache again. And here
you tell us that actually the core already handles that by performing
instead:
     0x34, 0x34, 0x34...

So what is the problem?
Or maybe I misunderstood the issue, but I think Chuanhong raised an issue that is already solved? Isn't it?

Quoted 4 lines

The issue is that the FORESEE NANDs require the first cache writing instruction to be WRITE_CACHE instead of UPDATE_CACHE. i.e. it needs a command sequence of:

    0x32, 0x34, 0x34, 0x34...

So Foresee NANDs do not support update_cache, why are they advertised in the first place? Could you we have a less impacting solution for the other NANDs?

Quoted 6 lines
Quoted 2 lines

This patch does exactly that, making the first instruction issued 0x32. It should be applied to fix the issue above.

My understanding is that this is very specific to FORESEE NANDs and you are changing this for all NANDs. I have fears that it will break everywhere else.

I just checked a few datasheets of SPI-NANDs from Toshiba/Winbond/Etron/ESMT/GigaDevice/Macronix.
All of them specify the programming sequence to be:
write_enable->write_cache->update_cache if needed-> program_execute->poll status.
Some of them mentions that the only difference between write_cache and update_cache is whether the page cache is cleared before writing (Winbond), while others don't specifically say that.

Quoted 3 lines

The original sequence doesn't seem to be following any manufacturers' instructions. We just haven't run into any problems until this FORESEE one.

That is what I am saying. It currently works, and we may encounter silent failures all around because of this change, even if it might be theoretically correct. Anyhow, we have these templates (both write and update) so let's try to re-use them.

However I dislike the current implementation which changes the global wdesc template and puts it back. Can you please propose something cleaner?

Thanks,
Miquèl