Skip to contentSkip to conversation

mtd: spi-nor: Check that BP bits are set properly

PATCH Superseded Aaron Sierra 2 participants 3 messages Oct 29, 2017 – Dec 4, 2017

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: spi-nor: Check that BP bits are set properly

Previously, the lock and unlock functions returned success even if the BP bits were not actually updated in the status register due to hardware write protection. Introduce write_sr_and_check() to write and read back the status register to ensure the desired BP bits are actually set as requested.

---
 drivers/mtd/spi-nor/spi-nor.c | 39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +27 -12
index 19c00072..8a87bd1 100644
@@ -550,6 +550,31 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
 	return ret;
 }
 
+/* Write status register and ensure bits in mask match written values */
+static int write_sr_and_check(struct spi_nor *nor, u8 new, u8 mask)
+{
+	int ret;
+
+	ret = spi_nor_wait_till_ready(nor);
+	if (ret)
+		return ret;
+
+	write_enable(nor);
+	ret = write_sr(nor, new);
+	if (ret)
+		return ret;
+
+	ret = spi_nor_wait_till_ready(nor);
+	if (ret)
+		return ret;
+
+	ret = read_sr(nor);
+	if (ret < 0)
+		return ret;
+
+	return ((ret & mask) != (new & mask)) ? -EIO : 0;
+}
+
 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
 				 uint64_t *len)
 {
@@ -648,7 +673,6 @@ static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 	loff_t lock_len;
 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
 	bool use_top;
-	int ret;
 
 	status_old = read_sr(nor);
 	if (status_old < 0)
@@ -712,11 +736,7 @@ static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 	if ((status_new & mask) < (status_old & mask))
 		return -EINVAL;
 
-	write_enable(nor);
-	ret = write_sr(nor, status_new);
-	if (ret)
-		return ret;
-	return spi_nor_wait_till_ready(nor);
+	return write_sr_and_check(nor, status_new, mask);
 }
 
 /*
@@ -733,7 +753,6 @@ static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 	loff_t lock_len;
 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
 	bool use_top;
-	int ret;
 
 	status_old = read_sr(nor);
 	if (status_old < 0)
@@ -800,11 +819,7 @@ static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 	if ((status_new & mask) > (status_old & mask))
 		return -EINVAL;
 
-	write_enable(nor);
-	ret = write_sr(nor, status_new);
-	if (ret)
-		return ret;
-	return spi_nor_wait_till_ready(nor);
+	return write_sr_and_check(nor, status_new, mask);
 }
 
 /*
Signature · 1 line
2.7.4
#1 lore raw

Hi Aaron,

Le 29/10/2017 à 17:11, Aaron Sierra a écrit :

Quoted 17 lines

Previously, the lock and unlock functions returned success even if the BP bits were not actually updated in the status register due to hardware write protection. Introduce write_sr_and_check() to write and read back the status register to ensure the desired BP bits are actually set as requested.

---
 drivers/mtd/spi-nor/spi-nor.c | 39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +0 -0
index 19c00072..8a87bd1 100644

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr) · line 554

Quoted 5 lines
drivers/mtd/spi-nor/spi-nor.c +2 -0
@@ -550,6 +550,31 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
 	return ret;
 }
 
+/* Write status register and ensure bits in mask match written values */
+static int write_sr_and_check(struct spi_nor *nor, u8 new, u8 mask)

Maybe keep the previous name 'status_new' to be consistent with stm_lock() and stm_unlock() and also because 'new' is a C++ reserved keyword so it might be better to avoid using it to name some variable (editor syntax coloring issue for instance).

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr) · line 561

Quoted 7 lines
drivers/mtd/spi-nor/spi-nor.c +7 -0
+{
+	int ret;
+
+	ret = spi_nor_wait_till_ready(nor);
+	if (ret)
+		return ret;
+

Why do you poll the status register here ?
Based on the source code before your patch, it should not be needed. Do you think it is ?

Otherwise, your patch looks good to me :)

Best regards,

Cyrille

Quoted 62 lines
drivers/mtd/spi-nor/spi-nor.c +18 -12
+	write_enable(nor);
+	ret = write_sr(nor, new);
+	if (ret)
+		return ret;
+
+	ret = spi_nor_wait_till_ready(nor);
+	if (ret)
+		return ret;
+
+	ret = read_sr(nor);
+	if (ret < 0)
+		return ret;
+
+	return ((ret & mask) != (new & mask)) ? -EIO : 0;
+}
+
 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
 				 uint64_t *len)
 {
@@ -648,7 +673,6 @@ static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 	loff_t lock_len;
 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
 	bool use_top;
-	int ret;
 
 	status_old = read_sr(nor);
 	if (status_old < 0)
@@ -712,11 +736,7 @@ static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 	if ((status_new & mask) < (status_old & mask))
 		return -EINVAL;
 
-	write_enable(nor);
-	ret = write_sr(nor, status_new);
-	if (ret)
-		return ret;
-	return spi_nor_wait_till_ready(nor);
+	return write_sr_and_check(nor, status_new, mask);
 }
 
 /*
@@ -733,7 +753,6 @@ static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 	loff_t lock_len;
 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
 	bool use_top;
-	int ret;
 
 	status_old = read_sr(nor);
 	if (status_old < 0)
@@ -800,11 +819,7 @@ static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 	if ((status_new & mask) > (status_old & mask))
 		return -EINVAL;
 
-	write_enable(nor);
-	ret = write_sr(nor, status_new);
-	if (ret)
-		return ret;
-	return spi_nor_wait_till_ready(nor);
+	return write_sr_and_check(nor, status_new, mask);
 }
 
 /*
#2 lore raw

----- Original Message -----

Quoted 2 lines

From: "Cyrille Pitchen" <[email protected]>
Sent: Thursday, November 30, 2017 7:57:27 AM

Hi Cyrille,

Quoted 24 lines

Hi Aaron,

Le 29/10/2017 à 17:11, Aaron Sierra a écrit :

Quoted 21 lines

Previously, the lock and unlock functions returned success even if the BP bits were not actually updated in the status register due to hardware write protection. Introduce write_sr_and_check() to write and read back the status register to ensure the desired BP bits are actually set as requested.

---
 drivers/mtd/spi-nor/spi-nor.c | 39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +0 -0
index 19c00072..8a87bd1 100644
@@ -550,6 +550,31 @@ static int spi_nor_erase(struct mtd_info *mtd, struct
 erase_info *instr)
 	return ret;
 }
 
Quoted 7 lines

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr) · line 554

Quoted 2 lines
drivers/mtd/spi-nor/spi-nor.c +2 -0
+/* Write status register and ensure bits in mask match written values */
+static int write_sr_and_check(struct spi_nor *nor, u8 new, u8 mask)

Maybe keep the previous name 'status_new' to be consistent with stm_lock() and stm_unlock() and also because 'new' is a C++ reserved keyword so it might be better to avoid using it to name some variable (editor syntax coloring issue for instance).

Sure, both of those reasons seem reasonable.

Quoted 11 lines

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr) · line 561

Quoted 7 lines
drivers/mtd/spi-nor/spi-nor.c +7 -0
+{
+	int ret;
+
+	ret = spi_nor_wait_till_ready(nor);
+	if (ret)
+		return ret;
+

Why do you poll the status register here ?
Based on the source code before your patch, it should not be needed. Do you think it is ?

This polling was due to an excess of caution. I've reviewed the preceding code and agree that this can be removed from the next version of this patch.

Quoted 1 line

Otherwise, your patch looks good to me :)

Thanks for your review!

-Aaron S.

Quoted 4 lines

Best regards,

Cyrille