spi: sf_ops: Check the return value from spi_flash_cmd_read_status()
[oweals/u-boot.git] / drivers / mtd / spi / sf_ops.c
index d8324645b2cbe8a9b51e7a4109d4b6beeac79e72..3a56d7f55c80d998397a0468b5d216443013e0fa 100644 (file)
@@ -268,9 +268,12 @@ int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
                return -1;
        }
 
-       if (flash->flash_is_locked(flash, offset, len) > 0) {
-               printf("offset 0x%x is protected and cannot be erased\n", offset);
-               return -EINVAL;
+       if (flash->flash_is_locked) {
+               if (flash->flash_is_locked(flash, offset, len) > 0) {
+                       printf("offset 0x%x is protected and cannot be erased\n",
+                              offset);
+                       return -EINVAL;
+               }
        }
 
        cmd[0] = flash->erase_cmd;
@@ -315,9 +318,12 @@ int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
 
        page_size = flash->page_size;
 
-       if (flash->flash_is_locked(flash, offset, len) > 0) {
-               printf("offset 0x%x is protected and cannot be written\n", offset);
-               return -EINVAL;
+       if (flash->flash_is_locked) {
+               if (flash->flash_is_locked(flash, offset, len) > 0) {
+                       printf("offset 0x%x is protected and cannot be written\n",
+                              offset);
+                       return -EINVAL;
+               }
        }
 
        cmd[0] = flash->write_cmd;
@@ -577,7 +583,7 @@ int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
 }
 #endif
 
-#ifdef CONFIG_SPI_FLASH_STMICRO
+#if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
 static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs,
                                 u32 *len)
 {
@@ -657,8 +663,11 @@ int stm_lock(struct spi_flash *flash, u32 ofs, size_t len)
        u8 status_old, status_new;
        u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
        u8 shift = ffs(mask) - 1, pow, val;
+       int ret;
 
-       spi_flash_cmd_read_status(flash, &status_old);
+       ret = spi_flash_cmd_read_status(flash, &status_old);
+       if (ret < 0)
+               return ret;
 
        /* SPI NOR always locks to the end */
        if (ofs + len != flash->size) {
@@ -708,8 +717,11 @@ int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
        uint8_t status_old, status_new;
        u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
        u8 shift = ffs(mask) - 1, pow, val;
+       int ret;
 
-       spi_flash_cmd_read_status(flash, &status_old);
+       ret = spi_flash_cmd_read_status(flash, &status_old);
+       if (ret < 0)
+               return ret;
 
        /* Cannot unlock; would unlock larger region than requested */
        if (stm_is_locked_sr(flash, status_old, ofs - flash->erase_size,
@@ -744,4 +756,4 @@ int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
 
        return 0;
 }
-#endif  /* CONFIG_SPI_FLASH_STMICRO */
+#endif