Skip to content

Commit 1fdb43c

Browse files
robhancocksedkartben
authored andcommitted
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 <robert.hancock@calian.com>
1 parent 4278e98 commit 1fdb43c

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

drivers/flash/spi_nor.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ static int spi_nor_access(const struct device *const dev,
381381
struct spi_nor_data *const driver_data = dev->data;
382382
bool is_addressed = (access & NOR_ACCESS_ADDRESSED) != 0U;
383383
bool is_write = (access & NOR_ACCESS_WRITE) != 0U;
384-
uint8_t buf[5] = { 0 };
385-
struct spi_buf spi_buf[2] = {
384+
uint8_t buf[5] = {opcode};
385+
struct spi_buf spi_buf_tx[2] = {
386386
{
387387
.buf = buf,
388388
.len = 1,
@@ -392,8 +392,17 @@ static int spi_nor_access(const struct device *const dev,
392392
.len = length
393393
}
394394
};
395+
struct spi_buf spi_buf_rx[2] = {
396+
{
397+
.buf = NULL,
398+
.len = 1,
399+
},
400+
{
401+
.buf = data,
402+
.len = length
403+
}
404+
};
395405

396-
buf[0] = opcode;
397406
if (is_addressed) {
398407
bool access_24bit = (access & NOR_ACCESS_24BIT_ADDR) != 0;
399408
bool access_32bit = (access & NOR_ACCESS_32BIT_ADDR) != 0;
@@ -409,20 +418,22 @@ static int spi_nor_access(const struct device *const dev,
409418

410419
if (use_32bit) {
411420
memcpy(&buf[1], &addr32.u8[0], 4);
412-
spi_buf[0].len += 4;
421+
spi_buf_tx[0].len += 4;
422+
spi_buf_rx[0].len += 4;
413423
} else {
414424
memcpy(&buf[1], &addr32.u8[1], 3);
415-
spi_buf[0].len += 3;
425+
spi_buf_tx[0].len += 3;
426+
spi_buf_rx[0].len += 3;
416427
}
417428
};
418429

419430
const struct spi_buf_set tx_set = {
420-
.buffers = spi_buf,
421-
.count = (length != 0) ? 2 : 1,
431+
.buffers = spi_buf_tx,
432+
.count = (is_write && length != 0) ? 2 : 1,
422433
};
423434

424435
const struct spi_buf_set rx_set = {
425-
.buffers = spi_buf,
436+
.buffers = spi_buf_rx,
426437
.count = 2,
427438
};
428439

0 commit comments

Comments
 (0)