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
42
43
44
45
46
47
48
49
50
51
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
100
101
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
135
136
137
140
141
145
146
147
148
149
150
151
152
153
154
155
156
157
158
164
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
201
202
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
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
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
312
313
314
315
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
346
347
348
349
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
389
390
391
392
393
394
395
396
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
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
538
539
540
541
542
543
544
545
546
547
548
549
553
554
555
556
560
561
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
590
591
592
593
594
595
596
597
598
599
600
601
602
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
640
641
646
647
648
649
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
706
707
708
709
710
711
715
716
717
718
719
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
746
747
752
753
754
755
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
796
797
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
821
826
827
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
888
889
890
891
892
893
894
895
896
897
898
905
906
907
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
962
963
964
965
966
967
968
969
970
971
972
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1029
1030
1031
1032
1033
1034
1035
1036
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <jtag/jtag.h>
#include "imp.h"
#include <target/algorithm.h>
#include <target/mips32.h>
#include <target/mips_m4k.h>
#include <target/target_type.h>
#include <target/register.h>
7 includes
#define PIC32_MANUF_ID 0x029
#define PIC32MM_PHYS_RAM 0x00000000
#define PIC32MM_PHYS_PGM_FLASH 0x1D000000
#define PIC32MM_PHYS_PERIPHERALS 0x1F800000
#define PIC32MM_PHYS_BOOT_FLASH 0x1FC00000
/* ... */
#define Virt2Phys(v) ((v) & 0x1FFFFFFF)
#define PIC32MM_DEVID 0xBF803660
#define PIC32MM_NVMCON 0xBF802930
#define PIC32MM_NVMCONCLR 0xBF802934
#define PIC32MM_NVMCONSET 0xBF802938
#define PIC32MM_NVMCONINV 0xBF80293C
#define NVMCON_NVMWR (1 << 15)
#define NVMCON_NVMWREN (1 << 14)
#define NVMCON_NVMERR (1 << 13)
#define NVMCON_LVDERR (1 << 12)
#define NVMCON_LVDSTAT (1 << 11)
#define NVMCON_OP_PFM_ERASE 0x5
#define NVMCON_OP_PAGE_ERASE 0x4
#define NVMCON_OP_ROW_PROG 0x3
#define NVMCON_OP_DWORD_PROG 0x2
#define NVMCON_OP_WORD_PROG 0x1
#define NVMCON_OP_NOP 0x0
#define PIC32MM_NVMKEY 0xBF802940
#define PIC32MM_NVMADDR 0xBF802950
#define PIC32MM_NVMADDRCLR 0xBF802954
#define PIC32MM_NVMADDRSET 0xBF802958
#define PIC32MM_NVMADDRINV 0xBF80295C
#define PIC32MM_NVMDATA0 0xBF802960
#define PIC32MM_NVMDATA1 0xBF802970
#define PIC32MM_NVMSRCADDR 0xBF802980
#define PIC32MM_NVMPWP 0xBF802990
#define PIC32MM_NVMBWP 0xBF8029A0
#define NVMKEY1 0xAA996655
#define NVMKEY2 0x556699AA
34 defines
/* ... */
static const struct {
const char *name;
uint32_t devid;
...} pic32mm_devs[] = {
{"PIC32MM0016GPL020", 0x6B04},
{"PIC32MM0032GPL020", 0x6B0C},
{"PIC32MM0064GPL020", 0x6B14},
{"PIC32MM0016GPL028", 0x6B02},
{"PIC32MM0032GPL028", 0x6B0A},
{"PIC32MM0064GPL028", 0x6B12},
{"PIC32MM0016GPL036", 0x6B06},
{"PIC32MM0032GPL036", 0x6B0B},
{"PIC32MM0064GPL036", 0x6B16},
{"PIC32MM0064GPM028", 0x7708},
{"PIC32MM0128GPM028", 0x7710},
{"PIC32MM0256GPM028", 0x7718},
{"PIC32MM0064GPM036", 0x770A},
{"PIC32MM0128GPM036", 0x7712},
{"PIC32MM0256GPM036", 0x771A},
{"PIC32MM0064GPM048", 0x772C},
{"PIC32MM0128GPM048", 0x7734},
{"PIC32MM0256GPM048", 0x773C},
{"PIC32MM0064GPM064", 0x770E},
{"PIC32MM0128GPM064", 0x7716},
{"PIC32MM0256GPM064", 0x771E},
...};
struct pic32mm_device_layout
{
uint32_t flash_size_in_bytes, ram_size_in_bytes, boot_flash_size_in_bytes;
uint32_t page_size_in_words, row_size_in_words;
uint32_t boot_pages_per_protection_region;
...};
enum
{
PIC32MM_FLASH_WORD_SIZE_IN_BYTES = 4,
...};
static inline bool pic32mm_is_boot_bank(struct flash_bank *bank)
{
return Virt2Phys(bank->base) == PIC32MM_PHYS_BOOT_FLASH;
}{ ... }
static const char *pic32mm_find_device(struct target *target, bool log);
static int pic32mm_compute_device_layout(struct target *target, struct pic32mm_device_layout *layout, bool log)
{
const char *pDeviceName = pic32mm_find_device(target, log);
if (!pDeviceName)
return ERROR_FAIL;
memset(layout, 0, sizeof(*layout));
int FLASHNumber = atoi(pDeviceName + 7);
int bootFLASHProtectionRegionSize = 0;
if (pDeviceName[13] == 'M')
{
layout->ram_size_in_bytes = ((FLASHNumber >= 256) ? 32 : 16) * 1024;
bootFLASHProtectionRegionSize = 0x800;
}if (pDeviceName[13] == 'M') { ... }
else if (pDeviceName[13] == 'L')
{
layout->ram_size_in_bytes = ((FLASHNumber >= 32) ? 8 : 4) * 1024;
bootFLASHProtectionRegionSize = 0x800;
}else if (pDeviceName[13] == 'L') { ... }
else
return ERROR_FAIL;
layout->flash_size_in_bytes = FLASHNumber * 1024;
layout->boot_flash_size_in_bytes = 0x1700;
layout->boot_flash_size_in_bytes += 0xE8;
layout->row_size_in_words = 64;
layout->page_size_in_words = 512;
layout->boot_pages_per_protection_region = bootFLASHProtectionRegionSize / (layout->page_size_in_words * PIC32MM_FLASH_WORD_SIZE_IN_BYTES);
if (log)
{
LOG_INFO("Detected %s with %d KB FLASH, %d KB boot FLASH and %d KB RAM.",
pDeviceName,
layout->flash_size_in_bytes / 1024,
layout->boot_flash_size_in_bytes / 1024,
layout->ram_size_in_bytes / 1024);
LOG_INFO("FLASH row size is %d words and page size is %d words",
layout->row_size_in_words,
layout->page_size_in_words);
}if (log) { ... }
return ERROR_OK;
}{ ... }
struct pic32mm_flash_bank {
bool probed;
struct pic32mm_device_layout layout;
...};
/* ... */
FLASH_BANK_COMMAND_HANDLER(pic32mm_flash_bank_command)
{
struct pic32mm_flash_bank *pic32mm_info;
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
pic32mm_info = calloc(1, sizeof(struct pic32mm_flash_bank));
bank->driver_priv = pic32mm_info;
pic32mm_info->probed = false;
return ERROR_OK;
}{ ... }
static uint32_t pic32mm_get_flash_status(struct flash_bank *bank)
{
struct target *target = bank->target;
uint32_t status;
target_read_u32(target, PIC32MM_NVMCON, &status);
return status;
}{ ... }
static uint32_t pic32mm_wait_status_busy(struct flash_bank *bank, int timeout)
{
uint32_t status;
while (((status = pic32mm_get_flash_status(bank)) & NVMCON_NVMWR) && (timeout-- > 0)) {
LOG_DEBUG("status: 0x%" PRIx32, status);
alive_sleep(1);
}while (((status = pic32mm_get_flash_status(bank)) & NVMCON_NVMWR) && (timeout-- > 0)) { ... }
if (timeout <= 0)
LOG_DEBUG("timeout: status: 0x%" PRIx32, status);
return status;
}{ ... }
static int pic32mm_nvm_exec(struct flash_bank *bank, uint32_t op, uint32_t timeout)
{
struct target *target = bank->target;
uint32_t status;
target_write_u32(target, PIC32MM_NVMCON, NVMCON_NVMWREN | op);
uint32_t tmp;
target_read_u32(target, PIC32MM_NVMCON, &tmp);
target_write_u32(target, PIC32MM_NVMKEY, NVMKEY1);
target_write_u32(target, PIC32MM_NVMKEY, NVMKEY2);
target_write_u32(target, PIC32MM_NVMCONSET, NVMCON_NVMWR);
status = pic32mm_wait_status_busy(bank, timeout);
target_write_u32(target, PIC32MM_NVMCONCLR, NVMCON_NVMWREN);
return status;
}{ ... }
static void pic32mm_recompute_sector_protection(struct flash_bank *bank);
static int pic32mm_protect_check(struct flash_bank *bank)
{
struct target *target = bank->target;
struct pic32mm_flash_bank *pic32mm_info = bank->driver_priv;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
pic32mm_recompute_sector_protection(bank);
return ERROR_OK;
}{ ... }
static int pic32mm_unprotect_sectors(struct flash_bank *bank, unsigned int first, unsigned int last)
{
uint32_t reg, mask = 0;
int retval;
struct pic32mm_flash_bank *pic32mm_info = bank->driver_priv;
if (pic32mm_is_boot_bank(bank))
{
retval = target_read_u32(bank->target, PIC32MM_NVMBWP, ®);
if (retval != ERROR_OK)
return retval;
for (unsigned page = first; page <= last; page++)
{
uint32_t group = page / pic32mm_info->layout.boot_pages_per_protection_region;
if (group > 2)
break;
mask |= (1 << (8 + group));
}for (unsigned page = first; page <= last; page++) { ... }
if (reg & mask)
{
if (!(reg & 0x8000))
{
LOG_ERROR("The NVMBWP register is locked and cannot be modified");
return ERROR_FLASH_OPERATION_FAILED;
}if (!(reg & 0x8000)) { ... }
reg &= ~mask;
if (!(reg & 0x8000))
{
LOG_ERROR("Internal error: trying to permanently lock NVMBWP");
return ERROR_FLASH_OPERATION_FAILED;
}if (!(reg & 0x8000)) { ... }
target_write_u32(bank->target, PIC32MM_NVMKEY, NVMKEY1);
target_write_u32(bank->target, PIC32MM_NVMKEY, NVMKEY2);
retval = target_write_u32(bank->target, PIC32MM_NVMBWP, reg);
}if (reg & mask) { ... }
return retval;
}if (pic32mm_is_boot_bank(bank)) { ... }
else
{
retval = target_read_u32(bank->target, PIC32MM_NVMPWP, ®);
if (retval != ERROR_OK)
return retval;
if (!(reg & 0x00FFFFFF))
return ERROR_OK;
uint32_t sector_offset = (last + 1) * pic32mm_info->layout.page_size_in_words * PIC32MM_FLASH_WORD_SIZE_IN_BYTES;
if (sector_offset < (reg & 0x00FFFFFF))
{
if (!(reg & 0x80000000))
{
LOG_ERROR("The NVMPWP register is locked and cannot be modified");
return ERROR_FLASH_OPERATION_FAILED;
}if (!(reg & 0x80000000)) { ... }
reg = (reg & 0xFF000000) | sector_offset;
if (!(reg & 0x80000000))
{
LOG_ERROR("Internal error: trying to permanently lock NVMPWP");
return ERROR_FLASH_OPERATION_FAILED;
}if (!(reg & 0x80000000)) { ... }
target_write_u32(bank->target, PIC32MM_NVMKEY, NVMKEY1);
target_write_u32(bank->target, PIC32MM_NVMKEY, NVMKEY2);
retval = target_write_u32(bank->target, PIC32MM_NVMPWP, reg);
}if (sector_offset < (reg & 0x00FFFFFF)) { ... }
return retval;
}else { ... }
}{ ... }
static int pic32mm_invalidate_flash_line_buffer(struct flash_bank *bank)
{
uint8_t tmp[4];
int res = target_read_memory(bank->target, bank->base, 4, 1, tmp);
if (res != ERROR_OK)
return res;
struct pic32mm_flash_bank *pic32mm_info = bank->driver_priv;
res = target_read_memory(bank->target, bank->base + pic32mm_info->layout.page_size_in_words * PIC32MM_FLASH_WORD_SIZE_IN_BYTES, 4, 1, tmp);
return res;
}{ ... }
static int pic32mm_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
struct target *target = bank->target;
uint32_t status;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
status = pic32mm_unprotect_sectors(bank, first, last);
if (status != ERROR_OK)
return status;
if ((first == 0) && (last == (bank->num_sectors - 1))
&& (Virt2Phys(bank->base) == PIC32MM_PHYS_PGM_FLASH)) {
/* ... */
LOG_DEBUG("Erasing entire program flash");
status = pic32mm_nvm_exec(bank, NVMCON_OP_PFM_ERASE, 50);
if (status & NVMCON_NVMERR)
return ERROR_FLASH_OPERATION_FAILED;
if (status & NVMCON_LVDERR)
return ERROR_FLASH_OPERATION_FAILED;
return ERROR_OK;
}if ((first == 0) && (last == (bank->num_sectors - 1)) && (Virt2Phys(bank->base) == PIC32MM_PHYS_PGM_FLASH)) { ... }
for (unsigned int i = first; i <= last; i++) {
target_write_u32(target, PIC32MM_NVMADDR, Virt2Phys(bank->base + bank->sectors[i].offset));
status = pic32mm_nvm_exec(bank, NVMCON_OP_PAGE_ERASE, 10);
if (status & NVMCON_NVMERR)
return ERROR_FLASH_OPERATION_FAILED;
if (status & NVMCON_LVDERR)
return ERROR_FLASH_OPERATION_FAILED;
bank->sectors[i].is_erased = 1;
}for (unsigned int i = first; i <= last; i++) { ... }
return ERROR_OK;
}{ ... }
static int pic32mm_protect(struct flash_bank *bank, int set, unsigned int first,
unsigned int last)
{
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
return ERROR_OK;
}{ ... }
static uint16_t pic32mm_flash_write_code[] = {
0x41a8, 0xaa99,
0x5108, 0x6655,
0x41a9, 0x5566,
0x5129, 0x99aa,
0x41aa, 0xbf80,
0x514a, 0x2930,
0x5160, 0x4003,
0x5180, 0x8000,
0xb266, 0x0040,
0xb413, 0x0010,
0x51a0, 0x4000,
0xf8aa, 0x0020,
0xf88a, 0x0050,
0x4060, 0x002b,
0x3084, 0x0100,
0x30a5, 0x0100,
0x9400, 0xffee,
0x30c6, 0xffc0,
0x41b5, 0xa000,
0x52b5, 0x0000,
0x02a4, 0x2290,
0x9400, 0x0014,
0x5160, 0x4002,
0xfe84, 0x0000,
0xfa8a, 0x0030,
0xfe84, 0x0004,
0xfa8a, 0x0040,
0xf8aa, 0x0020,
0x4060, 0x000d,
0x3084, 0x0008,
0x6ed4,
0x6f6e,
0x8f03,
0x6f6e,
0xaf6d,
0x0c00,
0x9400, 0x0002,
0x0c80,
0x0c91,
0x46c0,
0xf96a, 0x0000,
0xf90a, 0x0010,
0xf92a, 0x0010,
0xf98a, 0x0008,
0xfe0a, 0x0000,
0x0190, 0x8250,
0xac7b,
0x0c00,
0x0c00,
0x0c00,
0x0c00,
0x0c00,
0xfe2a, 0x0000,
0xd220, 0x3000,
0xace7,
0xf9aa, 0x0004,
0x459f,
0x0c00,
...};
static int pic32mm_call_flash_loader(struct target *target,
struct reg_param reg_params[],
struct working_area *source,
struct working_area *write_algorithm,
uint32_t address_in_flash,
uint32_t word_count)
{
struct mips32_algorithm mips32_info;
mips32_info.common_magic = MIPS32_COMMON_MAGIC;
mips32_info.isa_mode = MIPS32_ISA_MIPS32;
buf_set_u32(reg_params[0].value, 0, 32, Virt2Phys(source->address));
buf_set_u32(reg_params[1].value, 0, 32, Virt2Phys(address_in_flash));
buf_set_u32(reg_params[2].value, 0, 32, word_count);
if (!word_count || (word_count & 1))
{
LOG_ERROR("unexpected word count in pic32mm_call_flash_loader(): %d", word_count);
return ERROR_FLASH_OPERATION_FAILED;
}if (!word_count || (word_count & 1)) { ... }
int retval = target_run_algorithm(target,
0,
NULL,
3,
reg_params,
write_algorithm->address | 0x01,
0,
10000,
&mips32_info);
if (retval != ERROR_OK) {
LOG_ERROR("error executing pic32mm flash write algorithm");
return ERROR_FLASH_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
uint32_t status = buf_get_u32(reg_params[0].value, 0, 32);
if (status & NVMCON_NVMERR) {
LOG_ERROR("Flash write error NVMERR (status = 0x%08" PRIx32 ")", status);
return ERROR_FLASH_OPERATION_FAILED;
}if (status & NVMCON_NVMERR) { ... }
if (status & NVMCON_LVDERR) {
LOG_ERROR("Flash write error LVDERR (status = 0x%08" PRIx32 ")", status);
return ERROR_FLASH_OPERATION_FAILED;
}if (status & NVMCON_LVDERR) { ... }
return ERROR_OK;
}{ ... }
static int pic32mm_write_using_loader(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
{
struct target *target = bank->target;
uint32_t buffer_size = 16384;
struct working_area *write_algorithm;
struct working_area *source;
uint32_t address = bank->base + offset;
struct reg_param reg_params[3];
int retval = ERROR_OK;
struct pic32mm_flash_bank *pic32mm_info = bank->driver_priv;
uint32_t row_size = pic32mm_info->layout.row_size_in_words * PIC32MM_FLASH_WORD_SIZE_IN_BYTES;
if (target_alloc_working_area(target,
sizeof(pic32mm_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(pic32mm_flash_write_code), &write_algorithm) != ERROR_OK) { ... }
uint8_t code[sizeof(pic32mm_flash_write_code)];
target_buffer_set_u16_array(target,
code,
ARRAY_SIZE(pic32mm_flash_write_code),
pic32mm_flash_write_code);
retval = target_write_buffer(target, write_algorithm->address, sizeof(code), code);
if (retval != ERROR_OK)
return retval;
while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) {
buffer_size /= 2;
buffer_size = (buffer_size / row_size) * row_size;
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], "r4", 32, PARAM_IN_OUT);
init_reg_param(®_params[1], "r5", 32, PARAM_OUT);
init_reg_param(®_params[2], "r6", 32, PARAM_OUT);
uint8_t *multi_row_buffer = malloc(buffer_size);
while (count > 0)
{
uint32_t offset_in_row = offset % row_size;
if (offset_in_row)
memset(multi_row_buffer, 0xFF, offset_in_row);
uint32_t loadable_bytes = min(count, buffer_size - offset_in_row);
uint32_t programmable_bytes = loadable_bytes + offset_in_row;
memcpy(multi_row_buffer + offset_in_row, buffer, loadable_bytes);
uint32_t post_padding = programmable_bytes % row_size;
if (post_padding)
post_padding = row_size - post_padding;
if((programmable_bytes + post_padding) > buffer_size)
{
LOG_ERROR("pic32mm: Internal error: invalid post-padding");
return ERROR_FAIL;
}if ((programmable_bytes + post_padding) > buffer_size) { ... }
if (post_padding)
{
memset(multi_row_buffer + programmable_bytes, 0xFF, post_padding);
programmable_bytes += post_padding;
}if (post_padding) { ... }
uint32_t row_offset = offset - offset_in_row;
if ((programmable_bytes % row_size) || (row_offset % row_size))
{
LOG_ERROR("pic32mm: Internal error: multi-row buffer is not properly aligned");
return ERROR_FAIL;
}if ((programmable_bytes % row_size) || (row_offset % row_size)) { ... }
retval = target_write_buffer(target,
source->address,
programmable_bytes,
multi_row_buffer);
if (retval != ERROR_OK)
break;
retval = pic32mm_call_flash_loader(target, reg_params, source, write_algorithm, address, programmable_bytes / PIC32MM_FLASH_WORD_SIZE_IN_BYTES);
count -= loadable_bytes;
offset += loadable_bytes;
buffer += loadable_bytes;
address += loadable_bytes;
}while (count > 0) { ... }
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]);
free(multi_row_buffer);
return retval;
}{ ... }
static int pic32mm_write_dword(struct flash_bank *bank, uint32_t address, uint32_t word0, uint32_t word1)
{
struct target *target = bank->target;
target_write_u32(target, PIC32MM_NVMADDR, Virt2Phys(address));
target_write_u32(target, PIC32MM_NVMDATA0, word0);
target_write_u32(target, PIC32MM_NVMDATA1, word1);
int status = pic32mm_nvm_exec(bank, NVMCON_OP_DWORD_PROG, 5);
if (status & NVMCON_NVMERR)
return ERROR_FLASH_OPERATION_FAILED;
if (status & NVMCON_LVDERR)
return ERROR_FLASH_OPERATION_FAILED;
return ERROR_OK;
}{ ... }
static int pic32mm_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
{
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
int retval = pic32mm_write_using_loader(bank, buffer, offset, count);
if (retval == ERROR_OK)
return retval;
if (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
LOG_ERROR("flash writing failed");
return retval;
}if (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE) { ... }
LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
union
{
uint32_t u32[2];
uint8_t bytes[8];
...} pair;
while (count > 0)
{
uint32_t offset_in_row = offset % sizeof(pair);
if (offset_in_row)
memset(pair.bytes, 0xFF, offset_in_row);
uint32_t loadable_bytes = min(count, sizeof(pair) - offset_in_row);
uint32_t programmable_bytes = loadable_bytes + offset_in_row;
memcpy(pair.bytes + offset_in_row, buffer, loadable_bytes);
uint32_t post_padding = programmable_bytes % sizeof(pair);
if (post_padding)
post_padding = sizeof(pair) - post_padding;
if((programmable_bytes + post_padding) > sizeof(pair))
{
LOG_ERROR("pic32mm: Internal error: invalid post-padding");
return ERROR_FAIL;
}if ((programmable_bytes + post_padding) > sizeof(pair)) { ... }
if (post_padding)
{
memset(pair.bytes + programmable_bytes, 0xFF, post_padding);
programmable_bytes += post_padding;
}if (post_padding) { ... }
uint32_t row_offset = offset - offset_in_row;
if ((programmable_bytes % sizeof(pair)) || (row_offset % sizeof(pair)))
{
LOG_ERROR("pic32mm: Internal error: multi-row buffer is not properly aligned");
return ERROR_FAIL;
}if ((programmable_bytes % sizeof(pair)) || (row_offset % sizeof(pair))) { ... }
int retval = pic32mm_write_dword(bank, bank->base + row_offset, pair.u32[0], pair.u32[1]);
if (retval != ERROR_OK)
return retval;
count -= loadable_bytes;
offset += loadable_bytes;
buffer += loadable_bytes;
}while (count > 0) { ... }
return ERROR_OK;
}{ ... }
static const char *pic32mm_find_device(struct target *target, bool log)
{
struct mips32_common *mips32 = target->arch_info;
struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
uint32_t devID = (unsigned)((ejtag_info->idcode >> 12) & 0xffff);
uint32_t manufacturingID = (unsigned)((ejtag_info->idcode >> 1) & 0x7ff);
int retval;
if (log)
{
LOG_INFO("device id = 0x%08" PRIx32 " (manuf 0x%03x dev 0x%04x, ver 0x%02x)",
ejtag_info->idcode,
manufacturingID,
(unsigned)((ejtag_info->idcode >> 28) & 0xf),
devID);
}if (log) { ... }
if (manufacturingID != PIC32_MANUF_ID) {
LOG_WARNING("Cannot identify target as a PIC32MM family. Unexpected manufacturing ID: %02x", manufacturingID);
return NULL;
}if (manufacturingID != PIC32_MANUF_ID) { ... }
for (int i = 0; i < (sizeof(pic32mm_devs) / sizeof(pic32mm_devs[0])); i++) {
if (pic32mm_devs[i].devid == devID) {
return pic32mm_devs[i].name;
}if (pic32mm_devs[i].devid == devID) { ... }
}for (int i = 0; i < (sizeof(pic32mm_devs) / sizeof(pic32mm_devs[0])); i++) { ... }
if (log)
LOG_WARNING("Cannot identify target as a PIC32MM family. Unexpected device ID: %02x", devID);
return NULL;
}{ ... }
static void pic32mm_recompute_sector_protection(struct flash_bank *bank)
{
struct pic32mm_flash_bank *pic32mm_info = bank->driver_priv;
struct target *target = bank->target;
uint32_t protection_status = 0;
if (pic32mm_is_boot_bank(bank))
{
if (target_read_u32(target, PIC32MM_NVMBWP, &protection_status) != ERROR_OK)
protection_status = 0;
}if (pic32mm_is_boot_bank(bank)) { ... }
else
{
if (target_read_u32(target, PIC32MM_NVMPWP, &protection_status) != ERROR_OK)
protection_status = 0;
}else { ... }
for (unsigned i = 0; i < bank->num_sectors; i++) {
if(pic32mm_is_boot_bank(bank))
{
int mask = 1 << (8 + (i / pic32mm_info->layout.boot_pages_per_protection_region));
bank->sectors[i].is_protected = (protection_status & mask) != 0;
}if (pic32mm_is_boot_bank(bank)) { ... }
else
bank->sectors[i].is_protected = (bank->sectors[i].offset + bank->sectors[i].size) <= (protection_status & 0x00FFFFFF);
}for (unsigned i = 0; i < bank->num_sectors; i++) { ... }
}{ ... }
static int pic32mm_probe(struct flash_bank *bank)
{
struct target *target = bank->target;
struct pic32mm_flash_bank *pic32mm_info = bank->driver_priv;
struct mips32_common *mips32 = target->arch_info;
struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
uint32_t devID = (unsigned)((ejtag_info->idcode >> 12) & 0xffff);
uint32_t manufacturingID = (unsigned)((ejtag_info->idcode >> 1) & 0x7ff);
int retval;
pic32mm_info->probed = false;
retval = pic32mm_compute_device_layout(target, &pic32mm_info->layout, false);
if (retval != ERROR_OK)
{
LOG_WARNING("Cannot compute FLASH memory layout");
return ERROR_FLASH_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
free(bank->sectors);
unsigned page_size_in_bytes = pic32mm_info->layout.page_size_in_words * PIC32MM_FLASH_WORD_SIZE_IN_BYTES;
bank->size = pic32mm_is_boot_bank(bank) ? pic32mm_info->layout.boot_flash_size_in_bytes : pic32mm_info->layout.flash_size_in_bytes;
bank->num_sectors = (bank->size + page_size_in_bytes - 1) / page_size_in_bytes;
bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
for (unsigned i = 0; i < bank->num_sectors; i++) {
bank->sectors[i].offset = i * page_size_in_bytes;
bank->sectors[i].size = page_size_in_bytes;
bank->sectors[i].is_erased = -1;
if ((bank->sectors[i].offset + bank->sectors[i].size) > bank->size)
bank->sectors[i].size = bank->size - bank->sectors[i].offset;
}for (unsigned i = 0; i < bank->num_sectors; i++) { ... }
pic32mm_recompute_sector_protection(bank);
pic32mm_info->probed = true;
return ERROR_OK;
}{ ... }
static int pic32mm_auto_probe(struct flash_bank *bank)
{
struct pic32mm_flash_bank *pic32mm_info = bank->driver_priv;
if (pic32mm_info->probed)
return ERROR_OK;
return pic32mm_probe(bank);
}{ ... }
static int pic32mm_info(struct flash_bank *bank, struct command_invocation *cmd)
{
struct target *target = bank->target;
struct mips32_common *mips32 = target->arch_info;
struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
uint32_t device_id;
device_id = ejtag_info->idcode;
if (((device_id >> 1) & 0x7ff) != PIC32_MANUF_ID) {
command_print_sameline(cmd,
"Cannot identify target as a PIC32MM family (manufacturer 0x%03x != 0x%03x)\n",
(unsigned)((device_id >> 1) & 0x7ff),
PIC32_MANUF_ID);
return ERROR_FLASH_OPERATION_FAILED;
}if (((device_id >> 1) & 0x7ff) != PIC32_MANUF_ID) { ... }
int i;
for (i = 0; pic32mm_devs[i].name != NULL; i++) {
if (pic32mm_devs[i].devid == (device_id & 0x0fffffff)) {
command_print_sameline(cmd, "PIC32MM%s", pic32mm_devs[i].name);
break;
}if (pic32mm_devs[i].devid == (device_id & 0x0fffffff)) { ... }
}for (i = 0; pic32mm_devs[i].name != NULL; i++) { ... }
if (pic32mm_devs[i].name == NULL)
command_print_sameline(cmd, "Unknown");
command_print_sameline(cmd,
" Ver: 0x%02x",
(unsigned)((device_id >> 28) & 0xf));
return ERROR_OK;
}{ ... }
extern const struct flash_driver virtual_flash;
struct flash_bank *virtual_get_master_bank(struct flash_bank *bank);
COMMAND_HANDLER(pic32mm_handle_find_work_area_command)
{
struct target *target = get_current_target(CMD_CTX);
struct pic32mm_device_layout layout;
int retval = pic32mm_compute_device_layout(target, &layout, true);
if (retval != ERROR_OK)
return retval;
target->working_area_size = layout.ram_size_in_bytes;
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(pic32mm_handle_pgm_word_command)
{
uint32_t address;
uint64_t value;
int status, res;
if (CMD_ARGC != 3)
return ERROR_COMMAND_SYNTAX_ERROR;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
COMMAND_PARSE_NUMBER(u64, CMD_ARGV[1], value);
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 2, &bank);
if (ERROR_OK != retval)
return retval;
if (address < bank->base || address >= (bank->base + bank->size)) {
command_print(CMD, "flash address '%s' is out of bounds", CMD_ARGV[0]);
return ERROR_OK;
}if (address < bank->base || address >= (bank->base + bank->size)) { ... }
struct flash_bank *pic32mm_bank = bank;
if (bank->driver == &virtual_flash)
pic32mm_bank = virtual_get_master_bank(bank);
struct pic32mm_flash_bank *pic32mm_info = pic32mm_bank->driver_priv;
unsigned sector = (address - bank->base) / (pic32mm_info->layout.page_size_in_words * PIC32MM_FLASH_WORD_SIZE_IN_BYTES);
res = pic32mm_unprotect_sectors(pic32mm_bank, sector, sector);
if (res != ERROR_OK)
{
command_print(CMD, "failed to unlock FLASH");
return ERROR_OK;
}if (res != ERROR_OK) { ... }
res = pic32mm_write_dword(pic32mm_bank, address, (uint32_t)value, (uint32_t)(value >> 32));
if (res != ERROR_OK)
command_print(CMD, "pic32mm pgm word failed (status = 0x%x)", status);
else
{
pic32mm_invalidate_flash_line_buffer(bank);
uint64_t read_value;
res = target_read_memory(bank->target, address, 4, 2, (uint8_t *)&read_value);
if (read_value == value)
command_print(CMD, "pic32mm: programmed and verified word at 0x%08x", address);
else
command_print(CMD, "pic32mm: failed to verify word at 0x%08x. Read 0x%16llx", address, read_value);
}else { ... }
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(pic32mm_handle_unlock_command)
{
struct target *target = NULL;
struct mips_m4k_common *mips_m4k;
struct mips_ejtag *ejtag_info;
int timeout = 10;
if (CMD_ARGC < 1) {
command_print(CMD, "pic32mm unlock <bank>");
return ERROR_COMMAND_SYNTAX_ERROR;
}if (CMD_ARGC < 1) { ... }
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (ERROR_OK != retval)
return retval;
target = bank->target;
mips_m4k = target_to_m4k(target);
ejtag_info = &mips_m4k->mips32.ejtag_info;
mips_ejtag_set_instr(ejtag_info, MTAP_SW_MTAP);
mips_ejtag_set_instr(ejtag_info, MTAP_COMMAND);
uint8_t mchip_cmd = MCHP_STATUS;
mips_ejtag_drscan_8(ejtag_info, &mchip_cmd);
if (mchip_cmd & (1 << 7)) {
command_print(CMD, "pic32mm is already unlocked, erasing anyway");
}if (mchip_cmd & (1 << 7)) { ... }
mips_ejtag_drscan_8_out(ejtag_info, MCHP_ASERT_RST);
jtag_add_sleep(200);
mips_ejtag_drscan_8_out(ejtag_info, MCHP_ERASE);
do {
mchip_cmd = MCHP_STATUS;
mips_ejtag_drscan_8(ejtag_info, &mchip_cmd);
if (timeout-- == 0) {
LOG_DEBUG("timeout waiting for unlock: 0x%" PRIx8 "", mchip_cmd);
break;
}if (timeout-- == 0) { ... }
alive_sleep(1);
...} while ((mchip_cmd & (1 << 2)) || (!(mchip_cmd & (1 << 3))));
mips_ejtag_drscan_8_out(ejtag_info, MCHP_DE_ASSERT_RST);
mips_ejtag_set_instr(ejtag_info, MTAP_SW_ETAP);
command_print(CMD, "pic32mm unlocked.\n"
"INFO: a reset or power cycle is required "
"for the new settings to take effect.");
return ERROR_OK;
}{ ... }
static int pic32mm_verify(struct flash_bank *bank,
const uint8_t *buffer,
uint32_t offset,
uint32_t count)
{
uint8_t *tmp = (uint8_t *)malloc(count + 4);
if (!tmp)
return ENOMEM;
uint32_t aligned_offset = offset & ~3;
int retval = target_read_memory(bank->target, bank->base + aligned_offset, 4, (count + offset - aligned_offset + 3) / 4, tmp);
int status = memcmp(tmp + (offset - aligned_offset), buffer, count);
if (status)
{
LOG_INFO("The following FLASH bytes do not match the expected values:");
LOG_INFO(" Address | Expected | Actual");
bool hasNonFICDDifferences = false;
for (int i = 0; i < count; i++)
{
uint8_t expected = buffer[i];
uint8_t actual = tmp[offset - aligned_offset + i];
if (expected != actual)
{
uint32_t address = (uint32_t)(bank->base + offset + i);
LOG_INFO("0x%08x | 0x%02X | 0x%02X ", address, expected, actual);
if(Virt2Phys(address) != 0x1FC017C8 || ((expected ^ actual) & 0x23))
hasNonFICDDifferences = true;
}if (expected != actual) { ... }
}for (int i = 0; i < count; i++) { ... }
if (!hasNonFICDDifferences)
{
LOG_INFO("PIC32MM: ignoring different FICD value during FLASH verification");
status = 0;
}if (!hasNonFICDDifferences) { ... }
}if (status) { ... }
free(tmp);
if (status == 0)
return ERROR_OK;
else
return ERROR_FAIL;
}{ ... }
static const struct command_registration pic32mm_exec_command_handlers[] = {
{
.name = "pgm_word",
.usage = "<addr> <value> <bank>",
.handler = pic32mm_handle_pgm_word_command,
.mode = COMMAND_EXEC,
.help = "program a word",
...},
{
.name = "find_work_area",
.handler = pic32mm_handle_find_work_area_command,
.mode = COMMAND_EXEC,
.usage = "",
.help = "Find work area based on the chip ID",
...},
{
.name = "unlock",
.handler = pic32mm_handle_unlock_command,
.mode = COMMAND_EXEC,
.usage = "[bank_id]",
.help = "Unlock/Erase entire device.",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration pic32mm_command_handlers[] = {
{
.name = "pic32mm",
.mode = COMMAND_ANY,
.help = "pic32mm flash command group",
.usage = "",
.chain = pic32mm_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
const struct flash_driver pic32mm_flash = {
.name = "pic32mm",
.commands = pic32mm_command_handlers,
.flash_bank_command = pic32mm_flash_bank_command,
.erase = pic32mm_erase,
.protect = pic32mm_protect,
.write = pic32mm_write,
.read = default_flash_read,
.probe = pic32mm_probe,
.auto_probe = pic32mm_auto_probe,
.erase_check = default_flash_blank_check,
.protect_check = pic32mm_protect_check,
.verify = pic32mm_verify,
.info = pic32mm_info,
.free_driver_priv = default_flash_free_driver_priv,
...};