[PATCH v3 1/2] mtd: rawnand: serialize OTP operations in the core

James Hilliard james.hilliard1 at gmail.com
Thu Nov 26 12:04:51 PST 2026


OTP operations on Macronix MX30LFxG18AC chips switch the device into a
dedicated mode before reading or programming pages. Their protection-
register MTD callbacks bypass the raw NAND read/write wrappers. A
concurrent NAND operation can therefore access the device while OTP
mode is active, and OTP access can bypass the normal wait for a
suspended chip.

Add OTP callbacks to nand_chip_ops and route raw NAND protection-
register requests through core wrappers. The wrappers hold the chip and
controller locks around each callback, so normal NAND I/O cannot
interleave with OTP access. If the chip is suspended, OTP access waits
for nand_resume() to clear the suspended flag.
Device-specific callbacks retain target selection and OTP mode handling.

Fixes: e87161321a40 ("mtd: rawnand: macronix: OTP access for MX30LFxG18AC")
Cc: stable at vger.kernel.org
Cc: Arseniy Krasnov <AVKrasnov at sberdevices.ru>
Signed-off-by: James Hilliard <james.hilliard1 at gmail.com>
---
 drivers/mtd/nand/raw/nand_base.c     | 106 +++++++++++++++++++++++++++++++++++
 drivers/mtd/nand/raw/nand_macronix.c |  32 +++++------
 include/linux/mtd/rawnand.h          |  24 ++++++++
 3 files changed, 145 insertions(+), 17 deletions(-)

diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
index 58dbcb004a62..5c877e2536e4 100644
--- a/drivers/mtd/nand/raw/nand_base.c
+++ b/drivers/mtd/nand/raw/nand_base.c
@@ -6180,6 +6180,98 @@ static const struct nand_ops rawnand_ops = {
 	.isbad = rawnand_isbad,
 };
 
+static int nand_get_fact_prot_info(struct mtd_info *mtd, size_t len,
+				   size_t *retlen, struct otp_info *buf)
+{
+	struct nand_chip *chip = mtd_to_nand(mtd);
+	int ret;
+
+	nand_get_device(chip);
+	ret = chip->ops.get_fact_prot_info(chip, len, retlen, buf);
+	nand_release_device(chip);
+
+	return ret;
+}
+
+static int nand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
+				   size_t len, size_t *retlen, u_char *buf)
+{
+	struct nand_chip *chip = mtd_to_nand(mtd);
+	int ret;
+
+	nand_get_device(chip);
+	ret = chip->ops.read_fact_prot_reg(chip, from, len, retlen, buf);
+	nand_release_device(chip);
+
+	return ret;
+}
+
+static int nand_get_user_prot_info(struct mtd_info *mtd, size_t len,
+				   size_t *retlen, struct otp_info *buf)
+{
+	struct nand_chip *chip = mtd_to_nand(mtd);
+	int ret;
+
+	nand_get_device(chip);
+	ret = chip->ops.get_user_prot_info(chip, len, retlen, buf);
+	nand_release_device(chip);
+
+	return ret;
+}
+
+static int nand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
+				   size_t len, size_t *retlen, u_char *buf)
+{
+	struct nand_chip *chip = mtd_to_nand(mtd);
+	int ret;
+
+	nand_get_device(chip);
+	ret = chip->ops.read_user_prot_reg(chip, from, len, retlen, buf);
+	nand_release_device(chip);
+
+	return ret;
+}
+
+static int nand_write_user_prot_reg(struct mtd_info *mtd, loff_t to,
+				    size_t len, size_t *retlen,
+				    const u_char *buf)
+{
+	struct nand_chip *chip = mtd_to_nand(mtd);
+	int ret;
+
+	nand_get_device(chip);
+	ret = chip->ops.write_user_prot_reg(chip, to, len, retlen, buf);
+	nand_release_device(chip);
+
+	return ret;
+}
+
+static int nand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
+				   size_t len)
+{
+	struct nand_chip *chip = mtd_to_nand(mtd);
+	int ret;
+
+	nand_get_device(chip);
+	ret = chip->ops.lock_user_prot_reg(chip, from, len);
+	nand_release_device(chip);
+
+	return ret;
+}
+
+static int nand_erase_user_prot_reg(struct mtd_info *mtd, loff_t from,
+				    size_t len)
+{
+	struct nand_chip *chip = mtd_to_nand(mtd);
+	int ret;
+
+	nand_get_device(chip);
+	ret = chip->ops.erase_user_prot_reg(chip, from, len);
+	nand_release_device(chip);
+
+	return ret;
+}
+
 /**
  * nand_scan_tail - Scan for the NAND device
  * @chip: NAND chip object
@@ -6443,6 +6535,20 @@ static int nand_scan_tail(struct nand_chip *chip)
 	mtd->_block_isbad = nand_block_isbad;
 	mtd->_block_markbad = nand_block_markbad;
 	mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks;
+	if (chip->ops.get_fact_prot_info)
+		mtd->_get_fact_prot_info = nand_get_fact_prot_info;
+	if (chip->ops.read_fact_prot_reg)
+		mtd->_read_fact_prot_reg = nand_read_fact_prot_reg;
+	if (chip->ops.get_user_prot_info)
+		mtd->_get_user_prot_info = nand_get_user_prot_info;
+	if (chip->ops.read_user_prot_reg)
+		mtd->_read_user_prot_reg = nand_read_user_prot_reg;
+	if (chip->ops.write_user_prot_reg)
+		mtd->_write_user_prot_reg = nand_write_user_prot_reg;
+	if (chip->ops.lock_user_prot_reg)
+		mtd->_lock_user_prot_reg = nand_lock_user_prot_reg;
+	if (chip->ops.erase_user_prot_reg)
+		mtd->_erase_user_prot_reg = nand_erase_user_prot_reg;
 
 	/*
 	 * Initialize bitflip_threshold to its default prior scan_bbt() call.
diff --git a/drivers/mtd/nand/raw/nand_macronix.c b/drivers/mtd/nand/raw/nand_macronix.c
index 16847f28b19d..30e42594daec 100644
--- a/drivers/mtd/nand/raw/nand_macronix.c
+++ b/drivers/mtd/nand/raw/nand_macronix.c
@@ -326,7 +326,7 @@ static void macronix_nand_deep_power_down_support(struct nand_chip *chip)
 	chip->ops.resume = mxic_nand_resume;
 }
 
-static int macronix_30lfxg18ac_get_otp_info(struct mtd_info *mtd, size_t len,
+static int macronix_30lfxg18ac_get_otp_info(struct nand_chip *nand, size_t len,
 					    size_t *retlen,
 					    struct otp_info *buf)
 {
@@ -368,18 +368,16 @@ static int macronix_30lfxg18ac_otp_disable(struct nand_chip *nand)
 				 feature_buf);
 }
 
-static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
+static int __macronix_30lfxg18ac_rw_otp(struct nand_chip *nand,
 					loff_t offs_in_flash,
 					size_t len, size_t *retlen,
 					u_char *buf, bool write)
 {
-	struct nand_chip *nand;
 	size_t bytes_handled;
 	off_t offs_in_page;
 	u64 page;
 	int ret, cleanup_ret;
 
-	nand = mtd_to_nand(mtd);
 	nand_select_target(nand, 0);
 
 	ret = macronix_30lfxg18ac_otp_enable(nand);
@@ -418,11 +416,13 @@ static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
 
 out_otp:
 	if (ret)
-		dev_err(&mtd->dev, "failed to perform OTP IO: %i\n", ret);
+		dev_err(&nand_to_mtd(nand)->dev,
+			"failed to perform OTP IO: %i\n", ret);
 
 	cleanup_ret = macronix_30lfxg18ac_otp_disable(nand);
 	if (cleanup_ret)
-		dev_err(&mtd->dev, "failed to leave OTP mode after %s\n",
+		dev_err(&nand_to_mtd(nand)->dev,
+			"failed to leave OTP mode after %s\n",
 			write ? "write" : "read");
 
 	nand_deselect_target(nand);
@@ -430,22 +430,22 @@ static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
 	return ret ? ret : cleanup_ret;
 }
 
-static int macronix_30lfxg18ac_write_otp(struct mtd_info *mtd, loff_t to,
+static int macronix_30lfxg18ac_write_otp(struct nand_chip *nand, loff_t to,
 					 size_t len, size_t *rlen,
 					 const u_char *buf)
 {
-	return __macronix_30lfxg18ac_rw_otp(mtd, to, len, rlen, (u_char *)buf,
+	return __macronix_30lfxg18ac_rw_otp(nand, to, len, rlen, (u_char *)buf,
 					    true);
 }
 
-static int macronix_30lfxg18ac_read_otp(struct mtd_info *mtd, loff_t from,
+static int macronix_30lfxg18ac_read_otp(struct nand_chip *nand, loff_t from,
 					size_t len, size_t *rlen,
 					u_char *buf)
 {
-	return __macronix_30lfxg18ac_rw_otp(mtd, from, len, rlen, buf, false);
+	return __macronix_30lfxg18ac_rw_otp(nand, from, len, rlen, buf, false);
 }
 
-static int macronix_30lfxg18ac_lock_otp(struct mtd_info *mtd, loff_t from,
+static int macronix_30lfxg18ac_lock_otp(struct nand_chip *nand, loff_t from,
 					size_t len)
 {
 	/* See comment in 'macronix_30lfxg18ac_get_otp_info()'. */
@@ -459,7 +459,6 @@ static void macronix_nand_setup_otp(struct nand_chip *chip)
 		"MX30LF2G18AC",
 		"MX30LF4G18AC",
 	};
-	struct mtd_info *mtd;
 
 	if (match_string(supported_otp_models,
 			 ARRAY_SIZE(supported_otp_models),
@@ -474,11 +473,10 @@ static void macronix_nand_setup_otp(struct nand_chip *chip)
 	bitmap_set(chip->parameters.set_feature_list,
 		   ONFI_FEATURE_ADDR_30LFXG18AC_OTP, 1);
 
-	mtd = nand_to_mtd(chip);
-	mtd->_get_user_prot_info = macronix_30lfxg18ac_get_otp_info;
-	mtd->_read_user_prot_reg = macronix_30lfxg18ac_read_otp;
-	mtd->_write_user_prot_reg = macronix_30lfxg18ac_write_otp;
-	mtd->_lock_user_prot_reg = macronix_30lfxg18ac_lock_otp;
+	chip->ops.get_user_prot_info = macronix_30lfxg18ac_get_otp_info;
+	chip->ops.read_user_prot_reg = macronix_30lfxg18ac_read_otp;
+	chip->ops.write_user_prot_reg = macronix_30lfxg18ac_write_otp;
+	chip->ops.lock_user_prot_reg = macronix_30lfxg18ac_lock_otp;
 }
 
 static int macronix_nand_init(struct nand_chip *chip)
diff --git a/include/linux/mtd/rawnand.h b/include/linux/mtd/rawnand.h
index 43fefff02025..b08aaadf133c 100644
--- a/include/linux/mtd/rawnand.h
+++ b/include/linux/mtd/rawnand.h
@@ -1191,6 +1191,16 @@ struct nand_legacy {
  * @unlock_area: Unlock operation
  * @setup_read_retry: Set the read-retry mode (mostly needed for MLC NANDs)
  * @choose_interface_config: Choose the best interface configuration
+ * @get_fact_prot_info: Get information about the factory protection area
+ * @read_fact_prot_reg: Read the factory protection area
+ * @get_user_prot_info: Get information about the user protection area
+ * @read_user_prot_reg: Read the user protection area
+ * @write_user_prot_reg: Write the user protection area
+ * @lock_user_prot_reg: Lock the user protection area
+ * @erase_user_prot_reg: Erase the user protection area
+ *
+ * Protection-register callbacks are called with the chip and controller
+ * locked. They select and deselect the target as required.
  */
 struct nand_chip_ops {
 	int (*suspend)(struct nand_chip *chip);
@@ -1200,6 +1210,20 @@ struct nand_chip_ops {
 	int (*setup_read_retry)(struct nand_chip *chip, int retry_mode);
 	int (*choose_interface_config)(struct nand_chip *chip,
 				       struct nand_interface_config *iface);
+	int (*get_fact_prot_info)(struct nand_chip *chip, size_t len,
+				  size_t *retlen, struct otp_info *buf);
+	int (*read_fact_prot_reg)(struct nand_chip *chip, loff_t from,
+				  size_t len, size_t *retlen, u_char *buf);
+	int (*get_user_prot_info)(struct nand_chip *chip, size_t len,
+				  size_t *retlen, struct otp_info *buf);
+	int (*read_user_prot_reg)(struct nand_chip *chip, loff_t from,
+				  size_t len, size_t *retlen, u_char *buf);
+	int (*write_user_prot_reg)(struct nand_chip *chip, loff_t to,
+				   size_t len, size_t *retlen, const u_char *buf);
+	int (*lock_user_prot_reg)(struct nand_chip *chip, loff_t from,
+				  size_t len);
+	int (*erase_user_prot_reg)(struct nand_chip *chip, loff_t from,
+				   size_t len);
 };
 
 /**

-- 
2.53.0




More information about the linux-mtd mailing list