public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Jagan Teki <jteki@openedev.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v7 17/34] sf: Use mtd_info ops instead of spi_flash ops
Date: Thu, 26 Nov 2015 17:34:01 +0530	[thread overview]
Message-ID: <1448539458-14306-18-git-send-email-jteki@openedev.com> (raw)
In-Reply-To: <1448539458-14306-1-git-send-email-jteki@openedev.com>

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@denx.de>
Signed-off-by: Jagan Teki <jteki@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

  parent reply	other threads:[~2015-11-26 12:04 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-26 12:03 [U-Boot] [PATCH v7 00/34] sf: MTD support Jagan Teki
2015-11-26 12:03 ` [U-Boot] [PATCH v7 01/34] sf: spi_flash_validate_params => spi_flash_scan Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:03 ` [U-Boot] [PATCH v7 02/34] sf: Move spi_flash_scan code to sf_ops Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:03 ` [U-Boot] [PATCH v7 03/34] sf: Move read_id " Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:03 ` [U-Boot] [PATCH v7 04/34] sf: probe: Code cleanup Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:03 ` [U-Boot] [PATCH v7 05/34] sf: Use static for file-scope functions Jagan Teki
2015-11-26 12:03 ` [U-Boot] [PATCH v7 06/34] sf: Fix Makefile Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-12-03 10:20   ` Bin Meng
2015-11-26 12:03 ` [U-Boot] [PATCH v7 07/34] sf: Use simple name for register access functions Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:03 ` [U-Boot] [PATCH v7 08/34] sf: Use flash function pointers in dm_spi_flash_ops Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-12-03 16:42     ` Jagan Teki
2015-11-26 12:03 ` [U-Boot] [PATCH v7 09/34] sf: Flash power up read-only based on idcode0 Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:03 ` [U-Boot] [PATCH v7 10/34] sf: Use static for file-scope functions Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:03 ` [U-Boot] [PATCH v7 11/34] sf: Remove unneeded header includes Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:03 ` [U-Boot] [PATCH v7 12/34] sf: probe: Use spi_flash_scan in dm-spi-flash Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 19:10     ` Jagan Teki
2015-11-26 12:03 ` [U-Boot] [PATCH v7 13/34] sf: Re-factorize spi_flash_probe_tail code Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:03 ` [U-Boot] [PATCH v7 14/34] dm-sf: Re-factorize spi_flash_std_probe code Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:03 ` [U-Boot] [PATCH v7 15/34] zynq: Enable CONFIG_SPL_MTD_SUPPORT Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:04 ` [U-Boot] [PATCH v7 16/34] sf: Add MTD support to spi_flash Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 19:14     ` Jagan Teki
2015-11-26 12:04 ` Jagan Teki [this message]
2015-11-26 12:04 ` [U-Boot] [PATCH v7 18/34] cmd_sf: Use mtd->size instead of flash->size Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 19/34] sf: Use mtd->erasesize instead of flash->erase_size Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 20/34] dm-sf: use mtd_ops, drop dm_spi_flash_ops Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:04 ` [U-Boot] [PATCH v7 21/34] sf: Use MTD lock operations Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 22/34] sf: Add MTD support for non-dm spi_flash interface Jagan Teki
2015-11-26 17:50   ` Simon Glass
2015-11-26 12:04 ` [U-Boot] [PATCH v7 23/34] sf: probe: Minor cleanup Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 24/34] sf: Drop SNOR_F_SST_WR flash->flags Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 25/34] sf: Remove unneeded SST_BP and SST_WP Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 26/34] sf: ops: Fix missing break on spansion read_bar Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 27/34] sf: Drop SPI_FLASH_MTD driver Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 28/34] configs: Remove CONFIG_SPI_FLASH_MTD Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 29/34] sf: dataflash: Remove unneeded spi data Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 30/34] sf: dataflash: Move flash id detection into jedec_probe Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 31/34] sf: dataflash: Fix add_dataflash return logic Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 32/34] sf: dataflash: Add MTD core support Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 33/34] sf: dataflash: Rename sf_dataflash.c to mtd_dataflash.c Jagan Teki
2015-11-26 12:04 ` [U-Boot] [PATCH v7 34/34] mtd: dataflash: Minor cleanups Jagan Teki
2015-11-26 12:24 ` [U-Boot] [PATCH v7 00/34] sf: MTD support Jagan Teki
2015-11-26 12:32   ` Stefan Roese
2015-11-26 12:46     ` Jagan Teki
2015-11-26 13:04       ` Stefan Roese
2015-11-26 13:24         ` Jagan Teki
2015-11-26 16:48         ` Simon Glass
2015-11-26 17:47           ` Stefan Roese
2015-11-26 17:55             ` Simon Glass
2015-11-26 18:12               ` Stefan Roese
2015-11-26 18:22                 ` Simon Glass
2015-11-27  4:01                   ` Stefan Roese
2015-11-27  6:32                     ` Stefan Roese
2015-11-26 17:50 ` Simon Glass
2015-11-26 18:54   ` Jagan Teki
2015-11-27  2:25     ` Bin Meng
2015-11-27  9:21       ` Jagan Teki
2015-11-30 23:17         ` Simon Glass
2015-12-03 10:14           ` Jagan Teki
2015-11-27  2:51     ` Simon Glass
2015-11-27  1:54 ` Bin Meng
2015-11-27  8:50   ` Jagan Teki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1448539458-14306-18-git-send-email-jteki@openedev.com \
    --to=jteki@openedev.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox