From: Ashok Reddy Soma <ashok.reddy.s...@xilinx.com>

Write enable(06h) command will be sent to a flash device to
set the write enable latch bit before every program, erase,
write command. After that write disable command (04h) needs
to be sent to clear the write enable latch.

This write_disable() is missing at the majority of the places
in the driver, add it to clear write enable latch.

Signed-off-by: Ashok Reddy Soma <ashok.reddy.s...@xilinx.com>
Signed-off-by: Tejas Bhumkar <tejas.arvind.bhum...@amd.com>
Acked-by: Amit Kumar Mahapatra <amit.kumar-mahapa...@xilinx.com>
---
 drivers/mtd/spi/spi-nor-core.c | 47 +++++++++++++++++++++++++++++-----
 1 file changed, 41 insertions(+), 6 deletions(-)

diff --git a/drivers/mtd/spi/spi-nor-core.c b/drivers/mtd/spi/spi-nor-core.c
index 454ae6cd4e..d790116994 100644
--- a/drivers/mtd/spi/spi-nor-core.c
+++ b/drivers/mtd/spi/spi-nor-core.c
@@ -873,6 +873,7 @@ static int spi_nor_wait_till_ready(struct spi_nor *nor)
 static int clean_bar(struct spi_nor *nor)
 {
        u8 cmd, bank_sel = 0;
+       int ret;
 
        if (nor->bank_curr == 0)
                return 0;
@@ -880,7 +881,11 @@ static int clean_bar(struct spi_nor *nor)
        nor->bank_curr = 0;
        write_enable(nor);
 
-       return nor->write_reg(nor, cmd, &bank_sel, 1);
+       ret = nor->write_reg(nor, cmd, &bank_sel, 1);
+       if (ret)
+               return ret;
+
+       return write_disable(nor);
 }
 
 static int write_bar(struct spi_nor *nor, u32 offset)
@@ -1070,11 +1075,15 @@ static int spi_nor_erase(struct mtd_info *mtd, struct 
erase_info *instr)
                                nor->spi->flags &= ~SPI_XFER_U_PAGE;
                        }
                }
+               if (nor->addr_width == 3) {
 #ifdef CONFIG_SPI_FLASH_BAR
-               ret = write_bar(nor, addr);
-               if (ret < 0)
-                       goto erase_err;
+               /* Update Extended Address Register */
+                       ret = write_bar(nor, addr);
+                       if (ret < 0)
+                               goto erase_err;
 #endif
+               }
+
                ret = write_enable(nor);
                if (ret < 0)
                        goto erase_err;
@@ -1195,6 +1204,10 @@ static int write_sr_and_check(struct spi_nor *nor, u8 
status_new, u8 mask)
        if (ret)
                return ret;
 
+       ret = write_disable(nor);
+       if (ret)
+               return ret;
+
        ret = read_sr(nor);
        if (ret < 0)
                return ret;
@@ -1758,13 +1771,18 @@ static int sst26_lock_ctl(struct spi_nor *nor, loff_t 
ofs, uint64_t len, enum lo
        if (ctl == SST26_CTL_CHECK)
                return 0;
 
+       /* Write latch enable before write operation */
+       ret = write_enable(nor);
+       if (ret)
+               return ret;
+
        ret = nor->write_reg(nor, SPINOR_OP_WRITE_BPR, bpr_buff, bpr_size);
        if (ret < 0) {
                dev_err(nor->dev, "fail to write block-protection register\n");
                return ret;
        }
 
-       return 0;
+       return write_disable(nor);
 }
 
 static int sst26_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
@@ -2138,7 +2156,7 @@ static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
                return ret;
        }
 
-       return 0;
+       return write_disable(nor);
 }
 
 /**
@@ -4248,6 +4266,7 @@ static int spi_nor_octal_dtr_enable(struct spi_nor *nor)
 
 static int spi_nor_init(struct spi_nor *nor)
 {
+       u8 sr2;
        int err;
 
        if (nor->flags & SNOR_F_HAS_PARALLEL)
@@ -4271,6 +4290,22 @@ static int spi_nor_init(struct spi_nor *nor)
                write_enable(nor);
                write_sr(nor, 0);
                spi_nor_wait_till_ready(nor);
+
+               if (JEDEC_MFR(nor->info) == SNOR_MFR_WINBOND) {
+                       write_enable(nor);
+                       sr2 = 0;
+                       err = nor->write_reg(nor,
+                                            SPINOR_OP_WIN_WRSR2, &sr2, 1);
+                       if (err < 0) {
+                               dev_dbg(nor->dev,
+                                       "error while writing SR-2 register\n");
+                               return -EINVAL;
+                       }
+                       spi_nor_wait_till_ready(nor);
+               }
+               err = write_disable(nor);
+               if (err)
+                       return err;
        }
 
        if (nor->quad_enable) {
-- 
2.27.0

Reply via email to