]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/mtd/spi/spi_flash.c
Merge branch 'master' of git://git.denx.de/u-boot-arm
[karo-tx-uboot.git] / drivers / mtd / spi / spi_flash.c
index f689cc47cfccac051e4257de47bedfcecb5bd13a..9991d47a468e0a07f323213de933c793f5257632 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <fdtdec.h>
 #include <malloc.h>
 #include <spi.h>
 #include <spi_flash.h>
@@ -15,6 +16,8 @@
 
 #include "spi_flash_internal.h"
 
+DECLARE_GLOBAL_DATA_PTR;
+
 static void spi_flash_addr(u32 addr, u8 *cmd)
 {
        /* cmd[0] is actual command */
@@ -87,6 +90,9 @@ int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset,
        for (actual = 0; actual < len; actual += chunk_len) {
                chunk_len = min(len - actual, page_size - byte_addr);
 
+               if (flash->spi->max_write_size)
+                       chunk_len = min(chunk_len, flash->spi->max_write_size);
+
                cmd[1] = page_addr >> 8;
                cmd[2] = page_addr;
                cmd[3] = byte_addr;
@@ -111,13 +117,13 @@ int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset,
                if (ret)
                        break;
 
-               page_addr++;
-               byte_addr = 0;
+               byte_addr += chunk_len;
+               if (byte_addr == page_size) {
+                       page_addr++;
+                       byte_addr = 0;
+               }
        }
 
-       debug("SF: program %s %zu bytes @ %#x\n",
-             ret ? "failure" : "success", len, offset);
-
        spi_release_bus(flash->spi);
        return ret;
 }
@@ -140,6 +146,12 @@ int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
 {
        u8 cmd[5];
 
+       /* Handle memory-mapped SPI */
+       if (flash->memory_map) {
+               memcpy(data, flash->memory_map + offset, len);
+               return 0;
+       }
+
        cmd[0] = CMD_READ_ARRAY_FAST;
        spi_flash_addr(offset, cmd);
        cmd[4] = 0x00;
@@ -190,10 +202,9 @@ int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
                CMD_READ_STATUS, STATUS_WIP);
 }
 
-int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
-                       u32 offset, size_t len)
+int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
 {
-       u32 start, end, erase_size;
+       u32 end, erase_size;
        int ret;
        u8 cmd[4];
 
@@ -209,9 +220,11 @@ int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
                return ret;
        }
 
-       cmd[0] = erase_cmd;
-       start = offset;
-       end = start + len;
+       if (erase_size == 4096)
+               cmd[0] = CMD_ERASE_4K;
+       else
+               cmd[0] = CMD_ERASE_64K;
+       end = offset + len;
 
        while (offset < end) {
                spi_flash_addr(offset, cmd);
@@ -233,13 +246,66 @@ int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
                        goto out;
        }
 
-       debug("SF: Successfully erased %zu bytes @ %#x\n", len, start);
-
  out:
        spi_release_bus(flash->spi);
        return ret;
 }
 
+int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
+{
+       u8 cmd;
+       int ret;
+
+       ret = spi_flash_cmd_write_enable(flash);
+       if (ret < 0) {
+               debug("SF: enabling write failed\n");
+               return ret;
+       }
+
+       cmd = CMD_WRITE_STATUS;
+       ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
+       if (ret) {
+               debug("SF: fail to write status register\n");
+               return ret;
+       }
+
+       ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
+       if (ret < 0) {
+               debug("SF: write status register timed out\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+#ifdef CONFIG_OF_CONTROL
+int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
+{
+       fdt_addr_t addr;
+       fdt_size_t size;
+       int node;
+
+       /* If there is no node, do nothing */
+       node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
+       if (node < 0)
+               return 0;
+
+       addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
+       if (addr == FDT_ADDR_T_NONE) {
+               debug("%s: Cannot decode address\n", __func__);
+               return 0;
+       }
+
+       if (flash->size != size) {
+               debug("%s: Memory map must cover entire device\n", __func__);
+               return -1;
+       }
+       flash->memory_map = (void *)addr;
+
+       return 0;
+}
+#endif /* CONFIG_OF_CONTROL */
+
 /*
  * The following table holds all device probe functions
  *
@@ -276,6 +342,9 @@ static const struct {
 #ifdef CONFIG_SPI_FLASH_EON
        { 0, 0x1c, spi_flash_probe_eon, },
 #endif
+#ifdef CONFIG_SPI_FLASH_GIGADEVICE
+       { 0, 0xc8, spi_flash_probe_gigadevice, },
+#endif
 #ifdef CONFIG_SPI_FLASH_MACRONIX
        { 0, 0xc2, spi_flash_probe_macronix, },
 #endif
@@ -356,9 +425,18 @@ struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
                goto err_manufacturer_probe;
        }
 
+#ifdef CONFIG_OF_CONTROL
+       if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
+               debug("SF: FDT decode error\n");
+               goto err_manufacturer_probe;
+       }
+#endif
        printf("SF: Detected %s with page size ", flash->name);
        print_size(flash->sector_size, ", total ");
-       print_size(flash->size, "\n");
+       print_size(flash->size, "");
+       if (flash->memory_map)
+               printf(", mapped at %p", flash->memory_map);
+       puts("\n");
 
        spi_release_bus(spi);
 
@@ -372,6 +450,31 @@ err_claim_bus:
        return NULL;
 }
 
+void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
+                        const char *name)
+{
+       struct spi_flash *flash;
+       void *ptr;
+
+       ptr = malloc(size);
+       if (!ptr) {
+               debug("SF: Failed to allocate memory\n");
+               return NULL;
+       }
+       memset(ptr, '\0', size);
+       flash = (struct spi_flash *)(ptr + offset);
+
+       /* Set up some basic fields - caller will sort out sizes */
+       flash->spi = spi;
+       flash->name = name;
+
+       flash->read = spi_flash_cmd_read_fast;
+       flash->write = spi_flash_cmd_write_multi;
+       flash->erase = spi_flash_cmd_erase;
+
+       return flash;
+}
+
 void spi_flash_free(struct spi_flash *flash)
 {
        spi_free_slave(flash->spi);