Skip to content

drivers: flash: spi_nor: optimizations and better Micron flash support #88467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2025

Conversation

robhancocksed
Copy link
Contributor

  • Avoid providing redundant TX buffers to the SPI driver for the data portion of read commands where the TX data is not used. This is more efficient and also avoids passing uninitialized data to an external device.
  • Add support for the flag status register used on some Micron flash devices, which improves error detection and in some cases must be read for the flash device to function properly.
  • Add support for fast read commands using a dummy byte between the address and data phases, which often allows the flash device to operate at a higher SPI clock rate.

Note, there might be a compliance error raised, as I wasn't able to satisfy both clang-format and checkpatch with regard to how one line was formatted..

@robhancocksed
Copy link
Contributor Author

Looks like the drivers.console.line_splitting.plus.some_option test is currently failing, for presumably unrelated reasons..

@robhancocksed
Copy link
Contributor Author

Ping.. any feedback?

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request improves SPI NOR flash support by optimizing read operations and adding better support for Micron flash devices.

  • Introduces new device tree properties for flag status register and fast read support.
  • Adds corresponding flag and command definitions in the SPI NOR driver headers and source.
  • Modifies the access and read functions to handle dummy bytes and leverage the flag status register when available.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
dts/bindings/mtd/jedec,spi-nor-common.yaml Adds DT properties for flag status register and fast read.
drivers/flash/spi_nor.h Introduces flag status register bits and new flash command macros.
drivers/flash/spi_nor.c Updates access and read routines to support fast read and flag status register operations.
Comments suppressed due to low confidence (1)

drivers/flash/spi_nor.c:471

  • [nitpick] Consider consolidating the similar fast-read command macros to reduce code duplication and improve maintainability.
#define spi_nor_cmd_addr_fast_read(dev, opcode, addr, dest, length) \

} 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;
}
};
Copy link
Preview

Copilot AI May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a comment clarifying that the dummy byte is intentionally left with its default (zero) value to improve code clarity.

Suggested change
};
};
/* If a dummy byte is required, increase the buffer lengths.
* The dummy byte is intentionally left with its default (zero) value
* as it serves as a placeholder for timing or protocol requirements.
*/

Copilot uses AI. Check for mistakes.

@robhancocksed robhancocksed requested review from de-nordic and kartben May 7, 2025 20:48
de-nordic
de-nordic previously approved these changes May 8, 2025
@de-nordic de-nordic requested a review from anangl May 8, 2025 12:14
@@ -496,8 +518,8 @@ static int spi_nor_wait_until_ready(const struct device *dev, k_timeout_t poll_d

while (true) {
/* If flag status register is present, check it rather than the standard
* status register since it allows better error detection (and some devices
* that have it require it to be read after a program operation).
* status register since it allows better error detection. Also, some devices
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This correction should be done in the second commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, not sure how it ended up in the third one. Moved this change to the second commit.


const struct spi_buf_set tx_set = {
.buffers = spi_buf,
.buffers = spi_buf_tx,
.count = (length != 0) ? 2 : 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just change this line to:

Suggested change
.count = (length != 0) ? 2 : 1,
.count = (is_write && length != 0) ? 2 : 1,

to prevent that undesired use of data for TX in RX transactions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks like this works and simplifies things a bit.

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 <[email protected]>
Some Micron (and possibly other) SPI NOR devices implement a flag status
register which provides more information on the success/failure of erase
and program operations. In addition to better error checking, some of
these devices actually don't function properly if the flag status
register is not read after a program operation (subsequent reads will
only return 0xFF bytes).

Add a device tree parameter to indicate that the flag status register is
supported. When specified, the flag status register will be used for
ready/error checks rather than the standard status register.

Signed-off-by: Robert Hancock <[email protected]>
Most SPI NOR flash devices support a "fast read" command which uses
dummy bits between the address and the start of the data transfer. In
many cases, the maximum SPI clock speed of the device is lower for the
regular read command due to the limited time between the address and
data phases, so using the fast read command will remove this restriction
and allow for faster transfers.

Add a device tree flag to indicate that fast reads should be used for
the device.

Signed-off-by: Robert Hancock <[email protected]>
Copy link

sonarqubecloud bot commented May 8, 2025

@de-nordic de-nordic added this to the v4.2.0 milestone May 13, 2025
@kartben kartben merged commit 3a2639a into zephyrproject-rtos:main May 13, 2025
26 checks passed
@robhancocksed robhancocksed deleted the spi-nor-enhancements branch May 13, 2025 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants