Skip to contentSkip to conversation

sf: Add MTD support to spi_flash

PATCH New Jagan Teki 3 participants 16 messages Oct 12, 2015 – Nov 6, 2015 Memory technology devices (MTD)

lore
Share & tools
lore

Jagan Teki

author
#0 lore raw

PATCH 1/8 sf: Add MTD support to spi_flash

This patch adds mtd_info support to spi_flash layer, MTD has proven core for flash operations so adding MTD to spi_flash will extends more functionality.

---
 drivers/mtd/spi/sf_ops.c   | 45 +++++++++++++++++++++++++--------------------
 drivers/mtd/spi/sf_probe.c | 26 ++++++++++++++++++--------
 include/spi_flash.h        |  9 +++++----
 3 files changed, 48 insertions(+), 32 deletions(-)
drivers/mtd/spi/sf_ops.c +25 -20
index 703099f..f5ee376 100644
@@ -146,7 +146,7 @@ static int spi_flash_read_bar(struct spi_flash *flash, u8 idcode0)
 	u8 curr_bank = 0;
 	int ret;
 
-	if (flash->size <= SPI_FLASH_16MB_BOUN)
+	if (flash->mtd->size <= SPI_FLASH_16MB_BOUN)
 		goto bank_end;
 
 	switch (idcode0) {
@@ -176,8 +176,8 @@ static void spi_flash_dual(struct spi_flash *flash, u32 *addr)
 {
 	switch (flash->dual_flash) {
 	case SF_DUAL_STACKED_FLASH:
-		if (*addr >= (flash->size >> 1)) {
-			*addr -= flash->size >> 1;
+		if (*addr >= (flash->mtd->size >> 1)) {
+			*addr -= flash->mtd->size >> 1;
 			flash->spi->flags |= SPI_XFER_U_PAGE;
 		} else {
 			flash->spi->flags &= ~SPI_XFER_U_PAGE;
@@ -303,7 +303,7 @@ static int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset,
 	u8 cmd[SPI_FLASH_CMD_LEN];
 	int ret = -1;
 
-	erase_size = flash->erase_size;
+	erase_size = mtd->erasesize;
 	if (offset % erase_size || len % erase_size) {
 		debug("SF: Erase offset/length not multiple of erase size\n");
 		return -1;
@@ -693,7 +693,7 @@ int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
 		return 0;
 	}
 
-	if (flash->size != size) {
+	if (flash->mtd->size != size) {
 		debug("%s: Memory map must cover entire device\n", __func__);
 		return -1;
 	}
@@ -705,6 +705,7 @@ int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
 
 int spi_flash_scan(struct spi_flash *flash)
 {
+	struct mtd_info *mtd = flash->mtd;
 	const struct spi_flash_params *params;
 	u16 jedec, ext_jedec;
 	u8 idcode[5];
@@ -754,24 +755,27 @@ int spi_flash_scan(struct spi_flash *flash)
 
 	/* Assign spi data */
 	flash->name = params->name;
+	mtd->type = MTD_NORFLASH;
+	mtd->writesize = 1;
+	mtd->flags = MTD_CAP_NORFLASH;
 	flash->memory_map = flash->spi->memory_map;
 	flash->dual_flash = flash->spi->option;
 
 	/* Assign spi_flash ops */
-	flash->write = spi_flash_cmd_write_ops;
+	mtd->_write = spi_flash_cmd_write_ops;
 #if defined(CONFIG_SPI_FLASH_SST)
 	if (params->flags & SST_WR)
 		flash->flags |= SNOR_F_SST_WR;
 
 	if (params->flags & SNOR_F_SST_WR) {
 		if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
-			flash->write = sst_write_bp;
+			mtd->_write = sst_write_bp;
 		else
-			flash->write = sst_write_wp;
+			mtd->_write = sst_write_wp;
 	}
 #endif
-	flash->erase = spi_flash_cmd_erase_ops;
-	flash->read = spi_flash_cmd_read_ops;
+	mtd->_erase = spi_flash_cmd_erase_ops;
+	mtd->_read = spi_flash_cmd_read_ops;
 
 	/* Compute the flash size */
 	flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
@@ -790,27 +794,28 @@ int spi_flash_scan(struct spi_flash *flash)
 		flash->page_size = 256;
 	}
 	flash->page_size <<= flash->shift;
+	mtd->writebufsize = flash->page_size;
 	flash->sector_size = params->sector_size << flash->shift;
-	flash->size = flash->sector_size * params->nr_sectors << flash->shift;
+	mtd->size = flash->sector_size * params->nr_sectors << flash->shift;
 #ifdef CONFIG_SF_DUAL_FLASH
 	if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
-		flash->size <<= 1;
+		mtd->size <<= 1;
 #endif
 
 	/* Compute erase sector and command */
 	if (params->flags & SECT_4K) {
 		flash->erase_cmd = CMD_ERASE_4K;
-		flash->erase_size = 4096 << flash->shift;
+		mtd->erasesize = 4096 << flash->shift;
 	} else if (params->flags & SECT_32K) {
 		flash->erase_cmd = CMD_ERASE_32K;
-		flash->erase_size = 32768 << flash->shift;
+		mtd->erasesize = 32768 << flash->shift;
 	} else {
 		flash->erase_cmd = CMD_ERASE_64K;
-		flash->erase_size = flash->sector_size;
+		mtd->erasesize = flash->sector_size;
 	}
 
 	/* Now erase size becomes valid sector size */
-	flash->sector_size = flash->erase_size;
+	flash->sector_size = mtd->erasesize;
 
 	/* Look for the fastest read cmd */
 	cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
@@ -882,8 +887,8 @@ int spi_flash_scan(struct spi_flash *flash)
 #ifndef CONFIG_SPL_BUILD
 	printf("SF: Detected %s with page size ", flash->name);
 	print_size(flash->page_size, ", erase size ");
-	print_size(flash->erase_size, ", total ");
-	print_size(flash->size, "");
+	print_size(mtd->erasesize, ", total ");
+	print_size(mtd->size, "");
 	if (flash->memory_map)
 		printf(", mapped at %p", flash->memory_map);
 	puts("\n");
@@ -891,9 +896,9 @@ int spi_flash_scan(struct spi_flash *flash)
 
 #ifndef CONFIG_SPI_FLASH_BAR
 	if (((flash->dual_flash == SF_SINGLE_FLASH) &&
-	     (flash->size > SPI_FLASH_16MB_BOUN)) ||
+	     (mtd->size > SPI_FLASH_16MB_BOUN)) ||
 	     ((flash->dual_flash > SF_SINGLE_FLASH) &&
-	     (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
+	     (mtd->size > SPI_FLASH_16MB_BOUN << 1))) {
 		puts("SF: Warning - Only lower 16MiB accessible,");
 		puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
 	}
drivers/mtd/spi/sf_probe.c +18 -8
index 87ac33e..b8704e2 100644
@@ -14,9 +14,15 @@
 #include <malloc.h>
 #include <spi.h>
 #include <spi_flash.h>
+#include <linux/mtd/mtd.h>
 
 #include "sf_internal.h"
 
+struct spi_flash_priv {
+	struct spi_flash	flash;
+	struct mtd_info		mtd;
+};
+
 #ifndef CONFIG_DM_SPI_FLASH
 struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
 {
@@ -123,12 +129,19 @@ int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
 
 int spi_flash_std_probe(struct udevice *dev)
 {
-	struct spi_flash *flash = dev_get_uclass_priv(dev);
+	struct spi_flash_priv *priv = dev_get_uclass_priv(dev);
 	struct spi_slave *slave = dev_get_parentdata(dev);
+	struct spi_flash *flash;
 	int ret;
 
-	flash->dev = dev;
+	flash = &priv->flash;
+	flash->mtd = &priv->mtd;
+
 	flash->spi = slave;
+	flash->priv = priv;
+
+	priv->mtd.priv = flash;
+	flash->dev = dev;
 
 	/* Claim spi bus */
 	ret = spi_claim_bus(slave);
@@ -143,19 +156,16 @@ int spi_flash_std_probe(struct udevice *dev)
 		goto err_scan;
 	}
 
-#ifdef CONFIG_SPI_FLASH_MTD
-	ret = spi_flash_mtd_register(flash);
+	ret = add_mtd_device(&priv->mtd);
 	if (ret) {
 		printf("SF: failed to register mtd device: %d\n", ret);
 		goto err_mtd;
 	}
-#endif
+
 	return ret;
 
-#ifdef CONFIG_SPI_FLASH_MTD
 err_mtd:
 	spi_free_slave(slave);
-#endif
 err_scan:
 	spi_release_bus(slave);
 	return ret;
@@ -177,7 +187,7 @@ U_BOOT_DRIVER(spi_flash_std) = {
 	.id		= UCLASS_SPI_FLASH,
 	.of_match	= spi_flash_std_ids,
 	.probe		= spi_flash_std_probe,
-	.priv_auto_alloc_size = sizeof(struct spi_flash),
+	.priv_auto_alloc_size = sizeof(struct spi_flash_priv),
 	.ops		= &spi_flash_std_ops,
 };
 
include/spi_flash.h +5 -4
index 0732172..d0af8d3 100644
@@ -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
@@ -36,16 +37,15 @@ struct spi_slave;
 /**
  * struct spi_flash - SPI flash structure
  *
+ * @mtd:		point to a mtd_info structure
  * @spi:		SPI slave
  * @dev:		SPI flash device
  * @name:		Name of SPI flash
  * @dual_flash:		Indicates dual flash memories - dual stacked, parallel
  * @shift:		Flash shift useful in dual parallel
  * @flags:		Indication of spi flash flags
- * @size:		Total flash size
  * @page_size:		Write (page) size
  * @sector_size:	Sector size
- * @erase_size:		Erase size
  * @bank_read_cmd:	Bank read cmd
  * @bank_write_cmd:	Bank write cmd
  * @bank_curr:		Current flash bank
@@ -54,6 +54,7 @@ struct spi_slave;
  * @write_cmd:		Write cmd - page and quad program.
  * @dummy_byte:		Dummy cycles for read operation.
  * @memory_map:		Address of read-only SPI flash access
+ * @priv:		the private data
  * @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
@@ -63,6 +64,7 @@ struct spi_slave;
  * return 0 - Success, 1 - Failure
  */
 struct spi_flash {
+	struct mtd_info *mtd;
 	struct spi_slave *spi;
 #ifdef CONFIG_DM_SPI_FLASH
 	struct udevice *dev;
@@ -72,10 +74,8 @@ struct spi_flash {
 	u8 shift;
 	u16 flags;
 
-	u32 size;
 	u32 page_size;
 	u32 sector_size;
-	u32 erase_size;
 #ifdef CONFIG_SPI_FLASH_BAR
 	u8 bank_read_cmd;
 	u8 bank_write_cmd;
@@ -87,6 +87,7 @@ struct spi_flash {
 	u8 dummy_byte;
 
 	void *memory_map;
+	void *priv;
 	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);
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 2/8 sf: Use mtd_info ops instead of spi_flash ops

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

---
 drivers/mtd/spi/sf_ops.c | 66 ++++++++++++++++++++++++++++--------------------
 include/spi_flash.h      | 24 ++++++++----------
 2 files changed, 49 insertions(+), 41 deletions(-)
drivers/mtd/spi/sf_ops.c +39 -27
index f5ee376..d10ca45 100644
@@ -296,18 +296,16 @@ 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];
 	int ret = -1;
 
-	erase_size = mtd->erasesize;
-	if (offset % erase_size || len % erase_size) {
-		debug("SF: Erase offset/length not multiple of erase size\n");
-		return -1;
-	}
+	offset = instr->addr;
+	len = instr->len;
 
 	cmd[0] = flash->erase_cmd;
 	while (len) {
@@ -330,19 +328,24 @@ 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;
 	}
 
 	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;
@@ -384,6 +387,7 @@ static int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
 		}
 
 		offset += chunk_len;
+		*retlen += chunk_len;
 	}
 
 	return ret;
@@ -417,11 +421,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;
 
@@ -478,6 +483,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);
@@ -485,7 +491,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] = {
@@ -506,12 +513,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];
@@ -525,7 +535,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;
 	}
@@ -542,7 +552,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);
 
@@ -559,6 +569,7 @@ static int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
 
 		cmd_len = 1;
 		offset += 2;
+		*retlen += 2;
 	}
 
 	if (!ret)
@@ -566,19 +577,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;
 
@@ -589,7 +601,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;
@@ -600,7 +612,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);
include/spi_flash.h +10 -14
index d0af8d3..fe03b8d 100644
@@ -55,13 +55,6 @@ struct spi_slave;
  * @dummy_byte:		Dummy cycles for read operation.
  * @memory_map:		Address of read-only SPI flash access
  * @priv:		the private data
- * @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 {
 	struct mtd_info *mtd;
@@ -88,10 +81,6 @@ struct spi_flash {
 
 	void *memory_map;
 	void *priv;
-	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 {
@@ -156,19 +145,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;
Signature · 1 line
1.9.1
#2 lore raw

PATCH 2/8 sf: Use mtd_info ops instead of spi_flash ops

Hello Jagan,

Am 12.10.2015 um 21:54 schrieb Jagan Teki:

Quoted 8 lines

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

---
  drivers/mtd/spi/sf_ops.c | 66 ++++++++++++++++++++++++++++--------------------
  include/spi_flash.h      | 24 ++++++++----------
  2 files changed, 49 insertions(+), 41 deletions(-)

bye,
Heiko

Quoted 249 lines
drivers/mtd/spi/sf_ops.c +39 -27
index f5ee376..d10ca45 100644
@@ -296,18 +296,16 @@ 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];
  	int ret = -1;
 
-	erase_size = mtd->erasesize;
-	if (offset % erase_size || len % erase_size) {
-		debug("SF: Erase offset/length not multiple of erase size\n");
-		return -1;
-	}
+	offset = instr->addr;
+	len = instr->len;
 
  	cmd[0] = flash->erase_cmd;
  	while (len) {
@@ -330,19 +328,24 @@ 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;
  	}
 
  	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;
@@ -384,6 +387,7 @@ static int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
  		}
 
  		offset += chunk_len;
+		*retlen += chunk_len;
  	}
 
  	return ret;
@@ -417,11 +421,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;
 
@@ -478,6 +483,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);
@@ -485,7 +491,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] = {
@@ -506,12 +513,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];
@@ -525,7 +535,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;
  	}
@@ -542,7 +552,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);
 
@@ -559,6 +569,7 @@ static int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
 
  		cmd_len = 1;
  		offset += 2;
+		*retlen += 2;
  	}
 
  	if (!ret)
@@ -566,19 +577,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;
 
@@ -589,7 +601,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;
@@ -600,7 +612,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);
include/spi_flash.h +10 -14
index d0af8d3..fe03b8d 100644
@@ -55,13 +55,6 @@ struct spi_slave;
   * @dummy_byte:		Dummy cycles for read operation.
   * @memory_map:		Address of read-only SPI flash access
   * @priv:		the private data
- * @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 {
  	struct mtd_info *mtd;
@@ -88,10 +81,6 @@ struct spi_flash {
 
  	void *memory_map;
  	void *priv;
-	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 {
@@ -156,19 +145,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;
Signature · 2 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
author
#3 lore raw

PATCH 3/8 cmd_sf: Use mtd->size instead of flash->size

In reply to Jagan Teki

Since mtd got added, replace flash->size with mtd->size.

---
 common/cmd_sf.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
common/cmd_sf.c +8 -8
index ac7f5df..a501376 100644
@@ -275,13 +275,13 @@ static int do_spi_flash_read_write(int argc, char * const argv[])
 		return -1;
 
 	if (mtd_arg_off_size(argc - 2, &argv[2], &dev, &offset, &len,
-			     &maxsize, MTD_DEV_TYPE_NOR, flash->size))
+			     &maxsize, MTD_DEV_TYPE_NOR, flash->mtd->size))
 		return -1;
 
 	/* Consistency checking */
-	if (offset + len > flash->size) {
-		printf("ERROR: attempting %s past flash size (%#x)\n",
-		       argv[0], flash->size);
+	if (offset + len > flash->mtd->size) {
+		printf("ERROR: attempting %s past flash size (0x%llx)\n",
+		       argv[0], flash->mtd->size);
 		return 1;
 	}
 
@@ -327,7 +327,7 @@ static int do_spi_flash_erase(int argc, char * const argv[])
 		return -1;
 
 	if (mtd_arg_off(argv[1], &dev, &offset, &len, &maxsize,
-			MTD_DEV_TYPE_NOR, flash->size))
+			MTD_DEV_TYPE_NOR, flash->mtd->size))
 		return -1;
 
 	ret = sf_parse_len_arg(argv[2], &size);
@@ -335,9 +335,9 @@ static int do_spi_flash_erase(int argc, char * const argv[])
 		return -1;
 
 	/* Consistency checking */
-	if (offset + size > flash->size) {
-		printf("ERROR: attempting %s past flash size (%#x)\n",
-		       argv[0], flash->size);
+	if (offset + size > flash->mtd->size) {
+		printf("ERROR: attempting %s past flash size (0x%llx)\n",
+		       argv[0], flash->mtd->size);
 		return 1;
 	}
 
Signature · 1 line
1.9.1
#4 lore raw

PATCH 3/8 cmd_sf: Use mtd->size instead of flash->size

Hello Jagan,

Am 12.10.2015 um 21:54 schrieb Jagan Teki:

Quoted 6 lines

Since mtd got added, replace flash->size with mtd->size.

---
  common/cmd_sf.c | 16 ++++++++--------
  1 file changed, 8 insertions(+), 8 deletions(-)

bye,
Heiko

Quoted 46 lines
common/cmd_sf.c +8 -8
index ac7f5df..a501376 100644
@@ -275,13 +275,13 @@ static int do_spi_flash_read_write(int argc, char * const argv[])
  		return -1;
 
  	if (mtd_arg_off_size(argc - 2, &argv[2], &dev, &offset, &len,
-			     &maxsize, MTD_DEV_TYPE_NOR, flash->size))
+			     &maxsize, MTD_DEV_TYPE_NOR, flash->mtd->size))
  		return -1;
 
  	/* Consistency checking */
-	if (offset + len > flash->size) {
-		printf("ERROR: attempting %s past flash size (%#x)\n",
-		       argv[0], flash->size);
+	if (offset + len > flash->mtd->size) {
+		printf("ERROR: attempting %s past flash size (0x%llx)\n",
+		       argv[0], flash->mtd->size);
  		return 1;
  	}
 
@@ -327,7 +327,7 @@ static int do_spi_flash_erase(int argc, char * const argv[])
  		return -1;
 
  	if (mtd_arg_off(argv[1], &dev, &offset, &len, &maxsize,
-			MTD_DEV_TYPE_NOR, flash->size))
+			MTD_DEV_TYPE_NOR, flash->mtd->size))
  		return -1;
 
  	ret = sf_parse_len_arg(argv[2], &size);
@@ -335,9 +335,9 @@ static int do_spi_flash_erase(int argc, char * const argv[])
  		return -1;
 
  	/* Consistency checking */
-	if (offset + size > flash->size) {
-		printf("ERROR: attempting %s past flash size (%#x)\n",
-		       argv[0], flash->size);
+	if (offset + size > flash->mtd->size) {
+		printf("ERROR: attempting %s past flash size (0x%llx)\n",
+		       argv[0], flash->mtd->size);
  		return 1;
  	}
 
Signature · 2 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
author
#5 lore raw

PATCH 4/8 dm-sf: use mtd_ops, drop dm_spi_flash_ops

In reply to Jagan Teki

Since mtd_info ops got introduced, just drop the unneeded dm_spi_flash operations.

---
 drivers/mtd/spi/sf-uclass.c | 16 --------
 drivers/mtd/spi/sf_probe.c  | 30 ---------------
 include/spi_flash.h         | 91 +++++++--------------------------------------
 3 files changed, 14 insertions(+), 123 deletions(-)
drivers/mtd/spi/sf-uclass.c +0 -16
index 350e21a..5cdbd1b 100644
@@ -11,22 +11,6 @@
 #include <dm/device-internal.h>
 #include "sf_internal.h"
 
-int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf)
-{
-	return sf_get_ops(dev)->read(dev, offset, len, buf);
-}
-
-int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
-		       const void *buf)
-{
-	return sf_get_ops(dev)->write(dev, offset, len, buf);
-}
-
-int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
-{
-	return sf_get_ops(dev)->erase(dev, offset, len);
-}
-
 /*
  * TODO(sjg at chromium.org): This is an old-style function. We should remove
  * it when all SPI flash drivers use dm
drivers/mtd/spi/sf_probe.c +0 -30
index b8704e2..5e314e2 100644
@@ -104,29 +104,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_uclass_priv(dev);
@@ -171,12 +148,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" },
 	{ }
@@ -188,7 +159,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 */
include/spi_flash.h +14 -77

122 lines not on this page · raw message

Signature · 1 line
1.9.1
#6 lore raw

PATCH 4/8 dm-sf: use mtd_ops, drop dm_spi_flash_ops

Hello Jagan,

Am 12.10.2015 um 21:54 schrieb Jagan Teki:

Quoted 9 lines

Since mtd_info ops got introduced, just drop the unneeded dm_spi_flash operations.

---
  drivers/mtd/spi/sf-uclass.c | 16 --------
  drivers/mtd/spi/sf_probe.c  | 30 ---------------
  include/spi_flash.h         | 91 +++++++--------------------------------------
  3 files changed, 14 insertions(+), 123 deletions(-)

Does this not break: drivers/mtd/spi/sf-uclass.c ?

added simon to cc.

bye,
Heiko

Quoted 213 lines
drivers/mtd/spi/sf-uclass.c +0 -16
index 350e21a..5cdbd1b 100644
@@ -11,22 +11,6 @@
  #include <dm/device-internal.h>
  #include "sf_internal.h"
 
-int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf)
-{
-	return sf_get_ops(dev)->read(dev, offset, len, buf);
-}
-
-int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
-		       const void *buf)
-{
-	return sf_get_ops(dev)->write(dev, offset, len, buf);
-}
-
-int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
-{
-	return sf_get_ops(dev)->erase(dev, offset, len);
-}
-
  /*
   * TODO(sjg at chromium.org): This is an old-style function. We should remove
   * it when all SPI flash drivers use dm
drivers/mtd/spi/sf_probe.c +0 -30
index b8704e2..5e314e2 100644
@@ -104,29 +104,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_uclass_priv(dev);
@@ -171,12 +148,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" },
  	{ }
@@ -188,7 +159,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 */
include/spi_flash.h +14 -77
index fe03b8d..8dd000d 100644
@@ -83,52 +83,7 @@ struct spi_flash {
  	void *priv;
  };
 
-struct dm_spi_flash_ops {
-	int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
-	int (*write)(struct udevice *dev, u32 offset, size_t len,
-		     const void *buf);
-	int (*erase)(struct udevice *dev, u32 offset, size_t len);
-};
-
-/* Access the serial operations for a device */
-#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
-
  #ifdef CONFIG_DM_SPI_FLASH
-/**
- * spi_flash_read_dm() - Read data from SPI flash
- *
- * @dev:	SPI flash device
- * @offset:	Offset into device in bytes to read from
- * @len:	Number of bytes to read
- * @buf:	Buffer to put the data that is read
- * @return 0 if OK, -ve on error
- */
-int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
-
-/**
- * spi_flash_write_dm() - Write data to SPI flash
- *
- * @dev:	SPI flash device
- * @offset:	Offset into device in bytes to write to
- * @len:	Number of bytes to write
- * @buf:	Buffer containing bytes to write
- * @return 0 if OK, -ve on error
- */
-int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
-		       const void *buf);
-
-/**
- * spi_flash_erase_dm() - Erase blocks of the SPI flash
- *
- * Note that @len must be a muiltiple of the flash sector size.
- *
- * @dev:	SPI flash device
- * @offset:	Offset into device in bytes to start erasing
- * @len:	Number of bytes to erase
- * @return 0 if OK, -ve on error
- */
-int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
-
  int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
  			   unsigned int max_hz, unsigned int spi_mode,
  			   struct udevice **devp);
@@ -142,31 +97,6 @@ void spi_flash_free(struct spi_flash *flash);
 
  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 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 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)
-{
-	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;
 
  int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
@@ -191,25 +121,32 @@ struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
  				      int spi_node);
 
  void spi_flash_free(struct spi_flash *flash);
+#endif
 
  static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
-		size_t len, void *buf)
+				 size_t len, void *buf)
  {
-	return flash->read(flash, 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)
+				  size_t len, const void *buf)
  {
-	return flash->write(flash, 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)
+				  size_t len)
  {
-	return flash->erase(flash, 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);
  }
-#endif
 
  void spi_boot(void) __noreturn;
  void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
Signature · 2 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
author
#7 lore raw

PATCH 4/8 dm-sf: use mtd_ops, drop dm_spi_flash_ops

Hi Heiko,

Quoted 16 lines · Heiko Schocher

Hello Jagan,

Am 12.10.2015 um 21:54 schrieb Jagan Teki:

Quoted 11 lines

Since mtd_info ops got introduced, just drop the unneeded dm_spi_flash operations.

---
  drivers/mtd/spi/sf-uclass.c | 16 --------
  drivers/mtd/spi/sf_probe.c  | 30 ---------------
  include/spi_flash.h         | 91
+++++++--------------------------------------
  3 files changed, 14 insertions(+), 123 deletions(-)
Quoted 1 line

Does this not break: drivers/mtd/spi/sf-uclass.c ?

This wouldn't break the dm because mtd_info ops are common to dm or non-dm spi-flash which I tuned in this series[1] That means the tuned sf series is prior to this MTD.

Quoted 221 lines

added simon to cc.

Quoted 217 lines
drivers/mtd/spi/sf-uclass.c +0 -16
index 350e21a..5cdbd1b 100644
@@ -11,22 +11,6 @@
  #include <dm/device-internal.h>
  #include "sf_internal.h"
 
-int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void
 *buf)
-{
-       return sf_get_ops(dev)->read(dev, offset, len, buf);
-}
-
-int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
-                      const void *buf)
-{
-       return sf_get_ops(dev)->write(dev, offset, len, buf);
-}
-
-int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
-{
-       return sf_get_ops(dev)->erase(dev, offset, len);
-}
-
  /*
   * TODO(sjg at chromium.org): This is an old-style function. We should

remove
* it when all SPI flash drivers use dm

drivers/mtd/spi/sf_probe.c +0 -23
index b8704e2..5e314e2 100644
@@ -104,29 +104,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_uclass_priv(dev);
drivers/mtd/spi/sf_probe.c +0 -7
@@ -171,12 +148,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" },
        { }
@@ -188,7 +159,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 */
include/spi_flash.h +0 -45
index fe03b8d..8dd000d 100644
@@ -83,52 +83,7 @@ struct spi_flash {
        void *priv;
  };
 
-struct dm_spi_flash_ops {
-       int (*read)(struct udevice *dev, u32 offset, size_t len, void
 *buf);
-       int (*write)(struct udevice *dev, u32 offset, size_t len,
-                    const void *buf);
-       int (*erase)(struct udevice *dev, u32 offset, size_t len);
-};
-
-/* Access the serial operations for a device */
-#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
-
  #ifdef CONFIG_DM_SPI_FLASH
-/**
- * spi_flash_read_dm() - Read data from SPI flash
- *
- * @dev:       SPI flash device
- * @offset:    Offset into device in bytes to read from
- * @len:       Number of bytes to read
- * @buf:       Buffer to put the data that is read
- * @return 0 if OK, -ve on error
- */
-int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void
 *buf);
-
-/**
- * spi_flash_write_dm() - Write data to SPI flash
- *
- * @dev:       SPI flash device
- * @offset:    Offset into device in bytes to write to
- * @len:       Number of bytes to write
- * @buf:       Buffer containing bytes to write
- * @return 0 if OK, -ve on error
- */
-int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
-                      const void *buf);
-
-/**
- * spi_flash_erase_dm() - Erase blocks of the SPI flash
- *
- * Note that @len must be a muiltiple of the flash sector size.
- *
- * @dev:       SPI flash device
- * @offset:    Offset into device in bytes to start erasing
- * @len:       Number of bytes to erase
- * @return 0 if OK, -ve on error
- */
-int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
-
  int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,

unsigned int max_hz, unsigned int spi_mode, struct udevice **devp);

include/spi_flash.h +0 -25
@@ -142,31 +97,6 @@ void spi_flash_free(struct spi_flash *flash);
 
  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 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 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)
-{
-       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;
 
  int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int

cs,

include/spi_flash.h +14 -7
@@ -191,25 +121,32 @@ struct spi_flash *spi_flash_probe_fdt(const void
 *blob, int slave_node,
                                      int spi_node);
 
  void spi_flash_free(struct spi_flash *flash);
+#endif
 
  static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
-               size_t len, void *buf)
+                                size_t len, void *buf)
  {
-       return flash->read(flash, 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)
+                                 size_t len, const void *buf)
  {
-       return flash->write(flash, 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)
+                                 size_t len)
  {
-       return flash->erase(flash, 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);
  }
-#endif
 
Quoted 2 lines
Quoted 2 lines
include/spi_flash.h +0 -0
  void spi_boot(void) __noreturn;

void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);

[1] https://www.mail-archive.com/u-boot at lists.denx.de/msg190086.html

Signature · 1 line
Jagan | openedev.
#8 lore raw

PATCH 4/8 dm-sf: use mtd_ops, drop dm_spi_flash_ops

Hi Jagan,

Quoted 47 lines · Jagan Teki

Hi Heiko,

Quoted 16 lines · Heiko Schocher

Hello Jagan,

Am 12.10.2015 um 21:54 schrieb Jagan Teki:

Quoted 11 lines

Since mtd_info ops got introduced, just drop the unneeded dm_spi_flash operations.

---
  drivers/mtd/spi/sf-uclass.c | 16 --------
  drivers/mtd/spi/sf_probe.c  | 30 ---------------
  include/spi_flash.h         | 91
+++++++--------------------------------------
  3 files changed, 14 insertions(+), 123 deletions(-)
Quoted 1 line

Does this not break: drivers/mtd/spi/sf-uclass.c ?

This wouldn't break the dm because mtd_info ops are common to dm or non-dm spi-flash which I tuned in this series[1] That means the tuned sf series is prior to this MTD.

Quoted 22 lines

added simon to cc.

Quoted 18 lines
drivers/mtd/spi/sf-uclass.c +0 -8
index 350e21a..5cdbd1b 100644
@@ -11,22 +11,6 @@
  #include <dm/device-internal.h>
  #include "sf_internal.h"
 
-int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void
 *buf)
-{
-       return sf_get_ops(dev)->read(dev, offset, len, buf);
-}
-
-int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
-                      const void *buf)
-{
Quoted 8 lines
Quoted 8 lines

drivers/mtd/spi/sf-uclass.c line 29

Quoted 8 lines
drivers/mtd/spi/sf-uclass.c +0 -8
-       return sf_get_ops(dev)->write(dev, offset, len, buf);
-}
-
-int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
-{
-       return sf_get_ops(dev)->erase(dev, offset, len);
-}
-

These are the functions that we should keep. It is the the ones that don't take a struct udevice parameter which should be removed...

Regards,
Simon

author
#9 lore raw

PATCH 5/8 sf: Add MTD support for non-dm spi_flash interface

In reply to Jagan Teki

This patch adds MTD support to non-dm spi_flash
interface code.

---
 drivers/mtd/spi/sf_probe.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/mtd/spi/sf_probe.c +14 -12
index 5e314e2..60abaf2 100644
@@ -26,17 +26,24 @@ struct spi_flash_priv {
 #ifndef CONFIG_DM_SPI_FLASH
 struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
 {
+	struct spi_flash_priv *priv;
 	struct spi_flash *flash;
 	int ret;
 
-	/* Allocate space if needed (not used by sf-uclass */
-	flash = calloc(1, sizeof(*flash));
-	if (!flash) {
-		debug("SF: Failed to allocate spi_flash\n");
+	/* Allocate space if needed (not used by sf-uclass) */
+	priv = calloc(1, sizeof(*priv));
+	if (!priv) {
+		debug("SF: Failed to allocate spi_flash_priv\n");
 		return NULL;
 	}
 
+	flash = &priv->flash;
+	flash->mtd = &priv->mtd;
+
 	flash->spi = bus;
+	flash->priv = priv;
+
+	priv->mtd.priv = flash;
 
 	/* Claim spi bus */
 	ret = spi_claim_bus(bus);
@@ -49,19 +56,16 @@ struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
 	if (ret)
 		goto err_scan;
 
-#ifdef CONFIG_SPI_FLASH_MTD
-	ret = spi_flash_mtd_register(flash);
+	ret = add_mtd_device(&priv->mtd);
 	if (ret) {
 		printf("SF: failed to register mtd device: %d\n", ret);
 		goto err_mtd;
 	}
-#endif
+
 	return flash;
 
-#ifdef CONFIG_SPI_FLASH_MTD
 err_mtd:
 	spi_free_slave(bus);
-#endif
 err_scan:
 	spi_release_bus(bus);
 err_claim:
@@ -95,9 +99,7 @@ struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
 
 void spi_flash_free(struct spi_flash *flash)
 {
-#ifdef CONFIG_SPI_FLASH_MTD
-	spi_flash_mtd_unregister();
-#endif
+	del_mtd_device(flash->mtd);
 	spi_free_slave(flash->spi);
 	free(flash);
 }
Signature · 1 line
1.9.1
#10 lore raw

PATCH 5/8 sf: Add MTD support for non-dm spi_flash interface

Hello Jagan,

Am 12.10.2015 um 21:54 schrieb Jagan Teki:

Quoted 7 lines

This patch adds MTD support to non-dm spi_flash
interface code.

---
  drivers/mtd/spi/sf_probe.c | 26 ++++++++++++++------------
  1 file changed, 14 insertions(+), 12 deletions(-)

bye,
Heiko

Quoted 68 lines
drivers/mtd/spi/sf_probe.c +14 -12
index 5e314e2..60abaf2 100644
@@ -26,17 +26,24 @@ struct spi_flash_priv {
  #ifndef CONFIG_DM_SPI_FLASH
  struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
  {
+	struct spi_flash_priv *priv;
  	struct spi_flash *flash;
  	int ret;
 
-	/* Allocate space if needed (not used by sf-uclass */
-	flash = calloc(1, sizeof(*flash));
-	if (!flash) {
-		debug("SF: Failed to allocate spi_flash\n");
+	/* Allocate space if needed (not used by sf-uclass) */
+	priv = calloc(1, sizeof(*priv));
+	if (!priv) {
+		debug("SF: Failed to allocate spi_flash_priv\n");
  		return NULL;
  	}
 
+	flash = &priv->flash;
+	flash->mtd = &priv->mtd;
+
  	flash->spi = bus;
+	flash->priv = priv;
+
+	priv->mtd.priv = flash;
 
  	/* Claim spi bus */
  	ret = spi_claim_bus(bus);
@@ -49,19 +56,16 @@ struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
  	if (ret)
  		goto err_scan;
 
-#ifdef CONFIG_SPI_FLASH_MTD
-	ret = spi_flash_mtd_register(flash);
+	ret = add_mtd_device(&priv->mtd);
  	if (ret) {
  		printf("SF: failed to register mtd device: %d\n", ret);
  		goto err_mtd;
  	}
-#endif
+
  	return flash;
 
-#ifdef CONFIG_SPI_FLASH_MTD
  err_mtd:
  	spi_free_slave(bus);
-#endif
  err_scan:
  	spi_release_bus(bus);
  err_claim:
@@ -95,9 +99,7 @@ struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
 
  void spi_flash_free(struct spi_flash *flash)
  {
-#ifdef CONFIG_SPI_FLASH_MTD
-	spi_flash_mtd_unregister();
-#endif
+	del_mtd_device(flash->mtd);
  	spi_free_slave(flash->spi);
  	free(flash);
  }
Signature · 2 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
author
#11 lore raw

PATCH 6/8 sf: probe: Minor cleanup

In reply to Jagan Teki

- Use static for file-scope function
- Remove unneeded header file
- Use spi instead of slave notation for spi_slave {}

---
 drivers/mtd/spi/sf_probe.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/mtd/spi/sf_probe.c +7 -7
index 60abaf2..112ab27 100644
@@ -24,7 +24,7 @@ struct spi_flash_priv {
 };
 
 #ifndef CONFIG_DM_SPI_FLASH
-struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
+static struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
 {
 	struct spi_flash_priv *priv;
 	struct spi_flash *flash;
@@ -106,24 +106,24 @@ void spi_flash_free(struct spi_flash *flash)
 
 #else /* defined CONFIG_DM_SPI_FLASH */
 
-int spi_flash_std_probe(struct udevice *dev)
+static int spi_flash_std_probe(struct udevice *dev)
 {
 	struct spi_flash_priv *priv = dev_get_uclass_priv(dev);
-	struct spi_slave *slave = dev_get_parentdata(dev);
+	struct spi_slave *spi = dev_get_parentdata(dev);
 	struct spi_flash *flash;
 	int ret;
 
 	flash = &priv->flash;
 	flash->mtd = &priv->mtd;
 
-	flash->spi = slave;
+	flash->spi = spi;
 	flash->priv = priv;
 
 	priv->mtd.priv = flash;
 	flash->dev = dev;
 
 	/* Claim spi bus */
-	ret = spi_claim_bus(slave);
+	ret = spi_claim_bus(spi);
 	if (ret) {
 		debug("SF: Failed to claim SPI bus: %d\n", ret);
 		return ret;
@@ -144,9 +144,9 @@ int spi_flash_std_probe(struct udevice *dev)
 	return ret;
 
 err_mtd:
-	spi_free_slave(slave);
+	spi_free_slave(spi);
 err_scan:
-	spi_release_bus(slave);
+	spi_release_bus(spi);
 	return ret;
 }
 
Signature · 1 line
1.9.1
#12 lore raw

PATCH 6/8 sf: probe: Minor cleanup

Hello Jagan,

Am 12.10.2015 um 21:54 schrieb Jagan Teki:

Quoted 8 lines

- Use static for file-scope function
- Remove unneeded header file
- Use spi instead of slave notation for spi_slave {}

---
  drivers/mtd/spi/sf_probe.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)

bye,
Heiko

Quoted 56 lines
drivers/mtd/spi/sf_probe.c +7 -7
index 60abaf2..112ab27 100644
@@ -24,7 +24,7 @@ struct spi_flash_priv {
  };
 
  #ifndef CONFIG_DM_SPI_FLASH
-struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
+static struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
  {
  	struct spi_flash_priv *priv;
  	struct spi_flash *flash;
@@ -106,24 +106,24 @@ void spi_flash_free(struct spi_flash *flash)
 
  #else /* defined CONFIG_DM_SPI_FLASH */
 
-int spi_flash_std_probe(struct udevice *dev)
+static int spi_flash_std_probe(struct udevice *dev)
  {
  	struct spi_flash_priv *priv = dev_get_uclass_priv(dev);
-	struct spi_slave *slave = dev_get_parentdata(dev);
+	struct spi_slave *spi = dev_get_parentdata(dev);
  	struct spi_flash *flash;
  	int ret;
 
  	flash = &priv->flash;
  	flash->mtd = &priv->mtd;
 
-	flash->spi = slave;
+	flash->spi = spi;
  	flash->priv = priv;
 
  	priv->mtd.priv = flash;
  	flash->dev = dev;
 
  	/* Claim spi bus */
-	ret = spi_claim_bus(slave);
+	ret = spi_claim_bus(spi);
  	if (ret) {
  		debug("SF: Failed to claim SPI bus: %d\n", ret);
  		return ret;
@@ -144,9 +144,9 @@ int spi_flash_std_probe(struct udevice *dev)
  	return ret;
 
  err_mtd:
-	spi_free_slave(slave);
+	spi_free_slave(spi);
  err_scan:
-	spi_release_bus(slave);
+	spi_release_bus(spi);
  	return ret;
  }
 
Signature · 2 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
author
#13 lore raw

PATCH 7/8 sf: Drop SPI_FLASH_MTD driver

In reply to Jagan Teki

Now MTD core has been added as part of spi-flash layer, so there is no need for explicit driver for handling mtd stuff, hence removed all neccessary code regarding SPI_FLASH_MTD driver.

Cc Daniel Schwierzeck <[email protected]>
Cc Heiko Schocher <[email protected]>
---
 drivers/mtd/spi/Kconfig       |  12 -----
 drivers/mtd/spi/Makefile      |   1 -
 drivers/mtd/spi/sf_internal.h |   5 --
 drivers/mtd/spi/sf_mtd.c      | 104 ------------------------------------------
 4 files changed, 122 deletions(-)
 delete mode 100644 drivers/mtd/spi/sf_mtd.c
drivers/mtd/spi/Kconfig +0 -12
index 3f7433c..78932a4 100644
@@ -116,16 +116,4 @@ config SPI_FLASH_DATAFLASH
 
 	  If unsure, say N
 
-config SPI_FLASH_MTD
-	bool "SPI Flash MTD support"
-	depends on SPI_FLASH
-	help
-          Enable the MTD support for spi flash layer, this adapter is for
-	  translating mtd_read/mtd_write commands into spi_flash_read/write
-	  commands. It is not intended to use it within sf_cmd or the SPI
-	  flash subsystem. Such an adapter is needed for subsystems like
-	  UBI which can only operate on top of the MTD layer.
-
-	  If unsure, say N
-
 endmenu # menu "SPI Flash Support"
drivers/mtd/spi/Makefile +0 -1
index 66c4424..1824261 100644
@@ -14,6 +14,5 @@ endif
 
 obj-$(CONFIG_SPI_FLASH) += sf_probe.o sf_ops.o sf_params.o sf.o
 obj-$(CONFIG_SPI_FLASH_DATAFLASH) += sf_dataflash.o
-obj-$(CONFIG_SPI_FLASH_MTD) += sf_mtd.o
 obj-$(CONFIG_SPI_FLASH_SANDBOX) += sandbox.o
 obj-$(CONFIG_SPI_M95XXX) += eeprom_m95xxx.o
drivers/mtd/spi/sf_internal.h +0 -5
index d74bc18..84afddf 100644
@@ -191,11 +191,6 @@ int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
 		size_t cmd_len, void *data, size_t data_len);
 
-#ifdef CONFIG_SPI_FLASH_MTD
-int spi_flash_mtd_register(struct spi_flash *flash);
-void spi_flash_mtd_unregister(void);
-#endif
-
 /**
  * spi_flash_scan - scan the SPI FLASH
  * @flash:	the spi flash structure
drivers/mtd/spi/sf_mtd.c deleted +0 -104

104 lines not on this page · raw message

Signature · 1 line
1.9.1
author
#14 lore raw

PATCH 8/8 configs: Remove CONFIG_SPI_FLASH_MTD

In reply to Jagan Teki

No explict spi-flash mtd handling driver, it's been part of spi-flash layer iteself, hence removed it from board configs.

Cc Daniel Schwierzeck <[email protected]>
Cc Heiko Schocher <[email protected]>
---
 include/configs/aristainetos-common.h | 1 -
 include/configs/gw_ventana.h          | 1 -
 include/configs/socfpga_common.h      | 1 -
 3 files changed, 3 deletions(-)
include/configs/aristainetos-common.h +0 -1
index f03297e..a7d938b 100644
@@ -42,7 +42,6 @@
 #define CONFIG_PHY_MICREL
 
 #define CONFIG_CMD_SF
-#define CONFIG_SPI_FLASH_MTD
 #define CONFIG_SPI_FLASH_STMICRO
 #define CONFIG_MXC_SPI
 #define CONFIG_SF_DEFAULT_SPEED		20000000
include/configs/gw_ventana.h +0 -1
index 484d763..a63db66 100644
@@ -69,7 +69,6 @@
 #define CONFIG_CMD_SF
 #ifdef CONFIG_CMD_SF
   #define CONFIG_MXC_SPI
-  #define CONFIG_SPI_FLASH_MTD
   #define CONFIG_SPI_FLASH_BAR
   #define CONFIG_SPI_FLASH_WINBOND
   #define CONFIG_SF_DEFAULT_BUS              0
include/configs/socfpga_common.h +0 -1
index cece095..f288350 100644
@@ -196,7 +196,6 @@ unsigned int cm_get_l4_sp_clk_hz(void);
 #define CONFIG_SPI_FLASH_STMICRO	/* Micron/Numonyx flash */
 #define CONFIG_SPI_FLASH_SPANSION	/* Spansion flash */
 #ifndef CONFIG_SPL_BUILD
-#define CONFIG_SPI_FLASH_MTD
 #define CONFIG_CMD_MTDPARTS
 #define CONFIG_MTD_DEVICE
 #define CONFIG_MTD_PARTITIONS
Signature · 1 line
1.9.1
#15 lore raw

PATCH 1/8 sf: Add MTD support to spi_flash

In reply to Jagan Teki

Hello Jgan,

Am 12.10.2015 um 21:54 schrieb Jagan Teki:

Quoted 10 lines

This patch adds mtd_info support to spi_flash layer, MTD has proven core for flash operations so adding MTD to spi_flash will extends more functionality.

---
  drivers/mtd/spi/sf_ops.c   | 45 +++++++++++++++++++++++++--------------------
  drivers/mtd/spi/sf_probe.c | 26 ++++++++++++++++++--------
  include/spi_flash.h        |  9 +++++----
  3 files changed, 48 insertions(+), 32 deletions(-)

bye,
Heiko

Quoted 281 lines
drivers/mtd/spi/sf_ops.c +25 -20
index 703099f..f5ee376 100644
@@ -146,7 +146,7 @@ static int spi_flash_read_bar(struct spi_flash *flash, u8 idcode0)
  	u8 curr_bank = 0;
  	int ret;
 
-	if (flash->size <= SPI_FLASH_16MB_BOUN)
+	if (flash->mtd->size <= SPI_FLASH_16MB_BOUN)
  		goto bank_end;
 
  	switch (idcode0) {
@@ -176,8 +176,8 @@ static void spi_flash_dual(struct spi_flash *flash, u32 *addr)
  {
  	switch (flash->dual_flash) {
  	case SF_DUAL_STACKED_FLASH:
-		if (*addr >= (flash->size >> 1)) {
-			*addr -= flash->size >> 1;
+		if (*addr >= (flash->mtd->size >> 1)) {
+			*addr -= flash->mtd->size >> 1;
  			flash->spi->flags |= SPI_XFER_U_PAGE;
  		} else {
  			flash->spi->flags &= ~SPI_XFER_U_PAGE;
@@ -303,7 +303,7 @@ static int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset,
  	u8 cmd[SPI_FLASH_CMD_LEN];
  	int ret = -1;
 
-	erase_size = flash->erase_size;
+	erase_size = mtd->erasesize;
  	if (offset % erase_size || len % erase_size) {
  		debug("SF: Erase offset/length not multiple of erase size\n");
  		return -1;
@@ -693,7 +693,7 @@ int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
  		return 0;
  	}
 
-	if (flash->size != size) {
+	if (flash->mtd->size != size) {
  		debug("%s: Memory map must cover entire device\n", __func__);
  		return -1;
  	}
@@ -705,6 +705,7 @@ int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
 
  int spi_flash_scan(struct spi_flash *flash)
  {
+	struct mtd_info *mtd = flash->mtd;
  	const struct spi_flash_params *params;
  	u16 jedec, ext_jedec;
  	u8 idcode[5];
@@ -754,24 +755,27 @@ int spi_flash_scan(struct spi_flash *flash)
 
  	/* Assign spi data */
  	flash->name = params->name;
+	mtd->type = MTD_NORFLASH;
+	mtd->writesize = 1;
+	mtd->flags = MTD_CAP_NORFLASH;
  	flash->memory_map = flash->spi->memory_map;
  	flash->dual_flash = flash->spi->option;
 
  	/* Assign spi_flash ops */
-	flash->write = spi_flash_cmd_write_ops;
+	mtd->_write = spi_flash_cmd_write_ops;
  #if defined(CONFIG_SPI_FLASH_SST)
  	if (params->flags & SST_WR)
  		flash->flags |= SNOR_F_SST_WR;
 
  	if (params->flags & SNOR_F_SST_WR) {
  		if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
-			flash->write = sst_write_bp;
+			mtd->_write = sst_write_bp;
  		else
-			flash->write = sst_write_wp;
+			mtd->_write = sst_write_wp;
  	}
  #endif
-	flash->erase = spi_flash_cmd_erase_ops;
-	flash->read = spi_flash_cmd_read_ops;
+	mtd->_erase = spi_flash_cmd_erase_ops;
+	mtd->_read = spi_flash_cmd_read_ops;
 
  	/* Compute the flash size */
  	flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
@@ -790,27 +794,28 @@ int spi_flash_scan(struct spi_flash *flash)
  		flash->page_size = 256;
  	}
  	flash->page_size <<= flash->shift;
+	mtd->writebufsize = flash->page_size;
  	flash->sector_size = params->sector_size << flash->shift;
-	flash->size = flash->sector_size * params->nr_sectors << flash->shift;
+	mtd->size = flash->sector_size * params->nr_sectors << flash->shift;
  #ifdef CONFIG_SF_DUAL_FLASH
  	if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
-		flash->size <<= 1;
+		mtd->size <<= 1;
  #endif
 
  	/* Compute erase sector and command */
  	if (params->flags & SECT_4K) {
  		flash->erase_cmd = CMD_ERASE_4K;
-		flash->erase_size = 4096 << flash->shift;
+		mtd->erasesize = 4096 << flash->shift;
  	} else if (params->flags & SECT_32K) {
  		flash->erase_cmd = CMD_ERASE_32K;
-		flash->erase_size = 32768 << flash->shift;
+		mtd->erasesize = 32768 << flash->shift;
  	} else {
  		flash->erase_cmd = CMD_ERASE_64K;
-		flash->erase_size = flash->sector_size;
+		mtd->erasesize = flash->sector_size;
  	}
 
  	/* Now erase size becomes valid sector size */
-	flash->sector_size = flash->erase_size;
+	flash->sector_size = mtd->erasesize;
 
  	/* Look for the fastest read cmd */
  	cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
@@ -882,8 +887,8 @@ int spi_flash_scan(struct spi_flash *flash)
  #ifndef CONFIG_SPL_BUILD
  	printf("SF: Detected %s with page size ", flash->name);
  	print_size(flash->page_size, ", erase size ");
-	print_size(flash->erase_size, ", total ");
-	print_size(flash->size, "");
+	print_size(mtd->erasesize, ", total ");
+	print_size(mtd->size, "");
  	if (flash->memory_map)
  		printf(", mapped at %p", flash->memory_map);
  	puts("\n");
@@ -891,9 +896,9 @@ int spi_flash_scan(struct spi_flash *flash)
 
  #ifndef CONFIG_SPI_FLASH_BAR
  	if (((flash->dual_flash == SF_SINGLE_FLASH) &&
-	     (flash->size > SPI_FLASH_16MB_BOUN)) ||
+	     (mtd->size > SPI_FLASH_16MB_BOUN)) ||
  	     ((flash->dual_flash > SF_SINGLE_FLASH) &&
-	     (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
+	     (mtd->size > SPI_FLASH_16MB_BOUN << 1))) {
  		puts("SF: Warning - Only lower 16MiB accessible,");
  		puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
  	}
drivers/mtd/spi/sf_probe.c +18 -8
index 87ac33e..b8704e2 100644
@@ -14,9 +14,15 @@
  #include <malloc.h>
  #include <spi.h>
  #include <spi_flash.h>
+#include <linux/mtd/mtd.h>
 
  #include "sf_internal.h"
 
+struct spi_flash_priv {
+	struct spi_flash	flash;
+	struct mtd_info		mtd;
+};
+
  #ifndef CONFIG_DM_SPI_FLASH
  struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
  {
@@ -123,12 +129,19 @@ int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
 
  int spi_flash_std_probe(struct udevice *dev)
  {
-	struct spi_flash *flash = dev_get_uclass_priv(dev);
+	struct spi_flash_priv *priv = dev_get_uclass_priv(dev);
  	struct spi_slave *slave = dev_get_parentdata(dev);
+	struct spi_flash *flash;
  	int ret;
 
-	flash->dev = dev;
+	flash = &priv->flash;
+	flash->mtd = &priv->mtd;
+
  	flash->spi = slave;
+	flash->priv = priv;
+
+	priv->mtd.priv = flash;
+	flash->dev = dev;
 
  	/* Claim spi bus */
  	ret = spi_claim_bus(slave);
@@ -143,19 +156,16 @@ int spi_flash_std_probe(struct udevice *dev)
  		goto err_scan;
  	}
 
-#ifdef CONFIG_SPI_FLASH_MTD
-	ret = spi_flash_mtd_register(flash);
+	ret = add_mtd_device(&priv->mtd);
  	if (ret) {
  		printf("SF: failed to register mtd device: %d\n", ret);
  		goto err_mtd;
  	}
-#endif
+
  	return ret;
 
-#ifdef CONFIG_SPI_FLASH_MTD
  err_mtd:
  	spi_free_slave(slave);
-#endif
  err_scan:
  	spi_release_bus(slave);
  	return ret;
@@ -177,7 +187,7 @@ U_BOOT_DRIVER(spi_flash_std) = {
  	.id		= UCLASS_SPI_FLASH,
  	.of_match	= spi_flash_std_ids,
  	.probe		= spi_flash_std_probe,
-	.priv_auto_alloc_size = sizeof(struct spi_flash),
+	.priv_auto_alloc_size = sizeof(struct spi_flash_priv),
  	.ops		= &spi_flash_std_ops,
  };
 
include/spi_flash.h +5 -4
index 0732172..d0af8d3 100644
@@ -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
@@ -36,16 +37,15 @@ struct spi_slave;
  /**
   * struct spi_flash - SPI flash structure
   *
+ * @mtd:		point to a mtd_info structure
   * @spi:		SPI slave
   * @dev:		SPI flash device
   * @name:		Name of SPI flash
   * @dual_flash:		Indicates dual flash memories - dual stacked, parallel
   * @shift:		Flash shift useful in dual parallel
   * @flags:		Indication of spi flash flags
- * @size:		Total flash size
   * @page_size:		Write (page) size
   * @sector_size:	Sector size
- * @erase_size:		Erase size
   * @bank_read_cmd:	Bank read cmd
   * @bank_write_cmd:	Bank write cmd
   * @bank_curr:		Current flash bank
@@ -54,6 +54,7 @@ struct spi_slave;
   * @write_cmd:		Write cmd - page and quad program.
   * @dummy_byte:		Dummy cycles for read operation.
   * @memory_map:		Address of read-only SPI flash access
+ * @priv:		the private data
   * @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
@@ -63,6 +64,7 @@ struct spi_slave;
   * return 0 - Success, 1 - Failure
   */
  struct spi_flash {
+	struct mtd_info *mtd;
  	struct spi_slave *spi;
  #ifdef CONFIG_DM_SPI_FLASH
  	struct udevice *dev;
@@ -72,10 +74,8 @@ struct spi_flash {
  	u8 shift;
  	u16 flags;
 
-	u32 size;
  	u32 page_size;
  	u32 sector_size;
-	u32 erase_size;
  #ifdef CONFIG_SPI_FLASH_BAR
  	u8 bank_read_cmd;
  	u8 bank_write_cmd;
@@ -87,6 +87,7 @@ struct spi_flash {
  	u8 dummy_byte;
 
  	void *memory_map;
+	void *priv;
  	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);
Signature · 2 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany