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
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
87
88
101
102
103
104
105
106
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
207
208
212
213
217
218
222
223
227
228
229
236
237
238
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
278
279
283
284
294
295
296
297
298
301
302
303
304
305
306
307
308
309
321
322
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
346
347
348
356
357
363
364
365
366
367
368
369
370
374
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
414
415
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
444
445
446
447
448
449
450
451
452
453
454
455
456
457
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
509
510
511
512
516
517
522
523
524
525
526
527
528
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
559
560
561
562
563
564
565
569
570
571
572
573
577
578
579
580
581
585
586
587
588
589
593
594
595
596
597
601
602
603
604
605
609
610
611
612
613
618
619
620
621
622
623
624
625
626
627
628
637
638
639
645
646
654
655
656
657
658
659
660
661
662
663
664
678
679
680
681
682
683
684
685
686
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
714
715
722
723
724
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
819
820
821
822
823
824
825
826
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
869
870
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
932
933
934
935
939
940
941
945
946
947
948
952
953
954
955
959
960
961
962
963
964
965
966
967
968
969
973
974
979
980
981
982
983
984
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
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
1084
1085
1086
1087
1088
1089
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1122
1123
1124
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1173
1174
1175
1176
1177
1178
1179
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include <helper/binarybuffer.h>
#include <target/algorithm.h>
#include <target/armv7m.h>
#include <target/cortex_m.h>
5 includes
#define EFM_FAMILY_ID_GIANT_GECKO 72
#define EFM_FAMILY_ID_LEOPARD_GECKO 74
#define EFM32_FLASH_ERASE_TMO 100
#define EFM32_FLASH_WDATAREADY_TMO 100
#define EFM32_FLASH_WRITE_TMO 100
#define EFM32_FLASH_BASE 0
#define LOCKWORDS_SZ 512
#define EFM32_MSC_INFO_BASE 0x0fe00000
#define EFM32_MSC_USER_DATA EFM32_MSC_INFO_BASE
#define EFM32_MSC_LOCK_BITS (EFM32_MSC_INFO_BASE+0x4000)
#define EFM32_MSC_LOCK_BITS_EXTRA (EFM32_MSC_LOCK_BITS+LOCKWORDS_SZ)
#define EFM32_MSC_DEV_INFO (EFM32_MSC_INFO_BASE+0x8000)
#define EFM32_MSC_DI_PAGE_SIZE (EFM32_MSC_DEV_INFO+0x1e7)
#define EFM32_MSC_DI_FLASH_SZ (EFM32_MSC_DEV_INFO+0x1f8)
#define EFM32_MSC_DI_RAM_SZ (EFM32_MSC_DEV_INFO+0x1fa)
#define EFM32_MSC_DI_PART_NUM (EFM32_MSC_DEV_INFO+0x1fc)
#define EFM32_MSC_DI_PART_FAMILY (EFM32_MSC_DEV_INFO+0x1fe)
#define EFM32_MSC_DI_PROD_REV (EFM32_MSC_DEV_INFO+0x1ff)
#define EFM32_MSC_REGBASE 0x400c0000
#define EFM32_MSC_REGBASE_SERIES1 0x400e0000
#define EFM32_MSC_REG_WRITECTRL 0x008
#define EFM32_MSC_WRITECTRL_WREN_MASK 0x1
#define EFM32_MSC_REG_WRITECMD 0x00c
#define EFM32_MSC_WRITECMD_LADDRIM_MASK 0x1
#define EFM32_MSC_WRITECMD_ERASEPAGE_MASK 0x2
#define EFM32_MSC_WRITECMD_WRITEONCE_MASK 0x8
#define EFM32_MSC_REG_ADDRB 0x010
#define EFM32_MSC_REG_WDATA 0x018
#define EFM32_MSC_REG_STATUS 0x01c
#define EFM32_MSC_STATUS_BUSY_MASK 0x1
#define EFM32_MSC_STATUS_LOCKED_MASK 0x2
#define EFM32_MSC_STATUS_INVADDR_MASK 0x4
#define EFM32_MSC_STATUS_WDATAREADY_MASK 0x8
#define EFM32_MSC_STATUS_WORDTIMEOUT_MASK 0x10
#define EFM32_MSC_STATUS_ERASEABORTED_MASK 0x20
#define EFM32_MSC_REG_LOCK 0x03c
#define EFM32_MSC_REG_LOCK_SERIES1 0x040
#define EFM32_MSC_LOCK_LOCKKEY 0x1b71
38 defines
enum efm32_bank_index {
EFM32_BANK_INDEX_MAIN,
EFM32_BANK_INDEX_USER_DATA,
EFM32_BANK_INDEX_LOCK_BITS,
EFM32_N_BANKS
...};
static int efm32x_get_bank_index(target_addr_t base)
{
switch (base) {
case EFM32_FLASH_BASE:
return EFM32_BANK_INDEX_MAIN;case EFM32_FLASH_BASE:
case EFM32_MSC_USER_DATA:
return EFM32_BANK_INDEX_USER_DATA;case EFM32_MSC_USER_DATA:
case EFM32_MSC_LOCK_BITS:
return EFM32_BANK_INDEX_LOCK_BITS;case EFM32_MSC_LOCK_BITS:
default:
return ERROR_FAIL;default
}switch (base) { ... }
}{ ... }
struct efm32_family_data {
int family_id;
const char *name;
/* ... */
int series;
int page_size;
uint32_t msc_regbase;
...};
struct efm32_info {
const struct efm32_family_data *family_data;
uint16_t flash_sz_kib;
uint16_t ram_sz_kib;
uint16_t part_num;
uint8_t part_family;
uint8_t prod_rev;
uint16_t page_size;
...};
struct efm32x_flash_chip {
struct efm32_info info;
bool probed[EFM32_N_BANKS];
uint32_t lb_page[LOCKWORDS_SZ/4];
uint32_t reg_base;
uint32_t reg_lock;
uint32_t refcount;
...};
static const struct efm32_family_data efm32_families[] = {
{ 16, "EFR32MG1P Mighty", .series = 1 },
{ 17, "EFR32MG1B Mighty", .series = 1 },
{ 18, "EFR32MG1V Mighty", .series = 1 },
{ 19, "EFR32BG1P Blue", .series = 1 },
{ 20, "EFR32BG1B Blue", .series = 1 },
{ 21, "EFR32BG1V Blue", .series = 1 },
{ 25, "EFR32FG1P Flex", .series = 1 },
{ 26, "EFR32FG1B Flex", .series = 1 },
{ 27, "EFR32FG1V Flex", .series = 1 },
{ 28, "EFR32MG2P Mighty", .series = 1 },
{ 29, "EFR32MG2B Mighty", .series = 1 },
{ 30, "EFR32MG2V Mighty", .series = 1 },
{ 31, "EFR32BG12P Blue", .series = 1 },
{ 32, "EFR32BG12B Blue", .series = 1 },
{ 33, "EFR32BG12V Blue", .series = 1 },
{ 37, "EFR32FG12P Flex", .series = 1 },
{ 38, "EFR32FG12B Flex", .series = 1 },
{ 39, "EFR32FG12V Flex", .series = 1 },
{ 40, "EFR32MG13P Mighty", .series = 1 },
{ 41, "EFR32MG13B Mighty", .series = 1 },
{ 42, "EFR32MG13V Mighty", .series = 1 },
{ 43, "EFR32BG13P Blue", .series = 1 },
{ 44, "EFR32BG13B Blue", .series = 1 },
{ 45, "EFR32BG13V Blue", .series = 1 },
{ 46, "EFR32ZG13P Zen", .series = 1 },
{ 49, "EFR32FG13P Flex", .series = 1 },
{ 50, "EFR32FG13B Flex", .series = 1 },
{ 51, "EFR32FG13V Flex", .series = 1 },
{ 52, "EFR32MG14P Mighty", .series = 1 },
{ 53, "EFR32MG14B Mighty", .series = 1 },
{ 54, "EFR32MG14V Mighty", .series = 1 },
{ 55, "EFR32BG14P Blue", .series = 1 },
{ 56, "EFR32BG14B Blue", .series = 1 },
{ 57, "EFR32BG14V Blue", .series = 1 },
{ 58, "EFR32ZG14P Zen", .series = 1 },
{ 61, "EFR32FG14P Flex", .series = 1 },
{ 62, "EFR32FG14B Flex", .series = 1 },
{ 63, "EFR32FG14V Flex", .series = 1 },
{ 71, "EFM32G", .series = 0, .page_size = 512 },
{ 72, "EFM32GG Giant", .series = 0 },
{ 73, "EFM32TG Tiny", .series = 0, .page_size = 512 },
{ 74, "EFM32LG Leopard", .series = 0 },
{ 75, "EFM32WG Wonder", .series = 0 },
{ 76, "EFM32ZG Zero", .series = 0, .page_size = 1024 },
{ 77, "EFM32HG Happy", .series = 0, .page_size = 1024 },
{ 81, "EFM32PG1B Pearl", .series = 1 },
{ 83, "EFM32JG1B Jade", .series = 1 },
{ 85, "EFM32PG12B Pearl", .series = 1 },
{ 87, "EFM32JG12B Jade", .series = 1 },
{ 89, "EFM32PG13B Pearl", .series = 1 },
{ 91, "EFM32JG13B Jade", .series = 1 },
{ 100, "EFM32GG11B Giant", .series = 1, .msc_regbase = 0x40000000 },
{ 103, "EFM32TG11B Tiny", .series = 1, .msc_regbase = 0x40000000 },
{ 106, "EFM32GG12B Giant", .series = 1, .msc_regbase = 0x40000000 },
{ 120, "EZR32WG Wonder", .series = 0 },
{ 121, "EZR32LG Leopard", .series = 0 },
{ 122, "EZR32HG Happy", .series = 0, .page_size = 1024 },
...};
const struct flash_driver efm32_flash;
static int efm32x_priv_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t addr, uint32_t count);
static int efm32x_write_only_lockbits(struct flash_bank *bank);
static int efm32x_get_flash_size(struct flash_bank *bank, uint16_t *flash_sz)
{
return target_read_u16(bank->target, EFM32_MSC_DI_FLASH_SZ, flash_sz);
}{ ... }
static int efm32x_get_ram_size(struct flash_bank *bank, uint16_t *ram_sz)
{
return target_read_u16(bank->target, EFM32_MSC_DI_RAM_SZ, ram_sz);
}{ ... }
static int efm32x_get_part_num(struct flash_bank *bank, uint16_t *pnum)
{
return target_read_u16(bank->target, EFM32_MSC_DI_PART_NUM, pnum);
}{ ... }
static int efm32x_get_part_family(struct flash_bank *bank, uint8_t *pfamily)
{
return target_read_u8(bank->target, EFM32_MSC_DI_PART_FAMILY, pfamily);
}{ ... }
static int efm32x_get_prod_rev(struct flash_bank *bank, uint8_t *prev)
{
return target_read_u8(bank->target, EFM32_MSC_DI_PROD_REV, prev);
}{ ... }
static int efm32x_read_reg_u32(struct flash_bank *bank, target_addr_t offset,
uint32_t *value)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
uint32_t base = efm32x_info->reg_base;
return target_read_u32(bank->target, base + offset, value);
}{ ... }
static int efm32x_write_reg_u32(struct flash_bank *bank, target_addr_t offset,
uint32_t value)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
uint32_t base = efm32x_info->reg_base;
return target_write_u32(bank->target, base + offset, value);
}{ ... }
static int efm32x_read_info(struct flash_bank *bank)
{
int ret;
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
struct efm32_info *efm32_info = &(efm32x_info->info);
memset(efm32_info, 0, sizeof(struct efm32_info));
ret = efm32x_get_flash_size(bank, &(efm32_info->flash_sz_kib));
if (ret != ERROR_OK)
return ret;
ret = efm32x_get_ram_size(bank, &(efm32_info->ram_sz_kib));
if (ret != ERROR_OK)
return ret;
ret = efm32x_get_part_num(bank, &(efm32_info->part_num));
if (ret != ERROR_OK)
return ret;
ret = efm32x_get_part_family(bank, &(efm32_info->part_family));
if (ret != ERROR_OK)
return ret;
ret = efm32x_get_prod_rev(bank, &(efm32_info->prod_rev));
if (ret != ERROR_OK)
return ret;
for (size_t i = 0; i < ARRAY_SIZE(efm32_families); i++) {
if (efm32_families[i].family_id == efm32_info->part_family)
efm32_info->family_data = &efm32_families[i];
}for (size_t i = 0; i < ARRAY_SIZE(efm32_families); i++) { ... }
if (!efm32_info->family_data) {
LOG_ERROR("Unknown MCU family %d", efm32_info->part_family);
return ERROR_FAIL;
}if (!efm32_info->family_data) { ... }
switch (efm32_info->family_data->series) {
case 0:
efm32x_info->reg_base = EFM32_MSC_REGBASE;
efm32x_info->reg_lock = EFM32_MSC_REG_LOCK;
break;case 0:
case 1:
efm32x_info->reg_base = EFM32_MSC_REGBASE_SERIES1;
efm32x_info->reg_lock = EFM32_MSC_REG_LOCK_SERIES1;
break;case 1:
}switch (efm32_info->family_data->series) { ... }
if (efm32_info->family_data->msc_regbase != 0)
efm32x_info->reg_base = efm32_info->family_data->msc_regbase;
if (efm32_info->family_data->page_size != 0) {
efm32_info->page_size = efm32_info->family_data->page_size;
}if (efm32_info->family_data->page_size != 0) { ... } else {
uint8_t pg_size = 0;
ret = target_read_u8(bank->target, EFM32_MSC_DI_PAGE_SIZE,
&pg_size);
if (ret != ERROR_OK)
return ret;
efm32_info->page_size = (1 << ((pg_size+10) & 0xff));
if (efm32_info->part_family == EFM_FAMILY_ID_GIANT_GECKO ||
efm32_info->part_family == EFM_FAMILY_ID_LEOPARD_GECKO) {
if (efm32_info->prod_rev < 18) {
/* ... */
if (efm32_info->flash_sz_kib < 512)
efm32_info->page_size = 2048;
else
efm32_info->page_size = 4096;
}if (efm32_info->prod_rev < 18) { ... }
}if (efm32_info->part_family == EFM_FAMILY_ID_GIANT_GECKO || efm32_info->part_family == EFM_FAMILY_ID_LEOPARD_GECKO) { ... }
if ((efm32_info->page_size != 2048) &&
(efm32_info->page_size != 4096)) {
LOG_ERROR("Invalid page size %u", efm32_info->page_size);
return ERROR_FAIL;
}if ((efm32_info->page_size != 2048) && (efm32_info->page_size != 4096)) { ... }
}else { ... }
return ERROR_OK;
}{ ... }
FLASH_BANK_COMMAND_HANDLER(efm32x_flash_bank_command)
{
struct efm32x_flash_chip *efm32x_info = NULL;
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
int bank_index = efm32x_get_bank_index(bank->base);
if (bank_index < 0) {
LOG_ERROR("Flash bank with base address %" PRIx32 " is not supported",
(uint32_t) bank->base);
return ERROR_FAIL;
}if (bank_index < 0) { ... }
for (struct flash_bank *bank_iter = flash_bank_list(); bank_iter; bank_iter = bank_iter->next) {
if (bank_iter->driver == &efm32_flash
&& bank_iter->target == bank->target
&& bank->driver_priv) {
efm32x_info = bank->driver_priv;
break;
}if (bank_iter->driver == &efm32_flash && bank_iter->target == bank->target && bank->driver_priv) { ... }
}for (struct flash_bank *bank_iter = flash_bank_list(); bank_iter; bank_iter = bank_iter->next) { ... }
if (!efm32x_info) {
efm32x_info = calloc(1, sizeof(struct efm32x_flash_chip));
memset(efm32x_info->lb_page, 0xff, LOCKWORDS_SZ);
}if (!efm32x_info) { ... }
++efm32x_info->refcount;
bank->driver_priv = efm32x_info;
return ERROR_OK;
}{ ... }
/* ... */
static void efm32x_free_driver_priv(struct flash_bank *bank)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
if (efm32x_info) {
/* ... */
--efm32x_info->refcount;
if (efm32x_info->refcount == 0) {
free(efm32x_info);
bank->driver_priv = NULL;
}if (efm32x_info->refcount == 0) { ... }
}if (efm32x_info) { ... }
}{ ... }
static int efm32x_set_reg_bits(struct flash_bank *bank, uint32_t reg,
uint32_t bitmask, int set)
{
int ret = 0;
uint32_t reg_val = 0;
ret = efm32x_read_reg_u32(bank, reg, ®_val);
if (ret != ERROR_OK)
return ret;
if (set)
reg_val |= bitmask;
else
reg_val &= ~bitmask;
return efm32x_write_reg_u32(bank, reg, reg_val);
}{ ... }
static int efm32x_set_wren(struct flash_bank *bank, int write_enable)
{
return efm32x_set_reg_bits(bank, EFM32_MSC_REG_WRITECTRL,
EFM32_MSC_WRITECTRL_WREN_MASK, write_enable);
}{ ... }
static int efm32x_msc_lock(struct flash_bank *bank, int lock)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
return efm32x_write_reg_u32(bank, efm32x_info->reg_lock,
(lock ? 0 : EFM32_MSC_LOCK_LOCKKEY));
}{ ... }
static int efm32x_wait_status(struct flash_bank *bank, int timeout,
uint32_t wait_mask, int wait_for_set)
{
int ret = 0;
uint32_t status = 0;
while (1) {
ret = efm32x_read_reg_u32(bank, EFM32_MSC_REG_STATUS, &status);
if (ret != ERROR_OK)
break;
LOG_DEBUG("status: 0x%" PRIx32 "", status);
if (((status & wait_mask) == 0) && (wait_for_set == 0))
break;
else if (((status & wait_mask) != 0) && wait_for_set)
break;
if (timeout-- <= 0) {
LOG_ERROR("timed out waiting for MSC status");
return ERROR_FAIL;
}if (timeout-- <= 0) { ... }
alive_sleep(1);
}while (1) { ... }
if (status & EFM32_MSC_STATUS_ERASEABORTED_MASK)
LOG_WARNING("page erase was aborted");
return ret;
}{ ... }
static int efm32x_erase_page(struct flash_bank *bank, uint32_t addr)
{
/* ... */
int ret = 0;
uint32_t status = 0;
LOG_DEBUG("erasing flash page at 0x%08" PRIx32, addr);
ret = efm32x_write_reg_u32(bank, EFM32_MSC_REG_ADDRB, addr);
if (ret != ERROR_OK)
return ret;
ret = efm32x_set_reg_bits(bank, EFM32_MSC_REG_WRITECMD,
EFM32_MSC_WRITECMD_LADDRIM_MASK, 1);
if (ret != ERROR_OK)
return ret;
ret = efm32x_read_reg_u32(bank, EFM32_MSC_REG_STATUS, &status);
if (ret != ERROR_OK)
return ret;
LOG_DEBUG("status 0x%" PRIx32, status);
if (status & EFM32_MSC_STATUS_LOCKED_MASK) {
LOG_ERROR("Page is locked");
return ERROR_FAIL;
}if (status & EFM32_MSC_STATUS_LOCKED_MASK) { ... } else if (status & EFM32_MSC_STATUS_INVADDR_MASK) {
LOG_ERROR("Invalid address 0x%" PRIx32, addr);
return ERROR_FAIL;
}else if (status & EFM32_MSC_STATUS_INVADDR_MASK) { ... }
ret = efm32x_set_reg_bits(bank, EFM32_MSC_REG_WRITECMD,
EFM32_MSC_WRITECMD_ERASEPAGE_MASK, 1);
if (ret != ERROR_OK)
return ret;
return efm32x_wait_status(bank, EFM32_FLASH_ERASE_TMO,
EFM32_MSC_STATUS_BUSY_MASK, 0);
}{ ... }
static int efm32x_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
struct target *target = bank->target;
int ret = 0;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
efm32x_msc_lock(bank, 0);
ret = efm32x_set_wren(bank, 1);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to enable MSC write");
return ret;
}if (ret != ERROR_OK) { ... }
for (unsigned int i = first; i <= last; i++) {
ret = efm32x_erase_page(bank, bank->base + bank->sectors[i].offset);
if (ret != ERROR_OK)
LOG_ERROR("Failed to erase page %d", i);
}for (unsigned int i = first; i <= last; i++) { ... }
ret = efm32x_set_wren(bank, 0);
efm32x_msc_lock(bank, 1);
if (ret != ERROR_OK)
return ret;
if (bank->base == EFM32_MSC_LOCK_BITS) {
ret = efm32x_write_only_lockbits(bank);
if (ret != ERROR_OK)
LOG_ERROR("Failed to restore lockbits after erase");
}if (bank->base == EFM32_MSC_LOCK_BITS) { ... }
return ret;
}{ ... }
static int efm32x_read_lock_data(struct flash_bank *bank)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
struct target *target = bank->target;
int data_size = 0;
uint32_t *ptr = NULL;
int ret = 0;
assert(bank->num_sectors > 0);
data_size = (bank->num_sectors + 31) / 32;
ptr = efm32x_info->lb_page;
for (int i = 0; i < data_size; i++, ptr++) {
ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+i*4, ptr);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read PLW %d", i);
return ret;
}if (ret != ERROR_OK) { ... }
}for (int i = 0; i < data_size; i++, ptr++) { ... }
ptr = efm32x_info->lb_page + 126;
ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+126*4, ptr);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read ULW");
return ret;
}if (ret != ERROR_OK) { ... }
ptr = efm32x_info->lb_page + 127;
ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+127*4, ptr);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read DLW");
return ret;
}if (ret != ERROR_OK) { ... }
ptr = efm32x_info->lb_page + 125;
ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+125*4, ptr);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read MLW");
return ret;
}if (ret != ERROR_OK) { ... }
ptr = efm32x_info->lb_page + 124;
ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+124*4, ptr);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read ALW");
return ret;
}if (ret != ERROR_OK) { ... }
ptr = efm32x_info->lb_page + 123;
ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+123*4, ptr);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read CLW1");
return ret;
}if (ret != ERROR_OK) { ... }
ptr = efm32x_info->lb_page + 122;
ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+122*4, ptr);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read CLW0");
return ret;
}if (ret != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int efm32x_write_only_lockbits(struct flash_bank *bank)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
return efm32x_priv_write(bank, (uint8_t *)efm32x_info->lb_page, EFM32_MSC_LOCK_BITS, LOCKWORDS_SZ);
}{ ... }
static int efm32x_write_lock_data(struct flash_bank *bank)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
int ret = 0;
assert(efm32x_info->info.page_size >= LOCKWORDS_SZ);
uint32_t extra_bytes = efm32x_info->info.page_size - LOCKWORDS_SZ;
uint8_t *extra_data = NULL;
if (extra_bytes) {
extra_data = malloc(extra_bytes);
ret = target_read_buffer(bank->target, EFM32_MSC_LOCK_BITS_EXTRA, extra_bytes, extra_data);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read extra contents of LB page");
free(extra_data);
return ret;
}if (ret != ERROR_OK) { ... }
}if (extra_bytes) { ... }
ret = efm32x_erase_page(bank, EFM32_MSC_LOCK_BITS);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to erase LB page");
if (extra_data)
free(extra_data);
return ret;
}if (ret != ERROR_OK) { ... }
if (extra_data) {
ret = efm32x_priv_write(bank, extra_data, EFM32_MSC_LOCK_BITS_EXTRA, extra_bytes);
free(extra_data);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to restore extra contents of LB page");
return ret;
}if (ret != ERROR_OK) { ... }
}if (extra_data) { ... }
return efm32x_write_only_lockbits(bank);
}{ ... }
static int efm32x_get_page_lock(struct flash_bank *bank, size_t page)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
uint32_t dw = 0;
uint32_t mask = 0;
switch (bank->base) {
case EFM32_FLASH_BASE:
dw = efm32x_info->lb_page[page >> 5];
mask = 1 << (page & 0x1f);
break;case EFM32_FLASH_BASE:
case EFM32_MSC_USER_DATA:
dw = efm32x_info->lb_page[126];
mask = 0x1;
break;case EFM32_MSC_USER_DATA:
case EFM32_MSC_LOCK_BITS:
dw = efm32x_info->lb_page[126];
mask = 0x2;
break;case EFM32_MSC_LOCK_BITS:
}switch (bank->base) { ... }
return (dw & mask) ? 0 : 1;
}{ ... }
static int efm32x_set_page_lock(struct flash_bank *bank, size_t page, int set)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
if (bank->base != EFM32_FLASH_BASE) {
LOG_ERROR("Locking user and lockbits pages is not supported yet");
return ERROR_FAIL;
}if (bank->base != EFM32_FLASH_BASE) { ... }
uint32_t *dw = &efm32x_info->lb_page[page >> 5];
uint32_t mask = 0;
mask = 1 << (page & 0x1f);
if (!set)
*dw |= mask;
else
*dw &= ~mask;
return ERROR_OK;
}{ ... }
static int efm32x_protect(struct flash_bank *bank, int set, unsigned int first,
unsigned int last)
{
struct target *target = bank->target;
int ret = 0;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
for (unsigned int i = first; i <= last; i++) {
ret = efm32x_set_page_lock(bank, i, set);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to set lock on page %d", i);
return ret;
}if (ret != ERROR_OK) { ... }
}for (unsigned int i = first; i <= last; i++) { ... }
ret = efm32x_write_lock_data(bank);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to write LB page");
return ret;
}if (ret != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int efm32x_write_block(struct flash_bank *bank, const uint8_t *buf,
uint32_t address, uint32_t count)
{
struct target *target = bank->target;
uint32_t buffer_size = 16384;
struct working_area *write_algorithm;
struct working_area *source;
struct reg_param reg_params[5];
struct armv7m_algorithm armv7m_info;
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
int ret = ERROR_OK;
static const uint8_t efm32x_flash_write_code[] = {
0x01, 0x26,
0x86, 0x60,
0x16, 0x68,
0x00, 0x2e,
0x22, 0xd0,
0x55, 0x68,
0xb5, 0x42,
0xf9, 0xd0,
0x04, 0x61,
0x01, 0x26,
0xc6, 0x60,
0xc6, 0x69,
0x06, 0x27,
0x3e, 0x42,
0x16, 0xd1,
0xc6, 0x69,
0x08, 0x27,
0x3e, 0x42,
0xfb, 0xd0,
0x2e, 0x68,
0x86, 0x61,
0x08, 0x26,
0xc6, 0x60,
0x04, 0x35,
0x04, 0x34,
0xc6, 0x69,
0x01, 0x27,
0x3e, 0x42,
0xfb, 0xd1,
0x9d, 0x42,
0x01, 0xd3,
0x15, 0x46,
0x08, 0x35,
0x55, 0x60,
0x01, 0x39,
0x00, 0x29,
0x02, 0xd0,
0xdb, 0xe7,
0x00, 0x20,
0x50, 0x60,
0x30, 0x46,
0x00, 0xbe,
...};
if (target_alloc_working_area(target, sizeof(efm32x_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(efm32x_flash_write_code), &write_algorithm) != ERROR_OK) { ... }
ret = target_write_buffer(target, write_algorithm->address,
sizeof(efm32x_flash_write_code), efm32x_flash_write_code);
if (ret != ERROR_OK)
return ret;
while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) {
buffer_size /= 2;
buffer_size &= ~3UL;
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);
buf_set_u32(reg_params[0].value, 0, 32, efm32x_info->reg_base);
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;
ret = target_run_flash_async_algorithm(target, buf, count, 4,
0, NULL,
5, reg_params,
source->address, source->size,
write_algorithm->address, 0,
&armv7m_info);
if (ret == ERROR_FLASH_OPERATION_FAILED) {
LOG_ERROR("flash write failed at address 0x%"PRIx32,
buf_get_u32(reg_params[4].value, 0, 32));
if (buf_get_u32(reg_params[0].value, 0, 32) &
EFM32_MSC_STATUS_LOCKED_MASK) {
LOG_ERROR("flash memory write protected");
}if (buf_get_u32(reg_params[0].value, 0, 32) & EFM32_MSC_STATUS_LOCKED_MASK) { ... }
if (buf_get_u32(reg_params[0].value, 0, 32) &
EFM32_MSC_STATUS_INVADDR_MASK) {
LOG_ERROR("invalid flash memory write address");
}if (buf_get_u32(reg_params[0].value, 0, 32) & EFM32_MSC_STATUS_INVADDR_MASK) { ... }
}if (ret == 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]);
destroy_reg_param(®_params[4]);
return ret;
}{ ... }
static int efm32x_write_word(struct flash_bank *bank, uint32_t addr,
uint32_t val)
{
/* ... */
/* ... */
int ret = 0;
uint32_t status = 0;
keep_alive();
ret = efm32x_write_reg_u32(bank, EFM32_MSC_REG_ADDRB, addr);
if (ret != ERROR_OK)
return ret;
ret = efm32x_set_reg_bits(bank, EFM32_MSC_REG_WRITECMD,
EFM32_MSC_WRITECMD_LADDRIM_MASK, 1);
if (ret != ERROR_OK)
return ret;
ret = efm32x_read_reg_u32(bank, EFM32_MSC_REG_STATUS, &status);
if (ret != ERROR_OK)
return ret;
LOG_DEBUG("status 0x%" PRIx32, status);
if (status & EFM32_MSC_STATUS_LOCKED_MASK) {
LOG_ERROR("Page is locked");
return ERROR_FAIL;
}if (status & EFM32_MSC_STATUS_LOCKED_MASK) { ... } else if (status & EFM32_MSC_STATUS_INVADDR_MASK) {
LOG_ERROR("Invalid address 0x%" PRIx32, addr);
return ERROR_FAIL;
}else if (status & EFM32_MSC_STATUS_INVADDR_MASK) { ... }
ret = efm32x_wait_status(bank, EFM32_FLASH_WDATAREADY_TMO,
EFM32_MSC_STATUS_WDATAREADY_MASK, 1);
if (ret != ERROR_OK) {
LOG_ERROR("Wait for WDATAREADY failed");
return ret;
}if (ret != ERROR_OK) { ... }
ret = efm32x_write_reg_u32(bank, EFM32_MSC_REG_WDATA, val);
if (ret != ERROR_OK) {
LOG_ERROR("WDATA write failed");
return ret;
}if (ret != ERROR_OK) { ... }
ret = efm32x_write_reg_u32(bank, EFM32_MSC_REG_WRITECMD,
EFM32_MSC_WRITECMD_WRITEONCE_MASK);
if (ret != ERROR_OK) {
LOG_ERROR("WRITECMD write failed");
return ret;
}if (ret != ERROR_OK) { ... }
ret = efm32x_wait_status(bank, EFM32_FLASH_WRITE_TMO,
EFM32_MSC_STATUS_BUSY_MASK, 0);
if (ret != ERROR_OK) {
LOG_ERROR("Wait for BUSY failed");
return ret;
}if (ret != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int efm32x_priv_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t addr, uint32_t count)
{
struct target *target = bank->target;
uint8_t *new_buffer = NULL;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if (addr & 0x3) {
LOG_ERROR("addr 0x%" PRIx32 " breaks required 4-byte "
"alignment", addr);
return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
}if (addr & 0x3) { ... }
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);
}if (count & 0x3) { ... }
uint32_t words_remaining = count / 4;
int retval, retval2;
efm32x_msc_lock(bank, 0);
retval = efm32x_set_wren(bank, 1);
if (retval != ERROR_OK)
goto cleanup;
retval = efm32x_write_block(bank, buffer, addr, words_remaining);
if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
/* ... */
LOG_WARNING("couldn't use block writes, falling back to single "
"memory accesses");
while (words_remaining > 0) {
uint32_t value;
memcpy(&value, buffer, sizeof(uint32_t));
retval = efm32x_write_word(bank, addr, value);
if (retval != ERROR_OK)
goto reset_pg_and_lock;
words_remaining--;
buffer += 4;
addr += 4;
}while (words_remaining > 0) { ... }
}if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) { ... }
reset_pg_and_lock:
retval2 = efm32x_set_wren(bank, 0);
efm32x_msc_lock(bank, 1);
if (retval == ERROR_OK)
retval = retval2;
cleanup:
free(new_buffer);
return retval;
}{ ... }
static int efm32x_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
if (bank->base == EFM32_MSC_LOCK_BITS && offset < LOCKWORDS_SZ) {
LOG_ERROR("Cannot write to lock words");
return ERROR_FAIL;
}if (bank->base == EFM32_MSC_LOCK_BITS && offset < LOCKWORDS_SZ) { ... }
return efm32x_priv_write(bank, buffer, bank->base + offset, count);
}{ ... }
static int efm32x_probe(struct flash_bank *bank)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
struct efm32_info *efm32_mcu_info = &(efm32x_info->info);
int ret;
int bank_index = efm32x_get_bank_index(bank->base);
assert(bank_index >= 0);
efm32x_info->probed[bank_index] = false;
memset(efm32x_info->lb_page, 0xff, LOCKWORDS_SZ);
ret = efm32x_read_info(bank);
if (ret != ERROR_OK)
return ret;
LOG_INFO("detected part: %s Gecko, rev %d",
efm32_mcu_info->family_data->name, efm32_mcu_info->prod_rev);
LOG_INFO("flash size = %d KiB", efm32_mcu_info->flash_sz_kib);
LOG_INFO("flash page size = %d B", efm32_mcu_info->page_size);
assert(efm32_mcu_info->page_size != 0);
free(bank->sectors);
bank->sectors = NULL;
if (bank->base == EFM32_FLASH_BASE) {
bank->num_sectors = efm32_mcu_info->flash_sz_kib * 1024 /
efm32_mcu_info->page_size;
assert(bank->num_sectors > 0);
ret = efm32x_read_lock_data(bank);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read LB data");
return ret;
}if (ret != ERROR_OK) { ... }
}if (bank->base == EFM32_FLASH_BASE) { ... } else
bank->num_sectors = 1;
bank->size = bank->num_sectors * efm32_mcu_info->page_size;
bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
for (uint32_t i = 0; i < bank->num_sectors; i++) {
bank->sectors[i].offset = i * efm32_mcu_info->page_size;
bank->sectors[i].size = efm32_mcu_info->page_size;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = 1;
}for (uint32_t i = 0; i < bank->num_sectors; i++) { ... }
efm32x_info->probed[bank_index] = true;
return ERROR_OK;
}{ ... }
static int efm32x_auto_probe(struct flash_bank *bank)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
int bank_index = efm32x_get_bank_index(bank->base);
assert(bank_index >= 0);
if (efm32x_info->probed[bank_index])
return ERROR_OK;
return efm32x_probe(bank);
}{ ... }
static int efm32x_protect_check(struct flash_bank *bank)
{
struct target *target = bank->target;
int ret = 0;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
ret = efm32x_read_lock_data(bank);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read LB data");
return ret;
}if (ret != ERROR_OK) { ... }
assert(bank->sectors);
for (unsigned int i = 0; i < bank->num_sectors; i++)
bank->sectors[i].is_protected = efm32x_get_page_lock(bank, i);
return ERROR_OK;
}{ ... }
static int get_efm32x_info(struct flash_bank *bank, struct command_invocation *cmd)
{
struct efm32x_flash_chip *efm32x_info = bank->driver_priv;
int ret;
ret = efm32x_read_info(bank);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read EFM32 info");
return ret;
}if (ret != ERROR_OK) { ... }
command_print_sameline(cmd, "%s Gecko, rev %d", efm32x_info->info.family_data->name,
efm32x_info->info.prod_rev);
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(efm32x_handle_debuglock_command)
{
struct target *target = NULL;
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 efm32x_flash_chip *efm32x_info = bank->driver_priv;
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 *ptr;
ptr = efm32x_info->lb_page + 127;
*ptr = 0;
retval = efm32x_write_lock_data(bank);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to write LB page");
return retval;
}if (retval != ERROR_OK) { ... }
command_print(CMD, "efm32x debug interface locked, reset the device to apply");
return ERROR_OK;
}{ ... }
static const struct command_registration efm32x_exec_command_handlers[] = {
{
.name = "debuglock",
.handler = efm32x_handle_debuglock_command,
.mode = COMMAND_EXEC,
.usage = "bank_id",
.help = "Lock the debug interface of the device.",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration efm32x_command_handlers[] = {
{
.name = "efm32",
.mode = COMMAND_ANY,
.help = "efm32 flash command group",
.usage = "",
.chain = efm32x_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
const struct flash_driver efm32_flash = {
.name = "efm32",
.commands = efm32x_command_handlers,
.flash_bank_command = efm32x_flash_bank_command,
.erase = efm32x_erase,
.protect = efm32x_protect,
.write = efm32x_write,
.read = default_flash_read,
.probe = efm32x_probe,
.auto_probe = efm32x_auto_probe,
.erase_check = default_flash_blank_check,
.protect_check = efm32x_protect_check,
.info = get_efm32x_info,
.free_driver_priv = efm32x_free_driver_priv,
...};