Skip to contentSkip to conversation

mtd: spinand: Handle the case where PROGRAM LOAD does not reset the cache

PATCH Applied Boris Brezillon 4 participants 8 messages Jan 24, 2019 – Jan 31, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: spinand: Handle the case where PROGRAM LOAD does not reset the cache

Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset the cache content to 0xFF (depends on vendor implementation), so we must fill the page cache entirely even if we only want to program the data portion of the page, otherwise we might corrupt the BBM or user data previously programmed in OOB area.

Fixes 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")
---
 drivers/mtd/nand/spi/core.c | 42 ++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 22 deletions(-)
drivers/mtd/nand/spi/core.c +20 -22
index 479c2f2cf17f..8bf37da19663 100644
@@ -304,24 +304,30 @@ static int spinand_write_to_cache_op(struct spinand_device *spinand,
 	struct nand_device *nand = spinand_to_nand(spinand);
 	struct mtd_info *mtd = nanddev_to_mtd(nand);
 	struct nand_page_io_req adjreq = *req;
-	unsigned int nbytes = 0;
-	void *buf = NULL;
+	void *buf = spinand->databuf;
+	unsigned int nbytes;
 	u16 column = 0;
 	int ret;
 
-	memset(spinand->databuf, 0xff,
-	       nanddev_page_size(nand) +
-	       nanddev_per_page_oobsize(nand));
+	/*
+	 * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset
+	 * the cache content to 0xFF (depends on vendor implementation), so we
+	 * must fill the page cache entirely even if we only want to program
+	 * the data portion of the page, otherwise we might corrupt the BBM or
+	 * user data previously programmed in OOB area.
+	 */
+	nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand);
+	memset(spinand->databuf, 0xff, nbytes);
+	adjreq.dataoffs = 0;
+	adjreq.datalen = nanddev_page_size(nand);
+	adjreq.databuf.out = spinand->databuf;
+	adjreq.ooblen = nanddev_per_page_oobsize(nand);
+	adjreq.ooboffs = 0;
+	adjreq.oobbuf.out = spinand->oobbuf;
 
-	if (req->datalen) {
+	if (req->datalen)
 		memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
 		       req->datalen);
-		adjreq.dataoffs = 0;
-		adjreq.datalen = nanddev_page_size(nand);
-		adjreq.databuf.out = spinand->databuf;
-		nbytes = adjreq.datalen;
-		buf = spinand->databuf;
-	}
 
 	if (req->ooblen) {
 		if (req->mode == MTD_OPS_AUTO_OOB)
@@ -332,14 +338,6 @@ static int spinand_write_to_cache_op(struct spinand_device *spinand,
 		else
 			memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
 			       req->ooblen);
-
-		adjreq.ooblen = nanddev_per_page_oobsize(nand);
-		adjreq.ooboffs = 0;
-		nbytes += nanddev_per_page_oobsize(nand);
-		if (!buf) {
-			buf = spinand->oobbuf;
-			column = nanddev_page_size(nand);
-		}
 	}
 
 	spinand_cache_op_adjust_colum(spinand, &adjreq, &column);
@@ -370,8 +368,8 @@ static int spinand_write_to_cache_op(struct spinand_device *spinand,
 
 		/*
 		 * We need to use the RANDOM LOAD CACHE operation if there's
-		 * more than one iteration, because the LOAD operation resets
-		 * the cache to 0xff.
+		 * more than one iteration, because the LOAD operation might
+		 * reset the cache to 0xff.
 		 */
 		if (nbytes) {
 			column = op.addr.val;
Signature · 1 line
2.17.1
Tested-by Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Boris Brezillon

Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset the cache content to 0xFF (depends on vendor implementation), so we must fill the page cache entirely even if we only want to program the data portion of the page, otherwise we might corrupt the BBM or user data previously programmed in OOB area.

Fixes 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")

Works fine (limited testing only yet), so:

Thanks,
Stefan

#2 lore raw

Hi,

Den tors 24 jan. 2019 kl 16:28 skrev Stefan Roese <[email protected]>:

Quoted 13 lines · Boris Brezillon
Quoted 10 lines

Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset the cache content to 0xFF (depends on vendor implementation), so we must fill the page cache entirely even if we only want to program the data portion of the page, otherwise we might corrupt the BBM or user data previously programmed in OOB area.

Fixes 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")
Quoted 8 lines

Works fine (limited testing only yet), so:

Thanks,
Stefan

Can this quirk be made vendor specific? It seems a waste of SPI transfer cycles to write 0xff to the whole OOB area when we only want to program the data area if the chip doesn't need this quirk. For which logic is this needed anyway? According to the GigaDevice datasheet, if a Program Load is followed by Program Execute, "uninitialized" bytes will be set to 0xff, which is the flow that is used by spinand core.

/Emil

#3 lore raw
Quoted 18 lines · On Thu, 24 Jan 2019 17:16:37 +0100 Emil Lenngren <[email protected]>…

Hi,

Den tors 24 jan. 2019 kl 16:28 skrev Stefan Roese <[email protected]>:

Quoted 15 lines · Boris Brezillon
Quoted 10 lines

Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset the cache content to 0xFF (depends on vendor implementation), so we must fill the page cache entirely even if we only want to program the data portion of the page, otherwise we might corrupt the BBM or user data previously programmed in OOB area.

Fixes 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")

Works fine (limited testing only yet), so:

Quoted 8 lines
Quoted 6 lines

Thanks,
Stefan

Can this quirk be made vendor specific?

We can make it vendor specific, as long as it's an opt-in thing. This way, the default behavior is the safest one, and only when we know a chip does reset the cache content on a PROGRAM LOAD time can we add this flag.

Quoted 7 lines

It seems a waste of SPI
transfer cycles to write 0xff to the whole OOB area when we only want to program the data area if the chip doesn't need this quirk. For which logic is this needed anyway? According to the GigaDevice datasheet, if a Program Load is followed by Program Execute, "uninitialized" bytes will be set to 0xff, which is the flow that is used by spinand core.

Except you're not guaranteed that the controller can fill the page in a single spi_mem_exec_op() call (because of FIFO size limitations), so you might have one PROGRAM LOAD followed by several PROGRAM RANDOM LOAD. I guess most manufacturers reset the cache content at PROGRAM LOAD time, but GigaDevice does not.

Quoted 2 lines

/Emil

maintainer
#4 lore raw

Hello,

Boris Brezillon <[email protected]> wrote on Thu, 24 Jan 2019 17:33:57 +0100:

Quoted 28 lines
Quoted 25 lines · On Thu, 24 Jan 2019 17:16:37 +0100 Emil Lenngren <[email protected]>…

Hi,

Den tors 24 jan. 2019 kl 16:28 skrev Stefan Roese <[email protected]>:

Quoted 21 lines · Boris Brezillon
Quoted 10 lines

Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset the cache content to 0xFF (depends on vendor implementation), so we must fill the page cache entirely even if we only want to program the data portion of the page, otherwise we might corrupt the BBM or user data previously programmed in OOB area.

Fixes 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")

Works fine (limited testing only yet), so:

Thanks,
Stefan

Quoted 6 lines
Quoted 1 line

Can this quirk be made vendor specific?

We can make it vendor specific, as long as it's an opt-in thing. This way, the default behavior is the safest one, and only when we know a chip does reset the cache content on a PROGRAM LOAD time can we add this flag.

I am fine with this approach.

Thanks,
Miquèl

#5 lore raw
Quoted 33 lines · On Fri, 25 Jan 2019 12:09:10 +0100 Miquel Raynal <[email protected]>…

Hello,

Boris Brezillon <[email protected]> wrote on Thu, 24 Jan 2019 17:33:57 +0100:

Quoted 28 lines
Quoted 25 lines · On Thu, 24 Jan 2019 17:16:37 +0100 Emil Lenngren <[email protected]>…

Hi,

Den tors 24 jan. 2019 kl 16:28 skrev Stefan Roese <[email protected]>:

Quoted 21 lines · Boris Brezillon
Quoted 10 lines

Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset the cache content to 0xFF (depends on vendor implementation), so we must fill the page cache entirely even if we only want to program the data portion of the page, otherwise we might corrupt the BBM or user data previously programmed in OOB area.

Fixes 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")

Works fine (limited testing only yet), so:

Thanks,
Stefan

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Can this quirk be made vendor specific?

We can make it vendor specific, as long as it's an opt-in thing. This way, the default behavior is the safest one, and only when we know a chip does reset the cache content on a PROGRAM LOAD time can we add this flag.

I am fine with this approach.

Does that stand for a Reviewed-by/Acked-by? To make it clear, I was saying that we should fix things first (with this fix) and only then optimize things for chips that actually reset the cache when PROGRAM LOAD is executed. I was not planning on sending a new version of this patch, unless you see good reasons to do so.

maintainer Acked-by
#6 lore raw

Hi Boris,

Boris Brezillon <[email protected]> wrote on Fri, 25 Jan 2019 17:08:51 +0100:

Quoted 43 lines
Quoted 40 lines · On Fri, 25 Jan 2019 12:09:10 +0100 Miquel Raynal <[email protected]>…

Hello,

Boris Brezillon <[email protected]> wrote on Thu, 24 Jan 2019 17:33:57 +0100:

Quoted 34 lines
Quoted 18 lines · On Thu, 24 Jan 2019 17:16:37 +0100 Emil Lenngren <[email protected]>…

Hi,

Den tors 24 jan. 2019 kl 16:28 skrev Stefan Roese <[email protected]>:

Quoted 15 lines · Boris Brezillon
Quoted 10 lines

Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset the cache content to 0xFF (depends on vendor implementation), so we must fill the page cache entirely even if we only want to program the data portion of the page, otherwise we might corrupt the BBM or user data previously programmed in OOB area.

Fixes 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")

Works fine (limited testing only yet), so:

Quoted 8 lines
Quoted 6 lines

Thanks,
Stefan

Can this quirk be made vendor specific?

We can make it vendor specific, as long as it's an opt-in thing. This way, the default behavior is the safest one, and only when we know a chip does reset the cache content on a PROGRAM LOAD time can we add this flag.

Quoted 7 lines
Quoted 1 line

I am fine with this approach.

Does that stand for a Reviewed-by/Acked-by? To make it clear, I was saying that we should fix things first (with this fix) and only then optimize things for chips that actually reset the cache when PROGRAM LOAD is executed. I was not planning on sending a new version of this patch, unless you see good reasons to do so.

I thought you would send a v2 but I am fine with this approach too.

Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author Applied
#7 lore raw
In reply to Boris Brezillon
Quoted 6 lines · Boris Brezillon

Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset the cache content to 0xFF (depends on vendor implementation), so we must fill the page cache entirely even if we only want to program the data portion of the page, otherwise we might corrupt the BBM or user data previously programmed in OOB area.

Quoted 6 lines
Fixes 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs")

Applied to http://git.infradead.org/linux-mtd.git mtd/fixes.

Boris