Re: [PATCH RFC v2 06/35] spi: spi-mem: Add support for dummy cycles

From: Bough Chen

Date: Mon Nov 30 2026 - 05:54:29 EST


On Fri, Nov 27, 2026 at 03:22:28PM +0100, Miquel Raynal wrote:
> The dummy phase is a number of clock cycles, not a number of bytes. SPI
> NOR flashes commonly require a number of dummy cycles that is not a
> multiple of 8, and the byte-based representation cannot express it.
>
> Add a dummy.ncycles field and make it the primary representation. Keep
> dummy.nbytes for the legacy byte-oriented controllers and users. The two
> fields are mutually exclusive and spi_mem_check_op() enforces that rule.
>
> Controllers that can program an arbitrary number of cycles advertise the
> dummy_cycles capability and consume dummy.ncycles directly. The core
> converts the legacy dummy.nbytes to cycles for them, so the users that
> still describe the dummy phase in bytes keep working. If the byte count
> cannot be expressed as a whole number of cycles, the operation is
> rejected.
>
> For the other controllers the core converts dummy.ncycles to the legacy
> dummy.nbytes representation using the usual maths. If the cycle count
> cannot be represented exactly, the operation is rejected instead of
> being silently truncated.
>
> Link: https://lore.kernel.org/all/20220911174551.653599-1-sergiu.moga@xxxxxxxxxxxxx/
> Link: https://lore.kernel.org/all/20250115134158.19908-1-pratyush@xxxxxxxxxx/
>
> Link: https://lore.kernel.org/all/20220911174551.653599-1-sergiu.moga@xxxxxxxxxxxxx/
> Link: https://lore.kernel.org/all/20250115134158.19908-1-pratyush@xxxxxxxxxx/

Duplicate Link. With this removed:

Reviewed-by: Haibo Chen <haibo.chen@xxxxxxx>


> Signed-off-by: Miquel Raynal <miquel.raynal@xxxxxxxxxxx>
> ---
> drivers/spi/spi-mem.c | 224 ++++++++++++++++++++++++++++++++++++++---
> include/linux/spi/spi-mem.h | 39 ++++++-
> include/trace/events/spi-mem.h | 5 +-
> 3 files changed, 245 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c
> index a1f129c47698..1642388b7004 100644
> --- a/drivers/spi/spi-mem.c
> +++ b/drivers/spi/spi-mem.c
> @@ -12,6 +12,24 @@
> #include <linux/spi/spi-mem.h>
> #include <linux/sched/task_stack.h>
>
> +static unsigned int
> +spi_mem_get_dummy_nbytes(const struct spi_mem_op *op)
> +{
> + u32 bits;
> +
> + if (!op->dummy.ncycles)
> + return op->dummy.nbytes;
> +
> + if (!op->dummy.buswidth)
> + return 0;
> +
> + bits = (u32)op->dummy.ncycles * op->dummy.buswidth;
> + if (op->dummy.dtr)
> + bits *= 2;
> +
> + return DIV_ROUND_UP(bits, 8);
> +}
> +
> #define CREATE_TRACE_POINTS
> #include <trace/events/spi-mem.h>
>
> @@ -252,6 +270,111 @@ static bool spi_mem_buswidth_is_valid(u8 buswidth)
> return true;
> }
>
> +/**
> + * spi_mem_dummy_cycles_to_bytes() - Convert the dummy phase cycles to bytes
> + * @op: the SPI memory operation
> + *
> + * Return: the number of dummy bytes on success, a negative error code if the
> + * dummy phase cannot be represented as a whole number of bytes.
> + * If @op->dummy.ncycles is zero, @op->dummy.nbytes is returned.
> + *
> + * This helper can be used to convert dummy cycles into dummy bytes for
> + * controllers that can only insert a whole number of dummy bytes.
> + */
> +static int spi_mem_dummy_cycles_to_bytes(const struct spi_mem_op *op)
> +{
> + u32 bits;
> +
> + if (!op->dummy.ncycles)
> + return op->dummy.nbytes;
> +
> + if (!op->dummy.buswidth)
> + return -EOPNOTSUPP;
> +
> + bits = (u32)op->dummy.ncycles * op->dummy.buswidth;
> + if (op->dummy.dtr)
> + bits *= 2;
> +
> + if (bits % 8 || bits / 8 > U8_MAX)
> + return -EOPNOTSUPP;
> +
> + return bits / 8;
> +}
> +
> +/**
> + * spi_mem_dummy_bytes_to_cycles() - Convert the dummy phase bytes to cycles
> + * @op: the SPI memory operation
> + *
> + * Return: the number of dummy clock cycles on success, a negative error code
> + * if the dummy phase cannot be represented as a whole number of clock
> + * cycles. If @op->dummy.nbytes is zero, @op->dummy.ncycles is returned.
> + *
> + * This helper can be used to convert inlet operations using dummy bytes
> + * into operations using dummy cycles.
> + */
> +static int spi_mem_dummy_bytes_to_cycles(const struct spi_mem_op *op)
> +{
> + u32 bits, bits_per_cycle;
> +
> + if (!op->dummy.nbytes)
> + return op->dummy.ncycles;
> +
> + bits_per_cycle = op->dummy.buswidth;
> + if (op->dummy.dtr)
> + bits_per_cycle *= 2;
> + if (!bits_per_cycle)
> + return -EOPNOTSUPP;
> +
> + bits = (u32)op->dummy.nbytes * 8;
> + if (bits % bits_per_cycle)
> + return -EOPNOTSUPP;
> +
> + bits /= bits_per_cycle;
> + if (bits > U8_MAX)
> + return -EOPNOTSUPP;
> +
> + return bits;
> +}
> +
> +/*
> + * Controllers either program the dummy phase in clock cycles or in whole
> + * bytes. Convert the representation used by the operation to the one the
> + * controller expects.
> + *
> + * Return: 0 if the operation is lossless, -EOPNOTSUPP otherwise.
> + */
> +static int spi_mem_convert_dummy(struct spi_mem *mem, struct spi_mem_op *op)
> +{
> + struct spi_controller *ctlr = mem->spi->controller;
> + int n;
> +
> + if (op->dummy.ncycles) {
> + if (spi_mem_controller_is_capable(ctlr, dummy_cycles))
> + return 0;
> +
> + n = spi_mem_dummy_cycles_to_bytes(op);
> + if (n < 0)
> + return n;
> +
> + op->dummy.nbytes = n;
> + op->dummy.ncycles = 0;
> + } else if (op->dummy.nbytes) {
> + if (!spi_mem_controller_is_capable(ctlr, dummy_cycles))
> + return 0;
> +
> + n = spi_mem_dummy_bytes_to_cycles(op);
> + if (n < 0)
> + return n;
> +
> + op->dummy.ncycles = n;
> + op->dummy.nbytes = 0;
> +
> + return 0;
> + }
> +
> + return 0;
> +}
> +
> static int spi_mem_check_op(const struct spi_mem_op *op)
> {
> if (!op->cmd.buswidth || !op->cmd.nbytes)
> @@ -259,9 +382,13 @@ static int spi_mem_check_op(const struct spi_mem_op *op)
>
> if ((op->addr.nbytes && !op->addr.buswidth) ||
> (op->dummy.nbytes && !op->dummy.buswidth) ||
> + (op->dummy.ncycles && !op->dummy.buswidth) ||
> (op->data.nbytes && !op->data.buswidth))
> return -EINVAL;
>
> + if (op->dummy.nbytes && op->dummy.ncycles)
> + return -EINVAL;
> +
> if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) ||
> !spi_mem_buswidth_is_valid(op->addr.buswidth) ||
> !spi_mem_buswidth_is_valid(op->dummy.buswidth) ||
> @@ -321,6 +448,9 @@ bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
> if (spi_mem_check_op(&eval_op))
> return false;
>
> + if (spi_mem_convert_dummy(mem, &eval_op))
> + return false;
> +
> return spi_mem_internal_supports_op(mem, &eval_op);
> }
> EXPORT_SYMBOL_GPL(spi_mem_supports_op);
> @@ -366,6 +496,7 @@ static void spi_mem_access_end(struct spi_mem *mem)
> static void spi_mem_add_op_stats(struct spi_statistics __percpu *pcpu_stats,
> const struct spi_mem_op *op, int exec_op_ret)
> {
> + int dummy_nbytes = spi_mem_get_dummy_nbytes(op);
> struct spi_statistics *stats;
> u64 len, l2len;
>
> @@ -382,7 +513,7 @@ static void spi_mem_add_op_stats(struct spi_statistics __percpu *pcpu_stats,
>
> /* Use the sum of all lengths as bytes count and histogram value. */
> len = op->cmd.nbytes + op->addr.nbytes;
> - len += op->dummy.nbytes + op->data.nbytes;
> + len += dummy_nbytes + op->data.nbytes;
> u64_stats_add(&stats->bytes, len);
> l2len = min(fls(len), SPI_STATISTICS_HISTO_SIZE) - 1;
> u64_stats_inc(&stats->transfer_bytes_histo[l2len]);
> @@ -409,31 +540,41 @@ static void spi_mem_add_op_stats(struct spi_statistics __percpu *pcpu_stats,
> /**
> * spi_mem_exec_op() - Execute a memory operation
> * @mem: the SPI memory
> - * @op: the memory operation to execute
> + * @ro_op: the memory operation to execute (shall not be edited)
> *
> * Executes a memory operation.
> *
> - * This function first checks that @op is supported and then tries to execute
> - * it.
> + * This function first checks that @ro_op is supported and then tries to
> + * execute it.
> *
> * Return: 0 in case of success, a negative error code otherwise.
> */
> -int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
> +int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *ro_op)
> {
> unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
> struct spi_controller *ctlr = mem->spi->controller;
> + struct spi_mem_op wr_op = *ro_op, *op = &wr_op;
> struct spi_transfer xfers[4] = { };
> struct spi_message msg;
> u8 *tmpbuf;
> int ret;
>
> /* Make sure the operation frequency is correct before going futher */
> - spi_mem_adjust_op_freq(mem, (struct spi_mem_op *)op);
> + spi_mem_adjust_op_freq(mem, op);
> +
> + ret = spi_mem_check_op(op);
> + if (ret)
> + return ret;
> +
> + /* Also make sure the dummy phase can be consumed by the controller */
> + ret = spi_mem_convert_dummy(mem, op);
> + if (ret)
> + return ret;
>
> dev_vdbg(&mem->spi->dev, "[cmd: 0x%02x][%dB addr: %#8llx][%2dB dummy][%4dB data %s] %d%c-%d%c-%d%c-%d%c @ %uHz\n",
> op->cmd.opcode,
> op->addr.nbytes, (op->addr.nbytes ? op->addr.val : 0),
> - op->dummy.nbytes,
> + spi_mem_get_dummy_nbytes(op),
> op->data.nbytes, (op->data.nbytes ? (op->data.dir == SPI_MEM_DATA_IN ? " read" : "write") : " "),
> op->cmd.buswidth, op->cmd.dtr ? 'D' : 'S',
> op->addr.buswidth, op->addr.dtr ? 'D' : 'S',
> @@ -441,10 +582,6 @@ int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
> op->data.buswidth, op->data.dtr ? 'D' : 'S',
> op->max_freq ? op->max_freq : mem->spi->max_speed_hz);
>
> - ret = spi_mem_check_op(op);
> - if (ret)
> - return ret;
> -
> if (!spi_mem_internal_supports_op(mem, op))
> return -EOPNOTSUPP;
>
> @@ -472,6 +609,22 @@ int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
> }
> }
>
> + /*
> + * spi_mem_convert_dummy() converted the dummy phase to cycles because
> + * the controller advertised the dummy_cycles capability. The regular
> + * SPI transfer path below builds spi_transfer segments and can only
> + * express the dummy phase in bytes, so convert it back. Reject the
> + * operation if the cycle count cannot be represented exactly.
> + */
> + if (op->dummy.ncycles) {
> + ret = spi_mem_dummy_cycles_to_bytes(op);
> + if (ret < 0)
> + return ret;
> +
> + op->dummy.nbytes = ret;
> + op->dummy.ncycles = 0;
> + }
> +
> tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
>
> /*
> @@ -625,12 +778,22 @@ int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
> {
> struct spi_controller *ctlr = mem->spi->controller;
> size_t len;
> + int ret;
> +
> + ret = spi_mem_convert_dummy(mem, op);
> + if (ret)
> + return ret;
>
> if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
> return ctlr->mem_ops->adjust_op_size(mem, op);
>
> if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
> - len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
> + int dummy_nbytes = spi_mem_dummy_cycles_to_bytes(op);
> +
> + if (dummy_nbytes < 0)
> + return dummy_nbytes;
> +
> + len = op->cmd.nbytes + op->addr.nbytes + dummy_nbytes;
>
> if (len > spi_max_transfer_size(mem->spi))
> return -EINVAL;
> @@ -701,9 +864,12 @@ u64 spi_mem_calc_op_duration(struct spi_mem *mem, struct spi_mem_op *op)
> ncycles += ((op->cmd.nbytes * 8) / op->cmd.buswidth) / (op->cmd.dtr ? 2 : 1);
> ncycles += ((op->addr.nbytes * 8) / op->addr.buswidth) / (op->addr.dtr ? 2 : 1);
>
> - /* Dummy bytes are optional for some SPI flash memory operations */
> - if (op->dummy.nbytes)
> - ncycles += ((op->dummy.nbytes * 8) / op->dummy.buswidth) / (op->dummy.dtr ? 2 : 1);
> + /* Dummy cycles are optional for some SPI flash memory operations */
> + if (op->dummy.ncycles)
> + ncycles += op->dummy.ncycles;
> + else if (op->dummy.nbytes)
> + ncycles += ((op->dummy.nbytes * 8) / op->dummy.buswidth) /
> + (op->dummy.dtr ? 2 : 1);
>
> ncycles += ((op->data.nbytes * 8) / op->data.buswidth) / (op->data.dtr ? 2 : 1);
>
> @@ -809,6 +975,21 @@ spi_mem_dirmap_create(struct spi_mem *mem,
> desc->mem = mem;
> desc->info = *info;
> desc->info.op_tmpl = &desc->info.primary_op_tmpl;
> +
> + ret = spi_mem_convert_dummy(mem, &desc->info.primary_op_tmpl);
> + if (ret) {
> + kfree(desc);
> + return ERR_PTR(ret);
> + }
> +
> + if (desc->info.secondary_op_tmpl.cmd.opcode) {
> + ret = spi_mem_convert_dummy(mem, &desc->info.secondary_op_tmpl);
> + if (ret) {
> + kfree(desc);
> + return ERR_PTR(ret);
> + }
> + }
> +
> if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create) {
> ret = spi_mem_access_start(mem);
> if (ret) {
> @@ -819,6 +1000,8 @@ spi_mem_dirmap_create(struct spi_mem *mem,
> ret = ctlr->mem_ops->dirmap_create(desc);
>
> spi_mem_access_end(mem);
> + } else {
> + ret = -EOPNOTSUPP;
> }
>
> if (ret) {
> @@ -1065,6 +1248,7 @@ int spi_mem_poll_status(struct spi_mem *mem,
> u16 timeout_ms)
> {
> struct spi_controller *ctlr = mem->spi->controller;
> + struct spi_mem_op eval_op = *op;
> int ret = -EOPNOTSUPP;
> int read_status_ret;
> u16 status;
> @@ -1074,11 +1258,19 @@ int spi_mem_poll_status(struct spi_mem *mem,
> return -EINVAL;
>
> if (ctlr->mem_ops && ctlr->mem_ops->poll_status && !spi_get_csgpiod(mem->spi, 0)) {
> + ret = spi_mem_check_op(&eval_op);
> + if (ret)
> + return ret;
> +
> + ret = spi_mem_convert_dummy(mem, &eval_op);
> + if (ret)
> + return ret;
> +
> ret = spi_mem_access_start(mem);
> if (ret)
> return ret;
>
> - ret = ctlr->mem_ops->poll_status(mem, op, mask, match,
> + ret = ctlr->mem_ops->poll_status(mem, &eval_op, mask, match,
> initial_delay_us, polling_delay_us,
> timeout_ms);
>
> diff --git a/include/linux/spi/spi-mem.h b/include/linux/spi/spi-mem.h
> index 0cce6b57242a..fc9f75c0609f 100644
> --- a/include/linux/spi/spi-mem.h
> +++ b/include/linux/spi/spi-mem.h
> @@ -11,6 +11,7 @@
> #ifndef __LINUX_SPI_MEM_H
> #define __LINUX_SPI_MEM_H
>
> +#include <linux/math.h>
> #include <linux/spi/spi.h>
>
> #define SPI_MEM_OP_CMD(__opcode, __buswidth) \
> @@ -75,6 +76,12 @@
> .buswidth = __buswidth, \
> }
>
> +#define SPI_MEM_OP_DUMMY_CYCLES(__ncycles, __buswidth) \
> + { \
> + .ncycles = __ncycles, \
> + .buswidth = __buswidth, \
> + }
> +
> #define SPI_MEM_DTR_OP_DUMMY(__nbytes, __buswidth) \
> { \
> .nbytes = __nbytes, \
> @@ -82,6 +89,13 @@
> .dtr = true, \
> }
>
> +#define SPI_MEM_DTR_OP_DUMMY_CYCLES(__ncycles, __buswidth) \
> + { \
> + .ncycles = __ncycles, \
> + .buswidth = __buswidth, \
> + .dtr = true, \
> + }
> +
> #define SPI_MEM_OP_NO_DUMMY { }
>
> #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \
> @@ -154,10 +168,18 @@ enum spi_mem_data_dir {
> * address value, so users should make sure the value fits in the
> * assigned number of bytes.
> * @dummy: data for dummy operation
> - * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can
> - * be zero if the operation does not require dummy bytes
> - * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes
> - * @dummy.dtr: whether the dummy bytes should be sent in DTR mode or not
> + * @dummy.ncycles: number of dummy clock cycles to insert after an opcode or
> + * address. Can be zero if the operation does not require
> + * dummy cycles. Mutually exclusive with @dummy.nbytes.
> + * @dummy.nbytes: legacy number of dummy bytes to send after an opcode or
> + * address. Can be zero if the operation does not require dummy
> + * bytes. Mutually exclusive with @dummy.ncycles.
> + * @dummy.buswidth: number of IO lanes used during the dummy phase. It is
> + * also used to convert @dummy.ncycles to bytes for controllers
> + * that can only insert a whole number of dummy bytes.
> + * @dummy.dtr: whether the dummy phase is clocked in DTR mode. This only
> + * describes the pin state and does not change the number of
> + * clock cycles.
> * @data: the data attributes
> * @data.buswidth: number of IO lanes used to send/receive the data
> * @data.dtr: whether the data should be sent in DTR mode or not
> @@ -191,6 +213,7 @@ struct spi_mem_op {
> } addr;
>
> struct {
> + u8 ncycles;
> u8 nbytes;
> u8 buswidth;
> u8 dtr : 1;
> @@ -392,6 +415,11 @@ struct spi_controller_mem_ops {
> * is a pause in the transfer (eg. internal bus contention or
> * DMA arbitration on an interconnect). Features such as NAND
> * continuous reads shall not be leveraged.
> + * @dummy_cycles: Supports programming the exact number of dummy clock cycles.
> + * When set, the core converts the legacy @dummy.nbytes to clock
> + * cycles. When unset, the core converts @dummy.ncycles to whole
> + * dummy bytes for the controller and rejects operations that
> + * cannot be converted exactly.
> */
> struct spi_controller_mem_caps {
> bool dtr;
> @@ -400,6 +428,7 @@ struct spi_controller_mem_caps {
> bool per_op_freq;
> bool secondary_op_tmpl;
> bool no_cs_assertion;
> + bool dummy_cycles;
> };
>
> #define spi_mem_controller_is_capable(ctlr, cap) \
> @@ -473,7 +502,7 @@ bool spi_mem_supports_op(struct spi_mem *mem,
> const struct spi_mem_op *op);
>
> int spi_mem_exec_op(struct spi_mem *mem,
> - const struct spi_mem_op *op);
> + const struct spi_mem_op *ro_op);
>
> const char *spi_mem_get_name(struct spi_mem *mem);
>
> diff --git a/include/trace/events/spi-mem.h b/include/trace/events/spi-mem.h
> index d13f0bcff5e7..9df571d7799d 100644
> --- a/include/trace/events/spi-mem.h
> +++ b/include/trace/events/spi-mem.h
> @@ -22,7 +22,8 @@ TRACE_EVENT(spi_mem_start_op,
>
> TP_STRUCT__entry(
> __string(name, mem->name)
> - __dynamic_array(u8, op, 1 + op->addr.nbytes + op->dummy.nbytes)
> + __dynamic_array(u8, op, 1 + op->addr.nbytes +
> + spi_mem_get_dummy_nbytes(op))
> __dynamic_array(u8, data, op->data.dir == SPI_MEM_DATA_OUT ?
> min(op->data.nbytes, 64) : 0)
> __field(u32, data_len)
> @@ -53,7 +54,7 @@ TRACE_EVENT(spi_mem_start_op,
> op->addr.val >> (8 * (op->addr.nbytes - i - 1));
>
> memset(((u8 *)__get_dynamic_array(op)) + op->addr.nbytes + 1,
> - 0xff, op->dummy.nbytes);
> + 0xff, spi_mem_get_dummy_nbytes(op));
>
> __entry->data_len = op->data.nbytes;
> __entry->data_buswidth = op->data.buswidth;
>
> --
> 2.56.0
>