1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-Source-Code)
4 * Copyright (C) 2009 by Duane Ellis <openocd@duaneellis.com>
7 * Copyright (C) 2010 by Olaf Lüke <olaf@uni-paderborn.de>
8 * Copyright (C) 2011 by Olivier Schonken and Jim Norris
10 * Some of the lower level code was based on code supplied by
11 * ATMEL under BSD-Source-Code License and this copyright.
12 * ATMEL Microcontroller Software Support
13 * Copyright (c) 2009, Atmel Corporation. All rights reserved.
21 #include <helper/time_support.h>
23 #define REG_NAME_WIDTH (12)
25 /* at91sam3u series (has one or two flash banks) */
26 #define FLASH_BANK0_BASE_U 0x00080000
27 #define FLASH_BANK1_BASE_U 0x00100000
29 /* at91sam3s series (has always one flash bank) */
30 #define FLASH_BANK_BASE_S 0x00400000
32 /* at91sam3sd series (has always two flash banks) */
33 #define FLASH_BANK0_BASE_SD FLASH_BANK_BASE_S
34 #define FLASH_BANK1_BASE_512K_SD (FLASH_BANK0_BASE_SD+(512*1024/2))
37 /* at91sam3n series (has always one flash bank) */
38 #define FLASH_BANK_BASE_N 0x00400000
40 /* at91sam3a/x series has two flash banks*/
41 #define FLASH_BANK0_BASE_AX 0x00080000
42 /*Bank 1 of the at91sam3a/x series starts at 0x00080000 + half flash size*/
43 #define FLASH_BANK1_BASE_256K_AX 0x000A0000
44 #define FLASH_BANK1_BASE_512K_AX 0x000C0000
46 #define AT91C_EFC_FCMD_GETD (0x0) /* (EFC) Get Flash Descriptor */
47 #define AT91C_EFC_FCMD_WP (0x1) /* (EFC) Write Page */
48 #define AT91C_EFC_FCMD_WPL (0x2) /* (EFC) Write Page and Lock */
49 #define AT91C_EFC_FCMD_EWP (0x3) /* (EFC) Erase Page and Write Page */
50 #define AT91C_EFC_FCMD_EWPL (0x4) /* (EFC) Erase Page and Write Page then Lock */
51 #define AT91C_EFC_FCMD_EA (0x5) /* (EFC) Erase All */
52 /* cmd6 is not present in the at91sam3u4/2/1 data sheet table 17-2 */
53 /* #define AT91C_EFC_FCMD_EPL (0x6) // (EFC) Erase plane? */
54 /* cmd7 is not present in the at91sam3u4/2/1 data sheet table 17-2 */
55 /* #define AT91C_EFC_FCMD_EPA (0x7) // (EFC) Erase pages? */
56 #define AT91C_EFC_FCMD_SLB (0x8) /* (EFC) Set Lock Bit */
57 #define AT91C_EFC_FCMD_CLB (0x9) /* (EFC) Clear Lock Bit */
58 #define AT91C_EFC_FCMD_GLB (0xA) /* (EFC) Get Lock Bit */
59 #define AT91C_EFC_FCMD_SFB (0xB) /* (EFC) Set Fuse Bit */
60 #define AT91C_EFC_FCMD_CFB (0xC) /* (EFC) Clear Fuse Bit */
61 #define AT91C_EFC_FCMD_GFB (0xD) /* (EFC) Get Fuse Bit */
62 #define AT91C_EFC_FCMD_STUI (0xE) /* (EFC) Start Read Unique ID */
63 #define AT91C_EFC_FCMD_SPUI (0xF) /* (EFC) Stop Read Unique ID */
65 #define OFFSET_EFC_FMR 0
66 #define OFFSET_EFC_FCR 4
67 #define OFFSET_EFC_FSR 8
68 #define OFFSET_EFC_FRR 12
70 extern const struct flash_driver at91sam3_flash
;
72 static float _tomhz(uint32_t freq_hz
)
76 f
= ((float)(freq_hz
)) / 1000000.0;
80 /* How the chip is configured. */
82 uint32_t unique_id
[4];
86 uint32_t mainosc_freq
;
96 #define SAM3_CHIPID_CIDR (0x400E0740)
98 #define SAM3_CHIPID_CIDR2 (0x400E0940) /*SAM3X and SAM3A cidr at this address*/
99 uint32_t CHIPID_CIDR2
;
100 #define SAM3_CHIPID_EXID (0x400E0744)
101 uint32_t CHIPID_EXID
;
102 #define SAM3_CHIPID_EXID2 (0x400E0944) /*SAM3X and SAM3A cidr at this address*/
103 uint32_t CHIPID_EXID2
;
106 #define SAM3_PMC_BASE (0x400E0400)
107 #define SAM3_PMC_SCSR (SAM3_PMC_BASE + 0x0008)
109 #define SAM3_PMC_PCSR (SAM3_PMC_BASE + 0x0018)
111 #define SAM3_CKGR_UCKR (SAM3_PMC_BASE + 0x001c)
113 #define SAM3_CKGR_MOR (SAM3_PMC_BASE + 0x0020)
115 #define SAM3_CKGR_MCFR (SAM3_PMC_BASE + 0x0024)
117 #define SAM3_CKGR_PLLAR (SAM3_PMC_BASE + 0x0028)
119 #define SAM3_PMC_MCKR (SAM3_PMC_BASE + 0x0030)
121 #define SAM3_PMC_PCK0 (SAM3_PMC_BASE + 0x0040)
123 #define SAM3_PMC_PCK1 (SAM3_PMC_BASE + 0x0044)
125 #define SAM3_PMC_PCK2 (SAM3_PMC_BASE + 0x0048)
127 #define SAM3_PMC_SR (SAM3_PMC_BASE + 0x0068)
129 #define SAM3_PMC_IMR (SAM3_PMC_BASE + 0x006c)
131 #define SAM3_PMC_FSMR (SAM3_PMC_BASE + 0x0070)
133 #define SAM3_PMC_FSPR (SAM3_PMC_BASE + 0x0074)
138 * The AT91SAM3N data sheet 04-Oct-2010, AT91SAM3U data sheet 22-Aug-2011
139 * and AT91SAM3S data sheet 09-Feb-2011 state that for flash writes
140 * the flash wait state (FWS) should be set to 6. It seems like that the
141 * cause of the problem is not the flash itself, but the flash write
142 * buffer. Ie the wait states have to be set before writing into the
144 * Tested and confirmed with SAM3N and SAM3U
147 struct sam3_bank_private
{
149 /* DANGER: THERE ARE DRAGONS HERE.. */
150 /* NOTE: If you add more 'ghost' pointers */
151 /* be aware that you must *manually* update */
152 /* these pointers in the function sam3_get_details() */
153 /* See the comment "Here there be dragons" */
155 /* so we can find the chip we belong to */
156 struct sam3_chip
*chip
;
157 /* so we can find the original bank pointer */
158 struct flash_bank
*bank
;
159 unsigned bank_number
;
160 uint32_t controller_address
;
161 uint32_t base_address
;
162 uint32_t flash_wait_states
;
166 unsigned sector_size
;
170 struct sam3_chip_details
{
171 /* THERE ARE DRAGONS HERE.. */
172 /* note: If you add pointers here */
173 /* be careful about them as they */
174 /* may need to be updated inside */
175 /* the function: "sam3_get_details() */
176 /* which copy/overwrites the */
177 /* 'runtime' copy of this structure */
178 uint32_t chipid_cidr
;
182 #define SAM3_N_NVM_BITS 3
183 unsigned gpnvm
[SAM3_N_NVM_BITS
];
184 unsigned total_flash_size
;
185 unsigned total_sram_size
;
187 #define SAM3_MAX_FLASH_BANKS 2
188 /* these are "initialized" from the global const data */
189 struct sam3_bank_private bank
[SAM3_MAX_FLASH_BANKS
];
193 struct sam3_chip
*next
;
196 /* this is "initialized" from the global const structure */
197 struct sam3_chip_details details
;
198 struct target
*target
;
203 struct sam3_reg_list
{
204 uint32_t address
; size_t struct_offset
; const char *name
;
205 void (*explain_func
)(struct sam3_chip
*chip
);
208 static struct sam3_chip
*all_sam3_chips
;
210 static struct sam3_chip
*get_current_sam3(struct command_invocation
*cmd
)
213 static struct sam3_chip
*p
;
215 t
= get_current_target(cmd
->ctx
);
217 command_print_sameline(cmd
, "No current target?\n");
223 /* this should not happen */
224 /* the command is not registered until the chip is created? */
225 command_print_sameline(cmd
, "No SAM3 chips exist?\n");
234 command_print_sameline(cmd
, "Cannot find SAM3 chip?\n");
238 /* these are used to *initialize* the "chip->details" structure. */
239 static const struct sam3_chip_details all_sam3_details
[] = {
240 /* Start at91sam3u* series */
242 .chipid_cidr
= 0x28100960,
243 .name
= "at91sam3u4e",
244 .total_flash_size
= 256 * 1024,
245 .total_sram_size
= 52 * 1024,
249 /* System boots at address 0x0 */
250 /* gpnvm[1] = selects boot code */
251 /* if gpnvm[1] == 0 */
252 /* boot is via "SAMBA" (rom) */
254 /* boot is via FLASH */
255 /* Selection is via gpnvm[2] */
258 /* NOTE: banks 0 & 1 switch places */
259 /* if gpnvm[2] == 0 */
260 /* Bank0 is the boot rom */
262 /* Bank1 is the boot rom */
271 .base_address
= FLASH_BANK0_BASE_U
,
272 .controller_address
= 0x400e0800,
273 .flash_wait_states
= 6, /* workaround silicon bug */
275 .size_bytes
= 128 * 1024,
287 .base_address
= FLASH_BANK1_BASE_U
,
288 .controller_address
= 0x400e0a00,
289 .flash_wait_states
= 6, /* workaround silicon bug */
291 .size_bytes
= 128 * 1024,
300 .chipid_cidr
= 0x281a0760,
301 .name
= "at91sam3u2e",
302 .total_flash_size
= 128 * 1024,
303 .total_sram_size
= 36 * 1024,
307 /* System boots at address 0x0 */
308 /* gpnvm[1] = selects boot code */
309 /* if gpnvm[1] == 0 */
310 /* boot is via "SAMBA" (rom) */
312 /* boot is via FLASH */
313 /* Selection is via gpnvm[2] */
322 .base_address
= FLASH_BANK0_BASE_U
,
323 .controller_address
= 0x400e0800,
324 .flash_wait_states
= 6, /* workaround silicon bug */
326 .size_bytes
= 128 * 1024,
340 .chipid_cidr
= 0x28190560,
341 .name
= "at91sam3u1e",
342 .total_flash_size
= 64 * 1024,
343 .total_sram_size
= 20 * 1024,
347 /* System boots at address 0x0 */
348 /* gpnvm[1] = selects boot code */
349 /* if gpnvm[1] == 0 */
350 /* boot is via "SAMBA" (rom) */
352 /* boot is via FLASH */
353 /* Selection is via gpnvm[2] */
364 .base_address
= FLASH_BANK0_BASE_U
,
365 .controller_address
= 0x400e0800,
366 .flash_wait_states
= 6, /* workaround silicon bug */
368 .size_bytes
= 64 * 1024,
384 .chipid_cidr
= 0x28000960,
385 .name
= "at91sam3u4c",
386 .total_flash_size
= 256 * 1024,
387 .total_sram_size
= 52 * 1024,
391 /* System boots at address 0x0 */
392 /* gpnvm[1] = selects boot code */
393 /* if gpnvm[1] == 0 */
394 /* boot is via "SAMBA" (rom) */
396 /* boot is via FLASH */
397 /* Selection is via gpnvm[2] */
400 /* NOTE: banks 0 & 1 switch places */
401 /* if gpnvm[2] == 0 */
402 /* Bank0 is the boot rom */
404 /* Bank1 is the boot rom */
413 .base_address
= FLASH_BANK0_BASE_U
,
414 .controller_address
= 0x400e0800,
415 .flash_wait_states
= 6, /* workaround silicon bug */
417 .size_bytes
= 128 * 1024,
428 .base_address
= FLASH_BANK1_BASE_U
,
429 .controller_address
= 0x400e0a00,
430 .flash_wait_states
= 6, /* workaround silicon bug */
432 .size_bytes
= 128 * 1024,
441 .chipid_cidr
= 0x280a0760,
442 .name
= "at91sam3u2c",
443 .total_flash_size
= 128 * 1024,
444 .total_sram_size
= 36 * 1024,
448 /* System boots at address 0x0 */
449 /* gpnvm[1] = selects boot code */
450 /* if gpnvm[1] == 0 */
451 /* boot is via "SAMBA" (rom) */
453 /* boot is via FLASH */
454 /* Selection is via gpnvm[2] */
463 .base_address
= FLASH_BANK0_BASE_U
,
464 .controller_address
= 0x400e0800,
465 .flash_wait_states
= 6, /* workaround silicon bug */
467 .size_bytes
= 128 * 1024,
481 .chipid_cidr
= 0x28090560,
482 .name
= "at91sam3u1c",
483 .total_flash_size
= 64 * 1024,
484 .total_sram_size
= 20 * 1024,
488 /* System boots at address 0x0 */
489 /* gpnvm[1] = selects boot code */
490 /* if gpnvm[1] == 0 */
491 /* boot is via "SAMBA" (rom) */
493 /* boot is via FLASH */
494 /* Selection is via gpnvm[2] */
505 .base_address
= FLASH_BANK0_BASE_U
,
506 .controller_address
= 0x400e0800,
507 .flash_wait_states
= 6, /* workaround silicon bug */
509 .size_bytes
= 64 * 1024,
524 /* Start at91sam3s* series */
526 /* Note: The preliminary at91sam3s datasheet says on page 302 */
527 /* that the flash controller is at address 0x400E0800. */
528 /* This is _not_ the case, the controller resides at address 0x400e0a00. */
530 .chipid_cidr
= 0x28A00960,
531 .name
= "at91sam3s4c",
532 .total_flash_size
= 256 * 1024,
533 .total_sram_size
= 48 * 1024,
543 .base_address
= FLASH_BANK_BASE_S
,
544 .controller_address
= 0x400e0a00,
545 .flash_wait_states
= 6, /* workaround silicon bug */
547 .size_bytes
= 256 * 1024,
549 .sector_size
= 16384,
563 .chipid_cidr
= 0x28900960,
564 .name
= "at91sam3s4b",
565 .total_flash_size
= 256 * 1024,
566 .total_sram_size
= 48 * 1024,
576 .base_address
= FLASH_BANK_BASE_S
,
577 .controller_address
= 0x400e0a00,
578 .flash_wait_states
= 6, /* workaround silicon bug */
580 .size_bytes
= 256 * 1024,
582 .sector_size
= 16384,
595 .chipid_cidr
= 0x28800960,
596 .name
= "at91sam3s4a",
597 .total_flash_size
= 256 * 1024,
598 .total_sram_size
= 48 * 1024,
608 .base_address
= FLASH_BANK_BASE_S
,
609 .controller_address
= 0x400e0a00,
610 .flash_wait_states
= 6, /* workaround silicon bug */
612 .size_bytes
= 256 * 1024,
614 .sector_size
= 16384,
627 .chipid_cidr
= 0x28AA0760,
628 .name
= "at91sam3s2c",
629 .total_flash_size
= 128 * 1024,
630 .total_sram_size
= 32 * 1024,
640 .base_address
= FLASH_BANK_BASE_S
,
641 .controller_address
= 0x400e0a00,
642 .flash_wait_states
= 6, /* workaround silicon bug */
644 .size_bytes
= 128 * 1024,
646 .sector_size
= 16384,
659 .chipid_cidr
= 0x289A0760,
660 .name
= "at91sam3s2b",
661 .total_flash_size
= 128 * 1024,
662 .total_sram_size
= 32 * 1024,
672 .base_address
= FLASH_BANK_BASE_S
,
673 .controller_address
= 0x400e0a00,
674 .flash_wait_states
= 6, /* workaround silicon bug */
676 .size_bytes
= 128 * 1024,
678 .sector_size
= 16384,
691 .chipid_cidr
= 0x298B0A60,
692 .name
= "at91sam3sd8a",
693 .total_flash_size
= 512 * 1024,
694 .total_sram_size
= 64 * 1024,
704 .base_address
= FLASH_BANK0_BASE_SD
,
705 .controller_address
= 0x400e0a00,
706 .flash_wait_states
= 6, /* workaround silicon bug */
708 .size_bytes
= 256 * 1024,
710 .sector_size
= 32768,
719 .base_address
= FLASH_BANK1_BASE_512K_SD
,
720 .controller_address
= 0x400e0a00,
721 .flash_wait_states
= 6, /* workaround silicon bug */
723 .size_bytes
= 256 * 1024,
725 .sector_size
= 32768,
731 .chipid_cidr
= 0x299B0A60,
732 .name
= "at91sam3sd8b",
733 .total_flash_size
= 512 * 1024,
734 .total_sram_size
= 64 * 1024,
744 .base_address
= FLASH_BANK0_BASE_SD
,
745 .controller_address
= 0x400e0a00,
746 .flash_wait_states
= 6, /* workaround silicon bug */
748 .size_bytes
= 256 * 1024,
750 .sector_size
= 32768,
759 .base_address
= FLASH_BANK1_BASE_512K_SD
,
760 .controller_address
= 0x400e0a00,
761 .flash_wait_states
= 6, /* workaround silicon bug */
763 .size_bytes
= 256 * 1024,
765 .sector_size
= 32768,
771 .chipid_cidr
= 0x29ab0a60,
772 .name
= "at91sam3sd8c",
773 .total_flash_size
= 512 * 1024,
774 .total_sram_size
= 64 * 1024,
784 .base_address
= FLASH_BANK0_BASE_SD
,
785 .controller_address
= 0x400e0a00,
786 .flash_wait_states
= 6, /* workaround silicon bug */
788 .size_bytes
= 256 * 1024,
790 .sector_size
= 32768,
799 .base_address
= FLASH_BANK1_BASE_512K_SD
,
800 .controller_address
= 0x400e0a00,
801 .flash_wait_states
= 6, /* workaround silicon bug */
803 .size_bytes
= 256 * 1024,
805 .sector_size
= 32768,
811 .chipid_cidr
= 0x288A0760,
812 .name
= "at91sam3s2a",
813 .total_flash_size
= 128 * 1024,
814 .total_sram_size
= 32 * 1024,
824 .base_address
= FLASH_BANK_BASE_S
,
825 .controller_address
= 0x400e0a00,
826 .flash_wait_states
= 6, /* workaround silicon bug */
828 .size_bytes
= 128 * 1024,
830 .sector_size
= 16384,
843 .chipid_cidr
= 0x28A90560,
844 .name
= "at91sam3s1c",
845 .total_flash_size
= 64 * 1024,
846 .total_sram_size
= 16 * 1024,
856 .base_address
= FLASH_BANK_BASE_S
,
857 .controller_address
= 0x400e0a00,
858 .flash_wait_states
= 6, /* workaround silicon bug */
860 .size_bytes
= 64 * 1024,
862 .sector_size
= 16384,
875 .chipid_cidr
= 0x28990560,
876 .name
= "at91sam3s1b",
877 .total_flash_size
= 64 * 1024,
878 .total_sram_size
= 16 * 1024,
888 .base_address
= FLASH_BANK_BASE_S
,
889 .controller_address
= 0x400e0a00,
890 .flash_wait_states
= 6, /* workaround silicon bug */
892 .size_bytes
= 64 * 1024,
894 .sector_size
= 16384,
907 .chipid_cidr
= 0x28890560,
908 .name
= "at91sam3s1a",
909 .total_flash_size
= 64 * 1024,
910 .total_sram_size
= 16 * 1024,
920 .base_address
= FLASH_BANK_BASE_S
,
921 .controller_address
= 0x400e0a00,
922 .flash_wait_states
= 6, /* workaround silicon bug */
924 .size_bytes
= 64 * 1024,
926 .sector_size
= 16384,
939 .chipid_cidr
= 0x288B0A60,
940 .name
= "at91sam3s8a",
941 .total_flash_size
= 256 * 2048,
942 .total_sram_size
= 64 * 1024,
952 .base_address
= FLASH_BANK_BASE_S
,
953 .controller_address
= 0x400e0a00,
954 .flash_wait_states
= 6, /* workaround silicon bug */
956 .size_bytes
= 256 * 2048,
958 .sector_size
= 32768,
971 .chipid_cidr
= 0x289B0A60,
972 .name
= "at91sam3s8b",
973 .total_flash_size
= 256 * 2048,
974 .total_sram_size
= 64 * 1024,
984 .base_address
= FLASH_BANK_BASE_S
,
985 .controller_address
= 0x400e0a00,
986 .flash_wait_states
= 6, /* workaround silicon bug */
988 .size_bytes
= 256 * 2048,
990 .sector_size
= 32768,
1003 .chipid_cidr
= 0x28AB0A60,
1004 .name
= "at91sam3s8c",
1005 .total_flash_size
= 256 * 2048,
1006 .total_sram_size
= 64 * 1024,
1016 .base_address
= FLASH_BANK_BASE_S
,
1017 .controller_address
= 0x400e0a00,
1018 .flash_wait_states
= 6, /* workaround silicon bug */
1020 .size_bytes
= 256 * 2048,
1022 .sector_size
= 32768,
1035 /* Start at91sam3n* series */
1037 .chipid_cidr
= 0x29540960,
1038 .name
= "at91sam3n4c",
1039 .total_flash_size
= 256 * 1024,
1040 .total_sram_size
= 24 * 1024,
1044 /* System boots at address 0x0 */
1045 /* gpnvm[1] = selects boot code */
1046 /* if gpnvm[1] == 0 */
1047 /* boot is via "SAMBA" (rom) */
1049 /* boot is via FLASH */
1050 /* Selection is via gpnvm[2] */
1053 /* NOTE: banks 0 & 1 switch places */
1054 /* if gpnvm[2] == 0 */
1055 /* Bank0 is the boot rom */
1057 /* Bank1 is the boot rom */
1066 .base_address
= FLASH_BANK_BASE_N
,
1067 .controller_address
= 0x400e0A00,
1068 .flash_wait_states
= 6, /* workaround silicon bug */
1070 .size_bytes
= 256 * 1024,
1072 .sector_size
= 16384,
1086 .chipid_cidr
= 0x29440960,
1087 .name
= "at91sam3n4b",
1088 .total_flash_size
= 256 * 1024,
1089 .total_sram_size
= 24 * 1024,
1093 /* System boots at address 0x0 */
1094 /* gpnvm[1] = selects boot code */
1095 /* if gpnvm[1] == 0 */
1096 /* boot is via "SAMBA" (rom) */
1098 /* boot is via FLASH */
1099 /* Selection is via gpnvm[2] */
1102 /* NOTE: banks 0 & 1 switch places */
1103 /* if gpnvm[2] == 0 */
1104 /* Bank0 is the boot rom */
1106 /* Bank1 is the boot rom */
1115 .base_address
= FLASH_BANK_BASE_N
,
1116 .controller_address
= 0x400e0A00,
1117 .flash_wait_states
= 6, /* workaround silicon bug */
1119 .size_bytes
= 256 * 1024,
1121 .sector_size
= 16384,
1135 .chipid_cidr
= 0x29340960,
1136 .name
= "at91sam3n4a",
1137 .total_flash_size
= 256 * 1024,
1138 .total_sram_size
= 24 * 1024,
1142 /* System boots at address 0x0 */
1143 /* gpnvm[1] = selects boot code */
1144 /* if gpnvm[1] == 0 */
1145 /* boot is via "SAMBA" (rom) */
1147 /* boot is via FLASH */
1148 /* Selection is via gpnvm[2] */
1151 /* NOTE: banks 0 & 1 switch places */
1152 /* if gpnvm[2] == 0 */
1153 /* Bank0 is the boot rom */
1155 /* Bank1 is the boot rom */
1164 .base_address
= FLASH_BANK_BASE_N
,
1165 .controller_address
= 0x400e0A00,
1166 .flash_wait_states
= 6, /* workaround silicon bug */
1168 .size_bytes
= 256 * 1024,
1170 .sector_size
= 16384,
1184 .chipid_cidr
= 0x29590760,
1185 .name
= "at91sam3n2c",
1186 .total_flash_size
= 128 * 1024,
1187 .total_sram_size
= 16 * 1024,
1191 /* System boots at address 0x0 */
1192 /* gpnvm[1] = selects boot code */
1193 /* if gpnvm[1] == 0 */
1194 /* boot is via "SAMBA" (rom) */
1196 /* boot is via FLASH */
1197 /* Selection is via gpnvm[2] */
1200 /* NOTE: banks 0 & 1 switch places */
1201 /* if gpnvm[2] == 0 */
1202 /* Bank0 is the boot rom */
1204 /* Bank1 is the boot rom */
1213 .base_address
= FLASH_BANK_BASE_N
,
1214 .controller_address
= 0x400e0A00,
1215 .flash_wait_states
= 6, /* workaround silicon bug */
1217 .size_bytes
= 128 * 1024,
1219 .sector_size
= 16384,
1233 .chipid_cidr
= 0x29490760,
1234 .name
= "at91sam3n2b",
1235 .total_flash_size
= 128 * 1024,
1236 .total_sram_size
= 16 * 1024,
1240 /* System boots at address 0x0 */
1241 /* gpnvm[1] = selects boot code */
1242 /* if gpnvm[1] == 0 */
1243 /* boot is via "SAMBA" (rom) */
1245 /* boot is via FLASH */
1246 /* Selection is via gpnvm[2] */
1249 /* NOTE: banks 0 & 1 switch places */
1250 /* if gpnvm[2] == 0 */
1251 /* Bank0 is the boot rom */
1253 /* Bank1 is the boot rom */
1262 .base_address
= FLASH_BANK_BASE_N
,
1263 .controller_address
= 0x400e0A00,
1264 .flash_wait_states
= 6, /* workaround silicon bug */
1266 .size_bytes
= 128 * 1024,
1268 .sector_size
= 16384,
1282 .chipid_cidr
= 0x29390760,
1283 .name
= "at91sam3n2a",
1284 .total_flash_size
= 128 * 1024,
1285 .total_sram_size
= 16 * 1024,
1289 /* System boots at address 0x0 */
1290 /* gpnvm[1] = selects boot code */
1291 /* if gpnvm[1] == 0 */
1292 /* boot is via "SAMBA" (rom) */
1294 /* boot is via FLASH */
1295 /* Selection is via gpnvm[2] */
1298 /* NOTE: banks 0 & 1 switch places */
1299 /* if gpnvm[2] == 0 */
1300 /* Bank0 is the boot rom */
1302 /* Bank1 is the boot rom */
1311 .base_address
= FLASH_BANK_BASE_N
,
1312 .controller_address
= 0x400e0A00,
1313 .flash_wait_states
= 6, /* workaround silicon bug */
1315 .size_bytes
= 128 * 1024,
1317 .sector_size
= 16384,
1331 .chipid_cidr
= 0x29580560,
1332 .name
= "at91sam3n1c",
1333 .total_flash_size
= 64 * 1024,
1334 .total_sram_size
= 8 * 1024,
1338 /* System boots at address 0x0 */
1339 /* gpnvm[1] = selects boot code */
1340 /* if gpnvm[1] == 0 */
1341 /* boot is via "SAMBA" (rom) */
1343 /* boot is via FLASH */
1344 /* Selection is via gpnvm[2] */
1347 /* NOTE: banks 0 & 1 switch places */
1348 /* if gpnvm[2] == 0 */
1349 /* Bank0 is the boot rom */
1351 /* Bank1 is the boot rom */
1360 .base_address
= FLASH_BANK_BASE_N
,
1361 .controller_address
= 0x400e0A00,
1362 .flash_wait_states
= 6, /* workaround silicon bug */
1364 .size_bytes
= 64 * 1024,
1366 .sector_size
= 16384,
1380 .chipid_cidr
= 0x29480560,
1381 .name
= "at91sam3n1b",
1382 .total_flash_size
= 64 * 1024,
1383 .total_sram_size
= 8 * 1024,
1387 /* System boots at address 0x0 */
1388 /* gpnvm[1] = selects boot code */
1389 /* if gpnvm[1] == 0 */
1390 /* boot is via "SAMBA" (rom) */
1392 /* boot is via FLASH */
1393 /* Selection is via gpnvm[2] */
1396 /* NOTE: banks 0 & 1 switch places */
1397 /* if gpnvm[2] == 0 */
1398 /* Bank0 is the boot rom */
1400 /* Bank1 is the boot rom */
1409 .base_address
= FLASH_BANK_BASE_N
,
1410 .controller_address
= 0x400e0A00,
1411 .flash_wait_states
= 6, /* workaround silicon bug */
1413 .size_bytes
= 64 * 1024,
1415 .sector_size
= 16384,
1429 .chipid_cidr
= 0x29380560,
1430 .name
= "at91sam3n1a",
1431 .total_flash_size
= 64 * 1024,
1432 .total_sram_size
= 8 * 1024,
1436 /* System boots at address 0x0 */
1437 /* gpnvm[1] = selects boot code */
1438 /* if gpnvm[1] == 0 */
1439 /* boot is via "SAMBA" (rom) */
1441 /* boot is via FLASH */
1442 /* Selection is via gpnvm[2] */
1445 /* NOTE: banks 0 & 1 switch places */
1446 /* if gpnvm[2] == 0 */
1447 /* Bank0 is the boot rom */
1449 /* Bank1 is the boot rom */
1458 .base_address
= FLASH_BANK_BASE_N
,
1459 .controller_address
= 0x400e0A00,
1460 .flash_wait_states
= 6, /* workaround silicon bug */
1462 .size_bytes
= 64 * 1024,
1464 .sector_size
= 16384,
1478 .chipid_cidr
= 0x29480360,
1479 .name
= "at91sam3n0b",
1480 .total_flash_size
= 32 * 1024,
1481 .total_sram_size
= 8 * 1024,
1492 .base_address
= FLASH_BANK_BASE_N
,
1493 .controller_address
= 0x400e0A00,
1494 .flash_wait_states
= 6, /* workaround silicon bug */
1496 .size_bytes
= 32 * 1024,
1498 .sector_size
= 16384,
1512 .chipid_cidr
= 0x29380360,
1513 .name
= "at91sam3n0a",
1514 .total_flash_size
= 32 * 1024,
1515 .total_sram_size
= 8 * 1024,
1526 .base_address
= FLASH_BANK_BASE_N
,
1527 .controller_address
= 0x400e0A00,
1528 .flash_wait_states
= 6, /* workaround silicon bug */
1530 .size_bytes
= 32 * 1024,
1532 .sector_size
= 16384,
1546 .chipid_cidr
= 0x29450260,
1547 .name
= "at91sam3n00b",
1548 .total_flash_size
= 16 * 1024,
1549 .total_sram_size
= 4 * 1024,
1560 .base_address
= FLASH_BANK_BASE_N
,
1561 .controller_address
= 0x400e0A00,
1562 .flash_wait_states
= 6, /* workaround silicon bug */
1564 .size_bytes
= 16 * 1024,
1566 .sector_size
= 16384,
1580 .chipid_cidr
= 0x29350260,
1581 .name
= "at91sam3n00a",
1582 .total_flash_size
= 16 * 1024,
1583 .total_sram_size
= 4 * 1024,
1594 .base_address
= FLASH_BANK_BASE_N
,
1595 .controller_address
= 0x400e0A00,
1596 .flash_wait_states
= 6, /* workaround silicon bug */
1598 .size_bytes
= 16 * 1024,
1600 .sector_size
= 16384,
1614 /* Start at91sam3a series*/
1615 /* System boots at address 0x0 */
1616 /* gpnvm[1] = selects boot code */
1617 /* if gpnvm[1] == 0 */
1618 /* boot is via "SAMBA" (rom) */
1620 /* boot is via FLASH */
1621 /* Selection is via gpnvm[2] */
1624 /* NOTE: banks 0 & 1 switch places */
1625 /* if gpnvm[2] == 0 */
1626 /* Bank0 is the boot rom */
1628 /* Bank1 is the boot rom */
1632 .chipid_cidr
= 0x283E0A60,
1633 .name
= "at91sam3a8c",
1634 .total_flash_size
= 512 * 1024,
1635 .total_sram_size
= 96 * 1024,
1645 .base_address
= FLASH_BANK0_BASE_AX
,
1646 .controller_address
= 0x400e0a00,
1647 .flash_wait_states
= 6, /* workaround silicon bug */
1649 .size_bytes
= 256 * 1024,
1651 .sector_size
= 16384,
1660 .base_address
= FLASH_BANK1_BASE_512K_AX
,
1661 .controller_address
= 0x400e0c00,
1662 .flash_wait_states
= 6, /* workaround silicon bug */
1664 .size_bytes
= 256 * 1024,
1666 .sector_size
= 16384,
1673 .chipid_cidr
= 0x283B0960,
1674 .name
= "at91sam3a4c",
1675 .total_flash_size
= 256 * 1024,
1676 .total_sram_size
= 64 * 1024,
1686 .base_address
= FLASH_BANK0_BASE_AX
,
1687 .controller_address
= 0x400e0a00,
1688 .flash_wait_states
= 6, /* workaround silicon bug */
1690 .size_bytes
= 128 * 1024,
1692 .sector_size
= 16384,
1701 .base_address
= FLASH_BANK1_BASE_256K_AX
,
1702 .controller_address
= 0x400e0c00,
1703 .flash_wait_states
= 6, /* workaround silicon bug */
1705 .size_bytes
= 128 * 1024,
1707 .sector_size
= 16384,
1714 /* Start at91sam3x* series */
1715 /* System boots at address 0x0 */
1716 /* gpnvm[1] = selects boot code */
1717 /* if gpnvm[1] == 0 */
1718 /* boot is via "SAMBA" (rom) */
1720 /* boot is via FLASH */
1721 /* Selection is via gpnvm[2] */
1724 /* NOTE: banks 0 & 1 switch places */
1725 /* if gpnvm[2] == 0 */
1726 /* Bank0 is the boot rom */
1728 /* Bank1 is the boot rom */
1730 /*at91sam3x8h - ES has an incorrect CIDR of 0x286E0A20*/
1732 .chipid_cidr
= 0x286E0A20,
1733 .name
= "at91sam3x8h - ES",
1734 .total_flash_size
= 512 * 1024,
1735 .total_sram_size
= 96 * 1024,
1745 .base_address
= FLASH_BANK0_BASE_AX
,
1746 .controller_address
= 0x400e0a00,
1747 .flash_wait_states
= 6, /* workaround silicon bug */
1749 .size_bytes
= 256 * 1024,
1751 .sector_size
= 16384,
1760 .base_address
= FLASH_BANK1_BASE_512K_AX
,
1761 .controller_address
= 0x400e0c00,
1762 .flash_wait_states
= 6, /* workaround silicon bug */
1764 .size_bytes
= 256 * 1024,
1766 .sector_size
= 16384,
1772 /*at91sam3x8h - ES2 and up uses the correct CIDR of 0x286E0A60*/
1774 .chipid_cidr
= 0x286E0A60,
1775 .name
= "at91sam3x8h",
1776 .total_flash_size
= 512 * 1024,
1777 .total_sram_size
= 96 * 1024,
1787 .base_address
= FLASH_BANK0_BASE_AX
,
1788 .controller_address
= 0x400e0a00,
1789 .flash_wait_states
= 6, /* workaround silicon bug */
1791 .size_bytes
= 256 * 1024,
1793 .sector_size
= 16384,
1802 .base_address
= FLASH_BANK1_BASE_512K_AX
,
1803 .controller_address
= 0x400e0c00,
1804 .flash_wait_states
= 6, /* workaround silicon bug */
1806 .size_bytes
= 256 * 1024,
1808 .sector_size
= 16384,
1815 .chipid_cidr
= 0x285E0A60,
1816 .name
= "at91sam3x8e",
1817 .total_flash_size
= 512 * 1024,
1818 .total_sram_size
= 96 * 1024,
1828 .base_address
= FLASH_BANK0_BASE_AX
,
1829 .controller_address
= 0x400e0a00,
1830 .flash_wait_states
= 6, /* workaround silicon bug */
1832 .size_bytes
= 256 * 1024,
1834 .sector_size
= 16384,
1843 .base_address
= FLASH_BANK1_BASE_512K_AX
,
1844 .controller_address
= 0x400e0c00,
1845 .flash_wait_states
= 6, /* workaround silicon bug */
1847 .size_bytes
= 256 * 1024,
1849 .sector_size
= 16384,
1856 .chipid_cidr
= 0x284E0A60,
1857 .name
= "at91sam3x8c",
1858 .total_flash_size
= 512 * 1024,
1859 .total_sram_size
= 96 * 1024,
1869 .base_address
= FLASH_BANK0_BASE_AX
,
1870 .controller_address
= 0x400e0a00,
1871 .flash_wait_states
= 6, /* workaround silicon bug */
1873 .size_bytes
= 256 * 1024,
1875 .sector_size
= 16384,
1884 .base_address
= FLASH_BANK1_BASE_512K_AX
,
1885 .controller_address
= 0x400e0c00,
1886 .flash_wait_states
= 6, /* workaround silicon bug */
1888 .size_bytes
= 256 * 1024,
1890 .sector_size
= 16384,
1897 .chipid_cidr
= 0x285B0960,
1898 .name
= "at91sam3x4e",
1899 .total_flash_size
= 256 * 1024,
1900 .total_sram_size
= 64 * 1024,
1910 .base_address
= FLASH_BANK0_BASE_AX
,
1911 .controller_address
= 0x400e0a00,
1912 .flash_wait_states
= 6, /* workaround silicon bug */
1914 .size_bytes
= 128 * 1024,
1916 .sector_size
= 16384,
1925 .base_address
= FLASH_BANK1_BASE_256K_AX
,
1926 .controller_address
= 0x400e0c00,
1927 .flash_wait_states
= 6, /* workaround silicon bug */
1929 .size_bytes
= 128 * 1024,
1931 .sector_size
= 16384,
1938 .chipid_cidr
= 0x284B0960,
1939 .name
= "at91sam3x4c",
1940 .total_flash_size
= 256 * 1024,
1941 .total_sram_size
= 64 * 1024,
1951 .base_address
= FLASH_BANK0_BASE_AX
,
1952 .controller_address
= 0x400e0a00,
1953 .flash_wait_states
= 6, /* workaround silicon bug */
1955 .size_bytes
= 128 * 1024,
1957 .sector_size
= 16384,
1966 .base_address
= FLASH_BANK1_BASE_256K_AX
,
1967 .controller_address
= 0x400e0c00,
1968 .flash_wait_states
= 6, /* workaround silicon bug */
1970 .size_bytes
= 128 * 1024,
1972 .sector_size
= 16384,
1986 /***********************************************************************
1987 **********************************************************************
1988 **********************************************************************
1989 **********************************************************************
1990 **********************************************************************
1991 **********************************************************************/
1992 /* *ATMEL* style code - from the SAM3 driver code */
1995 * Get the current status of the EEFC and
1996 * the value of some status bits (LOCKE, PROGE).
1997 * @param private - info about the bank
1998 * @param v - result goes here
2000 static int efc_get_status(struct sam3_bank_private
*private, uint32_t *v
)
2003 r
= target_read_u32(private->chip
->target
,
2004 private->controller_address
+ OFFSET_EFC_FSR
,
2006 LOG_DEBUG("Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
2008 ((unsigned int)((*v
>> 2) & 1)),
2009 ((unsigned int)((*v
>> 1) & 1)),
2010 ((unsigned int)((*v
>> 0) & 1)));
2016 * Get the result of the last executed command.
2017 * @param private - info about the bank
2018 * @param v - result goes here
2020 static int efc_get_result(struct sam3_bank_private
*private, uint32_t *v
)
2024 r
= target_read_u32(private->chip
->target
,
2025 private->controller_address
+ OFFSET_EFC_FRR
,
2029 LOG_DEBUG("Result: 0x%08x", ((unsigned int)(rv
)));
2033 static int efc_start_command(struct sam3_bank_private
*private,
2034 unsigned command
, unsigned argument
)
2043 /* Check command & argument */
2046 case AT91C_EFC_FCMD_WP
:
2047 case AT91C_EFC_FCMD_WPL
:
2048 case AT91C_EFC_FCMD_EWP
:
2049 case AT91C_EFC_FCMD_EWPL
:
2050 /* case AT91C_EFC_FCMD_EPL: */
2051 /* case AT91C_EFC_FCMD_EPA: */
2052 case AT91C_EFC_FCMD_SLB
:
2053 case AT91C_EFC_FCMD_CLB
:
2054 n
= (private->size_bytes
/ private->page_size
);
2056 LOG_ERROR("*BUG*: Embedded flash has only %u pages", (unsigned)(n
));
2059 case AT91C_EFC_FCMD_SFB
:
2060 case AT91C_EFC_FCMD_CFB
:
2061 if (argument
>= private->chip
->details
.n_gpnvms
) {
2062 LOG_ERROR("*BUG*: Embedded flash has only %d GPNVMs",
2063 private->chip
->details
.n_gpnvms
);
2067 case AT91C_EFC_FCMD_GETD
:
2068 case AT91C_EFC_FCMD_EA
:
2069 case AT91C_EFC_FCMD_GLB
:
2070 case AT91C_EFC_FCMD_GFB
:
2071 case AT91C_EFC_FCMD_STUI
:
2072 case AT91C_EFC_FCMD_SPUI
:
2074 LOG_ERROR("Argument is meaningless for cmd: %d", command
);
2077 LOG_ERROR("Unknown command %d", command
);
2081 if (command
== AT91C_EFC_FCMD_SPUI
) {
2082 /* this is a very special situation. */
2083 /* Situation (1) - error/retry - see below */
2084 /* And we are being called recursively */
2085 /* Situation (2) - normal, finished reading unique id */
2087 /* it should be "ready" */
2088 efc_get_status(private, &v
);
2090 /* then it is ready */
2094 /* we have done this before */
2095 /* the controller is not responding. */
2096 LOG_ERROR("flash controller(%d) is not ready! Error",
2097 private->bank_number
);
2101 LOG_ERROR("Flash controller(%d) is not ready, attempting reset",
2102 private->bank_number
);
2103 /* we do that by issuing the *STOP* command */
2104 efc_start_command(private, AT91C_EFC_FCMD_SPUI
, 0);
2105 /* above is recursive, and further recursion is blocked by */
2106 /* if (command == AT91C_EFC_FCMD_SPUI) above */
2112 v
= (0x5A << 24) | (argument
<< 8) | command
;
2113 LOG_DEBUG("Command: 0x%08x", ((unsigned int)(v
)));
2114 r
= target_write_u32(private->bank
->target
,
2115 private->controller_address
+ OFFSET_EFC_FCR
, v
);
2117 LOG_DEBUG("Error Write failed");
2122 * Performs the given command and wait until its completion (or an error).
2123 * @param private - info about the bank
2124 * @param command - Command to perform.
2125 * @param argument - Optional command argument.
2126 * @param status - put command status bits here
2128 static int efc_perform_command(struct sam3_bank_private
*private,
2136 int64_t ms_now
, ms_end
;
2142 r
= efc_start_command(private, command
, argument
);
2146 ms_end
= 500 + timeval_ms();
2149 r
= efc_get_status(private, &v
);
2152 ms_now
= timeval_ms();
2153 if (ms_now
> ms_end
) {
2155 LOG_ERROR("Command timeout");
2158 } while ((v
& 1) == 0);
2162 *status
= (v
& 0x6);
2168 * Read the unique ID.
2169 * @param private - info about the bank
2170 * The unique ID is stored in the 'private' structure.
2172 static int flashd_read_uid(struct sam3_bank_private
*private)
2178 private->chip
->cfg
.unique_id
[0] = 0;
2179 private->chip
->cfg
.unique_id
[1] = 0;
2180 private->chip
->cfg
.unique_id
[2] = 0;
2181 private->chip
->cfg
.unique_id
[3] = 0;
2184 r
= efc_start_command(private, AT91C_EFC_FCMD_STUI
, 0);
2188 for (x
= 0; x
< 4; x
++) {
2189 r
= target_read_u32(private->chip
->target
,
2190 private->bank
->base
+ (x
* 4),
2194 private->chip
->cfg
.unique_id
[x
] = v
;
2197 r
= efc_perform_command(private, AT91C_EFC_FCMD_SPUI
, 0, NULL
);
2198 LOG_DEBUG("End: R=%d, id = 0x%08x, 0x%08x, 0x%08x, 0x%08x",
2200 (unsigned int)(private->chip
->cfg
.unique_id
[0]),
2201 (unsigned int)(private->chip
->cfg
.unique_id
[1]),
2202 (unsigned int)(private->chip
->cfg
.unique_id
[2]),
2203 (unsigned int)(private->chip
->cfg
.unique_id
[3]));
2209 * Erases the entire flash.
2210 * @param private - the info about the bank.
2212 static int flashd_erase_entire_bank(struct sam3_bank_private
*private)
2215 return efc_perform_command(private, AT91C_EFC_FCMD_EA
, 0, NULL
);
2219 * Gets current GPNVM state.
2220 * @param private - info about the bank.
2221 * @param gpnvm - GPNVM bit index.
2222 * @param puthere - result stored here.
2224 /* ------------------------------------------------------------------------------ */
2225 static int flashd_get_gpnvm(struct sam3_bank_private
*private, unsigned gpnvm
, unsigned *puthere
)
2231 if (private->bank_number
!= 0) {
2232 LOG_ERROR("GPNVM only works with Bank0");
2236 if (gpnvm
>= private->chip
->details
.n_gpnvms
) {
2237 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
2238 gpnvm
, private->chip
->details
.n_gpnvms
);
2242 /* Get GPNVMs status */
2243 r
= efc_perform_command(private, AT91C_EFC_FCMD_GFB
, 0, NULL
);
2244 if (r
!= ERROR_OK
) {
2245 LOG_ERROR("Failed");
2249 r
= efc_get_result(private, &v
);
2252 /* Check if GPNVM is set */
2253 /* get the bit and make it a 0/1 */
2254 *puthere
= (v
>> gpnvm
) & 1;
2261 * Clears the selected GPNVM bit.
2262 * @param private info about the bank
2263 * @param gpnvm GPNVM index.
2264 * @returns 0 if successful; otherwise returns an error code.
2266 static int flashd_clr_gpnvm(struct sam3_bank_private
*private, unsigned gpnvm
)
2272 if (private->bank_number
!= 0) {
2273 LOG_ERROR("GPNVM only works with Bank0");
2277 if (gpnvm
>= private->chip
->details
.n_gpnvms
) {
2278 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
2279 gpnvm
, private->chip
->details
.n_gpnvms
);
2283 r
= flashd_get_gpnvm(private, gpnvm
, &v
);
2284 if (r
!= ERROR_OK
) {
2285 LOG_DEBUG("Failed: %d", r
);
2288 r
= efc_perform_command(private, AT91C_EFC_FCMD_CFB
, gpnvm
, NULL
);
2289 LOG_DEBUG("End: %d", r
);
2294 * Sets the selected GPNVM bit.
2295 * @param private info about the bank
2296 * @param gpnvm GPNVM index.
2298 static int flashd_set_gpnvm(struct sam3_bank_private
*private, unsigned gpnvm
)
2303 if (private->bank_number
!= 0) {
2304 LOG_ERROR("GPNVM only works with Bank0");
2308 if (gpnvm
>= private->chip
->details
.n_gpnvms
) {
2309 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
2310 gpnvm
, private->chip
->details
.n_gpnvms
);
2314 r
= flashd_get_gpnvm(private, gpnvm
, &v
);
2322 r
= efc_perform_command(private, AT91C_EFC_FCMD_SFB
, gpnvm
, NULL
);
2328 * Returns a bit field (at most 64) of locked regions within a page.
2329 * @param private info about the bank
2330 * @param v where to store locked bits
2332 static int flashd_get_lock_bits(struct sam3_bank_private
*private, uint32_t *v
)
2336 r
= efc_perform_command(private, AT91C_EFC_FCMD_GLB
, 0, NULL
);
2338 r
= efc_get_result(private, v
);
2339 LOG_DEBUG("End: %d", r
);
2344 * Unlocks all the regions in the given address range.
2345 * @param private info about the bank
2346 * @param start_sector first sector to unlock
2347 * @param end_sector last (inclusive) to unlock
2350 static int flashd_unlock(struct sam3_bank_private
*private,
2351 unsigned start_sector
,
2352 unsigned end_sector
)
2357 uint32_t pages_per_sector
;
2359 pages_per_sector
= private->sector_size
/ private->page_size
;
2361 /* Unlock all pages */
2362 while (start_sector
<= end_sector
) {
2363 pg
= start_sector
* pages_per_sector
;
2365 r
= efc_perform_command(private, AT91C_EFC_FCMD_CLB
, pg
, &status
);
2376 * @param private - info about the bank
2377 * @param start_sector - first sector to lock
2378 * @param end_sector - last sector (inclusive) to lock
2380 static int flashd_lock(struct sam3_bank_private
*private,
2381 unsigned start_sector
,
2382 unsigned end_sector
)
2386 uint32_t pages_per_sector
;
2389 pages_per_sector
= private->sector_size
/ private->page_size
;
2391 /* Lock all pages */
2392 while (start_sector
<= end_sector
) {
2393 pg
= start_sector
* pages_per_sector
;
2395 r
= efc_perform_command(private, AT91C_EFC_FCMD_SLB
, pg
, &status
);
2403 /****** END SAM3 CODE ********/
2405 /* begin helpful debug code */
2406 /* print the fieldname, the field value, in dec & hex, and return field value */
2407 static uint32_t sam3_reg_fieldname(struct sam3_chip
*chip
,
2408 const char *regname
,
2417 /* extract the field */
2419 v
= v
& ((1 << width
)-1);
2428 /* show the basics */
2429 LOG_USER_N("\t%*s: %*" PRIu32
" [0x%0*" PRIx32
"] ",
2430 REG_NAME_WIDTH
, regname
,
2436 static const char _unknown
[] = "unknown";
2437 static const char *const eproc_names
[] = {
2441 "Cortex-M3", /* 3 */
2443 "arm926ejs", /* 5 */
2456 #define nvpsize2 nvpsize /* these two tables are identical */
2457 static const char *const nvpsize
[] = {
2460 "16K bytes", /* 2 */
2461 "32K bytes", /* 3 */
2463 "64K bytes", /* 5 */
2465 "128K bytes", /* 7 */
2467 "256K bytes", /* 9 */
2468 "512K bytes", /* 10 */
2470 "1024K bytes", /* 12 */
2472 "2048K bytes", /* 14 */
2476 static const char *const sramsize
[] = {
2477 "48K Bytes", /* 0 */
2481 "112K Bytes", /* 4 */
2483 "80K Bytes", /* 6 */
2484 "160K Bytes", /* 7 */
2486 "16K Bytes", /* 9 */
2487 "32K Bytes", /* 10 */
2488 "64K Bytes", /* 11 */
2489 "128K Bytes", /* 12 */
2490 "256K Bytes", /* 13 */
2491 "96K Bytes", /* 14 */
2492 "512K Bytes", /* 15 */
2496 static const struct archnames
{ unsigned value
; const char *name
; } archnames
[] = {
2497 { 0x19, "AT91SAM9xx Series" },
2498 { 0x29, "AT91SAM9XExx Series" },
2499 { 0x34, "AT91x34 Series" },
2500 { 0x37, "CAP7 Series" },
2501 { 0x39, "CAP9 Series" },
2502 { 0x3B, "CAP11 Series" },
2503 { 0x40, "AT91x40 Series" },
2504 { 0x42, "AT91x42 Series" },
2505 { 0x55, "AT91x55 Series" },
2506 { 0x60, "AT91SAM7Axx Series" },
2507 { 0x61, "AT91SAM7AQxx Series" },
2508 { 0x63, "AT91x63 Series" },
2509 { 0x70, "AT91SAM7Sxx Series" },
2510 { 0x71, "AT91SAM7XCxx Series" },
2511 { 0x72, "AT91SAM7SExx Series" },
2512 { 0x73, "AT91SAM7Lxx Series" },
2513 { 0x75, "AT91SAM7Xxx Series" },
2514 { 0x76, "AT91SAM7SLxx Series" },
2515 { 0x80, "ATSAM3UxC Series (100-pin version)" },
2516 { 0x81, "ATSAM3UxE Series (144-pin version)" },
2517 { 0x83, "ATSAM3AxC Series (100-pin version)" },
2518 { 0x84, "ATSAM3XxC Series (100-pin version)" },
2519 { 0x85, "ATSAM3XxE Series (144-pin version)" },
2520 { 0x86, "ATSAM3XxG Series (208/217-pin version)" },
2521 { 0x88, "ATSAM3SxA Series (48-pin version)" },
2522 { 0x89, "ATSAM3SxB Series (64-pin version)" },
2523 { 0x8A, "ATSAM3SxC Series (100-pin version)" },
2524 { 0x92, "AT91x92 Series" },
2525 { 0x93, "ATSAM3NxA Series (48-pin version)" },
2526 { 0x94, "ATSAM3NxB Series (64-pin version)" },
2527 { 0x95, "ATSAM3NxC Series (100-pin version)" },
2528 { 0x98, "ATSAM3SDxA Series (48-pin version)" },
2529 { 0x99, "ATSAM3SDxB Series (64-pin version)" },
2530 { 0x9A, "ATSAM3SDxC Series (100-pin version)" },
2531 { 0xA5, "ATSAM5A" },
2532 { 0xF0, "AT75Cxx Series" },
2536 static const char *const nvptype
[] = {
2538 "romless or onchip flash", /* 1 */
2539 "embedded flash memory",/* 2 */
2540 "rom(nvpsiz) + embedded flash (nvpsiz2)", /* 3 */
2541 "sram emulating flash", /* 4 */
2547 static const char *_yes_or_no(uint32_t v
)
2555 static const char *const _rc_freq
[] = {
2556 "4 MHz", "8 MHz", "12 MHz", "reserved"
2559 static void sam3_explain_ckgr_mor(struct sam3_chip
*chip
)
2564 v
= sam3_reg_fieldname(chip
, "MOSCXTEN", chip
->cfg
.CKGR_MOR
, 0, 1);
2565 LOG_USER("(main xtal enabled: %s)", _yes_or_no(v
));
2566 v
= sam3_reg_fieldname(chip
, "MOSCXTBY", chip
->cfg
.CKGR_MOR
, 1, 1);
2567 LOG_USER("(main osc bypass: %s)", _yes_or_no(v
));
2568 rcen
= sam3_reg_fieldname(chip
, "MOSCRCEN", chip
->cfg
.CKGR_MOR
, 3, 1);
2569 LOG_USER("(onchip RC-OSC enabled: %s)", _yes_or_no(rcen
));
2570 v
= sam3_reg_fieldname(chip
, "MOSCRCF", chip
->cfg
.CKGR_MOR
, 4, 3);
2571 LOG_USER("(onchip RC-OSC freq: %s)", _rc_freq
[v
]);
2573 chip
->cfg
.rc_freq
= 0;
2577 chip
->cfg
.rc_freq
= 0;
2580 chip
->cfg
.rc_freq
= 4 * 1000 * 1000;
2583 chip
->cfg
.rc_freq
= 8 * 1000 * 1000;
2586 chip
->cfg
.rc_freq
= 12 * 1000 * 1000;
2591 v
= sam3_reg_fieldname(chip
, "MOSCXTST", chip
->cfg
.CKGR_MOR
, 8, 8);
2592 LOG_USER("(startup clks, time= %f uSecs)",
2593 ((float)(v
* 1000000)) / ((float)(chip
->cfg
.slow_freq
)));
2594 v
= sam3_reg_fieldname(chip
, "MOSCSEL", chip
->cfg
.CKGR_MOR
, 24, 1);
2595 LOG_USER("(mainosc source: %s)",
2596 v
? "external xtal" : "internal RC");
2598 v
= sam3_reg_fieldname(chip
, "CFDEN", chip
->cfg
.CKGR_MOR
, 25, 1);
2599 LOG_USER("(clock failure enabled: %s)",
2603 static void sam3_explain_chipid_cidr(struct sam3_chip
*chip
)
2609 sam3_reg_fieldname(chip
, "Version", chip
->cfg
.CHIPID_CIDR
, 0, 5);
2612 v
= sam3_reg_fieldname(chip
, "EPROC", chip
->cfg
.CHIPID_CIDR
, 5, 3);
2613 LOG_USER("%s", eproc_names
[v
]);
2615 v
= sam3_reg_fieldname(chip
, "NVPSIZE", chip
->cfg
.CHIPID_CIDR
, 8, 4);
2616 LOG_USER("%s", nvpsize
[v
]);
2618 v
= sam3_reg_fieldname(chip
, "NVPSIZE2", chip
->cfg
.CHIPID_CIDR
, 12, 4);
2619 LOG_USER("%s", nvpsize2
[v
]);
2621 v
= sam3_reg_fieldname(chip
, "SRAMSIZE", chip
->cfg
.CHIPID_CIDR
, 16, 4);
2622 LOG_USER("%s", sramsize
[v
]);
2624 v
= sam3_reg_fieldname(chip
, "ARCH", chip
->cfg
.CHIPID_CIDR
, 20, 8);
2626 for (x
= 0; archnames
[x
].name
; x
++) {
2627 if (v
== archnames
[x
].value
) {
2628 cp
= archnames
[x
].name
;
2635 v
= sam3_reg_fieldname(chip
, "NVPTYP", chip
->cfg
.CHIPID_CIDR
, 28, 3);
2636 LOG_USER("%s", nvptype
[v
]);
2638 v
= sam3_reg_fieldname(chip
, "EXTID", chip
->cfg
.CHIPID_CIDR
, 31, 1);
2639 LOG_USER("(exists: %s)", _yes_or_no(v
));
2642 static void sam3_explain_ckgr_mcfr(struct sam3_chip
*chip
)
2646 v
= sam3_reg_fieldname(chip
, "MAINFRDY", chip
->cfg
.CKGR_MCFR
, 16, 1);
2647 LOG_USER("(main ready: %s)", _yes_or_no(v
));
2649 v
= sam3_reg_fieldname(chip
, "MAINF", chip
->cfg
.CKGR_MCFR
, 0, 16);
2651 v
= (v
* chip
->cfg
.slow_freq
) / 16;
2652 chip
->cfg
.mainosc_freq
= v
;
2654 LOG_USER("(%3.03f Mhz (%" PRIu32
".%03" PRIu32
"khz slowclk)",
2656 (uint32_t)(chip
->cfg
.slow_freq
/ 1000),
2657 (uint32_t)(chip
->cfg
.slow_freq
% 1000));
2660 static void sam3_explain_ckgr_plla(struct sam3_chip
*chip
)
2662 uint32_t mula
, diva
;
2664 diva
= sam3_reg_fieldname(chip
, "DIVA", chip
->cfg
.CKGR_PLLAR
, 0, 8);
2666 mula
= sam3_reg_fieldname(chip
, "MULA", chip
->cfg
.CKGR_PLLAR
, 16, 11);
2668 chip
->cfg
.plla_freq
= 0;
2670 LOG_USER("\tPLLA Freq: (Disabled,mula = 0)");
2672 LOG_USER("\tPLLA Freq: (Disabled,diva = 0)");
2673 else if (diva
>= 1) {
2674 chip
->cfg
.plla_freq
= (chip
->cfg
.mainosc_freq
* (mula
+ 1) / diva
);
2675 LOG_USER("\tPLLA Freq: %3.03f MHz",
2676 _tomhz(chip
->cfg
.plla_freq
));
2680 static void sam3_explain_mckr(struct sam3_chip
*chip
)
2682 uint32_t css
, pres
, fin
= 0;
2684 const char *cp
= NULL
;
2686 css
= sam3_reg_fieldname(chip
, "CSS", chip
->cfg
.PMC_MCKR
, 0, 2);
2689 fin
= chip
->cfg
.slow_freq
;
2693 fin
= chip
->cfg
.mainosc_freq
;
2697 fin
= chip
->cfg
.plla_freq
;
2701 if (chip
->cfg
.CKGR_UCKR
& (1 << 16)) {
2702 fin
= 480 * 1000 * 1000;
2706 cp
= "upll (*ERROR* UPLL is disabled)";
2714 LOG_USER("%s (%3.03f Mhz)",
2717 pres
= sam3_reg_fieldname(chip
, "PRES", chip
->cfg
.PMC_MCKR
, 4, 3);
2718 switch (pres
& 0x07) {
2721 cp
= "selected clock";
2755 LOG_USER("(%s)", cp
);
2757 /* sam3 has a *SINGLE* clock - */
2758 /* other at91 series parts have divisors for these. */
2759 chip
->cfg
.cpu_freq
= fin
;
2760 chip
->cfg
.mclk_freq
= fin
;
2761 chip
->cfg
.fclk_freq
= fin
;
2762 LOG_USER("\t\tResult CPU Freq: %3.03f",
2767 static struct sam3_chip
*target2sam3(struct target
*target
)
2769 struct sam3_chip
*chip
;
2774 chip
= all_sam3_chips
;
2776 if (chip
->target
== target
)
2777 break; /* return below */
2785 static uint32_t *sam3_get_reg_ptr(struct sam3_cfg
*cfg
, const struct sam3_reg_list
*list
)
2787 /* this function exists to help */
2788 /* keep funky offsetof() errors */
2789 /* and casting from causing bugs */
2791 /* By using prototypes - we can detect what would */
2792 /* be casting errors. */
2794 return (uint32_t *)(void *)(((char *)(cfg
)) + list
->struct_offset
);
2798 #define SAM3_ENTRY(NAME, FUNC) { .address = SAM3_ ## NAME, .struct_offset = offsetof( \
2800 NAME), # NAME, FUNC }
2801 static const struct sam3_reg_list sam3_all_regs
[] = {
2802 SAM3_ENTRY(CKGR_MOR
, sam3_explain_ckgr_mor
),
2803 SAM3_ENTRY(CKGR_MCFR
, sam3_explain_ckgr_mcfr
),
2804 SAM3_ENTRY(CKGR_PLLAR
, sam3_explain_ckgr_plla
),
2805 SAM3_ENTRY(CKGR_UCKR
, NULL
),
2806 SAM3_ENTRY(PMC_FSMR
, NULL
),
2807 SAM3_ENTRY(PMC_FSPR
, NULL
),
2808 SAM3_ENTRY(PMC_IMR
, NULL
),
2809 SAM3_ENTRY(PMC_MCKR
, sam3_explain_mckr
),
2810 SAM3_ENTRY(PMC_PCK0
, NULL
),
2811 SAM3_ENTRY(PMC_PCK1
, NULL
),
2812 SAM3_ENTRY(PMC_PCK2
, NULL
),
2813 SAM3_ENTRY(PMC_PCSR
, NULL
),
2814 SAM3_ENTRY(PMC_SCSR
, NULL
),
2815 SAM3_ENTRY(PMC_SR
, NULL
),
2816 SAM3_ENTRY(CHIPID_CIDR
, sam3_explain_chipid_cidr
),
2817 SAM3_ENTRY(CHIPID_CIDR2
, sam3_explain_chipid_cidr
),
2818 SAM3_ENTRY(CHIPID_EXID
, NULL
),
2819 SAM3_ENTRY(CHIPID_EXID2
, NULL
),
2820 /* TERMINATE THE LIST */
2825 static struct sam3_bank_private
*get_sam3_bank_private(struct flash_bank
*bank
)
2827 return bank
->driver_priv
;
2831 * Given a pointer to where it goes in the structure,
2832 * determine the register name, address from the all registers table.
2834 static const struct sam3_reg_list
*sam3_get_reg(struct sam3_chip
*chip
, uint32_t *goes_here
)
2836 const struct sam3_reg_list
*reg
;
2838 reg
= &(sam3_all_regs
[0]);
2842 /* calculate where this one go.. */
2843 /* it is "possibly" this register. */
2845 possible
= ((uint32_t *)(void *)(((char *)(&(chip
->cfg
))) + reg
->struct_offset
));
2847 /* well? Is it this register */
2848 if (possible
== goes_here
) {
2856 /* This is *TOTAL*PANIC* - we are totally screwed. */
2857 LOG_ERROR("INVALID SAM3 REGISTER");
2861 static int sam3_read_this_reg(struct sam3_chip
*chip
, uint32_t *goes_here
)
2863 const struct sam3_reg_list
*reg
;
2866 reg
= sam3_get_reg(chip
, goes_here
);
2870 r
= target_read_u32(chip
->target
, reg
->address
, goes_here
);
2871 if (r
!= ERROR_OK
) {
2872 LOG_ERROR("Cannot read SAM3 register: %s @ 0x%08x, Err: %d",
2873 reg
->name
, (unsigned)(reg
->address
), r
);
2878 static int sam3_read_all_regs(struct sam3_chip
*chip
)
2881 const struct sam3_reg_list
*reg
;
2883 reg
= &(sam3_all_regs
[0]);
2885 r
= sam3_read_this_reg(chip
,
2886 sam3_get_reg_ptr(&(chip
->cfg
), reg
));
2887 if (r
!= ERROR_OK
) {
2888 LOG_ERROR("Cannot read SAM3 register: %s @ 0x%08x, Error: %d",
2889 reg
->name
, ((unsigned)(reg
->address
)), r
);
2895 /* Chip identification register
2897 * Unfortunately, the chip identification register is not at
2898 * a constant address across all of the SAM3 series'. As a
2899 * consequence, a simple heuristic is used to find where it's
2902 * If the contents at the first address is zero, then we know
2903 * that the second address is where the chip id register is.
2904 * We can deduce this because for those SAM's that have the
2905 * chip id @ 0x400e0940, the first address, 0x400e0740, is
2906 * located in the memory map of the Power Management Controller
2907 * (PMC). Furthermore, the address is not used by the PMC.
2908 * So when read, the memory controller returns zero.*/
2909 if (chip
->cfg
.CHIPID_CIDR
== 0) {
2910 /*Put the correct CIDR and EXID values in the chip structure */
2911 chip
->cfg
.CHIPID_CIDR
= chip
->cfg
.CHIPID_CIDR2
;
2912 chip
->cfg
.CHIPID_EXID
= chip
->cfg
.CHIPID_EXID2
;
2917 static int sam3_get_info(struct sam3_chip
*chip
)
2919 const struct sam3_reg_list
*reg
;
2922 reg
= &(sam3_all_regs
[0]);
2924 /* display all regs */
2925 LOG_DEBUG("Start: %s", reg
->name
);
2926 regval
= *sam3_get_reg_ptr(&(chip
->cfg
), reg
);
2927 LOG_USER("%*s: [0x%08" PRIx32
"] -> 0x%08" PRIx32
,
2932 if (reg
->explain_func
)
2933 (*(reg
->explain_func
))(chip
);
2934 LOG_DEBUG("End: %s", reg
->name
);
2937 LOG_USER(" rc-osc: %3.03f MHz", _tomhz(chip
->cfg
.rc_freq
));
2938 LOG_USER(" mainosc: %3.03f MHz", _tomhz(chip
->cfg
.mainosc_freq
));
2939 LOG_USER(" plla: %3.03f MHz", _tomhz(chip
->cfg
.plla_freq
));
2940 LOG_USER(" cpu-freq: %3.03f MHz", _tomhz(chip
->cfg
.cpu_freq
));
2941 LOG_USER("mclk-freq: %3.03f MHz", _tomhz(chip
->cfg
.mclk_freq
));
2943 LOG_USER(" UniqueId: 0x%08" PRIx32
" 0x%08" PRIx32
" 0x%08" PRIx32
" 0x%08" PRIx32
,
2944 chip
->cfg
.unique_id
[0],
2945 chip
->cfg
.unique_id
[1],
2946 chip
->cfg
.unique_id
[2],
2947 chip
->cfg
.unique_id
[3]);
2952 static int sam3_protect_check(struct flash_bank
*bank
)
2957 struct sam3_bank_private
*private;
2960 if (bank
->target
->state
!= TARGET_HALTED
) {
2961 LOG_ERROR("Target not halted");
2962 return ERROR_TARGET_NOT_HALTED
;
2965 private = get_sam3_bank_private(bank
);
2967 LOG_ERROR("no private for this bank?");
2970 if (!(private->probed
))
2971 return ERROR_FLASH_BANK_NOT_PROBED
;
2973 r
= flashd_get_lock_bits(private, &v
);
2974 if (r
!= ERROR_OK
) {
2975 LOG_DEBUG("Failed: %d", r
);
2979 for (x
= 0; x
< private->nsectors
; x
++)
2980 bank
->sectors
[x
].is_protected
= (!!(v
& (1 << x
)));
2985 FLASH_BANK_COMMAND_HANDLER(sam3_flash_bank_command
)
2987 struct sam3_chip
*chip
;
2989 chip
= all_sam3_chips
;
2991 /* is this an existing chip? */
2993 if (chip
->target
== bank
->target
)
2999 /* this is a *NEW* chip */
3000 chip
= calloc(1, sizeof(struct sam3_chip
));
3002 LOG_ERROR("NO RAM!");
3005 chip
->target
= bank
->target
;
3006 /* insert at head */
3007 chip
->next
= all_sam3_chips
;
3008 all_sam3_chips
= chip
;
3009 chip
->target
= bank
->target
;
3010 /* assumption is this runs at 32khz */
3011 chip
->cfg
.slow_freq
= 32768;
3012 chip
->probed
= false;
3015 switch (bank
->base
) {
3017 LOG_ERROR("Address 0x%08x invalid bank address (try 0x%08x or 0x%08x "
3018 "[at91sam3u series] or 0x%08x [at91sam3s series] or "
3019 "0x%08x [at91sam3n series] or 0x%08x or 0x%08x or 0x%08x[at91sam3ax series] )",
3020 ((unsigned int)(bank
->base
)),
3021 ((unsigned int)(FLASH_BANK0_BASE_U
)),
3022 ((unsigned int)(FLASH_BANK1_BASE_U
)),
3023 ((unsigned int)(FLASH_BANK_BASE_S
)),
3024 ((unsigned int)(FLASH_BANK_BASE_N
)),
3025 ((unsigned int)(FLASH_BANK0_BASE_AX
)),
3026 ((unsigned int)(FLASH_BANK1_BASE_256K_AX
)),
3027 ((unsigned int)(FLASH_BANK1_BASE_512K_AX
)));
3030 /* at91sam3s and at91sam3n series only has bank 0*/
3031 /* at91sam3u and at91sam3ax series has the same address for bank 0*/
3032 case FLASH_BANK_BASE_S
:
3033 case FLASH_BANK0_BASE_U
:
3034 bank
->driver_priv
= &(chip
->details
.bank
[0]);
3035 bank
->bank_number
= 0;
3036 chip
->details
.bank
[0].chip
= chip
;
3037 chip
->details
.bank
[0].bank
= bank
;
3040 /* Bank 1 of at91sam3u or at91sam3ax series */
3041 case FLASH_BANK1_BASE_U
:
3042 case FLASH_BANK1_BASE_256K_AX
:
3043 case FLASH_BANK1_BASE_512K_AX
:
3044 bank
->driver_priv
= &(chip
->details
.bank
[1]);
3045 bank
->bank_number
= 1;
3046 chip
->details
.bank
[1].chip
= chip
;
3047 chip
->details
.bank
[1].bank
= bank
;
3051 /* we initialize after probing. */
3056 * Remove all chips from the internal list without distinguishing which one
3057 * is owned by this bank. This simplification works only for one shot
3058 * deallocation like current flash_free_all_banks()
3060 static void sam3_free_driver_priv(struct flash_bank
*bank
)
3062 struct sam3_chip
*chip
= all_sam3_chips
;
3064 struct sam3_chip
*next
= chip
->next
;
3068 all_sam3_chips
= NULL
;
3071 static int sam3_get_details(struct sam3_bank_private
*private)
3073 const struct sam3_chip_details
*details
;
3074 struct sam3_chip
*chip
;
3075 struct flash_bank
*saved_banks
[SAM3_MAX_FLASH_BANKS
];
3079 details
= all_sam3_details
;
3080 while (details
->name
) {
3081 /* Compare cidr without version bits */
3082 if (((details
->chipid_cidr
^ private->chip
->cfg
.CHIPID_CIDR
) & 0xFFFFFFE0) == 0)
3087 if (!details
->name
) {
3088 LOG_ERROR("SAM3 ChipID 0x%08x not found in table (perhaps you can ID this chip?)",
3089 (unsigned int)(private->chip
->cfg
.CHIPID_CIDR
));
3090 /* Help the victim, print details about the chip */
3091 LOG_INFO("SAM3 CHIPID_CIDR: 0x%08" PRIx32
" decodes as follows",
3092 private->chip
->cfg
.CHIPID_CIDR
);
3093 sam3_explain_chipid_cidr(private->chip
);
3097 /* DANGER: THERE ARE DRAGONS HERE */
3099 /* get our chip - it is going */
3100 /* to be over-written shortly */
3101 chip
= private->chip
;
3103 /* Note that, in reality: */
3105 /* private = &(chip->details.bank[0]) */
3106 /* or private = &(chip->details.bank[1]) */
3109 /* save the "bank" pointers */
3110 for (x
= 0; x
< SAM3_MAX_FLASH_BANKS
; x
++)
3111 saved_banks
[x
] = chip
->details
.bank
[x
].bank
;
3113 /* Overwrite the "details" structure. */
3114 memcpy(&(private->chip
->details
),
3116 sizeof(private->chip
->details
));
3118 /* now fix the ghosted pointers */
3119 for (x
= 0; x
< SAM3_MAX_FLASH_BANKS
; x
++) {
3120 chip
->details
.bank
[x
].chip
= chip
;
3121 chip
->details
.bank
[x
].bank
= saved_banks
[x
];
3124 /* update the *BANK*SIZE* */
3130 static int _sam3_probe(struct flash_bank
*bank
, int noise
)
3133 struct sam3_bank_private
*private;
3136 LOG_DEBUG("Begin: Bank: %u, Noise: %d", bank
->bank_number
, noise
);
3137 if (bank
->target
->state
!= TARGET_HALTED
) {
3138 LOG_ERROR("Target not halted");
3139 return ERROR_TARGET_NOT_HALTED
;
3142 private = get_sam3_bank_private(bank
);
3144 LOG_ERROR("Invalid/unknown bank number");
3148 r
= sam3_read_all_regs(private->chip
);
3153 if (private->chip
->probed
)
3154 r
= sam3_get_info(private->chip
);
3156 r
= sam3_get_details(private);
3160 /* update the flash bank size */
3161 for (unsigned int x
= 0; x
< SAM3_MAX_FLASH_BANKS
; x
++) {
3162 if (bank
->base
== private->chip
->details
.bank
[x
].base_address
) {
3163 bank
->size
= private->chip
->details
.bank
[x
].size_bytes
;
3168 if (!bank
->sectors
) {
3169 bank
->sectors
= calloc(private->nsectors
, (sizeof((bank
->sectors
)[0])));
3170 if (!bank
->sectors
) {
3171 LOG_ERROR("No memory!");
3174 bank
->num_sectors
= private->nsectors
;
3176 for (unsigned int x
= 0; x
< bank
->num_sectors
; x
++) {
3177 bank
->sectors
[x
].size
= private->sector_size
;
3178 bank
->sectors
[x
].offset
= x
* (private->sector_size
);
3179 /* mark as unknown */
3180 bank
->sectors
[x
].is_erased
= -1;
3181 bank
->sectors
[x
].is_protected
= -1;
3185 private->probed
= true;
3187 r
= sam3_protect_check(bank
);
3191 LOG_DEBUG("Bank = %d, nbanks = %d",
3192 private->bank_number
, private->chip
->details
.n_banks
);
3193 if ((private->bank_number
+ 1) == private->chip
->details
.n_banks
) {
3194 /* read unique id, */
3195 /* it appears to be associated with the *last* flash bank. */
3196 flashd_read_uid(private);
3202 static int sam3_probe(struct flash_bank
*bank
)
3204 return _sam3_probe(bank
, 1);
3207 static int sam3_auto_probe(struct flash_bank
*bank
)
3209 return _sam3_probe(bank
, 0);
3212 static int sam3_erase(struct flash_bank
*bank
, unsigned int first
,
3215 struct sam3_bank_private
*private;
3219 if (bank
->target
->state
!= TARGET_HALTED
) {
3220 LOG_ERROR("Target not halted");
3221 return ERROR_TARGET_NOT_HALTED
;
3224 r
= sam3_auto_probe(bank
);
3225 if (r
!= ERROR_OK
) {
3226 LOG_DEBUG("Here,r=%d", r
);
3230 private = get_sam3_bank_private(bank
);
3231 if (!(private->probed
))
3232 return ERROR_FLASH_BANK_NOT_PROBED
;
3234 if ((first
== 0) && ((last
+ 1) == private->nsectors
)) {
3237 return flashd_erase_entire_bank(private);
3239 LOG_INFO("sam3 auto-erases while programming (request ignored)");
3243 static int sam3_protect(struct flash_bank
*bank
, int set
, unsigned int first
,
3246 struct sam3_bank_private
*private;
3250 if (bank
->target
->state
!= TARGET_HALTED
) {
3251 LOG_ERROR("Target not halted");
3252 return ERROR_TARGET_NOT_HALTED
;
3255 private = get_sam3_bank_private(bank
);
3256 if (!(private->probed
))
3257 return ERROR_FLASH_BANK_NOT_PROBED
;
3260 r
= flashd_lock(private, first
, last
);
3262 r
= flashd_unlock(private, first
, last
);
3263 LOG_DEBUG("End: r=%d", r
);
3269 static int sam3_page_read(struct sam3_bank_private
*private, unsigned pagenum
, uint8_t *buf
)
3274 adr
= pagenum
* private->page_size
;
3275 adr
+= private->base_address
;
3277 r
= target_read_memory(private->chip
->target
,
3279 4, /* THIS*MUST*BE* in 32bit values */
3280 private->page_size
/ 4,
3283 LOG_ERROR("SAM3: Flash program failed to read page phys address: 0x%08x",
3284 (unsigned int)(adr
));
3288 static int sam3_page_write(struct sam3_bank_private
*private, unsigned pagenum
, const uint8_t *buf
)
3292 uint32_t fmr
; /* EEFC Flash Mode Register */
3295 adr
= pagenum
* private->page_size
;
3296 adr
+= private->base_address
;
3298 /* Get flash mode register value */
3299 r
= target_read_u32(private->chip
->target
, private->controller_address
, &fmr
);
3301 LOG_DEBUG("Error Read failed: read flash mode register");
3303 /* Clear flash wait state field */
3306 /* set FWS (flash wait states) field in the FMR (flash mode register) */
3307 fmr
|= (private->flash_wait_states
<< 8);
3309 LOG_DEBUG("Flash Mode: 0x%08x", ((unsigned int)(fmr
)));
3310 r
= target_write_u32(private->bank
->target
, private->controller_address
, fmr
);
3312 LOG_DEBUG("Error Write failed: set flash mode register");
3314 LOG_DEBUG("Wr Page %u @ phys address: 0x%08x", pagenum
, (unsigned int)(adr
));
3315 r
= target_write_memory(private->chip
->target
,
3317 4, /* THIS*MUST*BE* in 32bit values */
3318 private->page_size
/ 4,
3320 if (r
!= ERROR_OK
) {
3321 LOG_ERROR("SAM3: Failed to write (buffer) page at phys address 0x%08x",
3322 (unsigned int)(adr
));
3326 r
= efc_perform_command(private,
3327 /* send Erase & Write Page */
3333 LOG_ERROR("SAM3: Error performing Erase & Write page @ phys address 0x%08x",
3334 (unsigned int)(adr
));
3335 if (status
& (1 << 2)) {
3336 LOG_ERROR("SAM3: Page @ Phys address 0x%08x is locked", (unsigned int)(adr
));
3339 if (status
& (1 << 1)) {
3340 LOG_ERROR("SAM3: Flash Command error @phys address 0x%08x", (unsigned int)(adr
));
3346 static int sam3_write(struct flash_bank
*bank
,
3347 const uint8_t *buffer
,
3355 unsigned page_offset
;
3356 struct sam3_bank_private
*private;
3357 uint8_t *pagebuffer
;
3359 /* in case we bail further below, set this to null */
3362 /* ignore dumb requests */
3368 if (bank
->target
->state
!= TARGET_HALTED
) {
3369 LOG_ERROR("Target not halted");
3370 r
= ERROR_TARGET_NOT_HALTED
;
3374 private = get_sam3_bank_private(bank
);
3375 if (!(private->probed
)) {
3376 r
= ERROR_FLASH_BANK_NOT_PROBED
;
3380 if ((offset
+ count
) > private->size_bytes
) {
3381 LOG_ERROR("Flash write error - past end of bank");
3382 LOG_ERROR(" offset: 0x%08x, count 0x%08x, BankEnd: 0x%08x",
3383 (unsigned int)(offset
),
3384 (unsigned int)(count
),
3385 (unsigned int)(private->size_bytes
));
3390 pagebuffer
= malloc(private->page_size
);
3392 LOG_ERROR("No memory for %d Byte page buffer", (int)(private->page_size
));
3397 /* what page do we start & end in? */
3398 page_cur
= offset
/ private->page_size
;
3399 page_end
= (offset
+ count
- 1) / private->page_size
;
3401 LOG_DEBUG("Offset: 0x%08x, Count: 0x%08x", (unsigned int)(offset
), (unsigned int)(count
));
3402 LOG_DEBUG("Page start: %d, Page End: %d", (int)(page_cur
), (int)(page_end
));
3404 /* Special case: all one page */
3407 /* (1) non-aligned start */
3408 /* (2) body pages */
3409 /* (3) non-aligned end. */
3411 /* Handle special case - all one page. */
3412 if (page_cur
== page_end
) {
3413 LOG_DEBUG("Special case, all in one page");
3414 r
= sam3_page_read(private, page_cur
, pagebuffer
);
3418 page_offset
= (offset
& (private->page_size
-1));
3419 memcpy(pagebuffer
+ page_offset
,
3423 r
= sam3_page_write(private, page_cur
, pagebuffer
);
3430 /* non-aligned start */
3431 page_offset
= offset
& (private->page_size
- 1);
3433 LOG_DEBUG("Not-Aligned start");
3434 /* read the partial */
3435 r
= sam3_page_read(private, page_cur
, pagebuffer
);
3439 /* over-write with new data */
3440 n
= (private->page_size
- page_offset
);
3441 memcpy(pagebuffer
+ page_offset
,
3445 r
= sam3_page_write(private, page_cur
, pagebuffer
);
3455 /* By checking that offset is correct here, we also
3456 fix a clang warning */
3457 assert(offset
% private->page_size
== 0);
3459 /* intermediate large pages */
3460 /* also - the final *terminal* */
3461 /* if that terminal page is a full page */
3462 LOG_DEBUG("Full Page Loop: cur=%d, end=%d, count = 0x%08x",
3463 (int)page_cur
, (int)page_end
, (unsigned int)(count
));
3465 while ((page_cur
< page_end
) &&
3466 (count
>= private->page_size
)) {
3467 r
= sam3_page_write(private, page_cur
, buffer
);
3470 count
-= private->page_size
;
3471 buffer
+= private->page_size
;
3475 /* terminal partial page? */
3477 LOG_DEBUG("Terminal partial page, count = 0x%08x", (unsigned int)(count
));
3478 /* we have a partial page */
3479 r
= sam3_page_read(private, page_cur
, pagebuffer
);
3482 /* data goes at start */
3483 memcpy(pagebuffer
, buffer
, count
);
3484 r
= sam3_page_write(private, page_cur
, pagebuffer
);
3495 COMMAND_HANDLER(sam3_handle_info_command
)
3497 struct sam3_chip
*chip
;
3498 chip
= get_current_sam3(CMD
);
3505 /* bank0 must exist before we can do anything */
3506 if (!chip
->details
.bank
[0].bank
) {
3510 "Please define bank %d via command: flash bank %s ... ",
3512 at91sam3_flash
.name
);
3516 /* if bank 0 is not probed, then probe it */
3517 if (!(chip
->details
.bank
[0].probed
)) {
3518 r
= sam3_auto_probe(chip
->details
.bank
[0].bank
);
3522 /* above guarantees the "chip details" structure is valid */
3523 /* and thus, bank private areas are valid */
3524 /* and we have a SAM3 chip, what a concept! */
3526 /* auto-probe other banks, 0 done above */
3527 for (x
= 1; x
< SAM3_MAX_FLASH_BANKS
; x
++) {
3528 /* skip banks not present */
3529 if (!(chip
->details
.bank
[x
].present
))
3532 if (!chip
->details
.bank
[x
].bank
)
3535 if (chip
->details
.bank
[x
].probed
)
3538 r
= sam3_auto_probe(chip
->details
.bank
[x
].bank
);
3543 r
= sam3_get_info(chip
);
3544 if (r
!= ERROR_OK
) {
3545 LOG_DEBUG("Sam3Info, Failed %d", r
);
3552 COMMAND_HANDLER(sam3_handle_gpnvm_command
)
3556 struct sam3_chip
*chip
;
3558 chip
= get_current_sam3(CMD
);
3562 if (chip
->target
->state
!= TARGET_HALTED
) {
3563 LOG_ERROR("sam3 - target not halted");
3564 return ERROR_TARGET_NOT_HALTED
;
3567 if (!chip
->details
.bank
[0].bank
) {
3568 command_print(CMD
, "Bank0 must be defined first via: flash bank %s ...",
3569 at91sam3_flash
.name
);
3572 if (!chip
->details
.bank
[0].probed
) {
3573 r
= sam3_auto_probe(chip
->details
.bank
[0].bank
);
3580 return ERROR_COMMAND_SYNTAX_ERROR
;
3587 if ((strcmp(CMD_ARGV
[0], "show") == 0) && (strcmp(CMD_ARGV
[1], "all") == 0))
3591 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], v32
);
3597 if (strcmp("show", CMD_ARGV
[0]) == 0) {
3601 for (x
= 0; x
< chip
->details
.n_gpnvms
; x
++) {
3602 r
= flashd_get_gpnvm(&(chip
->details
.bank
[0]), x
, &v
);
3605 command_print(CMD
, "sam3-gpnvm%u: %u", x
, v
);
3609 if ((who
>= 0) && (((unsigned)(who
)) < chip
->details
.n_gpnvms
)) {
3610 r
= flashd_get_gpnvm(&(chip
->details
.bank
[0]), who
, &v
);
3612 command_print(CMD
, "sam3-gpnvm%u: %u", who
, v
);
3615 command_print(CMD
, "sam3-gpnvm invalid GPNVM: %u", who
);
3616 return ERROR_COMMAND_SYNTAX_ERROR
;
3621 command_print(CMD
, "Missing GPNVM number");
3622 return ERROR_COMMAND_SYNTAX_ERROR
;
3625 if (strcmp("set", CMD_ARGV
[0]) == 0)
3626 r
= flashd_set_gpnvm(&(chip
->details
.bank
[0]), who
);
3627 else if ((strcmp("clr", CMD_ARGV
[0]) == 0) ||
3628 (strcmp("clear", CMD_ARGV
[0]) == 0)) /* quietly accept both */
3629 r
= flashd_clr_gpnvm(&(chip
->details
.bank
[0]), who
);
3631 command_print(CMD
, "Unknown command: %s", CMD_ARGV
[0]);
3632 r
= ERROR_COMMAND_SYNTAX_ERROR
;
3637 COMMAND_HANDLER(sam3_handle_slowclk_command
)
3639 struct sam3_chip
*chip
;
3641 chip
= get_current_sam3(CMD
);
3653 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], v
);
3655 /* absurd slow clock of 200Khz? */
3656 command_print(CMD
, "Absurd/illegal slow clock freq: %d\n", (int)(v
));
3657 return ERROR_COMMAND_SYNTAX_ERROR
;
3659 chip
->cfg
.slow_freq
= v
;
3664 command_print(CMD
, "Too many parameters");
3665 return ERROR_COMMAND_SYNTAX_ERROR
;
3667 command_print(CMD
, "Slowclk freq: %d.%03dkhz",
3668 (int)(chip
->cfg
.slow_freq
/ 1000),
3669 (int)(chip
->cfg
.slow_freq
% 1000));
3673 static const struct command_registration at91sam3_exec_command_handlers
[] = {
3676 .handler
= sam3_handle_gpnvm_command
,
3677 .mode
= COMMAND_EXEC
,
3678 .usage
= "[('clr'|'set'|'show') bitnum]",
3679 .help
= "Without arguments, shows all bits in the gpnvm "
3680 "register. Otherwise, clears, sets, or shows one "
3681 "General Purpose Non-Volatile Memory (gpnvm) bit.",
3685 .handler
= sam3_handle_info_command
,
3686 .mode
= COMMAND_EXEC
,
3687 .help
= "Print information about the current at91sam3 chip "
3688 "and its flash configuration.",
3693 .handler
= sam3_handle_slowclk_command
,
3694 .mode
= COMMAND_EXEC
,
3695 .usage
= "[clock_hz]",
3696 .help
= "Display or set the slowclock frequency "
3697 "(default 32768 Hz).",
3699 COMMAND_REGISTRATION_DONE
3701 static const struct command_registration at91sam3_command_handlers
[] = {
3704 .mode
= COMMAND_ANY
,
3705 .help
= "at91sam3 flash command group",
3707 .chain
= at91sam3_exec_command_handlers
,
3709 COMMAND_REGISTRATION_DONE
3712 const struct flash_driver at91sam3_flash
= {
3714 .commands
= at91sam3_command_handlers
,
3715 .flash_bank_command
= sam3_flash_bank_command
,
3716 .erase
= sam3_erase
,
3717 .protect
= sam3_protect
,
3718 .write
= sam3_write
,
3719 .read
= default_flash_read
,
3720 .probe
= sam3_probe
,
3721 .auto_probe
= sam3_auto_probe
,
3722 .erase_check
= default_flash_blank_check
,
3723 .protect_check
= sam3_protect_check
,
3724 .free_driver_priv
= sam3_free_driver_priv
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)