Skip to contentSkip to conversation

mtd: spi-nand: Add support for randomizer feature

PATCH Applied Cheng Ming Lin 3 participants 6 messages May 5 – May 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v10 cover mtd: spi-nand: Add support for randomizer feature

From: Cheng Ming Lin <[email protected]>

This patch series introduces randomizer support for SPI NAND devices.

- Patch 1: add the nand-randomizer property to the generic nand-chip.yaml bindings. Instead of using boolean properties, it introduces a uint32 enum (0 or 1). If the property is omitted, the driver will not interfere and will leave the randomizer in its current state.
- Patch 2: add the initialization logic and the set_randomizer callback to the core framework. The core will now parse the device tree properties and enable or disable the randomizer accordingly during spinand_init.
- Patch 3: implement the set_randomizer callback specifically for Macronix chips (MX35LF/UF series) to handle the vendor-specific register operations.

v10:
* mtd: spi-nand: Add support for randomizer: set_randomizer call now passes the rand_val directly.

v9:
* Replaced 'nand-enable-randomizer'/'nand-disable-randomizer' boolean properties with a single 'nand-randomizer' uint32 enum.
* Switched to use of_property_read_u32() to align with the new binding.

v8:
* dt-bindings: spi-nand: macronix: Remove the chip-specific Application Note link from the commit message to keep the binding generic.
* mtd: spi-nand: macronix: Update the broken Application Note link and move it here from the dt-binding patch per Miquel's suggestion.

v7:
* Directly return the result in macronix_set_randomizer()

v6:
* Added mutual-exclusive constraints using not with required for nand-randomizer-enable and nand-randomizer-disable properties based on Krzysztof's feedback.
* Simplified the return path in macronix_set_randomizer() to directly return ret; and dropped unrelated formatting changes.

v5:
* Promoted the randomizer configuration to use generic NAND properties (nand-randomizer-enable and nand-randomizer-disable) instead of vendor-specific bindings.
* Refactored the initialization architecture in core.c. The core framework is now responsible for parsing the device tree properties and deciding whether to enable or disable the randomizer.

v4:
* Fix a build error in spinand_randomizer_init() where a value was returned from a void function. (Reported by kernel test robot)
* Update the return type to int.

v3:
* Revert the device tree property to the vendor-specific "mxic,randomizer-enable" to strictly follow vendor-specific bindings.

* Update the 'set_randomizer' callback signature to accept a boolean 'enable' argument, allowing the feature to be explicitly enabled or disabled.

* Switch the implementation to use the standard SET_FEATURE command to modify the Configuration Register (0x10), replacing the previous special program command method.

v2:
* Create a global NAND DT property

Cheng Ming Lin (3):
dt-bindings: mtd: nand: Add nand-randomizer property
mtd: spi-nand: Add support for randomizer
mtd: spi-nand: macronix: Enable randomizer support

 .../devicetree/bindings/mtd/nand-chip.yaml    |  9 +++++
 drivers/mtd/nand/spi/core.c                   | 20 ++++++++++
 drivers/mtd/nand/spi/macronix.c               | 38 ++++++++++++++-----
 include/linux/mtd/spinand.h                   |  9 +++++
 4 files changed, 66 insertions(+), 10 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v10 1/3 dt-bindings: mtd: nand: Add nand-randomizer property

From: Cheng Ming Lin <[email protected]>

Add the 'nand-randomizer' property to control the data randomizer feature. This is used to improve data reliability by reducing cell-to-cell interference.

Depending on the hardware architecture, this property is designed to be generic and can apply to either the NAND chip's internal randomizer or the hardware randomizer engine embedded in the NAND host controller.

This property is defined as a uint32 enum (0 or 1) instead of a simple boolean. This design choice explicitly supports the "not present" case. If the property is omitted, the driver will not interfere and will leave the randomizer in its current state (e.g., as already configured by the bootloader or hardware default).

---
 Documentation/devicetree/bindings/mtd/nand-chip.yaml | 9 +++++++++
 1 file changed, 9 insertions(+)
Documentation/devicetree/bindings/mtd/nand-chip.yaml +9 -0
index 8800d1d07..effdc4f99 100644
@@ -23,6 +23,15 @@ properties:
     description:
       Contains the chip-select IDs.
 
+  nand-randomizer:
+    description: |
+      Control the data randomizer feature.
+      0: Disable randomizer.
+      1: Enable randomizer.
+      If absent, the current hardware state is left unchanged.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    enum: [0, 1]
+
 required:
   - reg
 
Signature · 1 line
2.25.1
maintainer Reviewed-by on v10 1/3
#2 lore raw

Re: [PATCH v10 1/3] dt-bindings: mtd: nand: Add nand-randomizer property

Quoted 16 lines · Cheng Ming Lin

From: Cheng Ming Lin <[email protected]>

Add the 'nand-randomizer' property to control the data randomizer feature. This is used to improve data reliability by reducing cell-to-cell interference.

Depending on the hardware architecture, this property is designed to be generic and can apply to either the NAND chip's internal randomizer or the hardware randomizer engine embedded in the NAND host controller.

This property is defined as a uint32 enum (0 or 1) instead of a simple boolean. This design choice explicitly supports the "not present" case. If the property is omitted, the driver will not interfere and will leave the randomizer in its current state (e.g., as already configured by the bootloader or hardware default).

Quoted 5 lines
---
 Documentation/devicetree/bindings/mtd/nand-chip.yaml | 9 +++++++++
 1 file changed, 9 insertions(+)
#3 lore raw

PATCH v10 2/3 mtd: spi-nand: Add support for randomizer

In reply to Cheng Ming Lin

From: Cheng Ming Lin <[email protected]>

This patch adds support for the randomizer feature.

It introduces a 'set_randomizer' callback in 'struct spinand_info' and 'struct spinand_device'.

If a driver implements this callback, the core will invoke it during device initialization (spinand_init) to enable or disable the randomizer feature based on the device tree configuration.

---
 drivers/mtd/nand/spi/core.c | 20 ++++++++++++++++++++
 include/linux/mtd/spinand.h |  9 +++++++++
 2 files changed, 29 insertions(+)
drivers/mtd/nand/spi/core.c +20 -0
index 8aa3753aa..78557e48f 100644
@@ -1307,6 +1307,22 @@ static int spinand_create_dirmaps(struct spinand_device *spinand)
 	return 0;
 }
 
+static int spinand_randomizer_init(struct spinand_device *spinand)
+{
+	struct device_node *np = spinand->spimem->spi->dev.of_node;
+	u32 rand_val;
+	int ret;
+
+	if (!spinand->set_randomizer)
+		return 0;
+
+	ret = of_property_read_u32(np, "nand-randomizer", &rand_val);
+	if (ret)
+		return 0;
+
+	return spinand->set_randomizer(spinand, rand_val);
+}
+
 static const struct nand_ops spinand_ops = {
 	.erase = spinand_erase,
 	.markbad = spinand_markbad,
@@ -1594,6 +1610,7 @@ int spinand_match_and_init(struct spinand_device *spinand,
 		spinand->user_otp = &table[i].user_otp;
 		spinand->read_retries = table[i].read_retries;
 		spinand->set_read_retry = table[i].set_read_retry;
+		spinand->set_randomizer = table[i].set_randomizer;
 
 		/* I/O variants selection with single-spi SDR commands */
 
@@ -1881,6 +1898,9 @@ static int spinand_init(struct spinand_device *spinand)
 	 * ECC initialization must have happened previously.
 	 */
 	spinand_cont_read_init(spinand);
+	ret = spinand_randomizer_init(spinand);
+	if (ret)
+		goto err_cleanup_nanddev;
 
 	mtd->_read_oob = spinand_mtd_read;
 	mtd->_write_oob = spinand_mtd_write;
include/linux/mtd/spinand.h +9 -0
index 58abd306e..9ec45b727 100644
@@ -585,6 +585,7 @@ enum spinand_bus_interface {
  * @user_otp: SPI NAND user OTP info.
  * @read_retries: the number of read retry modes supported
  * @set_read_retry: enable/disable read retry for data recovery
+ * @set_randomizer: enable/disable randomizer support
  *
  * Each SPI NAND manufacturer driver should have a spinand_info table
  * describing all the chips supported by the driver.
@@ -613,6 +614,8 @@ struct spinand_info {
 	unsigned int read_retries;
 	int (*set_read_retry)(struct spinand_device *spinand,
 			     unsigned int read_retry);
+	int (*set_randomizer)(struct spinand_device *spinand,
+			      bool enable);
 };
 
 #define SPINAND_ID(__method, ...)					\
@@ -669,6 +672,9 @@ struct spinand_info {
 	.read_retries = __read_retries,					\
 	.set_read_retry = __set_read_retry
 
+#define SPINAND_RANDOMIZER(__set_randomizer)				\
+	.set_randomizer = __set_randomizer
+
 #define SPINAND_INFO(__model, __id, __memorg, __eccreq, __op_variants,	\
 		     __flags, ...)					\
 	{								\
@@ -754,6 +760,7 @@ struct spinand_mem_ops {
  * @user_otp: SPI NAND user OTP info.
  * @read_retries: the number of read retry modes supported
  * @set_read_retry: Enable/disable the read retry feature
+ * @set_randomizer: Enable/disable the randomizer feature
  */
 struct spinand_device {
 	struct nand_device base;
@@ -787,6 +794,8 @@ struct spinand_device {
 	bool cont_read_possible;
 	int (*set_cont_read)(struct spinand_device *spinand,
 			     bool enable);
+	int (*set_randomizer)(struct spinand_device *spinand,
+			      bool enable);
 
 	const struct spinand_fact_otp *fact_otp;
 	const struct spinand_user_otp *user_otp;
Signature · 1 line
2.25.1
#4 lore raw

PATCH v10 3/3 mtd: spi-nand: macronix: Enable randomizer support

In reply to Cheng Ming Lin

From: Cheng Ming Lin <[email protected]>

Implement the 'set_randomizer' callback for Macronix SPI NAND chips. The randomizer is enabled by setting bit 1 of the Configuration Register (address 0x10).

This patch adds support for the following chips:
- MX35LFxG24AD series
- MX35UFxG24AD series

When the randomizer is enabled, data is scrambled internally during program operations and automatically descrambled during read operations. This helps reduce bit errors caused by program disturbance.

Please refer to the following link for randomizer feature:

---
 drivers/mtd/nand/spi/macronix.c | 38 ++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 10 deletions(-)
drivers/mtd/nand/spi/macronix.c +28 -10
index 67cafa1bb..7dfcc34e9 100644
@@ -14,6 +14,8 @@
 #define MACRONIX_ECCSR_BF_LAST_PAGE(eccsr) FIELD_GET(GENMASK(3, 0), eccsr)
 #define MACRONIX_ECCSR_BF_ACCUMULATED_PAGES(eccsr) FIELD_GET(GENMASK(7, 4), eccsr)
 #define MACRONIX_CFG_CONT_READ         BIT(2)
+#define MACRONIX_CFG_RANDOMIZER_EN     BIT(1)
+#define MACRONIX_FEATURE_ADDR_RANDOMIZER 0x10
 #define MACRONIX_FEATURE_ADDR_READ_RETRY 0x70
 #define MACRONIX_NUM_READ_RETRY_MODES 5
 
@@ -170,6 +172,12 @@ static int macronix_set_read_retry(struct spinand_device *spinand,
 	return spi_mem_exec_op(spinand->spimem, &op);
 }
 
+static int macronix_set_randomizer(struct spinand_device *spinand, bool enable)
+{
+	return spinand_write_reg_op(spinand, MACRONIX_FEATURE_ADDR_RANDOMIZER,
+				    enable ? MACRONIX_CFG_RANDOMIZER_EN : 0);
+}
+
 static const struct spinand_info macronix_spinand_table[] = {
 	SPINAND_INFO("MX35LF1GE4AB",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x12),
@@ -231,7 +239,8 @@ static const struct spinand_info macronix_spinand_table[] = {
 		     SPINAND_HAS_QE_BIT,
 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL),
 		     SPINAND_READ_RETRY(MACRONIX_NUM_READ_RETRY_MODES,
-					macronix_set_read_retry)),
+					macronix_set_read_retry),
+		     SPINAND_RANDOMIZER(macronix_set_randomizer)),
 	SPINAND_INFO("MX35LF2G24AD",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x24, 0x03),
 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 2, 1, 1),
@@ -243,7 +252,8 @@ static const struct spinand_info macronix_spinand_table[] = {
 		     SPINAND_HAS_PROG_PLANE_SELECT_BIT,
 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL),
 		     SPINAND_READ_RETRY(MACRONIX_NUM_READ_RETRY_MODES,
-					macronix_set_read_retry)),
+					macronix_set_read_retry),
+		     SPINAND_RANDOMIZER(macronix_set_randomizer)),
 	SPINAND_INFO("MX35LF2G24AD-Z4I8",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x64, 0x03),
 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
@@ -254,7 +264,8 @@ static const struct spinand_info macronix_spinand_table[] = {
 		     SPINAND_HAS_QE_BIT,
 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL),
 		     SPINAND_READ_RETRY(MACRONIX_NUM_READ_RETRY_MODES,
-					macronix_set_read_retry)),
+					macronix_set_read_retry),
+		     SPINAND_RANDOMIZER(macronix_set_randomizer)),
 	SPINAND_INFO("MX35LF4G24AD",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x35, 0x03),
 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 2, 1, 1),
@@ -266,7 +277,8 @@ static const struct spinand_info macronix_spinand_table[] = {
 		     SPINAND_HAS_PROG_PLANE_SELECT_BIT,
 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL),
 		     SPINAND_READ_RETRY(MACRONIX_NUM_READ_RETRY_MODES,
-					macronix_set_read_retry)),
+					macronix_set_read_retry),
+		     SPINAND_RANDOMIZER(macronix_set_randomizer)),
 	SPINAND_INFO("MX35LF4G24AD-Z4I8",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x75, 0x03),
 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
@@ -277,7 +289,8 @@ static const struct spinand_info macronix_spinand_table[] = {
 		     SPINAND_HAS_QE_BIT,
 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL),
 		     SPINAND_READ_RETRY(MACRONIX_NUM_READ_RETRY_MODES,
-					macronix_set_read_retry)),
+					macronix_set_read_retry),
+		     SPINAND_RANDOMIZER(macronix_set_randomizer)),
 	SPINAND_INFO("MX31LF1GE4BC",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x1e),
 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
@@ -327,7 +340,8 @@ static const struct spinand_info macronix_spinand_table[] = {
 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
 				     macronix_ecc_get_status),
 		     SPINAND_READ_RETRY(MACRONIX_NUM_READ_RETRY_MODES,
-					macronix_set_read_retry)),
+					macronix_set_read_retry),
+		     SPINAND_RANDOMIZER(macronix_set_randomizer)),
 	SPINAND_INFO("MX35UF4G24AD-Z4I8",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xf5, 0x03),
 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
@@ -340,7 +354,8 @@ static const struct spinand_info macronix_spinand_table[] = {
 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
 				     macronix_ecc_get_status),
 		     SPINAND_READ_RETRY(MACRONIX_NUM_READ_RETRY_MODES,
-					macronix_set_read_retry)),
+					macronix_set_read_retry),
+		     SPINAND_RANDOMIZER(macronix_set_randomizer)),
 	SPINAND_INFO("MX35UF4GE4AD",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xb7, 0x03),
 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
@@ -381,7 +396,8 @@ static const struct spinand_info macronix_spinand_table[] = {
 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
 				     macronix_ecc_get_status),
 		     SPINAND_READ_RETRY(MACRONIX_NUM_READ_RETRY_MODES,
-					macronix_set_read_retry)),
+					macronix_set_read_retry),
+		     SPINAND_RANDOMIZER(macronix_set_randomizer)),
 	SPINAND_INFO("MX35UF2G24AD-Z4I8",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xe4, 0x03),
 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
@@ -394,7 +410,8 @@ static const struct spinand_info macronix_spinand_table[] = {
 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
 				     macronix_ecc_get_status),
 		     SPINAND_READ_RETRY(MACRONIX_NUM_READ_RETRY_MODES,
-					macronix_set_read_retry)),
+					macronix_set_read_retry),
+		     SPINAND_RANDOMIZER(macronix_set_randomizer)),
 	SPINAND_INFO("MX35UF2GE4AD",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xa6, 0x03),
 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
@@ -444,7 +461,8 @@ static const struct spinand_info macronix_spinand_table[] = {
 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
 				     macronix_ecc_get_status),
 		     SPINAND_READ_RETRY(MACRONIX_NUM_READ_RETRY_MODES,
-					macronix_set_read_retry)),
+					macronix_set_read_retry),
+		     SPINAND_RANDOMIZER(macronix_set_randomizer)),
 	SPINAND_INFO("MX35UF1GE4AD",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x96, 0x03),
 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
Signature · 1 line
2.25.1
maintainer Applied
#5 lore raw
In reply to Cheng Ming Lin
Quoted 17 lines · Cheng Ming Lin

From: Cheng Ming Lin <[email protected]>

This patch series introduces randomizer support for SPI NAND devices.

- Patch 1: add the nand-randomizer property to the generic nand-chip.yaml bindings. Instead of using boolean properties, it introduces a uint32 enum (0 or 1). If the property is omitted, the driver will not interfere and will leave the randomizer in its current state.
- Patch 2: add the initialization logic and the set_randomizer callback to the core framework. The core will now parse the device tree properties and enable or disable the randomizer accordingly during spinand_init.
- Patch 3: implement the set_randomizer callback specifically for Macronix chips (MX35LF/UF series) to handle the vendor-specific register operations.

Quoted 1 line

[...]

Applied to nand/next, thanks!

[1/3] dt-bindings: mtd: nand: Add nand-randomizer property

      commit: cf496ebf1380bde700c5d1790c31919eea2f4851
[2/3] mtd: spi-nand: Add support for randomizer
      commit: 874aa8e4b8307cd922979c20dc78df26c05f3c28
[3/3] mtd: spi-nand: macronix: Enable randomizer support
      commit: 474ef6c5196a2d923c75cb318aa83ba790bd381f

Patche(s) should be available on mtd/linux.git and will be part of the next PR (provided that no robot complains by then).

Kind regards,
Miquèl