Skip to contentSkip to conversation

sf: spi_flash_validate_params => spi_flash_scan

PATCH New Jagan Teki 1 participant 4 messages Dec 11, 2015 Memory technology devices (MTD)

lore
Share & tools
lore

Jagan Teki

author
#0 lore raw

PATCH v10 1/14 sf: spi_flash_validate_params => spi_flash_scan

Rename spi_flash_validate_params to spi_flash_scan as this code not only deals with params setup but also configure all spi_flash attributes.

And also moved all flash related code into spi_flash_scan for future functionality addition.

---
Changes on v10:
- Fixed ALIGN warning

 drivers/mtd/spi/sf_probe.c | 147 +++++++++++++++++++++++----------------------
 1 file changed, 76 insertions(+), 71 deletions(-)
drivers/mtd/spi/sf_probe.c +76 -71
index a619182..3da72df 100644
@@ -130,13 +130,42 @@ bank_end:
 }
 #endif
 
-static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
-				     struct spi_flash *flash)
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
+{
+	fdt_addr_t addr;
+	fdt_size_t size;
+	int node;
+
+	/* If there is no node, do nothing */
+	node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
+	if (node < 0)
+		return 0;
+
+	addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
+	if (addr == FDT_ADDR_T_NONE) {
+		debug("%s: Cannot decode address\n", __func__);
+		return 0;
+	}
+
+	if (flash->size != size) {
+		debug("%s: Memory map must cover entire device\n", __func__);
+		return -1;
+	}
+	flash->memory_map = map_sysmem(addr, size);
+
+	return 0;
+}
+#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
+
+static int spi_flash_scan(struct spi_slave *spi, u8 *idcode,
+			  struct spi_flash *flash)
 {
 	const struct spi_flash_params *params;
 	u8 cmd;
 	u16 jedec = idcode[1] << 8 | idcode[2];
 	u16 ext_jedec = idcode[3] << 8 | idcode[4];
+	int ret;
 
 	/* Validate params from spi_flash_params table */
 	params = spi_flash_params_table;
@@ -158,6 +187,13 @@ static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
 		return -EPROTONOSUPPORT;
 	}
 
+	/* Flash powers up read-only, so clear BP# bits */
+#if defined(CONFIG_SPI_FLASH_ATMEL) || \
+	defined(CONFIG_SPI_FLASH_MACRONIX) || \
+	defined(CONFIG_SPI_FLASH_SST)
+		spi_flash_cmd_write_status(flash, 0);
+#endif
+
 	/* Assign spi data */
 	flash->spi = spi;
 	flash->name = params->name;
@@ -253,6 +289,17 @@ static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
 		/* Go for default supported write cmd */
 		flash->write_cmd = CMD_PAGE_PROGRAM;
 
+	/* Set the quad enable bit - only for quad commands */
+	if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
+	    (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
+	    (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
+		ret = spi_flash_set_qeb(flash, idcode[0]);
+		if (ret) {
+			debug("SF: Fail to set QEB for %02x\n", idcode[0]);
+			return -EINVAL;
+		}
+	}
+
 	/* Read dummy_byte: dummy byte is determined based on the
 	 * dummy cycles of a particular command.
 	 * Fast commands - dummy_byte = dummy_cycles/8
@@ -279,48 +326,41 @@ static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
 
 	/* Configure the BAR - discover bank cmds and read current bank */
 #ifdef CONFIG_SPI_FLASH_BAR
-	int ret = spi_flash_read_bank(flash, idcode[0]);
+	ret = spi_flash_read_bank(flash, idcode[0]);
 	if (ret < 0)
 		return ret;
 #endif
 
-	/* Flash powers up read-only, so clear BP# bits */
-#if defined(CONFIG_SPI_FLASH_ATMEL) || \
-	defined(CONFIG_SPI_FLASH_MACRONIX) || \
-	defined(CONFIG_SPI_FLASH_SST)
-		spi_flash_cmd_write_status(flash, 0);
-#endif
-
-	return 0;
-}
-
 #if CONFIG_IS_ENABLED(OF_CONTROL)
-int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
-{
-	fdt_addr_t addr;
-	fdt_size_t size;
-	int node;
-
-	/* If there is no node, do nothing */
-	node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
-	if (node < 0)
-		return 0;
-
-	addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
-	if (addr == FDT_ADDR_T_NONE) {
-		debug("%s: Cannot decode address\n", __func__);
-		return 0;
+	ret = spi_flash_decode_fdt(gd->fdt_blob, flash);
+	if (ret) {
+		debug("SF: FDT decode error\n");
+		return -EINVAL;
 	}
+#endif
 
-	if (flash->size != size) {
-		debug("%s: Memory map must cover entire device\n", __func__);
-		return -1;
+#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, "");
+	if (flash->memory_map)
+		printf(", mapped at %p", flash->memory_map);
+	puts("\n");
+#endif
+
+#ifndef CONFIG_SPI_FLASH_BAR
+	if (((flash->dual_flash == SF_SINGLE_FLASH) &&
+	     (flash->size > SPI_FLASH_16MB_BOUN)) ||
+	     ((flash->dual_flash > SF_SINGLE_FLASH) &&
+	     (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
+		puts("SF: Warning - Only lower 16MiB accessible,");
+		puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
 	}
-	flash->memory_map = map_sysmem(addr, size);
+#endif
 
-	return 0;
+	return ret;
 }
-#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
 
 /**
  * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
@@ -359,47 +399,12 @@ int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash)
 	print_buffer(0, idcode, 1, sizeof(idcode), 0);
 #endif
 
-	if (spi_flash_validate_params(spi, idcode, flash)) {
+	ret = spi_flash_scan(spi, idcode, flash);
+	if (ret) {
 		ret = -EINVAL;
 		goto err_read_id;
 	}
 
-	/* Set the quad enable bit - only for quad commands */
-	if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
-	    (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
-	    (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
-		if (spi_flash_set_qeb(flash, idcode[0])) {
-			debug("SF: Fail to set QEB for %02x\n", idcode[0]);
-			ret = -EINVAL;
-			goto err_read_id;
-		}
-	}
-
-#if CONFIG_IS_ENABLED(OF_CONTROL)
-	if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
-		debug("SF: FDT decode error\n");
-		ret = -EINVAL;
-		goto err_read_id;
-	}
-#endif
-#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, "");
-	if (flash->memory_map)
-		printf(", mapped at %p", flash->memory_map);
-	puts("\n");
-#endif
-#ifndef CONFIG_SPI_FLASH_BAR
-	if (((flash->dual_flash == SF_SINGLE_FLASH) &&
-	     (flash->size > SPI_FLASH_16MB_BOUN)) ||
-	     ((flash->dual_flash > SF_SINGLE_FLASH) &&
-	     (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
-		puts("SF: Warning - Only lower 16MiB accessible,");
-		puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
-	}
-#endif
 #ifdef CONFIG_SPI_FLASH_MTD
 	ret = spi_flash_mtd_register(flash);
 #endif
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v10 1/14 sf: spi_flash_validate_params => spi_flash_scan

Quoted 225 lines · Jagan Teki

Rename spi_flash_validate_params to spi_flash_scan as this code not only deals with params setup but also configure all spi_flash attributes.

And also moved all flash related code into spi_flash_scan for future functionality addition.

---
Changes on v10:
- Fixed ALIGN warning

 drivers/mtd/spi/sf_probe.c | 147 +++++++++++++++++++++++----------------------
 1 file changed, 76 insertions(+), 71 deletions(-)
drivers/mtd/spi/sf_probe.c +76 -72
index a619182..3da72df 100644
@@ -130,13 +130,42 @@ bank_end:
 }
 #endif
 
-static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
-                                    struct spi_flash *flash)
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
+{
+       fdt_addr_t addr;
+       fdt_size_t size;
+       int node;
+
+       /* If there is no node, do nothing */
+       node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
+       if (node < 0)
+               return 0;
+
+       addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
+       if (addr == FDT_ADDR_T_NONE) {
+               debug("%s: Cannot decode address\n", __func__);
+               return 0;
+       }
+
+       if (flash->size != size) {
+               debug("%s: Memory map must cover entire device\n", __func__);
+               return -1;
+       }
+       flash->memory_map = map_sysmem(addr, size);
+
+       return 0;
+}
+#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
+
+static int spi_flash_scan(struct spi_slave *spi, u8 *idcode,
+                         struct spi_flash *flash)
 {
        const struct spi_flash_params *params;
        u8 cmd;
        u16 jedec = idcode[1] << 8 | idcode[2];
        u16 ext_jedec = idcode[3] << 8 | idcode[4];
+       int ret;
 
        /* Validate params from spi_flash_params table */
        params = spi_flash_params_table;
@@ -158,6 +187,13 @@ static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
                return -EPROTONOSUPPORT;
        }
 
+       /* Flash powers up read-only, so clear BP# bits */
+#if defined(CONFIG_SPI_FLASH_ATMEL) || \
+       defined(CONFIG_SPI_FLASH_MACRONIX) || \
+       defined(CONFIG_SPI_FLASH_SST)
+               spi_flash_cmd_write_status(flash, 0);
+#endif
+
        /* Assign spi data */
        flash->spi = spi;
        flash->name = params->name;
@@ -253,6 +289,17 @@ static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
                /* Go for default supported write cmd */
                flash->write_cmd = CMD_PAGE_PROGRAM;
 
+       /* Set the quad enable bit - only for quad commands */
+       if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
+           (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
+           (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
+               ret = spi_flash_set_qeb(flash, idcode[0]);
+               if (ret) {
+                       debug("SF: Fail to set QEB for %02x\n", idcode[0]);
+                       return -EINVAL;
+               }
+       }
+
        /* Read dummy_byte: dummy byte is determined based on the
         * dummy cycles of a particular command.
         * Fast commands - dummy_byte = dummy_cycles/8
@@ -279,48 +326,41 @@ static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
 
        /* Configure the BAR - discover bank cmds and read current bank */
 #ifdef CONFIG_SPI_FLASH_BAR
-       int ret = spi_flash_read_bank(flash, idcode[0]);
+       ret = spi_flash_read_bank(flash, idcode[0]);
        if (ret < 0)
                return ret;
 #endif
 
-       /* Flash powers up read-only, so clear BP# bits */
-#if defined(CONFIG_SPI_FLASH_ATMEL) || \
-       defined(CONFIG_SPI_FLASH_MACRONIX) || \
-       defined(CONFIG_SPI_FLASH_SST)
-               spi_flash_cmd_write_status(flash, 0);
-#endif
-
-       return 0;
-}
-
 #if CONFIG_IS_ENABLED(OF_CONTROL)
-int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
-{
-       fdt_addr_t addr;
-       fdt_size_t size;
-       int node;
-
-       /* If there is no node, do nothing */
-       node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
-       if (node < 0)
-               return 0;
-
-       addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
-       if (addr == FDT_ADDR_T_NONE) {
-               debug("%s: Cannot decode address\n", __func__);
-               return 0;
+       ret = spi_flash_decode_fdt(gd->fdt_blob, flash);
+       if (ret) {
+               debug("SF: FDT decode error\n");
+               return -EINVAL;
        }
+#endif
 
-       if (flash->size != size) {
-               debug("%s: Memory map must cover entire device\n", __func__);
-               return -1;
+#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, "");
+       if (flash->memory_map)
+               printf(", mapped at %p", flash->memory_map);
+       puts("\n");
+#endif
+
+#ifndef CONFIG_SPI_FLASH_BAR
+       if (((flash->dual_flash == SF_SINGLE_FLASH) &&
+            (flash->size > SPI_FLASH_16MB_BOUN)) ||
+            ((flash->dual_flash > SF_SINGLE_FLASH) &&
+            (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
+               puts("SF: Warning - Only lower 16MiB accessible,");
+               puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
        }
-       flash->memory_map = map_sysmem(addr, size);
+#endif
 
-       return 0;
+       return ret;
 }
-#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
 
 /**
  * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
@@ -359,47 +399,12 @@ int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash)
        print_buffer(0, idcode, 1, sizeof(idcode), 0);
 #endif
 
-       if (spi_flash_validate_params(spi, idcode, flash)) {
+       ret = spi_flash_scan(spi, idcode, flash);
+       if (ret) {
                ret = -EINVAL;
                goto err_read_id;
        }
 
-       /* Set the quad enable bit - only for quad commands */
-       if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
-           (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
-           (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
-               if (spi_flash_set_qeb(flash, idcode[0])) {
-                       debug("SF: Fail to set QEB for %02x\n", idcode[0]);
-                       ret = -EINVAL;
-                       goto err_read_id;
-               }
-       }
-
-#if CONFIG_IS_ENABLED(OF_CONTROL)
-       if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
-               debug("SF: FDT decode error\n");
-               ret = -EINVAL;
-               goto err_read_id;
-       }
-#endif
-#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, "");
-       if (flash->memory_map)
-               printf(", mapped at %p", flash->memory_map);
-       puts("\n");
-#endif
-#ifndef CONFIG_SPI_FLASH_BAR
-       if (((flash->dual_flash == SF_SINGLE_FLASH) &&
-            (flash->size > SPI_FLASH_16MB_BOUN)) ||
-            ((flash->dual_flash > SF_SINGLE_FLASH) &&
-            (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
-               puts("SF: Warning - Only lower 16MiB accessible,");
-               puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
-       }
-#endif
 #ifdef CONFIG_SPI_FLASH_MTD
        ret = spi_flash_mtd_register(flash);
 #endif
--
Quoted 2 lines

1.9.1

Applied to u-boot-spi/master

thanks!

Signature · 1 line
Jagan.
author
#2 lore raw

PATCH v10 2/14 sf: Move spi_flash_scan code to sf_ops

In reply to Jagan Teki

Intension is that sf_ops should deals all spi_flash related stuff and sf_probe (which should renamed future) should be an interface layer for spi_flash versus spi drivers.

sf_ops => spi_flash interface
sf_probe => interface layer vs spi_flash(sf_probe) to spi drivers

---
Changes for v10
- Rebase on "v10 01/14"

 drivers/mtd/spi/sf_internal.h |  14 ++
 drivers/mtd/spi/sf_ops.c      | 343 ++++++++++++++++++++++++++++++++++++++++++
 drivers/mtd/spi/sf_probe.c    | 342 -----------------------------------------
 3 files changed, 357 insertions(+), 342 deletions(-)
drivers/mtd/spi/sf_internal.h +14 -0
index 85c8a89..f3eb6f3 100644
@@ -245,4 +245,18 @@ int spi_flash_mtd_register(struct spi_flash *flash);
 void spi_flash_mtd_unregister(void);
 #endif
 
+/**
+ * spi_flash_scan - scan the SPI FLASH
+ * @spi:	the spi slave structure
+ * @idcode:	idcode of spi flash
+ * @flash:	the spi flash structure
+ *
+ * The drivers can use this fuction to scan the SPI FLASH.
+ * In the scanning, it will try to get all the necessary information to
+ * fill the spi_flash{}.
+ *
+ * Return: 0 for success, others for failure.
+ */
+int spi_flash_scan(struct spi_slave *spi, u8 *idcode, struct spi_flash *flash);
+
 #endif /* _SF_INTERNAL_H_ */
drivers/mtd/spi/sf_ops.c +343 -0

358 lines not on this page · raw message

drivers/mtd/spi/sf_probe.c +0 -342

348 lines not on this page · raw message

Signature · 1 line
1.9.1
author
#3 lore raw

PATCH v10 2/14 sf: Move spi_flash_scan code to sf_ops

Quoted 7 lines · Jagan Teki

Intension is that sf_ops should deals all spi_flash related stuff and sf_probe (which should renamed future) should be an interface layer for spi_flash versus spi drivers.

sf_ops => spi_flash interface
sf_probe => interface layer vs spi_flash(sf_probe) to spi drivers

Quoted 9 lines

---
Changes for v10
- Rebase on "v10 01/14"

Applied to u-boot-spi/master

thanks!

Signature · 1 line
Jagan.