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
|
/*
*
*
*/
#include "cy8cmbr_swd.h"
/*****************************************************************************
*
*****************************************************************************/
struct swd_data {
struct touchkey_i2c *swd_tkey_i2c;
struct device *dev;
int (*power) (void *, bool on);
int swdio_gpio;
int swdck_gpio;
const u8 *fw_img;
size_t fw_img_size;
};
static struct swd_data _swd_data;
static struct swd_data *g_sd = NULL;
/*****************************************************************************
* SWD
*****************************************************************************/
/* Macros for SWD Physical layer functions */
#define swd_set_swdck_out(sd) gpio_direction_output(sd->swdck_gpio, 0)
#define swd_set_swdck_hi(sd) gpio_set_value(sd->swdck_gpio, 1)
#define swd_set_swdck_lo(sd) gpio_set_value(sd->swdck_gpio, 0)
#define swd_set_swdio_out(sd) gpio_direction_output(sd->swdio_gpio, 0)
#define swd_set_swdio_in(sd) gpio_direction_input(sd->swdio_gpio)
#define swd_set_swdio_hi(sd) gpio_set_value(sd->swdio_gpio, 1)
#define swd_set_swdio_lo(sd) gpio_set_value(sd->swdio_gpio, 0)
#define swd_get_swdio(sd) gpio_get_value(sd->swdio_gpio)
/*****************************************************************************
* AN84858
*
*
*
*
*****************************************************************************/
#include "SWD_UpperPacketLayer.h"
#include "SWD_PacketLayer.h"
//#define HEX_FIRMWARE
#define FORCE_PROTECT
#define CY8C40xx_FAMILY 1
/******************************************************************************
* Timeout
******************************************************************************/
#define DEVICE_ACQUIRE_TIMEOUT_US 50000
#define SROM_POLLING_TIMEOUT_MS 1000
/******************************************************************************
* Hex
******************************************************************************/
/* The below definitions are not dependent on hex file and are always
constant */
#if (CY8C40xx_FAMILY)
#define BYTES_PER_FLASH_ROW 64
#else
#define BYTES_PER_FLASH_ROW 128
#endif
#define SILICON_ID_BYTE_LENGTH 4
#define CHECKSUM_BYTE_LENGTH 2
#define MAXIMUM_ROW_PROTECTION_BYTE_LENGTH 32
#ifndef HEX_FIRMWARE
#define NUMBER_OF_FLASH_ROWS_HEX_FILE 128
#endif
#ifdef HEX_FIRMWARE
#include "HexImage.h"
static void HEX_ReadSiliconId(u32 * hexSiliconId)
{
u8 i;
for (i = 0; i < SILICON_ID_BYTE_LENGTH; i++) {
*hexSiliconId =
*hexSiliconId | ((u32) (deviceSiliconId_HexFile[i]) <<
(8 * i));
}
}
static void HEX_ReadRowData(u16 rowCount, u8 * rowData)
{
u16 i; /* Maximum value of 'i' can be 256 */
for (i = 0; i < BYTES_PER_FLASH_ROW; i++) {
rowData[i] = flashData_HexFile[rowCount][i];
}
}
static void HEX_ReadChipProtectionData(u8 * chipProtectionData)
{
*chipProtectionData = chipProtectionData_HexFile;
}
static void HEX_ReadRowProtectionData(u8 rowProtectionByteSize,
u8 * rowProtectionData)
{
u16 i;
for (i = 0; i < rowProtectionByteSize; i++) {
rowProtectionData[i] = flashProtectionData_HexFile[i];
}
}
static void HEX_ReadChecksumData(u16 * checksumData)
{
u8 i;
for (i = 0; i < CHECKSUM_BYTE_LENGTH; i++) {
*checksumData |= (checksumData_HexFile[i] << (8 * i));
}
}
#endif //HEX_FIRMWARE
static u16 GetFlashRowCount(void)
{
return (NUMBER_OF_FLASH_ROWS_HEX_FILE);
}
/******************************************************************************
* Programming Steps
******************************************************************************/
/* Return value definitions for high level Programming functions */
#define FAILURE 0
#define SUCCESS (!FAILURE)
/* Return value of PollSromStatus function */
#define SROM_SUCCESS 0x01
/* Error codes returned by Swd_PacketAck whenever any top-level step returns a
failure status */
/* This bit field is set if programmer fails to acquire the device in 1.5 ms */
#define PORT_ACQUIRE_TIMEOUT_ERROR 0x10
/* This bit field is set if the SROM does not return the success status code
within the SROM Polling timeout duration*/
#define SROM_TIMEOUT_ERROR 0x20
/* This bit field is set in case of JTAG ID mismatch or Flash data verification
mismatch or Checksum data mismatch */
#define VERIFICATION_ERROR 0x40
/* This bit field is set if wrong transition of chip protection settings is
detected */
#define TRANSITION_ERROR 0x80
/* Constants for Address Space of CPU */
#if (CY8C40xx_FAMILY)
#define CPUSS_SYSREQ 0x40100004
#define CPUSS_SYSARG 0x40100008
#else
#define CPUSS_SYSREQ 0x40000004
#define CPUSS_SYSARG 0x40000008
#endif
#define TEST_MODE 0x40030014
#define SRAM_PARAMS_BASE 0x20000100
#define SFLASH_MACRO 0x0FFFF000
#define SFLASH_CPUSS_PROTECTION 0x0FFFF07C
/* SROM Constants */
#define SROM_KEY1 0xB6
#define SROM_KEY2 0xD3
#define SROM_SYSREQ_BIT 0x80000000
#define SROM_PRIVILEGED_BIT 0x10000000
#define SROM_STATUS_SUCCEEDED 0xA0000000
#define SROM_STATUS_FAILED 0xF0000000
/* SROM Commands */
#define SROM_CMD_GET_SILICON_ID 0x00
#define SROM_CMD_LOAD_LATCH 0x04
#define SROM_CMD_PROGRAM_ROW 0x06
#define SROM_CMD_ERASE_ALL 0x0A
#define SROM_CMD_CHECKSUM 0x0B
#define SROM_CMD_WRITE_PROTECTION 0x0D
#if (CY8C40xx_FAMILY)
#define SROM_CMD_SET_IMO_48MHZ 0x15
#endif
/* Chip Level Protection */
#define CHIP_PROT_VIRGIN 0x00
#define CHIP_PROT_OPEN 0x01
#define CHIP_PROT_PROTECTED 0x02
#define CHIP_PROT_KILL 0x04
/* Mask for status code returned by polling SROM */
#define SROM_STATUS_SUCCESS_MASK 0xF0000000
/* Constant for computing checksum of entire flash */
#define CHECKSUM_ENTIRE_FLASH 0x8000
/* Constant DAP ID of ARM Cortex M0 CPU */
#define CM0_DAP_ID 0x0BB11477
/* Constant maximum number of rows in PSoC 4 */
#define ROWS_PER_ARRAY 256
static u32 checksum_Privileged = 0;
static u32 statusCode = 0;
static u8 result = 0;
#if defined(HEX_FIRMWARE) || defined(FORCE_PROTECT)
static u8 chipProtectionData_Chip = 0;
#endif
static u8 randompara = 0;
static enum Transition_mode { OPEN_XXX, VIRGIN_OPEN, PROT_XXX,
WRONG_TRANSITION } flow;
#if defined(FORCE_PROTECT)
static void HEX_ReadChipProtectionData(u8 * chipProtectionData)
{
*chipProtectionData = CHIP_PROT_OPEN;
}
static void HEX_ReadRowProtectionData(u8 rowProtectionByteSize, u8 * rowProtectionData)
{
u16 i;
for(i = 0; i < rowProtectionByteSize; i++)
rowProtectionData[i] = 0xFF;
}
#endif
static u8 PollSromStatus(void)
{
unsigned long end_time;
end_time = jiffies + msecs_to_jiffies(SROM_POLLING_TIMEOUT_MS);
do {
/* Read CPUSS_SYSREQ register and check if SROM_SYSREQ_BIT and
SROM_PRIVILEGED_BIT are reset to 0 */
Read_IO(CPUSS_SYSREQ, &statusCode);
statusCode &= (SROM_SYSREQ_BIT | SROM_PRIVILEGED_BIT);
} while ((statusCode != 0) && time_before_eq(jiffies, end_time));
randompara = 10;
/* If time exceeds the timeout value, set the SROM_TIMEOUT_ERROR bit in
swd_PacketAck */
if (!time_before_eq(jiffies, end_time)) {
swd_PacketAck = swd_PacketAck | SROM_TIMEOUT_ERROR;
Read_IO(CPUSS_SYSARG, &statusCode);
return (FAILURE);
}
randompara = 12;
/* Read CPUSS_SYSARG register to check if the SROM command executed
successfully else set SROM_TIMEOUT_ERROR in swd_PacketAck */
Read_IO(CPUSS_SYSARG, &statusCode);
if ((statusCode & SROM_STATUS_SUCCESS_MASK) != SROM_STATUS_SUCCEEDED) {
swd_PacketAck = swd_PacketAck | SROM_TIMEOUT_ERROR;
randompara = 14;
return (FAILURE);
} else {
return (SUCCESS);
}
}
#if (CY8C40xx_FAMILY)
static void SetIMO48MHz(void)
{
u32 parameter1 = 0;
/* Load the Parameter1 with the SROM command to read silicon ID */
parameter1 = (u32) (((u32) SROM_KEY1 << 0) + //
(((u32) SROM_KEY2 +
(u32) SROM_CMD_SET_IMO_48MHZ) << 8));
/* Write the command to CPUSS_SYSARG register */
Write_IO(CPUSS_SYSARG, parameter1);
Write_IO(CPUSS_SYSREQ, SROM_SYSREQ_BIT | SROM_CMD_SET_IMO_48MHZ);
}
#endif
#ifdef HEX_FIRMWARE
static u8 GetChipProtectionVal(void)
{
u32 parameter1 = 0;
u32 chipProtData = 0;
/* Load the Parameter1 with the SROM command to read silicon ID */
parameter1 = (u32) (((u32) SROM_KEY1 << 0) + //
(((u32) SROM_KEY2 +
(u32) SROM_CMD_GET_SILICON_ID) << 8));
/* Write the command to CPUSS_SYSARG register */
Write_IO(CPUSS_SYSARG, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Request system call by writing to CPUSS_SYSREQ register */
Write_IO(CPUSS_SYSREQ, SROM_SYSREQ_BIT | SROM_CMD_GET_SILICON_ID);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Read status of the operation */
result = PollSromStatus();
if (result != SROM_SUCCESS) {
return (FAILURE);
}
/* Read CPUSS_SYSREQ register to get the current protection setting of the
chip */
Read_IO(CPUSS_SYSREQ, &chipProtData);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
chipProtectionData_Chip = (u8) (chipProtData >> 12);
input_info(true, g_sd->dev, "%s: chipProtectionData_Chip=%d\n", __func__,
chipProtectionData_Chip);
return (SUCCESS);
}
static u8 GetTransitionMode(void)
{
u8 chipProtectionData_Hex;
/* Get the chip protection setting in the HEX file */
HEX_ReadChipProtectionData(&chipProtectionData_Hex);
/* enum variable flow stores the transition (current protection setting to
setting in hex file) of the chip */
flow = WRONG_TRANSITION;
input_info(true, g_sd->dev, "%s: chipProtectionData_Chip=%s\n", __func__,
chipProtectionData_Chip ==
CHIP_PROT_VIRGIN ? "CHIP_PROT_VIRGIN" : chipProtectionData_Chip
==
CHIP_PROT_OPEN ? "CHIP_PROT_OPEN" : chipProtectionData_Chip ==
CHIP_PROT_PROTECTED ? "CHIP_PROT_PROTECTED" : "ELSE");
switch (chipProtectionData_Chip) {
/* virgin to open protection setting is the only allowed transition */
case CHIP_PROT_VIRGIN:
if (chipProtectionData_Hex == CHIP_PROT_OPEN)
flow = VIRGIN_OPEN;
else
flow = WRONG_TRANSITION;
break;
/* All transitions from Open are allowed other than transition to virgin
mode */
case CHIP_PROT_OPEN:
if (chipProtectionData_Hex == CHIP_PROT_VIRGIN)
flow = WRONG_TRANSITION;
else
flow = OPEN_XXX;
break;
/* Protected to Protected and Protected to Open are the allowed
transitions */
case CHIP_PROT_PROTECTED:
if ((chipProtectionData_Hex == CHIP_PROT_OPEN)
|| (chipProtectionData_Hex == CHIP_PROT_PROTECTED))
flow = PROT_XXX;
else
flow = WRONG_TRANSITION;
break;
default:
flow = WRONG_TRANSITION;
break;
}
/* Set TRANSITION_ERROR bit high in Swd_PacketAck to show wrong transition
error */
if (flow == WRONG_TRANSITION) {
swd_PacketAck = swd_PacketAck | TRANSITION_ERROR;
return (FAILURE);
}
input_info(true, g_sd->dev, "%s: flow=%s\n", __func__,
flow == OPEN_XXX ? "OPEN_XXX" :
flow == VIRGIN_OPEN ? "VIRGIN_OPEN" :
flow == PROT_XXX ? "PROT_XXX" :
flow == WRONG_TRANSITION ? "WRONG_TRANSITION" : "");
return (SUCCESS);
}
#endif //HEX_FIRMWARE
static u8 LoadLatch(u8 arrayID, u8 * rowData)
{
u32 parameter1 = 0;
u32 parameter2 = 0;
u8 i = 0;
/* Load parameter1 with the SROM command to load the page latch buffer
with programming data */
parameter1 = ((u32) SROM_KEY1 << 0) + //
(((u32) SROM_KEY2 + (u32) SROM_CMD_LOAD_LATCH) << 8) + //
(0x00 << 16) + ((u32) arrayID << 24);
/* Number of Bytes to load minus 1 */
parameter2 = (BYTES_PER_FLASH_ROW - 1);
/* Write parameter1 in SRAM */
Write_IO(SRAM_PARAMS_BASE + 0x00, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Write parameter2 in SRAM */
Write_IO(SRAM_PARAMS_BASE + 0x04, parameter2);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Put row data into SRAM buffer */
for (i = 0; i < BYTES_PER_FLASH_ROW; i += 4) {
parameter1 =
(rowData[i] << 0) + (rowData[i + 1] << 8) +
(rowData[i + 2] << 16) + (rowData[i + 3] << 24);
/* Write parameter1 in SRAM */
Write_IO(SRAM_PARAMS_BASE + 0x08 + i, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
}
/* Call "Load Latch" SROM API */
/* Set location of parameters */
Write_IO(CPUSS_SYSARG, SRAM_PARAMS_BASE);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Request SROM operation */
Write_IO(CPUSS_SYSREQ, SROM_SYSREQ_BIT | SROM_CMD_LOAD_LATCH);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Read status of the operation */
result = PollSromStatus();
if (result != SROM_SUCCESS) {
return (FAILURE);
}
return (SUCCESS);
}
#if defined(HEX_FIRMWARE) || defined(FORCE_PROTECT)
static u8 LoadLatch_protect(u8 arrayID, u8 * rowData, u8 rowProtectionByteSize)
{
u32 parameter1 = 0;
u32 parameter2 = 0;
u8 i = 0;
/* Load parameter1 with the SROM command to load the page latch buffer
with programming data */
parameter1 = ((u32) SROM_KEY1 << 0) + //
(((u32) SROM_KEY2 + (u32) SROM_CMD_LOAD_LATCH) << 8) + //
(0x00 << 16) + ((u32) arrayID << 24);
/* Number of Bytes to load minus 1 */
parameter2 = (rowProtectionByteSize - 1);
/* Write parameter1 in SRAM */
Write_IO(SRAM_PARAMS_BASE + 0x00, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Write parameter2 in SRAM */
Write_IO(SRAM_PARAMS_BASE + 0x04, parameter2);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Put row data into SRAM buffer */
for (i = 0; i < rowProtectionByteSize; i += 4) {
parameter1 =
(rowData[i] << 0) + (rowData[i + 1] << 8) +
(rowData[i + 2] << 16) + (rowData[i + 3] << 24);
/* Write parameter1 in SRAM */
Write_IO(SRAM_PARAMS_BASE + 0x08 + i, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
}
/* Call "Load Latch" SROM API */
/* Set location of parameters */
Write_IO(CPUSS_SYSARG, SRAM_PARAMS_BASE);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Request SROM operation */
Write_IO(CPUSS_SYSREQ, SROM_SYSREQ_BIT | SROM_CMD_LOAD_LATCH);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Read status of the operation */
result = PollSromStatus();
if (result != SROM_SUCCESS) {
return (FAILURE);
}
return (SUCCESS);
}
#endif //HEX_FIRMWARE
static u8 ChecksumAPI(u16 checksumRow, u32 * checksum)
{
u32 parameter1 = 0;
u32 checksum_chip = 0;
/* Load parameter1 with the SROM command to compute checksum of whole
flash */
parameter1 = ((u32) SROM_KEY1 << 00) +
(((u32) SROM_KEY2 + (u32) SROM_CMD_CHECKSUM) << 8) +
(((u32) checksumRow & 0x000000FF) << 16) +
(((u32) checksumRow & 0x0000FF00) << 16);
/* Load CPUSS_SYSARG register with parameter1 command */
Write_IO(CPUSS_SYSARG, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Request SROM operation */
Write_IO(CPUSS_SYSREQ, SROM_SYSREQ_BIT | SROM_CMD_CHECKSUM);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Read status of the operation */
result = PollSromStatus();
if (result != SROM_SUCCESS) {
return (FAILURE);
}
/* Read CPUSS_SYSARG register to get the checksum value */
Read_IO(CPUSS_SYSARG, &checksum_chip);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* 28-bit checksum */
*checksum = (checksum_chip & 0x0FFFFFFF);
return (SUCCESS);
}
#define SetXresHigh() g_sd->power(g_sd->swd_tkey_i2c, 1)
#define SetXresLow() g_sd->power(g_sd->swd_tkey_i2c, 0)
static u8 DeviceAcquire(void)
{
u32 chip_DAP_Id = 0;
u16 total_packet_count = 0;
u32 status = 0;
unsigned long end_time;
struct swd_data *sd = g_sd;
input_dbg(true, g_sd->dev, "%s: \n", __func__);
/* Aquiring Sequence */
/* Set SWDIO and SWDCK initial pin directions and out values */
swd_set_swdio_out(sd);
swd_set_swdio_lo(sd);
swd_set_swdck_out(sd);
swd_set_swdck_lo(sd);
/* Set XRES of PSoC 4 low for 100us with SWDCK and SWDIO low (min delay
required is 5us) */
SetXresLow();
msleep(100); //DelayHundredUs();
SetXresHigh();
end_time = jiffies + usecs_to_jiffies(DEVICE_ACQUIRE_TIMEOUT_US);
do {
/* Call Swd_LineReset (Standard ARM command to reset DAP) and read
DAP_ID from chip */
Swd_LineReset();
Read_DAP(DPACC_DP_IDCODE_READ, &chip_DAP_Id);
total_packet_count++;
} while ((swd_PacketAck != SWD_OK_ACK)
&& time_before_eq(jiffies, end_time));
/* Set PORT_ACQUIRE_TIMEOUT_ERROR bit in swd_PacketAck if time
exceeds 1.5 ms */
if (!time_before_eq(jiffies, end_time)) {
swd_PacketAck = swd_PacketAck | PORT_ACQUIRE_TIMEOUT_ERROR;
input_err(true, g_sd->dev, "%s: fail, swd_PacketAck=0x%02x \n",
__func__, swd_PacketAck);
goto testmode_fail; //return(FAILURE);
}
input_info(true, g_sd->dev, "%s: chip_DAP_Id=0x%08x \n", __func__, chip_DAP_Id);
/* Set VERIFICATION_ERROR bit in swd_PacketAck if the DAP_ID read
from chip does not match with the ARM CM0_DAP_ID (MACRO defined in
ProgrammingSteps.h file - 0x0BB11477) */
if (chip_DAP_Id != CM0_DAP_ID) {
swd_PacketAck = swd_PacketAck | VERIFICATION_ERROR;
input_dbg(true, g_sd->dev, "%s:%d: chip_DAP_Id:%x\n", __func__, __LINE__, chip_DAP_Id);
goto testmode_fail; //return(FAILURE);
}
/* Initialize Debug Port */
Write_DAP(DPACC_DP_CTRLSTAT_WRITE, 0x54000000);
if (swd_PacketAck != SWD_OK_ACK) {
input_dbg(true, g_sd->dev, "%s:%d: swd_PacketAck:%x\n", __func__, __LINE__, swd_PacketAck);
goto testmode_fail; //return(FAILURE);
}
Write_DAP(DPACC_DP_SELECT_WRITE, 0x00000000);
if (swd_PacketAck != SWD_OK_ACK) {
input_dbg(true, g_sd->dev, "%s:%d: swd_PacketAck:%x\n", __func__, __LINE__, swd_PacketAck);
goto testmode_fail; //return(FAILURE);
}
Write_DAP(DPACC_AP_CSW_WRITE, 0x00000002);
if (swd_PacketAck != SWD_OK_ACK) {
input_dbg(true, g_sd->dev, "%s:%d: swd_PacketAck:%x\n", __func__, __LINE__, swd_PacketAck);
goto testmode_fail; //return(FAILURE);
}
/* Enter CPU into Test Mode */
Write_IO(TEST_MODE, 0x80000000);
if (swd_PacketAck != SWD_OK_ACK) {
input_dbg(true, g_sd->dev, "%s:%d: swd_PacketAck:%x\n", __func__, __LINE__, swd_PacketAck);
goto testmode_fail; //return(FAILURE);
}
Read_IO(TEST_MODE, &status);
if (swd_PacketAck != SWD_OK_ACK) {
input_dbg(true, g_sd->dev, "%s:%d: swd_PacketAck:%x\n", __func__, __LINE__, swd_PacketAck);
goto testmode_fail; //return(FAILURE);
}
if ((status & 0x80000000) != 0x80000000) {
input_dbg(true, g_sd->dev, "%s:%d: status:%x\n", __func__, __LINE__, status);
goto testmode_fail; //return (FAILURE);
}
/* Read status of the operation */
result = PollSromStatus();
if (result != SROM_SUCCESS) {
input_dbg(true, g_sd->dev, "%s:%d: result:%x\n", __func__, __LINE__, result);
goto testmode_fail; //return(FAILURE);
}
#if (CY8C40xx_FAMILY)
/* Set IMO to 48 MHz */
SetIMO48MHz();
/* Read status of the operation */
result = PollSromStatus();
if (result != SROM_SUCCESS) {
input_err(true, g_sd->dev, "%s:%d: fail result:%x\n", __func__, __LINE__, result);
return (FAILURE);
}
#endif
input_info(true, g_sd->dev, "%s: success\n", __func__);
return (SUCCESS);
testmode_fail:
return (FAILURE);
}
#ifdef HEX_FIRMWARE
static u8 VerifySiliconId(void)
{
u32 deviceSiliconID;
u32 hexSiliconId = 0;
u32 parameter1 = 0;
u32 siliconIdData1 = 0;
u32 siliconIdData2 = 0;
input_info(true, g_sd->dev, "%s: \n", __func__);
/* Read and store Silicon ID from HEX file to hexSiliconId array */
HEX_ReadSiliconId(&hexSiliconId);
/* Load Parameter1 with the SROM command to read silicon ID from PSoC 4
chip */
parameter1 = (u32) (((u32) SROM_KEY1 << 0) + //
(((u32) SROM_KEY2 +
(u32) SROM_CMD_GET_SILICON_ID) << 8));
/* Load CPUSS_SYSARG register with parameter1 */
Write_IO(CPUSS_SYSARG, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Request SROM operation */
Write_IO(CPUSS_SYSREQ, SROM_SYSREQ_BIT | SROM_CMD_GET_SILICON_ID);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Read status of the operation */
result = PollSromStatus();
if (result != SROM_SUCCESS) {
return (FAILURE);
}
/* Read CPUSS_SYSARG and CPUSS_SYSREQ to read 4 bytes of silicon ID */
Read_IO(CPUSS_SYSARG, &siliconIdData1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
Read_IO(CPUSS_SYSREQ, &siliconIdData2);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/*
SiliconIdData2 (0th byte) = 4th byte of Device Silicon ID (MSB)
SiliconIdData1 (3rd byte) = 3rd byte of Device Silicon ID
SiliconIdData1 (1st byte) = 2nd byte of Device Silicon ID
SiliconIdData1 (2nd byte) = 1st byte of Device Silicon ID (LSB)
*/
deviceSiliconID = (((siliconIdData2 << 24) & 0xFF000000) + (siliconIdData1 & 0x00FF0000) + //
((siliconIdData1 << 8) & 0x0000FF00) +
((siliconIdData1 >> 8) & 0x000000FF));
input_dbg(true, g_sd->dev, "%s: deviceSiliconID=0x%08x\n", __func__,
deviceSiliconID);
input_dbg(true, g_sd->dev, "%s: hexSiliconId=0x%08x\n", __func__,
hexSiliconId);
input_info(true, g_sd->dev, "%s: success\n", __func__);
return (SUCCESS);
}
#endif //HEX_FIRMWARE
static u8 EraseAllFlash(void)
{
u32 parameter1 = 0;
input_dbg(true, g_sd->dev, "%s: \n", __func__);
#ifdef HEX_FIRMWARE
/* Get current chip protection setting */
GetChipProtectionVal();
/* Check if the Chip protection setting transition is valid */
result = GetTransitionMode();
if (result != SUCCESS) {
return (FAILURE);
}
#else
flow = OPEN_XXX;
#endif
/* If the transition is from open to any protection setting or from virgin to
open, call ERASE_ALL SROM command */
if ((flow == OPEN_XXX) || (flow == VIRGIN_OPEN)) {
parameter1 = (u32) (((u32) SROM_KEY1 << 0) + //
(((u32) SROM_KEY2 +
(u32) SROM_CMD_ERASE_ALL) << 8));
/* Load ERASE_ALL SROM command in parameter1 to SRAM */
Write_IO(SRAM_PARAMS_BASE + 0x00, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Set location of parameters */
Write_IO(CPUSS_SYSARG, SRAM_PARAMS_BASE);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Request SROM call */
Write_IO(CPUSS_SYSREQ, SROM_SYSREQ_BIT | SROM_CMD_ERASE_ALL);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Read status of the operation */
result = PollSromStatus();
if (result != SUCCESS) {
return (FAILURE);
}
input_info(true, g_sd->dev, "%s: erase all succeeded\n", __func__);
}
/* If the transition is from protected mode to open mode or protected mode to
protected mode only, call ERASE_ALL SROM command */
else if (flow == PROT_XXX) {
/* Move chip to open state: 0x01 corresponds to open state, 0x00 to
macro 1 */
parameter1 = ((u32) SROM_KEY1 << 0) + //
(((u32) SROM_KEY2 + (u32) SROM_CMD_WRITE_PROTECTION) << 8) + //
(0x01 << 16) + (0x00 << 24);
/* Load the write protection command to SRAM */
Write_IO(CPUSS_SYSARG, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Request SROM call */
Write_IO(CPUSS_SYSREQ,
SROM_SYSREQ_BIT | SROM_CMD_WRITE_PROTECTION);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Read status of the operation */
result = PollSromStatus();
if (result != SUCCESS) {
return (FAILURE);
}
/* Re-acquire chip in OPEN mode */
result = DeviceAcquire();
if (result != SUCCESS) {
return (FAILURE);
}
input_info(true, g_sd->dev, "%s: move to open state succeeded\n",
__func__);
}
input_dbg(true, g_sd->dev, "%s: success\n", __func__);
return (SUCCESS);
}
static u8 ChecksumPrivileged(void)
{
input_dbg(true, g_sd->dev, "%s: \n", __func__);
result = ChecksumAPI(CHECKSUM_ENTIRE_FLASH, &checksum_Privileged);
if (result != SUCCESS) {
return (FAILURE);
}
input_info(true, g_sd->dev, "%s: checksum_Privileged=0x%08x\n", __func__,
checksum_Privileged);
input_dbg(true, g_sd->dev, "%s: success\n", __func__);
return (SUCCESS);
}
static u8 ProgramFlash(void)
{
u8 arrayID = 0;
u8 rowData[BYTES_PER_FLASH_ROW];
u16 numOfFlashRows = 0;
u16 rowCount = 0;
u32 parameter1 = 0;
input_info(true, g_sd->dev, "%s: \n", __func__);
/* Get the total number of flash rows in the Target PSoC 4 device */
numOfFlashRows = GetFlashRowCount();
input_dbg(true, g_sd->dev, "%s: numOfFlashRows=%d\n", __func__,
numOfFlashRows);
/* Program all flash rows */
for (rowCount = 0; rowCount < numOfFlashRows; rowCount++) {
#ifdef HEX_FIRMWARE
HEX_ReadRowData(rowCount, &rowData[0]);
#else
memcpy(&rowData[0],
g_sd->fw_img + (rowCount * BYTES_PER_FLASH_ROW),
BYTES_PER_FLASH_ROW);
#endif
if (rowCount == 0)
input_dbg(true, g_sd->dev,
"%s: row%d file=0x%02x 0x%02x 0x%02x 0x%02x\n",
__func__, rowCount, rowData[0], rowData[1],
rowData[2], rowData[3]);
else if (rowCount == (numOfFlashRows - 1))
input_dbg(true, g_sd->dev,
"%s: row%d file=0x%02x 0x%02x 0x%02x 0x%02x\n",
__func__, rowCount, rowData[0], rowData[1],
rowData[2], rowData[3]);
arrayID = rowCount / ROWS_PER_ARRAY;
result = LoadLatch(arrayID, &rowData[0]);
if (result != SUCCESS) {
input_err(true, g_sd->dev, "%s: fail load latch\n", __func__);
return (FAILURE);
}
/* Load parameter1 with Program Row - SROM command */
parameter1 = (u32) (((u32) SROM_KEY1 << 0) +
(((u32) SROM_KEY2 +
(u32) SROM_CMD_PROGRAM_ROW) << 8) +
(((u32) rowCount & 0x000000FF) << 16) +
(((u32) rowCount & 0x0000FF00) << 16));
/* Write parameters in SRAM */
Write_IO(SRAM_PARAMS_BASE + 0x00, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
input_err(true, g_sd->dev, "%s: fail prog row\n", __func__);
return (FAILURE);
}
/* Set location of parameters */
Write_IO(CPUSS_SYSARG, SRAM_PARAMS_BASE);
if (swd_PacketAck != SWD_OK_ACK) {
input_err(true, g_sd->dev, "%s: fail set location\n", __func__);
return (FAILURE);
}
/* Request SROM operation */
Write_IO(CPUSS_SYSREQ, SROM_SYSREQ_BIT | SROM_CMD_PROGRAM_ROW);
if (swd_PacketAck != SWD_OK_ACK) {
input_err(true, g_sd->dev, "%s: fail request SROM operation\n",
__func__);
return (FAILURE);
}
/* Read status of the operation */
result = PollSromStatus();
if (result != SROM_SUCCESS) {
input_err(true, g_sd->dev, "%s: fail read status\n", __func__);
return (FAILURE);
}
}
input_info(true, g_sd->dev, "%s: success\n", __func__);
return (SUCCESS);
}
static u8 VerifyFlash(void)
{
u32 flashData = 0;
u16 numOfFlashRows = 0;
u16 rowAddress = 0;
u16 rowCount;
u8 i;
u8 rowData[BYTES_PER_FLASH_ROW];
u8 chipdata[BYTES_PER_FLASH_ROW];
input_dbg(true, g_sd->dev, "%s: \n", __func__);
/* Get the total number of flash rows in the Target PSoC 4 device */
numOfFlashRows = GetFlashRowCount();
input_info(true, g_sd->dev, "%s: numOfFlashRows=%d\n", __func__,
numOfFlashRows);
/* Read and Verify Flash rows */
for (rowCount = 0; rowCount < numOfFlashRows; rowCount++) {
/* Read row from hex file */
/* linear address of row in flash */
rowAddress = BYTES_PER_FLASH_ROW * rowCount;
/* Extract 128-byte row from the hex-file from address: ?�rowCount??into
buffer - ?�rowData?? */
#ifdef HEX_FIRMWARE
HEX_ReadRowData(rowCount, &rowData[0]);
#else
memcpy(&rowData[0],
g_sd->fw_img + (rowCount * BYTES_PER_FLASH_ROW),
BYTES_PER_FLASH_ROW);
#endif
if (rowCount == 0)
input_dbg(true, g_sd->dev,
"%s: row%d file=0x%02x 0x%02x 0x%02x 0x%02x\n",
__func__, rowCount, rowData[0], rowData[1],
rowData[2], rowData[3]);
else if (rowCount == (numOfFlashRows - 1))
input_dbg(true, g_sd->dev,
"%s: row%d file=0x%02x 0x%02x 0x%02x 0x%02x\n",
__func__, rowCount, rowData[0], rowData[1],
rowData[2], rowData[3]);
/* Read row from chip */
for (i = 0; i < BYTES_PER_FLASH_ROW; i += 4) {
/* Read flash via AHB-interface */
Read_IO(rowAddress + i, &flashData);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
chipdata[i + 0] = (flashData >> 0) & 0xFF;
chipdata[i + 1] = (flashData >> 8) & 0xFF;
chipdata[i + 2] = (flashData >> 16) & 0xFF;
chipdata[i + 3] = (flashData >> 24) & 0xFF;
}
if (rowCount == 0)
input_dbg(true, g_sd->dev,
"%s: row%d chip=0x%02x 0x%02x 0x%02x 0x%02x\n",
__func__, rowCount, chipdata[0], chipdata[1],
chipdata[2], chipdata[3]);
else if (rowCount == (numOfFlashRows - 1))
input_dbg(true, g_sd->dev,
"%s: row%d chip=0x%02x 0x%02x 0x%02x 0x%02x\n",
__func__, rowCount, chipdata[0], chipdata[1],
chipdata[2], chipdata[3]);
/* Compare the row data of HEX file with chip data */
for (i = 0; i < BYTES_PER_FLASH_ROW; i++) {
if (chipdata[i] != rowData[i]) {
swd_PacketAck =
swd_PacketAck | VERIFICATION_ERROR;
return (FAILURE);
}
}
}
input_info(true, g_sd->dev, "%s: success\n", __func__);
return (SUCCESS);
}
#if defined(HEX_FIRMWARE) || defined(FORCE_PROTECT)
static u8 ProgramProtectionSettings(void)
{
u8 arrayID = 0;
u8 rowProtectionByteSize = 0;
u8 rowProtectionData[MAXIMUM_ROW_PROTECTION_BYTE_LENGTH];
u8 chipProtectionData_Hex;
u16 numOfFlashRows = 0;
u32 parameter1 = 0;
input_info(true, g_sd->dev, "%s: \n", __func__);
/* Get total number of flash rows to determine the size of row protection data
and arrayID */
numOfFlashRows = GetFlashRowCount();
input_dbg(true, g_sd->dev, "%s: numOfFlashRows=%d\n", __func__,
numOfFlashRows);
rowProtectionByteSize = numOfFlashRows / 8;
input_dbg(true, g_sd->dev, "%s: rowProtectionByteSize=%d\n", __func__,
rowProtectionByteSize);
arrayID = numOfFlashRows / ROWS_PER_ARRAY;
input_dbg(true, g_sd->dev, "%s: arrayID=%d\n", __func__, arrayID);
HEX_ReadChipProtectionData(&chipProtectionData_Hex);
input_dbg(true, g_sd->dev, "%s: chipProtectionData=%d\n", __func__,
chipProtectionData_Hex);
HEX_ReadRowProtectionData(rowProtectionByteSize, &rowProtectionData[0]);
input_dbg(true, g_sd->dev, "%s: rowProtectionData0~2:0x%02x 0x%02x 0x%02x \n",
__func__, rowProtectionData[0], rowProtectionData[1],
rowProtectionData[2]);
/* Load protection setting of current macro into volatile latch using
LoadLatch API */
result =
LoadLatch_protect(arrayID, &rowProtectionData[0],
rowProtectionByteSize);
if (result != SUCCESS) {
return (FAILURE);
}
/* Program protection setting into supervisory row */
parameter1 = (u32) (((u32) SROM_KEY1 << 0) + //
(((u32) SROM_KEY2 + (u32) SROM_CMD_WRITE_PROTECTION) << 8) + //
((u32) chipProtectionData_Hex << 16));
/* Load parameter1 in CPUSS_SYSARG register */
Write_IO(CPUSS_SYSARG, parameter1);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Request SROM call */
Write_IO(CPUSS_SYSREQ, SROM_SYSREQ_BIT | SROM_CMD_WRITE_PROTECTION);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
/* Read status of the operation */
result = PollSromStatus();
if (result != SROM_SUCCESS) {
return (FAILURE);
}
input_info(true, g_sd->dev, "%s: success\n", __func__);
return (SUCCESS);
}
static u8 VerifyProtectionSettings(void)
{
u32 protectionData = 0;
u32 flashProtectionAddress = 0;
u16 numOfFlashRows = 0;
u8 chipProtectionData_Hex = 0;
u8 rowProtectionByteSize = 0;
u8 i;
u8 rowProtectionData[MAXIMUM_ROW_PROTECTION_BYTE_LENGTH];
u8 rowProtectionFlashData[MAXIMUM_ROW_PROTECTION_BYTE_LENGTH];
input_info(true, g_sd->dev, "%s: \n", __func__);
numOfFlashRows = GetFlashRowCount();
rowProtectionByteSize = numOfFlashRows / 8;
input_dbg(true, g_sd->dev, "%s: rowProtectionByteSize=%d\n", __func__,
rowProtectionByteSize);
flashProtectionAddress = SFLASH_MACRO;
/* Read Protection settings from hex-file */
HEX_ReadRowProtectionData(rowProtectionByteSize, &rowProtectionData[0]);
/* Read Protection settings from silicon */
for (i = 0; i < rowProtectionByteSize; i += 4) {
Read_IO(flashProtectionAddress + i, &protectionData);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
rowProtectionFlashData[i + 0] = (protectionData >> 0) & 0xFF;
rowProtectionFlashData[i + 1] = (protectionData >> 8) & 0xFF;
rowProtectionFlashData[i + 2] = (protectionData >> 16) & 0xFF;
rowProtectionFlashData[i + 3] = (protectionData >> 24) & 0xFF;
if (i == 0)
input_dbg(true, g_sd->dev,
"%s: rowProtectionFlashData0~2:0x%02x 0x%02x 0x%02x \n",
__func__, rowProtectionData[0],
rowProtectionData[1], rowProtectionData[2]);
}
/* Compare hex and silicon?�s data */
for (i = 0; i < rowProtectionByteSize; i++) {
if (rowProtectionData[i] != rowProtectionFlashData[i]) {
/* Set the verification error bit for Flash protection data
mismatch and return failure */
swd_PacketAck = swd_PacketAck | VERIFICATION_ERROR;
return (FAILURE);
}
}
/* Read Chip Level Protection from hex-file */
HEX_ReadChipProtectionData(&chipProtectionData_Hex);
input_dbg(true, g_sd->dev, "%s: chipProtectionData=%d\n", __func__,
chipProtectionData_Hex);
/* Read Chip Level Protection from the silicon */
Read_IO(SFLASH_CPUSS_PROTECTION, &protectionData);
if (swd_PacketAck != SWD_OK_ACK) {
return (FAILURE);
}
chipProtectionData_Chip = (protectionData >> 24) & 0x0F;
input_dbg(true, g_sd->dev, "%s: chipProtectionData_Chip=0x%02x\n", __func__,
chipProtectionData_Chip);
if (chipProtectionData_Chip == CHIP_PROT_VIRGIN) {
chipProtectionData_Chip = CHIP_PROT_OPEN;
//input_dbg(true, g_sd->dev, "%s: chip protection open\n", __func__);
} else if (chipProtectionData_Chip == CHIP_PROT_OPEN) {
chipProtectionData_Chip = CHIP_PROT_VIRGIN;
//input_dbg(true, g_sd->dev, "%s: chip protection virgin\n", __func__);
}
/* Compare hex?�s and silicon?�s chip protection data */
if (chipProtectionData_Chip != chipProtectionData_Hex) {
/* Set the verification error bit for Flash protection data
mismatch and return failure */
swd_PacketAck = swd_PacketAck | VERIFICATION_ERROR;
return (FAILURE);
}
input_info(true, g_sd->dev, "%s: success\n", __func__);
return (SUCCESS);
}
#endif
static u8 VerifyChecksum(void)
{
u32 checksum_All = 0;
u16 chip_Checksum = 0;
input_info(true, g_sd->dev, "%s: \n", __func__);
/* Read the checksum of entire flash */
result = ChecksumAPI(CHECKSUM_ENTIRE_FLASH, &checksum_All);
if (result != SUCCESS) {
input_err(true, g_sd->dev, "%s: fail ChecksumAPI\n", __func__);
return (FAILURE);
}
input_dbg(true, g_sd->dev, "%s: checksum of entire flash=0x%08x\n", __func__,
checksum_All);
/* Calculate checksum of user flash */
chip_Checksum = (u16) checksum_All - (u16) checksum_Privileged;
input_info(true, g_sd->dev, "%s: chip_Checksum=0x%08x\n", __func__,
chip_Checksum);
input_info(true, g_sd->dev, "%s: checksum from fw file=0x%08x\n", __func__,
g_sd->swd_tkey_i2c->fw_img->checksum);
/* Compare the checksum data of silicon and hex file */
if (chip_Checksum != g_sd->swd_tkey_i2c->fw_img->checksum) {
swd_PacketAck = swd_PacketAck | VERIFICATION_ERROR;
input_err(true, g_sd->dev, "%s: fail compare checksum\n", __func__);
return (FAILURE);
}
input_info(true, g_sd->dev, "%s: success\n", __func__);
return (SUCCESS);
}
static void ExitProgrammingMode(void)
{
input_info(true, g_sd->dev, "%s: \n", __func__);
/* Generate active low rest pulse for 100 uS */
SetXresLow();
msleep(20);
SetXresHigh();
}
static u8 ProgramDevice(struct swd_data *sd)
{
if (DeviceAcquire() == FAILURE)
goto fail;
#if !defined(CRC_CHECK_INTERNAL)
if (sd->swd_tkey_i2c->do_checksum == true) {
if (VerifyChecksum() == SUCCESS) {
input_info(true, sd->dev, "%s: fw update pass\n", __func__);
goto out;
}
}
#endif
#ifdef HEX_FIRMWARE
if (VerifySiliconId() == FAILURE)
goto fail;
#endif
if (EraseAllFlash() == FAILURE)
goto fail;
if (ChecksumPrivileged() == FAILURE)
goto fail;
if (ProgramFlash() == FAILURE)
goto fail;
if (VerifyFlash() == FAILURE)
goto fail;
#if defined(HEX_FIRMWARE) || defined(FORCE_PROTECT)
if (ProgramProtectionSettings() == FAILURE)
goto fail;
if (VerifyProtectionSettings() == FAILURE)
goto fail;
#endif
if (VerifyChecksum() == FAILURE)
goto fail;
#if !defined(CRC_CHECK_INTERNAL)
out:
#endif
ExitProgrammingMode();
/* All the steps were completed successfully */
return (SUCCESS);
fail:
ExitProgrammingMode();
return (FAILURE);
}
/*****************************************************************************
* probe
*****************************************************************************/
int cy8cmbr_swd_program(struct touchkey_i2c *tkey_i2c)
{
struct swd_data *sd;
int rc;
input_info(true, &tkey_i2c->client->dev, "%s: \n", __func__);
memset(&_swd_data, 0, sizeof(_swd_data));
sd = &_swd_data;
sd->swd_tkey_i2c = tkey_i2c;
sd->power = tkey_i2c->power;
sd->swdio_gpio = tkey_i2c->dtdata->gpio_sda;
sd->swdck_gpio = tkey_i2c->dtdata->gpio_scl;
sd->fw_img = tkey_i2c->fw_img->data;
sd->fw_img_size = tkey_i2c->fw_img->fw_len;
sd->dev = &tkey_i2c->client->dev;
g_sd = sd;
Swd_configPhysical(sd->swdio_gpio, sd->swdck_gpio);
if (ProgramDevice(sd) == SUCCESS)
rc = 0;
else
rc = -1;
input_info(true, &tkey_i2c->client->dev, "%s: end, rc=%d\n", __func__, rc);
return rc;
}
|