]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
mtd: spi-nor: Create a local SR cache
authorMiquel Raynal <miquel.raynal@bootlin.com>
Tue, 26 May 2026 14:56:41 +0000 (16:56 +0200)
committerPratyush Yadav <pratyush@kernel.org>
Tue, 26 May 2026 15:21:05 +0000 (17:21 +0200)
In order to be able to generate debugfs output without having to
actually reach the flash, create a SPI NOR local cache of the status
registers. What matters in our case are all the bits related to sector
locking. As such, in order to make it clear that this cache is not
intended to be used anywhere else, we zero the irrelevant bits.

The cache is initialized once during the early init, and then maintained
every time the write protection scheme is updated.

Suggested-by: Michael Walle <mwalle@kernel.org>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Signed-off-by: Pratyush Yadav <pratyush@kernel.org>
drivers/mtd/spi-nor/core.c
drivers/mtd/spi-nor/core.h
drivers/mtd/spi-nor/swp.c
include/linux/mtd/spi-nor.h

index 2799c21d0b67ebbcd87436188db8ecefb33135f8..1fc71d6e0fec21b30b319ad4180ac4133a8e81d3 100644 (file)
@@ -3326,10 +3326,12 @@ static int spi_nor_init(struct spi_nor *nor)
         * protection bits are volatile. The latter is indicated by
         * SNOR_F_SWP_IS_VOLATILE.
         */
+       spi_nor_cache_sr_lock_bits(nor, NULL);
        if (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE) ||
            (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE_ON_VOLATILE) &&
-            nor->flags & SNOR_F_SWP_IS_VOLATILE))
+            nor->flags & SNOR_F_SWP_IS_VOLATILE)) {
                spi_nor_try_unlock_all(nor);
+       }
 
        if (nor->addr_nbytes == 4 &&
            nor->read_proto != SNOR_PROTO_8_8_8_DTR &&
index 0753d0a6f8b836e02ce83b4a86638135059001ba..cd355e94b97e520552160c0fb4416a679a3c2c0f 100644 (file)
@@ -679,6 +679,7 @@ int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
 
 void spi_nor_init_default_locking_ops(struct spi_nor *nor);
 void spi_nor_try_unlock_all(struct spi_nor *nor);
+void spi_nor_cache_sr_lock_bits(struct spi_nor *nor, u8 *sr);
 void spi_nor_set_mtd_locking_ops(struct spi_nor *nor);
 void spi_nor_set_mtd_otp_ops(struct spi_nor *nor);
 
index 2d1b1c8a535a59ee1f5c187c6b3a75cbcdc86cd1..cd37fec08c0efc0d106e9a8ef494fad91551872a 100644 (file)
@@ -162,6 +162,25 @@ static int spi_nor_build_sr(struct spi_nor *nor, const u8 *old_sr, u8 *new_sr,
        return 0;
 }
 
+/*
+ * Keep a local cache containing all lock-related bits for debugfs use only.
+ * This way, debugfs never needs to access the flash directly.
+ */
+void spi_nor_cache_sr_lock_bits(struct spi_nor *nor, u8 *sr)
+{
+       u8 bp_mask = spi_nor_get_sr_bp_mask(nor);
+       u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
+
+       if (!sr) {
+               if (spi_nor_read_sr(nor, nor->bouncebuf))
+                       return;
+
+               sr = nor->bouncebuf;
+       }
+
+       nor->dfs_sr_cache[0] = sr[0] & (bp_mask | tb_mask | SR_SRWD);
+}
+
 /*
  * Lock a region of the flash. Compatible with ST Micro and similar flash.
  * Supports the block protection bits BP{0,1,2}/BP{0,1,2,3} in the status
@@ -271,7 +290,13 @@ static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, u64 len)
            (ofs_old < ofs_new || (ofs_new + len_new) < (ofs_old + len_old)))
                return -EINVAL;
 
-       return spi_nor_write_sr_and_check(nor, status_new[0]);
+       ret = spi_nor_write_sr_and_check(nor, status_new[0]);
+       if (ret)
+               return ret;
+
+       spi_nor_cache_sr_lock_bits(nor, status_new);
+
+       return 0;
 }
 
 /*
@@ -353,7 +378,13 @@ static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, u64 len)
            (ofs_new < ofs_old || (ofs_old + len_old) < (ofs_new + len_new)))
                return -EINVAL;
 
-       return spi_nor_write_sr_and_check(nor, status_new[0]);
+       ret = spi_nor_write_sr_and_check(nor, status_new[0]);
+       if (ret)
+               return ret;
+
+       spi_nor_cache_sr_lock_bits(nor, status_new);
+
+       return 0;
 }
 
 /*
index 90a0cf58351295c63baea4f064b49b7390337d37..9ad77f9e76c270f4cb332f73de5a84b748aefa22 100644 (file)
@@ -371,6 +371,7 @@ struct spi_nor_flash_parameter;
  * @reg_proto:         the SPI protocol for read_reg/write_reg/erase operations
  * @sfdp:              the SFDP data of the flash
  * @debugfs_root:      pointer to the debugfs directory
+ * @dfs_sr_cache:      Status Register cached value for debugfs use only
  * @controller_ops:    SPI NOR controller driver specific operations.
  * @params:            [FLASH-SPECIFIC] SPI NOR flash parameters and settings.
  *                      The structure includes legacy flash parameters and
@@ -409,6 +410,7 @@ struct spi_nor {
        enum spi_nor_cmd_ext    cmd_ext_type;
        struct sfdp             *sfdp;
        struct dentry           *debugfs_root;
+       u8                      dfs_sr_cache[2];
 
        const struct spi_nor_controller_ops *controller_ops;