Re: [PATCH v3 1/3] spi: spi-mem: add automatic poll status functions

Boris Brezillon
2021-05-18T11:40:49Z

On Tue, 18 May 2021 11:39:49 +0200
<patrice.chotard@foss.st.com> wrote:

> +/**
> + * spi_mem_poll_status() - Poll memory device status
> + * @mem: SPI memory device
> + * @op: the memory operation to execute
> + * @mask: status bitmask to ckeck
> + * @match: (status & mask) expected value
> + * @initial_delay_us: delay in us before starting to poll
> + * @polling_delay_us: time to sleep between reads in us
> + * @timeout_ms: timeout in milliseconds
> + *
> + * This function send a polling status request to the controller driver
> + *
> + * Return: 0 in case of success, -ETIMEDOUT in case of error,
> + * -EOPNOTSUPP if not supported.
> + */
> +int spi_mem_poll_status(struct spi_mem *mem,
> + const struct spi_mem_op *op,
> + u16 mask, u16 match,
> + unsigned long initial_delay_us,
> + unsigned long polling_delay_us,
> + u16 timeout_ms)
> +{
> + struct spi_controller *ctlr = mem->spi->controller;
> + int ret = -EOPNOTSUPP;
> + int read_status_ret;
> + u16 status;
> +
> + if (op->data.nbytes < 1 || op->data.nbytes > 2)
> + return -EINVAL;

We should also make sure this is a read operation.

> +
> + if (ctlr->mem_ops && ctlr->mem_ops->poll_status) {
> + ret = spi_mem_access_start(mem);
> + if (ret)
> + return ret;
> +
> + ret = ctlr->mem_ops->poll_status(mem, op, mask, match,
> + initial_delay_us, polling_delay_us,
> + timeout_ms);
> +
> + spi_mem_access_end(mem);
> + }
> +
> + if (ret == -EOPNOTSUPP) {
> + if (!spi_mem_supports_op(mem, op))
> + return ret;
> +
> + if (initial_delay_us < 10)
> + udelay(initial_delay_us);
> + else
> + usleep_range((initial_delay_us >> 2) + 1,
> + initial_delay_us);
> +
> + ret = read_poll_timeout(spi_mem_read_status, read_status_ret,
> + (read_status_ret || ((status) & mask) == match),
> + polling_delay_us, timeout_ms * 1000, false, mem,
> + op, &status);
> + if (read_status_ret)
> + return read_status_ret;
> + }
> +
> + return ret;
> +}