All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyrille Pitchen <cyrille.pitchen@atmel.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 08/18] sf: share read generic algorithm
Date: Tue, 15 Mar 2016 19:12:30 +0100	[thread overview]
Message-ID: <4c8dc93194d30d39127537d3ed57a22c71f24a34.1458063638.git.cyrille.pitchen@atmel.com> (raw)
In-Reply-To: <cover.1458063638.git.cyrille.pitchen@atmel.com>

This patch splits the generic algorithm to read data from the
actual implementation which is specific to each UCLASS_SPI_FLASH driver.

For now, the sf_probe.c driver is the only instance of this driver class
using this generic algorithm but other driver instances are to come.
This patch will ease their implementation.

Please note that the sf_dataflash.c driver has its own implementation of
the .read hook (of the struct dm_spi_flash_ops) which is not compatible
with this generic algorithm. This is why we can't simply change the
prototype of .read hook and create a spi_flash_read() wrapper.

Signed-off-by: Cyrille Pitchen <cyrille.pitchen@atmel.com>
---
 drivers/mtd/spi/sf_internal.h |  4 ++++
 drivers/mtd/spi/spi_flash.c   | 48 ++++++++++++++++++++++++++++---------------
 2 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/drivers/mtd/spi/sf_internal.h b/drivers/mtd/spi/sf_internal.h
index 592c23cd4ce1..569863a31684 100644
--- a/drivers/mtd/spi/sf_internal.h
+++ b/drivers/mtd/spi/sf_internal.h
@@ -223,6 +223,10 @@ int spi_flash_write_alg(struct spi_flash *flash, u32 offset, size_t len,
 int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
 		size_t len, void *data);
 
+typedef int (*spi_flash_read_fn)(struct spi_flash *, u32, size_t, void *);
+int spi_flash_read_alg(struct spi_flash *flash, u32 offset, size_t len,
+		void *data, spi_flash_read_fn read_fn);
+
 #ifdef CONFIG_SPI_FLASH_MTD
 int spi_flash_mtd_register(struct spi_flash *flash);
 void spi_flash_mtd_unregister(void);
diff --git a/drivers/mtd/spi/spi_flash.c b/drivers/mtd/spi/spi_flash.c
index 3c8224c2b084..4be99ea0b424 100644
--- a/drivers/mtd/spi/spi_flash.c
+++ b/drivers/mtd/spi/spi_flash.c
@@ -473,11 +473,10 @@ void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len)
 	memcpy(data, offset, len);
 }
 
-int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
-		size_t len, void *data)
+int spi_flash_read_alg(struct spi_flash *flash, u32 offset, size_t len,
+		       void *data, spi_flash_read_fn read_fn)
 {
 	struct spi_slave *spi = flash->spi;
-	u8 *cmd, cmdsz;
 	u32 remain_len, read_len, read_addr;
 	int bank_sel = 0;
 	int ret = -1;
@@ -496,15 +495,6 @@ int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
 		goto release;
 	}
 
-	cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
-	cmd = calloc(1, cmdsz);
-	if (!cmd) {
-		debug("SF: Failed to allocate cmd\n");
-		ret = -ENOMEM;
-		goto release;
-	}
-
-	cmd[0] = flash->read_cmd;
 	while (len) {
 		read_addr = offset;
 
@@ -525,9 +515,7 @@ int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
 		else
 			read_len = remain_len;
 
-		spi_flash_addr(read_addr, cmd);
-
-		ret = spi_flash_cmd_read(spi, cmd, cmdsz, data, read_len);
+		ret = read_fn(flash, read_addr, read_len, data);
 		if (ret < 0) {
 			debug("SF: read failed\n");
 			break;
@@ -538,14 +526,40 @@ int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
 		data += read_len;
 	}
 
-	free(cmd);
-
 release:
 	spi_release_bus(spi);
 
 	return ret;
 }
 
+static int spi_flash_read_impl(struct spi_flash *flash, u32 offset,
+			       size_t len, void *buf)
+{
+	struct spi_slave *spi = flash->spi;
+	u8 *cmd;
+	size_t cmdsz;
+	int ret;
+
+	cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
+	cmd = calloc(1, cmdsz);
+	if (!cmd) {
+		debug("SF: Failed to allocate cmd\n");
+		return -ENOMEM;
+	}
+
+	cmd[0] = flash->read_cmd;
+	spi_flash_addr(offset, cmd);
+	ret = spi_flash_cmd_read(spi, cmd, cmdsz, buf, len);
+	free(cmd);
+	return ret;
+}
+
+int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
+		size_t len, void *buf)
+{
+	return spi_flash_read_alg(flash, offset, len, buf, spi_flash_read_impl);
+}
+
 #ifdef CONFIG_SPI_FLASH_SST
 static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
 {
-- 
1.8.2.2

  parent reply	other threads:[~2016-03-15 18:12 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-15 18:12 [U-Boot] [PATCH 00/18] sf: fix support of QSPI memories and controllers Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 01/18] Revert "sf: Fix quad bit set for micron devices" Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 02/18] sf: call spi_claim_bus() and spi_release_bus() only once per read, write or erase Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 03/18] sf: replace spi_flash_read_common() calls by spi_flash_cmd_read() Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 04/18] sf: remove spi_flash_write_common() Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 05/18] sf: export spi_flash_wait_ready() function Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 06/18] sf: share erase generic algorithm Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 07/18] sf: share write " Cyrille Pitchen
2016-03-15 18:12 ` Cyrille Pitchen [this message]
2016-03-15 18:12 ` [U-Boot] [PATCH 09/18] sf: add hooks to handle register read and write operations Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 10/18] sf: move support of SST flash into generic spi_flash_write_alg() Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 11/18] sf: fix selection of supported READ commands for QSPI memories Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 12/18] sf: fix detection of QSPI memories when they boot in Quad or Dual mode Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 13/18] sf: add helper function to set the number of dummy bytes Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 14/18] sf: add 4byte address opcodes Cyrille Pitchen
2016-03-21  8:58   ` [U-Boot] [PATCH 14/18 v2] " Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 15/18] sf: prepare next fixes to support of QSPI memories by manufacturer Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 16/18] sf: fix support of Micron memories Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 17/18] ARM: at91: clock: add function to get QSPI clocks Cyrille Pitchen
2016-03-15 18:12 ` [U-Boot] [PATCH 18/18] sf: add driver for Atmel QSPI controller Cyrille Pitchen
2016-03-15 18:21 ` [U-Boot] [PATCH 00/18] sf: fix support of QSPI memories and controllers Jagan Teki
2016-03-15 19:17   ` Marek Vasut
2016-03-16 13:30   ` Cyrille Pitchen
2016-03-16 14:14     ` Jagan Teki
2016-03-16 16:23       ` Albert ARIBAUD
2016-03-16 16:34         ` Jagan Teki
2016-03-17  7:30           ` Albert ARIBAUD
2016-03-16 16:30       ` Cyrille Pitchen
2016-03-16 16:41         ` [U-Boot] [PATCH 1/4] rework board config files Cyrille Pitchen
2016-03-16 16:41           ` [U-Boot] [PATCH 2/4] sama5d2_xplained: add support of QSPI controllers Cyrille Pitchen
2016-03-16 16:41           ` [U-Boot] [PATCH 3/4] dts: add dts file for Atmel sama5d2 xplained board Cyrille Pitchen
2016-03-16 16:41           ` [U-Boot] [PATCH 4/4] sf: fix sf probe Cyrille Pitchen
2016-03-18 13:48       ` [U-Boot] [PATCH 00/18] sf: fix support of QSPI memories and controllers Stefan Roese
2016-03-18 15:18         ` Cyrille Pitchen
2016-03-21  9:07           ` Jagan Teki
2016-03-21 13:13             ` Cyrille Pitchen

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=4c8dc93194d30d39127537d3ed57a22c71f24a34.1458063638.git.cyrille.pitchen@atmel.com \
    --to=cyrille.pitchen@atmel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.