Skip to contentSkip to conversation

mtd: spi-nand: Add support for randomizer feature

PATCH Superseded Cheng Ming Lin 2 participants 6 messages Feb 3 – Feb 4

Subsystems (2)

v4 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v4 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 adds the hook and initialization logic to the core framework. Patch 2 implements the randomizer setting specifically for Macronix chips (MX35LF/UF series) and allows control via device tree.

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 (2):
mtd: spi-nand: Add support for randomizer
mtd: spi-nand: macronix: Enable randomizer support

 drivers/mtd/nand/spi/core.c     | 23 ++++++++++++++++
 drivers/mtd/nand/spi/macronix.c | 47 ++++++++++++++++++++++++++-------
 include/linux/mtd/spinand.h     |  9 +++++++
 3 files changed, 69 insertions(+), 10 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v4 1/2 mtd: spi-nand: Add support for randomizer

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 call it during device initialization (spinand_init) to enable the randomizer.

---
 drivers/mtd/nand/spi/core.c | 23 +++++++++++++++++++++++
 include/linux/mtd/spinand.h |  9 +++++++++
 2 files changed, 32 insertions(+)
drivers/mtd/nand/spi/core.c +23 -0
index d207286572d8..4f8ff94f5ccc 100644
@@ -206,6 +206,12 @@ static int spinand_cont_read_enable(struct spinand_device *spinand,
 	return spinand->set_cont_read(spinand, enable);
 }
 
+static int spinand_randomizer_enable(struct spinand_device *spinand,
+				     bool enable)
+{
+	return spinand->set_randomizer(spinand, enable);
+}
+
 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
 {
 	struct nand_device *nand = spinand_to_nand(spinand);
@@ -1218,6 +1224,19 @@ static int spinand_create_dirmaps(struct spinand_device *spinand)
 	return 0;
 }
 
+static int spinand_randomizer_init(struct spinand_device *spinand)
+{
+	int ret;
+
+	if (spinand->set_randomizer) {
+		ret = spinand_randomizer_enable(spinand, true);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static const struct nand_ops spinand_ops = {
 	.erase = spinand_erase,
 	.markbad = spinand_markbad,
@@ -1412,6 +1431,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;
 
 		op = spinand_select_op_variant(spinand,
 					       info->op_variants.read_cache);
@@ -1588,6 +1608,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 ce76f5c632e1..e01315a71222 100644
@@ -501,6 +501,7 @@ struct spinand_user_otp {
  * @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.
@@ -527,6 +528,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, ...)					\
@@ -580,6 +583,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, ...)					\
 	{								\
@@ -635,6 +641,7 @@ struct spinand_dirmap {
  * @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;
@@ -668,6 +675,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
maintainer
#2 lore raw

Re: [PATCH v4 1/2] mtd: spi-nand: Add support for randomizer

Hello Cheng Ming,

Quoted 9 lines · 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 call it during device initialization (spinand_init) to enable the randomizer.

I recently had a request regarding the sunxi controller driver randomizer. I would like to get back to having a nand wide randomizer DT property, because a Macronix specific value honestly no longer makes sense. Randomizers have been there for ages, they are not so often used because scrambling data is not convenient for developers but they may have a real interest depending on the NAND chip or the external conditions.

We will need two mutually exclusive optional properties in nand-chip.yaml:
nand-enable-randomizer;
nand-disable-randomizer;

BTW, where is the patch adding the property to the bindings? Did we already merge it? I do not find it anymore.

Looking at the current implementation, I really don't like the core always calling in the "init" and the manufacturer driver reading the DT and deciding whether or not it enables it.

Quoted 11 lines
---
 drivers/mtd/nand/spi/core.c | 23 +++++++++++++++++++++++
 include/linux/mtd/spinand.h |  9 +++++++++
 2 files changed, 32 insertions(+)
drivers/mtd/nand/spi/core.c +0 -0
index d207286572d8..4f8ff94f5ccc 100644

drivers/mtd/nand/spi/core.c static int spinand_cont_read_enable(struct spinand_device *spinand, · line 212

Quoted 7 lines
drivers/mtd/nand/spi/core.c +4 -0
@@ -206,6 +206,12 @@ static int spinand_cont_read_enable(struct spinand_device *spinand,
 	return spinand->set_cont_read(spinand, enable);
 }
 
+static int spinand_randomizer_enable(struct spinand_device *spinand,
+				     bool enable)
+{
+	return spinand->set_randomizer(spinand, enable);

This helper seems mostly useless.

Quoted 6 lines
drivers/mtd/nand/spi/core.c +2 -0
+}
+
 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
 {
 	struct nand_device *nand = spinand_to_nand(spinand);

drivers/mtd/nand/spi/core.c static int spinand_create_dirmaps(struct spinand_device *spinand) · line 1232

Quoted 9 lines
drivers/mtd/nand/spi/core.c +6 -0
@@ -1218,6 +1224,19 @@ static int spinand_create_dirmaps(struct spinand_device *spinand)
 	return 0;
 }
 
+static int spinand_randomizer_init(struct spinand_device *spinand)
+{
+	int ret;
+
+	if (spinand->set_randomizer) {
+		ret = spinand_randomizer_enable(spinand, true);

Please directly call the hook.

Given the previous feedback, you should change "true" to be based on the presence of the DT property.

drivers/mtd/nand/spi/core.c static int spinand_create_dirmaps(struct spinand_device *spinand) · line 1238

Quoted 6 lines
drivers/mtd/nand/spi/core.c +6 -0
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}

Rest LGTM.

Thanks,
Miquèl

#3 lore raw

Re: [PATCH v4 1/2] mtd: spi-nand: Add support for randomizer

Hi Miquel,

Miquel Raynal <[email protected]> 於 2026年2月3日週二 下午10:40寫道:

Quoted 23 lines

Hello Cheng Ming,

Quoted 9 lines · 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 call it during device initialization (spinand_init) to enable the randomizer.

I recently had a request regarding the sunxi controller driver randomizer. I would like to get back to having a nand wide randomizer DT property, because a Macronix specific value honestly no longer makes sense. Randomizers have been there for ages, they are not so often used because scrambling data is not convenient for developers but they may have a real interest depending on the NAND chip or the external conditions.

Quoted 4 lines

We will need two mutually exclusive optional properties in nand-chip.yaml:
nand-enable-randomizer;
nand-disable-randomizer;

Agreed. It makes sense to switch to generic device tree properties as you suggested.

I will introduce nand-enable-randomizer and nand-disable-randomizer in nand-chip.yaml.

In addition, I will also update the Macronix raw nand flash driver to use these generic randomizer properties for consistency.

Quoted 3 lines

BTW, where is the patch adding the property to the bindings? Did we already merge it? I do not find it anymore.

It has not been merged yet. Since the current patch series uses the vendor-specific property "mxic,randomizer-enable", I did not include a patch to update the generic bindings.

I will include a new patch in v5 to add these properties to nand-chip.yaml.

Quoted 4 lines

Looking at the current implementation, I really don't like the core always calling in the "init" and the manufacturer driver reading the DT and deciding whether or not it enables it.

I agree. I will refactor the logic in v5 so that the core parses the generic DT properties (nand-enable-randomizer and nand-disable-randomizer).

Then, spinand_randomizer_init will determine the state and pass a boolean argument to the set_randomizer callback, leaving the driver to strictly handle the hardware configuration.

Quoted 52 lines
Quoted 12 lines
---
 drivers/mtd/nand/spi/core.c | 23 +++++++++++++++++++++++
 include/linux/mtd/spinand.h |  9 +++++++++
 2 files changed, 32 insertions(+)
drivers/mtd/nand/spi/core.c +0 -0
index d207286572d8..4f8ff94f5ccc 100644

drivers/mtd/nand/spi/core.c static int spinand_cont_read_enable(struct spinand_device *spinand, · line 212

Quoted 7 lines
drivers/mtd/nand/spi/core.c +4 -0
@@ -206,6 +206,12 @@ static int spinand_cont_read_enable(struct spinand_device *spinand,
      return spinand->set_cont_read(spinand, enable);
 }
 
+static int spinand_randomizer_enable(struct spinand_device *spinand,
+                                  bool enable)
+{
+     return spinand->set_randomizer(spinand, enable);

This helper seems mostly useless.

Quoted 6 lines
drivers/mtd/nand/spi/core.c +2 -0
+}
+
 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
 {
      struct nand_device *nand = spinand_to_nand(spinand);

drivers/mtd/nand/spi/core.c static int spinand_create_dirmaps(struct spinand_device *spinand) · line 1232

Quoted 9 lines
drivers/mtd/nand/spi/core.c +6 -0
@@ -1218,6 +1224,19 @@ static int spinand_create_dirmaps(struct spinand_device *spinand)
      return 0;
 }
 
+static int spinand_randomizer_init(struct spinand_device *spinand)
+{
+     int ret;
+
+     if (spinand->set_randomizer) {
+             ret = spinand_randomizer_enable(spinand, true);

Please directly call the hook.

Given the previous feedback, you should change "true" to be based on the presence of the DT property.

drivers/mtd/nand/spi/core.c static int spinand_create_dirmaps(struct spinand_device *spinand) · line 1238

Quoted 6 lines
drivers/mtd/nand/spi/core.c +6 -0
+             if (ret)
+                     return ret;
+     }
+
+     return 0;
+}

Rest LGTM.

Quoted 2 lines

Thanks,
Miquèl

Thanks,
Cheng Ming Lin

maintainer
#4 lore raw

Re: [PATCH v4 1/2] mtd: spi-nand: Add support for randomizer

Hi Cheng Ming,

Quoted 11 lines
Quoted 3 lines

Looking at the current implementation, I really don't like the core always calling in the "init" and the manufacturer driver reading the DT and deciding whether or not it enables it.

I agree. I will refactor the logic in v5 so that the core parses the generic DT properties (nand-enable-randomizer and nand-disable-randomizer).

Then, spinand_randomizer_init will determine the state and pass a boolean argument to the set_randomizer callback, leaving the driver to strictly handle the hardware configuration.

Perfect.

Thanks,
Miquèl

#5 lore raw

PATCH v4 2/2 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. This feature is controlled via the "mxic,randomizer-enable" device tree property.

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.

---
 drivers/mtd/nand/spi/macronix.c | 47 ++++++++++++++++++++++++++-------
 1 file changed, 37 insertions(+), 10 deletions(-)
drivers/mtd/nand/spi/macronix.c +37 -10
index edf63b9996cf..0212d44b1a3f 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
 
@@ -155,6 +157,21 @@ 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)
+{
+	struct device_node *np = spinand->spimem->spi->dev.of_node;
+	int ret;
+
+	if (of_property_read_bool(np, "mxic,randomizer-enable")) {
+		ret = spinand_write_reg_op(spinand, MACRONIX_FEATURE_ADDR_RANDOMIZER,
+					   enable ? MACRONIX_CFG_RANDOMIZER_EN : 0);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static const struct spinand_info macronix_spinand_table[] = {
 	SPINAND_INFO("MX35LF1GE4AB",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x12),
@@ -213,7 +230,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),
@@ -225,7 +243,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),
@@ -236,7 +255,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),
@@ -248,7 +268,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),
@@ -259,7 +280,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),
@@ -305,7 +327,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),
@@ -317,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("MX35UF4GE4AD",
 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xb7, 0x03),
 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
@@ -355,7 +379,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),
@@ -367,7 +392,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),
@@ -413,7 +439,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