[U-Boot] [PATCH v5 11/32] cmd: sf: Add mtd_info for mtd-dm-spi-nor code

Jagan Teki jteki at openedev.com
Wed Feb 10 20:08:06 CET 2016


This patch adds support for mtd_info operations
handling from cmd/sf.c

Cc: Simon Glass <sjg at chromium.org>
Cc: Bin Meng <bmeng.cn at gmail.com>
Cc: Mugunthan V N <mugunthanvnm at ti.com>
Cc: Michal Simek <michal.simek at xilinx.com>
Cc: Siva Durga Prasad Paladugu <sivadur at xilinx.com>
Signed-off-by: Jagan Teki <jteki at openedev.com>
---
 cmd/sf.c            | 34 +++++++++++++++++-----------------
 include/spi_flash.h | 48 ++++++++++++++++++++++++++++++++++++------------
 2 files changed, 53 insertions(+), 29 deletions(-)

diff --git a/cmd/sf.c b/cmd/sf.c
index e4d1274..5dd7177 100644
--- a/cmd/sf.c
+++ b/cmd/sf.c
@@ -19,7 +19,7 @@
 #include <asm/io.h>
 #include <dm/device-internal.h>
 
-static struct spi_flash *flash;
+static spi_flash_t *flash;
 
 /*
  * This function computes the length argument for the erase command.
@@ -53,8 +53,8 @@ static int sf_parse_len_arg(char *arg, ulong *len)
 	if (ep == arg || *ep != '\0')
 		return -1;
 
-	if (round_up_len && flash->sector_size > 0)
-		*len = ROUND(len_arg, flash->sector_size);
+	if (round_up_len && flash->erasesize > 0)
+		*len = ROUND(len_arg, flash->erasesize);
 	else
 		*len = len_arg;
 
@@ -89,7 +89,7 @@ static int do_spi_flash_probe(int argc, char * const argv[])
 	struct udevice *new, *bus_dev;
 	int ret;
 #else
-	struct spi_flash *new;
+	spi_flash_t *new;
 #endif
 
 	if (argc >= 2) {
@@ -166,15 +166,15 @@ static int do_spi_flash_probe(int argc, char * const argv[])
  * @param skipped	Count of skipped data (incremented by this function)
  * @return NULL if OK, else a string containing the stage which failed
  */
-static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
+static const char *spi_flash_update_block(spi_flash_t *flash, u32 offset,
 		size_t len, const char *buf, char *cmp_buf, size_t *skipped)
 {
 	char *ptr = (char *)buf;
 
-	debug("offset=%#x, sector_size=%#x, len=%#zx\n",
-	      offset, flash->sector_size, len);
+	debug("offset=%#x, erasesize=%#x, len=%#zx\n",
+	      offset, flash->erasesize, len);
 	/* Read the entire sector so to allow for rewriting */
-	if (spi_flash_read(flash, offset, flash->sector_size, cmp_buf))
+	if (spi_flash_read(flash, offset, flash->erasesize, cmp_buf))
 		return "read";
 	/* Compare only what is meaningful (len) */
 	if (memcmp(cmp_buf, buf, len) == 0) {
@@ -184,15 +184,15 @@ static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
 		return NULL;
 	}
 	/* Erase the entire sector */
-	if (spi_flash_erase(flash, offset, flash->sector_size))
+	if (spi_flash_erase(flash, offset, flash->erasesize))
 		return "erase";
 	/* If it's a partial sector, copy the data into the temp-buffer */
-	if (len != flash->sector_size) {
+	if (len != flash->erasesize) {
 		memcpy(cmp_buf, buf, len);
 		ptr = cmp_buf;
 	}
 	/* Write one complete sector */
-	if (spi_flash_write(flash, offset, flash->sector_size, ptr))
+	if (spi_flash_write(flash, offset, flash->erasesize, ptr))
 		return "write";
 
 	return NULL;
@@ -208,7 +208,7 @@ static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
  * @param buf		buffer to write from
  * @return 0 if ok, 1 on error
  */
-static int spi_flash_update(struct spi_flash *flash, u32 offset,
+static int spi_flash_update(spi_flash_t *flash, u32 offset,
 		size_t len, const char *buf)
 {
 	const char *err_oper = NULL;
@@ -223,12 +223,12 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
 
 	if (end - buf >= 200)
 		scale = (end - buf) / 100;
-	cmp_buf = memalign(ARCH_DMA_MINALIGN, flash->sector_size);
+	cmp_buf = memalign(ARCH_DMA_MINALIGN, flash->erasesize);
 	if (cmp_buf) {
 		ulong last_update = get_timer(0);
 
 		for (; buf < end && !err_oper; buf += todo, offset += todo) {
-			todo = min_t(size_t, end - buf, flash->sector_size);
+			todo = min_t(size_t, end - buf, flash->erasesize);
 			if (get_timer(last_update) > 100) {
 				printf("   \rUpdating, %zu%% %lu B/s",
 				       100 - (end - buf) / scale,
@@ -280,7 +280,7 @@ static int do_spi_flash_read_write(int argc, char * const argv[])
 
 	/* Consistency checking */
 	if (offset + len > flash->size) {
-		printf("ERROR: attempting %s past flash size (%#x)\n",
+		printf("ERROR: attempting %s past flash size (%#llx)\n",
 		       argv[0], flash->size);
 		return 1;
 	}
@@ -336,7 +336,7 @@ static int do_spi_flash_erase(int argc, char * const argv[])
 
 	/* Consistency checking */
 	if (offset + size > flash->size) {
-		printf("ERROR: attempting %s past flash size (%#x)\n",
+		printf("ERROR: attempting %s past flash size (%#llx)\n",
 		       argv[0], flash->size);
 		return 1;
 	}
@@ -436,7 +436,7 @@ static void spi_test_next_stage(struct test_info *test)
  * @param vbuf		Verification buffer
  * @return 0 if ok, -1 on error
  */
-static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len,
+static int spi_flash_test(spi_flash_t *flash, uint8_t *buf, ulong len,
 			   ulong offset, uint8_t *vbuf)
 {
 	struct test_info test;
diff --git a/include/spi_flash.h b/include/spi_flash.h
index 1dcce13..719a6e3 100644
--- a/include/spi_flash.h
+++ b/include/spi_flash.h
@@ -12,6 +12,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
@@ -192,6 +193,41 @@ void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
 
 #elif CONFIG_MTD_DM_SPI_NOR
 
+typedef struct mtd_info spi_flash_t;
+
+static inline int spi_flash_read(spi_flash_t *info, u32 offset,
+				 size_t len, void *buf)
+{
+	return mtd_read(info, offset, len, &len, (u_char *)buf);
+}
+
+static inline int spi_flash_write(spi_flash_t *info, u32 offset,
+				  size_t len, const void *buf)
+{
+	return mtd_write(info, offset, len, &len, (u_char *)buf);
+}
+
+static inline int spi_flash_erase(spi_flash_t *info, u32 offset, size_t len)
+{
+	struct erase_info instr;
+
+	instr.mtd = info;
+	instr.addr = offset;
+	instr.len = len;
+	instr.callback = 0;
+
+	return mtd_erase(info, &instr);
+}
+
+static inline int spi_flash_protect(spi_flash_t *info, u32 ofs,
+				    u32 len, bool prot)
+{
+	if (prot)
+		return mtd_lock(info, ofs, len);
+	else
+		return mtd_unlock(info, ofs, len);
+}
+
 int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
 			   unsigned int max_hz, unsigned int spi_mode,
 			   struct udevice **devp);
@@ -237,18 +273,6 @@ static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
 }
 #endif
 
-static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
-					bool prot)
-{
-	if (!flash->flash_lock || !flash->flash_unlock)
-		return -EOPNOTSUPP;
-
-	if (prot)
-		return flash->flash_lock(flash, ofs, len);
-	else
-		return flash->flash_unlock(flash, ofs, len);
-}
-
 void spi_boot(void) __noreturn;
 void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
 
-- 
1.9.1



More information about the U-Boot mailing list