public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Vignesh R <vigneshr@ti.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v3 06/20] spi: Add non DM version of SPI_MEM
Date: Tue, 29 Jan 2019 11:19:53 +0530	[thread overview]
Message-ID: <20190129055007.17376-7-vigneshr@ti.com> (raw)
In-Reply-To: <20190129055007.17376-1-vigneshr@ti.com>

Add non DM version of SPI_MEM to support easy migration to new SPI NOR
framework. This can be removed once DM_SPI conversion is complete.

Signed-off-by: Vignesh R <vigneshr@ti.com>
Tested-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
Tested-by: Stefan Roese <sr@denx.de>
Tested-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 drivers/spi/Kconfig        |   4 +-
 drivers/spi/Makefile       |   1 +
 drivers/spi/spi-mem-nodm.c | 105 +++++++++++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+), 2 deletions(-)
 create mode 100644 drivers/spi/spi-mem-nodm.c

diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index de4d62dd8fd1..df4c1a447842 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -16,8 +16,6 @@ config DM_SPI
 	  typically use driver-private data instead of extending the
 	  spi_slave structure.
 
-if DM_SPI
-
 config SPI_MEM
 	bool "SPI memory extension"
 	help
@@ -25,6 +23,8 @@ config SPI_MEM
 	  This extension is meant to simplify interaction with SPI memories
 	  by providing an high-level interface to send memory-like commands.
 
+if DM_SPI
+
 config ALTERA_SPI
 	bool "Altera SPI driver"
 	help
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index 4acec3ea17d7..39026712931b 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -11,6 +11,7 @@ obj-$(CONFIG_SOFT_SPI) += soft_spi.o
 obj-$(CONFIG_SPI_MEM) += spi-mem.o
 else
 obj-y += spi.o
+obj-$(CONFIG_SPI_MEM) += spi-mem-nodm.o
 obj-$(CONFIG_SOFT_SPI) += soft_spi_legacy.o
 endif
 
diff --git a/drivers/spi/spi-mem-nodm.c b/drivers/spi/spi-mem-nodm.c
new file mode 100644
index 000000000000..4447d4499135
--- /dev/null
+++ b/drivers/spi/spi-mem-nodm.c
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
+ */
+
+#include <spi.h>
+#include <spi-mem.h>
+
+int spi_mem_exec_op(struct spi_slave *slave,
+		    const struct spi_mem_op *op)
+{
+	unsigned int pos = 0;
+	const u8 *tx_buf = NULL;
+	u8 *rx_buf = NULL;
+	u8 *op_buf;
+	int op_len;
+	u32 flag;
+	int ret;
+	int i;
+
+	if (op->data.nbytes) {
+		if (op->data.dir == SPI_MEM_DATA_IN)
+			rx_buf = op->data.buf.in;
+		else
+			tx_buf = op->data.buf.out;
+	}
+
+	op_len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
+	op_buf = calloc(1, op_len);
+
+	ret = spi_claim_bus(slave);
+	if (ret < 0)
+		return ret;
+
+	op_buf[pos++] = op->cmd.opcode;
+
+	if (op->addr.nbytes) {
+		for (i = 0; i < op->addr.nbytes; i++)
+			op_buf[pos + i] = op->addr.val >>
+				(8 * (op->addr.nbytes - i - 1));
+
+		pos += op->addr.nbytes;
+	}
+
+	if (op->dummy.nbytes)
+		memset(op_buf + pos, 0xff, op->dummy.nbytes);
+
+	/* 1st transfer: opcode + address + dummy cycles */
+	flag = SPI_XFER_BEGIN;
+	/* Make sure to set END bit if no tx or rx data messages follow */
+	if (!tx_buf && !rx_buf)
+		flag |= SPI_XFER_END;
+
+	ret = spi_xfer(slave, op_len * 8, op_buf, NULL, flag);
+	if (ret)
+		return ret;
+
+	/* 2nd transfer: rx or tx data path */
+	if (tx_buf || rx_buf) {
+		ret = spi_xfer(slave, op->data.nbytes * 8, tx_buf,
+			       rx_buf, SPI_XFER_END);
+		if (ret)
+			return ret;
+	}
+
+	spi_release_bus(slave);
+
+	for (i = 0; i < pos; i++)
+		debug("%02x ", op_buf[i]);
+	debug("| [%dB %s] ",
+	      tx_buf || rx_buf ? op->data.nbytes : 0,
+	      tx_buf || rx_buf ? (tx_buf ? "out" : "in") : "-");
+	for (i = 0; i < op->data.nbytes; i++)
+		debug("%02x ", tx_buf ? tx_buf[i] : rx_buf[i]);
+	debug("[ret %d]\n", ret);
+
+	free(op_buf);
+
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+int spi_mem_adjust_op_size(struct spi_slave *slave,
+			   struct spi_mem_op *op)
+{
+	unsigned int len;
+
+	len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
+	if (slave->max_write_size && len > slave->max_write_size)
+		return -EINVAL;
+
+	if (op->data.dir == SPI_MEM_DATA_IN && slave->max_read_size)
+		op->data.nbytes = min(op->data.nbytes,
+				      slave->max_read_size);
+	else if (slave->max_write_size)
+		op->data.nbytes = min(op->data.nbytes,
+				      slave->max_write_size - len);
+
+	if (!op->data.nbytes)
+		return -EINVAL;
+
+	return 0;
+}
-- 
2.20.1

  parent reply	other threads:[~2019-01-29  5:49 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-29  5:49 [U-Boot] [PATCH v3 00/20] SF: Migrate to Linux SPI NOR framework Vignesh R
2019-01-29  5:49 ` [U-Boot] [PATCH v3 01/20] configs: Move CONFIG_SPI_FLASH into defconfigs Vignesh R
2019-01-29  5:49 ` [U-Boot] [PATCH v3 02/20] bitops: Fix GENMASK definition for Sandbox Vignesh R
2019-01-31  0:41   ` Simon Glass
2019-01-31 13:34     ` Vignesh R
2019-01-29  5:49 ` [U-Boot] [PATCH v3 03/20] spi: spi-mem: Allow use of spi_mem_exec_op for all SPI modes Vignesh R
2019-01-29  5:49 ` [U-Boot] [PATCH v3 04/20] spi: spi-mem: Extend spi_mem_adjust_op_size() to honor max xfer size Vignesh R
2019-01-29  5:49 ` [U-Boot] [PATCH v3 05/20] spi: spi-mem: Claim SPI bus before spi mem access Vignesh R
2019-01-29  5:49 ` Vignesh R [this message]
2019-01-29  5:49 ` [U-Boot] [PATCH v3 07/20] sh: bitops: add hweight*() macros Vignesh R
2019-01-29  5:49 ` [U-Boot] [PATCH v3 08/20] mtd: spi: Port SPI NOR framework from Linux Vignesh R
2019-01-29  5:49 ` [U-Boot] [PATCH v3 09/20] mtd: spi: spi-nor-core: Add SPI MEM support Vignesh R
2019-01-29  5:49 ` [U-Boot] [PATCH v3 10/20] mtd: spi: spi-nor-core: Add 4 Byte addressing support Vignesh R
2019-01-29  5:49 ` [U-Boot] [PATCH v3 11/20] mtd: spi: spi-nor-core: Add SFDP support Vignesh R
2019-01-29  5:49 ` [U-Boot] [PATCH v3 12/20] mtd: spi: spi-nor-core: Add back U-Boot specific features Vignesh R
2019-01-29  5:50 ` [U-Boot] [PATCH v3 13/20] mtd: spi: sf_probe: Add "jedec, spi-nor" compatible string Vignesh R
2019-01-29  5:50 ` [U-Boot] [PATCH v3 14/20] mtd: spi: Switch to new SPI NOR framework Vignesh R
2019-01-31 12:23   ` Jagan Teki
2019-01-31 12:39     ` Vignesh R
2019-01-31 12:39       ` Jagan Teki
2019-01-31 12:55         ` Vignesh R
2019-01-31 12:57           ` Jagan Teki
2019-01-29  5:50 ` [U-Boot] [PATCH v3 15/20] mtd: spi: Remove unused files Vignesh R
2019-01-29  5:50 ` [U-Boot] [PATCH v3 16/20] mtd: spi: Add lightweight SPI flash stack for SPL Vignesh R
2019-01-31 12:06   ` Jagan Teki
2019-01-31 17:51     ` Vignesh R
2019-02-01 15:48       ` Jagan Teki
2019-02-01 17:04         ` Vignesh R
2019-02-02 13:12           ` Jagan Teki
2019-01-29  5:50 ` [U-Boot] [PATCH v3 17/20] spl: Kconfig: Enable SPI_FLASH_TINY by default " Vignesh R
2019-01-29  5:50 ` [U-Boot] [PATCH v3 18/20] configs: Remove SF_DUAL_FLASH Vignesh R
2019-01-29  5:50 ` [U-Boot] [PATCH v3 19/20] configs: Don't use SPI_FLASH_BAR as default Vignesh R
2019-01-31 13:18   ` Jagan Teki
2019-01-31 13:33     ` Vignesh R
2019-01-31 13:36       ` Jagan Teki
2019-01-31 13:47         ` Vignesh R
2019-01-31 13:50           ` Jagan Teki
2019-01-31 17:35             ` Vignesh R
2019-02-01 15:54               ` Jagan Teki
2019-02-01 17:08                 ` Vignesh R
2019-02-02 13:10                   ` Jagan Teki
2019-01-29  5:50 ` [U-Boot] [PATCH v3 20/20] MAINTAINERS: Add an entry for SPI NOR Vignesh R
2019-01-29  8:15 ` [U-Boot] [PATCH v3 00/20] SF: Migrate to Linux SPI NOR framework Simon Goldschmidt
2019-01-29 11:56   ` Vignesh R
2019-01-29 11:58     ` Simon Goldschmidt
2019-01-29 12:25 ` Tom Rini
2019-01-31 14:40 ` Jagan Teki
2019-01-31 14:42   ` Tom Rini
2019-01-31 14:45     ` Jagan Teki
2019-02-01  8:27       ` Vignesh R
2019-02-04 19:03         ` Jagan Teki
2019-01-31 14:48     ` Simon Goldschmidt
2019-01-31 14:51       ` Jagan Teki
2019-01-31 14:54         ` Tom Rini
2019-01-31 14:58           ` Simon Goldschmidt
2019-01-31 15:02             ` Tom Rini
2019-01-31 15:08               ` Simon Goldschmidt
2019-01-31 15:00           ` Jagan Teki

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=20190129055007.17376-7-vigneshr@ti.com \
    --to=vigneshr@ti.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox