1
2
3
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
138
139
142
143
144
145
146
147
148
149
150
151
152
153
163
167
168
169
170
171
172
173
174
175
176
179
180
181
182
183
184
185
186
187
188
189
190
200
204
205
206
207
208
209
210
211
212
213
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
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
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
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
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
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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
719
720
721
722
723
724
725
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
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
846
847
848
849
850
851
852
853
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
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
949
956
963
970
977
984
991
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
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
1167
1168
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
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
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
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
1370
1371
1375
1376
1380
1381
1382
1383
1384
1388
1389
1390
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
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
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
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
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include <helper/binarybuffer.h>
#include <target/algorithm.h>
#include <target/armv7m.h>
#define FLASH_DRIVER_VER 0x00010000
#define CHIPID_ADDR 0xF0000000
#define K1921VK01T_ID 0x00000000
/* ... */
#define MAIN_MEM_TYPE 0
#define INFO_MEM_TYPE 1
#define SERVICE_MODE_ERASE_ADDR 0x80030164
#define MAGIC_KEY 0xA442
#define BOOTFLASH_BASE 0xA001C000
#define FMA (BOOTFLASH_BASE + 0x00)
#define FMD1 (BOOTFLASH_BASE + 0x04)
#define FMC (BOOTFLASH_BASE + 0x08)
#define FCIS (BOOTFLASH_BASE + 0x0C)
#define FCIM (BOOTFLASH_BASE + 0x10)
#define FCIC (BOOTFLASH_BASE + 0x14)
#define FMD2 (BOOTFLASH_BASE + 0x50)
#define FMD3 (BOOTFLASH_BASE + 0x54)
#define FMD4 (BOOTFLASH_BASE + 0x58)
#define FMC_WRITE (1<<0)
#define FMC_PAGE_ERASE (1<<1)
#define FMC_FULL_ERASE (1<<2)
#define FMC_WRITE_IFB (1<<4)
#define FMC_PAGEERASE_IFB (1<<5)
#define FMC_MAGIC_KEY (MAGIC_KEY<<16)
#define FCIS_OP_CMLT (1<<0)
#define FCIS_OP_ERROR (1<<1)
#define FCIC_CLR_OPCMLT (1<<0)
#define FCIC_CLR_OPERROR (1<<1)
-- BOOTFLASH
#define USERFLASH_PAGE_SIZE 256
#define USERFLASH_PAGE_TOTALNUM 256
#define USERFLASH_BASE 0xA0022000
#define UFMA (USERFLASH_BASE + 0x00)
#define UFMD (USERFLASH_BASE + 0x04)
#define UFMC (USERFLASH_BASE + 0x08)
#define UFCIS (USERFLASH_BASE + 0x0C)
#define UFCIM (USERFLASH_BASE + 0x10)
#define UFCIC (USERFLASH_BASE + 0x14)
#define UFMC_WRITE (1<<0)
#define UFMC_PAGE_ERASE (1<<1)
#define UFMC_FULL_ERASE (1<<2)
#define UFMC_READ (1<<3)
#define UFMC_WRITE_IFB (1<<4)
#define UFMC_PAGEERASE_IFB (1<<5)
#define UFMC_READ_IFB (1<<6)
#define UFMC_MAGIC_KEY (MAGIC_KEY<<16)
#define UFCIS_OP_CMLT (1<<0)
#define UFCIS_OP_ERROR (1<<1)
#define UFCIC_CLR_OPCMLT (1<<0)
#define UFCIC_CLR_OPERROR (1<<1)
#define INFOWORD0_ADDR 0x00
#define INFOWORD0_BOOTFROM_IFB (1<<0)
#define INFOWORD0_EN_GPIO (1<<1)
#define INFOWORD0_BOOTFROM_IFB_POS 0
#define INFOWORD0_EN_GPIO_POS 1
#define INFOWORD0_EXTMEM_SEL_POS 3
#define INFOWORD1_ADDR 0x01
#define INFOWORD1_PINNUM_POS 0
#define INFOWORD1_PORTNUM_POS 4
#define INFOWORD2_ADDR 0x02
#define INFOWORD2_LOCK_IFB_BF (1<<0)
#define INFOWORD3_ADDR 0x03
#define INFOWORD3_LOCK_IFB_UF (1<<0)
#define BF_LOCK_ADDR 0x40
#define UF_LOCK_ADDR 0x80
63 defines
/* ... */
struct niietcm4_flash_bank {
bool probed;
uint32_t chipid;
char *chip_name;
char chip_brief[4096];
uint32_t uflash_width;
uint32_t uflash_size;
uint32_t uflash_pagetotal;
uint32_t uflash_info_size;
uint32_t uflash_info_pagetotal;
bool bflash_info_remap;
char *extmem_boot_port;
uint32_t extmem_boot_pin;
uint32_t extmem_boot_altfunc;
bool extmem_boot;
...};
/* ... */
/* ... */
static int niietcm4_opstatus_check(struct flash_bank *bank)
{
struct target *target = bank->target;
int retval;
int timeout = 5000;
uint32_t flash_status;
retval = target_read_u32(target, FCIS, &flash_status);
if (retval != ERROR_OK)
return retval;
while (flash_status == 0x00) {
retval = target_read_u32(target, FCIS, &flash_status);
if (retval != ERROR_OK)
return retval;
if (timeout-- <= 0) {
LOG_ERROR("Bootflash operation timeout");
return ERROR_FLASH_OPERATION_FAILED;
}if (timeout-- <= 0) { ... }
busy_sleep(1);
}while (flash_status == 0x00) { ... }
if (flash_status == FCIS_OP_ERROR) {
LOG_ERROR("Bootflash operation error");
return ERROR_FLASH_OPERATION_FAILED;
}if (flash_status == FCIS_OP_ERROR) { ... }
uint32_t flash_cmd = FCIC_CLR_OPCMLT | FCIC_CLR_OPERROR;
retval = target_write_u32(target, FCIC, flash_cmd);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
/* ... */
static int niietcm4_uopstatus_check(struct flash_bank *bank)
{
struct target *target = bank->target;
int retval;
int timeout = 5000;
uint32_t uflash_status;
retval = target_read_u32(target, UFCIS, &uflash_status);
if (retval != ERROR_OK)
return retval;
while (uflash_status == 0x00) {
retval = target_read_u32(target, UFCIS, &uflash_status);
if (retval != ERROR_OK)
return retval;
if (timeout-- <= 0) {
LOG_ERROR("Userflash operation timeout");
return ERROR_FLASH_OPERATION_FAILED;
}if (timeout-- <= 0) { ... }
busy_sleep(1);
}while (uflash_status == 0x00) { ... }
if (uflash_status == UFCIS_OP_ERROR) {
LOG_ERROR("Userflash operation error");
return ERROR_FLASH_OPERATION_FAILED;
}if (uflash_status == UFCIS_OP_ERROR) { ... }
uint32_t uflash_cmd = UFCIC_CLR_OPCMLT | UFCIC_CLR_OPERROR;
retval = target_write_u32(target, UFCIC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
/* ... */
static int niietcm4_dump_uflash_page(struct flash_bank *bank, uint32_t *dump, int page_num, int mem_type)
{
struct target *target = bank->target;
int i;
int retval = ERROR_OK;
uint32_t uflash_cmd;
if (mem_type == INFO_MEM_TYPE)
uflash_cmd = UFMC_MAGIC_KEY | UFMC_READ_IFB;
else
uflash_cmd = UFMC_MAGIC_KEY | UFMC_READ;
int first = page_num*USERFLASH_PAGE_SIZE;
int last = first + USERFLASH_PAGE_SIZE;
for (i = first; i < last; i++) {
retval = target_write_u32(target, UFMA, i);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_uopstatus_check(bank);
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, UFMD, &dump[i]);
if (retval != ERROR_OK)
return retval;
}for (i = first; i < last; i++) { ... }
return retval;
}{ ... }
/* ... */
static int niietcm4_load_uflash_page(struct flash_bank *bank, uint32_t *dump, int page_num, int mem_type)
{
struct target *target = bank->target;
int i;
int retval = ERROR_OK;
uint32_t uflash_cmd;
if (mem_type == INFO_MEM_TYPE)
uflash_cmd = UFMC_MAGIC_KEY | UFMC_WRITE_IFB;
else
uflash_cmd = UFMC_MAGIC_KEY | UFMC_WRITE;
int first = page_num*USERFLASH_PAGE_SIZE;
int last = first + USERFLASH_PAGE_SIZE;
for (i = first; i < last; i++) {
retval = target_write_u32(target, UFMA, i);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMD, dump[i]);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_uopstatus_check(bank);
if (retval != ERROR_OK)
return retval;
}for (i = first; i < last; i++) { ... }
return retval;
}{ ... }
/* ... */
static int niietcm4_uflash_page_erase(struct flash_bank *bank, int page_num, int mem_type)
{
struct target *target = bank->target;
int retval;
uint32_t uflash_cmd;
if (mem_type == INFO_MEM_TYPE)
uflash_cmd = UFMC_MAGIC_KEY | UFMC_PAGEERASE_IFB;
else
uflash_cmd = UFMC_MAGIC_KEY | UFMC_PAGE_ERASE;
retval = target_write_u32(target, UFMA, page_num*USERFLASH_PAGE_SIZE);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMD, 0xFF);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_uopstatus_check(bank);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
/* ... */
static int niietcm4_uflash_protect(struct flash_bank *bank, int mem_type,
int set, unsigned int first, unsigned int last)
{
int retval;
if (mem_type == INFO_MEM_TYPE) {
uint32_t uflash_dump[USERFLASH_PAGE_SIZE];
retval = niietcm4_dump_uflash_page(bank, uflash_dump, 0, 1);
if (retval != ERROR_OK)
return retval;
if (set)
uflash_dump[INFOWORD2_ADDR] &= ~INFOWORD3_LOCK_IFB_UF;
else
uflash_dump[INFOWORD2_ADDR] |= INFOWORD3_LOCK_IFB_UF;
retval = niietcm4_uflash_page_erase(bank, 0, 1);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_load_uflash_page(bank, uflash_dump, 0, 1);
if (retval != ERROR_OK)
return retval;
}if (mem_type == INFO_MEM_TYPE) { ... } else {
uint32_t uflash_dump[USERFLASH_PAGE_SIZE];
retval = niietcm4_dump_uflash_page(bank, uflash_dump, 0, 1);
if (retval != ERROR_OK)
return retval;
for (unsigned int i = first; i <= last; i++) {
uint32_t reg_num = i/8;
uint32_t bit_num = i%8;
if (set)
uflash_dump[UF_LOCK_ADDR+reg_num] &= ~(1<<bit_num);
else
uflash_dump[UF_LOCK_ADDR+reg_num] |= (1<<bit_num);
}for (unsigned int i = first; i <= last; i++) { ... }
retval = niietcm4_uflash_page_erase(bank, 0, 1);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_load_uflash_page(bank, uflash_dump, 0, 1);
if (retval != ERROR_OK)
return retval;
}else { ... }
return retval;
}{ ... }
/* ... */
COMMAND_HANDLER(niietcm4_handle_uflash_read_byte_command)
{
if (CMD_ARGC < 3)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct target *target = bank->target;
CMD_ARGC--;
CMD_ARGV++;
uint32_t uflash_addr;
uint32_t uflash_cmd;
uint32_t uflash_data;
if (strcmp("info", CMD_ARGV[0]) == 0)
uflash_cmd = UFMC_MAGIC_KEY | UFMC_READ_IFB;
else if (strcmp("main", CMD_ARGV[0]) == 0)
uflash_cmd = UFMC_MAGIC_KEY | UFMC_READ;
else
return ERROR_COMMAND_SYNTAX_ERROR;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], uflash_addr);
retval = target_write_u32(target, UFMA, uflash_addr);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_uopstatus_check(bank);
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, UFMD, &uflash_data);
if (retval != ERROR_OK)
return retval;
command_print(CMD, "Read userflash %s region:\n"
"address = 0x%04" PRIx32 ",\n"
"value = 0x%02" PRIx32 ".", CMD_ARGV[0], uflash_addr, uflash_data);
return retval;
}{ ... }
COMMAND_HANDLER(niietcm4_handle_uflash_write_byte_command)
{
if (CMD_ARGC < 4)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
CMD_ARGC--;
CMD_ARGV++;
uint32_t uflash_addr;
uint32_t uflash_data;
int mem_type;
if (strcmp("info", CMD_ARGV[0]) == 0)
mem_type = 1;
else if (strcmp("main", CMD_ARGV[0]) == 0)
mem_type = 0;
else
return ERROR_COMMAND_SYNTAX_ERROR;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], uflash_addr);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], uflash_data);
int page_num = uflash_addr/USERFLASH_PAGE_SIZE;
command_print(CMD, "Write userflash %s region:\n"
"address = 0x%04" PRIx32 ",\n"
"value = 0x%02" PRIx32 ".\n"
"Please wait ... ", CMD_ARGV[0], uflash_addr, uflash_data);
uint32_t uflash_dump[USERFLASH_PAGE_SIZE];
niietcm4_dump_uflash_page(bank, uflash_dump, page_num, mem_type);
uflash_dump[uflash_addr%USERFLASH_PAGE_SIZE] = uflash_data;
niietcm4_uflash_page_erase(bank, page_num, mem_type);
niietcm4_load_uflash_page(bank, uflash_dump, page_num, mem_type);
command_print(CMD, "done!");
return retval;
}{ ... }
COMMAND_HANDLER(niietcm4_handle_uflash_full_erase_command)
{
if (CMD_ARGC < 1)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
uint32_t uflash_addr = 0;
uint32_t uflash_data = 0xFF;
uint32_t uflash_cmd = UFMC_MAGIC_KEY | UFMC_FULL_ERASE;
retval = target_write_u32(target, UFMA, uflash_addr);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMD, uflash_data);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_uopstatus_check(bank);
if (retval != ERROR_OK)
return retval;
command_print(CMD, "Userflash full erase done!");
return retval;
}{ ... }
COMMAND_HANDLER(niietcm4_handle_uflash_erase_command)
{
if (CMD_ARGC < 4)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
CMD_ARGC--;
CMD_ARGV++;
unsigned int first, last;
int mem_type;
if (strcmp("info", CMD_ARGV[0]) == 0)
mem_type = 1;
else if (strcmp("main", CMD_ARGV[0]) == 0)
mem_type = 0;
else
return ERROR_COMMAND_SYNTAX_ERROR;
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], first);
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], last);
for (unsigned int i = first; i <= last; i++) {
retval = niietcm4_uflash_page_erase(bank, i, mem_type);
if (retval != ERROR_OK)
return retval;
}for (unsigned int i = first; i <= last; i++) { ... }
command_print(CMD, "Erase %s userflash pages %u through %u done!", CMD_ARGV[0], first, last);
return retval;
}{ ... }
COMMAND_HANDLER(niietcm4_handle_uflash_protect_check_command)
{
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
CMD_ARGC--;
CMD_ARGV++;
int mem_type;
if (strcmp("info", CMD_ARGV[0]) == 0)
mem_type = 1;
else if (strcmp("main", CMD_ARGV[0]) == 0)
mem_type = 0;
else
return ERROR_COMMAND_SYNTAX_ERROR;
int i, j;
uint32_t uflash_addr;
uint32_t uflash_cmd;
uint32_t uflash_data;
if (mem_type == INFO_MEM_TYPE) {
uflash_addr = INFOWORD3_ADDR;
uflash_cmd = UFMC_MAGIC_KEY | UFMC_READ_IFB;
retval = target_write_u32(target, UFMA, uflash_addr);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_uopstatus_check(bank);
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, UFMD, &uflash_data);
if (retval != ERROR_OK)
return retval;
if (uflash_data & INFOWORD3_LOCK_IFB_UF)
command_print(CMD, "All sectors of info userflash are not protected!");
else
command_print(CMD, "All sectors of info userflash are protected!");
}if (mem_type == INFO_MEM_TYPE) { ... } else {
uflash_addr = UF_LOCK_ADDR;
uflash_cmd = UFMC_MAGIC_KEY | UFMC_READ_IFB;
for (i = 0; i < USERFLASH_PAGE_TOTALNUM/8; i++) {
retval = target_write_u32(target, UFMA, uflash_addr);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_uopstatus_check(bank);
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, UFMD, &uflash_data);
if (retval != ERROR_OK)
return retval;
for (j = 0; j < 8; j++) {
if (uflash_data & 0x1)
command_print(CMD, "Userflash sector #%03d: 0x%04x (0x100) is not protected!",
i*8+j, (i*8+j)*USERFLASH_PAGE_SIZE);
else
command_print(CMD, "Userflash sector #%03d: 0x%04x (0x100) is protected!",
i*8+j, (i*8+j)*USERFLASH_PAGE_SIZE);
uflash_data = uflash_data >> 1;
}for (j = 0; j < 8; j++) { ... }
uflash_addr++;
}for (i = 0; i < USERFLASH_PAGE_TOTALNUM/8; i++) { ... }
}else { ... }
return retval;
}{ ... }
COMMAND_HANDLER(niietcm4_handle_uflash_protect_command)
{
if (CMD_ARGC < 5)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
CMD_ARGC--;
CMD_ARGV++;
int mem_type;
if (strcmp("info", CMD_ARGV[0]) == 0)
mem_type = 1;
else if (strcmp("main", CMD_ARGV[0]) == 0)
mem_type = 0;
else
return ERROR_COMMAND_SYNTAX_ERROR;
unsigned int first, last;
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], first);
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], last);
int set;
if (strcmp("on", CMD_ARGV[3]) == 0) {
command_print(CMD, "Try to enable %s userflash sectors %u through %u protection. Please wait ... ",
CMD_ARGV[0], first, last);
set = 1;
}if (strcmp("on", CMD_ARGV[3]) == 0) { ... } else if (strcmp("off", CMD_ARGV[3]) == 0) {
command_print(CMD, "Try to disable %s userflash sectors %u through %u protection. Please wait ... ",
CMD_ARGV[0], first, last);
set = 0;
}else if (strcmp("off", CMD_ARGV[3]) == 0) { ... } else
return ERROR_COMMAND_SYNTAX_ERROR;
retval = niietcm4_uflash_protect(bank, mem_type, set, first, last);
if (retval != ERROR_OK)
return retval;
command_print(CMD, "done!");
return retval;
}{ ... }
COMMAND_HANDLER(niietcm4_handle_bflash_info_remap_command)
{
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
CMD_ARGC--;
CMD_ARGV++;
int set;
if (strcmp("on", CMD_ARGV[0]) == 0) {
command_print(CMD, "Try to enable bootflash info region remap. Please wait ...");
set = 1;
}if (strcmp("on", CMD_ARGV[0]) == 0) { ... } else if (strcmp("off", CMD_ARGV[0]) == 0) {
command_print(CMD, "Try to disable bootflash info region remap. Please wait ...");
set = 0;
}else if (strcmp("off", CMD_ARGV[0]) == 0) { ... } else
return ERROR_COMMAND_SYNTAX_ERROR;
uint32_t uflash_dump[USERFLASH_PAGE_SIZE];
niietcm4_dump_uflash_page(bank, uflash_dump, 0, 1);
if (set)
uflash_dump[INFOWORD0_ADDR] &= ~INFOWORD0_BOOTFROM_IFB;
else
uflash_dump[INFOWORD0_ADDR] |= INFOWORD0_BOOTFROM_IFB;
niietcm4_uflash_page_erase(bank, 0, 1);
niietcm4_load_uflash_page(bank, uflash_dump, 0, 1);
command_print(CMD, "done!");
return retval;
}{ ... }
COMMAND_HANDLER(niietcm4_handle_extmem_cfg_command)
{
if (CMD_ARGC < 4)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
CMD_ARGC--;
CMD_ARGV++;
uint32_t port;
if (strcmp("gpioa", CMD_ARGV[0]) == 0)
port = 8;
else if (strcmp("gpiob", CMD_ARGV[0]) == 0)
port = 9;
else if (strcmp("gpioc", CMD_ARGV[0]) == 0)
port = 10;
else if (strcmp("gpiod", CMD_ARGV[0]) == 0)
port = 11;
else if (strcmp("gpioe", CMD_ARGV[0]) == 0)
port = 12;
else if (strcmp("gpiof", CMD_ARGV[0]) == 0)
port = 13;
else if (strcmp("gpiog", CMD_ARGV[0]) == 0)
port = 14;
else if (strcmp("gpioh", CMD_ARGV[0]) == 0)
port = 15;
else
return ERROR_COMMAND_SYNTAX_ERROR;
uint32_t pin;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pin);
if (pin > 15)
return ERROR_COMMAND_SYNTAX_ERROR;
uint32_t func;
if (strcmp("func1", CMD_ARGV[2]) == 0)
func = 0;
else if (strcmp("func3", CMD_ARGV[2]) == 0)
func = 3;
else
return ERROR_COMMAND_SYNTAX_ERROR;
command_print(CMD, "Try to configure external memory boot interface:\n"
"port = %s\n"
"pin = %s\n"
"func = %s\n"
"Please wait ...", CMD_ARGV[0], CMD_ARGV[1], CMD_ARGV[2]);
uint32_t uflash_dump[USERFLASH_PAGE_SIZE];
niietcm4_dump_uflash_page(bank, uflash_dump, 0, 1);
uflash_dump[INFOWORD0_ADDR] &= ~(3<<INFOWORD0_EXTMEM_SEL_POS);
uflash_dump[INFOWORD0_ADDR] |= func<<INFOWORD0_EXTMEM_SEL_POS;
uflash_dump[INFOWORD1_ADDR] = (port<<INFOWORD1_PORTNUM_POS) | (pin<<INFOWORD1_PINNUM_POS);
niietcm4_uflash_page_erase(bank, 0, 1);
niietcm4_load_uflash_page(bank, uflash_dump, 0, 1);
command_print(CMD, "done!");
return retval;
}{ ... }
COMMAND_HANDLER(niietcm4_handle_extmem_boot_command)
{
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
CMD_ARGC--;
CMD_ARGV++;
int set;
if (strcmp("on", CMD_ARGV[0]) == 0) {
command_print(CMD, "Try to enable boot from external memory. Please wait ...");
set = 1;
}if (strcmp("on", CMD_ARGV[0]) == 0) { ... } else if (strcmp("off", CMD_ARGV[0]) == 0) {
command_print(CMD, "Try to disable boot from external memory. Please wait ...");
set = 0;
}else if (strcmp("off", CMD_ARGV[0]) == 0) { ... } else
return ERROR_COMMAND_SYNTAX_ERROR;
uint32_t uflash_dump[USERFLASH_PAGE_SIZE];
niietcm4_dump_uflash_page(bank, uflash_dump, 0, 1);
if (set)
uflash_dump[INFOWORD0_ADDR] &= ~INFOWORD0_EN_GPIO;
else
uflash_dump[INFOWORD0_ADDR] |= INFOWORD0_EN_GPIO;
niietcm4_uflash_page_erase(bank, 0, 1);
niietcm4_load_uflash_page(bank, uflash_dump, 0, 1);
command_print(CMD, "done!");
return retval;
}{ ... }
COMMAND_HANDLER(niietcm4_handle_service_mode_erase_command)
{
if (CMD_ARGC < 1)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct target *target = bank->target;
command_print(CMD, "Try to perform service mode erase. Please wait ...");
retval = target_write_u32(target, SERVICE_MODE_ERASE_ADDR, 1);
if (retval != ERROR_OK)
return retval;
int timeout = 500;
uint32_t status;
retval = target_read_u32(target, SERVICE_MODE_ERASE_ADDR, &status);
if (retval != ERROR_OK)
return retval;
while (status != 0x03) {
retval = target_read_u32(target, SERVICE_MODE_ERASE_ADDR, &status);
if (retval != ERROR_OK)
return retval;
if (timeout-- <= 0) {
LOG_ERROR("Service mode erase timeout");
return ERROR_FLASH_OPERATION_FAILED;
}if (timeout-- <= 0) { ... }
busy_sleep(1);
}while (status != 0x03) { ... }
command_print(CMD, "done! All data erased.");
return retval;
}{ ... }
COMMAND_HANDLER(niietcm4_handle_driver_info_command)
{
if (CMD_ARGC < 1)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
command_print(CMD, "niietcm4 flash driver\n"
"version: %d.%d\n"
"author: Bogdan Kolbov\n"
"mail: kolbov@niiet.ru",
FLASH_DRIVER_VER>>16,
FLASH_DRIVER_VER&0xFFFF);
return retval;
}{ ... }
static const struct command_registration niietcm4_exec_command_handlers[] = {
{
.name = "uflash_read_byte",
.handler = niietcm4_handle_uflash_read_byte_command,
.mode = COMMAND_EXEC,
.usage = "bank_id ('main'|'info') address",
.help = "Read byte from main or info userflash region",
...},
{
.name = "uflash_write_byte",
.handler = niietcm4_handle_uflash_write_byte_command,
.mode = COMMAND_EXEC,
.usage = "bank_id ('main'|'info') address value",
.help = "Write byte to main or info userflash region",
...},
{
.name = "uflash_full_erase",
.handler = niietcm4_handle_uflash_full_erase_command,
.mode = COMMAND_EXEC,
.usage = "bank_id",
.help = "Erase all userflash including info region",
...},
{
.name = "uflash_erase",
.handler = niietcm4_handle_uflash_erase_command,
.mode = COMMAND_EXEC,
.usage = "bank_id ('main'|'info') first_sector_num last_sector_num",
.help = "Erase sectors of main or info userflash region, starting at sector first up to and including last.",
...},
{
.name = "uflash_protect_check",
.handler = niietcm4_handle_uflash_protect_check_command,
.mode = COMMAND_EXEC,
.usage = "bank_id ('main'|'info')",
.help = "Check sectors protect.",
...},
{
.name = "uflash_protect",
.handler = niietcm4_handle_uflash_protect_command,
.mode = COMMAND_EXEC,
.usage = "bank_id ('main'|'info') first_sector_num last_sector_num ('on'|'off')",
.help = "Protect sectors of main or info userflash region, starting at sector first up to and including last.",
...},
{
.name = "bflash_info_remap",
.handler = niietcm4_handle_bflash_info_remap_command,
.mode = COMMAND_EXEC,
.usage = "bank_id ('on'|'off')",
.help = "Enable remapping bootflash info region to 0x00000000 (or 0x40000000 if external memory boot used).",
...},
{
.name = "extmem_cfg",
.handler = niietcm4_handle_extmem_cfg_command,
.mode = COMMAND_EXEC,
.usage = "bank_id ('gpioa'|'gpiob'|'gpioc'|'gpiod'|'gpioe'|'gpiof'|'gpiog'|'gpioh') pin_num ('func1'|'func3')",
.help = "Configure external memory interface for boot.",
...},
{
.name = "extmem_boot",
.handler = niietcm4_handle_extmem_boot_command,
.mode = COMMAND_EXEC,
.usage = "bank_id ('on'|'off')",
.help = "Enable boot from external memory.",
...},
{
.name = "service_mode_erase",
.handler = niietcm4_handle_service_mode_erase_command,
.mode = COMMAND_EXEC,
.usage = "bank_id",
.help = "Perform emergency erase of all flash (bootflash and userflash).",
...},
{
.name = "driver_info",
.handler = niietcm4_handle_driver_info_command,
.mode = COMMAND_EXEC,
.usage = "bank_id",
.help = "Show information about flash driver.",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration niietcm4_command_handlers[] = {
{
.name = "niietcm4",
.mode = COMMAND_ANY,
.help = "niietcm4 flash command group",
.usage = "",
.chain = niietcm4_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
/* ... */
FLASH_BANK_COMMAND_HANDLER(niietcm4_flash_bank_command)
{
struct niietcm4_flash_bank *niietcm4_info;
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
niietcm4_info = malloc(sizeof(struct niietcm4_flash_bank));
bank->driver_priv = niietcm4_info;
niietcm4_info->probed = false;
niietcm4_info->chipid = 0;
niietcm4_info->chip_name = NULL;
niietcm4_info->uflash_width = 0;
niietcm4_info->uflash_size = 0;
niietcm4_info->uflash_pagetotal = 0;
niietcm4_info->uflash_info_size = 0;
niietcm4_info->uflash_info_pagetotal = 0;
niietcm4_info->bflash_info_remap = false;
niietcm4_info->extmem_boot_port = NULL;
niietcm4_info->extmem_boot_pin = 0;
niietcm4_info->extmem_boot_altfunc = 0;
niietcm4_info->extmem_boot = false;
return ERROR_OK;
}{ ... }
static int niietcm4_protect_check(struct flash_bank *bank)
{
struct target *target = bank->target;
struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
int retval = ERROR_FLASH_OPERATION_FAILED;
int set;
uint32_t uflash_addr;
uint32_t uflash_cmd;
uint32_t uflash_data;
if (niietcm4_info->bflash_info_remap) {
uflash_addr = INFOWORD2_ADDR;
uflash_cmd = UFMC_MAGIC_KEY | UFMC_READ_IFB;
retval = target_write_u32(target, UFMA, uflash_addr);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_uopstatus_check(bank);
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, UFMD, &uflash_data);
if (retval != ERROR_OK)
return retval;
if (uflash_data & INFOWORD2_LOCK_IFB_BF)
set = 0;
else
set = 1;
bank->sectors[0].is_protected = set;
}if (niietcm4_info->bflash_info_remap) { ... } else {
uflash_addr = BF_LOCK_ADDR;
uflash_cmd = UFMC_MAGIC_KEY | UFMC_READ_IFB;
for (unsigned int i = 0; i < bank->num_sectors/8; i++) {
retval = target_write_u32(target, UFMA, uflash_addr);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_uopstatus_check(bank);
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, UFMD, &uflash_data);
if (retval != ERROR_OK)
return retval;
for (int j = 0; j < 8; j++) {
if (uflash_data & 0x1)
set = 0;
else
set = 1;
bank->sectors[i*8+j].is_protected = set;
uflash_data = uflash_data >> 1;
}for (int j = 0; j < 8; j++) { ... }
uflash_addr++;
}for (unsigned int i = 0; i < bank->num_sectors/8; i++) { ... }
}else { ... }
return retval;
}{ ... }
static int niietcm4_mass_erase(struct flash_bank *bank)
{
struct target *target = bank->target;
int retval;
uint32_t flash_cmd;
flash_cmd = FMC_MAGIC_KEY | FMC_FULL_ERASE;
retval = target_write_u32(target, FMC, flash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_opstatus_check(bank);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
static int niietcm4_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
struct target *target = bank->target;
struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
int retval = ERROR_FLASH_OPERATION_FAILED;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if ((first == 0) && (last == (bank->num_sectors - 1))) {
retval = niietcm4_mass_erase(bank);
return retval;
}if ((first == 0) && (last == (bank->num_sectors - 1))) { ... }
uint32_t flash_cmd, flash_addr;
if (niietcm4_info->bflash_info_remap)
flash_cmd = FMC_MAGIC_KEY | FMC_PAGEERASE_IFB;
else
flash_cmd = FMC_MAGIC_KEY | FMC_PAGE_ERASE;
unsigned int page_size = bank->size / bank->num_sectors;
for (unsigned int i = first; i <= last; i++) {
flash_addr = i*page_size;
retval = target_write_u32(target, FMA, flash_addr);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, FMC, flash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_opstatus_check(bank);
if (retval != ERROR_OK)
return retval;
}for (unsigned int i = first; i <= last; i++) { ... }
return retval;
}{ ... }
static int niietcm4_protect(struct flash_bank *bank, int set,
unsigned int first, unsigned int last)
{
struct target *target = bank->target;
struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
LOG_INFO("Please wait ...");
if (niietcm4_info->bflash_info_remap) {
uint32_t uflash_dump[USERFLASH_PAGE_SIZE];
retval = niietcm4_dump_uflash_page(bank, uflash_dump, 0, 1);
if (retval != ERROR_OK)
return retval;
if (set)
uflash_dump[INFOWORD2_ADDR] &= ~INFOWORD2_LOCK_IFB_BF;
else
uflash_dump[INFOWORD2_ADDR] |= INFOWORD2_LOCK_IFB_BF;
retval = niietcm4_uflash_page_erase(bank, 0, 1);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_load_uflash_page(bank, uflash_dump, 0, 1);
if (retval != ERROR_OK)
return retval;
}if (niietcm4_info->bflash_info_remap) { ... } else {
uint32_t uflash_dump[USERFLASH_PAGE_SIZE];
retval = niietcm4_dump_uflash_page(bank, uflash_dump, 0, 1);
if (retval != ERROR_OK)
return retval;
for (unsigned int i = first; i <= last; i++) {
uint32_t reg_num = i/8;
uint32_t bit_num = i%8;
if (set)
uflash_dump[BF_LOCK_ADDR+reg_num] &= ~(1<<bit_num);
else
uflash_dump[BF_LOCK_ADDR+reg_num] |= (1<<bit_num);
}for (unsigned int i = first; i <= last; i++) { ... }
retval = niietcm4_uflash_page_erase(bank, 0, 1);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_load_uflash_page(bank, uflash_dump, 0, 1);
if (retval != ERROR_OK)
return retval;
}else { ... }
return retval;
}{ ... }
static int niietcm4_write_block(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
struct target *target = bank->target;
struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
uint32_t buffer_size = 32768 + 8;
struct working_area *write_algorithm;
struct working_area *source;
uint32_t address = bank->base + offset;
struct reg_param reg_params[5];
struct armv7m_algorithm armv7m_info;
int retval = ERROR_OK;
static const uint8_t niietcm4_flash_write_code[] = {
0x14, 0x4f, 0x16, 0x68, 0x00, 0x2e, 0x23, 0xd0, 0x55, 0x68, 0xb5, 0x42, 0xf9, 0xd0, 0x2e, 0x68,
0x7e, 0x60, 0x04, 0x35, 0x2e, 0x68, 0x3e, 0x65, 0x04, 0x35, 0x2e, 0x68, 0x7e, 0x65, 0x04, 0x35,
0x2e, 0x68, 0xbe, 0x65, 0x04, 0x35, 0x3c, 0x60, 0x10, 0x34, 0xb8, 0x60, 0xfe, 0x68, 0x00, 0x2e,
0xfc, 0xd0, 0x02, 0x2e, 0x0a, 0xd0, 0x01, 0x26, 0x7e, 0x61, 0x9d, 0x42, 0x01, 0xd3, 0x15, 0x46,
0x08, 0x35, 0x55, 0x60, 0x01, 0x39, 0x00, 0x29, 0x02, 0xd0, 0xda, 0xe7, 0x00, 0x20, 0x50, 0x60,
0x30, 0x46, 0x00, 0xbe, 0x00, 0xc0, 0x01, 0xa0
...};
if (target_alloc_working_area(target, sizeof(niietcm4_flash_write_code),
&write_algorithm) != ERROR_OK) {
LOG_WARNING("no working area available, can't do block memory writes");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (target_alloc_working_area(target, sizeof(niietcm4_flash_write_code), &write_algorithm) != ERROR_OK) { ... }
retval = target_write_buffer(target, write_algorithm->address,
sizeof(niietcm4_flash_write_code), niietcm4_flash_write_code);
if (retval != ERROR_OK)
return retval;
while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) {
buffer_size /= 2;
buffer_size &= ~15UL;
buffer_size += 8;
if (buffer_size <= 256) {
/* ... */
target_free_working_area(target, write_algorithm);
LOG_WARNING("no large enough working area available, can't do block memory writes");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (buffer_size <= 256) { ... }
}while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) { ... }
init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT);
init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
init_reg_param(®_params[4], "r4", 32, PARAM_IN_OUT);
uint32_t flash_cmd;
if (niietcm4_info->bflash_info_remap)
flash_cmd = FMC_MAGIC_KEY | FMC_WRITE_IFB;
else
flash_cmd = FMC_MAGIC_KEY | FMC_WRITE;
buf_set_u32(reg_params[0].value, 0, 32, flash_cmd);
buf_set_u32(reg_params[1].value, 0, 32, count);
buf_set_u32(reg_params[2].value, 0, 32, source->address);
buf_set_u32(reg_params[3].value, 0, 32, source->address + source->size);
buf_set_u32(reg_params[4].value, 0, 32, address);
armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
armv7m_info.core_mode = ARM_MODE_THREAD;
retval = target_run_flash_async_algorithm(target, buffer, count, 16,
0, NULL,
5, reg_params,
source->address, source->size,
write_algorithm->address, 0,
&armv7m_info);
if (retval == ERROR_FLASH_OPERATION_FAILED)
LOG_ERROR("flash write failed at address 0x%"PRIx32,
buf_get_u32(reg_params[4].value, 0, 32));
target_free_working_area(target, source);
target_free_working_area(target, write_algorithm);
destroy_reg_param(®_params[0]);
destroy_reg_param(®_params[1]);
destroy_reg_param(®_params[2]);
destroy_reg_param(®_params[3]);
destroy_reg_param(®_params[4]);
return retval;
}{ ... }
static int niietcm4_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
struct target *target = bank->target;
struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
uint8_t *new_buffer = NULL;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (offset & 0xF) {
LOG_ERROR("offset 0x%" PRIx32 " breaks required 4-word alignment", offset);
return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
}if (offset & 0xF) { ... }
/* ... */
int rem = count % 16;
if (rem) {
new_buffer = malloc(count + 16 - rem);
if (!new_buffer) {
LOG_ERROR("Odd number of words to write and no memory for padding buffer");
return ERROR_FAIL;
}if (!new_buffer) { ... }
LOG_INFO("Odd number of words to write, padding with 0xFFFFFFFF");
buffer = memcpy(new_buffer, buffer, count);
while (rem < 16) {
new_buffer[count++] = 0xff;
rem++;
}while (rem < 16) { ... }
}if (rem) { ... }
int retval;
retval = niietcm4_write_block(bank, buffer, offset, count/16);
uint32_t flash_addr, flash_cmd, flash_data;
if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
/* ... */
LOG_WARNING("Can't use block writes, falling back to single memory accesses");
LOG_INFO("Please wait ...");
if (niietcm4_info->bflash_info_remap)
flash_cmd = FMC_MAGIC_KEY | FMC_WRITE_IFB;
else
flash_cmd = FMC_MAGIC_KEY | FMC_WRITE;
for (unsigned int i = 0; i < count; i += 16) {
LOG_INFO("%u byte of %" PRIu32, i, count);
flash_addr = offset + i;
retval = target_write_u32(target, FMA, flash_addr);
if (retval != ERROR_OK)
goto free_buffer;
uint32_t value[4];
memcpy(&value, buffer + i*16, 4*sizeof(uint32_t));
flash_data = value[0];
retval = target_write_u32(target, FMD1, flash_data);
if (retval != ERROR_OK)
goto free_buffer;
flash_data = value[1];
retval = target_write_u32(target, FMD2, flash_data);
if (retval != ERROR_OK)
goto free_buffer;
flash_data = value[2];
retval = target_write_u32(target, FMD3, flash_data);
if (retval != ERROR_OK)
goto free_buffer;
flash_data = value[3];
retval = target_write_u32(target, FMD4, flash_data);
if (retval != ERROR_OK)
goto free_buffer;
retval = target_write_u32(target, FMC, flash_cmd);
if (retval != ERROR_OK)
goto free_buffer;
retval = niietcm4_opstatus_check(bank);
if (retval != ERROR_OK)
goto free_buffer;
}for (unsigned int i = 0; i < count; i += 16) { ... }
}if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) { ... }
free_buffer:
free(new_buffer);
return retval;
}{ ... }
static int niietcm4_probe_k1921vk01t(struct flash_bank *bank)
{
struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
struct target *target = bank->target;
int retval;
niietcm4_info->chip_name = "K1921VK01T";
uint32_t service_mode;
retval = target_read_u32(target, 0x80017000, &service_mode);
if (retval != ERROR_OK)
return retval;
service_mode = (service_mode>>2) & 0x1;
if (!service_mode) {
niietcm4_info->uflash_width = 8;
niietcm4_info->uflash_size = 0x10000;
niietcm4_info->uflash_pagetotal = 256;
niietcm4_info->uflash_info_size = 0x200;
niietcm4_info->uflash_info_pagetotal = 2;
uint32_t uflash_data[2];
uint32_t uflash_cmd = UFMC_MAGIC_KEY | UFMC_READ_IFB;
for (int i = 0; i < 2; i++) {
retval = target_write_u32(target, UFMA, i);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, UFMC, uflash_cmd);
if (retval != ERROR_OK)
return retval;
retval = niietcm4_uopstatus_check(bank);
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, UFMD, &uflash_data[i]);
if (retval != ERROR_OK)
return retval;
}for (int i = 0; i < 2; i++) { ... }
int boot_from_ifb = (uflash_data[0]>>INFOWORD0_BOOTFROM_IFB_POS) & 0x1;
int en_gpio = (uflash_data[0]>>INFOWORD0_EN_GPIO_POS) & 0x1;
int extmem_sel = (uflash_data[0]>>INFOWORD0_EXTMEM_SEL_POS) & 0x3;
int pinnum = (uflash_data[1]>>INFOWORD1_PINNUM_POS) & 0xF;
int portnum = (uflash_data[1]>>INFOWORD1_PORTNUM_POS) & 0x7;
if (boot_from_ifb)
niietcm4_info->bflash_info_remap = false;
else
niietcm4_info->bflash_info_remap = true;
if (extmem_sel == 0x2)
niietcm4_info->extmem_boot_altfunc = 3;
else
niietcm4_info->extmem_boot_altfunc = 1;
if (portnum == 0x0)
niietcm4_info->extmem_boot_port = "GPIOA";
else if (portnum == 0x1)
niietcm4_info->extmem_boot_port = "GPIOB";
else if (portnum == 0x2)
niietcm4_info->extmem_boot_port = "GPIOC";
else if (portnum == 0x3)
niietcm4_info->extmem_boot_port = "GPIOD";
else if (portnum == 0x4)
niietcm4_info->extmem_boot_port = "GPIOE";
else if (portnum == 0x5)
niietcm4_info->extmem_boot_port = "GPIOF";
else if (portnum == 0x6)
niietcm4_info->extmem_boot_port = "GPIOG";
else if (portnum == 0x7)
niietcm4_info->extmem_boot_port = "GPIOH";
else
niietcm4_info->extmem_boot_port = "not defined";
if (en_gpio)
niietcm4_info->extmem_boot = false;
else
niietcm4_info->extmem_boot = true;
niietcm4_info->extmem_boot_pin = pinnum;
uint32_t extmem_boot_port_data;
retval = target_read_u32(target, 0x80010000 + 0x1000*portnum, &extmem_boot_port_data);
if (retval != ERROR_OK)
return retval;
int extmem_boot_pin_data = (extmem_boot_port_data>>pinnum) & 0x1;
uint32_t extmem_base;
uint32_t bflash_base;
if (extmem_boot_pin_data && niietcm4_info->extmem_boot) {
extmem_base = 0x00000000;
bflash_base = 0x40000000;
}if (extmem_boot_pin_data && niietcm4_info->extmem_boot) { ... } else {
extmem_base = 0x40000000;
bflash_base = 0x00000000;
}else { ... }
uint32_t bflash_size = 0x100000;
uint32_t bflash_pages = 128;
uint32_t bflash_info_size = 0x2000;
uint32_t bflash_info_pages = 1;
if (niietcm4_info->bflash_info_remap) {
bflash_base += 0x2000;
bflash_size -= 0x2000;
bflash_pages--;
bank->size = bflash_info_size;
bank->num_sectors = bflash_info_pages;
}if (niietcm4_info->bflash_info_remap) { ... } else {
bank->size = bflash_size;
bank->num_sectors = bflash_pages;
}else { ... }
char info_bootflash_addr_str[64];
if (niietcm4_info->bflash_info_remap)
snprintf(info_bootflash_addr_str, sizeof(info_bootflash_addr_str),
TARGET_ADDR_FMT " base address", bank->base);
else
snprintf(info_bootflash_addr_str, sizeof(info_bootflash_addr_str),
"not mapped to global address space");
snprintf(niietcm4_info->chip_brief,
sizeof(niietcm4_info->chip_brief),
"\n"
"MEMORY CONFIGURATION\n"
"Bootflash :\n"
" %" PRIu32 " kB total\n"
" %" PRIu32 " pages %" PRIu32 " kB each\n"
" 0x%08" PRIx32 " base address\n"
"%s"
"Info bootflash :\n"
" %" PRIu32 " kB total\n"
" %" PRIu32 " pages %" PRIu32 " kB each\n"
" %s\n"
"%s"
"Userflash :\n"
" %" PRIu32 " kB total\n"
" %" PRIu32 " pages %" PRIu32 " B each\n"
" %" PRIu32 " bit cells\n"
" not mapped to global address space\n"
"Info userflash :\n"
" %" PRIu32 " B total\n"
" %" PRIu32 " pages of %" PRIu32 " B each\n"
" %" PRIu32 " bit cells\n"
" not mapped to global address space\n"
"RAM :\n"
" 192 kB total\n"
" 0x20000000 base address\n"
"External memory :\n"
" 8/16 bit address space\n"
" 0x%08" PRIx32 " base address\n"
"\n"
"INFOWORD STATUS\n"
"Bootflash info region remap :\n"
" %s\n"
"External memory boot port :\n"
" %s\n"
"External memory boot pin :\n"
" %" PRIu32 "\n"
"External memory interface alternative function :\n"
" %" PRIu32 "\n"
"Option boot from external memory :\n"
" %s\n",
bflash_size/1024,
bflash_pages,
(bflash_size/bflash_pages)/1024,
bflash_base,
niietcm4_info->bflash_info_remap ? "" : " this flash will be used for debugging, writing and etc\n",
bflash_info_size/1024,
bflash_info_pages,
(bflash_info_size/bflash_info_pages)/1024,
info_bootflash_addr_str,
niietcm4_info->bflash_info_remap ? " this flash will be used for debugging, writing and etc\n" : "",
niietcm4_info->uflash_size/1024,
niietcm4_info->uflash_pagetotal,
niietcm4_info->uflash_size/niietcm4_info->uflash_pagetotal,
niietcm4_info->uflash_width,
niietcm4_info->uflash_info_size,
niietcm4_info->uflash_info_pagetotal,
niietcm4_info->uflash_info_size/niietcm4_info->uflash_info_pagetotal,
niietcm4_info->uflash_width,
extmem_base,
niietcm4_info->bflash_info_remap ? "enable" : "disable",
niietcm4_info->extmem_boot_port,
niietcm4_info->extmem_boot_pin,
niietcm4_info->extmem_boot_altfunc,
niietcm4_info->extmem_boot ? "enable" : "disable");
}if (!service_mode) { ... } else {
bank->size = 0x100000;
bank->num_sectors = 128;
sprintf(niietcm4_info->chip_brief,
"\n"
"H[2] was HIGH while startup. Device entered service mode.\n"
"All flashes were locked.\n"
"If you want to perform emergency erase (erase all flashes),\n"
"please use \"service_mode_erase\" command and reset device.\n"
"Do not forget to pull H[2] down while reset for returning to normal operation mode.\n"
);
}else { ... }
return retval;
}{ ... }
static int niietcm4_probe(struct flash_bank *bank)
{
struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
struct target *target = bank->target;
free(bank->sectors);
bank->sectors = NULL;
uint32_t retval;
uint32_t chipid;
retval = target_read_u32(target, CHIPID_ADDR, &chipid);
if (retval != ERROR_OK) {
chipid = K1921VK01T_ID;
LOG_INFO("unknown chipid, assuming K1921VK01T");
}if (retval != ERROR_OK) { ... }
if (chipid == K1921VK01T_ID)
niietcm4_probe_k1921vk01t(bank);
int page_total = bank->num_sectors;
int page_size = bank->size / page_total;
bank->sectors = malloc(sizeof(struct flash_sector) * page_total);
for (int i = 0; i < page_total; i++) {
bank->sectors[i].offset = i * page_size;
bank->sectors[i].size = page_size;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = -1;
}for (int i = 0; i < page_total; i++) { ... }
niietcm4_info->probed = true;
return ERROR_OK;
}{ ... }
static int niietcm4_auto_probe(struct flash_bank *bank)
{
struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
if (niietcm4_info->probed)
return ERROR_OK;
return niietcm4_probe(bank);
}{ ... }
static int get_niietcm4_info(struct flash_bank *bank, struct command_invocation *cmd)
{
struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
command_print_sameline(cmd, "\nNIIET Cortex-M4F %s\n%s",
niietcm4_info->chip_name, niietcm4_info->chip_brief);
return ERROR_OK;
}{ ... }
const struct flash_driver niietcm4_flash = {
.name = "niietcm4",
.usage = "flash bank <name> niietcm4 <base> <size> 0 0 <target#>",
.commands = niietcm4_command_handlers,
.flash_bank_command = niietcm4_flash_bank_command,
.erase = niietcm4_erase,
.protect = niietcm4_protect,
.write = niietcm4_write,
.read = default_flash_read,
.probe = niietcm4_probe,
.auto_probe = niietcm4_auto_probe,
.erase_check = default_flash_blank_check,
.protect_check = niietcm4_protect_check,
.info = get_niietcm4_info,
.free_driver_priv = default_flash_free_driver_priv,
...};