-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathkinetis.c
More file actions
3449 lines (2909 loc) · 101 KB
/
kinetis.c
File metadata and controls
3449 lines (2909 loc) · 101 KB
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
// SPDX-License-Identifier: GPL-2.0-or-later
/***************************************************************************
* Copyright (C) 2011 by Mathias Kuester *
* kesmtp@freenet.de *
* *
* Copyright (C) 2011 sleep(5) ltd *
* tomas@sleepfive.com *
* *
* Copyright (C) 2012 by Christopher D. Kilgour *
* techie at whiterocker.com *
* *
* Copyright (C) 2013 Nemui Trinomius *
* nemuisan_kawausogasuki@live.jp *
* *
* Copyright (C) 2015 Tomas Vanek *
* vanekt@fbl.cz *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "jtag/interface.h"
#include "imp.h"
#include <helper/binarybuffer.h>
#include <helper/time_support.h>
#include <target/target_type.h>
#include <target/algorithm.h>
#include <target/arm_adi_v5.h>
#include <target/armv7m.h>
#include <target/cortex_m.h>
/*
* Implementation Notes
*
* The persistent memories in the Kinetis chip families K10 through
* K70 are all manipulated with the Flash Memory Module. Some
* variants call this module the FTFE, others call it the FTFL. To
* indicate that both are considered here, we use FTFX.
*
* Within the module, according to the chip variant, the persistent
* memory is divided into what Freescale terms Program Flash, FlexNVM,
* and FlexRAM. All chip variants have Program Flash. Some chip
* variants also have FlexNVM and FlexRAM, which always appear
* together.
*
* A given Kinetis chip may have 1, 2 or 4 blocks of flash. Here we map
* each block to a separate bank. Each block size varies by chip and
* may be determined by the read-only SIM_FCFG1 register. The sector
* size within each bank/block varies by chip, and may be 1, 2 or 4k.
* The sector size may be different for flash and FlexNVM.
*
* The first half of the flash (1 or 2 blocks) is always Program Flash
* and always starts at address 0x00000000. The "PFLSH" flag, bit 23
* of the read-only SIM_FCFG2 register, determines whether the second
* half of the flash is also Program Flash or FlexNVM+FlexRAM. When
* PFLSH is set, the second from the first half. When PFLSH is clear,
* the second half of flash is FlexNVM and always starts at address
* 0x10000000. FlexRAM, which is also present when PFLSH is clear,
* always starts at address 0x14000000.
*
* The Flash Memory Module provides a register set where flash
* commands are loaded to perform flash operations like erase and
* program. Different commands are available depending on whether
* Program Flash or FlexNVM/FlexRAM is being manipulated. Although
* the commands used are quite consistent between flash blocks, the
* parameters they accept differ according to the flash sector size.
*
*/
/* Addresses */
#define FCF_ADDRESS 0x00000400
#define FCF_FPROT 0x8
#define FCF_FSEC 0xc
#define FCF_FOPT 0xd
#define FCF_FDPROT 0xf
#define FCF_SIZE 0x10
#define FLEXRAM 0x14000000
#define MSCM_OCMDR0 0x40001400
#define MSCM_OCMDR1 0x40001404
#define FMC_PFB01CR 0x4001f004
#define FTFX_FSTAT 0x40020000
#define FTFX_FCNFG 0x40020001
#define FTFX_FCCOB3 0x40020004
#define FTFX_FPROT3 0x40020010
#define FTFX_FDPROT 0x40020017
#define SIM_BASE 0x40047000
#define SIM_BASE_KL28 0x40074000
#define SIM_COPC 0x40048100
/* SIM_COPC does not exist on devices with changed SIM_BASE */
#define WDOG_BASE 0x40052000
#define WDOG32_KE1X 0x40052000
#define WDOG32_KL28 0x40076000
#define SMC_PMCTRL 0x4007E001
#define SMC_PMSTAT 0x4007E003
#define SMC32_PMCTRL 0x4007E00C
#define SMC32_PMSTAT 0x4007E014
#define PMC_REGSC 0x4007D002
#define MC_PMCTRL 0x4007E003
#define MCM_PLACR 0xF000300C
/* Offsets */
#define SIM_SOPT1_OFFSET 0x0000
#define SIM_SDID_OFFSET 0x1024
#define SIM_FCFG1_OFFSET 0x104c
#define SIM_FCFG2_OFFSET 0x1050
#define WDOG_STCTRLH_OFFSET 0
#define WDOG32_CS_OFFSET 0
/* Values */
#define PM_STAT_RUN 0x01
#define PM_STAT_VLPR 0x04
#define PM_CTRL_RUNM_RUN 0x00
/* Commands */
#define FTFX_CMD_BLOCKSTAT 0x00
#define FTFX_CMD_SECTSTAT 0x01
#define FTFX_CMD_LWORDPROG 0x06
#define FTFX_CMD_SECTERASE 0x09
#define FTFX_CMD_SECTWRITE 0x0b
#define FTFX_CMD_MASSERASE 0x44
#define FTFX_CMD_PGMPART 0x80
#define FTFX_CMD_SETFLEXRAM 0x81
/* The older Kinetis K series uses the following SDID layout :
* Bit 31-16 : 0
* Bit 15-12 : REVID
* Bit 11-7 : DIEID
* Bit 6-4 : FAMID
* Bit 3-0 : PINID
*
* The newer Kinetis series uses the following SDID layout :
* Bit 31-28 : FAMID
* Bit 27-24 : SUBFAMID
* Bit 23-20 : SERIESID
* Bit 19-16 : SRAMSIZE
* Bit 15-12 : REVID
* Bit 6-4 : Reserved (0)
* Bit 3-0 : PINID
*
* We assume that if bits 31-16 are 0 then it's an older
* K-series MCU.
*/
#define KINETIS_SOPT1_RAMSIZE_MASK 0x0000F000
#define KINETIS_SOPT1_RAMSIZE_K24FN1M 0x0000B000
#define KINETIS_SDID_K_SERIES_MASK 0x0000FFFF
#define KINETIS_SDID_DIEID_MASK 0x00000F80
#define KINETIS_SDID_DIEID_K22FN128 0x00000680 /* smaller pflash with FTFA */
#define KINETIS_SDID_DIEID_K22FN256 0x00000A80
#define KINETIS_SDID_DIEID_K22FN512 0x00000E80
#define KINETIS_SDID_DIEID_K24FN256 0x00000700
#define KINETIS_SDID_DIEID_K24FN1M 0x00000300 /* Detect Errata 7534 */
/* We can't rely solely on the FAMID field to determine the MCU
* type since some FAMID values identify multiple MCUs with
* different flash sector sizes (K20 and K22 for instance).
* Therefore we combine it with the DIEID bits which may possibly
* break if Freescale bumps the DIEID for a particular MCU. */
#define KINETIS_K_SDID_TYPE_MASK 0x00000FF0
#define KINETIS_K_SDID_K10_M50 0x00000000
#define KINETIS_K_SDID_K10_M72 0x00000080
#define KINETIS_K_SDID_K10_M100 0x00000100
#define KINETIS_K_SDID_K10_M120 0x00000180
#define KINETIS_K_SDID_K11 0x00000220
#define KINETIS_K_SDID_K12 0x00000200
#define KINETIS_K_SDID_K20_M50 0x00000010
#define KINETIS_K_SDID_K20_M72 0x00000090
#define KINETIS_K_SDID_K20_M100 0x00000110
#define KINETIS_K_SDID_K20_M120 0x00000190
#define KINETIS_K_SDID_K21_M50 0x00000230
#define KINETIS_K_SDID_K21_M120 0x00000330
#define KINETIS_K_SDID_K22_M50 0x00000210
#define KINETIS_K_SDID_K22_M120 0x00000310
#define KINETIS_K_SDID_K30_M72 0x000000A0
#define KINETIS_K_SDID_K30_M100 0x00000120
#define KINETIS_K_SDID_K40_M72 0x000000B0
#define KINETIS_K_SDID_K40_M100 0x00000130
#define KINETIS_K_SDID_K50_M72 0x000000E0
#define KINETIS_K_SDID_K51_M72 0x000000F0
#define KINETIS_K_SDID_K53 0x00000170
#define KINETIS_K_SDID_K60_M100 0x00000140
#define KINETIS_K_SDID_K60_M150 0x000001C0
#define KINETIS_K_SDID_K70_M150 0x000001D0
#define KINETIS_K_REVID_MASK 0x0000F000
#define KINETIS_K_REVID_SHIFT 12
#define KINETIS_SDID_SERIESID_MASK 0x00F00000
#define KINETIS_SDID_SERIESID_K 0x00000000
#define KINETIS_SDID_SERIESID_KL 0x00100000
#define KINETIS_SDID_SERIESID_KE 0x00200000
#define KINETIS_SDID_SERIESID_KW 0x00500000
#define KINETIS_SDID_SERIESID_KV 0x00600000
#define KINETIS_SDID_SUBFAMID_SHIFT 24
#define KINETIS_SDID_SUBFAMID_MASK 0x0F000000
#define KINETIS_SDID_SUBFAMID_KX0 0x00000000
#define KINETIS_SDID_SUBFAMID_KX1 0x01000000
#define KINETIS_SDID_SUBFAMID_KX2 0x02000000
#define KINETIS_SDID_SUBFAMID_KX3 0x03000000
#define KINETIS_SDID_SUBFAMID_KX4 0x04000000
#define KINETIS_SDID_SUBFAMID_KX5 0x05000000
#define KINETIS_SDID_SUBFAMID_KX6 0x06000000
#define KINETIS_SDID_SUBFAMID_KX7 0x07000000
#define KINETIS_SDID_SUBFAMID_KX8 0x08000000
#define KINETIS_SDID_FAMILYID_SHIFT 28
#define KINETIS_SDID_FAMILYID_MASK 0xF0000000
#define KINETIS_SDID_FAMILYID_K0X 0x00000000
#define KINETIS_SDID_FAMILYID_K1X 0x10000000
#define KINETIS_SDID_FAMILYID_K2X 0x20000000
#define KINETIS_SDID_FAMILYID_K3X 0x30000000
#define KINETIS_SDID_FAMILYID_K4X 0x40000000
#define KINETIS_SDID_FAMILYID_K5X 0x50000000
#define KINETIS_SDID_FAMILYID_K6X 0x60000000
#define KINETIS_SDID_FAMILYID_K7X 0x70000000
#define KINETIS_SDID_FAMILYID_K8X 0x80000000
#define KINETIS_SDID_FAMILYID_KL8X 0x90000000
/* The field originally named DIEID has new name/meaning on KE1x */
#define KINETIS_SDID_PROJECTID_MASK KINETIS_SDID_DIEID_MASK
#define KINETIS_SDID_PROJECTID_KE1XF 0x00000080
#define KINETIS_SDID_PROJECTID_KE1XZ 0x00000100
/* The S32K series uses a different, incompatible SDID layout :
* Bit 31-28 : GENERATION
* Bit 27-24 : SUBSERIES
* Bit 23-20 : DERIVATE
* Bit 19-16 : RAMSIZE
* Bit 15-12 : REVID
* Bit 11-8 : PACKAGE
* Bit 7-0 : FEATURES
*/
#define KINETIS_SDID_S32K_SERIES_MASK 0xFF000000 /* GENERATION + SUBSERIES */
#define KINETIS_SDID_S32K_SERIES_K11X 0x11000000
#define KINETIS_SDID_S32K_SERIES_K14X 0x14000000
#define KINETIS_SDID_S32K_DERIVATE_MASK 0x00F00000
#define KINETIS_SDID_S32K_DERIVATE_KXX2 0x00200000
#define KINETIS_SDID_S32K_DERIVATE_KXX3 0x00300000
#define KINETIS_SDID_S32K_DERIVATE_KXX4 0x00400000
#define KINETIS_SDID_S32K_DERIVATE_KXX5 0x00500000
#define KINETIS_SDID_S32K_DERIVATE_KXX6 0x00600000
#define KINETIS_SDID_S32K_DERIVATE_KXX8 0x00800000
struct kinetis_flash_bank {
struct kinetis_chip *k_chip;
bool probed;
unsigned bank_number; /* bank number in particular chip */
struct flash_bank *bank;
uint32_t sector_size;
uint32_t protection_size;
uint32_t prog_base; /* base address for FTFx operations */
/* usually same as bank->base for pflash, differs for FlexNVM */
uint32_t protection_block; /* number of first protection block in this bank */
enum {
FC_AUTO = 0,
FC_PFLASH,
FC_FLEX_NVM,
FC_FLEX_RAM,
} flash_class;
};
#define KINETIS_MAX_BANKS 4u
struct kinetis_chip {
struct target *target;
bool probed;
uint32_t sim_sdid;
uint32_t sim_fcfg1;
uint32_t sim_fcfg2;
uint32_t fcfg2_maxaddr0_shifted;
uint32_t fcfg2_maxaddr1_shifted;
unsigned num_pflash_blocks, num_nvm_blocks;
unsigned pflash_sector_size, nvm_sector_size;
unsigned max_flash_prog_size;
uint32_t pflash_base;
uint32_t pflash_size;
uint32_t nvm_base;
uint32_t nvm_size; /* whole FlexNVM */
uint32_t dflash_size; /* accessible rest of FlexNVM if EEPROM backup uses part of FlexNVM */
uint32_t progr_accel_ram;
uint32_t sim_base;
enum {
CT_KINETIS = 0,
CT_S32K,
} chip_type;
enum {
FS_PROGRAM_SECTOR = 1,
FS_PROGRAM_LONGWORD = 2,
FS_PROGRAM_PHRASE = 4, /* Unsupported */
FS_NO_CMD_BLOCKSTAT = 0x40,
FS_WIDTH_256BIT = 0x80,
FS_ECC = 0x100,
} flash_support;
enum {
KINETIS_CACHE_NONE,
KINETIS_CACHE_K, /* invalidate using FMC->PFB0CR/PFB01CR */
KINETIS_CACHE_L, /* invalidate using MCM->PLACR */
KINETIS_CACHE_MSCM, /* devices like KE1xF, invalidate MSCM->OCMDR0 */
KINETIS_CACHE_MSCM2, /* devices like S32K, invalidate MSCM->OCMDR0 and MSCM->OCMDR1 */
} cache_type;
enum {
KINETIS_WDOG_NONE,
KINETIS_WDOG_K,
KINETIS_WDOG_COP,
KINETIS_WDOG32_KE1X,
KINETIS_WDOG32_KL28,
} watchdog_type;
enum {
KINETIS_SMC,
KINETIS_SMC32,
KINETIS_MC,
} sysmodectrlr_type;
char name[40];
unsigned num_banks;
struct kinetis_flash_bank banks[KINETIS_MAX_BANKS];
};
struct kinetis_type {
uint32_t sdid;
char *name;
};
static const struct kinetis_type kinetis_types_old[] = {
{ KINETIS_K_SDID_K10_M50, "MK10D%s5" },
{ KINETIS_K_SDID_K10_M72, "MK10D%s7" },
{ KINETIS_K_SDID_K10_M100, "MK10D%s10" },
{ KINETIS_K_SDID_K10_M120, "MK10F%s12" },
{ KINETIS_K_SDID_K11, "MK11D%s5" },
{ KINETIS_K_SDID_K12, "MK12D%s5" },
{ KINETIS_K_SDID_K20_M50, "MK20D%s5" },
{ KINETIS_K_SDID_K20_M72, "MK20D%s7" },
{ KINETIS_K_SDID_K20_M100, "MK20D%s10" },
{ KINETIS_K_SDID_K20_M120, "MK20F%s12" },
{ KINETIS_K_SDID_K21_M50, "MK21D%s5" },
{ KINETIS_K_SDID_K21_M120, "MK21F%s12" },
{ KINETIS_K_SDID_K22_M50, "MK22D%s5" },
{ KINETIS_K_SDID_K22_M120, "MK22F%s12" },
{ KINETIS_K_SDID_K30_M72, "MK30D%s7" },
{ KINETIS_K_SDID_K30_M100, "MK30D%s10" },
{ KINETIS_K_SDID_K40_M72, "MK40D%s7" },
{ KINETIS_K_SDID_K40_M100, "MK40D%s10" },
{ KINETIS_K_SDID_K50_M72, "MK50D%s7" },
{ KINETIS_K_SDID_K51_M72, "MK51D%s7" },
{ KINETIS_K_SDID_K53, "MK53D%s10" },
{ KINETIS_K_SDID_K60_M100, "MK60D%s10" },
{ KINETIS_K_SDID_K60_M150, "MK60F%s15" },
{ KINETIS_K_SDID_K70_M150, "MK70F%s15" },
};
#define MDM_AP 1
#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 MDM_CTRL_FMEIP (1<<0)
#define MDM_CTRL_DBG_DIS (1<<1)
#define MDM_CTRL_DBG_REQ (1<<2)
#define MDM_CTRL_SYS_RES_REQ (1<<3)
#define MDM_CTRL_CORE_HOLD_RES (1<<4)
#define MDM_CTRL_VLLSX_DBG_REQ (1<<5)
#define MDM_CTRL_VLLSX_DBG_ACK (1<<6)
#define MDM_CTRL_VLLSX_STAT_ACK (1<<7)
#define MDM_ACCESS_TIMEOUT 500 /* msec */
static bool allow_fcf_writes;
static uint8_t fcf_fopt = 0xff;
static bool create_banks;
const struct flash_driver kinetis_flash;
static int kinetis_write_inner(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count);
static int kinetis_probe_chip(struct kinetis_chip *k_chip);
static int kinetis_probe_chip_s32k(struct kinetis_chip *k_chip);
static int kinetis_auto_probe(struct flash_bank *bank);
static int kinetis_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, MDM_AP);
if (!ap) {
LOG_DEBUG("MDM: failed to get AP");
return ERROR_FAIL;
}
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;
}
retval = dap_run(dap);
dap_put_ap(ap);
if (retval != ERROR_OK) {
LOG_DEBUG("MDM: dap_run failed");
return retval;
}
return ERROR_OK;
}
static int kinetis_mdm_read_register(struct adiv5_dap *dap, unsigned reg, uint32_t *result)
{
struct adiv5_ap *ap = dap_get_ap(dap, MDM_AP);
if (!ap) {
LOG_DEBUG("MDM: failed to get AP");
return ERROR_FAIL;
}
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;
}
retval = dap_run(dap);
dap_put_ap(ap);
if (retval != ERROR_OK) {
LOG_DEBUG("MDM: dap_run failed");
return retval;
}
LOG_DEBUG("MDM_REG[0x%02x]: %08" PRIX32, reg, *result);
return ERROR_OK;
}
static int kinetis_mdm_poll_register(struct adiv5_dap *dap, unsigned reg,
uint32_t mask, uint32_t value, uint32_t timeout_ms)
{
uint32_t val;
int retval;
int64_t ms_timeout = timeval_ms() + timeout_ms;
do {
retval = kinetis_mdm_read_register(dap, reg, &val);
if (retval != ERROR_OK || (val & mask) == value)
return retval;
alive_sleep(1);
} while (timeval_ms() < ms_timeout);
LOG_DEBUG("MDM: polling timed out");
return ERROR_FAIL;
}
/*
* This command can be used to break a watchdog reset loop when
* connecting to an unsecured target. Unlike other commands, halt will
* automatically retry as it does not know how far into the boot process
* it is when the command is called.
*/
COMMAND_HANDLER(kinetis_mdm_halt)
{
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;
int retval;
int tries = 0;
uint32_t stat;
int64_t ms_timeout = timeval_ms() + MDM_ACCESS_TIMEOUT;
if (!dap) {
LOG_ERROR("Cannot perform halt with a high-level adapter");
return ERROR_FAIL;
}
while (true) {
tries++;
kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_CORE_HOLD_RES);
alive_sleep(1);
retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &stat);
if (retval != ERROR_OK) {
LOG_DEBUG("MDM: failed to read MDM_REG_STAT");
continue;
}
/* Repeat setting MDM_CTRL_CORE_HOLD_RES until system is out of
* reset with flash ready and without security
*/
if ((stat & (MDM_STAT_FREADY | MDM_STAT_SYSSEC | MDM_STAT_SYSRES))
== (MDM_STAT_FREADY | MDM_STAT_SYSRES))
break;
if (timeval_ms() >= ms_timeout) {
LOG_ERROR("MDM: halt timed out");
return ERROR_FAIL;
}
}
LOG_DEBUG("MDM: halt succeeded after %d attempts.", tries);
target_poll(target);
/* enable polling in case kinetis_check_flash_security_status disabled it */
jtag_poll_set_enabled(true);
alive_sleep(100);
target->reset_halt = true;
target->type->assert_reset(target);
retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
return retval;
}
target->type->deassert_reset(target);
return ERROR_OK;
}
COMMAND_HANDLER(kinetis_mdm_reset)
{
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;
int retval;
if (!dap) {
LOG_ERROR("Cannot perform reset with a high-level adapter");
return ERROR_FAIL;
}
retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_SYS_RES_REQ);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: failed to write MDM_REG_CTRL");
return retval;
}
retval = kinetis_mdm_poll_register(dap, MDM_REG_STAT, MDM_STAT_SYSRES, 0, 500);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: failed to assert reset");
return retval;
}
retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
return retval;
}
return ERROR_OK;
}
/*
* This function implements the procedure to mass erase the flash via
* SWD/JTAG on Kinetis K and L series of devices as it is described in
* AN4835 "Production Flash Programming Best Practices for Kinetis K-
* and L-series MCUs" Section 4.2.1. To prevent a watchdog reset loop,
* the core remains halted after this function completes as suggested
* by the application note.
*/
COMMAND_HANDLER(kinetis_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;
}
int retval;
/*
* ... Power on the processor, or if power has already been
* applied, assert the RESET pin to reset the processor. For
* devices that do not have a RESET pin, write the System
* Reset Request bit in the MDM-AP control register after
* establishing communication...
*/
/* assert SRST if configured */
bool has_srst = jtag_get_reset_config() & RESET_HAS_SRST;
if (has_srst)
adapter_assert_reset();
retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_SYS_RES_REQ);
if (retval != ERROR_OK && !has_srst) {
LOG_ERROR("MDM: failed to assert reset");
goto deassert_reset_and_exit;
}
/*
* ... Read the MDM-AP status register repeatedly and wait for
* stable conditions suitable for mass erase:
* - mass erase is enabled
* - flash is ready
* - reset is finished
*
* Mass erase is started as soon as all conditions are met in 32
* subsequent status reads.
*
* In case of not stable conditions (RESET/WDOG loop in secured device)
* the user is asked for manual pressing of RESET button
* as a last resort.
*/
int cnt_mass_erase_disabled = 0;
int cnt_ready = 0;
int64_t ms_start = timeval_ms();
bool man_reset_requested = false;
do {
uint32_t stat = 0;
int64_t ms_elapsed = timeval_ms() - ms_start;
if (!man_reset_requested && ms_elapsed > 100) {
LOG_INFO("MDM: Press RESET button now if possible.");
man_reset_requested = true;
}
if (ms_elapsed > 3000) {
LOG_ERROR("MDM: waiting for mass erase conditions timed out.");
LOG_INFO("Mass erase of a secured MCU is not possible without hardware reset.");
LOG_INFO("Connect SRST, use 'reset_config srst_only' and retry.");
goto deassert_reset_and_exit;
}
retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &stat);
if (retval != ERROR_OK) {
cnt_ready = 0;
continue;
}
if (!(stat & MDM_STAT_FMEEN)) {
cnt_ready = 0;
cnt_mass_erase_disabled++;
if (cnt_mass_erase_disabled > 10) {
LOG_ERROR("MDM: mass erase is disabled");
goto deassert_reset_and_exit;
}
continue;
}
if ((stat & (MDM_STAT_FREADY | MDM_STAT_SYSRES)) == MDM_STAT_FREADY)
cnt_ready++;
else
cnt_ready = 0;
} while (cnt_ready < 32);
/*
* ... Write the MDM-AP control register to set the Flash Mass
* Erase in Progress bit. This will start the mass erase
* process...
*/
retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_SYS_RES_REQ | MDM_CTRL_FMEIP);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: failed to start mass erase");
goto deassert_reset_and_exit;
}
/*
* ... Read the MDM-AP control register until the Flash Mass
* Erase in Progress bit clears...
* Data sheed defines erase time <3.6 sec/512kB flash block.
* The biggest device has 4 pflash blocks => timeout 16 sec.
*/
retval = kinetis_mdm_poll_register(dap, MDM_REG_CTRL, MDM_CTRL_FMEIP, 0, 16000);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: mass erase timeout");
goto deassert_reset_and_exit;
}
target_poll(target);
/* enable polling in case kinetis_check_flash_security_status disabled it */
jtag_poll_set_enabled(true);
alive_sleep(100);
target->reset_halt = true;
target->type->assert_reset(target);
/*
* ... Negate the RESET signal or clear the System Reset Request
* bit in the MDM-AP control register.
*/
retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
if (retval != ERROR_OK)
LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
target->type->deassert_reset(target);
return retval;
deassert_reset_and_exit:
kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
if (has_srst)
adapter_deassert_reset();
return retval;
}
static const uint32_t kinetis_known_mdm_ids[] = {
0x001C0000, /* Kinetis-K Series */
0x001C0020, /* Kinetis-L/M/V/E Series */
0x001C0030, /* Kinetis with a Cortex-M7, in time of writing KV58 */
};
/*
* This function implements the procedure to connect to
* SWD/JTAG on Kinetis K and L series of devices as it is described in
* AN4835 "Production Flash Programming Best Practices for Kinetis K-
* and L-series MCUs" Section 4.1.1
*/
COMMAND_HANDLER(kinetis_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->ops)
return ERROR_OK; /* too early to check, in JTAG mode ops may not be initialised */
uint32_t val;
int retval;
/*
* ... The MDM-AP ID register can be read to verify that the
* connection is working correctly...
*/
retval = kinetis_mdm_read_register(dap, MDM_REG_ID, &val);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: failed to read ID register");
return ERROR_OK;
}
if (val == 0)
return ERROR_OK; /* dap not yet initialised */
bool found = false;
for (size_t i = 0; i < ARRAY_SIZE(kinetis_known_mdm_ids); i++) {
if (val == kinetis_known_mdm_ids[i]) {
found = true;
break;
}
}
if (!found)
LOG_WARNING("MDM: unknown ID %08" PRIX32, val);
/*
* ... Read the System Security bit to determine if security is enabled.
* If System Security = 0, then proceed. If System Security = 1, then
* communication with the internals of the processor, including the
* flash, will not be possible without issuing a mass erase command or
* unsecuring the part through other means (backdoor key unlock)...
*/
retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &val);
if (retval != ERROR_OK) {
LOG_ERROR("MDM: failed to read MDM_REG_STAT");
return ERROR_OK;
}
/*
* System Security bit is also active for short time during reset.
* If a MCU has blank flash and runs in RESET/WDOG loop,
* System Security bit is active most of time!
* We should observe Flash Ready bit and read status several times
* to avoid false detection of secured MCU
*/
int secured_score = 0, flash_not_ready_score = 0;
if ((val & (MDM_STAT_SYSSEC | MDM_STAT_FREADY)) != MDM_STAT_FREADY) {
uint32_t stats[32];
struct adiv5_ap *ap = dap_get_ap(dap, MDM_AP);
if (!ap) {
LOG_ERROR("MDM: failed to get AP");
return ERROR_OK;
}
for (unsigned int i = 0; i < 32; i++) {
stats[i] = MDM_STAT_FREADY;
dap_queue_ap_read(ap, MDM_REG_STAT, &stats[i]);
}
retval = dap_run(dap);
dap_put_ap(ap);
if (retval != ERROR_OK) {
LOG_DEBUG("MDM: dap_run failed when validating secured state");
return ERROR_OK;
}
for (unsigned int i = 0; i < 32; i++) {
if (stats[i] & MDM_STAT_SYSSEC)
secured_score++;
if (!(stats[i] & MDM_STAT_FREADY))
flash_not_ready_score++;
}
}
if (flash_not_ready_score <= 8 && secured_score > 24) {
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 mdm mass_erase' ****");
LOG_WARNING("**** command, power cycle the MCU and restart OpenOCD. ****");
LOG_WARNING("**** ****");
LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
} else if (flash_not_ready_score > 24) {
jtag_poll_set_enabled(false);
LOG_WARNING("**** Your Kinetis MCU is probably locked-up in RESET/WDOG loop. ****");
LOG_WARNING("**** Common reason is a blank flash (at least a reset vector). ****");
LOG_WARNING("**** Issue 'kinetis mdm halt' command or if SRST is connected ****");
LOG_WARNING("**** and configured, use 'reset halt' ****");
LOG_WARNING("**** If MCU cannot be halted, it is likely secured and running ****");
LOG_WARNING("**** in RESET/WDOG loop. Issue 'kinetis mdm mass_erase' ****");
} else {
LOG_INFO("MDM: Chip is unsecured. Continuing.");
jtag_poll_set_enabled(true);
}
return ERROR_OK;
}
static struct kinetis_chip *kinetis_get_chip(struct target *target)
{
struct flash_bank *bank_iter;
struct kinetis_flash_bank *k_bank;
/* iterate over all kinetis banks */
for (bank_iter = flash_bank_list(); bank_iter; bank_iter = bank_iter->next) {
if (bank_iter->driver != &kinetis_flash
|| bank_iter->target != target)
continue;
k_bank = bank_iter->driver_priv;
if (!k_bank)
continue;
if (k_bank->k_chip)
return k_bank->k_chip;
}
return NULL;
}
static int kinetis_chip_options(struct kinetis_chip *k_chip, int argc, const char *argv[])
{
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-sim-base") == 0) {
if (i + 1 < argc)
k_chip->sim_base = strtoul(argv[++i], NULL, 0);
} else if (strcmp(argv[i], "-s32k") == 0) {
k_chip->chip_type = CT_S32K;
} else
LOG_ERROR("Unsupported flash bank option %s", argv[i]);
}
return ERROR_OK;
}
FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
{
struct target *target = bank->target;
struct kinetis_chip *k_chip;
struct kinetis_flash_bank *k_bank;
int retval;
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
LOG_INFO("add flash_bank kinetis %s", bank->name);
k_chip = kinetis_get_chip(target);
if (!k_chip) {
k_chip = calloc(1, sizeof(struct kinetis_chip));
if (!k_chip) {
LOG_ERROR("No memory");
return ERROR_FAIL;
}
k_chip->target = target;
/* only the first defined bank can define chip options */
retval = kinetis_chip_options(k_chip, CMD_ARGC - 6, CMD_ARGV + 6);
if (retval != ERROR_OK)
return retval;
}
if (k_chip->num_banks >= KINETIS_MAX_BANKS) {
LOG_ERROR("Only %u Kinetis flash banks are supported", KINETIS_MAX_BANKS);
return ERROR_FAIL;
}
bank->driver_priv = k_bank = &(k_chip->banks[k_chip->num_banks]);
k_bank->k_chip = k_chip;
k_bank->bank_number = k_chip->num_banks;
k_bank->bank = bank;
k_chip->num_banks++;
return ERROR_OK;
}
static void kinetis_free_driver_priv(struct flash_bank *bank)
{
struct kinetis_flash_bank *k_bank = bank->driver_priv;
if (!k_bank)
return;
struct kinetis_chip *k_chip = k_bank->k_chip;
if (!k_chip)
return;
k_chip->num_banks--;
if (k_chip->num_banks == 0)
free(k_chip);
}
static int kinetis_create_missing_banks(struct kinetis_chip *k_chip)
{
unsigned num_blocks;
struct kinetis_flash_bank *k_bank;
struct flash_bank *bank;
char base_name[69], name[87], num[11];
char *class, *p;
num_blocks = k_chip->num_pflash_blocks + k_chip->num_nvm_blocks;
if (num_blocks > KINETIS_MAX_BANKS) {
LOG_ERROR("Only %u Kinetis flash banks are supported", KINETIS_MAX_BANKS);
return ERROR_FAIL;
}
bank = k_chip->banks[0].bank;
if (bank && bank->name) {
strncpy(base_name, bank->name, sizeof(base_name) - 1);
base_name[sizeof(base_name) - 1] = '\0';
p = strstr(base_name, ".pflash");
if (p) {
*p = '\0';
if (k_chip->num_pflash_blocks > 1) {
/* rename first bank if numbering is needed */