Skip to contentSkip to conversation

mtd: spi-nor: add unlock all config option

PATCH New Michael Walle 1 participant 2 messages Dec 9, 2020 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 mtd: spi-nor: add unlock all config option

Provide an explicit configuration option to disable default "unlock all" of any flash chip which supports locking. It doesn't make sense to automatically unprotect the entire flash on each u-boot startup if the block protection bits are actually used.

Traditionally, the unlock was there to be able to write to flash devices which powered-up with the block protection bits set. Over time this feature creeped into all flash devices which support locking.

For a more detailed description and discussion see:
https://lore.kernel.org/linux-mtd/20201203162959.29589-8-michael at walle.cc/

Keep things simple in u-boot and just provide a configration option to disable this behavior which can be set per board.

---
 drivers/mtd/spi/Kconfig        | 10 ++++++++++
 drivers/mtd/spi/spi-nor-core.c |  9 +++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
drivers/mtd/spi/Kconfig +10 -0
index 018e8c597e..bb8e8a9153 100644
@@ -95,6 +95,16 @@ config SPI_FLASH_BAR
 	  Bank/Extended address registers are used to access the flash
 	  which has size > 16MiB in 3-byte addressing.
 
+config SPI_FLASH_UNLOCK_ALL
+	bool "Unlock the entire SPI flash on u-boot startup"
+	default y
+	help
+	 Some flashes tend to power up with the software write protection
+	 bits set. If this option is set, the whole flash will be unlocked.
+
+	 For legacy reasons, this option default to y. But if you intend to
+	 actually use the software protection bits you should say n here.
+
 config SF_DUAL_FLASH
 	bool "SPI DUAL flash memory support"
 	help
drivers/mtd/spi/spi-nor-core.c +5 -4
index e16b0e1462..ef426dac02 100644
@@ -2443,10 +2443,11 @@ static int spi_nor_init(struct spi_nor *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) {
+	if (IS_ENABLED(CONFIG_SPI_FLASH_UNLOCK_ALL) &&
+	    (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)) {
 		write_enable(nor);
 		write_sr(nor, 0);
 		spi_nor_wait_till_ready(nor);
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/2 board: kontron: disable flash unlock all

Although the status register is protected by the hardware write protection, there is a hardware jumper to disable that hardware write protection. Thus if a user would set this jumper any u-boot start would disable the write protection altogether.

Circumvent that by not disabling the write protection in the first place.

---
 configs/kontron_sl28_defconfig | 1 +
 1 file changed, 1 insertion(+)
configs/kontron_sl28_defconfig +1 -0
index c1a096799c..12720f343e 100644
@@ -73,6 +73,7 @@ CONFIG_MMC_HS400_SUPPORT=y
 CONFIG_FSL_ESDHC=y
 CONFIG_FSL_ESDHC_SUPPORT_ADMA2=y
 CONFIG_DM_SPI_FLASH=y
+# CONFIG_SPI_FLASH_UNLOCK_ALL is not set
 CONFIG_SPI_FLASH_WINBOND=y
 # CONFIG_SPI_FLASH_USE_4K_SECTORS is not set
 CONFIG_PHYLIB=y
Signature · 1 line
2.20.1