blob: 27d182950180a542b4a1b6e5cce7f0f919c2dd09 [file] [log] [blame]
Patrick Delaunay14d6a242018-05-17 15:24:05 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 */
5
Patrick Delaunayfbefc102020-11-06 19:01:31 +01006#define LOG_CATEGORY UCLASS_MISC
7
Patrick Delaunay14d6a242018-05-17 15:24:05 +02008#include <common.h>
Patrick Delaunayaaf1f962021-02-25 13:43:07 +01009#include <clk.h>
Patrick Delaunay14d6a242018-05-17 15:24:05 +020010#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Patrick Delaunay14d6a242018-05-17 15:24:05 +020012#include <misc.h>
13#include <asm/io.h>
Patrick Delaunay6332c042020-06-16 18:27:44 +020014#include <asm/arch/bsec.h>
Patrick Delaunay7858d7e2019-02-12 11:44:40 +010015#include <asm/arch/stm32mp1_smc.h>
Patrick Delaunayfbefc102020-11-06 19:01:31 +010016#include <dm/device_compat.h>
Patrick Delaunay7858d7e2019-02-12 11:44:40 +010017#include <linux/arm-smccc.h>
Patrick Delaunay2fa55eb2019-04-18 17:32:39 +020018#include <linux/iopoll.h>
Patrick Delaunay14d6a242018-05-17 15:24:05 +020019
20#define BSEC_OTP_MAX_VALUE 95
Patrick Delaunay14d6a242018-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 Delaunay6332c042020-06-16 18:27:44 +020029#define BSEC_DENABLE_OFF 0x014
Patrick Delaunay14d6a242018-05-17 15:24:05 +020030#define BSEC_DISTURBED_OFF 0x01C
31#define BSEC_ERROR_OFF 0x034
Patrick Delaunayb10cddf2020-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 Delaunay14d6a242018-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 Delaunay6332c042020-06-16 18:27:44 +020055/* DENABLE Register */
56#define BSEC_DENABLE_DBGSWENABLE BIT(10)
57
Patrick Delaunay14d6a242018-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 Delaunayb10cddf2020-02-12 19:37:38 +010065 * bsec_lock() - manage lock for each type SR/SP/SW
66 * @address: address of bsec IP register
Patrick Delaunay14d6a242018-05-17 15:24:05 +020067 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
Patrick Delaunayb10cddf2020-02-12 19:37:38 +010068 * Return: true if locked else false
Patrick Delaunay14d6a242018-05-17 15:24:05 +020069 */
Patrick Delaunayb10cddf2020-02-12 19:37:38 +010070static bool bsec_read_lock(u32 address, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +020071{
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
Patrick Delaunayb10cddf2020-02-12 19:37:38 +010078 return !!(readl(address + bank) & bit);
Patrick Delaunay14d6a242018-05-17 15:24:05 +020079}
80
81/**
Patrick Delaunayb10cddf2020-02-12 19:37:38 +010082 * bsec_check_error() - Check status of one otp
83 * @base: base address of bsec IP
Patrick Delaunay14d6a242018-05-17 15:24:05 +020084 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
Patrick Delaunayb10cddf2020-02-12 19:37:38 +010085 * Return: 0 if no error, -EAGAIN or -ENOTSUPP
Patrick Delaunay14d6a242018-05-17 15:24:05 +020086 */
Patrick Delaunayb10cddf2020-02-12 19:37:38 +010087static u32 bsec_check_error(u32 base, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +020088{
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
Patrick Delaunayb10cddf2020-02-12 19:37:38 +010095 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;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200101}
102
103/**
104 * 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 Delaunayfbefc102020-11-06 19:01:31 +0100167static int bsec_shadow_register(struct udevice *dev, u32 base, u32 otp)
Patrick Delaunay14d6a242018-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 Delaunayfbefc102020-11-06 19:01:31 +0100175 dev_dbg(dev, "OTP %d is locked and refreshed with 0\n",
176 otp);
Patrick Delaunay14d6a242018-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 Delaunayf95686b2019-02-27 17:01:28 +0100184 power_up = true;
Patrick Delaunay14d6a242018-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 Delaunayfbefc102020-11-06 19:01:31 +0100211static int bsec_read_shadow(struct udevice *dev, u32 base, u32 *val, u32 otp)
Patrick Delaunay14d6a242018-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 Delaunayfbefc102020-11-06 19:01:31 +0100225static int bsec_write_shadow(struct udevice *dev, u32 base, u32 val, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200226{
227 /* check if programming of otp is locked */
228 if (bsec_read_SW_lock(base, otp))
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100229 dev_dbg(dev, "OTP %d is lock, write will be ignore\n", otp);
Patrick Delaunay14d6a242018-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 Delaunayfbefc102020-11-06 19:01:31 +0100244static int bsec_program_otp(struct udevice *dev, long base, u32 val, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200245{
246 u32 ret;
247 bool power_up = false;
248
249 if (bsec_read_SP_lock(base, otp))
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100250 dev_dbg(dev, "OTP %d locked, prog will be ignore\n", otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200251
252 if (readl(base + BSEC_OTP_LOCK_OFF) & (1 << BSEC_LOCK_PROGRAM))
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100253 dev_dbg(dev, "Global lock, prog will be ignore\n");
Patrick Delaunay14d6a242018-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 Glassb75b15b2020-12-03 16:55:23 -0700288struct stm32mp_bsec_plat {
Patrick Delaunay14d6a242018-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 Glassb75b15b2020-12-03 16:55:23 -0700294 struct stm32mp_bsec_plat *plat;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200295 u32 tmp_data = 0;
296 int ret;
297
Patrick Delaunay72a57622021-10-11 09:52:50 +0200298 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200299 return stm32_smc(STM32_SMC_BSEC,
300 STM32_SMC_READ_OTP,
301 otp, 0, val);
302
Simon Glassfa20e932020-12-03 16:55:20 -0700303 plat = dev_get_plat(dev);
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200304
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200305 /* read current shadow value */
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100306 ret = bsec_read_shadow(dev, plat->base, &tmp_data, otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200307 if (ret)
308 return ret;
309
310 /* copy otp in shadow */
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100311 ret = bsec_shadow_register(dev, plat->base, otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200312 if (ret)
313 return ret;
314
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100315 ret = bsec_read_shadow(dev, plat->base, val, otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200316 if (ret)
317 return ret;
318
319 /* restore shadow value */
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100320 ret = bsec_write_shadow(dev, plat->base, tmp_data, otp);
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200321
Patrick Delaunay14d6a242018-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 Glassb75b15b2020-12-03 16:55:23 -0700327 struct stm32mp_bsec_plat *plat;
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200328
Patrick Delaunay72a57622021-10-11 09:52:50 +0200329 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200330 return stm32_smc(STM32_SMC_BSEC,
331 STM32_SMC_READ_SHADOW,
332 otp, 0, val);
333
Simon Glassfa20e932020-12-03 16:55:20 -0700334 plat = dev_get_plat(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200335
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100336 return bsec_read_shadow(dev, plat->base, val, otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200337}
338
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100339static int stm32mp_bsec_read_lock(struct udevice *dev, u32 *val, u32 otp)
340{
Simon Glassb75b15b2020-12-03 16:55:23 -0700341 struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
Patrick Delaunayb10cddf2020-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 Delaunay14d6a242018-05-17 15:24:05 +0200349static int stm32mp_bsec_write_otp(struct udevice *dev, u32 val, u32 otp)
350{
Simon Glassb75b15b2020-12-03 16:55:23 -0700351 struct stm32mp_bsec_plat *plat;
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200352
Patrick Delaunay72a57622021-10-11 09:52:50 +0200353 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200354 return stm32_smc_exec(STM32_SMC_BSEC,
355 STM32_SMC_PROG_OTP,
356 otp, val);
357
Simon Glassfa20e932020-12-03 16:55:20 -0700358 plat = dev_get_plat(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200359
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100360 return bsec_program_otp(dev, plat->base, val, otp);
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200361
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200362}
363
364static int stm32mp_bsec_write_shadow(struct udevice *dev, u32 val, u32 otp)
365{
Simon Glassb75b15b2020-12-03 16:55:23 -0700366 struct stm32mp_bsec_plat *plat;
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200367
Patrick Delaunay72a57622021-10-11 09:52:50 +0200368 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200369 return stm32_smc_exec(STM32_SMC_BSEC,
370 STM32_SMC_WRITE_SHADOW,
371 otp, val);
372
Simon Glassfa20e932020-12-03 16:55:20 -0700373 plat = dev_get_plat(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200374
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100375 return bsec_write_shadow(dev, plat->base, val, otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200376}
377
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100378static int stm32mp_bsec_write_lock(struct udevice *dev, u32 val, u32 otp)
379{
Patrick Delaunay72a57622021-10-11 09:52:50 +0200380 if (!IS_ENABLED(CONFIG_ARM_SMCCC) || IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200381 return -ENOTSUPP;
382
Patrick Delaunayb10cddf2020-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 Delaunayb10cddf2020-02-12 19:37:38 +0100391}
392
Patrick Delaunay14d6a242018-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 Delaunayb10cddf2020-02-12 19:37:38 +0100398 bool shadow = true, lock = false;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200399 int nb_otp = size / sizeof(u32);
400 int otp;
Patrick Delaunay4c7c0742019-06-21 15:26:43 +0200401 unsigned int offs = offset;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200402
Patrick Delaunayb10cddf2020-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 Delaunay4c7c0742019-06-21 15:26:43 +0200407 offs -= STM32_BSEC_OTP_OFFSET;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200408 shadow = false;
409 }
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200410
Patrick Delaunay3b7dbd42020-02-12 19:37:37 +0100411 if ((offs % 4) || (size % 4))
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200412 return -EINVAL;
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +0200413
414 otp = offs / sizeof(u32);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200415
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +0200416 for (i = otp; i < (otp + nb_otp) && i <= BSEC_OTP_MAX_VALUE; i++) {
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200417 u32 *addr = &((u32 *)buf)[i - otp];
418
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100419 if (lock)
420 ret = stm32mp_bsec_read_lock(dev, addr, i);
421 else if (shadow)
Patrick Delaunay14d6a242018-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 Delaunay7e5f8e32019-08-02 13:08:02 +0200429 if (ret)
430 return ret;
431 else
432 return (i - otp) * 4;
Patrick Delaunay14d6a242018-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 Delaunayb10cddf2020-02-12 19:37:38 +0100440 bool shadow = true, lock = false;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200441 int nb_otp = size / sizeof(u32);
442 int otp;
Patrick Delaunay4c7c0742019-06-21 15:26:43 +0200443 unsigned int offs = offset;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200444
Patrick Delaunayb10cddf2020-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 Delaunay4c7c0742019-06-21 15:26:43 +0200449 offs -= STM32_BSEC_OTP_OFFSET;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200450 shadow = false;
451 }
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200452
Patrick Delaunay3b7dbd42020-02-12 19:37:37 +0100453 if ((offs % 4) || (size % 4))
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200454 return -EINVAL;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200455
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +0200456 otp = offs / sizeof(u32);
457
458 for (i = otp; i < otp + nb_otp && i <= BSEC_OTP_MAX_VALUE; i++) {
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200459 u32 *val = &((u32 *)buf)[i - otp];
460
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100461 if (lock)
462 ret = stm32mp_bsec_write_lock(dev, *val, i);
463 else if (shadow)
Patrick Delaunay14d6a242018-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 Delaunay7e5f8e32019-08-02 13:08:02 +0200470 if (ret)
471 return ret;
472 else
473 return (i - otp) * 4;
Patrick Delaunay14d6a242018-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 Glassaad29ae2020-12-03 16:55:21 -0700481static int stm32mp_bsec_of_to_plat(struct udevice *dev)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200482{
Simon Glassb75b15b2020-12-03 16:55:23 -0700483 struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200484
485 plat->base = (u32)dev_read_addr_ptr(dev);
486
487 return 0;
488}
489
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100490static int stm32mp_bsec_probe(struct udevice *dev)
491{
492 int otp;
Simon Glassb75b15b2020-12-03 16:55:23 -0700493 struct stm32mp_bsec_plat *plat;
Patrick Delaunayaaf1f962021-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 Delaunayf95686b2019-02-27 17:01:28 +0100503
Patrick Delaunayb6cc5052020-05-25 12:19:41 +0200504 /*
505 * update unlocked shadow for OTP cleared by the rom code
Patrick Delaunaydd2ca252021-10-11 09:52:48 +0200506 * only executed in SPL, it is done in TF-A for TFABOOT
Patrick Delaunayb6cc5052020-05-25 12:19:41 +0200507 */
Patrick Delaunaydd2ca252021-10-11 09:52:48 +0200508 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
Simon Glassfa20e932020-12-03 16:55:20 -0700509 plat = dev_get_plat(dev);
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200510
511 for (otp = 57; otp <= BSEC_OTP_MAX_VALUE; otp++)
512 if (!bsec_read_SR_lock(plat->base, otp))
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100513 bsec_shadow_register(dev, plat->base, otp);
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200514 }
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100515
516 return 0;
517}
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100518
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200519static const struct udevice_id stm32mp_bsec_ids[] = {
Patrick Delaunaybdd71362019-02-27 17:01:27 +0100520 { .compatible = "st,stm32mp15-bsec" },
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200521 {}
522};
523
524U_BOOT_DRIVER(stm32mp_bsec) = {
525 .name = "stm32mp_bsec",
526 .id = UCLASS_MISC,
527 .of_match = stm32mp_bsec_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700528 .of_to_plat = stm32mp_bsec_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700529 .plat_auto = sizeof(struct stm32mp_bsec_plat),
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200530 .ops = &stm32mp_bsec_ops,
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100531 .probe = stm32mp_bsec_probe,
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200532};
Patrick Delaunay6332c042020-06-16 18:27:44 +0200533
534bool bsec_dbgswenable(void)
535{
536 struct udevice *dev;
Simon Glassb75b15b2020-12-03 16:55:23 -0700537 struct stm32mp_bsec_plat *plat;
Patrick Delaunay6332c042020-06-16 18:27:44 +0200538 int ret;
539
540 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700541 DM_DRIVER_GET(stm32mp_bsec), &dev);
Patrick Delaunay6332c042020-06-16 18:27:44 +0200542 if (ret || !dev) {
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100543 log_debug("bsec driver not available\n");
Patrick Delaunay6332c042020-06-16 18:27:44 +0200544 return false;
545 }
546
Simon Glassfa20e932020-12-03 16:55:20 -0700547 plat = dev_get_plat(dev);
Patrick Delaunay6332c042020-06-16 18:27:44 +0200548 if (readl(plat->base + BSEC_DENABLE_OFF) & BSEC_DENABLE_DBGSWENABLE)
549 return true;
550
551 return false;
552}