1
2
3
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
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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
201
212
215
216
217
218
219
220
221
222
223
225
226
227
233
234
235
236
237
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
424
425
426
427
428
429
430
434
435
436
437
438
439
440
441
442
446
450
451
452
453
454
455
456
457
458
462
463
464
465
466
467
468
469
470
474
478
482
486
487
488
489
490
491
492
493
494
498
502
506
510
514
515
516
517
518
519
520
521
522
526
530
534
538
539
540
543
545
546
547
552
553
554
555
556
557
558
559
560
561
562
563
564
568
569
570
571
572
573
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
664
665
666
667
668
669
676
677
678
679
680
681
682
683
684
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
722
723
724
725
726
727
728
729
730
731
737
738
743
744
745
746
747
748
749
750
751
752
756
757
758
759
760
761
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
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
817
818
819
820
821
822
823
824
825
826
827
828
829
833
834
835
836
837
838
839
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
871
872
873
874
875
876
877
878
879
880
881
882
883
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
911
912
913
914
915
916
917
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
967
968
969
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
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
1040
1041
1042
1043
1044
1045
1049
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
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
/* ... */
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include <helper/binarybuffer.h>
#define DBGU_CIDR 0xFFFFF240
#define CKGR_MCFR 0xFFFFFC24
#define CKGR_MOR 0xFFFFFC20
#define CKGR_MCFR_MAINRDY 0x10000
#define CKGR_PLLR 0xFFFFFC2c
#define CKGR_PLLR_DIV 0xff
#define CKGR_PLLR_MUL 0x07ff0000
#define PMC_MCKR 0xFFFFFC30
#define PMC_MCKR_CSS 0x03
#define PMC_MCKR_PRES 0x1c
#define WP 0x01
#define SLB 0x02
#define WPL 0x03
#define CLB 0x04
#define EA 0x08
#define SGPB 0x0B
#define CGPB 0x0D
#define SSB 0x0F
#define MC_FSR_FRDY 1
#define MC_FSR_EOL 2
#define RC_FREQ 32000
#define FMR_TIMING_NONE 0
#define FMR_TIMING_NVBITS 1
#define FMR_TIMING_FLASH 2
#define FLASH_SIZE_8KB 1
#define FLASH_SIZE_16KB 2
#define FLASH_SIZE_32KB 3
#define FLASH_SIZE_64KB 5
#define FLASH_SIZE_128KB 7
#define FLASH_SIZE_256KB 9
#define FLASH_SIZE_512KB 10
#define FLASH_SIZE_1024KB 12
#define FLASH_SIZE_2048KB 14
33 defines
static int at91sam7_protect_check(struct flash_bank *bank);
static int at91sam7_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset,
uint32_t count);
static uint32_t at91sam7_get_flash_status(struct target *target, int bank_number);
static void at91sam7_set_flash_mode(struct flash_bank *bank, int mode);
static uint32_t at91sam7_wait_status_busy(struct flash_bank *bank, uint32_t waitbits, int timeout);
static int at91sam7_flash_command(struct flash_bank *bank, uint8_t cmd, uint16_t pagen);
static const uint32_t mc_fmr[4] = { 0xFFFFFF60, 0xFFFFFF70, 0xFFFFFF80, 0xFFFFFF90 };
static const uint32_t mc_fcr[4] = { 0xFFFFFF64, 0xFFFFFF74, 0xFFFFFF84, 0xFFFFFF94 };
static const uint32_t mc_fsr[4] = { 0xFFFFFF68, 0xFFFFFF78, 0xFFFFFF88, 0xFFFFFF98 };
static const char *eproc[8] = {
"Unknown", "ARM946-E", "ARM7TDMI", "Unknown", "ARM920T", "ARM926EJ-S", "Unknown", "Unknown"
...};
struct at91sam7_flash_bank {
uint32_t cidr;
uint16_t cidr_ext;
uint16_t cidr_nvptyp;
uint16_t cidr_arch;
uint16_t cidr_sramsiz;
uint16_t cidr_nvpsiz;
uint16_t cidr_nvpsiz2;
uint16_t cidr_eproc;
uint16_t cidr_version;
const char *target_name;
uint8_t flash_autodetection;
uint16_t pages_per_sector;
uint16_t pagesize;
uint16_t pages_in_lockregion;
uint16_t num_lockbits_on;
uint16_t lockbits;
uint16_t num_nvmbits;
uint16_t num_nvmbits_on;
uint16_t nvmbits;
uint8_t securitybit;
/* ... */
uint8_t flashmode;
uint8_t mck_valid;
uint32_t mck_freq;
uint32_t ext_freq;
...};
#if 0
static long SRAMSIZ[16] = {
-1,
0x0400,
0x0800,
-1,
0x1c000,
0x1000,
0x14000,
0x28000,
0x2000,
0x4000,
0x8000,
0x10000,
0x20000,
0x40000,
0x18000,
0x80000,
...};/* ... */
#endif
static uint32_t at91sam7_get_flash_status(struct target *target, int bank_number)
{
uint32_t fsr;
target_read_u32(target, mc_fsr[bank_number], &fsr);
return fsr;
}{ ... }
static void at91sam7_read_clock_info(struct flash_bank *bank)
{
struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
struct target *target = bank->target;
uint32_t mckr, mcfr, pllr, mor;
unsigned long tmp = 0, mainfreq;
target_read_u32(target, CKGR_MOR, &mor);
target_read_u32(target, CKGR_MCFR, &mcfr);
target_read_u32(target, PMC_MCKR, &mckr);
target_read_u32(target, CKGR_PLLR, &pllr);
at91sam7_info->mck_valid = 0;
at91sam7_info->mck_freq = 0;
switch (mckr & PMC_MCKR_CSS) {
case 0:
at91sam7_info->mck_valid = 1;
tmp = RC_FREQ;
break;
case 0:
case 1:
if ((mcfr & CKGR_MCFR_MAINRDY) &&
(at91sam7_info->ext_freq == 0)) {
at91sam7_info->mck_valid = 1;
tmp = RC_FREQ / 16ul * (mcfr & 0xffff);
}if ((mcfr & CKGR_MCFR_MAINRDY) && (at91sam7_info->ext_freq == 0)) { ... } else if (at91sam7_info->ext_freq != 0) {
at91sam7_info->mck_valid = 1;
tmp = at91sam7_info->ext_freq;
}else if (at91sam7_info->ext_freq != 0) { ... }
break;
case 1:
case 2:
break;
case 2:
case 3:
if ((mcfr & CKGR_MCFR_MAINRDY) &&
(at91sam7_info->ext_freq == 0)) {
target_read_u32(target, CKGR_PLLR, &pllr);
if (!(pllr & CKGR_PLLR_DIV))
break;
at91sam7_info->mck_valid = 1;
mainfreq = RC_FREQ / 16ul * (mcfr & 0xffff);
/* ... */
tmp = mainfreq / (pllr & CKGR_PLLR_DIV)*
(((pllr & CKGR_PLLR_MUL) >> 16) + 1);
}if ((mcfr & CKGR_MCFR_MAINRDY) && (at91sam7_info->ext_freq == 0)) { ... } else if ((at91sam7_info->ext_freq != 0) &&
((pllr&CKGR_PLLR_DIV) != 0)) {
at91sam7_info->mck_valid = 1;
tmp = at91sam7_info->ext_freq / (pllr&CKGR_PLLR_DIV)*
(((pllr & CKGR_PLLR_MUL) >> 16) + 1);
}else if ((at91sam7_info->ext_freq != 0) && ((pllr&CKGR_PLLR_DIV) != 0)) { ... }
break;case 3:
}switch (mckr & PMC_MCKR_CSS) { ... }
if ((((mckr & PMC_MCKR_PRES) >> 2) == 7) || (tmp == 0)) {
at91sam7_info->mck_valid = 0;
at91sam7_info->mck_freq = 0;
}if ((((mckr & PMC_MCKR_PRES) >> 2) == 7) || (tmp == 0)) { ... } else if (((mckr & PMC_MCKR_PRES) >> 2) != 0)
at91sam7_info->mck_freq = tmp >> ((mckr & PMC_MCKR_PRES) >> 2);
else
at91sam7_info->mck_freq = tmp;
}{ ... }
static void at91sam7_set_flash_mode(struct flash_bank *bank, int mode)
{
uint32_t fmr, fmcn = 0, fws = 0;
struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
struct target *target = bank->target;
if (mode && (mode != at91sam7_info->flashmode)) {
if (mode == FMR_TIMING_NVBITS) {
if (at91sam7_info->cidr_arch == 0x60) {
fmcn = (at91sam7_info->mck_freq/10000000ul) + 1;
}if (at91sam7_info->cidr_arch == 0x60) { ... } else {
fmcn = (at91sam7_info->mck_freq/1000000ul) + 1;
}else { ... }
}if (mode == FMR_TIMING_NVBITS) { ... } else if (mode == FMR_TIMING_FLASH) {
fmcn = (at91sam7_info->mck_freq/1000000ul)+
(at91sam7_info->mck_freq/2000000ul) + 1;
}else if (mode == FMR_TIMING_FLASH) { ... }
if (fmcn > 0xFF)
fmcn = 0xFF;
if (at91sam7_info->mck_freq <= 33333ul)
fmcn = 0;
if (at91sam7_info->mck_freq > 30000000ul)
fws = 1;
LOG_DEBUG("fmcn[%i]: %i", bank->bank_number, (int)(fmcn));
fmr = fmcn << 16 | fws << 8;
target_write_u32(target, mc_fmr[bank->bank_number], fmr);
}if (mode && (mode != at91sam7_info->flashmode)) { ... }
at91sam7_info->flashmode = mode;
}{ ... }
static uint32_t at91sam7_wait_status_busy(struct flash_bank *bank, uint32_t waitbits, int timeout)
{
uint32_t status;
while ((!((status = at91sam7_get_flash_status(bank->target,
bank->bank_number)) & waitbits)) && (timeout-- > 0)) {
LOG_DEBUG("status[%i]: 0x%" PRIx32 "", (int)bank->bank_number, status);
alive_sleep(1);
}while ((!((status = at91sam7_get_flash_status(bank->target, bank->bank_number)) & waitbits)) && (timeout-- > 0)) { ... }
LOG_DEBUG("status[%i]: 0x%" PRIx32 "", bank->bank_number, status);
if (status & 0x0C) {
LOG_ERROR("status register: 0x%" PRIx32 "", status);
if (status & 0x4)
LOG_ERROR("Lock Error Bit Detected, Operation Abort");
if (status & 0x8)
LOG_ERROR("Invalid command and/or bad keyword, Operation Abort");
if (status & 0x10)
LOG_ERROR("Security Bit Set, Operation Abort");
}if (status & 0x0C) { ... }
return status;
}{ ... }
static int at91sam7_flash_command(struct flash_bank *bank, uint8_t cmd, uint16_t pagen)
{
uint32_t fcr;
struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
struct target *target = bank->target;
fcr = (0x5A << 24) | ((pagen&0x3FF) << 8) | cmd;
target_write_u32(target, mc_fcr[bank->bank_number], fcr);
LOG_DEBUG("Flash command: 0x%" PRIx32 ", flash bank: %i, page number: %u",
fcr,
bank->bank_number + 1,
pagen);
if ((at91sam7_info->cidr_arch == 0x60) && ((cmd == SLB) | (cmd == CLB))) {
if (at91sam7_wait_status_busy(bank, MC_FSR_EOL, 10)&0x0C)
return ERROR_FLASH_OPERATION_FAILED;
return ERROR_OK;
}if ((at91sam7_info->cidr_arch == 0x60) && ((cmd == SLB) | (cmd == CLB))) { ... }
if (at91sam7_wait_status_busy(bank, MC_FSR_FRDY, 10)&0x0C)
return ERROR_FLASH_OPERATION_FAILED;
return ERROR_OK;
}{ ... }
static int at91sam7_read_part_info(struct flash_bank *bank)
{
struct at91sam7_flash_bank *at91sam7_info;
struct target *target = bank->target;
uint16_t bnk, sec;
uint16_t arch;
uint32_t cidr;
uint8_t banks_num = 0;
uint16_t num_nvmbits = 0;
uint16_t sectors_num = 0;
uint16_t pages_per_sector = 0;
uint16_t page_size = 0;
uint32_t ext_freq;
uint32_t bank_size;
uint32_t base_address = 0;
char *target_name_t = "Unknown";
at91sam7_info = bank->driver_priv;
if (at91sam7_info->cidr != 0) {
for (struct flash_bank *t_bank = bank; t_bank; t_bank = t_bank->next) {
if (t_bank->target != target)
continue;
at91sam7_read_clock_info(t_bank);
at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
at91sam7_protect_check(t_bank);
}for (struct flash_bank *t_bank = bank; t_bank; t_bank = t_bank->next) { ... }
return ERROR_OK;
}if (at91sam7_info->cidr != 0) { ... }
target_read_u32(target, DBGU_CIDR, &cidr);
if (cidr == 0) {
LOG_WARNING("Cannot identify target as an AT91SAM");
return ERROR_FLASH_OPERATION_FAILED;
}if (cidr == 0) { ... }
if (at91sam7_info->flash_autodetection == 0) {
for (struct flash_bank *t_bank = bank; t_bank; t_bank = t_bank->next) {
if (t_bank->target != target)
continue;
at91sam7_info = t_bank->driver_priv;
at91sam7_info->cidr = cidr;
at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
at91sam7_info->cidr_version = cidr&0x001F;
at91sam7_read_clock_info(t_bank);
at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
at91sam7_protect_check(t_bank);
}for (struct flash_bank *t_bank = bank; t_bank; t_bank = t_bank->next) { ... }
return ERROR_OK;
}if (at91sam7_info->flash_autodetection == 0) { ... }
arch = (cidr >> 20)&0x00FF;
switch ((cidr >> 8)&0x000F) {
case FLASH_SIZE_8KB:
break;
case FLASH_SIZE_8KB:
case FLASH_SIZE_16KB:
banks_num = 1;
sectors_num = 8;
pages_per_sector = 32;
page_size = 64;
base_address = 0x00100000;
if (arch == 0x70) {
num_nvmbits = 2;
target_name_t = "AT91SAM7S161/16";
}if (arch == 0x70) { ... }
break;
case FLASH_SIZE_16KB:
case FLASH_SIZE_32KB:
banks_num = 1;
sectors_num = 8;
pages_per_sector = 32;
page_size = 128;
base_address = 0x00100000;
if (arch == 0x70) {
num_nvmbits = 2;
target_name_t = "AT91SAM7S321/32";
}if (arch == 0x70) { ... }
if (arch == 0x72) {
num_nvmbits = 3;
target_name_t = "AT91SAM7SE32";
}if (arch == 0x72) { ... }
break;
case FLASH_SIZE_32KB:
case FLASH_SIZE_64KB:
banks_num = 1;
sectors_num = 16;
pages_per_sector = 32;
page_size = 128;
base_address = 0x00100000;
if (arch == 0x70) {
num_nvmbits = 2;
target_name_t = "AT91SAM7S64";
}if (arch == 0x70) { ... }
break;
case FLASH_SIZE_64KB:
case FLASH_SIZE_128KB:
banks_num = 1;
sectors_num = 8;
pages_per_sector = 64;
page_size = 256;
base_address = 0x00100000;
if (arch == 0x70) {
num_nvmbits = 2;
target_name_t = "AT91SAM7S128";
}if (arch == 0x70) { ... }
if (arch == 0x71) {
num_nvmbits = 3;
target_name_t = "AT91SAM7XC128";
}if (arch == 0x71) { ... }
if (arch == 0x72) {
num_nvmbits = 3;
target_name_t = "AT91SAM7SE128";
}if (arch == 0x72) { ... }
if (arch == 0x75) {
num_nvmbits = 3;
target_name_t = "AT91SAM7X128";
}if (arch == 0x75) { ... }
break;
case FLASH_SIZE_128KB:
case FLASH_SIZE_256KB:
banks_num = 1;
sectors_num = 16;
pages_per_sector = 64;
page_size = 256;
base_address = 0x00100000;
if (arch == 0x60) {
num_nvmbits = 3;
target_name_t = "AT91SAM7A3";
}if (arch == 0x60) { ... }
if (arch == 0x70) {
num_nvmbits = 2;
target_name_t = "AT91SAM7S256";
}if (arch == 0x70) { ... }
if (arch == 0x71) {
num_nvmbits = 3;
target_name_t = "AT91SAM7XC256";
}if (arch == 0x71) { ... }
if (arch == 0x72) {
num_nvmbits = 3;
target_name_t = "AT91SAM7SE256";
}if (arch == 0x72) { ... }
if (arch == 0x75) {
num_nvmbits = 3;
target_name_t = "AT91SAM7X256";
}if (arch == 0x75) { ... }
break;
case FLASH_SIZE_256KB:
case FLASH_SIZE_512KB:
banks_num = 2;
sectors_num = 16;
pages_per_sector = 64;
page_size = 256;
base_address = 0x00100000;
if (arch == 0x70) {
num_nvmbits = 2;
target_name_t = "AT91SAM7S512";
}if (arch == 0x70) { ... }
if (arch == 0x71) {
num_nvmbits = 3;
target_name_t = "AT91SAM7XC512";
}if (arch == 0x71) { ... }
if (arch == 0x72) {
num_nvmbits = 3;
target_name_t = "AT91SAM7SE512";
}if (arch == 0x72) { ... }
if (arch == 0x75) {
num_nvmbits = 3;
target_name_t = "AT91SAM7X512";
}if (arch == 0x75) { ... }
break;
case FLASH_SIZE_512KB:
case FLASH_SIZE_1024KB:
break;
case FLASH_SIZE_1024KB:
case FLASH_SIZE_2048KB:
break;case FLASH_SIZE_2048KB:
}switch ((cidr >> 8)&0x000F) { ... }
if (strcmp(target_name_t, "Unknown") == 0) {
LOG_ERROR(
"Target autodetection failed! Please specify target parameters in configuration file");
return ERROR_FLASH_OPERATION_FAILED;
}if (strcmp(target_name_t, "Unknown") == 0) { ... }
ext_freq = at91sam7_info->ext_freq;
bank_size = sectors_num * pages_per_sector * page_size;
for (bnk = 0; bnk < banks_num; bnk++) {
struct flash_bank *t_bank = bank;
if (bnk > 0) {
if (!t_bank->next) {
struct flash_bank *fb = calloc(1, sizeof(struct flash_bank));
if (!fb) {
LOG_ERROR("No memory for flash bank");
return ERROR_FAIL;
}if (!fb) { ... }
fb->target = target;
fb->driver = bank->driver;
fb->default_padded_value = 0xff;
fb->erased_value = 0xff;
fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
if (!fb->driver_priv) {
free(fb);
LOG_ERROR("No memory for flash driver priv");
return ERROR_FAIL;
}if (!fb->driver_priv) { ... }
fb->name = strdup("sam7_probed");
t_bank->next = fb;
}if (!t_bank->next) { ... }
t_bank = t_bank->next;
}if (bnk > 0) { ... }
t_bank->bank_number = bnk;
t_bank->base = base_address + bnk * bank_size;
t_bank->size = bank_size;
t_bank->num_sectors = sectors_num;
t_bank->sectors = malloc(sectors_num * sizeof(struct flash_sector));
for (sec = 0; sec < sectors_num; sec++) {
t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
t_bank->sectors[sec].size = pages_per_sector * page_size;
t_bank->sectors[sec].is_erased = -1;
t_bank->sectors[sec].is_protected = -1;
}for (sec = 0; sec < sectors_num; sec++) { ... }
at91sam7_info = t_bank->driver_priv;
at91sam7_info->cidr = cidr;
at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
at91sam7_info->cidr_version = cidr&0x001F;
at91sam7_info->target_name = target_name_t;
at91sam7_info->flashmode = 0;
at91sam7_info->ext_freq = ext_freq;
at91sam7_info->num_nvmbits = num_nvmbits;
at91sam7_info->num_nvmbits_on = 0;
at91sam7_info->pagesize = page_size;
at91sam7_info->pages_per_sector = pages_per_sector;
at91sam7_read_clock_info(t_bank);
at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
at91sam7_protect_check(t_bank);
}for (bnk = 0; bnk < banks_num; bnk++) { ... }
LOG_DEBUG("nvptyp: 0x%3.3x, arch: 0x%4.4x",
at91sam7_info->cidr_nvptyp,
at91sam7_info->cidr_arch);
return ERROR_OK;
}{ ... }
static int at91sam7_erase_check(struct flash_bank *bank)
{
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
at91sam7_read_clock_info(bank);
at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
return default_flash_blank_check(bank);
}{ ... }
static int at91sam7_protect_check(struct flash_bank *bank)
{
uint8_t lock_pos, gpnvm_pos;
uint32_t status;
struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
if (at91sam7_info->cidr == 0)
return ERROR_FLASH_BANK_NOT_PROBED;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
status = at91sam7_get_flash_status(bank->target, bank->bank_number);
at91sam7_info->lockbits = (status >> 16);
at91sam7_info->num_lockbits_on = 0;
for (lock_pos = 0; lock_pos < bank->num_sectors; lock_pos++) {
if (((status >> (16 + lock_pos))&(0x0001)) == 1) {
at91sam7_info->num_lockbits_on++;
bank->sectors[lock_pos].is_protected = 1;
}if (((status >> (16 + lock_pos))&(0x0001)) == 1) { ... } else
bank->sectors[lock_pos].is_protected = 0;
}for (lock_pos = 0; lock_pos < bank->num_sectors; lock_pos++) { ... }
status = at91sam7_get_flash_status(bank->target, 0);
at91sam7_info->securitybit = (status >> 4)&0x01;
at91sam7_info->nvmbits = (status >> 8)&0xFF;
at91sam7_info->num_nvmbits_on = 0;
for (gpnvm_pos = 0; gpnvm_pos < at91sam7_info->num_nvmbits; gpnvm_pos++) {
if (((status >> (8 + gpnvm_pos))&(0x01)) == 1)
at91sam7_info->num_nvmbits_on++;
}for (gpnvm_pos = 0; gpnvm_pos < at91sam7_info->num_nvmbits; gpnvm_pos++) { ... }
return ERROR_OK;
}{ ... }
FLASH_BANK_COMMAND_HANDLER(at91sam7_flash_bank_command)
{
struct flash_bank *t_bank = bank;
struct at91sam7_flash_bank *at91sam7_info;
struct target *target = t_bank->target;
uint32_t base_address;
uint32_t bank_size;
uint32_t ext_freq = 0;
unsigned int banks_num;
unsigned int num_sectors;
uint16_t pages_per_sector;
uint16_t page_size;
uint16_t num_nvmbits;
at91sam7_info = malloc(sizeof(struct at91sam7_flash_bank));
t_bank->driver_priv = at91sam7_info;
at91sam7_info->cidr = 0;
at91sam7_info->flashmode = 0;
at91sam7_info->ext_freq = 0;
at91sam7_info->flash_autodetection = 0;
if (CMD_ARGC < 13) {
at91sam7_info->flash_autodetection = 1;
return ERROR_OK;
}if (CMD_ARGC < 13) { ... }
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], base_address);
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[8], banks_num);
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[9], num_sectors);
COMMAND_PARSE_NUMBER(u16, CMD_ARGV[10], pages_per_sector);
COMMAND_PARSE_NUMBER(u16, CMD_ARGV[11], page_size);
COMMAND_PARSE_NUMBER(u16, CMD_ARGV[12], num_nvmbits);
if (CMD_ARGC == 14) {
unsigned long freq;
COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[13], freq);
ext_freq = freq * 1000;
at91sam7_info->ext_freq = ext_freq;
}if (CMD_ARGC == 14) { ... }
if ((banks_num == 0) || (num_sectors == 0) ||
(pages_per_sector == 0) || (page_size == 0) || (num_nvmbits == 0)) {
at91sam7_info->flash_autodetection = 1;
return ERROR_OK;
}if ((banks_num == 0) || (num_sectors == 0) || (pages_per_sector == 0) || (page_size == 0) || (num_nvmbits == 0)) { ... }
bank_size = num_sectors * pages_per_sector * page_size;
for (unsigned int bnk = 0; bnk < banks_num; bnk++) {
if (bnk > 0) {
if (!t_bank->next) {
struct flash_bank *fb = calloc(1, sizeof(struct flash_bank));
if (!fb) {
LOG_ERROR("No memory for flash bank");
return ERROR_FAIL;
}if (!fb) { ... }
fb->target = target;
fb->driver = bank->driver;
fb->default_padded_value = 0xff;
fb->erased_value = 0xff;
fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
if (!fb->driver_priv) {
free(fb);
LOG_ERROR("No memory for flash driver priv");
return ERROR_FAIL;
}if (!fb->driver_priv) { ... }
fb->name = strdup("sam7_probed");
t_bank->next = fb;
}if (!t_bank->next) { ... }
t_bank = t_bank->next;
}if (bnk > 0) { ... }
t_bank->bank_number = bnk;
t_bank->base = base_address + bnk * bank_size;
t_bank->size = bank_size;
t_bank->num_sectors = num_sectors;
t_bank->sectors = malloc(num_sectors * sizeof(struct flash_sector));
for (unsigned int sec = 0; sec < num_sectors; sec++) {
t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
t_bank->sectors[sec].size = pages_per_sector * page_size;
t_bank->sectors[sec].is_erased = -1;
t_bank->sectors[sec].is_protected = -1;
}for (unsigned int sec = 0; sec < num_sectors; sec++) { ... }
at91sam7_info = t_bank->driver_priv;
at91sam7_info->target_name = strdup(CMD_ARGV[7]);
at91sam7_info->flashmode = 0;
at91sam7_info->ext_freq = ext_freq;
at91sam7_info->num_nvmbits = num_nvmbits;
at91sam7_info->num_nvmbits_on = 0;
at91sam7_info->pagesize = page_size;
at91sam7_info->pages_per_sector = pages_per_sector;
}for (unsigned int bnk = 0; bnk < banks_num; bnk++) { ... }
return ERROR_OK;
}{ ... }
static int at91sam7_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
uint32_t nbytes, pos;
uint8_t *buffer;
uint8_t erase_all;
if (at91sam7_info->cidr == 0)
return ERROR_FLASH_BANK_NOT_PROBED;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if ((last < first) || (last >= bank->num_sectors))
return ERROR_FLASH_SECTOR_INVALID;
erase_all = 0;
if ((first == 0) && (last == (bank->num_sectors-1)))
erase_all = 1;
at91sam7_read_clock_info(bank);
at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
if (erase_all) {
if (at91sam7_flash_command(bank, EA, 0) != ERROR_OK)
return ERROR_FLASH_OPERATION_FAILED;
}if (erase_all) { ... } else {
nbytes = (last - first + 1) * bank->sectors[first].size;
buffer = malloc(nbytes * sizeof(uint8_t));
for (pos = 0; pos < nbytes; pos++)
buffer[pos] = 0xFF;
if (at91sam7_write(bank, buffer, bank->sectors[first].offset, nbytes) != ERROR_OK) {
free(buffer);
return ERROR_FLASH_OPERATION_FAILED;
}if (at91sam7_write(bank, buffer, bank->sectors[first].offset, nbytes) != ERROR_OK) { ... }
free(buffer);
}else { ... }
for (unsigned int sec = first; sec <= last; sec++)
bank->sectors[sec].is_erased = 1;
return ERROR_OK;
}{ ... }
static int at91sam7_protect(struct flash_bank *bank, int set,
unsigned int first, unsigned int last)
{
uint32_t cmd;
uint32_t pagen;
struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
if (at91sam7_info->cidr == 0)
return ERROR_FLASH_BANK_NOT_PROBED;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if ((last < first) || (last >= bank->num_sectors))
return ERROR_FLASH_SECTOR_INVALID;
at91sam7_read_clock_info(bank);
at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
for (unsigned int sector = first; sector <= last; sector++) {
if (set)
cmd = SLB;
else
cmd = CLB;
/* ... */
pagen = sector * at91sam7_info->pages_per_sector;
if (at91sam7_flash_command(bank, cmd, pagen) != ERROR_OK)
return ERROR_FLASH_OPERATION_FAILED;
}for (unsigned int sector = first; sector <= last; sector++) { ... }
at91sam7_protect_check(bank);
return ERROR_OK;
}{ ... }
static int at91sam7_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
{
int retval;
struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
struct target *target = bank->target;
uint32_t dst_min_alignment, wcount, bytes_remaining = count;
uint32_t first_page, last_page, pagen, buffer_pos;
if (at91sam7_info->cidr == 0)
return ERROR_FLASH_BANK_NOT_PROBED;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (offset + count > bank->size)
return ERROR_FLASH_DST_OUT_OF_BANK;
dst_min_alignment = at91sam7_info->pagesize;
if (offset % dst_min_alignment) {
LOG_WARNING("offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "",
offset,
dst_min_alignment);
return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
}if (offset % dst_min_alignment) { ... }
if (at91sam7_info->cidr_arch == 0)
return ERROR_FLASH_BANK_NOT_PROBED;
first_page = offset/dst_min_alignment;
last_page = DIV_ROUND_UP(offset + count, dst_min_alignment);
LOG_DEBUG("first_page: %i, last_page: %i, count %i",
(int)first_page,
(int)last_page,
(int)count);
at91sam7_read_clock_info(bank);
at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
for (pagen = first_page; pagen < last_page; pagen++) {
if (bytes_remaining < dst_min_alignment)
count = bytes_remaining;
else
count = dst_min_alignment;
bytes_remaining -= count;
buffer_pos = (pagen-first_page)*dst_min_alignment;
wcount = DIV_ROUND_UP(count, 4);
retval = target_write_memory(target, bank->base + pagen*dst_min_alignment, 4,
wcount, buffer + buffer_pos);
if (retval != ERROR_OK)
return retval;
if (at91sam7_flash_command(bank, WP, pagen) != ERROR_OK)
return ERROR_FLASH_OPERATION_FAILED;
LOG_DEBUG("Write flash bank:%u page number:%" PRIu32, bank->bank_number, pagen);
}for (pagen = first_page; pagen < last_page; pagen++) { ... }
return ERROR_OK;
}{ ... }
static int at91sam7_probe(struct flash_bank *bank)
{
/* ... */
int retval;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
retval = at91sam7_read_part_info(bank);
if (retval != ERROR_OK)
return retval;
return ERROR_OK;
}{ ... }
static int get_at91sam7_info(struct flash_bank *bank, struct command_invocation *cmd)
{
struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
if (at91sam7_info->cidr == 0)
return ERROR_FLASH_BANK_NOT_PROBED;
command_print_sameline(cmd, "\n at91sam7 driver information: Chip is %s\n",
at91sam7_info->target_name);
command_print_sameline(cmd,
" Cidr: 0x%8.8" PRIx32 " | Arch: 0x%4.4x | Eproc: %s | Version: 0x%3.3x | "
"Flashsize: 0x%8.8" PRIx32 "\n",
at91sam7_info->cidr,
at91sam7_info->cidr_arch,
eproc[at91sam7_info->cidr_eproc],
at91sam7_info->cidr_version,
bank->size);
command_print_sameline(cmd,
" Master clock (estimated): %u kHz | External clock: %u kHz\n",
(unsigned)(at91sam7_info->mck_freq / 1000),
(unsigned)(at91sam7_info->ext_freq / 1000));
command_print_sameline(cmd,
" Pagesize: %i bytes | Lockbits(%u): %i 0x%4.4x | Pages in lock region: %i\n",
at91sam7_info->pagesize,
bank->num_sectors,
at91sam7_info->num_lockbits_on,
at91sam7_info->lockbits,
at91sam7_info->pages_per_sector * at91sam7_info->num_lockbits_on);
command_print_sameline(cmd, " Securitybit: %i | Nvmbits(%i): %i 0x%1.1x\n",
at91sam7_info->securitybit, at91sam7_info->num_nvmbits,
at91sam7_info->num_nvmbits_on, at91sam7_info->nvmbits);
return ERROR_OK;
}{ ... }
/* ... */
COMMAND_HANDLER(at91sam7_handle_gpnvm_command)
{
struct flash_bank *bank;
int bit;
uint8_t flashcmd;
uint32_t status;
struct at91sam7_flash_bank *at91sam7_info;
int retval;
if (CMD_ARGC != 2)
return ERROR_COMMAND_SYNTAX_ERROR;
bank = get_flash_bank_by_num_noprobe(0);
if (!bank)
return ERROR_FLASH_BANK_INVALID;
if (strcmp(bank->driver->name, "at91sam7")) {
command_print(CMD, "not an at91sam7 flash bank '%s'", CMD_ARGV[0]);
return ERROR_FLASH_BANK_INVALID;
}if (strcmp(bank->driver->name, "at91sam7")) { ... }
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("target has to be halted to perform flash operation");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (strcmp(CMD_ARGV[1], "set") == 0)
flashcmd = SGPB;
else if (strcmp(CMD_ARGV[1], "clear") == 0)
flashcmd = CGPB;
else
return ERROR_COMMAND_SYNTAX_ERROR;
at91sam7_info = bank->driver_priv;
if (at91sam7_info->cidr == 0) {
retval = at91sam7_read_part_info(bank);
if (retval != ERROR_OK)
return retval;
}if (at91sam7_info->cidr == 0) { ... }
COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], bit);
if ((bit < 0) || (bit >= at91sam7_info->num_nvmbits)) {
command_print(CMD,
"gpnvm bit '#%s' is out of bounds for target %s",
CMD_ARGV[0],
at91sam7_info->target_name);
return ERROR_OK;
}if ((bit < 0) || (bit >= at91sam7_info->num_nvmbits)) { ... }
at91sam7_read_clock_info(bank);
at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
if (at91sam7_flash_command(bank, flashcmd, bit) != ERROR_OK)
return ERROR_FLASH_OPERATION_FAILED;
status = at91sam7_get_flash_status(bank->target, 0);
LOG_DEBUG("at91sam7_handle_gpnvm_command: cmd 0x%x, value %d, status 0x%" PRIx32,
flashcmd,
bit,
status);
at91sam7_protect_check(bank);
return ERROR_OK;
}{ ... }
static const struct command_registration at91sam7_exec_command_handlers[] = {
{
.name = "gpnvm",
.handler = at91sam7_handle_gpnvm_command,
.mode = COMMAND_EXEC,
.help = "set or clear one General Purpose Non-Volatile Memory "
"(gpnvm) bit",
.usage = "bitnum ('set'|'clear')",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration at91sam7_command_handlers[] = {
{
.name = "at91sam7",
.mode = COMMAND_ANY,
.help = "at91sam7 flash command group",
.usage = "",
.chain = at91sam7_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
const struct flash_driver at91sam7_flash = {
.name = "at91sam7",
.usage = "gpnvm <bit> <set | clear>",
.commands = at91sam7_command_handlers,
.flash_bank_command = at91sam7_flash_bank_command,
.erase = at91sam7_erase,
.protect = at91sam7_protect,
.write = at91sam7_write,
.read = default_flash_read,
.probe = at91sam7_probe,
.auto_probe = at91sam7_probe,
.erase_check = at91sam7_erase_check,
.protect_check = at91sam7_protect_check,
.info = get_at91sam7_info,
...};