Skip to content

[nrf noup] boot: zephyr: cleanup of SPIM for nRF91#606

Open
diegosolano wants to merge 1 commit into
nrfconnect:mainfrom
diegosolano:spim-cleanup
Open

[nrf noup] boot: zephyr: cleanup of SPIM for nRF91#606
diegosolano wants to merge 1 commit into
nrfconnect:mainfrom
diegosolano:spim-cleanup

Conversation

@diegosolano
Copy link
Copy Markdown

Summary

  • Stop SPIM EasyDMA and disable all instances in nrf_cleanup_peripheral() before chain-loading the application
  • On nRF91 with TF-M and external SPI flash, MCUboot leaves SPIM active after reading the secondary slot. When TF-M reconfigures SPU SRAM permissions, the stale DMA triggers RAMACCERR

This follows the same pattern as PR #602 (SQSPI cleanup for nRF54L) and the existing UARTE/RTC cleanup code.

Test plan

  • Tested on custom nRF9151 board with external SPI flash (MCUboot secondary slot)
  • MCUboot + TF-M + non-secure application boots cleanly with this patch
  • Without it, SPU fires RAMACCERR on every boot

Context

nrf_cleanup.c handles peripheral teardown for peripherals that Zephyr cannot deinitialize. SPIM was missing from the cleanup list, which is a problem for any nRF91 board that uses SPI flash for the MCUboot secondary slot.

Copilot AI review requested due to automatic review settings February 18, 2026 10:00
@diegosolano diegosolano requested a review from a team as a code owner February 18, 2026 10:00
@NordicBuilder
Copy link
Copy Markdown

none

Note: This comment is automatically posted and updated by the Contribs GitHub Action.

Copy link
Copy Markdown

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 PR updates the Zephyr-based MCUboot Nordic peripheral teardown (nrf_cleanup_peripheral()) to also stop SPIM EasyDMA and disable SPIM instances before chain-loading, preventing RAMACCERR faults on nRF91 when TF-M changes SPU SRAM permissions after MCUboot used SPIM for external flash reads.

Changes:

  • Add SPIM HAL include guarded by SPIM_PRESENT.
  • Stop SPIM (trigger STOP task), disable SPIM, and disable SPIM interrupts for all available SPIM instances during peripheral cleanup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread boot/zephyr/nrf_cleanup.c
Comment on lines +206 to +207
/* Stop SPIM EasyDMA and disable all instances to prevent stale DMA
* from causing RAMACCERR when TF-M reconfigures SPU permissions. */
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

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

nrf_cleanup_peripheral() now also stops/disables SPIM, but the public API comment in boot/zephyr/include/nrf_cleanup.h still says it only disables RTC and UARTE. Please update that header comment so the documented behavior matches what the function actually does (and so future additions don’t get missed).

Suggested change
/* Stop SPIM EasyDMA and disable all instances to prevent stale DMA
* from causing RAMACCERR when TF-M reconfigures SPU permissions. */
/* nrf_cleanup_peripheral() also stops SPIM EasyDMA and disables all
* SPIM instances to prevent stale DMA from causing RAMACCERR when
* TF-M reconfigures SPU permissions.
*
* NOTE: Keep the public API comment in boot/zephyr/include/nrf_cleanup.h
* in sync with this function's behavior (RTC, UARTE, SPIM, and any
* future peripherals handled here). */

Copilot uses AI. Check for mistakes.
Stop SPIM EasyDMA and disable all instances in nrf_cleanup_peripheral()
before chain-loading the application. On nRF91 with TF-M and external
SPI flash, MCUboot leaves SPIM active after reading the secondary slot.
When TF-M reconfigures SPU SRAM permissions the stale DMA triggers
RAMACCERR.

This follows the same pattern used for UARTE, RTC, and SQSPI cleanup.

Signed-off-by: Diego Solano <diegosolano@gmail.com>
@sonarqubecloud
Copy link
Copy Markdown

@ahasztag ahasztag self-requested a review March 6, 2026 10:58
Comment thread boot/zephyr/nrf_cleanup.c
};

for (int i = 0; i < sizeof(spim_inst) / sizeof(spim_inst[0]); ++i) {
nrf_spim_task_trigger(spim_inst[i], NRF_SPIM_TASK_STOP);
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.

in theory code should wait for STOPPED event. Same goes for existing UARTE code. Reference:

The STOP task may not be always needed (the peripheral might already be stopped), but if it is sent, software shall wait until the STOPPED event was received as a response before disabling the peripheral through the ENABLE register.

Copy link
Copy Markdown
Contributor

@de-nordic de-nordic left a comment

Choose a reason for hiding this comment

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

Where is sdk-nrf manifest update that takes the change in?

diegosolano added a commit to diegosolano/sdk-nrf that referenced this pull request Mar 26, 2026
Update sdk-mcuboot to include SPIM cleanup for nRF91 from PR:
nrfconnect/sdk-mcuboot#606

Signed-off-by: Diego Solano <diegosolano@gmail.com>
@diegosolano
Copy link
Copy Markdown
Author

Companion sdk-nrf manifest PR: nrfconnect/sdk-nrf#27737

The revision will be updated to the final merge commit once this PR lands.

diegosolano added a commit to diegosolano/sdk-nrf that referenced this pull request Apr 20, 2026
Update sdk-mcuboot to include SPIM cleanup for nRF91 from PR:
nrfconnect/sdk-mcuboot#606

Signed-off-by: Diego Solano <diegosolano@gmail.com>
@diegosolano
Copy link
Copy Markdown
Author

@de-nordic the companion manifest PR is live and rebased: nrfconnect/sdk-nrf#27737 — CLA cleared, no conflicts. Ready for the sdk-nrf side to proceed once this one merges.

@diegosolano
Copy link
Copy Markdown
Author

@de-nordic friendly ping - the companion sdk-nrf manifest PR is now open and rebased: nrfconnect/sdk-nrf#27737 (CLA cleared). Could you re-review here when you have a moment? Once this lands I'll bump the manifest revision to the final merge SHA. Thanks!

diegosolano added a commit to diegosolano/sdk-nrf that referenced this pull request May 2, 2026
Update sdk-mcuboot to include SPIM cleanup for nRF91 from PR:
nrfconnect/sdk-mcuboot#606

Signed-off-by: Diego Solano <diegosolano@gmail.com>
@diegosolano
Copy link
Copy Markdown
Author

@nika-nordic Thanks — you're right per the SPIM/UARTE spec. The Nordic docs explicitly say "if [STOP] is sent, software shall wait until the STOPPED event was received as a response before disabling the peripheral through the ENABLE register."

I've left this PR as-is for now to match the existing UARTE cleanup convention in the same file (which also issues STOP without waiting for STOPPED before nrfy_uarte_disable()). Doing it for only SPIM would create an inconsistency, and doing it for both feels like it belongs in its own change so the wait-for-STOPPED semantics — including a sensible timeout / fallback if the event never asserts — get reviewed on their own merits across all the peripherals in nrf_cleanup_peripheral().

Happy to file that as a follow-up PR if you'd like, or to roll it into this one if you'd prefer it not ship without the fix. Let me know which you'd rather.

@diegosolano
Copy link
Copy Markdown
Author

diegosolano commented May 14, 2026

@de-nordic the companion sdk-nrf manifest update is nrfconnect/sdk-nrf#27737 — it's been open since Mar 26, CLA cleared, and currently points at this PR's branch tip (488e045e); I'll bump it to the merge SHA once this lands. The handle manifest PR check originally failed before #27737 existed; could a maintainer re-run that workflow now that the companion is in place? (I tried rerun-failed-jobs myself but it needs admin rights on the repo.) Let me know if there's anything else you need from my side here.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants