blob: fe39bd80cfe454809dd93e560bde59dc14c71d3f [file] [log] [blame]
Patrick Delaunay19f58992018-05-17 15:24:05 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 */
5
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +01006#define LOG_CATEGORY UCLASS_MISC
7
Patrick Delaunay19f58992018-05-17 15:24:05 +02008#include <common.h>
Patrick Delaunay622c9562021-02-25 13:43:07 +01009#include <clk.h>
Patrick Delaunay19f58992018-05-17 15:24:05 +020010#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Patrick Delaunay19f58992018-05-17 15:24:05 +020012#include <misc.h>
13#include <asm/io.h>
Patrick Delaunaybd3f60d2020-06-16 18:27:44 +020014#include <asm/arch/bsec.h>
Patrick Delaunayd859c612019-02-12 11:44:40 +010015#include <asm/arch/stm32mp1_smc.h>
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +010016#include <dm/device_compat.h>
Patrick Delaunayd859c612019-02-12 11:44:40 +010017#include <linux/arm-smccc.h>
Patrick Delaunayee7d7722019-04-18 17:32:39 +020018#include <linux/iopoll.h>
Patrick Delaunay19f58992018-05-17 15:24:05 +020019
20#define BSEC_OTP_MAX_VALUE 95
Patrick Delaunay19f58992018-05-17 15:24:05 +020021#define BSEC_TIMEOUT_US 10000
22
23/* BSEC REGISTER OFFSET (base relative) */
24#define BSEC_OTP_CONF_OFF 0x000
25#define BSEC_OTP_CTRL_OFF 0x004
26#define BSEC_OTP_WRDATA_OFF 0x008
27#define BSEC_OTP_STATUS_OFF 0x00C
28#define BSEC_OTP_LOCK_OFF 0x010
Patrick Delaunaybd3f60d2020-06-16 18:27:44 +020029#define BSEC_DENABLE_OFF 0x014
Patrick Delaunay19f58992018-05-17 15:24:05 +020030#define BSEC_DISTURBED_OFF 0x01C
31#define BSEC_ERROR_OFF 0x034
Patrick Delaunay7ae22d72020-02-12 19:37:38 +010032#define BSEC_WRLOCK_OFF 0x04C /* OTP write permananet lock */
33#define BSEC_SPLOCK_OFF 0x064 /* OTP write sticky lock */
34#define BSEC_SWLOCK_OFF 0x07C /* shadow write sticky lock */
35#define BSEC_SRLOCK_OFF 0x094 /* shadow read sticky lock */
Patrick Delaunay19f58992018-05-17 15:24:05 +020036#define BSEC_OTP_DATA_OFF 0x200
37
38/* BSEC_CONFIGURATION Register MASK */
39#define BSEC_CONF_POWER_UP 0x001
40
41/* BSEC_CONTROL Register */
42#define BSEC_READ 0x000
43#define BSEC_WRITE 0x100
44
45/* LOCK Register */
46#define OTP_LOCK_MASK 0x1F
47#define OTP_LOCK_BANK_SHIFT 0x05
48#define OTP_LOCK_BIT_MASK 0x01
49
50/* STATUS Register */
51#define BSEC_MODE_BUSY_MASK 0x08
52#define BSEC_MODE_PROGFAIL_MASK 0x10
53#define BSEC_MODE_PWR_MASK 0x20
54
Patrick Delaunaybd3f60d2020-06-16 18:27:44 +020055/* DENABLE Register */
56#define BSEC_DENABLE_DBGSWENABLE BIT(10)
57
Patrick Delaunay19f58992018-05-17 15:24:05 +020058/*
59 * OTP Lock services definition
60 * Value must corresponding to the bit number in the register
61 */
62#define BSEC_LOCK_PROGRAM 0x04
63
64/**
Patrick Delaunay7ae22d72020-02-12 19:37:38 +010065 * bsec_lock() - manage lock for each type SR/SP/SW
66 * @address: address of bsec IP register
67 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
68 * Return: true if locked else false
69 */
70static bool bsec_read_lock(u32 address, u32 otp)
71{
72 u32 bit;
73 u32 bank;
74
75 bit = 1 << (otp & OTP_LOCK_MASK);
76 bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
77
78 return !!(readl(address + bank) & bit);
79}
80
Patrick Delaunay7ae22d72020-02-12 19:37:38 +010081/**
Patrick Delaunay19f58992018-05-17 15:24:05 +020082 * bsec_check_error() - Check status of one otp
83 * @base: base address of bsec IP
84 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
85 * Return: 0 if no error, -EAGAIN or -ENOTSUPP
86 */
87static u32 bsec_check_error(u32 base, u32 otp)
88{
89 u32 bit;
90 u32 bank;
91
92 bit = 1 << (otp & OTP_LOCK_MASK);
93 bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
94
95 if (readl(base + BSEC_DISTURBED_OFF + bank) & bit)
96 return -EAGAIN;
97 else if (readl(base + BSEC_ERROR_OFF + bank) & bit)
98 return -ENOTSUPP;
99
100 return 0;
101}
102
103/**
Patrick Delaunay19f58992018-05-17 15:24:05 +0200104 * bsec_read_SR_lock() - read SR lock (Shadowing)
105 * @base: base address of bsec IP
106 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
107 * Return: true if locked else false
108 */
109static bool bsec_read_SR_lock(u32 base, u32 otp)
110{
111 return bsec_read_lock(base + BSEC_SRLOCK_OFF, otp);
112}
113
114/**
115 * bsec_read_SP_lock() - read SP lock (program Lock)
116 * @base: base address of bsec IP
117 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
118 * Return: true if locked else false
119 */
120static bool bsec_read_SP_lock(u32 base, u32 otp)
121{
122 return bsec_read_lock(base + BSEC_SPLOCK_OFF, otp);
123}
124
125/**
126 * bsec_SW_lock() - manage SW lock (Write in Shadow)
127 * @base: base address of bsec IP
128 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
129 * Return: true if locked else false
130 */
131static bool bsec_read_SW_lock(u32 base, u32 otp)
132{
133 return bsec_read_lock(base + BSEC_SWLOCK_OFF, otp);
134}
135
136/**
137 * bsec_power_safmem() - Activate or deactivate safmem power
138 * @base: base address of bsec IP
139 * @power: true to power up , false to power down
140 * Return: 0 if succeed
141 */
142static int bsec_power_safmem(u32 base, bool power)
143{
144 u32 val;
145 u32 mask;
146
147 if (power) {
148 setbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
149 mask = BSEC_MODE_PWR_MASK;
150 } else {
151 clrbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
152 mask = 0;
153 }
154
155 /* waiting loop */
156 return readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
157 val, (val & BSEC_MODE_PWR_MASK) == mask,
158 BSEC_TIMEOUT_US);
159}
160
161/**
162 * bsec_shadow_register() - copy safmen otp to bsec data
163 * @base: base address of bsec IP
164 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
165 * Return: 0 if no error
166 */
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100167static int bsec_shadow_register(struct udevice *dev, u32 base, u32 otp)
Patrick Delaunay19f58992018-05-17 15:24:05 +0200168{
169 u32 val;
170 int ret;
171 bool power_up = false;
172
173 /* check if shadowing of otp is locked */
174 if (bsec_read_SR_lock(base, otp))
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100175 dev_dbg(dev, "OTP %d is locked and refreshed with 0\n",
176 otp);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200177
178 /* check if safemem is power up */
179 val = readl(base + BSEC_OTP_STATUS_OFF);
180 if (!(val & BSEC_MODE_PWR_MASK)) {
181 ret = bsec_power_safmem(base, true);
182 if (ret)
183 return ret;
Patrick Delaunay815bc8b2019-02-27 17:01:28 +0100184 power_up = true;
Patrick Delaunay19f58992018-05-17 15:24:05 +0200185 }
186 /* set BSEC_OTP_CTRL_OFF with the otp value*/
187 writel(otp | BSEC_READ, base + BSEC_OTP_CTRL_OFF);
188
189 /* check otp status*/
190 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
191 val, (val & BSEC_MODE_BUSY_MASK) == 0,
192 BSEC_TIMEOUT_US);
193 if (ret)
194 return ret;
195
196 ret = bsec_check_error(base, otp);
197
198 if (power_up)
199 bsec_power_safmem(base, false);
200
201 return ret;
202}
203
204/**
205 * bsec_read_shadow() - read an otp data value from shadow
206 * @base: base address of bsec IP
207 * @val: read value
208 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
209 * Return: 0 if no error
210 */
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100211static int bsec_read_shadow(struct udevice *dev, u32 base, u32 *val, u32 otp)
Patrick Delaunay19f58992018-05-17 15:24:05 +0200212{
213 *val = readl(base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
214
215 return bsec_check_error(base, otp);
216}
217
218/**
219 * bsec_write_shadow() - write value in BSEC data register in shadow
220 * @base: base address of bsec IP
221 * @val: value to write
222 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
223 * Return: 0 if no error
224 */
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100225static int bsec_write_shadow(struct udevice *dev, u32 base, u32 val, u32 otp)
Patrick Delaunay19f58992018-05-17 15:24:05 +0200226{
227 /* check if programming of otp is locked */
228 if (bsec_read_SW_lock(base, otp))
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100229 dev_dbg(dev, "OTP %d is lock, write will be ignore\n", otp);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200230
231 writel(val, base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
232
233 return bsec_check_error(base, otp);
234}
235
236/**
237 * bsec_program_otp() - program a bit in SAFMEM
238 * @base: base address of bsec IP
239 * @val: value to program
240 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
241 * after the function the otp data is not refreshed in shadow
242 * Return: 0 if no error
243 */
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100244static int bsec_program_otp(struct udevice *dev, long base, u32 val, u32 otp)
Patrick Delaunay19f58992018-05-17 15:24:05 +0200245{
246 u32 ret;
247 bool power_up = false;
248
249 if (bsec_read_SP_lock(base, otp))
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100250 dev_dbg(dev, "OTP %d locked, prog will be ignore\n", otp);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200251
252 if (readl(base + BSEC_OTP_LOCK_OFF) & (1 << BSEC_LOCK_PROGRAM))
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100253 dev_dbg(dev, "Global lock, prog will be ignore\n");
Patrick Delaunay19f58992018-05-17 15:24:05 +0200254
255 /* check if safemem is power up */
256 if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) {
257 ret = bsec_power_safmem(base, true);
258 if (ret)
259 return ret;
260
261 power_up = true;
262 }
263 /* set value in write register*/
264 writel(val, base + BSEC_OTP_WRDATA_OFF);
265
266 /* set BSEC_OTP_CTRL_OFF with the otp value */
267 writel(otp | BSEC_WRITE, base + BSEC_OTP_CTRL_OFF);
268
269 /* check otp status*/
270 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
271 val, (val & BSEC_MODE_BUSY_MASK) == 0,
272 BSEC_TIMEOUT_US);
273 if (ret)
274 return ret;
275
276 if (val & BSEC_MODE_PROGFAIL_MASK)
277 ret = -EACCES;
278 else
279 ret = bsec_check_error(base, otp);
280
281 if (power_up)
282 bsec_power_safmem(base, false);
283
284 return ret;
285}
286
287/* BSEC MISC driver *******************************************************/
Simon Glass8a8d24b2020-12-03 16:55:23 -0700288struct stm32mp_bsec_plat {
Patrick Delaunay19f58992018-05-17 15:24:05 +0200289 u32 base;
290};
291
292static int stm32mp_bsec_read_otp(struct udevice *dev, u32 *val, u32 otp)
293{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700294 struct stm32mp_bsec_plat *plat;
Patrick Delaunay19f58992018-05-17 15:24:05 +0200295 u32 tmp_data = 0;
296 int ret;
297
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200298 if (IS_ENABLED(CONFIG_TFABOOT))
299 return stm32_smc(STM32_SMC_BSEC,
300 STM32_SMC_READ_OTP,
301 otp, 0, val);
302
Simon Glassc69cda22020-12-03 16:55:20 -0700303 plat = dev_get_plat(dev);
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200304
Patrick Delaunay19f58992018-05-17 15:24:05 +0200305 /* read current shadow value */
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100306 ret = bsec_read_shadow(dev, plat->base, &tmp_data, otp);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200307 if (ret)
308 return ret;
309
310 /* copy otp in shadow */
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100311 ret = bsec_shadow_register(dev, plat->base, otp);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200312 if (ret)
313 return ret;
314
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100315 ret = bsec_read_shadow(dev, plat->base, val, otp);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200316 if (ret)
317 return ret;
318
319 /* restore shadow value */
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100320 ret = bsec_write_shadow(dev, plat->base, tmp_data, otp);
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200321
Patrick Delaunay19f58992018-05-17 15:24:05 +0200322 return ret;
323}
324
325static int stm32mp_bsec_read_shadow(struct udevice *dev, u32 *val, u32 otp)
326{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700327 struct stm32mp_bsec_plat *plat;
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200328
329 if (IS_ENABLED(CONFIG_TFABOOT))
330 return stm32_smc(STM32_SMC_BSEC,
331 STM32_SMC_READ_SHADOW,
332 otp, 0, val);
333
Simon Glassc69cda22020-12-03 16:55:20 -0700334 plat = dev_get_plat(dev);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200335
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100336 return bsec_read_shadow(dev, plat->base, val, otp);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200337}
338
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100339static int stm32mp_bsec_read_lock(struct udevice *dev, u32 *val, u32 otp)
340{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700341 struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100342
343 /* return OTP permanent write lock status */
344 *val = bsec_read_lock(plat->base + BSEC_WRLOCK_OFF, otp);
345
346 return 0;
347}
348
Patrick Delaunay19f58992018-05-17 15:24:05 +0200349static int stm32mp_bsec_write_otp(struct udevice *dev, u32 val, u32 otp)
350{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700351 struct stm32mp_bsec_plat *plat;
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200352
353 if (IS_ENABLED(CONFIG_TFABOOT))
354 return stm32_smc_exec(STM32_SMC_BSEC,
355 STM32_SMC_PROG_OTP,
356 otp, val);
357
Simon Glassc69cda22020-12-03 16:55:20 -0700358 plat = dev_get_plat(dev);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200359
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100360 return bsec_program_otp(dev, plat->base, val, otp);
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200361
Patrick Delaunay19f58992018-05-17 15:24:05 +0200362}
363
364static int stm32mp_bsec_write_shadow(struct udevice *dev, u32 val, u32 otp)
365{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700366 struct stm32mp_bsec_plat *plat;
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200367
368 if (IS_ENABLED(CONFIG_TFABOOT))
369 return stm32_smc_exec(STM32_SMC_BSEC,
370 STM32_SMC_WRITE_SHADOW,
371 otp, val);
372
Simon Glassc69cda22020-12-03 16:55:20 -0700373 plat = dev_get_plat(dev);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200374
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100375 return bsec_write_shadow(dev, plat->base, val, otp);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200376}
377
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100378static int stm32mp_bsec_write_lock(struct udevice *dev, u32 val, u32 otp)
379{
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200380 if (!IS_ENABLED(CONFIG_TFABOOT))
381 return -ENOTSUPP;
382
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100383 if (val == 1)
384 return stm32_smc_exec(STM32_SMC_BSEC,
385 STM32_SMC_WRLOCK_OTP,
386 otp, 0);
387 if (val == 0)
388 return 0; /* nothing to do */
389
390 return -EINVAL;
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100391}
392
Patrick Delaunay19f58992018-05-17 15:24:05 +0200393static int stm32mp_bsec_read(struct udevice *dev, int offset,
394 void *buf, int size)
395{
396 int ret;
397 int i;
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100398 bool shadow = true, lock = false;
Patrick Delaunay19f58992018-05-17 15:24:05 +0200399 int nb_otp = size / sizeof(u32);
400 int otp;
Patrick Delaunay745b6762019-06-21 15:26:43 +0200401 unsigned int offs = offset;
Patrick Delaunay19f58992018-05-17 15:24:05 +0200402
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100403 if (offs >= STM32_BSEC_LOCK_OFFSET) {
404 offs -= STM32_BSEC_LOCK_OFFSET;
405 lock = true;
406 } else if (offs >= STM32_BSEC_OTP_OFFSET) {
Patrick Delaunay745b6762019-06-21 15:26:43 +0200407 offs -= STM32_BSEC_OTP_OFFSET;
Patrick Delaunay19f58992018-05-17 15:24:05 +0200408 shadow = false;
409 }
Patrick Delaunay0c8620d2019-08-02 13:08:02 +0200410
Patrick Delaunaydf2d1b82020-02-12 19:37:37 +0100411 if ((offs % 4) || (size % 4))
Patrick Delaunay0c8620d2019-08-02 13:08:02 +0200412 return -EINVAL;
413
Patrick Delaunay745b6762019-06-21 15:26:43 +0200414 otp = offs / sizeof(u32);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200415
Patrick Delaunay0c8620d2019-08-02 13:08:02 +0200416 for (i = otp; i < (otp + nb_otp) && i <= BSEC_OTP_MAX_VALUE; i++) {
Patrick Delaunay19f58992018-05-17 15:24:05 +0200417 u32 *addr = &((u32 *)buf)[i - otp];
418
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100419 if (lock)
420 ret = stm32mp_bsec_read_lock(dev, addr, i);
421 else if (shadow)
Patrick Delaunay19f58992018-05-17 15:24:05 +0200422 ret = stm32mp_bsec_read_shadow(dev, addr, i);
423 else
424 ret = stm32mp_bsec_read_otp(dev, addr, i);
425
426 if (ret)
427 break;
428 }
Patrick Delaunay0c8620d2019-08-02 13:08:02 +0200429 if (ret)
430 return ret;
431 else
432 return (i - otp) * 4;
Patrick Delaunay19f58992018-05-17 15:24:05 +0200433}
434
435static int stm32mp_bsec_write(struct udevice *dev, int offset,
436 const void *buf, int size)
437{
438 int ret = 0;
439 int i;
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100440 bool shadow = true, lock = false;
Patrick Delaunay19f58992018-05-17 15:24:05 +0200441 int nb_otp = size / sizeof(u32);
442 int otp;
Patrick Delaunay745b6762019-06-21 15:26:43 +0200443 unsigned int offs = offset;
Patrick Delaunay19f58992018-05-17 15:24:05 +0200444
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100445 if (offs >= STM32_BSEC_LOCK_OFFSET) {
446 offs -= STM32_BSEC_LOCK_OFFSET;
447 lock = true;
448 } else if (offs >= STM32_BSEC_OTP_OFFSET) {
Patrick Delaunay745b6762019-06-21 15:26:43 +0200449 offs -= STM32_BSEC_OTP_OFFSET;
Patrick Delaunay19f58992018-05-17 15:24:05 +0200450 shadow = false;
451 }
Patrick Delaunay0c8620d2019-08-02 13:08:02 +0200452
Patrick Delaunaydf2d1b82020-02-12 19:37:37 +0100453 if ((offs % 4) || (size % 4))
Patrick Delaunay0c8620d2019-08-02 13:08:02 +0200454 return -EINVAL;
455
Patrick Delaunay745b6762019-06-21 15:26:43 +0200456 otp = offs / sizeof(u32);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200457
Patrick Delaunay0c8620d2019-08-02 13:08:02 +0200458 for (i = otp; i < otp + nb_otp && i <= BSEC_OTP_MAX_VALUE; i++) {
Patrick Delaunay19f58992018-05-17 15:24:05 +0200459 u32 *val = &((u32 *)buf)[i - otp];
460
Patrick Delaunay7ae22d72020-02-12 19:37:38 +0100461 if (lock)
462 ret = stm32mp_bsec_write_lock(dev, *val, i);
463 else if (shadow)
Patrick Delaunay19f58992018-05-17 15:24:05 +0200464 ret = stm32mp_bsec_write_shadow(dev, *val, i);
465 else
466 ret = stm32mp_bsec_write_otp(dev, *val, i);
467 if (ret)
468 break;
469 }
Patrick Delaunay0c8620d2019-08-02 13:08:02 +0200470 if (ret)
471 return ret;
472 else
473 return (i - otp) * 4;
Patrick Delaunay19f58992018-05-17 15:24:05 +0200474}
475
476static const struct misc_ops stm32mp_bsec_ops = {
477 .read = stm32mp_bsec_read,
478 .write = stm32mp_bsec_write,
479};
480
Simon Glassd1998a92020-12-03 16:55:21 -0700481static int stm32mp_bsec_of_to_plat(struct udevice *dev)
Patrick Delaunay19f58992018-05-17 15:24:05 +0200482{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700483 struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
Patrick Delaunay19f58992018-05-17 15:24:05 +0200484
485 plat->base = (u32)dev_read_addr_ptr(dev);
486
487 return 0;
488}
489
Patrick Delaunay815bc8b2019-02-27 17:01:28 +0100490static int stm32mp_bsec_probe(struct udevice *dev)
491{
492 int otp;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700493 struct stm32mp_bsec_plat *plat;
Patrick Delaunay622c9562021-02-25 13:43:07 +0100494 struct clk_bulk clk_bulk;
495 int ret;
496
497 ret = clk_get_bulk(dev, &clk_bulk);
498 if (!ret) {
499 ret = clk_enable_bulk(&clk_bulk);
500 if (ret)
501 return ret;
502 }
Patrick Delaunay815bc8b2019-02-27 17:01:28 +0100503
Patrick Delaunay95bd49a2020-05-25 12:19:41 +0200504 /*
505 * update unlocked shadow for OTP cleared by the rom code
506 * only executed in U-Boot proper when TF-A is not used
507 */
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200508
509 if (!IS_ENABLED(CONFIG_TFABOOT) && !IS_ENABLED(CONFIG_SPL_BUILD)) {
Simon Glassc69cda22020-12-03 16:55:20 -0700510 plat = dev_get_plat(dev);
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200511
512 for (otp = 57; otp <= BSEC_OTP_MAX_VALUE; otp++)
513 if (!bsec_read_SR_lock(plat->base, otp))
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100514 bsec_shadow_register(dev, plat->base, otp);
Patrick Delaunay4e9e3582020-07-31 16:31:51 +0200515 }
Patrick Delaunay815bc8b2019-02-27 17:01:28 +0100516
517 return 0;
518}
Patrick Delaunay815bc8b2019-02-27 17:01:28 +0100519
Patrick Delaunay19f58992018-05-17 15:24:05 +0200520static const struct udevice_id stm32mp_bsec_ids[] = {
Patrick Delaunaybfe1f082019-02-27 17:01:27 +0100521 { .compatible = "st,stm32mp15-bsec" },
Patrick Delaunay19f58992018-05-17 15:24:05 +0200522 {}
523};
524
525U_BOOT_DRIVER(stm32mp_bsec) = {
526 .name = "stm32mp_bsec",
527 .id = UCLASS_MISC,
528 .of_match = stm32mp_bsec_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700529 .of_to_plat = stm32mp_bsec_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700530 .plat_auto = sizeof(struct stm32mp_bsec_plat),
Patrick Delaunay19f58992018-05-17 15:24:05 +0200531 .ops = &stm32mp_bsec_ops,
Patrick Delaunay815bc8b2019-02-27 17:01:28 +0100532 .probe = stm32mp_bsec_probe,
Patrick Delaunay19f58992018-05-17 15:24:05 +0200533};
Patrick Delaunaybd3f60d2020-06-16 18:27:44 +0200534
535bool bsec_dbgswenable(void)
536{
537 struct udevice *dev;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700538 struct stm32mp_bsec_plat *plat;
Patrick Delaunaybd3f60d2020-06-16 18:27:44 +0200539 int ret;
540
541 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65e25be2020-12-28 20:34:56 -0700542 DM_DRIVER_GET(stm32mp_bsec), &dev);
Patrick Delaunaybd3f60d2020-06-16 18:27:44 +0200543 if (ret || !dev) {
Patrick Delaunayb66bfdf2020-11-06 19:01:31 +0100544 log_debug("bsec driver not available\n");
Patrick Delaunaybd3f60d2020-06-16 18:27:44 +0200545 return false;
546 }
547
Simon Glassc69cda22020-12-03 16:55:20 -0700548 plat = dev_get_plat(dev);
Patrick Delaunaybd3f60d2020-06-16 18:27:44 +0200549 if (readl(plat->base + BSEC_DENABLE_OFF) & BSEC_DENABLE_DBGSWENABLE)
550 return true;
551
552 return false;
553}