[PATCH v2 5/5] mfd: ocelot: Read LAN9645x registers in a single SPI transfer

From: Jens Emil Schulz Østergaard

Date: Fri Dec 04 2026 - 07:03:47 EST


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 transmit
and receive buffers are allocated per regmap, so the regmap lock
serializes their use. The transmit buffer stays zero after the
address, so a read only copies the address into it.

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@xxxxxxxxxxxxx>
---
Changes in v2:
- Use separate transmit and receive buffers, so a read no longer clears
the padding bytes (Lee)
- Copy the address and the value with memcpy() (Lee)
- Comment the three transfer read and the dummy buffer allocation (Lee)
- Decide on the single transfer read once in the probe instead of
looking up the match data per regmap
---
drivers/mfd/ocelot-spi.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++--
drivers/mfd/ocelot.h | 7 +++-
2 files changed, 91 insertions(+), 3 deletions(-)

diff --git a/drivers/mfd/ocelot-spi.c b/drivers/mfd/ocelot-spi.c
index eb040be91204..dd586f477339 100644
--- a/drivers/mfd/ocelot-spi.c
+++ b/drivers/mfd/ocelot-spi.c
@@ -13,6 +13,7 @@
* Author: Colin Foster <colin.foster@xxxxxxxxxxxxxxxx>
*/

+#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,
@@ -61,6 +67,12 @@ static const struct resource lan9645x_gcb_resource =
DEFINE_RES_REG_NAMED(LAN9645X_GCB_RES_START, LAN9645X_GCB_RES_SIZE,
"devcpu_gcb");

+struct ocelot_spi_regmap_ctx {
+ struct device *dev;
+ u8 tx[OCELOT_SPI_XFER_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
+ u8 rx[OCELOT_SPI_XFER_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
+};
+
static int vsc7512_spi_padding_bytes(u32 max_speed_hz)
{
if (max_speed_hz <= 500000)
@@ -217,8 +229,60 @@ 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 ocelot_ddata *ddata = dev_get_drvdata(ctx->dev);
+ struct spi_transfer xfer = {};
+ size_t off;
+ int err;
+
+ 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;
+
+ memcpy(ctx->tx, reg, OCELOT_SPI_ADDR_SIZE);
+
+ xfer.tx_buf = ctx->tx;
+ xfer.rx_buf = ctx->rx;
+ xfer.len = off + OCELOT_SPI_VAL_SIZE;
+
+ err = spi_sync_transfer(to_spi_device(ctx->dev), &xfer, 1);
+ if (err)
+ return err;
+
+ memcpy(val, &ctx->rx[off], OCELOT_SPI_VAL_SIZE);
+
+ 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_full_duplex(struct spi_device *spi)
+{
+ return !(spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) &&
+ !(spi->mode & SPI_3WIRE);
+}
+
struct regmap *ocelot_spi_init_regmap(struct device *dev, const struct resource *res)
{
+ struct ocelot_ddata *ddata = dev_get_drvdata(dev);
+ struct ocelot_spi_regmap_ctx *ctx;
struct regmap_config regmap_config;

memcpy(&regmap_config, &ocelot_spi_regmap_config, sizeof(regmap_config));
@@ -227,7 +291,20 @@ struct regmap *ocelot_spi_init_regmap(struct device *dev, const struct resource
regmap_config.max_register = resource_size(res) - 1;
regmap_config.reg_base = res->start;

- return devm_regmap_init(dev, &ocelot_spi_regmap_bus, dev, &regmap_config);
+ /*
+ * Without single transfer reads, the address, the padding bytes and
+ * the value of a read are sent as three transfers.
+ */
+ if (!ddata->single_xfer_read)
+ return devm_regmap_init(dev, &ocelot_spi_regmap_bus, dev, &regmap_config);
+
+ 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");

@@ -260,6 +337,7 @@ static int ocelot_spi_probe(struct spi_device *spi)
gcb_res = &lan9645x_gcb_resource;
ddata->if_ctrl = LAN9645X_IF_CTRL;
ddata->if_cfgstat = LAN9645X_IF_CFGSTAT;
+ ddata->single_xfer_read = ocelot_spi_full_duplex(spi);
reset_padding = LAN9645X_RESET_PADDING_BYTES;
padding = lan9645x_spi_padding_bytes(spi->max_speed_hz);
break;
@@ -274,8 +352,13 @@ static int ocelot_spi_probe(struct spi_device *spi)
"SPI clock of %u Hz needs more than %d padding bytes\n",
spi->max_speed_hz, OCELOT_SPI_MAX_PADDING_BYTES);

+ /*
+ * A single transfer read sends the padding bytes from the transfer
+ * buffer of its regmap, so the dummy buffer is only needed for reads
+ * in three transfers.
+ */
dummy_len = max(padding, reset_padding);
- if (dummy_len) {
+ if (!ddata->single_xfer_read && dummy_len) {
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 802a7db1722e..99909a109e1d 100644
--- a/drivers/mfd/ocelot.h
+++ b/drivers/mfd/ocelot.h
@@ -35,9 +35,13 @@ enum ocelot_chip {
* read data gets returned. This is calculated during
* initialization based on bus speed, or set to the
* reset default of the chip around a chip reset.
+ * @single_xfer_read: Read a register in one full duplex SPI transfer instead
+ * of separate transfers for the address, the padding
+ * bytes and the value.
* @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;
@@ -47,6 +51,7 @@ struct ocelot_ddata {
unsigned int if_cfgstat;
u32 if_cfgstat_si;
int spi_padding_bytes;
+ bool single_xfer_read;
void *dummy_buf;
};


--
2.52.0