From: david laight <david.laight@runbox.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Prajna Rajendra Kumar <prajna.rajendrakumar@microchip.com>,
linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org,
Mark Brown <broonie@kernel.org>
Subject: Re: [PATCH v2 2/6] spi: microchip-core: Refactor FIFO read and write handlers
Date: Wed, 26 Nov 2025 09:21:45 +0000 [thread overview]
Message-ID: <20251126092145.2f8e4c8d@pumpkin> (raw)
In-Reply-To: <20251126075558.2035012-3-andriy.shevchenko@linux.intel.com>
On Wed, 26 Nov 2025 08:54:40 +0100
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> Make both handlers to be shorter and easier to understand.
> While at it, unify their style.
The read code looks dubious... (nothing new though)
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
> drivers/spi/spi-microchip-core-spi.c | 32 +++++++++-------------------
> 1 file changed, 10 insertions(+), 22 deletions(-)
>
> diff --git a/drivers/spi/spi-microchip-core-spi.c b/drivers/spi/spi-microchip-core-spi.c
> index 08ccdc5f0cc9..f8184b711272 100644
> --- a/drivers/spi/spi-microchip-core-spi.c
> +++ b/drivers/spi/spi-microchip-core-spi.c
> @@ -91,21 +91,14 @@ static inline void mchp_corespi_disable(struct mchp_corespi *spi)
> static inline void mchp_corespi_read_fifo(struct mchp_corespi *spi, u32 fifo_max)
> {
> for (int i = 0; i < fifo_max; i++) {
> - u32 data;
> -
> while (readb(spi->regs + MCHP_CORESPI_REG_STAT) &
> MCHP_CORESPI_STATUS_RXFIFO_EMPTY)
> ;
This is a hard spin until data is available.
I think 'spi' is a bit like 'i2c' (etc) so is quite slow.
Even if the code thinks there are 'fifo_max' bytes in the fifo it seems
wrong to spin indefinitely.
If the code is trying to read a response that is still arriving from the
physical hardware is is positively wrong.
If some hardware has a glitch that FIFO_EMPTY is temporarily incorrectly set
after a read - then maybe you need some recovery code.
Otherwise I suspect FIFO_EMPTY should generate a short read.
The write code (below) does an 'early terminate'' on fifo full.
Presumably it is woken by an interrupt to continue the write?
I actually doubt that transferring messages that are larger than the
device fifo is ever going to be completely reliable.
You'd need to guarantee the interrupt latency to update the fifo be short
enough to guarantee the fifo won't underflow/overflow.
(Unless the spi hardware 'clock stops' the physical interface when the fifo
if full/empty - which is effectively what happens when software 'bit-bangs'
these serial interfaces.)
>
> - data = readb(spi->regs + MCHP_CORESPI_REG_RXDATA);
> + if (spi->rx_buf)
> + *spi->rx_buf++ = readb(spi->regs + MCHP_CORESPI_REG_RXDATA);
>
> spi->rx_len--;
> - if (!spi->rx_buf)
> - continue;
> -
> - *spi->rx_buf = data;
> -
> - spi->rx_buf++;
> }
> }
>
> @@ -127,23 +120,18 @@ static void mchp_corespi_disable_ints(struct mchp_corespi *spi)
>
> static inline void mchp_corespi_write_fifo(struct mchp_corespi *spi, u32 fifo_max)
> {
> - int i = 0;
> -
> - while ((i < fifo_max) &&
> - !(readb(spi->regs + MCHP_CORESPI_REG_STAT) &
> - MCHP_CORESPI_STATUS_TXFIFO_FULL)) {
> - u32 word;
> -
> - word = spi->tx_buf ? *spi->tx_buf : 0xaa;
> - writeb(word, spi->regs + MCHP_CORESPI_REG_TXDATA);
> + for (int i = 0; i < fifo_max; i++) {
unsigned int or u32 ??
> + if (readb(spi->regs + MCHP_CORESPI_REG_STAT) &
> + MCHP_CORESPI_STATUS_TXFIFO_FULL)
> + break;
>
> if (spi->tx_buf)
> - spi->tx_buf++;
> + writeb(*spi->tx_buf++, spi->regs + MCHP_CORESPI_REG_TXDATA);
> + elsespi->regs + MCHP_CORESPI_REG_TXDATA);
> + writeb(0xaa, spi->regs + MCHP_CORESPI_REG_TXDATA);
I'm not sure I don't prefer the version with one writeb() call.
How about:
writeb(spi->tx_buf ? *spi->tx_buf++ : 0xaa,
spi->regs + MCHP_CORESPI_REG_TXDATA);
David
>
> - i++;
> + spi->tx_len--;
> }
> -
> - spi->tx_len -= i;
> }
>
> static void mchp_corespi_set_cs(struct spi_device *spi, bool disable)
next prev parent reply other threads:[~2025-11-26 9:22 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-26 7:54 [PATCH v2 0/6] spi: microchip-core: Code improvements Andy Shevchenko
2025-11-26 7:54 ` [PATCH v2 1/6] spi: microchip-core: use min() instead of min_t() Andy Shevchenko
2025-11-26 9:22 ` david laight
2025-11-27 15:55 ` Prajna Rajendra Kumar
2025-11-26 7:54 ` [PATCH v2 2/6] spi: microchip-core: Refactor FIFO read and write handlers Andy Shevchenko
2025-11-26 9:21 ` david laight [this message]
2025-11-26 11:17 ` Andy Shevchenko
2025-11-26 12:05 ` Mark Brown
2025-11-26 12:13 ` Andy Shevchenko
2025-11-27 16:08 ` Prajna Rajendra Kumar
2025-11-27 16:49 ` Andy Shevchenko
2025-11-27 16:50 ` Andy Shevchenko
2025-11-27 18:05 ` Conor Dooley
2025-11-27 19:01 ` Andy Shevchenko
2025-11-26 7:54 ` [PATCH v2 3/6] spi: microchip-core: Replace dead code (-ENOMEM error message) Andy Shevchenko
2025-11-27 15:58 ` Prajna Rajendra Kumar
2025-11-26 7:54 ` [PATCH v2 4/6] spi: microchip-core: Utilise temporary variable for struct device Andy Shevchenko
2025-11-27 15:59 ` Prajna Rajendra Kumar
2025-11-26 7:54 ` [PATCH v2 5/6] spi: microchip-core: Use SPI_MODE_X_MASK Andy Shevchenko
2025-11-27 16:00 ` Prajna Rajendra Kumar
2025-11-26 7:54 ` [PATCH v2 6/6] spi: microchip-core: Remove unneeded PM related macro Andy Shevchenko
2025-11-27 16:01 ` Prajna Rajendra Kumar
2025-11-28 18:01 ` (subset) [PATCH v2 0/6] spi: microchip-core: Code improvements Mark Brown
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251126092145.2f8e4c8d@pumpkin \
--to=david.laight@runbox.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=broonie@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-spi@vger.kernel.org \
--cc=prajna.rajendrakumar@microchip.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox