1
2
3
7
8
9
10
11
12
13
14
15
16
17
22
23
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
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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
202
203
204
205
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
228
229
230
231
232
233
234
235
236
237
238
239
240
244
245
246
247
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
272
273
274
278
279
280
281
282
283
284
299
300
301
302
306
307
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
328
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
357
358
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
384
385
389
390
391
396
397
400
410
411
412
413
414
415
416
417
418
419
420
425
426
430
431
432
437
438
439
448
449
450
451
452
453
454
455
456
457
458
459
463
464
465
466
467
468
472
473
474
475
476
477
478
479
481
482
483
484
485
486
487
488
489
490
494
495
496
497
498
502
503
505
506
507
508
509
510
511
512
513
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
561
562
566
567
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
630
631
632
633
634
641
648
649
650
651
652
653
654
655
656
666
667
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include <jtag/jtag.h>
#include <target/cortex_m.h>
/* ... */
/* ... */
#define SAM4L_NUM_SECTORS 16
#define SAM4L_FLASH ((uint32_t)0x00000000)
#define SAM4L_FLASH_USER 0x00800000
#define SAM4L_FLASHCALW 0x400A0000
#define SAM4L_CHIPID 0x400E0740
#define SAM4L_FCR 0x00
#define SAM4L_FCMD 0x04
#define SAM4L_FSR 0x08
#define SAM4L_FPR 0x0C
#define SAM4L_FVR 0x10
#define SAM4L_FGPFRHI 0x14
#define SAM4L_FGPFRLO 0x18
#define SAM4L_CIDR 0x00
#define SAM4L_EXID 0x04
#define SAM4L_FCMD_NOP 0
#define SAM4L_FCMD_WP 1
#define SAM4L_FCMD_EP 2
#define SAM4L_FCMD_CPB 3
#define SAM4L_FCMD_LP 4
#define SAM4L_FCMD_UP 5
#define SAM4L_FCMD_EA 6
#define SAM4L_FCMD_WGPB 7
#define SAM4L_FCMD_EGPB 8
#define SAM4L_FCMD_SSB 9
#define SAM4L_FCMD_PGPFB 10
#define SAM4L_FCMD_EAGPF 11
#define SAM4L_FCMD_QPR 12
#define SAM4L_FCMD_WUP 13
#define SAM4L_FCMD_EUP 14
#define SAM4L_FCMD_QPRUP 15
#define SAM4L_FCMD_HSEN 16
#define SAM4L_FCMD_HSDIS 17
#define SAM4L_FMCD_CMDKEY 0xA5UL
#define SMAP_BASE 0x400A3000
#define SMAP_SCR (SMAP_BASE + 8)
#define SMAP_SCR_HCR (1 << 1)
36 defines
struct sam4l_chip_info {
uint32_t id;
uint32_t exid;
const char *name;
...};
static const struct sam4l_chip_info sam4l_known_chips[] = {
{ 0xAB0B0AE0, 0x1400000F, "ATSAM4LC8C" },
{ 0xAB0A09E0, 0x0400000F, "ATSAM4LC4C" },
{ 0xAB0A07E0, 0x0400000F, "ATSAM4LC2C" },
{ 0xAB0B0AE0, 0x1300000F, "ATSAM4LC8B" },
{ 0xAB0A09E0, 0x0300000F, "ATSAM4LC4B" },
{ 0xAB0A07E0, 0x0300000F, "ATSAM4LC2B" },
{ 0xAB0B0AE0, 0x1200000F, "ATSAM4LC8A" },
{ 0xAB0A09E0, 0x0200000F, "ATSAM4LC4A" },
{ 0xAB0A07E0, 0x0200000F, "ATSAM4LC2A" },
{ 0xAB0B0AE0, 0x14000002, "ATSAM4LS8C" },
{ 0xAB0A09E0, 0x04000002, "ATSAM4LS4C" },
{ 0xAB0A07E0, 0x04000002, "ATSAM4LS2C" },
{ 0xAB0B0AE0, 0x13000002, "ATSAM4LS8B" },
{ 0xAB0A09E0, 0x03000002, "ATSAM4LS4B" },
{ 0xAB0A07E0, 0x03000002, "ATSAM4LS2B" },
{ 0xAB0B0AE0, 0x12000002, "ATSAM4LS8A" },
{ 0xAB0A09E0, 0x02000002, "ATSAM4LS4A" },
{ 0xAB0A07E0, 0x02000002, "ATSAM4LS2A" },
...};
static const uint16_t sam4l_ram_sizes[16] = { 48, 1, 2, 6, 24, 4, 80, 160, 8, 16, 32, 64, 128, 256, 96, 512 };
static const uint16_t sam4l_page_sizes[8] = { 32, 64, 128, 256, 512, 1024, 2048, 4096 };
struct sam4l_info {
const struct sam4l_chip_info *details;
uint32_t flash_kb;
uint32_t ram_kb;
uint32_t page_size;
int num_pages;
int sector_size;
unsigned int pages_per_sector;
bool probed;
struct target *target;
...};
static int sam4l_flash_wait_until_ready(struct target *target)
{
volatile unsigned int t = 0;
uint32_t st;
int res;
do {
res = target_read_u32(target, SAM4L_FLASHCALW + SAM4L_FSR, &st);
...} while (res == ERROR_OK && !(st & (1<<0)) && ++t < 10);
return res;
}{ ... }
static int sam4l_flash_check_error(struct target *target, uint32_t *err)
{
uint32_t st;
int res;
res = target_read_u32(target, SAM4L_FLASHCALW + SAM4L_FSR, &st);
if (res == ERROR_OK)
*err = st & ((1<<3) | (1<<2));
return res;
}{ ... }
static int sam4l_flash_command(struct target *target, uint8_t cmd, int page)
{
int res;
uint32_t fcmd;
uint32_t err;
res = sam4l_flash_wait_until_ready(target);
if (res != ERROR_OK)
return res;
if (page >= 0) {
/* ... */
fcmd = (SAM4L_FMCD_CMDKEY << 24) | ((page & 0xFFFF) << 8) | (cmd & 0x3F);
}if (page >= 0) { ... } else {
res = target_read_u32(target, SAM4L_FLASHCALW + SAM4L_FCMD, &fcmd);
if (res != ERROR_OK)
return res;
fcmd &= ~0x3F;
fcmd |= (SAM4L_FMCD_CMDKEY << 24) | (cmd & 0x3F);
}else { ... }
res = target_write_u32(target, SAM4L_FLASHCALW + SAM4L_FCMD, fcmd);
if (res != ERROR_OK)
return res;
res = sam4l_flash_check_error(target, &err);
if (res != ERROR_OK)
return res;
if (err != 0)
LOG_ERROR("%s got error status 0x%08" PRIx32, __func__, err);
res = sam4l_flash_wait_until_ready(target);
return res;
}{ ... }
FLASH_BANK_COMMAND_HANDLER(sam4l_flash_bank_command)
{
if (bank->base != SAM4L_FLASH) {
LOG_ERROR("Address " TARGET_ADDR_FMT
" invalid bank address (try 0x%08" PRIx32
"[at91sam4l series] )",
bank->base, SAM4L_FLASH);
return ERROR_FAIL;
}if (bank->base != SAM4L_FLASH) { ... }
struct sam4l_info *chip;
chip = calloc(1, sizeof(*chip));
if (!chip) {
LOG_ERROR("No memory for flash bank chip info");
return ERROR_FAIL;
}if (!chip) { ... }
chip->target = bank->target;
chip->probed = false;
bank->driver_priv = chip;
return ERROR_OK;
}{ ... }
static const struct sam4l_chip_info *sam4l_find_chip_name(uint32_t id, uint32_t exid)
{
unsigned int i;
id &= ~0xF;
for (i = 0; i < ARRAY_SIZE(sam4l_known_chips); i++) {
if (sam4l_known_chips[i].id == id && sam4l_known_chips[i].exid == exid)
return &sam4l_known_chips[i];
}for (i = 0; i < ARRAY_SIZE(sam4l_known_chips); i++) { ... }
return NULL;
}{ ... }
static int sam4l_check_page_erased(struct flash_bank *bank, uint32_t pn,
bool *is_erased_p)
{
int res;
uint32_t st;
res = sam4l_flash_command(bank->target, SAM4L_FCMD_QPR, pn);
if (res != ERROR_OK) {
LOG_ERROR("Quick page read %" PRIu32 " failed", pn);
return res;
}if (res != ERROR_OK) { ... }
res = target_read_u32(bank->target, SAM4L_FLASHCALW + SAM4L_FSR, &st);
if (res != ERROR_OK) {
LOG_ERROR("Couldn't read erase status");
return res;
}if (res != ERROR_OK) { ... }
*is_erased_p = !!(st & (1<<5));
return ERROR_OK;
}{ ... }
static int sam4l_probe(struct flash_bank *bank)
{
uint32_t id, exid, param;
int res;
struct sam4l_info *chip = (struct sam4l_info *)bank->driver_priv;
if (chip->probed)
return ERROR_OK;
res = target_read_u32(bank->target, SAM4L_CHIPID + SAM4L_CIDR, &id);
if (res != ERROR_OK) {
LOG_ERROR("Couldn't read chip ID");
return res;
}if (res != ERROR_OK) { ... }
res = target_read_u32(bank->target, SAM4L_CHIPID + SAM4L_EXID, &exid);
if (res != ERROR_OK) {
LOG_ERROR("Couldn't read extended chip ID");
return res;
}if (res != ERROR_OK) { ... }
chip->details = sam4l_find_chip_name(id, exid);
chip->ram_kb = sam4l_ram_sizes[0xF & (id >> 16)];
switch (0xF & (id >> 8)) {
case 0x07:
chip->flash_kb = 128;
break;case 0x07:
case 0x09:
chip->flash_kb = 256;
break;case 0x09:
case 0x0A:
chip->flash_kb = 512;
break;case 0x0A:
default:
LOG_ERROR("Unknown flash size (chip ID is %08" PRIx32 "), assuming 128K", id);
chip->flash_kb = 128;
break;default
}switch (0xF & (id >> 8)) { ... }
res = target_read_u32(bank->target, SAM4L_FLASHCALW + SAM4L_FPR, ¶m);
if (res != ERROR_OK) {
LOG_ERROR("Couldn't read Flash parameters");
return res;
}if (res != ERROR_OK) { ... }
/* ... */
chip->page_size = sam4l_page_sizes[0x7 & (param >> 8)];
assert(chip->page_size);
chip->num_pages = chip->flash_kb * 1024 / chip->page_size;
chip->sector_size = (chip->flash_kb * 1024) / SAM4L_NUM_SECTORS;
chip->pages_per_sector = chip->sector_size / chip->page_size;
bank->size = chip->flash_kb * 1024;
bank->num_sectors = SAM4L_NUM_SECTORS;
bank->sectors = calloc(bank->num_sectors, (sizeof((bank->sectors)[0])));
if (!bank->sectors)
return ERROR_FAIL;
/* ... */
for (unsigned int i = 0; i < bank->num_sectors; i++) {
bank->sectors[i].size = chip->sector_size;
bank->sectors[i].offset = i * chip->sector_size;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = -1;
}for (unsigned int i = 0; i < bank->num_sectors; i++) { ... }
chip->probed = true;
LOG_INFO("SAM4L MCU: %s (Rev %c) (%" PRIu32 "KB Flash with %d %" PRIu32 "B pages, %" PRIu32 "KB RAM)",
chip->details ? chip->details->name : "unknown", (char)('A' + (id & 0xF)),
chip->flash_kb, chip->num_pages, chip->page_size, chip->ram_kb);
return ERROR_OK;
}{ ... }
static int sam4l_protect_check(struct flash_bank *bank)
{
int res;
uint32_t st;
struct sam4l_info *chip = (struct sam4l_info *)bank->driver_priv;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (!chip->probed) {
if (sam4l_probe(bank) != ERROR_OK)
return ERROR_FLASH_BANK_NOT_PROBED;
}if (!chip->probed) { ... }
res = target_read_u32(bank->target, SAM4L_FLASHCALW + SAM4L_FSR, &st);
if (res != ERROR_OK)
return res;
st >>= 16;
for (unsigned int i = 0; i < bank->num_sectors; i++)
bank->sectors[i].is_protected = !!(st & (1<<i));
return ERROR_OK;
}{ ... }
static int sam4l_protect(struct flash_bank *bank, int set, unsigned int first,
unsigned int last)
{
struct sam4l_info *chip = (struct sam4l_info *)bank->driver_priv;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (!chip->probed) {
if (sam4l_probe(bank) != ERROR_OK)
return ERROR_FLASH_BANK_NOT_PROBED;
}if (!chip->probed) { ... }
if (first >= bank->num_sectors || last >= bank->num_sectors) {
LOG_ERROR("Protect range %u - %u not valid (%u sectors total)", first, last,
bank->num_sectors);
return ERROR_FAIL;
}if (first >= bank->num_sectors || last >= bank->num_sectors) { ... }
/* ... */
for (unsigned int i = first; i <= last; i++) {
int res;
res = sam4l_flash_command(bank->target,
set ? SAM4L_FCMD_LP : SAM4L_FCMD_UP, i * chip->pages_per_sector);
if (res != ERROR_OK) {
LOG_ERROR("Can't %slock region containing page %d", set ? "" : "un", i);
return res;
}if (res != ERROR_OK) { ... }
}for (unsigned int i = first; i <= last; i++) { ... }
return ERROR_OK;
}{ ... }
static int sam4l_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
int ret;
struct sam4l_info *chip = (struct sam4l_info *)bank->driver_priv;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (!chip->probed) {
if (sam4l_probe(bank) != ERROR_OK)
return ERROR_FLASH_BANK_NOT_PROBED;
}if (!chip->probed) { ... }
if (first >= bank->num_sectors || last >= bank->num_sectors) {
LOG_ERROR("Erase range %u - %u not valid (%u sectors total)", first, last,
bank->num_sectors);
return ERROR_FAIL;
}if (first >= bank->num_sectors || last >= bank->num_sectors) { ... }
if ((first == 0) && ((last + 1) == bank->num_sectors)) {
LOG_DEBUG("Erasing the whole chip");
ret = sam4l_flash_command(bank->target, SAM4L_FCMD_EA, -1);
if (ret != ERROR_OK) {
LOG_ERROR("Erase All failed");
return ret;
}if (ret != ERROR_OK) { ... }
}if ((first == 0) && ((last + 1) == bank->num_sectors)) { ... } else {
LOG_DEBUG("Erasing sectors %u through %u...\n", first, last);
for (unsigned int i = first; i <= last; i++) {
for (unsigned int j = 0; j < chip->pages_per_sector; j++) {
unsigned int pn = i * chip->pages_per_sector + j;
bool is_erased = false;
ret = sam4l_flash_command(bank->target, SAM4L_FCMD_EP, pn);
if (ret != ERROR_OK) {
LOG_ERROR("Erasing page %u failed", pn);
return ret;
}if (ret != ERROR_OK) { ... }
ret = sam4l_check_page_erased(bank, pn, &is_erased);
if (ret != ERROR_OK)
return ret;
if (!is_erased) {
LOG_DEBUG("Page %u was not erased.", pn);
return ERROR_FAIL;
}if (!is_erased) { ... }
}for (unsigned int j = 0; j < chip->pages_per_sector; j++) { ... }
}for (unsigned int i = first; i <= last; i++) { ... }
}else { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int sam4l_write_page(struct sam4l_info *chip, struct target *target,
uint32_t address, const uint8_t *buf)
{
int res;
LOG_DEBUG("sam4l_write_page address=%08" PRIx32, address);
res = sam4l_flash_command(target, SAM4L_FCMD_CPB, -1);
if (res != ERROR_OK) {
LOG_ERROR("%s: can't clear page buffer", __func__);
return res;
}if (res != ERROR_OK) { ... }
res = target_write_memory(target, address, 4, chip->page_size / 4, buf);
if (res != ERROR_OK) {
LOG_ERROR("%s: %d", __func__, __LINE__);
return res;
}if (res != ERROR_OK) { ... }
/* ... */
res = sam4l_flash_command(target, SAM4L_FCMD_EP, -1);
if (res != ERROR_OK)
return res;
res = sam4l_flash_command(target, SAM4L_FCMD_WP, -1);
return res;
}{ ... }
/* ... */
static int sam4l_write_page_partial(struct sam4l_info *chip,
struct flash_bank *bank, uint32_t address, const uint8_t *buf,
uint32_t page_offset, uint32_t nb)
{
int res;
uint8_t *pg = malloc(chip->page_size);
if (!pg)
return ERROR_FAIL;
LOG_DEBUG("sam4l_write_page_partial address=%08" PRIx32 " nb=%08" PRIx32, address, nb);
assert(page_offset + nb < chip->page_size);
assert((address % chip->page_size) == 0);
res = target_read_memory(bank->target, address, 4,
chip->page_size / 4, pg);
if (res != ERROR_OK) {
free(pg);
return res;
}if (res != ERROR_OK) { ... }
memcpy(pg + (page_offset % chip->page_size), buf, nb);
res = sam4l_write_page(chip, bank->target, address, pg);
free(pg);
return res;
}{ ... }
static int sam4l_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
int res;
uint32_t nb = 0;
struct sam4l_info *chip = (struct sam4l_info *)bank->driver_priv;
LOG_DEBUG("sam4l_write offset=%08" PRIx32 " count=%08" PRIx32, offset, count);
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
if (!chip->probed) {
if (sam4l_probe(bank) != ERROR_OK)
return ERROR_FLASH_BANK_NOT_PROBED;
}if (!chip->probed) { ... }
if (offset % chip->page_size) {
/* ... */
nb = chip->page_size - (offset % chip->page_size);
if (nb > count)
nb = count;
}if (offset % chip->page_size) { ... } else if (count < chip->page_size) {
nb = count;
}else if (count < chip->page_size) { ... }
if (nb > 0) {
res = sam4l_write_page_partial(chip, bank,
(offset / chip->page_size) * chip->page_size + bank->base,
buffer,
offset % chip->page_size, nb);
if (res != ERROR_OK)
return res;
count -= nb;
offset += nb;
}if (nb > 0) { ... }
if (count >= chip->page_size) {
assert(chip->page_size > 0);
int np = count / chip->page_size + ((count % chip->page_size) ? 1 : 0);
for (int i = 0; i < np; i++) {
if (count >= chip->page_size) {
res = sam4l_write_page(chip, bank->target,
bank->base + offset,
buffer + (i * chip->page_size));
offset += chip->page_size;
count -= chip->page_size;
}if (count >= chip->page_size) { ... } else {
res = sam4l_write_page_partial(chip, bank,
bank->base + offset,
buffer + (i * chip->page_size), 0, count);
offset += count;
count = 0;
}else { ... }
if (res != ERROR_OK)
return res;
}for (int i = 0; i < np; i++) { ... }
}if (count >= chip->page_size) { ... }
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(sam4l_handle_reset_deassert)
{
struct target *target = get_current_target(CMD_CTX);
int retval = ERROR_OK;
enum reset_types jtag_reset_config = jtag_get_reset_config();
/* ... */
if (!target_was_examined(target))
target_examine_one(target);
target_poll(target);
/* ... */
if (target->reset_halt && (jtag_reset_config & RESET_HAS_SRST)) {
retval = target_write_u32(target, DCB_DHCSR, DBGKEY | C_HALT | C_DEBUGEN);
if (retval == ERROR_OK)
retval = target_write_u32(target, DCB_DEMCR,
TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
}if (target->reset_halt && (jtag_reset_config & RESET_HAS_SRST)) { ... }
int retval2 = target_write_u32(target, SMAP_SCR, SMAP_SCR_HCR);
if (retval2 != ERROR_OK)
return retval2;
return retval;
}{ ... }
static const struct command_registration at91sam4l_exec_command_handlers[] = {
{
.name = "smap_reset_deassert",
.handler = sam4l_handle_reset_deassert,
.mode = COMMAND_EXEC,
.help = "deassert internal reset held by SMAP",
.usage = "",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration at91sam4l_command_handlers[] = {
{
.name = "at91sam4l",
.mode = COMMAND_ANY,
.help = "at91sam4l flash command group",
.usage = "",
.chain = at91sam4l_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
const struct flash_driver at91sam4l_flash = {
.name = "at91sam4l",
.commands = at91sam4l_command_handlers,
.flash_bank_command = sam4l_flash_bank_command,
.erase = sam4l_erase,
.protect = sam4l_protect,
.write = sam4l_write,
.read = default_flash_read,
.probe = sam4l_probe,
.auto_probe = sam4l_probe,
.erase_check = default_flash_blank_check,
.protect_check = sam4l_protect_check,
.free_driver_priv = default_flash_free_driver_priv,
...};