ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
trustzone_init.c
Go to the documentation of this file.
1
57
58#include "trustzone_init.h"
59
60#include <stdint.h>
61
62#include "ra8_boot_intrinsics.h"
63
64#ifdef RA8_TRUSTZONE_ENABLE
65
66/* =============================================================================
67 * SAU register addresses (System Control Space, secure alias)
68 * =============================================================================
69 */
70
72typedef enum : uint32_t {
73 k_sau_sregion_mask = 0xFFU,
74} sau_type_mask_t;
75
76typedef enum : uintptr_t {
77 k_ra8_sau_ctrl_addr = 0xE000EDD0UL,
78 k_ra8_sau_type_addr = 0xE000EDD4UL,
79 k_ra8_sau_rnr_addr = 0xE000EDD8UL,
80 k_ra8_sau_rbar_addr = 0xE000EDDCUL,
81 k_ra8_sau_rlar_addr = 0xE000EDE0UL,
82 k_ra8_sfsr_addr = 0xE000EDE4UL,
83} ra8_tz_sau_addr_t;
84
89typedef enum : uint32_t {
90 k_ra8_sau_ctrl_enable = 1UL << 0,
91 k_ra8_sau_ctrl_allns = 1UL << 1,
92} ra8_tz_sau_ctrl_bit_t;
93
98typedef enum : uint32_t {
99 k_ra8_sau_rlar_enable = 1UL << 0,
100 k_ra8_sau_rlar_nsc = 1UL << 1,
101} ra8_tz_sau_rlar_bit_t;
102
111typedef enum : uint32_t {
112 k_ra8_tz_ns_mram_base = 0x02080000UL,
113 k_ra8_tz_ns_mram_limit = 0x020FFFE0UL,
114 k_ra8_tz_ns_sram_base = 0x22100000UL,
115 k_ra8_tz_ns_sram_limit = 0x221FFFE0UL,
116 k_ra8_tz_ns_sdram_base = 0x6A000000UL,
117 k_ra8_tz_ns_sdram_limit = 0x6BFFFFE0UL,
118 k_ra8_tz_nsc_veneer_base = 0x10000000UL,
119 k_ra8_tz_nsc_veneer_lim = 0x100FFFE0UL,
120} ra8_tz_partition_t;
121
122/* =============================================================================
123 * Internal helpers
124 * =============================================================================
125 */
126
143static void internal_sau_set_region(uint32_t region, uint32_t base, uint32_t limit, bool is_nsc)
144{
145 ra8_boot_write32(k_ra8_sau_rnr_addr, region);
146 ra8_boot_write32(k_ra8_sau_rbar_addr, base);
147 uint32_t rlar = limit | (uint32_t)k_ra8_sau_rlar_enable;
148 if (is_nsc) {
149 rlar |= (uint32_t)k_ra8_sau_rlar_nsc;
150 }
151 ra8_boot_write32(k_ra8_sau_rlar_addr, rlar);
152}
153
154/* =============================================================================
155 * Public entry point
156 * =============================================================================
157 */
158
159#endif /* RA8_TRUSTZONE_ENABLE */
160
162{
163#ifdef RA8_TRUSTZONE_ENABLE
164 /* Sanity check: SAU_TYPE.SREGION must report >= 4 implemented
165 * regions for our partition to fit. The Cortex-M85 always has 8,
166 * but a chip-specific override could trim the count. */
167 const uint32_t sau_type = ra8_boot_read32(k_ra8_sau_type_addr);
168 if ((sau_type & k_sau_sregion_mask) < 4U) {
169 /* Refuse to bring up TrustZone on an SAU we cannot use. The
170 * caller will see SAU_CTRL.ENABLE clear and fall back to the
171 * single-world model. */
172 return;
173 }
174
175 /* Region 0: NS upper MRAM */
177 (uint32_t)k_ra8_tz_ns_mram_base,
178 (uint32_t)k_ra8_tz_ns_mram_limit,
179 /*is_nsc=*/false);
180
181 /* Region 1: NS upper SRAM */
183 (uint32_t)k_ra8_tz_ns_sram_base,
184 (uint32_t)k_ra8_tz_ns_sram_limit,
185 /*is_nsc=*/false);
186
187 /* Region 2: NS upper SDRAM */
189 (uint32_t)k_ra8_tz_ns_sdram_base,
190 (uint32_t)k_ra8_tz_ns_sdram_limit,
191 /*is_nsc=*/false);
192
193 /* Region 3: NSC veneer alias ( will place .gnu.sgstubs
194 * here via the linker script). */
196 (uint32_t)k_ra8_tz_nsc_veneer_base,
197 (uint32_t)k_ra8_tz_nsc_veneer_lim,
198 /*is_nsc=*/true);
199
200 /* Enable the SAU. Leave ALLNS clear: anything we have not
201 * explicitly carved out stays secure (default-deny). */
202 ra8_boot_dsb();
203 ra8_boot_write32(k_ra8_sau_ctrl_addr, (uint32_t)k_ra8_sau_ctrl_enable);
204 ra8_boot_dsb();
205 ra8_boot_isb();
206#endif
207}
@ k_ra8_sfsr_addr
SecureFault Status Register.
void ra8_trustzone_init(void)
Programme + enable the SAU per the partition.
static uint32_t ra8_boot_read32(uintptr_t addr)
Volatile 32-bit MMIO read.
static void ra8_boot_write32(uintptr_t addr, uint32_t value)
Volatile 32-bit MMIO write.
static void ra8_boot_isb(void)
Instruction synchronisation barrier (no-op under RA8_OFF_TARGET).
static void ra8_boot_dsb(void)
Data synchronisation barrier (no-op under RA8_OFF_TARGET).
static void internal_sau_set_region(uint8_t region, uint32_t base, uint32_t limit, bool is_nsc)
Programme one SAU region via RNR/RBAR/RLAR.