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
62
63#include "trustzone_init.h"
64
65#include <stdint.h>
66
67#include "ra8_boot_intrinsics.h"
68
69#ifdef RA8_TRUSTZONE_ENABLE
70
71/* =============================================================================
72 * SAU register addresses (System Control Space, secure alias)
73 * =============================================================================
74 */
75
77typedef enum : uint32_t {
78 k_sau_sregion_mask = 0xFFU,
79} sau_type_mask_t;
80
81typedef enum : uintptr_t {
82 k_ra8_sau_ctrl_addr = 0xE000EDD0UL,
83 k_ra8_sau_type_addr = 0xE000EDD4UL,
84 k_ra8_sau_rnr_addr = 0xE000EDD8UL,
85 k_ra8_sau_rbar_addr = 0xE000EDDCUL,
86 k_ra8_sau_rlar_addr = 0xE000EDE0UL,
87 k_ra8_sfsr_addr = 0xE000EDE4UL,
88} ra8_tz_sau_addr_t;
89
94typedef enum : uint32_t {
95 k_ra8_sau_ctrl_enable = 1UL << 0,
96 k_ra8_sau_ctrl_allns = 1UL << 1,
97} ra8_tz_sau_ctrl_bit_t;
98
103typedef enum : uint32_t {
104 k_ra8_sau_rlar_enable = 1UL << 0,
105 k_ra8_sau_rlar_nsc = 1UL << 1,
106} ra8_tz_sau_rlar_bit_t;
107
116typedef enum : uint32_t {
117 k_ra8_tz_ns_mram_base = 0x02080000UL,
118 k_ra8_tz_ns_mram_limit = 0x020FFFE0UL,
119 k_ra8_tz_ns_sram_base = 0x22100000UL,
120 k_ra8_tz_ns_sram_limit = 0x221FFFE0UL,
121 k_ra8_tz_ns_sdram_base = 0x6A000000UL,
122 k_ra8_tz_ns_sdram_limit = 0x6BFFFFE0UL,
123 k_ra8_tz_nsc_veneer_base = 0x10000000UL,
124 k_ra8_tz_nsc_veneer_lim = 0x100FFFE0UL,
125} ra8_tz_partition_t;
126
127/* =============================================================================
128 * Internal helpers
129 * =============================================================================
130 */
131
148static void internal_sau_set_region(uint32_t region, uint32_t base, uint32_t limit, bool is_nsc)
149{
150 ra8_boot_write32(k_ra8_sau_rnr_addr, region);
151 ra8_boot_write32(k_ra8_sau_rbar_addr, base);
152 uint32_t rlar = limit | (uint32_t)k_ra8_sau_rlar_enable;
153 if (is_nsc) {
154 rlar |= (uint32_t)k_ra8_sau_rlar_nsc;
155 }
156 ra8_boot_write32(k_ra8_sau_rlar_addr, rlar);
157}
158
159/* =============================================================================
160 * Public entry point
161 * =============================================================================
162 */
163
164#endif /* RA8_TRUSTZONE_ENABLE */
165
167{
168#ifdef RA8_TRUSTZONE_ENABLE
169 /* Sanity check: SAU_TYPE.SREGION must report >= 4 implemented
170 * regions for our partition to fit. The Cortex-M85 always has 8,
171 * but a chip-specific override could trim the count. */
172 const uint32_t sau_type = ra8_boot_read32(k_ra8_sau_type_addr);
173 if ((sau_type & k_sau_sregion_mask) < 4U) {
174 /* Refuse to bring up TrustZone on an SAU we cannot use. The
175 * caller will see SAU_CTRL.ENABLE clear and fall back to the
176 * single-world model. */
177 return;
178 }
179
180 /* Region 0: NS upper MRAM */
182 (uint32_t)k_ra8_tz_ns_mram_base,
183 (uint32_t)k_ra8_tz_ns_mram_limit,
184 /*is_nsc=*/false);
185
186 /* Region 1: NS upper SRAM */
188 (uint32_t)k_ra8_tz_ns_sram_base,
189 (uint32_t)k_ra8_tz_ns_sram_limit,
190 /*is_nsc=*/false);
191
192 /* Region 2: NS upper SDRAM */
194 (uint32_t)k_ra8_tz_ns_sdram_base,
195 (uint32_t)k_ra8_tz_ns_sdram_limit,
196 /*is_nsc=*/false);
197
198 /* Region 3: NSC veneer alias ( will place .gnu.sgstubs
199 * here via the linker script). */
201 (uint32_t)k_ra8_tz_nsc_veneer_base,
202 (uint32_t)k_ra8_tz_nsc_veneer_lim,
203 /*is_nsc=*/true);
204
205 /* Enable the SAU. Leave ALLNS clear: anything we have not
206 * explicitly carved out stays secure (default-deny). */
207 ra8_boot_dsb();
208 ra8_boot_write32(k_ra8_sau_ctrl_addr, (uint32_t)k_ra8_sau_ctrl_enable);
209 ra8_boot_dsb();
210 ra8_boot_isb();
211#endif
212}
@ 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.