summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/nvddk/nvddk_fuse_ap20.c
blob: 804be01ddf8902b8acdda68222c62eb36f58fe6a (plain)
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
/*
 * Copyright (c) 2010 NVIDIA Corporation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the NVIDIA Corporation nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "fuse_bitmap.h"
#include "ap20/arclk_rst.h"
#include "ap20/arfuse.h"
#include "nvddk_bootdevices.h"
#include "nvddk_operatingmodes.h"
#include "nvddk_fuse.h"
#include "nvrm_hardware_access.h"
#include "nvassert.h"
#include "nvrm_drf.h"
#include "nvos.h"
#include "nvodm_query_discovery.h"
#include "nvodm_services.h"
#include "nvrm_pmu.h"

// Set below macro to 0, to disable voltage required for Fuse programming.
#define ENABLE_FUSE_VOLTAGE 1
// By setting below macro to 1, a dummy interface of fuse is enabled for
// verifying sysfs interface.
#define FUSE_DUMMY_CODE  0
// Set below macro to 1 to get prints during fuse read/ program operations.
#define ENABLE_DEBUG_PRINTS 0

#define FUSE_BOOT_DEVICE_INFO_0_BOOT_DEVICE_CONFIG_RANGE 13:0
#define FUSE_RESERVED_SW_0_BOOT_DEVICE_SELECT_RANGE       2:0
#define FUSE_RESERVED_SW_0_SKIP_DEV_SEL_STRAPS_RANGE      3:3
#define FUSE_RESERVED_SW_0_SW_RESERVED_RANGE              7:4

#define FLAGS_PROGRAM_SECURE_BOOT_KEY     0x001
#define FLAGS_PROGRAM_DEVICE_KEY          0x002
#define FLAGS_PROGRAM_JTAG_DISABLE        0x004
#define FLAGS_PROGRAM_BOOT_DEV_SEL        0x008
#define FLAGS_PROGRAM_BOOT_DEV_CONFIG     0x010
#define FLAGS_PROGRAM_SW_RESERVED         0x020
#define FLAGS_PROGRAM_ODM_PRODUCTION      0x040
#define FLAGS_PROGRAM_SPARE_BITS          0x080
#define FLAGS_PROGRAM_SKIP_DEV_SEL_STRAPS 0x100
#define FLAGS_PROGRAM_RESERVED_ODM 0x200

#define FLAGS_PROGRAM_ILLEGAL         0x80000000

#define NVDDK_DEVICE_KEY_BYTES       (4)
#define NVDDK_SECURE_BOOT_KEY_BYTES  (16)
#define NVDDK_RESERVED_ODM_BYTES  (32)

#define NVDDK_TPROGRAM_VALUE_CORRECTED 0

// Number of words of fuses as defined by the hardware.
#define FUSE_WORDS 64

/*
 * Representation of the fuse data.
 *
 */
typedef struct NvDdkFuseDataRec
{
    // Specifies the Secure Boot Key (SBK).
    NvU8   SecureBootKey[NVDDK_SECURE_BOOT_KEY_BYTES];

    // Specifies the Device Key (DK).
    NvU8   DeviceKey[NVDDK_DEVICE_KEY_BYTES];

    // Specifies the JTAG Disable fuse value.
    NvBool JtagDisable;

    // Specifies the device selection value in terms of the API.
    NvU32  SecBootDeviceSelect;

    // Specifies the device selection value in the actual fuses.
    NvU32  SecBootDeviceSelectRaw;

    // Specifies the device configuration value (right aligned).
    NvU32  SecBootDeviceConfig;

    // Specifies the SwReserved value.
    NvU32  SwReserved;

    // Specifies the ODM Production fuse value.
    NvBool OdmProduction;

    // Specifies the SkipDevSelStraps value.
    NvU32  SkipDevSelStraps;

    // Specifies the Reserved Odm value
    NvU8    ReservedOdm[NVDDK_RESERVED_ODM_BYTES];

    // Flags that indicate what to program.
    NvU32  ProgramFlags;

    // Flag indicates whether reservedodm fuses are setting or not
    NvBool ReservedOdmFlag;
} NvDdkFuseData;

static NvDdkFuseData s_FuseData = {{0}};

typedef enum
{
    FuseBootDev_Sdmmc,
    FuseBootDev_SnorFlash,
    FuseBootDev_SpiFlash,
    FuseBootDev_NandFlash,
    FuseBootDev_NandFlash_x8  = FuseBootDev_NandFlash,
    FuseBootDev_NandFlash_x16 = FuseBootDev_NandFlash,
    FuseBootDev_MobileLbaNand,
    FuseBootDev_MuxOneNand,
    FuseBootDev_Max, /* Must appear after the last legal item */
    FuseBootDev_Force32 = 0x7fffffff
} FuseBootDev;

// Holds various params that are needed by Fuse module.
typedef struct NvDdkFuseRec
{
    // Fuse registers physical base address
    NvRmPhysAddr pBaseAddress;
    // Holds the virtual address for accessing registers.
    NvU32 *pVirtualAddress;
    // Holds the register map size.
    NvU32 BankSize;

    // Fuse registers physical base address
    NvRmPhysAddr pCarBaseAddress;
    // Holds the virtual address for accessing registers.
    NvU32 *pCarVirtualAddress;
    // Holds the register map size.
    NvU32 CarBankSize;

    // Mutex for fuse
    NvOsMutexHandle Mutex;
}NvDdkFuse;

static NvDdkFuse* s_pFuseRec = NULL;

// Size of the data items, in bytes
static NvU32 s_DataSize[] =
{
    0, // FuseDataType_None
    sizeof(NvU64),  // FuseDataType_DeviceKey
    sizeof(NvBool), // FuseDataType_JtagDisable
    sizeof(NvBool), // FuseDataType_KeyProgrammed
    sizeof(NvBool), // FuseDataType_OdmProductionEnable
    sizeof(NvU32),  // FuseDataType_SecBootDeviceConfig
    sizeof(NvU32),  // FuseDataType_SecBootDeviceSelect
    NVDDK_SECURE_BOOT_KEY_BYTES, // FuseDataType_SecureBootKey
    sizeof(NvU32),  // FuseDataType_Sku
    sizeof(NvU32),  // FuseDataType_SpareBits
    sizeof(NvU32),  // FuseDataType_SwReserved
    sizeof(NvBool), // FuseDataType_SkipDevSelStraps
    sizeof(NvU32),  // FuseDataType_SecBootDeviceSelectRaw
    NVDDK_RESERVED_ODM_BYTES  // NvDdkFuseDataType_ReservedOdm fuse
};


// Map NvDdkSecBootDeviceType enum onto AP20 Boot Device Selection values.
static NvU32 NvDdkToFuseBootDeviceSel[] =
{
    FuseBootDev_Sdmmc,         // None
    FuseBootDev_NandFlash,     // NvDdkSecBootDeviceType_Nand
    FuseBootDev_SnorFlash,     // NvDdkSecBootDeviceType_Nor
    FuseBootDev_SpiFlash,      // NvDdkSecBootDeviceType_Spi_Flash
    FuseBootDev_Sdmmc,         // NvDdkSecBootDeviceType_eMMC
    FuseBootDev_NandFlash,     // NvDdkSecBootDeviceType_Nand_x16
    FuseBootDev_MobileLbaNand, // NvDdkSecBootDeviceType_MobileLbaNand
    FuseBootDev_Sdmmc,         // NvDdkSecBootDeviceType_eMMC
    FuseBootDev_MuxOneNand,    // NvDdkSecBootDeviceType_MuxOneNand
};

// Map AP20 fuse values onto NvDdkSecBootDeviceType enum values.
static NvU32 FuseToNvDdkBootDeviceSel[] =
{
    NvDdkSecBootDeviceType_Sdmmc,         // FuseBootDev_Sdmmc
    NvDdkSecBootDeviceType_Nor,           // FuseBootDev_SnorFlash
    NvDdkSecBootDeviceType_Spi,           // FuseBootDev_SpiFlash
    NvDdkSecBootDeviceType_Nand,          // FuseBootDev_NandFlash
    NvDdkSecBootDeviceType_MobileLbaNand, // FuseBootDev_MobileLbaNand
    NvDdkSecBootDeviceType_MuxOneNand,    // FuseBootDev_MuxOneNand
};

// Storage for the array of fuse words & mask words.
static NvU32 s_FuseArray   [FUSE_WORDS] = { 0 };
static NvU32 s_MaskArray   [FUSE_WORDS] = { 0 };
static NvU32 s_TempFuseData[FUSE_WORDS];


#define FUSE_NV_READ32(offset) \
    NV_READ32(s_pFuseRec->pVirtualAddress + (offset >> 2))

#define FUSE_NV_WRITE32(offset, val) \
    NV_WRITE32((s_pFuseRec->pVirtualAddress + (offset >> 2)), val)

#define CLOCK_NV_READ32(offset) \
    NV_READ32(s_pFuseRec->pCarVirtualAddress + (offset >> 2))

#define CLOCK_NV_WRITE32(offset, val) \
    NV_WRITE32((s_pFuseRec->pCarVirtualAddress + (offset >> 2)), val)

// Macro to get the difference between two numbers T1 & T2 and T1 > T2.
#define DIFF(T1, T2) \
 (((T1) > (T2)) ? ((T1) - (T2)) : ((T2) - (T1)))
// For rollover proof,
// (((T1) > (T2)) ? ((T1) - (T2)) : ((T1) + ((NvU64)0xFFFFFFFFFFFFFFFF - (T2))))

static void NvFuseUtilWaitUS(NvU32 usec)
{
    NvU64 t0;
    NvU64 t1;

    t0 = NvOsGetTimeUS();
    t1 = t0;
    // Use the difference for the comparison to be wraparound safe
    while (DIFF(t1, t0) < usec)
    {
        t1 = NvOsGetTimeUS();
    }
}

static void fusememset(void* Source, NvU8 val, NvU32 size)
{
    NvOsMemset(Source, val, size);
}

static void fusememcpy(void* Destination, void* Source, NvU32 size)
{
    NvOsMemcpy(Destination, Source, size);
}
/**
 * Reports whether any of the SBK fuses are set (burned)
 *
 * @param none
 *
 * @return NV_TRUE if the SBK is non-zero
 * @return NV_FALSE otherwise
 */
static NvBool NvDdkFuseIsSbkSet(void)
{
    NvU32 AllKeysOred;
    AllKeysOred  = FUSE_NV_READ32(FUSE_PRIVATE_KEY0_NONZERO_0);
    AllKeysOred |= FUSE_NV_READ32(FUSE_PRIVATE_KEY1_NONZERO_0);
    AllKeysOred |= FUSE_NV_READ32(FUSE_PRIVATE_KEY2_NONZERO_0);
    AllKeysOred |= FUSE_NV_READ32(FUSE_PRIVATE_KEY3_NONZERO_0);
    if (AllKeysOred)
        return NV_TRUE;
    else
        return NV_FALSE;
}

/**
 * Reports whether any of the SBK or DK fuses are set (burned)
 *
 * @param none
 *
 * @return NV_TRUE if the SBK or the DK is non-zero
 * @return NV_FALSE otherwise
 */
static NvBool IsSbkOrDkSet(void)
{
    NvU32 AllKeysOred;

    AllKeysOred  = NvDdkFuseIsSbkSet();
    AllKeysOred |= FUSE_NV_READ32(FUSE_PRIVATE_KEY4_NONZERO_0);

    if (AllKeysOred)
        return NV_TRUE;
    else
        return NV_FALSE;
}


/**
 * Reports whether the ODM Production Mode fuse is set (burned)
 *
 * Note that this fuse by itself does not determine whether the chip is in
 * ODM Production Mode.
 *
 * @param none
 *
 * @return NV_TRUE if ODM Production Mode fuse is set (burned); else NV_FALSE
 */
static NvBool NvDdkFuseIsOdmProductionModeFuseSet(void)
{
    NvU32 RegValue;
    RegValue = FUSE_NV_READ32(FUSE_SECURITY_MODE_0);
    if (RegValue)
    {
        return NV_TRUE;
    }
    else
    {
        return NV_FALSE;
    }
}


/**
 * Reports whether the Disable Register Program register is set.
 *
 * @param none
 *
 * @return NV_TRUE if  Disable reg program is iset, else NV_FALSE
 */

static NvBool NvDdkPrivIsDisableRegProgramSet(void)
{
    NvU32 RegValue;
    RegValue = FUSE_NV_READ32(FUSE_DISABLEREGPROGRAM_0);
    if (RegValue)
    {
        return NV_TRUE;
    }
    else
    {
        return NV_FALSE;
    }
}

static NvBool NvDdkFuseIsOdmProductionMode(void)
{
    if (NvDdkFuseIsOdmProductionModeFuseSet())
    {
        return NV_TRUE;
    }
    return NV_FALSE;
}

static NvU32 NvDdkFuseGetSecBootDeviceRaw(void)
{
    NvU32 RegData;

    RegData = FUSE_NV_READ32(FUSE_RESERVED_SW_0);
    RegData = NV_DRF_VAL(FUSE, RESERVED_SW, BOOT_DEVICE_SELECT, RegData);
    if (RegData >= (NvU32) FuseBootDev_Max)
    {
        RegData = FuseBootDev_Sdmmc;
    }

    return RegData;
}

static NvDdkSecBootDeviceType NvDdkFuseGetSecBootDevice(void)
{
    NvU32                 RegData;
    NvDdkSecBootDeviceType SecBootDevice;

    RegData = NvDdkFuseGetSecBootDeviceRaw();

    // Map from AP20 definitions onto NvDdkSecBootDeviceType definitions.
    SecBootDevice = FuseToNvDdkBootDeviceSel[RegData];

    return SecBootDevice;
}

// p = prefix, c = copy, i = index, d = data

#define FUSE_BASE(p, c, i) FUSE_##p##__##c##_ALIAS_##i
#define FUSE_DATA(p, c, i) FUSE_##p##__##c##_ALIAS_##i##_DATA
#define FUSE_WIDTH(p, c, i) FUSE_##p##__##c##_ALIAS_##i##_WIDTH

#define SET_FUSE(p, c, i, d)                       \
    s_FuseArray[FUSE_BASE(p, c, i)] =              \
    (s_FuseArray[FUSE_BASE(p, c, i)] &             \
    ~NV_FIELD_SHIFTMASK(FUSE_DATA(p, c, i))) |     \
    ((d & (NV_FIELD_MASK(FUSE_DATA(p, c, i)))) <<  \
    (NV_FIELD_SHIFT(FUSE_DATA(p, c, i))))

#define SET_MASK(p, c, i) \
    s_MaskArray[FUSE_BASE(p,c,i)] |= NV_FIELD_SHIFTMASK(FUSE_DATA(p,c,i))

#define SET_FUSE_DATA(p, c, i, d) \
    SET_FUSE(p,c,i,d);            \
    SET_MASK(p,c,i);

#define SET_FUSE_PRI(p, i, d) SET_FUSE_DATA(p, PRI, i, d);
#define SET_FUSE_RED(p, i, d) SET_FUSE_DATA(p, RED, i, d);

#define SET_FUSE_BOTH(p, i, d)    \
    SET_FUSE_PRI(p,i,d);  \
    SET_FUSE_RED(p,i,d);

// Update fuses with an explicit mask.
#define SET_FUSE_BOTH_WITH_MASK(p, i, d, md)         \
    SET_FUSE(p, PRI, i, d);                       \
    SET_FUSE(p, RED, i, d);                       \
    s_MaskArray[FUSE_BASE(p, PRI, i)] |=          \
      (md << NV_FIELD_SHIFT(FUSE_DATA(p, PRI, i))); \
    s_MaskArray[FUSE_BASE(p, RED, i)] |=          \
      (md << NV_FIELD_SHIFT(FUSE_DATA(p, RED, i)));

#define UPDATE_DATA(p,i,d) (d >>= FUSE_WIDTH(p,PRI,i));

#define SET_SPARE_BIT(n)                   \
    SET_FUSE_PRI(SPARE_BIT_##n, 0, Data);  \
    UPDATE_DATA (SPARE_BIT_##n, 0, Data);


static void MapDataToFuseArrays(void)
{
    NvU32 *Src;
    NvU32  Data = 0;
    NvU32  MaskData;

    // Start by clearing the arrays.
    fusememset(s_FuseArray, 0, sizeof(s_FuseArray));
    fusememset(s_MaskArray, 0, sizeof(s_MaskArray));
    // Set ENABLE_FUSE_PROGRAM bit to 1
    SET_FUSE_BOTH(ENABLE_FUSE_PROGRAM, 0, 1);

    // Reserved Odm fuse
    if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_RESERVED_ODM)
    {
        Src = (NvU32*)(s_FuseData.ReservedOdm);
        Data = Src[0];
        SET_FUSE_BOTH(RESERVED_ODM0, 0, Data);
        UPDATE_DATA  (RESERVED_ODM0, 0, Data);
        SET_FUSE_BOTH(RESERVED_ODM0, 1, Data);

        Data = Src[1];
        SET_FUSE_BOTH(RESERVED_ODM1, 0, Data);
        UPDATE_DATA  (RESERVED_ODM1, 0, Data);
        SET_FUSE_BOTH(RESERVED_ODM1, 1, Data);

        Data = Src[2];
        SET_FUSE_BOTH(RESERVED_ODM2, 0, Data);
        UPDATE_DATA  (RESERVED_ODM2, 0, Data);
        SET_FUSE_BOTH(RESERVED_ODM2, 1, Data);

        Data = Src[3];
        SET_FUSE_BOTH(RESERVED_ODM3, 0, Data);
        UPDATE_DATA  (RESERVED_ODM3, 0, Data);
        SET_FUSE_BOTH(RESERVED_ODM3, 1, Data);

        Data = Src[4];
        SET_FUSE_BOTH(RESERVED_ODM4, 0, Data);
        UPDATE_DATA  (RESERVED_ODM4, 0, Data);
        SET_FUSE_BOTH(RESERVED_ODM4, 1, Data);

        Data = Src[5];
        SET_FUSE_BOTH(RESERVED_ODM5, 0, Data);
        UPDATE_DATA  (RESERVED_ODM5, 0, Data);
        SET_FUSE_BOTH(RESERVED_ODM5, 1, Data);

        Data = Src[6];
        SET_FUSE_BOTH(RESERVED_ODM6, 0, Data);
        UPDATE_DATA  (RESERVED_ODM6, 0, Data);
        SET_FUSE_BOTH(RESERVED_ODM6, 1, Data);

        Data = Src[7];
        SET_FUSE_BOTH(RESERVED_ODM7, 0, Data);
        UPDATE_DATA  (RESERVED_ODM7, 0, Data);
        SET_FUSE_BOTH(RESERVED_ODM7, 1, Data);
    }

    // If Secure mode is set, we can not burn any other fuses, so return.
    if (NvDdkFuseIsOdmProductionModeFuseSet())
    {
        return;
    }

    if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_JTAG_DISABLE)
    {
        Data = (NvU32)(s_FuseData.JtagDisable);
        SET_FUSE_BOTH(ARM_DEBUG_DIS, 0, Data);
    }

    if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_ODM_PRODUCTION)
    {
        Data = (NvU32)(s_FuseData.OdmProduction);
        SET_FUSE_BOTH(SECURITY_MODE, 0, Data);
    }

    // SBK & DK
    if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_SECURE_BOOT_KEY)
    {
        Src = (NvU32*)(s_FuseData.SecureBootKey);
        Data = Src[0];
        SET_FUSE_BOTH(PRIVATE_KEY0, 0, Data);
        UPDATE_DATA  (PRIVATE_KEY0, 0, Data);
        SET_FUSE_BOTH(PRIVATE_KEY0, 1, Data);

        Data = Src[1];
        SET_FUSE_BOTH(PRIVATE_KEY1, 0, Data);
        UPDATE_DATA  (PRIVATE_KEY1, 0, Data);
        SET_FUSE_BOTH(PRIVATE_KEY1, 1, Data);

        Data = Src[2];
        SET_FUSE_BOTH(PRIVATE_KEY2, 0, Data);
        UPDATE_DATA  (PRIVATE_KEY2, 0, Data);
        SET_FUSE_BOTH(PRIVATE_KEY2, 1, Data);

        Data = Src[3];
        SET_FUSE_BOTH(PRIVATE_KEY3, 0, Data);
        UPDATE_DATA  (PRIVATE_KEY3, 0, Data);
        SET_FUSE_BOTH(PRIVATE_KEY3, 1, Data);
    }

    if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_DEVICE_KEY)
    {
        Src = (NvU32*)(s_FuseData.DeviceKey);
        Data = Src[0];
        SET_FUSE_BOTH(PRIVATE_KEY4, 0, Data);
        UPDATE_DATA  (PRIVATE_KEY4, 0, Data);
        SET_FUSE_BOTH(PRIVATE_KEY4, 1, Data);
    }

    if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_BOOT_DEV_CONFIG)
    {
        // BootDeviceConfig is 14 bits so mask other bits
        Data = (s_FuseData.SecBootDeviceConfig  & 0x3FFF);
        SET_FUSE_BOTH(BOOT_DEVICE_INFO, 0, Data);
    }

    // Assemble RESERVED_SW
    if ((s_FuseData.ProgramFlags & FLAGS_PROGRAM_BOOT_DEV_SEL       ) ||
        (s_FuseData.ProgramFlags & FLAGS_PROGRAM_SKIP_DEV_SEL_STRAPS) ||
        (s_FuseData.ProgramFlags & FLAGS_PROGRAM_SW_RESERVED        ))
    {
        Data = 0;
        MaskData = 0;

        if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_BOOT_DEV_SEL)
        {
            Data     |= (s_FuseData.SecBootDeviceSelectRaw & 0x7);
            MaskData |= 0x7;
        }

        if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_SKIP_DEV_SEL_STRAPS)
        {
            Data     |= ((s_FuseData.SkipDevSelStraps & 0x1) << 3);
            MaskData |= (0x1 << 3);
        }

        if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_SW_RESERVED)
        {
            Data     |= ((s_FuseData.SwReserved & 0xF) << 4);
            MaskData |= (0xF <<  4);
        }

        SET_FUSE_BOTH_WITH_MASK(RESERVED_SW, 0, Data, MaskData);
    }
}


static void FuseCopyBytes(NvU32 RegAddress, NvU8 *pByte, const NvU32 nBytes)
{
    NvU32 RegData;
    NvU32 i;

     NV_ASSERT((pByte != NULL) || (nBytes == 0));
     NV_ASSERT (RegAddress != 0);

    for (i = 0, RegData = 0; i < nBytes; i++)
    {
        if ((i&3) == 0)
        {
            RegData = FUSE_NV_READ32(RegAddress);
            RegAddress += 4;
        }
        pByte[i] = RegData & 0xFF;
        RegData >>= 8;
    }
}

// Expose (Visibility = 1) or hide (Visibility = 0) the fuse registers.
static void SetFuseRegVisibility(NvU32 Visibility)
{
    NvU32 RegData;

    RegData = CLOCK_NV_READ32(CLK_RST_CONTROLLER_MISC_CLK_ENB_0);
    RegData = NV_FLD_SET_DRF_NUM(CLK_RST_CONTROLLER,
                                 MISC_CLK_ENB,
                                 CFG_ALL_VISIBLE,
                                 Visibility,
                                 RegData);
    CLOCK_NV_WRITE32(CLK_RST_CONTROLLER_MISC_CLK_ENB_0, RegData);
}

// Wait for completion (state machine goes idle).
static void WaitForFuseIdle(void)
{
    NvU32 RegData;

    do
    {
        RegData = FUSE_NV_READ32(FUSE_FUSECTRL_0);
    } while (NV_DRF_VAL(FUSE, FUSECTRL, FUSECTRL_STATE, RegData) !=
             FUSE_FUSECTRL_0_FUSECTRL_STATE_STATE_IDLE);
}


// Start the regulator and wait
static void StartRegulator(void)
{
    NvU32 RegData;
    RegData = NV_DRF_DEF(FUSE, PWR_GOOD_SW, PWR_GOOD_SW_VAL, PWR_GOOD_OK);
    FUSE_NV_WRITE32(FUSE_PWR_GOOD_SW_0, RegData);
    /*
     * Wait for at least 150ns. In HDEFUSE_64X32_E2F2R2_L1_A.pdf,
     * this is found on p.11 as TSUP_PWRGD.
     */
    NvFuseUtilWaitUS(1);
}

// Stop the regulator and wait
static void StopRegulator(void)
{
    NvU32 RegData;

    RegData = NV_DRF_DEF(FUSE, PWR_GOOD_SW, PWR_GOOD_SW_VAL, PWR_GOOD_NOT_OK);
    FUSE_NV_WRITE32(FUSE_PWR_GOOD_SW_0, RegData);

    /*
     * Wait for at least 150ns. In HDEFUSE_64X32_E2F2R2_L1_A.pdf,
     *  this is found on p.11 as TSUP_PWRGD
     */
    NvFuseUtilWaitUS(1);
}

 /*
  * Read a word from the fuses.
  * Note: The fuses must already have been sensed, and
  * the programming power should be off.
  */
static NvU32 ReadFuseWord(NvU32 Addr)
{
    NvU32 RegData;

    // Prepare the data
    FUSE_NV_WRITE32(FUSE_FUSEADDR_0, Addr);

    // Trigger the read
    RegData = FUSE_NV_READ32(FUSE_FUSECTRL_0);
    RegData = NV_FLD_SET_DRF_DEF(FUSE, FUSECTRL, FUSECTRL_CMD, READ, RegData);
    FUSE_NV_WRITE32(FUSE_FUSECTRL_0, RegData);

    // Wait for completion (state machine goes idle).
    WaitForFuseIdle();

    RegData = FUSE_NV_READ32(FUSE_FUSERDATA_0);

    return RegData;
}

/*
 * Write a word to the fuses.
 * Note: The programming power should be on, and the only non-zero
 * bits should be fuses that have not yet been blown.
 */
static void WriteFuseWord(NvU32 Addr, NvU32 Data)
{
    NvU32 RegData;

    if (Data == 0) return;

    // Prepare the data
    FUSE_NV_WRITE32(FUSE_FUSEADDR_0,  Addr);
    FUSE_NV_WRITE32(FUSE_FUSEWDATA_0, Data);

    // Trigger the write
    RegData = FUSE_NV_READ32(FUSE_FUSECTRL_0);
    RegData = NV_FLD_SET_DRF_DEF(FUSE, FUSECTRL, FUSECTRL_CMD, WRITE, RegData);
    FUSE_NV_WRITE32(FUSE_FUSECTRL_0, RegData);

    // Wait for completion (state machine goes idle).
    WaitForFuseIdle();
}

// Sense the fuse array & wait until done.
static void SenseFuseArray(void)
{
    NvU32 RegData;

    RegData = FUSE_NV_READ32(FUSE_FUSECTRL_0);
    RegData = NV_FLD_SET_DRF_DEF(FUSE,
                                 FUSECTRL,
                                 FUSECTRL_CMD,
                                 SENSE_CTRL,
                                 RegData);
    FUSE_NV_WRITE32(FUSE_FUSECTRL_0, RegData);

    // Wait for completion (state machine goes idle).
    WaitForFuseIdle();
}


// The following has not appeared in any netlist headers.
#ifndef FUSE_ENABLE_FUSE_PROGRAM__PRI_ALIAS_0
#define FUSE_ENABLE_FUSE_PROGRAM__PRI_ALIAS_0       0x0
#define FUSE_ENABLE_FUSE_PROGRAM__PRI_ALIAS_0_DATA  0:0
#define FUSE_ENABLE_FUSE_PROGRAM__PRI_ALIAS_0_WIDTH 1
#define FUSE_ENABLE_FUSE_PROGRAM__RED_ALIAS_0       0x1
#define FUSE_ENABLE_FUSE_PROGRAM__RED_ALIAS_0_DATA  0:0
#define FUSE_ENABLE_FUSE_PROGRAM__RED_ALIAS_0_WIDTH 1
#endif
#ifndef FUSE_ENABLE_FUSE_PROGRAM__PRI_ALIAS_0_SHIFT
#define FUSE_ENABLE_FUSE_PROGRAM__PRI_ALIAS_0_SHIFT 0
#define FUSE_ENABLE_FUSE_PROGRAM__RED_ALIAS_0_SHIFT 0
#endif

static void
NvDdkFuseProgramFuseArray(
                                NvU32 *FuseData,
                                NvU32 *MaskData,
                                NvU32  NumWords,
                                NvU32  TProgramCycles)
{
    NvU32  RegData;
    NvU32  i;
    NvU32  Addr = 0;
    NvU32 *Data = NULL;
    NvU32 *Mask = NULL;
    NvU32  FuseWord0; // Needed for initially enabling fuse programming.
    NvU32  FuseWord1; // Needed for initially enabling fuse programming.

    // Make all registers visible first
    SetFuseRegVisibility(1);
    // Read the first two fuse words.
    SenseFuseArray();
    FuseWord0 = ReadFuseWord(FUSE_ENABLE_FUSE_PROGRAM__PRI_ALIAS_0);
    FuseWord1 = ReadFuseWord(FUSE_ENABLE_FUSE_PROGRAM__RED_ALIAS_0);
    // also indicate power good to start the regulator and wait (again)
    StartRegulator();
    /*
     *In AP20, the fuse macro is a high density macro, with a completely
    * different interface from the procedure used in AP15.  Fuses are
    * burned using an addressing mechanism, so no need to prepare
    * the full list, but more write to control registers are needed, not
    * a big deal
    * The only bit that can be written at first is bit 0, a special write
    * protection bit by assumptions all other bits are at 0
    * Note that the bit definitions are no more part of arfuse.h
    * (unfortunately). So we define them here
    *
    * Modify the fuse programming time.
    *
    * For AP20, the programming pulse must have a precise width of
    * [9000, 11000] ns.
    */
    if (TProgramCycles > 0)
    {
        RegData = NV_DRF_NUM(FUSE,
                             FUSETIME_PGM2,
                             FUSETIME_PGM2_TWIDTH_PGM,
                             TProgramCycles);
        FUSE_NV_WRITE32(FUSE_FUSETIME_PGM2_0, RegData);
    }
    /* FuseWord0 and FuseWord1 should be left with only the Fuse Enable fuse
     * set to 1, and then only if this fuse has not yet been burned.
     */
    FuseWord0 = (0x1 << FUSE_ENABLE_FUSE_PROGRAM__PRI_ALIAS_0_SHIFT) &
      ~FuseWord0;
    FuseWord1 = (0x1 << FUSE_ENABLE_FUSE_PROGRAM__RED_ALIAS_0_SHIFT) &
      ~FuseWord1;

    WriteFuseWord(FUSE_ENABLE_FUSE_PROGRAM__PRI_ALIAS_0, FuseWord0);
    WriteFuseWord(FUSE_ENABLE_FUSE_PROGRAM__RED_ALIAS_0, FuseWord1);
    // Remove power good as we may toggle fuse_src in the model, wait
    StopRegulator();
    /*
     * Sense the fuse macro, this will allow programming of other fuses
     * and the reading of the existing fuse values
     */
    SenseFuseArray();
    /*
     * Clear out all bits in FuseData that have already been burned
     * or that have been masked out.
     */

    fusememcpy(s_TempFuseData, FuseData, sizeof(NvU32) * 64);
    Addr = 0;
    Data = s_TempFuseData;
    Mask = MaskData;
    for (i = 0; i < NumWords; i++, Addr++, Data++, Mask++)
    {
        RegData = ReadFuseWord(Addr);
        *Data = (*Data & ~RegData) & *Mask;
    }

    // Enable power good
    StartRegulator();
    /*
      * Finally loop on all fuses, program the non zero ones
     * Words 0 and 1 are written last and they contain control fuses and we
     * need a sense after writing to a control word (with the exception of
     * the master enable fuse) this is also the reason we write them last
     */
    Addr = 2;
    Data = s_TempFuseData + Addr;
    for (; Addr < NumWords; Addr++, Data++)
    {
        WriteFuseWord(Addr, *Data);
    }
    // write the two control words, we need a sense after each write
    Addr = 0;
    Data = s_TempFuseData;
    for (; Addr < NV_MIN(2, NumWords); Addr++, Data++)
    {
        WriteFuseWord(Addr, *Data);
        // Remove power good as we may toggle fuse_src in the model, wait
        StopRegulator();
        SenseFuseArray();
        StartRegulator();
    }
    // Read all data into the chip options
    RegData = NV_DRF_NUM(FUSE, PRIV2INTFC_START, PRIV2INTFC_START_DATA, 0x1);
    FUSE_NV_WRITE32(FUSE_PRIV2INTFC_START_0, RegData);
    /*
     * Not sure if still needs to be set back to 0
     * we wait a little bit, then set it back to 0, then loop on state
     */

    NvFuseUtilWaitUS(1);

    RegData = NV_DRF_NUM(FUSE, PRIV2INTFC_START, PRIV2INTFC_START_DATA, 0x0);
    FUSE_NV_WRITE32(FUSE_PRIV2INTFC_START_0, RegData);
    /*
      * Wait until done (polling)
      * this one needs to use fuse_sense done, the FSM follows a periodic
      * sequence that includes idle
      */
    do {
        RegData = FUSE_NV_READ32(FUSE_FUSECTRL_0);
  } while (NV_DRF_VAL(FUSE, FUSECTRL, FUSECTRL_FUSE_SENSE_DONE, RegData) != 0x1);
}

static NvError
NvDdkFuseVerifyFuseArray(
                                    NvU32 *FuseData,
                                    NvU32 *MaskData,
                                    NvU32 NumWords)
{
    NvU32        RegData;
    NvU32        i;
    NvU32        Addr = 0;
    NvU32       *Data = NULL;
    NvU32       *Mask = NULL;
    NvError  e   = NvSuccess;

    // Make all registers visible first
    SetFuseRegVisibility(1);

    // Sense the fuse array
    SenseFuseArray();

    // Loop over the data, checking fuse registers.
    Addr = 0;
    Data = FuseData;
    Mask = MaskData;
    for (i = 0; i < NumWords; i++, Addr++, Data++, Mask++)
    {
        RegData = (ReadFuseWord(Addr) & (*Mask));

        /*
          * Once Odm production mode fuse word(0th word) is set, it can
         * not set any other fuse word  in the fuse cell, including the
         * redundent fuse word (1st word).
         * Hence it makes no sense to verify it.
         */
        if ((i == 1) && NvDdkFuseIsOdmProductionMode())
        {
            continue;
        }

        // Check the results.
        if (RegData != ((*Data) & (*Mask)))
        {
            // Abort with an error on a miscompare
            e = NvError_BadValue;
            goto fail;
        }
    }

    // Fallthrough on sucessful completion.

 fail:
    // Hide back registers
    SetFuseRegVisibility(0);
    return e;
}

/*
 *************************************
 * PUBLIC API
 *************************************
 */

/**
 * Clears the cache of fuse data.
 */
void NvDdkFuseClear(void)
{
    if(!s_pFuseRec)
    {
        // NV_ASSERT(0);
        NvOsDebugPrintf("\r\n Plz call NvDdkFuseOpen before using this API");
        return;
    }
    NvOsMutexLock(s_pFuseRec->Mutex);
    fusememset(&s_FuseData, 0, sizeof(NvDdkFuseData));
    NvOsMutexUnlock(s_pFuseRec->Mutex);
}

/**
 * Read the current fuse data into the fuse registers.
 */
void NvDdkFuseSense(void)
{
    if(!s_pFuseRec)
    {
        // NV_ASSERT(0);
        NvOsDebugPrintf("\r\n Plz call NvDdkFuseOpen before using this API");
        return;
    }
    NvOsMutexLock(s_pFuseRec->Mutex);
    SetFuseRegVisibility(1);
    SenseFuseArray();
    SetFuseRegVisibility(0);
    NvOsMutexUnlock(s_pFuseRec->Mutex);
}

/**
 * Macro's to swap byte order for an NvU32
 */

#define EXTRACT_BYTE_NVU32(Data32, ByteNum) \
    (((Data32) >> ((ByteNum)*8)) & 0xFF)

#define SWAP_BYTES_NVU32(Data32)                    \
    do {                                            \
         NV_ASSERT(sizeof(Data32)==4);               \
        (Data32) =                                  \
            (EXTRACT_BYTE_NVU32(Data32, 0) << 24) | \
            (EXTRACT_BYTE_NVU32(Data32, 1) << 16) | \
            (EXTRACT_BYTE_NVU32(Data32, 2) <<  8) | \
            (EXTRACT_BYTE_NVU32(Data32, 3) <<  0);  \
    } while (0)

#if FUSE_DUMMY_CODE
// Sizes of different fuses in bytes.
static NvU8 DeviceKey[4];
static NvU8 JtagDisable[1];
static NvU8 KeyProgrammed[1];
static NvU8 OdmProduction[1];
static NvU8 SecBootDeviceConfig[2];
static NvU8 SecBootDeviceSelect[1];
static NvU8 SecureBootKey[16];
static NvU8 Sku[4];
static NvU8 SpareBits[4];
static NvU8 SwReserved[1];
static NvU8 SkipDevSelStraps[1];
static NvU8 SecBootDeviceSelectRaw[4];
static NvU8 ReservedOdm[32];

NvError NvDdkFuseGet(NvDdkFuseDataType Type, void *pData, NvU32 *pSize)
{
    switch (Type)
    {
        case NvDdkFuseDataType_DeviceKey:
            fusememcpy(pData, DeviceKey, sizeof(DeviceKey));
            break;
        case NvDdkFuseDataType_JtagDisable:
            fusememcpy(pData, JtagDisable, sizeof(JtagDisable));
            break;
        case NvDdkFuseDataType_KeyProgrammed:
            fusememcpy(pData, KeyProgrammed, sizeof(KeyProgrammed));
            break;
        case NvDdkFuseDataType_OdmProduction:
            fusememcpy(pData, OdmProduction, sizeof(OdmProduction));
            break;
        case NvDdkFuseDataType_SecBootDeviceConfig:
            fusememcpy(pData, SecBootDeviceConfig, sizeof(SecBootDeviceConfig));
            break;
        case NvDdkFuseDataType_SecBootDeviceSelect:
            fusememcpy(pData, SecBootDeviceSelect, sizeof(SecBootDeviceSelect));
            break;
        case NvDdkFuseDataType_SecureBootKey:
            fusememcpy(pData, SecureBootKey, sizeof(SecureBootKey));
            break;
        case NvDdkFuseDataType_Sku:
            fusememcpy(pData, Sku, sizeof(Sku));
            break;
        case NvDdkFuseDataType_SpareBits:
            fusememcpy(pData, SpareBits, sizeof(SpareBits));
            break;
        case NvDdkFuseDataType_SwReserved:
            fusememcpy(pData, SwReserved, sizeof(SwReserved));
            break;
        case NvDdkFuseDataType_SkipDevSelStraps:
            fusememcpy(pData, SkipDevSelStraps, sizeof(SkipDevSelStraps));
            break;
        case NvDdkFuseDataType_SecBootDeviceSelectRaw:
            fusememcpy(pData, SecBootDeviceSelectRaw, sizeof(SecBootDeviceSelectRaw));
            break;
        case NvDdkFuseDataType_ReservedOdm:
            fusememcpy(pData, ReservedOdm, sizeof(ReservedOdm));
            break;
        default:
            NvOsDebugPrintf("\r\n Invalid fuse type selected");
            break;
    }
    return NvSuccess;
}
#else
/**
 * This gets value from the fuse cache.
 *
 * To read from the actual fuses, NvDdkFuseSense() must be called first.
 * Note that NvDdkFuseSet() follwed by NvDdkFuseGet() for the same data will
 * return the set data, not the actual fuse values.
 *
 * By passing a size of zero, the caller is requesting tfor the
 * expected size.
 */
NvError NvDdkFuseGet(NvDdkFuseDataType Type, void *pData, NvU32 *pSize)
{
    NvU32 RegData;
    NvU32 Size;
    NvU32 DataSizeArrayLen;

    if(!s_pFuseRec)
    {
        NvOsDebugPrintf("\r\n Plz call NvDdkFuseOpen before using this API");
        return 1;
    }
    DataSizeArrayLen = ((sizeof(s_DataSize)) / (sizeof(NvU32)));
    // Check the arguments
    // NvDdkFuseDataType_Num is not ap20 specific as s_DataSize
    if (Type == NvDdkFuseDataType_None ||
        (Type > (NvDdkFuseDataType)DataSizeArrayLen))
            return NvError_BadValue;
    if (pSize == NULL) return NvError_BadParameter;

    Size = s_DataSize[(NvU32)Type];

    if (*pSize == 0)
    {
        *pSize = Size;
        return NvError_Success;
    }

//    if (*pSize > Size) return NvError_InsufficientMemory;
    if (pData == NULL) return NvError_BadParameter;

    NvOsMutexLock(s_pFuseRec->Mutex);
    switch (Type)
    {
        case NvDdkFuseDataType_DeviceKey:
            /*
             * Boot ROM expects DK to be stored in big-endian byte order;
             * since cpu is little-endian and client treats data as an NvU32,
             * perform byte swapping here
             */
            RegData = FUSE_NV_READ32(FUSE_PRIVATE_KEY4_0);
            SWAP_BYTES_NVU32(RegData);
            *((NvU32*)pData) = RegData;
            break;

        case NvDdkFuseDataType_JtagDisable:
            RegData = FUSE_NV_READ32(FUSE_ARM_DEBUG_DIS_0);
            *((NvBool*)pData) = RegData ? NV_TRUE : NV_FALSE;
            break;

        case NvDdkFuseDataType_KeyProgrammed:
            *((NvBool*)pData) = IsSbkOrDkSet();
            break;

        case NvDdkFuseDataType_OdmProduction:
            *((NvBool*)pData) = NvDdkFuseIsOdmProductionModeFuseSet();
            break;

        case NvDdkFuseDataType_SecBootDeviceConfig:
            RegData = FUSE_NV_READ32(FUSE_BOOT_DEVICE_INFO_0);
            RegData = NV_DRF_VAL(FUSE,
                                 BOOT_DEVICE_INFO,
                                 BOOT_DEVICE_CONFIG,
                                 RegData);

            *((NvU32*)pData) = RegData;
            break;

        case NvDdkFuseDataType_SecBootDeviceSelect:
            RegData = (NvU32)NvDdkFuseGetSecBootDevice();
            *((NvU32*)pData) = RegData;
            break;

        case NvDdkFuseDataType_SecBootDeviceSelectRaw:
            RegData = NvDdkFuseGetSecBootDeviceRaw();
            *((NvU32*)pData) = RegData;
            break;

        case NvDdkFuseDataType_SecureBootKey:
            FuseCopyBytes(FUSE_PRIVATE_KEY0_0,
                          pData,
                          NVDDK_SECURE_BOOT_KEY_BYTES);
            break;

        case NvDdkFuseDataType_Sku:
            *((NvU32*)pData) = FUSE_NV_READ32(FUSE_SKU_INFO_0);
            break;

        case NvDdkFuseDataType_SwReserved:
            RegData = FUSE_NV_READ32(FUSE_RESERVED_SW_0);
            RegData = NV_DRF_VAL(FUSE, RESERVED_SW, SW_RESERVED, RegData);
            *((NvU32*)pData) = RegData;
            break;

        case NvDdkFuseDataType_SkipDevSelStraps:
            RegData = FUSE_NV_READ32(FUSE_RESERVED_SW_0);
            RegData = NV_DRF_VAL(FUSE, RESERVED_SW, SKIP_DEV_SEL_STRAPS, RegData);
            *((NvU32*)pData) = RegData;
            break;

        case NvDdkFuseDataType_ReservedOdm:
            FuseCopyBytes(FUSE_RESERVED_ODM0_0,
                          pData,
                          NVDDK_RESERVED_ODM_BYTES);
            break;

        default:
            NvOsMutexUnlock(s_pFuseRec->Mutex);
            return(NvError_BadValue);
    }
    NvOsMutexUnlock(s_pFuseRec->Mutex);
    return NvError_Success;
}
#endif

#if ENABLE_FUSE_VOLTAGE
static NvRmDeviceHandle *ps_hRmDevice = NULL;
#endif
NvError NvDdkFuseOpen(NvRmDeviceHandle hRmDevice)
{
    NvError e;

    // Allocate memory for handle.
    s_pFuseRec = NvOsAlloc(sizeof(NvDdkFuse));
    if (s_pFuseRec == NULL)
    {
        e = NvError_InsufficientMemory;
        NvOsDebugPrintf("\r\n NvDdkFuseOpen malloc failed");
        return e;
    }

    e = NvOsMutexCreate(&s_pFuseRec->Mutex);
    if (e)
    {
        NvOsDebugPrintf("\r\n NvDdkFuseOpen Mutex creation failed");
        goto fail;
    }

#if ENABLE_FUSE_VOLTAGE
    ps_hRmDevice = NvOsAlloc(sizeof(NvRmDeviceHandle));
    if (ps_hRmDevice == NULL)
    {
        e = NvError_InsufficientMemory;
        NvOsDebugPrintf("\r\n NvDdkFuseOpen malloc failed");
        return e;
    }

    *ps_hRmDevice = hRmDevice;
#endif
    // Get the base address of the Fuse registers
    NvRmModuleGetBaseAddress(hRmDevice,
        NVRM_MODULE_ID(NvRmModuleID_Fuse, 0),
        &(s_pFuseRec->pBaseAddress), &(s_pFuseRec->BankSize));
    NV_CHECK_ERROR_CLEANUP(NvRmPhysicalMemMap(s_pFuseRec->pBaseAddress,
        s_pFuseRec->BankSize, NVOS_MEM_READ_WRITE, NvOsMemAttribute_Uncached,
        (void **)&(s_pFuseRec->pVirtualAddress)));

    // Get the base address of the CAR registers
    NvRmModuleGetBaseAddress(hRmDevice,
        NVRM_MODULE_ID(NvRmPrivModuleID_ClockAndReset, 0),
        &(s_pFuseRec->pCarBaseAddress), &(s_pFuseRec->CarBankSize));
    NV_CHECK_ERROR_CLEANUP(NvRmPhysicalMemMap(s_pFuseRec->pCarBaseAddress,
        s_pFuseRec->CarBankSize, NVOS_MEM_READ_WRITE, NvOsMemAttribute_Uncached,
        (void **)&(s_pFuseRec->pCarVirtualAddress)));

    return NvSuccess;
fail:
    NvOsFree(s_pFuseRec);
    return e;
}

void NvDdkFuseClose()
{
    NvOsDebugPrintf("\r\n NvDdkFuseClose ");
    NvOsMutexDestroy(s_pFuseRec->Mutex);
    NvOsFree(s_pFuseRec);
    s_pFuseRec = NULL;
}

#if FUSE_DUMMY_CODE
NvError NvDdkFuseSet(NvDdkFuseDataType Type, void *pData, NvU32 *pSize)
{
#if ENABLE_DEBUG_PRINTS
    NvU8 i;
    NvOsDebugPrintf("\n data to be written:");
    for(i = 0; i < *pSize; i++)
        NvOsDebugPrintf("\t 0x%x", *((NvU8 *)pData + i));
#endif
    switch (Type)
    {
        case NvDdkFuseDataType_DeviceKey:
            fusememcpy(DeviceKey, pData, sizeof(DeviceKey));
            break;
        case NvDdkFuseDataType_JtagDisable:
            fusememcpy(JtagDisable, pData, sizeof(JtagDisable));
            break;
        case NvDdkFuseDataType_KeyProgrammed:
            fusememcpy(KeyProgrammed, pData, sizeof(KeyProgrammed));
            break;
        case NvDdkFuseDataType_OdmProduction:
            fusememcpy(OdmProduction, pData, sizeof(OdmProduction));
            break;
        case NvDdkFuseDataType_SecBootDeviceConfig:
            fusememcpy(SecBootDeviceConfig, pData, sizeof(SecBootDeviceConfig));
            break;
        case NvDdkFuseDataType_SecBootDeviceSelect:
            fusememcpy(SecBootDeviceSelect, pData, sizeof(SecBootDeviceSelect));
            break;
        case NvDdkFuseDataType_SecureBootKey:
            fusememcpy(SecureBootKey, pData, sizeof(SecureBootKey));
            break;
        case NvDdkFuseDataType_Sku:
            fusememcpy(Sku, pData, sizeof(Sku));
            break;
        case NvDdkFuseDataType_SpareBits:
            fusememcpy(SpareBits, pData, sizeof(SpareBits));
            break;
        case NvDdkFuseDataType_SwReserved:
            fusememcpy(SwReserved, pData, sizeof(SwReserved));
            break;
        case NvDdkFuseDataType_SkipDevSelStraps:
            fusememcpy(SkipDevSelStraps, pData, sizeof(SkipDevSelStraps));
            break;
        case NvDdkFuseDataType_SecBootDeviceSelectRaw:
            fusememcpy(SecBootDeviceSelectRaw, pData, sizeof(SecBootDeviceSelectRaw));
            break;
        case NvDdkFuseDataType_ReservedOdm:
            fusememcpy(ReservedOdm, pData, sizeof(ReservedOdm));
            break;
        default:
            NvOsDebugPrintf("\r\n Invalid fuse type selected");
            break;
    }
    return NvSuccess;
}

#else

/**
 * Schedule fuses to be programmed to the specified values when the next
 * NvDdkFuseProgram() operation is performed
 *
 * By passing a size of zero, the caller is requesting to be told the
 * expected size.
 */
#define FUSE_SET(name, flag)                                              \
    case NvDdkFuseDataType_##name:                                         \
    {                                                                     \
        NvU8 *p = (NvU8 *)&(p_FuseData.name);                           \
        NvU32 i;                                                          \
        NvError e = NvSuccess;  \
        /* read existing fuse value */                                    \
        e = NvDdkFuseGet(NvDdkFuseDataType_##name, p, &Size);   \
        if (e != NvSuccess) \
        {\
            NvOsDebugPrintf("\r\n Err returned from Fuse Get:0x%x in Set",e); \
            return e; \
        }\
        /* check consistency between existing and desired fuse values. */ \
        /* fuses cannot be unburned, so desired value cannot specify   */ \
        /* any unburned (0x0) bits where the existing value already    */ \
        /* contains burned (0x1) bits.                                 */ \
        for (i=0; i<Size; i++)                                            \
            if ((p[i] | pDataPtr[i]) != pDataPtr[i])          \
            { \
                    e = NvError_InvalidState;                     \
                    NvOsDebugPrintf("\n p[%d] = 0x%x, pDataptr[%d] = 0x%x",i, p[i],i,*(NvU32*)pDataPtr); \
                    NvOsDebugPrintf("\r\n Consistency check failure in Fuse Set:0x%x",e); \
                    return e; \
            } \
        /* consistency check passed; schedule fuses to be burned */       \
        fusememcpy(&(s_FuseData.name), (void *)pDataPtr, Size);                    \
        s_FuseData.ProgramFlags |= FLAGS_PROGRAM_##flag;                  \
    }                                                                     \
    break

NvError NvDdkFuseSet(NvDdkFuseDataType Type, void *pData, NvU32 *pSize)
{
    NvError e = NvError_Success;
    NvU32 Size;
    NvU32 DataSizeArrayLen;
    NvDdkFuseData p_FuseData;
    volatile NvU8* pDataPtr = (volatile NvU8*)pData;
    if(!s_pFuseRec)
    {
        // NV_ASSERT(0);
        NvOsDebugPrintf("\r\n Plz call NvDdkFuseOpen before using this API");
        return 1;
    }
    NvOsMutexLock(s_pFuseRec->Mutex);
    DataSizeArrayLen = ((sizeof(s_DataSize)) / (sizeof(NvU32)));

    if (Type == NvDdkFuseDataType_None ||
        (Type > (NvDdkFuseDataType)DataSizeArrayLen))
   {
        e = NvError_BadValue;
        NvOsDebugPrintf("\r\n NvDdkFuseDataType is incorrect");
        goto fail;
    }

    if (pSize == NULL)
   {
        e = NvError_BadParameter;
        goto fail;
    }

    Size = s_DataSize[(NvU32)Type];

    // Return the required size, if requested.
    if (*pSize == 0)
    {
        *pSize = Size;
        e = NvError_Success;
        goto fail;
    }

    if (*pSize > Size)
   {
        NvOsDebugPrintf("\n passed size = %d, fuse size = %d", *pSize, Size);
        e = NvError_InsufficientMemory;
        goto fail;
    }

    if (pData == NULL)
   {
        e = NvError_BadParameter;
        goto fail;
    }

    /*
     * If Disable Reg program is set, chip can not be fused, so return as
     *  "Access denied".
     */
    if (NvDdkPrivIsDisableRegProgramSet())
    {
        e = NvError_AccessDenied;
        goto fail;
    }


    // Only reserved odm fuses are allowed to burn in secure mode
    if (NvDdkFuseIsOdmProductionModeFuseSet() &&
                    (Type != NvDdkFuseDataType_ReservedOdm))
    {
        e = NvError_BadValue;
        NvOsDebugPrintf("\r\n only reserved odm fuses are allowed to burn \
         in secure mode");
        goto fail;
    }

    switch (Type)
    {
        FUSE_SET(DeviceKey,                 DEVICE_KEY              );
        FUSE_SET(JtagDisable,                JTAG_DISABLE           );
        FUSE_SET(OdmProduction,          ODM_PRODUCTION     );
        FUSE_SET(SecBootDeviceConfig, BOOT_DEV_CONFIG    );

        case NvDdkFuseDataType_SecBootDeviceSelect:
        case NvDdkFuseDataType_SecBootDeviceSelectRaw:
            {
                NvU8 *p = (NvU8 *)&(s_FuseData.SecBootDeviceSelectRaw);
                NvU32 ApiData;  // API symbolic value for device selection
                NvU32 FuseData; // Raw fuse data for the device selection

                if (Type == NvDdkFuseDataType_SecBootDeviceSelect)
                {
                    /* Read out the argument. */

                    fusememcpy(&(ApiData), pData, Size);

                    if (ApiData == 0 || ApiData >= NvDdkSecBootDeviceType_Max)
                    {
                        e = NvError_BadValue;
                        NvOsDebugPrintf("\r\n ApiData is incorrect");
                        goto fail;
                    }

                    /* Map the symbolic value to a fuse value. */
                    FuseData = NvDdkToFuseBootDeviceSel[ApiData];
                }
                else
                {
                    /*
                     * Type == NvDdkFuseDataType_SecBootDeviceSelectRaw
                     * Read out the argument.
                     */
                    fusememcpy(&(FuseData), pData, Size);
                    if (FuseData >= FuseBootDev_Max)
                    {
                        e = NvError_BadValue;
                        NvOsDebugPrintf("\r\n FuseData is incorrect");
                        goto fail;
                    }

                    /* Map the fuse value to a symbolic value */
                    ApiData = FuseToNvDdkBootDeviceSel[FuseData];
                }

                /* read existing fuse value */
                NV_CHECK_ERROR(NvDdkFuseGet(NvDdkFuseDataType_SecBootDeviceSelectRaw,
                                           p, &Size));

                /*
                 * Check consistency between existing and desired fuse values.
                 * fuses cannot be unburned, so desired value cannot specify
                 * any unburned (0x0) bits where the existing value already
                 * contains burned (0x1) bits.
                 */
                if ((s_FuseData.SecBootDeviceSelectRaw | FuseData) != FuseData)
                {
                    e = NvError_InvalidState;
                    goto fail;
                }


                /* Consistency check passed; schedule fuses to be burned */
                s_FuseData.SecBootDeviceSelect    = ApiData;
                s_FuseData.SecBootDeviceSelectRaw = FuseData;
                s_FuseData.ProgramFlags |= FLAGS_PROGRAM_BOOT_DEV_SEL;
            }
            break;

        FUSE_SET(SecureBootKey,      SECURE_BOOT_KEY       );
        FUSE_SET(SwReserved,          SW_RESERVED               );
        FUSE_SET(SkipDevSelStraps,  SKIP_DEV_SEL_STRAPS  );
        FUSE_SET(ReservedOdm,        RESERVED_ODM             );

        default:
        {
            e = NvError_BadValue;
            NvOsDebugPrintf("\r\n Entered default case");
            goto fail;
        }
    }
    // Set the ReservOdm flag if it present
    if (Type == NvDdkFuseDataType_ReservedOdm)
    {
        s_FuseData.ReservedOdmFlag = NV_TRUE;
    }

    // Check for invalid state combinations.
    if ((s_FuseData.ProgramFlags & FLAGS_PROGRAM_ODM_PRODUCTION) &&
        (s_FuseData.ProgramFlags & (FLAGS_PROGRAM_SECURE_BOOT_KEY |
                                    FLAGS_PROGRAM_DEVICE_KEY)))
    {
        s_FuseData.ProgramFlags |= FLAGS_PROGRAM_ILLEGAL;
    }

    if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_ILLEGAL)
    {
        e = NvError_InvalidState;
        goto fail;
    }

fail:
    NvOsMutexUnlock(s_pFuseRec->Mutex);
    if (e != NvSuccess)
        NvOsDebugPrintf("\r\n NvDdkFuseSet exit with err value e = 0x%x",e);
    return e;
}
#endif

#if FUSE_DUMMY_CODE
NvError NvDdkFuseProgram(void)
{

}
#else
#if ENABLE_FUSE_VOLTAGE
static void EnableFuseVoltage(void)
{
    const NvOdmPeripheralConnectivity *pConnectivity = NULL;
    NvRmPmuVddRailCapabilities RailCaps;
    NvU32 i;

    pConnectivity = NvOdmPeripheralGetGuid(NV_VDD_FUSE_ODM_ID);
    if (pConnectivity != NULL)
    {
        for (i = 0; i < pConnectivity->NumAddress; i++)
        {
            // Search for the vdd rail entry
            if (pConnectivity->AddressList[i].Interface == NvOdmIoModule_Vdd)
            {
                NvRmPmuGetCapabilities((*ps_hRmDevice),
                            pConnectivity->AddressList[i].Address, &RailCaps);

                NvRmPmuSetVoltage((*ps_hRmDevice),
                            pConnectivity->AddressList[i].Address,
                            RailCaps.requestMilliVolts, NULL);
            }
        }
    }
}
#endif
/**
 * Program all fuses based on cache data changed via the NvDdkFuseSet() API.
 *
 * Caller is responsible for supplying valid fuse programming voltage prior to
 * invoking this routine.
 *
 * NOTE: All values that are not intended to be programmed must have
 *       value 0. There is no need to conditionalize this code based on which
 *       logical values were actually set through the API.
 */
NvError NvDdkFuseProgram(void)
{
    if(!s_pFuseRec)
    {
        NvOsDebugPrintf("\r\n Plz call NvDdkFuseOpen before using this API");
        return 1;
    }
#if ENABLE_FUSE_VOLTAGE
    EnableFuseVoltage();
#endif
    NvOsMutexLock(s_pFuseRec->Mutex);
    /*
     * Validate the current state.
     * If Secure mode is already burned, only reserved odm fuses are allowed to burn
     */
    if (NvDdkFuseIsOdmProductionModeFuseSet() && (!s_FuseData.ReservedOdmFlag))
    {
        NvOsDebugPrintf("\r\n NvDdkFuseProgram err - InvalidState");
        return NvError_InvalidState;
    }
    /*
     * If Disable Reg program is set, chip can not be fused, so return as
     *  "Access denied".
     */
    if (NvDdkPrivIsDisableRegProgramSet())
    {
        NvOsDebugPrintf("\r\n NvDdkFuseProgram err - AccessDenied");
        return NvError_AccessDenied;
    }

    if (s_FuseData.ProgramFlags & FLAGS_PROGRAM_ILLEGAL)
    {
        NvOsDebugPrintf("\r\n NvDdkFuseProgram err - InvalidState");
        return NvError_InvalidState;
    }

    // Map data onto a fuse array.
    MapDataToFuseArrays();
    NvDdkFuseProgramFuseArray(s_FuseArray, s_MaskArray, FUSE_WORDS,
                               NVDDK_TPROGRAM_VALUE_CORRECTED);
    NvOsMutexUnlock(s_pFuseRec->Mutex);
    // Clear reserved odm flag
    s_FuseData.ReservedOdmFlag = NV_FALSE;
    return NvError_Success;
}
#endif

#if FUSE_DUMMY_CODE
NvError NvDdkFuseVerify(void)
{}
#else
/**
 * Verify all fuses scheduled via the NvDdkFuseSet*() API's
 */

NvError NvDdkFuseVerify(void)
{
    NvError e = NvError_Success;
    if(!s_pFuseRec)
    {
        NvOsDebugPrintf("\r\n Plz call NvDdkFuseOpen before using this API");
        return 1;
    }
    NvOsMutexLock(s_pFuseRec->Mutex);
    // Map data onto a fuse array.
    MapDataToFuseArrays();
    // Check to see if the data matches the real fuses.
    e = NvDdkFuseVerifyFuseArray(s_FuseArray, s_MaskArray, FUSE_WORDS);
    NvOsMutexUnlock(s_pFuseRec->Mutex);
    if (e != NvError_Success)
    {
        e = NvError_InvalidState;
        NvOsDebugPrintf("\r\n NvDdkFuseVerify returning modified err:0x%x",e);
    }
    return e;
}
#endif
/*
 * NvDdkDisableFuseProgram API disables the fuse programming until the next
 * next system reset.
 */

void NvDdkDisableFuseProgram(void)
{
    NvU32 RegData;
    if(!s_pFuseRec)
    {
        // NV_ASSERT(0);
        NvOsDebugPrintf("\r\n Plz call NvDdkFuseOpen before using this API");
        return;
    }
    NvOsMutexLock(s_pFuseRec->Mutex);
    RegData = NV_DRF_DEF(FUSE, DISABLEREGPROGRAM, DISABLEREGPROGRAM_VAL, ENABLE);
    FUSE_NV_WRITE32(FUSE_DISABLEREGPROGRAM_0, RegData);
    NvOsMutexUnlock(s_pFuseRec->Mutex);
}

/** @} */