forked from torvalds/linux
/
stm32-romem.c
689 lines (578 loc) · 16.5 KB
/
stm32-romem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// SPDX-License-Identifier: GPL-2.0
/*
* STM32 Factory-programmed memory read access driver
*
* Copyright (C) 2017, STMicroelectronics - All Rights Reserved
* Author: Fabrice Gasnier <fabrice.gasnier@st.com> for STMicroelectronics.
*/
#include <linux/arm-smccc.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/nvmem-provider.h>
#include <linux/of_device.h>
#include <linux/tee_drv.h>
/* BSEC secure service access from non-secure */
#define STM32_SMC_BSEC 0x82001003
#define STM32_SMC_READ_SHADOW 0x01
#define STM32_SMC_PROG_OTP 0x02
#define STM32_SMC_WRITE_SHADOW 0x03
#define STM32_SMC_READ_OTP 0x04
/* shadow registers offset */
#define STM32MP15_BSEC_DATA0 0x200
struct stm32_romem_cfg {
int size;
u8 lower;
bool ta;
};
struct stm32_romem_priv {
void __iomem *base;
struct nvmem_config cfg;
u8 lower;
struct device *ta;
};
struct device *stm32_bsec_pta_find(struct device *dev);
static int stm32_bsec_pta_read(void *context, unsigned int offset, void *buf,
size_t bytes);
static int stm32_bsec_pta_write(void *context, unsigned int offset, void *buf,
size_t bytes);
static int stm32_romem_read(void *context, unsigned int offset, void *buf,
size_t bytes)
{
struct stm32_romem_priv *priv = context;
u8 *buf8 = buf;
int i;
for (i = offset; i < offset + bytes; i++)
*buf8++ = readb_relaxed(priv->base + i);
return 0;
}
static int stm32_bsec_smc(u8 op, u32 otp, u32 data, u32 *result)
{
#if IS_ENABLED(CONFIG_HAVE_ARM_SMCCC)
struct arm_smccc_res res;
arm_smccc_smc(STM32_SMC_BSEC, op, otp, data, 0, 0, 0, 0, &res);
if (res.a0)
return -EIO;
if (result)
*result = (u32)res.a1;
return 0;
#else
return -ENXIO;
#endif
}
static bool stm32_bsec_check(void)
{
u32 val;
int ret;
/* check that the OP-TEE support the BSEC SMC (legacy mode) */
ret = stm32_bsec_smc(STM32_SMC_READ_SHADOW, 0, 0, &val);
return !ret;
}
static int stm32_bsec_read(void *context, unsigned int offset, void *buf,
size_t bytes)
{
struct stm32_romem_priv *priv = context;
struct device *dev = priv->cfg.dev;
u32 roffset, rbytes, val;
u8 *buf8 = buf, *val8 = (u8 *)&val;
int i, j = 0, ret, skip_bytes, size;
/* Round unaligned access to 32-bits */
roffset = rounddown(offset, 4);
skip_bytes = offset & 0x3;
rbytes = roundup(bytes + skip_bytes, 4);
if (roffset + rbytes > priv->cfg.size)
return -EINVAL;
for (i = roffset; (i < roffset + rbytes); i += 4) {
u32 otp = i >> 2;
if (otp < priv->lower) {
/* read lower data from shadow registers */
val = readl_relaxed(
priv->base + STM32MP15_BSEC_DATA0 + i);
} else {
ret = stm32_bsec_smc(STM32_SMC_READ_SHADOW, otp, 0,
&val);
if (ret) {
dev_err(dev, "Can't read data%d (%d)\n", otp,
ret);
return ret;
}
}
/* skip first bytes in case of unaligned read */
if (skip_bytes)
size = min(bytes, (size_t)(4 - skip_bytes));
else
size = min(bytes, (size_t)4);
memcpy(&buf8[j], &val8[skip_bytes], size);
bytes -= size;
j += size;
skip_bytes = 0;
}
return 0;
}
static int stm32_bsec_write(void *context, unsigned int offset, void *buf,
size_t bytes)
{
struct stm32_romem_priv *priv = context;
struct device *dev = priv->cfg.dev;
u32 *buf32 = buf;
int ret, i;
/* Allow only writing complete 32-bits aligned words */
if ((bytes % 4) || (offset % 4))
return -EINVAL;
for (i = offset; i < offset + bytes; i += 4) {
ret = stm32_bsec_smc(STM32_SMC_PROG_OTP, i >> 2, *buf32++,
NULL);
if (ret) {
dev_err(dev, "Can't write data%d (%d)\n", i >> 2, ret);
return ret;
}
}
if (offset + bytes >= priv->lower * 4)
dev_warn(dev, "Update of upper OTPs with ECC protection (word programming, only once)\n");
return 0;
}
static bool optee_presence_check(void)
{
struct device_node *np;
bool tee_detected = false;
/* check that the OP-TEE node is present and available. */
np = of_find_node_by_path("/firmware/optee");
if (np && of_device_is_available(np))
tee_detected = true;
of_node_put(np);
return tee_detected;
}
static int stm32_romem_probe(struct platform_device *pdev)
{
const struct stm32_romem_cfg *cfg;
struct device *dev = &pdev->dev;
struct stm32_romem_priv *priv;
struct resource *res;
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
priv->base = devm_ioremap_resource(dev, res);
if (IS_ERR(priv->base))
return PTR_ERR(priv->base);
priv->cfg.name = "stm32-romem";
priv->cfg.word_size = 1;
priv->cfg.stride = 1;
priv->cfg.dev = dev;
priv->cfg.priv = priv;
priv->cfg.owner = THIS_MODULE;
priv->cfg.type = NVMEM_TYPE_OTP;
priv->lower = 0;
cfg = (const struct stm32_romem_cfg *)
of_match_device(dev->driver->of_match_table, dev)->data;
if (!cfg) {
priv->cfg.read_only = true;
priv->cfg.size = resource_size(res);
priv->cfg.reg_read = stm32_romem_read;
} else {
priv->cfg.size = cfg->size;
priv->lower = cfg->lower;
if (cfg->ta || optee_presence_check()) {
priv->ta = stm32_bsec_pta_find(dev);
/* wait for OP-TEE client driver to be up and ready */
if (!priv->ta) {
/* BSEC PTA is required or SMC not ready */
if (cfg->ta || !stm32_bsec_check())
return -EPROBE_DEFER;
}
}
if (priv->ta) {
priv->cfg.reg_read = stm32_bsec_pta_read;
priv->cfg.reg_write = stm32_bsec_pta_write;
} else {
priv->cfg.reg_read = stm32_bsec_read;
priv->cfg.reg_write = stm32_bsec_write;
}
}
return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &priv->cfg));
}
/*
* STM32MP15/13 BSEC OTP regions: 4096 OTP bits (with 3072 effective bits)
* => 96 x 32-bits data words
* - Lower: 1K bits, 2:1 redundancy, incremental bit programming
* => 32 (x 32-bits) lower shadow registers = words 0 to 31
* - Upper: 2K bits, ECC protection, word programming only
* => 64 (x 32-bits) = words 32 to 95
*/
static const struct stm32_romem_cfg stm32mp15_bsec_cfg = {
.size = 384,
.lower = 32,
.ta = false,
};
static const struct stm32_romem_cfg stm32mp13_bsec_cfg = {
.size = 384,
.lower = 32,
.ta = true,
};
static const struct of_device_id stm32_romem_of_match[] = {
{ .compatible = "st,stm32f4-otp", }, {
.compatible = "st,stm32mp15-bsec",
.data = (void *)&stm32mp15_bsec_cfg,
}, {
.compatible = "st,stm32mp13-bsec",
.data = (void *)&stm32mp13_bsec_cfg,
},
{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, stm32_romem_of_match);
static struct platform_driver stm32_romem_driver = {
.probe = stm32_romem_probe,
.driver = {
.name = "stm32-romem",
.of_match_table = of_match_ptr(stm32_romem_of_match),
},
};
#if IS_ENABLED(CONFIG_OPTEE)
/*************************************************************************
* BSEC PTA : OP-TEE client driver to pseudo trusted application
*************************************************************************/
/*
* Read OTP memory
*
* [in] value[0].a OTP start offset in byte
* [in] value[0].b Access type (0 : shadow,
* 1 : fuse, 2 : lock)
* [out] memref[1].buffer Output buffer to store read values
* [out] memref[1].size Size of OTP to be read
*
* Return codes:
* TEE_SUCCESS - Invoke command success
* TEE_ERROR_BAD_PARAMETERS - Incorrect input param
*/
#define PTA_BSEC_READ_MEM 0x0 /* Read OTP */
/*
* Write OTP memory
*
* [in] value[0].a OTP start offset in byte
* [in] value[0].b Access type (0 : shadow,
* 1 : fuse, 2 : lock)
* [in] memref[1].buffer Input buffer to read values
* [in] memref[1].size Size of OTP to be written
*
* Return codes:
* TEE_SUCCESS - Invoke command success
* TEE_ERROR_BAD_PARAMETERS - Incorrect input param
*/
#define PTA_BSEC_WRITE_MEM 0x1 /* Write OTP */
/* value of PTA_BSEC access type = value[in] b */
#define SHADOW_ACCESS 0
#define FUSE_ACCESS 1
#define LOCK_ACCESS 2
/* Bitfield definition for LOCK status */
#define LOCK_PERM BIT(30)
/**
* struct stm32_bsec_pta_priv - OP-TEE BSEC TA private data
* @ctx: OP-TEE context handler.
* @session_id: TA session identifier.
*/
struct stm32_bsec_pta_priv {
struct tee_context *ctx;
u32 session_id;
};
/*
* Check whether this driver supports the BSEC TA in the TEE instance
* represented by the params (ver/data) to this function.
*/
static int stm32_bsec_pta_match(struct tee_ioctl_version_data *ver, const void *data)
{
/*
* Currently this driver only supports GP compliant, OP-TEE based TA
*/
if ((ver->impl_id == TEE_IMPL_ID_OPTEE) &&
(ver->gen_caps & TEE_GEN_CAP_GP))
return 1;
else
return 0;
}
/**
* stm32_bsec_pta_probe() - initialize the PTA BSEC
* @dev: the platform_device description.
*
* Return:
* On success, 0. On failure, -errno.
*/
static int stm32_bsec_pta_probe(struct device *dev)
{
int rc;
struct tee_ioctl_open_session_arg sess_arg;
struct tee_client_device *tee_device = to_tee_client_device(dev);
struct stm32_bsec_pta_priv *priv;
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
/* Open context with TEE driver */
priv->ctx = tee_client_open_context(NULL, stm32_bsec_pta_match, NULL, NULL);
if (IS_ERR(priv->ctx)) {
if (PTR_ERR(priv->ctx) == -ENOENT)
return -EPROBE_DEFER;
dev_err(dev, "%s: tee_client_open_context failed\n", __func__);
return PTR_ERR(priv->ctx);
}
/* Open a session with BSEC TA */
memset(&sess_arg, 0, sizeof(sess_arg));
export_uuid(sess_arg.uuid, &tee_device->id.uuid);
sess_arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL;
sess_arg.num_params = 0;
rc = tee_client_open_session(priv->ctx, &sess_arg, NULL);
if ((rc < 0) || (sess_arg.ret != 0)) {
dev_err(dev, "%s: tee_client_open_session failed, err=%x\n",
__func__, sess_arg.ret);
rc = -EINVAL;
goto out_tee_session;
}
priv->session_id = sess_arg.session;
dev_set_drvdata(dev, priv);
return 0;
out_tee_session:
tee_client_close_context(priv->ctx);
priv->ctx = NULL;
return rc;
}
/**
* stm32_bsec_pta_remove() - remove the BSEC TEE device
* @dev: the platform_device description.
*
* Return:
* 0 always.
*/
static int stm32_bsec_pta_remove(struct device *dev)
{
struct stm32_bsec_pta_priv *priv = dev_get_drvdata(dev);
if (!IS_ERR_OR_NULL(priv->ctx)) {
tee_client_close_session(priv->ctx, priv->session_id);
tee_client_close_context(priv->ctx);
}
return 0;
}
/**
* stm32_bsec_pta_read() - nvmem read access using PTA client driver
* @context: nvmem context => romem privdate data
* @offset: nvmem offset
* @buf: buffer to fill with nvem values
* @bytes: number of bytes to read
*
* Return:
* On success, 0. On failure, -errno.
*/
static int stm32_bsec_pta_read(void *context, unsigned int offset, void *buf,
size_t bytes)
{
struct stm32_romem_priv *romem_priv = context;
struct device *dev;
struct stm32_bsec_pta_priv *priv;
struct tee_shm *shm;
struct tee_ioctl_invoke_arg arg;
struct tee_param param[2];
u8 *shm_buf;
u32 start, num_bytes;
int ret;
dev = romem_priv->ta;
if (!dev) {
pr_err("TA_BSEC invoke with driver\n");
return -ENXIO;
}
priv = dev_get_drvdata(dev);
memset(&arg, 0, sizeof(arg));
memset(¶m, 0, sizeof(param));
arg.func = PTA_BSEC_READ_MEM;
arg.session = priv->session_id;
arg.num_params = 2;
/* align access on 32bits */
start = ALIGN_DOWN(offset, 4);
num_bytes = round_up(offset + bytes - start, 4);
param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
param[0].u.value.a = start;
param[0].u.value.b = SHADOW_ACCESS;
shm = tee_shm_alloc_kernel_buf(priv->ctx, num_bytes);
if (IS_ERR(shm))
return PTR_ERR(shm);
param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
param[1].u.memref.shm = shm;
param[1].u.memref.size = num_bytes;
ret = tee_client_invoke_func(priv->ctx, &arg, param);
if (ret < 0 || arg.ret != 0) {
dev_err(dev, "TA_BSEC invoke failed TEE err: %x, ret:%x\n",
arg.ret, ret);
if (!ret)
ret = -EIO;
}
if (!ret) {
shm_buf = tee_shm_get_va(shm, 0);
if (IS_ERR(shm_buf)) {
dev_err(dev, "tee_shm_get_va failed for transmit\n");
ret = PTR_ERR(shm_buf);
} else {
ret = 0;
/* read data from 32 bits aligned buffer */
memcpy(buf, &shm_buf[offset % 4], bytes);
}
}
tee_shm_free(shm);
return ret;
}
/**
* stm32_bsec_pta_write() - nvmem write access using PTA client driver
* @context: nvmem context => romem privdate data
* @offset: nvmem offset
* @buf: buffer with nvem values
* @bytes: number of bytes to write
*
* Return:
* On success, 0. On failure, -errno.
*/
static int stm32_bsec_pta_write(void *context, unsigned int offset, void *buf,
size_t bytes)
{
struct stm32_romem_priv *romem_priv = context;
struct device *dev;
struct stm32_bsec_pta_priv *priv;
struct tee_shm *shm;
struct tee_ioctl_invoke_arg arg;
struct tee_param param[2];
u8 *shm_buf;
int ret;
dev = romem_priv->ta;
if (!dev) {
pr_err("TA_BSEC invoke with driver\n");
return -ENXIO;
}
/* Allow only writing complete 32-bits aligned words */
if ((bytes % 4) || (offset % 4))
return -EINVAL;
priv = dev_get_drvdata(dev);
memset(&arg, 0, sizeof(arg));
memset(¶m, 0, sizeof(param));
arg.func = PTA_BSEC_WRITE_MEM;
arg.session = priv->session_id;
arg.num_params = 2;
param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
param[0].u.value.a = offset;
param[0].u.value.b = FUSE_ACCESS;
shm = tee_shm_alloc_kernel_buf(priv->ctx, bytes);
if (IS_ERR(shm))
return PTR_ERR(shm);
param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
param[1].u.memref.shm = shm;
param[1].u.memref.size = bytes;
shm_buf = tee_shm_get_va(shm, 0);
if (IS_ERR(shm_buf)) {
dev_err(dev, "tee_shm_get_va failed for transmit\n");
return PTR_ERR(shm_buf);
}
memcpy(shm_buf, buf, bytes);
ret = tee_client_invoke_func(priv->ctx, &arg, param);
if (ret < 0 || arg.ret != 0) {
dev_err(dev, "TA_BSEC invoke failed TEE err: %x, ret:%x\n",
arg.ret, ret);
if (!ret)
ret = -EIO;
}
dev_dbg(dev, "Write OTPs %d to %d, ret=%d\n",
offset / 4, (offset + bytes) / 4, ret);
/* Lock the upper OTPs with ECC protection, word programming only */
if (!ret && ((offset + bytes) >= (romem_priv->lower * 4))) {
u32 start, nb_lock;
u32 *lock = (u32 *)shm_buf;
int i;
/*
* don't lock the lower OTPs, no ECC protection and incremental
* bit programming, a second write is allowed
*/
start = max_t(u32, offset, romem_priv->lower * 4);
nb_lock = (offset + bytes - start) / 4;
param[0].u.value.a = start;
param[0].u.value.b = LOCK_ACCESS;
param[1].u.memref.size = nb_lock * 4;
for (i = 0; i < nb_lock; i++)
lock[i] = LOCK_PERM;
ret = tee_client_invoke_func(priv->ctx, &arg, param);
if (ret < 0 || arg.ret != 0) {
dev_err(dev, "TA_BSEC invoke failed TEE err: %x, ret:%x\n",
arg.ret, ret);
if (!ret)
ret = -EIO;
}
dev_dbg(dev, "Lock upper OTPs %d to %d, ret=%d\n",
start / 4, start / 4 + nb_lock, ret);
}
tee_shm_free(shm);
return ret;
}
static const struct tee_client_device_id stm32_bsec_id_table[] = {
{
UUID_INIT(0x94cf71ad, 0x80e6, 0x40b5,
0xa7, 0xc6, 0x3d, 0xc5, 0x01, 0xeb, 0x28, 0x03)
},
{ }
};
MODULE_DEVICE_TABLE(tee, stm32_bsec_id_table);
static struct tee_client_driver stm32_bsec_pta_driver = {
.id_table = stm32_bsec_id_table,
.driver = {
.name = "stm32-bsec-pta",
.bus = &tee_bus_type,
.probe = stm32_bsec_pta_probe,
.remove = stm32_bsec_pta_remove,
},
};
static void stm32_bsec_put_device(void *data)
{
put_device(data);
}
struct device *stm32_bsec_pta_find(struct device *dev)
{
struct device *pta_dev;
pta_dev = driver_find_next_device(&stm32_bsec_pta_driver.driver, NULL);
if (pta_dev && devm_add_action_or_reset(dev, stm32_bsec_put_device, pta_dev)) {
dev_err(dev, "unable to register cleanup action\n");
return NULL;
}
return pta_dev;
}
#else
static int stm32_bsec_pta_read(void *context, unsigned int offset, void *buf,
size_t bytes)
{
pr_debug("%s: TA BSEC request without OPTEE support\n", __func__);
return -ENXIO;
}
static int stm32_bsec_pta_write(void *context, unsigned int offset, void *buf,
size_t bytes)
{
pr_debug("%s: TA BSEC request without OPTEE support\n", __func__);
return -ENXIO;
}
struct device *stm32_bsec_pta_find(struct device *dev)
{
pr_debug("%s: TA BSEC request without OPTEE support\n", __func__);
return NULL;
}
#endif
static int __init stm32_romem_init(void)
{
int rc;
rc = platform_driver_register(&stm32_romem_driver);
if (rc)
return rc;
#if IS_ENABLED(CONFIG_OPTEE)
rc = driver_register(&stm32_bsec_pta_driver.driver);
#endif
return rc;
}
static void __exit stm32_romem_exit(void)
{
platform_driver_unregister(&stm32_romem_driver);
#if IS_ENABLED(CONFIG_OPTEE)
driver_unregister(&stm32_bsec_pta_driver.driver);
#endif
}
module_init(stm32_romem_init);
module_exit(stm32_romem_exit);
MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
MODULE_DESCRIPTION("STMicroelectronics STM32 RO-MEM");
MODULE_ALIAS("platform:nvmem-stm32-romem");
MODULE_LICENSE("GPL v2");