Skip to content

drivers: flash: max32: Add read support with ECC workaround #93437

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 6 commits into
base: main
Choose a base branch
from

Conversation

ttmut
Copy link
Contributor

@ttmut ttmut commented Jul 21, 2025

The MAX32657 SoC includes an ECC engine capable of correcting single-bit errors. However, when reading from an unwritten flash location, the ECC controller incorrectly detects an error and flips a bit in the 16th byte, causing 0xFF to be read as 0xFD.

This PR introduces a workaround and related fixes to mitigate this issue:

  1. Mark storage region as non-cacheable using fixed MPU regions. This ensures ECC metadata is correctly updated whenever flash memory is read, as cached flash content bypasses ECC correction.
  2. Relocate flash access functions to SRAM to prevent execution from jumping to a different flash section, which could inadvertently update ECC information.
  3. Disable interrupts during flash access to avoid context switches that might lead to unintended ECC updates.
  4. Introduce a new flash read function that detects and corrects the bit flip when reading from unwritten flash sections.

These changes improve reliability when interacting with flash memory under ECC protection.

A similar issue can be seen in #80351.

Tests

> west build -p auto -b max32657evkit/max32657 .\tests\drivers\flash\common\ -T drivers.flash.common.default

Before:

*** Booting Zephyr OS build v4.2.0-rc1-759-g87917a10874d ***
Running TESTSUITE flash_driver
===================================================================
START - test_flash_copy
Test will run on device flash_controller@29000
 PASS - test_flash_copy in 0.242 seconds
===================================================================
START - test_flash_erase
Test will run on device flash_controller@29000
i=15:   read_buf[i]=253 expected=255
i=31:   read_buf[i]=253 expected=255
i=47:   read_buf[i]=253 expected=255
...
i=495:  read_buf[i]=253 expected=255
i=511:  read_buf[i]=253 expected=255

    Assertion failed at WEST_TOPDIR/zephyr/tests/drivers/flash/common/src/main.c:318: flash_driver_test_flash_erase: (comparison_result is false)
Write operation failed
 FAIL - test_flash_erase in 0.157 seconds
===================================================================
START - test_flash_fill
Test will run on device flash_controller@29000
 PASS - test_flash_fill in 0.132 seconds
...
START - test_read_unaligned_address
Test will run on device flash_controller@29000
 PASS - test_read_unaligned_address in 0.046 seconds
===================================================================
TESTSUITE flash_driver failed.

------ TESTSUITE SUMMARY START ------

SUITE FAIL -  83.33% [flash_driver]: pass = 5, fail = 1, skip = 1, total = 7 duration = 0.754 seconds
 - PASS - [flash_driver.test_flash_copy] duration = 0.242 seconds
 - FAIL - [flash_driver.test_flash_erase] duration = 0.157 seconds
 - PASS - [flash_driver.test_flash_fill] duration = 0.132 seconds
 - PASS - [flash_driver.test_flash_flatten] duration = 0.132 seconds
 - PASS - [flash_driver.test_flash_page_layout] duration = 0.025 seconds
 - SKIP - [flash_driver.test_get_size] duration = 0.020 seconds
 - PASS - [flash_driver.test_read_unaligned_address] duration = 0.046 seconds

------ TESTSUITE SUMMARY END ------

===================================================================
PROJECT EXECUTION FAILED

After:

*** Booting Zephyr OS build v4.2.0-rc1-765-g7abd4d0e463e ***
Running TESTSUITE flash_driver
===================================================================
START - test_flash_copy
Test will run on device flash_controller@29000
 PASS - test_flash_copy in 0.239 seconds
===================================================================
START - test_flash_erase
Test will run on device flash_controller@29000
 PASS - test_flash_erase in 0.042 seconds
===================================================================
START - test_flash_fill
Test will run on device flash_controller@29000
 PASS - test_flash_fill in 0.131 seconds
===================================================================
START - test_flash_flatten
Test will run on device flash_controller@29000
 PASS - test_flash_flatten in 0.131 seconds
===================================================================
START - test_flash_page_layout
Test will run on device flash_controller@29000
start_offset=0xf0000    size=8192       index=120
page_count=128
 PASS - test_flash_page_layout in 0.025 seconds
===================================================================
START - test_get_size
Test will run on device flash_controller@29000
 SKIP - test_get_size in 0.005 seconds
===================================================================
START - test_read_unaligned_address
Test will run on device flash_controller@29000
 PASS - test_read_unaligned_address in 0.047 seconds
===================================================================
TESTSUITE flash_driver succeeded

------ TESTSUITE SUMMARY START ------

SUITE PASS - 100.00% [flash_driver]: pass = 6, fail = 0, skip = 1, total = 7 duration = 0.620 seconds
 - PASS - [flash_driver.test_flash_copy] duration = 0.239 seconds
 - PASS - [flash_driver.test_flash_erase] duration = 0.042 seconds
 - PASS - [flash_driver.test_flash_fill] duration = 0.131 seconds
 - PASS - [flash_driver.test_flash_flatten] duration = 0.131 seconds
 - PASS - [flash_driver.test_flash_page_layout] duration = 0.025 seconds
 - SKIP - [flash_driver.test_get_size] duration = 0.005 seconds
 - PASS - [flash_driver.test_read_unaligned_address] duration = 0.047 seconds

------ TESTSUITE SUMMARY END ------

===================================================================
PROJECT EXECUTION SUCCESSFUL

ttmut and others added 4 commits July 21, 2025 14:15
Default MPU configuration marks whole flash area as cacheable. When
reading from an erased section of flash, cache controller may fill cache
lines with ECC corrected data. To prevent this, disable caching on
storage section so that ECC workaround can be applied during reads and
correct data is returned.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
Move functions in .flashprog section into RAMFUNC so that they can be
executed from SRAM.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
Disable interrupts during flash operations to prevent unintended jumps.

Interrupts are now disabled before read, erase, and write operations to
avoid accidental jumps to other flash sections while working on a
specific section.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
To fix MAX32690 flash problems, I created a wrap version of
MXC_FLC_Write(...) function which disables ICC before calling write
function and enables ICC after this function.

Signed-off-by: Furkan Akkiz <hasanfurkan.akkiz@analog.com>
Copy link

github-actions bot commented Jul 21, 2025

The following west manifest projects have changed revision in this Pull Request:

Name Old Revision New Revision Diff
hal_adi zephyrproject-rtos/hal_adi@16829b7 zephyrproject-rtos/hal_adi@d2886b8 (main) zephyrproject-rtos/hal_adi@16829b77..d2886b8b

All manifest checks OK

Note: This message is automatically posted and updated by the Manifest GitHub Action.

@github-actions github-actions bot added manifest manifest-hal_adi DNM (manifest) This PR should not be merged (controlled by action-manifest) labels Jul 21, 2025
ttmut added 2 commits July 21, 2025 17:34
Update hal_adi to include the new flash read function that checks for
false-positive ECC failures and applies a workaround if needed.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
Use wrapper function for read operations to allow using the new HAL
function that handles ECC checks and erased page detection.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
@github-actions github-actions bot removed the DNM (manifest) This PR should not be merged (controlled by action-manifest) label Jul 21, 2025
Copy link

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.

5 participants