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
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
146
147
148
153
154
155
156
160
161
162
163
164
165
166
167
171
172
173
178
179
180
181
185
186
187
188
189
190
191
192
193
194
195
196
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
240
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
284
285
286
287
288
289
290
291
292
295
296
297
298
299
300
301
302
303
304
311
312
314
315
316
317
318
319
324
325
326
330
333
334
335
336
337
338
339
344
345
346
347
348
349
350
353
354
355
356
357
358
359
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
398
399
400
401
402
403
404
407
408
409
410
411
412
413
414
415
416
422
423
424
425
426
427
428
436
437
438
439
440
441
442
443
444
445
446
447
448
451
452
456
457
458
459
460
461
462
466
467
468
472
473
474
475
476
477
478
479
480
481
482
483
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
514
515
516
517
518
519
520
521
522
523
526
527
528
529
530
533
534
535
536
540
541
545
546
547
548
549
550
551
552
553
554
555
559
560
561
562
563
564
565
566
567
568
569
570
571
574
575
581
582
583
584
585
586
587
591
592
593
594
595
599
600
604
605
606
612
613
614
615
616
619
620
621
622
626
627
634
635
639
640
641
642
643
644
645
646
647
648
649
650
651
652
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
781
782
783
784
785
786
787
788
789
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
860
861
862
863
864
865
866
867
868
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
911
912
913
914
915
916
917
918
919
920
921
922
923
935
936
937
938
939
940
941
942
943
944
945
946
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
973
974
975
976
977
978
982
983
984
985
986
987
988
989
990
991
992
993
997
998
999
1000
1001
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
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
1053
1054
1071
1072
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
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
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
1200
1201
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
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "jtag/interface.h"
#include "imp.h"
#include <helper/binarybuffer.h>
#include <target/algorithm.h>
#include <target/arm_adi_v5.h>
#include <target/armv7m.h>
#include <target/cortex_m.h>
7 includes
#define SIM_SRSID 0x40048000
#define ICS_C1 0x40064000
#define ICS_C2 0x40064001
#define ICS_C3 0x40064002
#define ICS_C4 0x40064003
#define ICS_S 0x40064004
#define SIM_BUSDIV 0x40048018
#define SIM_CLKDIV_KE06 0x40048024
#define SIM_CLKDIV_KE04_44_64_80 0x40048024
#define SIM_CLKDIV_KE04_16_20_24 0x4004801C
#define WDOG_CS1 0x40052000
#define ICS_C2_BDIV_MASK 0xE0
#define ICS_C2_BDIV_SHIFT 5
#define ICS_C2_BDIV(x) (((uint8_t)(((uint8_t)(x))<<ICS_C2_BDIV_SHIFT))&ICS_C2_BDIV_MASK)
#define ICS_S_LOCK_MASK 0x40
#define ICS_C4_SCFTRIM_MASK 0x1
#define SIM_CLKDIV_OUTDIV2_MASK 0x1000000
#define FTMRX_FCLKDIV_FDIV_MASK 0x3F
#define FTMRX_FCLKDIV_FDIV_SHIFT 0
#define FTMRX_FCLKDIV_FDIV(x) (((uint8_t)(((uint8_t)(x))<<FTMRX_FCLKDIV_FDIV_SHIFT))&FTMRX_FCLKDIV_FDIV_MASK)
#define FTMRX_FCLKDIV_FDIVLCK_MASK 0x40
#define FTMRX_FCLKDIV_FDIVLCK_SHIFT 6
#define FTMRX_FCLKDIV_FDIVLD_MASK 0x80
#define FTMRX_FCLKDIV_FDIVLD_SHIFT 7
#define FTMRX_FSTAT_CCIF_MASK 0x80
#define FTMRX_FSTAT_MGSTAT0_MASK 0x01
#define FTMRX_FSTAT_MGSTAT1_MASK 0x02
#define FTMRX_CMD_ALLERASED 0x01
#define FTMRX_CMD_BLOCKERASED 0x02
#define FTMRX_CMD_SECTIONERASED 0x03
#define FTMRX_CMD_READONCE 0x04
#define FTMRX_CMD_PROGFLASH 0x06
#define FTMRX_CMD_PROGONCE 0x07
#define FTMRX_CMD_ERASEALL 0x08
#define FTMRX_CMD_ERASEBLOCK 0x09
#define FTMRX_CMD_ERASESECTOR 0x0A
#define FTMRX_CMD_UNSECURE 0x0B
#define FTMRX_CMD_VERIFYACCESS 0x0C
#define FTMRX_CMD_SETMARGINLVL 0x0D
#define FTMRX_CMD_SETFACTORYLVL 0x0E
#define FTMRX_CMD_CONFIGNVM 0x0F
#define FTMRX_ERROR_ACCERR 0x20
#define FTMRX_ERROR_FPVIOL 0x10
#define KINETIS_KE_SRSID_FAMID(x) ((x >> 28) & 0x0F)
#define KINETIS_KE_SRSID_SUBFAMID(x) ((x >> 24) & 0x0F)
#define KINETIS_KE_SRSID_PINCOUNT(x) ((x >> 16) & 0x0F)
#define KINETIS_KE_SRSID_KEX2 0x02
#define KINETIS_KE_SRSID_KEX4 0x04
#define KINETIS_KE_SRSID_KEX6 0x06
49 defines
struct kinetis_ke_flash_bank {
uint32_t sector_size;
uint32_t protection_size;
uint32_t sim_srsid;
uint32_t ftmrx_fclkdiv_addr;
uint32_t ftmrx_fccobix_addr;
uint32_t ftmrx_fstat_addr;
uint32_t ftmrx_fprot_addr;
uint32_t ftmrx_fccobhi_addr;
uint32_t ftmrx_fccoblo_addr;
...};
#define MDM_REG_STAT 0x00
#define MDM_REG_CTRL 0x04
#define MDM_REG_ID 0xfc
#define MDM_STAT_FMEACK (1<<0)
#define MDM_STAT_FREADY (1<<1)
#define MDM_STAT_SYSSEC (1<<2)
#define MDM_STAT_SYSRES (1<<3)
#define MDM_STAT_FMEEN (1<<5)
#define MDM_STAT_BACKDOOREN (1<<6)
#define MDM_STAT_LPEN (1<<7)
#define MDM_STAT_VLPEN (1<<8)
#define MDM_STAT_LLSMODEXIT (1<<9)
#define MDM_STAT_VLLSXMODEXIT (1<<10)
#define MDM_STAT_CORE_HALTED (1<<16)
#define MDM_STAT_CORE_SLEEPDEEP (1<<17)
#define MDM_STAT_CORESLEEPING (1<<18)
#define MEM_CTRL_FMEIP (1<<0)
#define MEM_CTRL_DBG_DIS (1<<1)
#define MEM_CTRL_DBG_REQ (1<<2)
#define MEM_CTRL_SYS_RES_REQ (1<<3)
#define MEM_CTRL_CORE_HOLD_RES (1<<4)
#define MEM_CTRL_VLLSX_DBG_REQ (1<<5)
#define MEM_CTRL_VLLSX_DBG_ACK (1<<6)
#define MEM_CTRL_VLLSX_STAT_ACK (1<<7)
#define MDM_ACCESS_TIMEOUT 3000
25 defines
static int kinetis_ke_mdm_write_register(struct adiv5_dap *dap, unsigned reg, uint32_t value)
{
LOG_DEBUG("MDM_REG[0x%02x] <- %08" PRIX32, reg, value);
struct adiv5_ap *ap = dap_get_ap(dap, 1);
if (!ap) {
LOG_DEBUG("MDM: failed to get AP");
return ERROR_FAIL;
}if (!ap) { ... }
int retval = dap_queue_ap_write(ap, reg, value);
if (retval != ERROR_OK) {
LOG_DEBUG("MDM: failed to queue a write request");
dap_put_ap(ap);
return retval;
}if (retval != ERROR_OK) { ... }
retval = dap_run(dap);
dap_put_ap(ap);
if (retval != ERROR_OK) {
LOG_DEBUG("MDM: dap_run failed");
return retval;
}if (retval != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int kinetis_ke_mdm_read_register(struct adiv5_dap *dap, unsigned reg, uint32_t *result)
{
struct adiv5_ap *ap = dap_get_ap(dap, 1);
if (!ap) {
LOG_DEBUG("MDM: failed to get AP");
return ERROR_FAIL;
}if (!ap) { ... }
int retval = dap_queue_ap_read(ap, reg, result);
if (retval != ERROR_OK) {
LOG_DEBUG("MDM: failed to queue a read request");
dap_put_ap(ap);
return retval;
}if (retval != ERROR_OK) { ... }
retval = dap_run(dap);
dap_put_ap(ap);
if (retval != ERROR_OK) {
LOG_DEBUG("MDM: dap_run failed");
return retval;
}if (retval != ERROR_OK) { ... }
LOG_DEBUG("MDM_REG[0x%02x]: %08" PRIX32, reg, *result);
return ERROR_OK;
}{ ... }
static int kinetis_ke_mdm_poll_register(struct adiv5_dap *dap, unsigned reg, uint32_t mask, uint32_t value)
{
uint32_t val;
int retval;
int timeout = MDM_ACCESS_TIMEOUT;
do {
retval = kinetis_ke_mdm_read_register(dap, reg, &val);
if (retval != ERROR_OK || (val & mask) == value)
return retval;
alive_sleep(1);
...} while (timeout--);
LOG_DEBUG("MDM: polling timed out");
return ERROR_FAIL;
}{ ... }
static int kinetis_ke_prepare_flash(struct flash_bank *bank)
{
struct target *target = bank->target;
struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
uint8_t c2, c3, c4, s = 0;
uint16_t trim_value = 0;
uint16_t timeout = 0;
uint32_t bus_clock = 0;
uint32_t bus_reg_val = 0;
uint32_t bus_reg_addr = 0;
uint32_t flash_clk_div;
uint8_t fclkdiv;
int result;
/* ... */
/* ... */
switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) {
case KINETIS_KE_SRSID_KEX2:
trim_value = 0x4C;
break;
case KINETIS_KE_SRSID_KEX2:
case KINETIS_KE_SRSID_KEX4:
trim_value = 0x54;
break;
case KINETIS_KE_SRSID_KEX4:
case KINETIS_KE_SRSID_KEX6:
trim_value = 0x58;
break;case KINETIS_KE_SRSID_KEX6:
}switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) { ... }
result = target_read_u8(target, ICS_C4, &c4);
if (result != ERROR_OK)
return result;
c3 = trim_value;
c4 = (c4 & ~(ICS_C4_SCFTRIM_MASK)) | ((trim_value >> 8) & 0x01);
result = target_write_u8(target, ICS_C3, c3);
if (result != ERROR_OK)
return result;
result = target_write_u8(target, ICS_C4, c4);
if (result != ERROR_OK)
return result;
result = target_read_u8(target, ICS_S, &s);
if (result != ERROR_OK)
return result;
while (!(s & ICS_S_LOCK_MASK)) {
if (timeout <= 1000) {
timeout++;
alive_sleep(1);
}if (timeout <= 1000) { ... } else {
return ERROR_FAIL;
}else { ... }
result = target_read_u8(target, ICS_S, &s);
if (result != ERROR_OK)
return result;
}while (!(s & ICS_S_LOCK_MASK)) { ... }
/* ... */
switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) {
case KINETIS_KE_SRSID_KEX2:
bus_reg_val = 0;
bus_reg_addr = SIM_BUSDIV;
bus_clock = 20000000;
break;
/* ... */
case KINETIS_KE_SRSID_KEX2: case KINETIS_KE_SRSID_KEX4:
/* ... */
switch (KINETIS_KE_SRSID_PINCOUNT(kinfo->sim_srsid)) {
case 1:
case 2:
case 3:
bus_reg_addr = SIM_CLKDIV_KE04_16_20_24;
break;
case 3:
case 5:
case 7:
case 8:
bus_reg_addr = SIM_CLKDIV_KE04_44_64_80;
break;
case 8:
default:
LOG_ERROR("KE04 - Unknown pin count");
return ERROR_FAIL;default
}switch (KINETIS_KE_SRSID_PINCOUNT(kinfo->sim_srsid)) { ... }
bus_reg_val = SIM_CLKDIV_OUTDIV2_MASK;
bus_clock = 24000000;
break;
case KINETIS_KE_SRSID_KEX4:
case KINETIS_KE_SRSID_KEX6:
bus_reg_val = SIM_CLKDIV_OUTDIV2_MASK;
bus_reg_addr = SIM_CLKDIV_KE06;
bus_clock = 24000000;
break;case KINETIS_KE_SRSID_KEX6:
}switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) { ... }
result = target_write_u32(target, bus_reg_addr, bus_reg_val);
if (result != ERROR_OK)
return result;
/* ... */
result = target_read_u8(target, ICS_C2, &c2);
if (result != ERROR_OK)
return result;
c2 &= ~ICS_C2_BDIV_MASK;
switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) {
case KINETIS_KE_SRSID_KEX2:
/* ... */
c2 |= ICS_C2_BDIV(1);
break;
case KINETIS_KE_SRSID_KEX2:
case KINETIS_KE_SRSID_KEX4:
case KINETIS_KE_SRSID_KEX6:
c2 |= ICS_C2_BDIV(0);
break;case KINETIS_KE_SRSID_KEX6:
}switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) { ... }
result = target_write_u8(target, ICS_C2, c2);
if (result != ERROR_OK)
return result;
result = target_write_u8(target, ICS_C1, 4);
if (result != ERROR_OK)
return result;
result = target_read_u8(target, ICS_S, &s);
if (result != ERROR_OK)
return result;
while (!(s & ICS_S_LOCK_MASK)) {
if (timeout <= 1000) {
timeout++;
alive_sleep(1);
}if (timeout <= 1000) { ... } else {
return ERROR_FLASH_OPERATION_FAILED;
}else { ... }
result = target_read_u8(target, ICS_S, &s);
if (result != ERROR_OK)
return result;
}while (!(s & ICS_S_LOCK_MASK)) { ... }
/* ... */
flash_clk_div = bus_clock / 1000000L - 1;
result = target_read_u8(target, kinfo->ftmrx_fclkdiv_addr, &fclkdiv);
if (result != ERROR_OK)
return result;
if (!(fclkdiv & FTMRX_FCLKDIV_FDIVLCK_MASK)) {
if ((fclkdiv & FTMRX_FCLKDIV_FDIVLD_MASK) &&
((fclkdiv & FTMRX_FCLKDIV_FDIV_MASK) != FTMRX_FCLKDIV_FDIV(flash_clk_div))) {
LOG_WARNING("Flash clock was already set and contains an invalid value.");
LOG_WARNING("Please reset the target.");
return ERROR_FAIL;
}if ((fclkdiv & FTMRX_FCLKDIV_FDIVLD_MASK) && ((fclkdiv & FTMRX_FCLKDIV_FDIV_MASK) != FTMRX_FCLKDIV_FDIV(flash_clk_div))) { ... }
fclkdiv = (fclkdiv & ~(FTMRX_FCLKDIV_FDIV_MASK)) | FTMRX_FCLKDIV_FDIV(flash_clk_div);
result = target_write_u8(target, kinfo->ftmrx_fclkdiv_addr, fclkdiv);
if (result != ERROR_OK)
return result;
}if (!(fclkdiv & FTMRX_FCLKDIV_FDIVLCK_MASK)) { ... } else {
if ((fclkdiv & FTMRX_FCLKDIV_FDIV_MASK) != FTMRX_FCLKDIV_FDIV(flash_clk_div)) {
LOG_WARNING("Flash clock register is locked and contains an invalid value.");
LOG_WARNING("Please reset the target.");
return ERROR_FAIL;
}if ((fclkdiv & FTMRX_FCLKDIV_FDIV_MASK) != FTMRX_FCLKDIV_FDIV(flash_clk_div)) { ... }
}else { ... }
LOG_INFO("Flash clock ready");
return ERROR_OK;
}{ ... }
static int kinetis_ke_stop_watchdog(struct target *target)
{
struct working_area *watchdog_algorithm;
struct armv7m_algorithm armv7m_info;
int retval;
uint8_t cs1;
static const uint8_t watchdog_code[] = {
#include "../../../contrib/loaders/flash/kinetis_ke/kinetis_ke_watchdog.inc"
...};
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
retval = target_read_u8(target, WDOG_CS1, &cs1);
if (retval != ERROR_OK)
return retval;
if (!(cs1 & 0x80)) {
return ERROR_OK;
}if (!(cs1 & 0x80)) { ... }
if (target_alloc_working_area(target, sizeof(watchdog_code), &watchdog_algorithm) != ERROR_OK) {
LOG_WARNING("No working area available for watchdog algorithm");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (target_alloc_working_area(target, sizeof(watchdog_code), &watchdog_algorithm) != ERROR_OK) { ... }
retval = target_write_buffer(target, watchdog_algorithm->address,
sizeof(watchdog_code), watchdog_code);
if (retval != ERROR_OK)
return retval;
armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
armv7m_info.core_mode = ARM_MODE_THREAD;
retval = target_run_algorithm(target, 0, NULL, 0, NULL,
watchdog_algorithm->address, 0, 100000, &armv7m_info);
if (retval != ERROR_OK) {
LOG_ERROR("Error executing Kinetis KE watchdog algorithm");
}if (retval != ERROR_OK) { ... } else {
LOG_INFO("Watchdog stopped");
}else { ... }
target_free_working_area(target, watchdog_algorithm);
return retval;
}{ ... }
COMMAND_HANDLER(kinetis_ke_disable_wdog_handler)
{
struct target *target = get_current_target(CMD_CTX);
if (CMD_ARGC > 0)
return ERROR_COMMAND_SYNTAX_ERROR;
return kinetis_ke_stop_watchdog(target);
}{ ... }
COMMAND_HANDLER(kinetis_ke_mdm_mass_erase)
{
struct target *target = get_current_target(CMD_CTX);
struct cortex_m_common *cortex_m = target_to_cm(target);
struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
if (!dap) {
LOG_ERROR("Cannot perform mass erase with a high-level adapter");
return ERROR_FAIL;
}if (!dap) { ... }
int retval;
if (jtag_get_reset_config() & RESET_HAS_SRST)
adapter_assert_reset();
/* ... */
retval = kinetis_ke_mdm_write_register(dap, MDM_REG_CTRL, MEM_CTRL_SYS_RES_REQ);
if (retval != ERROR_OK)
return retval;
/* ... */
retval = kinetis_ke_mdm_poll_register(dap, MDM_REG_STAT,
MDM_STAT_FREADY | MDM_STAT_SYSRES,
MDM_STAT_FREADY);
if (retval != ERROR_OK) {
LOG_ERROR("MDM : flash ready timeout");
return retval;
}if (retval != ERROR_OK) { ... }
/* ... */
retval = kinetis_ke_mdm_write_register(dap, MDM_REG_CTRL, MEM_CTRL_FMEIP);
if (retval != ERROR_OK)
return retval;
retval = kinetis_ke_mdm_poll_register(dap, MDM_REG_STAT,
MDM_STAT_FMEACK, MDM_STAT_FMEACK);
if (retval != ERROR_OK)
return retval;
/* ... */
retval = kinetis_ke_mdm_poll_register(dap, MDM_REG_CTRL,
MEM_CTRL_FMEIP,
0);
if (retval != ERROR_OK)
return retval;
if (jtag_get_reset_config() & RESET_HAS_SRST)
adapter_deassert_reset();
return ERROR_OK;
}{ ... }
static const uint32_t kinetis_ke_known_mdm_ids[] = {
0x001C0020,
...};
/* ... */
COMMAND_HANDLER(kinetis_ke_check_flash_security_status)
{
struct target *target = get_current_target(CMD_CTX);
struct cortex_m_common *cortex_m = target_to_cm(target);
struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
if (!dap) {
LOG_WARNING("Cannot check flash security status with a high-level adapter");
return ERROR_OK;
}if (!dap) { ... }
uint32_t val;
int retval;
/* ... */
retval = kinetis_ke_mdm_read_register(dap, MDM_REG_ID, &val);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: failed to read ID register");
goto fail;
}if (retval != ERROR_OK) { ... }
bool found = false;
for (size_t i = 0; i < ARRAY_SIZE(kinetis_ke_known_mdm_ids); i++) {
if (val == kinetis_ke_known_mdm_ids[i]) {
found = true;
break;
}if (val == kinetis_ke_known_mdm_ids[i]) { ... }
}for (size_t i = 0; i < ARRAY_SIZE(kinetis_ke_known_mdm_ids); i++) { ... }
if (!found)
LOG_WARNING("MDM: unknown ID %08" PRIX32, val);
/* ... */
retval = kinetis_ke_mdm_poll_register(dap, MDM_REG_STAT,
MDM_STAT_FREADY,
MDM_STAT_FREADY);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: flash ready timeout");
goto fail;
}if (retval != ERROR_OK) { ... }
/* ... */
retval = kinetis_ke_mdm_read_register(dap, MDM_REG_STAT, &val);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: failed to read MDM_REG_STAT");
goto fail;
}if (retval != ERROR_OK) { ... }
if (val & MDM_STAT_SYSSEC) {
jtag_poll_set_enabled(false);
LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
LOG_WARNING("**** ****");
LOG_WARNING("**** Your Kinetis MCU is in secured state, which means that, ****");
LOG_WARNING("**** with exception for very basic communication, JTAG/SWD ****");
LOG_WARNING("**** interface will NOT work. In order to restore its ****");
LOG_WARNING("**** functionality please issue 'kinetis_ke mdm mass_erase' ****");
LOG_WARNING("**** command, power cycle the MCU and restart OpenOCD. ****");
LOG_WARNING("**** ****");
LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
}if (val & MDM_STAT_SYSSEC) { ... } else {
LOG_INFO("MDM: Chip is unsecured. Continuing.");
jtag_poll_set_enabled(true);
}else { ... }
return ERROR_OK;
fail:
LOG_ERROR("MDM: Failed to check security status of the MCU. Cannot proceed further");
jtag_poll_set_enabled(false);
return retval;
}{ ... }
FLASH_BANK_COMMAND_HANDLER(kinetis_ke_flash_bank_command)
{
struct kinetis_ke_flash_bank *bank_info;
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
LOG_INFO("add flash_bank kinetis_ke %s", bank->name);
bank_info = malloc(sizeof(struct kinetis_ke_flash_bank));
memset(bank_info, 0, sizeof(struct kinetis_ke_flash_bank));
bank->driver_priv = bank_info;
return ERROR_OK;
}{ ... }
static uint8_t kinetis_ke_flash_write_code[] = {
#include "../../../contrib/loaders/flash/kinetis_ke/kinetis_ke_flash.inc"
...};
static int kinetis_ke_write_words(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t words)
{
struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
struct target *target = bank->target;
uint32_t ram_buffer_size = 512 + 16;
struct working_area *write_algorithm;
struct working_area *source;
uint32_t address = bank->base + offset;
struct reg_param reg_params[4];
struct armv7m_algorithm armv7m_info;
int retval = ERROR_OK;
uint32_t flash_code_size;
LOG_INFO("Kinetis KE: FLASH Write ...");
if (target_alloc_working_area(target, sizeof(kinetis_ke_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(kinetis_ke_flash_write_code), &write_algorithm) != ERROR_OK) { ... }
flash_code_size = sizeof(kinetis_ke_flash_write_code);
buf_set_u32(&kinetis_ke_flash_write_code[flash_code_size-16], 0, 32, kinfo->ftmrx_fstat_addr);
buf_set_u32(&kinetis_ke_flash_write_code[flash_code_size-12], 0, 32, kinfo->ftmrx_fccobix_addr);
buf_set_u32(&kinetis_ke_flash_write_code[flash_code_size-8], 0, 32, kinfo->ftmrx_fccobhi_addr);
buf_set_u32(&kinetis_ke_flash_write_code[flash_code_size-4], 0, 32, kinfo->ftmrx_fccoblo_addr);
retval = target_write_buffer(target, write_algorithm->address,
sizeof(kinetis_ke_flash_write_code), kinetis_ke_flash_write_code);
if (retval != ERROR_OK)
return retval;
if (target_alloc_working_area(target, ram_buffer_size, &source) != ERROR_OK) {
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 (target_alloc_working_area(target, ram_buffer_size, &source) != ERROR_OK) { ... }
armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
armv7m_info.core_mode = ARM_MODE_THREAD;
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);
buf_set_u32(reg_params[0].value, 0, 32, address);
buf_set_u32(reg_params[1].value, 0, 32, words);
buf_set_u32(reg_params[2].value, 0, 32, source->address);
buf_set_u32(reg_params[3].value, 0, 32, source->address + source->size);
retval = target_run_flash_async_algorithm(target, buffer, words, 4,
0, NULL,
4, reg_params,
source->address, source->size,
write_algorithm->address, 0,
&armv7m_info);
if (retval == ERROR_FLASH_OPERATION_FAILED) {
if (buf_get_u32(reg_params[0].value, 0, 32) & FTMRX_ERROR_ACCERR)
LOG_ERROR("flash access error");
if (buf_get_u32(reg_params[0].value, 0, 32) & FTMRX_ERROR_FPVIOL)
LOG_ERROR("flash protection violation");
}if (retval == ERROR_FLASH_OPERATION_FAILED) { ... }
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]);
return retval;
}{ ... }
static int kinetis_ke_protect(struct flash_bank *bank, int set,
unsigned int first, unsigned int last)
{
LOG_WARNING("kinetis_ke_protect not supported yet");
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
return ERROR_FLASH_BANK_INVALID;
}{ ... }
static int kinetis_ke_protect_check(struct flash_bank *bank)
{
struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
int result;
uint8_t fprot;
uint8_t fpopen, fpldis, fphdis;
uint8_t fphs, fpls;
uint32_t lprot_size = 0, hprot_size = 0;
uint32_t lprot_to = 0, hprot_from = 0;
result = target_read_u8(bank->target, kinfo->ftmrx_fprot_addr, &fprot);
if (result != ERROR_OK)
return result;
fpopen = fprot & 0x80;
fpldis = fprot & 0x04;
fphdis = fprot & 0x20;
fphs = (fprot >> 3) & 0x03;
fpls = fprot & 0x03;
if (fpopen && fpldis && fphdis) {
LOG_WARNING("No flash protection found.");
for (unsigned int i = 0; i < bank->num_sectors; i++)
bank->sectors[i].is_protected = 0;
kinfo->protection_size = 0;
}if (fpopen && fpldis && fphdis) { ... } else {
LOG_WARNING("Flash protected. FPOPEN=%i FPLDIS=%i FPHDIS=%i FPLS=%i FPHS=%i",
fpopen ? 1 : 0, fpldis ? 1 : 0, fphdis ? 1 : 0, fpls, fphs);
if (fpopen) {
if (fpldis == 0)
lprot_size = (kinfo->sector_size * 4) << fpls;
if (fphdis == 0)
hprot_size = (kinfo->sector_size * 2) << fphs;
}if (fpopen) { ... } else {
if (fpldis == 1)
lprot_size = (kinfo->sector_size * 4) << fpls;
if (fphdis == 1)
hprot_size = (kinfo->sector_size * 2) << fphs;
}else { ... }
kinfo->protection_size = lprot_size + hprot_size;
lprot_to = lprot_size / kinfo->sector_size;
hprot_from = (0x8000 - hprot_size) / kinfo->sector_size;
for (unsigned int i = 0; i < bank->num_sectors; i++) {
if (bank->sectors[i].offset < 0x4000) {
if (lprot_to && (i < lprot_to))
bank->sectors[i].is_protected = 1;
else
bank->sectors[i].is_protected = 0;
/* ... */
}if (bank->sectors[i].offset < 0x4000) { ... } else if (bank->sectors[i].offset < 0x6000 || bank->sectors[i].offset >= 0x8000) {
if (fpopen)
bank->sectors[i].is_protected = 0;
else
bank->sectors[i].is_protected = 1;
}else if (bank->sectors[i].offset < 0x6000 || bank->sectors[i].offset >= 0x8000) { ... } else if (bank->sectors[i].offset < 0x8000) {
if (hprot_from && (i > hprot_from))
bank->sectors[i].is_protected = 1;
else
bank->sectors[i].is_protected = 0;
}else if (bank->sectors[i].offset < 0x8000) { ... }
}for (unsigned int i = 0; i < bank->num_sectors; i++) { ... }
}else { ... }
return ERROR_OK;
}{ ... }
static int kinetis_ke_ftmrx_command(struct flash_bank *bank, uint8_t count,
uint8_t *FCCOBIX, uint8_t *FCCOBHI, uint8_t *FCCOBLO, uint8_t *fstat)
{
uint8_t i;
int result;
struct target *target = bank->target;
struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
uint32_t timeout = 0;
result = target_write_u8(target, kinfo->ftmrx_fstat_addr, 0x30);
if (result != ERROR_OK)
return result;
for (i = 0; i < count; i++) {
result = target_write_u8(target, kinfo->ftmrx_fccobix_addr, FCCOBIX[i]);
if (result != ERROR_OK)
return result;
result = target_write_u8(target, kinfo->ftmrx_fccobhi_addr, FCCOBHI[i]);
if (result != ERROR_OK)
return result;
if (FCCOBLO) {
result = target_write_u8(target, kinfo->ftmrx_fccoblo_addr, FCCOBLO[i]);
if (result != ERROR_OK)
return result;
}if (FCCOBLO) { ... }
}for (i = 0; i < count; i++) { ... }
result = target_write_u8(target, kinfo->ftmrx_fstat_addr, 0x80);
if (result != ERROR_OK)
return result;
result = target_read_u8(target, kinfo->ftmrx_fstat_addr, fstat);
if (result != ERROR_OK)
return result;
while (!(*fstat & FTMRX_FSTAT_CCIF_MASK)) {
if (timeout <= 1000) {
timeout++;
alive_sleep(1);
}if (timeout <= 1000) { ... } else {
return ERROR_FLASH_OPERATION_FAILED;
}else { ... }
result = target_read_u8(target, kinfo->ftmrx_fstat_addr, fstat);
if (result != ERROR_OK)
return result;
}while (!(*fstat & FTMRX_FSTAT_CCIF_MASK)) { ... }
return ERROR_OK;
}{ ... }
static int kinetis_ke_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
int result;
uint8_t FCCOBIX[2], FCCOBHI[2], FCCOBLO[2], fstat;
bool fcf_erased = false;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if ((first > bank->num_sectors) || (last > bank->num_sectors))
return ERROR_FLASH_OPERATION_FAILED;
result = kinetis_ke_prepare_flash(bank);
if (result != ERROR_OK)
return result;
for (unsigned int i = first; i <= last; i++) {
FCCOBIX[0] = 0;
FCCOBHI[0] = FTMRX_CMD_ERASESECTOR;
FCCOBLO[0] = (bank->base + bank->sectors[i].offset) >> 16;
FCCOBIX[1] = 1;
FCCOBHI[1] = (bank->base + bank->sectors[i].offset) >> 8;
FCCOBLO[1] = (bank->base + bank->sectors[i].offset);
result = kinetis_ke_ftmrx_command(bank, 2, FCCOBIX, FCCOBHI, FCCOBLO, &fstat);
if (result != ERROR_OK) {
LOG_WARNING("erase sector %u failed", i);
return ERROR_FLASH_OPERATION_FAILED;
}if (result != ERROR_OK) { ... }
if (i == 2)
fcf_erased = true;
}for (unsigned int i = first; i <= last; i++) { ... }
if (fcf_erased) {
LOG_WARNING
("flash configuration field erased, please reset the device");
}if (fcf_erased) { ... }
return ERROR_OK;
}{ ... }
static int kinetis_ke_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
int result;
uint8_t *new_buffer = NULL;
uint32_t words = count / 4;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (offset > bank->size)
return ERROR_FLASH_BANK_INVALID;
if (offset & 0x3) {
LOG_WARNING("offset 0x%" PRIx32 " breaks the required alignment", offset);
return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
}if (offset & 0x3) { ... }
result = kinetis_ke_stop_watchdog(bank->target);
if (result != ERROR_OK)
return result;
result = kinetis_ke_prepare_flash(bank);
if (result != ERROR_OK)
return result;
if (count & 0x3) {
uint32_t old_count = count;
count = (old_count | 3) + 1;
new_buffer = malloc(count);
if (!new_buffer) {
LOG_ERROR("odd number of bytes to write and no memory "
"for padding buffer");
return ERROR_FAIL;
}if (!new_buffer) { ... }
LOG_INFO("odd number of bytes to write (%" PRIu32 "), extending to %" PRIu32 " "
"and padding with 0xff", old_count, count);
memset(new_buffer, 0xff, count);
buffer = memcpy(new_buffer, buffer, old_count);
words++;
}if (count & 0x3) { ... }
result = kinetis_ke_write_words(bank, buffer, offset, words);
free(new_buffer);
return result;
}{ ... }
static int kinetis_ke_probe(struct flash_bank *bank)
{
int result;
uint32_t offset = 0;
struct target *target = bank->target;
struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
result = target_read_u32(target, SIM_SRSID, &kinfo->sim_srsid);
if (result != ERROR_OK)
return result;
if (KINETIS_KE_SRSID_FAMID(kinfo->sim_srsid) != 0x00) {
LOG_ERROR("Unsupported KE family");
return ERROR_FLASH_OPER_UNSUPPORTED;
}if (KINETIS_KE_SRSID_FAMID(kinfo->sim_srsid) != 0x00) { ... }
switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) {
case KINETIS_KE_SRSID_KEX2:
LOG_INFO("KE02 sub-family");
break;
case KINETIS_KE_SRSID_KEX2:
case KINETIS_KE_SRSID_KEX4:
LOG_INFO("KE04 sub-family");
break;
case KINETIS_KE_SRSID_KEX4:
case KINETIS_KE_SRSID_KEX6:
LOG_INFO("KE06 sub-family");
break;
case KINETIS_KE_SRSID_KEX6:
default:
LOG_ERROR("Unsupported KE sub-family");
return ERROR_FLASH_OPER_UNSUPPORTED;default
}switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) { ... }
/* ... */
bank->base = 0x00000000;
kinfo->sector_size = 512;
switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) {
case KINETIS_KE_SRSID_KEX2:
bank->size = 0x00010000;
bank->num_sectors = 128;
/* ... */
kinfo->ftmrx_fclkdiv_addr = 0x40020000;
kinfo->ftmrx_fccobix_addr = 0x40020002;
kinfo->ftmrx_fstat_addr = 0x40020006;
kinfo->ftmrx_fprot_addr = 0x40020008;
kinfo->ftmrx_fccobhi_addr = 0x4002000A;
kinfo->ftmrx_fccoblo_addr = 0x4002000B;
break;
case KINETIS_KE_SRSID_KEX2:
case KINETIS_KE_SRSID_KEX6:
case KINETIS_KE_SRSID_KEX4:
bank->size = 0x00020000;
bank->num_sectors = 256;
/* ... */
kinfo->ftmrx_fclkdiv_addr = 0x40020003;
kinfo->ftmrx_fccobix_addr = 0x40020001;
kinfo->ftmrx_fstat_addr = 0x40020005;
kinfo->ftmrx_fprot_addr = 0x4002000B;
kinfo->ftmrx_fccobhi_addr = 0x40020009;
kinfo->ftmrx_fccoblo_addr = 0x40020008;
break;case KINETIS_KE_SRSID_KEX4:
}switch (KINETIS_KE_SRSID_SUBFAMID(kinfo->sim_srsid)) { ... }
free(bank->sectors);
assert(bank->num_sectors > 0);
bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
for (unsigned int i = 0; i < bank->num_sectors; i++) {
bank->sectors[i].offset = offset;
bank->sectors[i].size = kinfo->sector_size;
offset += kinfo->sector_size;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = 1;
}for (unsigned int i = 0; i < bank->num_sectors; i++) { ... }
return ERROR_OK;
}{ ... }
static int kinetis_ke_auto_probe(struct flash_bank *bank)
{
struct kinetis_ke_flash_bank *kinfo = bank->driver_priv;
if (kinfo->sim_srsid)
return ERROR_OK;
return kinetis_ke_probe(bank);
}{ ... }
static int kinetis_ke_info(struct flash_bank *bank, struct command_invocation *cmd)
{
command_print_sameline(cmd, "%s driver for flash bank %s at " TARGET_ADDR_FMT,
bank->driver->name, bank->name, bank->base);
return ERROR_OK;
}{ ... }
static int kinetis_ke_blank_check(struct flash_bank *bank)
{
uint8_t FCCOBIX[3], FCCOBHI[3], FCCOBLO[3], fstat;
uint16_t longwords = 0;
int result;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
result = kinetis_ke_prepare_flash(bank);
if (result != ERROR_OK)
return result;
FCCOBIX[0] = 0;
FCCOBHI[0] = FTMRX_CMD_ALLERASED;
result = kinetis_ke_ftmrx_command(bank, 1, FCCOBIX, FCCOBHI, NULL, &fstat);
if (result != ERROR_OK)
return result;
if (fstat & (FTMRX_FSTAT_MGSTAT0_MASK | FTMRX_FSTAT_MGSTAT1_MASK)) {
for (unsigned int i = 0; i < bank->num_sectors; i++) {
FCCOBIX[0] = 0;
FCCOBHI[0] = FTMRX_CMD_SECTIONERASED;
FCCOBLO[0] = (bank->base + bank->sectors[i].offset) >> 16;
FCCOBIX[1] = 1;
FCCOBHI[1] = (bank->base + bank->sectors[i].offset) >> 8;
FCCOBLO[1] = (bank->base + bank->sectors[i].offset);
longwords = 128;
FCCOBIX[2] = 2;
FCCOBHI[2] = longwords >> 8;
FCCOBLO[2] = longwords;
result = kinetis_ke_ftmrx_command(bank, 3, FCCOBIX, FCCOBHI, FCCOBLO, &fstat);
if (result == ERROR_OK) {
bank->sectors[i].is_erased = !(fstat & (FTMRX_FSTAT_MGSTAT0_MASK | FTMRX_FSTAT_MGSTAT1_MASK));
}if (result == ERROR_OK) { ... } else {
LOG_DEBUG("Ignoring error on PFlash sector blank-check");
bank->sectors[i].is_erased = -1;
}else { ... }
}for (unsigned int i = 0; i < bank->num_sectors; i++) { ... }
}if (fstat & (FTMRX_FSTAT_MGSTAT0_MASK | FTMRX_FSTAT_MGSTAT1_MASK)) { ... } else {
for (unsigned int i = 0; i < bank->num_sectors; i++)
bank->sectors[i].is_erased = 1;
}else { ... }
return ERROR_OK;
}{ ... }
static const struct command_registration kinetis_ke_security_command_handlers[] = {
{
.name = "check_security",
.mode = COMMAND_EXEC,
.help = "Check status of device security lock",
.usage = "",
.handler = kinetis_ke_check_flash_security_status,
...},
{
.name = "mass_erase",
.mode = COMMAND_EXEC,
.help = "Issue a complete flash erase via the MDM-AP",
.usage = "",
.handler = kinetis_ke_mdm_mass_erase,
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration kinetis_ke_exec_command_handlers[] = {
{
.name = "mdm",
.mode = COMMAND_ANY,
.help = "MDM-AP command group",
.usage = "",
.chain = kinetis_ke_security_command_handlers,
...},
{
.name = "disable_wdog",
.mode = COMMAND_EXEC,
.help = "Disable the watchdog timer",
.usage = "",
.handler = kinetis_ke_disable_wdog_handler,
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration kinetis_ke_command_handler[] = {
{
.name = "kinetis_ke",
.mode = COMMAND_ANY,
.help = "Kinetis KE flash controller commands",
.usage = "",
.chain = kinetis_ke_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
const struct flash_driver kinetis_ke_flash = {
.name = "kinetis_ke",
.commands = kinetis_ke_command_handler,
.flash_bank_command = kinetis_ke_flash_bank_command,
.erase = kinetis_ke_erase,
.protect = kinetis_ke_protect,
.write = kinetis_ke_write,
.read = default_flash_read,
.probe = kinetis_ke_probe,
.auto_probe = kinetis_ke_auto_probe,
.erase_check = kinetis_ke_blank_check,
.protect_check = kinetis_ke_protect_check,
.info = kinetis_ke_info,
.free_driver_priv = default_flash_free_driver_priv,
...};