Skip to contentSkip to conversation

mtd: spi-nor: write protection at power-up

PATCH Superseded [email protected] 2 participants 14 messages Jul 17, 2019 – Aug 12, 2019

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

cover mtd: spi-nor: write protection at power-up

From: Tudor Ambarus <[email protected]>

There is no functional change intended for the first 2 patches.

Patch 3 adds support for the Global Block Unlock command: a single command cycle that unlocks the entire memory array.

Patch 4 unlocks the global block protection on sst26vf064b. This is identical with what other nor flashes are doing by clearing the block protection bits from the status register: disable the write protection after a power-on reset cycle.

Patch 5 adds a Kconfig option to disable the write protection at power-up. This permits users to choose if they want the write protection at power-up enabled or not. Backward compatibility imposes to disable the write protection at power-up, so the Kconfig option is selected by default.

Tudor Ambarus (5):
mtd: spi-nor: fix description for int (*flash_is_locked)()
mtd: spi-nor: group the code about the write protection at power-up
mtd: spi-nor: add Global Block Unlock support
mtd: spi-nor: unlock global block protection on sst26vf064b
mtd: spi-nor: add Kconfig option to disable write protection at
power-up

 drivers/mtd/spi-nor/Kconfig   |  8 ++++++
 drivers/mtd/spi-nor/spi-nor.c | 66 ++++++++++++++++++++++++++++++++-----------
 include/linux/mtd/spi-nor.h   |  9 +++---
 3 files changed, 62 insertions(+), 21 deletions(-)
Signature · 1 line
2.9.5
#1 lore raw

PATCH 1/5 mtd: spi-nor: fix description for int (*flash_is_locked)()

From: Tudor Ambarus <[email protected]>

The description was interleaved.

---
 include/linux/mtd/spi-nor.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/mtd/spi-nor.h +1 -1
index 9f57cdfcc93d..c4c2c5971284 100644
@@ -372,10 +372,10 @@ struct flash_info;
  * @flash_lock:		[FLASH-SPECIFIC] lock a region of the SPI NOR
  * @flash_unlock:	[FLASH-SPECIFIC] unlock a region of the SPI NOR
  * @flash_is_locked:	[FLASH-SPECIFIC] check if a region of the SPI NOR is
+ *			completely locked
  * @quad_enable:	[FLASH-SPECIFIC] enables SPI NOR quad mode
  * @clear_sr_bp:	[FLASH-SPECIFIC] clears the Block Protection Bits from
  *			the SPI NOR Status Register.
- *			completely locked
  * @priv:		the private data
  */
 struct spi_nor {
Signature · 1 line
2.9.5
#7 lore raw

PATCH 3/5 mtd: spi-nor: add Global Block Unlock support

In reply to [email protected]

From: Tudor Ambarus <[email protected]>

To avoid inadvertent writes during power-up, some flashes are write-protected by default after a power-on reset cycle. A Global Block-Protection Unlock command offers a single command cycle that unlocks the entire memory array. This is identical with what other nor flashes are doing by clearing the block protection bits from the status register: disable the write protection after a power-on reset cycle.

We can't determine this purely by manufacturer type and it's not autodetectable by anything like SFDP, so make a new flag for it:
UNLOCK_GLOBAL_BLOCK.

Note that the Global Block Unlock command has different names depending on the manufacturer, but always the same command value:
0x98. Macronix's MX25U12835F names it Gang Block Unlock, Winbound's W25Q128FV names it Global Block Unlock and Microchip's SST26VF064B names it Global Block Protection Unlock.

---
 drivers/mtd/spi-nor/spi-nor.c | 21 ++++++++++++++++++++-
 include/linux/mtd/spi-nor.h   |  1 +
 2 files changed, 21 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/spi-nor.c +20 -1
index e9e441f91b68..767e2e6eb1b8 100644
@@ -250,7 +250,7 @@ struct flash_info {
 	u16		page_size;
 	u16		addr_width;
 
-	u16		flags;
+	u32		flags;
 #define SECT_4K			BIT(0)	/* SPINOR_OP_BE_4K works uniformly */
 #define SPI_NOR_NO_ERASE	BIT(1)	/* No erase command needed */
 #define SST_WRITE		BIT(2)	/* use SST byte programming */
@@ -279,6 +279,7 @@ struct flash_info {
 #define SPI_NOR_SKIP_SFDP	BIT(13)	/* Skip parsing of SFDP tables */
 #define USE_CLSR		BIT(14)	/* use CLSR command */
 #define SPI_NOR_OCTAL_READ	BIT(15)	/* Flash supports Octal Read */
+#define UNLOCK_GLOBAL_BLOCK	BIT(16) /* Unlock global block protection */
 
 	/* Part specific fixup hooks. */
 	const struct spi_nor_fixups *fixups;
@@ -1725,6 +1726,20 @@ static int spi_nor_spansion_clear_sr_bp(struct spi_nor *nor)
 	return spi_nor_clear_sr_bp(nor);
 }
 
+static int spi_nor_unlock_global_block_protection(struct spi_nor *nor)
+{
+	int ret;
+
+	write_enable(nor);
+
+	ret = nor->write_reg(nor, SPINOR_OP_ULBPR, NULL, 0);
+	if (ret < 0) {
+		dev_err(nor->dev, "error %d on ULBPR\n", ret);
+		return ret;
+	}
+	return spi_nor_wait_till_ready(nor);
+}
+
 /* Used when the "_ext_id" is two bytes at most */
 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags)	\
 		.id = {							\
@@ -4053,6 +4068,10 @@ static int spi_nor_init(struct spi_nor *nor)
 				spi_nor_spansion_clear_sr_bp;
 	}
 
+	if (nor->info->flags & UNLOCK_GLOBAL_BLOCK)
+		nor->disable_write_protection =
+			spi_nor_unlock_global_block_protection;
+
 	if (nor->disable_write_protection) {
 		err = nor->disable_write_protection(nor);
 		if (err) {
include/linux/mtd/spi-nor.h +1 -0
index 6c3273760700..84d279fd287e 100644
@@ -65,6 +65,7 @@
 #define SPINOR_OP_CLFSR		0x50	/* Clear flag status register */
 #define SPINOR_OP_RDEAR		0xc8	/* Read Extended Address Register */
 #define SPINOR_OP_WREAR		0xc5	/* Write Extended Address Register */
+#define SPINOR_OP_ULBPR		0x98    /* Global Block Unlock Protection */
 
 /* 4-byte address opcodes - used on Spansion and some Macronix flashes. */
 #define SPINOR_OP_READ_4B	0x13	/* Read data bytes (low frequency) */
Signature · 1 line
2.9.5
#10 lore raw

PATCH 5/5 mtd: spi-nor: add Kconfig option to disable write protection at power-up

In reply to [email protected]

From: Tudor Ambarus <[email protected]>

Some spi-nor flashes come write protected by default after a power-on sequence to avoid destructing commands (erase, write) during power-up.

Backward compatibility imposes to disable the write protection at power-up by default. Add a Kconfig option to let the user benefit of the power-up write protection.

---
 drivers/mtd/spi-nor/Kconfig   | 8 ++++++++
 drivers/mtd/spi-nor/spi-nor.c | 2 ++
 2 files changed, 10 insertions(+)
drivers/mtd/spi-nor/Kconfig +8 -0
index 6de83277ce8b..b550e10657f1 100644
@@ -22,6 +22,14 @@ config MTD_SPI_NOR_USE_4K_SECTORS
 	  Please note that some tools/drivers/filesystems may not work with
 	  4096 B erase size (e.g. UBIFS requires 15 KiB as a minimum).
 
+config MTD_SPI_NOR_DISABLE_POWER_UP_WRITE_PROTECTION
+	bool "Disable write protection during power-up"
+	default y
+	help
+	   Some spi-nor flashes are write protected by default after a power-on
+	   reset cycle, in order to avoid inadvertend writes during power-up.
+	   Disable the write protection during power-up.
+
 config SPI_ASPEED_SMC
 	tristate "Aspeed flash controllers in SPI mode"
 	depends on ARCH_ASPEED || COMPILE_TEST
drivers/mtd/spi-nor/spi-nor.c +2 -0
index ffb53740031c..e5627fa6b1cd 100644
@@ -4049,6 +4049,7 @@ static int spi_nor_init(struct spi_nor *nor)
 {
 	int err;
 
+#ifdef CONFIG_MTD_SPI_NOR_DISABLE_POWER_UP_WRITE_PROTECTION
 	/*
 	 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
 	 * with the software protection bits set.
@@ -4082,6 +4083,7 @@ static int spi_nor_init(struct spi_nor *nor)
 			return err;
 		}
 	}
+#endif
 
 	if (nor->quad_enable) {
 		err = nor->quad_enable(nor);
Signature · 1 line
2.9.5
#4 lore raw

PATCH 2/5 mtd: spi-nor: group the code about the write protection at power-up

In reply to [email protected]

From: Tudor Ambarus <[email protected]>

The write protection at power-up logic was split across functions because of a dependency to spansion_quad_enable(). Group the code in spi_nor_init() as the pointer to spansion_quad_enable() can be retrieved from nor->quad_enable.

While touching this code, rename nor->clear_sr_bp() to nor->disable_write_protection() to better indicate its scope: it disables the default write protection after a power-on reset cycle.

No functional change intended.

---
 drivers/mtd/spi-nor/spi-nor.c | 39 ++++++++++++++++++++++++---------------
 include/linux/mtd/spi-nor.h   |  6 +++---
 2 files changed, 27 insertions(+), 18 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +24 -15
index 03cc788511d5..e9e441f91b68 100644
@@ -3780,8 +3780,6 @@ static int spi_nor_init_params(struct spi_nor *nor,
 		default:
 			/* Kept only for backward compatibility purpose. */
 			params->quad_enable = spansion_quad_enable;
-			if (nor->clear_sr_bp)
-				nor->clear_sr_bp = spi_nor_spansion_clear_sr_bp;
 			break;
 		}
 
@@ -4034,11 +4032,32 @@ static int spi_nor_init(struct spi_nor *nor)
 {
 	int err;
 
-	if (nor->clear_sr_bp) {
-		err = nor->clear_sr_bp(nor);
+	/*
+	 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
+	 * with the software protection bits set.
+	 */
+	if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
+	    JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
+	    JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
+	    nor->info->flags & SPI_NOR_HAS_LOCK) {
+		nor->disable_write_protection = spi_nor_clear_sr_bp;
+
+		/*
+		 * In case of spansion flashes, when the configuration register
+		 * Quad Enable bit is one, only the the Write Status (01h)
+		 * command with two data bytes may be used to clear the block
+		 * protection bits.
+		 */
+		if (nor->quad_enable == spansion_quad_enable)
+			nor->disable_write_protection =
+				spi_nor_spansion_clear_sr_bp;
+	}
+
+	if (nor->disable_write_protection) {
+		err = nor->disable_write_protection(nor);
 		if (err) {
 			dev_err(nor->dev,
-				"fail to clear block protection bits\n");
+				"failed to unlock the flash at init\n");
 			return err;
 		}
 	}
@@ -4165,16 +4184,6 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
 	if (info->flags & SPI_S3AN)
 		nor->flags |=  SNOR_F_READY_XSR_RDY;
 
-	/*
-	 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
-	 * with the software protection bits set.
-	 */
-	if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
-	    JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
-	    JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
-	    nor->info->flags & SPI_NOR_HAS_LOCK)
-		nor->clear_sr_bp = spi_nor_clear_sr_bp;
-
 	/* Parse the Serial Flash Discoverable Parameters table. */
 	ret = spi_nor_init_params(nor, &params);
 	if (ret)
include/linux/mtd/spi-nor.h +3 -3
index c4c2c5971284..6c3273760700 100644
@@ -374,8 +374,8 @@ struct flash_info;
  * @flash_is_locked:	[FLASH-SPECIFIC] check if a region of the SPI NOR is
  *			completely locked
  * @quad_enable:	[FLASH-SPECIFIC] enables SPI NOR quad mode
- * @clear_sr_bp:	[FLASH-SPECIFIC] clears the Block Protection Bits from
- *			the SPI NOR Status Register.
+ * @disable_write_protection: [FLASH-SPECIFIC] disable write protection during
+ *                            power-up
  * @priv:		the private data
  */
 struct spi_nor {
@@ -412,7 +412,7 @@ struct spi_nor {
 	int (*flash_unlock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
 	int (*flash_is_locked)(struct spi_nor *nor, loff_t ofs, uint64_t len);
 	int (*quad_enable)(struct spi_nor *nor);
-	int (*clear_sr_bp)(struct spi_nor *nor);
+	int (*disable_write_protection)(struct spi_nor *nor);
 
 	void *priv;
 };
Signature · 1 line
2.9.5
#9 lore raw

PATCH 4/5 mtd: spi-nor: unlock global block protection on sst26vf064b

In reply to [email protected]

From: Tudor Ambarus <[email protected]>

To avoid inadvertent writes during power-up, sst26vf064b is write-protected by default after a power-on reset cycle. Unlock the serial flash memory by using the Global Block Protection Unlock command - it offers a single command cycle that unlocks the entire memory array.

---
 drivers/mtd/spi-nor/spi-nor.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/spi-nor.c +3 -1
index 767e2e6eb1b8..ffb53740031c 100644
@@ -2075,7 +2075,9 @@ static const struct flash_info spi_nor_ids[] = {
 	{ "sst25wf040b", INFO(0x621613, 0, 64 * 1024,  8, SECT_4K) },
 	{ "sst25wf040",  INFO(0xbf2504, 0, 64 * 1024,  8, SECT_4K | SST_WRITE) },
 	{ "sst25wf080",  INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
-	{ "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
+	{ "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128,
+			      SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
+			      UNLOCK_GLOBAL_BLOCK) },
 
 	/* ST Microelectronics -- newer production may have feature updates */
 	{ "m25p05",  INFO(0x202010,  0,  32 * 1024,   2, 0) },
Signature · 1 line
2.9.5
#11 lore raw

Re: [PATCH 5/5] mtd: spi-nor: add Kconfig option to disable write protection at power-up

In reply to [email protected]
Quoted 10 lines · On 07/17/2019 11:48 AM, Tudor Ambarus - M18064 wrote:

From: Tudor Ambarus <[email protected]>

Some spi-nor flashes come write protected by default after a power-on sequence to avoid destructing commands (erase, write) during power-up.

Backward compatibility imposes to disable the write protection at power-up by default. Add a Kconfig option to let the user benefit of the power-up write protection.

Quoted 5 lines
---
 drivers/mtd/spi-nor/Kconfig   | 8 ++++++++
 drivers/mtd/spi-nor/spi-nor.c | 2 ++
 2 files changed, 10 insertions(+)

I'll have to mark spi_nor_spansion_clear_sr_bp() and spi_nor_unlock_global_block_protection() definitions as __maybe_unused.

drivers/mtd/spi-nor/spi-nor.c:1729:12: warning:
‘spi_nor_unlock_global_block_protection’ defined but not used [-Wunused-function]
static int spi_nor_unlock_global_block_protection(struct spi_nor *nor)

            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/mtd/spi-nor/spi-nor.c:1687:12: warning: ‘spi_nor_spansion_clear_sr_bp’
defined but not used [-Wunused-function]
 static int spi_nor_spansion_clear_sr_bp(struct spi_nor *nor)
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#12 lore raw

PATCH v2 5/5 mtd: spi-nor: add Kconfig option to disable write protection at power-up

From: Tudor Ambarus <[email protected]>

Some spi-nor flashes come write protected by default after a power-on sequence to avoid destructing commands (erase, write) during power-up.

Backward compatibility imposes to disable the write protection at power-up by default. Add a Kconfig option to let the user benefit of the power-up write protection.

---
v2: mark spi_nor_spansion_clear_sr_bp() and spi_nor_unlock_global_block_protection() definitions as __maybe_unused, to avoid -Wunused-function warnings when MTD_SPI_NOR_DISABLE_POWER_UP_WRITE_PROTECTION is not selected.

 drivers/mtd/spi-nor/Kconfig   | 8 ++++++++
 drivers/mtd/spi-nor/spi-nor.c | 7 +++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/Kconfig +8 -0
index 6de83277ce8b..b550e10657f1 100644
@@ -22,6 +22,14 @@ config MTD_SPI_NOR_USE_4K_SECTORS
 	  Please note that some tools/drivers/filesystems may not work with
 	  4096 B erase size (e.g. UBIFS requires 15 KiB as a minimum).
 
+config MTD_SPI_NOR_DISABLE_POWER_UP_WRITE_PROTECTION
+	bool "Disable write protection during power-up"
+	default y
+	help
+	   Some spi-nor flashes are write protected by default after a power-on
+	   reset cycle, in order to avoid inadvertend writes during power-up.
+	   Disable the write protection during power-up.
+
 config SPI_ASPEED_SMC
 	tristate "Aspeed flash controllers in SPI mode"
 	depends on ARCH_ASPEED || COMPILE_TEST
drivers/mtd/spi-nor/spi-nor.c +5 -2
index ffb53740031c..9b948295ef27 100644
@@ -1684,7 +1684,7 @@ static int spi_nor_clear_sr_bp(struct spi_nor *nor)
  *
  * Return: 0 on success, -errno otherwise.
  */
-static int spi_nor_spansion_clear_sr_bp(struct spi_nor *nor)
+static int __maybe_unused spi_nor_spansion_clear_sr_bp(struct spi_nor *nor)
 {
 	int ret;
 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
@@ -1726,7 +1726,8 @@ static int spi_nor_spansion_clear_sr_bp(struct spi_nor *nor)
 	return spi_nor_clear_sr_bp(nor);
 }
 
-static int spi_nor_unlock_global_block_protection(struct spi_nor *nor)
+static int __maybe_unused
+spi_nor_unlock_global_block_protection(struct spi_nor *nor)
 {
 	int ret;
 
@@ -4049,6 +4050,7 @@ static int spi_nor_init(struct spi_nor *nor)
 {
 	int err;
 
+#ifdef CONFIG_MTD_SPI_NOR_DISABLE_POWER_UP_WRITE_PROTECTION
 	/*
 	 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
 	 * with the software protection bits set.
@@ -4082,6 +4084,7 @@ static int spi_nor_init(struct spi_nor *nor)
 			return err;
 		}
 	}
+#endif
 
 	if (nor->quad_enable) {
 		err = nor->quad_enable(nor);
Signature · 1 line
2.9.5
maintainer Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] mtd: spi-nor: fix description for int (*flash_is_locked)()

In reply to [email protected]
Quoted 5 lines · On 17/07/19 2:18 PM, [email protected] wrote:

From: Tudor Ambarus <[email protected]>

The description was interleaved.

Quoted 21 lines
---
 include/linux/mtd/spi-nor.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/mtd/spi-nor.h +1 -1
index 9f57cdfcc93d..c4c2c5971284 100644
@@ -372,10 +372,10 @@ struct flash_info;
  * @flash_lock:		[FLASH-SPECIFIC] lock a region of the SPI NOR
  * @flash_unlock:	[FLASH-SPECIFIC] unlock a region of the SPI NOR
  * @flash_is_locked:	[FLASH-SPECIFIC] check if a region of the SPI NOR is
+ *			completely locked
  * @quad_enable:	[FLASH-SPECIFIC] enables SPI NOR quad mode
  * @clear_sr_bp:	[FLASH-SPECIFIC] clears the Block Protection Bits from
  *			the SPI NOR Status Register.
- *			completely locked
  * @priv:		the private data
  */
 struct spi_nor {
Signature · 2 lines
Regards
Vignesh
#5 lore raw

Re: [PATCH 2/5] mtd: spi-nor: group the code about the write protection at power-up

In reply to [email protected]
Quoted 10 lines · On 17/07/19 2:18 PM, [email protected] wrote:

From: Tudor Ambarus <[email protected]>

The write protection at power-up logic was split across functions because of a dependency to spansion_quad_enable(). Group the code in spi_nor_init() as the pointer to spansion_quad_enable() can be retrieved from nor->quad_enable.

While touching this code, rename nor->clear_sr_bp() to nor->disable_write_protection() to better indicate its scope: it disables the default write protection after a power-on reset cycle.

I prefer this function to be renamed to nor->disable_block_protection() so as to avoid being confused with write protect signal input to the flash.

Regards
Vignesh

Quoted 100 lines

No functional change intended.

---
 drivers/mtd/spi-nor/spi-nor.c | 39 ++++++++++++++++++++++++---------------
 include/linux/mtd/spi-nor.h   |  6 +++---
 2 files changed, 27 insertions(+), 18 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +24 -15
index 03cc788511d5..e9e441f91b68 100644
@@ -3780,8 +3780,6 @@ static int spi_nor_init_params(struct spi_nor *nor,
 		default:
 			/* Kept only for backward compatibility purpose. */
 			params->quad_enable = spansion_quad_enable;
-			if (nor->clear_sr_bp)
-				nor->clear_sr_bp = spi_nor_spansion_clear_sr_bp;
 			break;
 		}
 
@@ -4034,11 +4032,32 @@ static int spi_nor_init(struct spi_nor *nor)
 {
 	int err;
 
-	if (nor->clear_sr_bp) {
-		err = nor->clear_sr_bp(nor);
+	/*
+	 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
+	 * with the software protection bits set.
+	 */
+	if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
+	    JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
+	    JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
+	    nor->info->flags & SPI_NOR_HAS_LOCK) {
+		nor->disable_write_protection = spi_nor_clear_sr_bp;
+
+		/*
+		 * In case of spansion flashes, when the configuration register
+		 * Quad Enable bit is one, only the the Write Status (01h)
+		 * command with two data bytes may be used to clear the block
+		 * protection bits.
+		 */
+		if (nor->quad_enable == spansion_quad_enable)
+			nor->disable_write_protection =
+				spi_nor_spansion_clear_sr_bp;
+	}
+
+	if (nor->disable_write_protection) {
+		err = nor->disable_write_protection(nor);
 		if (err) {
 			dev_err(nor->dev,
-				"fail to clear block protection bits\n");
+				"failed to unlock the flash at init\n");
 			return err;
 		}
 	}
@@ -4165,16 +4184,6 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
 	if (info->flags & SPI_S3AN)
 		nor->flags |=  SNOR_F_READY_XSR_RDY;
 
-	/*
-	 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
-	 * with the software protection bits set.
-	 */
-	if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
-	    JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
-	    JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
-	    nor->info->flags & SPI_NOR_HAS_LOCK)
-		nor->clear_sr_bp = spi_nor_clear_sr_bp;
-
 	/* Parse the Serial Flash Discoverable Parameters table. */
 	ret = spi_nor_init_params(nor, &params);
 	if (ret)
include/linux/mtd/spi-nor.h +3 -3
index c4c2c5971284..6c3273760700 100644
@@ -374,8 +374,8 @@ struct flash_info;
  * @flash_is_locked:	[FLASH-SPECIFIC] check if a region of the SPI NOR is
  *			completely locked
  * @quad_enable:	[FLASH-SPECIFIC] enables SPI NOR quad mode
- * @clear_sr_bp:	[FLASH-SPECIFIC] clears the Block Protection Bits from
- *			the SPI NOR Status Register.
+ * @disable_write_protection: [FLASH-SPECIFIC] disable write protection during
+ *                            power-up
  * @priv:		the private data
  */
 struct spi_nor {
@@ -412,7 +412,7 @@ struct spi_nor {
 	int (*flash_unlock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
 	int (*flash_is_locked)(struct spi_nor *nor, loff_t ofs, uint64_t len);
 	int (*quad_enable)(struct spi_nor *nor);
-	int (*clear_sr_bp)(struct spi_nor *nor);
+	int (*disable_write_protection)(struct spi_nor *nor);
 
 	void *priv;
 };
Signature · 2 lines
Regards
Vignesh
maintainer Reviewed-by on v1 3/5
#8 lore raw

Re: [PATCH 3/5] mtd: spi-nor: add Global Block Unlock support

In reply to [email protected]
Quoted 14 lines · On 17/07/19 2:18 PM, [email protected] wrote:

From: Tudor Ambarus <[email protected]>

To avoid inadvertent writes during power-up, some flashes are write-protected by default after a power-on reset cycle. A Global Block-Protection Unlock command offers a single command cycle that unlocks the entire memory array. This is identical with what other nor flashes are doing by clearing the block protection bits from the status register: disable the write protection after a power-on reset cycle.

We can't determine this purely by manufacturer type and it's not autodetectable by anything like SFDP, so make a new flag for it:
UNLOCK_GLOBAL_BLOCK.

Quoted 7 lines

Note that the Global Block Unlock command has different names depending on the manufacturer, but always the same command value:
0x98. Macronix's MX25U12835F names it Gang Block Unlock, Winbound's W25Q128FV names it Global Block Unlock and Microchip's SST26VF064B names it Global Block Protection Unlock.

Quoted 71 lines
---
 drivers/mtd/spi-nor/spi-nor.c | 21 ++++++++++++++++++++-
 include/linux/mtd/spi-nor.h   |  1 +
 2 files changed, 21 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/spi-nor.c +20 -1
index e9e441f91b68..767e2e6eb1b8 100644
@@ -250,7 +250,7 @@ struct flash_info {
 	u16		page_size;
 	u16		addr_width;
 
-	u16		flags;
+	u32		flags;
 #define SECT_4K			BIT(0)	/* SPINOR_OP_BE_4K works uniformly */
 #define SPI_NOR_NO_ERASE	BIT(1)	/* No erase command needed */
 #define SST_WRITE		BIT(2)	/* use SST byte programming */
@@ -279,6 +279,7 @@ struct flash_info {
 #define SPI_NOR_SKIP_SFDP	BIT(13)	/* Skip parsing of SFDP tables */
 #define USE_CLSR		BIT(14)	/* use CLSR command */
 #define SPI_NOR_OCTAL_READ	BIT(15)	/* Flash supports Octal Read */
+#define UNLOCK_GLOBAL_BLOCK	BIT(16) /* Unlock global block protection */
 
 	/* Part specific fixup hooks. */
 	const struct spi_nor_fixups *fixups;
@@ -1725,6 +1726,20 @@ static int spi_nor_spansion_clear_sr_bp(struct spi_nor *nor)
 	return spi_nor_clear_sr_bp(nor);
 }
 
+static int spi_nor_unlock_global_block_protection(struct spi_nor *nor)
+{
+	int ret;
+
+	write_enable(nor);
+
+	ret = nor->write_reg(nor, SPINOR_OP_ULBPR, NULL, 0);
+	if (ret < 0) {
+		dev_err(nor->dev, "error %d on ULBPR\n", ret);
+		return ret;
+	}
+	return spi_nor_wait_till_ready(nor);
+}
+
 /* Used when the "_ext_id" is two bytes at most */
 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags)	\
 		.id = {							\
@@ -4053,6 +4068,10 @@ static int spi_nor_init(struct spi_nor *nor)
 				spi_nor_spansion_clear_sr_bp;
 	}
 
+	if (nor->info->flags & UNLOCK_GLOBAL_BLOCK)
+		nor->disable_write_protection =
+			spi_nor_unlock_global_block_protection;
+
 	if (nor->disable_write_protection) {
 		err = nor->disable_write_protection(nor);
 		if (err) {
include/linux/mtd/spi-nor.h +1 -0
index 6c3273760700..84d279fd287e 100644
@@ -65,6 +65,7 @@
 #define SPINOR_OP_CLFSR		0x50	/* Clear flag status register */
 #define SPINOR_OP_RDEAR		0xc8	/* Read Extended Address Register */
 #define SPINOR_OP_WREAR		0xc5	/* Write Extended Address Register */
+#define SPINOR_OP_ULBPR		0x98    /* Global Block Unlock Protection */
 
 /* 4-byte address opcodes - used on Spansion and some Macronix flashes. */
 #define SPINOR_OP_READ_4B	0x13	/* Read data bytes (low frequency) */
Signature · 2 lines
Regards
Vignesh
#6 lore raw

Re: [PATCH 2/5] mtd: spi-nor: group the code about the write protection at power-up

In reply to Vignesh Raghavendra
Quoted 7 lines · Vignesh Raghavendra
Quoted 7 lines

From: Tudor Ambarus <[email protected]>

The write protection at power-up logic was split across functions because of a dependency to spansion_quad_enable(). Group the code in spi_nor_init() as the pointer to spansion_quad_enable() can be retrieved from nor->quad_enable.

Quoted 5 lines
Quoted 3 lines

While touching this code, rename nor->clear_sr_bp() to nor->disable_write_protection() to better indicate its scope: it disables the default write protection after a power-on reset cycle.

I prefer this function to be renamed to nor->disable_block_protection() so as to avoid being confused with write protect signal input to the flash.

You're right, I'll rename it.

Thanks,
ta

#13 lore raw

Re: [PATCH v2 5/5] mtd: spi-nor: add Kconfig option to disable write protection at power-up

In reply to [email protected]

Hi Tudor

Quoted 1 line · On 17-Jul-19 5:00 PM, [email protected] wrote:

From: Tudor Ambarus <[email protected]>

[...]

Quoted 11 lines
 drivers/mtd/spi-nor/Kconfig   | 8 ++++++++
 drivers/mtd/spi-nor/spi-nor.c | 7 +++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/Kconfig +0 -0
index 6de83277ce8b..b550e10657f1 100644
@@ -22,6 +22,14 @@ config MTD_SPI_NOR_USE_4K_SECTORS
 	  Please note that some tools/drivers/filesystems may not work with

drivers/mtd/spi-nor/Kconfig config MTD_SPI_NOR_USE_4K_SECTORS · line 32

Quoted 10 lines
drivers/mtd/spi-nor/Kconfig +8 -0
 	  4096 B erase size (e.g. UBIFS requires 15 KiB as a minimum).
 
+config MTD_SPI_NOR_DISABLE_POWER_UP_WRITE_PROTECTION
+	bool "Disable write protection during power-up"
+	default y
+	help
+	   Some spi-nor flashes are write protected by default after a power-on
+	   reset cycle, in order to avoid inadvertend writes during power-up.
+	   Disable the write protection during power-up.
+

Hmm, how about setting MTD_POWERUP_LOCK flag in mtd->flags whenever flash powers up with WP enabled? User can disable WP by declaring partitions rw or keep partitions locked with ro. MTD core takes care of calling mtd->unlock() depending up on the rw/ro flag as part of add_mtd_device()

We could probably enhance spi_nor_unlock() to use spi_nor_unlock_global_block_protection() when asked to unlock entire flash.

Kconfig option does not scale well for multi-platform build. There would not be a way to have WP enabled on one platform but disabled on other.

Regards
Vignesh

Quoted 36 lines
drivers/mtd/spi-nor/Kconfig +0 -0
 config SPI_ASPEED_SMC
 	tristate "Aspeed flash controllers in SPI mode"
 	depends on ARCH_ASPEED || COMPILE_TEST
drivers/mtd/spi-nor/spi-nor.c +4 -2
index ffb53740031c..9b948295ef27 100644
@@ -1684,7 +1684,7 @@ static int spi_nor_clear_sr_bp(struct spi_nor *nor)
  *
  * Return: 0 on success, -errno otherwise.
  */
-static int spi_nor_spansion_clear_sr_bp(struct spi_nor *nor)
+static int __maybe_unused spi_nor_spansion_clear_sr_bp(struct spi_nor *nor)
 {
 	int ret;
 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
@@ -1726,7 +1726,8 @@ static int spi_nor_spansion_clear_sr_bp(struct spi_nor *nor)
 	return spi_nor_clear_sr_bp(nor);
 }
 
-static int spi_nor_unlock_global_block_protection(struct spi_nor *nor)
+static int __maybe_unused
+spi_nor_unlock_global_block_protection(struct spi_nor *nor)
 {
 	int ret;
 
@@ -4049,6 +4050,7 @@ static int spi_nor_init(struct spi_nor *nor)
 {
 	int err;
 
+#ifdef CONFIG_MTD_SPI_NOR_DISABLE_POWER_UP_WRITE_PROTECTION
 	/*
 	 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
 	 * with the software protection bits set.

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_init(struct spi_nor *nor) · line 4090

Quoted 7 lines
drivers/mtd/spi-nor/spi-nor.c +1 -0
@@ -4082,6 +4084,7 @@ static int spi_nor_init(struct spi_nor *nor)
 			return err;
 		}
 	}
+#endif
 
 	if (nor->quad_enable) {
 		err = nor->quad_enable(nor);

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author Applied
#3 lore raw

Re: [PATCH 1/5] mtd: spi-nor: fix description for int (*flash_is_locked)()

In reply to Vignesh Raghavendra
Quoted 4 lines · Vignesh Raghavendra

External E-Mail

Quoted 9 lines
Quoted 5 lines · On 17/07/19 2:18 PM, [email protected] wrote:

From: Tudor Ambarus <[email protected]>

The description was interleaved.

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git,
spi-nor/next branch.

Thanks,
ta