From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH v2 2/4] mtd: nand: mxc_nand: Cleanup code creating bad block table
Date: Mon, 6 May 2024 16:46:09 +0200 [thread overview]
Message-ID: <20240506144606.1562643-8-u.kleine-koenig@pengutronix.de> (raw)
In-Reply-To: <20240506144606.1562643-6-u.kleine-koenig@pengutronix.de>
Let the variable "numblocks" contain the number of blocks of the NAND
device (and not twice this value). Also the loop variable counts blocks
now instead of the doubled value. Further calculate the offset of the
i-th block from i instead of incrementing in each step and pick a more
sensbile variable name for it.
Also checkbad() only makes use of *mtd which is already available in the
caller, so pass that one directly. Additionally initialize the struct
mtd_oob_ops in the declarator (which has the nice side effect of zeroing
the members not mentioned) and improve code comments.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
drivers/mtd/nand/raw/mxc_nand.c | 41 ++++++++++++++++-----------------
1 file changed, 20 insertions(+), 21 deletions(-)
diff --git a/drivers/mtd/nand/raw/mxc_nand.c b/drivers/mtd/nand/raw/mxc_nand.c
index f7ef2e4374a9..8e564fa76a33 100644
--- a/drivers/mtd/nand/raw/mxc_nand.c
+++ b/drivers/mtd/nand/raw/mxc_nand.c
@@ -1555,19 +1555,18 @@ static const struct nand_controller_ops mxcnd_controller_ops = {
* From this point on we can forget about the BBMs and rely completely
* on the flash BBT.
*/
-static int checkbad(struct nand_chip *chip, loff_t ofs)
+static int checkbad(struct mtd_info *mtd, loff_t ofs)
{
- struct mtd_info *mtd = nand_to_mtd(chip);
int ret;
uint8_t buf[mtd->writesize + mtd->oobsize];
- struct mtd_oob_ops ops;
-
- ops.mode = MTD_OPS_RAW;
- ops.ooboffs = 0;
- ops.datbuf = buf;
- ops.len = mtd->writesize;
- ops.oobbuf = buf + mtd->writesize;
- ops.ooblen = mtd->oobsize;
+ struct mtd_oob_ops ops = {
+ .mode = MTD_OPS_RAW,
+ .ooboffs = 0,
+ .datbuf = buf,
+ .len = mtd->writesize,
+ .oobbuf = buf + mtd->writesize,
+ .ooblen = mtd->oobsize,
+ };
ret = mtd_read_oob(mtd, ofs, &ops);
if (ret < 0)
@@ -1585,31 +1584,31 @@ static int imxnd_create_bbt(struct nand_chip *chip)
{
struct mtd_info *mtd = nand_to_mtd(chip);
int len, i, numblocks, ret;
- loff_t from = 0;
uint8_t *bbt;
- len = mtd->size >> (chip->bbt_erase_shift + 2);
+ numblocks = mtd->size >> chip->bbt_erase_shift;
- /* Allocate memory (2bit per block) and clear the memory bad block table */
+ /*
+ * Allocate memory (2bit per block = 1 byte per 4 blocks) and clear the
+ * memory bad block table
+ */
+ len = (numblocks + 3) >> 2;
bbt = kzalloc(len, GFP_KERNEL);
if (!bbt)
return -ENOMEM;
- numblocks = mtd->size >> (chip->bbt_erase_shift - 1);
+ for (i = 0; i < numblocks; ++i) {
+ loff_t ofs = i << chip->bbt_erase_shift;
- for (i = 0; i < numblocks;) {
- ret = checkbad(chip, from);
+ ret = checkbad(mtd, ofs);
if (ret < 0)
goto out;
if (ret) {
- bbt[i >> 3] |= 0x03 << (i & 0x6);
+ bbt[i >> 2] |= 0x03 << (2 * (i & 0x3));
dev_info(mtd->dev.parent, "Bad eraseblock %d at 0x%08x\n",
- i >> 1, (unsigned int)from);
+ i, (unsigned int)ofs);
}
-
- i += 2;
- from += (1 << chip->bbt_erase_shift);
}
chip->bbt_td->options |= NAND_BBT_CREATE;
--
2.43.0
next prev parent reply other threads:[~2024-05-06 14:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-06 14:46 [PATCH v2 0/4] mtd: nand: mxc_nand: Only automatically create BBT if NAND seems to be pristine Uwe Kleine-König
2024-05-06 14:46 ` [PATCH v2 1/4] mtd: nand: mxc_nand: Improve comment about vendor BBM and address verschwurbelung Uwe Kleine-König
2024-05-06 14:46 ` Uwe Kleine-König [this message]
2024-05-06 14:46 ` [PATCH v2 3/4] mtd: nand: mxc_nand: Add error message if BBT creation fails Uwe Kleine-König
2024-05-06 14:46 ` [PATCH v2 4/4] mtd: nand: mxc_nand: Only automatically create BBT if NAND seems to be pristine Uwe Kleine-König
2024-05-07 7:31 ` [PATCH v2 0/4] " Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240506144606.1562643-8-u.kleine-koenig@pengutronix.de \
--to=u.kleine-koenig@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox