Skip to contentSkip to conversation

NAND: add support for reading ONFI parameters from NAND device

PATCH Superseded Florian Fainelli 5 participants 11 messages Jul 28, 2010 – Aug 9, 2010

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 NAND: add support for reading ONFI parameters from NAND device

A nand_chip which has valid ONFI parameters gets its options field updated with the NAND_ONFI flag. In that case both the ONFI version (in BCD format) as well as the complete page parameters is available in the struct nand_chip. This allows for better detection of some new devices, as well as fine tuning of NAND driver timings. This patch only adds support for ONFI 1.0 parameters.

---

drivers/mtd/nand/nand_base.c +86 -0
index 4a7b864..c255cec 100644
@@ -2773,6 +2773,83 @@ static void nand_set_defaults(struct nand_chip *chip, int busw)
 }
 
 /*
+ * sanitize ONFI strings so we can safely print them
+ */
+static void sanitize_string(uint8_t *s, size_t len)
+{
+	ssize_t i;
+
+	/* null terminate */
+	s[len - 1] = 0;
+
+	/* remove non printable chars */
+	for (i = 0; i < len - 1; i++) {
+		if (s[i] < ' ' || s[i] > 127)
+			s[i] = '?';
+	}
+
+	/* remove trailing spaces */
+	for (i = len - 1; i >= 0; i--) {
+		if (s[i] && s[i] != ' ')
+			break;
+		s[i] = 0;
+	}
+}
+
+/*
+ * Check whether flash support ONFI and read ONFI parameters in that
+ * case
+ */
+static void nand_read_onfi(struct mtd_info *mtd, struct nand_chip *chip)
+{
+	struct nand_onfi_params *p;
+	uint8_t sig[4];
+	uint16_t val;
+
+	if (!chip->cmdfunc || !chip->read_buf)
+		return;
+
+	/* read ONFI signature */
+	chip->cmdfunc(mtd, NAND_CMD_READID, NAND_ADDR_ONFI_ID, -1);
+	chip->read_buf(mtd, sig, sizeof(sig));
+
+	if (memcmp(sig, "ONFI", sizeof(sig)))
+		return;
+
+	/* ONFI seems supported */
+	chip->cmdfunc(mtd, NAND_CMD_READ_ONFI_PARAMS, 0, -1);
+	p = &chip->onfi_params;
+	chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
+
+	/* recheck signature */
+	if (memcmp(p->sig, "ONFI", sizeof(p->sig))) {
+		printk(KERN_INFO "%s: bad ONFI params signature\n", __func__);
+		return;
+	}
+
+	/* check version */
+	val = le16_to_cpu(p->revision);
+	if (!is_power_of_2(val) || val == 1 || val > (1 << 4)) {
+		printk(KERN_INFO "%s: unsupported ONFI version\n", __func__);
+		return;
+	}
+
+	if (val & (1 << 1))
+		chip->onfi_version = 10;
+	else if (val & (1 << 2))
+		chip->onfi_version = 20;
+	else if (val & (1 << 3))
+		chip->onfi_version = 21;
+	else
+		chip->onfi_version = 22;
+
+	chip->options |= NAND_ONFI;
+
+	sanitize_string(p->manufacturer, sizeof(p->manufacturer));
+	sanitize_string(p->model, sizeof(p->model));
+}
+
+/*
  * Get the flash and manufacturer id and lookup if the type is supported
  */
 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
@@ -2958,10 +3035,19 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 	if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
 		chip->cmdfunc = nand_command_lp;
 
+	nand_read_onfi(mtd, chip);
+
 	printk(KERN_INFO "NAND device: Manufacturer ID:"
 	       " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
 	       nand_manuf_ids[maf_idx].name, type->name);
 
+	if (chip->options & NAND_ONFI)
+		printk(KERN_INFO "NAND is ONFI %d.%d compliant "
+		       "(man:%s model:%s)\n",
+		       chip->onfi_version / 10, chip->onfi_version % 10,
+		       chip->onfi_params.manufacturer,
+		       chip->onfi_params.model);
+
 	return type;
 }
 
include/linux/mtd/nand.h +67 -0

97 lines not on this page · raw message

#1 lore raw
Quoted 4 lines
include/linux/mtd/nand.h +0 -0
index a81b185..ad7f58f 100644

<snip>

include/linux/mtd/nand.h typedef enum { · line 198

Quoted 6 lines
include/linux/mtd/nand.h +2 -0
@@ -190,6 +194,9 @@ typedef enum {
  /* Device behaves just like nand, but is readonly */
  #define NAND_ROM		0x00000800
 
+/* Chip supports ONFI */
+#define NAND_ONFI		0x00001000

I've been wondering: how independent are the flags in include/linux/mtd/bbm.h and nand.h? I've working on some patches dealing with various such flags. For instance, I know that the following patch dealt with a potential conflict between flags in bbm.h and nand.h:
http://lists.infradead.org/pipermail/linux-mtd/2010-June/030703.html

I don't know if there's a possibility of conflict between NAND_BBT_WRITE (bbm.h) and your new NAND_ONFI (nand.h); both are 0x00001000. I know *some* options are written into nand_chip->options and later copied onto the options in nand_bbt_descr->options for BBT usage, e.g., I just rewrote part of this as a new function nand_create_default_bbt_descr() in nand_bbt.c:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html

Other pieces of the code perform similar functions at the moment.

Brian

#2 lore raw

Hi Brian,

Quoted 29 lines · On Thursday 29 July 2010 01:38:01 Brian Norris wrote:
Quoted 4 lines
include/linux/mtd/nand.h +0 -0
index a81b185..ad7f58f 100644

<snip>

include/linux/mtd/nand.h typedef enum { · line 199

Quoted 7 lines
include/linux/mtd/nand.h +2 -0
@@ -190,6 +194,9 @@ typedef enum {
 
  /* Device behaves just like nand, but is readonly */
  #define NAND_ROM		0x00000800
 
+/* Chip supports ONFI */
+#define NAND_ONFI		0x00001000

I've been wondering: how independent are the flags in include/linux/mtd/bbm.h and nand.h? I've working on some patches dealing with various such flags. For instance, I know that the following patch dealt with a potential conflict between flags in bbm.h and nand.h:
http://lists.infradead.org/pipermail/linux-mtd/2010-June/030703.html

I don't know if there's a possibility of conflict between NAND_BBT_WRITE (bbm.h) and your new NAND_ONFI (nand.h); both are 0x00001000. I know *some* options are written into nand_chip->options and later copied onto the options in nand_bbt_descr->options for BBT usage, e.g., I just rewrote part of this as a new function nand_create_default_bbt_descr() in nand_bbt.c:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html

Quoted 1 line

Other pieces of the code perform similar functions at the moment.

I admit I did not look too closely at these recent changes, but it seems safe to move the NAND_ONFI a bit higher. I will take the other comments and respin the patch. Thanks!
--
Florian

#3 lore raw
In reply to Florian Fainelli

Hi,

Quoted 38 lines · Florian Fainelli

A nand_chip which has valid ONFI parameters gets its options field updated with the NAND_ONFI flag. In that case both the ONFI version (in BCD format) as well as the complete page parameters is available in the struct nand_chip. This allows for better detection of some new devices, as well as fine tuning of NAND driver timings. This patch only adds support for ONFI 1.0 parameters.

---

drivers/mtd/nand/nand_base.c +21 -0
index 4a7b864..c255cec 100644
@@ -2773,6 +2773,83 @@ static void nand_set_defaults(struct nand_chip *chip, int busw)
 }
 
 
+
+/*
+ * Check whether flash support ONFI and read ONFI parameters in that
+ * case
+ */
+static void nand_read_onfi(struct mtd_info *mtd, struct nand_chip *chip)
+{
+	struct nand_onfi_params *p;
+	uint8_t sig[4];
+	uint16_t val;
+
+	if (!chip->cmdfunc || !chip->read_buf)
+		return;
+
+	/* read ONFI signature */
+	chip->cmdfunc(mtd, NAND_CMD_READID, NAND_ADDR_ONFI_ID, -1);
+	chip->read_buf(mtd, sig, sizeof(sig));
+
+	if (memcmp(sig, "ONFI", sizeof(sig)))
+		return;
+

drivers/mtd/nand/nand_base.c static void nand_set_defaults(struct nand_chip *chip, int busw) · line 2806

Quoted 10 lines
drivers/mtd/nand/nand_base.c +10 -0
+	/* ONFI seems supported */
+	chip->cmdfunc(mtd, NAND_CMD_READ_ONFI_PARAMS, 0, -1);
+	p = &chip->onfi_params;
+	chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
+
+	/* recheck signature */
+	if (memcmp(p->sig, "ONFI", sizeof(p->sig))) {
+		printk(KERN_INFO "%s: bad ONFI params signature\n", __func__);
+		return;
+	}

You need to check crc. onfi param can be corrupted (bitflip ?) and should try at least 3 page.

Also you don't handle endianness (integer are little endian) for value in nand_onfi_params.

Also I am not sure it is worth to export all the onfi param. Fill the mtd param for page, erase, oob size and other stuff. After all rev info and features block, manufacturer information block, memory organization block should only be used by mtd layer. Only electrical parameter block can interest driver for fine tunning.

Quoted 13 lines
drivers/mtd/nand/nand_base.c +2 -0
  * Get the flash and manufacturer id and lookup if the type is supported
  */
 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
@@ -2958,10 +3035,19 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 	if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
 		chip->cmdfunc = nand_command_lp;
 
+	nand_read_onfi(mtd, chip);
+
 	printk(KERN_INFO "NAND device: Manufacturer ID:"
 	       " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
 	       nand_manuf_ids[maf_idx].name, type->name);
 

drivers/mtd/nand/nand_base.c static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, · line 3053

Quoted 10 lines
drivers/mtd/nand/nand_base.c +7 -0
+	if (chip->options & NAND_ONFI)
+		printk(KERN_INFO "NAND is ONFI %d.%d compliant "
+		       "(man:%s model:%s)\n",
+		       chip->onfi_version / 10, chip->onfi_version % 10,
+		       chip->onfi_params.manufacturer,
+		       chip->onfi_params.model);
+
 	return type;
 }
 

This won't work this unknown nand, and not work with some LP nand that doesn't provide additional id bytes.

Matthieu

#4 lore raw

Hi Matthieu,

Quoted 48 lines · On Thursday 29 July 2010 09:54:20 Matthieu CASTET wrote:

Hi,

Quoted 45 lines · Florian Fainelli

A nand_chip which has valid ONFI parameters gets its options field updated with the NAND_ONFI flag. In that case both the ONFI version (in BCD format) as well as the complete page parameters is available in the struct nand_chip. This allows for better detection of some new devices, as well as fine tuning of NAND driver timings. This patch only adds support for ONFI 1.0 parameters.

---

drivers/mtd/nand/nand_base.c +26 -0
index 4a7b864..c255cec 100644
@@ -2773,6 +2773,83 @@ static void nand_set_defaults(struct nand_chip
 *chip, int busw)
 
 }
 
+
+/*
+ * Check whether flash support ONFI and read ONFI parameters in that
+ * case
+ */
+static void nand_read_onfi(struct mtd_info *mtd, struct nand_chip *chip)
+{
+	struct nand_onfi_params *p;
+	uint8_t sig[4];
+	uint16_t val;
+
+	if (!chip->cmdfunc || !chip->read_buf)
+		return;
+
+	/* read ONFI signature */
+	chip->cmdfunc(mtd, NAND_CMD_READID, NAND_ADDR_ONFI_ID, -1);
+	chip->read_buf(mtd, sig, sizeof(sig));
+
+	if (memcmp(sig, "ONFI", sizeof(sig)))
+		return;
+
+	/* ONFI seems supported */
+	chip->cmdfunc(mtd, NAND_CMD_READ_ONFI_PARAMS, 0, -1);
+	p = &chip->onfi_params;
+	chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
+
Quoted 8 lines

drivers/mtd/nand/nand_base.c static void nand_set_defaults(struct nand_chip *chip, int busw) · line 2828

Quoted 5 lines
drivers/mtd/nand/nand_base.c +5 -0
+	/* recheck signature */
+	if (memcmp(p->sig, "ONFI", sizeof(p->sig))) {
+		printk(KERN_INFO "%s: bad ONFI params signature\n", __func__);
+		return;
+	}

You need to check crc. onfi param can be corrupted (bitflip ?) and should try at least 3 page.

Ok.

Quoted 4 lines

Also you don't handle endianness (integer are little endian) for value in nand_onfi_params.

Yes, so far the drivers using those values were doing the correct endian conversion when they need to use them.

Quoted 3 lines

Also I am not sure it is worth to export all the onfi param. Fill the mtd param for page, erase, oob size and other stuff.

What we were interested about are mostly timings, manufacturer and model string because they can help handling new parts correctly.

Quoted 2 lines

After all rev info and features block, manufacturer information block, memory organization block should only be used by mtd layer.

You are right.

Quoted 1 line

Only electrical parameter block can interest driver for fine tunning

Yes.

Quoted 29 lines
Quoted 29 lines
drivers/mtd/nand/nand_base.c +9 -0
  * Get the flash and manufacturer id and lookup if the type is supported
  */
 
 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
@@ -2958,10 +3035,19 @@ static struct nand_flash_dev
 *nand_get_flash_type(struct mtd_info *mtd,
 
 	if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
 
 		chip->cmdfunc = nand_command_lp;
 
+	nand_read_onfi(mtd, chip);
+
 
 	printk(KERN_INFO "NAND device: Manufacturer ID:"
 
 	       " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
 	       nand_manuf_ids[maf_idx].name, type->name);
 
+	if (chip->options & NAND_ONFI)
+		printk(KERN_INFO "NAND is ONFI %d.%d compliant "
+		       "(man:%s model:%s)\n",
+		       chip->onfi_version / 10, chip->onfi_version % 10,
+		       chip->onfi_params.manufacturer,
+		       chip->onfi_params.model);
+
Quoted 6 lines
Quoted 3 lines

return type;

}

This won't work this unknown nand, and not work with some LP nand that doesn't provide additional id bytes.

So how do you see things regarding the provisioning of the relevant ONFI parameters?
--
Florian

#5 lore raw
Quoted 10 lines · Florian Fainelli

Hi Matthieu,

Quoted 5 lines · On Thursday 29 July 2010 09:54:20 Matthieu CASTET wrote:

Hi,

Also you don't handle endianness (integer are little endian) for value in nand_onfi_params.

Yes, so far the drivers using those values were doing the correct endian conversion when they need to use them.

In that case use le16, le32, ... type. Also prefer kernel type over uintx_t type.

Quoted 4 lines
Quoted 2 lines

This won't work this unknown nand, and not work with some LP nand that doesn't provide additional id bytes.

So how do you see things regarding the provisioning of the relevant ONFI parameters?

I will see something like in the patch attached in
http://article.gmane.org/gmane.linux.drivers.mtd/30935.

ONFI parsing is done early in nand_get_flash_type (unknow chip or LP nand). If the ONFI parsing is ok we bypass the old identification method (additional id bytes).

As an example I attach a patch that mix your patch and mine.

Matthieu

PS : the NAND_ONFI flags seems useless, we can use onfi_version (0 means no onfi).

drivers/mtd/nand/nand_base.c +135 -30
index 4a7b864..25a44fa 100644
@@ -2772,15 +2772,50 @@ static void nand_set_defaults(struct nand_chip *chip, int busw)
 
 }
 
+static u16 onfi_crc(u16 crc, unsigned char const *p, size_t len)
+{
+	int i;
+	while (len--) {
+		crc ^= *p++ << 8;
+		for (i = 0; i < 8; i++)
+			crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
+	}
+	return crc;
+}
+
+/*
+ * sanitize ONFI strings so we can safely print them
+ */
+static void sanitize_string(uint8_t *s, size_t len)
+{
+	ssize_t i;
+
+	/* null terminate */
+	s[len - 1] = 0;
+
+	/* remove non printable chars */
+	for (i = 0; i < len - 1; i++) {
+		if (s[i] < ' ' || s[i] > 127)
+			s[i] = '?';
+	}
+
+	/* remove trailing spaces */
+	for (i = len - 1; i >= 0; i--) {
+		if (s[i] && s[i] != ' ')
+			break;
+		s[i] = 0;
+	}
+}
+
 /*
  * Get the flash and manufacturer id and lookup if the type is supported
  */
 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 						  struct nand_chip *chip,
-						  int busw, int *maf_id,
+						  int busw, int *maf_id, int *dev_id
 						  struct nand_flash_dev *type)
 {
-	int i, dev_id, maf_idx;
+	int i, maf_idx;
 	u8 id_data[8];
 
 	/* Select the device */
@@ -2797,7 +2832,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 
 	/* Read manufacturer and device IDs */
 	*maf_id = chip->read_byte(mtd);
-	dev_id = chip->read_byte(mtd);
+	*dev_id = chip->read_byte(mtd);
 
 	/* Try again to make sure, as some systems the bus-hold or other
 	 * interface concerns can cause random data which looks like a
@@ -2807,15 +2842,13 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 
 	chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
 
-	/* Read entire ID string */
-
-	for (i = 0; i < 8; i++)
+	for (i = 0; i < 2; i++)
 		id_data[i] = chip->read_byte(mtd);
 
-	if (id_data[0] != *maf_id || id_data[1] != dev_id) {
+	if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
 		printk(KERN_INFO "%s: second ID read did not match "
 		       "%02x,%02x against %02x,%02x\n", __func__,
-		       *maf_id, dev_id, id_data[0], id_data[1]);
+		       *maf_id, *dev_id, id_data[0], id_data[1]);
 		return ERR_PTR(-ENODEV);
 	}
 
@@ -2823,8 +2856,79 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 		type = nand_flash_ids;
 
 	for (; type->name != NULL; type++)
-		if (dev_id == type->id)
+		if (*dev_id == type->id)
                         break;
+#if 1
+	chip->onfi_version = 0;
+	if (!type->name || !type->pagesize) {
+		/* try ONFI for unknow chip or LP */
+		chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
+		if (chip->read_byte(mtd) == 'O' &&
+			chip->read_byte(mtd) == 'N' &&
+			chip->read_byte(mtd) == 'F' &&
+			chip->read_byte(mtd) == 'I') {
+
+			struct nand_onfi_params *p = &chip->onfi_params;
+			int i;
+
+			printk(KERN_INFO "ONFI flash detected\n");
+			chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
+			for (i = 0; i < 3; i++) {
+				/* XXX this that ok to use read_buf at this stage ? */
+				chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
+				if (onfi_crc(0x4F4E, (uint8_t *)p, 254) == le16_to_cpu(p->crc))
+				{
+					 printk(KERN_INFO "ONFI param page %d valid\n", i);
+					 break;
+				}
+			}
+			if (i < 3) {
+				/* check version */
+				int val = le16_to_cpu(p->revision);
+				if (!is_power_of_2(val) || val == 1 || val > (1 << 4)) {
+					printk(KERN_INFO "%s: unsupported ONFI version\n", __func__);
+				}
+				else {
+					if (val & (1 << 1))
+						chip->onfi_version = 10;
+					else if (val & (1 << 2))
+						chip->onfi_version = 20;
+					else if (val & (1 << 3))
+						chip->onfi_version = 21;
+					else
+						chip->onfi_version = 22;
+				}
+			}
+			if (chip->onfi_version) {
+				sanitize_string(p->manufacturer, sizeof(p->manufacturer));
+				sanitize_string(p->model, sizeof(p->model));
+				if (!mtd->name)
+					mtd->name = p->model;
+				mtd->writesize = le32_to_cpu(p->byte_per_page);
+				mtd->erasesize = le32_to_cpu(p->pages_per_block)*mtd->writesize;
+				mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
+				chip->chipsize = le32_to_cpu(p->blocks_per_lun) * mtd->erasesize;
+				busw = 0;
+				if (le16_to_cpu(p->features) & 1)
+					busw = NAND_BUSWIDTH_16;
+
+				chip->options &= ~NAND_CHIPOPTIONS_MSK;
+				chip->options |= (NAND_NO_READRDY | NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;
+
+				/* emulate cellinfo ??? */
+				//chip->cellinfo = 0x8;
+				goto ident_done;
+
+			}
+		}
+	}
+#endif
+	chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
+
+	/* Read entire ID string */
+
+	for (i = 0; i < 8; i++)
+		id_data[i] = chip->read_byte(mtd);
 
 	if (!type->name)
 		return ERR_PTR(-ENODEV);
@@ -2886,6 +2990,21 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 		mtd->oobsize = mtd->writesize / 32;
 		busw = type->options & NAND_BUSWIDTH_16;
 	}
+	/* Get chip options, preserve non chip based options */
+	chip->options &= ~NAND_CHIPOPTIONS_MSK;
+	chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
+
+	/* Check if chip is a not a samsung device. Do not clear the
+	 * options for chips which are not having an extended id.
+	 */
+	if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
+		chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
+ident_done:
+
+	/*
+	 * Set chip as a default. Board drivers can override it, if necessary
+	 */
+	chip->options |= NAND_NO_AUTOINCR;
 
 	/* Try to identify manufacturer */
 	for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
@@ -2900,7 +3019,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 	if (busw != (chip->options & NAND_BUSWIDTH_16)) {
 		printk(KERN_INFO "NAND device: Manufacturer ID:"
 		       " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
-		       dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
+		       *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
 		printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
 		       (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
 		       busw ? 16 : 8);
@@ -2924,21 +3043,6 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 		NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
 	chip->badblockbits = 8;
 
-	/* Get chip options, preserve non chip based options */
-	chip->options &= ~NAND_CHIPOPTIONS_MSK;
-	chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
-
-	/*
-	 * Set chip as a default. Board drivers can override it, if necessary
-	 */
-	chip->options |= NAND_NO_AUTOINCR;
-
-	/* Check if chip is a not a samsung device. Do not clear the
-	 * options for chips which are not having an extended id.
-	 */
-	if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
-		chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
-
 	/*
 	 * Bad block marker is stored in the last page of each block
 	 * on Samsung and Hynix MLC devices
@@ -2958,9 +3062,10 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 	if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
 		chip->cmdfunc = nand_command_lp;
 
+	/* TODO onfi flash name */
 	printk(KERN_INFO "NAND device: Manufacturer ID:"
-	       " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
-	       nand_manuf_ids[maf_idx].name, type->name);
+	       " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
+	       nand_manuf_ids[maf_idx].name, chip->onfi_version?type->name:chip->onfi_params.model);
 
 	return type;
 }
@@ -2979,7 +3084,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 int nand_scan_ident(struct mtd_info *mtd, int maxchips,
 		    struct nand_flash_dev *table)
 {
-	int i, busw, nand_maf_id;
+	int i, busw, nand_maf_id, nand_dev_id;
 	struct nand_chip *chip = mtd->priv;
 	struct nand_flash_dev *type;
 
@@ -2989,7 +3094,7 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,
 	nand_set_defaults(chip, busw);
 
 	/* Read the flash type */
-	type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, table);
+	type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, &nand_dev_id, table);
 
 	if (IS_ERR(type)) {
 		if (!(chip->options & NAND_SCAN_SILENT_NODEV))
@@ -3007,7 +3112,7 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,
 		chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
 		/* Read manufacturer and device IDs */
 		if (nand_maf_id != chip->read_byte(mtd) ||
-		    type->id != chip->read_byte(mtd))
+		    nand_dev_id != chip->read_byte(mtd))
 			break;
 	}
 	if (i > 1)
include/linux/mtd/nand.h +66 -0

84 lines not on this page · raw message

#6 lore raw

Hi Matthieu,

Quoted 22 lines · On Monday 02 August 2010 11:25:49 Matthieu CASTET wrote:
Quoted 11 lines · Florian Fainelli

Hi Matthieu,

Quoted 5 lines · On Thursday 29 July 2010 09:54:20 Matthieu CASTET wrote:

Hi,

Also you don't handle endianness (integer are little endian) for value in nand_onfi_params.

Yes, so far the drivers using those values were doing the correct endian conversion when they need to use them.

In that case use le16, le32, ... type. Also prefer kernel type over uintx_t type.

Quoted 5 lines
Quoted 2 lines

This won't work this unknown nand, and not work with some LP nand that doesn't provide additional id bytes.

So how do you see things regarding the provisioning of the relevant ONFI parameters?

Quoted 6 lines

I will see something like in the patch attached in
http://article.gmane.org/gmane.linux.drivers.mtd/30935.

ONFI parsing is done early in nand_get_flash_type (unknow chip or LP nand). If the ONFI parsing is ok we bypass the old identification method (additional id bytes).

Looks ok to me.

Quoted 8 lines

As an example I attach a patch that mix your patch and mine.

Matthieu

PS : the NAND_ONFI flags seems useless, we can use onfi_version (0 means no onfi).

Right, thanks for noticing that.

I got a couple of comments on your patch that I inlined, the rest looks good.
--

drivers/mtd/nand/nand_base.c +51 -9
index 4a7b864..25a44fa 100644
@@ -2772,15 +2772,50 @@ static void nand_set_defaults(struct nand_chip *chip, int busw)
 
 }
 
+static u16 onfi_crc(u16 crc, unsigned char const *p, size_t len)
+{
+       int i;
+       while (len--) {
+               crc ^= *p++ << 8;
+               for (i = 0; i < 8; i++)
+                       crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
+       }
+       return crc;
+}
+
+/*
+ * sanitize ONFI strings so we can safely print them
+ */
+static void sanitize_string(uint8_t *s, size_t len)
+{
+       ssize_t i;
+
+       /* null terminate */
+       s[len - 1] = 0;
+
+       /* remove non printable chars */
+       for (i = 0; i < len - 1; i++) {
+               if (s[i] < ' ' || s[i] > 127)
+                       s[i] = '?';
+       }
+
+       /* remove trailing spaces */
+       for (i = len - 1; i >= 0; i--) {
+               if (s[i] && s[i] != ' ')
+                       break;
+               s[i] = 0;
+       }
+}
+
 /*
  * Get the flash and manufacturer id and lookup if the type is supported
  */
 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
                                                  struct nand_chip *chip,
-                                                 int busw, int *maf_id,
+                                                 int busw, int *maf_id, int *dev_id
                                                  struct nand_flash_dev *type)
 {
-       int i, dev_id, maf_idx;
+       int i, maf_idx;
        u8 id_data[8];
 
        /* Select the device */
@@ -2797,7 +2832,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 
        /* Read manufacturer and device IDs */
        *maf_id = chip->read_byte(mtd);
-       dev_id = chip->read_byte(mtd);
+       *dev_id = chip->read_byte(mtd);
 
        /* Try again to make sure, as some systems the bus-hold or other
         * interface concerns can cause random data which looks like a
@@ -2807,15 +2842,13 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 
        chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
 
-       /* Read entire ID string */
-
-       for (i = 0; i < 8; i++)
+       for (i = 0; i < 2; i++)
                id_data[i] = chip->read_byte(mtd);
 
-       if (id_data[0] != *maf_id || id_data[1] != dev_id) {
+       if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
                printk(KERN_INFO "%s: second ID read did not match "
                       "%02x,%02x against %02x,%02x\n", __func__,
-                      *maf_id, dev_id, id_data[0], id_data[1]);
+                      *maf_id, *dev_id, id_data[0], id_data[1]);
                return ERR_PTR(-ENODEV);
        }
 
@@ -2823,8 +2856,79 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
                type = nand_flash_ids;
 
        for (; type->name != NULL; type++)
-               if (dev_id == type->id)
+               if (*dev_id == type->id)
                         break;
+#if 1
+       chip->onfi_version = 0;
+       if (!type->name || !type->pagesize) {
+               /* try ONFI for unknow chip or LP */
+               chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
+               if (chip->read_byte(mtd) == 'O' &&
+                       chip->read_byte(mtd) == 'N' &&
+                       chip->read_byte(mtd) == 'F' &&
+                       chip->read_byte(mtd) == 'I') {
 

Why not use what was in our original patch and do the memcmp? That looks cleaner to me and allows to invert the logic on the if statement to get the code cleaner. That's just cosmetic anyway.

+
+ struct nand_onfi_params *p = &chip->onfi_params;
+ int i;
+

+                       printk(KERN_INFO "ONFI flash detected\n");
+                       chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
+                       for (i = 0; i < 3; i++) {
+                               /* XXX this that ok to use read_buf at this stage ? */
+                               chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
+                               if (onfi_crc(0x4F4E, (uint8_t *)p, 254) == le16_to_cpu(p->crc))
+                               {

+ printk(KERN_INFO "ONFI param page %d valid\n", i);
+ break;
+ }
+ }
+ if (i < 3) {

+                               /* check version */
+                               int val = le16_to_cpu(p->revision);
+                               if (!is_power_of_2(val) || val == 1 || val > (1 << 4)) {

the !is_power_of_2 check does not work for ONFI version 2.1 (3), so I would only keep the two other checks.

+ printk(KERN_INFO "%s: unsupported ONFI version\n", __func__);
+ }
+ else {
+ if (val & (1 << 1))
+ chip->onfi_version = 10;
+ else if (val & (1 << 2))
+ chip->onfi_version = 20;
+ else if (val & (1 << 3))
+ chip->onfi_version = 21;
+ else
+ chip->onfi_version = 22;
+ }
+ }
+ if (chip->onfi_version) {

+                               sanitize_string(p->manufacturer, sizeof(p->manufacturer));
+                               sanitize_string(p->model, sizeof(p->model));
+                               if (!mtd->name)
+                                       mtd->name = p->model;
+                               mtd->writesize = le32_to_cpu(p->byte_per_page);
+                               mtd->erasesize = le32_to_cpu(p->pages_per_block)*mtd->writesize;
+                               mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
+                               chip->chipsize = le32_to_cpu(p->blocks_per_lun) * mtd->erasesize;
+                               busw = 0;
+                               if (le16_to_cpu(p->features) & 1)

+ busw = NAND_BUSWIDTH_16;
+
+ chip->options &= ~NAND_CHIPOPTIONS_MSK;
+ chip->options |= (NAND_NO_READRDY | NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;
+

+                               /* emulate cellinfo ??? */
+                               //chip->cellinfo = 0x8;
+                               goto ident_done;

+
+ }
+ }
+ }
+#endif
+ chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
+
+ /* Read entire ID string */
+
+ for (i = 0; i < 8; i++)
+ id_data[i] = chip->read_byte(mtd);

        if (!type->name)
                return ERR_PTR(-ENODEV);
@@ -2886,6 +2990,21 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
                mtd->oobsize = mtd->writesize / 32;
                busw = type->options & NAND_BUSWIDTH_16;
        }
+       /* Get chip options, preserve non chip based options */
+       chip->options &= ~NAND_CHIPOPTIONS_MSK;
+       chip->options |= type->options & NAND_CHIPOPTIONS_MSK;

+
+ /* Check if chip is a not a samsung device. Do not clear the
+ * options for chips which are not having an extended id.
+ */
+ if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
+ chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
+ident_done:
+
+ /*
+ * Set chip as a default. Board drivers can override it, if necessary
+ */
+ chip->options |= NAND_NO_AUTOINCR;

        /* Try to identify manufacturer */
        for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
@@ -2900,7 +3019,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
        if (busw != (chip->options & NAND_BUSWIDTH_16)) {
                printk(KERN_INFO "NAND device: Manufacturer ID:"
                       " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
-                      dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
+                      *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
                printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
                       (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
                       busw ? 16 : 8);
@@ -2924,21 +3043,6 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
                NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
        chip->badblockbits = 8;
-       /* Get chip options, preserve non chip based options */
-       chip->options &= ~NAND_CHIPOPTIONS_MSK;
-       chip->options |= type->options & NAND_CHIPOPTIONS_MSK;

-
- /*
- * Set chip as a default. Board drivers can override it, if necessary
- */
- chip->options |= NAND_NO_AUTOINCR;
-
- /* Check if chip is a not a samsung device. Do not clear the
- * options for chips which are not having an extended id.
- */
- if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
- chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
-
/*
* Bad block marker is stored in the last page of each block
* on Samsung and Hynix MLC devices
@@ -2958,9 +3062,10 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,

        if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
                chip->cmdfunc = nand_command_lp;

+ /* TODO onfi flash name */
printk(KERN_INFO "NAND device: Manufacturer ID:"

-              " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
-              nand_manuf_ids[maf_idx].name, type->name);
+              " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
+              nand_manuf_ids[maf_idx].name, chip->onfi_version?type->name:chip->onfi_params.model);
        return type;

}
@@ -2979,7 +3084,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
int nand_scan_ident(struct mtd_info *mtd, int maxchips,
struct nand_flash_dev *table)
{
- int i, busw, nand_maf_id;

+       int i, busw, nand_maf_id, nand_dev_id;
        struct nand_chip *chip = mtd->priv;
        struct nand_flash_dev *type;

@@ -2989,7 +3094,7 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,

        nand_set_defaults(chip, busw);
 
        /* Read the flash type */

- type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, table);
+ type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, &nand_dev_id, table);

        if (IS_ERR(type)) {
                if (!(chip->options & NAND_SCAN_SILENT_NODEV))
@@ -3007,7 +3112,7 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,
                chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
                /* Read manufacturer and device IDs */
                if (nand_maf_id != chip->read_byte(mtd) ||
-                   type->id != chip->read_byte(mtd))
+                   nand_dev_id != chip->read_byte(mtd))
                        break;
        }
        if (i > 1)

--
Florian

#7 lore raw

Hi Florian,

Quoted 54 lines · Florian Fainelli

Hi Matthieu,

Quoted 16 lines · On Monday 02 August 2010 11:25:49 Matthieu CASTET wrote:
Quoted 10 lines · Florian Fainelli

Hi Matthieu,

Quoted 5 lines · On Thursday 29 July 2010 09:54:20 Matthieu CASTET wrote:

Hi,

Also you don't handle endianness (integer are little endian) for value in nand_onfi_params.

Yes, so far the drivers using those values were doing the correct endian conversion when they need to use them.

In that case use le16, le32, ... type. Also prefer kernel type over uintx_t type.

Quoted 2 lines
Quoted 2 lines

This won't work this unknown nand, and not work with some LP nand that doesn't provide additional id bytes.

Quoted 8 lines
Quoted 2 lines

So how do you see things regarding the provisioning of the relevant ONFI parameters?

I will see something like in the patch attached in
http://article.gmane.org/gmane.linux.drivers.mtd/30935.

ONFI parsing is done early in nand_get_flash_type (unknow chip or LP nand). If the ONFI parsing is ok we bypass the old identification method (additional id bytes).

Looks ok to me.

Quoted 7 lines

As an example I attach a patch that mix your patch and mine.

Matthieu

PS : the NAND_ONFI flags seems useless, we can use onfi_version (0 means no onfi).

Right, thanks for noticing that.

I got a couple of comments on your patch that I inlined, the rest looks good.
--

drivers/mtd/nand/nand_base.c +9 -0
+#if 1
+       chip->onfi_version = 0;
+       if (!type->name || !type->pagesize) {
+               /* try ONFI for unknow chip or LP */
+               chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
+               if (chip->read_byte(mtd) == 'O' &&
+                       chip->read_byte(mtd) == 'N' &&
+                       chip->read_byte(mtd) == 'F' &&
+                       chip->read_byte(mtd) == 'I') {
Quoted 3 lines

Why not use what was in our original patch and do the memcmp? That looks cleaner to me and allows to invert the logic on the if statement to get the code cleaner. That's just cosmetic anyway.

I wanted to avoid to use read_buf, because some advanced controller (those who implement cmdfunc) need to overrides all io access.
But some driver assumed that nand_scan_ident only used read_byte. That the case of the denali driver [1]. Using it will cause random read in memory and likely a kernel panic.

But we need read_buf for reading onfi page. Also these advanced controllers will break because they won't handle correctly in cmdfunc new NAND_CMD_READID and NAND_CMD_PARAM.

I don't know what the best way to handle them.

Quoted 8 lines
(quoted diff) +4 -0
+                       if (i < 3) {
+                               /* check version */
+                               int val = le16_to_cpu(p->revision);
+                               if (!is_power_of_2(val) || val == 1 || val > (1 << 4)) {

the !is_power_of_2 check does not work for ONFI version 2.1 (3), so I would only keep the two other checks.

Ok.

Will you take care to post a new patch ?

Matthieu

[1]
/* register the driver with the NAND core subsystem */

    denali->nand.select_chip = denali_select_chip;
    denali->nand.cmdfunc = denali_cmdfunc;
    denali->nand.read_byte = denali_read_byte;
    denali->nand.waitfunc = denali_waitfunc;

    /* scan for NAND devices attached to the controller
     * this is the first stage in a two step process to register
     * with the nand subsystem */
    if (nand_scan_ident(&denali->mtd, LLD_MAX_FLASH_BANKS, NULL))
#8 lore raw

Hi Matthieu,

Quoted 61 lines · On Monday 09 August 2010 11:25:18 Matthieu CASTET wrote:

Hi Florian,

Quoted 58 lines · Florian Fainelli

Hi Matthieu,

Quoted 22 lines · On Monday 02 August 2010 11:25:49 Matthieu CASTET wrote:
Quoted 11 lines · Florian Fainelli

Hi Matthieu,

Quoted 5 lines · On Thursday 29 July 2010 09:54:20 Matthieu CASTET wrote:

Hi,

Also you don't handle endianness (integer are little endian) for value in nand_onfi_params.

Yes, so far the drivers using those values were doing the correct endian conversion when they need to use them.

In that case use le16, le32, ... type. Also prefer kernel type over uintx_t type.

Quoted 5 lines
Quoted 2 lines

This won't work this unknown nand, and not work with some LP nand that doesn't provide additional id bytes.

So how do you see things regarding the provisioning of the relevant ONFI parameters?

Quoted 6 lines

I will see something like in the patch attached in
http://article.gmane.org/gmane.linux.drivers.mtd/30935.

ONFI parsing is done early in nand_get_flash_type (unknow chip or LP nand). If the ONFI parsing is ok we bypass the old identification method (additional id bytes).

Looks ok to me.

Quoted 7 lines

As an example I attach a patch that mix your patch and mine.

Matthieu

PS : the NAND_ONFI flags seems useless, we can use onfi_version (0 means no onfi).

Right, thanks for noticing that.

I got a couple of comments on your patch that I inlined, the rest looks good.
--

drivers/mtd/nand/nand_base.c +9 -0
+#if 1
+       chip->onfi_version = 0;
+       if (!type->name || !type->pagesize) {
+               /* try ONFI for unknow chip or LP */
+               chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
+               if (chip->read_byte(mtd) == 'O' &&
+                       chip->read_byte(mtd) == 'N' &&
+                       chip->read_byte(mtd) == 'F' &&
+                       chip->read_byte(mtd) == 'I') {
Quoted 6 lines
Quoted 3 lines

Why not use what was in our original patch and do the memcmp? That looks cleaner to me and allows to invert the logic on the if statement to get the code cleaner. That's just cosmetic anyway.

I wanted to avoid to use read_buf, because some advanced controller (those who implement cmdfunc) need to overrides all io access.

Ok.

Quoted 3 lines

But some driver assumed that nand_scan_ident only used read_byte. That the case of the denali driver [1]. Using it will cause random read in memory and likely a kernel panic.

Ok, then I will update it as part as the patch adding ONFI reading so that it is future-proof anyway.

Quoted 6 lines

But we need read_buf for reading onfi page. Also these advanced controllers will break because they won't handle correctly in cmdfunc new NAND_CMD_READID and NAND_CMD_PARAM.

I don't know what the best way to handle them.

Such driver need to handle those anyway.

Quoted 7 lines
Quoted 7 lines
(quoted diff) +4 -0
+                       if (i < 3) {
+                               /* check version */
+                               int val = le16_to_cpu(p->revision);
+                               if (!is_power_of_2(val) || val == 1 ||

val > (1 << 4)) {

Quoted 6 lines
Quoted 2 lines

the !is_power_of_2 check does not work for ONFI version 2.1 (3), so I would only keep the two other checks.

Ok.

Will you take care to post a new patch ?

Sure, thanks for your follow-up.

Quoted 20 lines

Matthieu

[1]
/* register the driver with the NAND core subsystem */

     denali->nand.select_chip = denali_select_chip;
     denali->nand.cmdfunc = denali_cmdfunc;
     denali->nand.read_byte = denali_read_byte;
     denali->nand.waitfunc = denali_waitfunc;

     /* scan for NAND devices attached to the controller
      * this is the first stage in a two step process to register
      * with the nand subsystem */
     if (nand_scan_ident(&denali->mtd, LLD_MAX_FLASH_BANKS, NULL))

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

#9 lore raw
In reply to Florian Fainelli
Quoted 8 lines · Florian Fainelli

A nand_chip which has valid ONFI parameters gets its options field updated with the NAND_ONFI flag. In that case both the ONFI version (in BCD format) as well as the complete page parameters is available in the struct nand_chip. This allows for better detection of some new devices, as well as fine tuning of NAND driver timings. This patch only adds support for ONFI 1.0 parameters.

I assume you will send v2 of this patch, so not taking this one.

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
#10 lore raw
Quoted 11 lines · Artem Bityutskiy
Quoted 8 lines · Florian Fainelli

A nand_chip which has valid ONFI parameters gets its options field updated with the NAND_ONFI flag. In that case both the ONFI version (in BCD format) as well as the complete page parameters is available in the struct nand_chip. This allows for better detection of some new devices, as well as fine tuning of NAND driver timings. This patch only adds support for ONFI 1.0 parameters.

I assume you will send v2 of this patch, so not taking this one.

Please don't

We still need to address all remarks Matthieu did, and of course gives this more testing (this was more an "RFC" than a "PATCH", sorry)

Signature · 1 line
Maxime