Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 1 | /* |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2024, STMicroelectronics - All Rights Reserved |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <limits.h> |
| 9 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 10 | #include <arch_helpers.h> |
| 11 | #include <common/debug.h> |
| 12 | #include <drivers/st/bsec.h> |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 13 | #include <drivers/st/bsec2_reg.h> |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 14 | #include <lib/mmio.h> |
| 15 | #include <lib/spinlock.h> |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 16 | #include <libfdt.h> |
| 17 | |
| 18 | #include <platform_def.h> |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 19 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 20 | #define BSEC_IP_VERSION_1_1 U(0x11) |
| 21 | #define BSEC_IP_VERSION_2_0 U(0x20) |
| 22 | #define BSEC_IP_ID_2 U(0x100032) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 23 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 24 | /* |
| 25 | * IP configuration |
| 26 | */ |
| 27 | #define BSEC_OTP_MASK GENMASK(4, 0) |
| 28 | #define BSEC_OTP_BANK_SHIFT 5 |
| 29 | #define BSEC_TIMEOUT_VALUE U(0xFFFF) |
| 30 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 31 | #define OTP_ACCESS_SIZE (round_up(OTP_MAX_SIZE, __WORD_BIT) / __WORD_BIT) |
| 32 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 33 | static uint32_t otp_nsec_access[OTP_ACCESS_SIZE] __maybe_unused; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 34 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 35 | static uint32_t bsec_shadow_register(uint32_t otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 36 | static uint32_t bsec_power_safmem(bool power); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 37 | static uint32_t bsec_get_version(void); |
| 38 | static uint32_t bsec_get_id(void); |
| 39 | static uint32_t bsec_get_status(void); |
| 40 | static uint32_t bsec_read_permanent_lock(uint32_t otp, bool *value); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 41 | |
| 42 | /* BSEC access protection */ |
| 43 | static spinlock_t bsec_spinlock; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 44 | |
| 45 | static void bsec_lock(void) |
| 46 | { |
Yann Gautier | f540a59 | 2019-05-22 19:13:51 +0200 | [diff] [blame] | 47 | if (stm32mp_lock_available()) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 48 | spin_lock(&bsec_spinlock); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static void bsec_unlock(void) |
| 53 | { |
Yann Gautier | f540a59 | 2019-05-22 19:13:51 +0200 | [diff] [blame] | 54 | if (stm32mp_lock_available()) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 55 | spin_unlock(&bsec_spinlock); |
| 56 | } |
| 57 | } |
| 58 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 59 | static bool is_otp_invalid_mode(void) |
| 60 | { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 61 | bool ret = ((bsec_get_status() & BSEC_OTP_STATUS_INVALID) == BSEC_OTP_STATUS_INVALID); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 62 | |
| 63 | if (ret) { |
| 64 | ERROR("OTP mode is OTP-INVALID\n"); |
| 65 | } |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | #if defined(IMAGE_BL32) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 71 | static int bsec_get_dt_node(struct dt_node_info *info) |
| 72 | { |
| 73 | int node; |
| 74 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 75 | node = dt_get_node(info, -1, DT_BSEC_COMPAT); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 76 | if (node < 0) { |
| 77 | return -FDT_ERR_NOTFOUND; |
| 78 | } |
| 79 | |
| 80 | return node; |
| 81 | } |
| 82 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 83 | static void enable_non_secure_access(uint32_t otp) |
| 84 | { |
| 85 | otp_nsec_access[otp / __WORD_BIT] |= BIT(otp % __WORD_BIT); |
| 86 | |
| 87 | if (bsec_shadow_register(otp) != BSEC_OK) { |
| 88 | panic(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static bool non_secure_can_access(uint32_t otp) |
| 93 | { |
| 94 | return (otp_nsec_access[otp / __WORD_BIT] & |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 95 | BIT(otp % __WORD_BIT)) != 0U; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 98 | static void bsec_dt_otp_nsec_access(void *fdt, int bsec_node) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 99 | { |
| 100 | int bsec_subnode; |
| 101 | |
| 102 | fdt_for_each_subnode(bsec_subnode, fdt, bsec_node) { |
| 103 | const fdt32_t *cuint; |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 104 | uint32_t otp; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 105 | uint32_t i; |
| 106 | uint32_t size; |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 107 | uint32_t offset; |
| 108 | uint32_t length; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 109 | |
| 110 | cuint = fdt_getprop(fdt, bsec_subnode, "reg", NULL); |
| 111 | if (cuint == NULL) { |
| 112 | panic(); |
| 113 | } |
| 114 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 115 | offset = fdt32_to_cpu(*cuint); |
| 116 | cuint++; |
| 117 | length = fdt32_to_cpu(*cuint); |
| 118 | |
| 119 | otp = offset / sizeof(uint32_t); |
| 120 | |
| 121 | if (otp < STM32MP1_UPPER_OTP_START) { |
| 122 | unsigned int otp_end = round_up(offset + length, |
| 123 | sizeof(uint32_t)) / |
| 124 | sizeof(uint32_t); |
| 125 | |
| 126 | if (otp_end > STM32MP1_UPPER_OTP_START) { |
| 127 | /* |
| 128 | * OTP crosses Lower/Upper boundary, consider |
| 129 | * only the upper part. |
| 130 | */ |
| 131 | otp = STM32MP1_UPPER_OTP_START; |
| 132 | length -= (STM32MP1_UPPER_OTP_START * |
| 133 | sizeof(uint32_t)) - offset; |
| 134 | offset = STM32MP1_UPPER_OTP_START * |
| 135 | sizeof(uint32_t); |
| 136 | |
| 137 | WARN("OTP crosses Lower/Upper boundary\n"); |
| 138 | } else { |
| 139 | continue; |
| 140 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 143 | if ((fdt_getprop(fdt, bsec_subnode, |
| 144 | "st,non-secure-otp", NULL)) == NULL) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 145 | continue; |
| 146 | } |
| 147 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 148 | if (((offset % sizeof(uint32_t)) != 0U) || |
| 149 | ((length % sizeof(uint32_t)) != 0U)) { |
| 150 | ERROR("Unaligned non-secure OTP\n"); |
| 151 | panic(); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 154 | size = length / sizeof(uint32_t); |
| 155 | |
| 156 | for (i = otp; i < (otp + size); i++) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 157 | enable_non_secure_access(i); |
| 158 | } |
| 159 | } |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void bsec_late_init(void) |
| 163 | { |
| 164 | void *fdt; |
| 165 | int node; |
| 166 | struct dt_node_info bsec_info; |
| 167 | |
| 168 | if (fdt_get_address(&fdt) == 0) { |
Yann Gautier | 0f2bc61 | 2023-11-02 15:36:12 +0100 | [diff] [blame^] | 169 | EARLY_ERROR("%s: DT not found\n", __func__); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 170 | panic(); |
| 171 | } |
| 172 | |
| 173 | node = bsec_get_dt_node(&bsec_info); |
| 174 | if (node < 0) { |
Yann Gautier | 0f2bc61 | 2023-11-02 15:36:12 +0100 | [diff] [blame^] | 175 | EARLY_ERROR("%s: BSEC node not found\n", __func__); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 176 | panic(); |
| 177 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 178 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 179 | assert(bsec_info.base == BSEC_BASE); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 180 | |
| 181 | bsec_dt_otp_nsec_access(fdt, node); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 182 | } |
| 183 | #endif |
| 184 | |
| 185 | static uint32_t otp_bank_offset(uint32_t otp) |
| 186 | { |
| 187 | assert(otp <= STM32MP1_OTP_MAX_ID); |
| 188 | |
| 189 | return ((otp & ~BSEC_OTP_MASK) >> BSEC_OTP_BANK_SHIFT) * |
| 190 | sizeof(uint32_t); |
| 191 | } |
| 192 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 193 | static uint32_t otp_bit_mask(uint32_t otp) |
| 194 | { |
| 195 | return BIT(otp & BSEC_OTP_MASK); |
| 196 | } |
| 197 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 198 | /* |
| 199 | * bsec_check_error: check BSEC error status. |
| 200 | * otp: OTP number. |
| 201 | * check_disturbed: check only error (false), |
| 202 | * or error and disturbed status (true). |
| 203 | * return value: BSEC_OK if no error. |
| 204 | */ |
| 205 | static uint32_t bsec_check_error(uint32_t otp, bool check_disturbed) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 206 | { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 207 | uint32_t bit = otp_bit_mask(otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 208 | uint32_t bank = otp_bank_offset(otp); |
| 209 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 210 | if ((mmio_read_32(BSEC_BASE + BSEC_ERROR_OFF + bank) & bit) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 211 | return BSEC_ERROR; |
| 212 | } |
| 213 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 214 | if (!check_disturbed) { |
| 215 | return BSEC_OK; |
| 216 | } |
| 217 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 218 | if ((mmio_read_32(BSEC_BASE + BSEC_DISTURBED_OFF + bank) & bit) != 0U) { |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 219 | return BSEC_DISTURBED; |
| 220 | } |
| 221 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 222 | return BSEC_OK; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * bsec_probe: initialize BSEC driver. |
| 227 | * return value: BSEC_OK if no error. |
| 228 | */ |
| 229 | uint32_t bsec_probe(void) |
| 230 | { |
Yann Gautier | 0f2bc61 | 2023-11-02 15:36:12 +0100 | [diff] [blame^] | 231 | uint32_t version; |
| 232 | uint32_t id; |
| 233 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 234 | if (is_otp_invalid_mode()) { |
Yann Gautier | 0f2bc61 | 2023-11-02 15:36:12 +0100 | [diff] [blame^] | 235 | EARLY_ERROR("%s: otp_invalid_mod\n", __func__); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 236 | return BSEC_ERROR; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Yann Gautier | 0f2bc61 | 2023-11-02 15:36:12 +0100 | [diff] [blame^] | 239 | version = bsec_get_version(); |
| 240 | id = bsec_get_id(); |
| 241 | |
| 242 | if (((version != BSEC_IP_VERSION_1_1) && |
| 243 | (version != BSEC_IP_VERSION_2_0)) || |
| 244 | (id != BSEC_IP_ID_2)) { |
| 245 | EARLY_ERROR("%s: version = 0x%x, id = 0x%x\n", __func__, version, id); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 246 | panic(); |
| 247 | } |
| 248 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 249 | #if defined(IMAGE_BL32) |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 250 | bsec_late_init(); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 251 | #endif |
| 252 | return BSEC_OK; |
| 253 | } |
| 254 | |
| 255 | /* |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 256 | * bsec_shadow_register: copy SAFMEM OTP to BSEC data. |
| 257 | * otp: OTP number. |
| 258 | * return value: BSEC_OK if no error. |
| 259 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 260 | static uint32_t bsec_shadow_register(uint32_t otp) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 261 | { |
| 262 | uint32_t result; |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 263 | bool value; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 264 | bool power_up = false; |
| 265 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 266 | if (is_otp_invalid_mode()) { |
| 267 | return BSEC_ERROR; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 270 | result = bsec_read_sr_lock(otp, &value); |
| 271 | if (result != BSEC_OK) { |
| 272 | ERROR("BSEC: %u Sticky-read bit read Error %u\n", otp, result); |
| 273 | return result; |
| 274 | } |
| 275 | |
| 276 | if (value) { |
| 277 | VERBOSE("BSEC: OTP %u is locked and will not be refreshed\n", |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 278 | otp); |
| 279 | } |
| 280 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 281 | if ((bsec_get_status() & BSEC_OTP_STATUS_PWRON) == 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 282 | result = bsec_power_safmem(true); |
| 283 | |
| 284 | if (result != BSEC_OK) { |
| 285 | return result; |
| 286 | } |
| 287 | |
| 288 | power_up = true; |
| 289 | } |
| 290 | |
| 291 | bsec_lock(); |
| 292 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 293 | mmio_write_32(BSEC_BASE + BSEC_OTP_CTRL_OFF, otp | BSEC_READ); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 294 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 295 | while ((bsec_get_status() & BSEC_OTP_STATUS_BUSY) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 296 | ; |
| 297 | } |
| 298 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 299 | result = bsec_check_error(otp, true); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 300 | |
| 301 | bsec_unlock(); |
| 302 | |
| 303 | if (power_up) { |
| 304 | if (bsec_power_safmem(false) != BSEC_OK) { |
| 305 | panic(); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return result; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * bsec_read_otp: read an OTP data value. |
| 314 | * val: read value. |
| 315 | * otp: OTP number. |
| 316 | * return value: BSEC_OK if no error. |
| 317 | */ |
| 318 | uint32_t bsec_read_otp(uint32_t *val, uint32_t otp) |
| 319 | { |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 320 | if (is_otp_invalid_mode()) { |
| 321 | return BSEC_ERROR; |
| 322 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 323 | |
| 324 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 325 | return BSEC_INVALID_PARAM; |
| 326 | } |
| 327 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 328 | *val = mmio_read_32(BSEC_BASE + BSEC_OTP_DATA_OFF + |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 329 | (otp * sizeof(uint32_t))); |
| 330 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 331 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* |
| 335 | * bsec_write_otp: write value in BSEC data register. |
| 336 | * val: value to write. |
| 337 | * otp: OTP number. |
| 338 | * return value: BSEC_OK if no error. |
| 339 | */ |
| 340 | uint32_t bsec_write_otp(uint32_t val, uint32_t otp) |
| 341 | { |
| 342 | uint32_t result; |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 343 | bool value; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 344 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 345 | if (is_otp_invalid_mode()) { |
| 346 | return BSEC_ERROR; |
| 347 | } |
| 348 | |
| 349 | result = bsec_read_sw_lock(otp, &value); |
| 350 | if (result != BSEC_OK) { |
| 351 | ERROR("BSEC: %u Sticky-write bit read Error %u\n", otp, result); |
| 352 | return result; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 355 | if (value) { |
| 356 | VERBOSE("BSEC: OTP %u is locked and write will be ignored\n", |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 357 | otp); |
| 358 | } |
| 359 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 360 | /* Ensure integrity of each register access sequence */ |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 361 | bsec_lock(); |
| 362 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 363 | mmio_write_32(BSEC_BASE + BSEC_OTP_DATA_OFF + |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 364 | (otp * sizeof(uint32_t)), val); |
| 365 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 366 | bsec_unlock(); |
| 367 | |
| 368 | return result; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * bsec_program_otp: program a bit in SAFMEM after the prog. |
| 373 | * The OTP data is not refreshed. |
| 374 | * val: value to program. |
| 375 | * otp: OTP number. |
| 376 | * return value: BSEC_OK if no error. |
| 377 | */ |
| 378 | uint32_t bsec_program_otp(uint32_t val, uint32_t otp) |
| 379 | { |
| 380 | uint32_t result; |
| 381 | bool power_up = false; |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 382 | bool sp_lock; |
| 383 | bool perm_lock; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 384 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 385 | if (is_otp_invalid_mode()) { |
| 386 | return BSEC_ERROR; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 389 | result = bsec_read_sp_lock(otp, &sp_lock); |
| 390 | if (result != BSEC_OK) { |
| 391 | ERROR("BSEC: %u Sticky-prog bit read Error %u\n", otp, result); |
| 392 | return result; |
| 393 | } |
| 394 | |
| 395 | result = bsec_read_permanent_lock(otp, &perm_lock); |
| 396 | if (result != BSEC_OK) { |
| 397 | ERROR("BSEC: %u permanent bit read Error %u\n", otp, result); |
| 398 | return result; |
| 399 | } |
| 400 | |
| 401 | if (sp_lock || perm_lock) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 402 | WARN("BSEC: OTP locked, prog will be ignored\n"); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 403 | return BSEC_PROG_FAIL; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 406 | if ((mmio_read_32(BSEC_BASE + BSEC_OTP_LOCK_OFF) & GPLOCK_LOCK_MASK) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 407 | WARN("BSEC: GPLOCK activated, prog will be ignored\n"); |
| 408 | } |
| 409 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 410 | if ((bsec_get_status() & BSEC_OTP_STATUS_PWRON) == 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 411 | result = bsec_power_safmem(true); |
| 412 | |
| 413 | if (result != BSEC_OK) { |
| 414 | return result; |
| 415 | } |
| 416 | |
| 417 | power_up = true; |
| 418 | } |
| 419 | |
| 420 | bsec_lock(); |
| 421 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 422 | mmio_write_32(BSEC_BASE + BSEC_OTP_WRDATA_OFF, val); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 423 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 424 | mmio_write_32(BSEC_BASE + BSEC_OTP_CTRL_OFF, otp | BSEC_WRITE); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 425 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 426 | while ((bsec_get_status() & BSEC_OTP_STATUS_BUSY) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 427 | ; |
| 428 | } |
| 429 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 430 | if ((bsec_get_status() & BSEC_OTP_STATUS_PROGFAIL) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 431 | result = BSEC_PROG_FAIL; |
| 432 | } else { |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 433 | result = bsec_check_error(otp, true); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | bsec_unlock(); |
| 437 | |
| 438 | if (power_up) { |
| 439 | if (bsec_power_safmem(false) != BSEC_OK) { |
| 440 | panic(); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return result; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * bsec_permanent_lock_otp: permanent lock of OTP in SAFMEM. |
| 449 | * otp: OTP number. |
| 450 | * return value: BSEC_OK if no error. |
| 451 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 452 | #if defined(IMAGE_BL32) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 453 | uint32_t bsec_permanent_lock_otp(uint32_t otp) |
| 454 | { |
| 455 | uint32_t result; |
| 456 | bool power_up = false; |
| 457 | uint32_t data; |
| 458 | uint32_t addr; |
| 459 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 460 | if (is_otp_invalid_mode()) { |
| 461 | return BSEC_ERROR; |
| 462 | } |
| 463 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 464 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 465 | return BSEC_INVALID_PARAM; |
| 466 | } |
| 467 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 468 | if ((bsec_get_status() & BSEC_OTP_STATUS_PWRON) == 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 469 | result = bsec_power_safmem(true); |
| 470 | |
| 471 | if (result != BSEC_OK) { |
| 472 | return result; |
| 473 | } |
| 474 | |
| 475 | power_up = true; |
| 476 | } |
| 477 | |
| 478 | if (otp < STM32MP1_UPPER_OTP_START) { |
| 479 | addr = otp >> ADDR_LOWER_OTP_PERLOCK_SHIFT; |
| 480 | data = DATA_LOWER_OTP_PERLOCK_BIT << |
| 481 | ((otp & DATA_LOWER_OTP_PERLOCK_MASK) << 1U); |
| 482 | } else { |
| 483 | addr = (otp >> ADDR_UPPER_OTP_PERLOCK_SHIFT) + 2U; |
| 484 | data = DATA_UPPER_OTP_PERLOCK_BIT << |
| 485 | (otp & DATA_UPPER_OTP_PERLOCK_MASK); |
| 486 | } |
| 487 | |
| 488 | bsec_lock(); |
| 489 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 490 | mmio_write_32(BSEC_BASE + BSEC_OTP_WRDATA_OFF, data); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 491 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 492 | mmio_write_32(BSEC_BASE + BSEC_OTP_CTRL_OFF, |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 493 | addr | BSEC_WRITE | BSEC_LOCK); |
| 494 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 495 | while ((bsec_get_status() & BSEC_OTP_STATUS_BUSY) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 496 | ; |
| 497 | } |
| 498 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 499 | if ((bsec_get_status() & BSEC_OTP_STATUS_PROGFAIL) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 500 | result = BSEC_PROG_FAIL; |
| 501 | } else { |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 502 | result = bsec_check_error(otp, false); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | bsec_unlock(); |
| 506 | |
| 507 | if (power_up) { |
| 508 | if (bsec_power_safmem(false) != BSEC_OK) { |
| 509 | panic(); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return result; |
| 514 | } |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 515 | #endif |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 516 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 517 | /* |
| 518 | * bsec_read_debug_conf: return debug configuration register value. |
| 519 | */ |
| 520 | uint32_t bsec_read_debug_conf(void) |
| 521 | { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 522 | return mmio_read_32(BSEC_BASE + BSEC_DEN_OFF); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 523 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 524 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 525 | /* |
| 526 | * bsec_write_scratch: write value in scratch register. |
| 527 | * val: value to write. |
| 528 | * return value: none. |
| 529 | */ |
| 530 | void bsec_write_scratch(uint32_t val) |
| 531 | { |
| 532 | #if defined(IMAGE_BL32) |
| 533 | if (is_otp_invalid_mode()) { |
| 534 | return; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 535 | } |
| 536 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 537 | bsec_lock(); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 538 | mmio_write_32(BSEC_BASE + BSEC_SCRATCH_OFF, val); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 539 | bsec_unlock(); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 540 | #else |
| 541 | mmio_write_32(BSEC_BASE + BSEC_SCRATCH_OFF, val); |
| 542 | #endif |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 546 | * bsec_get_status: return status register value. |
| 547 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 548 | static uint32_t bsec_get_status(void) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 549 | { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 550 | return mmio_read_32(BSEC_BASE + BSEC_OTP_STATUS_OFF); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 554 | * bsec_get_version: return BSEC version register value. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 555 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 556 | static uint32_t bsec_get_version(void) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 557 | { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 558 | return mmio_read_32(BSEC_BASE + BSEC_IPVR_OFF) & BSEC_IPVR_MSK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 562 | * bsec_get_id: return BSEC ID register value. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 563 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 564 | static uint32_t bsec_get_id(void) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 565 | { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 566 | return mmio_read_32(BSEC_BASE + BSEC_IP_ID_OFF); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 570 | * bsec_set_sr_lock: set shadow-read lock. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 571 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 572 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 573 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 574 | uint32_t bsec_set_sr_lock(uint32_t otp) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 575 | { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 576 | uint32_t bank = otp_bank_offset(otp); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 577 | uint32_t otp_mask = otp_bit_mask(otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 578 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 579 | if (is_otp_invalid_mode()) { |
| 580 | return BSEC_ERROR; |
| 581 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 582 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 583 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 584 | return BSEC_INVALID_PARAM; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 587 | bsec_lock(); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 588 | mmio_write_32(BSEC_BASE + BSEC_SRLOCK_OFF + bank, otp_mask); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 589 | bsec_unlock(); |
| 590 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 591 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* |
| 595 | * bsec_read_sr_lock: read shadow-read lock. |
| 596 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 597 | * value: read value (true or false). |
| 598 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 599 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 600 | uint32_t bsec_read_sr_lock(uint32_t otp, bool *value) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 601 | { |
| 602 | uint32_t bank = otp_bank_offset(otp); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 603 | uint32_t otp_mask = otp_bit_mask(otp); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 604 | uint32_t bank_value; |
| 605 | |
| 606 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 607 | return BSEC_INVALID_PARAM; |
| 608 | } |
| 609 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 610 | bank_value = mmio_read_32(BSEC_BASE + BSEC_SRLOCK_OFF + bank); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 611 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 612 | *value = ((bank_value & otp_mask) != 0U); |
| 613 | |
| 614 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 618 | * bsec_set_sw_lock: set shadow-write lock. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 619 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 620 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 621 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 622 | uint32_t bsec_set_sw_lock(uint32_t otp) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 623 | { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 624 | uint32_t bank = otp_bank_offset(otp); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 625 | uint32_t otp_mask = otp_bit_mask(otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 626 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 627 | if (is_otp_invalid_mode()) { |
| 628 | return BSEC_ERROR; |
| 629 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 630 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 631 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 632 | return BSEC_INVALID_PARAM; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 633 | } |
| 634 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 635 | bsec_lock(); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 636 | mmio_write_32(BSEC_BASE + BSEC_SWLOCK_OFF + bank, otp_mask); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 637 | bsec_unlock(); |
| 638 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 639 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* |
| 643 | * bsec_read_sw_lock: read shadow-write lock. |
| 644 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 645 | * value: read value (true or false). |
| 646 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 647 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 648 | uint32_t bsec_read_sw_lock(uint32_t otp, bool *value) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 649 | { |
| 650 | uint32_t bank = otp_bank_offset(otp); |
| 651 | uint32_t otp_mask = BIT(otp & BSEC_OTP_MASK); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 652 | uint32_t bank_value; |
| 653 | |
| 654 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 655 | return BSEC_INVALID_PARAM; |
| 656 | } |
| 657 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 658 | bank_value = mmio_read_32(BSEC_BASE + BSEC_SWLOCK_OFF + bank); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 659 | |
| 660 | *value = ((bank_value & otp_mask) != 0U); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 661 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 662 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 666 | * bsec_set_sp_lock: set shadow-program lock. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 667 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 668 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 669 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 670 | uint32_t bsec_set_sp_lock(uint32_t otp) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 671 | { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 672 | uint32_t bank = otp_bank_offset(otp); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 673 | uint32_t otp_mask = otp_bit_mask(otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 674 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 675 | if (is_otp_invalid_mode()) { |
| 676 | return BSEC_ERROR; |
| 677 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 678 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 679 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 680 | return BSEC_INVALID_PARAM; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 683 | bsec_lock(); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 684 | mmio_write_32(BSEC_BASE + BSEC_SPLOCK_OFF + bank, otp_mask); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 685 | bsec_unlock(); |
| 686 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 687 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /* |
| 691 | * bsec_read_sp_lock: read shadow-program lock. |
| 692 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 693 | * value: read value (true or false). |
| 694 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 695 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 696 | uint32_t bsec_read_sp_lock(uint32_t otp, bool *value) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 697 | { |
| 698 | uint32_t bank = otp_bank_offset(otp); |
| 699 | uint32_t otp_mask = BIT(otp & BSEC_OTP_MASK); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 700 | uint32_t bank_value; |
| 701 | |
| 702 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 703 | return BSEC_INVALID_PARAM; |
| 704 | } |
| 705 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 706 | bank_value = mmio_read_32(BSEC_BASE + BSEC_SPLOCK_OFF + bank); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 707 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 708 | *value = ((bank_value & otp_mask) != 0U); |
| 709 | |
| 710 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 714 | * bsec_read_permanent_lock: Read permanent lock status. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 715 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 716 | * value: read value (true or false). |
| 717 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 718 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 719 | static uint32_t bsec_read_permanent_lock(uint32_t otp, bool *value) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 720 | { |
| 721 | uint32_t bank = otp_bank_offset(otp); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 722 | uint32_t otp_mask = otp_bit_mask(otp); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 723 | uint32_t bank_value; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 724 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 725 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 726 | return BSEC_INVALID_PARAM; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 727 | } |
| 728 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 729 | bank_value = mmio_read_32(BSEC_BASE + BSEC_WRLOCK_OFF + bank); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 730 | |
| 731 | *value = ((bank_value & otp_mask) != 0U); |
| 732 | |
| 733 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 737 | * bsec_power_safmem: Activate or deactivate SAFMEM power. |
| 738 | * power: true to power up, false to power down. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 739 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 740 | */ |
| 741 | static uint32_t bsec_power_safmem(bool power) |
| 742 | { |
| 743 | uint32_t register_val; |
| 744 | uint32_t timeout = BSEC_TIMEOUT_VALUE; |
| 745 | |
| 746 | bsec_lock(); |
| 747 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 748 | register_val = mmio_read_32(BSEC_BASE + BSEC_OTP_CONF_OFF); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 749 | |
| 750 | if (power) { |
| 751 | register_val |= BSEC_CONF_POWER_UP_MASK; |
| 752 | } else { |
| 753 | register_val &= ~BSEC_CONF_POWER_UP_MASK; |
| 754 | } |
| 755 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 756 | mmio_write_32(BSEC_BASE + BSEC_OTP_CONF_OFF, register_val); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 757 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 758 | if (power) { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 759 | while (((bsec_get_status() & BSEC_OTP_STATUS_PWRON) == 0U) && |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 760 | (timeout != 0U)) { |
| 761 | timeout--; |
| 762 | } |
| 763 | } else { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 764 | while (((bsec_get_status() & BSEC_OTP_STATUS_PWRON) != 0U) && |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 765 | (timeout != 0U)) { |
| 766 | timeout--; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | bsec_unlock(); |
| 771 | |
| 772 | if (timeout == 0U) { |
| 773 | return BSEC_TIMEOUT; |
| 774 | } |
| 775 | |
| 776 | return BSEC_OK; |
| 777 | } |
| 778 | |
| 779 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 780 | * bsec_shadow_read_otp: Load OTP from SAFMEM and provide its value. |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 781 | * val: read value. |
| 782 | * otp: OTP number. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 783 | * return value: BSEC_OK if no error. |
| 784 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 785 | uint32_t bsec_shadow_read_otp(uint32_t *val, uint32_t otp) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 786 | { |
| 787 | uint32_t result; |
| 788 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 789 | result = bsec_shadow_register(otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 790 | if (result != BSEC_OK) { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 791 | ERROR("BSEC: %u Shadowing Error %u\n", otp, result); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 792 | return result; |
| 793 | } |
| 794 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 795 | result = bsec_read_otp(val, otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 796 | if (result != BSEC_OK) { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 797 | ERROR("BSEC: %u Read Error %u\n", otp, result); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | return result; |
| 801 | } |
| 802 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 803 | #if defined(IMAGE_BL32) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 804 | /* |
| 805 | * bsec_check_nsec_access_rights: check non-secure access rights to target OTP. |
| 806 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 807 | * return value: BSEC_OK if authorized access. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 808 | */ |
| 809 | uint32_t bsec_check_nsec_access_rights(uint32_t otp) |
| 810 | { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 811 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 812 | return BSEC_INVALID_PARAM; |
| 813 | } |
| 814 | |
| 815 | if (otp >= STM32MP1_UPPER_OTP_START) { |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 816 | if (!non_secure_can_access(otp)) { |
| 817 | return BSEC_ERROR; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 818 | } |
| 819 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 820 | |
| 821 | return BSEC_OK; |
| 822 | } |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 823 | #endif |
| 824 | |
| 825 | uint32_t bsec_get_secure_state(void) |
| 826 | { |
| 827 | uint32_t status = bsec_get_status(); |
| 828 | uint32_t result = BSEC_STATE_INVALID; |
| 829 | uint32_t otp_enc_id __maybe_unused; |
| 830 | uint32_t otp_bit_len __maybe_unused; |
| 831 | int res __maybe_unused; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 832 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 833 | if ((status & BSEC_OTP_STATUS_INVALID) != 0U) { |
| 834 | result = BSEC_STATE_INVALID; |
| 835 | } else { |
| 836 | if ((status & BSEC_OTP_STATUS_SECURE) != 0U) { |
Yann Gautier | 3e33475 | 2023-02-01 15:04:30 +0100 | [diff] [blame] | 837 | if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 838 | result = BSEC_STATE_SEC_CLOSED; |
| 839 | } else { |
| 840 | result = BSEC_STATE_SEC_OPEN; |
| 841 | } |
| 842 | } else { |
| 843 | /* OTP modes OPEN1 and OPEN2 are not supported */ |
| 844 | result = BSEC_STATE_INVALID; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | return result; |
| 849 | } |