From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796218523; b=FtRjucCpU46gYVBTajtR9qUv85QzLv0L6CMgwnn9LLSfAEglVDTE4GP60BBrD9ykzi5Z R9WLUKg8gKG7LMlSXjko5cLP0cA3/7H4PTvVVX/zAj6doSeC5Mum4u4yDG9Ol4xk1UW7T tmdS1FUyVj71tmzaUoN7dTiNUzaZkSEz65DDgassUHF7gxOZWmLQHDkLm/CIHCMD6Rbga WKHzXqgUaIqWwdM5JgXeB3X48xa6AYdy4D4TxhiSfTmS5f7XOpS4k1UeQGK6PxdbjUSzI 79Xf/mDm3T6TfUHd8hPyMfmcpO6t9pNMwIvho2ufn5QTIiEphlpRniMzfMAi5W6hRzg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796218523; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition: Content-Transfer-Encoding:In-Reply-To:X-AI-Review-Draft; bh=0EZ9GlQDsrAV6BagzIiZvYQSFnTPxThw6c9OJxceQTw=; b=jSMdSRR27XxBj4oyVcponbNF0EmmFu9XKHLYThL3NHBSORNfIqP1M76/6jNlgWNRW7uj e5QfRyFSIuiZ8Yi0xZjLAza1bXPzY90Ei7scA5kf2q0b6hX8LVFBk1pF8o8pt1WGlSl2v cazL7KRwgTPctIn33H9qaKUUUEBNy05cRrTFRWIOk5CSunaA1bsiPM40LA09GGm6hBDbZ cdtieqEibAkq7a1PsdLE17EjJIADX1qe9FEi76ug2LV3jpNA7fFPbGwFpa9OgRFaBBUZw GeEVP1f8P5uu5IwrfdMRhzjJnEL78JAdJ/Gf0m5w6lNqSpmwAuMa1A9ufZPZviTuh2g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Xb+5A15w; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E16B7606A31A; Wed, 2 Dec 2026 13:35:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Xb+5A15w Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00CDD1F00893; Wed, 2 Dec 2026 13:35:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796218522; bh=0EZ9GlQDsrAV6BagzIiZvYQSFnTPxThw6c9OJxceQTw=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=Xb+5A15w7FsUiWD4j+vHcmzeFsg8Fb2SIn6tNhd1GqFewddXWmtTepkFlxG+tF55C fKFVe/pXrJNeboaEaqzDRLEuAIav5urC5DqWKc7FvklaECWHYNdqFUxeT1zSEM8E+a EQ7ve3LYp+O0o6nphV3D6rJJV5at+VyVUEypFrYOd1JH7Ky8cEIBF3DuPM/iDUv9gL M9Nf6X0WlyKTZfK7q79uKoAwb2MwdiMWHRSspejWcUPfXHRFdKZbm9357qcjmNdrpB t+fq8fMZvaGZ1xFKaSnYIHUgUPyhsaAsZQgqL+K4uJEOMzGM9bW1FbPuuIZCgEKT/3 d3P41pyP6XvrA== Date: Wed, 2 Dec 2026 13:35:17 +0000 From: Lee Jones To: Jens Emil Schulz =?iso-8859-1?Q?=D8stergaard?= Cc: Colin Foster , UNGLinuxDriver@microchip.com, Rob Herring , Krzysztof Kozlowski , Conor Dooley , Richard Cochran , 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 Message-ID: <179568544891.32537.5068972183702816273@kernel.org> References: <20261123-mfd-ocelot-add-lan9645x-support-v1-0-567d811fe1de@microchip.com> <20261123-mfd-ocelot-add-lan9645x-support-v1-4-567d811fe1de@microchip.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20261123-mfd-ocelot-add-lan9645x-support-v1-4-567d811fe1de@microchip.com> X-AI-Review-Draft: <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 > --- > 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 > */ > > +#include > #include > #include > #include > @@ -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(®map_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, ®map_config); > + if (!ocelot_spi_single_xfer_read(spi, spi_get_device_match_data(spi))) > + return devm_regmap_init(dev, &ocelot_spi_regmap_bus, dev, ®map_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, ®map_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