Merge tag 'ntb-4.4' of git://github.com/jonmason/ntb
[firefly-linux-kernel-4.4.55.git] / drivers / mtd / spi-nor / spi-nor.c
index 0397dbad9709a8f1dc0d8ddf4637124ad7b4ceba..49883905a434b68b98af50e515a0356731c62fa6 100644 (file)
@@ -18,7 +18,6 @@
 #include <linux/math64.h>
 #include <linux/sizes.h>
 
-#include <linux/mtd/cfi.h>
 #include <linux/mtd/mtd.h>
 #include <linux/of_platform.h>
 #include <linux/spi/flash.h>
@@ -191,11 +190,11 @@ static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
        u8 cmd;
 
        switch (JEDEC_MFR(info)) {
-       case CFI_MFR_ST: /* Micron, actually */
+       case SNOR_MFR_MICRON:
                /* Some Micron need WREN command; all will accept it */
                need_wren = true;
-       case CFI_MFR_MACRONIX:
-       case 0xEF /* winbond */:
+       case SNOR_MFR_MACRONIX:
+       case SNOR_MFR_WINBOND:
                if (need_wren)
                        write_enable(nor);
 
@@ -401,72 +400,171 @@ erase_err:
        return ret;
 }
 
+static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
+                                uint64_t *len)
+{
+       struct mtd_info *mtd = &nor->mtd;
+       u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
+       int shift = ffs(mask) - 1;
+       int pow;
+
+       if (!(sr & mask)) {
+               /* No protection */
+               *ofs = 0;
+               *len = 0;
+       } else {
+               pow = ((sr & mask) ^ mask) >> shift;
+               *len = mtd->size >> pow;
+               *ofs = mtd->size - *len;
+       }
+}
+
+/*
+ * Return 1 if the entire region is locked, 0 otherwise
+ */
+static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
+                           u8 sr)
+{
+       loff_t lock_offs;
+       uint64_t lock_len;
+
+       stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
+
+       return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
+}
+
+/*
+ * Lock a region of the flash. Compatible with ST Micro and similar flash.
+ * Supports only the block protection bits BP{0,1,2} in the status register
+ * (SR). Does not support these features found in newer SR bitfields:
+ *   - TB: top/bottom protect - only handle TB=0 (top protect)
+ *   - SEC: sector/block protect - only handle SEC=0 (block protect)
+ *   - CMP: complement protect - only support CMP=0 (range is not complemented)
+ *
+ * Sample table portion for 8MB flash (Winbond w25q64fw):
+ *
+ *   SEC  |  TB   |  BP2  |  BP1  |  BP0  |  Prot Length  | Protected Portion
+ *  --------------------------------------------------------------------------
+ *    X   |   X   |   0   |   0   |   0   |  NONE         | NONE
+ *    0   |   0   |   0   |   0   |   1   |  128 KB       | Upper 1/64
+ *    0   |   0   |   0   |   1   |   0   |  256 KB       | Upper 1/32
+ *    0   |   0   |   0   |   1   |   1   |  512 KB       | Upper 1/16
+ *    0   |   0   |   1   |   0   |   0   |  1 MB         | Upper 1/8
+ *    0   |   0   |   1   |   0   |   1   |  2 MB         | Upper 1/4
+ *    0   |   0   |   1   |   1   |   0   |  4 MB         | Upper 1/2
+ *    X   |   X   |   1   |   1   |   1   |  8 MB         | ALL
+ *
+ * Returns negative on errors, 0 on success.
+ */
 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 {
        struct mtd_info *mtd = &nor->mtd;
-       uint32_t offset = ofs;
-       uint8_t status_old, status_new;
-       int ret = 0;
+       u8 status_old, status_new;
+       u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
+       u8 shift = ffs(mask) - 1, pow, val;
 
        status_old = read_sr(nor);
 
-       if (offset < mtd->size - (mtd->size / 2))
-               status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
-       else if (offset < mtd->size - (mtd->size / 4))
-               status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
-       else if (offset < mtd->size - (mtd->size / 8))
-               status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
-       else if (offset < mtd->size - (mtd->size / 16))
-               status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
-       else if (offset < mtd->size - (mtd->size / 32))
-               status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
-       else if (offset < mtd->size - (mtd->size / 64))
-               status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
-       else
-               status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
+       /* SPI NOR always locks to the end */
+       if (ofs + len != mtd->size) {
+               /* Does combined region extend to end? */
+               if (!stm_is_locked_sr(nor, ofs + len, mtd->size - ofs - len,
+                                     status_old))
+                       return -EINVAL;
+               len = mtd->size - ofs;
+       }
+
+       /*
+        * Need smallest pow such that:
+        *
+        *   1 / (2^pow) <= (len / size)
+        *
+        * so (assuming power-of-2 size) we do:
+        *
+        *   pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
+        */
+       pow = ilog2(mtd->size) - ilog2(len);
+       val = mask - (pow << shift);
+       if (val & ~mask)
+               return -EINVAL;
+       /* Don't "lock" with no region! */
+       if (!(val & mask))
+               return -EINVAL;
+
+       status_new = (status_old & ~mask) | val;
 
        /* Only modify protection if it will not unlock other areas */
-       if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) >
-                               (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
-               write_enable(nor);
-               ret = write_sr(nor, status_new);
-       }
+       if ((status_new & mask) <= (status_old & mask))
+               return -EINVAL;
 
-       return ret;
+       write_enable(nor);
+       return write_sr(nor, status_new);
 }
 
+/*
+ * Unlock a region of the flash. See stm_lock() for more info
+ *
+ * Returns negative on errors, 0 on success.
+ */
 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 {
        struct mtd_info *mtd = &nor->mtd;
-       uint32_t offset = ofs;
        uint8_t status_old, status_new;
-       int ret = 0;
+       u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
+       u8 shift = ffs(mask) - 1, pow, val;
 
        status_old = read_sr(nor);
 
-       if (offset+len > mtd->size - (mtd->size / 64))
-               status_new = status_old & ~(SR_BP2 | SR_BP1 | SR_BP0);
-       else if (offset+len > mtd->size - (mtd->size / 32))
-               status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
-       else if (offset+len > mtd->size - (mtd->size / 16))
-               status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
-       else if (offset+len > mtd->size - (mtd->size / 8))
-               status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
-       else if (offset+len > mtd->size - (mtd->size / 4))
-               status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
-       else if (offset+len > mtd->size - (mtd->size / 2))
-               status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
-       else
-               status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
+       /* Cannot unlock; would unlock larger region than requested */
+       if (stm_is_locked_sr(nor, status_old, ofs - mtd->erasesize,
+                            mtd->erasesize))
+               return -EINVAL;
 
-       /* Only modify protection if it will not lock other areas */
-       if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) <
-                               (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
-               write_enable(nor);
-               ret = write_sr(nor, status_new);
+       /*
+        * Need largest pow such that:
+        *
+        *   1 / (2^pow) >= (len / size)
+        *
+        * so (assuming power-of-2 size) we do:
+        *
+        *   pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
+        */
+       pow = ilog2(mtd->size) - order_base_2(mtd->size - (ofs + len));
+       if (ofs + len == mtd->size) {
+               val = 0; /* fully unlocked */
+       } else {
+               val = mask - (pow << shift);
+               /* Some power-of-two sizes are not supported */
+               if (val & ~mask)
+                       return -EINVAL;
        }
 
-       return ret;
+       status_new = (status_old & ~mask) | val;
+
+       /* Only modify protection if it will not lock other areas */
+       if ((status_new & mask) >= (status_old & mask))
+               return -EINVAL;
+
+       write_enable(nor);
+       return write_sr(nor, status_new);
+}
+
+/*
+ * Check if a region of the flash is (completely) locked. See stm_lock() for
+ * more info.
+ *
+ * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
+ * negative on errors.
+ */
+static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
+{
+       int status;
+
+       status = read_sr(nor);
+       if (status < 0)
+               return status;
+
+       return stm_is_locked_sr(nor, ofs, len, status);
 }
 
 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
@@ -499,6 +597,21 @@ static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
        return ret;
 }
 
+static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
+{
+       struct spi_nor *nor = mtd_to_spi_nor(mtd);
+       int ret;
+
+       ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
+       if (ret)
+               return ret;
+
+       ret = nor->flash_is_locked(nor, ofs, len);
+
+       spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
+       return ret;
+}
+
 /* Used when the "_ext_id" is two bytes at most */
 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags)     \
                .id = {                                                 \
@@ -649,12 +762,13 @@ static const struct flash_info spi_nor_ids[] = {
        { "s25sl016a",  INFO(0x010214,      0,  64 * 1024,  32, 0) },
        { "s25sl032a",  INFO(0x010215,      0,  64 * 1024,  64, 0) },
        { "s25sl064a",  INFO(0x010216,      0,  64 * 1024, 128, 0) },
+       { "s25fl004k",  INFO(0xef4013,      0,  64 * 1024,   8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
        { "s25fl008k",  INFO(0xef4014,      0,  64 * 1024,  16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
        { "s25fl016k",  INFO(0xef4015,      0,  64 * 1024,  32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
        { "s25fl064k",  INFO(0xef4017,      0,  64 * 1024, 128, SECT_4K) },
        { "s25fl132k",  INFO(0x014016,      0,  64 * 1024,  64, SECT_4K) },
        { "s25fl164k",  INFO(0x014017,      0,  64 * 1024, 128, SECT_4K) },
-       { "s25fl204k",  INFO(0x014013,      0,  64 * 1024,   8, SECT_4K) },
+       { "s25fl204k",  INFO(0x014013,      0,  64 * 1024,   8, SECT_4K | SPI_NOR_DUAL_READ) },
 
        /* SST -- large erase sizes are "overlays", "sectors" are 4K */
        { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024,  8, SECT_4K | SST_WRITE) },
@@ -715,10 +829,10 @@ static const struct flash_info spi_nor_ids[] = {
        { "w25x16", INFO(0xef3015, 0, 64 * 1024,  32, SECT_4K) },
        { "w25x32", INFO(0xef3016, 0, 64 * 1024,  64, SECT_4K) },
        { "w25q32", INFO(0xef4016, 0, 64 * 1024,  64, SECT_4K) },
-       { "w25q32dw", INFO(0xef6016, 0, 64 * 1024,  64, SECT_4K) },
+       { "w25q32dw", INFO(0xef6016, 0, 64 * 1024,  64, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
        { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
        { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
-       { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K) },
+       { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
        { "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
        { "w25q80", INFO(0xef5014, 0, 64 * 1024,  16, SECT_4K) },
        { "w25q80bl", INFO(0xef4014, 0, 64 * 1024,  16, SECT_4K) },
@@ -997,14 +1111,14 @@ static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
        int status;
 
        switch (JEDEC_MFR(info)) {
-       case CFI_MFR_MACRONIX:
+       case SNOR_MFR_MACRONIX:
                status = macronix_quad_enable(nor);
                if (status) {
                        dev_err(nor->dev, "Macronix quad-read not enabled\n");
                        return -EINVAL;
                }
                return status;
-       case CFI_MFR_ST:
+       case SNOR_MFR_MICRON:
                status = micron_quad_enable(nor);
                if (status) {
                        dev_err(nor->dev, "Micron quad-read not enabled\n");
@@ -1080,13 +1194,14 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
        mutex_init(&nor->lock);
 
        /*
-        * Atmel, SST and Intel/Numonyx serial nor tend to power
-        * up with the software protection bits set
+        * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
+        * with the software protection bits set
         */
 
-       if (JEDEC_MFR(info) == CFI_MFR_ATMEL ||
-           JEDEC_MFR(info) == CFI_MFR_INTEL ||
-           JEDEC_MFR(info) == CFI_MFR_SST) {
+       if (JEDEC_MFR(info) == SNOR_MFR_ATMEL ||
+           JEDEC_MFR(info) == SNOR_MFR_INTEL ||
+           JEDEC_MFR(info) == SNOR_MFR_SST ||
+           JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
                write_enable(nor);
                write_sr(nor, 0);
        }
@@ -1101,15 +1216,18 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
        mtd->_erase = spi_nor_erase;
        mtd->_read = spi_nor_read;
 
-       /* nor protection support for STmicro chips */
-       if (JEDEC_MFR(info) == CFI_MFR_ST) {
+       /* NOR protection support for STmicro/Micron chips and similar */
+       if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
+           JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
                nor->flash_lock = stm_lock;
                nor->flash_unlock = stm_unlock;
+               nor->flash_is_locked = stm_is_locked;
        }
 
-       if (nor->flash_lock && nor->flash_unlock) {
+       if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
                mtd->_lock = spi_nor_lock;
                mtd->_unlock = spi_nor_unlock;
+               mtd->_is_locked = spi_nor_is_locked;
        }
 
        /* sst nor chips use AAI word program */
@@ -1196,7 +1314,7 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
        else if (mtd->size > 0x1000000) {
                /* enable 4-byte addressing if the device exceeds 16MiB */
                nor->addr_width = 4;
-               if (JEDEC_MFR(info) == CFI_MFR_AMD) {
+               if (JEDEC_MFR(info) == SNOR_MFR_SPANSION) {
                        /* Dedicated 4-byte command set */
                        switch (nor->flash_read) {
                        case SPI_NOR_QUAD: