1 /***************************************************************************
2 * Copyright (C) 2009 by Duane Ellis *
3 * openocd@duaneellis.com *
5 * Copyright (C) 2010 by Olaf Lüke (at91sam3s* support) *
6 * olaf@uni-paderborn.de *
8 * Copyright (C) 2011 by Olivier Schonken (at91sam3x* support) * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE. See the *
18 * GNU General public License for more details. *
20 * You should have received a copy of the GNU General public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
24 ****************************************************************************/
26 /* Some of the the lower level code was based on code supplied by
27 * ATMEL under this copyright. */
29 /* BEGIN ATMEL COPYRIGHT */
30 /* ----------------------------------------------------------------------------
31 * ATMEL Microcontroller Software Support
32 * ----------------------------------------------------------------------------
33 * Copyright (c) 2009, Atmel Corporation
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions are met:
40 * - Redistributions of source code must retain the above copyright notice,
41 * this list of conditions and the disclaimer below.
43 * Atmel's name may not be used to endorse or promote products derived from
44 * this software without specific prior written permission.
46 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
49 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
50 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
52 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
55 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 * ----------------------------------------------------------------------------
58 /* END ATMEL COPYRIGHT */
65 #include <helper/time_support.h>
67 #define REG_NAME_WIDTH (12)
69 /* at91sam3u series (has one or two flash banks) */
70 #define FLASH_BANK0_BASE_U 0x00080000
71 #define FLASH_BANK1_BASE_U 0x00100000
73 /* at91sam3s series (has always one flash bank) */
74 #define FLASH_BANK_BASE_S 0x00400000
76 /* at91sam3n series (has always one flash bank) */
77 #define FLASH_BANK_BASE_N 0x00400000
79 /* at91sam3a/x series has two flash banks*/
80 #define FLASH_BANK0_BASE_AX 0x00080000
81 /*Bank 1 of the at91sam3a/x series starts at 0x00080000 + half flash size*/
82 #define FLASH_BANK1_BASE_256K_AX 0x000A0000
83 #define FLASH_BANK1_BASE_512K_AX 0x000C0000
85 #define AT91C_EFC_FCMD_GETD (0x0) /* (EFC) Get Flash Descriptor */
86 #define AT91C_EFC_FCMD_WP (0x1) /* (EFC) Write Page */
87 #define AT91C_EFC_FCMD_WPL (0x2) /* (EFC) Write Page and Lock */
88 #define AT91C_EFC_FCMD_EWP (0x3) /* (EFC) Erase Page and Write Page */
89 #define AT91C_EFC_FCMD_EWPL (0x4) /* (EFC) Erase Page and Write Page
91 #define AT91C_EFC_FCMD_EA (0x5) /* (EFC) Erase All */
92 /* cmd6 is not present int he at91sam3u4/2/1 data sheet table 17-2 */
93 /* #define AT91C_EFC_FCMD_EPL (0x6) // (EFC) Erase plane? */
94 /* cmd7 is not present int he at91sam3u4/2/1 data sheet table 17-2 */
95 /* #define AT91C_EFC_FCMD_EPA (0x7) // (EFC) Erase pages? */
96 #define AT91C_EFC_FCMD_SLB (0x8) /* (EFC) Set Lock Bit */
97 #define AT91C_EFC_FCMD_CLB (0x9) /* (EFC) Clear Lock Bit */
98 #define AT91C_EFC_FCMD_GLB (0xA) /* (EFC) Get Lock Bit */
99 #define AT91C_EFC_FCMD_SFB (0xB) /* (EFC) Set Fuse Bit */
100 #define AT91C_EFC_FCMD_CFB (0xC) /* (EFC) Clear Fuse Bit */
101 #define AT91C_EFC_FCMD_GFB (0xD) /* (EFC) Get Fuse Bit */
102 #define AT91C_EFC_FCMD_STUI (0xE) /* (EFC) Start Read Unique ID */
103 #define AT91C_EFC_FCMD_SPUI (0xF) /* (EFC) Stop Read Unique ID */
105 #define offset_EFC_FMR 0
106 #define offset_EFC_FCR 4
107 #define offset_EFC_FSR 8
108 #define offset_EFC_FRR 12
110 extern struct flash_driver at91sam3_flash
;
112 static float _tomhz(uint32_t freq_hz
)
116 f
= ((float)(freq_hz
)) / 1000000.0;
120 /* How the chip is configured. */
122 uint32_t unique_id
[4];
126 uint32_t mainosc_freq
;
136 #define SAM3_CHIPID_CIDR (0x400E0740)
137 uint32_t CHIPID_CIDR
;
138 #define SAM3_CHIPID_CIDR2 (0x400E0940) /*SAM3X and SAM3A cidr at this address*/
139 uint32_t CHIPID_CIDR2
;
140 #define SAM3_CHIPID_EXID (0x400E0744)
141 uint32_t CHIPID_EXID
;
142 #define SAM3_CHIPID_EXID2 (0x400E0944) /*SAM3X and SAM3A cidr at this address*/
143 uint32_t CHIPID_EXID2
;
146 #define SAM3_PMC_BASE (0x400E0400)
147 #define SAM3_PMC_SCSR (SAM3_PMC_BASE + 0x0008)
149 #define SAM3_PMC_PCSR (SAM3_PMC_BASE + 0x0018)
151 #define SAM3_CKGR_UCKR (SAM3_PMC_BASE + 0x001c)
153 #define SAM3_CKGR_MOR (SAM3_PMC_BASE + 0x0020)
155 #define SAM3_CKGR_MCFR (SAM3_PMC_BASE + 0x0024)
157 #define SAM3_CKGR_PLLAR (SAM3_PMC_BASE + 0x0028)
159 #define SAM3_PMC_MCKR (SAM3_PMC_BASE + 0x0030)
161 #define SAM3_PMC_PCK0 (SAM3_PMC_BASE + 0x0040)
163 #define SAM3_PMC_PCK1 (SAM3_PMC_BASE + 0x0044)
165 #define SAM3_PMC_PCK2 (SAM3_PMC_BASE + 0x0048)
167 #define SAM3_PMC_SR (SAM3_PMC_BASE + 0x0068)
169 #define SAM3_PMC_IMR (SAM3_PMC_BASE + 0x006c)
171 #define SAM3_PMC_FSMR (SAM3_PMC_BASE + 0x0070)
173 #define SAM3_PMC_FSPR (SAM3_PMC_BASE + 0x0074)
178 * The AT91SAM3N data sheet 04-Oct-2010, AT91SAM3U data sheet 22-Aug-2011
179 * and AT91SAM3S data sheet 09-Feb-2011 state that for flash writes
180 * the flash wait state (FWS) should be set to 6. It seems like that the
181 * cause of the problem is not the flash itself, but the flash write
182 * buffer. Ie the wait states have to be set before writing into the
184 * Tested and confirmed with SAM3N and SAM3U
187 struct sam3_bank_private
{
189 /* DANGER: THERE ARE DRAGONS HERE.. */
190 /* NOTE: If you add more 'ghost' pointers */
191 /* be aware that you must *manually* update */
192 /* these pointers in the function sam3_GetDetails() */
193 /* See the comment "Here there be dragons" */
195 /* so we can find the chip we belong to */
196 struct sam3_chip
*pChip
;
197 /* so we can find the orginal bank pointer */
198 struct flash_bank
*pBank
;
199 unsigned bank_number
;
200 uint32_t controller_address
;
201 uint32_t base_address
;
202 uint32_t flash_wait_states
;
206 unsigned sector_size
;
210 struct sam3_chip_details
{
211 /* THERE ARE DRAGONS HERE.. */
212 /* note: If you add pointers here */
213 /* becareful about them as they */
214 /* may need to be updated inside */
215 /* the function: "sam3_GetDetails() */
216 /* which copy/overwrites the */
217 /* 'runtime' copy of this structure */
218 uint32_t chipid_cidr
;
222 #define SAM3_N_NVM_BITS 3
223 unsigned gpnvm
[SAM3_N_NVM_BITS
];
224 unsigned total_flash_size
;
225 unsigned total_sram_size
;
227 #define SAM3_MAX_FLASH_BANKS 2
228 /* these are "initialized" from the global const data */
229 struct sam3_bank_private bank
[SAM3_MAX_FLASH_BANKS
];
233 struct sam3_chip
*next
;
236 /* this is "initialized" from the global const structure */
237 struct sam3_chip_details details
;
238 struct target
*target
;
243 struct sam3_reg_list
{
244 uint32_t address
; size_t struct_offset
; const char *name
;
245 void (*explain_func
)(struct sam3_chip
*pInfo
);
248 static struct sam3_chip
*all_sam3_chips
;
250 static struct sam3_chip
*get_current_sam3(struct command_context
*cmd_ctx
)
253 static struct sam3_chip
*p
;
255 t
= get_current_target(cmd_ctx
);
257 command_print(cmd_ctx
, "No current target?");
263 /* this should not happen */
264 /* the command is not registered until the chip is created? */
265 command_print(cmd_ctx
, "No SAM3 chips exist?");
274 command_print(cmd_ctx
, "Cannot find SAM3 chip?");
278 /* these are used to *initialize* the "pChip->details" structure. */
279 static const struct sam3_chip_details all_sam3_details
[] = {
280 /* Start at91sam3u* series */
282 .chipid_cidr
= 0x28100960,
283 .name
= "at91sam3u4e",
284 .total_flash_size
= 256 * 1024,
285 .total_sram_size
= 52 * 1024,
289 /* System boots at address 0x0 */
290 /* gpnvm[1] = selects boot code */
291 /* if gpnvm[1] == 0 */
292 /* boot is via "SAMBA" (rom) */
294 /* boot is via FLASH */
295 /* Selection is via gpnvm[2] */
298 /* NOTE: banks 0 & 1 switch places */
299 /* if gpnvm[2] == 0 */
300 /* Bank0 is the boot rom */
302 /* Bank1 is the boot rom */
311 .base_address
= FLASH_BANK0_BASE_U
,
312 .controller_address
= 0x400e0800,
313 .flash_wait_states
= 6, /* workaround silicon bug */
315 .size_bytes
= 128 * 1024,
327 .base_address
= FLASH_BANK1_BASE_U
,
328 .controller_address
= 0x400e0a00,
329 .flash_wait_states
= 6, /* workaround silicon bug */
331 .size_bytes
= 128 * 1024,
340 .chipid_cidr
= 0x281a0760,
341 .name
= "at91sam3u2e",
342 .total_flash_size
= 128 * 1024,
343 .total_sram_size
= 36 * 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] */
362 .base_address
= FLASH_BANK0_BASE_U
,
363 .controller_address
= 0x400e0800,
364 .flash_wait_states
= 6, /* workaround silicon bug */
366 .size_bytes
= 128 * 1024,
380 .chipid_cidr
= 0x28190560,
381 .name
= "at91sam3u1e",
382 .total_flash_size
= 64 * 1024,
383 .total_sram_size
= 20 * 1024,
387 /* System boots at address 0x0 */
388 /* gpnvm[1] = selects boot code */
389 /* if gpnvm[1] == 0 */
390 /* boot is via "SAMBA" (rom) */
392 /* boot is via FLASH */
393 /* Selection is via gpnvm[2] */
404 .base_address
= FLASH_BANK0_BASE_U
,
405 .controller_address
= 0x400e0800,
406 .flash_wait_states
= 6, /* workaround silicon bug */
408 .size_bytes
= 64 * 1024,
424 .chipid_cidr
= 0x28000960,
425 .name
= "at91sam3u4c",
426 .total_flash_size
= 256 * 1024,
427 .total_sram_size
= 52 * 1024,
431 /* System boots at address 0x0 */
432 /* gpnvm[1] = selects boot code */
433 /* if gpnvm[1] == 0 */
434 /* boot is via "SAMBA" (rom) */
436 /* boot is via FLASH */
437 /* Selection is via gpnvm[2] */
440 /* NOTE: banks 0 & 1 switch places */
441 /* if gpnvm[2] == 0 */
442 /* Bank0 is the boot rom */
444 /* Bank1 is the boot rom */
453 .base_address
= FLASH_BANK0_BASE_U
,
454 .controller_address
= 0x400e0800,
455 .flash_wait_states
= 6, /* workaround silicon bug */
457 .size_bytes
= 128 * 1024,
468 .base_address
= FLASH_BANK1_BASE_U
,
469 .controller_address
= 0x400e0a00,
470 .flash_wait_states
= 6, /* workaround silicon bug */
472 .size_bytes
= 128 * 1024,
481 .chipid_cidr
= 0x280a0760,
482 .name
= "at91sam3u2c",
483 .total_flash_size
= 128 * 1024,
484 .total_sram_size
= 36 * 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] */
503 .base_address
= FLASH_BANK0_BASE_U
,
504 .controller_address
= 0x400e0800,
505 .flash_wait_states
= 6, /* workaround silicon bug */
507 .size_bytes
= 128 * 1024,
521 .chipid_cidr
= 0x28090560,
522 .name
= "at91sam3u1c",
523 .total_flash_size
= 64 * 1024,
524 .total_sram_size
= 20 * 1024,
528 /* System boots at address 0x0 */
529 /* gpnvm[1] = selects boot code */
530 /* if gpnvm[1] == 0 */
531 /* boot is via "SAMBA" (rom) */
533 /* boot is via FLASH */
534 /* Selection is via gpnvm[2] */
545 .base_address
= FLASH_BANK0_BASE_U
,
546 .controller_address
= 0x400e0800,
547 .flash_wait_states
= 6, /* workaround silicon bug */
549 .size_bytes
= 64 * 1024,
564 /* Start at91sam3s* series */
566 /* Note: The preliminary at91sam3s datasheet says on page 302 */
567 /* that the flash controller is at address 0x400E0800. */
568 /* This is _not_ the case, the controller resides at address 0x400e0a0. */
570 .chipid_cidr
= 0x28A00960,
571 .name
= "at91sam3s4c",
572 .total_flash_size
= 256 * 1024,
573 .total_sram_size
= 48 * 1024,
583 .base_address
= FLASH_BANK_BASE_S
,
584 .controller_address
= 0x400e0a00,
585 .flash_wait_states
= 6, /* workaround silicon bug */
587 .size_bytes
= 256 * 1024,
589 .sector_size
= 16384,
603 .chipid_cidr
= 0x28900960,
604 .name
= "at91sam3s4b",
605 .total_flash_size
= 256 * 1024,
606 .total_sram_size
= 48 * 1024,
616 .base_address
= FLASH_BANK_BASE_S
,
617 .controller_address
= 0x400e0a00,
618 .flash_wait_states
= 6, /* workaround silicon bug */
620 .size_bytes
= 256 * 1024,
622 .sector_size
= 16384,
635 .chipid_cidr
= 0x28800960,
636 .name
= "at91sam3s4a",
637 .total_flash_size
= 256 * 1024,
638 .total_sram_size
= 48 * 1024,
648 .base_address
= FLASH_BANK_BASE_S
,
649 .controller_address
= 0x400e0a00,
650 .flash_wait_states
= 6, /* workaround silicon bug */
652 .size_bytes
= 256 * 1024,
654 .sector_size
= 16384,
667 .chipid_cidr
= 0x28AA0760,
668 .name
= "at91sam3s2c",
669 .total_flash_size
= 128 * 1024,
670 .total_sram_size
= 32 * 1024,
680 .base_address
= FLASH_BANK_BASE_S
,
681 .controller_address
= 0x400e0a00,
682 .flash_wait_states
= 6, /* workaround silicon bug */
684 .size_bytes
= 128 * 1024,
686 .sector_size
= 16384,
699 .chipid_cidr
= 0x289A0760,
700 .name
= "at91sam3s2b",
701 .total_flash_size
= 128 * 1024,
702 .total_sram_size
= 32 * 1024,
712 .base_address
= FLASH_BANK_BASE_S
,
713 .controller_address
= 0x400e0a00,
714 .flash_wait_states
= 6, /* workaround silicon bug */
716 .size_bytes
= 128 * 1024,
718 .sector_size
= 16384,
731 .chipid_cidr
= 0x288A0760,
732 .name
= "at91sam3s2a",
733 .total_flash_size
= 128 * 1024,
734 .total_sram_size
= 32 * 1024,
744 .base_address
= FLASH_BANK_BASE_S
,
745 .controller_address
= 0x400e0a00,
746 .flash_wait_states
= 6, /* workaround silicon bug */
748 .size_bytes
= 128 * 1024,
750 .sector_size
= 16384,
763 .chipid_cidr
= 0x28A90560,
764 .name
= "at91sam3s1c",
765 .total_flash_size
= 64 * 1024,
766 .total_sram_size
= 16 * 1024,
776 .base_address
= FLASH_BANK_BASE_S
,
777 .controller_address
= 0x400e0a00,
778 .flash_wait_states
= 6, /* workaround silicon bug */
780 .size_bytes
= 64 * 1024,
782 .sector_size
= 16384,
795 .chipid_cidr
= 0x28990560,
796 .name
= "at91sam3s1b",
797 .total_flash_size
= 64 * 1024,
798 .total_sram_size
= 16 * 1024,
808 .base_address
= FLASH_BANK_BASE_S
,
809 .controller_address
= 0x400e0a00,
810 .flash_wait_states
= 6, /* workaround silicon bug */
812 .size_bytes
= 64 * 1024,
814 .sector_size
= 16384,
827 .chipid_cidr
= 0x28890560,
828 .name
= "at91sam3s1a",
829 .total_flash_size
= 64 * 1024,
830 .total_sram_size
= 16 * 1024,
840 .base_address
= FLASH_BANK_BASE_S
,
841 .controller_address
= 0x400e0a00,
842 .flash_wait_states
= 6, /* workaround silicon bug */
844 .size_bytes
= 64 * 1024,
846 .sector_size
= 16384,
859 /* Start at91sam3n* series */
861 .chipid_cidr
= 0x29540960,
862 .name
= "at91sam3n4c",
863 .total_flash_size
= 256 * 1024,
864 .total_sram_size
= 24 * 1024,
868 /* System boots at address 0x0 */
869 /* gpnvm[1] = selects boot code */
870 /* if gpnvm[1] == 0 */
871 /* boot is via "SAMBA" (rom) */
873 /* boot is via FLASH */
874 /* Selection is via gpnvm[2] */
877 /* NOTE: banks 0 & 1 switch places */
878 /* if gpnvm[2] == 0 */
879 /* Bank0 is the boot rom */
881 /* Bank1 is the boot rom */
890 .base_address
= FLASH_BANK_BASE_N
,
891 .controller_address
= 0x400e0A00,
892 .flash_wait_states
= 6, /* workaround silicon bug */
894 .size_bytes
= 256 * 1024,
896 .sector_size
= 16384,
910 .chipid_cidr
= 0x29440960,
911 .name
= "at91sam3n4b",
912 .total_flash_size
= 256 * 1024,
913 .total_sram_size
= 24 * 1024,
917 /* System boots at address 0x0 */
918 /* gpnvm[1] = selects boot code */
919 /* if gpnvm[1] == 0 */
920 /* boot is via "SAMBA" (rom) */
922 /* boot is via FLASH */
923 /* Selection is via gpnvm[2] */
926 /* NOTE: banks 0 & 1 switch places */
927 /* if gpnvm[2] == 0 */
928 /* Bank0 is the boot rom */
930 /* Bank1 is the boot rom */
939 .base_address
= FLASH_BANK_BASE_N
,
940 .controller_address
= 0x400e0A00,
941 .flash_wait_states
= 6, /* workaround silicon bug */
943 .size_bytes
= 256 * 1024,
945 .sector_size
= 16384,
959 .chipid_cidr
= 0x29340960,
960 .name
= "at91sam3n4a",
961 .total_flash_size
= 256 * 1024,
962 .total_sram_size
= 24 * 1024,
966 /* System boots at address 0x0 */
967 /* gpnvm[1] = selects boot code */
968 /* if gpnvm[1] == 0 */
969 /* boot is via "SAMBA" (rom) */
971 /* boot is via FLASH */
972 /* Selection is via gpnvm[2] */
975 /* NOTE: banks 0 & 1 switch places */
976 /* if gpnvm[2] == 0 */
977 /* Bank0 is the boot rom */
979 /* Bank1 is the boot rom */
988 .base_address
= FLASH_BANK_BASE_N
,
989 .controller_address
= 0x400e0A00,
990 .flash_wait_states
= 6, /* workaround silicon bug */
992 .size_bytes
= 256 * 1024,
994 .sector_size
= 16384,
1008 .chipid_cidr
= 0x29590760,
1009 .name
= "at91sam3n2c",
1010 .total_flash_size
= 128 * 1024,
1011 .total_sram_size
= 16 * 1024,
1015 /* System boots at address 0x0 */
1016 /* gpnvm[1] = selects boot code */
1017 /* if gpnvm[1] == 0 */
1018 /* boot is via "SAMBA" (rom) */
1020 /* boot is via FLASH */
1021 /* Selection is via gpnvm[2] */
1024 /* NOTE: banks 0 & 1 switch places */
1025 /* if gpnvm[2] == 0 */
1026 /* Bank0 is the boot rom */
1028 /* Bank1 is the boot rom */
1037 .base_address
= FLASH_BANK_BASE_N
,
1038 .controller_address
= 0x400e0A00,
1039 .flash_wait_states
= 6, /* workaround silicon bug */
1041 .size_bytes
= 128 * 1024,
1043 .sector_size
= 16384,
1057 .chipid_cidr
= 0x29490760,
1058 .name
= "at91sam3n2b",
1059 .total_flash_size
= 128 * 1024,
1060 .total_sram_size
= 16 * 1024,
1064 /* System boots at address 0x0 */
1065 /* gpnvm[1] = selects boot code */
1066 /* if gpnvm[1] == 0 */
1067 /* boot is via "SAMBA" (rom) */
1069 /* boot is via FLASH */
1070 /* Selection is via gpnvm[2] */
1073 /* NOTE: banks 0 & 1 switch places */
1074 /* if gpnvm[2] == 0 */
1075 /* Bank0 is the boot rom */
1077 /* Bank1 is the boot rom */
1086 .base_address
= FLASH_BANK_BASE_N
,
1087 .controller_address
= 0x400e0A00,
1088 .flash_wait_states
= 6, /* workaround silicon bug */
1090 .size_bytes
= 128 * 1024,
1092 .sector_size
= 16384,
1106 .chipid_cidr
= 0x29390760,
1107 .name
= "at91sam3n2a",
1108 .total_flash_size
= 128 * 1024,
1109 .total_sram_size
= 16 * 1024,
1113 /* System boots at address 0x0 */
1114 /* gpnvm[1] = selects boot code */
1115 /* if gpnvm[1] == 0 */
1116 /* boot is via "SAMBA" (rom) */
1118 /* boot is via FLASH */
1119 /* Selection is via gpnvm[2] */
1122 /* NOTE: banks 0 & 1 switch places */
1123 /* if gpnvm[2] == 0 */
1124 /* Bank0 is the boot rom */
1126 /* Bank1 is the boot rom */
1135 .base_address
= FLASH_BANK_BASE_N
,
1136 .controller_address
= 0x400e0A00,
1137 .flash_wait_states
= 6, /* workaround silicon bug */
1139 .size_bytes
= 128 * 1024,
1141 .sector_size
= 16384,
1155 .chipid_cidr
= 0x29580560,
1156 .name
= "at91sam3n1c",
1157 .total_flash_size
= 64 * 1024,
1158 .total_sram_size
= 8 * 1024,
1162 /* System boots at address 0x0 */
1163 /* gpnvm[1] = selects boot code */
1164 /* if gpnvm[1] == 0 */
1165 /* boot is via "SAMBA" (rom) */
1167 /* boot is via FLASH */
1168 /* Selection is via gpnvm[2] */
1171 /* NOTE: banks 0 & 1 switch places */
1172 /* if gpnvm[2] == 0 */
1173 /* Bank0 is the boot rom */
1175 /* Bank1 is the boot rom */
1184 .base_address
= FLASH_BANK_BASE_N
,
1185 .controller_address
= 0x400e0A00,
1186 .flash_wait_states
= 6, /* workaround silicon bug */
1188 .size_bytes
= 64 * 1024,
1190 .sector_size
= 16384,
1204 .chipid_cidr
= 0x29480560,
1205 .name
= "at91sam3n1b",
1206 .total_flash_size
= 64 * 1024,
1207 .total_sram_size
= 8 * 1024,
1211 /* System boots at address 0x0 */
1212 /* gpnvm[1] = selects boot code */
1213 /* if gpnvm[1] == 0 */
1214 /* boot is via "SAMBA" (rom) */
1216 /* boot is via FLASH */
1217 /* Selection is via gpnvm[2] */
1220 /* NOTE: banks 0 & 1 switch places */
1221 /* if gpnvm[2] == 0 */
1222 /* Bank0 is the boot rom */
1224 /* Bank1 is the boot rom */
1233 .base_address
= FLASH_BANK_BASE_N
,
1234 .controller_address
= 0x400e0A00,
1235 .flash_wait_states
= 6, /* workaround silicon bug */
1237 .size_bytes
= 64 * 1024,
1239 .sector_size
= 16384,
1253 .chipid_cidr
= 0x29380560,
1254 .name
= "at91sam3n1a",
1255 .total_flash_size
= 64 * 1024,
1256 .total_sram_size
= 8 * 1024,
1260 /* System boots at address 0x0 */
1261 /* gpnvm[1] = selects boot code */
1262 /* if gpnvm[1] == 0 */
1263 /* boot is via "SAMBA" (rom) */
1265 /* boot is via FLASH */
1266 /* Selection is via gpnvm[2] */
1269 /* NOTE: banks 0 & 1 switch places */
1270 /* if gpnvm[2] == 0 */
1271 /* Bank0 is the boot rom */
1273 /* Bank1 is the boot rom */
1282 .base_address
= FLASH_BANK_BASE_N
,
1283 .controller_address
= 0x400e0A00,
1284 .flash_wait_states
= 6, /* workaround silicon bug */
1286 .size_bytes
= 64 * 1024,
1288 .sector_size
= 16384,
1301 /* Start at91sam3a series*/
1302 /* System boots at address 0x0 */
1303 /* gpnvm[1] = selects boot code */
1304 /* if gpnvm[1] == 0 */
1305 /* boot is via "SAMBA" (rom) */
1307 /* boot is via FLASH */
1308 /* Selection is via gpnvm[2] */
1311 /* NOTE: banks 0 & 1 switch places */
1312 /* if gpnvm[2] == 0 */
1313 /* Bank0 is the boot rom */
1315 /* Bank1 is the boot rom */
1319 .chipid_cidr
= 0x283E0A60,
1320 .name
= "at91sam3a8c",
1321 .total_flash_size
= 512 * 1024,
1322 .total_sram_size
= 96 * 1024,
1332 .base_address
= FLASH_BANK0_BASE_AX
,
1333 .controller_address
= 0x400e0a00,
1334 .flash_wait_states
= 6, /* workaround silicon bug */
1336 .size_bytes
= 256 * 1024,
1338 .sector_size
= 16384,
1347 .base_address
= FLASH_BANK1_BASE_512K_AX
,
1348 .controller_address
= 0x400e0c00,
1349 .flash_wait_states
= 6, /* workaround silicon bug */
1351 .size_bytes
= 256 * 1024,
1353 .sector_size
= 16384,
1360 .chipid_cidr
= 0x283B0960,
1361 .name
= "at91sam3a4c",
1362 .total_flash_size
= 256 * 1024,
1363 .total_sram_size
= 64 * 1024,
1373 .base_address
= FLASH_BANK0_BASE_AX
,
1374 .controller_address
= 0x400e0a00,
1375 .flash_wait_states
= 6, /* workaround silicon bug */
1377 .size_bytes
= 128 * 1024,
1379 .sector_size
= 16384,
1388 .base_address
= FLASH_BANK1_BASE_256K_AX
,
1389 .controller_address
= 0x400e0c00,
1390 .flash_wait_states
= 6, /* workaround silicon bug */
1392 .size_bytes
= 128 * 1024,
1394 .sector_size
= 16384,
1401 /* Start at91sam3x* series */
1402 /* System boots at address 0x0 */
1403 /* gpnvm[1] = selects boot code */
1404 /* if gpnvm[1] == 0 */
1405 /* boot is via "SAMBA" (rom) */
1407 /* boot is via FLASH */
1408 /* Selection is via gpnvm[2] */
1411 /* NOTE: banks 0 & 1 switch places */
1412 /* if gpnvm[2] == 0 */
1413 /* Bank0 is the boot rom */
1415 /* Bank1 is the boot rom */
1417 /*at91sam3x8h - ES has an incorrect CIDR of 0x286E0A20*/
1419 .chipid_cidr
= 0x286E0A20,
1420 .name
= "at91sam3x8h - ES",
1421 .total_flash_size
= 512 * 1024,
1422 .total_sram_size
= 96 * 1024,
1432 .base_address
= FLASH_BANK0_BASE_AX
,
1433 .controller_address
= 0x400e0a00,
1434 .flash_wait_states
= 6, /* workaround silicon bug */
1436 .size_bytes
= 256 * 1024,
1438 .sector_size
= 16384,
1447 .base_address
= FLASH_BANK1_BASE_512K_AX
,
1448 .controller_address
= 0x400e0c00,
1449 .flash_wait_states
= 6, /* workaround silicon bug */
1451 .size_bytes
= 256 * 1024,
1453 .sector_size
= 16384,
1459 /*at91sam3x8h - ES2 and up uses the correct CIDR of 0x286E0A60*/
1461 .chipid_cidr
= 0x286E0A60,
1462 .name
= "at91sam3x8h",
1463 .total_flash_size
= 512 * 1024,
1464 .total_sram_size
= 96 * 1024,
1474 .base_address
= FLASH_BANK0_BASE_AX
,
1475 .controller_address
= 0x400e0a00,
1476 .flash_wait_states
= 6, /* workaround silicon bug */
1478 .size_bytes
= 256 * 1024,
1480 .sector_size
= 16384,
1489 .base_address
= FLASH_BANK1_BASE_512K_AX
,
1490 .controller_address
= 0x400e0c00,
1491 .flash_wait_states
= 6, /* workaround silicon bug */
1493 .size_bytes
= 256 * 1024,
1495 .sector_size
= 16384,
1502 .chipid_cidr
= 0x285E0A60,
1503 .name
= "at91sam3x8e",
1504 .total_flash_size
= 512 * 1024,
1505 .total_sram_size
= 96 * 1024,
1515 .base_address
= FLASH_BANK0_BASE_AX
,
1516 .controller_address
= 0x400e0a00,
1517 .flash_wait_states
= 6, /* workaround silicon bug */
1519 .size_bytes
= 256 * 1024,
1521 .sector_size
= 16384,
1530 .base_address
= FLASH_BANK1_BASE_512K_AX
,
1531 .controller_address
= 0x400e0c00,
1532 .flash_wait_states
= 6, /* workaround silicon bug */
1534 .size_bytes
= 256 * 1024,
1536 .sector_size
= 16384,
1543 .chipid_cidr
= 0x284E0A60,
1544 .name
= "at91sam3x8c",
1545 .total_flash_size
= 512 * 1024,
1546 .total_sram_size
= 96 * 1024,
1556 .base_address
= FLASH_BANK0_BASE_AX
,
1557 .controller_address
= 0x400e0a00,
1558 .flash_wait_states
= 6, /* workaround silicon bug */
1560 .size_bytes
= 256 * 1024,
1562 .sector_size
= 16384,
1571 .base_address
= FLASH_BANK1_BASE_512K_AX
,
1572 .controller_address
= 0x400e0c00,
1573 .flash_wait_states
= 6, /* workaround silicon bug */
1575 .size_bytes
= 256 * 1024,
1577 .sector_size
= 16384,
1584 .chipid_cidr
= 0x285B0960,
1585 .name
= "at91sam3x4e",
1586 .total_flash_size
= 256 * 1024,
1587 .total_sram_size
= 64 * 1024,
1597 .base_address
= FLASH_BANK0_BASE_AX
,
1598 .controller_address
= 0x400e0a00,
1599 .flash_wait_states
= 6, /* workaround silicon bug */
1601 .size_bytes
= 128 * 1024,
1603 .sector_size
= 16384,
1612 .base_address
= FLASH_BANK1_BASE_256K_AX
,
1613 .controller_address
= 0x400e0c00,
1614 .flash_wait_states
= 6, /* workaround silicon bug */
1616 .size_bytes
= 128 * 1024,
1618 .sector_size
= 16384,
1625 .chipid_cidr
= 0x284B0960,
1626 .name
= "at91sam3x4c",
1627 .total_flash_size
= 256 * 1024,
1628 .total_sram_size
= 64 * 1024,
1638 .base_address
= FLASH_BANK0_BASE_AX
,
1639 .controller_address
= 0x400e0a00,
1640 .flash_wait_states
= 6, /* workaround silicon bug */
1642 .size_bytes
= 128 * 1024,
1644 .sector_size
= 16384,
1653 .base_address
= FLASH_BANK1_BASE_256K_AX
,
1654 .controller_address
= 0x400e0c00,
1655 .flash_wait_states
= 6, /* workaround silicon bug */
1657 .size_bytes
= 128 * 1024,
1659 .sector_size
= 16384,
1673 /***********************************************************************
1674 **********************************************************************
1675 **********************************************************************
1676 **********************************************************************
1677 **********************************************************************
1678 **********************************************************************/
1679 /* *ATMEL* style code - from the SAM3 driver code */
1682 * Get the current status of the EEFC and
1683 * the value of some status bits (LOCKE, PROGE).
1684 * @param pPrivate - info about the bank
1685 * @param v - result goes here
1687 static int EFC_GetStatus(struct sam3_bank_private
*pPrivate
, uint32_t *v
)
1690 r
= target_read_u32(pPrivate
->pChip
->target
,
1691 pPrivate
->controller_address
+ offset_EFC_FSR
,
1693 LOG_DEBUG("Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
1695 ((unsigned int)((*v
>> 2) & 1)),
1696 ((unsigned int)((*v
>> 1) & 1)),
1697 ((unsigned int)((*v
>> 0) & 1)));
1703 * Get the result of the last executed command.
1704 * @param pPrivate - info about the bank
1705 * @param v - result goes here
1707 static int EFC_GetResult(struct sam3_bank_private
*pPrivate
, uint32_t *v
)
1711 r
= target_read_u32(pPrivate
->pChip
->target
,
1712 pPrivate
->controller_address
+ offset_EFC_FRR
,
1716 LOG_DEBUG("Result: 0x%08x", ((unsigned int)(rv
)));
1720 static int EFC_StartCommand(struct sam3_bank_private
*pPrivate
,
1721 unsigned command
, unsigned argument
)
1730 /* Check command & argument */
1733 case AT91C_EFC_FCMD_WP
:
1734 case AT91C_EFC_FCMD_WPL
:
1735 case AT91C_EFC_FCMD_EWP
:
1736 case AT91C_EFC_FCMD_EWPL
:
1737 /* case AT91C_EFC_FCMD_EPL: */
1738 /* case AT91C_EFC_FCMD_EPA: */
1739 case AT91C_EFC_FCMD_SLB
:
1740 case AT91C_EFC_FCMD_CLB
:
1741 n
= (pPrivate
->size_bytes
/ pPrivate
->page_size
);
1743 LOG_ERROR("*BUG*: Embedded flash has only %u pages", (unsigned)(n
));
1746 case AT91C_EFC_FCMD_SFB
:
1747 case AT91C_EFC_FCMD_CFB
:
1748 if (argument
>= pPrivate
->pChip
->details
.n_gpnvms
) {
1749 LOG_ERROR("*BUG*: Embedded flash has only %d GPNVMs",
1750 pPrivate
->pChip
->details
.n_gpnvms
);
1754 case AT91C_EFC_FCMD_GETD
:
1755 case AT91C_EFC_FCMD_EA
:
1756 case AT91C_EFC_FCMD_GLB
:
1757 case AT91C_EFC_FCMD_GFB
:
1758 case AT91C_EFC_FCMD_STUI
:
1759 case AT91C_EFC_FCMD_SPUI
:
1761 LOG_ERROR("Argument is meaningless for cmd: %d", command
);
1764 LOG_ERROR("Unknown command %d", command
);
1768 if (command
== AT91C_EFC_FCMD_SPUI
) {
1769 /* this is a very special situation. */
1770 /* Situation (1) - error/retry - see below */
1771 /* And we are being called recursively */
1772 /* Situation (2) - normal, finished reading unique id */
1774 /* it should be "ready" */
1775 EFC_GetStatus(pPrivate
, &v
);
1777 /* then it is ready */
1781 /* we have done this before */
1782 /* the controller is not responding. */
1783 LOG_ERROR("flash controller(%d) is not ready! Error",
1784 pPrivate
->bank_number
);
1788 LOG_ERROR("Flash controller(%d) is not ready, attempting reset",
1789 pPrivate
->bank_number
);
1790 /* we do that by issuing the *STOP* command */
1791 EFC_StartCommand(pPrivate
, AT91C_EFC_FCMD_SPUI
, 0);
1792 /* above is recursive, and further recursion is blocked by */
1793 /* if (command == AT91C_EFC_FCMD_SPUI) above */
1799 v
= (0x5A << 24) | (argument
<< 8) | command
;
1800 LOG_DEBUG("Command: 0x%08x", ((unsigned int)(v
)));
1801 r
= target_write_u32(pPrivate
->pBank
->target
,
1802 pPrivate
->controller_address
+ offset_EFC_FCR
, v
);
1804 LOG_DEBUG("Error Write failed");
1809 * Performs the given command and wait until its completion (or an error).
1810 * @param pPrivate - info about the bank
1811 * @param command - Command to perform.
1812 * @param argument - Optional command argument.
1813 * @param status - put command status bits here
1815 static int EFC_PerformCommand(struct sam3_bank_private
*pPrivate
,
1823 long long ms_now
, ms_end
;
1829 r
= EFC_StartCommand(pPrivate
, command
, argument
);
1833 ms_end
= 500 + timeval_ms();
1836 r
= EFC_GetStatus(pPrivate
, &v
);
1839 ms_now
= timeval_ms();
1840 if (ms_now
> ms_end
) {
1842 LOG_ERROR("Command timeout");
1845 } while ((v
& 1) == 0);
1849 *status
= (v
& 0x6);
1855 * Read the unique ID.
1856 * @param pPrivate - info about the bank
1857 * The unique ID is stored in the 'pPrivate' structure.
1859 static int FLASHD_ReadUniqueID(struct sam3_bank_private
*pPrivate
)
1865 pPrivate
->pChip
->cfg
.unique_id
[0] = 0;
1866 pPrivate
->pChip
->cfg
.unique_id
[1] = 0;
1867 pPrivate
->pChip
->cfg
.unique_id
[2] = 0;
1868 pPrivate
->pChip
->cfg
.unique_id
[3] = 0;
1871 r
= EFC_StartCommand(pPrivate
, AT91C_EFC_FCMD_STUI
, 0);
1875 for (x
= 0; x
< 4; x
++) {
1876 r
= target_read_u32(pPrivate
->pChip
->target
,
1877 pPrivate
->pBank
->base
+ (x
* 4),
1881 pPrivate
->pChip
->cfg
.unique_id
[x
] = v
;
1884 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_SPUI
, 0, NULL
);
1885 LOG_DEBUG("End: R=%d, id = 0x%08x, 0x%08x, 0x%08x, 0x%08x",
1887 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[0]),
1888 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[1]),
1889 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[2]),
1890 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[3]));
1896 * Erases the entire flash.
1897 * @param pPrivate - the info about the bank.
1899 static int FLASHD_EraseEntireBank(struct sam3_bank_private
*pPrivate
)
1902 return EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_EA
, 0, NULL
);
1906 * Gets current GPNVM state.
1907 * @param pPrivate - info about the bank.
1908 * @param gpnvm - GPNVM bit index.
1909 * @param puthere - result stored here.
1911 /* ------------------------------------------------------------------------------ */
1912 static int FLASHD_GetGPNVM(struct sam3_bank_private
*pPrivate
, unsigned gpnvm
, unsigned *puthere
)
1918 if (pPrivate
->bank_number
!= 0) {
1919 LOG_ERROR("GPNVM only works with Bank0");
1923 if (gpnvm
>= pPrivate
->pChip
->details
.n_gpnvms
) {
1924 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1925 gpnvm
, pPrivate
->pChip
->details
.n_gpnvms
);
1929 /* Get GPNVMs status */
1930 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_GFB
, 0, NULL
);
1931 if (r
!= ERROR_OK
) {
1932 LOG_ERROR("Failed");
1936 r
= EFC_GetResult(pPrivate
, &v
);
1939 /* Check if GPNVM is set */
1940 /* get the bit and make it a 0/1 */
1941 *puthere
= (v
>> gpnvm
) & 1;
1948 * Clears the selected GPNVM bit.
1949 * @param pPrivate info about the bank
1950 * @param gpnvm GPNVM index.
1951 * @returns 0 if successful; otherwise returns an error code.
1953 static int FLASHD_ClrGPNVM(struct sam3_bank_private
*pPrivate
, unsigned gpnvm
)
1959 if (pPrivate
->bank_number
!= 0) {
1960 LOG_ERROR("GPNVM only works with Bank0");
1964 if (gpnvm
>= pPrivate
->pChip
->details
.n_gpnvms
) {
1965 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1966 gpnvm
, pPrivate
->pChip
->details
.n_gpnvms
);
1970 r
= FLASHD_GetGPNVM(pPrivate
, gpnvm
, &v
);
1971 if (r
!= ERROR_OK
) {
1972 LOG_DEBUG("Failed: %d", r
);
1975 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_CFB
, gpnvm
, NULL
);
1976 LOG_DEBUG("End: %d", r
);
1981 * Sets the selected GPNVM bit.
1982 * @param pPrivate info about the bank
1983 * @param gpnvm GPNVM index.
1985 static int FLASHD_SetGPNVM(struct sam3_bank_private
*pPrivate
, unsigned gpnvm
)
1990 if (pPrivate
->bank_number
!= 0) {
1991 LOG_ERROR("GPNVM only works with Bank0");
1995 if (gpnvm
>= pPrivate
->pChip
->details
.n_gpnvms
) {
1996 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1997 gpnvm
, pPrivate
->pChip
->details
.n_gpnvms
);
2001 r
= FLASHD_GetGPNVM(pPrivate
, gpnvm
, &v
);
2009 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_SFB
, gpnvm
, NULL
);
2015 * Returns a bit field (at most 64) of locked regions within a page.
2016 * @param pPrivate info about the bank
2017 * @param v where to store locked bits
2019 static int FLASHD_GetLockBits(struct sam3_bank_private
*pPrivate
, uint32_t *v
)
2023 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_GLB
, 0, NULL
);
2025 r
= EFC_GetResult(pPrivate
, v
);
2026 LOG_DEBUG("End: %d", r
);
2031 * Unlocks all the regions in the given address range.
2032 * @param pPrivate info about the bank
2033 * @param start_sector first sector to unlock
2034 * @param end_sector last (inclusive) to unlock
2037 static int FLASHD_Unlock(struct sam3_bank_private
*pPrivate
,
2038 unsigned start_sector
,
2039 unsigned end_sector
)
2044 uint32_t pages_per_sector
;
2046 pages_per_sector
= pPrivate
->sector_size
/ pPrivate
->page_size
;
2048 /* Unlock all pages */
2049 while (start_sector
<= end_sector
) {
2050 pg
= start_sector
* pages_per_sector
;
2052 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_CLB
, pg
, &status
);
2063 * @param pPrivate - info about the bank
2064 * @param start_sector - first sector to lock
2065 * @param end_sector - last sector (inclusive) to lock
2067 static int FLASHD_Lock(struct sam3_bank_private
*pPrivate
,
2068 unsigned start_sector
,
2069 unsigned end_sector
)
2073 uint32_t pages_per_sector
;
2076 pages_per_sector
= pPrivate
->sector_size
/ pPrivate
->page_size
;
2078 /* Lock all pages */
2079 while (start_sector
<= end_sector
) {
2080 pg
= start_sector
* pages_per_sector
;
2082 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_SLB
, pg
, &status
);
2090 /****** END SAM3 CODE ********/
2092 /* begin helpful debug code */
2093 /* print the fieldname, the field value, in dec & hex, and return field value */
2094 static uint32_t sam3_reg_fieldname(struct sam3_chip
*pChip
,
2095 const char *regname
,
2104 /* extract the field */
2106 v
= v
& ((1 << width
)-1);
2115 /* show the basics */
2116 LOG_USER_N("\t%*s: %*d [0x%0*x] ",
2117 REG_NAME_WIDTH
, regname
,
2123 static const char _unknown
[] = "unknown";
2124 static const char *const eproc_names
[] = {
2128 "cortex-m3", /* 3 */
2130 "arm926ejs", /* 5 */
2143 #define nvpsize2 nvpsize /* these two tables are identical */
2144 static const char *const nvpsize
[] = {
2147 "16K bytes", /* 2 */
2148 "32K bytes", /* 3 */
2150 "64K bytes", /* 5 */
2152 "128K bytes", /* 7 */
2154 "256K bytes", /* 9 */
2155 "512K bytes", /* 10 */
2157 "1024K bytes", /* 12 */
2159 "2048K bytes", /* 14 */
2163 static const char *const sramsize
[] = {
2164 "48K Bytes", /* 0 */
2168 "112K Bytes", /* 4 */
2170 "80K Bytes", /* 6 */
2171 "160K Bytes", /* 7 */
2173 "16K Bytes", /* 9 */
2174 "32K Bytes", /* 10 */
2175 "64K Bytes", /* 11 */
2176 "128K Bytes", /* 12 */
2177 "256K Bytes", /* 13 */
2178 "96K Bytes", /* 14 */
2179 "512K Bytes", /* 15 */
2183 static const struct archnames
{ unsigned value
; const char *name
; } archnames
[] = {
2184 { 0x19, "AT91SAM9xx Series" },
2185 { 0x29, "AT91SAM9XExx Series" },
2186 { 0x34, "AT91x34 Series" },
2187 { 0x37, "CAP7 Series" },
2188 { 0x39, "CAP9 Series" },
2189 { 0x3B, "CAP11 Series" },
2190 { 0x40, "AT91x40 Series" },
2191 { 0x42, "AT91x42 Series" },
2192 { 0x55, "AT91x55 Series" },
2193 { 0x60, "AT91SAM7Axx Series" },
2194 { 0x61, "AT91SAM7AQxx Series" },
2195 { 0x63, "AT91x63 Series" },
2196 { 0x70, "AT91SAM7Sxx Series" },
2197 { 0x71, "AT91SAM7XCxx Series" },
2198 { 0x72, "AT91SAM7SExx Series" },
2199 { 0x73, "AT91SAM7Lxx Series" },
2200 { 0x75, "AT91SAM7Xxx Series" },
2201 { 0x76, "AT91SAM7SLxx Series" },
2202 { 0x80, "ATSAM3UxC Series (100-pin version)" },
2203 { 0x81, "ATSAM3UxE Series (144-pin version)" },
2204 { 0x83, "ATSAM3AxC Series (100-pin version)" },
2205 { 0x84, "ATSAM3XxC Series (100-pin version)" },
2206 { 0x85, "ATSAM3XxE Series (144-pin version)" },
2207 { 0x86, "ATSAM3XxG Series (208/217-pin version)" },
2208 { 0x88, "ATSAM3SxA Series (48-pin version)" },
2209 { 0x89, "ATSAM3SxB Series (64-pin version)" },
2210 { 0x8A, "ATSAM3SxC Series (100-pin version)" },
2211 { 0x92, "AT91x92 Series" },
2212 { 0x93, "ATSAM3NxA Series (48-pin version)" },
2213 { 0x94, "ATSAM3NxB Series (64-pin version)" },
2214 { 0x95, "ATSAM3NxC Series (100-pin version)" },
2215 { 0x98, "ATSAM3SDxA Series (48-pin version)" },
2216 { 0x99, "ATSAM3SDxB Series (64-pin version)" },
2217 { 0x9A, "ATSAM3SDxC Series (100-pin version)" },
2218 { 0xA5, "ATSAM5A" },
2219 { 0xF0, "AT75Cxx Series" },
2223 static const char *const nvptype
[] = {
2225 "romless or onchip flash", /* 1 */
2226 "embedded flash memory",/* 2 */
2227 "rom(nvpsiz) + embedded flash (nvpsiz2)", /* 3 */
2228 "sram emulating flash", /* 4 */
2234 static const char *_yes_or_no(uint32_t v
)
2242 static const char *const _rc_freq
[] = {
2243 "4 MHz", "8 MHz", "12 MHz", "reserved"
2246 static void sam3_explain_ckgr_mor(struct sam3_chip
*pChip
)
2251 v
= sam3_reg_fieldname(pChip
, "MOSCXTEN", pChip
->cfg
.CKGR_MOR
, 0, 1);
2252 LOG_USER("(main xtal enabled: %s)", _yes_or_no(v
));
2253 v
= sam3_reg_fieldname(pChip
, "MOSCXTBY", pChip
->cfg
.CKGR_MOR
, 1, 1);
2254 LOG_USER("(main osc bypass: %s)", _yes_or_no(v
));
2255 rcen
= sam3_reg_fieldname(pChip
, "MOSCRCEN", pChip
->cfg
.CKGR_MOR
, 3, 1);
2256 LOG_USER("(onchip RC-OSC enabled: %s)", _yes_or_no(rcen
));
2257 v
= sam3_reg_fieldname(pChip
, "MOSCRCF", pChip
->cfg
.CKGR_MOR
, 4, 3);
2258 LOG_USER("(onchip RC-OSC freq: %s)", _rc_freq
[v
]);
2260 pChip
->cfg
.rc_freq
= 0;
2264 pChip
->cfg
.rc_freq
= 0;
2267 pChip
->cfg
.rc_freq
= 4 * 1000 * 1000;
2270 pChip
->cfg
.rc_freq
= 8 * 1000 * 1000;
2273 pChip
->cfg
.rc_freq
= 12 * 1000 * 1000;
2278 v
= sam3_reg_fieldname(pChip
, "MOSCXTST", pChip
->cfg
.CKGR_MOR
, 8, 8);
2279 LOG_USER("(startup clks, time= %f uSecs)",
2280 ((float)(v
* 1000000)) / ((float)(pChip
->cfg
.slow_freq
)));
2281 v
= sam3_reg_fieldname(pChip
, "MOSCSEL", pChip
->cfg
.CKGR_MOR
, 24, 1);
2282 LOG_USER("(mainosc source: %s)",
2283 v
? "external xtal" : "internal RC");
2285 v
= sam3_reg_fieldname(pChip
, "CFDEN", pChip
->cfg
.CKGR_MOR
, 25, 1);
2286 LOG_USER("(clock failure enabled: %s)",
2290 static void sam3_explain_chipid_cidr(struct sam3_chip
*pChip
)
2296 sam3_reg_fieldname(pChip
, "Version", pChip
->cfg
.CHIPID_CIDR
, 0, 5);
2299 v
= sam3_reg_fieldname(pChip
, "EPROC", pChip
->cfg
.CHIPID_CIDR
, 5, 3);
2300 LOG_USER("%s", eproc_names
[v
]);
2302 v
= sam3_reg_fieldname(pChip
, "NVPSIZE", pChip
->cfg
.CHIPID_CIDR
, 8, 4);
2303 LOG_USER("%s", nvpsize
[v
]);
2305 v
= sam3_reg_fieldname(pChip
, "NVPSIZE2", pChip
->cfg
.CHIPID_CIDR
, 12, 4);
2306 LOG_USER("%s", nvpsize2
[v
]);
2308 v
= sam3_reg_fieldname(pChip
, "SRAMSIZE", pChip
->cfg
.CHIPID_CIDR
, 16, 4);
2309 LOG_USER("%s", sramsize
[v
]);
2311 v
= sam3_reg_fieldname(pChip
, "ARCH", pChip
->cfg
.CHIPID_CIDR
, 20, 8);
2313 for (x
= 0; archnames
[x
].name
; x
++) {
2314 if (v
== archnames
[x
].value
) {
2315 cp
= archnames
[x
].name
;
2322 v
= sam3_reg_fieldname(pChip
, "NVPTYP", pChip
->cfg
.CHIPID_CIDR
, 28, 3);
2323 LOG_USER("%s", nvptype
[v
]);
2325 v
= sam3_reg_fieldname(pChip
, "EXTID", pChip
->cfg
.CHIPID_CIDR
, 31, 1);
2326 LOG_USER("(exists: %s)", _yes_or_no(v
));
2329 static void sam3_explain_ckgr_mcfr(struct sam3_chip
*pChip
)
2333 v
= sam3_reg_fieldname(pChip
, "MAINFRDY", pChip
->cfg
.CKGR_MCFR
, 16, 1);
2334 LOG_USER("(main ready: %s)", _yes_or_no(v
));
2336 v
= sam3_reg_fieldname(pChip
, "MAINF", pChip
->cfg
.CKGR_MCFR
, 0, 16);
2338 v
= (v
* pChip
->cfg
.slow_freq
) / 16;
2339 pChip
->cfg
.mainosc_freq
= v
;
2341 LOG_USER("(%3.03f Mhz (%d.%03dkhz slowclk)",
2343 pChip
->cfg
.slow_freq
/ 1000,
2344 pChip
->cfg
.slow_freq
% 1000);
2347 static void sam3_explain_ckgr_plla(struct sam3_chip
*pChip
)
2349 uint32_t mula
, diva
;
2351 diva
= sam3_reg_fieldname(pChip
, "DIVA", pChip
->cfg
.CKGR_PLLAR
, 0, 8);
2353 mula
= sam3_reg_fieldname(pChip
, "MULA", pChip
->cfg
.CKGR_PLLAR
, 16, 11);
2355 pChip
->cfg
.plla_freq
= 0;
2357 LOG_USER("\tPLLA Freq: (Disabled,mula = 0)");
2359 LOG_USER("\tPLLA Freq: (Disabled,diva = 0)");
2360 else if (diva
== 1) {
2361 pChip
->cfg
.plla_freq
= (pChip
->cfg
.mainosc_freq
* (mula
+ 1));
2362 LOG_USER("\tPLLA Freq: %3.03f MHz",
2363 _tomhz(pChip
->cfg
.plla_freq
));
2367 static void sam3_explain_mckr(struct sam3_chip
*pChip
)
2369 uint32_t css
, pres
, fin
= 0;
2371 const char *cp
= NULL
;
2373 css
= sam3_reg_fieldname(pChip
, "CSS", pChip
->cfg
.PMC_MCKR
, 0, 2);
2376 fin
= pChip
->cfg
.slow_freq
;
2380 fin
= pChip
->cfg
.mainosc_freq
;
2384 fin
= pChip
->cfg
.plla_freq
;
2388 if (pChip
->cfg
.CKGR_UCKR
& (1 << 16)) {
2389 fin
= 480 * 1000 * 1000;
2393 cp
= "upll (*ERROR* UPLL is disabled)";
2401 LOG_USER("%s (%3.03f Mhz)",
2404 pres
= sam3_reg_fieldname(pChip
, "PRES", pChip
->cfg
.PMC_MCKR
, 4, 3);
2405 switch (pres
& 0x07) {
2408 cp
= "selected clock";
2442 LOG_USER("(%s)", cp
);
2444 /* sam3 has a *SINGLE* clock - */
2445 /* other at91 series parts have divisors for these. */
2446 pChip
->cfg
.cpu_freq
= fin
;
2447 pChip
->cfg
.mclk_freq
= fin
;
2448 pChip
->cfg
.fclk_freq
= fin
;
2449 LOG_USER("\t\tResult CPU Freq: %3.03f",
2454 static struct sam3_chip
*target2sam3(struct target
*pTarget
)
2456 struct sam3_chip
*pChip
;
2458 if (pTarget
== NULL
)
2461 pChip
= all_sam3_chips
;
2463 if (pChip
->target
== pTarget
)
2464 break; /* return below */
2466 pChip
= pChip
->next
;
2472 static uint32_t *sam3_get_reg_ptr(struct sam3_cfg
*pCfg
, const struct sam3_reg_list
*pList
)
2474 /* this function exists to help */
2475 /* keep funky offsetof() errors */
2476 /* and casting from causing bugs */
2478 /* By using prototypes - we can detect what would */
2479 /* be casting errors. */
2481 return (uint32_t *)(void *)(((char *)(pCfg
)) + pList
->struct_offset
);
2485 #define SAM3_ENTRY(NAME, FUNC) { .address = SAM3_ ## NAME, .struct_offset = offsetof( \
2487 NAME), # NAME, FUNC }
2488 static const struct sam3_reg_list sam3_all_regs
[] = {
2489 SAM3_ENTRY(CKGR_MOR
, sam3_explain_ckgr_mor
),
2490 SAM3_ENTRY(CKGR_MCFR
, sam3_explain_ckgr_mcfr
),
2491 SAM3_ENTRY(CKGR_PLLAR
, sam3_explain_ckgr_plla
),
2492 SAM3_ENTRY(CKGR_UCKR
, NULL
),
2493 SAM3_ENTRY(PMC_FSMR
, NULL
),
2494 SAM3_ENTRY(PMC_FSPR
, NULL
),
2495 SAM3_ENTRY(PMC_IMR
, NULL
),
2496 SAM3_ENTRY(PMC_MCKR
, sam3_explain_mckr
),
2497 SAM3_ENTRY(PMC_PCK0
, NULL
),
2498 SAM3_ENTRY(PMC_PCK1
, NULL
),
2499 SAM3_ENTRY(PMC_PCK2
, NULL
),
2500 SAM3_ENTRY(PMC_PCSR
, NULL
),
2501 SAM3_ENTRY(PMC_SCSR
, NULL
),
2502 SAM3_ENTRY(PMC_SR
, NULL
),
2503 SAM3_ENTRY(CHIPID_CIDR
, sam3_explain_chipid_cidr
),
2504 SAM3_ENTRY(CHIPID_CIDR2
, sam3_explain_chipid_cidr
),
2505 SAM3_ENTRY(CHIPID_EXID
, NULL
),
2506 SAM3_ENTRY(CHIPID_EXID2
, NULL
),
2507 /* TERMINATE THE LIST */
2512 static struct sam3_bank_private
*get_sam3_bank_private(struct flash_bank
*bank
)
2514 return (struct sam3_bank_private
*)(bank
->driver_priv
);
2518 * Given a pointer to where it goes in the structure,
2519 * determine the register name, address from the all registers table.
2521 static const struct sam3_reg_list
*sam3_GetReg(struct sam3_chip
*pChip
, uint32_t *goes_here
)
2523 const struct sam3_reg_list
*pReg
;
2525 pReg
= &(sam3_all_regs
[0]);
2526 while (pReg
->name
) {
2527 uint32_t *pPossible
;
2529 /* calculate where this one go.. */
2530 /* it is "possibly" this register. */
2532 pPossible
= ((uint32_t *)(void *)(((char *)(&(pChip
->cfg
))) + pReg
->struct_offset
));
2534 /* well? Is it this register */
2535 if (pPossible
== goes_here
) {
2543 /* This is *TOTAL*PANIC* - we are totally screwed. */
2544 LOG_ERROR("INVALID SAM3 REGISTER");
2548 static int sam3_ReadThisReg(struct sam3_chip
*pChip
, uint32_t *goes_here
)
2550 const struct sam3_reg_list
*pReg
;
2553 pReg
= sam3_GetReg(pChip
, goes_here
);
2557 r
= target_read_u32(pChip
->target
, pReg
->address
, goes_here
);
2558 if (r
!= ERROR_OK
) {
2559 LOG_ERROR("Cannot read SAM3 register: %s @ 0x%08x, Err: %d",
2560 pReg
->name
, (unsigned)(pReg
->address
), r
);
2565 static int sam3_ReadAllRegs(struct sam3_chip
*pChip
)
2568 const struct sam3_reg_list
*pReg
;
2570 pReg
= &(sam3_all_regs
[0]);
2571 while (pReg
->name
) {
2572 r
= sam3_ReadThisReg(pChip
,
2573 sam3_get_reg_ptr(&(pChip
->cfg
), pReg
));
2574 if (r
!= ERROR_OK
) {
2575 LOG_ERROR("Cannot read SAM3 registere: %s @ 0x%08x, Error: %d",
2576 pReg
->name
, ((unsigned)(pReg
->address
)), r
);
2582 /* Chip identification register
2584 * Unfortunately, the chip identification register is not at
2585 * a constant address across all of the SAM3 series'. As a
2586 * consequence, a simple heuristic is used to find where it's
2589 * If the contents at the first address is zero, then we know
2590 * that the second address is where the chip id register is.
2591 * We can deduce this because for those SAM's that have the
2592 * chip id @ 0x400e0940, the first address, 0x400e0740, is
2593 * located in the memory map of the Power Management Controller
2594 * (PMC). Furthermore, the address is not used by the PMC.
2595 * So when read, the memory controller returns zero.*/
2596 if (pChip
->cfg
.CHIPID_CIDR
== 0) {
2597 /*Put the correct CIDR and EXID values in the pChip structure */
2598 pChip
->cfg
.CHIPID_CIDR
= pChip
->cfg
.CHIPID_CIDR2
;
2599 pChip
->cfg
.CHIPID_EXID
= pChip
->cfg
.CHIPID_EXID2
;
2604 static int sam3_GetInfo(struct sam3_chip
*pChip
)
2606 const struct sam3_reg_list
*pReg
;
2609 pReg
= &(sam3_all_regs
[0]);
2610 while (pReg
->name
) {
2611 /* display all regs */
2612 LOG_DEBUG("Start: %s", pReg
->name
);
2613 regval
= *sam3_get_reg_ptr(&(pChip
->cfg
), pReg
);
2614 LOG_USER("%*s: [0x%08x] -> 0x%08x",
2619 if (pReg
->explain_func
)
2620 (*(pReg
->explain_func
))(pChip
);
2621 LOG_DEBUG("End: %s", pReg
->name
);
2624 LOG_USER(" rc-osc: %3.03f MHz", _tomhz(pChip
->cfg
.rc_freq
));
2625 LOG_USER(" mainosc: %3.03f MHz", _tomhz(pChip
->cfg
.mainosc_freq
));
2626 LOG_USER(" plla: %3.03f MHz", _tomhz(pChip
->cfg
.plla_freq
));
2627 LOG_USER(" cpu-freq: %3.03f MHz", _tomhz(pChip
->cfg
.cpu_freq
));
2628 LOG_USER("mclk-freq: %3.03f MHz", _tomhz(pChip
->cfg
.mclk_freq
));
2630 LOG_USER(" UniqueId: 0x%08x 0x%08x 0x%08x 0x%08x",
2631 pChip
->cfg
.unique_id
[0],
2632 pChip
->cfg
.unique_id
[1],
2633 pChip
->cfg
.unique_id
[2],
2634 pChip
->cfg
.unique_id
[3]);
2639 static int sam3_erase_check(struct flash_bank
*bank
)
2644 if (bank
->target
->state
!= TARGET_HALTED
) {
2645 LOG_ERROR("Target not halted");
2646 return ERROR_TARGET_NOT_HALTED
;
2648 if (0 == bank
->num_sectors
) {
2649 LOG_ERROR("Target: not supported/not probed");
2653 LOG_INFO("sam3 - supports auto-erase, erase_check ignored");
2654 for (x
= 0; x
< bank
->num_sectors
; x
++)
2655 bank
->sectors
[x
].is_erased
= 1;
2661 static int sam3_protect_check(struct flash_bank
*bank
)
2666 struct sam3_bank_private
*pPrivate
;
2669 if (bank
->target
->state
!= TARGET_HALTED
) {
2670 LOG_ERROR("Target not halted");
2671 return ERROR_TARGET_NOT_HALTED
;
2674 pPrivate
= get_sam3_bank_private(bank
);
2676 LOG_ERROR("no private for this bank?");
2679 if (!(pPrivate
->probed
))
2680 return ERROR_FLASH_BANK_NOT_PROBED
;
2682 r
= FLASHD_GetLockBits(pPrivate
, &v
);
2683 if (r
!= ERROR_OK
) {
2684 LOG_DEBUG("Failed: %d", r
);
2688 for (x
= 0; x
< pPrivate
->nsectors
; x
++)
2689 bank
->sectors
[x
].is_protected
= (!!(v
& (1 << x
)));
2694 FLASH_BANK_COMMAND_HANDLER(sam3_flash_bank_command
)
2696 struct sam3_chip
*pChip
;
2698 pChip
= all_sam3_chips
;
2700 /* is this an existing chip? */
2702 if (pChip
->target
== bank
->target
)
2704 pChip
= pChip
->next
;
2708 /* this is a *NEW* chip */
2709 pChip
= calloc(1, sizeof(struct sam3_chip
));
2711 LOG_ERROR("NO RAM!");
2714 pChip
->target
= bank
->target
;
2715 /* insert at head */
2716 pChip
->next
= all_sam3_chips
;
2717 all_sam3_chips
= pChip
;
2718 pChip
->target
= bank
->target
;
2719 /* assumption is this runs at 32khz */
2720 pChip
->cfg
.slow_freq
= 32768;
2724 switch (bank
->base
) {
2726 LOG_ERROR("Address 0x%08x invalid bank address (try 0x%08x or 0x%08x "
2727 "[at91sam3u series] or 0x%08x [at91sam3s series] or "
2728 "0x%08x [at91sam3n series] or 0x%08x or 0x%08x or 0x%08x[at91sam3ax series] )",
2729 ((unsigned int)(bank
->base
)),
2730 ((unsigned int)(FLASH_BANK0_BASE_U
)),
2731 ((unsigned int)(FLASH_BANK1_BASE_U
)),
2732 ((unsigned int)(FLASH_BANK_BASE_S
)),
2733 ((unsigned int)(FLASH_BANK_BASE_N
)),
2734 ((unsigned int)(FLASH_BANK0_BASE_AX
)),
2735 ((unsigned int)(FLASH_BANK1_BASE_256K_AX
)),
2736 ((unsigned int)(FLASH_BANK1_BASE_512K_AX
)));
2740 /* at91sam3s and at91sam3n series only has bank 0*/
2741 /* at91sam3u and at91sam3ax series has the same address for bank 0*/
2742 case FLASH_BANK_BASE_S
:
2743 case FLASH_BANK0_BASE_U
:
2744 bank
->driver_priv
= &(pChip
->details
.bank
[0]);
2745 bank
->bank_number
= 0;
2746 pChip
->details
.bank
[0].pChip
= pChip
;
2747 pChip
->details
.bank
[0].pBank
= bank
;
2750 /* Bank 1 of at91sam3u or at91sam3ax series */
2751 case FLASH_BANK1_BASE_U
:
2752 case FLASH_BANK1_BASE_256K_AX
:
2753 case FLASH_BANK1_BASE_512K_AX
:
2754 bank
->driver_priv
= &(pChip
->details
.bank
[1]);
2755 bank
->bank_number
= 1;
2756 pChip
->details
.bank
[1].pChip
= pChip
;
2757 pChip
->details
.bank
[1].pBank
= bank
;
2761 /* we initialize after probing. */
2765 static int sam3_GetDetails(struct sam3_bank_private
*pPrivate
)
2767 const struct sam3_chip_details
*pDetails
;
2768 struct sam3_chip
*pChip
;
2769 struct flash_bank
*saved_banks
[SAM3_MAX_FLASH_BANKS
];
2773 pDetails
= all_sam3_details
;
2774 while (pDetails
->name
) {
2775 /* Compare cidr without version bits */
2776 if (pDetails
->chipid_cidr
== (pPrivate
->pChip
->cfg
.CHIPID_CIDR
& 0xFFFFFFE0))
2781 if (pDetails
->name
== NULL
) {
2782 LOG_ERROR("SAM3 ChipID 0x%08x not found in table (perhaps you can ID this chip?)",
2783 (unsigned int)(pPrivate
->pChip
->cfg
.CHIPID_CIDR
));
2784 /* Help the victim, print details about the chip */
2785 LOG_INFO("SAM3 CHIPID_CIDR: 0x%08x decodes as follows",
2786 pPrivate
->pChip
->cfg
.CHIPID_CIDR
);
2787 sam3_explain_chipid_cidr(pPrivate
->pChip
);
2791 /* DANGER: THERE ARE DRAGONS HERE */
2793 /* get our pChip - it is going */
2794 /* to be over-written shortly */
2795 pChip
= pPrivate
->pChip
;
2797 /* Note that, in reality: */
2799 /* pPrivate = &(pChip->details.bank[0]) */
2800 /* or pPrivate = &(pChip->details.bank[1]) */
2803 /* save the "bank" pointers */
2804 for (x
= 0; x
< SAM3_MAX_FLASH_BANKS
; x
++)
2805 saved_banks
[x
] = pChip
->details
.bank
[x
].pBank
;
2807 /* Overwrite the "details" structure. */
2808 memcpy(&(pPrivate
->pChip
->details
),
2810 sizeof(pPrivate
->pChip
->details
));
2812 /* now fix the ghosted pointers */
2813 for (x
= 0; x
< SAM3_MAX_FLASH_BANKS
; x
++) {
2814 pChip
->details
.bank
[x
].pChip
= pChip
;
2815 pChip
->details
.bank
[x
].pBank
= saved_banks
[x
];
2818 /* update the *BANK*SIZE* */
2824 static int _sam3_probe(struct flash_bank
*bank
, int noise
)
2828 struct sam3_bank_private
*pPrivate
;
2831 LOG_DEBUG("Begin: Bank: %d, Noise: %d", bank
->bank_number
, noise
);
2832 if (bank
->target
->state
!= TARGET_HALTED
) {
2833 LOG_ERROR("Target not halted");
2834 return ERROR_TARGET_NOT_HALTED
;
2837 pPrivate
= get_sam3_bank_private(bank
);
2839 LOG_ERROR("Invalid/unknown bank number");
2843 r
= sam3_ReadAllRegs(pPrivate
->pChip
);
2848 if (pPrivate
->pChip
->probed
)
2849 r
= sam3_GetInfo(pPrivate
->pChip
);
2851 r
= sam3_GetDetails(pPrivate
);
2855 /* update the flash bank size */
2856 for (x
= 0; x
< SAM3_MAX_FLASH_BANKS
; x
++) {
2857 if (bank
->base
== pPrivate
->pChip
->details
.bank
[x
].base_address
) {
2858 bank
->size
= pPrivate
->pChip
->details
.bank
[x
].size_bytes
;
2863 if (bank
->sectors
== NULL
) {
2864 bank
->sectors
= calloc(pPrivate
->nsectors
, (sizeof((bank
->sectors
)[0])));
2865 if (bank
->sectors
== NULL
) {
2866 LOG_ERROR("No memory!");
2869 bank
->num_sectors
= pPrivate
->nsectors
;
2871 for (x
= 0; ((int)(x
)) < bank
->num_sectors
; x
++) {
2872 bank
->sectors
[x
].size
= pPrivate
->sector_size
;
2873 bank
->sectors
[x
].offset
= x
* (pPrivate
->sector_size
);
2874 /* mark as unknown */
2875 bank
->sectors
[x
].is_erased
= -1;
2876 bank
->sectors
[x
].is_protected
= -1;
2880 pPrivate
->probed
= 1;
2882 r
= sam3_protect_check(bank
);
2886 LOG_DEBUG("Bank = %d, nbanks = %d",
2887 pPrivate
->bank_number
, pPrivate
->pChip
->details
.n_banks
);
2888 if ((pPrivate
->bank_number
+ 1) == pPrivate
->pChip
->details
.n_banks
) {
2889 /* read unique id, */
2890 /* it appears to be associated with the *last* flash bank. */
2891 FLASHD_ReadUniqueID(pPrivate
);
2897 static int sam3_probe(struct flash_bank
*bank
)
2899 return _sam3_probe(bank
, 1);
2902 static int sam3_auto_probe(struct flash_bank
*bank
)
2904 return _sam3_probe(bank
, 0);
2907 static int sam3_erase(struct flash_bank
*bank
, int first
, int last
)
2909 struct sam3_bank_private
*pPrivate
;
2913 if (bank
->target
->state
!= TARGET_HALTED
) {
2914 LOG_ERROR("Target not halted");
2915 return ERROR_TARGET_NOT_HALTED
;
2918 r
= sam3_auto_probe(bank
);
2919 if (r
!= ERROR_OK
) {
2920 LOG_DEBUG("Here,r=%d", r
);
2924 pPrivate
= get_sam3_bank_private(bank
);
2925 if (!(pPrivate
->probed
))
2926 return ERROR_FLASH_BANK_NOT_PROBED
;
2928 if ((first
== 0) && ((last
+ 1) == ((int)(pPrivate
->nsectors
)))) {
2931 return FLASHD_EraseEntireBank(pPrivate
);
2933 LOG_INFO("sam3 auto-erases while programing (request ignored)");
2937 static int sam3_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
2939 struct sam3_bank_private
*pPrivate
;
2943 if (bank
->target
->state
!= TARGET_HALTED
) {
2944 LOG_ERROR("Target not halted");
2945 return ERROR_TARGET_NOT_HALTED
;
2948 pPrivate
= get_sam3_bank_private(bank
);
2949 if (!(pPrivate
->probed
))
2950 return ERROR_FLASH_BANK_NOT_PROBED
;
2953 r
= FLASHD_Lock(pPrivate
, (unsigned)(first
), (unsigned)(last
));
2955 r
= FLASHD_Unlock(pPrivate
, (unsigned)(first
), (unsigned)(last
));
2956 LOG_DEBUG("End: r=%d", r
);
2962 static int sam3_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
2964 if (bank
->target
->state
!= TARGET_HALTED
) {
2965 LOG_ERROR("Target not halted");
2966 return ERROR_TARGET_NOT_HALTED
;
2972 static int sam3_page_read(struct sam3_bank_private
*pPrivate
, unsigned pagenum
, uint8_t *buf
)
2977 adr
= pagenum
* pPrivate
->page_size
;
2978 adr
+= adr
+ pPrivate
->base_address
;
2980 r
= target_read_memory(pPrivate
->pChip
->target
,
2982 4, /* THIS*MUST*BE* in 32bit values */
2983 pPrivate
->page_size
/ 4,
2986 LOG_ERROR("SAM3: Flash program failed to read page phys address: 0x%08x",
2987 (unsigned int)(adr
));
2991 /* The code below is basically this: */
2993 /* arm-none-eabi-gcc -mthumb -mcpu = cortex-m3 -O9 -S ./foobar.c -o foobar.s */
2995 /* Only the *CPU* can write to the flash buffer. */
2996 /* the DAP cannot... so - we download this 28byte thing */
2997 /* Run the algorithm - (below) */
2998 /* to program the device */
3000 /* ======================================== */
3001 /* #include <stdint.h> */
3004 /* uint32_t *dst; */
3005 /* const uint32_t *src; */
3007 /* volatile uint32_t *base; */
3012 /* uint32_t sam3_function(struct foo *p) */
3014 /* volatile uint32_t *v; */
3016 /* const uint32_t *s; */
3031 /* v[ 1 ] = p->cmd; */
3034 /* } while (!(r&1)) */
3038 /* ======================================== */
3040 static const uint8_t
3041 sam3_page_write_opcodes
[] = {
3042 /* 24 0000 0446 mov r4, r0 */
3044 /* 25 0002 6168 ldr r1, [r4, #4] */
3046 /* 26 0004 0068 ldr r0, [r0, #0] */
3048 /* 27 0006 A268 ldr r2, [r4, #8] */
3050 /* 28 @ lr needed for prologue */
3052 /* 30 0008 51F8043B ldr r3, [r1], #4 */
3053 0x51, 0xf8, 0x04, 0x3b,
3054 /* 31 000c 12F1FF32 adds r2, r2, #-1 */
3055 0x12, 0xf1, 0xff, 0x32,
3056 /* 32 0010 40F8043B str r3, [r0], #4 */
3057 0x40, 0xf8, 0x04, 0x3b,
3058 /* 33 0014 F8D1 bne .L2 */
3060 /* 34 0016 E268 ldr r2, [r4, #12] */
3062 /* 35 0018 2369 ldr r3, [r4, #16] */
3064 /* 36 001a 5360 str r3, [r2, #4] */
3066 /* 37 001c 0832 adds r2, r2, #8 */
3069 /* 39 001e 1068 ldr r0, [r2, #0] */
3071 /* 40 0020 10F0010F tst r0, #1 */
3072 0x10, 0xf0, 0x01, 0x0f,
3073 /* 41 0024 FBD0 beq .L4 */
3075 0x00, 0xBE /* bkpt #0 */
3078 static int sam3_page_write(struct sam3_bank_private
*pPrivate
, unsigned pagenum
, uint8_t *buf
)
3082 uint32_t fmr
; /* EEFC Flash Mode Register */
3085 adr
= pagenum
* pPrivate
->page_size
;
3086 adr
+= (adr
+ pPrivate
->base_address
);
3088 /* Get flash mode register value */
3089 r
= target_read_u32(pPrivate
->pChip
->target
, pPrivate
->controller_address
, &fmr
);
3091 LOG_DEBUG("Error Read failed: read flash mode register");
3093 /* Clear flash wait state field */
3096 /* set FWS (flash wait states) field in the FMR (flash mode register) */
3097 fmr
|= (pPrivate
->flash_wait_states
<< 8);
3099 LOG_DEBUG("Flash Mode: 0x%08x", ((unsigned int)(fmr
)));
3100 r
= target_write_u32(pPrivate
->pBank
->target
, pPrivate
->controller_address
, fmr
);
3102 LOG_DEBUG("Error Write failed: set flash mode register");
3104 LOG_DEBUG("Wr Page %u @ phys address: 0x%08x", pagenum
, (unsigned int)(adr
));
3105 r
= target_write_memory(pPrivate
->pChip
->target
,
3107 4, /* THIS*MUST*BE* in 32bit values */
3108 pPrivate
->page_size
/ 4,
3110 if (r
!= ERROR_OK
) {
3111 LOG_ERROR("SAM3: Failed to write (buffer) page at phys address 0x%08x",
3112 (unsigned int)(adr
));
3116 r
= EFC_PerformCommand(pPrivate
,
3117 /* send Erase & Write Page */
3123 LOG_ERROR("SAM3: Error performing Erase & Write page @ phys address 0x%08x",
3124 (unsigned int)(adr
));
3125 if (status
& (1 << 2)) {
3126 LOG_ERROR("SAM3: Page @ Phys address 0x%08x is locked", (unsigned int)(adr
));
3129 if (status
& (1 << 1)) {
3130 LOG_ERROR("SAM3: Flash Command error @phys address 0x%08x", (unsigned int)(adr
));
3136 static int sam3_write(struct flash_bank
*bank
,
3145 unsigned page_offset
;
3146 struct sam3_bank_private
*pPrivate
;
3147 uint8_t *pagebuffer
;
3149 /* incase we bail further below, set this to null */
3152 /* ignore dumb requests */
3158 if (bank
->target
->state
!= TARGET_HALTED
) {
3159 LOG_ERROR("Target not halted");
3160 r
= ERROR_TARGET_NOT_HALTED
;
3164 pPrivate
= get_sam3_bank_private(bank
);
3165 if (!(pPrivate
->probed
)) {
3166 r
= ERROR_FLASH_BANK_NOT_PROBED
;
3170 if ((offset
+ count
) > pPrivate
->size_bytes
) {
3171 LOG_ERROR("Flash write error - past end of bank");
3172 LOG_ERROR(" offset: 0x%08x, count 0x%08x, BankEnd: 0x%08x",
3173 (unsigned int)(offset
),
3174 (unsigned int)(count
),
3175 (unsigned int)(pPrivate
->size_bytes
));
3180 pagebuffer
= malloc(pPrivate
->page_size
);
3182 LOG_ERROR("No memory for %d Byte page buffer", (int)(pPrivate
->page_size
));
3187 /* what page do we start & end in? */
3188 page_cur
= offset
/ pPrivate
->page_size
;
3189 page_end
= (offset
+ count
- 1) / pPrivate
->page_size
;
3191 LOG_DEBUG("Offset: 0x%08x, Count: 0x%08x", (unsigned int)(offset
), (unsigned int)(count
));
3192 LOG_DEBUG("Page start: %d, Page End: %d", (int)(page_cur
), (int)(page_end
));
3194 /* Special case: all one page */
3197 /* (1) non-aligned start */
3198 /* (2) body pages */
3199 /* (3) non-aligned end. */
3201 /* Handle special case - all one page. */
3202 if (page_cur
== page_end
) {
3203 LOG_DEBUG("Special case, all in one page");
3204 r
= sam3_page_read(pPrivate
, page_cur
, pagebuffer
);
3208 page_offset
= (offset
& (pPrivate
->page_size
-1));
3209 memcpy(pagebuffer
+ page_offset
,
3213 r
= sam3_page_write(pPrivate
, page_cur
, pagebuffer
);
3220 /* non-aligned start */
3221 page_offset
= offset
& (pPrivate
->page_size
- 1);
3223 LOG_DEBUG("Not-Aligned start");
3224 /* read the partial */
3225 r
= sam3_page_read(pPrivate
, page_cur
, pagebuffer
);
3229 /* over-write with new data */
3230 n
= (pPrivate
->page_size
- page_offset
);
3231 memcpy(pagebuffer
+ page_offset
,
3235 r
= sam3_page_write(pPrivate
, page_cur
, pagebuffer
);
3245 /* By checking that offset is correct here, we also
3246 fix a clang warning */
3247 assert(offset
% pPrivate
->page_size
== 0);
3249 /* intermediate large pages */
3250 /* also - the final *terminal* */
3251 /* if that terminal page is a full page */
3252 LOG_DEBUG("Full Page Loop: cur=%d, end=%d, count = 0x%08x",
3253 (int)page_cur
, (int)page_end
, (unsigned int)(count
));
3255 while ((page_cur
< page_end
) &&
3256 (count
>= pPrivate
->page_size
)) {
3257 r
= sam3_page_write(pPrivate
, page_cur
, buffer
);
3260 count
-= pPrivate
->page_size
;
3261 buffer
+= pPrivate
->page_size
;
3265 /* terminal partial page? */
3267 LOG_DEBUG("Terminal partial page, count = 0x%08x", (unsigned int)(count
));
3268 /* we have a partial page */
3269 r
= sam3_page_read(pPrivate
, page_cur
, pagebuffer
);
3272 /* data goes at start */
3273 memcpy(pagebuffer
, buffer
, count
);
3274 r
= sam3_page_write(pPrivate
, page_cur
, pagebuffer
);
3286 COMMAND_HANDLER(sam3_handle_info_command
)
3288 struct sam3_chip
*pChip
;
3289 pChip
= get_current_sam3(CMD_CTX
);
3296 /* bank0 must exist before we can do anything */
3297 if (pChip
->details
.bank
[0].pBank
== NULL
) {
3300 command_print(CMD_CTX
,
3301 "Please define bank %d via command: flash bank %s ... ",
3303 at91sam3_flash
.name
);
3307 /* if bank 0 is not probed, then probe it */
3308 if (!(pChip
->details
.bank
[0].probed
)) {
3309 r
= sam3_auto_probe(pChip
->details
.bank
[0].pBank
);
3313 /* above guarantees the "chip details" structure is valid */
3314 /* and thus, bank private areas are valid */
3315 /* and we have a SAM3 chip, what a concept! */
3317 /* auto-probe other banks, 0 done above */
3318 for (x
= 1; x
< SAM3_MAX_FLASH_BANKS
; x
++) {
3319 /* skip banks not present */
3320 if (!(pChip
->details
.bank
[x
].present
))
3323 if (pChip
->details
.bank
[x
].pBank
== NULL
)
3326 if (pChip
->details
.bank
[x
].probed
)
3329 r
= sam3_auto_probe(pChip
->details
.bank
[x
].pBank
);
3334 r
= sam3_GetInfo(pChip
);
3335 if (r
!= ERROR_OK
) {
3336 LOG_DEBUG("Sam3Info, Failed %d", r
);
3343 COMMAND_HANDLER(sam3_handle_gpnvm_command
)
3347 struct sam3_chip
*pChip
;
3349 pChip
= get_current_sam3(CMD_CTX
);
3353 if (pChip
->target
->state
!= TARGET_HALTED
) {
3354 LOG_ERROR("sam3 - target not halted");
3355 return ERROR_TARGET_NOT_HALTED
;
3358 if (pChip
->details
.bank
[0].pBank
== NULL
) {
3359 command_print(CMD_CTX
, "Bank0 must be defined first via: flash bank %s ...",
3360 at91sam3_flash
.name
);
3363 if (!pChip
->details
.bank
[0].probed
) {
3364 r
= sam3_auto_probe(pChip
->details
.bank
[0].pBank
);
3371 return ERROR_COMMAND_SYNTAX_ERROR
;
3380 if ((0 == strcmp(CMD_ARGV
[0], "show")) && (0 == strcmp(CMD_ARGV
[1], "all")))
3384 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], v32
);
3390 if (0 == strcmp("show", CMD_ARGV
[0])) {
3394 for (x
= 0; x
< pChip
->details
.n_gpnvms
; x
++) {
3395 r
= FLASHD_GetGPNVM(&(pChip
->details
.bank
[0]), x
, &v
);
3398 command_print(CMD_CTX
, "sam3-gpnvm%u: %u", x
, v
);
3402 if ((who
>= 0) && (((unsigned)(who
)) < pChip
->details
.n_gpnvms
)) {
3403 r
= FLASHD_GetGPNVM(&(pChip
->details
.bank
[0]), who
, &v
);
3404 command_print(CMD_CTX
, "sam3-gpnvm%u: %u", who
, v
);
3407 command_print(CMD_CTX
, "sam3-gpnvm invalid GPNVM: %u", who
);
3408 return ERROR_COMMAND_SYNTAX_ERROR
;
3413 command_print(CMD_CTX
, "Missing GPNVM number");
3414 return ERROR_COMMAND_SYNTAX_ERROR
;
3417 if (0 == strcmp("set", CMD_ARGV
[0]))
3418 r
= FLASHD_SetGPNVM(&(pChip
->details
.bank
[0]), who
);
3419 else if ((0 == strcmp("clr", CMD_ARGV
[0])) ||
3420 (0 == strcmp("clear", CMD_ARGV
[0]))) /* quietly accept both */
3421 r
= FLASHD_ClrGPNVM(&(pChip
->details
.bank
[0]), who
);
3423 command_print(CMD_CTX
, "Unknown command: %s", CMD_ARGV
[0]);
3424 r
= ERROR_COMMAND_SYNTAX_ERROR
;
3429 COMMAND_HANDLER(sam3_handle_slowclk_command
)
3431 struct sam3_chip
*pChip
;
3433 pChip
= get_current_sam3(CMD_CTX
);
3445 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], v
);
3447 /* absurd slow clock of 200Khz? */
3448 command_print(CMD_CTX
, "Absurd/illegal slow clock freq: %d\n", (int)(v
));
3449 return ERROR_COMMAND_SYNTAX_ERROR
;
3451 pChip
->cfg
.slow_freq
= v
;
3456 command_print(CMD_CTX
, "Too many parameters");
3457 return ERROR_COMMAND_SYNTAX_ERROR
;
3460 command_print(CMD_CTX
, "Slowclk freq: %d.%03dkhz",
3461 (int)(pChip
->cfg
.slow_freq
/ 1000),
3462 (int)(pChip
->cfg
.slow_freq
% 1000));
3466 static const struct command_registration at91sam3_exec_command_handlers
[] = {
3469 .handler
= sam3_handle_gpnvm_command
,
3470 .mode
= COMMAND_EXEC
,
3471 .usage
= "[('clr'|'set'|'show') bitnum]",
3472 .help
= "Without arguments, shows all bits in the gpnvm "
3473 "register. Otherwise, clears, sets, or shows one "
3474 "General Purpose Non-Volatile Memory (gpnvm) bit.",
3478 .handler
= sam3_handle_info_command
,
3479 .mode
= COMMAND_EXEC
,
3480 .help
= "Print information about the current at91sam3 chip"
3481 "and its flash configuration.",
3485 .handler
= sam3_handle_slowclk_command
,
3486 .mode
= COMMAND_EXEC
,
3487 .usage
= "[clock_hz]",
3488 .help
= "Display or set the slowclock frequency "
3489 "(default 32768 Hz).",
3491 COMMAND_REGISTRATION_DONE
3493 static const struct command_registration at91sam3_command_handlers
[] = {
3496 .mode
= COMMAND_ANY
,
3497 .help
= "at91sam3 flash command group",
3499 .chain
= at91sam3_exec_command_handlers
,
3501 COMMAND_REGISTRATION_DONE
3504 struct flash_driver at91sam3_flash
= {
3506 .commands
= at91sam3_command_handlers
,
3507 .flash_bank_command
= sam3_flash_bank_command
,
3508 .erase
= sam3_erase
,
3509 .protect
= sam3_protect
,
3510 .write
= sam3_write
,
3511 .read
= default_flash_read
,
3512 .probe
= sam3_probe
,
3513 .auto_probe
= sam3_auto_probe
,
3514 .erase_check
= sam3_erase_check
,
3515 .protect_check
= sam3_protect_check
,
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)