mtd: spinand: add support for detection with param page
Subsystems (2)
v2 cover mtd: spinand: add support for detection with param page
This patchset adds support for spi-nand detection using parameter page. This helps dealing with future JEDEC ID conflicts introduced by crazy vendors. Also, the added ability to auto-detect chip capacity can help reducing number of entries in our spi-nand id table.
Change since v1: extract common functions from raw nand
Chuanhong Guo (3):
mtd: nand: extract some onfi functions to nandcore
mtd: spinand: add support for detection with param page
mtd: spinand: probe Winbond W25N01GV/W using param page
drivers/mtd/nand/Makefile | 2 +- drivers/mtd/nand/onfi.c | 115 ++++++++++++++++ drivers/mtd/nand/raw/internals.h | 1 - drivers/mtd/nand/raw/nand_base.c | 18 --- drivers/mtd/nand/raw/nand_jedec.c | 4 +- drivers/mtd/nand/raw/nand_onfi.c | 70 +--------- drivers/mtd/nand/spi/Makefile | 2 +- drivers/mtd/nand/spi/core.c | 23 ++-- drivers/mtd/nand/spi/onfi.c | 219 ++++++++++++++++++++++++++++++ drivers/mtd/nand/spi/winbond.c | 25 ++-- include/linux/mtd/onfi.h | 8 ++ include/linux/mtd/spinand.h | 53 ++++++++ 12 files changed, 431 insertions(+), 109 deletions(-) create mode 100644 drivers/mtd/nand/onfi.c create mode 100644 drivers/mtd/nand/spi/onfi.c
Signature · 1 line
2.35.1
PATCH v2 1/3 mtd: nand: extract some onfi functions to nandcore
These functions will be shared between raw nand and spi nand. Extract them into mtd/nand/onfi.c.
---
Change since v1: new patch
drivers/mtd/nand/Makefile | 2 +- drivers/mtd/nand/onfi.c | 115 ++++++++++++++++++++++++++++++ drivers/mtd/nand/raw/internals.h | 1 - drivers/mtd/nand/raw/nand_base.c | 18 ----- drivers/mtd/nand/raw/nand_jedec.c | 4 +- drivers/mtd/nand/raw/nand_onfi.c | 70 ++---------------- include/linux/mtd/onfi.h | 8 +++ 7 files changed, 130 insertions(+), 88 deletions(-) create mode 100644 drivers/mtd/nand/onfi.c
drivers/mtd/nand/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 -nandcore-objs := core.o bbt.o +nandcore-objs := core.o bbt.o onfi.o obj-$(CONFIG_MTD_NAND_CORE) += nandcore.o obj-$(CONFIG_MTD_NAND_ECC_MEDIATEK) += ecc-mtk.o
drivers/mtd/nand/onfi.c new file +115 -0
115 lines not on this page · raw message
drivers/mtd/nand/raw/internals.h +0 -1
@@ -110,7 +110,6 @@ int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, unsigned int len);void nand_decode_ext_id (struct nand_chip *chip);void panic_nand_wait (struct nand_chip *chip,unsigned long timeo); -void sanitize_string (uint8_t *s,size_t len);static inline bool nand_has_exec_op (struct nand_chip *chip) {
drivers/mtd/nand/raw/nand_base.c +0 -18
@@ -4715,24 +4715,6 @@ static void nand_set_defaults(struct nand_chip *chip) chip->buf_align =1 ; } -/* Sanitize ONFI strings so we can safely print them */ -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 */ -strim (s); -} -/* * nand_id_has_period - Check if an ID string has a given wraparound period * @id_data: the ID string
drivers/mtd/nand/raw/nand_jedec.c +2 -2
@@ -87,8 +87,8 @@ int nand_jedec_detect(struct nand_chip *chip) goto free_jedec_param_page; } -sanitize_string (p->manufacturer,sizeof (p->manufacturer)); -sanitize_string (p->model,sizeof (p->model)); +onfi_sanitize_string (p->manufacturer,sizeof (p->manufacturer)); +onfi_sanitize_string (p->model,sizeof (p->model)); chip->parameters.model =kstrdup (p->model,GFP_KERNEL );if (!chip->parameters.model) { ret = -ENOMEM ;
drivers/mtd/nand/raw/nand_onfi.c +4 -66
104 lines not on this page · raw message
include/linux/mtd/onfi.h +8 -0
@@ -12,6 +12,7 @@#include <linux/types.h> #include <linux/bitfield.h> +#include <linux/mtd/nand.h> /* ONFI version bits */ #define ONFI_VERSION_1_0 BIT(1)
@@ -186,4 +187,11 @@ struct onfi_params { u8 vendor[88]; }; +void onfi_bit_wise_majority (const void **srcbufs,unsigned int nsrcbufs, +void *dstbuf,unsigned int bufsize); +u16 onfi_crc16 (u16 crc,u8 const *p,size_t len); +void onfi_parse_memorg (const struct nand_onfi_params *p, +struct nand_memory_organization *memorg); +void onfi_sanitize_string (uint8_t *s,size_t len); +#endif /* __LINUX_MTD_ONFI_H */
Signature · 1 line
2.35.1
PATCH v2 2/3 mtd: spinand: add support for detection with param page
In reply to Chuanhong GuoSPI-NAND detection using chip ID isn't always reliable.
Here are two known cases:
1. ESMT uses JEDEC ID from other vendors. This may collapse with future chips.
2. Winbond W25N01KV uses the exact same JEDEC ID as W25N01GV while having completely different chip parameters.
Recent SPI-NANDs have a parameter page which is stored in the same format as raw NAND ONFI data. There are strings for chip manufacturer and chip model. Chip ECC requirement and memory organization are available too.
This patch adds support for detecting SPI-NANDs with the parameter page after ID matching failed. In this detection, memory organization and ECC requirements are taken from the parameter page, and other SPI-NAND specific parameters are obtained by matching chip model string with a separated table.
It's common for vendors to release a series of SPI-NANDs with the same SPI-NAND parameters in different voltages and/or capacities. The chip table defined in this patch supports multiple model strings in one entry, and multiple chip models can be covered using only one entry.
---
The parameter page has sufficient checking to reject garbage data, and the reading sequence seems to be the same across vendors. So I think it isn't necessary to keep it vendor-private.
Change since v1: drop duplicated code from rawnand and use extracted functions instead.
drivers/mtd/nand/spi/Makefile | 2 +- drivers/mtd/nand/spi/core.c | 23 ++-- drivers/mtd/nand/spi/onfi.c | 217 ++++++++++++++++++++++++++++++++++ include/linux/mtd/spinand.h | 50 ++++++++ 4 files changed, 280 insertions(+), 12 deletions(-) create mode 100644 drivers/mtd/nand/spi/onfi.c
drivers/mtd/nand/spi/Makefile +1 -1
@@ -1,3 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 -spinand-objs := core.o gigadevice.o macronix.o micron.o paragon.o toshiba.o winbond.o +spinand-objs := core.o gigadevice.o macronix.o micron.o onfi.o paragon.o toshiba.o winbond.o obj-$(CONFIG_MTD_SPI_NAND) += spinand.o
drivers/mtd/nand/spi/core.c +12 -11
@@ -20,7 +20,7 @@#include <linux/spi/spi.h> #include <linux/spi/spi-mem.h> -static int spinand_read_reg_op (struct spinand_device *spinand,u8 reg,u8 *val) +int spinand_read_reg_op (struct spinand_device *spinand,u8 reg,u8 *val) {struct spi_mem_op op =SPINAND_GET_FEATURE_OP (reg, spinand->scratchbuf);
@@ -34,7 +34,7 @@ static int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val)return 0 ; } -static int spinand_write_reg_op (struct spinand_device *spinand,u8 reg,u8 val) +int spinand_write_reg_op (struct spinand_device *spinand,u8 reg,u8 val) {struct spi_mem_op op =SPINAND_SET_FEATURE_OP (reg, spinand->scratchbuf);
@@ -339,7 +339,7 @@ static void spinand_ondie_ecc_save_status(struct nand_device *nand, u8 status) engine_conf->status = status; } -static int spinand_write_enable_op (struct spinand_device *spinand) +int spinand_write_enable_op (struct spinand_device *spinand) {struct spi_mem_op op =SPINAND_WR_EN_DIS_OP (true);
@@ -496,10 +496,8 @@ static int spinand_erase_op(struct spinand_device *spinand,return spi_mem_exec_op (spinand->spimem, &op); } -static int spinand_wait (struct spinand_device *spinand, -unsigned long initial_delay_us, -unsigned long poll_delay_us, -u8 *s) +int spinand_wait (struct spinand_device *spinand,unsigned long initial_delay_us, +unsigned long poll_delay_us,u8 *s) {struct spi_mem_op op =SPINAND_GET_FEATURE_OP (REG_STATUS , spinand->scratchbuf);
@@ -1006,7 +1004,7 @@ static void spinand_manufacturer_cleanup(struct spinand_device *spinand)return spinand->manufacturer->ops->cleanup (spinand); } -static const struct spi_mem_op * +const struct spi_mem_op *spinand_select_op_variant (struct spinand_device *spinand,const struct spinand_op_variants *variants) {
@@ -1117,9 +1115,12 @@ static int spinand_detect(struct spinand_device *spinand) ret =spinand_id_detect (spinand);if (ret) { -dev_err (dev,"unknown raw ID %*phN\n" ,SPINAND_MAX_ID_LEN , - spinand->id.data); -return ret; + ret =spinand_onfi_detect (spinand); +if (ret) { +dev_err (dev,"unknown raw ID %*phN\n" , +SPINAND_MAX_ID_LEN , spinand->id.data); +return ret; + } }if (nand->memorg.ntargets >1 && !spinand->select_target) {
drivers/mtd/nand/spi/onfi.c new file +217 -0
217 lines not on this page · raw message
include/linux/mtd/spinand.h +50 -0
69 lines not on this page · raw message
Signature · 1 line
2.35.1
Quoted 1 line · On Fri, 15 Apr 2022 11:48:43 +0800 Chuanhong Guo <[email protected]> wrote:
+#define
SPINAND_IDR_ENBIT(6)
Looks like you're redefining CFG_OTP_ENABLE, why not use that definition directly?
Quoted 2 lines
drivers/mtd/nand/spi/onfi.c new file +2
-0+#define SPINAND_PARAM_PAGE 1 +#define ONFI_PARAM_PAGES 3
Review
Quoted 2 lines · On Fri, 15 Apr 2022 11:48:43 +0800 Chuanhong Guo <[email protected]> wrote:
(quoted diff) +2
-0+ +static const struct spinand_manufacturer *spinand_onfi_manufacturers[] = {};
Do we really need a separate manufacturer array? Looks like we could re-use the one we have in core.c and do the matching against it (we just need an extra NULL sentinel to detect the end of this array).
Quoted 32 lines
(quoted diff) +31
-0+ +static const struct spinand_onfi_info * +spinand_onfi_chip_match(struct nand_onfi_params *p, + const struct spinand_manufacturer *m) +{ + size_t i, j; + + for (i = 0; i < m->nchips; i++) + for (j = 0; m->onfi_chips[i].models[j]; j++) + if (!strcasecmp(m->onfi_chips[i].models[j], p->model)) + return &m->onfi_chips[i]; + return NULL; +} +/** + * struct spinand_onfi_info - Structure used to describe SPI NAND with ONFI + * parameter page + * @models: Model name array. Null terminated. + * @flags: OR-ing of the SPINAND_XXX flags + * @eccinfo: on-die ECC info + * @op_variants: operations variants + * @op_variants.read_cache: variants of the read-cache operation + * @op_variants.write_cache: variants of the write-cache operation + * @op_variants.update_cache: variants of the update-cache operation + * @select_target: function used to select a target/die. Required only for + * multi-die chips + * + * Each SPI NAND manufacturer driver should have a spinand_onfi_info table + * describing all the chips supported by the driver. + */ +struct spinand_onfi_info { + const char **const models;
Review
Quoted 10 lines
(quoted diff) +10
-0+ u32 flags; + struct spinand_ecc_info eccinfo; + struct { + const struct spinand_op_variants *read_cache; + const struct spinand_op_variants *write_cache; + const struct spinand_op_variants *update_cache; + } op_variants; + int (*select_target)(struct spinand_device *spinand, + unsigned int target); +};
Can't we just extend spinand_info instead of defining a new struct. AFAICT, the only difference is that model is replaced by a model array, and devid is dropped, and I think we can rework the existing ID-based matching logic to return ->models[0] instead of ->model.
Hi!
Quoted 1 line · Boris Brezillon
+#define
SPINAND_IDR_ENBIT(6)
Looks like you're redefining CFG_OTP_ENABLE, why not use that definition directly?
Oh. I didn't know that was available. I'll use it. (IDR_EN comes from a toshiba datasheet if I remember correctly.)
Quoted 10 lines
Review
Quoted 2 lines · On Fri, 15 Apr 2022 11:48:43 +0800 Chuanhong Guo <[email protected]> wrote:
(quoted diff) +2
-0+ +static const struct spinand_manufacturer *spinand_onfi_manufacturers[] = {};Do we really need a separate manufacturer array? Looks like we could re-use the one we have in
core.cand do the matching against it (we just need an extra NULL sentinel to detect the end of this array).
I'll do it.
I'll also add a onfi_nchips to spinand_manufacturer to share the same manufacturer entry between two types of ID list.
Quoted 37 lines
Quoted 37 lines
(quoted diff) +35
-0+ +static const struct spinand_onfi_info * +spinand_onfi_chip_match(struct nand_onfi_params *p, + const struct spinand_manufacturer *m) +{ + size_t i, j; + + for (i = 0; i < m->nchips; i++) + for (j = 0; m->onfi_chips[i].models[j]; j++) + if (!strcasecmp(m->onfi_chips[i].models[j], p->model)) + return &m->onfi_chips[i]; + return NULL; +} +/** + * struct spinand_onfi_info - Structure used to describe SPI NAND with ONFI + * parameter page + * @models: Model name array. Null terminated. + * @flags: OR-ing of the SPINAND_XXX flags + * @eccinfo: on-die ECC info + * @op_variants: operations variants + * @op_variants.read_cache: variants of the read-cache operation + * @op_variants.write_cache: variants of the write-cache operation + * @op_variants.update_cache: variants of the update-cache operation + * @select_target: function used to select a target/die. Required only for + * multi-die chips + * + * Each SPI NAND manufacturer driver should have a spinand_onfi_info table + * describing all the chips supported by the driver. + */ +struct spinand_onfi_info { + const char **const models; + u32 flags; + struct spinand_ecc_info eccinfo; + struct { + const struct spinand_op_variants *read_cache;
Quoted 8 lines
include/linux/mtd/spinand.h struct spinand_info { · line 417
Quoted 6 lines
include/linux/mtd/spinand.h +6
-0+const struct spinand_op_variants *write_cache; +const struct spinand_op_variants *update_cache; + } op_variants; +int (*select_target)(struct spinand_device *spinand, +unsigned int target); +};Can't we just extend
spinand_infoinstead of defining a new struct.
Yeah, that's better. I'll do it.
Quoted 3 lines
AFAICT, the only difference is that model is replaced by a model array, and devid is dropped, and I think we can rework the existing ID-based matching logic to return ->models[0] instead of ->model.
In fact we don't even use this model string anywhere at the moment.
Signature · 2 lines
Regards, Chuanhong Guo
PATCH v2 3/3 mtd: spinand: probe Winbond W25N01GV/W using param page
In reply to Chuanhong GuoThe JEDEC ID of EFAA21 is assigned to both W25N01G and W25N01K. Probing the chip with JEDEC ID isn't reliable anymore. Use parameter page instead.
---
Change since v1: none
drivers/mtd/nand/spi/onfi.c | 4 +++- drivers/mtd/nand/spi/winbond.c | 25 ++++++++++++++++--------- include/linux/mtd/spinand.h | 3 +++ 3 files changed, 22 insertions(+), 10 deletions(-)
drivers/mtd/nand/spi/onfi.c +3 -1
@@ -80,7 +80,9 @@ static bool spinand_onfi_validate(const struct nand_onfi_params *p)return crc ==le16_to_cpu (p->crc); } -static const struct spinand_manufacturer *spinand_onfi_manufacturers[] = {}; +static const struct spinand_manufacturer *spinand_onfi_manufacturers[] = { + &winbond_onfi_spinand_manufacturer, +};static const struct spinand_onfi_info * spinand_onfi_chip_match(struct nand_onfi_params *p,
drivers/mtd/nand/spi/winbond.c +16 -9
@@ -85,15 +85,15 @@ static const struct spinand_info winbond_spinand_table[] = {0 ,SPINAND_ECCINFO (&w25m02gv_ooblayout,NULL ), SPINAND_SELECT_TARGET(w25m02gv_select_target)), -SPINAND_INFO ("W25N01GV" , -SPINAND_ID (SPINAND_READID_METHOD_OPCODE_DUMMY ,0xaa ), -NAND_MEMORG (1 ,2048 ,64 ,64 ,1024 ,20 ,1 ,1 ,1 ), -NAND_ECCREQ (1 ,512 ), -SPINAND_INFO_OP_VARIANTS (&read_cache_variants, - &write_cache_variants, - &update_cache_variants), -0 , - SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL)), +}; + +static const struct spinand_onfi_info winbond_spinand_onfi_table[] = { +SPINAND_ONFI_INFO (SPINAND_ONFI_MODELS ("W25N01GV" ,"W25N01GW" ), +SPINAND_INFO_OP_VARIANTS (&read_cache_variants, + &write_cache_variants, + &update_cache_variants), +0 , +SPINAND_ECCINFO (&w25m02gv_ooblayout,NULL )), };static int winbond_spinand_init (struct spinand_device *spinand)
@@ -125,3 +125,10 @@ const struct spinand_manufacturer winbond_spinand_manufacturer = { .nchips = ARRAY_SIZE(winbond_spinand_table), .ops = &winbond_spinand_manuf_ops, }; + +const struct spinand_manufacturer winbond_onfi_spinand_manufacturer = { + .name ="Winbond" , + .onfi_chips = winbond_spinand_onfi_table, + .nchips =ARRAY_SIZE (winbond_spinand_onfi_table), + .ops = &winbond_spinand_manuf_ops, +};
include/linux/mtd/spinand.h +3 -0
@@ -268,6 +268,9 @@ extern const struct spinand_manufacturer paragon_spinand_manufacturer;extern const struct spinand_manufacturer toshiba_spinand_manufacturer;extern const struct spinand_manufacturer winbond_spinand_manufacturer; +/* SPI NAND manufacturers with ONFI parameter page support */ +extern const struct spinand_manufacturer winbond_onfi_spinand_manufacturer; +/** * struct spinand_op_variants - SPI NAND operation variants * @ops: the list of variants for a given operation
Signature · 1 line
2.35.1
Re: [PATCH v2 3/3] mtd: spinand: probe Winbond W25N01GV/W using param page
Hi!
Quoted 4 lines · Chuanhong Guo
The JEDEC ID of EFAA21 is assigned to both W25N01G and W25N01K. Probing the chip with JEDEC ID isn't reliable anymore. Use parameter page instead.
I just found an old W25N01GV with no valid parameter page. I need to rethink how to deal with this crap then. This patchset is abandoned.
Signature · 2 lines
Regards, Chuanhong Guo