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
|
/*
* Copyright 2021 NXP
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <caam.h>
#include <common/debug.h>
#include <dcfg.h>
#include <drivers/delay_timer.h>
#include <fuse_prov.h>
#include <sfp.h>
#include <sfp_error_codes.h>
static int write_a_fuse(uint32_t *fuse_addr, uint32_t *fuse_hdr_val,
uint32_t mask)
{
uint32_t last_stored_val = sfp_read32(fuse_addr);
/* Check if fuse already blown or not */
if ((last_stored_val & mask) == mask) {
return ERROR_ALREADY_BLOWN;
}
/* Write fuse in mirror registers */
sfp_write32(fuse_addr, last_stored_val | (*fuse_hdr_val & mask));
/* Read back to check if write success */
if (sfp_read32(fuse_addr) != (last_stored_val | (*fuse_hdr_val & mask))) {
return ERROR_WRITE;
}
return 0;
}
static int write_fuses(uint32_t *fuse_addr, uint32_t *fuse_hdr_val, uint8_t len)
{
int i;
/* Check if fuse already blown or not */
for (i = 0; i < len; i++) {
if (sfp_read32(&fuse_addr[i]) != 0) {
return ERROR_ALREADY_BLOWN;
}
}
/* Write fuse in mirror registers */
for (i = 0; i < len; i++) {
sfp_write32(&fuse_addr[i], fuse_hdr_val[i]);
}
/* Read back to check if write success */
for (i = 0; i < len; i++) {
if (sfp_read32(&fuse_addr[i]) != fuse_hdr_val[i]) {
return ERROR_WRITE;
}
}
return 0;
}
/*
* This function program Super Root Key Hash (SRKH) in fuse
* registers.
*/
static int prog_srkh(struct fuse_hdr_t *fuse_hdr,
struct sfp_ccsr_regs_t *sfp_ccsr_regs)
{
int ret = 0;
ret = write_fuses(sfp_ccsr_regs->srk_hash, fuse_hdr->srkh, 8);
if (ret != 0) {
ret = (ret == ERROR_ALREADY_BLOWN) ?
ERROR_SRKH_ALREADY_BLOWN : ERROR_SRKH_WRITE;
}
return ret;
}
/* This function program OEMUID[0-4] in fuse registers. */
static int prog_oemuid(struct fuse_hdr_t *fuse_hdr,
struct sfp_ccsr_regs_t *sfp_ccsr_regs)
{
int i, ret = 0;
for (i = 0; i < 5; i++) {
/* Check OEMUIDx to be blown or not */
if (((fuse_hdr->flags >> (FLAG_OUID0_SHIFT + i)) & 0x1) != 0) {
/* Check if OEMUID[i] already blown or not */
ret = write_fuses(&sfp_ccsr_regs->oem_uid[i],
&fuse_hdr->oem_uid[i], 1);
if (ret != 0) {
ret = (ret == ERROR_ALREADY_BLOWN) ?
ERROR_OEMUID_ALREADY_BLOWN
: ERROR_OEMUID_WRITE;
}
}
}
return ret;
}
/* This function program DCV[0-1], DRV[0-1] in fuse registers. */
static int prog_debug(struct fuse_hdr_t *fuse_hdr,
struct sfp_ccsr_regs_t *sfp_ccsr_regs)
{
int ret;
/* Check DCV to be blown or not */
if (((fuse_hdr->flags >> (FLAG_DCV0_SHIFT)) & 0x3) != 0) {
/* Check if DCV[i] already blown or not */
ret = write_fuses(sfp_ccsr_regs->dcv, fuse_hdr->dcv, 2);
if (ret != 0) {
ret = (ret == ERROR_ALREADY_BLOWN) ?
ERROR_DCV_ALREADY_BLOWN
: ERROR_DCV_WRITE;
}
}
/* Check DRV to be blown or not */
if ((((fuse_hdr->flags >> (FLAG_DRV0_SHIFT)) & 0x3)) != 0) {
/* Check if DRV[i] already blown or not */
ret = write_fuses(sfp_ccsr_regs->drv, fuse_hdr->drv, 2);
if (ret != 0) {
ret = (ret == ERROR_ALREADY_BLOWN) ?
ERROR_DRV_ALREADY_BLOWN
: ERROR_DRV_WRITE;
} else {
/* Check for DRV hamming error */
if (sfp_read32((void *)(get_sfp_addr()
+ SFP_SVHESR_OFFSET))
& SFP_SVHESR_DRV_MASK) {
return ERROR_DRV_HAMMING_ERROR;
}
}
}
return 0;
}
/*
* Turn a 256-bit random value (32 bytes) into an OTPMK code word
* modifying the input data array in place
*/
static void otpmk_make_code_word_256(uint8_t *otpmk, bool minimal_flag)
{
int i;
uint8_t parity_bit;
uint8_t code_bit;
if (minimal_flag == true) {
/*
* Force bits 252, 253, 254 and 255 to 1
* This is because these fuses may have already been blown
* and the OTPMK cannot force them back to 0
*/
otpmk[252/8] |= (1 << (252%8));
otpmk[253/8] |= (1 << (253%8));
otpmk[254/8] |= (1 << (254%8));
otpmk[255/8] |= (1 << (255%8));
}
/* Generate the hamming code for the code word */
parity_bit = 0;
code_bit = 0;
for (i = 0; i < 256; i += 1) {
if ((otpmk[i/8] & (1 << (i%8))) != 0) {
parity_bit ^= 1;
code_bit ^= i;
}
}
/* Inverting otpmk[code_bit] will cause the otpmk
* to become a valid code word (except for overall parity)
*/
if (code_bit < 252) {
otpmk[code_bit/8] ^= (1 << (code_bit % 8));
parity_bit ^= 1; // account for flipping a bit changing parity
} else {
/* Invert two bits: (code_bit - 4) and 4
* Because we invert two bits, no need to touch the parity bit
*/
otpmk[(code_bit - 4)/8] ^= (1 << ((code_bit - 4) % 8));
otpmk[4/8] ^= (1 << (4 % 8));
}
/* Finally, adjust the overall parity of the otpmk
* otpmk bit 0
*/
otpmk[0] ^= parity_bit;
}
/* This function program One Time Programmable Master Key (OTPMK)
* in fuse registers.
*/
static int prog_otpmk(struct fuse_hdr_t *fuse_hdr,
struct sfp_ccsr_regs_t *sfp_ccsr_regs)
{
int ret = 0;
uint32_t otpmk_flags;
uint32_t otpmk_random[8] __aligned(CACHE_WRITEBACK_GRANULE);
otpmk_flags = (fuse_hdr->flags >> (FLAG_OTPMK_SHIFT)) & FLAG_OTPMK_MASK;
switch (otpmk_flags) {
case PROG_OTPMK_MIN:
memset(fuse_hdr->otpmk, 0, sizeof(fuse_hdr->otpmk));
/* Minimal OTPMK value (252-255 bits set to 1) */
fuse_hdr->otpmk[0] |= OTPMK_MIM_BITS_MASK;
break;
case PROG_OTPMK_RANDOM:
if (is_sec_enabled() == false) {
ret = ERROR_OTPMK_SEC_DISABLED;
goto out;
}
/* Generate Random number using CAAM for OTPMK */
memset(otpmk_random, 0, sizeof(otpmk_random));
if (get_rand_bytes_hw((uint8_t *)otpmk_random,
sizeof(otpmk_random)) != 0) {
ret = ERROR_OTPMK_SEC_ERROR;
goto out;
}
/* Run hamming over random no. to make OTPMK */
otpmk_make_code_word_256((uint8_t *)otpmk_random, false);
/* Swap OTPMK */
fuse_hdr->otpmk[0] = otpmk_random[7];
fuse_hdr->otpmk[1] = otpmk_random[6];
fuse_hdr->otpmk[2] = otpmk_random[5];
fuse_hdr->otpmk[3] = otpmk_random[4];
fuse_hdr->otpmk[4] = otpmk_random[3];
fuse_hdr->otpmk[5] = otpmk_random[2];
fuse_hdr->otpmk[6] = otpmk_random[1];
fuse_hdr->otpmk[7] = otpmk_random[0];
break;
case PROG_OTPMK_USER:
break;
case PROG_OTPMK_RANDOM_MIN:
/* Here assumption is that user is aware of minimal OTPMK
* already blown.
*/
/* Generate Random number using CAAM for OTPMK */
if (is_sec_enabled() == false) {
ret = ERROR_OTPMK_SEC_DISABLED;
goto out;
}
memset(otpmk_random, 0, sizeof(otpmk_random));
if (get_rand_bytes_hw((uint8_t *)otpmk_random,
sizeof(otpmk_random)) != 0) {
ret = ERROR_OTPMK_SEC_ERROR;
goto out;
}
/* Run hamming over random no. to make OTPMK */
otpmk_make_code_word_256((uint8_t *)otpmk_random, true);
/* Swap OTPMK */
fuse_hdr->otpmk[0] = otpmk_random[7];
fuse_hdr->otpmk[1] = otpmk_random[6];
fuse_hdr->otpmk[2] = otpmk_random[5];
fuse_hdr->otpmk[3] = otpmk_random[4];
fuse_hdr->otpmk[4] = otpmk_random[3];
fuse_hdr->otpmk[5] = otpmk_random[2];
fuse_hdr->otpmk[6] = otpmk_random[1];
fuse_hdr->otpmk[7] = otpmk_random[0];
break;
case PROG_OTPMK_USER_MIN:
/*
* Here assumption is that user is aware of minimal OTPMK
* already blown. Check if minimal bits are set in user
* supplied OTPMK.
*/
if ((fuse_hdr->otpmk[0] & OTPMK_MIM_BITS_MASK) !=
OTPMK_MIM_BITS_MASK) {
ret = ERROR_OTPMK_USER_MIN;
goto out;
}
break;
default:
ret = 0;
goto out;
}
ret = write_fuses(sfp_ccsr_regs->otpmk, fuse_hdr->otpmk, 8);
if (ret != 0) {
ret = (ret == ERROR_ALREADY_BLOWN) ?
ERROR_OTPMK_ALREADY_BLOWN
: ERROR_OTPMK_WRITE;
} else {
/* Check for DRV hamming error */
if ((sfp_read32((void *)(get_sfp_addr() + SFP_SVHESR_OFFSET))
& SFP_SVHESR_OTPMK_MASK) != 0) {
ret = ERROR_OTPMK_HAMMING_ERROR;
}
}
out:
return ret;
}
/* This function program OSPR1 in fuse registers.
*/
static int prog_ospr1(struct fuse_hdr_t *fuse_hdr,
struct sfp_ccsr_regs_t *sfp_ccsr_regs)
{
int ret;
uint32_t mask = 0;
#ifdef NXP_SFP_VER_3_4
if (((fuse_hdr->flags >> FLAG_MC_SHIFT) & 0x1) != 0) {
mask = OSPR1_MC_MASK;
}
#endif
if (((fuse_hdr->flags >> FLAG_DBG_LVL_SHIFT) & 0x1) != 0) {
mask = mask | OSPR1_DBG_LVL_MASK;
}
ret = write_a_fuse(&sfp_ccsr_regs->ospr1, &fuse_hdr->ospr1, mask);
if (ret != 0) {
ret = (ret == ERROR_ALREADY_BLOWN) ?
ERROR_OSPR1_ALREADY_BLOWN
: ERROR_OSPR1_WRITE;
}
return ret;
}
/* This function program SYSCFG in fuse registers.
*/
static int prog_syscfg(struct fuse_hdr_t *fuse_hdr,
struct sfp_ccsr_regs_t *sfp_ccsr_regs)
{
int ret;
/* Check if SYSCFG already blown or not */
ret = write_a_fuse(&sfp_ccsr_regs->ospr, &fuse_hdr->sc, OSPR0_SC_MASK);
if (ret != 0) {
ret = (ret == ERROR_ALREADY_BLOWN) ?
ERROR_SC_ALREADY_BLOWN
: ERROR_SC_WRITE;
}
return ret;
}
/* This function does fuse provisioning.
*/
int provision_fuses(unsigned long long fuse_scr_addr,
bool en_povdd_status)
{
struct fuse_hdr_t *fuse_hdr = NULL;
struct sfp_ccsr_regs_t *sfp_ccsr_regs = (void *)(get_sfp_addr()
+ SFP_FUSE_REGS_OFFSET);
int ret = 0;
fuse_hdr = (struct fuse_hdr_t *)fuse_scr_addr;
/*
* Check for Write Protect (WP) fuse. If blown then do
* no fuse provisioning.
*/
if ((sfp_read32(&sfp_ccsr_regs->ospr) & 0x1) != 0) {
goto out;
}
/* Check if SRKH to be blown or not */
if (((fuse_hdr->flags >> FLAG_SRKH_SHIFT) & 0x1) != 0) {
INFO("Fuse: Program SRKH\n");
ret = prog_srkh(fuse_hdr, sfp_ccsr_regs);
if (ret != 0) {
error_handler(ret);
goto out;
}
}
/* Check if OEMUID to be blown or not */
if (((fuse_hdr->flags >> FLAG_OUID0_SHIFT) & FLAG_OUID_MASK) != 0) {
INFO("Fuse: Program OEMUIDs\n");
ret = prog_oemuid(fuse_hdr, sfp_ccsr_regs);
if (ret != 0) {
error_handler(ret);
goto out;
}
}
/* Check if Debug values to be blown or not */
if (((fuse_hdr->flags >> FLAG_DCV0_SHIFT) & FLAG_DEBUG_MASK) != 0) {
INFO("Fuse: Program Debug values\n");
ret = prog_debug(fuse_hdr, sfp_ccsr_regs);
if (ret != 0) {
error_handler(ret);
goto out;
}
}
/* Check if OTPMK values to be blown or not */
if (((fuse_hdr->flags >> FLAG_OTPMK_SHIFT) & PROG_NO_OTPMK) !=
PROG_NO_OTPMK) {
INFO("Fuse: Program OTPMK\n");
ret = prog_otpmk(fuse_hdr, sfp_ccsr_regs);
if (ret != 0) {
error_handler(ret);
goto out;
}
}
/* Check if MC or DBG LVL to be blown or not */
if ((((fuse_hdr->flags >> FLAG_MC_SHIFT) & 0x1) != 0) ||
(((fuse_hdr->flags >> FLAG_DBG_LVL_SHIFT) & 0x1) != 0)) {
INFO("Fuse: Program OSPR1\n");
ret = prog_ospr1(fuse_hdr, sfp_ccsr_regs);
if (ret != 0) {
error_handler(ret);
goto out;
}
}
/* Check if SYSCFG to be blown or not */
if (((fuse_hdr->flags >> FLAG_SYSCFG_SHIFT) & 0x1) != 0) {
INFO("Fuse: Program SYSCFG\n");
ret = prog_syscfg(fuse_hdr, sfp_ccsr_regs);
if (ret != 0) {
error_handler(ret);
goto out;
}
}
if (en_povdd_status) {
ret = sfp_program_fuses();
if (ret != 0) {
error_handler(ret);
goto out;
}
}
out:
return ret;
}
|