Skip to content

rghaddab/mram write robustness#106557

Open
rghaddab wants to merge 8 commits intozephyrproject-rtos:mainfrom
rghaddab:rghaddab/mram-write-robustness
Open

rghaddab/mram write robustness#106557
rghaddab wants to merge 8 commits intozephyrproject-rtos:mainfrom
rghaddab:rghaddab/mram-write-robustness

Conversation

@rghaddab
Copy link
Copy Markdown
Contributor

A series of commits that adds more robustness to the MRAM write/erase function.
It adds the support of non aligned addresses to the write_block_size
It recovers from a bad write by reading back written data and detecting a bus fault

Comment on lines 39 to 44
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How is this change relevant to the PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well this PR depends on the MRAM_LATENCY service.
This config depends on EVENTS which also depends on MULTITHREADING
so this test doesn't make any sense for me, and it fails because we run into a Kconfig dependency loop

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not just add - CONFIG_SOC_FLASH_NRF_MRAM=n in extra_configs instead of removing this test scenario?

d3zd3z
d3zd3z previously requested changes Apr 9, 2026
Copy link
Copy Markdown
Contributor

@d3zd3z d3zd3z left a comment

Choose a reason for hiding this comment

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

So, I have a bit of a fundamental question about the misaligned write. In general, with most non-volatile storage devices cannot do this in a memory safe way, and at least looking at this implementation, I believe that is also the case here.

What problem is supporting misaligned writes trying to solve. If it isn't done in a power-safe way, what application would find that acceptable.

In general, I would say that flash drivers should never provide workarounds like this, and with how this device appears to work, I would argue that should be the case here as well.

@rghaddab
Copy link
Copy Markdown
Contributor Author

So, I have a bit of a fundamental question about the misaligned write. In general, with most non-volatile storage devices cannot do this in a memory safe way, and at least looking at this implementation, I believe that is also the case here.

What problem is supporting misaligned writes trying to solve. If it isn't done in a power-safe way, what application would find that acceptable.

In general, I would say that flash drivers should never provide workarounds like this, and with how this device appears to work, I would argue that should be the case here as well.

Hi @d3zd3z I would agree as well that misaligned writes is not something that most application would use.
However in this implementation the driver returns -EINVAL when the address is not aligned or the length is not multiple to the MRAM_WORD_SIZE.
The only case where the misaligned writes are supported here is if the user intentionally changes the default write_block_size (== MRAM_WORD_SIZE) to a different value in the device tree.
So the default behavior is aligned writes and length. But the exception of doing a (read ->update -> modify) is only when device tree default values are intentionally changed to a different non aligned value to MRAM_WORD_SIZE.

@de-nordic
Copy link
Copy Markdown
Contributor

Hi @d3zd3z I would agree as well that misaligned writes is not something that most application would use. However in this implementation the driver returns -EINVAL when the address is not aligned or the length is not multiple to the MRAM_WORD_SIZE. The only case where the misaligned writes are supported here is if the user intentionally changes the default write_block_size (== MRAM_WORD_SIZE) to a different value in the device tree. So the default behavior is aligned writes and length. But the exception of doing a (read ->update -> modify) is only when device tree default values are intentionally changed to a different non aligned value to MRAM_WORD_SIZE.

Misalignment in DTS should not be allowed. DTS should have the write-block-size set to minimal of a device required and assert in driver should not allow that to even compile.
I have completely failed to notice this in review.

The only reason sub-writes could be allowed on MRAM would be the same as it implicitly happens in EEPROMS with larger sizes.

Copy link
Copy Markdown
Contributor

@jonathannilsen jonathannilsen left a comment

Choose a reason for hiding this comment

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

I ran into a few compilation issues when trying to integrate the changes here (building from nrfconnect/sdk-zephyr#3960):

Comment thread drivers/flash/soc_flash_nrf_mram.c
Comment thread drivers/flash/soc_flash_nrf_mram.c Outdated
Comment thread drivers/flash/soc_flash_nrf_mram.c Outdated
@rghaddab
Copy link
Copy Markdown
Contributor Author

Misalignment in DTS should not be allowed. DTS should have the write-block-size set to minimal of a device required and assert in driver should not allow that to even compile. I have completely failed to notice this in review.

The only reason sub-writes could be allowed on MRAM would be the same as it implicitly happens in EEPROMS with larger sizes.

@de-nordic I will remove sub-writes support ( < MRAM_WORD_SIZE).
But what about the address alignment ? should the driver add a constraint to limit writes only on aligned addresses to MRAM_WORD_SIZE and let the above layers handle non aligned addresses ?

@karstenkoenig
Copy link
Copy Markdown
Contributor

Misalignment in DTS should not be allowed. DTS should have the write-block-size set to minimal of a device required and assert in driver should not allow that to even compile. I have completely failed to notice this in review.
The only reason sub-writes could be allowed on MRAM would be the same as it implicitly happens in EEPROMS with larger sizes.

@de-nordic I will remove sub-writes support ( < MRAM_WORD_SIZE). But what about the address alignment ? should the driver add a constraint to limit writes only on aligned addresses to MRAM_WORD_SIZE and let the above layers handle non aligned addresses ?

The same would apply, either we have read/modify/write support in the flash driver or not.

@rghaddab rghaddab force-pushed the rghaddab/mram-write-robustness branch from 8bb5ba4 to 6d0f139 Compare April 13, 2026 16:33
@rghaddab rghaddab force-pushed the rghaddab/mram-write-robustness branch from 6d0f139 to 6847218 Compare April 14, 2026 13:27
@zephyrbot zephyrbot requested a review from kartben April 14, 2026 13:29
@d3zd3z
Copy link
Copy Markdown
Contributor

d3zd3z commented Apr 14, 2026

The same would apply, either we have read/modify/write support in the flash driver or not.

Flash drivers should never have read/modify/write support. It makes it impossible to use in a robust way. Robustness needs to be implemented by an FTL, or a filesystem that knows how to handle the complexity behind this.

@d3zd3z
Copy link
Copy Markdown
Contributor

d3zd3z commented Apr 14, 2026

The only reason sub-writes could be allowed on MRAM would be the same as it implicitly happens in EEPROMS with larger sizes.

Realistically, an EEPROM driver should know about this, and prevent it from being done (at least if it isn't robust). Short of a special kind of partial_write_but_lose_my_data() API. People really don't seem to casually grasp just how bad read/modify/write operations are.

@rghaddab rghaddab force-pushed the rghaddab/mram-write-robustness branch 2 times, most recently from 4f0ae3a to fa3de24 Compare April 15, 2026 08:53
@rghaddab
Copy link
Copy Markdown
Contributor Author

@d3zd3z can you remove the change request, as I removed the non aligned write-block-size support.

@kartben kartben added the AI-assisted At least one commit in the PR has an "Assisted-by: " entry. label Apr 20, 2026
Add a BULID_ASSERT to make sure that the write_block_size defined in the
device tree is a multiple of the MRAM_WORD_SIZE which is 16B (128 bits)

Signed-off-by: Riadh Ghaddab <riadh.ghaddab@nordicsemi.no>
When writes are issued to the mram controller without verifying that it
is ready, it can cause the reads to be stalled until all writes finish.
Fix this by adding a wait busy loop before each MRAM_WORD aligned write
and disable autopowerdown before starting the write.

Signed-off-by: Riadh Ghaddab <riadh.ghaddab@nordicsemi.no>
Fix the case where the MRAM writes get corrupted in some conditions.
After each "MRAM_WORD_SIZE write", a read of the written/erased MRAM word
is performed while masking the bus faults to avoid halting the system.
If an error is detected in the BFSR status bits, retry the write for a
maximum of CONFIG_NRF_MRAM_MAX_RETRIES (default 20) times.

Signed-off-by: Riadh Ghaddab <riadh.ghaddab@nordicsemi.no>
When requesting/releasing the no_latency mode for the mram, verify that
the request/release is successful to avoid running into infinite loop if
the mram controller is in autopowerdown mode.

Signed-off-by: Riadh Ghaddab <riadh.ghaddab@nordicsemi.no>
soc_flash_nrf_mram driver needs MULTITHREADING to be compiled.
As this test disables MULTITHREADING, the mram flash driver needs to be
disabled as well.

Signed-off-by: Riadh Ghaddab <riadh.ghaddab@nordicsemi.no>
memcpy is not optimized for the Cortex-M core due to this :
zephyrproject-rtos#95154
Modify this by writing each word individually.

Signed-off-by: Riadh Ghaddab <riadh.ghaddab@nordicsemi.no>
@rghaddab rghaddab force-pushed the rghaddab/mram-write-robustness branch from fa3de24 to 94e33fd Compare April 21, 2026 12:00
@rghaddab
Copy link
Copy Markdown
Contributor Author

rghaddab commented Apr 21, 2026

twister failures will be fixed after :
#107414
#107704
#107703

Add CONFIG_MULTITHREADING config fragment for nrf54h20/cpuapp

Signed-off-by: Riadh Ghaddab <riadh.ghaddab@nordicsemi.no>
RISCV_CORE_NORDIC_CORE platforms cannot be tested with settings
subsystem as they cannot enable the MRAM driver.

Signed-off-by: Riadh Ghaddab <riadh.ghaddab@nordicsemi.no>
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Architectures area: Boards/SoCs area: Flash area: Linker Scripts area: MCUBoot area: MSPI area: RISCV RISCV Architecture (32-bit & 64-bit) area: Samples Samples area: Settings Settings subsystem area: Tests Issues related to a particular existing or missing test DNM (manifest) This PR should not be merged (controlled by action-manifest) manifest manifest-mcuboot platform: nRF Nordic nRFx

Projects

None yet

Development

Successfully merging this pull request may close these issues.