OpenOCD
at91sam4.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-Source-Code)
2 
3 /*
4  * Copyright (C) 2009 by Duane Ellis <openocd@duaneellis.com>
5  *
6  * at91sam3s* support
7  * Copyright (C) 2010 by Olaf Lüke <olaf@uni-paderborn.de>
8  *
9  * at91sam3x* & at91sam4 support
10  * Copyright (C) 2011 by Olivier Schonken, Jim Norris
11  *
12  * Some of the lower level code was based on code supplied by
13  * ATMEL under BSD-Source-Code License and this copyright.
14  * ATMEL Microcontroller Software Support
15  * Copyright (c) 2009, Atmel Corporation. All rights reserved.
16  */
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include "imp.h"
23 #include <helper/time_support.h>
24 
25 #define REG_NAME_WIDTH (12)
26 
27 /* at91sam4s/at91sam4e/at91sam4c series (has always one flash bank)*/
28 #define FLASH_BANK_BASE_S 0x00400000
29 #define FLASH_BANK_BASE_C 0x01000000
30 
31 /* at91sam4sd series (two one flash banks), first bank address */
32 #define FLASH_BANK0_BASE_SD FLASH_BANK_BASE_S
33 /* at91sam4sd16x, second bank address */
34 #define FLASH_BANK1_BASE_1024K_SD (FLASH_BANK0_BASE_SD+(1024*1024/2))
35 /* at91sam4sd32x, second bank address */
36 #define FLASH_BANK1_BASE_2048K_SD (FLASH_BANK0_BASE_SD+(2048*1024/2))
37 
38 /* at91sam4c32x, first and second bank address */
39 #define FLASH_BANK0_BASE_C32 FLASH_BANK_BASE_C
40 #define FLASH_BANK1_BASE_C32 (FLASH_BANK_BASE_C+(2048*1024/2))
41 
42 #define AT91C_EFC_FCMD_GETD (0x0) /* (EFC) Get Flash Descriptor */
43 #define AT91C_EFC_FCMD_WP (0x1) /* (EFC) Write Page */
44 #define AT91C_EFC_FCMD_WPL (0x2) /* (EFC) Write Page and Lock */
45 #define AT91C_EFC_FCMD_EWP (0x3) /* (EFC) Erase Page and Write Page */
46 #define AT91C_EFC_FCMD_EWPL (0x4) /* (EFC) Erase Page and Write Page then Lock */
47 #define AT91C_EFC_FCMD_EA (0x5) /* (EFC) Erase All */
48 /* cmd6 is not present in the at91sam4u4/2/1 data sheet table 19-2 */
49 /* #define AT91C_EFC_FCMD_EPL (0x6) // (EFC) Erase plane? */
50 #define AT91C_EFC_FCMD_EPA (0x7) /* (EFC) Erase pages */
51 #define AT91C_EFC_FCMD_SLB (0x8) /* (EFC) Set Lock Bit */
52 #define AT91C_EFC_FCMD_CLB (0x9) /* (EFC) Clear Lock Bit */
53 #define AT91C_EFC_FCMD_GLB (0xA) /* (EFC) Get Lock Bit */
54 #define AT91C_EFC_FCMD_SFB (0xB) /* (EFC) Set Fuse Bit */
55 #define AT91C_EFC_FCMD_CFB (0xC) /* (EFC) Clear Fuse Bit */
56 #define AT91C_EFC_FCMD_GFB (0xD) /* (EFC) Get Fuse Bit */
57 #define AT91C_EFC_FCMD_STUI (0xE) /* (EFC) Start Read Unique ID */
58 #define AT91C_EFC_FCMD_SPUI (0xF) /* (EFC) Stop Read Unique ID */
59 
60 #define OFFSET_EFC_FMR 0
61 #define OFFSET_EFC_FCR 4
62 #define OFFSET_EFC_FSR 8
63 #define OFFSET_EFC_FRR 12
64 
65 static float _tomhz(uint32_t freq_hz)
66 {
67  float f;
68 
69  f = ((float)(freq_hz)) / 1000000.0;
70  return f;
71 }
72 
73 /* How the chip is configured. */
74 struct sam4_cfg {
75  uint32_t unique_id[4];
76 
77  uint32_t slow_freq;
78  uint32_t rc_freq;
79  uint32_t mainosc_freq;
80  uint32_t plla_freq;
81  uint32_t mclk_freq;
82  uint32_t cpu_freq;
83  uint32_t fclk_freq;
84  uint32_t pclk0_freq;
85  uint32_t pclk1_freq;
86  uint32_t pclk2_freq;
87 
88 
89 #define SAM4_CHIPID_CIDR (0x400E0740)
90  uint32_t CHIPID_CIDR;
91 #define SAM4_CHIPID_EXID (0x400E0744)
92  uint32_t CHIPID_EXID;
93 
94 #define SAM4_PMC_BASE (0x400E0400)
95 #define SAM4_PMC_SCSR (SAM4_PMC_BASE + 0x0008)
96  uint32_t PMC_SCSR;
97 #define SAM4_PMC_PCSR (SAM4_PMC_BASE + 0x0018)
98  uint32_t PMC_PCSR;
99 #define SAM4_CKGR_UCKR (SAM4_PMC_BASE + 0x001c)
100  uint32_t CKGR_UCKR;
101 #define SAM4_CKGR_MOR (SAM4_PMC_BASE + 0x0020)
102  uint32_t CKGR_MOR;
103 #define SAM4_CKGR_MCFR (SAM4_PMC_BASE + 0x0024)
104  uint32_t CKGR_MCFR;
105 #define SAM4_CKGR_PLLAR (SAM4_PMC_BASE + 0x0028)
106  uint32_t CKGR_PLLAR;
107 #define SAM4_PMC_MCKR (SAM4_PMC_BASE + 0x0030)
108  uint32_t PMC_MCKR;
109 #define SAM4_PMC_PCK0 (SAM4_PMC_BASE + 0x0040)
110  uint32_t PMC_PCK0;
111 #define SAM4_PMC_PCK1 (SAM4_PMC_BASE + 0x0044)
112  uint32_t PMC_PCK1;
113 #define SAM4_PMC_PCK2 (SAM4_PMC_BASE + 0x0048)
114  uint32_t PMC_PCK2;
115 #define SAM4_PMC_SR (SAM4_PMC_BASE + 0x0068)
116  uint32_t PMC_SR;
117 #define SAM4_PMC_IMR (SAM4_PMC_BASE + 0x006c)
118  uint32_t PMC_IMR;
119 #define SAM4_PMC_FSMR (SAM4_PMC_BASE + 0x0070)
120  uint32_t PMC_FSMR;
121 #define SAM4_PMC_FSPR (SAM4_PMC_BASE + 0x0074)
122  uint32_t PMC_FSPR;
123 };
124 
126  bool probed;
127  /* DANGER: THERE ARE DRAGONS HERE.. */
128  /* NOTE: If you add more 'ghost' pointers */
129  /* be aware that you must *manually* update */
130  /* these pointers in the function sam4_get_details() */
131  /* See the comment "Here there be dragons" */
132 
133  /* so we can find the chip we belong to */
134  struct sam4_chip *chip;
135  /* so we can find the original bank pointer */
136  struct flash_bank *bank;
137  unsigned int bank_number;
139  uint32_t base_address;
141  bool present;
142  unsigned int size_bytes;
143  unsigned int nsectors;
144  unsigned int sector_size;
145  unsigned int page_size;
146 };
147 
149  /* THERE ARE DRAGONS HERE.. */
150  /* note: If you add pointers here */
151  /* be careful about them as they */
152  /* may need to be updated inside */
153  /* the function: "sam4_get_details() */
154  /* which copy/overwrites the */
155  /* 'runtime' copy of this structure */
156  uint32_t chipid_cidr;
157  const char *name;
158 
159  unsigned int n_gpnvms;
160 #define SAM4_N_NVM_BITS 3
161  unsigned int gpnvm[SAM4_N_NVM_BITS];
162  unsigned int total_flash_size;
163  unsigned int total_sram_size;
164  unsigned int n_banks;
165 #define SAM4_MAX_FLASH_BANKS 2
166  /* these are "initialized" from the global const data */
168 };
169 
170 struct sam4_chip {
171  struct sam4_chip *next;
172  bool probed;
173 
174  /* this is "initialized" from the global const structure */
175  struct sam4_chip_details details;
176  struct target *target;
177  struct sam4_cfg cfg;
178 };
179 
180 
182  uint32_t address; size_t struct_offset; const char *name;
183  void (*explain_func)(struct sam4_chip *chip);
184 };
185 
186 static struct sam4_chip *all_sam4_chips;
187 
189 {
190  struct target *t;
191  static struct sam4_chip *p;
192 
193  t = get_current_target(cmd->ctx);
194  if (!t) {
195  command_print_sameline(cmd, "No current target?\n");
196  return NULL;
197  }
198 
199  p = all_sam4_chips;
200  if (!p) {
201  /* this should not happen */
202  /* the command is not registered until the chip is created? */
203  command_print_sameline(cmd, "No SAM4 chips exist?\n");
204  return NULL;
205  }
206 
207  while (p) {
208  if (p->target == t)
209  return p;
210  p = p->next;
211  }
212  command_print_sameline(cmd, "Cannot find SAM4 chip?\n");
213  return NULL;
214 }
215 
216 /*The actual sector size of the SAM4S flash memory is 65536 bytes. 16 sectors for a 1024KB device*/
217 /*The lockregions are 8KB per lock region, with a 1024KB device having 128 lock regions. */
218 /*For the best results, nsectors are thus set to the amount of lock regions, and the sector_size*/
219 /*set to the lock region size. Page erases are used to erase 8KB sections when programming*/
220 
221 /* these are used to *initialize* the "chip->details" structure. */
222 static const struct sam4_chip_details all_sam4_details[] = {
223  /* Start at91sam4c* series */
224  /* at91sam4c32e - LQFP144 */
225  {
226  .chipid_cidr = 0xA66D0EE0,
227  .name = "at91sam4c32e",
228  .total_flash_size = 2024 * 1024,
229  .total_sram_size = 256 * 1024,
230  .n_gpnvms = 3,
231  .n_banks = 2,
232 /* .bank[0] = { */
233  {
234  {
235  .probed = false,
236  .chip = NULL,
237  .bank = NULL,
238  .bank_number = 0,
239  .base_address = FLASH_BANK0_BASE_C32,
240  .controller_address = 0x400e0a00,
241  .flash_wait_states = 5,
242  .present = true,
243  .size_bytes = 1024 * 1024,
244  .nsectors = 128,
245  .sector_size = 8192,
246  .page_size = 512,
247  },
248 /* .bank[1] = { */
249  {
250  .probed = false,
251  .chip = NULL,
252  .bank = NULL,
253  .bank_number = 1,
254  .base_address = FLASH_BANK1_BASE_C32,
255  .controller_address = 0x400e0c00,
256  .flash_wait_states = 5,
257  .present = true,
258  .size_bytes = 1024 * 1024,
259  .nsectors = 128,
260  .sector_size = 8192,
261  .page_size = 512,
262  },
263  },
264  },
265  /* at91sam4c32c - LQFP100 */
266  {
267  .chipid_cidr = 0xA64D0EE0,
268  .name = "at91sam4c32c",
269  .total_flash_size = 2024 * 1024,
270  .total_sram_size = 256 * 1024,
271  .n_gpnvms = 3,
272  .n_banks = 2,
273 /* .bank[0] = { */
274  {
275  {
276  .probed = false,
277  .chip = NULL,
278  .bank = NULL,
279  .bank_number = 0,
280  .base_address = FLASH_BANK0_BASE_C32,
281  .controller_address = 0x400e0a00,
282  .flash_wait_states = 5,
283  .present = true,
284  .size_bytes = 1024 * 1024,
285  .nsectors = 128,
286  .sector_size = 8192,
287  .page_size = 512,
288  },
289 /* .bank[1] = { */
290  {
291  .probed = false,
292  .chip = NULL,
293  .bank = NULL,
294  .bank_number = 1,
295  .base_address = FLASH_BANK1_BASE_C32,
296  .controller_address = 0x400e0c00,
297  .flash_wait_states = 5,
298  .present = true,
299  .size_bytes = 1024 * 1024,
300  .nsectors = 128,
301  .sector_size = 8192,
302  .page_size = 512,
303  },
304  },
305  },
306  /* at91sam4c16c - LQFP100 */
307  {
308  .chipid_cidr = 0xA64C0CE0,
309  .name = "at91sam4c16c",
310  .total_flash_size = 1024 * 1024,
311  .total_sram_size = 128 * 1024,
312  .n_gpnvms = 2,
313  .n_banks = 1,
314  {
315 /* .bank[0] = {*/
316  {
317  .probed = false,
318  .chip = NULL,
319  .bank = NULL,
320  .bank_number = 0,
321  .base_address = FLASH_BANK_BASE_C,
322  .controller_address = 0x400e0a00,
323  .flash_wait_states = 5,
324  .present = true,
325  .size_bytes = 1024 * 1024,
326  .nsectors = 128,
327  .sector_size = 8192,
328  .page_size = 512,
329  },
330 /* .bank[1] = {*/
331  {
332  .present = false,
333  .probed = false,
334  .bank_number = 1,
335 
336  },
337  },
338  },
339  /* at91sam4c8c - LQFP100 */
340  {
341  .chipid_cidr = 0xA64C0AE0,
342  .name = "at91sam4c8c",
343  .total_flash_size = 512 * 1024,
344  .total_sram_size = 128 * 1024,
345  .n_gpnvms = 2,
346  .n_banks = 1,
347  {
348 /* .bank[0] = {*/
349  {
350  .probed = false,
351  .chip = NULL,
352  .bank = NULL,
353  .bank_number = 0,
354  .base_address = FLASH_BANK_BASE_C,
355  .controller_address = 0x400e0a00,
356  .flash_wait_states = 5,
357  .present = true,
358  .size_bytes = 512 * 1024,
359  .nsectors = 64,
360  .sector_size = 8192,
361  .page_size = 512,
362  },
363 /* .bank[1] = {*/
364  {
365  .present = false,
366  .probed = false,
367  .bank_number = 1,
368 
369  },
370  },
371  },
372  /* at91sam4c4c (rev B) - LQFP100 */
373  {
374  .chipid_cidr = 0xA64C0CE5,
375  .name = "at91sam4c4c",
376  .total_flash_size = 256 * 1024,
377  .total_sram_size = 128 * 1024,
378  .n_gpnvms = 2,
379  .n_banks = 1,
380  {
381 /* .bank[0] = {*/
382  {
383  .probed = false,
384  .chip = NULL,
385  .bank = NULL,
386  .bank_number = 0,
387  .base_address = FLASH_BANK_BASE_C,
388  .controller_address = 0x400e0a00,
389  .flash_wait_states = 5,
390  .present = true,
391  .size_bytes = 256 * 1024,
392  .nsectors = 32,
393  .sector_size = 8192,
394  .page_size = 512,
395  },
396 /* .bank[1] = {*/
397  {
398  .present = false,
399  .probed = false,
400  .bank_number = 1,
401 
402  },
403  },
404  },
405 
406  /* Start at91sam4e* series */
407  /*atsam4e16e - LQFP144/LFBGA144*/
408  {
409  .chipid_cidr = 0xA3CC0CE0,
410  .name = "at91sam4e16e",
411  .total_flash_size = 1024 * 1024,
412  .total_sram_size = 128 * 1024,
413  .n_gpnvms = 2,
414  .n_banks = 1,
415  {
416 /* .bank[0] = {*/
417  {
418  .probed = false,
419  .chip = NULL,
420  .bank = NULL,
421  .bank_number = 0,
422  .base_address = FLASH_BANK_BASE_S,
423  .controller_address = 0x400e0a00,
424  .flash_wait_states = 5,
425  .present = true,
426  .size_bytes = 1024 * 1024,
427  .nsectors = 128,
428  .sector_size = 8192,
429  .page_size = 512,
430  },
431 /* .bank[1] = {*/
432  {
433  .present = false,
434  .probed = false,
435  .bank_number = 1,
436 
437  },
438  },
439  },
440 
441  /* Start at91sam4n* series */
442  /*atsam4n8a - LQFP48/QFN48*/
443  {
444  .chipid_cidr = 0x293B0AE0,
445  .name = "at91sam4n8a",
446  .total_flash_size = 512 * 1024,
447  .total_sram_size = 64 * 1024,
448  .n_gpnvms = 2,
449  .n_banks = 1,
450  {
451 /* .bank[0] = {*/
452  {
453  .probed = false,
454  .chip = NULL,
455  .bank = NULL,
456  .bank_number = 0,
457  .base_address = FLASH_BANK_BASE_S,
458  .controller_address = 0x400e0a00,
459  .flash_wait_states = 5,
460  .present = true,
461  .size_bytes = 512 * 1024,
462  .nsectors = 64,
463  .sector_size = 8192,
464  .page_size = 512,
465  },
466 /* .bank[1] = {*/
467  {
468  .present = false,
469  .probed = false,
470  .bank_number = 1,
471 
472  },
473  },
474  },
475  /*atsam4n8b - LQFP64/QFN64*/
476  {
477  .chipid_cidr = 0x294B0AE0,
478  .name = "at91sam4n8b",
479  .total_flash_size = 512 * 1024,
480  .total_sram_size = 64 * 1024,
481  .n_gpnvms = 2,
482  .n_banks = 1,
483  {
484 /* .bank[0] = {*/
485  {
486  .probed = false,
487  .chip = NULL,
488  .bank = NULL,
489  .bank_number = 0,
490  .base_address = FLASH_BANK_BASE_S,
491  .controller_address = 0x400e0a00,
492  .flash_wait_states = 5,
493  .present = true,
494  .size_bytes = 512 * 1024,
495  .nsectors = 64,
496  .sector_size = 8192,
497  .page_size = 512,
498  },
499 /* .bank[1] = {*/
500  {
501  .present = false,
502  .probed = false,
503  .bank_number = 1,
504 
505  },
506  },
507  },
508  /*atsam4n8c - LQFP100/TFBGA100/VFBGA100*/
509  {
510  .chipid_cidr = 0x295B0AE0,
511  .name = "at91sam4n8c",
512  .total_flash_size = 512 * 1024,
513  .total_sram_size = 64 * 1024,
514  .n_gpnvms = 2,
515  .n_banks = 1,
516  {
517 /* .bank[0] = {*/
518  {
519  .probed = false,
520  .chip = NULL,
521  .bank = NULL,
522  .bank_number = 0,
523  .base_address = FLASH_BANK_BASE_S,
524  .controller_address = 0x400e0a00,
525  .flash_wait_states = 5,
526  .present = true,
527  .size_bytes = 512 * 1024,
528  .nsectors = 64,
529  .sector_size = 8192,
530  .page_size = 512,
531  },
532 /* .bank[1] = {*/
533  {
534  .present = false,
535  .probed = false,
536  .bank_number = 1,
537 
538  },
539  },
540  },
541  /*atsam4n16b - LQFP64/QFN64*/
542  {
543  .chipid_cidr = 0x29460CE0,
544  .name = "at91sam4n16b",
545  .total_flash_size = 1024 * 1024,
546  .total_sram_size = 80 * 1024,
547  .n_gpnvms = 2,
548  .n_banks = 1,
549  {
550 /* .bank[0] = {*/
551  {
552  .probed = false,
553  .chip = NULL,
554  .bank = NULL,
555  .bank_number = 0,
556  .base_address = FLASH_BANK_BASE_S,
557  .controller_address = 0x400e0a00,
558  .flash_wait_states = 5,
559  .present = true,
560  .size_bytes = 1024 * 1024,
561  .nsectors = 128,
562  .sector_size = 8192,
563  .page_size = 512,
564  },
565 /* .bank[1] = {*/
566  {
567  .present = false,
568  .probed = false,
569  .bank_number = 1,
570 
571  },
572  },
573  },
574  /*atsam4n16c - LQFP100/TFBGA100/VFBGA100*/
575  {
576  .chipid_cidr = 0x29560CE0,
577  .name = "at91sam4n16c",
578  .total_flash_size = 1024 * 1024,
579  .total_sram_size = 80 * 1024,
580  .n_gpnvms = 2,
581  .n_banks = 1,
582  {
583 /* .bank[0] = {*/
584  {
585  .probed = false,
586  .chip = NULL,
587  .bank = NULL,
588  .bank_number = 0,
589  .base_address = FLASH_BANK_BASE_S,
590  .controller_address = 0x400e0a00,
591  .flash_wait_states = 5,
592  .present = true,
593  .size_bytes = 1024 * 1024,
594  .nsectors = 128,
595  .sector_size = 8192,
596  .page_size = 512,
597  },
598 /* .bank[1] = {*/
599  {
600  .present = false,
601  .probed = false,
602  .bank_number = 1,
603 
604  },
605  },
606  },
607 
608  /* Start at91sam4s* series */
609  /*atsam4s16c - LQFP100/BGA100*/
610  {
611  .chipid_cidr = 0x28AC0CE0,
612  .name = "at91sam4s16c",
613  .total_flash_size = 1024 * 1024,
614  .total_sram_size = 128 * 1024,
615  .n_gpnvms = 2,
616  .n_banks = 1,
617  {
618 /* .bank[0] = {*/
619  {
620  .probed = false,
621  .chip = NULL,
622  .bank = NULL,
623  .bank_number = 0,
624  .base_address = FLASH_BANK_BASE_S,
625  .controller_address = 0x400e0a00,
626  .flash_wait_states = 5,
627  .present = true,
628  .size_bytes = 1024 * 1024,
629  .nsectors = 128,
630  .sector_size = 8192,
631  .page_size = 512,
632  },
633 /* .bank[1] = {*/
634  {
635  .present = false,
636  .probed = false,
637  .bank_number = 1,
638 
639  },
640  },
641  },
642  /*at91sam4sa16c - TFBGA100/VFBGA100/LQFP100*/
643  {
644  .chipid_cidr = 0x28a70ce0,
645  .name = "at91sam4sa16c",
646  .total_flash_size = 1024 * 1024,
647  .total_sram_size = 160 * 1024,
648  .n_gpnvms = 2,
649  .n_banks = 1,
650 
651 /* .bank[0] = { */
652  {
653  {
654  .probed = false,
655  .chip = NULL,
656  .bank = NULL,
657  .bank_number = 0,
658  .base_address = FLASH_BANK_BASE_S,
659  .controller_address = 0x400e0a00,
660  .flash_wait_states = 5,
661  .present = true,
662  .size_bytes = 1024 * 1024,
663  .nsectors = 128,
664  .sector_size = 8192,
665  .page_size = 512,
666  },
667 /* .bank[1] = {*/
668  {
669  .present = false,
670  .probed = false,
671  .bank_number = 1,
672 
673  },
674  },
675  },
676  /*atsam4s16b - LQFP64/QFN64/WLCSP64*/
677  {
678  .chipid_cidr = 0x289C0CE0,
679  .name = "at91sam4s16b",
680  .total_flash_size = 1024 * 1024,
681  .total_sram_size = 128 * 1024,
682  .n_gpnvms = 2,
683  .n_banks = 1,
684  {
685 /* .bank[0] = {*/
686  {
687  .probed = false,
688  .chip = NULL,
689  .bank = NULL,
690  .bank_number = 0,
691  .base_address = FLASH_BANK_BASE_S,
692  .controller_address = 0x400e0a00,
693  .flash_wait_states = 5,
694  .present = true,
695  .size_bytes = 1024 * 1024,
696  .nsectors = 128,
697  .sector_size = 8192,
698  .page_size = 512,
699  },
700 /* .bank[1] = {*/
701  {
702  .present = false,
703  .probed = false,
704  .bank_number = 1,
705 
706  },
707  },
708  },
709  /*atsam4sa16b - LQFP64/QFN64*/
710  {
711  .chipid_cidr = 0x28970CE0,
712  .name = "at91sam4sa16b",
713  .total_flash_size = 1024 * 1024,
714  .total_sram_size = 160 * 1024,
715  .n_gpnvms = 2,
716  .n_banks = 1,
717  {
718 /* .bank[0] = {*/
719  {
720  .probed = false,
721  .chip = NULL,
722  .bank = NULL,
723  .bank_number = 0,
724  .base_address = FLASH_BANK_BASE_S,
725  .controller_address = 0x400e0a00,
726  .flash_wait_states = 5,
727  .present = true,
728  .size_bytes = 1024 * 1024,
729  .nsectors = 128,
730  .sector_size = 8192,
731  .page_size = 512,
732  },
733 /* .bank[1] = {*/
734  {
735  .present = false,
736  .probed = false,
737  .bank_number = 1,
738 
739  },
740  },
741  },
742  /*atsam4s16a - LQFP48/QFN48*/
743  {
744  .chipid_cidr = 0x288C0CE0,
745  .name = "at91sam4s16a",
746  .total_flash_size = 1024 * 1024,
747  .total_sram_size = 128 * 1024,
748  .n_gpnvms = 2,
749  .n_banks = 1,
750  {
751 /* .bank[0] = {*/
752  {
753  .probed = false,
754  .chip = NULL,
755  .bank = NULL,
756  .bank_number = 0,
757  .base_address = FLASH_BANK_BASE_S,
758  .controller_address = 0x400e0a00,
759  .flash_wait_states = 5,
760  .present = true,
761  .size_bytes = 1024 * 1024,
762  .nsectors = 128,
763  .sector_size = 8192,
764  .page_size = 512,
765  },
766 /* .bank[1] = {*/
767  {
768  .present = false,
769  .probed = false,
770  .bank_number = 1,
771 
772  },
773  },
774  },
775  /*atsam4s8c - LQFP100/BGA100*/
776  {
777  .chipid_cidr = 0x28AC0AE0,
778  .name = "at91sam4s8c",
779  .total_flash_size = 512 * 1024,
780  .total_sram_size = 128 * 1024,
781  .n_gpnvms = 2,
782  .n_banks = 1,
783  {
784 /* .bank[0] = {*/
785  {
786  .probed = false,
787  .chip = NULL,
788  .bank = NULL,
789  .bank_number = 0,
790  .base_address = FLASH_BANK_BASE_S,
791  .controller_address = 0x400e0a00,
792  .flash_wait_states = 5,
793  .present = true,
794  .size_bytes = 512 * 1024,
795  .nsectors = 64,
796  .sector_size = 8192,
797  .page_size = 512,
798  },
799 /* .bank[1] = {*/
800  {
801  .present = false,
802  .probed = false,
803  .bank_number = 1,
804 
805  },
806  },
807  },
808  /*atsam4s8b - LQFP64/QFN64/WLCSP64*/
809  {
810  .chipid_cidr = 0x289C0AE0,
811  .name = "at91sam4s8b",
812  .total_flash_size = 512 * 1024,
813  .total_sram_size = 128 * 1024,
814  .n_gpnvms = 2,
815  .n_banks = 1,
816  {
817 /* .bank[0] = {*/
818  {
819  .probed = false,
820  .chip = NULL,
821  .bank = NULL,
822  .bank_number = 0,
823  .base_address = FLASH_BANK_BASE_S,
824  .controller_address = 0x400e0a00,
825  .flash_wait_states = 5,
826  .present = true,
827  .size_bytes = 512 * 1024,
828  .nsectors = 64,
829  .sector_size = 8192,
830  .page_size = 512,
831  },
832 /* .bank[1] = {*/
833  {
834  .present = false,
835  .probed = false,
836  .bank_number = 1,
837 
838  },
839  },
840  },
841  /*atsam4s8a - LQFP48/BGA48*/
842  {
843  .chipid_cidr = 0x288C0AE0,
844  .name = "at91sam4s8a",
845  .total_flash_size = 512 * 1024,
846  .total_sram_size = 128 * 1024,
847  .n_gpnvms = 2,
848  .n_banks = 1,
849  {
850 /* .bank[0] = {*/
851  {
852  .probed = false,
853  .chip = NULL,
854  .bank = NULL,
855  .bank_number = 0,
856  .base_address = FLASH_BANK_BASE_S,
857  .controller_address = 0x400e0a00,
858  .flash_wait_states = 5,
859  .present = true,
860  .size_bytes = 512 * 1024,
861  .nsectors = 64,
862  .sector_size = 8192,
863  .page_size = 512,
864  },
865 /* .bank[1] = {*/
866  {
867  .present = false,
868  .probed = false,
869  .bank_number = 1,
870 
871  },
872  },
873  },
874 
875  /*atsam4s4c - LQFP100/BGA100*/
876  {
877  .chipid_cidr = 0x28ab09e0,
878  .name = "at91sam4s4c",
879  .total_flash_size = 256 * 1024,
880  .total_sram_size = 64 * 1024,
881  .n_gpnvms = 2,
882  .n_banks = 1,
883  {
884 /* .bank[0] = {*/
885  {
886  .probed = false,
887  .chip = NULL,
888  .bank = NULL,
889  .bank_number = 0,
890  .base_address = FLASH_BANK_BASE_S,
891  .controller_address = 0x400e0a00,
892  .flash_wait_states = 5,
893  .present = true,
894  .size_bytes = 256 * 1024,
895  .nsectors = 32,
896  .sector_size = 8192,
897  .page_size = 512,
898  },
899 /* .bank[1] = {*/
900  {
901  .present = false,
902  .probed = false,
903  .bank_number = 1,
904 
905  },
906  },
907  },
908 
909  /*atsam4s4b - LQFP64/QFN64/WLCSP64*/
910  {
911  .chipid_cidr = 0x289b09e0,
912  .name = "at91sam4s4b",
913  .total_flash_size = 256 * 1024,
914  .total_sram_size = 64 * 1024,
915  .n_gpnvms = 2,
916  .n_banks = 1,
917  {
918 /* .bank[0] = {*/
919  {
920  .probed = false,
921  .chip = NULL,
922  .bank = NULL,
923  .bank_number = 0,
924  .base_address = FLASH_BANK_BASE_S,
925  .controller_address = 0x400e0a00,
926  .flash_wait_states = 5,
927  .present = true,
928  .size_bytes = 256 * 1024,
929  .nsectors = 32,
930  .sector_size = 8192,
931  .page_size = 512,
932  },
933 /* .bank[1] = {*/
934  {
935  .present = false,
936  .probed = false,
937  .bank_number = 1,
938 
939  },
940  },
941  },
942 
943  /*atsam4s4a - LQFP48/QFN48*/
944  {
945  .chipid_cidr = 0x288b09e0,
946  .name = "at91sam4s4a",
947  .total_flash_size = 256 * 1024,
948  .total_sram_size = 64 * 1024,
949  .n_gpnvms = 2,
950  .n_banks = 1,
951  {
952 /* .bank[0] = {*/
953  {
954  .probed = false,
955  .chip = NULL,
956  .bank = NULL,
957  .bank_number = 0,
958  .base_address = FLASH_BANK_BASE_S,
959  .controller_address = 0x400e0a00,
960  .flash_wait_states = 5,
961  .present = true,
962  .size_bytes = 256 * 1024,
963  .nsectors = 32,
964  .sector_size = 8192,
965  .page_size = 512,
966  },
967 /* .bank[1] = {*/
968  {
969  .present = false,
970  .probed = false,
971  .bank_number = 1,
972 
973  },
974  },
975  },
976 
977  /*atsam4s2c - LQFP100/BGA100*/
978  {
979  .chipid_cidr = 0x28ab07e0,
980  .name = "at91sam4s2c",
981  .total_flash_size = 128 * 1024,
982  .total_sram_size = 64 * 1024,
983  .n_gpnvms = 2,
984  .n_banks = 1,
985  {
986 /* .bank[0] = {*/
987  {
988  .probed = false,
989  .chip = NULL,
990  .bank = NULL,
991  .bank_number = 0,
992  .base_address = FLASH_BANK_BASE_S,
993  .controller_address = 0x400e0a00,
994  .flash_wait_states = 5,
995  .present = true,
996  .size_bytes = 128 * 1024,
997  .nsectors = 16,
998  .sector_size = 8192,
999  .page_size = 512,
1000  },
1001 /* .bank[1] = {*/
1002  {
1003  .present = false,
1004  .probed = false,
1005  .bank_number = 1,
1006 
1007  },
1008  },
1009  },
1010 
1011  /*atsam4s2b - LQPF64/QFN64/WLCSP64*/
1012  {
1013  .chipid_cidr = 0x289b07e0,
1014  .name = "at91sam4s2b",
1015  .total_flash_size = 128 * 1024,
1016  .total_sram_size = 64 * 1024,
1017  .n_gpnvms = 2,
1018  .n_banks = 1,
1019  {
1020 /* .bank[0] = {*/
1021  {
1022  .probed = false,
1023  .chip = NULL,
1024  .bank = NULL,
1025  .bank_number = 0,
1026  .base_address = FLASH_BANK_BASE_S,
1027  .controller_address = 0x400e0a00,
1028  .flash_wait_states = 5,
1029  .present = true,
1030  .size_bytes = 128 * 1024,
1031  .nsectors = 16,
1032  .sector_size = 8192,
1033  .page_size = 512,
1034  },
1035 /* .bank[1] = {*/
1036  {
1037  .present = false,
1038  .probed = false,
1039  .bank_number = 1,
1040 
1041  },
1042  },
1043  },
1044 
1045  /*atsam4s2a - LQFP48/QFN48*/
1046  {
1047  .chipid_cidr = 0x288b07e0,
1048  .name = "at91sam4s2a",
1049  .total_flash_size = 128 * 1024,
1050  .total_sram_size = 64 * 1024,
1051  .n_gpnvms = 2,
1052  .n_banks = 1,
1053  {
1054 /* .bank[0] = {*/
1055  {
1056  .probed = false,
1057  .chip = NULL,
1058  .bank = NULL,
1059  .bank_number = 0,
1060  .base_address = FLASH_BANK_BASE_S,
1061  .controller_address = 0x400e0a00,
1062  .flash_wait_states = 5,
1063  .present = true,
1064  .size_bytes = 128 * 1024,
1065  .nsectors = 16,
1066  .sector_size = 8192,
1067  .page_size = 512,
1068  },
1069 /* .bank[1] = {*/
1070  {
1071  .present = false,
1072  .probed = false,
1073  .bank_number = 1,
1074 
1075  },
1076  },
1077  },
1078 
1079  /*at91sam4sd32c - LQFP100/BGA100*/
1080  {
1081  .chipid_cidr = 0x29a70ee0,
1082  .name = "at91sam4sd32c",
1083  .total_flash_size = 2048 * 1024,
1084  .total_sram_size = 160 * 1024,
1085  .n_gpnvms = 3,
1086  .n_banks = 2,
1087 
1088 /* .bank[0] = { */
1089  {
1090  {
1091  .probed = false,
1092  .chip = NULL,
1093  .bank = NULL,
1094  .bank_number = 0,
1095  .base_address = FLASH_BANK0_BASE_SD,
1096  .controller_address = 0x400e0a00,
1097  .flash_wait_states = 5,
1098  .present = true,
1099  .size_bytes = 1024 * 1024,
1100  .nsectors = 128,
1101  .sector_size = 8192,
1102  .page_size = 512,
1103  },
1104 
1105 /* .bank[1] = { */
1106  {
1107  .probed = false,
1108  .chip = NULL,
1109  .bank = NULL,
1110  .bank_number = 1,
1111  .base_address = FLASH_BANK1_BASE_2048K_SD,
1112  .controller_address = 0x400e0c00,
1113  .flash_wait_states = 5,
1114  .present = true,
1115  .size_bytes = 1024 * 1024,
1116  .nsectors = 128,
1117  .sector_size = 8192,
1118  .page_size = 512,
1119  },
1120  },
1121  },
1122 
1123  /*at91sam4sd32b - LQFP64/BGA64*/
1124  {
1125  .chipid_cidr = 0x29970ee0,
1126  .name = "at91sam4sd32b",
1127  .total_flash_size = 2048 * 1024,
1128  .total_sram_size = 160 * 1024,
1129  .n_gpnvms = 3,
1130  .n_banks = 2,
1131 
1132 /* .bank[0] = { */
1133  {
1134  {
1135  .probed = false,
1136  .chip = NULL,
1137  .bank = NULL,
1138  .bank_number = 0,
1139  .base_address = FLASH_BANK0_BASE_SD,
1140  .controller_address = 0x400e0a00,
1141  .flash_wait_states = 5,
1142  .present = true,
1143  .size_bytes = 1024 * 1024,
1144  .nsectors = 128,
1145  .sector_size = 8192,
1146  .page_size = 512,
1147  },
1148 
1149 /* .bank[1] = { */
1150  {
1151  .probed = false,
1152  .chip = NULL,
1153  .bank = NULL,
1154  .bank_number = 1,
1155  .base_address = FLASH_BANK1_BASE_2048K_SD,
1156  .controller_address = 0x400e0c00,
1157  .flash_wait_states = 5,
1158  .present = true,
1159  .size_bytes = 1024 * 1024,
1160  .nsectors = 128,
1161  .sector_size = 8192,
1162  .page_size = 512,
1163  },
1164  },
1165  },
1166 
1167  /*at91sam4sd16c - LQFP100/BGA100*/
1168  {
1169  .chipid_cidr = 0x29a70ce0,
1170  .name = "at91sam4sd16c",
1171  .total_flash_size = 1024 * 1024,
1172  .total_sram_size = 160 * 1024,
1173  .n_gpnvms = 3,
1174  .n_banks = 2,
1175 
1176 /* .bank[0] = { */
1177  {
1178  {
1179  .probed = false,
1180  .chip = NULL,
1181  .bank = NULL,
1182  .bank_number = 0,
1183  .base_address = FLASH_BANK0_BASE_SD,
1184  .controller_address = 0x400e0a00,
1185  .flash_wait_states = 5,
1186  .present = true,
1187  .size_bytes = 512 * 1024,
1188  .nsectors = 64,
1189  .sector_size = 8192,
1190  .page_size = 512,
1191  },
1192 
1193 /* .bank[1] = { */
1194  {
1195  .probed = false,
1196  .chip = NULL,
1197  .bank = NULL,
1198  .bank_number = 1,
1199  .base_address = FLASH_BANK1_BASE_1024K_SD,
1200  .controller_address = 0x400e0c00,
1201  .flash_wait_states = 5,
1202  .present = true,
1203  .size_bytes = 512 * 1024,
1204  .nsectors = 64,
1205  .sector_size = 8192,
1206  .page_size = 512,
1207  },
1208  },
1209  },
1210 
1211  /*at91sam4sd16b - LQFP64/BGA64*/
1212  {
1213  .chipid_cidr = 0x29970ce0,
1214  .name = "at91sam4sd16b",
1215  .total_flash_size = 1024 * 1024,
1216  .total_sram_size = 160 * 1024,
1217  .n_gpnvms = 3,
1218  .n_banks = 2,
1219 
1220 /* .bank[0] = { */
1221  {
1222  {
1223  .probed = false,
1224  .chip = NULL,
1225  .bank = NULL,
1226  .bank_number = 0,
1227  .base_address = FLASH_BANK0_BASE_SD,
1228  .controller_address = 0x400e0a00,
1229  .flash_wait_states = 5,
1230  .present = true,
1231  .size_bytes = 512 * 1024,
1232  .nsectors = 64,
1233  .sector_size = 8192,
1234  .page_size = 512,
1235  },
1236 
1237 /* .bank[1] = { */
1238  {
1239  .probed = false,
1240  .chip = NULL,
1241  .bank = NULL,
1242  .bank_number = 1,
1243  .base_address = FLASH_BANK1_BASE_1024K_SD,
1244  .controller_address = 0x400e0c00,
1245  .flash_wait_states = 5,
1246  .present = true,
1247  .size_bytes = 512 * 1024,
1248  .nsectors = 64,
1249  .sector_size = 8192,
1250  .page_size = 512,
1251  },
1252  },
1253  },
1254 
1255  /* atsamg53n19 */
1256  {
1257  .chipid_cidr = 0x247e0ae0,
1258  .name = "atsamg53n19",
1259  .total_flash_size = 512 * 1024,
1260  .total_sram_size = 96 * 1024,
1261  .n_gpnvms = 2,
1262  .n_banks = 1,
1263 
1264 /* .bank[0] = {*/
1265  {
1266  {
1267  .probed = false,
1268  .chip = NULL,
1269  .bank = NULL,
1270  .bank_number = 0,
1271  .base_address = FLASH_BANK_BASE_S,
1272  .controller_address = 0x400e0a00,
1273  .flash_wait_states = 5,
1274  .present = true,
1275  .size_bytes = 512 * 1024,
1276  .nsectors = 64,
1277  .sector_size = 8192,
1278  .page_size = 512,
1279  },
1280 /* .bank[1] = {*/
1281  {
1282  .present = false,
1283  .probed = false,
1284  .bank_number = 1,
1285 
1286  },
1287  }
1288  },
1289 
1290  /* atsamg55g19 Rev.A */
1291  {
1292  .chipid_cidr = 0x24470ae0,
1293  .name = "atsamg55g19",
1294  .total_flash_size = 512 * 1024,
1295  .total_sram_size = 160 * 1024,
1296  .n_gpnvms = 2,
1297  .n_banks = 1,
1298 
1299  {
1300 /* .bank[0] = */
1301  {
1302  .probed = false,
1303  .chip = NULL,
1304  .bank = NULL,
1305  .bank_number = 0,
1306  .base_address = FLASH_BANK_BASE_S,
1307  .controller_address = 0x400e0a00,
1308  .flash_wait_states = 5,
1309  .present = true,
1310  .size_bytes = 512 * 1024,
1311  .nsectors = 64,
1312  .sector_size = 8192,
1313  .page_size = 512,
1314  },
1315 /* .bank[1] = */
1316  {
1317  .present = false,
1318  .probed = false,
1319  .bank_number = 1,
1320  },
1321  }
1322  },
1323 
1324  /* atsamg55g19 Rev.B */
1325  {
1326  .chipid_cidr = 0x24470ae1,
1327  .name = "atsamg55g19b",
1328  .total_flash_size = 512 * 1024,
1329  .total_sram_size = 160 * 1024,
1330  .n_gpnvms = 2,
1331  .n_banks = 1,
1332 
1333  {
1334 /* .bank[0] = */
1335  {
1336  .probed = false,
1337  .chip = NULL,
1338  .bank = NULL,
1339  .bank_number = 0,
1340  .base_address = FLASH_BANK_BASE_S,
1341  .controller_address = 0x400e0a00,
1342  .flash_wait_states = 5,
1343  .present = true,
1344  .size_bytes = 512 * 1024,
1345  .nsectors = 64,
1346  .sector_size = 8192,
1347  .page_size = 512,
1348  },
1349 /* .bank[1] = */
1350  {
1351  .present = false,
1352  .probed = false,
1353  .bank_number = 1,
1354  },
1355  }
1356  },
1357 
1358  /* atsamg55j19 Rev.A */
1359  {
1360  .chipid_cidr = 0x24570ae0,
1361  .name = "atsamg55j19",
1362  .total_flash_size = 512 * 1024,
1363  .total_sram_size = 160 * 1024,
1364  .n_gpnvms = 2,
1365  .n_banks = 1,
1366 
1367  {
1368 /* .bank[0] = */
1369  {
1370  .probed = false,
1371  .chip = NULL,
1372  .bank = NULL,
1373  .bank_number = 0,
1374  .base_address = FLASH_BANK_BASE_S,
1375  .controller_address = 0x400e0a00,
1376  .flash_wait_states = 5,
1377  .present = true,
1378  .size_bytes = 512 * 1024,
1379  .nsectors = 64,
1380  .sector_size = 8192,
1381  .page_size = 512,
1382  },
1383 /* .bank[1] = */
1384  {
1385  .present = false,
1386  .probed = false,
1387  .bank_number = 1,
1388  },
1389  }
1390  },
1391 
1392  /* atsamg55j19 Rev.B */
1393  {
1394  .chipid_cidr = 0x24570ae1,
1395  .name = "atsamg55j19b",
1396  .total_flash_size = 512 * 1024,
1397  .total_sram_size = 160 * 1024,
1398  .n_gpnvms = 2,
1399  .n_banks = 1,
1400 
1401  {
1402 /* .bank[0] = */
1403  {
1404  .probed = false,
1405  .chip = NULL,
1406  .bank = NULL,
1407  .bank_number = 0,
1408  .base_address = FLASH_BANK_BASE_S,
1409  .controller_address = 0x400e0a00,
1410  .flash_wait_states = 5,
1411  .present = true,
1412  .size_bytes = 512 * 1024,
1413  .nsectors = 64,
1414  .sector_size = 8192,
1415  .page_size = 512,
1416  },
1417 /* .bank[1] = */
1418  {
1419  .present = false,
1420  .probed = false,
1421  .bank_number = 1,
1422  },
1423  }
1424  },
1425 
1426  /* terminate */
1427  {
1428  .chipid_cidr = 0,
1429  .name = NULL,
1430  }
1431 };
1432 
1433 /* Globals above */
1434 /***********************************************************************
1435  **********************************************************************
1436  **********************************************************************
1437  **********************************************************************
1438  **********************************************************************
1439  **********************************************************************/
1440 /* *ATMEL* style code - from the SAM4 driver code */
1441 
1448 static int efc_get_status(struct sam4_bank_private *private, uint32_t *v)
1449 {
1450  int r;
1451  r = target_read_u32(private->chip->target,
1452  private->controller_address + OFFSET_EFC_FSR,
1453  v);
1454  LOG_DEBUG("Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
1455  (unsigned int)(*v),
1456  ((unsigned int)((*v >> 2) & 1)),
1457  ((unsigned int)((*v >> 1) & 1)),
1458  ((unsigned int)((*v >> 0) & 1)));
1459 
1460  return r;
1461 }
1462 
1468 static int efc_get_result(struct sam4_bank_private *private, uint32_t *v)
1469 {
1470  int r;
1471  uint32_t rv;
1472  r = target_read_u32(private->chip->target,
1473  private->controller_address + OFFSET_EFC_FRR,
1474  &rv);
1475  if (v)
1476  *v = rv;
1477  LOG_DEBUG("Result: 0x%08x", ((unsigned int)(rv)));
1478  return r;
1479 }
1480 
1481 static int efc_start_command(struct sam4_bank_private *private,
1482  unsigned int command, unsigned int argument)
1483 {
1484  uint32_t n, v;
1485  int r;
1486  int retry;
1487 
1488  retry = 0;
1489 do_retry:
1490 
1491  /* Check command & argument */
1492  switch (command) {
1493  case AT91C_EFC_FCMD_WP:
1494  case AT91C_EFC_FCMD_WPL:
1495  case AT91C_EFC_FCMD_EWP:
1496  case AT91C_EFC_FCMD_EWPL:
1497  /* case AT91C_EFC_FCMD_EPL: */
1498  case AT91C_EFC_FCMD_EPA:
1499  case AT91C_EFC_FCMD_SLB:
1500  case AT91C_EFC_FCMD_CLB:
1501  n = (private->size_bytes / private->page_size);
1502  if (argument >= n)
1503  LOG_ERROR("*BUG*: Embedded flash has only %" PRIu32 " pages", n);
1504  break;
1505 
1506  case AT91C_EFC_FCMD_SFB:
1507  case AT91C_EFC_FCMD_CFB:
1508  if (argument >= private->chip->details.n_gpnvms) {
1509  LOG_ERROR("*BUG*: Embedded flash has only %d GPNVMs",
1510  private->chip->details.n_gpnvms);
1511  }
1512  break;
1513 
1514  case AT91C_EFC_FCMD_GETD:
1515  case AT91C_EFC_FCMD_EA:
1516  case AT91C_EFC_FCMD_GLB:
1517  case AT91C_EFC_FCMD_GFB:
1518  case AT91C_EFC_FCMD_STUI:
1519  case AT91C_EFC_FCMD_SPUI:
1520  if (argument != 0)
1521  LOG_ERROR("Argument is meaningless for cmd: %d", command);
1522  break;
1523  default:
1524  LOG_ERROR("Unknown command %d", command);
1525  break;
1526  }
1527 
1528  if (command == AT91C_EFC_FCMD_SPUI) {
1529  /* this is a very special situation. */
1530  /* Situation (1) - error/retry - see below */
1531  /* And we are being called recursively */
1532  /* Situation (2) - normal, finished reading unique id */
1533  } else {
1534  /* it should be "ready" */
1535  efc_get_status(private, &v);
1536  if (v & 1) {
1537  /* then it is ready */
1538  /* we go on */
1539  } else {
1540  if (retry) {
1541  /* we have done this before */
1542  /* the controller is not responding. */
1543  LOG_ERROR("flash controller(%d) is not ready! Error",
1544  private->bank_number);
1545  return ERROR_FAIL;
1546  } else {
1547  retry++;
1548  LOG_ERROR("Flash controller(%d) is not ready, attempting reset",
1549  private->bank_number);
1550  /* we do that by issuing the *STOP* command */
1552  /* above is recursive, and further recursion is blocked by */
1553  /* if (command == AT91C_EFC_FCMD_SPUI) above */
1554  goto do_retry;
1555  }
1556  }
1557  }
1558 
1559  v = (0x5A << 24) | (argument << 8) | command;
1560  LOG_DEBUG("Command: 0x%08x", ((unsigned int)(v)));
1561  r = target_write_u32(private->bank->target,
1562  private->controller_address + OFFSET_EFC_FCR, v);
1563  if (r != ERROR_OK)
1564  LOG_DEBUG("Error Write failed");
1565  return r;
1566 }
1567 
1575 static int efc_perform_command(struct sam4_bank_private *private,
1576  unsigned int command,
1577  unsigned int argument,
1578  uint32_t *status)
1579 {
1580 
1581  int r;
1582  uint32_t v;
1583  int64_t ms_now, ms_end;
1584 
1585  /* default */
1586  if (status)
1587  *status = 0;
1588 
1589  r = efc_start_command(private, command, argument);
1590  if (r != ERROR_OK)
1591  return r;
1592 
1593  ms_end = 10000 + timeval_ms();
1594 
1595  do {
1596  r = efc_get_status(private, &v);
1597  if (r != ERROR_OK)
1598  return r;
1599  ms_now = timeval_ms();
1600  if (ms_now > ms_end) {
1601  /* error */
1602  LOG_ERROR("Command timeout");
1603  return ERROR_FAIL;
1604  }
1605  } while ((v & 1) == 0);
1606 
1607  /* error bits.. */
1608  if (status)
1609  *status = (v & 0x6);
1610  return ERROR_OK;
1611 
1612 }
1613 
1619 static int flashd_read_uid(struct sam4_bank_private *private)
1620 {
1621  int r;
1622  uint32_t v;
1623  int x;
1624  /* assume 0 */
1625  private->chip->cfg.unique_id[0] = 0;
1626  private->chip->cfg.unique_id[1] = 0;
1627  private->chip->cfg.unique_id[2] = 0;
1628  private->chip->cfg.unique_id[3] = 0;
1629 
1630  LOG_DEBUG("Begin");
1631  r = efc_start_command(private, AT91C_EFC_FCMD_STUI, 0);
1632  if (r < 0)
1633  return r;
1634 
1635  for (x = 0; x < 4; x++) {
1636  r = target_read_u32(private->chip->target,
1637  private->bank->base + (x * 4),
1638  &v);
1639  if (r < 0)
1640  return r;
1641  private->chip->cfg.unique_id[x] = v;
1642  }
1643 
1644  r = efc_perform_command(private, AT91C_EFC_FCMD_SPUI, 0, NULL);
1645  LOG_DEBUG("End: R=%d, id = 0x%08x, 0x%08x, 0x%08x, 0x%08x",
1646  r,
1647  (unsigned int)(private->chip->cfg.unique_id[0]),
1648  (unsigned int)(private->chip->cfg.unique_id[1]),
1649  (unsigned int)(private->chip->cfg.unique_id[2]),
1650  (unsigned int)(private->chip->cfg.unique_id[3]));
1651  return r;
1652 
1653 }
1654 
1659 static int flashd_erase_entire_bank(struct sam4_bank_private *private)
1660 {
1661  LOG_DEBUG("Here");
1662  return efc_perform_command(private, AT91C_EFC_FCMD_EA, 0, NULL);
1663 }
1664 
1672 static int flashd_erase_pages(struct sam4_bank_private *private,
1673  int first_page,
1674  int num_pages,
1675  uint32_t *status)
1676 {
1677  LOG_DEBUG("Here");
1678  uint8_t erase_pages;
1679  switch (num_pages) {
1680  case 4:
1681  erase_pages = 0x00;
1682  break;
1683  case 8:
1684  erase_pages = 0x01;
1685  break;
1686  case 16:
1687  erase_pages = 0x02;
1688  break;
1689  case 32:
1690  erase_pages = 0x03;
1691  break;
1692  default:
1693  erase_pages = 0x00;
1694  break;
1695  }
1696 
1697  /* AT91C_EFC_FCMD_EPA
1698  * According to the datasheet FARG[15:2] defines the page from which
1699  * the erase will start.This page must be modulo 4, 8, 16 or 32
1700  * according to the number of pages to erase. FARG[1:0] defines the
1701  * number of pages to be erased. Previously (firstpage << 2) was used
1702  * to conform to this, seems it should not be shifted...
1703  */
1704  return efc_perform_command(private,
1705  /* send Erase Page */
1707  (first_page) | erase_pages,
1708  status);
1709 }
1710 
1717 /* ------------------------------------------------------------------------------ */
1718 static int flashd_get_gpnvm(struct sam4_bank_private *private, unsigned int gpnvm, unsigned int *puthere)
1719 {
1720  uint32_t v;
1721  int r;
1722 
1723  LOG_DEBUG("Here");
1724  if (private->bank_number != 0) {
1725  LOG_ERROR("GPNVM only works with Bank0");
1726  return ERROR_FAIL;
1727  }
1728 
1729  if (gpnvm >= private->chip->details.n_gpnvms) {
1730  LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1731  gpnvm, private->chip->details.n_gpnvms);
1732  return ERROR_FAIL;
1733  }
1734 
1735  /* Get GPNVMs status */
1736  r = efc_perform_command(private, AT91C_EFC_FCMD_GFB, 0, NULL);
1737  if (r != ERROR_OK) {
1738  LOG_ERROR("Failed");
1739  return r;
1740  }
1741 
1742  r = efc_get_result(private, &v);
1743 
1744  if (puthere) {
1745  /* Check if GPNVM is set */
1746  /* get the bit and make it a 0/1 */
1747  *puthere = (v >> gpnvm) & 1;
1748  }
1749 
1750  return r;
1751 }
1752 
1759 static int flashd_clr_gpnvm(struct sam4_bank_private *private, unsigned int gpnvm)
1760 {
1761  int r;
1762  unsigned int v;
1763 
1764  LOG_DEBUG("Here");
1765  if (private->bank_number != 0) {
1766  LOG_ERROR("GPNVM only works with Bank0");
1767  return ERROR_FAIL;
1768  }
1769 
1770  if (gpnvm >= private->chip->details.n_gpnvms) {
1771  LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1772  gpnvm, private->chip->details.n_gpnvms);
1773  return ERROR_FAIL;
1774  }
1775 
1776  r = flashd_get_gpnvm(private, gpnvm, &v);
1777  if (r != ERROR_OK) {
1778  LOG_DEBUG("Failed: %d", r);
1779  return r;
1780  }
1782  LOG_DEBUG("End: %d", r);
1783  return r;
1784 }
1785 
1791 static int flashd_set_gpnvm(struct sam4_bank_private *private, unsigned int gpnvm)
1792 {
1793  int r;
1794  unsigned int v;
1795 
1796  if (private->bank_number != 0) {
1797  LOG_ERROR("GPNVM only works with Bank0");
1798  return ERROR_FAIL;
1799  }
1800 
1801  if (gpnvm >= private->chip->details.n_gpnvms) {
1802  LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1803  gpnvm, private->chip->details.n_gpnvms);
1804  return ERROR_FAIL;
1805  }
1806 
1807  r = flashd_get_gpnvm(private, gpnvm, &v);
1808  if (r != ERROR_OK)
1809  return r;
1810  if (v) {
1811  /* already set */
1812  r = ERROR_OK;
1813  } else {
1814  /* set it */
1816  }
1817  return r;
1818 }
1819 
1825 static int flashd_get_lock_bits(struct sam4_bank_private *private, uint32_t *v)
1826 {
1827  int r;
1828  LOG_DEBUG("Here");
1829  r = efc_perform_command(private, AT91C_EFC_FCMD_GLB, 0, NULL);
1830  if (r == ERROR_OK) {
1831  efc_get_result(private, v);
1832  efc_get_result(private, v);
1833  efc_get_result(private, v);
1834  r = efc_get_result(private, v);
1835  }
1836  LOG_DEBUG("End: %d", r);
1837  return r;
1838 }
1839 
1847 static int flashd_unlock(struct sam4_bank_private *private,
1848  unsigned int start_sector,
1849  unsigned int end_sector)
1850 {
1851  int r;
1852  uint32_t status;
1853  uint32_t pg;
1854  uint32_t pages_per_sector;
1855 
1856  pages_per_sector = private->sector_size / private->page_size;
1857 
1858  /* Unlock all pages */
1859  while (start_sector <= end_sector) {
1860  pg = start_sector * pages_per_sector;
1861 
1862  r = efc_perform_command(private, AT91C_EFC_FCMD_CLB, pg, &status);
1863  if (r != ERROR_OK)
1864  return r;
1865  start_sector++;
1866  }
1867 
1868  return ERROR_OK;
1869 }
1870 
1877 static int flashd_lock(struct sam4_bank_private *private,
1878  unsigned int start_sector,
1879  unsigned int end_sector)
1880 {
1881  uint32_t status;
1882  uint32_t pg;
1883  uint32_t pages_per_sector;
1884  int r;
1885 
1886  pages_per_sector = private->sector_size / private->page_size;
1887 
1888  /* Lock all pages */
1889  while (start_sector <= end_sector) {
1890  pg = start_sector * pages_per_sector;
1891 
1892  r = efc_perform_command(private, AT91C_EFC_FCMD_SLB, pg, &status);
1893  if (r != ERROR_OK)
1894  return r;
1895  start_sector++;
1896  }
1897  return ERROR_OK;
1898 }
1899 
1900 /****** END SAM4 CODE ********/
1901 
1902 /* begin helpful debug code */
1903 /* print the fieldname, the field value, in dec & hex, and return field value */
1904 static uint32_t sam4_reg_fieldname(struct sam4_chip *chip,
1905  const char *regname,
1906  uint32_t value,
1907  unsigned int shift,
1908  unsigned int width)
1909 {
1910  uint32_t v;
1911  int hwidth, dwidth;
1912 
1913 
1914  /* extract the field */
1915  v = value >> shift;
1916  v = v & ((1 << width)-1);
1917  if (width <= 16) {
1918  hwidth = 4;
1919  dwidth = 5;
1920  } else {
1921  hwidth = 8;
1922  dwidth = 12;
1923  }
1924 
1925  /* show the basics */
1926  LOG_USER_N("\t%*s: %*" PRIu32 " [0x%0*" PRIx32 "] ",
1927  REG_NAME_WIDTH, regname,
1928  dwidth, v,
1929  hwidth, v);
1930  return v;
1931 }
1932 
1933 static const char _unknown[] = "unknown";
1934 static const char *const eproc_names[] = {
1935  "Cortex-M7", /* 0 */
1936  "arm946es", /* 1 */
1937  "arm7tdmi", /* 2 */
1938  "Cortex-M3", /* 3 */
1939  "arm920t", /* 4 */
1940  "arm926ejs", /* 5 */
1941  "Cortex-A5", /* 6 */
1942  "Cortex-M4", /* 7 */
1943  _unknown, /* 8 */
1944  _unknown, /* 9 */
1945  _unknown, /* 10 */
1946  _unknown, /* 11 */
1947  _unknown, /* 12 */
1948  _unknown, /* 13 */
1949  _unknown, /* 14 */
1950  _unknown, /* 15 */
1951 };
1952 
1953 #define nvpsize2 nvpsize /* these two tables are identical */
1954 static const char *const nvpsize[] = {
1955  "none", /* 0 */
1956  "8K bytes", /* 1 */
1957  "16K bytes", /* 2 */
1958  "32K bytes", /* 3 */
1959  _unknown, /* 4 */
1960  "64K bytes", /* 5 */
1961  _unknown, /* 6 */
1962  "128K bytes", /* 7 */
1963  "160K bytes", /* 8 */
1964  "256K bytes", /* 9 */
1965  "512K bytes", /* 10 */
1966  _unknown, /* 11 */
1967  "1024K bytes", /* 12 */
1968  _unknown, /* 13 */
1969  "2048K bytes", /* 14 */
1970  _unknown, /* 15 */
1971 };
1972 
1973 static const char *const sramsize[] = {
1974  "48K Bytes", /* 0 */
1975  "1K Bytes", /* 1 */
1976  "2K Bytes", /* 2 */
1977  "6K Bytes", /* 3 */
1978  "112K Bytes", /* 4 */
1979  "4K Bytes", /* 5 */
1980  "80K Bytes", /* 6 */
1981  "160K Bytes", /* 7 */
1982  "8K Bytes", /* 8 */
1983  "16K Bytes", /* 9 */
1984  "32K Bytes", /* 10 */
1985  "64K Bytes", /* 11 */
1986  "128K Bytes", /* 12 */
1987  "256K Bytes", /* 13 */
1988  "96K Bytes", /* 14 */
1989  "512K Bytes", /* 15 */
1990 
1991 };
1992 
1993 static const struct archnames { unsigned int value; const char *name; } archnames[] = {
1994  { 0x19, "AT91SAM9xx Series" },
1995  { 0x29, "AT91SAM9XExx Series" },
1996  { 0x34, "AT91x34 Series" },
1997  { 0x37, "CAP7 Series" },
1998  { 0x39, "CAP9 Series" },
1999  { 0x3B, "CAP11 Series" },
2000  { 0x3C, "ATSAM4E" },
2001  { 0x40, "AT91x40 Series" },
2002  { 0x42, "AT91x42 Series" },
2003  { 0x43, "SAMG51 Series"
2004  },
2005  { 0x44, "SAMG55 Series (49-pin WLCSP)" },
2006  { 0x45, "SAMG55 Series (64-pin)" },
2007  { 0x47, "SAMG53 Series"
2008  },
2009  { 0x55, "AT91x55 Series" },
2010  { 0x60, "AT91SAM7Axx Series" },
2011  { 0x61, "AT91SAM7AQxx Series" },
2012  { 0x63, "AT91x63 Series" },
2013  { 0x64, "SAM4CxxC (100-pin version)" },
2014  { 0x66, "SAM4CxxE (144-pin version)" },
2015  { 0x70, "AT91SAM7Sxx Series" },
2016  { 0x71, "AT91SAM7XCxx Series" },
2017  { 0x72, "AT91SAM7SExx Series" },
2018  { 0x73, "AT91SAM7Lxx Series" },
2019  { 0x75, "AT91SAM7Xxx Series" },
2020  { 0x76, "AT91SAM7SLxx Series" },
2021  { 0x80, "ATSAM3UxC Series (100-pin version)" },
2022  { 0x81, "ATSAM3UxE Series (144-pin version)" },
2023  { 0x83, "ATSAM3A/SAM4A xC Series (100-pin version)"},
2024  { 0x84, "ATSAM3X/SAM4X xC Series (100-pin version)"},
2025  { 0x85, "ATSAM3X/SAM4X xE Series (144-pin version)"},
2026  { 0x86, "ATSAM3X/SAM4X xG Series (208/217-pin version)" },
2027  { 0x88, "ATSAM3S/SAM4S xA Series (48-pin version)" },
2028  { 0x89, "ATSAM3S/SAM4S xB Series (64-pin version)" },
2029  { 0x8A, "ATSAM3S/SAM4S xC Series (100-pin version)"},
2030  { 0x92, "AT91x92 Series" },
2031  { 0x93, "ATSAM3NxA Series (48-pin version)" },
2032  { 0x94, "ATSAM3NxB Series (64-pin version)" },
2033  { 0x95, "ATSAM3NxC Series (100-pin version)" },
2034  { 0x98, "ATSAM3SDxA Series (48-pin version)" },
2035  { 0x99, "ATSAM3SDxB Series (64-pin version)" },
2036  { 0x9A, "ATSAM3SDxC Series (100-pin version)" },
2037  { 0xA5, "ATSAM5A" },
2038  { 0xF0, "AT75Cxx Series" },
2039  { -1, NULL },
2040 };
2041 
2042 static const char *const nvptype[] = {
2043  "rom", /* 0 */
2044  "romless or onchip flash", /* 1 */
2045  "embedded flash memory",/* 2 */
2046  "rom(nvpsiz) + embedded flash (nvpsiz2)", /* 3 */
2047  "sram emulating flash", /* 4 */
2048  _unknown, /* 5 */
2049  _unknown, /* 6 */
2050  _unknown, /* 7 */
2051 };
2052 
2053 static const char *_yes_or_no(uint32_t v)
2054 {
2055  if (v)
2056  return "YES";
2057  else
2058  return "NO";
2059 }
2060 
2061 static const char *const _rc_freq[] = {
2062  "4 MHz", "8 MHz", "12 MHz", "reserved"
2063 };
2064 
2065 static void sam4_explain_ckgr_mor(struct sam4_chip *chip)
2066 {
2067  uint32_t v;
2068  uint32_t rcen;
2069 
2070  v = sam4_reg_fieldname(chip, "MOSCXTEN", chip->cfg.CKGR_MOR, 0, 1);
2071  LOG_USER("(main xtal enabled: %s)", _yes_or_no(v));
2072  v = sam4_reg_fieldname(chip, "MOSCXTBY", chip->cfg.CKGR_MOR, 1, 1);
2073  LOG_USER("(main osc bypass: %s)", _yes_or_no(v));
2074  rcen = sam4_reg_fieldname(chip, "MOSCRCEN", chip->cfg.CKGR_MOR, 3, 1);
2075  LOG_USER("(onchip RC-OSC enabled: %s)", _yes_or_no(rcen));
2076  v = sam4_reg_fieldname(chip, "MOSCRCF", chip->cfg.CKGR_MOR, 4, 3);
2077  LOG_USER("(onchip RC-OSC freq: %s)", _rc_freq[v]);
2078 
2079  chip->cfg.rc_freq = 0;
2080  if (rcen) {
2081  switch (v) {
2082  case 0:
2083  chip->cfg.rc_freq = 4 * 1000 * 1000;
2084  break;
2085  case 1:
2086  chip->cfg.rc_freq = 8 * 1000 * 1000;
2087  break;
2088  case 2:
2089  chip->cfg.rc_freq = 12 * 1000 * 1000;
2090  break;
2091  default:
2092  chip->cfg.rc_freq = 0;
2093  break;
2094  }
2095  }
2096 
2097  v = sam4_reg_fieldname(chip, "MOSCXTST", chip->cfg.CKGR_MOR, 8, 8);
2098  LOG_USER("(startup clks, time= %f uSecs)",
2099  ((float)(v * 1000000)) / ((float)(chip->cfg.slow_freq)));
2100  v = sam4_reg_fieldname(chip, "MOSCSEL", chip->cfg.CKGR_MOR, 24, 1);
2101  LOG_USER("(mainosc source: %s)",
2102  v ? "external xtal" : "internal RC");
2103 
2104  v = sam4_reg_fieldname(chip, "CFDEN", chip->cfg.CKGR_MOR, 25, 1);
2105  LOG_USER("(clock failure enabled: %s)",
2106  _yes_or_no(v));
2107 }
2108 
2109 static void sam4_explain_chipid_cidr(struct sam4_chip *chip)
2110 {
2111  int x;
2112  uint32_t v;
2113  const char *cp;
2114 
2115  sam4_reg_fieldname(chip, "Version", chip->cfg.CHIPID_CIDR, 0, 5);
2116  LOG_USER_N("\n");
2117 
2118  v = sam4_reg_fieldname(chip, "EPROC", chip->cfg.CHIPID_CIDR, 5, 3);
2119  LOG_USER("%s", eproc_names[v]);
2120 
2121  v = sam4_reg_fieldname(chip, "NVPSIZE", chip->cfg.CHIPID_CIDR, 8, 4);
2122  LOG_USER("%s", nvpsize[v]);
2123 
2124  v = sam4_reg_fieldname(chip, "NVPSIZE2", chip->cfg.CHIPID_CIDR, 12, 4);
2125  LOG_USER("%s", nvpsize2[v]);
2126 
2127  v = sam4_reg_fieldname(chip, "SRAMSIZE", chip->cfg.CHIPID_CIDR, 16, 4);
2128  LOG_USER("%s", sramsize[v]);
2129 
2130  v = sam4_reg_fieldname(chip, "ARCH", chip->cfg.CHIPID_CIDR, 20, 8);
2131  cp = _unknown;
2132  for (x = 0; archnames[x].name; x++) {
2133  if (v == archnames[x].value) {
2134  cp = archnames[x].name;
2135  break;
2136  }
2137  }
2138 
2139  LOG_USER("%s", cp);
2140 
2141  v = sam4_reg_fieldname(chip, "NVPTYP", chip->cfg.CHIPID_CIDR, 28, 3);
2142  LOG_USER("%s", nvptype[v]);
2143 
2144  v = sam4_reg_fieldname(chip, "EXTID", chip->cfg.CHIPID_CIDR, 31, 1);
2145  LOG_USER("(exists: %s)", _yes_or_no(v));
2146 }
2147 
2148 static void sam4_explain_ckgr_mcfr(struct sam4_chip *chip)
2149 {
2150  uint32_t v;
2151 
2152  v = sam4_reg_fieldname(chip, "MAINFRDY", chip->cfg.CKGR_MCFR, 16, 1);
2153  LOG_USER("(main ready: %s)", _yes_or_no(v));
2154 
2155  v = sam4_reg_fieldname(chip, "MAINF", chip->cfg.CKGR_MCFR, 0, 16);
2156 
2157  v = (v * chip->cfg.slow_freq) / 16;
2158  chip->cfg.mainosc_freq = v;
2159 
2160  LOG_USER("(%3.03f Mhz (%" PRIu32 ".%03" PRIu32 "khz slowclk)",
2161  _tomhz(v),
2162  (uint32_t)(chip->cfg.slow_freq / 1000),
2163  (uint32_t)(chip->cfg.slow_freq % 1000));
2164 }
2165 
2166 static void sam4_explain_ckgr_plla(struct sam4_chip *chip)
2167 {
2168  uint32_t mula, diva;
2169 
2170  diva = sam4_reg_fieldname(chip, "DIVA", chip->cfg.CKGR_PLLAR, 0, 8);
2171  LOG_USER_N("\n");
2172  mula = sam4_reg_fieldname(chip, "MULA", chip->cfg.CKGR_PLLAR, 16, 11);
2173  LOG_USER_N("\n");
2174  chip->cfg.plla_freq = 0;
2175  if (mula == 0)
2176  LOG_USER("\tPLLA Freq: (Disabled,mula = 0)");
2177  else if (diva == 0)
2178  LOG_USER("\tPLLA Freq: (Disabled,diva = 0)");
2179  else if (diva >= 1) {
2180  chip->cfg.plla_freq = (chip->cfg.mainosc_freq * (mula + 1) / diva);
2181  LOG_USER("\tPLLA Freq: %3.03f MHz",
2182  _tomhz(chip->cfg.plla_freq));
2183  }
2184 }
2185 
2186 static void sam4_explain_mckr(struct sam4_chip *chip)
2187 {
2188  uint32_t css, pres, fin = 0;
2189  int pdiv = 0;
2190  const char *cp = NULL;
2191 
2192  css = sam4_reg_fieldname(chip, "CSS", chip->cfg.PMC_MCKR, 0, 2);
2193  switch (css & 3) {
2194  case 0:
2195  fin = chip->cfg.slow_freq;
2196  cp = "slowclk";
2197  break;
2198  case 1:
2199  fin = chip->cfg.mainosc_freq;
2200  cp = "mainosc";
2201  break;
2202  case 2:
2203  fin = chip->cfg.plla_freq;
2204  cp = "plla";
2205  break;
2206  case 3:
2207  if (chip->cfg.CKGR_UCKR & (1 << 16)) {
2208  fin = 480 * 1000 * 1000;
2209  cp = "upll";
2210  } else {
2211  fin = 0;
2212  cp = "upll (*ERROR* UPLL is disabled)";
2213  }
2214  break;
2215  default:
2216  assert(0);
2217  break;
2218  }
2219 
2220  LOG_USER("%s (%3.03f Mhz)",
2221  cp,
2222  _tomhz(fin));
2223  pres = sam4_reg_fieldname(chip, "PRES", chip->cfg.PMC_MCKR, 4, 3);
2224  switch (pres & 0x07) {
2225  case 0:
2226  pdiv = 1;
2227  cp = "selected clock";
2228  break;
2229  case 1:
2230  pdiv = 2;
2231  cp = "clock/2";
2232  break;
2233  case 2:
2234  pdiv = 4;
2235  cp = "clock/4";
2236  break;
2237  case 3:
2238  pdiv = 8;
2239  cp = "clock/8";
2240  break;
2241  case 4:
2242  pdiv = 16;
2243  cp = "clock/16";
2244  break;
2245  case 5:
2246  pdiv = 32;
2247  cp = "clock/32";
2248  break;
2249  case 6:
2250  pdiv = 64;
2251  cp = "clock/64";
2252  break;
2253  case 7:
2254  pdiv = 6;
2255  cp = "clock/6";
2256  break;
2257  default:
2258  assert(0);
2259  break;
2260  }
2261  LOG_USER("(%s)", cp);
2262  fin = fin / pdiv;
2263  /* sam4 has a *SINGLE* clock - */
2264  /* other at91 series parts have divisors for these. */
2265  chip->cfg.cpu_freq = fin;
2266  chip->cfg.mclk_freq = fin;
2267  chip->cfg.fclk_freq = fin;
2268  LOG_USER("\t\tResult CPU Freq: %3.03f",
2269  _tomhz(fin));
2270 }
2271 
2272 #if 0
2273 static struct sam4_chip *target2sam4(struct target *target)
2274 {
2275  struct sam4_chip *chip;
2276 
2277  if (!target)
2278  return NULL;
2279 
2280  chip = all_sam4_chips;
2281  while (chip) {
2282  if (chip->target == target)
2283  break; /* return below */
2284  else
2285  chip = chip->next;
2286  }
2287  return chip;
2288 }
2289 #endif
2290 
2291 static uint32_t *sam4_get_reg_ptr(struct sam4_cfg *cfg, const struct sam4_reg_list *list)
2292 {
2293  /* this function exists to help */
2294  /* keep funky offsetof() errors */
2295  /* and casting from causing bugs */
2296 
2297  /* By using prototypes - we can detect what would */
2298  /* be casting errors. */
2299 
2300  return (uint32_t *)(void *)(((char *)(cfg)) + list->struct_offset);
2301 }
2302 
2303 
2304 #define SAM4_ENTRY(NAME, FUNC) { .address = SAM4_ ## NAME, .struct_offset = offsetof( \
2305  struct sam4_cfg, \
2306  NAME), # NAME, FUNC }
2307 static const struct sam4_reg_list sam4_all_regs[] = {
2310  SAM4_ENTRY(CKGR_PLLAR, sam4_explain_ckgr_plla),
2311  SAM4_ENTRY(CKGR_UCKR, NULL),
2312  SAM4_ENTRY(PMC_FSMR, NULL),
2313  SAM4_ENTRY(PMC_FSPR, NULL),
2314  SAM4_ENTRY(PMC_IMR, NULL),
2316  SAM4_ENTRY(PMC_PCK0, NULL),
2317  SAM4_ENTRY(PMC_PCK1, NULL),
2318  SAM4_ENTRY(PMC_PCK2, NULL),
2319  SAM4_ENTRY(PMC_PCSR, NULL),
2320  SAM4_ENTRY(PMC_SCSR, NULL),
2321  SAM4_ENTRY(PMC_SR, NULL),
2322  SAM4_ENTRY(CHIPID_CIDR, sam4_explain_chipid_cidr),
2323  SAM4_ENTRY(CHIPID_EXID, NULL),
2324  /* TERMINATE THE LIST */
2325  { .name = NULL }
2326 };
2327 #undef SAM4_ENTRY
2328 
2330 {
2331  return bank->driver_priv;
2332 }
2333 
2338 static const struct sam4_reg_list *sam4_get_reg(struct sam4_chip *chip, uint32_t *goes_here)
2339 {
2340  const struct sam4_reg_list *reg;
2341 
2342  reg = &(sam4_all_regs[0]);
2343  while (reg->name) {
2344  uint32_t *possible;
2345 
2346  /* calculate where this one go.. */
2347  /* it is "possibly" this register. */
2348 
2349  possible = ((uint32_t *)(void *)(((char *)(&(chip->cfg))) + reg->struct_offset));
2350 
2351  /* well? Is it this register */
2352  if (possible == goes_here) {
2353  /* Jump for joy! */
2354  return reg;
2355  }
2356 
2357  /* next... */
2358  reg++;
2359  }
2360  /* This is *TOTAL*PANIC* - we are totally screwed. */
2361  LOG_ERROR("INVALID SAM4 REGISTER");
2362  return NULL;
2363 }
2364 
2365 static int sam4_read_this_reg(struct sam4_chip *chip, uint32_t *goes_here)
2366 {
2367  const struct sam4_reg_list *reg;
2368  int r;
2369 
2370  reg = sam4_get_reg(chip, goes_here);
2371  if (!reg)
2372  return ERROR_FAIL;
2373 
2374  r = target_read_u32(chip->target, reg->address, goes_here);
2375  if (r != ERROR_OK) {
2376  LOG_ERROR("Cannot read SAM4 register: %s @ 0x%08" PRIx32 ", Err: %d",
2377  reg->name, reg->address, r);
2378  }
2379  return r;
2380 }
2381 
2382 static int sam4_read_all_regs(struct sam4_chip *chip)
2383 {
2384  int r;
2385  const struct sam4_reg_list *reg;
2386 
2387  reg = &(sam4_all_regs[0]);
2388  while (reg->name) {
2389  r = sam4_read_this_reg(chip,
2390  sam4_get_reg_ptr(&(chip->cfg), reg));
2391  if (r != ERROR_OK) {
2392  LOG_ERROR("Cannot read SAM4 register: %s @ 0x%08" PRIx32 ", Error: %d",
2393  reg->name, reg->address, r);
2394  return r;
2395  }
2396  reg++;
2397  }
2398 
2399  return ERROR_OK;
2400 }
2401 
2402 static int sam4_get_info(struct sam4_chip *chip)
2403 {
2404  const struct sam4_reg_list *reg;
2405  uint32_t regval;
2406  int r;
2407 
2408  r = sam4_read_all_regs(chip);
2409  if (r != ERROR_OK)
2410  return r;
2411 
2412  reg = &(sam4_all_regs[0]);
2413  while (reg->name) {
2414  /* display all regs */
2415  LOG_DEBUG("Start: %s", reg->name);
2416  regval = *sam4_get_reg_ptr(&(chip->cfg), reg);
2417  LOG_USER("%*s: [0x%08" PRIx32 "] -> 0x%08" PRIx32,
2419  reg->name,
2420  reg->address,
2421  regval);
2422  if (reg->explain_func)
2423  (*(reg->explain_func))(chip);
2424  LOG_DEBUG("End: %s", reg->name);
2425  reg++;
2426  }
2427  LOG_USER(" rc-osc: %3.03f MHz", _tomhz(chip->cfg.rc_freq));
2428  LOG_USER(" mainosc: %3.03f MHz", _tomhz(chip->cfg.mainosc_freq));
2429  LOG_USER(" plla: %3.03f MHz", _tomhz(chip->cfg.plla_freq));
2430  LOG_USER(" cpu-freq: %3.03f MHz", _tomhz(chip->cfg.cpu_freq));
2431  LOG_USER("mclk-freq: %3.03f MHz", _tomhz(chip->cfg.mclk_freq));
2432 
2433  LOG_USER(" UniqueId: 0x%08" PRIx32 " 0x%08" PRIx32 " 0x%08" PRIx32 " 0x%08"PRIx32,
2434  chip->cfg.unique_id[0],
2435  chip->cfg.unique_id[1],
2436  chip->cfg.unique_id[2],
2437  chip->cfg.unique_id[3]);
2438 
2439  return ERROR_OK;
2440 }
2441 
2443 {
2444  int r;
2445  uint32_t v[4] = {0};
2446  unsigned int x;
2447  struct sam4_bank_private *private;
2448 
2449  LOG_DEBUG("Begin");
2450  if (bank->target->state != TARGET_HALTED) {
2451  LOG_ERROR("Target not halted");
2452  return ERROR_TARGET_NOT_HALTED;
2453  }
2454 
2455  private = get_sam4_bank_private(bank);
2456  if (!private) {
2457  LOG_ERROR("no private for this bank?");
2458  return ERROR_FAIL;
2459  }
2460  if (!(private->probed))
2462 
2463  r = flashd_get_lock_bits(private, v);
2464  if (r != ERROR_OK) {
2465  LOG_DEBUG("Failed: %d", r);
2466  return r;
2467  }
2468 
2469  for (x = 0; x < private->nsectors; x++)
2470  bank->sectors[x].is_protected = (!!(v[x >> 5] & (1 << (x % 32))));
2471  LOG_DEBUG("Done");
2472  return ERROR_OK;
2473 }
2474 
2475 FLASH_BANK_COMMAND_HANDLER(sam4_flash_bank_command)
2476 {
2477  struct sam4_chip *chip;
2478 
2479  chip = all_sam4_chips;
2480 
2481  /* is this an existing chip? */
2482  while (chip) {
2483  if (chip->target == bank->target)
2484  break;
2485  chip = chip->next;
2486  }
2487 
2488  if (!chip) {
2489  /* this is a *NEW* chip */
2490  chip = calloc(1, sizeof(struct sam4_chip));
2491  if (!chip) {
2492  LOG_ERROR("NO RAM!");
2493  return ERROR_FAIL;
2494  }
2495  chip->target = bank->target;
2496  /* insert at head */
2497  chip->next = all_sam4_chips;
2498  all_sam4_chips = chip;
2499  chip->target = bank->target;
2500  /* assumption is this runs at 32khz */
2501  chip->cfg.slow_freq = 32768;
2502  chip->probed = false;
2503  }
2504 
2505  switch (bank->base) {
2506  /* at91sam4s series only has bank 0*/
2507  /* at91sam4sd series has the same address for bank 0 (FLASH_BANK0_BASE_SD)*/
2508  case FLASH_BANK_BASE_S:
2509  case FLASH_BANK_BASE_C:
2510  bank->driver_priv = &chip->details.bank[0];
2511  bank->bank_number = 0;
2512  chip->details.bank[0].chip = chip;
2513  chip->details.bank[0].bank = bank;
2514  break;
2515 
2516  /* Bank 1 of at91sam4sd/at91sam4c32 series */
2519  case FLASH_BANK1_BASE_C32:
2520  bank->driver_priv = &chip->details.bank[1];
2521  bank->bank_number = 1;
2522  chip->details.bank[1].chip = chip;
2523  chip->details.bank[1].bank = bank;
2524  break;
2525 
2526  default:
2527  LOG_ERROR("Address " TARGET_ADDR_FMT " invalid bank address (try 0x%08x"
2528  "[at91sam4s series] )",
2529  bank->base,
2531  return ERROR_FAIL;
2532  }
2533 
2534  /* we initialize after probing. */
2535  return ERROR_OK;
2536 }
2537 
2544 {
2545  struct sam4_chip *chip = all_sam4_chips;
2546  while (chip) {
2547  struct sam4_chip *next = chip->next;
2548  free(chip);
2549  chip = next;
2550  }
2551  all_sam4_chips = NULL;
2552 }
2553 
2554 static int sam4_get_details(struct sam4_bank_private *private)
2555 {
2556  const struct sam4_chip_details *details;
2557  struct sam4_chip *chip;
2558  struct flash_bank *saved_banks[SAM4_MAX_FLASH_BANKS];
2559  unsigned int x;
2560 
2561  LOG_DEBUG("Begin");
2562  details = all_sam4_details;
2563  while (details->name) {
2564  /* Compare cidr without version bits */
2565  if (details->chipid_cidr == (private->chip->cfg.CHIPID_CIDR & 0xFFFFFFE0))
2566  break;
2567  else
2568  details++;
2569  }
2570  if (!details->name) {
2571  LOG_ERROR("SAM4 ChipID 0x%08x not found in table (perhaps you can ID this chip?)",
2572  (unsigned int)(private->chip->cfg.CHIPID_CIDR));
2573  /* Help the victim, print details about the chip */
2574  LOG_INFO("SAM4 CHIPID_CIDR: 0x%08" PRIx32 " decodes as follows",
2575  private->chip->cfg.CHIPID_CIDR);
2576  sam4_explain_chipid_cidr(private->chip);
2577  return ERROR_FAIL;
2578  } else {
2579  LOG_DEBUG("SAM4 Found chip %s, CIDR 0x%08" PRIx32, details->name, details->chipid_cidr);
2580  }
2581 
2582  /* DANGER: THERE ARE DRAGONS HERE */
2583 
2584  /* get our chip - it is going */
2585  /* to be over-written shortly */
2586  chip = private->chip;
2587 
2588  /* Note that, in reality: */
2589  /* */
2590  /* private = &(chip->details.bank[0]) */
2591  /* or private = &(chip->details.bank[1]) */
2592  /* */
2593 
2594  /* save the "bank" pointers */
2595  for (x = 0; x < SAM4_MAX_FLASH_BANKS; x++)
2596  saved_banks[x] = chip->details.bank[x].bank;
2597 
2598  /* Overwrite the "details" structure. */
2599  memcpy(&(private->chip->details),
2600  details,
2601  sizeof(private->chip->details));
2602 
2603  /* now fix the ghosted pointers */
2604  for (x = 0; x < SAM4_MAX_FLASH_BANKS; x++) {
2605  chip->details.bank[x].chip = chip;
2606  chip->details.bank[x].bank = saved_banks[x];
2607  }
2608 
2609  /* update the *BANK*SIZE* */
2610 
2611  LOG_DEBUG("End");
2612  return ERROR_OK;
2613 }
2614 
2615 static int sam4_info(struct flash_bank *bank, struct command_invocation *cmd)
2616 {
2617  struct sam4_bank_private *private;
2618  int k = bank->size / 1024;
2619 
2620  private = get_sam4_bank_private(bank);
2621  if (!private)
2622  return ERROR_FAIL;
2623 
2624  command_print_sameline(cmd, "%s bank %d: %d kB at " TARGET_ADDR_FMT,
2625  private->chip->details.name,
2626  private->bank_number,
2627  k,
2628  bank->base);
2629 
2630  return ERROR_OK;
2631 }
2632 
2633 static int sam4_probe(struct flash_bank *bank)
2634 {
2635  int r;
2636  struct sam4_bank_private *private;
2637 
2638 
2639  LOG_DEBUG("Begin: Bank: %u", bank->bank_number);
2640  if (bank->target->state != TARGET_HALTED) {
2641  LOG_ERROR("Target not halted");
2642  return ERROR_TARGET_NOT_HALTED;
2643  }
2644 
2645  private = get_sam4_bank_private(bank);
2646  if (!private) {
2647  LOG_ERROR("Invalid/unknown bank number");
2648  return ERROR_FAIL;
2649  }
2650 
2651  r = sam4_read_all_regs(private->chip);
2652  if (r != ERROR_OK)
2653  return r;
2654 
2655  LOG_DEBUG("Here");
2656  if (private->chip->probed)
2657  r = sam4_get_info(private->chip);
2658  else
2659  r = sam4_get_details(private);
2660  if (r != ERROR_OK)
2661  return r;
2662 
2663  /* update the flash bank size */
2664  for (unsigned int x = 0; x < SAM4_MAX_FLASH_BANKS; x++) {
2665  if (bank->base == private->chip->details.bank[x].base_address) {
2666  bank->size = private->chip->details.bank[x].size_bytes;
2667  LOG_DEBUG("SAM4 Set flash bank to " TARGET_ADDR_FMT " - "
2668  TARGET_ADDR_FMT ", idx %d", bank->base,
2669  bank->base + bank->size, x);
2670  break;
2671  }
2672  }
2673 
2674  if (!bank->sectors) {
2675  bank->sectors = calloc(private->nsectors, (sizeof((bank->sectors)[0])));
2676  if (!bank->sectors) {
2677  LOG_ERROR("No memory!");
2678  return ERROR_FAIL;
2679  }
2680  bank->num_sectors = private->nsectors;
2681 
2682  for (unsigned int x = 0; x < bank->num_sectors; x++) {
2683  bank->sectors[x].size = private->sector_size;
2684  bank->sectors[x].offset = x * (private->sector_size);
2685  /* mark as unknown */
2686  bank->sectors[x].is_erased = -1;
2687  bank->sectors[x].is_protected = -1;
2688  }
2689  }
2690 
2691  private->probed = true;
2692 
2693  r = sam4_protect_check(bank);
2694  if (r != ERROR_OK)
2695  return r;
2696 
2697  LOG_DEBUG("Bank = %d, nbanks = %d",
2698  private->bank_number, private->chip->details.n_banks);
2699  if ((private->bank_number + 1) == private->chip->details.n_banks) {
2700  /* read unique id, */
2701  /* it appears to be associated with the *last* flash bank. */
2702  flashd_read_uid(private);
2703  }
2704 
2705  return r;
2706 }
2707 
2708 static int sam4_auto_probe(struct flash_bank *bank)
2709 {
2710  struct sam4_bank_private *private;
2711 
2712  private = get_sam4_bank_private(bank);
2713  if (private && private->probed)
2714  return ERROR_OK;
2715 
2716  return sam4_probe(bank);
2717 }
2718 
2719 static int sam4_erase(struct flash_bank *bank, unsigned int first,
2720  unsigned int last)
2721 {
2722  struct sam4_bank_private *private;
2723  int r;
2724  int page_count;
2725  /*16 pages equals 8KB - Same size as a lock region*/
2726  page_count = 16;
2727  uint32_t status;
2728 
2729  LOG_DEBUG("Here");
2730  if (bank->target->state != TARGET_HALTED) {
2731  LOG_ERROR("Target not halted");
2732  return ERROR_TARGET_NOT_HALTED;
2733  }
2734 
2735  r = sam4_auto_probe(bank);
2736  if (r != ERROR_OK) {
2737  LOG_DEBUG("Here,r=%d", r);
2738  return r;
2739  }
2740 
2741  private = get_sam4_bank_private(bank);
2742  if (!(private->probed))
2744 
2745  if ((first == 0) && ((last + 1) == private->nsectors)) {
2746  /* whole chip */
2747  LOG_DEBUG("Here");
2748  return flashd_erase_entire_bank(private);
2749  }
2750  LOG_INFO("sam4 does not auto-erase while programming (Erasing relevant sectors)");
2751  LOG_INFO("sam4 First: 0x%08x Last: 0x%08x", first, last);
2752  for (unsigned int i = first; i <= last; i++) {
2753  /*16 pages equals 8KB - Same size as a lock region*/
2754  r = flashd_erase_pages(private, (i * page_count), page_count, &status);
2755  LOG_INFO("Erasing sector: 0x%08x", i);
2756  if (r != ERROR_OK)
2757  LOG_ERROR("SAM4: Error performing Erase page @ lock region number %u",
2758  i);
2759  if (status & (1 << 2)) {
2760  LOG_ERROR("SAM4: Lock Region %u is locked", i);
2761  return ERROR_FAIL;
2762  }
2763  if (status & (1 << 1)) {
2764  LOG_ERROR("SAM4: Flash Command error @lock region %u", i);
2765  return ERROR_FAIL;
2766  }
2767  }
2768 
2769  return ERROR_OK;
2770 }
2771 
2772 static int sam4_protect(struct flash_bank *bank, int set, unsigned int first,
2773  unsigned int last)
2774 {
2775  struct sam4_bank_private *private;
2776  int r;
2777 
2778  LOG_DEBUG("Here");
2779  if (bank->target->state != TARGET_HALTED) {
2780  LOG_ERROR("Target not halted");
2781  return ERROR_TARGET_NOT_HALTED;
2782  }
2783 
2784  private = get_sam4_bank_private(bank);
2785  if (!(private->probed))
2787 
2788  if (set)
2789  r = flashd_lock(private, first, last);
2790  else
2791  r = flashd_unlock(private, first, last);
2792  LOG_DEBUG("End: r=%d", r);
2793 
2794  return r;
2795 
2796 }
2797 
2798 static int sam4_page_read(struct sam4_bank_private *private, unsigned int pagenum, uint8_t *buf)
2799 {
2800  uint32_t adr;
2801  int r;
2802 
2803  adr = pagenum * private->page_size;
2804  adr = adr + private->base_address;
2805 
2806  r = target_read_memory(private->chip->target,
2807  adr,
2808  4, /* THIS*MUST*BE* in 32bit values */
2809  private->page_size / 4,
2810  buf);
2811  if (r != ERROR_OK)
2812  LOG_ERROR("SAM4: Flash program failed to read page phys address: 0x%08x",
2813  (unsigned int)(adr));
2814  return r;
2815 }
2816 
2817 static int sam4_set_wait(struct sam4_bank_private *private)
2818 {
2819  uint32_t fmr; /* EEFC Flash Mode Register */
2820  int r;
2821 
2822  /* Get flash mode register value */
2823  r = target_read_u32(private->chip->target, private->controller_address, &fmr);
2824  if (r != ERROR_OK) {
2825  LOG_ERROR("Error Read failed: read flash mode register");
2826  return r;
2827  }
2828 
2829  /* Clear flash wait state field */
2830  fmr &= 0xfffff0ff;
2831 
2832  /* set FWS (flash wait states) field in the FMR (flash mode register) */
2833  fmr |= (private->flash_wait_states << 8);
2834 
2835  LOG_DEBUG("Flash Mode: 0x%08x", ((unsigned int)(fmr)));
2836  r = target_write_u32(private->bank->target, private->controller_address, fmr);
2837  if (r != ERROR_OK)
2838  LOG_ERROR("Error Write failed: set flash mode register");
2839 
2840  return r;
2841 }
2842 
2843 static int sam4_page_write(struct sam4_bank_private *private, unsigned int pagenum, const uint8_t *buf)
2844 {
2845  uint32_t adr;
2846  uint32_t status;
2847  int r;
2848 
2849  adr = pagenum * private->page_size;
2850  adr = (adr + private->base_address);
2851 
2852  /* 1st sector 8kBytes - page 0 - 15*/
2853  /* 2nd sector 8kBytes - page 16 - 30*/
2854  /* 3rd sector 48kBytes - page 31 - 127*/
2855  LOG_DEBUG("Wr Page %u @ phys address: 0x%08x", pagenum, (unsigned int)(adr));
2856  r = target_write_memory(private->chip->target,
2857  adr,
2858  4, /* THIS*MUST*BE* in 32bit values */
2859  private->page_size / 4,
2860  buf);
2861  if (r != ERROR_OK) {
2862  LOG_ERROR("SAM4: Failed to write (buffer) page at phys address 0x%08x",
2863  (unsigned int)(adr));
2864  return r;
2865  }
2866 
2867  r = efc_perform_command(private,
2868  /* send Erase & Write Page */
2869  AT91C_EFC_FCMD_WP, /*AT91C_EFC_FCMD_EWP only works on first two 8kb sectors*/
2870  pagenum,
2871  &status);
2872 
2873  if (r != ERROR_OK)
2874  LOG_ERROR("SAM4: Error performing Write page @ phys address 0x%08x",
2875  (unsigned int)(adr));
2876  if (status & (1 << 2)) {
2877  LOG_ERROR("SAM4: Page @ Phys address 0x%08x is locked", (unsigned int)(adr));
2878  return ERROR_FAIL;
2879  }
2880  if (status & (1 << 1)) {
2881  LOG_ERROR("SAM4: Flash Command error @phys address 0x%08x", (unsigned int)(adr));
2882  return ERROR_FAIL;
2883  }
2884  return ERROR_OK;
2885 }
2886 
2887 static int sam4_write(struct flash_bank *bank,
2888  const uint8_t *buffer,
2889  uint32_t offset,
2890  uint32_t count)
2891 {
2892  int n;
2893  unsigned int page_cur;
2894  unsigned int page_end;
2895  int r;
2896  unsigned int page_offset;
2897  struct sam4_bank_private *private;
2898  uint8_t *pagebuffer;
2899 
2900  /* in case we bail further below, set this to null */
2901  pagebuffer = NULL;
2902 
2903  /* ignore dumb requests */
2904  if (count == 0) {
2905  r = ERROR_OK;
2906  goto done;
2907  }
2908 
2909  if (bank->target->state != TARGET_HALTED) {
2910  LOG_ERROR("Target not halted");
2912  goto done;
2913  }
2914 
2915  private = get_sam4_bank_private(bank);
2916  if (!(private->probed)) {
2918  goto done;
2919  }
2920 
2921  if ((offset + count) > private->size_bytes) {
2922  LOG_ERROR("Flash write error - past end of bank");
2923  LOG_ERROR(" offset: 0x%08x, count 0x%08x, BankEnd: 0x%08x",
2924  (unsigned int)(offset),
2925  (unsigned int)(count),
2926  (unsigned int)(private->size_bytes));
2927  r = ERROR_FAIL;
2928  goto done;
2929  }
2930 
2931  pagebuffer = malloc(private->page_size);
2932  if (!pagebuffer) {
2933  LOG_ERROR("No memory for %d Byte page buffer", (int)(private->page_size));
2934  r = ERROR_FAIL;
2935  goto done;
2936  }
2937 
2938  r = sam4_set_wait(private);
2939  if (r != ERROR_OK)
2940  goto done;
2941 
2942  /* what page do we start & end in? */
2943  page_cur = offset / private->page_size;
2944  page_end = (offset + count - 1) / private->page_size;
2945 
2946  LOG_DEBUG("Offset: 0x%08x, Count: 0x%08x", (unsigned int)(offset), (unsigned int)(count));
2947  LOG_DEBUG("Page start: %d, Page End: %d", (int)(page_cur), (int)(page_end));
2948 
2949  /* Special case: all one page */
2950  /* */
2951  /* Otherwise: */
2952  /* (1) non-aligned start */
2953  /* (2) body pages */
2954  /* (3) non-aligned end. */
2955 
2956  /* Handle special case - all one page. */
2957  if (page_cur == page_end) {
2958  LOG_DEBUG("Special case, all in one page");
2959  r = sam4_page_read(private, page_cur, pagebuffer);
2960  if (r != ERROR_OK)
2961  goto done;
2962 
2963  page_offset = (offset & (private->page_size-1));
2964  memcpy(pagebuffer + page_offset,
2965  buffer,
2966  count);
2967 
2968  r = sam4_page_write(private, page_cur, pagebuffer);
2969  if (r != ERROR_OK)
2970  goto done;
2971  r = ERROR_OK;
2972  goto done;
2973  }
2974 
2975  /* non-aligned start */
2976  page_offset = offset & (private->page_size - 1);
2977  if (page_offset) {
2978  LOG_DEBUG("Not-Aligned start");
2979  /* read the partial */
2980  r = sam4_page_read(private, page_cur, pagebuffer);
2981  if (r != ERROR_OK)
2982  goto done;
2983 
2984  /* over-write with new data */
2985  n = (private->page_size - page_offset);
2986  memcpy(pagebuffer + page_offset,
2987  buffer,
2988  n);
2989 
2990  r = sam4_page_write(private, page_cur, pagebuffer);
2991  if (r != ERROR_OK)
2992  goto done;
2993 
2994  count -= n;
2995  offset += n;
2996  buffer += n;
2997  page_cur++;
2998  }
2999 
3000  /* By checking that offset is correct here, we also
3001  fix a clang warning */
3002  assert(offset % private->page_size == 0);
3003 
3004  /* intermediate large pages */
3005  /* also - the final *terminal* */
3006  /* if that terminal page is a full page */
3007  LOG_DEBUG("Full Page Loop: cur=%d, end=%d, count = 0x%08x",
3008  (int)page_cur, (int)page_end, (unsigned int)(count));
3009 
3010  while ((page_cur < page_end) &&
3011  (count >= private->page_size)) {
3012  r = sam4_page_write(private, page_cur, buffer);
3013  if (r != ERROR_OK)
3014  goto done;
3015  count -= private->page_size;
3016  buffer += private->page_size;
3017  page_cur += 1;
3018  }
3019 
3020  /* terminal partial page? */
3021  if (count) {
3022  LOG_DEBUG("Terminal partial page, count = 0x%08x", (unsigned int)(count));
3023  /* we have a partial page */
3024  r = sam4_page_read(private, page_cur, pagebuffer);
3025  if (r != ERROR_OK)
3026  goto done;
3027  /* data goes at start */
3028  memcpy(pagebuffer, buffer, count);
3029  r = sam4_page_write(private, page_cur, pagebuffer);
3030  if (r != ERROR_OK)
3031  goto done;
3032  }
3033  LOG_DEBUG("Done!");
3034  r = ERROR_OK;
3035 done:
3036  free(pagebuffer);
3037  return r;
3038 }
3039 
3040 COMMAND_HANDLER(sam4_handle_info_command)
3041 {
3042  struct sam4_chip *chip;
3043  chip = get_current_sam4(CMD);
3044  if (!chip)
3045  return ERROR_OK;
3046 
3047  unsigned int x;
3048  int r;
3049 
3050  /* bank0 must exist before we can do anything */
3051  if (!chip->details.bank[0].bank) {
3052  x = 0;
3053 need_define:
3055  "Please define bank %d via command: flash bank %s ... ",
3056  x,
3058  return ERROR_FAIL;
3059  }
3060 
3061  /* if bank 0 is not probed, then probe it */
3062  if (!(chip->details.bank[0].probed)) {
3063  r = sam4_auto_probe(chip->details.bank[0].bank);
3064  if (r != ERROR_OK)
3065  return ERROR_FAIL;
3066  }
3067  /* above guarantees the "chip details" structure is valid */
3068  /* and thus, bank private areas are valid */
3069  /* and we have a SAM4 chip, what a concept! */
3070 
3071  /* auto-probe other banks, 0 done above */
3072  for (x = 1; x < SAM4_MAX_FLASH_BANKS; x++) {
3073  /* skip banks not present */
3074  if (!(chip->details.bank[x].present))
3075  continue;
3076 
3077  if (!chip->details.bank[x].bank)
3078  goto need_define;
3079 
3080  if (chip->details.bank[x].probed)
3081  continue;
3082 
3083  r = sam4_auto_probe(chip->details.bank[x].bank);
3084  if (r != ERROR_OK)
3085  return r;
3086  }
3087 
3088  r = sam4_get_info(chip);
3089  if (r != ERROR_OK) {
3090  LOG_DEBUG("Sam4Info, Failed %d", r);
3091  return r;
3092  }
3093 
3094  return ERROR_OK;
3095 }
3096 
3097 COMMAND_HANDLER(sam4_handle_gpnvm_command)
3098 {
3099  unsigned int x, v;
3100  int r, who;
3101  struct sam4_chip *chip;
3102 
3103  chip = get_current_sam4(CMD);
3104  if (!chip)
3105  return ERROR_OK;
3106 
3107  if (chip->target->state != TARGET_HALTED) {
3108  LOG_ERROR("sam4 - target not halted");
3109  return ERROR_TARGET_NOT_HALTED;
3110  }
3111 
3112  if (!chip->details.bank[0].bank) {
3113  command_print(CMD, "Bank0 must be defined first via: flash bank %s ...",
3115  return ERROR_FAIL;
3116  }
3117  if (!chip->details.bank[0].probed) {
3118  r = sam4_auto_probe(chip->details.bank[0].bank);
3119  if (r != ERROR_OK)
3120  return r;
3121  }
3122 
3123  switch (CMD_ARGC) {
3124  case 0:
3125  goto showall;
3126  case 1:
3127  who = -1;
3128  break;
3129  case 2:
3130  if ((strcmp(CMD_ARGV[0], "show") == 0) && (strcmp(CMD_ARGV[1], "all") == 0)) {
3131  who = -1;
3132  } else {
3133  uint32_t v32;
3134  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], v32);
3135  who = v32;
3136  }
3137  break;
3138  default:
3140  }
3141 
3142  if (strcmp("show", CMD_ARGV[0]) == 0) {
3143  if (who == -1) {
3144 showall:
3145  r = ERROR_OK;
3146  for (x = 0; x < chip->details.n_gpnvms; x++) {
3147  r = flashd_get_gpnvm(&(chip->details.bank[0]), x, &v);
3148  if (r != ERROR_OK)
3149  break;
3150  command_print(CMD, "sam4-gpnvm%u: %u", x, v);
3151  }
3152  return r;
3153  }
3154  if ((who >= 0) && (((unsigned)(who)) < chip->details.n_gpnvms)) {
3155  r = flashd_get_gpnvm(&(chip->details.bank[0]), who, &v);
3156  if (r == ERROR_OK)
3157  command_print(CMD, "sam4-gpnvm%u: %u", who, v);
3158  return r;
3159  } else {
3160  command_print(CMD, "sam4-gpnvm invalid GPNVM: %u", who);
3162  }
3163  }
3164 
3165  if (who == -1) {
3166  command_print(CMD, "Missing GPNVM number");
3168  }
3169 
3170  if (strcmp("set", CMD_ARGV[0]) == 0)
3171  r = flashd_set_gpnvm(&(chip->details.bank[0]), who);
3172  else if ((strcmp("clr", CMD_ARGV[0]) == 0) ||
3173  (strcmp("clear", CMD_ARGV[0]) == 0)) /* quietly accept both */
3174  r = flashd_clr_gpnvm(&(chip->details.bank[0]), who);
3175  else {
3176  command_print(CMD, "Unknown command: %s", CMD_ARGV[0]);
3178  }
3179  return r;
3180 }
3181 
3182 COMMAND_HANDLER(sam4_handle_slowclk_command)
3183 {
3184  struct sam4_chip *chip;
3185 
3186  chip = get_current_sam4(CMD);
3187  if (!chip)
3188  return ERROR_OK;
3189 
3190  switch (CMD_ARGC) {
3191  case 0:
3192  /* show */
3193  break;
3194  case 1:
3195  {
3196  /* set */
3197  uint32_t v;
3198  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], v);
3199  if (v > 200000) {
3200  /* absurd slow clock of 200Khz? */
3201  command_print(CMD, "Absurd/illegal slow clock freq: %d\n", (int)(v));
3203  }
3204  chip->cfg.slow_freq = v;
3205  break;
3206  }
3207  default:
3208  /* error */
3209  command_print(CMD, "Too many parameters");
3211  }
3212  command_print(CMD, "Slowclk freq: %d.%03dkhz",
3213  (int)(chip->cfg.slow_freq / 1000),
3214  (int)(chip->cfg.slow_freq % 1000));
3215  return ERROR_OK;
3216 }
3217 
3218 static const struct command_registration at91sam4_exec_command_handlers[] = {
3219  {
3220  .name = "gpnvm",
3221  .handler = sam4_handle_gpnvm_command,
3222  .mode = COMMAND_EXEC,
3223  .usage = "[('clr'|'set'|'show') bitnum]",
3224  .help = "Without arguments, shows all bits in the gpnvm "
3225  "register. Otherwise, clears, sets, or shows one "
3226  "General Purpose Non-Volatile Memory (gpnvm) bit.",
3227  },
3228  {
3229  .name = "info",
3230  .handler = sam4_handle_info_command,
3231  .mode = COMMAND_EXEC,
3232  .help = "Print information about the current at91sam4 chip "
3233  "and its flash configuration.",
3234  .usage = "",
3235  },
3236  {
3237  .name = "slowclk",
3238  .handler = sam4_handle_slowclk_command,
3239  .mode = COMMAND_EXEC,
3240  .usage = "[clock_hz]",
3241  .help = "Display or set the slowclock frequency "
3242  "(default 32768 Hz).",
3243  },
3245 };
3246 static const struct command_registration at91sam4_command_handlers[] = {
3247  {
3248  .name = "at91sam4",
3249  .mode = COMMAND_ANY,
3250  .help = "at91sam4 flash command group",
3251  .usage = "",
3253  },
3255 };
3256 
3257 const struct flash_driver at91sam4_flash = {
3258  .name = "at91sam4",
3259  .commands = at91sam4_command_handlers,
3260  .flash_bank_command = sam4_flash_bank_command,
3261  .erase = sam4_erase,
3262  .protect = sam4_protect,
3263  .write = sam4_write,
3264  .read = default_flash_read,
3265  .probe = sam4_probe,
3266  .auto_probe = sam4_auto_probe,
3267  .erase_check = default_flash_blank_check,
3268  .protect_check = sam4_protect_check,
3269  .info = sam4_info,
3270  .free_driver_priv = sam4_free_driver_priv,
3271 };
#define FLASH_BANK0_BASE_C32
Definition: at91sam4.c:39
#define FLASH_BANK1_BASE_2048K_SD
Definition: at91sam4.c:36
#define AT91C_EFC_FCMD_WPL
Definition: at91sam4.c:44
static int sam4_page_write(struct sam4_bank_private *private, unsigned int pagenum, const uint8_t *buf)
Definition: at91sam4.c:2843
#define AT91C_EFC_FCMD_GLB
Definition: at91sam4.c:53
#define FLASH_BANK_BASE_S
Definition: at91sam4.c:28
static const struct sam4_reg_list * sam4_get_reg(struct sam4_chip *chip, uint32_t *goes_here)
Given a pointer to where it goes in the structure, determine the register name, address from the all ...
Definition: at91sam4.c:2338
static const char *const eproc_names[]
Definition: at91sam4.c:1934
#define AT91C_EFC_FCMD_EWPL
Definition: at91sam4.c:46
static const struct sam4_chip_details all_sam4_details[]
Definition: at91sam4.c:222
#define REG_NAME_WIDTH
Definition: at91sam4.c:25
static int sam4_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: at91sam4.c:2887
COMMAND_HANDLER(sam4_handle_info_command)
Definition: at91sam4.c:3040
static int sam4_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: at91sam4.c:2615
static void sam4_explain_chipid_cidr(struct sam4_chip *chip)
Definition: at91sam4.c:2109
static const char _unknown[]
Definition: at91sam4.c:1933
static int flashd_get_gpnvm(struct sam4_bank_private *private, unsigned int gpnvm, unsigned int *puthere)
Gets current GPNVM state.
Definition: at91sam4.c:1718
#define OFFSET_EFC_FRR
Definition: at91sam4.c:63
static int sam4_auto_probe(struct flash_bank *bank)
Definition: at91sam4.c:2708
static const char * _yes_or_no(uint32_t v)
Definition: at91sam4.c:2053
static struct sam4_bank_private * get_sam4_bank_private(struct flash_bank *bank)
Definition: at91sam4.c:2329
static void sam4_explain_ckgr_plla(struct sam4_chip *chip)
Definition: at91sam4.c:2166
#define AT91C_EFC_FCMD_SFB
Definition: at91sam4.c:54
#define AT91C_EFC_FCMD_EA
Definition: at91sam4.c:47
static int sam4_get_details(struct sam4_bank_private *private)
Definition: at91sam4.c:2554
#define FLASH_BANK1_BASE_1024K_SD
Definition: at91sam4.c:34
#define AT91C_EFC_FCMD_EPA
Definition: at91sam4.c:50
static struct sam4_chip * all_sam4_chips
Definition: at91sam4.c:186
static int sam4_read_this_reg(struct sam4_chip *chip, uint32_t *goes_here)
Definition: at91sam4.c:2365
#define AT91C_EFC_FCMD_GFB
Definition: at91sam4.c:56
static void sam4_explain_ckgr_mor(struct sam4_chip *chip)
Definition: at91sam4.c:2065
#define SAM4_ENTRY(NAME, FUNC)
Definition: at91sam4.c:2304
static uint32_t * sam4_get_reg_ptr(struct sam4_cfg *cfg, const struct sam4_reg_list *list)
Definition: at91sam4.c:2291
#define FLASH_BANK_BASE_C
Definition: at91sam4.c:29
static const struct sam4_reg_list sam4_all_regs[]
Definition: at91sam4.c:2307
static int sam4_protect_check(struct flash_bank *bank)
Definition: at91sam4.c:2442
static struct sam4_chip * get_current_sam4(struct command_invocation *cmd)
Definition: at91sam4.c:188
static int efc_get_status(struct sam4_bank_private *private, uint32_t *v)
Get the current status of the EEFC and the value of some status bits (LOCKE, PROGE).
Definition: at91sam4.c:1448
static const char *const nvpsize[]
Definition: at91sam4.c:1954
static void sam4_explain_mckr(struct sam4_chip *chip)
Definition: at91sam4.c:2186
#define AT91C_EFC_FCMD_EWP
Definition: at91sam4.c:45
static float _tomhz(uint32_t freq_hz)
Definition: at91sam4.c:65
static const struct command_registration at91sam4_exec_command_handlers[]
Definition: at91sam4.c:3218
#define AT91C_EFC_FCMD_WP
Definition: at91sam4.c:43
static int flashd_get_lock_bits(struct sam4_bank_private *private, uint32_t *v)
Returns a bit field (at most 64) of locked regions within a page.
Definition: at91sam4.c:1825
static int flashd_read_uid(struct sam4_bank_private *private)
Read the unique ID.
Definition: at91sam4.c:1619
#define SAM4_MAX_FLASH_BANKS
Definition: at91sam4.c:165
static const struct command_registration at91sam4_command_handlers[]
Definition: at91sam4.c:3246
static int flashd_set_gpnvm(struct sam4_bank_private *private, unsigned int gpnvm)
Sets the selected GPNVM bit.
Definition: at91sam4.c:1791
static const char *const nvptype[]
Definition: at91sam4.c:2042
static uint32_t sam4_reg_fieldname(struct sam4_chip *chip, const char *regname, uint32_t value, unsigned int shift, unsigned int width)
Definition: at91sam4.c:1904
#define FLASH_BANK1_BASE_C32
Definition: at91sam4.c:40
static void sam4_explain_ckgr_mcfr(struct sam4_chip *chip)
Definition: at91sam4.c:2148
#define OFFSET_EFC_FCR
Definition: at91sam4.c:61
#define FLASH_BANK0_BASE_SD
Definition: at91sam4.c:32
#define SAM4_N_NVM_BITS
Definition: at91sam4.c:160
static int efc_start_command(struct sam4_bank_private *private, unsigned int command, unsigned int argument)
Definition: at91sam4.c:1481
#define nvpsize2
Definition: at91sam4.c:1953
#define OFFSET_EFC_FSR
Definition: at91sam4.c:62
static int flashd_lock(struct sam4_bank_private *private, unsigned int start_sector, unsigned int end_sector)
Locks regions.
Definition: at91sam4.c:1877
static int sam4_set_wait(struct sam4_bank_private *private)
Definition: at91sam4.c:2817
#define AT91C_EFC_FCMD_STUI
Definition: at91sam4.c:57
static void sam4_free_driver_priv(struct flash_bank *bank)
Remove all chips from the internal list without distinguishing which one is owned by this bank.
Definition: at91sam4.c:2543
static int flashd_unlock(struct sam4_bank_private *private, unsigned int start_sector, unsigned int end_sector)
Unlocks all the regions in the given address range.
Definition: at91sam4.c:1847
#define AT91C_EFC_FCMD_GETD
Definition: at91sam4.c:42
static int flashd_erase_pages(struct sam4_bank_private *private, int first_page, int num_pages, uint32_t *status)
Erases the entire flash.
Definition: at91sam4.c:1672
static int sam4_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: at91sam4.c:2772
FLASH_BANK_COMMAND_HANDLER(sam4_flash_bank_command)
Definition: at91sam4.c:2475
static int efc_get_result(struct sam4_bank_private *private, uint32_t *v)
Get the result of the last executed command.
Definition: at91sam4.c:1468
static int sam4_read_all_regs(struct sam4_chip *chip)
Definition: at91sam4.c:2382
static const char *const sramsize[]
Definition: at91sam4.c:1973
const struct flash_driver at91sam4_flash
Definition: at91sam4.c:3257
static int sam4_probe(struct flash_bank *bank)
Definition: at91sam4.c:2633
static int flashd_clr_gpnvm(struct sam4_bank_private *private, unsigned int gpnvm)
Clears the selected GPNVM bit.
Definition: at91sam4.c:1759
#define AT91C_EFC_FCMD_SPUI
Definition: at91sam4.c:58
static int sam4_page_read(struct sam4_bank_private *private, unsigned int pagenum, uint8_t *buf)
Definition: at91sam4.c:2798
#define AT91C_EFC_FCMD_CLB
Definition: at91sam4.c:52
static int flashd_erase_entire_bank(struct sam4_bank_private *private)
Erases the entire flash.
Definition: at91sam4.c:1659
static int sam4_get_info(struct sam4_chip *chip)
Definition: at91sam4.c:2402
#define AT91C_EFC_FCMD_SLB
Definition: at91sam4.c:51
static int sam4_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: at91sam4.c:2719
static const char *const _rc_freq[]
Definition: at91sam4.c:2061
#define AT91C_EFC_FCMD_CFB
Definition: at91sam4.c:55
static int efc_perform_command(struct sam4_bank_private *private, unsigned int command, unsigned int argument, uint32_t *status)
Performs the given command and wait until its completion (or an error).
Definition: at91sam4.c:1575
#define CKGR_MOR
Definition: at91sam7.c:45
#define PMC_MCKR
Definition: at91sam7.c:50
#define CKGR_MCFR
Definition: at91sam7.c:44
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:348
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:371
#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:400
#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:440
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
unsigned short width
Definition: embeddedice.c:47
uint8_t bank
Definition: esirisc.c:135
#define ERROR_FLASH_BANK_NOT_PROBED
Definition: flash/common.h:35
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.
#define LOG_USER(expr ...)
Definition: log.h:136
#define ERROR_FAIL
Definition: log.h:174
#define LOG_USER_N(expr ...)
Definition: log.h:139
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define LOG_INFO(expr ...)
Definition: log.h:127
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
const char * name
Definition: at91sam3.c:2493
unsigned int value
Definition: at91sam3.c:2493
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:234
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
Definition: register.h:111
const char * name
Definition: register.h:113
uint32_t controller_address
Definition: at91sam4.c:138
unsigned int size_bytes
Definition: at91sam4.c:142
struct sam4_chip * chip
Definition: at91sam4.c:134
unsigned int bank_number
Definition: at91sam4.c:137
uint32_t base_address
Definition: at91sam4.c:139
uint32_t flash_wait_states
Definition: at91sam4.c:140
struct flash_bank * bank
Definition: at91sam4.c:136
unsigned int page_size
Definition: at91sam4.c:145
unsigned int nsectors
Definition: at91sam4.c:143
unsigned int sector_size
Definition: at91sam4.c:144
uint32_t rc_freq
Definition: at91sam4.c:78
uint32_t unique_id[4]
Definition: at91sam4.c:75
uint32_t fclk_freq
Definition: at91sam4.c:83
uint32_t PMC_IMR
Definition: at91sam4.c:118
uint32_t CHIPID_CIDR
Definition: at91sam4.c:90
uint32_t CKGR_UCKR
Definition: at91sam4.c:100
uint32_t PMC_MCKR
Definition: at91sam4.c:108
uint32_t PMC_FSPR
Definition: at91sam4.c:122
uint32_t PMC_PCK2
Definition: at91sam4.c:114
uint32_t mainosc_freq
Definition: at91sam4.c:79
uint32_t pclk2_freq
Definition: at91sam4.c:86
uint32_t pclk0_freq
Definition: at91sam4.c:84
uint32_t mclk_freq
Definition: at91sam4.c:81
uint32_t pclk1_freq
Definition: at91sam4.c:85
uint32_t slow_freq
Definition: at91sam4.c:77
uint32_t PMC_PCK0
Definition: at91sam4.c:110
uint32_t CHIPID_EXID
Definition: at91sam4.c:92
uint32_t cpu_freq
Definition: at91sam4.c:82
uint32_t PMC_FSMR
Definition: at91sam4.c:120
uint32_t plla_freq
Definition: at91sam4.c:80
uint32_t PMC_SCSR
Definition: at91sam4.c:96
uint32_t CKGR_MCFR
Definition: at91sam4.c:104
uint32_t CKGR_PLLAR
Definition: at91sam4.c:106
uint32_t CKGR_MOR
Definition: at91sam4.c:102
uint32_t PMC_PCK1
Definition: at91sam4.c:112
uint32_t PMC_PCSR
Definition: at91sam4.c:98
uint32_t PMC_SR
Definition: at91sam4.c:116
uint32_t chipid_cidr
Definition: at91sam4.c:156
unsigned int n_banks
Definition: at91sam4.c:164
const char * name
Definition: at91sam4.c:157
unsigned int total_flash_size
Definition: at91sam4.c:162
struct sam4_bank_private bank[SAM4_MAX_FLASH_BANKS]
Definition: at91sam4.c:167
unsigned int n_gpnvms
Definition: at91sam4.c:159
unsigned int total_sram_size
Definition: at91sam4.c:163
unsigned int gpnvm[SAM4_N_NVM_BITS]
Definition: at91sam4.c:161
bool probed
Definition: at91sam4.c:172
struct sam4_cfg cfg
Definition: at91sam4.c:177
struct sam4_chip_details details
Definition: at91sam4.c:175
struct sam4_chip * next
Definition: at91sam4.c:171
struct target * target
Definition: at91sam4.c:176
const char * name
Definition: at91sam4.c:182
size_t struct_offset
Definition: at91sam4.c:182
uint32_t address
Definition: at91sam4.c:182
void(* explain_func)(struct sam4_chip *chip)
Definition: at91sam4.c:183
Definition: target.h:119
enum target_state state
Definition: target.h:160
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:1275
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2650
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2559
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:1247
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:467
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:786
@ TARGET_HALTED
Definition: target.h:58
int64_t timeval_ms(void)
#define TARGET_ADDR_FMT
Definition: types.h:286
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22