From: Sean Anderson <seanga2@gmail.com>
To: u-boot@lists.denx.de
Subject: [PATCH v1 6/8] spi: dw: Add mem_ops
Date: Thu, 5 Mar 2020 14:19:23 -0500 [thread overview]
Message-ID: <20200305191925.959494-7-seanga2@gmail.com> (raw)
In-Reply-To: <20200305191925.959494-1-seanga2@gmail.com>
The designware ssi device has "broken" chip select behaviour [1], and needs
specific manipulation to use the built-in chip select. The existing fix is
to use an external GPIO for chip select, but typically the K210 has SPI3
directly connected to a flash chip with dedicated pins. This makes it
impossible to use the spi_xfer function to use spi, since the CS is
de-asserted in between calls. This patch adds an implementation of
exec_op, which gives correct behaviour when reading/writing spi flash.
Work on this device has been difficult because the only example code I have
to work off is Kendryte's sdk, and I do not have access to the datasheet
(if anyone does, I would love to have a look!). The MMC device is still not
working, but I have been making progress.
[1] https://lkml.org/lkml/2015/12/23/132
Signed-off-by: Sean Anderson <seanga2@gmail.com>
---
drivers/spi/designware_spi.c | 128 ++++++++++++++++++++++++++++++++++-
1 file changed, 126 insertions(+), 2 deletions(-)
diff --git a/drivers/spi/designware_spi.c b/drivers/spi/designware_spi.c
index 613eb0d0e6..298d1dbab5 100644
--- a/drivers/spi/designware_spi.c
+++ b/drivers/spi/designware_spi.c
@@ -17,6 +17,7 @@
#include <errno.h>
#include <malloc.h>
#include <spi.h>
+#include <spi-mem.h>
#include <fdtdec.h>
#include <reset.h>
#include <dm/device_compat.h>
@@ -108,8 +109,8 @@ struct dw_spi_priv {
int len;
u32 fifo_len; /* depth of the FIFO buffer */
- void *tx;
- void *tx_end;
+ const void *tx;
+ const void *tx_end;
void *rx;
void *rx_end;
@@ -471,6 +472,124 @@ static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
return ret;
}
+/*
+ * This function is necessary for reading SPI flash with the native CS
+ * c.f. https://lkml.org/lkml/2015/12/23/132
+ */
+static int dw_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
+{
+ bool read = op->data.dir == SPI_MEM_DATA_IN;
+ int pos, i, ret = 0;
+ struct udevice *bus = slave->dev->parent;
+ struct dw_spi_platdata *plat = dev_get_platdata(bus);
+ struct dw_spi_priv *priv = dev_get_priv(bus);
+ u8 op_len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
+ u8 op_buf[op_len];
+ u32 cr0;
+
+ if (read)
+ priv->tmode = SPI_TMOD_EPROMREAD;
+ else
+ priv->tmode = SPI_TMOD_TO;
+
+ debug("%s: buf=%p len=%u [bytes]\n",
+ __func__, op->data.buf.in, op->data.nbytes);
+
+ cr0 = GEN_CTRL0(priv, plat);
+ debug("%s: cr0=%08x\n", __func__, cr0);
+
+ spi_enable_chip(priv, 0);
+ dw_write(priv, DW_SPI_CTRL0, cr0);
+ if (read)
+ dw_write(priv, DW_SPI_CTRL1, op->data.nbytes - 1);
+ spi_enable_chip(priv, 1);
+
+ /* From spi_mem_exec_op */
+ pos = 0;
+ 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);
+
+ priv->tx = &op_buf;
+ priv->tx_end = priv->tx + op_len;
+ priv->rx = NULL;
+ priv->rx_end = NULL;
+ while (priv->tx != priv->tx_end) {
+ dw_writer(priv);
+ /* This loop needs a delay otherwise we can hang */
+ udelay(1);
+ }
+
+ /*
+ * XXX: The following are tight loops! Enabling debug messages may cause
+ * them to fail because we are not reading/writing the fifo fast enough.
+ *
+ * We heuristically break out of the loop when we stop getting data.
+ * This is to stop us from hanging if the device doesn't send any data
+ * (either at all, or after sending a response). For example, one flash
+ * chip I tested did not send anything back after the first 64K of data.
+ */
+ if (read) {
+ /* If we have gotten any data back yet */
+ bool got_data = false;
+ /* How many times we have looped without reading anything */
+ int loops_since_read = 0;
+ struct spi_mem_op *mut_op = (struct spi_mem_op *)op;
+
+ priv->rx = op->data.buf.in;
+ priv->rx_end = priv->rx + op->data.nbytes;
+
+ dw_write(priv, DW_SPI_SER, 1 << spi_chip_select(slave->dev));
+ while (priv->rx != priv->rx_end) {
+ void *last_rx = priv->rx;
+
+ dw_reader(priv);
+ if (priv->rx == last_rx) {
+ loops_since_read++;
+ /* Thresholds are arbitrary */
+ if (loops_since_read > 256)
+ break;
+ else if (got_data && loops_since_read > 32)
+ break;
+ } else {
+ got_data = true;
+ loops_since_read = 0;
+ }
+ }
+
+ /* Update with the actual amount of data read */
+ mut_op->data.nbytes -= priv->rx_end - priv->rx;
+ } else {
+ u32 val;
+
+ priv->tx = op->data.buf.out;
+ priv->tx_end = priv->tx + op->data.nbytes;
+
+ /* Fill up the write fifo before starting the transfer */
+ dw_writer(priv);
+ dw_write(priv, DW_SPI_SER, 1 << spi_chip_select(slave->dev));
+ while (priv->tx != priv->tx_end)
+ dw_writer(priv);
+
+ if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
+ (val & SR_TF_EMPT) && !(val & SR_BUSY),
+ RX_TIMEOUT * 1000)) {
+ ret = -ETIMEDOUT;
+ }
+ }
+
+ dw_write(priv, DW_SPI_SER, 0);
+ debug("%s: %u bytes xfered\n", __func__, op->data.nbytes);
+ return ret;
+}
+
static int dw_spi_set_speed(struct udevice *bus, uint speed)
{
struct dw_spi_platdata *plat = dev_get_platdata(bus);
@@ -534,8 +653,13 @@ static int dw_spi_remove(struct udevice *bus)
return 0;
}
+static const struct spi_controller_mem_ops dw_spi_mem_ops = {
+ .exec_op = dw_spi_exec_op,
+};
+
static const struct dm_spi_ops dw_spi_ops = {
.xfer = dw_spi_xfer,
+ .mem_ops = &dw_spi_mem_ops,
.set_speed = dw_spi_set_speed,
.set_mode = dw_spi_set_mode,
/*
--
2.25.0
next prev parent reply other threads:[~2020-03-05 19:19 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-05 19:19 [PATCH v1 0/8] riscv: Add SPI support for Kendryte K210 Sean Anderson
2020-03-05 19:19 ` [PATCH v1 1/8] doc: Fix typo in FIT documentation Sean Anderson
2020-03-22 1:52 ` Marek Vasut
2020-03-23 13:58 ` Tom Rini
2020-03-05 19:19 ` [PATCH v1 2/8] spi: dw: Add device tree properties for fields in CTRL0 Sean Anderson
2020-03-22 1:51 ` Marek Vasut
2020-03-22 2:36 ` Sean Anderson
2020-03-22 3:04 ` Marek Vasut
2020-03-22 3:08 ` Sean Anderson
2020-03-22 3:13 ` Marek Vasut
2020-03-22 3:33 ` Sean Anderson
2020-03-22 4:22 ` Marek Vasut
2020-03-05 19:19 ` [PATCH v1 3/8] spi: dw: Rename "cs-gpio" to "cs-gpios" Sean Anderson
2020-04-02 12:17 ` Jagan Teki
2020-04-02 12:33 ` Sean Anderson
2020-03-05 19:19 ` [PATCH v1 4/8] spi: dw: Use generic function to read reg address Sean Anderson
2020-04-02 12:24 ` Jagan Teki
2020-04-02 12:32 ` Sean Anderson
2020-03-05 19:19 ` [PATCH v1 5/8] spi: dw: Speed up transfer loops Sean Anderson
2020-03-22 1:49 ` Marek Vasut
2020-03-22 3:54 ` Sean Anderson
2020-03-05 19:19 ` Sean Anderson [this message]
2020-03-05 22:15 ` [PATCH v1 6/8] spi: dw: Add mem_ops Eugeniy Paltsev
2020-03-06 0:48 ` Sean Anderson
2020-03-06 17:03 ` Eugeniy Paltsev
2020-03-06 20:36 ` Sean Anderson
2020-03-06 21:20 ` Sean Anderson
2020-03-12 16:01 ` Eugeniy Paltsev
2020-03-05 19:19 ` [PATCH v1 7/8] riscv: Add device tree bindings for SPI Sean Anderson
2020-03-22 1:55 ` Marek Vasut
2020-03-22 2:39 ` Sean Anderson
2020-03-05 19:19 ` [PATCH v1 8/8] riscv: Add support for SPI on Kendryte K210 Sean Anderson
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=20200305191925.959494-7-seanga2@gmail.com \
--to=seanga2@gmail.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.