1
2
3
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
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
232
233
234
237
238
241
242
245
246
249
250
251
252
253
254
255
256
262
263
264
265
266
267
268
269
270
272
273
274
292
293
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
330
334
335
336
337
341
342
343
347
348
349
350
351
352
353
357
358
359
362
384
385
387
388
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
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
458
459
460
461
462
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
489
490
491
499
500
501
502
503
504
505
506
507
511
512
513
517
518
519
523
524
525
526
527
528
529
530
531
532
533
534
535
536
540
541
546
547
548
549
550
551
555
556
557
558
559
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
587
588
589
590
591
592
593
594
598
599
600
604
605
606
607
608
609
610
611
612
613
614
615
621
622
625
626
627
628
629
630
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
676
677
678
679
680
681
685
686
687
688
689
690
691
692
696
697
698
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
722
723
728
729
730
734
735
736
737
740
741
742
743
744
745
746
747
748
749
750
751
752
753
761
762
763
764
765
766
767
768
773
774
776
777
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
804
805
806
807
811
812
813
814
815
816
817
818
830
842
850
862
869
884
893
903
906
907
908
909
910
911
912
913
914
915
916
917
918
920
921
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
958
959
960
961
962
963
964
965
966
967
968
972
973
974
975
976
977
983
984
985
986
987
988
989
990
991
992
993
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1029
1033
1034
1037
1038
1039
1040
1041
1042
1043
1057
1058
1063
1064
1069
1070
1071
1072
1074
1075
1076
1077
1079
1080
1081
1082
1083
1084
1085
1087
1088
1089
1090
1093
1094
1095
1096
1097
1098
1101
1102
1103
1104
1106
1107
1109
1120
1121
1122
1123
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1142
1143
1144
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1168
1169
1170
1174
1175
1176
1180
1181
1182
1183
1185
1186
1187
1188
1189
1193
1194
1195
1198
1199
1200
1202
1203
1204
1205
1206
1207
1208
1209
1210
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include <helper/binarybuffer.h>
#include <target/algorithm.h>
#include <target/armv7m.h>
#define MAX_XMC_SECTORS 12
#define SCU_REG_BASE 0x50004000
#define SCU_ID_CHIP 0x04
#define PFLASH_BASE 0x0C000000
#define UCB0_BASE 0x00000000
#define UCB1_BASE 0x00000400
#define UCB2_BASE 0x00000800
#define FLASH_REG_BASE 0x58000000
#define FLASH_REG_PMU_ID (FLASH_REG_BASE | 0x0508)
#define PMU_MOD_REV_MASK 0xFF
#define PMU_MOD_TYPE_MASK 0xFF00
#define PMU_MOD_NO_MASK 0xFFFF0000
#define FLASH_REG_PREF_PCON (FLASH_REG_BASE | 0x4000)
#define PCON_IBYP (1 << 0)
#define PCON_IINV (1 << 1)
#define FLASH_REG_FLASH0_ID (FLASH_REG_BASE | 0x2008)
#define FLASH_REG_FLASH0_FSR (FLASH_REG_BASE | 0x2010)
#define FSR_PBUSY (0)
#define FSR_FABUSY (1)
#define FSR_PROG (4)
#define FSR_ERASE (5)
#define FSR_PFPAGE (6)
#define FSR_PFOPER (8)
#define FSR_SQER (10)
#define FSR_PROER (11)
#define FSR_PFSBER (12)
#define FSR_PFDBER (14)
#define FSR_PROIN (16)
#define FSR_RPROIN (18)
#define FSR_RPRODIS (19)
#define FSR_WPROIN0 (21)
#define FSR_WPROIN1 (22)
#define FSR_WPROIN2 (23)
#define FSR_WPRODIS0 (25)
#define FSR_WPRODIS1 (26)
#define FSR_SLM (28)
#define FSR_VER (31)
#define FSR_PBUSY_MASK (0x01 << FSR_PBUSY)
#define FSR_FABUSY_MASK (0x01 << FSR_FABUSY)
#define FSR_PROG_MASK (0x01 << FSR_PROG)
#define FSR_ERASE_MASK (0x01 << FSR_ERASE)
#define FSR_PFPAGE_MASK (0x01 << FSR_PFPAGE)
#define FSR_PFOPER_MASK (0x01 << FSR_PFOPER)
#define FSR_SQER_MASK (0x01 << FSR_SQER)
#define FSR_PROER_MASK (0x01 << FSR_PROER)
#define FSR_PFSBER_MASK (0x01 << FSR_PFSBER)
#define FSR_PFDBER_MASK (0x01 << FSR_PFDBER)
#define FSR_PROIN_MASK (0x01 << FSR_PROIN)
#define FSR_RPROIN_MASK (0x01 << FSR_RPROIN)
#define FSR_RPRODIS_MASK (0x01 << FSR_RPRODIS)
#define FSR_WPROIN0_MASK (0x01 << FSR_WPROIN0)
#define FSR_WPROIN1_MASK (0x01 << FSR_WPROIN1)
#define FSR_WPROIN2_MASK (0x01 << FSR_WPROIN2)
#define FSR_WPRODIS0_MASK (0x01 << FSR_WPRODIS0)
#define FSR_WPRODIS1_MASK (0x01 << FSR_WPRODIS1)
#define FSR_SLM_MASK (0x01 << FSR_SLM)
#define FSR_VER_MASK (0x01 << FSR_VER)
#define FLASH_REG_FLASH0_FCON (FLASH_REG_BASE | 0x2014)
#define FCON_WSPFLASH (0)
#define FCON_WSECPF (4)
#define FCON_IDLE (13)
#define FCON_ESLDIS (14)
#define FCON_SLEEP (15)
#define FCON_RPA (16)
#define FCON_DCF (17)
#define FCON_DDF (18)
#define FCON_VOPERM (24)
#define FCON_SQERM (25)
#define FCON_PROERM (26)
#define FCON_PFSBERM (27)
#define FCON_PFDBERM (29)
#define FCON_EOBM (31)
#define FCON_WSPFLASH_MASK (0x0f << FCON_WSPFLASH)
#define FCON_WSECPF_MASK (0x01 << FCON_WSECPF)
#define FCON_IDLE_MASK (0x01 << FCON_IDLE)
#define FCON_ESLDIS_MASK (0x01 << FCON_ESLDIS)
#define FCON_SLEEP_MASK (0x01 << FCON_SLEEP)
#define FCON_RPA_MASK (0x01 << FCON_RPA)
#define FCON_DCF_MASK (0x01 << FCON_DCF)
#define FCON_DDF_MASK (0x01 << FCON_DDF)
#define FCON_VOPERM_MASK (0x01 << FCON_VOPERM)
#define FCON_SQERM_MASK (0x01 << FCON_SQERM)
#define FCON_PROERM_MASK (0x01 << FCON_PROERM)
#define FCON_PFSBERM_MASK (0x01 << FCON_PFSBERM)
#define FCON_PFDBERM_MASK (0x01 << FCON_PFDBERM)
#define FCON_EOBM_MASK (0x01 << FCON_EOBM)
#define FLASH_REG_FLASH0_MARP (FLASH_REG_BASE | 0x2018)
#define MARP_MARGIN (0)
#define MARP_TRAPDIS (15)
#define MARP_MARGIN_MASK (0x0f << MARP_MARGIN)
#define MARP_TRAPDIS_MASK (0x01 << MARP_TRAPDIS)
#define FLASH_REG_FLASH0_PROCON0 (FLASH_REG_BASE | 0x2020)
#define FLASH_REG_FLASH0_PROCON1 (FLASH_REG_BASE | 0x2024)
#define FLASH_REG_FLASH0_PROCON2 (FLASH_REG_BASE | 0x2028)
#define PROCON_S0L (0)
#define PROCON_S1L (1)
#define PROCON_S2L (2)
#define PROCON_S3L (3)
#define PROCON_S4L (4)
#define PROCON_S5L (5)
#define PROCON_S6L (6)
#define PROCON_S7L (7)
#define PROCON_S8L (8)
#define PROCON_S9L (9)
#define PROCON_S10_S11L (10)
#define PROCON_RPRO (15)
#define PROCON_S0L_MASK (0x01 << PROCON_S0L)
#define PROCON_S1L_MASK (0x01 << PROCON_S1L)
#define PROCON_S2L_MASK (0x01 << PROCON_S2L)
#define PROCON_S3L_MASK (0x01 << PROCON_S3L)
#define PROCON_S4L_MASK (0x01 << PROCON_S4L)
#define PROCON_S5L_MASK (0x01 << PROCON_S5L)
#define PROCON_S6L_MASK (0x01 << PROCON_S6L)
#define PROCON_S7L_MASK (0x01 << PROCON_S7L)
#define PROCON_S8L_MASK (0x01 << PROCON_S8L)
#define PROCON_S9L_MASK (0x01 << PROCON_S9L)
#define PROCON_S10_S11L_MASK (0x01 << PROCON_S10_S11L)
#define PROCON_RPRO_MASK (0x01 << PROCON_RPRO)
#define FLASH_PROTECT_CONFIRMATION_CODE 0x8AFE15C3
#define FLASH_ID_XMC4500 0xA2
#define FLASH_ID_XMC4300_XMC4700_4800 0x92
#define FLASH_ID_XMC4100_4200 0x9C
#define FLASH_ID_XMC4400 0x9F
#define FLASH_OP_TIMEOUT 5000
/* ... */
#define FLASH_CMD_ERASE_1 0x0C005554
#define FLASH_CMD_ERASE_2 0x0C00AAA8
#define FLASH_CMD_ERASE_3 FLASH_CMD_ERASE_1
#define FLASH_CMD_ERASE_4 FLASH_CMD_ERASE_1
#define FLASH_CMD_ERASE_5 FLASH_CMD_ERASE_2
#define FLASH_CMD_CLEAR_STATUS FLASH_CMD_ERASE_1
#define FLASH_CMD_ENTER_PAGEMODE FLASH_CMD_ERASE_1
#define FLASH_CMD_LOAD_PAGE_1 0x0C0055F0
#define FLASH_CMD_LOAD_PAGE_2 0x0C0055F4
#define FLASH_CMD_WRITE_PAGE_1 FLASH_CMD_ERASE_1
#define FLASH_CMD_WRITE_PAGE_2 FLASH_CMD_ERASE_2
#define FLASH_CMD_WRITE_PAGE_3 FLASH_CMD_ERASE_1
#define FLASH_CMD_TEMP_UNPROT_1 FLASH_CMD_ERASE_1
#define FLASH_CMD_TEMP_UNPROT_2 FLASH_CMD_ERASE_2
#define FLASH_CMD_TEMP_UNPROT_3 0x0C00553C
#define FLASH_CMD_TEMP_UNPROT_4 FLASH_CMD_ERASE_2
#define FLASH_CMD_TEMP_UNPROT_5 FLASH_CMD_ERASE_2
#define FLASH_CMD_TEMP_UNPROT_6 0x0C005558
142 defines
struct xmc4xxx_flash_bank {
bool probed;
uint32_t fcon_id;
uint32_t pw1;
uint32_t pw2;
bool pw_set;
bool read_protected;
bool write_prot_otp[MAX_XMC_SECTORS];
...};
struct xmc4xxx_command_seq {
uint32_t address;
uint32_t magic;
...};
static const unsigned int sector_capacity_8[8] = {
16, 16, 16, 16, 16, 16, 16, 128
...};
static const unsigned int sector_capacity_9[9] = {
16, 16, 16, 16, 16, 16, 16, 128, 256
...};
static const unsigned int sector_capacity_12[12] = {
16, 16, 16, 16, 16, 16, 16, 16, 128, 256, 256, 256
...};
static const unsigned int sector_capacity_16[16] = {
16, 16, 16, 16, 16, 16, 16, 16, 128, 256, 256, 256, 256, 256, 256, 256
...};
static int xmc4xxx_write_command_sequence(struct flash_bank *bank,
struct xmc4xxx_command_seq *seq,
int seq_len)
{
int res = ERROR_OK;
for (int i = 0; i < seq_len; i++) {
res = target_write_u32(bank->target, seq[i].address,
seq[i].magic);
if (res != ERROR_OK)
return res;
}for (int i = 0; i < seq_len; i++) { ... }
return ERROR_OK;
}{ ... }
static int xmc4xxx_load_bank_layout(struct flash_bank *bank)
{
const unsigned int *capacity = NULL;
/* ... */
LOG_DEBUG("%u sectors", bank->num_sectors);
switch (bank->num_sectors) {
case 8:
capacity = sector_capacity_8;
break;case 8:
case 9:
capacity = sector_capacity_9;
break;case 9:
case 12:
capacity = sector_capacity_12;
break;case 12:
case 16:
capacity = sector_capacity_16;
break;case 16:
default:
LOG_ERROR("Unexpected number of sectors, %u\n",
bank->num_sectors);
return ERROR_FAIL;default
}switch (bank->num_sectors) { ... }
/* ... */
uint32_t total_offset = 0;
bank->sectors = calloc(bank->num_sectors,
sizeof(struct flash_sector));
for (unsigned int i = 0; i < bank->num_sectors; i++) {
bank->sectors[i].size = capacity[i] * 1024;
bank->sectors[i].offset = total_offset;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = -1;
bank->size += bank->sectors[i].size;
LOG_DEBUG("\t%d: %uk", i, capacity[i]);
total_offset += bank->sectors[i].size;
}for (unsigned int i = 0; i < bank->num_sectors; i++) { ... }
/* ... */
bank->default_padded_value = bank->erased_value = 0x00;
return ERROR_OK;
}{ ... }
static int xmc4xxx_probe(struct flash_bank *bank)
{
int res;
uint32_t devid, config;
struct xmc4xxx_flash_bank *fb = bank->driver_priv;
uint8_t flash_id;
if (fb->probed)
return ERROR_OK;
/* ... */
if (bank->target->state != TARGET_HALTED) {
LOG_WARNING("Cannot communicate... target not halted.");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
res = target_read_u32(bank->target, SCU_REG_BASE + SCU_ID_CHIP, &devid);
if (res != ERROR_OK) {
LOG_ERROR("Cannot read device identification register.");
return res;
}if (res != ERROR_OK) { ... }
if ((devid & 0xF0000) != 0x40000 && devid != 0) {
LOG_ERROR("Platform ID doesn't match XMC4xxx: 0x%08" PRIx32, devid);
return ERROR_FAIL;
}if ((devid & 0xF0000) != 0x40000 && devid != 0) { ... }
LOG_DEBUG("Found XMC4xxx with devid: 0x%08" PRIx32, devid);
res = target_read_u32(bank->target, FLASH_REG_FLASH0_ID,
&config);
if (res != ERROR_OK) {
LOG_ERROR("Cannot read Flash bank configuration.");
return res;
}if (res != ERROR_OK) { ... }
flash_id = (config & 0xff0000) >> 16;
/* ... */
switch (flash_id) {
case FLASH_ID_XMC4100_4200:
bank->num_sectors = 8;
LOG_DEBUG("XMC4xxx: XMC4100/4200 detected.");
break;case FLASH_ID_XMC4100_4200:
case FLASH_ID_XMC4400:
bank->num_sectors = 9;
LOG_DEBUG("XMC4xxx: XMC4400 detected.");
break;case FLASH_ID_XMC4400:
case FLASH_ID_XMC4500:
bank->num_sectors = 12;
LOG_DEBUG("XMC4xxx: XMC4500 detected.");
break;case FLASH_ID_XMC4500:
case FLASH_ID_XMC4300_XMC4700_4800:
bank->num_sectors = 16;
LOG_DEBUG("XMC4xxx: XMC4700/4800 detected.");
break;case FLASH_ID_XMC4300_XMC4700_4800:
default:
LOG_ERROR("XMC4xxx: Unexpected flash ID. got %02" PRIx8,
flash_id);
return ERROR_FAIL;default
}switch (flash_id) { ... }
/* ... */
res = xmc4xxx_load_bank_layout(bank);
if (res == ERROR_OK) {
fb->probed = true;
}if (res == ERROR_OK) { ... } else {
LOG_ERROR("Unable to load bank information.");
return ERROR_FAIL;
}else { ... }
return ERROR_OK;
}{ ... }
static int xmc4xxx_get_sector_start_addr(struct flash_bank *bank,
unsigned int sector, uint32_t *ret_addr)
{
if (sector > bank->num_sectors)
return ERROR_FAIL;
*ret_addr = bank->base + bank->sectors[sector].offset;
return ERROR_OK;
}{ ... }
static int xmc4xxx_clear_flash_status(struct flash_bank *bank)
{
int res;
LOG_INFO("Clearing flash status");
res = target_write_u32(bank->target, FLASH_CMD_CLEAR_STATUS,
0xF5);
if (res != ERROR_OK) {
LOG_ERROR("Unable to write erase command sequence");
return res;
}if (res != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int xmc4xxx_get_flash_status(struct flash_bank *bank, uint32_t *status)
{
int res;
res = target_read_u32(bank->target, FLASH_REG_FLASH0_FSR, status);
if (res != ERROR_OK)
LOG_ERROR("Cannot read flash status register.");
return res;
}{ ... }
static int xmc4xxx_wait_status_busy(struct flash_bank *bank, int timeout)
{
int res;
uint32_t status;
res = xmc4xxx_get_flash_status(bank, &status);
if (res != ERROR_OK)
return res;
while (status & FSR_PBUSY_MASK) {
res = xmc4xxx_get_flash_status(bank, &status);
if (res != ERROR_OK)
return res;
if (timeout-- <= 0) {
LOG_ERROR("Timed out waiting for flash");
return ERROR_FAIL;
}if (timeout-- <= 0) { ... }
alive_sleep(1);
keep_alive();
}while (status & FSR_PBUSY_MASK) { ... }
if (status & FSR_PROER_MASK) {
LOG_ERROR("XMC4xxx flash protected");
res = ERROR_FAIL;
}if (status & FSR_PROER_MASK) { ... }
return res;
}{ ... }
static int xmc4xxx_erase_sector(struct flash_bank *bank, uint32_t address,
bool user_config)
{
int res;
uint32_t status;
struct xmc4xxx_command_seq erase_cmd_seq[6] = {
{FLASH_CMD_ERASE_1, 0xAA},
{FLASH_CMD_ERASE_2, 0x55},
{FLASH_CMD_ERASE_3, 0x80},
{FLASH_CMD_ERASE_4, 0xAA},
{FLASH_CMD_ERASE_5, 0x55},
{0xFF, 0xFF}
...};
/* ... */
erase_cmd_seq[5].address = address;
if (user_config) {
/* ... */
erase_cmd_seq[5].address += bank->base;
erase_cmd_seq[5].magic = 0xC0;
}if (user_config) { ... } else {
erase_cmd_seq[5].magic = 0x30;
}else { ... }
res = xmc4xxx_write_command_sequence(bank, erase_cmd_seq,
ARRAY_SIZE(erase_cmd_seq));
if (res != ERROR_OK)
return res;
res = target_read_u32(bank->target, FLASH_REG_FLASH0_FSR, &status);
if (res != ERROR_OK) {
LOG_ERROR("Cannot read flash status register.");
return res;
}if (res != ERROR_OK) { ... }
if (status & FSR_SQER_MASK) {
LOG_ERROR("Error with flash erase sequence");
return ERROR_FAIL;
}if (status & FSR_SQER_MASK) { ... }
if (!(status & FSR_ERASE_MASK)) {
LOG_ERROR("Flash failed to erase");
return ERROR_FAIL;
}if (!(status & FSR_ERASE_MASK)) { ... }
res = xmc4xxx_wait_status_busy(bank, FLASH_OP_TIMEOUT);
return res;
}{ ... }
static int xmc4xxx_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
struct xmc4xxx_flash_bank *fb = bank->driver_priv;
int res;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Unable to erase, target is not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (!fb->probed) {
res = xmc4xxx_probe(bank);
if (res != ERROR_OK)
return res;
}if (!fb->probed) { ... }
uint32_t tmp_addr;
for (unsigned int i = first; i <= last; i++) {
res = xmc4xxx_get_sector_start_addr(bank, i, &tmp_addr);
if (res != ERROR_OK) {
LOG_ERROR("Invalid sector %u", i);
return res;
}if (res != ERROR_OK) { ... }
LOG_DEBUG("Erasing sector %u @ 0x%08"PRIx32, i, tmp_addr);
res = xmc4xxx_erase_sector(bank, tmp_addr, false);
if (res != ERROR_OK) {
LOG_ERROR("Unable to write erase command sequence");
goto clear_status_and_exit;
}if (res != ERROR_OK) { ... }
res = xmc4xxx_wait_status_busy(bank, FLASH_OP_TIMEOUT);
if (res != ERROR_OK)
goto clear_status_and_exit;
}for (unsigned int i = first; i <= last; i++) { ... }
clear_status_and_exit:
res = xmc4xxx_clear_flash_status(bank);
return res;
}{ ... }
static int xmc4xxx_enter_page_mode(struct flash_bank *bank)
{
int res;
uint32_t status;
res = target_write_u32(bank->target, FLASH_CMD_ENTER_PAGEMODE, 0x50);
if (res != ERROR_OK) {
LOG_ERROR("Unable to write enter page mode command");
return ERROR_FAIL;
}if (res != ERROR_OK) { ... }
res = xmc4xxx_get_flash_status(bank, &status);
if (res != ERROR_OK)
return res;
if (!(status & FSR_PFPAGE_MASK)) {
LOG_ERROR("Unable to enter page mode");
return ERROR_FAIL;
}if (!(status & FSR_PFPAGE_MASK)) { ... }
if (status & FSR_SQER_MASK) {
LOG_ERROR("Sequence error while entering page mode");
return ERROR_FAIL;
}if (status & FSR_SQER_MASK) { ... }
return res;
}{ ... }
static int xmc4xxx_write_page(struct flash_bank *bank, const uint8_t *pg_buf,
uint32_t offset, bool user_config)
{
int res;
uint32_t status;
struct xmc4xxx_command_seq write_cmd_seq[4] = {
{FLASH_CMD_WRITE_PAGE_1, 0xAA},
{FLASH_CMD_WRITE_PAGE_2, 0x55},
{FLASH_CMD_WRITE_PAGE_3, 0xFF},
{0xFF, 0xFF}
...};
/* ... */
if (user_config)
write_cmd_seq[2].magic = 0xC0;
else
write_cmd_seq[2].magic = 0xA0;
/* ... */
write_cmd_seq[3].address = bank->base + offset;
write_cmd_seq[3].magic = 0xAA;
/* ... */
res = xmc4xxx_enter_page_mode(bank);
if (res != ERROR_OK)
return res;
for (int i = 0; i < 256; i += 8) {
uint32_t w_lo = target_buffer_get_u32(bank->target, &pg_buf[i]);
uint32_t w_hi = target_buffer_get_u32(bank->target, &pg_buf[i + 4]);
LOG_DEBUG("WLO: %08"PRIx32, w_lo);
LOG_DEBUG("WHI: %08"PRIx32, w_hi);
res = target_write_u32(bank->target, FLASH_CMD_LOAD_PAGE_1, w_lo);
if (res != ERROR_OK)
return res;
res = target_write_u32(bank->target, FLASH_CMD_LOAD_PAGE_2, w_hi);
if (res != ERROR_OK)
return res;
res = xmc4xxx_get_flash_status(bank, &status);
if (res != ERROR_OK)
return res;
if (status & FSR_SQER_MASK) {
LOG_ERROR("Error loading page buffer");
return ERROR_FAIL;
}if (status & FSR_SQER_MASK) { ... }
}for (int i = 0; i < 256; i += 8) { ... }
res = xmc4xxx_write_command_sequence(bank, write_cmd_seq, ARRAY_SIZE(write_cmd_seq));
if (res != ERROR_OK) {
LOG_ERROR("Unable to enter write command sequence");
return res;
}if (res != ERROR_OK) { ... }
res = xmc4xxx_get_flash_status(bank, &status);
if (res != ERROR_OK)
return res;
if (status & FSR_SQER_MASK) {
LOG_ERROR("Error with flash write sequence");
return ERROR_FAIL;
}if (status & FSR_SQER_MASK) { ... }
if (!(status & FSR_PROG_MASK)) {
LOG_ERROR("Failed to write flash page");
return ERROR_FAIL;
}if (!(status & FSR_PROG_MASK)) { ... }
res = xmc4xxx_wait_status_busy(bank, FLASH_OP_TIMEOUT);
if (res != ERROR_OK)
return res;
return res;
}{ ... }
static int xmc4xxx_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
struct xmc4xxx_flash_bank *fb = bank->driver_priv;
int res = ERROR_OK;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Unable to erase, target is not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (!fb->probed) {
res = xmc4xxx_probe(bank);
if (res != ERROR_OK)
return res;
}if (!fb->probed) { ... }
if ((offset + count) > (bank->size)) {
LOG_ERROR("Attempting to write past the end of flash");
return ERROR_FAIL;
}if ((offset + count) > (bank->size)) { ... }
/* ... */
while (count) {
uint8_t tmp_buf[256] = {0};
int remaining;
int end_pad;
remaining = MIN(count, sizeof(tmp_buf));
end_pad = sizeof(tmp_buf) - remaining;
int start_pad = offset % 256;
if (start_pad) {
LOG_INFO("Write does not start on a 256 byte boundary. "
"Padding by %d bytes", start_pad);
memset(tmp_buf, 0xff, start_pad);
/* ... */
remaining -= start_pad;
}if (start_pad) { ... }
count -= remaining;
memcpy(&tmp_buf[start_pad], buffer, remaining);
if (end_pad) {
LOG_INFO("Padding end of page @" TARGET_ADDR_FMT " by %d bytes",
bank->base + offset, end_pad);
memset(&tmp_buf[256 - end_pad], 0xff, end_pad);
}if (end_pad) { ... }
/* ... */
res = xmc4xxx_write_page(bank, tmp_buf, (offset - start_pad), false);
if (res != ERROR_OK) {
LOG_ERROR("Unable to write flash page");
goto abort_write_and_exit;
}if (res != ERROR_OK) { ... }
buffer += remaining;
offset += remaining;
}while (count) { ... }
abort_write_and_exit:
xmc4xxx_clear_flash_status(bank);
return res;
}{ ... }
static int xmc4xxx_get_info_command(struct flash_bank *bank, struct command_invocation *cmd)
{
struct xmc4xxx_flash_bank *fb = bank->driver_priv;
uint32_t scu_idcode;
if (bank->target->state != TARGET_HALTED) {
LOG_WARNING("Cannot communicate... target not halted.");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
int res = target_read_u32(bank->target, SCU_REG_BASE + SCU_ID_CHIP, &scu_idcode);
if (res != ERROR_OK) {
LOG_ERROR("Cannot read device identification register.");
return res;
}if (res != ERROR_OK) { ... }
uint16_t dev_id = (scu_idcode & 0xfff0) >> 4;
uint16_t rev_id = scu_idcode & 0xf;
const char *dev_str;
const char *rev_str = NULL;
switch (dev_id) {
case 0x100:
dev_str = "XMC4100";
switch (rev_id) {
case 0x1:
rev_str = "AA";
break;case 0x1:
case 0x2:
rev_str = "AB";
break;case 0x2:
}switch (rev_id) { ... }
break;case 0x100:
case 0x200:
dev_str = "XMC4200";
switch (rev_id) {
case 0x1:
rev_str = "AA";
break;case 0x1:
case 0x2:
rev_str = "AB";
break;case 0x2:
}switch (rev_id) { ... }
break;case 0x200:
case 0x300:
dev_str = "XMC4300";
switch (rev_id) {
case 0x1:
rev_str = "AA";case 0x1:
}switch (rev_id) { ... }
break;case 0x300:
case 0x400:
dev_str = "XMC4400";
switch (rev_id) {
case 0x1:
rev_str = "AA";
break;case 0x1:
case 0x2:
rev_str = "AB";
break;case 0x2:
}switch (rev_id) { ... }
break;case 0x400:
case 0:
/* ... */
dev_str = "XMC4500 EES";
rev_str = "AA13";
break;case 0:
case 0x500:
dev_str = "XMC4500";
switch (rev_id) {
case 0x2:
rev_str = "AA";
break;case 0x2:
case 0x3:
rev_str = "AB";
break;case 0x3:
case 0x4:
rev_str = "AC";
break;case 0x4:
}switch (rev_id) { ... }
break;case 0x500:
case 0x700:
dev_str = "XMC4700";
switch (rev_id) {
case 0x1:
rev_str = "EES-AA";
break;case 0x1:
}switch (rev_id) { ... }
break;case 0x700:
case 0x800:
dev_str = "XMC4800";
switch (rev_id) {
case 0x1:
rev_str = "EES-AA";
break;case 0x1:
}switch (rev_id) { ... }
break;
case 0x800:
default:
command_print_sameline(cmd, "Cannot identify target as an XMC4xxx. SCU_ID: %"PRIx32 "\n", scu_idcode);
return ERROR_OK;default
}switch (dev_id) { ... }
char prot_str[512] = {0};
if (fb->read_protected)
snprintf(prot_str, sizeof(prot_str), "\nFlash is read protected");
bool otp_enabled = false;
for (unsigned int i = 0; i < bank->num_sectors; i++)
if (fb->write_prot_otp[i])
otp_enabled = true;
/* ... */
char otp_str[14];
if (otp_enabled) {
strcat(prot_str, "\nOTP Protection is enabled for sectors:\n");
for (unsigned int i = 0; i < bank->num_sectors; i++) {
if (fb->write_prot_otp[i]) {
snprintf(otp_str, sizeof(otp_str), "- %d\n", i);
strncat(prot_str, otp_str, sizeof(prot_str) - strlen(prot_str) - 1);
}if (fb->write_prot_otp[i]) { ... }
}for (unsigned int i = 0; i < bank->num_sectors; i++) { ... }
}if (otp_enabled) { ... }
if (rev_str)
command_print_sameline(cmd, "%s - Rev: %s%s", dev_str, rev_str, prot_str);
else
command_print_sameline(cmd, "%s - Rev: unknown (0x%01x)%s", dev_str, rev_id, prot_str);
return ERROR_OK;
}{ ... }
static int xmc4xxx_temp_unprotect(struct flash_bank *bank, int user_level)
{
struct xmc4xxx_flash_bank *fb;
int res = ERROR_OK;
uint32_t status = 0;
struct xmc4xxx_command_seq temp_unprot_seq[6] = {
{FLASH_CMD_TEMP_UNPROT_1, 0xAA},
{FLASH_CMD_TEMP_UNPROT_2, 0x55},
{FLASH_CMD_TEMP_UNPROT_3, 0xFF},
{FLASH_CMD_TEMP_UNPROT_4, 0xFF},
{FLASH_CMD_TEMP_UNPROT_5, 0xFF},
{FLASH_CMD_TEMP_UNPROT_6, 0x05}
...};
if (user_level < 0 || user_level > 2) {
LOG_ERROR("Invalid user level, must be 0-2");
return ERROR_FAIL;
}if (user_level < 0 || user_level > 2) { ... }
fb = bank->driver_priv;
temp_unprot_seq[2].magic = user_level;
temp_unprot_seq[3].magic = fb->pw1;
temp_unprot_seq[4].magic = fb->pw2;
res = xmc4xxx_write_command_sequence(bank, temp_unprot_seq,
ARRAY_SIZE(temp_unprot_seq));
if (res != ERROR_OK) {
LOG_ERROR("Unable to write temp unprotect sequence");
return res;
}if (res != ERROR_OK) { ... }
res = xmc4xxx_get_flash_status(bank, &status);
if (res != ERROR_OK)
return res;
if (status & FSR_WPRODIS0) {
LOG_INFO("Flash is temporarily unprotected");
}if (status & FSR_WPRODIS0) { ... } else {
LOG_INFO("Unable to disable flash protection");
res = ERROR_FAIL;
}else { ... }
return res;
}{ ... }
static int xmc4xxx_flash_unprotect(struct flash_bank *bank, int32_t level)
{
uint32_t addr;
int res;
switch (level) {
case 0:
addr = UCB0_BASE;
break;case 0:
case 1:
addr = UCB1_BASE;
break;case 1:
default:
LOG_ERROR("Invalid user level. Must be 0-1");
return ERROR_FAIL;default
}switch (level) { ... }
res = xmc4xxx_erase_sector(bank, addr, true);
if (res != ERROR_OK)
LOG_ERROR("Error erasing user configuration block");
return res;
}{ ... }
static int xmc4xxx_flash_protect(struct flash_bank *bank, int level, bool read_protect,
unsigned int first, unsigned int last)
{
uint8_t ucp0_buf[8 * sizeof(uint32_t)] = {0};
uint32_t ucb_base = 0;
uint32_t procon = 0;
int res = ERROR_OK;
uint32_t status = 0;
bool proin = false;
struct xmc4xxx_flash_bank *fb = bank->driver_priv;
/* ... */
if (level != 0 && read_protect) {
LOG_ERROR("Read protection is for user level 0 only!");
return ERROR_FAIL;
}if (level != 0 && read_protect) { ... }
/* ... */
res = xmc4xxx_get_flash_status(bank, &status);
if (res != ERROR_OK)
return res;
switch (level) {
case 0:
if ((status & FSR_RPROIN_MASK) || (status & FSR_WPROIN0_MASK))
proin = true;
break;case 0:
case 1:
if (status & FSR_WPROIN1_MASK)
proin = true;
break;case 1:
case 2:
if (status & FSR_WPROIN2_MASK)
proin = true;
break;case 2:
}switch (level) { ... }
if (proin) {
LOG_ERROR("Flash protection is installed for user %d"
" and must be removed before continuing", level);
return ERROR_FAIL;
}if (proin) { ... }
/* ... */
if ((bank->num_sectors == 12) && (last == 12))
last--;
/* ... */
for (unsigned int i = first; i <= last; i++)
procon |= 1 << i;
/* ... */
if (read_protect)
procon |= PROCON_RPRO_MASK;
LOG_DEBUG("Setting flash protection with procon:");
LOG_DEBUG("PROCON: %"PRIx32, procon);
/* ... */
target_buffer_set_u32(bank->target, &ucp0_buf[0 * 4], procon);
target_buffer_set_u32(bank->target, &ucp0_buf[2 * 4], procon);
/* ... */
target_buffer_set_u32(bank->target, &ucp0_buf[4 * 4], fb->pw1);
target_buffer_set_u32(bank->target, &ucp0_buf[5 * 4], fb->pw2);
target_buffer_set_u32(bank->target, &ucp0_buf[6 * 4], fb->pw1);
target_buffer_set_u32(bank->target, &ucp0_buf[7 * 4], fb->pw2);
/* ... */
target_buffer_set_u32(bank->target, &ucp0_buf[0 * 4], FLASH_PROTECT_CONFIRMATION_CODE);
target_buffer_set_u32(bank->target, &ucp0_buf[2 * 4], FLASH_PROTECT_CONFIRMATION_CODE);
/* ... */
/* ... */
switch (level) {
case 0:
ucb_base = UCB0_BASE;
break;case 0:
case 1:
ucb_base = UCB1_BASE;
break;case 1:
case 2:
ucb_base = UCB2_BASE;
break;case 2:
}switch (level) { ... }
res = xmc4xxx_write_page(bank, ucp0_buf, ucb_base, true);
if (res != ERROR_OK) {
LOG_ERROR("Error writing user configuration block 0");
return res;
}if (res != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int xmc4xxx_protect(struct flash_bank *bank, int set, unsigned int first,
unsigned int last)
{
int ret;
struct xmc4xxx_flash_bank *fb = bank->driver_priv;
if (!fb->pw_set) {
LOG_ERROR("Flash passwords not set, use xmc4xxx flash_password to set them");
return ERROR_FAIL;
}if (!fb->pw_set) { ... }
if (set == 0) {
LOG_WARNING("Flash protection will be temporarily disabled"
" for all pages (User 0 only)!");
ret = xmc4xxx_temp_unprotect(bank, 0);
return ret;
}if (set == 0) { ... }
ret = xmc4xxx_flash_protect(bank, 0, false, first, last);
return ret;
}{ ... }
static int xmc4xxx_protect_check(struct flash_bank *bank)
{
int ret;
uint32_t protection[3] = {0};
struct xmc4xxx_flash_bank *fb = bank->driver_priv;
ret = target_read_u32(bank->target, FLASH_REG_FLASH0_PROCON0, &protection[0]);
if (ret != ERROR_OK) {
LOG_ERROR("Unable to read flash User0 protection register");
return ret;
}if (ret != ERROR_OK) { ... }
ret = target_read_u32(bank->target, FLASH_REG_FLASH0_PROCON1, &protection[1]);
if (ret != ERROR_OK) {
LOG_ERROR("Unable to read flash User1 protection register");
return ret;
}if (ret != ERROR_OK) { ... }
ret = target_read_u32(bank->target, FLASH_REG_FLASH0_PROCON2, &protection[2]);
if (ret != ERROR_OK) {
LOG_ERROR("Unable to read flash User2 protection register");
return ret;
}if (ret != ERROR_OK) { ... }
unsigned int sectors = bank->num_sectors;
/* ... */
if (sectors == 12)
sectors--;
for (unsigned int i = 0; i < bank->num_sectors; i++) {
bank->sectors[i].is_protected = 0;
fb->write_prot_otp[i] = false;
}for (unsigned int i = 0; i < bank->num_sectors; i++) { ... }
fb->read_protected = false;
/* ... */
for (unsigned int i = 0; i < ARRAY_SIZE(protection); i++) {
/* ... */
for (unsigned int j = 0; j < sectors; j++) {
int set = (protection[i] & (1 << j)) ? 1 : 0;
bank->sectors[j].is_protected |= set;
if (j == 10)
bank->sectors[j + 1].is_protected |= set;
/* ... */
if (i == 2 && set) {
fb->write_prot_otp[j] = true;
if (j == 10)
fb->write_prot_otp[j + 1] = true;
}if (i == 2 && set) { ... }
}for (unsigned int j = 0; j < sectors; j++) { ... }
}for (unsigned int i = 0; i < ARRAY_SIZE(protection); i++) { ... }
/* ... */
if (protection[0] & PROCON_RPRO_MASK)
fb->read_protected = true;
return ERROR_OK;
}{ ... }
FLASH_BANK_COMMAND_HANDLER(xmc4xxx_flash_bank_command)
{
bank->driver_priv = malloc(sizeof(struct xmc4xxx_flash_bank));
if (!bank->driver_priv)
return ERROR_FLASH_OPERATION_FAILED;
(void)memset(bank->driver_priv, 0, sizeof(struct xmc4xxx_flash_bank));
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(xmc4xxx_handle_flash_password_command)
{
int res;
struct flash_bank *bank;
if (CMD_ARGC < 3)
return ERROR_COMMAND_SYNTAX_ERROR;
res = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (res != ERROR_OK)
return res;
struct xmc4xxx_flash_bank *fb = bank->driver_priv;
errno = 0;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], fb->pw1);
if (errno)
return ERROR_COMMAND_SYNTAX_ERROR;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], fb->pw2);
if (errno)
return ERROR_COMMAND_SYNTAX_ERROR;
fb->pw_set = true;
command_print(CMD, "XMC4xxx flash passwords set to:\n");
command_print(CMD, "-0x%08"PRIx32"\n", fb->pw1);
command_print(CMD, "-0x%08"PRIx32"\n", fb->pw2);
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(xmc4xxx_handle_flash_unprotect_command)
{
struct flash_bank *bank;
int res;
int32_t level;
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
res = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (res != ERROR_OK)
return res;
COMMAND_PARSE_NUMBER(s32, CMD_ARGV[1], level);
res = xmc4xxx_flash_unprotect(bank, level);
return res;
}{ ... }
static const struct command_registration xmc4xxx_exec_command_handlers[] = {
{
.name = "flash_password",
.handler = xmc4xxx_handle_flash_password_command,
.mode = COMMAND_EXEC,
.usage = "bank_id password1 password2",
.help = "Set the flash passwords used for protect operations. "
"Passwords should be in standard hex form (0x00000000). "
"(You must call this before any other protect commands) "
"NOTE: The xmc4xxx's UCB area only allows for FOUR cycles. "
"Please use protection carefully!",
...},
{
.name = "flash_unprotect",
.handler = xmc4xxx_handle_flash_unprotect_command,
.mode = COMMAND_EXEC,
.usage = "bank_id user_level[0-1]",
.help = "Permanently Removes flash protection (read and write) "
"for the specified user level",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration xmc4xxx_command_handlers[] = {
{
.name = "xmc4xxx",
.mode = COMMAND_ANY,
.help = "xmc4xxx flash command group",
.usage = "",
.chain = xmc4xxx_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
const struct flash_driver xmc4xxx_flash = {
.name = "xmc4xxx",
.commands = xmc4xxx_command_handlers,
.flash_bank_command = xmc4xxx_flash_bank_command,
.erase = xmc4xxx_erase,
.write = xmc4xxx_write,
.read = default_flash_read,
.probe = xmc4xxx_probe,
.auto_probe = xmc4xxx_probe,
.erase_check = default_flash_blank_check,
.info = xmc4xxx_get_info_command,
.protect_check = xmc4xxx_protect_check,
.protect = xmc4xxx_protect,
.free_driver_priv = default_flash_free_driver_priv,
...};