1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
11 #include <asm/arch/bsec.h>
12 #include <asm/arch/stm32mp1_smc.h>
13 #include <linux/arm-smccc.h>
14 #include <linux/iopoll.h>
16 #define BSEC_OTP_MAX_VALUE 95
17 #define BSEC_TIMEOUT_US 10000
19 /* BSEC REGISTER OFFSET (base relative) */
20 #define BSEC_OTP_CONF_OFF 0x000
21 #define BSEC_OTP_CTRL_OFF 0x004
22 #define BSEC_OTP_WRDATA_OFF 0x008
23 #define BSEC_OTP_STATUS_OFF 0x00C
24 #define BSEC_OTP_LOCK_OFF 0x010
25 #define BSEC_DENABLE_OFF 0x014
26 #define BSEC_DISTURBED_OFF 0x01C
27 #define BSEC_ERROR_OFF 0x034
28 #define BSEC_WRLOCK_OFF 0x04C /* OTP write permananet lock */
29 #define BSEC_SPLOCK_OFF 0x064 /* OTP write sticky lock */
30 #define BSEC_SWLOCK_OFF 0x07C /* shadow write sticky lock */
31 #define BSEC_SRLOCK_OFF 0x094 /* shadow read sticky lock */
32 #define BSEC_OTP_DATA_OFF 0x200
34 /* BSEC_CONFIGURATION Register MASK */
35 #define BSEC_CONF_POWER_UP 0x001
37 /* BSEC_CONTROL Register */
38 #define BSEC_READ 0x000
39 #define BSEC_WRITE 0x100
42 #define OTP_LOCK_MASK 0x1F
43 #define OTP_LOCK_BANK_SHIFT 0x05
44 #define OTP_LOCK_BIT_MASK 0x01
47 #define BSEC_MODE_BUSY_MASK 0x08
48 #define BSEC_MODE_PROGFAIL_MASK 0x10
49 #define BSEC_MODE_PWR_MASK 0x20
51 /* DENABLE Register */
52 #define BSEC_DENABLE_DBGSWENABLE BIT(10)
55 * OTP Lock services definition
56 * Value must corresponding to the bit number in the register
58 #define BSEC_LOCK_PROGRAM 0x04
61 * bsec_lock() - manage lock for each type SR/SP/SW
62 * @address: address of bsec IP register
63 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
64 * Return: true if locked else false
66 static bool bsec_read_lock(u32 address, u32 otp)
71 bit = 1 << (otp & OTP_LOCK_MASK);
72 bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
74 return !!(readl(address + bank) & bit);
78 * bsec_check_error() - Check status of one otp
79 * @base: base address of bsec IP
80 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
81 * Return: 0 if no error, -EAGAIN or -ENOTSUPP
83 static u32 bsec_check_error(u32 base, u32 otp)
88 bit = 1 << (otp & OTP_LOCK_MASK);
89 bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
91 if (readl(base + BSEC_DISTURBED_OFF + bank) & bit)
93 else if (readl(base + BSEC_ERROR_OFF + bank) & bit)
100 * bsec_read_SR_lock() - read SR lock (Shadowing)
101 * @base: base address of bsec IP
102 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
103 * Return: true if locked else false
105 static bool bsec_read_SR_lock(u32 base, u32 otp)
107 return bsec_read_lock(base + BSEC_SRLOCK_OFF, otp);
111 * bsec_read_SP_lock() - read SP lock (program Lock)
112 * @base: base address of bsec IP
113 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
114 * Return: true if locked else false
116 static bool bsec_read_SP_lock(u32 base, u32 otp)
118 return bsec_read_lock(base + BSEC_SPLOCK_OFF, otp);
122 * bsec_SW_lock() - manage SW lock (Write in Shadow)
123 * @base: base address of bsec IP
124 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
125 * Return: true if locked else false
127 static bool bsec_read_SW_lock(u32 base, u32 otp)
129 return bsec_read_lock(base + BSEC_SWLOCK_OFF, otp);
133 * bsec_power_safmem() - Activate or deactivate safmem power
134 * @base: base address of bsec IP
135 * @power: true to power up , false to power down
136 * Return: 0 if succeed
138 static int bsec_power_safmem(u32 base, bool power)
144 setbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
145 mask = BSEC_MODE_PWR_MASK;
147 clrbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
152 return readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
153 val, (val & BSEC_MODE_PWR_MASK) == mask,
158 * bsec_shadow_register() - copy safmen otp to bsec data
159 * @base: base address of bsec IP
160 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
161 * Return: 0 if no error
163 static int bsec_shadow_register(u32 base, u32 otp)
167 bool power_up = false;
169 /* check if shadowing of otp is locked */
170 if (bsec_read_SR_lock(base, otp))
171 pr_debug("bsec : OTP %d is locked and refreshed with 0\n", otp);
173 /* check if safemem is power up */
174 val = readl(base + BSEC_OTP_STATUS_OFF);
175 if (!(val & BSEC_MODE_PWR_MASK)) {
176 ret = bsec_power_safmem(base, true);
181 /* set BSEC_OTP_CTRL_OFF with the otp value*/
182 writel(otp | BSEC_READ, base + BSEC_OTP_CTRL_OFF);
184 /* check otp status*/
185 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
186 val, (val & BSEC_MODE_BUSY_MASK) == 0,
191 ret = bsec_check_error(base, otp);
194 bsec_power_safmem(base, false);
200 * bsec_read_shadow() - read an otp data value from shadow
201 * @base: base address of bsec IP
203 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
204 * Return: 0 if no error
206 static int bsec_read_shadow(u32 base, u32 *val, u32 otp)
208 *val = readl(base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
210 return bsec_check_error(base, otp);
214 * bsec_write_shadow() - write value in BSEC data register in shadow
215 * @base: base address of bsec IP
216 * @val: value to write
217 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
218 * Return: 0 if no error
220 static int bsec_write_shadow(u32 base, u32 val, u32 otp)
222 /* check if programming of otp is locked */
223 if (bsec_read_SW_lock(base, otp))
224 pr_debug("bsec : OTP %d is lock, write will be ignore\n", otp);
226 writel(val, base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
228 return bsec_check_error(base, otp);
232 * bsec_program_otp() - program a bit in SAFMEM
233 * @base: base address of bsec IP
234 * @val: value to program
235 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
236 * after the function the otp data is not refreshed in shadow
237 * Return: 0 if no error
239 static int bsec_program_otp(long base, u32 val, u32 otp)
242 bool power_up = false;
244 if (bsec_read_SP_lock(base, otp))
245 pr_debug("bsec : OTP %d locked, prog will be ignore\n", otp);
247 if (readl(base + BSEC_OTP_LOCK_OFF) & (1 << BSEC_LOCK_PROGRAM))
248 pr_debug("bsec : Global lock, prog will be ignore\n");
250 /* check if safemem is power up */
251 if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) {
252 ret = bsec_power_safmem(base, true);
258 /* set value in write register*/
259 writel(val, base + BSEC_OTP_WRDATA_OFF);
261 /* set BSEC_OTP_CTRL_OFF with the otp value */
262 writel(otp | BSEC_WRITE, base + BSEC_OTP_CTRL_OFF);
264 /* check otp status*/
265 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
266 val, (val & BSEC_MODE_BUSY_MASK) == 0,
271 if (val & BSEC_MODE_PROGFAIL_MASK)
274 ret = bsec_check_error(base, otp);
277 bsec_power_safmem(base, false);
282 /* BSEC MISC driver *******************************************************/
283 struct stm32mp_bsec_plat {
287 static int stm32mp_bsec_read_otp(struct udevice *dev, u32 *val, u32 otp)
289 struct stm32mp_bsec_plat *plat;
293 if (IS_ENABLED(CONFIG_TFABOOT))
294 return stm32_smc(STM32_SMC_BSEC,
298 plat = dev_get_plat(dev);
300 /* read current shadow value */
301 ret = bsec_read_shadow(plat->base, &tmp_data, otp);
305 /* copy otp in shadow */
306 ret = bsec_shadow_register(plat->base, otp);
310 ret = bsec_read_shadow(plat->base, val, otp);
314 /* restore shadow value */
315 ret = bsec_write_shadow(plat->base, tmp_data, otp);
320 static int stm32mp_bsec_read_shadow(struct udevice *dev, u32 *val, u32 otp)
322 struct stm32mp_bsec_plat *plat;
324 if (IS_ENABLED(CONFIG_TFABOOT))
325 return stm32_smc(STM32_SMC_BSEC,
326 STM32_SMC_READ_SHADOW,
329 plat = dev_get_plat(dev);
331 return bsec_read_shadow(plat->base, val, otp);
334 static int stm32mp_bsec_read_lock(struct udevice *dev, u32 *val, u32 otp)
336 struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
338 /* return OTP permanent write lock status */
339 *val = bsec_read_lock(plat->base + BSEC_WRLOCK_OFF, otp);
344 static int stm32mp_bsec_write_otp(struct udevice *dev, u32 val, u32 otp)
346 struct stm32mp_bsec_plat *plat;
348 if (IS_ENABLED(CONFIG_TFABOOT))
349 return stm32_smc_exec(STM32_SMC_BSEC,
353 plat = dev_get_plat(dev);
355 return bsec_program_otp(plat->base, val, otp);
359 static int stm32mp_bsec_write_shadow(struct udevice *dev, u32 val, u32 otp)
361 struct stm32mp_bsec_plat *plat;
363 if (IS_ENABLED(CONFIG_TFABOOT))
364 return stm32_smc_exec(STM32_SMC_BSEC,
365 STM32_SMC_WRITE_SHADOW,
368 plat = dev_get_plat(dev);
370 return bsec_write_shadow(plat->base, val, otp);
373 static int stm32mp_bsec_write_lock(struct udevice *dev, u32 val, u32 otp)
375 if (!IS_ENABLED(CONFIG_TFABOOT))
379 return stm32_smc_exec(STM32_SMC_BSEC,
380 STM32_SMC_WRLOCK_OTP,
383 return 0; /* nothing to do */
388 static int stm32mp_bsec_read(struct udevice *dev, int offset,
393 bool shadow = true, lock = false;
394 int nb_otp = size / sizeof(u32);
396 unsigned int offs = offset;
398 if (offs >= STM32_BSEC_LOCK_OFFSET) {
399 offs -= STM32_BSEC_LOCK_OFFSET;
401 } else if (offs >= STM32_BSEC_OTP_OFFSET) {
402 offs -= STM32_BSEC_OTP_OFFSET;
406 if ((offs % 4) || (size % 4))
409 otp = offs / sizeof(u32);
411 for (i = otp; i < (otp + nb_otp) && i <= BSEC_OTP_MAX_VALUE; i++) {
412 u32 *addr = &((u32 *)buf)[i - otp];
415 ret = stm32mp_bsec_read_lock(dev, addr, i);
417 ret = stm32mp_bsec_read_shadow(dev, addr, i);
419 ret = stm32mp_bsec_read_otp(dev, addr, i);
427 return (i - otp) * 4;
430 static int stm32mp_bsec_write(struct udevice *dev, int offset,
431 const void *buf, int size)
435 bool shadow = true, lock = false;
436 int nb_otp = size / sizeof(u32);
438 unsigned int offs = offset;
440 if (offs >= STM32_BSEC_LOCK_OFFSET) {
441 offs -= STM32_BSEC_LOCK_OFFSET;
443 } else if (offs >= STM32_BSEC_OTP_OFFSET) {
444 offs -= STM32_BSEC_OTP_OFFSET;
448 if ((offs % 4) || (size % 4))
451 otp = offs / sizeof(u32);
453 for (i = otp; i < otp + nb_otp && i <= BSEC_OTP_MAX_VALUE; i++) {
454 u32 *val = &((u32 *)buf)[i - otp];
457 ret = stm32mp_bsec_write_lock(dev, *val, i);
459 ret = stm32mp_bsec_write_shadow(dev, *val, i);
461 ret = stm32mp_bsec_write_otp(dev, *val, i);
468 return (i - otp) * 4;
471 static const struct misc_ops stm32mp_bsec_ops = {
472 .read = stm32mp_bsec_read,
473 .write = stm32mp_bsec_write,
476 static int stm32mp_bsec_of_to_plat(struct udevice *dev)
478 struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
480 plat->base = (u32)dev_read_addr_ptr(dev);
485 static int stm32mp_bsec_probe(struct udevice *dev)
488 struct stm32mp_bsec_plat *plat;
491 * update unlocked shadow for OTP cleared by the rom code
492 * only executed in U-Boot proper when TF-A is not used
495 if (!IS_ENABLED(CONFIG_TFABOOT) && !IS_ENABLED(CONFIG_SPL_BUILD)) {
496 plat = dev_get_plat(dev);
498 for (otp = 57; otp <= BSEC_OTP_MAX_VALUE; otp++)
499 if (!bsec_read_SR_lock(plat->base, otp))
500 bsec_shadow_register(plat->base, otp);
506 static const struct udevice_id stm32mp_bsec_ids[] = {
507 { .compatible = "st,stm32mp15-bsec" },
511 U_BOOT_DRIVER(stm32mp_bsec) = {
512 .name = "stm32mp_bsec",
514 .of_match = stm32mp_bsec_ids,
515 .of_to_plat = stm32mp_bsec_of_to_plat,
516 .plat_auto = sizeof(struct stm32mp_bsec_plat),
517 .ops = &stm32mp_bsec_ops,
518 .probe = stm32mp_bsec_probe,
521 bool bsec_dbgswenable(void)
524 struct stm32mp_bsec_plat *plat;
527 ret = uclass_get_device_by_driver(UCLASS_MISC,
528 DM_DRIVER_GET(stm32mp_bsec), &dev);
530 pr_debug("bsec driver not available\n");
534 plat = dev_get_plat(dev);
535 if (readl(plat->base + BSEC_DENABLE_OFF) & BSEC_DENABLE_DBGSWENABLE)