This is an automated email from the ASF dual-hosted git repository. jerzy pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 2d38232885f97dbb7c668712a8a51332ae1e15cb Author: Jerzy Kasenberg <[email protected]> AuthorDate: Thu Oct 31 13:17:42 2024 +0100 mcu/nrf5340: Enable NSC region Enable NSC region if sgstubs section is not empty Signed-off-by: Jerzy Kasenberg <[email protected]> --- hw/mcu/nordic/nrf5340/nrf5340.ld | 12 +++++++++++- hw/mcu/nordic/nrf5340/src/hal_system_start.c | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/hw/mcu/nordic/nrf5340/nrf5340.ld b/hw/mcu/nordic/nrf5340/nrf5340.ld index 339ac60ab..b4080fc6f 100644 --- a/hw/mcu/nordic/nrf5340/nrf5340.ld +++ b/hw/mcu/nordic/nrf5340/nrf5340.ld @@ -46,6 +46,8 @@ OUTPUT_FORMAT ("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") */ ENTRY(Reset_Handler) +NSC_REGION_SIZE = 0x200; + SECTIONS { .imghdr (NOLOAD): @@ -107,7 +109,6 @@ INCLUDE "link_tables.ld.h" . = ALIGN(4); } > FLASH - .net_core_img : { PROVIDE(net_core_img_start = .); @@ -127,6 +128,15 @@ INCLUDE "link_tables.ld.h" *(.ARM.exidx* .gnu.linkonce.armexidx.*) . = ALIGN(4); } > FLASH + + .gnu.sgstubs : + { + FILL(0xffff); + . = (ALIGN(16384) >= . + NSC_REGION_SIZE) ? (ALIGN(16384) - NSC_REGION_SIZE) : (ALIGN(16384) - NSC_REGION_SIZE + 16384); + _start_sg = .; + } > FLASH + PROVIDE(_end_sg = .); + __exidx_end = .; __etext = .; diff --git a/hw/mcu/nordic/nrf5340/src/hal_system_start.c b/hw/mcu/nordic/nrf5340/src/hal_system_start.c index 153f38796..17753e1dc 100644 --- a/hw/mcu/nordic/nrf5340/src/hal_system_start.c +++ b/hw/mcu/nordic/nrf5340/src/hal_system_start.c @@ -66,6 +66,26 @@ static const unsigned int periph_gpios[] = { UNMANGLE_MYNEWT_VAL(MYNEWT_VAL(MCU_ #endif extern uint8_t __StackTop[]; +extern uint8_t _start_sg[]; +extern uint8_t _end_sg[]; + +static void +init_nsc(void) +{ + int region = (uintptr_t)_start_sg / 0x4000; + uintptr_t region_start = region * 0x4000; + uintptr_t region_limit = region_start + 0x4000; + int nsc_region_size = 32; + int m = 1; + /* Calculate NSC region size by checking _start_sg offset in last 16K region */ + while ((uintptr_t)_start_sg < region_limit - nsc_region_size) { + m++; + nsc_region_size <<= 1; + } + assert(m <= 8); + NRF_SPU_S->FLASHNSC[0].REGION = region; + NRF_SPU_S->FLASHNSC[0].SIZE = m; +} void hal_system_start(void *img_start) @@ -106,6 +126,10 @@ hal_system_start(void *img_start) NRF_SPU->FLASHREGION[i].PERM &= ~SPU_FLASHREGION_PERM_SECATTR_Msk; } + if ((uint32_t)_start_sg < (uint32_t)_end_sg) { + init_nsc(); + } + /* Mark RAM as non-secure */ for (i = 0; i < 64; ++i) { NRF_SPU->RAMREGION[i].PERM &= ~SPU_FLASHREGION_PERM_SECATTR_Msk;
