mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: "Jens Emil Schulz Østergaard" <jensemil.schulzostergaard@microchip.com>
Cc: Colin Foster <colin.foster@in-advantage.com>,
	UNGLinuxDriver@microchip.com, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Richard Cochran <richardcochran@gmail.com>,
	mfd@lists.linux.dev, linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH 4/4] mfd: ocelot: Read LAN9645x registers in a single SPI transfer
Date: Wed, 2 Dec 2026 13:35:17 +0000	[thread overview]
Message-ID: <179568544891.32537.5068972183702816273@kernel.org> (raw)
In-Reply-To: <20261123-mfd-ocelot-add-lan9645x-support-v1-4-567d811fe1de@microchip.com>

On Mon, 23 Nov 2026, Jens Emil Schulz Østergaard wrote:

> A register read is three SPI transfers: the address, the padding bytes
> and the value. The SPI controller handles each transfer separately, with
> its own setup and interrupt.
>
> Read the address, the padding bytes and the value of a LAN9645x register
> in one full duplex transfer instead, which removes the handling of two
> transfers from every read. The three transfer read works on the
> LAN9645x as well, this only reduces the cost of a read. The transfer
> buffer is allocated per regmap, so the regmap lock serializes its use.
>
> With the interrupt driven Atmel SPI controller of a LAN966x host at
> 25 MHz and a GPIO chip select, a register read takes 35.8 us instead of
> 62.2 us, with a standard deviation below 0.1 us over seven runs of
> 13800 reads.
>
> Half duplex controllers and devices in 3-wire mode cannot transmit and
> receive in one transfer, so fall back to the three transfer read for
> them.
>
> Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>
> ---
>  drivers/mfd/ocelot-spi.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++--
>  drivers/mfd/ocelot.h     |  3 +-
>  2 files changed, 92 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/mfd/ocelot-spi.c b/drivers/mfd/ocelot-spi.c
> index 6d405627136d..4f2ef847c909 100644
> --- a/drivers/mfd/ocelot-spi.c
> +++ b/drivers/mfd/ocelot-spi.c
> @@ -13,6 +13,7 @@
>   * Author: Colin Foster <colin.foster@in-advantage.com>
>   */
>
> +#include <linux/cache.h>
>  #include <linux/device.h>
>  #include <linux/err.h>
>  #include <linux/errno.h>
> @@ -45,7 +46,12 @@
>  #define LAN9645X_RESET_PADDING_BYTES	15
>  #define LAN9645X_REG_ACCESS_TIME_US	2
>
> +#define OCELOT_SPI_ADDR_SIZE		3
>  #define OCELOT_SPI_MAX_PADDING_BYTES	15
> +#define OCELOT_SPI_VAL_SIZE		4
> +#define OCELOT_SPI_XFER_BUF_SIZE	(OCELOT_SPI_ADDR_SIZE + \
> +					 OCELOT_SPI_MAX_PADDING_BYTES + \
> +					 OCELOT_SPI_VAL_SIZE)
>
>  static const struct resource vsc7512_dev_cpuorg_resource =
>  	DEFINE_RES_REG_NAMED(VSC7512_DEVCPU_ORG_RES_START,
> @@ -70,6 +76,12 @@ struct ocelot_spi_data {
>  	const struct resource *gcb_res;
>  	int reset_padding_bytes;
>  	int (*padding_bytes)(u32 max_speed_hz);
> +	bool single_xfer_read;
> +};
> +
> +struct ocelot_spi_regmap_ctx {
> +	struct device *dev;
> +	u8 buf[OCELOT_SPI_XFER_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
>  };
>
>  static int vsc7512_spi_padding_bytes(u32 max_speed_hz)
> @@ -105,6 +117,7 @@ static const struct ocelot_spi_data lan9645x_spi_data = {
>  	.gcb_res = &lan9645x_gcb_resource,
>  	.reset_padding_bytes = LAN9645X_RESET_PADDING_BYTES,
>  	.padding_bytes = lan9645x_spi_padding_bytes,
> +	.single_xfer_read = true,
>  };
>
>  static const struct ocelot_spi_data vsc7512_spi_data = {
> @@ -249,12 +262,78 @@ static const struct regmap_bus ocelot_spi_regmap_bus = {
>  	.read = ocelot_spi_regmap_bus_read,
>  };
>
> +static int ocelot_spi_regmap_xfer_read(void *context, const void *reg,
> +				       size_t reg_size, void *val,
> +				       size_t val_size)
> +{
> +	struct ocelot_spi_regmap_ctx *ctx = context;
> +	struct spi_transfer xfer = {};
> +	struct ocelot_ddata *ddata;
> +	const u8 *regu8 = reg;
> +	u8 *buf = ctx->buf;
> +	u8 *valu8 = val;
> +	size_t off;
> +	int err;
> +
> +	ddata = dev_get_drvdata(ctx->dev);

Define and initialise this at the top.

> +
> +	if (reg_size != OCELOT_SPI_ADDR_SIZE || val_size != OCELOT_SPI_VAL_SIZE ||
> +	    ddata->spi_padding_bytes > OCELOT_SPI_MAX_PADDING_BYTES)
> +		return -EINVAL;
> +
> +	off = OCELOT_SPI_ADDR_SIZE + ddata->spi_padding_bytes;
> +
> +	buf[0] = regu8[0];
> +	buf[1] = regu8[1];
> +	buf[2] = regu8[2];
> +	memset(&buf[OCELOT_SPI_ADDR_SIZE], 0,
> +	       ddata->spi_padding_bytes + OCELOT_SPI_VAL_SIZE);

Why not use memcpy()?  Or start from a pre-initialised space?

> +	xfer.tx_buf = buf;
> +	xfer.rx_buf = buf;
> +	xfer.len = off + OCELOT_SPI_VAL_SIZE;
> +
> +	err = spi_sync_transfer(to_spi_device(ctx->dev), &xfer, 1);
> +	if (err)
> +		return err;
> +
> +	valu8[0] = buf[off + 0];
> +	valu8[1] = buf[off + 1];
> +	valu8[2] = buf[off + 2];
> +	valu8[3] = buf[off + 3];

memcpy()?

> +
> +	return 0;
> +}
> +
> +static int ocelot_spi_regmap_xfer_write(void *context, const void *data,
> +					size_t count)
> +{
> +	struct ocelot_spi_regmap_ctx *ctx = context;
> +
> +	return spi_write(to_spi_device(ctx->dev), data, count);
> +}
> +
> +static const struct regmap_bus ocelot_spi_regmap_xfer_bus = {
> +	.write = ocelot_spi_regmap_xfer_write,
> +	.read = ocelot_spi_regmap_xfer_read,
> +};
> +
> +static bool ocelot_spi_single_xfer_read(struct spi_device *spi,
> +					const struct ocelot_spi_data *data)
> +{
> +	return data->single_xfer_read &&
> +	       !(spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) &&
> +	       !(spi->mode & SPI_3WIRE);
> +}
> +
>  struct regmap *ocelot_spi_init_regmap(struct device *dev,
>  				      const struct resource *res,
>  				      const struct regmap_access_table *rd_table,
>  				      const struct regmap_access_table *wr_table)
>  {
> +	struct spi_device *spi = to_spi_device(dev);
>  	struct regmap_config regmap_config;
> +	struct ocelot_spi_regmap_ctx *ctx;
>
>  	memcpy(&regmap_config, &ocelot_spi_regmap_config, sizeof(regmap_config));
>
> @@ -264,7 +343,16 @@ struct regmap *ocelot_spi_init_regmap(struct device *dev,
>  	regmap_config.rd_table = rd_table;
>  	regmap_config.wr_table = wr_table;
>
> -	return devm_regmap_init(dev, &ocelot_spi_regmap_bus, dev, &regmap_config);
> +	if (!ocelot_spi_single_xfer_read(spi, spi_get_device_match_data(spi)))
> +		return devm_regmap_init(dev, &ocelot_spi_regmap_bus, dev, &regmap_config);

Needs a comment.

> +	ctx = devm_kzalloc_obj(dev, *ctx);
> +	if (!ctx)
> +		return ERR_PTR(-ENOMEM);
> +
> +	ctx->dev = dev;
> +
> +	return devm_regmap_init(dev, &ocelot_spi_regmap_xfer_bus, ctx, &regmap_config);
>  }
>  EXPORT_SYMBOL_NS(ocelot_spi_init_regmap, "MFD_OCELOT_SPI");
>
> @@ -296,7 +384,7 @@ static int ocelot_spi_probe(struct spi_device *spi)
>  				     spi->max_speed_hz, OCELOT_SPI_MAX_PADDING_BYTES);
>
>  	dummy_len = max(padding, data->reset_padding_bytes);
> -	if (dummy_len) {
> +	if (!ocelot_spi_single_xfer_read(spi, data) && dummy_len) {

Comment.

>  		ddata->dummy_buf = devm_kzalloc(dev, dummy_len, GFP_KERNEL);
>  		if (!ddata->dummy_buf)
>  			return -ENOMEM;
> diff --git a/drivers/mfd/ocelot.h b/drivers/mfd/ocelot.h
> index fa02590a5026..5d0301f0b194 100644
> --- a/drivers/mfd/ocelot.h
> +++ b/drivers/mfd/ocelot.h
> @@ -32,7 +32,8 @@ enum ocelot_chip {
>   *			reset default of the chip around a chip reset.
>   * @dummy_buf:		Zero-filled buffer of at least spi_padding_bytes size.
>   *			The dummy bytes that will be sent out between the
> - *			address and data of a SPI read operation.
> + *			address and data of a SPI read operation. Not
> + *			allocated when reads are done in a single transfer.
>   */
>  struct ocelot_ddata {
>  	enum ocelot_chip chip;
>
> -- 
> 2.52.0
>

-- 
Lee Jones

      reply	other threads:[~2026-12-02 13:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 13:25 [PATCH 0/4] mfd: ocelot: Support the LAN9645x switch family Jens Emil Schulz Østergaard
2026-11-23 13:25 ` [PATCH 1/4] mfd: ocelot: Select the chip through match data Jens Emil Schulz Østergaard
2026-12-02 12:34   ` Lee Jones
2026-11-23 13:25 ` [PATCH 2/4] dt-bindings: mfd: Add the Microchip LAN9645x switch family Jens Emil Schulz Østergaard
2026-11-23 13:25 ` [PATCH 3/4] mfd: ocelot: Add support for the " Jens Emil Schulz Østergaard
2026-12-02 13:28   ` Lee Jones
2026-11-23 13:25 ` [PATCH 4/4] mfd: ocelot: Read LAN9645x registers in a single SPI transfer Jens Emil Schulz Østergaard
2026-12-02 13:35   ` Lee Jones [this message]

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=179568544891.32537.5068972183702816273@kernel.org \
    --to=lee@kernel.org \
    --cc=UNGLinuxDriver@microchip.com \
    --cc=colin.foster@in-advantage.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jensemil.schulzostergaard@microchip.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mfd@lists.linux.dev \
    --cc=netdev@vger.kernel.org \
    --cc=richardcochran@gmail.com \
    --cc=robh@kernel.org \
    /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

all inboxes | Powered by JetHome®