From 0ff26c3761e5a17d21e7ee0bd9a2068f55c84d55 Mon Sep 17 00:00:00 2001
From: Yunhui Cui <yunhui.cui@nxp.com>
Date: Wed, 15 Jun 2016 14:57:08 +0800
Subject: [PATCH 57/93] driver: spi: Fix DSPI bug after adding exceed 16MB for
 QSPI

=> sf probe 1:0
SF: Detected N25Q128A with page size 256 Bytes, erase size 64 KiB, total
16 MiB
=> mw.l     80000000 55555555 1100
=> mw.l     80020000 aaaaaaaa 1100
=> sf erase 0 +1100
SF: 65536 bytes @ 0x0 Erased: OK
=> sf write 80020000 0 1100
device 0 offset 0x0, size 0x1100
SF: 4352 bytes @ 0x0 Written: OK
=> sf read  80000000 0 1100
device 0 offset 0x0, size 0x1100
SF: 4352 bytes @ 0x0 Read: OK
=> cmp.b 80000000 80020000 1100
byte at 0x00000000800010fe (0xff) != byte at 0x00000000800210fe (0xaa)
Total of 4350 byte(s) were the same

Signed-off-by: Yunhui Cui <yunhui.cui@nxp.com>
---
 drivers/mtd/spi/spi_flash.c |   21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/drivers/mtd/spi/spi_flash.c b/drivers/mtd/spi/spi_flash.c
index b0f09ab..e04bd55 100644
--- a/drivers/mtd/spi/spi_flash.c
+++ b/drivers/mtd/spi/spi_flash.c
@@ -326,8 +326,11 @@ int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
 			return -EINVAL;
 		}
 	}
+	if (flash->size > SPI_FLASH_16MB_BOUN)
+		cmdsz = SPI_FLASH_CMD_LEN_EXT + flash->dummy_byte;
+	else
+		cmdsz = SPI_FLASH_CMD_LEN;
 
-	cmdsz = SPI_FLASH_CMD_LEN_EXT + flash->dummy_byte;
 	cmd = calloc(1, cmdsz);
 	if (!cmd) {
 		debug("SF: Failed to allocate cmd\n");
@@ -353,7 +356,7 @@ int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
 		debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
 		      cmd[2], cmd[3], erase_addr);
 
-		ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
+		ret = spi_flash_write_common(flash, cmd, cmdsz, NULL, 0);
 		if (ret < 0) {
 			debug("SF: erase failed\n");
 			break;
@@ -385,8 +388,10 @@ int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
 			return -EINVAL;
 		}
 	}
-
-	cmdsz = SPI_FLASH_CMD_LEN_EXT + flash->dummy_byte;
+	if (flash->size > SPI_FLASH_16MB_BOUN)
+		cmdsz = SPI_FLASH_CMD_LEN_EXT + flash->dummy_byte;
+	else
+		cmdsz = SPI_FLASH_CMD_LEN;
 	cmd = calloc(1, cmdsz);
 	if (!cmd) {
 		debug("SF: Failed to allocate cmd\n");
@@ -418,7 +423,7 @@ int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
 		debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
 		      buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
 
-		ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
+		ret = spi_flash_write_common(flash, cmd, cmdsz,
 					buf + actual, chunk_len);
 		if (ret < 0) {
 			debug("SF: write failed\n");
@@ -482,7 +487,11 @@ int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
 		return 0;
 	}
 
-	cmdsz = SPI_FLASH_CMD_LEN_EXT + flash->dummy_byte;
+	if (flash->size > SPI_FLASH_16MB_BOUN)
+		cmdsz = SPI_FLASH_CMD_LEN_EXT + flash->dummy_byte;
+	else
+		cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
+
 	cmd = calloc(1, cmdsz);
 	if (!cmd) {
 		debug("SF: Failed to allocate cmd\n");
-- 
1.7.9.5

