Skip to contentSkip to conversation

dt-bindings: mtd: spi-nor: document new flag

PATCH In review Michael Walle 2 participants 6 messages Dec 14, 2019 – Dec 20, 2019

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

PATCH 1/2 dt-bindings: mtd: spi-nor: document new flag

Document the new flag "no-unlock".

---
Does the property need a prefix? I couldn't find any hint. If so, what should it be? "m25p," or "spi-nor," ?

 Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt | 6 ++++++
 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt +6 -0
index f03be904d3c2..2d305c893ed7 100644
@@ -78,6 +78,12 @@ Optional properties:
 		   cannot reboot properly if the flash is left in the "wrong"
 		   state. This boolean flag can be used on such systems, to
 		   denote the absence of a reliable reset mechanism.
+- no-unlock : By default, linux unlocks the whole flash because there
+		   are legacy flash devices which are locked by default
+		   after reset. Set this flag if you don't want linux to
+		   unlock the whole flash automatically. In this case you
+		   can control the non-volatile bits by the
+		   flash_lock/flash_unlock tools.
 
 Example:
 
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/2 mtd: spi-nor: add option to keep lock bits

Traditionally, linux unlocks the whole flash because there are legacy devices which has the write protections bits set by default at startup. If you actually want to use the flash protection bits, eg. because there is a read-only part for a bootloader, this automatic unlocking is harmful. If there is no hardware write protection in place (usually called WP#), a startup of the kernel just discards this protection.

Introduce a new device tree flag to indicate that the flash should not be automatically unlocked.

---
 drivers/mtd/spi-nor/spi-nor.c | 5 ++++-
 include/linux/mtd/spi-nor.h   | 1 +
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/spi-nor.c +4 -1
index f4afe123e9dc..d0bec0adf2f8 100644
@@ -4910,7 +4910,7 @@ static int spi_nor_quad_enable(struct spi_nor *nor)
  */
 static int spi_nor_unlock_all(struct spi_nor *nor)
 {
-	if (nor->flags & SNOR_F_HAS_LOCK)
+	if (nor->flags & SNOR_F_HAS_LOCK && !(nor->flags & SNOR_F_NO_UNLOCK))
 		return spi_nor_unlock(&nor->mtd, 0, nor->params.size);
 
 	return 0;
@@ -5159,6 +5159,9 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
 	if (of_property_read_bool(np, "broken-flash-reset"))
 		nor->flags |= SNOR_F_BROKEN_RESET;
 
+	if (of_property_read_bool(np, "no-unlock"))
+		nor->flags |= SNOR_F_NO_UNLOCK;
+
 	/*
 	 * Configure the SPI memory:
 	 * - select op codes for (Fast) Read, Page Program and Sector Erase.
include/linux/mtd/spi-nor.h +1 -0
index 5a4623fc586b..4cba5dda2d38 100644
@@ -244,6 +244,7 @@ enum spi_nor_option_flags {
 	SNOR_F_HAS_LOCK		= BIT(8),
 	SNOR_F_HAS_16BIT_SR	= BIT(9),
 	SNOR_F_NO_READ_CR	= BIT(10),
+	SNOR_F_NO_UNLOCK	= BIT(11),
 
 };
 
Signature · 1 line
2.20.1
#2 lore raw
In reply to Michael Walle

Hi,

Quoted 15 lines · Michael Walle

Document the new flag "no-unlock".

---
Does the property need a prefix? I couldn't find any hint. If so, what should it be? "m25p," or "spi-nor," ?

 Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt | 6 ++++++
 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt +0 -0
index f03be904d3c2..2d305c893ed7 100644

Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt Optional properties: · line 87

Quoted 10 lines
Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt +6 -0
@@ -78,6 +78,12 @@ Optional properties:
 		   cannot reboot properly if the flash is left in the "wrong"
 		   state. This boolean flag can be used on such systems, to
 		   denote the absence of a reliable reset mechanism.
+- no-unlock : By default, linux unlocks the whole flash because there
+		   are legacy flash devices which are locked by default
+		   after reset. Set this flag if you don't want linux to
+		   unlock the whole flash automatically. In this case you
+		   can control the non-volatile bits by the
+		   flash_lock/flash_unlock tools.
 

Current SPI NOR framework unconditionally unlocks entire flash which I agree is not the best thing to do, but I don't think we need new DT property here. MTD cmdline partitions and DT partitions already provide a way to specify that a partition should remain locked[1][2]

SPI NOR framework should instead set MTD_POWERUP_LOCK flags in mtd->flags for flash devices that power up with lock bits set. And MTD core will take care of unlocking flash regions while taking into account partition flags defined by user as part of MTD partitions defined in DT or via cmdline args.

So that change should to be set MTD_POWERUP_LOCK for
in SPI NOR core. Can you check below[3] (untested) diff helps? This should prevent unlocking partitions that are to remain locked as specified in DT/cmdline

[1] Documentation/devicetree/bindings/mtd/partition.txt
[2] drivers/mtd/parsers/cmdlinepart.c (see "lk" parameter)

[3]

drivers/mtd/spi-nor/spi-nor.c +5 -22
index 1082b6bb1393..6adb950849f6 100644
@@ -4914,23 +4914,6 @@ static int spi_nor_quad_enable(struct spi_nor *nor)
 	return nor->params.quad_enable(nor);
 }
 
-/**
- * spi_nor_unlock_all() - Unlocks the entire flash memory array.
- * @nor:	pointer to a 'struct spi_nor'.
- *
- * Some SPI NOR flashes are write protected by default after a power-on reset
- * cycle, in order to avoid inadvertent writes during power-up. Backward
- * compatibility imposes to unlock the entire flash memory array at power-up
- * by default.
- */
-static int spi_nor_unlock_all(struct spi_nor *nor)
-{
-	if (nor->flags & SNOR_F_HAS_LOCK)
-		return spi_nor_unlock(&nor->mtd, 0, nor->params.size);
-
-	return 0;
-}
-
 static int spi_nor_init(struct spi_nor *nor)
 {
 	int err;
@@ -4941,11 +4924,11 @@ static int spi_nor_init(struct spi_nor *nor)
 		return err;
 	}
 
-	err = spi_nor_unlock_all(nor);
-	if (err) {
-		dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
-		return err;
-	}
+	/*
+	 * Flashes may power up locked. Set this flag so that MTD core
+	 * takes care of unlocking partitions as required.
+	 */
+	nor->mtd.flags |= MTD_POWERUP_LOCK;
 
 	if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES)) {
 		/*
Signature · 6 lines
Regards
Vignesh

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

Hi,

Am 2019-12-16 09:54, schrieb Vignesh Raghavendra:

Quoted 25 lines

Hi,

Quoted 22 lines · Michael Walle

Document the new flag "no-unlock".

---
Does the property need a prefix? I couldn't find any hint. If so, what should it be? "m25p," or "spi-nor," ?

 Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt | 6 ++++++
 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt +4 -0
index f03be904d3c2..2d305c893ed7 100644
@@ -78,6 +78,12 @@ Optional properties:
 		   cannot reboot properly if the flash is left in the "wrong"
 		   state. This boolean flag can be used on such systems, to
 		   denote the absence of a reliable reset mechanism.
+- no-unlock : By default, linux unlocks the whole flash because there
+		   are legacy flash devices which are locked by default
+		   after reset. Set this flag if you don't want linux to
+		   unlock the whole flash automatically. In this case you
Quoted 8 lines

Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt Optional properties: · line 87

Quoted 3 lines
Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt +2 -0
+		   can control the non-volatile bits by the
+		   flash_lock/flash_unlock tools.
 

Current SPI NOR framework unconditionally unlocks entire flash which I agree is not the best thing to do, but I don't think we need new DT property here. MTD cmdline partitions and DT partitions already provide a way to specify that a partition should remain locked[1][2]

I know that the MTD layer has the same kind of unlocking. But that unlocking is done on a per mtd partition basis. Eg. consider something like the following

mtd1 bootloader (locked)
mtd2 firmware (locked)
mtd3 kernel
mtd4 environment

Further assume, that the end of mtd2 aligns with one of the possible locking areas which are supported by the flash chip. Eg. the first quarter.

The mtd layer would do two (or four, if "lock" property is set) unlock() calls, one for mtd1 and one for mtd2.

My point here is, that the mtd partitions doesn't always map to the locking regions of the SPI flash (at least if the are not merged together).

Quoted 10 lines

SPI NOR framework should instead set MTD_POWERUP_LOCK flags in mtd->flags for flash devices that power up with lock bits set. And MTD core will take care of unlocking flash regions while taking into account partition flags defined by user as part of MTD partitions defined in DT or via cmdline args.

So that change should to be set MTD_POWERUP_LOCK for
in SPI NOR core. Can you check below[3] (untested) diff helps? This should prevent unlocking partitions that are to remain locked as specified in DT/cmdline

As this change may help my use-case, unlocking is skipped because the partitions are marked as read only; I fear that the old behaviour will change. See above.

Mhh. thinking more about it, doesn't the calls also wear out the non-volatile bits in the NOR flash?

In any case, I'll try your suggestion.

-michael

Quoted 56 lines

[1] Documentation/devicetree/bindings/mtd/partition.txt
[2] drivers/mtd/parsers/cmdlinepart.c (see "lk" parameter)

[3]

drivers/mtd/spi-nor/spi-nor.c +0 -5
index 1082b6bb1393..6adb950849f6 100644
@@ -4914,23 +4914,6 @@ static int spi_nor_quad_enable(struct spi_nor 
 *nor)
 	return nor->params.quad_enable(nor);
 }
 
-/**
- * spi_nor_unlock_all() - Unlocks the entire flash memory array.
- * @nor:	pointer to a 'struct spi_nor'.
- *
- * Some SPI NOR flashes are write protected by default after a 

power-on reset
- * cycle, in order to avoid inadvertent writes during power-up. Backward
- * compatibility imposes to unlock the entire flash memory array at power-up
- * by default.
- */

drivers/mtd/spi-nor/spi-nor.c +5 -13
-static int spi_nor_unlock_all(struct spi_nor *nor)
-{
-	if (nor->flags & SNOR_F_HAS_LOCK)
-		return spi_nor_unlock(&nor->mtd, 0, nor->params.size);
-
-	return 0;
-}
-
 static int spi_nor_init(struct spi_nor *nor)
 {
 	int err;
@@ -4941,11 +4924,11 @@ static int spi_nor_init(struct spi_nor *nor)
 		return err;
 	}
 
-	err = spi_nor_unlock_all(nor);
-	if (err) {
-		dev_dbg(nor->dev, "Failed to unlock the entire flash memory 
 array\n");
-		return err;
-	}
+	/*
+	 * Flashes may power up locked. Set this flag so that MTD core
+	 * takes care of unlocking partitions as required.
+	 */
+	nor->mtd.flags |= MTD_POWERUP_LOCK;
 
 	if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES)) {

/*

#4 lore raw

Hi Michael,

[...]

Quoted 25 lines
Quoted 8 lines
Quoted 7 lines

+- no-unlock : By default, linux unlocks the whole flash because there +           are legacy flash devices which are locked by default +           after reset. Set this flag if you don't want linux to +           unlock the whole flash automatically. In this case you +           can control the non-volatile bits by the
+           flash_lock/flash_unlock tools.

Quoted 4 lines

Current SPI NOR framework unconditionally unlocks entire flash which I agree is not the best thing to do, but I don't think we need new DT property here. MTD cmdline partitions and DT partitions already provide a way to specify that a partition should remain locked[1][2]

I know that the MTD layer has the same kind of unlocking. But that unlocking is done on a per mtd partition basis. Eg. consider something like the following

 mtd1 bootloader  (locked)
 mtd2 firmware    (locked)
 mtd3 kernel
 mtd4 environment

Further assume, that the end of mtd2 aligns with one of the possible locking areas which are supported by the flash chip. Eg. the first quarter.

Quoted 8 lines

The mtd layer would do two (or four, if "lock" property is set) unlock() calls, one for mtd1 and one for mtd2.

My point here is, that the mtd partitions doesn't always map to the locking regions of the SPI flash (at least if the are not merged together).

You are right! This will be an issue if existing partitions are not aligned to locking regions.

I take my comments back... But I am not sure if a new DT property is the needed. This does not describe HW and is specific to Linux SPI NOR stack. How about a module parameter instead?
Module parameter won't provide per flash granularity in controlling unlocking behavior. But I don't think that matters.

Tudor,

You had a patch doing something similar. Does module param sound good to you?

Signature · 2 lines
Regards
Vignesh
#5 lore raw

Hi Vignesh,

Am 2019-12-19 06:33, schrieb Vignesh Raghavendra:

Quoted 38 lines

Hi Michael,

[...]

Quoted 26 lines
Quoted 9 lines
Quoted 8 lines

+- no-unlock : By default, linux unlocks the whole flash because there
+           are legacy flash devices which are locked by default +           after reset. Set this flag if you don't want linux to +           unlock the whole flash automatically. In this case you +           can control the non-volatile bits by the
+           flash_lock/flash_unlock tools.

Quoted 4 lines

Current SPI NOR framework unconditionally unlocks entire flash which I agree is not the best thing to do, but I don't think we need new DT property here. MTD cmdline partitions and DT partitions already provide a way to specify that a partition should remain locked[1][2]

I know that the MTD layer has the same kind of unlocking. But that unlocking is done on a per mtd partition basis. Eg. consider something like the following

 mtd1 bootloader  (locked)
 mtd2 firmware    (locked)
 mtd3 kernel
 mtd4 environment

Further assume, that the end of mtd2 aligns with one of the possible locking areas which are supported by the flash chip. Eg. the first quarter.

Quoted 8 lines

The mtd layer would do two (or four, if "lock" property is set) unlock() calls, one for mtd1 and one for mtd2.

My point here is, that the mtd partitions doesn't always map to the locking regions of the SPI flash (at least if the are not merged together).

Quoted 8 lines

You are right! This will be an issue if existing partitions are not aligned to locking regions.

I take my comments back... But I am not sure if a new DT property is the needed. This does not describe HW and is specific to Linux SPI NOR stack. How about a module parameter instead?
Module parameter won't provide per flash granularity in controlling unlocking behavior. But I don't think that matters.

I don't argue against having a kernel parameter, but just wanting to point out another alternative (which might be controversial):

- What is the purpose of this unlock_all() at all. Apparently there are some flashes which have the protection bits set. Either at startup (and then they are non-volatile) or they come in that state out of
the
factory. The latter makes little sense IMHO.

- Actually, all newer flashes we've used have these bits non-volatile
and
are unlocked by default.

So can't we have a whitelist (ie. a new flag in the spi_nor_ids) which supresses the unlock if they haven't any block protections bit set according to the manual? Because in this case the unlocking makes never sense IMHO.

-michael

Quoted 5 lines

Tudor,

You had a patch doing something similar. Does module param sound good to you?