blob: 705c994d9307c740c689161410a9ae6b5ee66336 [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>
Patrick Delaunayd5388292023-01-06 13:20:15 +010013#include <tee.h>
Patrick Delaunay14d6a242018-05-17 15:24:05 +020014#include <asm/io.h>
Patrick Delaunay6332c042020-06-16 18:27:44 +020015#include <asm/arch/bsec.h>
Patrick Delaunay7858d7e2019-02-12 11:44:40 +010016#include <asm/arch/stm32mp1_smc.h>
Patrick Delaunayd5388292023-01-06 13:20:15 +010017#include <dm/device.h>
Patrick Delaunayfbefc102020-11-06 19:01:31 +010018#include <dm/device_compat.h>
Patrick Delaunay7858d7e2019-02-12 11:44:40 +010019#include <linux/arm-smccc.h>
Patrick Delaunay2fa55eb2019-04-18 17:32:39 +020020#include <linux/iopoll.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
Patrick Delaunay14d6a242018-05-17 15:24:05 +020022
Patrick Delaunayb9201a72022-02-15 16:08:50 +010023#define BSEC_OTP_UPPER_START 32
Patrick Delaunay14d6a242018-05-17 15:24:05 +020024#define BSEC_TIMEOUT_US 10000
25
26/* BSEC REGISTER OFFSET (base relative) */
27#define BSEC_OTP_CONF_OFF 0x000
28#define BSEC_OTP_CTRL_OFF 0x004
29#define BSEC_OTP_WRDATA_OFF 0x008
30#define BSEC_OTP_STATUS_OFF 0x00C
31#define BSEC_OTP_LOCK_OFF 0x010
Patrick Delaunay6332c042020-06-16 18:27:44 +020032#define BSEC_DENABLE_OFF 0x014
Patrick Delaunay14d6a242018-05-17 15:24:05 +020033#define BSEC_DISTURBED_OFF 0x01C
34#define BSEC_ERROR_OFF 0x034
Patrick Delaunayb10cddf2020-02-12 19:37:38 +010035#define BSEC_WRLOCK_OFF 0x04C /* OTP write permananet lock */
36#define BSEC_SPLOCK_OFF 0x064 /* OTP write sticky lock */
37#define BSEC_SWLOCK_OFF 0x07C /* shadow write sticky lock */
38#define BSEC_SRLOCK_OFF 0x094 /* shadow read sticky lock */
Patrick Delaunay14d6a242018-05-17 15:24:05 +020039#define BSEC_OTP_DATA_OFF 0x200
40
41/* BSEC_CONFIGURATION Register MASK */
42#define BSEC_CONF_POWER_UP 0x001
43
44/* BSEC_CONTROL Register */
45#define BSEC_READ 0x000
46#define BSEC_WRITE 0x100
Patrick Delaunayb9201a72022-02-15 16:08:50 +010047#define BSEC_LOCK 0x200
Patrick Delaunay14d6a242018-05-17 15:24:05 +020048
49/* LOCK Register */
50#define OTP_LOCK_MASK 0x1F
51#define OTP_LOCK_BANK_SHIFT 0x05
52#define OTP_LOCK_BIT_MASK 0x01
53
54/* STATUS Register */
55#define BSEC_MODE_BUSY_MASK 0x08
56#define BSEC_MODE_PROGFAIL_MASK 0x10
57#define BSEC_MODE_PWR_MASK 0x20
58
Patrick Delaunay6332c042020-06-16 18:27:44 +020059/* DENABLE Register */
60#define BSEC_DENABLE_DBGSWENABLE BIT(10)
61
Patrick Delaunay14d6a242018-05-17 15:24:05 +020062/*
63 * OTP Lock services definition
64 * Value must corresponding to the bit number in the register
65 */
66#define BSEC_LOCK_PROGRAM 0x04
67
Patrick Delaunayd5388292023-01-06 13:20:15 +010068#define PTA_BSEC_UUID { 0x94cf71ad, 0x80e6, 0x40b5, \
69 { 0xa7, 0xc6, 0x3d, 0xc5, 0x01, 0xeb, 0x28, 0x03 } }
70
71/*
72 * Read OTP memory
73 *
74 * [in] value[0].a OTP start offset in byte
75 * [in] value[0].b Access type (0:shadow, 1:fuse, 2:lock)
76 * [out] memref[1].buffer Output buffer to store read values
77 * [out] memref[1].size Size of OTP to be read
78 *
79 * Return codes:
80 * TEE_SUCCESS - Invoke command success
81 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
82 * TEE_ERROR_ACCESS_DENIED - OTP not accessible by caller
83 */
84#define PTA_BSEC_READ_MEM 0x0
85
Patrick Delaunayb9201a72022-02-15 16:08:50 +010086/*
Patrick Delaunayd5388292023-01-06 13:20:15 +010087 * Write OTP memory
88 *
89 * [in] value[0].a OTP start offset in byte
90 * [in] value[0].b Access type (0:shadow, 1:fuse, 2:lock)
91 * [in] memref[1].buffer Input buffer to read values
92 * [in] memref[1].size Size of OTP to be written
93 *
94 * Return codes:
95 * TEE_SUCCESS - Invoke command success
96 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
97 * TEE_ERROR_ACCESS_DENIED - OTP not accessible by caller
Patrick Delaunayb9201a72022-02-15 16:08:50 +010098 */
Patrick Delaunayd5388292023-01-06 13:20:15 +010099#define PTA_BSEC_WRITE_MEM 0x1
100
101/* value of PTA_BSEC access type = value[in] b */
102#define SHADOW_ACCESS 0
103#define FUSE_ACCESS 1
104#define LOCK_ACCESS 2
Patrick Delaunayb9201a72022-02-15 16:08:50 +0100105
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200106/**
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100107 * bsec_lock() - manage lock for each type SR/SP/SW
108 * @address: address of bsec IP register
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200109 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100110 * Return: true if locked else false
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200111 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200112static bool bsec_read_lock(void __iomem *address, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200113{
114 u32 bit;
115 u32 bank;
116
117 bit = 1 << (otp & OTP_LOCK_MASK);
118 bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
119
Patrice Chotard1115c302023-10-27 16:43:00 +0200120 return !!(readl((address + bank)) & bit);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200121}
122
123/**
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100124 * bsec_check_error() - Check status of one otp
125 * @base: base address of bsec IP
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200126 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100127 * Return: 0 if no error, -EAGAIN or -ENOTSUPP
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200128 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200129static u32 bsec_check_error(void __iomem *base, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200130{
131 u32 bit;
132 u32 bank;
133
134 bit = 1 << (otp & OTP_LOCK_MASK);
135 bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32);
136
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100137 if (readl(base + BSEC_DISTURBED_OFF + bank) & bit)
138 return -EAGAIN;
139 else if (readl(base + BSEC_ERROR_OFF + bank) & bit)
140 return -ENOTSUPP;
141
142 return 0;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200143}
144
145/**
146 * bsec_read_SR_lock() - read SR lock (Shadowing)
147 * @base: base address of bsec IP
148 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
149 * Return: true if locked else false
150 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200151static bool bsec_read_SR_lock(void __iomem *base, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200152{
153 return bsec_read_lock(base + BSEC_SRLOCK_OFF, otp);
154}
155
156/**
157 * bsec_read_SP_lock() - read SP lock (program Lock)
158 * @base: base address of bsec IP
159 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
160 * Return: true if locked else false
161 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200162static bool bsec_read_SP_lock(void __iomem *base, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200163{
164 return bsec_read_lock(base + BSEC_SPLOCK_OFF, otp);
165}
166
167/**
168 * bsec_SW_lock() - manage SW lock (Write in Shadow)
169 * @base: base address of bsec IP
170 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
171 * Return: true if locked else false
172 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200173static bool bsec_read_SW_lock(void __iomem *base, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200174{
175 return bsec_read_lock(base + BSEC_SWLOCK_OFF, otp);
176}
177
178/**
179 * bsec_power_safmem() - Activate or deactivate safmem power
180 * @base: base address of bsec IP
181 * @power: true to power up , false to power down
182 * Return: 0 if succeed
183 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200184static int bsec_power_safmem(void __iomem *base, bool power)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200185{
186 u32 val;
187 u32 mask;
188
189 if (power) {
190 setbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
191 mask = BSEC_MODE_PWR_MASK;
192 } else {
193 clrbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP);
194 mask = 0;
195 }
196
197 /* waiting loop */
198 return readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
199 val, (val & BSEC_MODE_PWR_MASK) == mask,
200 BSEC_TIMEOUT_US);
201}
202
203/**
204 * bsec_shadow_register() - copy safmen otp to bsec data
Patrick Delaunay6292db12022-02-15 16:08:51 +0100205 * @dev: bsec IP device
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200206 * @base: base address of bsec IP
207 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
208 * Return: 0 if no error
209 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200210static int bsec_shadow_register(struct udevice *dev, void __iomem *base, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200211{
212 u32 val;
213 int ret;
214 bool power_up = false;
215
216 /* check if shadowing of otp is locked */
217 if (bsec_read_SR_lock(base, otp))
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100218 dev_dbg(dev, "OTP %d is locked and refreshed with 0\n",
219 otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200220
221 /* check if safemem is power up */
222 val = readl(base + BSEC_OTP_STATUS_OFF);
223 if (!(val & BSEC_MODE_PWR_MASK)) {
224 ret = bsec_power_safmem(base, true);
225 if (ret)
226 return ret;
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100227 power_up = true;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200228 }
229 /* set BSEC_OTP_CTRL_OFF with the otp value*/
230 writel(otp | BSEC_READ, base + BSEC_OTP_CTRL_OFF);
231
232 /* check otp status*/
233 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
234 val, (val & BSEC_MODE_BUSY_MASK) == 0,
235 BSEC_TIMEOUT_US);
236 if (ret)
237 return ret;
238
239 ret = bsec_check_error(base, otp);
240
241 if (power_up)
242 bsec_power_safmem(base, false);
243
244 return ret;
245}
246
247/**
248 * bsec_read_shadow() - read an otp data value from shadow
Patrick Delaunay6292db12022-02-15 16:08:51 +0100249 * @dev: bsec IP device
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200250 * @base: base address of bsec IP
251 * @val: read value
252 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
253 * Return: 0 if no error
254 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200255static int bsec_read_shadow(struct udevice *dev, void __iomem *base, u32 *val,
256 u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200257{
258 *val = readl(base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
259
260 return bsec_check_error(base, otp);
261}
262
263/**
264 * bsec_write_shadow() - write value in BSEC data register in shadow
Patrick Delaunay6292db12022-02-15 16:08:51 +0100265 * @dev: bsec IP device
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200266 * @base: base address of bsec IP
267 * @val: value to write
268 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
269 * Return: 0 if no error
270 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200271static int bsec_write_shadow(struct udevice *dev, void __iomem *base, u32 val, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200272{
273 /* check if programming of otp is locked */
274 if (bsec_read_SW_lock(base, otp))
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100275 dev_dbg(dev, "OTP %d is lock, write will be ignore\n", otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200276
277 writel(val, base + BSEC_OTP_DATA_OFF + otp * sizeof(u32));
278
279 return bsec_check_error(base, otp);
280}
281
282/**
283 * bsec_program_otp() - program a bit in SAFMEM
Patrick Delaunay6292db12022-02-15 16:08:51 +0100284 * @dev: bsec IP device
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200285 * @base: base address of bsec IP
286 * @val: value to program
287 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
288 * after the function the otp data is not refreshed in shadow
289 * Return: 0 if no error
290 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200291static int bsec_program_otp(struct udevice *dev, void __iomem *base, u32 val, u32 otp)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200292{
293 u32 ret;
294 bool power_up = false;
295
296 if (bsec_read_SP_lock(base, otp))
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100297 dev_dbg(dev, "OTP %d locked, prog will be ignore\n", otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200298
299 if (readl(base + BSEC_OTP_LOCK_OFF) & (1 << BSEC_LOCK_PROGRAM))
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100300 dev_dbg(dev, "Global lock, prog will be ignore\n");
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200301
302 /* check if safemem is power up */
303 if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) {
304 ret = bsec_power_safmem(base, true);
305 if (ret)
306 return ret;
307
308 power_up = true;
309 }
310 /* set value in write register*/
311 writel(val, base + BSEC_OTP_WRDATA_OFF);
312
313 /* set BSEC_OTP_CTRL_OFF with the otp value */
314 writel(otp | BSEC_WRITE, base + BSEC_OTP_CTRL_OFF);
315
316 /* check otp status*/
317 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
318 val, (val & BSEC_MODE_BUSY_MASK) == 0,
319 BSEC_TIMEOUT_US);
320 if (ret)
321 return ret;
322
323 if (val & BSEC_MODE_PROGFAIL_MASK)
324 ret = -EACCES;
325 else
326 ret = bsec_check_error(base, otp);
327
328 if (power_up)
329 bsec_power_safmem(base, false);
330
331 return ret;
332}
333
Patrick Delaunayb9201a72022-02-15 16:08:50 +0100334/**
335 * bsec_permanent_lock_otp() - permanent lock of OTP in SAFMEM
336 * @dev: bsec IP device
337 * @base: base address of bsec IP
338 * @otp: otp number (0 - BSEC_OTP_MAX_VALUE)
339 * Return: 0 if no error
340 */
Patrice Chotard1115c302023-10-27 16:43:00 +0200341static int bsec_permanent_lock_otp(struct udevice *dev, void __iomem *base, uint32_t otp)
Patrick Delaunayb9201a72022-02-15 16:08:50 +0100342{
343 int ret;
344 bool power_up = false;
345 u32 val, addr;
346
347 /* check if safemem is power up */
348 if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) {
349 ret = bsec_power_safmem(base, true);
350 if (ret)
351 return ret;
352
353 power_up = true;
354 }
355
356 /*
357 * low OTPs = 2 bits word for low OTPs, 1 bits per word for upper OTP
358 * and only 16 bits used in WRDATA
359 */
360 if (otp < BSEC_OTP_UPPER_START) {
361 addr = otp / 8;
362 val = 0x03 << ((otp * 2) & 0xF);
363 } else {
364 addr = BSEC_OTP_UPPER_START / 8 +
365 ((otp - BSEC_OTP_UPPER_START) / 16);
366 val = 0x01 << (otp & 0xF);
367 }
368
369 /* set value in write register*/
370 writel(val, base + BSEC_OTP_WRDATA_OFF);
371
372 /* set BSEC_OTP_CTRL_OFF with the otp addr and lock request*/
373 writel(addr | BSEC_WRITE | BSEC_LOCK, base + BSEC_OTP_CTRL_OFF);
374
375 /* check otp status*/
376 ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF,
377 val, (val & BSEC_MODE_BUSY_MASK) == 0,
378 BSEC_TIMEOUT_US);
379 if (ret)
380 return ret;
381
382 if (val & BSEC_MODE_PROGFAIL_MASK)
383 ret = -EACCES;
384 else
385 ret = bsec_check_error(base, otp);
386
387 if (power_up)
388 bsec_power_safmem(base, false);
389
390 return ret;
391}
392
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200393/* BSEC MISC driver *******************************************************/
Simon Glassb75b15b2020-12-03 16:55:23 -0700394struct stm32mp_bsec_plat {
Patrice Chotard1115c302023-10-27 16:43:00 +0200395 void __iomem *base;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200396};
397
Patrick Delaunayd5388292023-01-06 13:20:15 +0100398struct stm32mp_bsec_priv {
399 struct udevice *tee;
400};
401
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100402struct stm32mp_bsec_drvdata {
403 int size;
404 bool ta;
405};
406
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200407static int stm32mp_bsec_read_otp(struct udevice *dev, u32 *val, u32 otp)
408{
Simon Glassb75b15b2020-12-03 16:55:23 -0700409 struct stm32mp_bsec_plat *plat;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200410 u32 tmp_data = 0;
411 int ret;
412
Patrick Delaunay72a57622021-10-11 09:52:50 +0200413 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200414 return stm32_smc(STM32_SMC_BSEC,
415 STM32_SMC_READ_OTP,
416 otp, 0, val);
417
Simon Glassfa20e932020-12-03 16:55:20 -0700418 plat = dev_get_plat(dev);
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200419
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200420 /* read current shadow value */
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100421 ret = bsec_read_shadow(dev, plat->base, &tmp_data, otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200422 if (ret)
423 return ret;
424
425 /* copy otp in shadow */
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100426 ret = bsec_shadow_register(dev, plat->base, otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200427 if (ret)
428 return ret;
429
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100430 ret = bsec_read_shadow(dev, plat->base, val, otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200431 if (ret)
432 return ret;
433
434 /* restore shadow value */
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100435 ret = bsec_write_shadow(dev, plat->base, tmp_data, otp);
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200436
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200437 return ret;
438}
439
440static int stm32mp_bsec_read_shadow(struct udevice *dev, u32 *val, u32 otp)
441{
Simon Glassb75b15b2020-12-03 16:55:23 -0700442 struct stm32mp_bsec_plat *plat;
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200443
Patrick Delaunay72a57622021-10-11 09:52:50 +0200444 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200445 return stm32_smc(STM32_SMC_BSEC,
446 STM32_SMC_READ_SHADOW,
447 otp, 0, val);
448
Simon Glassfa20e932020-12-03 16:55:20 -0700449 plat = dev_get_plat(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200450
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100451 return bsec_read_shadow(dev, plat->base, val, otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200452}
453
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100454static int stm32mp_bsec_read_lock(struct udevice *dev, u32 *val, u32 otp)
455{
Simon Glassb75b15b2020-12-03 16:55:23 -0700456 struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
Patrick Delaunayb9201a72022-02-15 16:08:50 +0100457 u32 wrlock;
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100458
459 /* return OTP permanent write lock status */
Patrick Delaunayb9201a72022-02-15 16:08:50 +0100460 wrlock = bsec_read_lock(plat->base + BSEC_WRLOCK_OFF, otp);
461
462 *val = 0;
463 if (wrlock)
464 *val = BSEC_LOCK_PERM;
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100465
466 return 0;
467}
468
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200469static int stm32mp_bsec_write_otp(struct udevice *dev, u32 val, u32 otp)
470{
Simon Glassb75b15b2020-12-03 16:55:23 -0700471 struct stm32mp_bsec_plat *plat;
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200472
Patrick Delaunay72a57622021-10-11 09:52:50 +0200473 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200474 return stm32_smc_exec(STM32_SMC_BSEC,
475 STM32_SMC_PROG_OTP,
476 otp, val);
477
Simon Glassfa20e932020-12-03 16:55:20 -0700478 plat = dev_get_plat(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200479
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100480 return bsec_program_otp(dev, plat->base, val, otp);
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200481
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200482}
483
484static int stm32mp_bsec_write_shadow(struct udevice *dev, u32 val, u32 otp)
485{
Simon Glassb75b15b2020-12-03 16:55:23 -0700486 struct stm32mp_bsec_plat *plat;
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200487
Patrick Delaunay72a57622021-10-11 09:52:50 +0200488 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200489 return stm32_smc_exec(STM32_SMC_BSEC,
490 STM32_SMC_WRITE_SHADOW,
491 otp, val);
492
Simon Glassfa20e932020-12-03 16:55:20 -0700493 plat = dev_get_plat(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200494
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100495 return bsec_write_shadow(dev, plat->base, val, otp);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200496}
497
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100498static int stm32mp_bsec_write_lock(struct udevice *dev, u32 val, u32 otp)
499{
Patrick Delaunayb9201a72022-02-15 16:08:50 +0100500 struct stm32mp_bsec_plat *plat;
501
502 /* only permanent write lock is supported in U-Boot */
503 if (!(val & BSEC_LOCK_PERM)) {
504 dev_dbg(dev, "lock option without BSEC_LOCK_PERM: %x\n", val);
505 return 0; /* nothing to do */
506 }
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200507
Patrick Delaunayb9201a72022-02-15 16:08:50 +0100508 if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100509 return stm32_smc_exec(STM32_SMC_BSEC,
510 STM32_SMC_WRLOCK_OTP,
511 otp, 0);
Patrick Delaunayb9201a72022-02-15 16:08:50 +0100512
513 plat = dev_get_plat(dev);
514
515 return bsec_permanent_lock_otp(dev, plat->base, otp);
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100516}
517
Patrick Delaunayd5388292023-01-06 13:20:15 +0100518static int bsec_pta_open_session(struct udevice *tee, u32 *tee_session)
519{
520 const struct tee_optee_ta_uuid uuid = PTA_BSEC_UUID;
521 struct tee_open_session_arg arg;
522 int rc;
523
524 memset(&arg, 0, sizeof(arg));
525 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
526 arg.clnt_login = TEE_LOGIN_REE_KERNEL;
527 rc = tee_open_session(tee, &arg, 0, NULL);
528 if (rc < 0)
529 return -ENODEV;
530
531 *tee_session = arg.session;
532
533 return 0;
534}
535
536static int bsec_optee_open(struct udevice *dev)
537{
538 struct stm32mp_bsec_priv *priv = dev_get_priv(dev);
539 struct udevice *tee;
540 u32 tee_session;
541 int rc;
542
543 tee = tee_find_device(NULL, NULL, NULL, NULL);
544 if (!tee)
545 return -ENODEV;
546
547 /* try to open the STM32 BSEC TA */
548 rc = bsec_pta_open_session(tee, &tee_session);
549 if (rc)
550 return rc;
551
552 tee_close_session(tee, tee_session);
553
554 priv->tee = tee;
555
556 return 0;
557}
558
559static int bsec_optee_pta(struct udevice *dev, int cmd, int type, int offset,
560 void *buff, ulong size)
561{
562 struct stm32mp_bsec_priv *priv = dev_get_priv(dev);
563 u32 tee_session;
564 struct tee_invoke_arg arg;
565 struct tee_param param[2];
566 struct tee_shm *fw_shm;
567 int rc;
568
569 rc = bsec_pta_open_session(priv->tee, &tee_session);
570 if (rc)
571 return rc;
572
573 rc = tee_shm_register(priv->tee, buff, size, 0, &fw_shm);
574 if (rc)
575 goto close_session;
576
577 memset(&arg, 0, sizeof(arg));
578 arg.func = cmd;
579 arg.session = tee_session;
580
581 memset(param, 0, sizeof(param));
582
583 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
584 param[0].u.value.a = offset;
585 param[0].u.value.b = type;
586
587 if (cmd == PTA_BSEC_WRITE_MEM)
588 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
589 else
590 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
591
592 param[1].u.memref.shm = fw_shm;
593 param[1].u.memref.size = size;
594
595 rc = tee_invoke_func(priv->tee, &arg, 2, param);
596 if (rc < 0 || arg.ret != 0) {
597 dev_err(priv->tee,
598 "PTA_BSEC invoke failed TEE err: %x, err:%x\n",
599 arg.ret, rc);
600 if (!rc)
601 rc = -EIO;
602 }
603
604 tee_shm_free(fw_shm);
605
606close_session:
607 tee_close_session(priv->tee, tee_session);
608
609 return rc;
610}
611
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200612static int stm32mp_bsec_read(struct udevice *dev, int offset,
613 void *buf, int size)
614{
Patrick Delaunayd5388292023-01-06 13:20:15 +0100615 struct stm32mp_bsec_priv *priv = dev_get_priv(dev);
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100616 struct stm32mp_bsec_drvdata *data = (struct stm32mp_bsec_drvdata *)dev_get_driver_data(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200617 int ret;
618 int i;
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100619 bool shadow = true, lock = false;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200620 int nb_otp = size / sizeof(u32);
Patrick Delaunayd5388292023-01-06 13:20:15 +0100621 int otp, cmd;
Patrick Delaunay4c7c0742019-06-21 15:26:43 +0200622 unsigned int offs = offset;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200623
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100624 if (offs >= STM32_BSEC_LOCK_OFFSET) {
625 offs -= STM32_BSEC_LOCK_OFFSET;
626 lock = true;
627 } else if (offs >= STM32_BSEC_OTP_OFFSET) {
Patrick Delaunay4c7c0742019-06-21 15:26:43 +0200628 offs -= STM32_BSEC_OTP_OFFSET;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200629 shadow = false;
630 }
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200631
Patrick Delaunay8275ff32023-04-27 15:36:34 +0200632 if ((offs % 4) || (size % 4) || !size)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200633 return -EINVAL;
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +0200634
Patrick Delaunayd5388292023-01-06 13:20:15 +0100635 if (IS_ENABLED(CONFIG_OPTEE) && priv->tee) {
636 cmd = FUSE_ACCESS;
637 if (shadow)
638 cmd = SHADOW_ACCESS;
639 if (lock)
640 cmd = LOCK_ACCESS;
641 ret = bsec_optee_pta(dev, PTA_BSEC_READ_MEM, cmd, offs, buf, size);
642 if (ret)
643 return ret;
644
645 return size;
646 }
647
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +0200648 otp = offs / sizeof(u32);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200649
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100650 for (i = otp; i < (otp + nb_otp) && i < data->size; i++) {
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200651 u32 *addr = &((u32 *)buf)[i - otp];
652
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100653 if (lock)
654 ret = stm32mp_bsec_read_lock(dev, addr, i);
655 else if (shadow)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200656 ret = stm32mp_bsec_read_shadow(dev, addr, i);
657 else
658 ret = stm32mp_bsec_read_otp(dev, addr, i);
659
660 if (ret)
661 break;
662 }
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +0200663 if (ret)
664 return ret;
665 else
666 return (i - otp) * 4;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200667}
668
669static int stm32mp_bsec_write(struct udevice *dev, int offset,
670 const void *buf, int size)
671{
Patrick Delaunayd5388292023-01-06 13:20:15 +0100672 struct stm32mp_bsec_priv *priv = dev_get_priv(dev);
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100673 struct stm32mp_bsec_drvdata *data = (struct stm32mp_bsec_drvdata *)dev_get_driver_data(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200674 int ret = 0;
675 int i;
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100676 bool shadow = true, lock = false;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200677 int nb_otp = size / sizeof(u32);
Patrick Delaunayd5388292023-01-06 13:20:15 +0100678 int otp, cmd;
Patrick Delaunay4c7c0742019-06-21 15:26:43 +0200679 unsigned int offs = offset;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200680
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100681 if (offs >= STM32_BSEC_LOCK_OFFSET) {
682 offs -= STM32_BSEC_LOCK_OFFSET;
683 lock = true;
684 } else if (offs >= STM32_BSEC_OTP_OFFSET) {
Patrick Delaunay4c7c0742019-06-21 15:26:43 +0200685 offs -= STM32_BSEC_OTP_OFFSET;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200686 shadow = false;
687 }
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200688
Patrick Delaunay8275ff32023-04-27 15:36:34 +0200689 if ((offs % 4) || (size % 4) || !size)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200690 return -EINVAL;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200691
Patrick Delaunayd5388292023-01-06 13:20:15 +0100692 if (IS_ENABLED(CONFIG_OPTEE) && priv->tee) {
693 cmd = FUSE_ACCESS;
694 if (shadow)
695 cmd = SHADOW_ACCESS;
696 if (lock)
697 cmd = LOCK_ACCESS;
698 ret = bsec_optee_pta(dev, PTA_BSEC_WRITE_MEM, cmd, offs, (void *)buf, size);
699 if (ret)
700 return ret;
701
702 return size;
703 }
704
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +0200705 otp = offs / sizeof(u32);
706
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100707 for (i = otp; i < otp + nb_otp && i < data->size; i++) {
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200708 u32 *val = &((u32 *)buf)[i - otp];
709
Patrick Delaunayb10cddf2020-02-12 19:37:38 +0100710 if (lock)
711 ret = stm32mp_bsec_write_lock(dev, *val, i);
712 else if (shadow)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200713 ret = stm32mp_bsec_write_shadow(dev, *val, i);
714 else
715 ret = stm32mp_bsec_write_otp(dev, *val, i);
716 if (ret)
717 break;
718 }
Patrick Delaunay7e5f8e32019-08-02 13:08:02 +0200719 if (ret)
720 return ret;
721 else
722 return (i - otp) * 4;
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200723}
724
725static const struct misc_ops stm32mp_bsec_ops = {
726 .read = stm32mp_bsec_read,
727 .write = stm32mp_bsec_write,
728};
729
Simon Glassaad29ae2020-12-03 16:55:21 -0700730static int stm32mp_bsec_of_to_plat(struct udevice *dev)
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200731{
Simon Glassb75b15b2020-12-03 16:55:23 -0700732 struct stm32mp_bsec_plat *plat = dev_get_plat(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200733
Patrice Chotard1115c302023-10-27 16:43:00 +0200734 plat->base = dev_read_addr_ptr(dev);
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200735
736 return 0;
737}
738
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100739static int stm32mp_bsec_probe(struct udevice *dev)
740{
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100741 struct stm32mp_bsec_drvdata *data = (struct stm32mp_bsec_drvdata *)dev_get_driver_data(dev);
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100742 int otp;
Simon Glassb75b15b2020-12-03 16:55:23 -0700743 struct stm32mp_bsec_plat *plat;
Patrick Delaunayaaf1f962021-02-25 13:43:07 +0100744 struct clk_bulk clk_bulk;
745 int ret;
746
747 ret = clk_get_bulk(dev, &clk_bulk);
748 if (!ret) {
749 ret = clk_enable_bulk(&clk_bulk);
750 if (ret)
751 return ret;
752 }
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100753
Patrick Delaunayd5388292023-01-06 13:20:15 +0100754 if (IS_ENABLED(CONFIG_OPTEE))
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100755 ret = bsec_optee_open(dev);
756 else
757 ret = -ENOTSUPP;
758 /* failed if OP-TEE TA is required */
759 if (data->ta && !ret)
760 return ret;
Patrick Delaunayd5388292023-01-06 13:20:15 +0100761
Patrick Delaunayb6cc5052020-05-25 12:19:41 +0200762 /*
763 * update unlocked shadow for OTP cleared by the rom code
Patrick Delaunaydd2ca252021-10-11 09:52:48 +0200764 * only executed in SPL, it is done in TF-A for TFABOOT
Patrick Delaunayb6cc5052020-05-25 12:19:41 +0200765 */
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100766 if (IS_ENABLED(CONFIG_SPL_BUILD) && !data->ta) {
Simon Glassfa20e932020-12-03 16:55:20 -0700767 plat = dev_get_plat(dev);
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200768
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100769 /* here 57 is the value for STM32MP15x ROM code, only MPU with SPL support*/
770 for (otp = 57; otp < data->size; otp++)
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200771 if (!bsec_read_SR_lock(plat->base, otp))
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100772 bsec_shadow_register(dev, plat->base, otp);
Patrick Delaunay1dffeaf2020-07-31 16:31:51 +0200773 }
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100774
775 return 0;
776}
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100777
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100778static const struct stm32mp_bsec_drvdata stm32mp13_data = {
779 .size = 96,
780 .ta = true,
781};
782
783static const struct stm32mp_bsec_drvdata stm32mp15_data = {
784 .size = 96,
785 .ta = false,
786};
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200787static const struct udevice_id stm32mp_bsec_ids[] = {
Patrick Delaunay9356dbc2024-01-15 15:05:46 +0100788 { .compatible = "st,stm32mp13-bsec", .data = (ulong)&stm32mp13_data},
789 { .compatible = "st,stm32mp15-bsec", .data = (ulong)&stm32mp15_data},
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200790 {}
791};
792
793U_BOOT_DRIVER(stm32mp_bsec) = {
794 .name = "stm32mp_bsec",
795 .id = UCLASS_MISC,
796 .of_match = stm32mp_bsec_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700797 .of_to_plat = stm32mp_bsec_of_to_plat,
Patrick Delaunay5fa74b82023-01-06 13:20:14 +0100798 .plat_auto = sizeof(struct stm32mp_bsec_plat),
Patrick Delaunayd5388292023-01-06 13:20:15 +0100799 .priv_auto = sizeof(struct stm32mp_bsec_priv),
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200800 .ops = &stm32mp_bsec_ops,
Patrick Delaunayf95686b2019-02-27 17:01:28 +0100801 .probe = stm32mp_bsec_probe,
Patrick Delaunay14d6a242018-05-17 15:24:05 +0200802};
Patrick Delaunay6332c042020-06-16 18:27:44 +0200803
804bool bsec_dbgswenable(void)
805{
806 struct udevice *dev;
Simon Glassb75b15b2020-12-03 16:55:23 -0700807 struct stm32mp_bsec_plat *plat;
Patrick Delaunay6332c042020-06-16 18:27:44 +0200808 int ret;
809
810 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700811 DM_DRIVER_GET(stm32mp_bsec), &dev);
Patrick Delaunay6332c042020-06-16 18:27:44 +0200812 if (ret || !dev) {
Patrick Delaunayfbefc102020-11-06 19:01:31 +0100813 log_debug("bsec driver not available\n");
Patrick Delaunay6332c042020-06-16 18:27:44 +0200814 return false;
815 }
816
Simon Glassfa20e932020-12-03 16:55:20 -0700817 plat = dev_get_plat(dev);
Patrick Delaunay6332c042020-06-16 18:27:44 +0200818 if (readl(plat->base + BSEC_DENABLE_OFF) & BSEC_DENABLE_DBGSWENABLE)
819 return true;
820
821 return false;
822}
Patrick Delaunay9fa24a52022-05-20 18:24:41 +0200823
824u32 get_otp(int index, int shift, int mask)
825{
826 int ret;
827 struct udevice *dev;
828 u32 otp = 0;
829
830 ret = uclass_get_device_by_driver(UCLASS_MISC,
831 DM_DRIVER_GET(stm32mp_bsec),
832 &dev);
833
834 if (!ret)
835 ret = misc_read(dev, STM32_BSEC_SHADOW(index),
836 &otp, sizeof(otp));
837
838 return (otp >> shift) & mask;
839}