All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Lechner <dlechner@baylibre.com>
To: "Mark Brown" <broonie@kernel.org>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"Rob Herring" <robh+dt@kernel.org>,
	"Krzysztof Kozlowski" <krzysztof.kozlowski+dt@linaro.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Michael Hennerich" <michael.hennerich@analog.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Frank Rowand" <frowand.list@gmail.com>
Cc: "David Lechner" <dlechner@baylibre.com>,
	"Thierry Reding" <thierry.reding@gmail.com>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Jonathan Corbet" <corbet@lwn.net>,
	linux-spi@vger.kernel.org, linux-iio@vger.kernel.org,
	devicetree@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-pwm@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Lars-Peter Clausen" <lars@metafoo.de>
Subject: [PATCH 05/13] spi: axi-spi-engine: add SPI offload support
Date: Wed, 10 Jan 2024 13:49:46 -0600	[thread overview]
Message-ID: <20240109-axi-spi-engine-series-3-v1-5-e42c6a986580@baylibre.com> (raw)
In-Reply-To: <20240109-axi-spi-engine-series-3-v1-0-e42c6a986580@baylibre.com>

This adds an implementation of the SPI offload_ops to the AXI SPI Engine
driver to provide offload support.

Offload lookup is done by device property lookup. SPI Engine commands
and tx data  are recorded by writing to offload-specific FIFOs in the
SPI Engine hardware.

Co-developed-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 drivers/spi/spi-axi-spi-engine.c | 270 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 270 insertions(+)

diff --git a/drivers/spi/spi-axi-spi-engine.c b/drivers/spi/spi-axi-spi-engine.c
index 58280dd1c901..1d7ddc867b50 100644
--- a/drivers/spi/spi-axi-spi-engine.c
+++ b/drivers/spi/spi-axi-spi-engine.c
@@ -2,9 +2,11 @@
 /*
  * SPI-Engine SPI controller driver
  * Copyright 2015 Analog Devices Inc.
+ * Copyright 2023 BayLibre, SAS
  *  Author: Lars-Peter Clausen <lars@metafoo.de>
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/idr.h>
 #include <linux/interrupt.h>
@@ -38,11 +40,22 @@
 #define SPI_ENGINE_REG_SDI_DATA_FIFO		0xe8
 #define SPI_ENGINE_REG_SDI_DATA_FIFO_PEEK	0xec
 
+#define SPI_ENGINE_MAX_NUM_OFFLOADS		32
+
+#define SPI_ENGINE_REG_OFFLOAD_CTRL(x)		(0x100 + (SPI_ENGINE_MAX_NUM_OFFLOADS * x))
+#define SPI_ENGINE_REG_OFFLOAD_STATUS(x)	(0x104 + (SPI_ENGINE_MAX_NUM_OFFLOADS * x))
+#define SPI_ENGINE_REG_OFFLOAD_RESET(x)		(0x108 + (SPI_ENGINE_MAX_NUM_OFFLOADS * x))
+#define SPI_ENGINE_REG_OFFLOAD_CMD_FIFO(x)	(0x110 + (SPI_ENGINE_MAX_NUM_OFFLOADS * x))
+#define SPI_ENGINE_REG_OFFLOAD_SDO_FIFO(x)	(0x114 + (SPI_ENGINE_MAX_NUM_OFFLOADS * x))
+
 #define SPI_ENGINE_INT_CMD_ALMOST_EMPTY		BIT(0)
 #define SPI_ENGINE_INT_SDO_ALMOST_EMPTY		BIT(1)
 #define SPI_ENGINE_INT_SDI_ALMOST_FULL		BIT(2)
 #define SPI_ENGINE_INT_SYNC			BIT(3)
 
+#define SPI_ENGINE_OFFLOAD_CTRL_ENABLE		BIT(0)
+#define SPI_ENGINE_OFFLOAD_STATUS_ENABLED	BIT(0)
+
 #define SPI_ENGINE_CONFIG_CPHA			BIT(0)
 #define SPI_ENGINE_CONFIG_CPOL			BIT(1)
 #define SPI_ENGINE_CONFIG_3WIRE			BIT(2)
@@ -76,6 +89,10 @@
 #define SPI_ENGINE_CMD_SYNC(id) \
 	SPI_ENGINE_CMD(SPI_ENGINE_INST_MISC, SPI_ENGINE_MISC_SYNC, (id))
 
+/* default sizes - can be changed when SPI Engine firmware is compiled */
+#define SPI_ENGINE_OFFLOAD_CMD_FIFO_SIZE	16
+#define SPI_ENGINE_OFFLOAD_SDO_FIFO_SIZE	16
+
 struct spi_engine_program {
 	unsigned int length;
 	uint16_t instructions[];
@@ -107,6 +124,10 @@ struct spi_engine_message_state {
 	u8 sync_id;
 };
 
+struct spi_engine_offload {
+	unsigned int index;
+};
+
 struct spi_engine {
 	struct clk *clk;
 	struct clk *ref_clk;
@@ -119,6 +140,9 @@ struct spi_engine {
 	struct spi_controller *controller;
 
 	unsigned int int_enable;
+
+	struct spi_offload offloads[SPI_ENGINE_MAX_NUM_OFFLOADS];
+	struct spi_engine_offload offload_priv[SPI_ENGINE_MAX_NUM_OFFLOADS];
 };
 
 static void spi_engine_program_add_cmd(struct spi_engine_program *p,
@@ -603,6 +627,239 @@ static int spi_engine_transfer_one_message(struct spi_controller *host,
 	return 0;
 }
 
+static struct spi_offload *spi_engine_offload_get(struct spi_device *spi,
+						  unsigned int index)
+{
+	struct spi_controller *host = spi->controller;
+	struct spi_engine *spi_engine = spi_controller_get_devdata(host);
+	struct spi_offload *offload;
+	u32 vals[SPI_ENGINE_MAX_NUM_OFFLOADS];
+	int ret;
+
+	/* Use the adi,offloads array to find the offload at index. */
+
+	if (index >= ARRAY_SIZE(vals))
+		return ERR_PTR(-EINVAL);
+
+	ret = device_property_read_u32_array(&spi->dev, "adi,offloads", vals,
+					     index + 1);
+	if (ret < 0)
+		return ERR_PTR(ret);
+
+	if (vals[index] >= SPI_ENGINE_MAX_NUM_OFFLOADS)
+		return ERR_PTR(-EINVAL);
+
+	offload = &spi_engine->offloads[vals[index]];
+
+	return offload;
+}
+
+static int spi_engine_offload_prepare(struct spi_offload *offload,
+				      struct spi_message *msg)
+{
+	struct spi_controller *host = offload->controller;
+	struct spi_engine_offload *priv = offload->priv;
+	struct spi_engine *spi_engine = spi_controller_get_devdata(host);
+	struct spi_engine_program p_dry, *p __free(kfree) = NULL;
+	struct spi_transfer *xfer;
+	void __iomem *cmd_addr;
+	void __iomem *sdo_addr;
+	size_t tx_word_count = 0;
+	unsigned int i;
+
+	/* count total number of tx words in message */
+	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
+		if (!xfer->tx_buf)
+			continue;
+
+		if (xfer->bits_per_word <= 8)
+			tx_word_count += xfer->len;
+		else if (xfer->bits_per_word <= 16)
+			tx_word_count += xfer->len / 2;
+		else
+			tx_word_count += xfer->len / 4;
+	}
+
+	/* REVISIT: could get actual size from devicetree if needed */
+	if (tx_word_count > SPI_ENGINE_OFFLOAD_SDO_FIFO_SIZE)
+		return -EINVAL;
+
+	spi_engine_precompile_message(msg);
+
+	/* dry run to get length */
+	p_dry.length = 0;
+	spi_engine_compile_message(msg, true, &p_dry);
+
+	/* REVISIT: could get actual size from devicetree if needed */
+	if (p_dry.length > SPI_ENGINE_OFFLOAD_CMD_FIFO_SIZE)
+		return -EINVAL;
+
+	p = kzalloc(sizeof(*p) + sizeof(*p->instructions) * p_dry.length, GFP_KERNEL);
+	if (!p)
+		return -ENOMEM;
+
+	spi_engine_compile_message(msg, false, p);
+
+	cmd_addr = spi_engine->base + SPI_ENGINE_REG_OFFLOAD_CMD_FIFO(priv->index);
+	sdo_addr = spi_engine->base + SPI_ENGINE_REG_OFFLOAD_SDO_FIFO(priv->index);
+
+	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
+		if (!xfer->tx_buf)
+			continue;
+
+		if (xfer->bits_per_word <= 8) {
+			const u8 *buf = xfer->tx_buf;
+
+			for (i = 0; i < xfer->len; i++)
+				writel_relaxed(buf[i], sdo_addr);
+		} else if (xfer->bits_per_word <= 16) {
+			const u16 *buf = xfer->tx_buf;
+
+			for (i = 0; i < xfer->len / 2; i++)
+				writel_relaxed(buf[i], sdo_addr);
+		} else {
+			const u32 *buf = xfer->tx_buf;
+
+			for (i = 0; i < xfer->len / 4; i++)
+				writel_relaxed(buf[i], sdo_addr);
+		}
+	}
+
+	for (i = 0; i < p->length; i++)
+		writel_relaxed(p->instructions[i], cmd_addr);
+
+	return 0;
+}
+
+static void spi_engine_offload_unprepare(struct spi_offload *offload)
+{
+	struct spi_controller *host = offload->controller;
+	struct spi_engine_offload *priv = offload->priv;
+	struct spi_engine *spi_engine = spi_controller_get_devdata(host);
+
+	writel_relaxed(1, spi_engine->base +
+			  SPI_ENGINE_REG_OFFLOAD_RESET(priv->index));
+	writel_relaxed(0, spi_engine->base +
+			  SPI_ENGINE_REG_OFFLOAD_RESET(priv->index));
+}
+
+static int spi_engine_offload_enable(struct spi_offload *offload)
+{
+	struct spi_controller *host = offload->controller;
+	struct spi_engine_offload *priv = offload->priv;
+	struct spi_engine *spi_engine = spi_controller_get_devdata(host);
+	unsigned int reg;
+
+	reg = readl_relaxed(spi_engine->base +
+			    SPI_ENGINE_REG_OFFLOAD_CTRL(priv->index));
+	reg |= SPI_ENGINE_OFFLOAD_CTRL_ENABLE;
+	writel_relaxed(reg, spi_engine->base +
+			    SPI_ENGINE_REG_OFFLOAD_CTRL(priv->index));
+
+	return 0;
+}
+
+static void spi_engine_offload_disable(struct spi_offload *offload)
+{
+	struct spi_controller *host = offload->controller;
+	struct spi_engine_offload *priv = offload->priv;
+	struct spi_engine *spi_engine = spi_controller_get_devdata(host);
+	unsigned int reg;
+
+	reg = readl_relaxed(spi_engine->base +
+			    SPI_ENGINE_REG_OFFLOAD_CTRL(priv->index));
+	reg &= ~SPI_ENGINE_OFFLOAD_CTRL_ENABLE;
+	writel_relaxed(reg, spi_engine->base +
+			    SPI_ENGINE_REG_OFFLOAD_CTRL(priv->index));
+}
+
+static const struct spi_controller_offload_ops spi_engine_offload_ops = {
+	.get = spi_engine_offload_get,
+	.prepare = spi_engine_offload_prepare,
+	.unprepare = spi_engine_offload_unprepare,
+	.enable = spi_engine_offload_enable,
+	.disable = spi_engine_offload_disable,
+};
+
+static void spi_engine_offload_release(void *p)
+{
+	struct spi_offload *offload = p;
+	struct platform_device *pdev = container_of(offload->dev,
+						    struct platform_device, dev);
+
+	offload->dev = NULL;
+	platform_device_unregister(pdev);
+}
+
+/**
+ * devm_spi_engine_register_offload() - Registers platform device for offload.
+ *
+ * @dev: The parent platform device node.
+ * @offload: The offload firmware node.
+ *
+ * Return: 0 on success, negative error code otherwise.
+ */
+static int devm_spi_engine_register_offload(struct device *dev,
+					    struct spi_engine *spi_engine,
+					    struct fwnode_handle *fwnode)
+{
+	struct platform_device_info pdevinfo = {
+		.parent = dev,
+		.name = "offload",
+		.fwnode = fwnode,
+	};
+	struct platform_device *pdev;
+	struct spi_offload *offload;
+	u32 index;
+	int ret;
+
+	ret = fwnode_property_read_u32(fwnode, "reg", &index);
+	if (ret)
+		return ret;
+
+	if (index >= SPI_ENGINE_MAX_NUM_OFFLOADS)
+		return -EINVAL;
+
+	pdevinfo.id = index;
+
+	pdev = platform_device_register_full(&pdevinfo);
+	if (IS_ERR(pdev))
+		return PTR_ERR(pdev);
+
+	offload = &spi_engine->offloads[index];
+	offload->dev = &pdev->dev;
+
+	return devm_add_action_or_reset(dev, spi_engine_offload_release, offload);
+}
+
+/**
+ * spi_engine_offload_populate() - Registers platform device for each offload instance.
+ * @host: The SPI controller.
+ * @spi_engine: The SPI engine.
+ * @dev: The parent platform device.
+ */
+static void spi_engine_offload_populate(struct spi_controller *host,
+					struct spi_engine *spi_engine,
+					struct device *dev)
+{
+	struct fwnode_handle *offloads;
+	struct fwnode_handle *child;
+	int ret;
+
+	/* offloads are optional */
+	offloads = device_get_named_child_node(dev, "offloads");
+	if (!offloads)
+		return;
+
+	fwnode_for_each_available_child_node(offloads, child) {
+		ret = devm_spi_engine_register_offload(dev, spi_engine, child);
+		if (ret)
+			dev_warn(dev, "failed to register offload: %d\n", ret);
+	}
+
+	fwnode_handle_put(offloads);
+}
+
 static void spi_engine_timeout(struct timer_list *timer)
 {
 	struct spi_engine *spi_engine = from_timer(spi_engine, timer, watchdog_timer);
@@ -633,6 +890,7 @@ static int spi_engine_probe(struct platform_device *pdev)
 	unsigned int version;
 	int irq;
 	int ret;
+	int i;
 
 	irq = platform_get_irq(pdev, 0);
 	if (irq < 0)
@@ -670,6 +928,15 @@ static int spi_engine_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
+	for (i = 0; i < SPI_ENGINE_MAX_NUM_OFFLOADS; i++) {
+		struct spi_engine_offload *priv = &spi_engine->offload_priv[i];
+		struct spi_offload *offload = &spi_engine->offloads[i];
+
+		priv->index = i;
+		offload->controller = host;
+		offload->priv = priv;
+	}
+
 	writel_relaxed(0x00, spi_engine->base + SPI_ENGINE_REG_RESET);
 	writel_relaxed(0xff, spi_engine->base + SPI_ENGINE_REG_INT_PENDING);
 	writel_relaxed(0x00, spi_engine->base + SPI_ENGINE_REG_INT_ENABLE);
@@ -692,6 +959,7 @@ static int spi_engine_probe(struct platform_device *pdev)
 	host->prepare_message = spi_engine_prepare_message;
 	host->unprepare_message = spi_engine_unprepare_message;
 	host->num_chipselect = 8;
+	host->offload_ops = &spi_engine_offload_ops;
 
 	if (host->max_speed_hz == 0)
 		return dev_err_probe(&pdev->dev, -EINVAL, "spi_clk rate is 0");
@@ -702,6 +970,8 @@ static int spi_engine_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, host);
 
+	spi_engine_offload_populate(host, spi_engine, &pdev->dev);
+
 	return 0;
 }
 

-- 
2.43.0


  parent reply	other threads:[~2024-01-10 19:51 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-10 19:49 [PATCH 00/13] spi: axi-spi-engine: add offload support David Lechner
2024-01-10 19:49 ` [PATCH 01/13] spi: add core support for controllers with offload capabilities David Lechner
2024-01-10 21:36   ` Mark Brown
2024-01-11 20:54     ` David Lechner
2024-01-11 21:32       ` David Lechner
2024-01-11 21:49         ` Mark Brown
2024-01-12  9:03           ` David Jander
2024-01-12 20:09         ` David Lechner
2024-03-04 23:21     ` David Lechner
2024-03-05 18:50       ` Mark Brown
2024-03-09 17:54       ` Jonathan Cameron
2024-01-11  8:49   ` Nuno Sá
2024-01-11 13:33     ` Mark Brown
2024-01-11 14:11       ` Nuno Sá
2024-01-11 15:41         ` Mark Brown
2024-01-12  7:26           ` Nuno Sá
2024-01-10 19:49 ` [PATCH 02/13] scripts: dtc: checks: don't warn on SPI non-peripheral child nodes David Lechner
2024-01-11 22:03   ` Rob Herring
2024-01-10 19:49 ` [PATCH 03/13] spi: do not attempt to register DT nodes without @ in name David Lechner
2024-01-10 21:37   ` Mark Brown
2024-01-10 19:49 ` [PATCH 04/13] spi: dt-bindings: adi,axi-spi-engine: add offload bindings David Lechner
2024-01-10 23:14   ` Rob Herring
2024-01-11  0:06     ` David Lechner
2024-01-11  9:14       ` Nuno Sá
2024-01-11  9:07     ` Nuno Sá
2024-01-10 19:49 ` David Lechner [this message]
2024-01-10 21:39   ` [PATCH 05/13] spi: axi-spi-engine: add SPI offload support Mark Brown
2024-01-10 22:31     ` David Lechner
2024-01-11 13:00       ` Mark Brown
2024-01-11 17:57         ` David Lechner
2024-01-10 19:49 ` [PATCH 06/13] iio: buffer: add hardware triggered buffer support David Lechner
2024-01-12 12:37   ` Jonathan Cameron
2024-01-12 15:42     ` David Lechner
2024-01-12 16:50       ` Nuno Sá
2024-01-10 19:49 ` [PATCH 07/13] iio: buffer: dmaengine: add INDIO_HW_BUFFER_TRIGGERED flag David Lechner
2024-01-10 19:49 ` [PATCH 08/13] iio: buffer: add new hardware triggered buffer driver David Lechner
2024-01-11  9:24   ` Nuno Sá
2024-01-10 19:49 ` [PATCH 09/13] bus: auxiliary: increase AUXILIARY_NAME_SIZE David Lechner
2024-01-10 19:49 ` [PATCH 10/13] iio: buffer: dmaengine: export devm_iio_dmaengine_buffer_alloc() David Lechner
2024-01-12 12:37   ` Jonathan Cameron
2024-01-12 14:55     ` David Lechner
2024-01-10 19:49 ` [PATCH 11/13] dt-bindings: iio: offload: add binding for PWM/DMA triggered buffer David Lechner
2024-01-10 21:25   ` Rob Herring
2024-01-10 22:56   ` Rob Herring
2024-01-10 19:49 ` [PATCH 12/13] iio: offload: add new PWM triggered DMA buffer driver David Lechner
2024-01-11  9:31   ` Nuno Sá
2024-01-11 14:59   ` Nuno Sá
2024-01-12 12:51     ` Jonathan Cameron
2024-01-12 13:19       ` Nuno Sá
2024-01-12 12:45   ` Jonathan Cameron
2024-01-10 19:49 ` [PATCH 13/13] iio: adc: ad7380: add SPI offload support David Lechner

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=20240109-axi-spi-engine-series-3-v1-5-e42c6a986580@baylibre.com \
    --to=dlechner@baylibre.com \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=devicetree@vger.kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=jic23@kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=lars@metafoo.de \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=michael.hennerich@analog.com \
    --cc=nuno.sa@analog.com \
    --cc=robh+dt@kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=u.kleine-koenig@pengutronix.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.