1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
10 #include <asm/arch/stm32mp1_smc.h>
11 #include <linux/arm-smccc.h>
12 #include <linux/iopoll.h>
14 #define BSEC_OTP_MAX_VALUE 95
16 #ifndef CONFIG_STM32MP1_TRUSTED
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_DISTURBED_OFF 0x01C
26 #define BSEC_ERROR_OFF 0x034
27 #define BSEC_SPLOCK_OFF 0x064 /* Program safmem sticky lock */
28 #define BSEC_SWLOCK_OFF 0x07C /* write in OTP sticky lock */
29 #define BSEC_SRLOCK_OFF 0x094 /* shadowing sticky lock */
30 #define BSEC_OTP_DATA_OFF 0x200
32 /* BSEC_CONFIGURATION Register MASK */
33 #define BSEC_CONF_POWER_UP 0x001
35 /* BSEC_CONTROL Register */
36 #define BSEC_READ 0x000
37 #define BSEC_WRITE 0x100
40 #define OTP_LOCK_MASK 0x1F
41 #define OTP_LOCK_BANK_SHIFT 0x05
42 #define OTP_LOCK_BIT_MASK 0x01
45 #define BSEC_MODE_BUSY_MASK 0x08
46 #define BSEC_MODE_PROGFAIL_MASK 0x10
47 #define BSEC_MODE_PWR_MASK 0x20
50 * OTP Lock services definition
51 * Value must corresponding to the bit number in the register
53 #define BSEC_LOCK_PROGRAM 0x04
56 * bsec_check_error() - Check status of one otp
57 * @base: base address of bsec IP
58 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
59 * Return: 0 if no error, -EAGAIN or -ENOTSUPP
61 static u32 bsec_check_error(u32 base, u32 otp)
66 bit = 1 << (otp & OTP_LOCK_MASK);
67 bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
69 if (readl(base + BSEC_DISTURBED_OFF + bank) & bit)
71 else if (readl(base + BSEC_ERROR_OFF + bank) & bit)
78 * bsec_lock() - manage lock for each type SR/SP/SW
79 * @address: address of bsec IP register
80 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
81 * Return: true if locked else false
83 static bool bsec_read_lock(u32 address, u32 otp)
88 bit = 1 << (otp & OTP_LOCK_MASK);
89 bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
91 return !!(readl(address + bank) & bit);
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
100 static bool bsec_read_SR_lock(u32 base, u32 otp)
102 return bsec_read_lock(base + BSEC_SRLOCK_OFF, otp);
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
111 static bool bsec_read_SP_lock(u32 base, u32 otp)
113 return bsec_read_lock(base + BSEC_SPLOCK_OFF, otp);
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
122 static bool bsec_read_SW_lock(u32 base, u32 otp)
124 return bsec_read_lock(base + BSEC_SWLOCK_OFF, otp);
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
133 static int bsec_power_safmem(u32 base, bool power)
139 setbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
140 mask = BSEC_MODE_PWR_MASK;
142 clrbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
147 return readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
148 val, (val & BSEC_MODE_PWR_MASK) == mask,
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
158 static int bsec_shadow_register(u32 base, u32 otp)
162 bool power_up = false;
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);
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);
176 /* set BSEC_OTP_CTRL_OFF with the otp value*/
177 writel(otp | BSEC_READ, base + BSEC_OTP_CTRL_OFF);
179 /* check otp status*/
180 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
181 val, (val & BSEC_MODE_BUSY_MASK) == 0,
186 ret = bsec_check_error(base, otp);
189 bsec_power_safmem(base, false);
195 * bsec_read_shadow() - read an otp data value from shadow
196 * @base: base address of bsec IP
198 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
199 * Return: 0 if no error
201 static int bsec_read_shadow(u32 base, u32 *val, u32 otp)
203 *val = readl(base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
205 return bsec_check_error(base, otp);
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
215 static int bsec_write_shadow(u32 base, u32 val, u32 otp)
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);
221 writel(val, base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
223 return bsec_check_error(base, otp);
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
234 static int bsec_program_otp(long base, u32 val, u32 otp)
237 bool power_up = false;
239 if (bsec_read_SP_lock(base, otp))
240 pr_debug("bsec : OTP %d locked, prog will be ignore\n", otp);
242 if (readl(base + BSEC_OTP_LOCK_OFF) & (1 << BSEC_LOCK_PROGRAM))
243 pr_debug("bsec : Global lock, prog will be ignore\n");
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);
253 /* set value in write register*/
254 writel(val, base + BSEC_OTP_WRDATA_OFF);
256 /* set BSEC_OTP_CTRL_OFF with the otp value */
257 writel(otp | BSEC_WRITE, base + BSEC_OTP_CTRL_OFF);
259 /* check otp status*/
260 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
261 val, (val & BSEC_MODE_BUSY_MASK) == 0,
266 if (val & BSEC_MODE_PROGFAIL_MASK)
269 ret = bsec_check_error(base, otp);
272 bsec_power_safmem(base, false);
276 #endif /* CONFIG_STM32MP1_TRUSTED */
278 /* BSEC MISC driver *******************************************************/
279 struct stm32mp_bsec_platdata {
283 static int stm32mp_bsec_read_otp(struct udevice *dev, u32 *val, u32 otp)
285 #ifdef CONFIG_STM32MP1_TRUSTED
286 return stm32_smc(STM32_SMC_BSEC,
290 struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev);
294 /* read current shadow value */
295 ret = bsec_read_shadow(plat->base, &tmp_data, otp);
299 /* copy otp in shadow */
300 ret = bsec_shadow_register(plat->base, otp);
304 ret = bsec_read_shadow(plat->base, val, otp);
308 /* restore shadow value */
309 ret = bsec_write_shadow(plat->base, tmp_data, otp);
314 static int stm32mp_bsec_read_shadow(struct udevice *dev, u32 *val, u32 otp)
316 #ifdef CONFIG_STM32MP1_TRUSTED
317 return stm32_smc(STM32_SMC_BSEC,
318 STM32_SMC_READ_SHADOW,
321 struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev);
323 return bsec_read_shadow(plat->base, val, otp);
327 static int stm32mp_bsec_write_otp(struct udevice *dev, u32 val, u32 otp)
329 #ifdef CONFIG_STM32MP1_TRUSTED
330 return stm32_smc_exec(STM32_SMC_BSEC,
334 struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev);
336 return bsec_program_otp(plat->base, val, otp);
340 static int stm32mp_bsec_write_shadow(struct udevice *dev, u32 val, u32 otp)
342 #ifdef CONFIG_STM32MP1_TRUSTED
343 return stm32_smc_exec(STM32_SMC_BSEC,
344 STM32_SMC_WRITE_SHADOW,
347 struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev);
349 return bsec_write_shadow(plat->base, val, otp);
353 static int stm32mp_bsec_read(struct udevice *dev, int offset,
359 int nb_otp = size / sizeof(u32);
361 unsigned int offs = offset;
363 if (offs >= STM32_BSEC_OTP_OFFSET) {
364 offs -= STM32_BSEC_OTP_OFFSET;
367 otp = offs / sizeof(u32);
369 if (otp < 0 || (otp + nb_otp - 1) > BSEC_OTP_MAX_VALUE) {
370 dev_err(dev, "wrong value for otp, max value : %i\n",
375 for (i = otp; i < (otp + nb_otp); i++) {
376 u32 *addr = &((u32 *)buf)[i - otp];
379 ret = stm32mp_bsec_read_shadow(dev, addr, i);
381 ret = stm32mp_bsec_read_otp(dev, addr, i);
389 static int stm32mp_bsec_write(struct udevice *dev, int offset,
390 const void *buf, int size)
395 int nb_otp = size / sizeof(u32);
397 unsigned int offs = offset;
399 if (offs >= STM32_BSEC_OTP_OFFSET) {
400 offs -= STM32_BSEC_OTP_OFFSET;
403 otp = offs / sizeof(u32);
405 if (otp < 0 || (otp + nb_otp - 1) > BSEC_OTP_MAX_VALUE) {
406 dev_err(dev, "wrong value for otp, max value : %d\n",
411 for (i = otp; i < otp + nb_otp; i++) {
412 u32 *val = &((u32 *)buf)[i - otp];
415 ret = stm32mp_bsec_write_shadow(dev, *val, i);
417 ret = stm32mp_bsec_write_otp(dev, *val, i);
424 static const struct misc_ops stm32mp_bsec_ops = {
425 .read = stm32mp_bsec_read,
426 .write = stm32mp_bsec_write,
429 static int stm32mp_bsec_ofdata_to_platdata(struct udevice *dev)
431 struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev);
433 plat->base = (u32)dev_read_addr_ptr(dev);
438 #ifndef CONFIG_STM32MP1_TRUSTED
439 static int stm32mp_bsec_probe(struct udevice *dev)
442 struct stm32mp_bsec_platdata *plat = dev_get_platdata(dev);
444 /* update unlocked shadow for OTP cleared by the rom code */
445 for (otp = 57; otp <= BSEC_OTP_MAX_VALUE; otp++)
446 if (!bsec_read_SR_lock(plat->base, otp))
447 bsec_shadow_register(plat->base, otp);
453 static const struct udevice_id stm32mp_bsec_ids[] = {
454 { .compatible = "st,stm32mp15-bsec" },
458 U_BOOT_DRIVER(stm32mp_bsec) = {
459 .name = "stm32mp_bsec",
461 .of_match = stm32mp_bsec_ids,
462 .ofdata_to_platdata = stm32mp_bsec_ofdata_to_platdata,
463 .platdata_auto_alloc_size = sizeof(struct stm32mp_bsec_platdata),
464 .ops = &stm32mp_bsec_ops,
465 #ifndef CONFIG_STM32MP1_TRUSTED
466 .probe = stm32mp_bsec_probe,