@@ -39,7 +39,7 @@ static int m25p80_read_reg(struct spi_nor *nor, u8 cmd, u8 *val, int len)
if (nor->flags & SNOR_F_U_PAGE)
spi->flags |= SPI_XFER_U_PAGE;
- ret = spi_read_then_write(spi, &cmd, 1, NULL, val, len);
+ ret = spi_write_then_read(spi, &cmd, 1, NULL, val, len);
if (ret < 0) {
debug("m25p80: error %d reading register %x\n", ret, cmd);
return ret;
@@ -65,7 +65,7 @@ static int m25p80_write_reg(struct spi_nor *nor, u8 cmd, u8 *buf, int len)
if (nor->flags & SNOR_F_U_PAGE)
spi->flags |= SPI_XFER_U_PAGE;
- ret = spi_read_then_write(spi, &cmd, 1, buf, NULL, len);
+ ret = spi_write_then_read(spi, &cmd, 1, buf, NULL, len);
if (ret < 0) {
debug("m25p80: error %d writing register %x\n", ret, cmd);
return ret;
@@ -119,7 +119,7 @@ static int m25p80_read(struct spi_nor *nor, const u8 *cmd, size_t cmd_len,
if (nor->flags & SNOR_F_U_PAGE)
spi->flags |= SPI_XFER_U_PAGE;
- ret = spi_read_then_write(spi, cmd, cmd_len, NULL, data, data_len);
+ ret = spi_write_then_read(spi, cmd, cmd_len, NULL, data, data_len);
if (ret < 0) {
debug("m25p80: error %d reading %x\n", ret, *cmd);
return ret;
@@ -146,7 +146,7 @@ static int m25p80_write(struct spi_nor *nor, const u8 *cmd, size_t cmd_len,
if (nor->flags & SNOR_F_U_PAGE)
spi->flags |= SPI_XFER_U_PAGE;
- ret = spi_read_then_write(spi, cmd, cmd_len, data, NULL, data_len);
+ ret = spi_write_then_read(spi, cmd, cmd_len, data, NULL, data_len);
if (ret < 0) {
debug("m25p80: error %d writing %x\n", ret, *cmd);
return ret;
@@ -95,26 +95,25 @@ 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)
+int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
+ size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
+ size_t n_buf)
{
unsigned long flags = SPI_XFER_BEGIN;
int ret;
- if (data_len == 0)
+ if (n_buf == 0)
flags |= SPI_XFER_END;
- ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
+ ret = spi_xfer(slave, n_opcode * 8, opcode, 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);
+ n_opcode, ret);
+ } else if (n_buf != 0) {
+ ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
if (ret)
debug("spi: failed to transfer %zu bytes of data: %d\n",
- data_len, ret);
+ n_buf, ret);
}
return ret;
@@ -39,26 +39,25 @@ void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
return ptr;
}
-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)
+int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
+ size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
+ size_t n_buf)
{
unsigned long flags = SPI_XFER_BEGIN;
int ret;
- if (data_len == 0)
+ if (n_buf == 0)
flags |= SPI_XFER_END;
- ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
+ ret = spi_xfer(slave, n_opcode * 8, opcode, 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);
+ n_opcode, ret);
+ } else if (n_buf != 0) {
+ ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
if (ret)
debug("spi: failed to transfer %zu bytes of data: %d\n",
- data_len, ret);
+ n_buf, ret);
}
return ret;
@@ -265,10 +265,25 @@ 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);
+/**
+ * spi_write_then_read - SPI synchronous write followed by read
+ *
+ * This performs a half duplex transaction in which the first transaction
+ * is to send the opcode and if the length of buf is non-zero then it start
+ * the second transaction as tx or rx based on the need from respective slave.
+ *
+ * @slave: slave device with which opcode/data will be exchanged
+ * @opcode: opcode used for specific transfer
+ * @n_opcode: size of opcode, in bytes
+ * @txbuf: buffer into which data to be written
+ * @rxbuf: buffer into which data will be read
+ * @n_buf: size of buf (whether it's [tx|rx]buf), in bytes
+ *
+ * Returns: 0 on success, not 0 on failure
+ */
+int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
+ size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
+ size_t n_buf);
/* Copy memory mapped data */
void spi_flash_copy_mmap(void *data, void *offset, size_t len);
Since spi_read_then_write moved into spi layer, the meaning of data transfer is also change from read_then_write to write_then_read, this means first spi will write the opcode through and then read the respective buffer. Cc: Simon Glass <sjg@chromium.org> Cc: Bin Meng <bmeng.cn@gmail.com> Cc: Mugunthan V N <mugunthanvnm@ti.com> Cc: Michal Simek <michal.simek@xilinx.com> Cc: Siva Durga Prasad Paladugu <sivadur@xilinx.com> Signed-off-by: Jagan Teki <jteki@openedev.com> --- drivers/mtd/spi-nor/m25p80.c | 8 ++++---- drivers/spi/spi-uclass.c | 19 +++++++++---------- drivers/spi/spi.c | 19 +++++++++---------- include/spi.h | 23 +++++++++++++++++++---- 4 files changed, 41 insertions(+), 28 deletions(-)