[U-Boot] [PATCH v7 17/34] sf: Use mtd_info ops instead of spi_flash ops

Jagan Teki jteki at openedev.com
Thu Nov 26 13:04:01 CET 2015


Since MTD support is added in spi_flash layer, this patch uses
mtd_info operations instead of legacy spi_flash operations.

Reviewed-by: Heiko Schocher <hs at denx.de>
Signed-off-by: Jagan Teki <jteki at openedev.com>
---
 drivers/mtd/spi/sf_ops.c   | 75 +++++++++++++++++++++++++++++-----------------
 drivers/mtd/spi/sf_probe.c | 30 -------------------
 include/spi_flash.h        | 24 +++++++--------
 3 files changed, 59 insertions(+), 70 deletions(-)

diff --git a/drivers/mtd/spi/sf_ops.c b/drivers/mtd/spi/sf_ops.c
index c9f0bbd..1356161 100644
--- a/drivers/mtd/spi/sf_ops.c
+++ b/drivers/mtd/spi/sf_ops.c
@@ -14,6 +14,7 @@
 #include <mapmem.h>
 #include <spi.h>
 #include <spi_flash.h>
+#include <linux/math64.h>
 #include <linux/log2.h>
 #include <linux/mtd/mtd.h>
 
@@ -298,18 +299,21 @@ int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
 	return ret;
 }
 
-static int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset,
-					size_t len)
+static int spi_flash_cmd_erase_ops(struct mtd_info *mtd,
+					struct erase_info *instr)
 {
-	u32 erase_size, erase_addr;
+	struct spi_flash *flash = mtd->priv;
+	u32 offset, len, erase_addr;
 	u8 cmd[SPI_FLASH_CMD_LEN];
+	uint32_t rem;
 	int ret = -1;
 
-	erase_size = flash->erase_size;
-	if (offset % erase_size || len % erase_size) {
-		debug("SF: Erase offset/length not multiple of erase size\n");
-		return -1;
-	}
+	div_u64_rem(instr->len, mtd->erasesize, &rem);
+	if (rem)
+		return -EINVAL;
+
+	offset = instr->addr;
+	len = instr->len;
 
 	if (flash->flash_is_locked) {
 		if (flash->flash_is_locked(flash, offset, len) > 0) {
@@ -340,19 +344,27 @@ static int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset,
 		ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
 		if (ret < 0) {
 			debug("SF: erase failed\n");
-			break;
+			goto erase_err;
 		}
 
-		offset += erase_size;
-		len -= erase_size;
+		offset += mtd->erasesize;
+		len -= mtd->erasesize;
 	}
 
+	instr->state = MTD_ERASE_DONE;
+	mtd_erase_callback(instr);
+
+	return ret;
+
+erase_err:
+	instr->state = MTD_ERASE_FAILED;
 	return ret;
 }
 
-static int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
-		size_t len, const void *buf)
+static int spi_flash_cmd_write_ops(struct mtd_info *mtd, loff_t offset,
+				size_t len, size_t *retlen, const u_char *buf)
 {
+	struct spi_flash *flash = mtd->priv;
 	unsigned long byte_addr, page_size;
 	u32 write_addr;
 	size_t chunk_len, actual;
@@ -402,6 +414,7 @@ static int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
 		}
 
 		offset += chunk_len;
+		*retlen += chunk_len;
 	}
 
 	return ret;
@@ -435,11 +448,12 @@ void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len)
 	memcpy(data, offset, len);
 }
 
-static int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
-		size_t len, void *data)
+static int spi_flash_cmd_read_ops(struct mtd_info *mtd, loff_t offset,
+				size_t len, size_t *retlen, u_char *data)
 {
-	u8 *cmd, cmdsz;
+	struct spi_flash *flash = mtd->priv;
 	u32 remain_len, read_len, read_addr;
+	u8 *cmd, cmdsz;
 	int bank_sel = 0;
 	int ret = -1;
 
@@ -496,6 +510,7 @@ static int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
 		offset += read_len;
 		len -= read_len;
 		data += read_len;
+		*retlen += read_len;
 	}
 
 	free(cmd);
@@ -503,7 +518,8 @@ static int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
 }
 
 #ifdef CONFIG_SPI_FLASH_SST
-static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
+static int sst_byte_write(struct spi_flash *flash, u32 offset,
+				const void *buf, size_t *retlen)
 {
 	int ret;
 	u8 cmd[4] = {
@@ -524,12 +540,15 @@ static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
 	if (ret)
 		return ret;
 
+	*retlen += 1;
+
 	return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
 }
 
-static int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
-		const void *buf)
+static int sst_write_wp(struct mtd_info *mtd, loff_t offset, size_t len,
+				size_t *retlen, const u_char *buf)
 {
+	struct spi_flash *flash = mtd->priv;
 	size_t actual, cmd_len;
 	int ret;
 	u8 cmd[4];
@@ -543,7 +562,7 @@ static int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
 	/* If the data is not word aligned, write out leading single byte */
 	actual = offset % 2;
 	if (actual) {
-		ret = sst_byte_write(flash, offset, buf);
+		ret = sst_byte_write(flash, offset, buf, retlen);
 		if (ret)
 			goto done;
 	}
@@ -560,7 +579,7 @@ static int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
 	cmd[3] = offset;
 
 	for (; actual < len - 1; actual += 2) {
-		debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
+		debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06llx }\n",
 		      spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
 		      cmd[0], offset);
 
@@ -577,6 +596,7 @@ static int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
 
 		cmd_len = 1;
 		offset += 2;
+		*retlen += 2;
 	}
 
 	if (!ret)
@@ -584,19 +604,20 @@ static int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
 
 	/* If there is a single trailing byte, write it out */
 	if (!ret && actual != len)
-		ret = sst_byte_write(flash, offset, buf + actual);
+		ret = sst_byte_write(flash, offset, buf + actual, retlen);
 
  done:
-	debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
+	debug("SF: sst: program %s %zu bytes @ 0x%llx\n",
 	      ret ? "failure" : "success", len, offset - actual);
 
 	spi_release_bus(flash->spi);
 	return ret;
 }
 
-static int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
-		const void *buf)
+static int sst_write_bp(struct mtd_info *mtd, loff_t offset, size_t len,
+				size_t *retlen, const u_char *buf)
 {
+	struct spi_flash *flash = mtd->priv;
 	size_t actual;
 	int ret;
 
@@ -607,7 +628,7 @@ static int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
 	}
 
 	for (actual = 0; actual < len; actual++) {
-		ret = sst_byte_write(flash, offset, buf + actual);
+		ret = sst_byte_write(flash, offset, buf + actual, retlen);
 		if (ret) {
 			debug("SF: sst byte program failed\n");
 			break;
@@ -618,7 +639,7 @@ static int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
 	if (!ret)
 		ret = spi_flash_cmd_write_disable(flash);
 
-	debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
+	debug("SF: sst: program %s %zu bytes @ 0x%llx\n",
 	      ret ? "failure" : "success", len, offset - actual);
 
 	spi_release_bus(flash->spi);
diff --git a/drivers/mtd/spi/sf_probe.c b/drivers/mtd/spi/sf_probe.c
index a88d74e..f882b3f 100644
--- a/drivers/mtd/spi/sf_probe.c
+++ b/drivers/mtd/spi/sf_probe.c
@@ -106,29 +106,6 @@ void spi_flash_free(struct spi_flash *flash)
 
 #else /* defined CONFIG_DM_SPI_FLASH */
 
-static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
-			      void *buf)
-{
-	struct spi_flash *flash = dev_get_uclass_priv(dev);
-
-	return flash->read(flash, offset, len, buf);
-}
-
-int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
-			const void *buf)
-{
-	struct spi_flash *flash = dev_get_uclass_priv(dev);
-
-	return flash->write(flash, offset, len, buf);
-}
-
-int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
-{
-	struct spi_flash *flash = dev_get_uclass_priv(dev);
-
-	return flash->erase(flash, offset, len);
-}
-
 int spi_flash_std_probe(struct udevice *dev)
 {
 	struct spi_flash_priv *priv = dev_get_priv(dev);
@@ -173,12 +150,6 @@ err_scan:
 	return ret;
 }
 
-static const struct dm_spi_flash_ops spi_flash_std_ops = {
-	.read = spi_flash_std_read,
-	.write = spi_flash_std_write,
-	.erase = spi_flash_std_erase,
-};
-
 static const struct udevice_id spi_flash_std_ids[] = {
 	{ .compatible = "spi-flash" },
 	{ }
@@ -190,7 +161,6 @@ U_BOOT_DRIVER(spi_flash_std) = {
 	.of_match	= spi_flash_std_ids,
 	.probe		= spi_flash_std_probe,
 	.priv_auto_alloc_size = sizeof(struct spi_flash_priv),
-	.ops		= &spi_flash_std_ops,
 };
 
 #endif /* CONFIG_DM_SPI_FLASH */
diff --git a/include/spi_flash.h b/include/spi_flash.h
index 15f7471..2e81a14 100644
--- a/include/spi_flash.h
+++ b/include/spi_flash.h
@@ -17,6 +17,7 @@
 
 #include <dm.h>	/* Because we dereference struct udevice here */
 #include <linux/types.h>
+#include <linux/mtd/mtd.h>
 
 #ifndef CONFIG_SF_DEFAULT_SPEED
 # define CONFIG_SF_DEFAULT_SPEED	1000000
@@ -59,12 +60,6 @@ struct spi_slave;
  * @flash_lock:		lock a region of the SPI Flash
  * @flash_unlock:	unlock a region of the SPI Flash
  * @flash_is_locked:	check if a region of the SPI Flash is completely locked
- * @read:		Flash read ops: Read len bytes at offset into buf
- *			Supported cmds: Fast Array Read
- * @write:		Flash write ops: Write len bytes from buf into offset
- *			Supported cmds: Page Program
- * @erase:		Flash erase ops: Erase len bytes from offset
- *			Supported cmds: Sector erase 4K, 32K, 64K
  * return 0 - Success, 1 - Failure
  */
 struct spi_flash {
@@ -98,10 +93,6 @@ struct spi_flash {
 	int (*flash_lock)(struct spi_flash *flash, u32 ofs, size_t len);
 	int (*flash_unlock)(struct spi_flash *flash, u32 ofs, size_t len);
 	int (*flash_is_locked)(struct spi_flash *flash, u32 ofs, size_t len);
-	int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
-	int (*write)(struct spi_flash *flash, u32 offset, size_t len,
-			const void *buf);
-	int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
 };
 
 struct dm_spi_flash_ops {
@@ -166,19 +157,26 @@ int spi_flash_remove(struct udevice *flash);
 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
 				 size_t len, void *buf)
 {
-	return spi_flash_read_dm(flash->dev, offset, len, buf);
+	return mtd_read(flash->mtd, offset, len, &len, (u_char *)buf);
 }
 
 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
 				  size_t len, const void *buf)
 {
-	return spi_flash_write_dm(flash->dev, offset, len, buf);
+	return mtd_write(flash->mtd, offset, len, &len, (u_char *)buf);
 }
 
 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
 				  size_t len)
 {
-	return spi_flash_erase_dm(flash->dev, offset, len);
+	struct erase_info instr;
+
+	instr.mtd = flash->mtd;
+	instr.addr = offset;
+	instr.len = len;
+	instr.callback = 0;
+
+	return mtd_erase(flash->mtd, &instr);
 }
 
 struct sandbox_state;
-- 
1.9.1



More information about the U-Boot mailing list