From patchwork Mon Jun 22 13:16:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 242753 List-Id: U-Boot discussion From: mikhail.kshevetskiy at oktetlabs.ru (Mikhail Kshevetskiy) Date: Mon, 22 Jun 2020 16:16:31 +0300 Subject: [PATCH 1/4] mtd: spinand: Stop using spinand->oobbuf for buffering bad block markers Message-ID: <20200622131634.2132717-1-mikhail.kshevetskiy@oktetlabs.ru> From: Frieder Schrempf For reading and writing the bad block markers, spinand->oobbuf is currently used as a buffer for the marker bytes. During the underlying read and write operations to actually get/set the content of the OOB area, the content of spinand->oobbuf is reused and changed by accessing it through spinand->oobbuf and/or spinand->databuf. This is a flaw in the original design of the SPI NAND core and at the latest from 13c15e07eedf ("mtd: spinand: Handle the case where PROGRAM LOAD does not reset the cache") on, it results in not having the bad block marker written at all, as the spinand->oobbuf is cleared to 0xff after setting the marker bytes to zero. To fix it, we now just store the two bytes for the marker on the stack and let the read/write operations copy it from/to the page buffer later. Fixes: 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs") Cc: stable at vger.kernel.org Signed-off-by: Frieder Schrempf Reviewed-by: Boris Brezillon Signed-off-by: Miquel Raynal Link: https://lore.kernel.org/linux-mtd/20200218100432.32433-2-frieder.schrempf at kontron.de --- drivers/mtd/nand/spi/core.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c index 93371fdde0..410ea2382d 100644 --- a/drivers/mtd/nand/spi/core.c +++ b/drivers/mtd/nand/spi/core.c @@ -655,16 +655,16 @@ static int spinand_mtd_write(struct mtd_info *mtd, loff_t to, static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos) { struct spinand_device *spinand = nand_to_spinand(nand); + u8 marker[2] = { }; struct nand_page_io_req req = { .pos = *pos, - .ooblen = 2, + .ooblen = sizeof(marker), .ooboffs = 0, - .oobbuf.in = spinand->oobbuf, + .oobbuf.in = marker, .mode = MTD_OPS_RAW, }; int ret; - memset(spinand->oobbuf, 0, 2); ret = spinand_select_target(spinand, pos->target); if (ret) return ret; @@ -673,7 +673,7 @@ static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos) if (ret) return ret; - if (spinand->oobbuf[0] != 0xff || spinand->oobbuf[1] != 0xff) + if (marker[0] != 0xff || marker[1] != 0xff) return true; return false; @@ -702,11 +702,12 @@ static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs) static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos) { struct spinand_device *spinand = nand_to_spinand(nand); + u8 marker[2] = { }; struct nand_page_io_req req = { .pos = *pos, .ooboffs = 0, - .ooblen = 2, - .oobbuf.out = spinand->oobbuf, + .ooblen = sizeof(marker), + .oobbuf.out = marker, }; int ret; @@ -723,7 +724,6 @@ static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos) if (ret) return ret; - memset(spinand->oobbuf, 0, 2); return spinand_write_page(spinand, &req); } From patchwork Mon Jun 22 13:16:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 242754 List-Id: U-Boot discussion From: mikhail.kshevetskiy at oktetlabs.ru (Mikhail Kshevetskiy) Date: Mon, 22 Jun 2020 16:16:32 +0300 Subject: [PATCH 2/4] mtd: spinand: Explicitly use MTD_OPS_RAW to write the bad block marker to OOB In-Reply-To: <20200622131634.2132717-1-mikhail.kshevetskiy@oktetlabs.ru> References: <20200622131634.2132717-1-mikhail.kshevetskiy@oktetlabs.ru> Message-ID: <20200622131634.2132717-2-mikhail.kshevetskiy@oktetlabs.ru> From: Frieder Schrempf When writing the bad block marker to the OOB area the access mode should be set to MTD_OPS_RAW as it is done for reading the marker. Currently this only works because req.mode is initialized to MTD_OPS_PLACE_OOB (0) and spinand_write_to_cache_op() checks for req.mode != MTD_OPS_AUTO_OOB. Fix this by explicitly setting req.mode to MTD_OPS_RAW. Fixes: 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs") Signed-off-by: Frieder Schrempf Reviewed-by: Boris Brezillon Signed-off-by: Miquel Raynal Link: https://lore.kernel.org/linux-mtd/20200218100432.32433-3-frieder.schrempf at kontron.de --- drivers/mtd/nand/spi/core.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c index 410ea2382d..c74a7b5ef3 100644 --- a/drivers/mtd/nand/spi/core.c +++ b/drivers/mtd/nand/spi/core.c @@ -708,6 +708,7 @@ static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos) .ooboffs = 0, .ooblen = sizeof(marker), .oobbuf.out = marker, + .mode = MTD_OPS_RAW, }; int ret; From patchwork Mon Jun 22 13:16:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 242756 List-Id: U-Boot discussion From: mikhail.kshevetskiy at oktetlabs.ru (Mikhail Kshevetskiy) Date: Mon, 22 Jun 2020 16:16:33 +0300 Subject: [PATCH 3/4] mtd: spinand: Do not erase the block before writing a bad block marker In-Reply-To: <20200622131634.2132717-1-mikhail.kshevetskiy@oktetlabs.ru> References: <20200622131634.2132717-1-mikhail.kshevetskiy@oktetlabs.ru> Message-ID: <20200622131634.2132717-3-mikhail.kshevetskiy@oktetlabs.ru> From: Frieder Schrempf Currently when marking a block, we use spinand_erase_op() to erase the block before writing the marker to the OOB area. Doing so without waiting for the operation to finish can lead to the marking failing silently and no bad block marker being written to the flash. In fact we don't need to do an erase at all before writing the BBM. The ECC is disabled for raw accesses to the OOB data and we don't need to work around any issues with chips reporting ECC errors as it is known to be the case for raw NAND. Fixes: 7529df465248 ("mtd: nand: Add core infrastructure to support SPI NANDs") Cc: stable at vger.kernel.org Signed-off-by: Frieder Schrempf Reviewed-by: Boris Brezillon Signed-off-by: Miquel Raynal Link: https://lore.kernel.org/linux-mtd/20200218100432.32433-4-frieder.schrempf at kontron.de --- drivers/mtd/nand/spi/core.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c index c74a7b5ef3..e0ebd9c04b 100644 --- a/drivers/mtd/nand/spi/core.c +++ b/drivers/mtd/nand/spi/core.c @@ -712,19 +712,10 @@ static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos) }; int ret; - /* Erase block before marking it bad. */ ret = spinand_select_target(spinand, pos->target); if (ret) return ret; - ret = spinand_write_enable_op(spinand); - if (ret) - return ret; - - ret = spinand_erase_op(spinand, pos); - if (ret) - return ret; - return spinand_write_page(spinand, &req); } From patchwork Mon Jun 22 13:16:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikhail Kshevetskiy X-Patchwork-Id: 242755 List-Id: U-Boot discussion From: mikhail.kshevetskiy at oktetlabs.ru (Mikhail Kshevetskiy) Date: Mon, 22 Jun 2020 16:16:34 +0300 Subject: [PATCH 4/4] mtd: spinand: enable erasing of bad mtd blocks In-Reply-To: <20200622131634.2132717-1-mikhail.kshevetskiy@oktetlabs.ru> References: <20200622131634.2132717-1-mikhail.kshevetskiy@oktetlabs.ru> Message-ID: <20200622131634.2132717-4-mikhail.kshevetskiy@oktetlabs.ru> U-Boot is able to erase bad mtd blocks on raw nand devices, but this is not true for spinand flashes. Lets enable this feature for spinand flashes as well. This is extemelly useful for flash testing. Signed-off-by: Mikhail Kshevetskiy --- drivers/mtd/nand/core.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/nand/core.c b/drivers/mtd/nand/core.c index 6fbd24ba74..a39b4c4b30 100644 --- a/drivers/mtd/nand/core.c +++ b/drivers/mtd/nand/core.c @@ -130,10 +130,18 @@ EXPORT_SYMBOL_GPL(nanddev_isreserved); */ int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos) { + unsigned int entry; + if (nanddev_isbad(nand, pos) || nanddev_isreserved(nand, pos)) { pr_warn("attempt to erase a bad/reserved block @%llx\n", nanddev_pos_to_offs(nand, pos)); - return -EIO; + if (nanddev_isreserved(nand, pos)) + return -EIO; + + /* remove bad block from BBT */ + entry = nanddev_bbt_pos_to_entry(nand, pos); + nanddev_bbt_set_block_status(nand, entry, + NAND_BBT_BLOCK_STATUS_UNKNOWN); } return nand->ops->erase(nand, pos);