1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
6 #define LOG_CATEGORY UCLASS_MISC
14 #include <asm/arch/bsec.h>
15 #include <asm/arch/stm32mp1_smc.h>
16 #include <dm/device_compat.h>
17 #include <linux/arm-smccc.h>
18 #include <linux/iopoll.h>
20 #define BSEC_OTP_MAX_VALUE 95
21 #define BSEC_OTP_UPPER_START 32
22 #define BSEC_TIMEOUT_US 10000
24 /* BSEC REGISTER OFFSET (base relative) */
25 #define BSEC_OTP_CONF_OFF 0x000
26 #define BSEC_OTP_CTRL_OFF 0x004
27 #define BSEC_OTP_WRDATA_OFF 0x008
28 #define BSEC_OTP_STATUS_OFF 0x00C
29 #define BSEC_OTP_LOCK_OFF 0x010
30 #define BSEC_DENABLE_OFF 0x014
31 #define BSEC_DISTURBED_OFF 0x01C
32 #define BSEC_ERROR_OFF 0x034
33 #define BSEC_WRLOCK_OFF 0x04C /* OTP write permananet lock */
34 #define BSEC_SPLOCK_OFF 0x064 /* OTP write sticky lock */
35 #define BSEC_SWLOCK_OFF 0x07C /* shadow write sticky lock */
36 #define BSEC_SRLOCK_OFF 0x094 /* shadow read sticky lock */
37 #define BSEC_OTP_DATA_OFF 0x200
39 /* BSEC_CONFIGURATION Register MASK */
40 #define BSEC_CONF_POWER_UP 0x001
42 /* BSEC_CONTROL Register */
43 #define BSEC_READ 0x000
44 #define BSEC_WRITE 0x100
45 #define BSEC_LOCK 0x200
48 #define OTP_LOCK_MASK 0x1F
49 #define OTP_LOCK_BANK_SHIFT 0x05
50 #define OTP_LOCK_BIT_MASK 0x01
53 #define BSEC_MODE_BUSY_MASK 0x08
54 #define BSEC_MODE_PROGFAIL_MASK 0x10
55 #define BSEC_MODE_PWR_MASK 0x20
57 /* DENABLE Register */
58 #define BSEC_DENABLE_DBGSWENABLE BIT(10)
61 * OTP Lock services definition
62 * Value must corresponding to the bit number in the register
64 #define BSEC_LOCK_PROGRAM 0x04
67 * OTP status: bit 0 permanent lock
69 #define BSEC_LOCK_PERM BIT(0)
72 * bsec_lock() - manage lock for each type SR/SP/SW
73 * @address: address of bsec IP register
74 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
75 * Return: true if locked else false
77 static bool bsec_read_lock(u32 address, u32 otp)
82 bit = 1 << (otp & OTP_LOCK_MASK);
83 bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
85 return !!(readl(address + bank) & bit);
89 * bsec_check_error() - Check status of one otp
90 * @base: base address of bsec IP
91 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
92 * Return: 0 if no error, -EAGAIN or -ENOTSUPP
94 static u32 bsec_check_error(u32 base, u32 otp)
99 bit = 1 << (otp & OTP_LOCK_MASK);
100 bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
102 if (readl(base + BSEC_DISTURBED_OFF + bank) & bit)
104 else if (readl(base + BSEC_ERROR_OFF + bank) & bit)
111 * bsec_read_SR_lock() - read SR lock (Shadowing)
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_SR_lock(u32 base, u32 otp)
118 return bsec_read_lock(base + BSEC_SRLOCK_OFF, otp);
122 * bsec_read_SP_lock() - read SP lock (program Lock)
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_SP_lock(u32 base, u32 otp)
129 return bsec_read_lock(base + BSEC_SPLOCK_OFF, otp);
133 * bsec_SW_lock() - manage SW lock (Write in Shadow)
134 * @base: base address of bsec IP
135 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
136 * Return: true if locked else false
138 static bool bsec_read_SW_lock(u32 base, u32 otp)
140 return bsec_read_lock(base + BSEC_SWLOCK_OFF, otp);
144 * bsec_power_safmem() - Activate or deactivate safmem power
145 * @base: base address of bsec IP
146 * @power: true to power up , false to power down
147 * Return: 0 if succeed
149 static int bsec_power_safmem(u32 base, bool power)
155 setbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
156 mask = BSEC_MODE_PWR_MASK;
158 clrbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
163 return readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
164 val, (val & BSEC_MODE_PWR_MASK) == mask,
169 * bsec_shadow_register() - copy safmen otp to bsec data
170 * @dev: bsec IP device
171 * @base: base address of bsec IP
172 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
173 * Return: 0 if no error
175 static int bsec_shadow_register(struct udevice *dev, u32 base, u32 otp)
179 bool power_up = false;
181 /* check if shadowing of otp is locked */
182 if (bsec_read_SR_lock(base, otp))
183 dev_dbg(dev, "OTP %d is locked and refreshed with 0\n",
186 /* check if safemem is power up */
187 val = readl(base + BSEC_OTP_STATUS_OFF);
188 if (!(val & BSEC_MODE_PWR_MASK)) {
189 ret = bsec_power_safmem(base, true);
194 /* set BSEC_OTP_CTRL_OFF with the otp value*/
195 writel(otp | BSEC_READ, base + BSEC_OTP_CTRL_OFF);
197 /* check otp status*/
198 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
199 val, (val & BSEC_MODE_BUSY_MASK) == 0,
204 ret = bsec_check_error(base, otp);
207 bsec_power_safmem(base, false);
213 * bsec_read_shadow() - read an otp data value from shadow
214 * @dev: bsec IP device
215 * @base: base address of bsec IP
217 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
218 * Return: 0 if no error
220 static int bsec_read_shadow(struct udevice *dev, u32 base, u32 *val, u32 otp)
222 *val = readl(base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
224 return bsec_check_error(base, otp);
228 * bsec_write_shadow() - write value in BSEC data register in shadow
229 * @dev: bsec IP device
230 * @base: base address of bsec IP
231 * @val: value to write
232 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
233 * Return: 0 if no error
235 static int bsec_write_shadow(struct udevice *dev, u32 base, u32 val, u32 otp)
237 /* check if programming of otp is locked */
238 if (bsec_read_SW_lock(base, otp))
239 dev_dbg(dev, "OTP %d is lock, write will be ignore\n", otp);
241 writel(val, base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
243 return bsec_check_error(base, otp);
247 * bsec_program_otp() - program a bit in SAFMEM
248 * @dev: bsec IP device
249 * @base: base address of bsec IP
250 * @val: value to program
251 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
252 * after the function the otp data is not refreshed in shadow
253 * Return: 0 if no error
255 static int bsec_program_otp(struct udevice *dev, long base, u32 val, u32 otp)
258 bool power_up = false;
260 if (bsec_read_SP_lock(base, otp))
261 dev_dbg(dev, "OTP %d locked, prog will be ignore\n", otp);
263 if (readl(base + BSEC_OTP_LOCK_OFF) & (1 << BSEC_LOCK_PROGRAM))
264 dev_dbg(dev, "Global lock, prog will be ignore\n");
266 /* check if safemem is power up */
267 if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) {
268 ret = bsec_power_safmem(base, true);
274 /* set value in write register*/
275 writel(val, base + BSEC_OTP_WRDATA_OFF);
277 /* set BSEC_OTP_CTRL_OFF with the otp value */
278 writel(otp | BSEC_WRITE, base + BSEC_OTP_CTRL_OFF);
280 /* check otp status*/
281 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
282 val, (val & BSEC_MODE_BUSY_MASK) == 0,
287 if (val & BSEC_MODE_PROGFAIL_MASK)
290 ret = bsec_check_error(base, otp);
293 bsec_power_safmem(base, false);
299 * bsec_permanent_lock_otp() - permanent lock of OTP in SAFMEM
300 * @dev: bsec IP device
301 * @base: base address of bsec IP
302 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
303 * Return: 0 if no error
305 static int bsec_permanent_lock_otp(struct udevice *dev, long base, uint32_t otp)
308 bool power_up = false;
311 /* check if safemem is power up */
312 if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) {
313 ret = bsec_power_safmem(base, true);
321 * low OTPs = 2 bits word for low OTPs, 1 bits per word for upper OTP
322 * and only 16 bits used in WRDATA
324 if (otp < BSEC_OTP_UPPER_START) {
326 val = 0x03 << ((otp * 2) & 0xF);
328 addr = BSEC_OTP_UPPER_START / 8 +
329 ((otp - BSEC_OTP_UPPER_START) / 16);
330 val = 0x01 << (otp & 0xF);
333 /* set value in write register*/
334 writel(val, base + BSEC_OTP_WRDATA_OFF);
336 /* set BSEC_OTP_CTRL_OFF with the otp addr and lock request*/
337 writel(addr | BSEC_WRITE | BSEC_LOCK, base + BSEC_OTP_CTRL_OFF);
339 /* check otp status*/
340 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
341 val, (val & BSEC_MODE_BUSY_MASK) == 0,
346 if (val & BSEC_MODE_PROGFAIL_MASK)
349 ret = bsec_check_error(base, otp);
352 bsec_power_safmem(base, false);
357 /* BSEC MISC driver *******************************************************/
358 struct stm32mp_bsec_plat {
362 static int stm32mp_bsec_read_otp(struct udevice *dev, u32 *val, u32 otp)
364 struct stm32mp_bsec_plat *plat;
368 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
369 return stm32_smc(STM32_SMC_BSEC,
373 plat = dev_get_plat(dev);
375 /* read current shadow value */
376 ret = bsec_read_shadow(dev, plat->base, &tmp_data, otp);
380 /* copy otp in shadow */
381 ret = bsec_shadow_register(dev, plat->base, otp);
385 ret = bsec_read_shadow(dev, plat->base, val, otp);
389 /* restore shadow value */
390 ret = bsec_write_shadow(dev, plat->base, tmp_data, otp);
395 static int stm32mp_bsec_read_shadow(struct udevice *dev, u32 *val, u32 otp)
397 struct stm32mp_bsec_plat *plat;
399 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
400 return stm32_smc(STM32_SMC_BSEC,
401 STM32_SMC_READ_SHADOW,
404 plat = dev_get_plat(dev);
406 return bsec_read_shadow(dev, plat->base, val, otp);
409 static int stm32mp_bsec_read_lock(struct udevice *dev, u32 *val, u32 otp)
411 struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
414 /* return OTP permanent write lock status */
415 wrlock = bsec_read_lock(plat->base + BSEC_WRLOCK_OFF, otp);
419 *val = BSEC_LOCK_PERM;
424 static int stm32mp_bsec_write_otp(struct udevice *dev, u32 val, u32 otp)
426 struct stm32mp_bsec_plat *plat;
428 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
429 return stm32_smc_exec(STM32_SMC_BSEC,
433 plat = dev_get_plat(dev);
435 return bsec_program_otp(dev, plat->base, val, otp);
439 static int stm32mp_bsec_write_shadow(struct udevice *dev, u32 val, u32 otp)
441 struct stm32mp_bsec_plat *plat;
443 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
444 return stm32_smc_exec(STM32_SMC_BSEC,
445 STM32_SMC_WRITE_SHADOW,
448 plat = dev_get_plat(dev);
450 return bsec_write_shadow(dev, plat->base, val, otp);
453 static int stm32mp_bsec_write_lock(struct udevice *dev, u32 val, u32 otp)
455 struct stm32mp_bsec_plat *plat;
457 /* only permanent write lock is supported in U-Boot */
458 if (!(val & BSEC_LOCK_PERM)) {
459 dev_dbg(dev, "lock option without BSEC_LOCK_PERM: %x\n", val);
460 return 0; /* nothing to do */
463 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
464 return stm32_smc_exec(STM32_SMC_BSEC,
465 STM32_SMC_WRLOCK_OTP,
468 plat = dev_get_plat(dev);
470 return bsec_permanent_lock_otp(dev, plat->base, otp);
475 static int stm32mp_bsec_read(struct udevice *dev, int offset,
480 bool shadow = true, lock = false;
481 int nb_otp = size / sizeof(u32);
483 unsigned int offs = offset;
485 if (offs >= STM32_BSEC_LOCK_OFFSET) {
486 offs -= STM32_BSEC_LOCK_OFFSET;
488 } else if (offs >= STM32_BSEC_OTP_OFFSET) {
489 offs -= STM32_BSEC_OTP_OFFSET;
493 if ((offs % 4) || (size % 4))
496 otp = offs / sizeof(u32);
498 for (i = otp; i < (otp + nb_otp) && i <= BSEC_OTP_MAX_VALUE; i++) {
499 u32 *addr = &((u32 *)buf)[i - otp];
502 ret = stm32mp_bsec_read_lock(dev, addr, i);
504 ret = stm32mp_bsec_read_shadow(dev, addr, i);
506 ret = stm32mp_bsec_read_otp(dev, addr, i);
514 return (i - otp) * 4;
517 static int stm32mp_bsec_write(struct udevice *dev, int offset,
518 const void *buf, int size)
522 bool shadow = true, lock = false;
523 int nb_otp = size / sizeof(u32);
525 unsigned int offs = offset;
527 if (offs >= STM32_BSEC_LOCK_OFFSET) {
528 offs -= STM32_BSEC_LOCK_OFFSET;
530 } else if (offs >= STM32_BSEC_OTP_OFFSET) {
531 offs -= STM32_BSEC_OTP_OFFSET;
535 if ((offs % 4) || (size % 4))
538 otp = offs / sizeof(u32);
540 for (i = otp; i < otp + nb_otp && i <= BSEC_OTP_MAX_VALUE; i++) {
541 u32 *val = &((u32 *)buf)[i - otp];
544 ret = stm32mp_bsec_write_lock(dev, *val, i);
546 ret = stm32mp_bsec_write_shadow(dev, *val, i);
548 ret = stm32mp_bsec_write_otp(dev, *val, i);
555 return (i - otp) * 4;
558 static const struct misc_ops stm32mp_bsec_ops = {
559 .read = stm32mp_bsec_read,
560 .write = stm32mp_bsec_write,
563 static int stm32mp_bsec_of_to_plat(struct udevice *dev)
565 struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
567 plat->base = (u32)dev_read_addr_ptr(dev);
572 static int stm32mp_bsec_probe(struct udevice *dev)
575 struct stm32mp_bsec_plat *plat;
576 struct clk_bulk clk_bulk;
579 ret = clk_get_bulk(dev, &clk_bulk);
581 ret = clk_enable_bulk(&clk_bulk);
587 * update unlocked shadow for OTP cleared by the rom code
588 * only executed in SPL, it is done in TF-A for TFABOOT
590 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
591 plat = dev_get_plat(dev);
593 for (otp = 57; otp <= BSEC_OTP_MAX_VALUE; otp++)
594 if (!bsec_read_SR_lock(plat->base, otp))
595 bsec_shadow_register(dev, plat->base, otp);
601 static const struct udevice_id stm32mp_bsec_ids[] = {
602 { .compatible = "st,stm32mp15-bsec" },
606 U_BOOT_DRIVER(stm32mp_bsec) = {
607 .name = "stm32mp_bsec",
609 .of_match = stm32mp_bsec_ids,
610 .of_to_plat = stm32mp_bsec_of_to_plat,
611 .plat_auto = sizeof(struct stm32mp_bsec_plat),
612 .ops = &stm32mp_bsec_ops,
613 .probe = stm32mp_bsec_probe,
616 bool bsec_dbgswenable(void)
619 struct stm32mp_bsec_plat *plat;
622 ret = uclass_get_device_by_driver(UCLASS_MISC,
623 DM_DRIVER_GET(stm32mp_bsec), &dev);
625 log_debug("bsec driver not available\n");
629 plat = dev_get_plat(dev);
630 if (readl(plat->base + BSEC_DENABLE_OFF) & BSEC_DENABLE_DBGSWENABLE)