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

From: Cheng Ming Lin
Date: Fri Aug 08 2025 - 05:57:22 EST


From: Cheng Ming Lin <chengminglin@xxxxxxxxxxx>

Add randomizer operation support for user data scrambling.

For more high-reliability concern, if subpage write not available with
hardware ECC and then to enable randomizer is recommended by default.

Signed-off-by: Cheng Ming Lin <chengminglin@xxxxxxxxxxx>
---
drivers/mtd/nand/spi/core.c | 18 ++++++++++++++++++
include/linux/mtd/spinand.h | 7 +++++++
2 files changed, 25 insertions(+)

diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c
index b0898990b2a5..43ed5de282c5 100644
--- a/drivers/mtd/nand/spi/core.c
+++ b/drivers/mtd/nand/spi/core.c
@@ -854,6 +854,19 @@ static void spinand_cont_read_init(struct spinand_device *spinand)
}
}

+static int spinand_randomizer_init(struct spinand_device *spinand)
+{
+ int ret;
+
+ if (spinand->set_randomizer) {
+ ret = spinand->set_randomizer(spinand);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
static bool spinand_use_cont_read(struct mtd_info *mtd, loff_t from,
struct mtd_oob_ops *ops)
{
@@ -1366,6 +1379,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);
@@ -1543,6 +1557,10 @@ static int spinand_init(struct spinand_device *spinand)
*/
spinand_cont_read_init(spinand);

+ ret = spinand_randomizer_init(spinand);
+ if (ret)
+ goto err_cleanup_ecc_engine;
+
mtd->_read_oob = spinand_mtd_read;
mtd->_write_oob = spinand_mtd_write;
mtd->_block_isbad = spinand_mtd_block_isbad;
diff --git a/include/linux/mtd/spinand.h b/include/linux/mtd/spinand.h
index 27a45bdab7ec..80a5c6ac6cc5 100644
--- a/include/linux/mtd/spinand.h
+++ b/include/linux/mtd/spinand.h
@@ -499,6 +499,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 randomizer
*
* Each SPI NAND manufacturer driver should have a spinand_info table
* describing all the chips supported by the driver.
@@ -525,6 +526,7 @@ 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);
};

#define SPINAND_ID(__method, ...) \
@@ -578,6 +580,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, ...) \
{ \
@@ -633,6 +638,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 the randomizer feature
*/
struct spinand_device {
struct nand_device base;
@@ -673,6 +679,7 @@ struct spinand_device {
unsigned int read_retries;
int (*set_read_retry)(struct spinand_device *spinand,
unsigned int retry_mode);
+ int (*set_randomizer)(struct spinand_device *spinand);
};

/**
--
2.25.1