Skip to content

drivers: flash: flash_hp_ra: perform blank check before reading #88113

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jeremydick
Copy link

The value read from unwritten areas of Renesas RAxxx SoCs data flash is undefined. To prevent reading unwritten areas a blank check command is performed first. If the area is blank, we return dummy data so it behaves the same as other flash devices.

Copy link

github-actions bot commented Apr 3, 2025

Hello @jeremydick, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

@de-nordic
Copy link
Collaborator

@KhiemNguyenT re-assigning to you, your IC.

@de-nordic de-nordic assigned KhiemNguyenT and unassigned de-nordic Apr 3, 2025
@@ -30,4 +30,13 @@ config DUAL_BANK_MODE
help
Enable dual bank mode

config CHECK_BEFORE_READING
bool "Verify area before reading it"
default y
Copy link
Contributor

Choose a reason for hiding this comment

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

Not all Renesas RA MCU families with flash hp have undefined value after erasing. Can we add erased-undefined properties in dts and enable this config based on it? like default $(dt_nodelabel_bool_prop,flash1,erased-undefined)

Copy link
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 the RA4E1, RA4E2, RA4M2, RA4M3, RA6E1, RA6E2, RA6M1, RA6M2, RA6M3, RA6M4, RA6M5, RA8D1, RA8M1, and RA8T1 families have the data flash the will readback an undefined value when erased. Is that all of them, or are there any others I have missed?

Copy link
Contributor

@quytranpzz quytranpzz Apr 8, 2025

Choose a reason for hiding this comment

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

Sorry for late response. There is currently a PR to support the RA4L1 #88036, and the erase value for it is 0xff (RA4L1). Thanks for the classification.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks. Since the erase value for the RA4L1 family is 0xff nothing needs to be done for it, only families that have an undefined erase value need to have the erased-undefined property added

@@ -89,9 +144,21 @@ static int flash_ra_read(const struct device *dev, off_t offset, void *data, siz

LOG_DBG("flash: read 0x%lx, len: %u", (long)(offset + flash_data->area_address), len);

memcpy(data, (uint8_t *)(offset + flash_data->area_address), len);
#ifdef CONFIG_CHECK_BEFORE_READING
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we move the data flash region check here instead of inside the is_area_readable. Then, with the code flash region, we won't need to call is_area_readable

Copy link
Author

Choose a reason for hiding this comment

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

The flash region check has been moved

@jeremydick jeremydick force-pushed the renesas_flash_read_fix branch from 67461ac to a1b4517 Compare April 4, 2025 13:37
@github-actions github-actions bot requested a review from rghaddab April 4, 2025 13:37
@jeremydick jeremydick requested a review from quytranpzz April 4, 2025 13:41
@jeremydick jeremydick force-pushed the renesas_flash_read_fix branch 2 times, most recently from 271089f to 52abfff Compare April 7, 2025 19:51
@github-actions github-actions bot requested a review from thenguyenyf April 7, 2025 19:52
@quytranpzz
Copy link
Contributor

@KhiemNguyenT , @thaoluonguw , @duynguyenxa , @thenguyenyf, @khoa-nguyen-18
Performing a blank check when the erase value is undefined would improve the current flash driver. Please take a look.

Copy link
Contributor

@khoa-nguyen-18 khoa-nguyen-18 left a comment

Choose a reason for hiding this comment

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

LGTM

thenguyenyf
thenguyenyf previously approved these changes Apr 21, 2025
…lash

Add an erased-undefined property for Renesas RA series MCUS
data flash that will read back undefined values when erased

Signed-off-by: Jeremy Dick <[email protected]>
The value read from unwritten areas of Renesas RAxxx SoCs data
flash is undefined. To prevent reading unwritten areas a
blank check command is performed first. If the area is blank, we
return dummy data so it behaves the same as other flash devices.

Signed-off-by: Jeremy Dick <[email protected]>
Comment on lines +159 to +165
#ifdef CONFIG_CHECK_BEFORE_READING
} else if (rc == -ENODATA) {
/* Erased area, return dummy data as an erased page. */
memset(data, 0xFF, len);
rc = 0;
#endif
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not in this PR, nevertheless: @jeremydick @khoa-nguyen-18 @thenguyenyf We need to have a discussion on defining -ENODATA return for flash_read API call.
As far as I can see here, your memory has a erasea value (0xff) as a valid write data and erased is separate state.
I think that it may be useful for users to know that, in case of your memory, you can actually detect that the memory has not yet been written at specified location and in such case function should just return without providing bogus data (erase-value filled buffer).

Copy link
Contributor

@khoa-nguyen-18 khoa-nguyen-18 Apr 24, 2025

Choose a reason for hiding this comment

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

Hi @de-nordic, thank you for your feedback.

In my opinion, returning -ENODATA isn’t a typical way for users to check if a flash page has been erased. Most applications, including the flash/common test, simply check for (ret == 0) && (read_buffer_value == 0xFF) to confirm that the page is erased.

The issue we're facing is that the Renesas flash in some SoC series didn't have the 0xFF after erase, which causes compatibility problems with this common check. We’ve considered two options to address this:

  1. Update the Zephyr's flash test/application condition to accommodate different erased values, and make a broad announcement to inform users of Renesas flash drivers that they need to update their condition checks.
  2. Provide a configuration option in the Renesas flash driver that allows users to choose whether they want the erased buffer to be 0xFF (as proposed in this PR). If this configuration is not selected, we should return -ENODATA (need to update). We believe this makes sense, especially for users porting applications from other MCU vendors to Renesas MCUs who want to avoid making changes to their existing code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

As I have stated, we will not be able to resolve this in this PR, but we need a discussion on -ENODATA as valid return from flash_read.

Hi @de-nordic, thank you for your feedback.

In my opinion, returning -ENODATA isn’t a typical way for users to check if a flash page has been erased. Most applications, including the flash/common test, simply check for (ret == 0) && (read_buffer_value == 0xFF) to confirm that the page is erased.

It is not for checking whether flash page is erased, it is rather for checking whether user has placed data in specified location.
Note that there is code in Zephyr that reads page and compares it against erase-value to figure out whether page has been erased (looping through buffer), so it can now be written. In case of your device the code will not be able to recognize a difference between erased area and area that has been written with erase-value.

The issue we're facing is that the Renesas flash in some SoC series didn't have the 0xFF after erase, which causes compatibility problems with this common check. We’ve considered two options to address this:

1. Update the Zephyr's flash test/application condition to accommodate different erased values, and make a broad announcement to inform users of Renesas flash drivers that they need to update their condition checks.

2. Provide a configuration option in the Renesas flash driver that allows users to choose whether they want the erased buffer to be `0xFF` (as proposed in this PR). If this configuration is not selected, we should return `-ENODATA` (need to update). We believe this makes sense, especially for users porting applications from other MCU vendors to Renesas MCUs who want to avoid making changes to their existing code.

I agree with what you did; what I mean is that we need to clarify definition of flash_read on the matter of returning -ENODATA, modify in tree code (tests, etc) and announce the change, because it is API breaking change. This will take more effort and I do not think it should happen in this PR.

Copy link
Author

Choose a reason for hiding this comment

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

@de-nordic I agree, longer term it makes the most sense to update the API to allow -ENODATA as a valid return for flash_read, and update all usages of the flash driver within the tree handle that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Flash platform: Renesas RA Renesas Electronics Corporation, RA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants