Skip to contentSkip to conversation

nand bad block erase and i.MX bbt tool

PATCH New Sascha Hauer 3 participants 16 messages Mar 4, 2013 – Mar 5, 2013

Subsystems (5)
lore
Share & tools
lore

Sascha Hauer

author maintainer
#0 lore raw

PATCH 1/1 nand bad block erase and i.MX bbt tool

This series contains some NAND related updates.

First an option is introduced to allow erasing bad blocks. While this is a dangerous operation it is needed during development or when some foreign flash driver has touched the NAND. The option is hidden behind a Kconfig option and must be enabled explicitly with a device parameter.

Second the BBT (bad block table) type is exposed via a device parameter. This is useful to get information about the bbt.

Last not least the i.MX NAND driver no longer silently creates a BBT on 2k NAND flashes. This does not work properly because the Controller uses a non standard NAND layout and will not detect the factory bad block markers properly on a virgin NAND flash. We work around this by adding a command which creates a BBT on demand using the correct information. Whether this command needs to be executed can be detected with the bbt device parameter.

Sascha

The following changes since commit 94e71b843f6456abacc2fe76a5c375a461fabdf7:

libubi: Use global mtd_all_ff function (2013-03-04 10:54:46 +0100)

are available in the git repository at:

none

for you to fetch changes up to c3afd8676888d93d831bd4a8e71f1560ee5616c4:

mtd: nand: Add command to generate a flash BBT (2013-03-04 11:06:23 +0100)

----------------------------------------------------------------
Sascha Hauer (10):
mtd: nand: register nand flashes with nand specific function
mtd: Add parameter to allow erasing bad blocks
mtd nand: introduce bbm.h
nand command: use loff_t for block offset
nand command: use enumeration for command instead of bitmask nand command: check for <dev> directly after option parsing
mtd: nand: replace NAND_USE_FLASH_BBT with NAND_BBT_USE_FLASH mtd nand i.MX: remove unused code
mtd: nand: Add bbt parameter
mtd: nand: Add command to generate a flash BBT

 commands/nand.c                   |   48 ++++----
 drivers/mtd/core.c                |    6 +-
 drivers/mtd/mtdraw.c              |    6 +-
 drivers/mtd/nand/Kconfig          |   14 +++
 drivers/mtd/nand/Makefile         |    1 +
 drivers/mtd/nand/atmel_nand.c     |    4 +-
 drivers/mtd/nand/nand_base.c      |   50 +++++++++
 drivers/mtd/nand/nand_bbt.c       |    4 +-
 drivers/mtd/nand/nand_imx.c       |   30 +++--
 drivers/mtd/nand/nand_imx_bbm.c   |  220 +++++++++++++++++++++++++++++++++++++
 drivers/mtd/nand/nand_mxs.c       |    2 +-
 drivers/mtd/nand/nand_omap_gpmc.c |    2 +-
 drivers/mtd/nand/nand_s3c24xx.c   |    4 +-
 drivers/mtd/nand/nand_write.c     |    5 +-
 drivers/mtd/nand/nomadik_nand.c   |    2 +-
 include/linux/mtd/bbm.h           |   92 ++++++++++++++++
 include/linux/mtd/mtd.h           |    3 +
 include/linux/mtd/nand.h          |   86 ++-------------
 18 files changed, 456 insertions(+), 123 deletions(-)
 create mode 100644 drivers/mtd/nand/nand_imx_bbm.c
 create mode 100644 include/linux/mtd/bbm.h

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

author maintainer
#1 lore raw

PATCH 1/10 mtd: nand: register nand flashes with nand specific function

This allows us to have some NAND specific stuff during registration, like for example adding NAND specific device parameters.

---
 drivers/mtd/nand/atmel_nand.c     |    2 +-
 drivers/mtd/nand/nand_base.c      |   11 +++++++++++
 drivers/mtd/nand/nand_imx.c       |    2 +-
 drivers/mtd/nand/nand_mxs.c       |    2 +-
 drivers/mtd/nand/nand_omap_gpmc.c |    2 +-
 drivers/mtd/nand/nand_s3c24xx.c   |    2 +-
 drivers/mtd/nand/nomadik_nand.c   |    2 +-
 include/linux/mtd/nand.h          |    2 +-
 8 files changed, 18 insertions(+), 7 deletions(-)
drivers/mtd/nand/atmel_nand.c +1 -1
index 63484f8..81ccad9 100644
@@ -1223,7 +1223,7 @@ static int __init atmel_nand_probe(struct device_d *dev)
 		goto err_scan_tail;
 	}
 
-	add_mtd_device(mtd, "nand");
+	add_mtd_nand_device(mtd, "nand");
 
 	if (!res)
 		return res;
drivers/mtd/nand/nand_base.c +11 -0
index 411aba7..c0345c7 100644
@@ -1672,4 +1672,15 @@ EXPORT_SYMBOL(nand_scan_ident);
 EXPORT_SYMBOL(nand_scan_tail);
 EXPORT_SYMBOL(nand_release);
 
+int add_mtd_nand_device(struct mtd_info *mtd, char *devname)
+{
+	int ret;
+
+	ret = add_mtd_device(mtd, devname);
+	if (ret)
+		return ret;
+
+	return ret;
+}
+
 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
drivers/mtd/nand/nand_imx.c +1 -1
index dd66861..842c1de 100644
@@ -1260,7 +1260,7 @@ static int __init imxnd_probe(struct device_d *dev)
 		goto escan;
 	}
 
-	add_mtd_device(mtd, "nand");
+	add_mtd_nand_device(mtd, "nand");
 
 	dev->priv = host;
 
drivers/mtd/nand/nand_mxs.c +1 -1
index 3812ac9..dd43a95 100644
@@ -1252,7 +1252,7 @@ static int mxs_nand_probe(struct device_d *dev)
 	if (err)
 		goto err2;
 
-	return add_mtd_device(mtd, "nand");
+	return add_mtd_nand_device(mtd, "nand");
 err2:
 	free(nand_info->data_buf);
 	free(nand_info->cmd_buf);
drivers/mtd/nand/nand_omap_gpmc.c +1 -1
index 7849db5..d448251 100644
@@ -1060,7 +1060,7 @@ static int gpmc_nand_probe(struct device_d *pdev)
 		omap_gpmc_eccmode(oinfo, pdata->ecc_mode);
 
 	/* We are all set to register with the system now! */
-	err = add_mtd_device(minfo, "nand");
+	err = add_mtd_nand_device(minfo, "nand");
 	if (err) {
 		dev_dbg(pdev, "device registration failed\n");
 		goto out_release_mem;
drivers/mtd/nand/nand_s3c24xx.c +1 -1
index fef9432..c68c9fb 100644
@@ -483,7 +483,7 @@ static int s3c24x0_nand_probe(struct device_d *dev)
 		goto on_error;
 	}
 
-	return add_mtd_device(mtd, "nand");
+	return add_mtd_nand_device(mtd, "nand");
 
 on_error:
 	free(host);
drivers/mtd/nand/nomadik_nand.c +1 -1
index 834b6ac..afdbef1 100644
@@ -221,7 +221,7 @@ static int nomadik_nand_probe(struct device_d *dev)
 	}
 
 	pr_info("Registering %s as whole device\n", mtd->name);
-	add_mtd_device(mtd, "nand");
+	add_mtd_nand_device(mtd, "nand");
 
 	return 0;
 
include/linux/mtd/nand.h +1 -1
index d2f8648..b111b66 100644
@@ -618,7 +618,7 @@ extern int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
 			   int allowbbt);
 extern int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
 			size_t * retlen, uint8_t * buf);
-
+extern int add_mtd_nand_device(struct mtd_info *mtd, char *devname);
 /*
 * Constants for oob configuration
 */
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author maintainer
#2 lore raw

PATCH 2/10 mtd: Add parameter to allow erasing bad blocks

In reply to Sascha Hauer

While erasing bad blocks is a potentially dangerous operation it is sometimes needed during development or when some foreign code has touched the flash.

This patch adds a device parameter 'erasebad' to allow erasing bad blocks. Since this is not wanted during production this is behind a Kconfig option.

---
 drivers/mtd/core.c            |    6 +++++-
 drivers/mtd/mtdraw.c          |    6 +++++-
 drivers/mtd/nand/Kconfig      |    9 +++++++++
 drivers/mtd/nand/nand_base.c  |   20 ++++++++++++++++++++
 drivers/mtd/nand/nand_write.c |    3 ++-
 include/linux/mtd/mtd.h       |    3 +++
 6 files changed, 44 insertions(+), 3 deletions(-)
drivers/mtd/core.c +5 -1
index b6c1d01..e28d925 100644
@@ -112,7 +112,11 @@ static int mtd_op_erase(struct cdev *cdev, size_t count, loff_t offset)
 	while (count > 0) {
 		dev_dbg(cdev->dev, "erase %d %d\n", erase.addr, erase.len);
 
-		ret = mtd_block_isbad(mtd, erase.addr);
+		if (!mtd->allow_erasebad)
+			ret = mtd_block_isbad(mtd, erase.addr);
+		else
+			ret = 0;
+
 		if (ret > 0) {
 			printf("Skipping bad block at 0x%08x\n", erase.addr);
 		} else {
drivers/mtd/mtdraw.c +5 -1
index 5aaa017..384104e 100644
@@ -241,7 +241,11 @@ static int mtdraw_erase(struct cdev *cdev, size_t count, loff_t _offset)
 	while (count > 0) {
 		debug("erase %d %d\n", erase.addr, erase.len);
 
-		ret = mtd_block_isbad(mtd, erase.addr);
+		if (!mtd->allow_erasebad)
+			ret = mtd_block_isbad(mtd, erase.addr);
+		else
+			ret = 0;
+
 		if (ret > 0) {
 			printf("Skipping bad block at 0x%08x\n", erase.addr);
 		} else {
drivers/mtd/nand/Kconfig +9 -0
index 49daabf..1a9b929 100644
@@ -48,6 +48,15 @@ config NAND_BBT
 	  Say y here to include support for bad block tables. This speeds
 	  up the process of checking for bad blocks
 
+config NAND_ALLOW_ERASE_BAD
+	bool
+	depends on MTD_WRITE
+	prompt "Add device parameter to allow erasing bad blocks"
+	help
+	  This adds a 'erasebad' device parameter to nand devices. When set
+	  to '1' it will be allowed to erase bad blocks. This is a potientially
+	  dangerous operation, so if unsure say no to this option.
+
 config NAND_IMX
 	bool
 	prompt "i.MX NAND driver"
drivers/mtd/nand/nand_base.c +20 -0
index c0345c7..ac223ca 100644
@@ -1672,6 +1672,22 @@ EXPORT_SYMBOL(nand_scan_ident);
 EXPORT_SYMBOL(nand_scan_tail);
 EXPORT_SYMBOL(nand_release);
 
+static int mtd_set_erasebad(struct device_d *dev, struct param_d *param,
+		const char *val)
+{
+	struct mtd_info *mtd = container_of(dev, struct mtd_info, class_dev);
+	int erasebad;
+
+	erasebad = simple_strtoul(val, NULL, 0);
+
+	if (erasebad && !mtd->allow_erasebad)
+		dev_warn(dev, "Allowing to erase bad blocks. This may be dangerous!\n");
+
+	mtd->allow_erasebad = erasebad ? true : false;
+
+	return 0;
+}
+
 int add_mtd_nand_device(struct mtd_info *mtd, char *devname)
 {
 	int ret;
@@ -1680,6 +1696,10 @@ int add_mtd_nand_device(struct mtd_info *mtd, char *devname)
 	if (ret)
 		return ret;
 
+	if (IS_ENABLED(CONFIG_NAND_ALLOW_ERASE_BAD))
+		dev_add_param(&mtd->class_dev, "erasebad", mtd_set_erasebad,
+				NULL, 0);
+
 	return ret;
 }
 
drivers/mtd/nand/nand_write.c +2 -1
index eea5113..11005f3 100644
@@ -626,7 +626,8 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
 		/*
 		 * heck if we have a bad block, we do not erase bad blocks !
 		 */
-		if (nand_block_checkbad(mtd, ((loff_t) page) <<
+		if (!mtd->allow_erasebad &&
+				nand_block_checkbad(mtd, ((loff_t) page) <<
 					chip->page_shift, 0, allowbbt)) {
 			pr_warn("nand_erase: attempt to erase a "
 			       "bad block at page 0x%08x\n", page);
include/linux/mtd/mtd.h +3 -0
index e556837..390f4b0 100644
@@ -205,6 +205,9 @@ struct mtd_info {
 
 	struct param_d param_size;
 	char *size_str;
+
+	/* If true erasing bad blocks is allowed, this is set via a device parameter */
+	bool allow_erasebad;
 };
 
 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author maintainer
#3 lore raw

PATCH 3/10 mtd nand: introduce bbm.h

In reply to Sascha Hauer

This file is present in current kernels. In order to get closer to current mtd support introduce it for barebox aswell.

---
 include/linux/mtd/bbm.h  |   86 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mtd/nand.h |   77 +++--------------------------------------
 2 files changed, 91 insertions(+), 72 deletions(-)
 create mode 100644 include/linux/mtd/bbm.h
include/linux/mtd/bbm.h new file +86 -0
new file mode 100644
index 0000000..503ec6d
@@ -0,0 +1,86 @@
+/*
+ *  linux/include/linux/mtd/bbm.h
+ *
+ *  NAND family Bad Block Management (BBM) header file
+ *    - Bad Block Table (BBT) implementation
+ *
+ *  Copyright (c) 2005 Samsung Electronics
+ *  Kyungmin Park <[email protected]>
+ *
+ *  Copyright (c) 2000-2005
+ *  Thomas Gleixner <[email protected]>
+ *
+ */
+#ifndef __LINUX_MTD_BBM_H
+#define __LINUX_MTD_BBM_H
+
+/**
+ * struct nand_bbt_descr - bad block table descriptor
+ * @options:	options for this descriptor
+ * @pages:	the page(s) where we find the bbt, used with option BBT_ABSPAGE
+ *		when bbt is searched, then we store the found bbts pages here.
+ *		Its an array and supports up to 8 chips now
+ * @offs:	offset of the pattern in the oob area of the page
+ * @veroffs:	offset of the bbt version counter in the oob are of the page
+ * @version:	version read from the bbt page during scan
+ * @len:	length of the pattern, if 0 no pattern check is performed
+ * @maxblocks:	maximum number of blocks to search for a bbt. This number of
+ *		blocks is reserved at the end of the device where the tables are
+ *		written.
+ * @reserved_block_code: if non-0, this pattern denotes a reserved (rather than
+ *              bad) block in the stored bbt
+ * @pattern:	pattern to identify bad block table or factory marked good /
+ *		bad blocks, can be NULL, if len = 0
+ *
+ * Descriptor for the bad block table marker and the descriptor for the
+ * pattern which identifies good and bad blocks. The assumption is made
+ * that the pattern and the version count are always located in the oob area
+ * of the first block.
+ */
+struct nand_bbt_descr {
+	int	options;
+	int	pages[NAND_MAX_CHIPS];
+	int	offs;
+	int	veroffs;
+	uint8_t	version[NAND_MAX_CHIPS];
+	int	len;
+	int	maxblocks;
+	int	reserved_block_code;
+	uint8_t	*pattern;
+};
+
+/* Options for the bad block table descriptors */
+
+/* The number of bits used per block in the bbt on the device */
+#define NAND_BBT_NRBITS_MSK	0x0000000F
+#define NAND_BBT_1BIT		0x00000001
+#define NAND_BBT_2BIT		0x00000002
+#define NAND_BBT_4BIT		0x00000004
+#define NAND_BBT_8BIT		0x00000008
+/* The bad block table is in the last good block of the device */
+#define	NAND_BBT_LASTBLOCK	0x00000010
+/* The bbt is at the given page, else we must scan for the bbt */
+#define NAND_BBT_ABSPAGE	0x00000020
+/* The bbt is at the given page, else we must scan for the bbt */
+#define NAND_BBT_SEARCH		0x00000040
+/* bbt is stored per chip on multichip devices */
+#define NAND_BBT_PERCHIP	0x00000080
+/* bbt has a version counter at offset veroffs */
+#define NAND_BBT_VERSION	0x00000100
+/* Create a bbt if none axists */
+#define NAND_BBT_CREATE		0x00000200
+/* Search good / bad pattern through all pages of a block */
+#define NAND_BBT_SCANALLPAGES	0x00000400
+/* Scan block empty during good / bad block scan */
+#define NAND_BBT_SCANEMPTY	0x00000800
+/* Write bbt if neccecary */
+#define NAND_BBT_WRITE		0x00001000
+/* Read and write back block contents when writing bbt */
+#define NAND_BBT_SAVECONTENT	0x00002000
+/* Search good / bad pattern on the first and the second page */
+#define NAND_BBT_SCAN2NDPAGE	0x00004000
+
+/* The maximum number of blocks to scan for a bbt */
+#define NAND_BBT_SCAN_MAXBLOCKS	4
+
+#endif /* __LINUX_MTD_BBM_H */
include/linux/mtd/nand.h +5 -72

95 lines not on this page · raw message

Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author maintainer
#4 lore raw

PATCH 4/10 nand command: use loff_t for block offset

In reply to Sascha Hauer
---
 commands/nand.c |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
commands/nand.c +6 -5
index f000142..d00845f 100644
@@ -36,7 +36,8 @@
 static int do_nand(int argc, char *argv[])
 {
 	int opt;
-	int command = 0, badblock = 0;
+	int command = 0;
+	loff_t badblock = 0;
 
 	while((opt = getopt(argc, argv, "adb:")) > 0) {
 		if (command) {
@@ -53,7 +54,7 @@ static int do_nand(int argc, char *argv[])
 			break;
 		case 'b':
 			command = NAND_MARKBAD;
-			badblock = simple_strtoul(optarg, NULL, 0);
+			badblock = strtoull_suffix(optarg, NULL, 0);
 		}
 	}
 
@@ -76,9 +77,9 @@ static int do_nand(int argc, char *argv[])
 	if (command & NAND_MARKBAD) {
 		if (optind < argc) {
 			int ret = 0, fd;
-			loff_t __badblock = badblock;
 
-			printf("marking block at 0x%08x on %s as bad\n", badblock, argv[optind]);
+			printf("marking block at 0x%08llx on %s as bad\n",
+					badblock, argv[optind]);
 
 			fd = open(argv[optind], O_RDWR);
 			if (fd < 0) {
@@ -86,7 +87,7 @@ static int do_nand(int argc, char *argv[])
 				return 1;
 			}
 
-			ret = ioctl(fd, MEMSETBADBLOCK, &__badblock);
+			ret = ioctl(fd, MEMSETBADBLOCK, &badblock);
 			if (ret)
 				perror("ioctl");
 
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author maintainer
#5 lore raw

PATCH 5/10 nand command: use enumeration for command instead of bitmask

In reply to Sascha Hauer

A command can only be one of NAND_*, so use an enumeration instead of a bitmask.

---
 commands/nand.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
commands/nand.c +6 -6
index d00845f..79f6735 100644
@@ -29,9 +29,9 @@
 #include <fcntl.h>
 #include <libgen.h>
 
-#define NAND_ADD (1 << 0)
-#define NAND_DEL (1 << 1)
-#define NAND_MARKBAD (1 << 2)
+#define NAND_ADD	1
+#define NAND_DEL	2
+#define NAND_MARKBAD	3
 
 static int do_nand(int argc, char *argv[])
 {
@@ -58,7 +58,7 @@ static int do_nand(int argc, char *argv[])
 		}
 	}
 
-	if (command & NAND_ADD) {
+	if (command == NAND_ADD) {
 		while (optind < argc) {
 			if (dev_add_bb_dev(basename(argv[optind]), NULL))
 				return 1;
@@ -67,14 +67,14 @@ static int do_nand(int argc, char *argv[])
 		}
 	}
 
-	if (command & NAND_DEL) {
+	if (command == NAND_DEL) {
 		while (optind < argc) {
 			dev_remove_bb_dev(basename(argv[optind]));
 			optind++;
 		}
 	}
 
-	if (command & NAND_MARKBAD) {
+	if (command == NAND_MARKBAD) {
 		if (optind < argc) {
 			int ret = 0, fd;
 
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#6 lore raw

Re: [PATCH 05/10] nand command: use enumeration for command instead of bitmask

Hi,

Quoted 26 lines · Sascha Hauer

A command can only be one of NAND_*, so use an enumeration instead of a bitmask.

---
 commands/nand.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
commands/nand.c +3 -3
index d00845f..79f6735 100644
@@ -29,9 +29,9 @@
 #include <fcntl.h>
 #include <libgen.h>
 
-#define NAND_ADD (1 << 0)
-#define NAND_DEL (1 << 1)
-#define NAND_MARKBAD (1 << 2)
+#define NAND_ADD	1
+#define NAND_DEL	2
+#define NAND_MARKBAD	3
 
 static int do_nand(int argc, char *argv[])
 {

commands/nand.c static int do_nand(int argc, char *argv[]) · line 61

Quoted 5 lines
commands/nand.c +1 -1
@@ -58,7 +58,7 @@ static int do_nand(int argc, char *argv[])
 		}
 	}
 
-	if (command & NAND_ADD) {
+	if (command == NAND_ADD) {

some nitpick. Can we use switch case instead of if? With a default branch when command == 0 to return COMMAND_ERROR_USAGE?

Quoted 28 lines
commands/nand.c +2 -2
 		while (optind < argc) {
 			if (dev_add_bb_dev(basename(argv[optind]), NULL))
 				return 1;
@@ -67,14 +67,14 @@ static int do_nand(int argc, char *argv[])
 		}
 	}
 
-	if (command & NAND_DEL) {
+	if (command == NAND_DEL) {
 		while (optind < argc) {
 			dev_remove_bb_dev(basename(argv[optind]));
 			optind++;
 		}
 	}
 
-	if (command & NAND_MARKBAD) {
+	if (command == NAND_MARKBAD) {
 		if (optind < argc) {
 			int ret = 0, fd;
 
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

author maintainer
#7 lore raw

PATCH 6/10 nand command: check for <dev> directly after option parsing

In reply to Sascha Hauer

Allo subcommands need at least one nonopt arg, so check for it right after parsing the options and drop the check in the MARKBAD command.

---
 commands/nand.c |   31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
commands/nand.c +16 -15
index 79f6735..39fb64e 100644
@@ -58,6 +58,9 @@ static int do_nand(int argc, char *argv[])
 		}
 	}
 
+	if (optind >= argc)
+		return COMMAND_ERROR_USAGE;
+
 	if (command == NAND_ADD) {
 		while (optind < argc) {
 			if (dev_add_bb_dev(basename(argv[optind]), NULL))
@@ -75,25 +78,23 @@ static int do_nand(int argc, char *argv[])
 	}
 
 	if (command == NAND_MARKBAD) {
-		if (optind < argc) {
-			int ret = 0, fd;
+		int ret = 0, fd;
 
-			printf("marking block at 0x%08llx on %s as bad\n",
-					badblock, argv[optind]);
+		printf("marking block at 0x%08llx on %s as bad\n",
+				badblock, argv[optind]);
 
-			fd = open(argv[optind], O_RDWR);
-			if (fd < 0) {
-				perror("open");
-				return 1;
-			}
+		fd = open(argv[optind], O_RDWR);
+		if (fd < 0) {
+			perror("open");
+			return 1;
+		}
 
-			ret = ioctl(fd, MEMSETBADBLOCK, &badblock);
-			if (ret)
-				perror("ioctl");
+		ret = ioctl(fd, MEMSETBADBLOCK, &badblock);
+		if (ret)
+			perror("ioctl");
 
-			close(fd);
-			return ret;
-		}
+		close(fd);
+		return ret;
 	}
 
 	return 0;
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author maintainer
#8 lore raw

PATCH 7/10 mtd: nand: replace NAND_USE_FLASH_BBT with NAND_BBT_USE_FLASH

In reply to Sascha Hauer

To sync with the kernel.

---
 drivers/mtd/nand/atmel_nand.c   |    2 +-
 drivers/mtd/nand/nand_bbt.c     |    4 ++--
 drivers/mtd/nand/nand_imx.c     |    2 +-
 drivers/mtd/nand/nand_s3c24xx.c |    2 +-
 drivers/mtd/nand/nand_write.c   |    2 +-
 include/linux/mtd/bbm.h         |    6 ++++++
 include/linux/mtd/nand.h        |    7 ++++---
 7 files changed, 16 insertions(+), 9 deletions(-)
drivers/mtd/nand/atmel_nand.c +1 -1
index 81ccad9..3eb78b7 100644
@@ -1196,7 +1196,7 @@ static int __init atmel_nand_probe(struct device_d *dev)
 
 	if (host->board->on_flash_bbt) {
 		printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
-		nand_chip->options |= NAND_USE_FLASH_BBT;
+		nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
 	}
 
 
drivers/mtd/nand/nand_bbt.c +2 -2
index 56396bf..ba51e0b 100644
@@ -1176,12 +1176,12 @@ int nand_default_bbt(struct mtd_info *mtd)
 			this->bbt_td = &bbt_main_descr;
 			this->bbt_md = &bbt_mirror_descr;
 		}
-		this->options |= NAND_USE_FLASH_BBT;
+		this->bbt_options |= NAND_BBT_USE_FLASH;
 		return nand_scan_bbt(mtd, &agand_flashbased);
 	}
 
 	/* Is a flash based bad block table requested ? */
-	if (this->options & NAND_USE_FLASH_BBT) {
+	if (this->bbt_options & NAND_BBT_USE_FLASH) {
 		/* Use the default pattern descriptors */
 		if (!this->bbt_td) {
 			this->bbt_td = &bbt_main_descr;
drivers/mtd/nand/nand_imx.c +1 -1
index 842c1de..d056c7c 100644
@@ -1227,7 +1227,7 @@ static int __init imxnd_probe(struct device_d *dev)
 		this->bbt_td = &bbt_main_descr;
 		this->bbt_md = &bbt_mirror_descr;
 		/* update flash based bbt */
-		this->options |= NAND_USE_FLASH_BBT;
+		this->bbt_options |= NAND_BBT_USE_FLASH;
 	}
 
 	/* first scan to find the device and get the page size */
drivers/mtd/nand/nand_s3c24xx.c +1 -1
index c68c9fb..a3f80cb 100644
@@ -469,7 +469,7 @@ static int s3c24x0_nand_probe(struct device_d *dev)
 
 	if (pdata->flash_bbt) {
 		/* use a flash based bbt */
-		chip->options |= NAND_USE_FLASH_BBT;
+		chip->bbt_options |= NAND_BBT_USE_FLASH;
 	}
 
 	ret = s3c24x0_nand_inithw(host);
drivers/mtd/nand/nand_write.c +1 -1
index 11005f3..c2699ab 100644
@@ -74,7 +74,7 @@ int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
 		chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
 
 	/* Do we have a flash based bad block table ? */
-	if (IS_ENABLED(CONFIG_NAND_BBT) && chip->options & NAND_USE_FLASH_BBT)
+	if (IS_ENABLED(CONFIG_NAND_BBT) && chip->bbt_options & NAND_BBT_USE_FLASH)
 		ret = nand_update_bbt(mtd, ofs);
 	else {
 		/* We write two bytes, so we dont have to mess with 16 bit
include/linux/mtd/bbm.h +6 -0
index 503ec6d..d546f40 100644
@@ -80,6 +80,12 @@ struct nand_bbt_descr {
 /* Search good / bad pattern on the first and the second page */
 #define NAND_BBT_SCAN2NDPAGE	0x00004000
 
+/*
+ * Use a flash based bad block table. By default, OOB identifier is saved in
+ * OOB area. This option is passed to the default bad block table function.
+ */
+#define NAND_BBT_USE_FLASH      0x00020000
+
 /* The maximum number of blocks to scan for a bbt */
 #define NAND_BBT_SCAN_MAXBLOCKS	4
 
include/linux/mtd/nand.h +4 -3
index d8331f4..dc141a5 100644
@@ -189,9 +189,6 @@ typedef enum {
 #define NAND_CHIPOPTIONS_MSK	(0x0000ffff & ~NAND_NO_AUTOINCR)
 
 /* Non chip related options */
-/* Use a flash based bad block table. This option is passed to the
- * default bad block table function. */
-#define NAND_USE_FLASH_BBT	0x00010000
 /* This option skips the bbt scan during initialization. */
 #define NAND_SKIP_BBTSCAN	0x00020000
 /* This option is defined if the board driver allocates its own buffers
@@ -409,6 +406,9 @@ struct nand_buffers {
  * @data_poi:		[INTERN] pointer to a data buffer
  * @options:		[BOARDSPECIFIC] various chip options. They can partly be set to inform nand_scan about
  *			special functionality. See the defines for further explanation
+ * @bbt_options:	[INTERN] bad block specific options. All options used
+ *			here must come from bbm.h. By default, these options
+ *			will be copied to the appropriate nand_bbt_descr's.
  * @badblockpos:	[INTERN] position of the bad block marker in the oob area
  * @cellinfo:		[INTERN] MLC/multichip data from chip ident
  * @numchips:		[INTERN] number of physical chips
@@ -459,6 +459,7 @@ struct nand_chip {
 
 	int		chip_delay;
 	unsigned int	options;
+	unsigned int	bbt_options;
 
 	int		page_shift;
 	int		phys_erase_shift;
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author maintainer
#9 lore raw

PATCH 8/10 mtd nand i.MX: remove unused code

In reply to Sascha Hauer
---
 drivers/mtd/nand/nand_imx.c |    7 -------
 1 file changed, 7 deletions(-)
drivers/mtd/nand/nand_imx.c +0 -7
index d056c7c..22bdb74 100644
@@ -1192,13 +1192,6 @@ static int __init imxnd_probe(struct device_d *dev)
 	this->write_buf = imx_nand_write_buf;
 	this->read_buf = imx_nand_read_buf;
 	this->verify_buf = imx_nand_verify_buf;
-#if 0
-	host->clk = clk_get(&pdev->dev, "nfc_clk");
-	if (IS_ERR(host->clk))
-		goto eclk;
-
-	clk_enable(host->clk);
-#endif
 
 	if (pdata->hw_ecc) {
 		this->ecc.calculate = imx_nand_calculate_ecc;
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author maintainer
#10 lore raw

PATCH 9/10 mtd: nand: Add bbt parameter

In reply to Sascha Hauer

The information which kind of bbt (if any) is used is hidden somewhere in the NAND layer. Expose it to a device parameter to make it detectable and visible during runtime.

---
 drivers/mtd/nand/nand_base.c |   19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/mtd/nand/nand_base.c +19 -0
index ac223ca..7675695 100644
@@ -1688,6 +1688,23 @@ static int mtd_set_erasebad(struct device_d *dev, struct param_d *param,
 	return 0;
 }
 
+static const char *mtd_get_bbt_type(struct device_d *dev, struct param_d *p)
+{
+	struct mtd_info *mtd = container_of(dev, struct mtd_info, class_dev);
+	struct nand_chip *chip = mtd->priv;
+	char *str;
+
+	if (!chip->bbt)
+		str = "none";
+	else if ((chip->bbt_td && chip->bbt_td->pages[0] != -1) ||
+				(chip->bbt_md && chip->bbt_md->pages[0] != -1))
+		str = "flashbased";
+	else
+		str = "memorybased";
+
+	return str;
+}
+
 int add_mtd_nand_device(struct mtd_info *mtd, char *devname)
 {
 	int ret;
@@ -1700,6 +1717,8 @@ int add_mtd_nand_device(struct mtd_info *mtd, char *devname)
 		dev_add_param(&mtd->class_dev, "erasebad", mtd_set_erasebad,
 				NULL, 0);
 
+	dev_add_param(&mtd->class_dev, "bbt", NULL, mtd_get_bbt_type, 0);
+
 	return ret;
 }
 
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#11 lore raw

Re: [PATCH 09/10] mtd: nand: Add bbt parameter

drivers/mtd/nand/nand_base.c static int mtd_set_erasebad(struct device_d *dev, · line 1691

Quoted 8 lines · Sascha Hauer

[...]

drivers/mtd/nand/nand_base.c +1 -0
@@ -1688,6 +1688,23 @@ static int mtd_set_erasebad(struct device_d *dev,
 struct param_d *param, return 0;
 }
 
+static const char *mtd_get_bbt_type(struct device_d *dev, struct param_d 

*p) +{

drivers/mtd/nand/nand_base.c static int mtd_set_erasebad(struct device_d *dev, struct param_d *param, · line 1695

Quoted 3 lines
drivers/mtd/nand/nand_base.c +3 -0
+	struct mtd_info *mtd = container_of(dev, struct mtd_info, class_dev);
+	struct nand_chip *chip = mtd->priv;
+	char *str;

Should also be "const char *str"

drivers/mtd/nand/nand_base.c static int mtd_set_erasebad(struct device_d *dev, struct param_d *param, · line 1707

Quoted 12 lines
drivers/mtd/nand/nand_base.c +12 -0
+
+	if (!chip->bbt)
+		str = "none";
+	else if ((chip->bbt_td && chip->bbt_td->pages[0] != -1) ||
+				(chip->bbt_md && chip->bbt_md->pages[0] != -1))
+		str = "flashbased";
+	else
+		str = "memorybased";
+
+	return str;
+}
+

jbe

Signature · 7 lines
Pengutronix e.K.                              | Juergen Beisert             |
Linux Solutions for Science and Industry      | http://www.pengutronix.de/  |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author maintainer
#12 lore raw

Re: [PATCH 09/10] mtd: nand: Add bbt parameter

Quoted 8 lines · Juergen Beisert
Quoted 7 lines · Sascha Hauer

[...]

drivers/mtd/nand/nand_base.c +0 -0
@@ -1688,6 +1688,23 @@ static int mtd_set_erasebad(struct device_d *dev,
 struct param_d *param, return 0;
 }
 
Quoted 7 lines

drivers/mtd/nand/nand_base.c static int mtd_set_erasebad(struct device_d *dev, · line 1691

Quoted 1 line
drivers/mtd/nand/nand_base.c +1 -0
+static const char *mtd_get_bbt_type(struct device_d *dev, struct param_d 

*p) +{

drivers/mtd/nand/nand_base.c static int mtd_set_erasebad(struct device_d *dev, struct param_d *param, · line 1695

Quoted 3 lines
drivers/mtd/nand/nand_base.c +3 -0
+	struct mtd_info *mtd = container_of(dev, struct mtd_info, class_dev);
+	struct nand_chip *chip = mtd->priv;
+	char *str;

Should also be "const char *str"

ok.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author maintainer
#13 lore raw

PATCH 10/10 mtd: nand: Add command to generate a flash BBT

In reply to Sascha Hauer

With 2k NAND flashes the data layout in memory is not what is written on the flash device. This leads to the result that the factory provided bad block markers are not recognized correctly.

To preserve the factory bad block information the i.MX NAND driver will not scan for the bad blocks itself when there is no flash based bbt available, because the mtd layer would do so based on wrong information. Instead, a new command is introduced which allows to manually create a flash bbt based on the correct information.

As this command is tightly coupled to mtd and the i.MX NAND driver the command is placed under drivers/mtd/nand/ instead of commands/ where a command normally belongs to.

---
 drivers/mtd/nand/Kconfig        |    5 +
 drivers/mtd/nand/Makefile       |    1 +
 drivers/mtd/nand/nand_imx.c     |   19 +++-
 drivers/mtd/nand/nand_imx_bbm.c |  220 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 243 insertions(+), 2 deletions(-)
 create mode 100644 drivers/mtd/nand/nand_imx_bbm.c
drivers/mtd/nand/Kconfig +5 -0
index 1a9b929..de8fb5e 100644
@@ -62,6 +62,11 @@ config NAND_IMX
 	prompt "i.MX NAND driver"
 	depends on ARCH_IMX
 
+config NAND_IMX_BBM
+	bool
+	prompt "i.MX NAND flash bbt creation command"
+	depends on NAND_IMX
+
 config NAND_MXS
 	bool
 	select NAND_BBT
drivers/mtd/nand/Makefile +1 -0
index d52c272..0c7c8e2 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_NAND_BBT)			+= nand_bbt.o
 
 obj-$(CONFIG_MTD_NAND_NOMADIK)		+= nomadik_nand.o
 obj-$(CONFIG_NAND_IMX)			+= nand_imx.o
+obj-$(CONFIG_NAND_IMX_BBM)		+= nand_imx_bbm.o
 obj-$(CONFIG_NAND_OMAP_GPMC)		+= nand_omap_gpmc.o nand_omap_bch_decoder.o
 obj-$(CONFIG_NAND_ATMEL)		+= atmel_nand.o
 obj-$(CONFIG_NAND_S3C24XX)		+= nand_s3c24xx.o
drivers/mtd/nand/nand_imx.c +17 -2
index 22bdb74..75aefd6 100644
@@ -1068,7 +1068,7 @@ static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
 static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
 
 static struct nand_bbt_descr bbt_main_descr = {
-	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
+	.options = NAND_BBT_LASTBLOCK
 		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
 	.offs = 0,
 	.len = 4,
@@ -1078,7 +1078,7 @@ static struct nand_bbt_descr bbt_main_descr = {
 };
 
 static struct nand_bbt_descr bbt_mirror_descr = {
-	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
+	.options = NAND_BBT_LASTBLOCK
 		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
 	.offs = 0,
 	.len = 4,
@@ -1179,6 +1179,7 @@ static int __init imxnd_probe(struct device_d *dev)
 	mtd = &host->mtd;
 	mtd->priv = this;
 	mtd->parent = dev;
+	mtd->name = "imx_nand";
 
 	/* 50 us command delay time */
 	this->chip_delay = 5;
@@ -1235,6 +1236,10 @@ static int __init imxnd_probe(struct device_d *dev)
 	imx_nand_set_layout(mtd->writesize, pdata->width == 2 ? 16 : 8);
 
 	if (mtd->writesize >= 2048) {
+		if (!pdata->flash_bbt)
+			dev_warn(dev, "2k or 4k flash detected without flash_bbt. "
+					"You will loose factory bad block markers!\n");
+
 		if (mtd->writesize == 2048)
 			this->ecc.layout = oob_largepage;
 		else
@@ -1243,6 +1248,9 @@ static int __init imxnd_probe(struct device_d *dev)
 		if (nfc_is_v21())
 			writew(NFC_V2_SPAS_SPARESIZE(64), host->regs + NFC_V2_SPAS);
 	} else {
+		bbt_main_descr.options |= NAND_BBT_WRITE | NAND_BBT_CREATE;
+		bbt_mirror_descr.options |= NAND_BBT_WRITE | NAND_BBT_CREATE;
+
 		if (nfc_is_v21())
 			writew(NFC_V2_SPAS_SPARESIZE(16), host->regs + NFC_V2_SPAS);
 	}
@@ -1253,6 +1261,13 @@ static int __init imxnd_probe(struct device_d *dev)
 		goto escan;
 	}
 
+	if (pdata->flash_bbt && this->bbt_td->pages[0] == -1 && this->bbt_md->pages[0] == -1) {
+		dev_warn(dev, "no BBT found. create one using the imx_nand_bbm command\n");
+	} else {
+		bbt_main_descr.options |= NAND_BBT_WRITE | NAND_BBT_CREATE;
+		bbt_mirror_descr.options |= NAND_BBT_WRITE | NAND_BBT_CREATE;
+	}
+
 	add_mtd_nand_device(mtd, "nand");
 
 	dev->priv = host;
drivers/mtd/nand/nand_imx_bbm.c new file +220 -0

220 lines not on this page · raw message

Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#14 lore raw

Re: [PATCH 10/10] mtd: nand: Add command to generate a flash BBT

Quoted 10 lines · Sascha Hauer

[...]

drivers/mtd/nand/nand_imx_bbm.c new file +9 -0
+static int do_imx_nand_bbm(int argc, char *argv[])
+{
+	int opt, ret;
+	struct cdev *cdev;
+	struct mtd_info *mtd;
+	int yes = 0;
+	void *bbt;
+
+	while ((opt = getopt(argc, argv, "y")) > 0) {

drivers/mtd/nand/nand_imx_bbm.c line 151

Quoted 10 lines
drivers/mtd/nand/nand_imx_bbm.c new file +10 -0
+		switch (opt) {
+		case 'y':
+			yes = 1;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	cdev = cdev_open("nand0", O_RDWR);

Is a fixed name a good idea?

Quoted 1 line

[...]

jbe

Signature · 7 lines
Pengutronix e.K.                              | Juergen Beisert             |
Linux Solutions for Science and Industry      | http://www.pengutronix.de/  |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author maintainer
#15 lore raw

Re: [PATCH 10/10] mtd: nand: Add command to generate a flash BBT

Quoted 15 lines · Juergen Beisert
Quoted 14 lines · Sascha Hauer

[...]

drivers/mtd/nand/nand_imx_bbm.c new file +13 -0
+static int do_imx_nand_bbm(int argc, char *argv[])
+{
+	int opt, ret;
+	struct cdev *cdev;
+	struct mtd_info *mtd;
+	int yes = 0;
+	void *bbt;
+
+	while ((opt = getopt(argc, argv, "y")) > 0) {
+		switch (opt) {
+		case 'y':
+			yes = 1;
+			break;
Quoted 8 lines

drivers/mtd/nand/nand_imx_bbm.c line 151

Quoted 6 lines
drivers/mtd/nand/nand_imx_bbm.c new file +6 -0
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	cdev = cdev_open("nand0", O_RDWR);

Is a fixed name a good idea?

Since there is only one NAND device on i.MX I think it's ok.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox