Merge branch '2022-08-04-Kconfig-migrations'
[platform/kernel/u-boot.git] / drivers / mtd / spi / sf_dataflash.c
index e5c0e12..e19e342 100644 (file)
@@ -1,24 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Atmel DataFlash probing
  *
  * Copyright (C) 2004-2009, 2015 Freescale Semiconductor, Inc.
  * Haikun Wang (haikun.wang@freescale.com)
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <dm.h>
 #include <errno.h>
 #include <fdtdec.h>
+#include <log.h>
 #include <spi.h>
 #include <spi_flash.h>
 #include <div64.h>
+#include <linux/delay.h>
 #include <linux/err.h>
 #include <linux/math64.h>
 
 #include "sf_internal.h"
 
+#define CMD_READ_ID            0x9f
 /* reads can bypass the buffers */
 #define OP_READ_CONTINUOUS     0xE8
 #define OP_READ_PAGE           0xD2
@@ -67,6 +69,9 @@
 #define OP_WRITE_SECURITY_REVC 0x9A
 #define OP_WRITE_SECURITY      0x9B    /* revision D */
 
+#define DATAFLASH_SHIFT_EXTID  24
+#define DATAFLASH_SHIFT_ID     40
+
 struct dataflash {
        uint8_t                 command[16];
        unsigned short          page_offset;    /* offset in flash address */
@@ -76,12 +81,14 @@ struct dataflash {
 static inline int dataflash_status(struct spi_slave *spi)
 {
        int ret;
+       u8 opcode = OP_READ_STATUS;
        u8 status;
+
        /*
         * NOTE:  at45db321c over 25 MHz wants to write
         * a dummy byte after the opcode...
         */
-       ret = spi_flash_cmd(spi, OP_READ_STATUS, &status, 1);
+       ret =  spi_write_then_read(spi, &opcode, 1, NULL, &status, 1);
        return ret ? -EIO : status;
 }
 
@@ -173,7 +180,7 @@ static int spi_dataflash_erase(struct udevice *dev, u32 offset, size_t len)
                      command[0], command[1], command[2], command[3],
                      pageaddr);
 
-               status = spi_flash_cmd_write(spi, command, 4, NULL, 0);
+               status = spi_write_then_read(spi, command, 4, NULL, NULL, 0);
                if (status < 0) {
                        debug("%s: erase send command error!\n", dev->name);
                        return -EIO;
@@ -248,7 +255,7 @@ static int spi_dataflash_read(struct udevice *dev, u32 offset, size_t len,
        command[3] = (uint8_t)(addr >> 0);
 
        /* plus 4 "don't care" bytes, command len: 4 + 4 "don't care" bytes */
-       status = spi_flash_cmd_read(spi, command, 8, buf, len);
+       status = spi_write_then_read(spi, command, 8, NULL, buf, len);
 
        spi_release_bus(spi);
 
@@ -327,7 +334,8 @@ int spi_dataflash_write(struct udevice *dev, u32 offset, size_t len,
                        debug("TRANSFER: (%x) %x %x %x\n",
                              command[0], command[1], command[2], command[3]);
 
-                       status = spi_flash_cmd_write(spi, command, 4, NULL, 0);
+                       status = spi_write_then_read(spi, command, 4,
+                                                    NULL, NULL, 0);
                        if (status < 0) {
                                debug("%s: write(<pagesize) command error!\n",
                                      dev->name);
@@ -352,8 +360,8 @@ int spi_dataflash_write(struct udevice *dev, u32 offset, size_t len,
                debug("PROGRAM: (%x) %x %x %x\n",
                      command[0], command[1], command[2], command[3]);
 
-               status = spi_flash_cmd_write(spi, command,
-                                            4, writebuf, writelen);
+               status = spi_write_then_read(spi, command, 4,
+                                            writebuf, NULL, writelen);
                if (status < 0) {
                        debug("%s: write send command error!\n", dev->name);
                        return -EIO;
@@ -376,8 +384,8 @@ int spi_dataflash_write(struct udevice *dev, u32 offset, size_t len,
                debug("COMPARE: (%x) %x %x %x\n",
                      command[0], command[1], command[2], command[3]);
 
-               status = spi_flash_cmd_write(spi, command,
-                                            4, writebuf, writelen);
+               status = spi_write_then_read(spi, command, 4,
+                                            writebuf, NULL, writelen);
                if (status < 0) {
                        debug("%s: write(compare) send command error!\n",
                              dev->name);
@@ -442,14 +450,14 @@ static int add_dataflash(struct udevice *dev, char *name, int nr_pages,
        return 0;
 }
 
-struct flash_info {
+struct data_flash_info {
        char            *name;
 
        /*
         * JEDEC id has a high byte of zero plus three data bytes:
         * the manufacturer id, then a two byte device id.
         */
-       uint32_t        jedec_id;
+       uint64_t        jedec_id;
 
        /* The size listed here is what works with OP_ERASE_PAGE. */
        unsigned        nr_pages;
@@ -457,11 +465,12 @@ struct flash_info {
        uint16_t        pageoffset;
 
        uint16_t        flags;
+#define SUP_EXTID      0x0004          /* supports extended ID data */
 #define SUP_POW2PS     0x0002          /* supports 2^N byte pages */
 #define IS_POW2PS      0x0001          /* uses 2^N byte pages */
 };
 
-static struct flash_info dataflash_data[] = {
+static struct data_flash_info dataflash_data[] = {
        /*
         * NOTE:  chips with SUP_POW2PS (rev D and up) need two entries,
         * one with IS_POW2PS and the other without.  The entry with the
@@ -500,49 +509,31 @@ static struct flash_info dataflash_data[] = {
 
        { "AT45DB642x",  0x1f2800, 8192, 1056, 11, SUP_POW2PS},
        { "at45db642d",  0x1f2800, 8192, 1024, 10, SUP_POW2PS | IS_POW2PS},
+
+       { "AT45DB641E",  0x1f28000100ULL, 32768, 264, 9, SUP_EXTID | SUP_POW2PS},
+       { "at45db641e",  0x1f28000100ULL, 32768, 256, 8, SUP_EXTID | SUP_POW2PS | IS_POW2PS},
 };
 
-static struct flash_info *jedec_probe(struct spi_slave *spi)
+static struct data_flash_info *jedec_lookup(struct spi_slave *spi,
+                                           u64 jedec, bool use_extid)
+
 {
-       int                     tmp;
-       uint8_t                 id[5];
-       uint32_t                jedec;
-       struct flash_info       *info;
+       struct data_flash_info *info;
        int status;
 
-       /*
-        * JEDEC also defines an optional "extended device information"
-        * string for after vendor-specific data, after the three bytes
-        * we use here.  Supporting some chips might require using it.
-        *
-        * If the vendor ID isn't Atmel's (0x1f), assume this call failed.
-        * That's not an error; only rev C and newer chips handle it, and
-        * only Atmel sells these chips.
-        */
-       tmp = spi_flash_cmd(spi, CMD_READ_ID, id, sizeof(id));
-       if (tmp < 0) {
-               printf("dataflash: error %d reading JEDEC ID\n", tmp);
-               return ERR_PTR(tmp);
-       }
-       if (id[0] != 0x1f)
-               return NULL;
-
-       jedec = id[0];
-       jedec = jedec << 8;
-       jedec |= id[1];
-       jedec = jedec << 8;
-       jedec |= id[2];
+       for (info = dataflash_data;
+            info < dataflash_data + ARRAY_SIZE(dataflash_data);
+            info++) {
+               if (use_extid && !(info->flags & SUP_EXTID))
+                       continue;
 
-       for (tmp = 0, info = dataflash_data;
-                       tmp < ARRAY_SIZE(dataflash_data);
-                       tmp++, info++) {
                if (info->jedec_id == jedec) {
                        if (info->flags & SUP_POW2PS) {
                                status = dataflash_status(spi);
                                if (status < 0) {
                                        debug("dataflash: status error %d\n",
                                              status);
-                                       return NULL;
+                                       return ERR_PTR(status);
                                }
                                if (status & 0x1) {
                                        if (info->flags & IS_POW2PS)
@@ -557,12 +548,58 @@ static struct flash_info *jedec_probe(struct spi_slave *spi)
                }
        }
 
+       return ERR_PTR(-ENODEV);
+}
+
+static struct data_flash_info *jedec_probe(struct spi_slave *spi)
+{
+       int                     tmp;
+       uint64_t                jedec;
+       uint8_t                 id[sizeof(jedec)] = {0};
+       const unsigned int      id_size = 5;
+       struct data_flash_info  *info;
+       u8 opcode               = CMD_READ_ID;
+
+       /*
+        * JEDEC also defines an optional "extended device information"
+        * string for after vendor-specific data, after the three bytes
+        * we use here.  Supporting some chips might require using it.
+        *
+        * If the vendor ID isn't Atmel's (0x1f), assume this call failed.
+        * That's not an error; only rev C and newer chips handle it, and
+        * only Atmel sells these chips.
+        */
+       tmp = spi_write_then_read(spi, &opcode, 1, NULL, id, id_size);
+       if (tmp < 0) {
+               printf("dataflash: error %d reading JEDEC ID\n", tmp);
+               return ERR_PTR(tmp);
+       }
+
+       if (id[0] != 0x1f)
+               return NULL;
+
+       jedec = be64_to_cpup((__be64 *)id);
+
+       /*
+        * First, try to match device using extended device
+        * information
+        */
+       info = jedec_lookup(spi, jedec >> DATAFLASH_SHIFT_EXTID, true);
+       if (!IS_ERR(info))
+               return info;
+       /*
+        * If that fails, make another pass using regular ID
+        * information
+        */
+       info = jedec_lookup(spi, jedec >> DATAFLASH_SHIFT_ID, false);
+       if (!IS_ERR(info))
+               return info;
        /*
         * Treat other chips as errors ... we won't know the right page
         * size (it might be binary) even when we can tell which density
         * class is involved (legacy chip id scheme).
         */
-       printf("dataflash: JEDEC id %06x not handled\n", jedec);
+       printf("dataflash: JEDEC id 0x%016llx not handled\n", jedec);
        return ERR_PTR(-ENODEV);
 }
 
@@ -584,7 +621,7 @@ static int spi_dataflash_probe(struct udevice *dev)
 {
        struct spi_slave *spi = dev_get_parent_priv(dev);
        struct spi_flash *spi_flash;
-       struct flash_info *info;
+       struct data_flash_info *info;
        int status;
 
        spi_flash = dev_get_uclass_priv(dev);
@@ -611,6 +648,8 @@ static int spi_dataflash_probe(struct udevice *dev)
                                (info->flags & SUP_POW2PS) ? 'd' : 'c');
                if (status < 0)
                        goto err_status;
+               else
+                       return status;
        }
 
        /*
@@ -686,6 +725,6 @@ U_BOOT_DRIVER(spi_dataflash) = {
        .id             = UCLASS_SPI_FLASH,
        .of_match       = spi_dataflash_ids,
        .probe          = spi_dataflash_probe,
-       .priv_auto_alloc_size = sizeof(struct dataflash),
+       .priv_auto      = sizeof(struct dataflash),
        .ops            = &spi_dataflash_ops,
 };