[PATCH 4/4] mfd: ocelot: Read LAN9645x registers in a single SPI transfer
From: Jens Emil Schulz Østergaard
Date: Mon Nov 23 2026 - 08:26:46 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 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@xxxxxxxxxxxxx>
---
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@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,
@@ -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);
+
+ 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);
+
+ 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];
+
+ 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);
+
+ 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) {
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