OpenOCD
atsame5.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2017 by Tomas Vanek *
5  * vanekt@fbl.cz *
6  * *
7  * Based on at91samd.c *
8  * Copyright (C) 2013 by Andrey Yurovsky *
9  * Andrey Yurovsky <yurovsky@gmail.com> *
10  ***************************************************************************/
11 
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15 
16 #include "imp.h"
17 #include "helper/binarybuffer.h"
18 
19 #include <helper/time_support.h>
20 #include <jtag/jtag.h>
21 #include <target/cortex_m.h>
22 
23 /* A note to prefixing.
24  * Definitions and functions inherited from at91samd.c without
25  * any change retained the original prefix samd_ so they eventually
26  * may go to samd_common.h and .c
27  * As currently there are only 3 short functions identical with
28  * the original source, no common file was created. */
29 
30 #define SAME5_PAGES_PER_BLOCK 16
31 #define SAME5_NUM_PROT_BLOCKS 32
32 #define SAMD_PAGE_SIZE_MAX 1024
33 
34 #define SAMD_FLASH 0x00000000 /* physical Flash memory */
35 #define SAMD_USER_ROW 0x00804000 /* User Row of Flash */
36 
37 #define SAME5_PAC 0x40000000 /* Peripheral Access Control */
38 
39 #define SAMD_DSU 0x41002000 /* Device Service Unit */
40 #define SAMD_NVMCTRL 0x41004000 /* Non-volatile memory controller */
41 
42 #define SAMD_DSU_STATUSA 1 /* DSU status register */
43 #define SAMD_DSU_DID 0x18 /* Device ID register */
44 #define SAMD_DSU_CTRL_EXT 0x100 /* CTRL register, external access */
45 
46 #define SAME5_NVMCTRL_CTRLA 0x00 /* NVM control A register */
47 #define SAME5_NVMCTRL_CTRLB 0x04 /* NVM control B register */
48 #define SAMD_NVMCTRL_PARAM 0x08 /* NVM parameters register */
49 #define SAME5_NVMCTRL_INTFLAG 0x10 /* NVM interrupt flag register */
50 #define SAME5_NVMCTRL_STATUS 0x12 /* NVM status register */
51 #define SAME5_NVMCTRL_ADDR 0x14 /* NVM address register */
52 #define SAME5_NVMCTRL_LOCK 0x18 /* NVM Lock section register */
53 
54 #define SAMD_CMDEX_KEY 0xA5UL
55 #define SAMD_NVM_CMD(n) ((SAMD_CMDEX_KEY << 8) | (n & 0x7F))
56 
57 /* NVMCTRL commands. */
58 #define SAME5_NVM_CMD_EP 0x00 /* Erase Page (User Page only) */
59 #define SAME5_NVM_CMD_EB 0x01 /* Erase Block */
60 #define SAME5_NVM_CMD_WP 0x03 /* Write Page */
61 #define SAME5_NVM_CMD_WQW 0x04 /* Write Quad Word */
62 #define SAME5_NVM_CMD_LR 0x11 /* Lock Region */
63 #define SAME5_NVM_CMD_UR 0x12 /* Unlock Region */
64 #define SAME5_NVM_CMD_PBC 0x15 /* Page Buffer Clear */
65 #define SAME5_NVM_CMD_SSB 0x16 /* Set Security Bit */
66 
67 /* NVMCTRL bits */
68 #define SAME5_NVMCTRL_CTRLA_WMODE_MASK 0x30
69 
70 #define SAME5_NVMCTRL_INTFLAG_DONE (1 << 0)
71 #define SAME5_NVMCTRL_INTFLAG_ADDRE (1 << 1)
72 #define SAME5_NVMCTRL_INTFLAG_PROGE (1 << 2)
73 #define SAME5_NVMCTRL_INTFLAG_LOCKE (1 << 3)
74 #define SAME5_NVMCTRL_INTFLAG_ECCSE (1 << 4)
75 #define SAME5_NVMCTRL_INTFLAG_ECCDE (1 << 5)
76 #define SAME5_NVMCTRL_INTFLAG_NVME (1 << 6)
77 
78 
79 /* Known identifiers */
80 #define SAMD_PROCESSOR_M0 0x01
81 #define SAMD_PROCESSOR_M4 0x06
82 #define SAMD_FAMILY_D 0x00
83 #define SAMD_FAMILY_E 0x03
84 #define SAMD_SERIES_51 0x06
85 #define SAME_SERIES_51 0x01
86 #define SAME_SERIES_53 0x03
87 #define SAME_SERIES_54 0x04
88 #define PIC32CXSG_SERIES_41 0x07
89 #define PIC32CXSG_SERIES_60 0x00
90 #define PIC32CXSG_SERIES_61 0x02
91 
92 /* Device ID macros */
93 #define SAMD_GET_PROCESSOR(id) (id >> 28)
94 #define SAMD_GET_FAMILY(id) (((id >> 23) & 0x1F))
95 #define SAMD_GET_SERIES(id) (((id >> 16) & 0x3F))
96 #define SAMD_GET_DEVSEL(id) (id & 0xFF)
97 
98 /* Bits to mask user row */
99 #define NVMUSERROW_SAM_E5_D5_MASK 0x7FFF00FF3C007FFFULL
100 
101 struct samd_part {
102  uint8_t id;
103  const char *name;
104  uint32_t flash_kb;
105  uint32_t ram_kb;
106 };
107 
108 /* See SAM D5x/E5x Family Silicon Errata and Data Sheet Clarification
109  * DS80000748K */
110 /* Known SAMD51 parts. */
111 static const struct samd_part samd51_parts[] = {
112  { 0x00, "SAMD51P20A", 1024, 256 },
113  { 0x01, "SAMD51P19A", 512, 192 },
114  { 0x02, "SAMD51N20A", 1024, 256 },
115  { 0x03, "SAMD51N19A", 512, 192 },
116  { 0x04, "SAMD51J20A", 1024, 256 },
117  { 0x05, "SAMD51J19A", 512, 192 },
118  { 0x06, "SAMD51J18A", 256, 128 },
119  { 0x07, "SAMD51G19A", 512, 192 },
120  { 0x08, "SAMD51G18A", 256, 128 },
121 };
122 
123 /* Known SAME51 parts. */
124 static const struct samd_part same51_parts[] = {
125  { 0x00, "SAME51N20A", 1024, 256 },
126  { 0x01, "SAME51N19A", 512, 192 },
127  { 0x02, "SAME51J19A", 512, 192 },
128  { 0x03, "SAME51J18A", 256, 128 },
129  { 0x04, "SAME51J20A", 1024, 256 },
130  { 0x05, "SAME51G19A", 512, 192 }, /* New in rev D */
131  { 0x06, "SAME51G18A", 256, 128 }, /* New in rev D */
132 };
133 
134 /* Known SAME53 parts. */
135 static const struct samd_part same53_parts[] = {
136  { 0x02, "SAME53N20A", 1024, 256 },
137  { 0x03, "SAME53N19A", 512, 192 },
138  { 0x04, "SAME53J20A", 1024, 256 },
139  { 0x05, "SAME53J19A", 512, 192 },
140  { 0x06, "SAME53J18A", 256, 128 },
141  { 0x55, "LAN9255/ZMX020", 1024, 256 },
142  { 0x56, "LAN9255/ZMX019", 512, 192 },
143  { 0x57, "LAN9255/ZMX018", 256, 128 },
144 };
145 
146 /* Known SAME54 parts. */
147 static const struct samd_part same54_parts[] = {
148  { 0x00, "SAME54P20A", 1024, 256 },
149  { 0x01, "SAME54P19A", 512, 192 },
150  { 0x02, "SAME54N20A", 1024, 256 },
151  { 0x03, "SAME54N19A", 512, 192 },
152 };
153 
154 /* See PIC32CX SG41/SG60/SG61 Family Silicon Errata and Datasheet Clarifications
155  * DS80000985G */
156 /* Known PIC32CX-SG41 parts. */
157 static const struct samd_part pic32cxsg41_parts[] = {
158  { 0x00, "PIC32CX1025SG41128", 1024, 256 },
159  { 0x01, "PIC32CX1025SG41100", 1024, 256 },
160  { 0x02, "PIC32CX1025SG41064", 1024, 256 },
161 };
162 
163 /* Known PIC32CX-SG60 parts. */
164 static const struct samd_part pic32cxsg60_parts[] = {
165  { 0x00, "PIC32CX1025SG60128", 1024, 256 },
166  { 0x01, "PIC32CX1025SG60100", 1024, 256 },
167 };
168 
169 /* Known PIC32CX-SG61 parts. */
170 static const struct samd_part pic32cxsg61_parts[] = {
171  { 0x00, "PIC32CX1025SG61128", 1024, 256 },
172  { 0x01, "PIC32CX1025SG61100", 1024, 256 },
173 };
174 
175 /* Each family of parts contains a parts table in the DEVSEL field of DID. The
176  * processor ID, family ID, and series ID are used to determine which exact
177  * family this is and then we can use the corresponding table. */
178 struct samd_family {
179  uint8_t processor;
180  uint8_t family;
181  uint8_t series;
182  const struct samd_part *parts;
183  size_t num_parts;
184 };
185 
186 /* Known SAMD families */
187 static const struct samd_family samd_families[] = {
202 };
203 
204 struct samd_info {
205  const struct samd_params *par;
206  uint32_t page_size;
207  int num_pages;
208  int sector_size;
209  int prot_block_size;
210 
211  bool probed;
212  struct target *target;
213 };
214 
215 
221 static const struct samd_family *samd_find_family(uint32_t id)
222 {
223  uint8_t processor = SAMD_GET_PROCESSOR(id);
224  uint8_t family = SAMD_GET_FAMILY(id);
225  uint8_t series = SAMD_GET_SERIES(id);
226 
227  for (unsigned int i = 0; i < ARRAY_SIZE(samd_families); i++) {
228  if (samd_families[i].processor == processor &&
229  samd_families[i].series == series &&
231  return &samd_families[i];
232  }
233 
234  return NULL;
235 }
236 
242 static const struct samd_part *samd_find_part(uint32_t id)
243 {
244  uint8_t devsel = SAMD_GET_DEVSEL(id);
245  const struct samd_family *family = samd_find_family(id);
246  if (!family)
247  return NULL;
248 
249  for (unsigned int i = 0; i < family->num_parts; i++) {
250  if (family->parts[i].id == devsel)
251  return &family->parts[i];
252  }
253 
254  return NULL;
255 }
256 
258 {
259  int res;
260  uint32_t lock;
261 
262  res = target_read_u32(bank->target,
264  if (res != ERROR_OK)
265  return res;
266 
267  /* Lock bits are active-low */
268  for (unsigned int prot_block = 0; prot_block < bank->num_prot_blocks; prot_block++)
269  bank->prot_blocks[prot_block].is_protected = !(lock & (1u<<prot_block));
270 
271  return ERROR_OK;
272 }
273 
275  uint32_t *sizep, int *nump)
276 {
277  int res;
278  uint32_t param;
279 
281  if (res == ERROR_OK) {
282  /* The PSZ field (bits 18:16) indicate the page size bytes as 2^(3+n)
283  * so 0 is 8KB and 7 is 1024KB. */
284  if (sizep)
285  *sizep = (8 << ((param >> 16) & 0x7));
286  /* The NVMP field (bits 15:0) indicates the total number of pages */
287  if (nump)
288  *nump = param & 0xFFFF;
289  } else {
290  LOG_ERROR("Couldn't read NVM Parameters register");
291  }
292 
293  return res;
294 }
295 
296 static int same5_probe(struct flash_bank *bank)
297 {
298  uint32_t id;
299  int res;
300  struct samd_info *chip = (struct samd_info *)bank->driver_priv;
301  const struct samd_part *part;
302 
303  if (chip->probed)
304  return ERROR_OK;
305 
306  res = target_read_u32(bank->target, SAMD_DSU + SAMD_DSU_DID, &id);
307  if (res != ERROR_OK) {
308  LOG_ERROR("Couldn't read Device ID register");
309  return res;
310  }
311 
312  part = samd_find_part(id);
313  if (!part) {
314  LOG_ERROR("Couldn't find part corresponding to DID %08" PRIx32, id);
315  return ERROR_FAIL;
316  }
317 
318  bank->size = part->flash_kb * 1024;
319 
320  res = samd_get_flash_page_info(bank->target, &chip->page_size,
321  &chip->num_pages);
322  if (res != ERROR_OK) {
323  LOG_ERROR("Couldn't determine Flash page size");
324  return res;
325  }
326 
327  /* Sanity check: the total flash size in the DSU should match the page size
328  * multiplied by the number of pages. */
329  if (bank->size != chip->num_pages * chip->page_size) {
330  LOG_WARNING("SAM: bank size doesn't match NVM parameters. "
331  "Identified %" PRIu32 "KB Flash but NVMCTRL reports %u %" PRIu32 "B pages",
332  part->flash_kb, chip->num_pages, chip->page_size);
333  }
334 
335  /* Erase granularity = 1 block = 16 pages */
337 
338  /* Allocate the sector table */
339  bank->num_sectors = chip->num_pages / SAME5_PAGES_PER_BLOCK;
340  bank->sectors = alloc_block_array(0, chip->sector_size, bank->num_sectors);
341  if (!bank->sectors)
342  return ERROR_FAIL;
343 
344  /* 16 protection blocks per device */
346 
347  /* Allocate the table of protection blocks */
348  bank->num_prot_blocks = SAME5_NUM_PROT_BLOCKS;
349  bank->prot_blocks = alloc_block_array(0, chip->prot_block_size, bank->num_prot_blocks);
350  if (!bank->prot_blocks)
351  return ERROR_FAIL;
352 
354 
355  /* Done */
356  chip->probed = true;
357 
358  LOG_INFO("SAM MCU: %s (%" PRIu32 "KB Flash, %" PRIu32 "KB RAM)", part->name,
359  part->flash_kb, part->ram_kb);
360 
361  return ERROR_OK;
362 }
363 
365 {
366  int ret, ret2;
367  /* Table 54-40 lists the maximum erase block time as 200 ms.
368  * Include some margin.
369  */
370  int timeout_ms = 200 * 5;
371  int64_t ts_start = timeval_ms();
372  uint16_t intflag;
373 
374  do {
375  ret = target_read_u16(target,
376  SAMD_NVMCTRL + SAME5_NVMCTRL_INTFLAG, &intflag);
377  if (ret != ERROR_OK) {
378  LOG_ERROR("SAM: error reading the NVMCTRL_INTFLAG register");
379  return ret;
380  }
381  if (intflag & SAME5_NVMCTRL_INTFLAG_DONE)
382  break;
383  keep_alive();
384  } while (timeval_ms() - ts_start < timeout_ms);
385 
386  if (!(intflag & SAME5_NVMCTRL_INTFLAG_DONE)) {
387  LOG_ERROR("SAM: NVM programming timed out");
389  }
390 #if 0
391  if (intflag & SAME5_NVMCTRL_INTFLAG_ECCSE)
392  LOG_ERROR("SAM: ECC Single Error");
393 
394  if (intflag & SAME5_NVMCTRL_INTFLAG_ECCDE) {
395  LOG_ERROR("SAM: ECC Double Error");
397  }
398 #endif
399  if (intflag & SAME5_NVMCTRL_INTFLAG_ADDRE) {
400  LOG_ERROR("SAM: Addr Error");
402  }
403 
404  if (intflag & SAME5_NVMCTRL_INTFLAG_NVME) {
405  LOG_ERROR("SAM: NVM Error");
407  }
408 
409  if (intflag & SAME5_NVMCTRL_INTFLAG_LOCKE) {
410  LOG_ERROR("SAM: NVM lock error");
411  ret = ERROR_FLASH_PROTECTED;
412  }
413 
414  if (intflag & SAME5_NVMCTRL_INTFLAG_PROGE) {
415  LOG_ERROR("SAM: NVM programming error");
417  }
418 
419  /* Clear the error conditions by writing a one to them */
420  ret2 = target_write_u16(target,
422  if (ret2 != ERROR_OK)
423  LOG_ERROR("Can't clear NVM error conditions");
424 
425  return ret;
426 }
427 
428 static int same5_issue_nvmctrl_command(struct target *target, uint16_t cmd)
429 {
430  int res;
431 
432  if (target->state != TARGET_HALTED) {
433  LOG_ERROR("Target not halted");
435  }
436 
437  /* Issue the NVM command */
438  /* 32-bit write is used to ensure atomic operation on ST-Link */
439  res = target_write_u32(target,
441  if (res != ERROR_OK)
442  return res;
443 
444  /* Check to see if the NVM command resulted in an error condition. */
446 }
447 
454 static int same5_erase_block(struct target *target, uint32_t address)
455 {
456  int res;
457 
458  /* Set an address contained in the block to be erased */
459  res = target_write_u32(target,
460  SAMD_NVMCTRL + SAME5_NVMCTRL_ADDR, address);
461 
462  /* Issue the Erase Block command. */
463  if (res == ERROR_OK)
466 
467  if (res != ERROR_OK) {
468  LOG_ERROR("Failed to erase block containing %08" PRIx32, address);
469  return ERROR_FAIL;
470  }
471 
472  return ERROR_OK;
473 }
474 
475 
477 {
478  int res;
479  uint32_t nvm_ctrla;
480 
481  if (target->state != TARGET_HALTED) {
482  LOG_ERROR("Target not halted");
484  }
485 
486  /* Check if manual write mode is set */
488  if (res != ERROR_OK)
489  return res;
490 
491  if (nvm_ctrla & SAME5_NVMCTRL_CTRLA_WMODE_MASK) {
492  LOG_ERROR("The flash controller must be in manual write mode. Issue 'reset init' and retry.");
493  return ERROR_FAIL;
494  }
495 
496  return res;
497 }
498 
499 
513  const uint8_t *data, const uint8_t *mask,
514  uint32_t offset, uint32_t count)
515 {
516  int res;
517 
518  /* Retrieve the MCU's flash page size, in bytes. */
519  uint32_t page_size;
521  if (res != ERROR_OK) {
522  LOG_ERROR("Couldn't determine Flash page size");
523  return res;
524  }
525 
526  /* Make sure the size is sane. */
527  assert(page_size <= SAMD_PAGE_SIZE_MAX &&
528  page_size >= offset + count);
529 
530  uint8_t buf[SAMD_PAGE_SIZE_MAX];
531  /* Read the user row (comprising one page) by words. */
532  res = target_read_memory(target, SAMD_USER_ROW, 4, page_size / 4, buf);
533  if (res != ERROR_OK)
534  return res;
535 
536  /* Modify buffer and check if really changed */
537  bool changed = false;
538  uint32_t i;
539  for (i = 0; i < count; i++) {
540  uint8_t old_b = buf[offset+i];
541  uint8_t new_b = (old_b & ~mask[i]) | (data[i] & mask[i]);
542  buf[offset+i] = new_b;
543  if (old_b != new_b)
544  changed = true;
545  }
546 
547  if (!changed)
548  return ERROR_OK;
549 
551  if (res != ERROR_OK)
552  return res;
553 
555  if (res != ERROR_OK) {
556  LOG_ERROR("Couldn't erase user row");
557  return res;
558  }
559 
560  /* Write the page buffer back out to the target using Write Quad Word */
561  for (i = 0; i < page_size; i += 4 * 4) {
562  res = target_write_memory(target, SAMD_USER_ROW + i, 4, 4, buf + i);
563  if (res != ERROR_OK)
564  return res;
565 
566  /* Trigger flash write */
568  if (res != ERROR_OK)
569  return res;
570  }
571 
572  return res;
573 }
574 
583 static int same5_modify_user_row(struct target *target, uint32_t value,
584  uint8_t startb, uint8_t endb)
585 {
586  uint8_t buf_val[8] = { 0 };
587  uint8_t buf_mask[8] = { 0 };
588 
589  assert(startb <= endb && endb < 64);
590  buf_set_u32(buf_val, startb, endb + 1 - startb, value);
591  buf_set_u32(buf_mask, startb, endb + 1 - startb, 0xffffffff);
592 
594  buf_val, buf_mask, 0, 8);
595 }
596 
597 static int same5_protect(struct flash_bank *bank, int set, unsigned int first,
598  unsigned int last)
599 {
600  int res = ERROR_OK;
601 
602  /* We can issue lock/unlock region commands with the target running but
603  * the settings won't persist unless we're able to modify the LOCK regions
604  * and that requires the target to be halted. */
605  if (bank->target->state != TARGET_HALTED) {
606  LOG_ERROR("Target not halted");
608  }
609 
610  for (unsigned int prot_block = first; prot_block <= last; prot_block++) {
611  if (set != bank->prot_blocks[prot_block].is_protected) {
612  /* Load an address that is within this protection block (we use offset 0) */
613  res = target_write_u32(bank->target,
615  bank->prot_blocks[prot_block].offset);
616  if (res != ERROR_OK)
617  goto exit;
618 
619  /* Tell the controller to lock that block */
620  res = same5_issue_nvmctrl_command(bank->target,
622  if (res != ERROR_OK)
623  goto exit;
624  }
625  }
626 
627  /* We've now applied our changes, however they will be undone by the next
628  * reset unless we also apply them to the LOCK bits in the User Page.
629  * A '1' means unlocked and a '0' means locked. */
630  const uint8_t lock[4] = { 0, 0, 0, 0 };
631  const uint8_t unlock[4] = { 0xff, 0xff, 0xff, 0xff };
632  uint8_t mask[4] = { 0, 0, 0, 0 };
633 
634  buf_set_u32(mask, first, last + 1 - first, 0xffffffff);
635 
636  res = same5_modify_user_row_masked(bank->target,
637  set ? lock : unlock, mask, 8, 4);
638  if (res != ERROR_OK)
639  LOG_WARNING("SAM: protect settings were not made persistent!");
640 
641  res = ERROR_OK;
642 
643 exit:
645 
646  return res;
647 }
648 
649 static int same5_erase(struct flash_bank *bank, unsigned int first,
650  unsigned int last)
651 {
652  int res;
653  struct samd_info *chip = (struct samd_info *)bank->driver_priv;
654 
655  if (bank->target->state != TARGET_HALTED) {
656  LOG_ERROR("Target not halted");
657 
659  }
660 
661  if (!chip->probed)
663 
664  /* For each sector to be erased */
665  for (unsigned int s = first; s <= last; s++) {
666  res = same5_erase_block(bank->target, bank->sectors[s].offset);
667  if (res != ERROR_OK) {
668  LOG_ERROR("SAM: failed to erase sector %d at 0x%08" PRIx32, s, bank->sectors[s].offset);
669  return res;
670  }
671  }
672 
673  return ERROR_OK;
674 }
675 
676 
677 static int same5_write(struct flash_bank *bank, const uint8_t *buffer,
678  uint32_t offset, uint32_t count)
679 {
680  int res;
681  uint32_t address;
682  uint32_t pg_offset;
683  uint32_t nb;
684  uint32_t nw;
685  struct samd_info *chip = (struct samd_info *)bank->driver_priv;
686  uint8_t *pb = NULL;
687 
688  res = same5_pre_write_check(bank->target);
689  if (res != ERROR_OK)
690  return res;
691 
692  if (!chip->probed)
694 
696  if (res != ERROR_OK) {
697  LOG_ERROR("%s: %d", __func__, __LINE__);
698  return res;
699  }
700 
701  while (count) {
702  nb = chip->page_size - offset % chip->page_size;
703  if (count < nb)
704  nb = count;
705 
706  address = bank->base + offset;
707  pg_offset = offset % chip->page_size;
708 
709  if (offset % 4 || (offset + nb) % 4) {
710  /* Either start or end of write is not word aligned */
711  if (!pb) {
712  pb = malloc(chip->page_size);
713  if (!pb)
714  return ERROR_FAIL;
715  }
716 
717  /* Set temporary page buffer to 0xff and overwrite the relevant part */
718  memset(pb, 0xff, chip->page_size);
719  memcpy(pb + pg_offset, buffer, nb);
720 
721  /* Align start address to a word boundary */
722  address -= offset % 4;
723  pg_offset -= offset % 4;
724  assert(pg_offset % 4 == 0);
725 
726  /* Extend length to whole words */
727  nw = (nb + offset % 4 + 3) / 4;
728  assert(pg_offset + 4 * nw <= chip->page_size);
729 
730  /* Now we have original data extended by 0xff bytes
731  * to the nearest word boundary on both start and end */
732  res = target_write_memory(bank->target, address, 4, nw, pb + pg_offset);
733  } else {
734  assert(nb % 4 == 0);
735  nw = nb / 4;
736  assert(pg_offset + 4 * nw <= chip->page_size);
737 
738  /* Word aligned data, use direct write from buffer */
739  res = target_write_memory(bank->target, address, 4, nw, buffer);
740  }
741  if (res != ERROR_OK) {
742  LOG_ERROR("%s: %d", __func__, __LINE__);
743  goto free_pb;
744  }
745 
747  if (res != ERROR_OK) {
748  LOG_ERROR("%s: write failed at address 0x%08" PRIx32, __func__, address);
749  goto free_pb;
750  }
751 
752  /* We're done with the page contents */
753  count -= nb;
754  offset += nb;
755  buffer += nb;
756  }
757 
758 free_pb:
759  free(pb);
760  return res;
761 }
762 
763 
764 FLASH_BANK_COMMAND_HANDLER(same5_flash_bank_command)
765 {
766  if (bank->base != SAMD_FLASH) {
767  LOG_ERROR("Address " TARGET_ADDR_FMT " invalid bank address (try "
768  "0x%08x[same5] )", bank->base, SAMD_FLASH);
769  return ERROR_FAIL;
770  }
771 
772  struct samd_info *chip;
773  chip = calloc(1, sizeof(*chip));
774  if (!chip) {
775  LOG_ERROR("No memory for flash bank chip info");
776  return ERROR_FAIL;
777  }
778 
779  chip->target = bank->target;
780  chip->probed = false;
781 
782  bank->driver_priv = chip;
783 
784  return ERROR_OK;
785 }
786 
787 
788 COMMAND_HANDLER(same5_handle_chip_erase_command)
789 {
791  if (!target)
792  return ERROR_FAIL;
793 
794  /* Enable access to the DSU by disabling the write protect bit */
795  target_write_u32(target, SAME5_PAC, (1<<16) | (1<<5) | (1<<1));
796  /* intentionally without error checking - not accessible on secured chip */
797 
798  /* Tell the DSU to perform a full chip erase. It takes about 240ms to
799  * perform the erase. */
800  int res = target_write_u8(target, SAMD_DSU + SAMD_DSU_CTRL_EXT, (1<<4));
801  if (res == ERROR_OK)
802  command_print(CMD, "chip erase started");
803  else
804  command_print(CMD, "write to DSU CTRL failed");
805 
806  return res;
807 }
808 
809 
810 COMMAND_HANDLER(same5_handle_userpage_command)
811 {
812  int res = ERROR_OK;
814  if (!target)
815  return ERROR_FAIL;
816 
817  if (CMD_ARGC > 2) {
818  command_print(CMD, "Too much Arguments given.");
820  }
821 
822  if (CMD_ARGC >= 1) {
823  uint64_t value, mask = NVMUSERROW_SAM_E5_D5_MASK;
824  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], value);
825 
826  if (CMD_ARGC == 2) {
827  uint64_t mask_temp;
828  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[1], mask_temp);
829  mask &= mask_temp;
830  }
831 
832  uint8_t val_buf[8], mask_buf[8];
833  target_buffer_set_u64(target, val_buf, value);
834  target_buffer_set_u64(target, mask_buf, mask);
835 
837  val_buf, mask_buf, 0, sizeof(val_buf));
838  }
839 
840  uint8_t buffer[8];
841  int res2 = target_read_memory(target, SAMD_USER_ROW, 4, 2, buffer);
842  if (res2 == ERROR_OK) {
843  uint64_t value = target_buffer_get_u64(target, buffer);
844  command_print(CMD, "USER PAGE: 0x%016"PRIX64, value);
845  } else {
846  LOG_ERROR("USER PAGE could not be read.");
847  }
848 
849  if (CMD_ARGC >= 1)
850  return res;
851  else
852  return res2;
853 }
854 
855 
856 COMMAND_HANDLER(same5_handle_bootloader_command)
857 {
858  int res = ERROR_OK;
860  if (!target)
861  return ERROR_FAIL;
862 
863  if (CMD_ARGC >= 1) {
864  unsigned long size;
865 
866  COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[0], size);
867  uint32_t code = (size + 8191) / 8192;
868  if (code > 15) {
869  command_print(CMD, "Invalid bootloader size. Please "
870  "see datasheet for a list valid sizes.");
872  }
873 
874  res = same5_modify_user_row(target, 15 - code, 26, 29);
875  }
876 
877  uint32_t val;
878  int res2 = target_read_u32(target, SAMD_USER_ROW, &val);
879  if (res2 == ERROR_OK) {
880  uint32_t code = (val >> 26) & 0xf; /* grab size code */
881  uint32_t size = (15 - code) * 8192;
882  command_print(CMD, "Bootloader protected in the first %"
883  PRIu32 " bytes", size);
884  }
885 
886  if (CMD_ARGC >= 1)
887  return res;
888  else
889  return res2;
890 }
891 
892 
893 COMMAND_HANDLER(samd_handle_reset_deassert)
894 {
896  int res = ERROR_OK;
898  if (!target)
899  return ERROR_FAIL;
900 
901  /* If the target has been unresponsive before, try to re-establish
902  * communication now - CPU is held in reset by DSU, DAP is working */
906 
907  /* In case of sysresetreq, debug retains state set in cortex_m_assert_reset()
908  * so we just release reset held by DSU
909  *
910  * n_RESET (srst) clears the DP, so reenable debug and set vector catch here
911  *
912  * After vectreset DSU release is not needed however makes no harm
913  */
916  if (res == ERROR_OK)
919  /* do not return on error here, releasing DSU reset is more important */
920  }
921 
922  /* clear CPU Reset Phase Extension bit */
923  int res2 = target_write_u8(target, SAMD_DSU + SAMD_DSU_STATUSA, (1<<1));
924  if (res2 != ERROR_OK)
925  return res2;
926 
927  return res;
928 }
929 
930 static const struct command_registration same5_exec_command_handlers[] = {
931  {
932  .name = "dsu_reset_deassert",
933  .usage = "",
934  .handler = samd_handle_reset_deassert,
935  .mode = COMMAND_EXEC,
936  .help = "Deassert internal reset held by DSU."
937  },
938  {
939  .name = "chip-erase",
940  .usage = "",
941  .handler = same5_handle_chip_erase_command,
942  .mode = COMMAND_EXEC,
943  .help = "Erase the entire Flash by using the Chip-"
944  "Erase feature in the Device Service Unit (DSU).",
945  },
946  {
947  .name = "bootloader",
948  .usage = "[size_in_bytes]",
949  .handler = same5_handle_bootloader_command,
950  .mode = COMMAND_EXEC,
951  .help = "Show or set the bootloader protection size, stored in the User Row. "
952  "Changes are stored immediately but take affect after the MCU is "
953  "reset.",
954  },
955  {
956  .name = "userpage",
957  .usage = "[value] [mask]",
958  .handler = same5_handle_userpage_command,
959  .mode = COMMAND_EXEC,
960  .help = "Show or set the first 64-bit part of user page "
961  "located at address 0x804000. Use the optional mask argument "
962  "to prevent changes at positions where the bitvalue is zero. "
963  "For security reasons the reserved-bits are masked out "
964  "in background and therefore cannot be changed.",
965  },
967 };
968 
969 static const struct command_registration same5_command_handlers[] = {
970  {
971  .name = "atsame5",
972  .mode = COMMAND_ANY,
973  .help = "atsame5 flash command group",
974  .usage = "",
976  },
978 };
979 
980 const struct flash_driver atsame5_flash = {
981  .name = "atsame5",
982  .commands = same5_command_handlers,
983  .flash_bank_command = same5_flash_bank_command,
984  .erase = same5_erase,
985  .protect = same5_protect,
986  .write = same5_write,
987  .read = default_flash_read,
988  .probe = same5_probe,
989  .auto_probe = same5_probe,
990  .erase_check = default_flash_blank_check,
991  .protect_check = same5_protect_check,
992  .free_driver_priv = default_flash_free_driver_priv,
993 };
#define SAMD_DSU_STATUSA
Definition: atsame5.c:42
COMMAND_HANDLER(same5_handle_chip_erase_command)
Definition: atsame5.c:788
#define SAME5_NVM_CMD_EP
Definition: atsame5.c:58
static int same5_wait_and_check_error(struct target *target)
Definition: atsame5.c:364
#define SAMD_GET_SERIES(id)
Definition: atsame5.c:95
static const struct samd_part pic32cxsg60_parts[]
Definition: atsame5.c:164
static const struct samd_part same51_parts[]
Definition: atsame5.c:124
#define SAME5_NUM_PROT_BLOCKS
Definition: atsame5.c:31
static int same5_pre_write_check(struct target *target)
Definition: atsame5.c:476
#define SAMD_PROCESSOR_M4
Definition: atsame5.c:81
static int same5_issue_nvmctrl_command(struct target *target, uint16_t cmd)
Definition: atsame5.c:428
#define SAME5_NVMCTRL_INTFLAG_ADDRE
Definition: atsame5.c:71
#define SAMD_FAMILY_E
Definition: atsame5.c:83
static int samd_get_flash_page_info(struct target *target, uint32_t *sizep, int *nump)
Definition: atsame5.c:274
#define SAMD_FAMILY_D
Definition: atsame5.c:82
static const struct samd_family * samd_find_family(uint32_t id)
Gives the family structure to specific device id.
Definition: atsame5.c:221
static const struct samd_part pic32cxsg41_parts[]
Definition: atsame5.c:157
#define SAME5_NVM_CMD_LR
Definition: atsame5.c:62
#define SAME5_NVMCTRL_ADDR
Definition: atsame5.c:51
static int same5_protect_check(struct flash_bank *bank)
Definition: atsame5.c:257
#define SAMD_GET_DEVSEL(id)
Definition: atsame5.c:96
#define SAMD_GET_FAMILY(id)
Definition: atsame5.c:94
#define SAME5_NVM_CMD_EB
Definition: atsame5.c:59
#define SAME5_PAGES_PER_BLOCK
Definition: atsame5.c:30
#define SAME_SERIES_53
Definition: atsame5.c:86
#define SAME5_NVMCTRL_LOCK
Definition: atsame5.c:52
static int same5_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: atsame5.c:597
static const struct samd_part pic32cxsg61_parts[]
Definition: atsame5.c:170
#define NVMUSERROW_SAM_E5_D5_MASK
Definition: atsame5.c:99
static int same5_modify_user_row_masked(struct target *target, const uint8_t *data, const uint8_t *mask, uint32_t offset, uint32_t count)
Modify the contents of the User Row in Flash.
Definition: atsame5.c:512
#define SAMD_GET_PROCESSOR(id)
Definition: atsame5.c:93
static const struct samd_part same53_parts[]
Definition: atsame5.c:135
static int same5_probe(struct flash_bank *bank)
Definition: atsame5.c:296
#define PIC32CXSG_SERIES_41
Definition: atsame5.c:88
#define SAME5_NVM_CMD_UR
Definition: atsame5.c:63
static int same5_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: atsame5.c:649
#define SAMD_DSU_CTRL_EXT
Definition: atsame5.c:44
#define PIC32CXSG_SERIES_61
Definition: atsame5.c:90
#define SAME5_NVM_CMD_WP
Definition: atsame5.c:60
#define SAMD_DSU
Definition: atsame5.c:39
#define SAMD_NVMCTRL_PARAM
Definition: atsame5.c:48
#define SAME5_NVM_CMD_PBC
Definition: atsame5.c:64
#define SAMD_FLASH
Definition: atsame5.c:34
#define SAME5_NVMCTRL_INTFLAG_ECCSE
Definition: atsame5.c:74
#define SAMD_PAGE_SIZE_MAX
Definition: atsame5.c:32
#define SAMD_DSU_DID
Definition: atsame5.c:43
#define SAME5_NVMCTRL_INTFLAG_LOCKE
Definition: atsame5.c:73
#define SAME5_NVMCTRL_INTFLAG_PROGE
Definition: atsame5.c:72
FLASH_BANK_COMMAND_HANDLER(same5_flash_bank_command)
Definition: atsame5.c:764
static const struct command_registration same5_command_handlers[]
Definition: atsame5.c:969
#define SAME5_NVM_CMD_WQW
Definition: atsame5.c:61
static const struct samd_part same54_parts[]
Definition: atsame5.c:147
#define SAMD_NVMCTRL
Definition: atsame5.c:40
#define SAMD_NVM_CMD(n)
Definition: atsame5.c:55
#define SAME5_PAC
Definition: atsame5.c:37
#define SAME5_NVMCTRL_CTRLA
Definition: atsame5.c:46
#define SAMD_USER_ROW
Definition: atsame5.c:35
const struct flash_driver atsame5_flash
Definition: atsame5.c:980
#define SAME5_NVMCTRL_INTFLAG
Definition: atsame5.c:49
#define SAME5_NVMCTRL_INTFLAG_ECCDE
Definition: atsame5.c:75
#define SAME_SERIES_54
Definition: atsame5.c:87
static const struct samd_part * samd_find_part(uint32_t id)
Gives the part structure to specific device id.
Definition: atsame5.c:242
static const struct samd_family samd_families[]
Definition: atsame5.c:187
static const struct samd_part samd51_parts[]
Definition: atsame5.c:111
static const struct command_registration same5_exec_command_handlers[]
Definition: atsame5.c:930
#define SAME5_NVMCTRL_INTFLAG_DONE
Definition: atsame5.c:70
static int same5_modify_user_row(struct target *target, uint32_t value, uint8_t startb, uint8_t endb)
Modifies the user row register to the given value.
Definition: atsame5.c:583
#define PIC32CXSG_SERIES_60
Definition: atsame5.c:89
#define SAME5_NVMCTRL_CTRLB
Definition: atsame5.c:47
static int same5_erase_block(struct target *target, uint32_t address)
Erases a flash block or page at the given address.
Definition: atsame5.c:454
static int same5_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: atsame5.c:677
#define SAMD_SERIES_51
Definition: atsame5.c:84
#define SAME5_NVMCTRL_CTRLA_WMODE_MASK
Definition: atsame5.c:68
#define SAME_SERIES_51
Definition: atsame5.c:85
#define SAME5_NVMCTRL_INTFLAG_NVME
Definition: atsame5.c:76
Support functions to access arbitrary bits in a byte array.
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:443
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:442
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
#define DBGKEY
Definition: cortex_m.h:129
#define C_HALT
Definition: cortex_m.h:131
#define VC_BUSERR
Definition: cortex_m.h:145
#define VC_CORERESET
Definition: cortex_m.h:150
#define DCB_DEMCR
Definition: cortex_m.h:82
#define C_DEBUGEN
Definition: cortex_m.h:130
#define DCB_DHCSR
Definition: cortex_m.h:79
#define TRCENA
Definition: cortex_m.h:142
#define VC_HARDERR
Definition: cortex_m.h:143
uint8_t bank
Definition: esirisc.c:135
int mask
Definition: esirisc.c:1741
#define ERROR_FLASH_OPER_UNSUPPORTED
Definition: flash/common.h:36
#define ERROR_FLASH_PROTECTED
Definition: flash/common.h:37
#define ERROR_FLASH_BANK_NOT_PROBED
Definition: flash/common.h:35
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
struct flash_sector * alloc_block_array(uint32_t offset, uint32_t size, unsigned int num_blocks)
Allocate and fill an array of sectors or protection blocks.
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
static enum reset_types jtag_reset_config
Definition: jtag/core.c:87
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1734
The JTAG interface can be implemented with a software or hardware fifo.
reset_types
Definition: jtag.h:216
@ RESET_HAS_SRST
Definition: jtag.h:219
void keep_alive(void)
Definition: log.c:415
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define ERROR_OK
Definition: log.h:164
bool changed
Whether configuration changed.
Definition: rtt/rtt.c:38
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
char id[RTT_CB_MAX_ID_LENGTH]
Control block identifier.
Definition: rtt/rtt.c:32
const char * name
Definition: command.h:235
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
const struct samd_part * parts
Definition: at91samd.c:310
uint8_t processor
Definition: at91samd.c:307
size_t num_parts
Definition: at91samd.c:311
uint8_t series
Definition: at91samd.c:309
uint8_t family
Definition: at91samd.c:308
bool probed
Definition: at91samd.c:352
struct target * target
Definition: at91samd.c:353
int prot_block_size
Definition: at91samd.c:350
uint32_t page_size
Definition: at91samd.c:347
const struct samd_params * par
Definition: atsame5.c:205
int sector_size
Definition: at91samd.c:349
int num_pages
Definition: at91samd.c:348
const char * name
Definition: at91samd.c:85
uint32_t flash_kb
Definition: at91samd.c:86
uint32_t ram_kb
Definition: at91samd.c:87
uint8_t id
Definition: at91samd.c:84
Definition: target.h:116
enum target_state state
Definition: target.h:157
bool reset_halt
Definition: target.h:144
uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
Definition: target.c:307
int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
Definition: target.c:2683
int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
Definition: target.c:2662
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1265
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2641
int target_examine_one(struct target *target)
Examine the specified target, letting it perform any Initialisation that requires JTAG access.
Definition: target.c:672
int target_poll(struct target *target)
Definition: target.c:477
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2574
void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
Definition: target.c:343
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2550
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1237
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:458
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:790
static bool target_was_examined(const struct target *target)
Definition: target.h:436
@ TARGET_HALTED
Definition: target.h:56
int64_t timeval_ms(void)
#define TARGET_ADDR_FMT
Definition: types.h:342
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22