Skip to content

Commit

Permalink
mcu/nrf5340: Enable NSC region
Browse files Browse the repository at this point in the history
Enable NSC region if sgstubs section is not empty

Signed-off-by: Jerzy Kasenberg <[email protected]>
  • Loading branch information
kasjer committed Oct 31, 2024
1 parent f08817b commit 010af90
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
12 changes: 11 additions & 1 deletion hw/mcu/nordic/nrf5340/nrf5340.ld
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ OUTPUT_FORMAT ("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
*/
ENTRY(Reset_Handler)

NSC_REGION_SIZE = 0x200;

SECTIONS
{
.imghdr (NOLOAD):
Expand Down Expand Up @@ -107,7 +109,6 @@ INCLUDE "link_tables.ld.h"
. = ALIGN(4);
} > FLASH


.net_core_img :
{
PROVIDE(net_core_img_start = .);
Expand All @@ -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 = .;
Expand Down
25 changes: 25 additions & 0 deletions hw/mcu/nordic/nrf5340/src/hal_system_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ 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)
Expand Down Expand Up @@ -106,6 +127,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;
Expand Down

0 comments on commit 010af90

Please sign in to comment.