Skip to contentSkip to conversation

mtd: nand: brcmnand: Change brcmnand_set_wp() prototype

PATCH In review Kamal Dasu 4 participants 6 messages Feb 28, 2017

Subsystems (2)
lore
Share & tools
lore

Kamal Dasu

author
#0 lore raw

PATCH v4 1/2 mtd: nand: brcmnand: Change brcmnand_set_wp() prototype

Changing brcmnand_set_wp() prototype in prepration for refactoring the nand write protect logic to add flash status byte check for the protection bit.

---
 drivers/mtd/nand/brcmnand/brcmnand.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
drivers/mtd/nand/brcmnand/brcmnand.c +17 -10
index 42ebd73..c7c4efe 100644
@@ -765,11 +765,14 @@ enum {
 	CS_SELECT_AUTO_DEVICE_ID_CFG		= BIT(30),
 };
 
-static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
+static int brcmnand_set_wp(struct brcmnand_host *host, int en)
 {
+	struct brcmnand_controller *ctrl = host->ctrl;
 	u32 val = en ? CS_SELECT_NAND_WP : 0;
 
 	brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
+
+	return 0;
 }
 
 /***********************************************************************
@@ -1029,7 +1032,7 @@ static void brcmnand_wp(struct mtd_info *mtd, int wp)
 			dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
 			old_wp = wp;
 		}
-		brcmnand_set_wp(ctrl, wp);
+		brcmnand_set_wp(host, wp);
 	}
 }
 
@@ -2462,14 +2465,6 @@ int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
 	/* Disable XOR addressing */
 	brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
 
-	if (ctrl->features & BRCMNAND_HAS_WP) {
-		/* Permanently disable write protection */
-		if (wp_on == 2)
-			brcmnand_set_wp(ctrl, false);
-	} else {
-		wp_on = 0;
-	}
-
 	/* IRQ */
 	ctrl->irq = platform_get_irq(pdev, 0);
 	if ((int)ctrl->irq < 0) {
@@ -2522,6 +2517,18 @@ int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
 			}
 
 			list_add_tail(&host->node, &ctrl->host_list);
+
+			if (ctrl->features & BRCMNAND_HAS_WP) {
+				/* Permanently disable write protection */
+				if (wp_on == 2) {
+					ret = brcmnand_set_wp(host, false);
+					if (ret)
+						goto err;
+				}
+			} else {
+				wp_on = 0;
+			}
+
 		}
 	}
 
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v4 2/2 mtd: nand: brcmnand: Check flash #WP pin status before nand erase/program

brcmnand controller v6.x and v7.x lets driver to enable disable #WP pin via NAND_WP bit in CS_SELECT register. Driver implementation assumes that setting/resetting the bit would assert/de-assert #WP pin instantaneously from the flash part's perspective, and was proceeding to erase/program without verifying flash status byte for protection bit. In rigorous testing this was causing rare data corruptions with erase and/or subsequent programming. To fix this check NAND status in addition to controller #WP pin status to make sure both sides are ready to accept new commands.

---
 drivers/mtd/nand/brcmnand/brcmnand.c | 70 +++++++++++++++++++++++++++++++++---
 1 file changed, 65 insertions(+), 5 deletions(-)
drivers/mtd/nand/brcmnand/brcmnand.c +65 -5
index c7c4efe..af32685 100644
@@ -101,6 +101,9 @@ struct brcm_nand_dma_desc {
 #define BRCMNAND_MIN_BLOCKSIZE	(8 * 1024)
 #define BRCMNAND_MIN_DEVSIZE	(4ULL * 1024 * 1024)
 
+#define NAND_CTRL_RDY			(INTFC_CTLR_READY | INTFC_FLASH_READY)
+#define NAND_POLL_STATUS_TIMEOUT_MS	100
+
 /* Controller feature flags */
 enum {
 	BRCMNAND_HAS_1K_SECTORS			= BIT(0),
@@ -765,12 +768,69 @@ enum {
 	CS_SELECT_AUTO_DEVICE_ID_CFG		= BIT(30),
 };
 
-static int brcmnand_set_wp(struct brcmnand_host *host, int en)
+static int bcmnand_ctrl_poll_status(struct brcmnand_controller *ctrl,
+				    u32 mask, u32 expected_val,
+				    unsigned long timeout_ms)
+{
+	unsigned long limit;
+	u32 val;
+
+	if (!timeout_ms)
+		timeout_ms = NAND_POLL_STATUS_TIMEOUT_MS;
+
+	limit = jiffies + msecs_to_jiffies(timeout_ms);
+	do {
+		val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
+		if ((val & mask) == expected_val)
+			return 0;
+
+		cpu_relax();
+	} while (time_after(limit, jiffies));
+
+	dev_warn(ctrl->dev, "timeout on status poll (expected %x got %x)\n",
+		 expected_val, val & mask);
+
+	return -ETIMEDOUT;
+}
+
+static inline void brcmnand_set_wp_reg(struct brcmnand_controller *ctrl, int en)
 {
-	struct brcmnand_controller *ctrl = host->ctrl;
 	u32 val = en ? CS_SELECT_NAND_WP : 0;
 
 	brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
+}
+
+static int brcmnand_set_wp(struct brcmnand_host *host, int en)
+{
+	struct brcmnand_controller *ctrl = host->ctrl;
+	struct mtd_info *mtd = nand_to_mtd(&host->chip);
+	struct nand_chip *chip = mtd_to_nand(mtd);
+	int ret;
+
+	/*
+	 * make sure ctrl/flash ready before and after
+	 * changing state of WP PIN
+	 */
+	ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY | NAND_STATUS_READY,
+				       NAND_CTRL_RDY | NAND_STATUS_READY, 0);
+	if (ret)
+		return ret;
+
+	brcmnand_set_wp_reg(ctrl, en);
+	chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
+	/* NAND_STATUS_WP 0x00 = protected, 0x80 = not protected */
+	ret = bcmnand_ctrl_poll_status(ctrl,
+				       NAND_CTRL_RDY | NAND_STATUS_READY |
+				       NAND_STATUS_WP,
+				       NAND_CTRL_RDY | NAND_STATUS_READY |
+				       (en ? 0 : NAND_STATUS_WP), 0);
+
+	if (ret) {
+		dev_err_ratelimited(&host->pdev->dev,
+				    "nand #WP expected %s\n",
+				    en ? "On" : "Off");
+		return ret;
+	}
 
 	return 0;
 }
@@ -1160,15 +1220,15 @@ static irqreturn_t brcmnand_dma_irq(int irq, void *data)
 static void brcmnand_send_cmd(struct brcmnand_host *host, int cmd)
 {
 	struct brcmnand_controller *ctrl = host->ctrl;
-	u32 intfc;
+	int ret;
 
 	dev_dbg(ctrl->dev, "send native cmd %d addr_lo 0x%x\n", cmd,
 		brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS));
 	BUG_ON(ctrl->cmd_pending != 0);
 	ctrl->cmd_pending = cmd;
 
-	intfc = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
-	WARN_ON(!(intfc & INTFC_CTLR_READY));
+	ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, 0);
+	WARN_ON(ret);
 
 	mb(); /* flush previous writes */
 	brcmnand_write_reg(ctrl, BRCMNAND_CMD_START,
Signature · 1 line
1.9.1
#2 lore raw
In reply to Kamal Dasu
Quoted 58 lines · Kamal Dasu

Changing brcmnand_set_wp() prototype in prepration for refactoring the nand write protect logic to add flash status byte check for the protection bit.

---
 drivers/mtd/nand/brcmnand/brcmnand.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
drivers/mtd/nand/brcmnand/brcmnand.c +7 -10
index 42ebd73..c7c4efe 100644
@@ -765,11 +765,14 @@ enum {
 	CS_SELECT_AUTO_DEVICE_ID_CFG		= BIT(30),
 };
 
-static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
+static int brcmnand_set_wp(struct brcmnand_host *host, int en)
 {
+	struct brcmnand_controller *ctrl = host->ctrl;
 	u32 val = en ? CS_SELECT_NAND_WP : 0;
 
 	brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
+
+	return 0;
 }
 
 /***********************************************************************
@@ -1029,7 +1032,7 @@ static void brcmnand_wp(struct mtd_info *mtd, int wp)
 			dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
 			old_wp = wp;
 		}
-		brcmnand_set_wp(ctrl, wp);
+		brcmnand_set_wp(host, wp);
 	}
 }
 
@@ -2462,14 +2465,6 @@ int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
 	/* Disable XOR addressing */
 	brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
 
-	if (ctrl->features & BRCMNAND_HAS_WP) {
-		/* Permanently disable write protection */
-		if (wp_on == 2)
-			brcmnand_set_wp(ctrl, false);
-	} else {
-		wp_on = 0;
-	}
-
 	/* IRQ */
 	ctrl->irq = platform_get_irq(pdev, 0);
 	if ((int)ctrl->irq < 0) {
@@ -2522,6 +2517,18 @@ int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
 			}
 
 			list_add_tail(&host->node, &ctrl->host_list);
+
+			if (ctrl->features & BRCMNAND_HAS_WP) {

drivers/mtd/nand/brcmnand/brcmnand.c int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc) · line 2531

Quoted 10 lines
drivers/mtd/nand/brcmnand/brcmnand.c +10 -0
+				/* Permanently disable write protection */
+				if (wp_on == 2) {
+					ret = brcmnand_set_wp(host, false);
+					if (ret)
+						goto err;
+				}
+			} else {
+				wp_on = 0;
+			}
+

Would it be worth it to move this into separate function ? Or maybe make brcmnand_set_wp() check for BRCMNAND_HAS_WP and only set the WP if the controller is actually capable of that ? I think that might make the code readability a bit better ...

Quoted 4 lines
drivers/mtd/nand/brcmnand/brcmnand.c +0 -0
 		}
 	}
 
Signature · 2 lines
Best regards,
Marek Vasut
#3 lore raw

Marek,

Quoted 66 lines · Marek Vasut
Quoted 65 lines · Kamal Dasu

Changing brcmnand_set_wp() prototype in prepration for refactoring the nand write protect logic to add flash status byte check for the protection bit.

---
 drivers/mtd/nand/brcmnand/brcmnand.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
drivers/mtd/nand/brcmnand/brcmnand.c +14 -10
index 42ebd73..c7c4efe 100644
@@ -765,11 +765,14 @@ enum {
      CS_SELECT_AUTO_DEVICE_ID_CFG            = BIT(30),
 };
 
-static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
+static int brcmnand_set_wp(struct brcmnand_host *host, int en)
 {
+     struct brcmnand_controller *ctrl = host->ctrl;
      u32 val = en ? CS_SELECT_NAND_WP : 0;
 
      brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
+
+     return 0;
 }
 
 /***********************************************************************
@@ -1029,7 +1032,7 @@ static void brcmnand_wp(struct mtd_info *mtd, int wp)
                      dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
                      old_wp = wp;
              }
-             brcmnand_set_wp(ctrl, wp);
+             brcmnand_set_wp(host, wp);
      }
 }
 
@@ -2462,14 +2465,6 @@ int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
      /* Disable XOR addressing */
      brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
 
-     if (ctrl->features & BRCMNAND_HAS_WP) {
-             /* Permanently disable write protection */
-             if (wp_on == 2)
-                     brcmnand_set_wp(ctrl, false);
-     } else {
-             wp_on = 0;
-     }
-
      /* IRQ */
      ctrl->irq = platform_get_irq(pdev, 0);
      if ((int)ctrl->irq < 0) {
@@ -2522,6 +2517,18 @@ int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
                      }
 
                      list_add_tail(&host->node, &ctrl->host_list);
+
+                     if (ctrl->features & BRCMNAND_HAS_WP) {
+                             /* Permanently disable write protection */
+                             if (wp_on == 2) {
+                                     ret = brcmnand_set_wp(host, false);
+                                     if (ret)
+                                             goto err;
+                             }
+                     } else {
Quoted 13 lines

drivers/mtd/nand/brcmnand/brcmnand.c int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc) · line 2531

Quoted 3 lines
drivers/mtd/nand/brcmnand/brcmnand.c +3 -0
+                             wp_on = 0;
+                     }
+

Would it be worth it to move this into separate function ? Or maybe make brcmnand_set_wp() check for BRCMNAND_HAS_WP and only set the WP if the controller is actually capable of that ? I think that might make the code readability a bit better ...

Quoted 4 lines
drivers/mtd/nand/brcmnand/brcmnand.c +0 -0
              }
      }
 

The above code is called during the probe and only when the bootline wants to permanently set the wp off( wp == 2) hence structured that way. I would prefer to leave it that way.

In all other cases there is already brcmand_wp(mtd_info *mtd, int wp) checks and maintains the wp state.

Quoted 4 lines

--
Best regards,
Marek Vasut

Thanks
Kamal

#4 lore raw
In reply to Kamal Dasu
Quoted 39 lines · On Tue, 28 Feb 2017 11:55:25 -0500 Kamal Dasu <[email protected]> wrote:

Changing brcmnand_set_wp() prototype in prepration for refactoring the nand write protect logic to add flash status byte check for the protection bit.

---
 drivers/mtd/nand/brcmnand/brcmnand.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
drivers/mtd/nand/brcmnand/brcmnand.c +5 -2
index 42ebd73..c7c4efe 100644
@@ -765,11 +765,14 @@ enum {
 	CS_SELECT_AUTO_DEVICE_ID_CFG		= BIT(30),
 };
 
-static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
+static int brcmnand_set_wp(struct brcmnand_host *host, int en)
 {
+	struct brcmnand_controller *ctrl = host->ctrl;
 	u32 val = en ? CS_SELECT_NAND_WP : 0;
 
 	brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
+
+	return 0;
 }
 
 /***********************************************************************
@@ -1029,7 +1032,7 @@ static void brcmnand_wp(struct mtd_info *mtd, int wp)
 			dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
 			old_wp = wp;
 		}
-		brcmnand_set_wp(ctrl, wp);
+		brcmnand_set_wp(host, wp);
 	}
 }
 
@@ -2462,14 +2465,6 @@ int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
 	/* Disable XOR addressing */

drivers/mtd/nand/brcmnand/brcmnand.c int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc) · line 2472

Quoted 10 lines
drivers/mtd/nand/brcmnand/brcmnand.c +0 -8
 	brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
 
-	if (ctrl->features & BRCMNAND_HAS_WP) {
-		/* Permanently disable write protection */
-		if (wp_on == 2)
-			brcmnand_set_wp(ctrl, false);
-	} else {
-		wp_on = 0;
-	}
-
	if (!(ctrl->features & BRCMNAND_HAS_WP))
		wp_on = 0;
Quoted 9 lines
drivers/mtd/nand/brcmnand/brcmnand.c +2 -0
 	/* IRQ */
 	ctrl->irq = platform_get_irq(pdev, 0);
 	if ((int)ctrl->irq < 0) {
@@ -2522,6 +2517,18 @@ int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
 			}
 
 			list_add_tail(&host->node, &ctrl->host_list);
+
+			if (ctrl->features & BRCMNAND_HAS_WP) {

drivers/mtd/nand/brcmnand/brcmnand.c int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc) · line 2531

Quoted 10 lines
drivers/mtd/nand/brcmnand/brcmnand.c +10 -0
+				/* Permanently disable write protection */
+				if (wp_on == 2) {
+					ret = brcmnand_set_wp(host, false);
+					if (ret)
+						goto err;
+				}
+			} else {
+				wp_on = 0;
+			}
+
			/* Permanently disable write protection */
			if (wp_on == 2) {
				ret = brcmnand_set_wp(host, false);
				if (ret)
					goto err;
			}
Quoted 3 lines
drivers/mtd/nand/brcmnand/brcmnand.c +0 -0
 		}
 	}
 
author
#5 lore raw

Boris,

Quoted 74 lines · Boris Brezillon
Quoted 39 lines · On Tue, 28 Feb 2017 11:55:25 -0500 Kamal Dasu <[email protected]> wrote:

Changing brcmnand_set_wp() prototype in prepration for refactoring the nand write protect logic to add flash status byte check for the protection bit.

---
 drivers/mtd/nand/brcmnand/brcmnand.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
drivers/mtd/nand/brcmnand/brcmnand.c +5 -2
index 42ebd73..c7c4efe 100644
@@ -765,11 +765,14 @@ enum {
      CS_SELECT_AUTO_DEVICE_ID_CFG            = BIT(30),
 };
 
-static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
+static int brcmnand_set_wp(struct brcmnand_host *host, int en)
 {
+     struct brcmnand_controller *ctrl = host->ctrl;
      u32 val = en ? CS_SELECT_NAND_WP : 0;
 
      brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
+
+     return 0;
 }
 
 /***********************************************************************
@@ -1029,7 +1032,7 @@ static void brcmnand_wp(struct mtd_info *mtd, int wp)
                      dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
                      old_wp = wp;
              }
-             brcmnand_set_wp(ctrl, wp);
+             brcmnand_set_wp(host, wp);
      }
 }
 
@@ -2462,14 +2465,6 @@ int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
      /* Disable XOR addressing */

drivers/mtd/nand/brcmnand/brcmnand.c int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc) · line 2472

Quoted 10 lines
drivers/mtd/nand/brcmnand/brcmnand.c +0 -8
      brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
 
-     if (ctrl->features & BRCMNAND_HAS_WP) {
-             /* Permanently disable write protection */
-             if (wp_on == 2)
-                     brcmnand_set_wp(ctrl, false);
-     } else {
-             wp_on = 0;
-     }
-
        if (!(ctrl->features & BRCMNAND_HAS_WP))
                wp_on = 0;
Quoted 18 lines
drivers/mtd/nand/brcmnand/brcmnand.c +11 -0
      /* IRQ */
      ctrl->irq = platform_get_irq(pdev, 0);
      if ((int)ctrl->irq < 0) {
@@ -2522,6 +2517,18 @@ int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
                      }
 
                      list_add_tail(&host->node, &ctrl->host_list);
+
+                     if (ctrl->features & BRCMNAND_HAS_WP) {
+                             /* Permanently disable write protection */
+                             if (wp_on == 2) {
+                                     ret = brcmnand_set_wp(host, false);
+                                     if (ret)
+                                             goto err;
+                             }
+                     } else {
+                             wp_on = 0;
+                     }
Quoted 14 lines

drivers/mtd/nand/brcmnand/brcmnand.c int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc) · line 2531

Quoted 1 line
drivers/mtd/nand/brcmnand/brcmnand.c +1 -0
+
                        /* Permanently disable write protection */
                        if (wp_on == 2) {
                                ret = brcmnand_set_wp(host, false);
                                if (ret)
                                        goto err;
                        }
Quoted 3 lines
drivers/mtd/nand/brcmnand/brcmnand.c +0 -0
              }
      }
 

Let me rework this and send a new patch. Actually I just need to set the controller wp bit and the wp value since I do not need to worry about the status since we are not going to try and change the #WP at all in this case when we send the erase/program command. brcmnand_set_wp_reg() calling this should be sufficient during probe.