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) { |
| 169 | panic(); |
| 170 | } |
| 171 | |
| 172 | node = bsec_get_dt_node(&bsec_info); |
| 173 | if (node < 0) { |
| 174 | panic(); |
| 175 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 176 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 177 | assert(bsec_info.base == BSEC_BASE); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 178 | |
| 179 | bsec_dt_otp_nsec_access(fdt, node); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 180 | } |
| 181 | #endif |
| 182 | |
| 183 | static uint32_t otp_bank_offset(uint32_t otp) |
| 184 | { |
| 185 | assert(otp <= STM32MP1_OTP_MAX_ID); |
| 186 | |
| 187 | return ((otp & ~BSEC_OTP_MASK) >> BSEC_OTP_BANK_SHIFT) * |
| 188 | sizeof(uint32_t); |
| 189 | } |
| 190 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 191 | static uint32_t otp_bit_mask(uint32_t otp) |
| 192 | { |
| 193 | return BIT(otp & BSEC_OTP_MASK); |
| 194 | } |
| 195 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 196 | /* |
| 197 | * bsec_check_error: check BSEC error status. |
| 198 | * otp: OTP number. |
| 199 | * check_disturbed: check only error (false), |
| 200 | * or error and disturbed status (true). |
| 201 | * return value: BSEC_OK if no error. |
| 202 | */ |
| 203 | static uint32_t bsec_check_error(uint32_t otp, bool check_disturbed) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 204 | { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 205 | uint32_t bit = otp_bit_mask(otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 206 | uint32_t bank = otp_bank_offset(otp); |
| 207 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 208 | if ((mmio_read_32(BSEC_BASE + BSEC_ERROR_OFF + bank) & bit) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 209 | return BSEC_ERROR; |
| 210 | } |
| 211 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 212 | if (!check_disturbed) { |
| 213 | return BSEC_OK; |
| 214 | } |
| 215 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 216 | 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] | 217 | return BSEC_DISTURBED; |
| 218 | } |
| 219 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 220 | return BSEC_OK; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * bsec_probe: initialize BSEC driver. |
| 225 | * return value: BSEC_OK if no error. |
| 226 | */ |
| 227 | uint32_t bsec_probe(void) |
| 228 | { |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 229 | if (is_otp_invalid_mode()) { |
| 230 | return BSEC_ERROR; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 233 | if (((bsec_get_version() != BSEC_IP_VERSION_1_1) && |
| 234 | (bsec_get_version() != BSEC_IP_VERSION_2_0)) || |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 235 | (bsec_get_id() != BSEC_IP_ID_2)) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 236 | panic(); |
| 237 | } |
| 238 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 239 | #if defined(IMAGE_BL32) |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 240 | bsec_late_init(); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 241 | #endif |
| 242 | return BSEC_OK; |
| 243 | } |
| 244 | |
| 245 | /* |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 246 | * bsec_shadow_register: copy SAFMEM OTP to BSEC data. |
| 247 | * otp: OTP number. |
| 248 | * return value: BSEC_OK if no error. |
| 249 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 250 | static uint32_t bsec_shadow_register(uint32_t otp) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 251 | { |
| 252 | uint32_t result; |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 253 | bool value; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 254 | bool power_up = false; |
| 255 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 256 | if (is_otp_invalid_mode()) { |
| 257 | return BSEC_ERROR; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 260 | result = bsec_read_sr_lock(otp, &value); |
| 261 | if (result != BSEC_OK) { |
| 262 | ERROR("BSEC: %u Sticky-read bit read Error %u\n", otp, result); |
| 263 | return result; |
| 264 | } |
| 265 | |
| 266 | if (value) { |
| 267 | VERBOSE("BSEC: OTP %u is locked and will not be refreshed\n", |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 268 | otp); |
| 269 | } |
| 270 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 271 | if ((bsec_get_status() & BSEC_OTP_STATUS_PWRON) == 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 272 | result = bsec_power_safmem(true); |
| 273 | |
| 274 | if (result != BSEC_OK) { |
| 275 | return result; |
| 276 | } |
| 277 | |
| 278 | power_up = true; |
| 279 | } |
| 280 | |
| 281 | bsec_lock(); |
| 282 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 283 | mmio_write_32(BSEC_BASE + BSEC_OTP_CTRL_OFF, otp | BSEC_READ); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 284 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 285 | while ((bsec_get_status() & BSEC_OTP_STATUS_BUSY) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 286 | ; |
| 287 | } |
| 288 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 289 | result = bsec_check_error(otp, true); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 290 | |
| 291 | bsec_unlock(); |
| 292 | |
| 293 | if (power_up) { |
| 294 | if (bsec_power_safmem(false) != BSEC_OK) { |
| 295 | panic(); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return result; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * bsec_read_otp: read an OTP data value. |
| 304 | * val: read value. |
| 305 | * otp: OTP number. |
| 306 | * return value: BSEC_OK if no error. |
| 307 | */ |
| 308 | uint32_t bsec_read_otp(uint32_t *val, uint32_t otp) |
| 309 | { |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 310 | if (is_otp_invalid_mode()) { |
| 311 | return BSEC_ERROR; |
| 312 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 313 | |
| 314 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 315 | return BSEC_INVALID_PARAM; |
| 316 | } |
| 317 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 318 | *val = mmio_read_32(BSEC_BASE + BSEC_OTP_DATA_OFF + |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 319 | (otp * sizeof(uint32_t))); |
| 320 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 321 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
| 325 | * bsec_write_otp: write value in BSEC data register. |
| 326 | * val: value to write. |
| 327 | * otp: OTP number. |
| 328 | * return value: BSEC_OK if no error. |
| 329 | */ |
| 330 | uint32_t bsec_write_otp(uint32_t val, uint32_t otp) |
| 331 | { |
| 332 | uint32_t result; |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 333 | bool value; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 334 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 335 | if (is_otp_invalid_mode()) { |
| 336 | return BSEC_ERROR; |
| 337 | } |
| 338 | |
| 339 | result = bsec_read_sw_lock(otp, &value); |
| 340 | if (result != BSEC_OK) { |
| 341 | ERROR("BSEC: %u Sticky-write bit read Error %u\n", otp, result); |
| 342 | return result; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 345 | if (value) { |
| 346 | VERBOSE("BSEC: OTP %u is locked and write will be ignored\n", |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 347 | otp); |
| 348 | } |
| 349 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 350 | /* Ensure integrity of each register access sequence */ |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 351 | bsec_lock(); |
| 352 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 353 | mmio_write_32(BSEC_BASE + BSEC_OTP_DATA_OFF + |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 354 | (otp * sizeof(uint32_t)), val); |
| 355 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 356 | bsec_unlock(); |
| 357 | |
| 358 | return result; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * bsec_program_otp: program a bit in SAFMEM after the prog. |
| 363 | * The OTP data is not refreshed. |
| 364 | * val: value to program. |
| 365 | * otp: OTP number. |
| 366 | * return value: BSEC_OK if no error. |
| 367 | */ |
| 368 | uint32_t bsec_program_otp(uint32_t val, uint32_t otp) |
| 369 | { |
| 370 | uint32_t result; |
| 371 | bool power_up = false; |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 372 | bool sp_lock; |
| 373 | bool perm_lock; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 374 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 375 | if (is_otp_invalid_mode()) { |
| 376 | return BSEC_ERROR; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 379 | result = bsec_read_sp_lock(otp, &sp_lock); |
| 380 | if (result != BSEC_OK) { |
| 381 | ERROR("BSEC: %u Sticky-prog bit read Error %u\n", otp, result); |
| 382 | return result; |
| 383 | } |
| 384 | |
| 385 | result = bsec_read_permanent_lock(otp, &perm_lock); |
| 386 | if (result != BSEC_OK) { |
| 387 | ERROR("BSEC: %u permanent bit read Error %u\n", otp, result); |
| 388 | return result; |
| 389 | } |
| 390 | |
| 391 | if (sp_lock || perm_lock) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 392 | WARN("BSEC: OTP locked, prog will be ignored\n"); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 393 | return BSEC_PROG_FAIL; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 396 | 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] | 397 | WARN("BSEC: GPLOCK activated, prog will be ignored\n"); |
| 398 | } |
| 399 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 400 | if ((bsec_get_status() & BSEC_OTP_STATUS_PWRON) == 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 401 | result = bsec_power_safmem(true); |
| 402 | |
| 403 | if (result != BSEC_OK) { |
| 404 | return result; |
| 405 | } |
| 406 | |
| 407 | power_up = true; |
| 408 | } |
| 409 | |
| 410 | bsec_lock(); |
| 411 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 412 | mmio_write_32(BSEC_BASE + BSEC_OTP_WRDATA_OFF, val); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 413 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 414 | mmio_write_32(BSEC_BASE + BSEC_OTP_CTRL_OFF, otp | BSEC_WRITE); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 415 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 416 | while ((bsec_get_status() & BSEC_OTP_STATUS_BUSY) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 417 | ; |
| 418 | } |
| 419 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 420 | if ((bsec_get_status() & BSEC_OTP_STATUS_PROGFAIL) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 421 | result = BSEC_PROG_FAIL; |
| 422 | } else { |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 423 | result = bsec_check_error(otp, true); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | bsec_unlock(); |
| 427 | |
| 428 | if (power_up) { |
| 429 | if (bsec_power_safmem(false) != BSEC_OK) { |
| 430 | panic(); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | return result; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * bsec_permanent_lock_otp: permanent lock of OTP in SAFMEM. |
| 439 | * otp: OTP number. |
| 440 | * return value: BSEC_OK if no error. |
| 441 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 442 | #if defined(IMAGE_BL32) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 443 | uint32_t bsec_permanent_lock_otp(uint32_t otp) |
| 444 | { |
| 445 | uint32_t result; |
| 446 | bool power_up = false; |
| 447 | uint32_t data; |
| 448 | uint32_t addr; |
| 449 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 450 | if (is_otp_invalid_mode()) { |
| 451 | return BSEC_ERROR; |
| 452 | } |
| 453 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 454 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 455 | return BSEC_INVALID_PARAM; |
| 456 | } |
| 457 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 458 | if ((bsec_get_status() & BSEC_OTP_STATUS_PWRON) == 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 459 | result = bsec_power_safmem(true); |
| 460 | |
| 461 | if (result != BSEC_OK) { |
| 462 | return result; |
| 463 | } |
| 464 | |
| 465 | power_up = true; |
| 466 | } |
| 467 | |
| 468 | if (otp < STM32MP1_UPPER_OTP_START) { |
| 469 | addr = otp >> ADDR_LOWER_OTP_PERLOCK_SHIFT; |
| 470 | data = DATA_LOWER_OTP_PERLOCK_BIT << |
| 471 | ((otp & DATA_LOWER_OTP_PERLOCK_MASK) << 1U); |
| 472 | } else { |
| 473 | addr = (otp >> ADDR_UPPER_OTP_PERLOCK_SHIFT) + 2U; |
| 474 | data = DATA_UPPER_OTP_PERLOCK_BIT << |
| 475 | (otp & DATA_UPPER_OTP_PERLOCK_MASK); |
| 476 | } |
| 477 | |
| 478 | bsec_lock(); |
| 479 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 480 | mmio_write_32(BSEC_BASE + BSEC_OTP_WRDATA_OFF, data); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 481 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 482 | mmio_write_32(BSEC_BASE + BSEC_OTP_CTRL_OFF, |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 483 | addr | BSEC_WRITE | BSEC_LOCK); |
| 484 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 485 | while ((bsec_get_status() & BSEC_OTP_STATUS_BUSY) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 486 | ; |
| 487 | } |
| 488 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 489 | if ((bsec_get_status() & BSEC_OTP_STATUS_PROGFAIL) != 0U) { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 490 | result = BSEC_PROG_FAIL; |
| 491 | } else { |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 492 | result = bsec_check_error(otp, false); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | bsec_unlock(); |
| 496 | |
| 497 | if (power_up) { |
| 498 | if (bsec_power_safmem(false) != BSEC_OK) { |
| 499 | panic(); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | return result; |
| 504 | } |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 505 | #endif |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 506 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 507 | /* |
| 508 | * bsec_read_debug_conf: return debug configuration register value. |
| 509 | */ |
| 510 | uint32_t bsec_read_debug_conf(void) |
| 511 | { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 512 | return mmio_read_32(BSEC_BASE + BSEC_DEN_OFF); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 513 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 514 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 515 | /* |
| 516 | * bsec_write_scratch: write value in scratch register. |
| 517 | * val: value to write. |
| 518 | * return value: none. |
| 519 | */ |
| 520 | void bsec_write_scratch(uint32_t val) |
| 521 | { |
| 522 | #if defined(IMAGE_BL32) |
| 523 | if (is_otp_invalid_mode()) { |
| 524 | return; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 527 | bsec_lock(); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 528 | mmio_write_32(BSEC_BASE + BSEC_SCRATCH_OFF, val); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 529 | bsec_unlock(); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 530 | #else |
| 531 | mmio_write_32(BSEC_BASE + BSEC_SCRATCH_OFF, val); |
| 532 | #endif |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /* |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 536 | * bsec_get_status: return status register value. |
| 537 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 538 | static uint32_t bsec_get_status(void) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 539 | { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 540 | return mmio_read_32(BSEC_BASE + BSEC_OTP_STATUS_OFF); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 544 | * bsec_get_version: return BSEC version register value. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 545 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 546 | static uint32_t bsec_get_version(void) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 547 | { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 548 | return mmio_read_32(BSEC_BASE + BSEC_IPVR_OFF) & BSEC_IPVR_MSK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 552 | * bsec_get_id: return BSEC ID register value. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 553 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 554 | static uint32_t bsec_get_id(void) |
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 | return mmio_read_32(BSEC_BASE + BSEC_IP_ID_OFF); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 560 | * bsec_set_sr_lock: set shadow-read lock. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 561 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 562 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 563 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 564 | uint32_t bsec_set_sr_lock(uint32_t otp) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 565 | { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 566 | uint32_t bank = otp_bank_offset(otp); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 567 | uint32_t otp_mask = otp_bit_mask(otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 568 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 569 | if (is_otp_invalid_mode()) { |
| 570 | return BSEC_ERROR; |
| 571 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 572 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 573 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 574 | return BSEC_INVALID_PARAM; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 575 | } |
| 576 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 577 | bsec_lock(); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 578 | mmio_write_32(BSEC_BASE + BSEC_SRLOCK_OFF + bank, otp_mask); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 579 | bsec_unlock(); |
| 580 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 581 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* |
| 585 | * bsec_read_sr_lock: read shadow-read lock. |
| 586 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 587 | * value: read value (true or false). |
| 588 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 589 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 590 | uint32_t bsec_read_sr_lock(uint32_t otp, bool *value) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 591 | { |
| 592 | uint32_t bank = otp_bank_offset(otp); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 593 | uint32_t otp_mask = otp_bit_mask(otp); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 594 | uint32_t bank_value; |
| 595 | |
| 596 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 597 | return BSEC_INVALID_PARAM; |
| 598 | } |
| 599 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 600 | bank_value = mmio_read_32(BSEC_BASE + BSEC_SRLOCK_OFF + bank); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 601 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 602 | *value = ((bank_value & otp_mask) != 0U); |
| 603 | |
| 604 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 608 | * bsec_set_sw_lock: set shadow-write lock. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 609 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 610 | * return value: BSEC_OK if no error. |
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 | uint32_t bsec_set_sw_lock(uint32_t otp) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 613 | { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 614 | uint32_t bank = otp_bank_offset(otp); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 615 | uint32_t otp_mask = otp_bit_mask(otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 616 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 617 | if (is_otp_invalid_mode()) { |
| 618 | return BSEC_ERROR; |
| 619 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 620 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 621 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 622 | return BSEC_INVALID_PARAM; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 625 | bsec_lock(); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 626 | mmio_write_32(BSEC_BASE + BSEC_SWLOCK_OFF + bank, otp_mask); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 627 | bsec_unlock(); |
| 628 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 629 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /* |
| 633 | * bsec_read_sw_lock: read shadow-write lock. |
| 634 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 635 | * value: read value (true or false). |
| 636 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 637 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 638 | uint32_t bsec_read_sw_lock(uint32_t otp, bool *value) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 639 | { |
| 640 | uint32_t bank = otp_bank_offset(otp); |
| 641 | uint32_t otp_mask = BIT(otp & BSEC_OTP_MASK); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 642 | uint32_t bank_value; |
| 643 | |
| 644 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 645 | return BSEC_INVALID_PARAM; |
| 646 | } |
| 647 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 648 | bank_value = mmio_read_32(BSEC_BASE + BSEC_SWLOCK_OFF + bank); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 649 | |
| 650 | *value = ((bank_value & otp_mask) != 0U); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 651 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 652 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 656 | * bsec_set_sp_lock: set shadow-program lock. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 657 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 658 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 659 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 660 | uint32_t bsec_set_sp_lock(uint32_t otp) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 661 | { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 662 | uint32_t bank = otp_bank_offset(otp); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 663 | uint32_t otp_mask = otp_bit_mask(otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 664 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 665 | if (is_otp_invalid_mode()) { |
| 666 | return BSEC_ERROR; |
| 667 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 668 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 669 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 670 | return BSEC_INVALID_PARAM; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 673 | bsec_lock(); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 674 | mmio_write_32(BSEC_BASE + BSEC_SPLOCK_OFF + bank, otp_mask); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 675 | bsec_unlock(); |
| 676 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 677 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* |
| 681 | * bsec_read_sp_lock: read shadow-program lock. |
| 682 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 683 | * value: read value (true or false). |
| 684 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 685 | */ |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 686 | uint32_t bsec_read_sp_lock(uint32_t otp, bool *value) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 687 | { |
| 688 | uint32_t bank = otp_bank_offset(otp); |
| 689 | uint32_t otp_mask = BIT(otp & BSEC_OTP_MASK); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 690 | uint32_t bank_value; |
| 691 | |
| 692 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 693 | return BSEC_INVALID_PARAM; |
| 694 | } |
| 695 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 696 | bank_value = mmio_read_32(BSEC_BASE + BSEC_SPLOCK_OFF + bank); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 697 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 698 | *value = ((bank_value & otp_mask) != 0U); |
| 699 | |
| 700 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 704 | * bsec_read_permanent_lock: Read permanent lock status. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 705 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 706 | * value: read value (true or false). |
| 707 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 708 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 709 | static uint32_t bsec_read_permanent_lock(uint32_t otp, bool *value) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 710 | { |
| 711 | uint32_t bank = otp_bank_offset(otp); |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 712 | uint32_t otp_mask = otp_bit_mask(otp); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 713 | uint32_t bank_value; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 714 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 715 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 716 | return BSEC_INVALID_PARAM; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 717 | } |
| 718 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 719 | bank_value = mmio_read_32(BSEC_BASE + BSEC_WRLOCK_OFF + bank); |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 720 | |
| 721 | *value = ((bank_value & otp_mask) != 0U); |
| 722 | |
| 723 | return BSEC_OK; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /* |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 727 | * bsec_power_safmem: Activate or deactivate SAFMEM power. |
| 728 | * power: true to power up, false to power down. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 729 | * return value: BSEC_OK if no error. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 730 | */ |
| 731 | static uint32_t bsec_power_safmem(bool power) |
| 732 | { |
| 733 | uint32_t register_val; |
| 734 | uint32_t timeout = BSEC_TIMEOUT_VALUE; |
| 735 | |
| 736 | bsec_lock(); |
| 737 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 738 | register_val = mmio_read_32(BSEC_BASE + BSEC_OTP_CONF_OFF); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 739 | |
| 740 | if (power) { |
| 741 | register_val |= BSEC_CONF_POWER_UP_MASK; |
| 742 | } else { |
| 743 | register_val &= ~BSEC_CONF_POWER_UP_MASK; |
| 744 | } |
| 745 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 746 | mmio_write_32(BSEC_BASE + BSEC_OTP_CONF_OFF, register_val); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 747 | |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 748 | if (power) { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 749 | while (((bsec_get_status() & BSEC_OTP_STATUS_PWRON) == 0U) && |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 750 | (timeout != 0U)) { |
| 751 | timeout--; |
| 752 | } |
| 753 | } else { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 754 | while (((bsec_get_status() & BSEC_OTP_STATUS_PWRON) != 0U) && |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 755 | (timeout != 0U)) { |
| 756 | timeout--; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | bsec_unlock(); |
| 761 | |
| 762 | if (timeout == 0U) { |
| 763 | return BSEC_TIMEOUT; |
| 764 | } |
| 765 | |
| 766 | return BSEC_OK; |
| 767 | } |
| 768 | |
| 769 | /* |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 770 | * bsec_shadow_read_otp: Load OTP from SAFMEM and provide its value. |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 771 | * val: read value. |
| 772 | * otp: OTP number. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 773 | * return value: BSEC_OK if no error. |
| 774 | */ |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 775 | uint32_t bsec_shadow_read_otp(uint32_t *val, uint32_t otp) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 776 | { |
| 777 | uint32_t result; |
| 778 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 779 | result = bsec_shadow_register(otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 780 | if (result != BSEC_OK) { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 781 | ERROR("BSEC: %u Shadowing Error %u\n", otp, result); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 782 | return result; |
| 783 | } |
| 784 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 785 | result = bsec_read_otp(val, otp); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 786 | if (result != BSEC_OK) { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 787 | ERROR("BSEC: %u Read Error %u\n", otp, result); |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | return result; |
| 791 | } |
| 792 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 793 | #if defined(IMAGE_BL32) |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 794 | /* |
| 795 | * bsec_check_nsec_access_rights: check non-secure access rights to target OTP. |
| 796 | * otp: OTP number. |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 797 | * return value: BSEC_OK if authorized access. |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 798 | */ |
| 799 | uint32_t bsec_check_nsec_access_rights(uint32_t otp) |
| 800 | { |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 801 | if (otp > STM32MP1_OTP_MAX_ID) { |
| 802 | return BSEC_INVALID_PARAM; |
| 803 | } |
| 804 | |
| 805 | if (otp >= STM32MP1_UPPER_OTP_START) { |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 806 | if (!non_secure_can_access(otp)) { |
| 807 | return BSEC_ERROR; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 808 | } |
| 809 | } |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 810 | |
| 811 | return BSEC_OK; |
| 812 | } |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 813 | #endif |
| 814 | |
| 815 | uint32_t bsec_get_secure_state(void) |
| 816 | { |
| 817 | uint32_t status = bsec_get_status(); |
| 818 | uint32_t result = BSEC_STATE_INVALID; |
| 819 | uint32_t otp_enc_id __maybe_unused; |
| 820 | uint32_t otp_bit_len __maybe_unused; |
| 821 | int res __maybe_unused; |
Yann Gautier | 36a1e4b | 2019-01-17 14:52:47 +0100 | [diff] [blame] | 822 | |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 823 | if ((status & BSEC_OTP_STATUS_INVALID) != 0U) { |
| 824 | result = BSEC_STATE_INVALID; |
| 825 | } else { |
| 826 | if ((status & BSEC_OTP_STATUS_SECURE) != 0U) { |
Yann Gautier | 3e33475 | 2023-02-01 15:04:30 +0100 | [diff] [blame] | 827 | if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) { |
Patrick Delaunay | e720b5b | 2022-12-14 13:45:04 +0100 | [diff] [blame] | 828 | result = BSEC_STATE_SEC_CLOSED; |
| 829 | } else { |
| 830 | result = BSEC_STATE_SEC_OPEN; |
| 831 | } |
| 832 | } else { |
| 833 | /* OTP modes OPEN1 and OPEN2 are not supported */ |
| 834 | result = BSEC_STATE_INVALID; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | return result; |
| 839 | } |