Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <misc.h> |
| 9 | #include <asm/io.h> |
Patrick Delaunay | d859c61 | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 10 | #include <asm/arch/stm32mp1_smc.h> |
| 11 | #include <linux/arm-smccc.h> |
Patrick Delaunay | ee7d772 | 2019-04-18 17:32:39 +0200 | [diff] [blame] | 12 | #include <linux/iopoll.h> |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 13 | |
| 14 | #define BSEC_OTP_MAX_VALUE 95 |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 15 | #define BSEC_TIMEOUT_US 10000 |
| 16 | |
| 17 | /* BSEC REGISTER OFFSET (base relative) */ |
| 18 | #define BSEC_OTP_CONF_OFF 0x000 |
| 19 | #define BSEC_OTP_CTRL_OFF 0x004 |
| 20 | #define BSEC_OTP_WRDATA_OFF 0x008 |
| 21 | #define BSEC_OTP_STATUS_OFF 0x00C |
| 22 | #define BSEC_OTP_LOCK_OFF 0x010 |
| 23 | #define BSEC_DISTURBED_OFF 0x01C |
| 24 | #define BSEC_ERROR_OFF 0x034 |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 25 | #define BSEC_WRLOCK_OFF 0x04C /* OTP write permananet lock */ |
| 26 | #define BSEC_SPLOCK_OFF 0x064 /* OTP write sticky lock */ |
| 27 | #define BSEC_SWLOCK_OFF 0x07C /* shadow write sticky lock */ |
| 28 | #define BSEC_SRLOCK_OFF 0x094 /* shadow read sticky lock */ |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 29 | #define BSEC_OTP_DATA_OFF 0x200 |
| 30 | |
| 31 | /* BSEC_CONFIGURATION Register MASK */ |
| 32 | #define BSEC_CONF_POWER_UP 0x001 |
| 33 | |
| 34 | /* BSEC_CONTROL Register */ |
| 35 | #define BSEC_READ 0x000 |
| 36 | #define BSEC_WRITE 0x100 |
| 37 | |
| 38 | /* LOCK Register */ |
| 39 | #define OTP_LOCK_MASK 0x1F |
| 40 | #define OTP_LOCK_BANK_SHIFT 0x05 |
| 41 | #define OTP_LOCK_BIT_MASK 0x01 |
| 42 | |
| 43 | /* STATUS Register */ |
| 44 | #define BSEC_MODE_BUSY_MASK 0x08 |
| 45 | #define BSEC_MODE_PROGFAIL_MASK 0x10 |
| 46 | #define BSEC_MODE_PWR_MASK 0x20 |
| 47 | |
| 48 | /* |
| 49 | * OTP Lock services definition |
| 50 | * Value must corresponding to the bit number in the register |
| 51 | */ |
| 52 | #define BSEC_LOCK_PROGRAM 0x04 |
| 53 | |
| 54 | /** |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 55 | * bsec_lock() - manage lock for each type SR/SP/SW |
| 56 | * @address: address of bsec IP register |
| 57 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 58 | * Return: true if locked else false |
| 59 | */ |
| 60 | static bool bsec_read_lock(u32 address, u32 otp) |
| 61 | { |
| 62 | u32 bit; |
| 63 | u32 bank; |
| 64 | |
| 65 | bit = 1 << (otp & OTP_LOCK_MASK); |
| 66 | bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32); |
| 67 | |
| 68 | return !!(readl(address + bank) & bit); |
| 69 | } |
| 70 | |
Patrick Delaunay | 654706b | 2020-04-01 09:07:33 +0200 | [diff] [blame^] | 71 | #ifndef CONFIG_TFABOOT |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 72 | /** |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 73 | * bsec_check_error() - Check status of one otp |
| 74 | * @base: base address of bsec IP |
| 75 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 76 | * Return: 0 if no error, -EAGAIN or -ENOTSUPP |
| 77 | */ |
| 78 | static u32 bsec_check_error(u32 base, u32 otp) |
| 79 | { |
| 80 | u32 bit; |
| 81 | u32 bank; |
| 82 | |
| 83 | bit = 1 << (otp & OTP_LOCK_MASK); |
| 84 | bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32); |
| 85 | |
| 86 | if (readl(base + BSEC_DISTURBED_OFF + bank) & bit) |
| 87 | return -EAGAIN; |
| 88 | else if (readl(base + BSEC_ERROR_OFF + bank) & bit) |
| 89 | return -ENOTSUPP; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | /** |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 95 | * bsec_read_SR_lock() - read SR lock (Shadowing) |
| 96 | * @base: base address of bsec IP |
| 97 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 98 | * Return: true if locked else false |
| 99 | */ |
| 100 | static bool bsec_read_SR_lock(u32 base, u32 otp) |
| 101 | { |
| 102 | return bsec_read_lock(base + BSEC_SRLOCK_OFF, otp); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * bsec_read_SP_lock() - read SP lock (program Lock) |
| 107 | * @base: base address of bsec IP |
| 108 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 109 | * Return: true if locked else false |
| 110 | */ |
| 111 | static bool bsec_read_SP_lock(u32 base, u32 otp) |
| 112 | { |
| 113 | return bsec_read_lock(base + BSEC_SPLOCK_OFF, otp); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * bsec_SW_lock() - manage SW lock (Write in Shadow) |
| 118 | * @base: base address of bsec IP |
| 119 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 120 | * Return: true if locked else false |
| 121 | */ |
| 122 | static bool bsec_read_SW_lock(u32 base, u32 otp) |
| 123 | { |
| 124 | return bsec_read_lock(base + BSEC_SWLOCK_OFF, otp); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * bsec_power_safmem() - Activate or deactivate safmem power |
| 129 | * @base: base address of bsec IP |
| 130 | * @power: true to power up , false to power down |
| 131 | * Return: 0 if succeed |
| 132 | */ |
| 133 | static int bsec_power_safmem(u32 base, bool power) |
| 134 | { |
| 135 | u32 val; |
| 136 | u32 mask; |
| 137 | |
| 138 | if (power) { |
| 139 | setbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP); |
| 140 | mask = BSEC_MODE_PWR_MASK; |
| 141 | } else { |
| 142 | clrbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP); |
| 143 | mask = 0; |
| 144 | } |
| 145 | |
| 146 | /* waiting loop */ |
| 147 | return readl_poll_timeout(base + BSEC_OTP_STATUS_OFF, |
| 148 | val, (val & BSEC_MODE_PWR_MASK) == mask, |
| 149 | BSEC_TIMEOUT_US); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * bsec_shadow_register() - copy safmen otp to bsec data |
| 154 | * @base: base address of bsec IP |
| 155 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 156 | * Return: 0 if no error |
| 157 | */ |
| 158 | static int bsec_shadow_register(u32 base, u32 otp) |
| 159 | { |
| 160 | u32 val; |
| 161 | int ret; |
| 162 | bool power_up = false; |
| 163 | |
| 164 | /* check if shadowing of otp is locked */ |
| 165 | if (bsec_read_SR_lock(base, otp)) |
| 166 | pr_debug("bsec : OTP %d is locked and refreshed with 0\n", otp); |
| 167 | |
| 168 | /* check if safemem is power up */ |
| 169 | val = readl(base + BSEC_OTP_STATUS_OFF); |
| 170 | if (!(val & BSEC_MODE_PWR_MASK)) { |
| 171 | ret = bsec_power_safmem(base, true); |
| 172 | if (ret) |
| 173 | return ret; |
Patrick Delaunay | 815bc8b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 174 | power_up = true; |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 175 | } |
| 176 | /* set BSEC_OTP_CTRL_OFF with the otp value*/ |
| 177 | writel(otp | BSEC_READ, base + BSEC_OTP_CTRL_OFF); |
| 178 | |
| 179 | /* check otp status*/ |
| 180 | ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF, |
| 181 | val, (val & BSEC_MODE_BUSY_MASK) == 0, |
| 182 | BSEC_TIMEOUT_US); |
| 183 | if (ret) |
| 184 | return ret; |
| 185 | |
| 186 | ret = bsec_check_error(base, otp); |
| 187 | |
| 188 | if (power_up) |
| 189 | bsec_power_safmem(base, false); |
| 190 | |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * bsec_read_shadow() - read an otp data value from shadow |
| 196 | * @base: base address of bsec IP |
| 197 | * @val: read value |
| 198 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 199 | * Return: 0 if no error |
| 200 | */ |
| 201 | static int bsec_read_shadow(u32 base, u32 *val, u32 otp) |
| 202 | { |
| 203 | *val = readl(base + BSEC_OTP_DATA_OFF + otp * sizeof(u32)); |
| 204 | |
| 205 | return bsec_check_error(base, otp); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * bsec_write_shadow() - write value in BSEC data register in shadow |
| 210 | * @base: base address of bsec IP |
| 211 | * @val: value to write |
| 212 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 213 | * Return: 0 if no error |
| 214 | */ |
| 215 | static int bsec_write_shadow(u32 base, u32 val, u32 otp) |
| 216 | { |
| 217 | /* check if programming of otp is locked */ |
| 218 | if (bsec_read_SW_lock(base, otp)) |
| 219 | pr_debug("bsec : OTP %d is lock, write will be ignore\n", otp); |
| 220 | |
| 221 | writel(val, base + BSEC_OTP_DATA_OFF + otp * sizeof(u32)); |
| 222 | |
| 223 | return bsec_check_error(base, otp); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * bsec_program_otp() - program a bit in SAFMEM |
| 228 | * @base: base address of bsec IP |
| 229 | * @val: value to program |
| 230 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 231 | * after the function the otp data is not refreshed in shadow |
| 232 | * Return: 0 if no error |
| 233 | */ |
| 234 | static int bsec_program_otp(long base, u32 val, u32 otp) |
| 235 | { |
| 236 | u32 ret; |
| 237 | bool power_up = false; |
| 238 | |
| 239 | if (bsec_read_SP_lock(base, otp)) |
| 240 | pr_debug("bsec : OTP %d locked, prog will be ignore\n", otp); |
| 241 | |
| 242 | if (readl(base + BSEC_OTP_LOCK_OFF) & (1 << BSEC_LOCK_PROGRAM)) |
| 243 | pr_debug("bsec : Global lock, prog will be ignore\n"); |
| 244 | |
| 245 | /* check if safemem is power up */ |
| 246 | if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) { |
| 247 | ret = bsec_power_safmem(base, true); |
| 248 | if (ret) |
| 249 | return ret; |
| 250 | |
| 251 | power_up = true; |
| 252 | } |
| 253 | /* set value in write register*/ |
| 254 | writel(val, base + BSEC_OTP_WRDATA_OFF); |
| 255 | |
| 256 | /* set BSEC_OTP_CTRL_OFF with the otp value */ |
| 257 | writel(otp | BSEC_WRITE, base + BSEC_OTP_CTRL_OFF); |
| 258 | |
| 259 | /* check otp status*/ |
| 260 | ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF, |
| 261 | val, (val & BSEC_MODE_BUSY_MASK) == 0, |
| 262 | BSEC_TIMEOUT_US); |
| 263 | if (ret) |
| 264 | return ret; |
| 265 | |
| 266 | if (val & BSEC_MODE_PROGFAIL_MASK) |
| 267 | ret = -EACCES; |
| 268 | else |
| 269 | ret = bsec_check_error(base, otp); |
| 270 | |
| 271 | if (power_up) |
| 272 | bsec_power_safmem(base, false); |
| 273 | |
| 274 | return ret; |
| 275 | } |
Patrick Delaunay | 654706b | 2020-04-01 09:07:33 +0200 | [diff] [blame^] | 276 | #endif /* CONFIG_TFABOOT */ |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 277 | |
| 278 | /* BSEC MISC driver *******************************************************/ |
| 279 | struct stm32mp_bsec_platdata { |
| 280 | u32 base; |
| 281 | }; |
| 282 | |
| 283 | static int stm32mp_bsec_read_otp(struct udevice *dev, u32 *val, u32 otp) |
| 284 | { |
Patrick Delaunay | 654706b | 2020-04-01 09:07:33 +0200 | [diff] [blame^] | 285 | #ifdef CONFIG_TFABOOT |
Patrick Delaunay | d859c61 | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 286 | return stm32_smc(STM32_SMC_BSEC, |
| 287 | STM32_SMC_READ_OTP, |
| 288 | otp, 0, val); |
| 289 | #else |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 290 | struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev); |
| 291 | u32 tmp_data = 0; |
| 292 | int ret; |
| 293 | |
| 294 | /* read current shadow value */ |
| 295 | ret = bsec_read_shadow(plat->base, &tmp_data, otp); |
| 296 | if (ret) |
| 297 | return ret; |
| 298 | |
| 299 | /* copy otp in shadow */ |
| 300 | ret = bsec_shadow_register(plat->base, otp); |
| 301 | if (ret) |
| 302 | return ret; |
| 303 | |
| 304 | ret = bsec_read_shadow(plat->base, val, otp); |
| 305 | if (ret) |
| 306 | return ret; |
| 307 | |
| 308 | /* restore shadow value */ |
| 309 | ret = bsec_write_shadow(plat->base, tmp_data, otp); |
| 310 | return ret; |
Patrick Delaunay | d859c61 | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 311 | #endif |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static int stm32mp_bsec_read_shadow(struct udevice *dev, u32 *val, u32 otp) |
| 315 | { |
Patrick Delaunay | 654706b | 2020-04-01 09:07:33 +0200 | [diff] [blame^] | 316 | #ifdef CONFIG_TFABOOT |
Patrick Delaunay | d859c61 | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 317 | return stm32_smc(STM32_SMC_BSEC, |
| 318 | STM32_SMC_READ_SHADOW, |
| 319 | otp, 0, val); |
| 320 | #else |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 321 | struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev); |
| 322 | |
| 323 | return bsec_read_shadow(plat->base, val, otp); |
Patrick Delaunay | d859c61 | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 324 | #endif |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 327 | static int stm32mp_bsec_read_lock(struct udevice *dev, u32 *val, u32 otp) |
| 328 | { |
| 329 | struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev); |
| 330 | |
| 331 | /* return OTP permanent write lock status */ |
| 332 | *val = bsec_read_lock(plat->base + BSEC_WRLOCK_OFF, otp); |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 337 | static int stm32mp_bsec_write_otp(struct udevice *dev, u32 val, u32 otp) |
| 338 | { |
Patrick Delaunay | 654706b | 2020-04-01 09:07:33 +0200 | [diff] [blame^] | 339 | #ifdef CONFIG_TFABOOT |
Patrick Delaunay | d859c61 | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 340 | return stm32_smc_exec(STM32_SMC_BSEC, |
| 341 | STM32_SMC_PROG_OTP, |
| 342 | otp, val); |
| 343 | #else |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 344 | struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev); |
| 345 | |
| 346 | return bsec_program_otp(plat->base, val, otp); |
Patrick Delaunay | d859c61 | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 347 | #endif |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static int stm32mp_bsec_write_shadow(struct udevice *dev, u32 val, u32 otp) |
| 351 | { |
Patrick Delaunay | 654706b | 2020-04-01 09:07:33 +0200 | [diff] [blame^] | 352 | #ifdef CONFIG_TFABOOT |
Patrick Delaunay | d859c61 | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 353 | return stm32_smc_exec(STM32_SMC_BSEC, |
| 354 | STM32_SMC_WRITE_SHADOW, |
| 355 | otp, val); |
| 356 | #else |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 357 | struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev); |
| 358 | |
| 359 | return bsec_write_shadow(plat->base, val, otp); |
Patrick Delaunay | d859c61 | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 360 | #endif |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 363 | static int stm32mp_bsec_write_lock(struct udevice *dev, u32 val, u32 otp) |
| 364 | { |
Patrick Delaunay | 654706b | 2020-04-01 09:07:33 +0200 | [diff] [blame^] | 365 | #ifdef CONFIG_TFABOOT |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 366 | if (val == 1) |
| 367 | return stm32_smc_exec(STM32_SMC_BSEC, |
| 368 | STM32_SMC_WRLOCK_OTP, |
| 369 | otp, 0); |
| 370 | if (val == 0) |
| 371 | return 0; /* nothing to do */ |
| 372 | |
| 373 | return -EINVAL; |
| 374 | #else |
| 375 | return -ENOTSUPP; |
| 376 | #endif |
| 377 | } |
| 378 | |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 379 | static int stm32mp_bsec_read(struct udevice *dev, int offset, |
| 380 | void *buf, int size) |
| 381 | { |
| 382 | int ret; |
| 383 | int i; |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 384 | bool shadow = true, lock = false; |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 385 | int nb_otp = size / sizeof(u32); |
| 386 | int otp; |
Patrick Delaunay | 745b676 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 387 | unsigned int offs = offset; |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 388 | |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 389 | if (offs >= STM32_BSEC_LOCK_OFFSET) { |
| 390 | offs -= STM32_BSEC_LOCK_OFFSET; |
| 391 | lock = true; |
| 392 | } else if (offs >= STM32_BSEC_OTP_OFFSET) { |
Patrick Delaunay | 745b676 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 393 | offs -= STM32_BSEC_OTP_OFFSET; |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 394 | shadow = false; |
| 395 | } |
Patrick Delaunay | 0c8620d | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 396 | |
Patrick Delaunay | df2d1b8 | 2020-02-12 19:37:37 +0100 | [diff] [blame] | 397 | if ((offs % 4) || (size % 4)) |
Patrick Delaunay | 0c8620d | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 398 | return -EINVAL; |
| 399 | |
Patrick Delaunay | 745b676 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 400 | otp = offs / sizeof(u32); |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 401 | |
Patrick Delaunay | 0c8620d | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 402 | for (i = otp; i < (otp + nb_otp) && i <= BSEC_OTP_MAX_VALUE; i++) { |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 403 | u32 *addr = &((u32 *)buf)[i - otp]; |
| 404 | |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 405 | if (lock) |
| 406 | ret = stm32mp_bsec_read_lock(dev, addr, i); |
| 407 | else if (shadow) |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 408 | ret = stm32mp_bsec_read_shadow(dev, addr, i); |
| 409 | else |
| 410 | ret = stm32mp_bsec_read_otp(dev, addr, i); |
| 411 | |
| 412 | if (ret) |
| 413 | break; |
| 414 | } |
Patrick Delaunay | 0c8620d | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 415 | if (ret) |
| 416 | return ret; |
| 417 | else |
| 418 | return (i - otp) * 4; |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static int stm32mp_bsec_write(struct udevice *dev, int offset, |
| 422 | const void *buf, int size) |
| 423 | { |
| 424 | int ret = 0; |
| 425 | int i; |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 426 | bool shadow = true, lock = false; |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 427 | int nb_otp = size / sizeof(u32); |
| 428 | int otp; |
Patrick Delaunay | 745b676 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 429 | unsigned int offs = offset; |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 430 | |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 431 | if (offs >= STM32_BSEC_LOCK_OFFSET) { |
| 432 | offs -= STM32_BSEC_LOCK_OFFSET; |
| 433 | lock = true; |
| 434 | } else if (offs >= STM32_BSEC_OTP_OFFSET) { |
Patrick Delaunay | 745b676 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 435 | offs -= STM32_BSEC_OTP_OFFSET; |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 436 | shadow = false; |
| 437 | } |
Patrick Delaunay | 0c8620d | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 438 | |
Patrick Delaunay | df2d1b8 | 2020-02-12 19:37:37 +0100 | [diff] [blame] | 439 | if ((offs % 4) || (size % 4)) |
Patrick Delaunay | 0c8620d | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 440 | return -EINVAL; |
| 441 | |
Patrick Delaunay | 745b676 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 442 | otp = offs / sizeof(u32); |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 443 | |
Patrick Delaunay | 0c8620d | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 444 | for (i = otp; i < otp + nb_otp && i <= BSEC_OTP_MAX_VALUE; i++) { |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 445 | u32 *val = &((u32 *)buf)[i - otp]; |
| 446 | |
Patrick Delaunay | 7ae22d7 | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 447 | if (lock) |
| 448 | ret = stm32mp_bsec_write_lock(dev, *val, i); |
| 449 | else if (shadow) |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 450 | ret = stm32mp_bsec_write_shadow(dev, *val, i); |
| 451 | else |
| 452 | ret = stm32mp_bsec_write_otp(dev, *val, i); |
| 453 | if (ret) |
| 454 | break; |
| 455 | } |
Patrick Delaunay | 0c8620d | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 456 | if (ret) |
| 457 | return ret; |
| 458 | else |
| 459 | return (i - otp) * 4; |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static const struct misc_ops stm32mp_bsec_ops = { |
| 463 | .read = stm32mp_bsec_read, |
| 464 | .write = stm32mp_bsec_write, |
| 465 | }; |
| 466 | |
| 467 | static int stm32mp_bsec_ofdata_to_platdata(struct udevice *dev) |
| 468 | { |
| 469 | struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev); |
| 470 | |
| 471 | plat->base = (u32)dev_read_addr_ptr(dev); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
Patrick Delaunay | 654706b | 2020-04-01 09:07:33 +0200 | [diff] [blame^] | 476 | #ifndef CONFIG_TFABOOT |
Patrick Delaunay | 815bc8b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 477 | static int stm32mp_bsec_probe(struct udevice *dev) |
| 478 | { |
| 479 | int otp; |
| 480 | struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev); |
| 481 | |
| 482 | /* update unlocked shadow for OTP cleared by the rom code */ |
| 483 | for (otp = 57; otp <= BSEC_OTP_MAX_VALUE; otp++) |
| 484 | if (!bsec_read_SR_lock(plat->base, otp)) |
| 485 | bsec_shadow_register(plat->base, otp); |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | #endif |
| 490 | |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 491 | static const struct udevice_id stm32mp_bsec_ids[] = { |
Patrick Delaunay | bfe1f08 | 2019-02-27 17:01:27 +0100 | [diff] [blame] | 492 | { .compatible = "st,stm32mp15-bsec" }, |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 493 | {} |
| 494 | }; |
| 495 | |
| 496 | U_BOOT_DRIVER(stm32mp_bsec) = { |
| 497 | .name = "stm32mp_bsec", |
| 498 | .id = UCLASS_MISC, |
| 499 | .of_match = stm32mp_bsec_ids, |
| 500 | .ofdata_to_platdata = stm32mp_bsec_ofdata_to_platdata, |
| 501 | .platdata_auto_alloc_size = sizeof(struct stm32mp_bsec_platdata), |
| 502 | .ops = &stm32mp_bsec_ops, |
Patrick Delaunay | 654706b | 2020-04-01 09:07:33 +0200 | [diff] [blame^] | 503 | #ifndef CONFIG_TFABOOT |
Patrick Delaunay | 815bc8b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 504 | .probe = stm32mp_bsec_probe, |
| 505 | #endif |
Patrick Delaunay | 19f5899 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 506 | }; |