[U-Boot] [PATCH v7 18/87] mtd: spi-nor: Move spi_read_then_write to spi layer

Jagan Teki jteki at openedev.com
Tue Mar 22 08:37:30 CET 2016


Since spi_read_then_write is doing spi operations like
setting up commands, tx and rx through spi_xfer, So
it is meanfull to have this definition at spi layer and
flash layer should use this whenever required.

Cc: Simon Glass <sjg at chromium.org>
Cc: Bin Meng <bmeng.cn at gmail.com>
Cc: Mugunthan V N <mugunthanvnm at ti.com>
Cc: Michal Simek <michal.simek at xilinx.com>
Cc: Siva Durga Prasad Paladugu <sivadur at xilinx.com>
Signed-off-by: Jagan Teki <jteki at openedev.com>
---
 drivers/mtd/spi-nor/m25p80.c | 25 -------------------------
 drivers/spi/spi-uclass.c     | 25 +++++++++++++++++++++++++
 drivers/spi/spi.c            | 31 +++++++++++++++++++++++++++++++
 include/spi.h                |  5 +++++
 4 files changed, 61 insertions(+), 25 deletions(-)

diff --git a/drivers/mtd/spi-nor/m25p80.c b/drivers/mtd/spi-nor/m25p80.c
index 79293b2..6033b48 100644
--- a/drivers/mtd/spi-nor/m25p80.c
+++ b/drivers/mtd/spi-nor/m25p80.c
@@ -25,31 +25,6 @@ struct m25p {
 #endif
 };
 
-static int spi_read_then_write(struct spi_slave *spi, const u8 *cmd,
-			       size_t cmd_len, const u8 *data_out,
-			       u8 *data_in, size_t data_len)
-{
-	unsigned long flags = SPI_XFER_BEGIN;
-	int ret;
-
-	if (data_len == 0)
-		flags |= SPI_XFER_END;
-
-	ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
-	if (ret) {
-		debug("SF: Failed to send command (%zu bytes): %d\n",
-		      cmd_len, ret);
-	} else if (data_len != 0) {
-		ret = spi_xfer(spi, data_len * 8, data_out, data_in,
-					SPI_XFER_END);
-		if (ret)
-			debug("SF: Failed to transfer %zu bytes of data: %d\n",
-			      data_len, ret);
-	}
-
-	return ret;
-}
-
 static int m25p80_read_reg(struct spi_nor *nor, u8 cmd, u8 *val, int len)
 {
 	struct m25p *flash = nor->priv;
diff --git a/drivers/spi/spi-uclass.c b/drivers/spi/spi-uclass.c
index 5561f36..2cf2a52 100644
--- a/drivers/spi/spi-uclass.c
+++ b/drivers/spi/spi-uclass.c
@@ -95,6 +95,31 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
 	return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
 }
 
+int spi_read_then_write(struct spi_slave *spi, const u8 *cmd,
+			size_t cmd_len, const u8 *data_out,
+			u8 *data_in, size_t data_len)
+{
+	unsigned long flags = SPI_XFER_BEGIN;
+	int ret;
+
+	if (data_len == 0)
+		flags |= SPI_XFER_END;
+
+	ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
+	if (ret) {
+		debug("spi: failed to send command (%zu bytes): %d\n",
+		      cmd_len, ret);
+	} else if (data_len != 0) {
+		ret = spi_xfer(spi, data_len * 8, data_out, data_in,
+			       SPI_XFER_END);
+		if (ret)
+			debug("spi: failed to transfer %zu bytes of data: %d\n",
+			      data_len, ret);
+	}
+
+	return ret;
+}
+
 static int spi_post_bind(struct udevice *dev)
 {
 	/* Scan the bus for devices */
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 7d81fbd..aceaf9b 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -39,6 +39,37 @@ void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
 	return ptr;
 }
 
+int __weak spi_xfer(struct spi_slave *slave, unsigned int bitlen,
+		    const void *dout, void *din, unsigned long flags)
+{
+	return 0;
+}
+
+int spi_read_then_write(struct spi_slave *spi, const u8 *cmd,
+			size_t cmd_len, const u8 *data_out,
+			u8 *data_in, size_t data_len)
+{
+	unsigned long flags = SPI_XFER_BEGIN;
+	int ret;
+
+	if (data_len == 0)
+		flags |= SPI_XFER_END;
+
+	ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
+	if (ret) {
+		debug("spi: failed to send command (%zu bytes): %d\n",
+		      cmd_len, ret);
+	} else if (data_len != 0) {
+		ret = spi_xfer(spi, data_len * 8, data_out, data_in,
+					SPI_XFER_END);
+		if (ret)
+			debug("spi: failed to transfer %zu bytes of data: %d\n",
+			      data_len, ret);
+	}
+
+	return ret;
+}
+
 #ifdef CONFIG_OF_SPI
 struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
 					   int node)
diff --git a/include/spi.h b/include/spi.h
index 4b88d39..139292c 100644
--- a/include/spi.h
+++ b/include/spi.h
@@ -265,6 +265,11 @@ int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
 int  spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 		void *din, unsigned long flags);
 
+/* spi_write_then_read - SPI synchronous read followed by write */
+int spi_read_then_write(struct spi_slave *spi, const u8 *cmd,
+			size_t cmd_len, const u8 *data_out,
+			u8 *data_in, size_t data_len);
+
 /* Copy memory mapped data */
 void spi_flash_copy_mmap(void *data, void *offset, size_t len);
 
-- 
1.9.1



More information about the U-Boot mailing list