From 1fdb43cf279d03b40da1eaaceb58b33b4a060aa8 Mon Sep 17 00:00:00 2001 From: Robert Hancock Date: Wed, 9 Apr 2025 10:47:39 -0600 Subject: [PATCH] drivers: flash: spi_nor: optimize SPI buffer usage This driver was providing SPI buffers for both TX and RX on the data payload portion of read transfers, even though the TX buffer is not meaningful in these cases. As well as being less efficient, this also caused likely uninitialized data to be transferred to the device, which is possibly problematic. Update to not include the TX buffer for the read data payload SPI transfer, so that the SPI driver can generate dummy TX data internally. Signed-off-by: Robert Hancock --- drivers/flash/spi_nor.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/drivers/flash/spi_nor.c b/drivers/flash/spi_nor.c index c02e77bf9a6..b0ce6af878f 100644 --- a/drivers/flash/spi_nor.c +++ b/drivers/flash/spi_nor.c @@ -381,8 +381,8 @@ static int spi_nor_access(const struct device *const dev, struct spi_nor_data *const driver_data = dev->data; bool is_addressed = (access & NOR_ACCESS_ADDRESSED) != 0U; bool is_write = (access & NOR_ACCESS_WRITE) != 0U; - uint8_t buf[5] = { 0 }; - struct spi_buf spi_buf[2] = { + uint8_t buf[5] = {opcode}; + struct spi_buf spi_buf_tx[2] = { { .buf = buf, .len = 1, @@ -392,8 +392,17 @@ static int spi_nor_access(const struct device *const dev, .len = length } }; + struct spi_buf spi_buf_rx[2] = { + { + .buf = NULL, + .len = 1, + }, + { + .buf = data, + .len = length + } + }; - buf[0] = opcode; if (is_addressed) { bool access_24bit = (access & NOR_ACCESS_24BIT_ADDR) != 0; bool access_32bit = (access & NOR_ACCESS_32BIT_ADDR) != 0; @@ -409,20 +418,22 @@ static int spi_nor_access(const struct device *const dev, if (use_32bit) { memcpy(&buf[1], &addr32.u8[0], 4); - spi_buf[0].len += 4; + spi_buf_tx[0].len += 4; + spi_buf_rx[0].len += 4; } else { memcpy(&buf[1], &addr32.u8[1], 3); - spi_buf[0].len += 3; + spi_buf_tx[0].len += 3; + spi_buf_rx[0].len += 3; } }; const struct spi_buf_set tx_set = { - .buffers = spi_buf, - .count = (length != 0) ? 2 : 1, + .buffers = spi_buf_tx, + .count = (is_write && length != 0) ? 2 : 1, }; const struct spi_buf_set rx_set = { - .buffers = spi_buf, + .buffers = spi_buf_rx, .count = 2, };