sf: ops: Add spi_flash_copy_mmap function
[oweals/u-boot.git] / drivers / mtd / spi / sf_ops.c
index 759231f2e34b6d66f55ac38e25a85c179cd50732..900ec1f2a9ce8cb95ae4f2d087db3bc440c72b69 100644 (file)
@@ -14,6 +14,7 @@
 #include <spi.h>
 #include <spi_flash.h>
 #include <watchdog.h>
+#include <linux/compiler.h>
 
 #include "sf_internal.h"
 
@@ -154,21 +155,17 @@ static void spi_flash_dual_flash(struct spi_flash *flash, u32 *addr)
 }
 #endif
 
-int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
+static int spi_flash_poll_status(struct spi_slave *spi, unsigned long timeout,
+                                u8 cmd, u8 poll_bit)
 {
-       struct spi_slave *spi = flash->spi;
        unsigned long timebase;
        unsigned long flags = SPI_XFER_BEGIN;
        int ret;
        u8 status;
        u8 check_status = 0x0;
-       u8 poll_bit = STATUS_WIP;
-       u8 cmd = flash->poll_cmd;
 
-       if (cmd == CMD_FLAG_STATUS) {
-               poll_bit = STATUS_PEC;
+       if (cmd == CMD_FLAG_STATUS)
                check_status = poll_bit;
-       }
 
 #ifdef CONFIG_SF_DUAL_FLASH
        if (spi->flags & SPI_XFER_U_PAGE)
@@ -204,6 +201,28 @@ int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
        return -1;
 }
 
+int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
+{
+       struct spi_slave *spi = flash->spi;
+       int ret;
+       u8 poll_bit = STATUS_WIP;
+       u8 cmd = CMD_READ_STATUS;
+
+       ret = spi_flash_poll_status(spi, timeout, cmd, poll_bit);
+       if (ret < 0)
+               return ret;
+
+       if (flash->poll_cmd == CMD_FLAG_STATUS) {
+               poll_bit = STATUS_PEC;
+               cmd = CMD_FLAG_STATUS;
+               ret = spi_flash_poll_status(spi, timeout, cmd, poll_bit);
+               if (ret < 0)
+                       return ret;
+       }
+
+       return 0;
+}
+
 int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
                size_t cmd_len, const void *buf, size_t buf_len)
 {
@@ -360,6 +379,11 @@ int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
        return ret;
 }
 
+void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len)
+{
+       memcpy(data, offset, len);
+}
+
 int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
                size_t len, void *data)
 {
@@ -376,7 +400,7 @@ int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
                        return ret;
                }
                spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP);
-               memcpy(data, flash->memory_map + offset, len);
+               spi_flash_copy_mmap(data, flash->memory_map + offset, len);
                spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
                spi_release_bus(flash->spi);
                return 0;
@@ -517,4 +541,35 @@ int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
        spi_release_bus(flash->spi);
        return ret;
 }
+
+int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
+               const void *buf)
+{
+       size_t actual;
+       int ret;
+
+       ret = spi_claim_bus(flash->spi);
+       if (ret) {
+               debug("SF: Unable to claim SPI bus\n");
+               return ret;
+       }
+
+       for (actual = 0; actual < len; actual++) {
+               ret = sst_byte_write(flash, offset, buf + actual);
+               if (ret) {
+                       debug("SF: sst byte program failed\n");
+                       break;
+               }
+               offset++;
+       }
+
+       if (!ret)
+               ret = spi_flash_cmd_write_disable(flash);
+
+       debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
+             ret ? "failure" : "success", len, offset - actual);
+
+       spi_release_bus(flash->spi);
+       return ret;
+}
 #endif