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 *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE. See the *
17 * GNU General public License for more details. *
19 * You should have received a copy of the GNU General public License *
20 * along with this program; if not, write to the *
21 * Free Software Foundation, Inc., *
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23 ****************************************************************************/
25 /* Some of the the lower level code was based on code supplied by
26 * ATMEL under this copyright. */
28 /* BEGIN ATMEL COPYRIGHT */
29 /* ----------------------------------------------------------------------------
30 * ATMEL Microcontroller Software Support
31 * ----------------------------------------------------------------------------
32 * Copyright (c) 2009, Atmel Corporation
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions are met:
39 * - Redistributions of source code must retain the above copyright notice,
40 * this list of conditions and the disclaimer below.
42 * Atmel's name may not be used to endorse or promote products derived from
43 * this software without specific prior written permission.
45 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
47 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
48 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
51 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
52 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
53 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
54 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 * ----------------------------------------------------------------------------
57 /* END ATMEL COPYRIGHT */
64 #include <helper/time_support.h>
66 #define REG_NAME_WIDTH (12)
68 /* at91sam3u series (has one or two flash banks) */
69 #define FLASH_BANK0_BASE_U 0x00080000
70 #define FLASH_BANK1_BASE_U 0x00100000
72 /* at91sam3s series (has always one flash bank) */
73 #define FLASH_BANK_BASE_S 0x00400000
75 /* at91sam3n series (has always one flash bank) */
76 #define FLASH_BANK_BASE_N 0x00400000
78 #define AT91C_EFC_FCMD_GETD (0x0) /* (EFC) Get Flash Descriptor */
79 #define AT91C_EFC_FCMD_WP (0x1) /* (EFC) Write Page */
80 #define AT91C_EFC_FCMD_WPL (0x2) /* (EFC) Write Page and Lock */
81 #define AT91C_EFC_FCMD_EWP (0x3) /* (EFC) Erase Page and Write Page */
82 #define AT91C_EFC_FCMD_EWPL (0x4) /* (EFC) Erase Page and Write Page
84 #define AT91C_EFC_FCMD_EA (0x5) /* (EFC) Erase All */
85 /* cmd6 is not present int he at91sam3u4/2/1 data sheet table 17-2 */
86 /* #define AT91C_EFC_FCMD_EPL (0x6) // (EFC) Erase plane? */
87 /* cmd7 is not present int he at91sam3u4/2/1 data sheet table 17-2 */
88 /* #define AT91C_EFC_FCMD_EPA (0x7) // (EFC) Erase pages? */
89 #define AT91C_EFC_FCMD_SLB (0x8) /* (EFC) Set Lock Bit */
90 #define AT91C_EFC_FCMD_CLB (0x9) /* (EFC) Clear Lock Bit */
91 #define AT91C_EFC_FCMD_GLB (0xA) /* (EFC) Get Lock Bit */
92 #define AT91C_EFC_FCMD_SFB (0xB) /* (EFC) Set Fuse Bit */
93 #define AT91C_EFC_FCMD_CFB (0xC) /* (EFC) Clear Fuse Bit */
94 #define AT91C_EFC_FCMD_GFB (0xD) /* (EFC) Get Fuse Bit */
95 #define AT91C_EFC_FCMD_STUI (0xE) /* (EFC) Start Read Unique ID */
96 #define AT91C_EFC_FCMD_SPUI (0xF) /* (EFC) Stop Read Unique ID */
98 #define offset_EFC_FMR 0
99 #define offset_EFC_FCR 4
100 #define offset_EFC_FSR 8
101 #define offset_EFC_FRR 12
103 extern struct flash_driver at91sam3_flash
;
105 static float _tomhz(uint32_t freq_hz
)
109 f
= ((float)(freq_hz
)) / 1000000.0;
113 /* How the chip is configured. */
115 uint32_t unique_id
[4];
119 uint32_t mainosc_freq
;
129 #define SAM3_CHIPID_CIDR (0x400E0740)
130 uint32_t CHIPID_CIDR
;
131 #define SAM3_CHIPID_EXID (0x400E0744)
132 uint32_t CHIPID_EXID
;
134 #define SAM3_PMC_BASE (0x400E0400)
135 #define SAM3_PMC_SCSR (SAM3_PMC_BASE + 0x0008)
137 #define SAM3_PMC_PCSR (SAM3_PMC_BASE + 0x0018)
139 #define SAM3_CKGR_UCKR (SAM3_PMC_BASE + 0x001c)
141 #define SAM3_CKGR_MOR (SAM3_PMC_BASE + 0x0020)
143 #define SAM3_CKGR_MCFR (SAM3_PMC_BASE + 0x0024)
145 #define SAM3_CKGR_PLLAR (SAM3_PMC_BASE + 0x0028)
147 #define SAM3_PMC_MCKR (SAM3_PMC_BASE + 0x0030)
149 #define SAM3_PMC_PCK0 (SAM3_PMC_BASE + 0x0040)
151 #define SAM3_PMC_PCK1 (SAM3_PMC_BASE + 0x0044)
153 #define SAM3_PMC_PCK2 (SAM3_PMC_BASE + 0x0048)
155 #define SAM3_PMC_SR (SAM3_PMC_BASE + 0x0068)
157 #define SAM3_PMC_IMR (SAM3_PMC_BASE + 0x006c)
159 #define SAM3_PMC_FSMR (SAM3_PMC_BASE + 0x0070)
161 #define SAM3_PMC_FSPR (SAM3_PMC_BASE + 0x0074)
166 * The AT91SAM3N data sheet 04-Oct-2010, AT91SAM3U data sheet 22-Aug-2011
167 * and AT91SAM3S data sheet 09-Feb-2011 state that for flash writes
168 * the flash wait state (FWS) should be set to 6. It seems like that the
169 * cause of the problem is not the flash itself, but the flash write
170 * buffer. Ie the wait states have to be set before writing into the
172 * Tested and confirmed with SAM3N and SAM3U
175 struct sam3_bank_private
{
177 /* DANGER: THERE ARE DRAGONS HERE.. */
178 /* NOTE: If you add more 'ghost' pointers */
179 /* be aware that you must *manually* update */
180 /* these pointers in the function sam3_GetDetails() */
181 /* See the comment "Here there be dragons" */
183 /* so we can find the chip we belong to */
184 struct sam3_chip
*pChip
;
185 /* so we can find the orginal bank pointer */
186 struct flash_bank
*pBank
;
187 unsigned bank_number
;
188 uint32_t controller_address
;
189 uint32_t base_address
;
190 uint32_t flash_wait_states
;
194 unsigned sector_size
;
198 struct sam3_chip_details
{
199 /* THERE ARE DRAGONS HERE.. */
200 /* note: If you add pointers here */
201 /* becareful about them as they */
202 /* may need to be updated inside */
203 /* the function: "sam3_GetDetails() */
204 /* which copy/overwrites the */
205 /* 'runtime' copy of this structure */
206 uint32_t chipid_cidr
;
210 #define SAM3_N_NVM_BITS 3
211 unsigned gpnvm
[SAM3_N_NVM_BITS
];
212 unsigned total_flash_size
;
213 unsigned total_sram_size
;
215 #define SAM3_MAX_FLASH_BANKS 2
216 /* these are "initialized" from the global const data */
217 struct sam3_bank_private bank
[SAM3_MAX_FLASH_BANKS
];
221 struct sam3_chip
*next
;
224 /* this is "initialized" from the global const structure */
225 struct sam3_chip_details details
;
226 struct target
*target
;
231 struct sam3_reg_list
{
232 uint32_t address
; size_t struct_offset
; const char *name
;
233 void (*explain_func
)(struct sam3_chip
*pInfo
);
236 static struct sam3_chip
*all_sam3_chips
;
238 static struct sam3_chip
*get_current_sam3(struct command_context
*cmd_ctx
)
241 static struct sam3_chip
*p
;
243 t
= get_current_target(cmd_ctx
);
245 command_print(cmd_ctx
, "No current target?");
251 /* this should not happen */
252 /* the command is not registered until the chip is created? */
253 command_print(cmd_ctx
, "No SAM3 chips exist?");
262 command_print(cmd_ctx
, "Cannot find SAM3 chip?");
266 /* these are used to *initialize* the "pChip->details" structure. */
267 static const struct sam3_chip_details all_sam3_details
[] = {
268 /* Start at91sam3u* series */
270 .chipid_cidr
= 0x28100960,
271 .name
= "at91sam3u4e",
272 .total_flash_size
= 256 * 1024,
273 .total_sram_size
= 52 * 1024,
277 /* System boots at address 0x0 */
278 /* gpnvm[1] = selects boot code */
279 /* if gpnvm[1] == 0 */
280 /* boot is via "SAMBA" (rom) */
282 /* boot is via FLASH */
283 /* Selection is via gpnvm[2] */
286 /* NOTE: banks 0 & 1 switch places */
287 /* if gpnvm[2] == 0 */
288 /* Bank0 is the boot rom */
290 /* Bank1 is the boot rom */
299 .base_address
= FLASH_BANK0_BASE_U
,
300 .controller_address
= 0x400e0800,
301 .flash_wait_states
= 6, /* workaround silicon bug */
303 .size_bytes
= 128 * 1024,
315 .base_address
= FLASH_BANK1_BASE_U
,
316 .controller_address
= 0x400e0a00,
317 .flash_wait_states
= 6, /* workaround silicon bug */
319 .size_bytes
= 128 * 1024,
328 .chipid_cidr
= 0x281a0760,
329 .name
= "at91sam3u2e",
330 .total_flash_size
= 128 * 1024,
331 .total_sram_size
= 36 * 1024,
335 /* System boots at address 0x0 */
336 /* gpnvm[1] = selects boot code */
337 /* if gpnvm[1] == 0 */
338 /* boot is via "SAMBA" (rom) */
340 /* boot is via FLASH */
341 /* Selection is via gpnvm[2] */
350 .base_address
= FLASH_BANK0_BASE_U
,
351 .controller_address
= 0x400e0800,
352 .flash_wait_states
= 6, /* workaround silicon bug */
354 .size_bytes
= 128 * 1024,
368 .chipid_cidr
= 0x28190560,
369 .name
= "at91sam3u1e",
370 .total_flash_size
= 64 * 1024,
371 .total_sram_size
= 20 * 1024,
375 /* System boots at address 0x0 */
376 /* gpnvm[1] = selects boot code */
377 /* if gpnvm[1] == 0 */
378 /* boot is via "SAMBA" (rom) */
380 /* boot is via FLASH */
381 /* Selection is via gpnvm[2] */
392 .base_address
= FLASH_BANK0_BASE_U
,
393 .controller_address
= 0x400e0800,
394 .flash_wait_states
= 6, /* workaround silicon bug */
396 .size_bytes
= 64 * 1024,
412 .chipid_cidr
= 0x28000960,
413 .name
= "at91sam3u4c",
414 .total_flash_size
= 256 * 1024,
415 .total_sram_size
= 52 * 1024,
419 /* System boots at address 0x0 */
420 /* gpnvm[1] = selects boot code */
421 /* if gpnvm[1] == 0 */
422 /* boot is via "SAMBA" (rom) */
424 /* boot is via FLASH */
425 /* Selection is via gpnvm[2] */
428 /* NOTE: banks 0 & 1 switch places */
429 /* if gpnvm[2] == 0 */
430 /* Bank0 is the boot rom */
432 /* Bank1 is the boot rom */
441 .base_address
= FLASH_BANK0_BASE_U
,
442 .controller_address
= 0x400e0800,
443 .flash_wait_states
= 6, /* workaround silicon bug */
445 .size_bytes
= 128 * 1024,
456 .base_address
= FLASH_BANK1_BASE_U
,
457 .controller_address
= 0x400e0a00,
458 .flash_wait_states
= 6, /* workaround silicon bug */
460 .size_bytes
= 128 * 1024,
469 .chipid_cidr
= 0x280a0760,
470 .name
= "at91sam3u2c",
471 .total_flash_size
= 128 * 1024,
472 .total_sram_size
= 36 * 1024,
476 /* System boots at address 0x0 */
477 /* gpnvm[1] = selects boot code */
478 /* if gpnvm[1] == 0 */
479 /* boot is via "SAMBA" (rom) */
481 /* boot is via FLASH */
482 /* Selection is via gpnvm[2] */
491 .base_address
= FLASH_BANK0_BASE_U
,
492 .controller_address
= 0x400e0800,
493 .flash_wait_states
= 6, /* workaround silicon bug */
495 .size_bytes
= 128 * 1024,
509 .chipid_cidr
= 0x28090560,
510 .name
= "at91sam3u1c",
511 .total_flash_size
= 64 * 1024,
512 .total_sram_size
= 20 * 1024,
516 /* System boots at address 0x0 */
517 /* gpnvm[1] = selects boot code */
518 /* if gpnvm[1] == 0 */
519 /* boot is via "SAMBA" (rom) */
521 /* boot is via FLASH */
522 /* Selection is via gpnvm[2] */
533 .base_address
= FLASH_BANK0_BASE_U
,
534 .controller_address
= 0x400e0800,
535 .flash_wait_states
= 6, /* workaround silicon bug */
537 .size_bytes
= 64 * 1024,
552 /* Start at91sam3s* series */
554 /* Note: The preliminary at91sam3s datasheet says on page 302 */
555 /* that the flash controller is at address 0x400E0800. */
556 /* This is _not_ the case, the controller resides at address 0x400e0a0. */
558 .chipid_cidr
= 0x28A00960,
559 .name
= "at91sam3s4c",
560 .total_flash_size
= 256 * 1024,
561 .total_sram_size
= 48 * 1024,
571 .base_address
= FLASH_BANK_BASE_S
,
572 .controller_address
= 0x400e0a00,
573 .flash_wait_states
= 6, /* workaround silicon bug */
575 .size_bytes
= 256 * 1024,
577 .sector_size
= 16384,
591 .chipid_cidr
= 0x28900960,
592 .name
= "at91sam3s4b",
593 .total_flash_size
= 256 * 1024,
594 .total_sram_size
= 48 * 1024,
604 .base_address
= FLASH_BANK_BASE_S
,
605 .controller_address
= 0x400e0a00,
606 .flash_wait_states
= 6, /* workaround silicon bug */
608 .size_bytes
= 256 * 1024,
610 .sector_size
= 16384,
623 .chipid_cidr
= 0x28800960,
624 .name
= "at91sam3s4a",
625 .total_flash_size
= 256 * 1024,
626 .total_sram_size
= 48 * 1024,
636 .base_address
= FLASH_BANK_BASE_S
,
637 .controller_address
= 0x400e0a00,
638 .flash_wait_states
= 6, /* workaround silicon bug */
640 .size_bytes
= 256 * 1024,
642 .sector_size
= 16384,
655 .chipid_cidr
= 0x28AA0760,
656 .name
= "at91sam3s2c",
657 .total_flash_size
= 128 * 1024,
658 .total_sram_size
= 32 * 1024,
668 .base_address
= FLASH_BANK_BASE_S
,
669 .controller_address
= 0x400e0a00,
670 .flash_wait_states
= 6, /* workaround silicon bug */
672 .size_bytes
= 128 * 1024,
674 .sector_size
= 16384,
687 .chipid_cidr
= 0x289A0760,
688 .name
= "at91sam3s2b",
689 .total_flash_size
= 128 * 1024,
690 .total_sram_size
= 32 * 1024,
700 .base_address
= FLASH_BANK_BASE_S
,
701 .controller_address
= 0x400e0a00,
702 .flash_wait_states
= 6, /* workaround silicon bug */
704 .size_bytes
= 128 * 1024,
706 .sector_size
= 16384,
719 .chipid_cidr
= 0x288A0760,
720 .name
= "at91sam3s2a",
721 .total_flash_size
= 128 * 1024,
722 .total_sram_size
= 32 * 1024,
732 .base_address
= FLASH_BANK_BASE_S
,
733 .controller_address
= 0x400e0a00,
734 .flash_wait_states
= 6, /* workaround silicon bug */
736 .size_bytes
= 128 * 1024,
738 .sector_size
= 16384,
751 .chipid_cidr
= 0x28A90560,
752 .name
= "at91sam3s1c",
753 .total_flash_size
= 64 * 1024,
754 .total_sram_size
= 16 * 1024,
764 .base_address
= FLASH_BANK_BASE_S
,
765 .controller_address
= 0x400e0a00,
766 .flash_wait_states
= 6, /* workaround silicon bug */
768 .size_bytes
= 64 * 1024,
770 .sector_size
= 16384,
783 .chipid_cidr
= 0x28990560,
784 .name
= "at91sam3s1b",
785 .total_flash_size
= 64 * 1024,
786 .total_sram_size
= 16 * 1024,
796 .base_address
= FLASH_BANK_BASE_S
,
797 .controller_address
= 0x400e0a00,
798 .flash_wait_states
= 6, /* workaround silicon bug */
800 .size_bytes
= 64 * 1024,
802 .sector_size
= 16384,
815 .chipid_cidr
= 0x28890560,
816 .name
= "at91sam3s1a",
817 .total_flash_size
= 64 * 1024,
818 .total_sram_size
= 16 * 1024,
828 .base_address
= FLASH_BANK_BASE_S
,
829 .controller_address
= 0x400e0a00,
830 .flash_wait_states
= 6, /* workaround silicon bug */
832 .size_bytes
= 64 * 1024,
834 .sector_size
= 16384,
847 /* Start at91sam3n* series */
849 .chipid_cidr
= 0x29540960,
850 .name
= "at91sam3n4c",
851 .total_flash_size
= 256 * 1024,
852 .total_sram_size
= 24 * 1024,
856 /* System boots at address 0x0 */
857 /* gpnvm[1] = selects boot code */
858 /* if gpnvm[1] == 0 */
859 /* boot is via "SAMBA" (rom) */
861 /* boot is via FLASH */
862 /* Selection is via gpnvm[2] */
865 /* NOTE: banks 0 & 1 switch places */
866 /* if gpnvm[2] == 0 */
867 /* Bank0 is the boot rom */
869 /* Bank1 is the boot rom */
878 .base_address
= FLASH_BANK_BASE_N
,
879 .controller_address
= 0x400e0A00,
880 .flash_wait_states
= 6, /* workaround silicon bug */
882 .size_bytes
= 256 * 1024,
884 .sector_size
= 16384,
898 .chipid_cidr
= 0x29440960,
899 .name
= "at91sam3n4b",
900 .total_flash_size
= 256 * 1024,
901 .total_sram_size
= 24 * 1024,
905 /* System boots at address 0x0 */
906 /* gpnvm[1] = selects boot code */
907 /* if gpnvm[1] == 0 */
908 /* boot is via "SAMBA" (rom) */
910 /* boot is via FLASH */
911 /* Selection is via gpnvm[2] */
914 /* NOTE: banks 0 & 1 switch places */
915 /* if gpnvm[2] == 0 */
916 /* Bank0 is the boot rom */
918 /* Bank1 is the boot rom */
927 .base_address
= FLASH_BANK_BASE_N
,
928 .controller_address
= 0x400e0A00,
929 .flash_wait_states
= 6, /* workaround silicon bug */
931 .size_bytes
= 256 * 1024,
933 .sector_size
= 16384,
947 .chipid_cidr
= 0x29340960,
948 .name
= "at91sam3n4a",
949 .total_flash_size
= 256 * 1024,
950 .total_sram_size
= 24 * 1024,
954 /* System boots at address 0x0 */
955 /* gpnvm[1] = selects boot code */
956 /* if gpnvm[1] == 0 */
957 /* boot is via "SAMBA" (rom) */
959 /* boot is via FLASH */
960 /* Selection is via gpnvm[2] */
963 /* NOTE: banks 0 & 1 switch places */
964 /* if gpnvm[2] == 0 */
965 /* Bank0 is the boot rom */
967 /* Bank1 is the boot rom */
976 .base_address
= FLASH_BANK_BASE_N
,
977 .controller_address
= 0x400e0A00,
978 .flash_wait_states
= 6, /* workaround silicon bug */
980 .size_bytes
= 256 * 1024,
982 .sector_size
= 16384,
996 .chipid_cidr
= 0x29590760,
997 .name
= "at91sam3n2c",
998 .total_flash_size
= 128 * 1024,
999 .total_sram_size
= 16 * 1024,
1003 /* System boots at address 0x0 */
1004 /* gpnvm[1] = selects boot code */
1005 /* if gpnvm[1] == 0 */
1006 /* boot is via "SAMBA" (rom) */
1008 /* boot is via FLASH */
1009 /* Selection is via gpnvm[2] */
1012 /* NOTE: banks 0 & 1 switch places */
1013 /* if gpnvm[2] == 0 */
1014 /* Bank0 is the boot rom */
1016 /* Bank1 is the boot rom */
1025 .base_address
= FLASH_BANK_BASE_N
,
1026 .controller_address
= 0x400e0A00,
1027 .flash_wait_states
= 6, /* workaround silicon bug */
1029 .size_bytes
= 128 * 1024,
1031 .sector_size
= 16384,
1045 .chipid_cidr
= 0x29490760,
1046 .name
= "at91sam3n2b",
1047 .total_flash_size
= 128 * 1024,
1048 .total_sram_size
= 16 * 1024,
1052 /* System boots at address 0x0 */
1053 /* gpnvm[1] = selects boot code */
1054 /* if gpnvm[1] == 0 */
1055 /* boot is via "SAMBA" (rom) */
1057 /* boot is via FLASH */
1058 /* Selection is via gpnvm[2] */
1061 /* NOTE: banks 0 & 1 switch places */
1062 /* if gpnvm[2] == 0 */
1063 /* Bank0 is the boot rom */
1065 /* Bank1 is the boot rom */
1074 .base_address
= FLASH_BANK_BASE_N
,
1075 .controller_address
= 0x400e0A00,
1076 .flash_wait_states
= 6, /* workaround silicon bug */
1078 .size_bytes
= 128 * 1024,
1080 .sector_size
= 16384,
1094 .chipid_cidr
= 0x29390760,
1095 .name
= "at91sam3n2a",
1096 .total_flash_size
= 128 * 1024,
1097 .total_sram_size
= 16 * 1024,
1101 /* System boots at address 0x0 */
1102 /* gpnvm[1] = selects boot code */
1103 /* if gpnvm[1] == 0 */
1104 /* boot is via "SAMBA" (rom) */
1106 /* boot is via FLASH */
1107 /* Selection is via gpnvm[2] */
1110 /* NOTE: banks 0 & 1 switch places */
1111 /* if gpnvm[2] == 0 */
1112 /* Bank0 is the boot rom */
1114 /* Bank1 is the boot rom */
1123 .base_address
= FLASH_BANK_BASE_N
,
1124 .controller_address
= 0x400e0A00,
1125 .flash_wait_states
= 6, /* workaround silicon bug */
1127 .size_bytes
= 128 * 1024,
1129 .sector_size
= 16384,
1143 .chipid_cidr
= 0x29580560,
1144 .name
= "at91sam3n1c",
1145 .total_flash_size
= 64 * 1024,
1146 .total_sram_size
= 8 * 1024,
1150 /* System boots at address 0x0 */
1151 /* gpnvm[1] = selects boot code */
1152 /* if gpnvm[1] == 0 */
1153 /* boot is via "SAMBA" (rom) */
1155 /* boot is via FLASH */
1156 /* Selection is via gpnvm[2] */
1159 /* NOTE: banks 0 & 1 switch places */
1160 /* if gpnvm[2] == 0 */
1161 /* Bank0 is the boot rom */
1163 /* Bank1 is the boot rom */
1172 .base_address
= FLASH_BANK_BASE_N
,
1173 .controller_address
= 0x400e0A00,
1174 .flash_wait_states
= 6, /* workaround silicon bug */
1176 .size_bytes
= 64 * 1024,
1178 .sector_size
= 16384,
1192 .chipid_cidr
= 0x29480560,
1193 .name
= "at91sam3n1b",
1194 .total_flash_size
= 64 * 1024,
1195 .total_sram_size
= 8 * 1024,
1199 /* System boots at address 0x0 */
1200 /* gpnvm[1] = selects boot code */
1201 /* if gpnvm[1] == 0 */
1202 /* boot is via "SAMBA" (rom) */
1204 /* boot is via FLASH */
1205 /* Selection is via gpnvm[2] */
1208 /* NOTE: banks 0 & 1 switch places */
1209 /* if gpnvm[2] == 0 */
1210 /* Bank0 is the boot rom */
1212 /* Bank1 is the boot rom */
1221 .base_address
= FLASH_BANK_BASE_N
,
1222 .controller_address
= 0x400e0A00,
1223 .flash_wait_states
= 6, /* workaround silicon bug */
1225 .size_bytes
= 64 * 1024,
1227 .sector_size
= 16384,
1241 .chipid_cidr
= 0x29380560,
1242 .name
= "at91sam3n1a",
1243 .total_flash_size
= 64 * 1024,
1244 .total_sram_size
= 8 * 1024,
1248 /* System boots at address 0x0 */
1249 /* gpnvm[1] = selects boot code */
1250 /* if gpnvm[1] == 0 */
1251 /* boot is via "SAMBA" (rom) */
1253 /* boot is via FLASH */
1254 /* Selection is via gpnvm[2] */
1257 /* NOTE: banks 0 & 1 switch places */
1258 /* if gpnvm[2] == 0 */
1259 /* Bank0 is the boot rom */
1261 /* Bank1 is the boot rom */
1270 .base_address
= FLASH_BANK_BASE_N
,
1271 .controller_address
= 0x400e0A00,
1272 .flash_wait_states
= 6, /* workaround silicon bug */
1274 .size_bytes
= 64 * 1024,
1276 .sector_size
= 16384,
1297 /***********************************************************************
1298 **********************************************************************
1299 **********************************************************************
1300 **********************************************************************
1301 **********************************************************************
1302 **********************************************************************/
1303 /* *ATMEL* style code - from the SAM3 driver code */
1306 * Get the current status of the EEFC and
1307 * the value of some status bits (LOCKE, PROGE).
1308 * @param pPrivate - info about the bank
1309 * @param v - result goes here
1311 static int EFC_GetStatus(struct sam3_bank_private
*pPrivate
, uint32_t *v
)
1314 r
= target_read_u32(pPrivate
->pChip
->target
,
1315 pPrivate
->controller_address
+ offset_EFC_FSR
,
1317 LOG_DEBUG("Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
1319 ((unsigned int)((*v
>> 2) & 1)),
1320 ((unsigned int)((*v
>> 1) & 1)),
1321 ((unsigned int)((*v
>> 0) & 1)));
1327 * Get the result of the last executed command.
1328 * @param pPrivate - info about the bank
1329 * @param v - result goes here
1331 static int EFC_GetResult(struct sam3_bank_private
*pPrivate
, uint32_t *v
)
1335 r
= target_read_u32(pPrivate
->pChip
->target
,
1336 pPrivate
->controller_address
+ offset_EFC_FRR
,
1340 LOG_DEBUG("Result: 0x%08x", ((unsigned int)(rv
)));
1344 static int EFC_StartCommand(struct sam3_bank_private
*pPrivate
,
1345 unsigned command
, unsigned argument
)
1354 /* Check command & argument */
1357 case AT91C_EFC_FCMD_WP
:
1358 case AT91C_EFC_FCMD_WPL
:
1359 case AT91C_EFC_FCMD_EWP
:
1360 case AT91C_EFC_FCMD_EWPL
:
1361 /* case AT91C_EFC_FCMD_EPL: */
1362 /* case AT91C_EFC_FCMD_EPA: */
1363 case AT91C_EFC_FCMD_SLB
:
1364 case AT91C_EFC_FCMD_CLB
:
1365 n
= (pPrivate
->size_bytes
/ pPrivate
->page_size
);
1367 LOG_ERROR("*BUG*: Embedded flash has only %u pages", (unsigned)(n
));
1370 case AT91C_EFC_FCMD_SFB
:
1371 case AT91C_EFC_FCMD_CFB
:
1372 if (argument
>= pPrivate
->pChip
->details
.n_gpnvms
) {
1373 LOG_ERROR("*BUG*: Embedded flash has only %d GPNVMs",
1374 pPrivate
->pChip
->details
.n_gpnvms
);
1378 case AT91C_EFC_FCMD_GETD
:
1379 case AT91C_EFC_FCMD_EA
:
1380 case AT91C_EFC_FCMD_GLB
:
1381 case AT91C_EFC_FCMD_GFB
:
1382 case AT91C_EFC_FCMD_STUI
:
1383 case AT91C_EFC_FCMD_SPUI
:
1385 LOG_ERROR("Argument is meaningless for cmd: %d", command
);
1388 LOG_ERROR("Unknown command %d", command
);
1392 if (command
== AT91C_EFC_FCMD_SPUI
) {
1393 /* this is a very special situation. */
1394 /* Situation (1) - error/retry - see below */
1395 /* And we are being called recursively */
1396 /* Situation (2) - normal, finished reading unique id */
1398 /* it should be "ready" */
1399 EFC_GetStatus(pPrivate
, &v
);
1401 /* then it is ready */
1405 /* we have done this before */
1406 /* the controller is not responding. */
1407 LOG_ERROR("flash controller(%d) is not ready! Error",
1408 pPrivate
->bank_number
);
1412 LOG_ERROR("Flash controller(%d) is not ready, attempting reset",
1413 pPrivate
->bank_number
);
1414 /* we do that by issuing the *STOP* command */
1415 EFC_StartCommand(pPrivate
, AT91C_EFC_FCMD_SPUI
, 0);
1416 /* above is recursive, and further recursion is blocked by */
1417 /* if (command == AT91C_EFC_FCMD_SPUI) above */
1423 v
= (0x5A << 24) | (argument
<< 8) | command
;
1424 LOG_DEBUG("Command: 0x%08x", ((unsigned int)(v
)));
1425 r
= target_write_u32(pPrivate
->pBank
->target
,
1426 pPrivate
->controller_address
+ offset_EFC_FCR
, v
);
1428 LOG_DEBUG("Error Write failed");
1433 * Performs the given command and wait until its completion (or an error).
1434 * @param pPrivate - info about the bank
1435 * @param command - Command to perform.
1436 * @param argument - Optional command argument.
1437 * @param status - put command status bits here
1439 static int EFC_PerformCommand(struct sam3_bank_private
*pPrivate
,
1447 long long ms_now
, ms_end
;
1453 r
= EFC_StartCommand(pPrivate
, command
, argument
);
1457 ms_end
= 500 + timeval_ms();
1460 r
= EFC_GetStatus(pPrivate
, &v
);
1463 ms_now
= timeval_ms();
1464 if (ms_now
> ms_end
) {
1466 LOG_ERROR("Command timeout");
1469 } while ((v
& 1) == 0);
1473 *status
= (v
& 0x6);
1479 * Read the unique ID.
1480 * @param pPrivate - info about the bank
1481 * The unique ID is stored in the 'pPrivate' structure.
1483 static int FLASHD_ReadUniqueID(struct sam3_bank_private
*pPrivate
)
1489 pPrivate
->pChip
->cfg
.unique_id
[0] = 0;
1490 pPrivate
->pChip
->cfg
.unique_id
[1] = 0;
1491 pPrivate
->pChip
->cfg
.unique_id
[2] = 0;
1492 pPrivate
->pChip
->cfg
.unique_id
[3] = 0;
1495 r
= EFC_StartCommand(pPrivate
, AT91C_EFC_FCMD_STUI
, 0);
1499 for (x
= 0; x
< 4; x
++) {
1500 r
= target_read_u32(pPrivate
->pChip
->target
,
1501 pPrivate
->pBank
->base
+ (x
* 4),
1505 pPrivate
->pChip
->cfg
.unique_id
[x
] = v
;
1508 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_SPUI
, 0, NULL
);
1509 LOG_DEBUG("End: R=%d, id = 0x%08x, 0x%08x, 0x%08x, 0x%08x",
1511 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[0]),
1512 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[1]),
1513 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[2]),
1514 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[3]));
1520 * Erases the entire flash.
1521 * @param pPrivate - the info about the bank.
1523 static int FLASHD_EraseEntireBank(struct sam3_bank_private
*pPrivate
)
1526 return EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_EA
, 0, NULL
);
1530 * Gets current GPNVM state.
1531 * @param pPrivate - info about the bank.
1532 * @param gpnvm - GPNVM bit index.
1533 * @param puthere - result stored here.
1535 /* ------------------------------------------------------------------------------ */
1536 static int FLASHD_GetGPNVM(struct sam3_bank_private
*pPrivate
, unsigned gpnvm
, unsigned *puthere
)
1542 if (pPrivate
->bank_number
!= 0) {
1543 LOG_ERROR("GPNVM only works with Bank0");
1547 if (gpnvm
>= pPrivate
->pChip
->details
.n_gpnvms
) {
1548 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1549 gpnvm
, pPrivate
->pChip
->details
.n_gpnvms
);
1553 /* Get GPNVMs status */
1554 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_GFB
, 0, NULL
);
1555 if (r
!= ERROR_OK
) {
1556 LOG_ERROR("Failed");
1560 r
= EFC_GetResult(pPrivate
, &v
);
1563 /* Check if GPNVM is set */
1564 /* get the bit and make it a 0/1 */
1565 *puthere
= (v
>> gpnvm
) & 1;
1572 * Clears the selected GPNVM bit.
1573 * @param pPrivate info about the bank
1574 * @param gpnvm GPNVM index.
1575 * @returns 0 if successful; otherwise returns an error code.
1577 static int FLASHD_ClrGPNVM(struct sam3_bank_private
*pPrivate
, unsigned gpnvm
)
1583 if (pPrivate
->bank_number
!= 0) {
1584 LOG_ERROR("GPNVM only works with Bank0");
1588 if (gpnvm
>= pPrivate
->pChip
->details
.n_gpnvms
) {
1589 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1590 gpnvm
, pPrivate
->pChip
->details
.n_gpnvms
);
1594 r
= FLASHD_GetGPNVM(pPrivate
, gpnvm
, &v
);
1595 if (r
!= ERROR_OK
) {
1596 LOG_DEBUG("Failed: %d", r
);
1599 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_CFB
, gpnvm
, NULL
);
1600 LOG_DEBUG("End: %d", r
);
1605 * Sets the selected GPNVM bit.
1606 * @param pPrivate info about the bank
1607 * @param gpnvm GPNVM index.
1609 static int FLASHD_SetGPNVM(struct sam3_bank_private
*pPrivate
, unsigned gpnvm
)
1614 if (pPrivate
->bank_number
!= 0) {
1615 LOG_ERROR("GPNVM only works with Bank0");
1619 if (gpnvm
>= pPrivate
->pChip
->details
.n_gpnvms
) {
1620 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1621 gpnvm
, pPrivate
->pChip
->details
.n_gpnvms
);
1625 r
= FLASHD_GetGPNVM(pPrivate
, gpnvm
, &v
);
1633 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_SFB
, gpnvm
, NULL
);
1639 * Returns a bit field (at most 64) of locked regions within a page.
1640 * @param pPrivate info about the bank
1641 * @param v where to store locked bits
1643 static int FLASHD_GetLockBits(struct sam3_bank_private
*pPrivate
, uint32_t *v
)
1647 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_GLB
, 0, NULL
);
1649 r
= EFC_GetResult(pPrivate
, v
);
1650 LOG_DEBUG("End: %d", r
);
1655 * Unlocks all the regions in the given address range.
1656 * @param pPrivate info about the bank
1657 * @param start_sector first sector to unlock
1658 * @param end_sector last (inclusive) to unlock
1661 static int FLASHD_Unlock(struct sam3_bank_private
*pPrivate
,
1662 unsigned start_sector
,
1663 unsigned end_sector
)
1668 uint32_t pages_per_sector
;
1670 pages_per_sector
= pPrivate
->sector_size
/ pPrivate
->page_size
;
1672 /* Unlock all pages */
1673 while (start_sector
<= end_sector
) {
1674 pg
= start_sector
* pages_per_sector
;
1676 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_CLB
, pg
, &status
);
1687 * @param pPrivate - info about the bank
1688 * @param start_sector - first sector to lock
1689 * @param end_sector - last sector (inclusive) to lock
1691 static int FLASHD_Lock(struct sam3_bank_private
*pPrivate
,
1692 unsigned start_sector
,
1693 unsigned end_sector
)
1697 uint32_t pages_per_sector
;
1700 pages_per_sector
= pPrivate
->sector_size
/ pPrivate
->page_size
;
1702 /* Lock all pages */
1703 while (start_sector
<= end_sector
) {
1704 pg
= start_sector
* pages_per_sector
;
1706 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_SLB
, pg
, &status
);
1714 /****** END SAM3 CODE ********/
1716 /* begin helpful debug code */
1717 /* print the fieldname, the field value, in dec & hex, and return field value */
1718 static uint32_t sam3_reg_fieldname(struct sam3_chip
*pChip
,
1719 const char *regname
,
1728 /* extract the field */
1730 v
= v
& ((1 << width
)-1);
1739 /* show the basics */
1740 LOG_USER_N("\t%*s: %*d [0x%0*x] ",
1741 REG_NAME_WIDTH
, regname
,
1747 static const char _unknown
[] = "unknown";
1748 static const char *const eproc_names
[] = {
1752 "cortex-m3", /* 3 */
1754 "arm926ejs", /* 5 */
1767 #define nvpsize2 nvpsize /* these two tables are identical */
1768 static const char *const nvpsize
[] = {
1771 "16K bytes", /* 2 */
1772 "32K bytes", /* 3 */
1774 "64K bytes", /* 5 */
1776 "128K bytes", /* 7 */
1778 "256K bytes", /* 9 */
1779 "512K bytes", /* 10 */
1781 "1024K bytes", /* 12 */
1783 "2048K bytes", /* 14 */
1787 static const char *const sramsize
[] = {
1788 "48K Bytes", /* 0 */
1792 "112K Bytes", /* 4 */
1794 "80K Bytes", /* 6 */
1795 "160K Bytes", /* 7 */
1797 "16K Bytes", /* 9 */
1798 "32K Bytes", /* 10 */
1799 "64K Bytes", /* 11 */
1800 "128K Bytes", /* 12 */
1801 "256K Bytes", /* 13 */
1802 "96K Bytes", /* 14 */
1803 "512K Bytes", /* 15 */
1807 static const struct archnames
{ unsigned value
; const char *name
; } archnames
[] = {
1808 { 0x19, "AT91SAM9xx Series" },
1809 { 0x29, "AT91SAM9XExx Series" },
1810 { 0x34, "AT91x34 Series" },
1811 { 0x37, "CAP7 Series" },
1812 { 0x39, "CAP9 Series" },
1813 { 0x3B, "CAP11 Series" },
1814 { 0x40, "AT91x40 Series" },
1815 { 0x42, "AT91x42 Series" },
1816 { 0x55, "AT91x55 Series" },
1817 { 0x60, "AT91SAM7Axx Series" },
1818 { 0x61, "AT91SAM7AQxx Series" },
1819 { 0x63, "AT91x63 Series" },
1820 { 0x70, "AT91SAM7Sxx Series" },
1821 { 0x71, "AT91SAM7XCxx Series" },
1822 { 0x72, "AT91SAM7SExx Series" },
1823 { 0x73, "AT91SAM7Lxx Series" },
1824 { 0x75, "AT91SAM7Xxx Series" },
1825 { 0x76, "AT91SAM7SLxx Series" },
1826 { 0x80, "ATSAM3UxC Series (100-pin version)" },
1827 { 0x81, "ATSAM3UxE Series (144-pin version)" },
1828 { 0x83, "ATSAM3AxC Series (100-pin version)" },
1829 { 0x84, "ATSAM3XxC Series (100-pin version)" },
1830 { 0x85, "ATSAM3XxE Series (144-pin version)" },
1831 { 0x86, "ATSAM3XxG Series (208/217-pin version)" },
1832 { 0x88, "ATSAM3SxA Series (48-pin version)" },
1833 { 0x89, "ATSAM3SxB Series (64-pin version)" },
1834 { 0x8A, "ATSAM3SxC Series (100-pin version)" },
1835 { 0x92, "AT91x92 Series" },
1836 { 0x93, "ATSAM3NxA Series (48-pin version)" },
1837 { 0x94, "ATSAM3NxB Series (64-pin version)" },
1838 { 0x95, "ATSAM3NxC Series (100-pin version)" },
1839 { 0x98, "ATSAM3SDxA Series (48-pin version)" },
1840 { 0x99, "ATSAM3SDxB Series (64-pin version)" },
1841 { 0x9A, "ATSAM3SDxC Series (100-pin version)" },
1842 { 0xA5, "ATSAM5A" },
1843 { 0xF0, "AT75Cxx Series" },
1847 static const char *const nvptype
[] = {
1849 "romless or onchip flash", /* 1 */
1850 "embedded flash memory",/* 2 */
1851 "rom(nvpsiz) + embedded flash (nvpsiz2)", /* 3 */
1852 "sram emulating flash", /* 4 */
1858 static const char *_yes_or_no(uint32_t v
)
1866 static const char *const _rc_freq
[] = {
1867 "4 MHz", "8 MHz", "12 MHz", "reserved"
1870 static void sam3_explain_ckgr_mor(struct sam3_chip
*pChip
)
1875 v
= sam3_reg_fieldname(pChip
, "MOSCXTEN", pChip
->cfg
.CKGR_MOR
, 0, 1);
1876 LOG_USER("(main xtal enabled: %s)", _yes_or_no(v
));
1877 v
= sam3_reg_fieldname(pChip
, "MOSCXTBY", pChip
->cfg
.CKGR_MOR
, 1, 1);
1878 LOG_USER("(main osc bypass: %s)", _yes_or_no(v
));
1879 rcen
= sam3_reg_fieldname(pChip
, "MOSCRCEN", pChip
->cfg
.CKGR_MOR
, 3, 1);
1880 LOG_USER("(onchip RC-OSC enabled: %s)", _yes_or_no(rcen
));
1881 v
= sam3_reg_fieldname(pChip
, "MOSCRCF", pChip
->cfg
.CKGR_MOR
, 4, 3);
1882 LOG_USER("(onchip RC-OSC freq: %s)", _rc_freq
[v
]);
1884 pChip
->cfg
.rc_freq
= 0;
1888 pChip
->cfg
.rc_freq
= 0;
1891 pChip
->cfg
.rc_freq
= 4 * 1000 * 1000;
1894 pChip
->cfg
.rc_freq
= 8 * 1000 * 1000;
1897 pChip
->cfg
.rc_freq
= 12 * 1000 * 1000;
1902 v
= sam3_reg_fieldname(pChip
, "MOSCXTST", pChip
->cfg
.CKGR_MOR
, 8, 8);
1903 LOG_USER("(startup clks, time= %f uSecs)",
1904 ((float)(v
* 1000000)) / ((float)(pChip
->cfg
.slow_freq
)));
1905 v
= sam3_reg_fieldname(pChip
, "MOSCSEL", pChip
->cfg
.CKGR_MOR
, 24, 1);
1906 LOG_USER("(mainosc source: %s)",
1907 v
? "external xtal" : "internal RC");
1909 v
= sam3_reg_fieldname(pChip
, "CFDEN", pChip
->cfg
.CKGR_MOR
, 25, 1);
1910 LOG_USER("(clock failure enabled: %s)",
1914 static void sam3_explain_chipid_cidr(struct sam3_chip
*pChip
)
1920 sam3_reg_fieldname(pChip
, "Version", pChip
->cfg
.CHIPID_CIDR
, 0, 5);
1923 v
= sam3_reg_fieldname(pChip
, "EPROC", pChip
->cfg
.CHIPID_CIDR
, 5, 3);
1924 LOG_USER("%s", eproc_names
[v
]);
1926 v
= sam3_reg_fieldname(pChip
, "NVPSIZE", pChip
->cfg
.CHIPID_CIDR
, 8, 4);
1927 LOG_USER("%s", nvpsize
[v
]);
1929 v
= sam3_reg_fieldname(pChip
, "NVPSIZE2", pChip
->cfg
.CHIPID_CIDR
, 12, 4);
1930 LOG_USER("%s", nvpsize2
[v
]);
1932 v
= sam3_reg_fieldname(pChip
, "SRAMSIZE", pChip
->cfg
.CHIPID_CIDR
, 16, 4);
1933 LOG_USER("%s", sramsize
[v
]);
1935 v
= sam3_reg_fieldname(pChip
, "ARCH", pChip
->cfg
.CHIPID_CIDR
, 20, 8);
1937 for (x
= 0; archnames
[x
].name
; x
++) {
1938 if (v
== archnames
[x
].value
) {
1939 cp
= archnames
[x
].name
;
1946 v
= sam3_reg_fieldname(pChip
, "NVPTYP", pChip
->cfg
.CHIPID_CIDR
, 28, 3);
1947 LOG_USER("%s", nvptype
[v
]);
1949 v
= sam3_reg_fieldname(pChip
, "EXTID", pChip
->cfg
.CHIPID_CIDR
, 31, 1);
1950 LOG_USER("(exists: %s)", _yes_or_no(v
));
1953 static void sam3_explain_ckgr_mcfr(struct sam3_chip
*pChip
)
1957 v
= sam3_reg_fieldname(pChip
, "MAINFRDY", pChip
->cfg
.CKGR_MCFR
, 16, 1);
1958 LOG_USER("(main ready: %s)", _yes_or_no(v
));
1960 v
= sam3_reg_fieldname(pChip
, "MAINF", pChip
->cfg
.CKGR_MCFR
, 0, 16);
1962 v
= (v
* pChip
->cfg
.slow_freq
) / 16;
1963 pChip
->cfg
.mainosc_freq
= v
;
1965 LOG_USER("(%3.03f Mhz (%d.%03dkhz slowclk)",
1967 pChip
->cfg
.slow_freq
/ 1000,
1968 pChip
->cfg
.slow_freq
% 1000);
1971 static void sam3_explain_ckgr_plla(struct sam3_chip
*pChip
)
1973 uint32_t mula
, diva
;
1975 diva
= sam3_reg_fieldname(pChip
, "DIVA", pChip
->cfg
.CKGR_PLLAR
, 0, 8);
1977 mula
= sam3_reg_fieldname(pChip
, "MULA", pChip
->cfg
.CKGR_PLLAR
, 16, 11);
1979 pChip
->cfg
.plla_freq
= 0;
1981 LOG_USER("\tPLLA Freq: (Disabled,mula = 0)");
1983 LOG_USER("\tPLLA Freq: (Disabled,diva = 0)");
1984 else if (diva
== 1) {
1985 pChip
->cfg
.plla_freq
= (pChip
->cfg
.mainosc_freq
* (mula
+ 1));
1986 LOG_USER("\tPLLA Freq: %3.03f MHz",
1987 _tomhz(pChip
->cfg
.plla_freq
));
1991 static void sam3_explain_mckr(struct sam3_chip
*pChip
)
1993 uint32_t css
, pres
, fin
= 0;
1995 const char *cp
= NULL
;
1997 css
= sam3_reg_fieldname(pChip
, "CSS", pChip
->cfg
.PMC_MCKR
, 0, 2);
2000 fin
= pChip
->cfg
.slow_freq
;
2004 fin
= pChip
->cfg
.mainosc_freq
;
2008 fin
= pChip
->cfg
.plla_freq
;
2012 if (pChip
->cfg
.CKGR_UCKR
& (1 << 16)) {
2013 fin
= 480 * 1000 * 1000;
2017 cp
= "upll (*ERROR* UPLL is disabled)";
2025 LOG_USER("%s (%3.03f Mhz)",
2028 pres
= sam3_reg_fieldname(pChip
, "PRES", pChip
->cfg
.PMC_MCKR
, 4, 3);
2029 switch (pres
& 0x07) {
2032 cp
= "selected clock";
2066 LOG_USER("(%s)", cp
);
2068 /* sam3 has a *SINGLE* clock - */
2069 /* other at91 series parts have divisors for these. */
2070 pChip
->cfg
.cpu_freq
= fin
;
2071 pChip
->cfg
.mclk_freq
= fin
;
2072 pChip
->cfg
.fclk_freq
= fin
;
2073 LOG_USER("\t\tResult CPU Freq: %3.03f",
2078 static struct sam3_chip
*target2sam3(struct target
*pTarget
)
2080 struct sam3_chip
*pChip
;
2082 if (pTarget
== NULL
)
2085 pChip
= all_sam3_chips
;
2087 if (pChip
->target
== pTarget
)
2088 break; /* return below */
2090 pChip
= pChip
->next
;
2096 static uint32_t *sam3_get_reg_ptr(struct sam3_cfg
*pCfg
, const struct sam3_reg_list
*pList
)
2098 /* this function exists to help */
2099 /* keep funky offsetof() errors */
2100 /* and casting from causing bugs */
2102 /* By using prototypes - we can detect what would */
2103 /* be casting errors. */
2105 return (uint32_t *)(void *)(((char *)(pCfg
)) + pList
->struct_offset
);
2109 #define SAM3_ENTRY(NAME, FUNC) { .address = SAM3_ ## NAME, .struct_offset = offsetof( \
2111 NAME), # NAME, FUNC }
2112 static const struct sam3_reg_list sam3_all_regs
[] = {
2113 SAM3_ENTRY(CKGR_MOR
, sam3_explain_ckgr_mor
),
2114 SAM3_ENTRY(CKGR_MCFR
, sam3_explain_ckgr_mcfr
),
2115 SAM3_ENTRY(CKGR_PLLAR
, sam3_explain_ckgr_plla
),
2116 SAM3_ENTRY(CKGR_UCKR
, NULL
),
2117 SAM3_ENTRY(PMC_FSMR
, NULL
),
2118 SAM3_ENTRY(PMC_FSPR
, NULL
),
2119 SAM3_ENTRY(PMC_IMR
, NULL
),
2120 SAM3_ENTRY(PMC_MCKR
, sam3_explain_mckr
),
2121 SAM3_ENTRY(PMC_PCK0
, NULL
),
2122 SAM3_ENTRY(PMC_PCK1
, NULL
),
2123 SAM3_ENTRY(PMC_PCK2
, NULL
),
2124 SAM3_ENTRY(PMC_PCSR
, NULL
),
2125 SAM3_ENTRY(PMC_SCSR
, NULL
),
2126 SAM3_ENTRY(PMC_SR
, NULL
),
2127 SAM3_ENTRY(CHIPID_CIDR
, sam3_explain_chipid_cidr
),
2128 SAM3_ENTRY(CHIPID_EXID
, NULL
),
2130 /* TERMINATE THE LIST */
2135 static struct sam3_bank_private
*get_sam3_bank_private(struct flash_bank
*bank
)
2137 return (struct sam3_bank_private
*)(bank
->driver_priv
);
2141 * Given a pointer to where it goes in the structure,
2142 * determine the register name, address from the all registers table.
2144 static const struct sam3_reg_list
*sam3_GetReg(struct sam3_chip
*pChip
, uint32_t *goes_here
)
2146 const struct sam3_reg_list
*pReg
;
2148 pReg
= &(sam3_all_regs
[0]);
2149 while (pReg
->name
) {
2150 uint32_t *pPossible
;
2152 /* calculate where this one go.. */
2153 /* it is "possibly" this register. */
2155 pPossible
= ((uint32_t *)(void *)(((char *)(&(pChip
->cfg
))) + pReg
->struct_offset
));
2157 /* well? Is it this register */
2158 if (pPossible
== goes_here
) {
2166 /* This is *TOTAL*PANIC* - we are totally screwed. */
2167 LOG_ERROR("INVALID SAM3 REGISTER");
2171 static int sam3_ReadThisReg(struct sam3_chip
*pChip
, uint32_t *goes_here
)
2173 const struct sam3_reg_list
*pReg
;
2176 pReg
= sam3_GetReg(pChip
, goes_here
);
2180 r
= target_read_u32(pChip
->target
, pReg
->address
, goes_here
);
2181 if (r
!= ERROR_OK
) {
2182 LOG_ERROR("Cannot read SAM3 register: %s @ 0x%08x, Err: %d",
2183 pReg
->name
, (unsigned)(pReg
->address
), r
);
2188 static int sam3_ReadAllRegs(struct sam3_chip
*pChip
)
2191 const struct sam3_reg_list
*pReg
;
2193 pReg
= &(sam3_all_regs
[0]);
2194 while (pReg
->name
) {
2195 r
= sam3_ReadThisReg(pChip
,
2196 sam3_get_reg_ptr(&(pChip
->cfg
), pReg
));
2197 if (r
!= ERROR_OK
) {
2198 LOG_ERROR("Cannot read SAM3 registere: %s @ 0x%08x, Error: %d",
2199 pReg
->name
, ((unsigned)(pReg
->address
)), r
);
2209 static int sam3_GetInfo(struct sam3_chip
*pChip
)
2211 const struct sam3_reg_list
*pReg
;
2214 pReg
= &(sam3_all_regs
[0]);
2215 while (pReg
->name
) {
2216 /* display all regs */
2217 LOG_DEBUG("Start: %s", pReg
->name
);
2218 regval
= *sam3_get_reg_ptr(&(pChip
->cfg
), pReg
);
2219 LOG_USER("%*s: [0x%08x] -> 0x%08x",
2224 if (pReg
->explain_func
)
2225 (*(pReg
->explain_func
))(pChip
);
2226 LOG_DEBUG("End: %s", pReg
->name
);
2229 LOG_USER(" rc-osc: %3.03f MHz", _tomhz(pChip
->cfg
.rc_freq
));
2230 LOG_USER(" mainosc: %3.03f MHz", _tomhz(pChip
->cfg
.mainosc_freq
));
2231 LOG_USER(" plla: %3.03f MHz", _tomhz(pChip
->cfg
.plla_freq
));
2232 LOG_USER(" cpu-freq: %3.03f MHz", _tomhz(pChip
->cfg
.cpu_freq
));
2233 LOG_USER("mclk-freq: %3.03f MHz", _tomhz(pChip
->cfg
.mclk_freq
));
2235 LOG_USER(" UniqueId: 0x%08x 0x%08x 0x%08x 0x%08x",
2236 pChip
->cfg
.unique_id
[0],
2237 pChip
->cfg
.unique_id
[1],
2238 pChip
->cfg
.unique_id
[2],
2239 pChip
->cfg
.unique_id
[3]);
2244 static int sam3_erase_check(struct flash_bank
*bank
)
2249 if (bank
->target
->state
!= TARGET_HALTED
) {
2250 LOG_ERROR("Target not halted");
2251 return ERROR_TARGET_NOT_HALTED
;
2253 if (0 == bank
->num_sectors
) {
2254 LOG_ERROR("Target: not supported/not probed");
2258 LOG_INFO("sam3 - supports auto-erase, erase_check ignored");
2259 for (x
= 0; x
< bank
->num_sectors
; x
++)
2260 bank
->sectors
[x
].is_erased
= 1;
2266 static int sam3_protect_check(struct flash_bank
*bank
)
2271 struct sam3_bank_private
*pPrivate
;
2274 if (bank
->target
->state
!= TARGET_HALTED
) {
2275 LOG_ERROR("Target not halted");
2276 return ERROR_TARGET_NOT_HALTED
;
2279 pPrivate
= get_sam3_bank_private(bank
);
2281 LOG_ERROR("no private for this bank?");
2284 if (!(pPrivate
->probed
))
2285 return ERROR_FLASH_BANK_NOT_PROBED
;
2287 r
= FLASHD_GetLockBits(pPrivate
, &v
);
2288 if (r
!= ERROR_OK
) {
2289 LOG_DEBUG("Failed: %d", r
);
2293 for (x
= 0; x
< pPrivate
->nsectors
; x
++)
2294 bank
->sectors
[x
].is_protected
= (!!(v
& (1 << x
)));
2299 FLASH_BANK_COMMAND_HANDLER(sam3_flash_bank_command
)
2301 struct sam3_chip
*pChip
;
2303 pChip
= all_sam3_chips
;
2305 /* is this an existing chip? */
2307 if (pChip
->target
== bank
->target
)
2309 pChip
= pChip
->next
;
2313 /* this is a *NEW* chip */
2314 pChip
= calloc(1, sizeof(struct sam3_chip
));
2316 LOG_ERROR("NO RAM!");
2319 pChip
->target
= bank
->target
;
2320 /* insert at head */
2321 pChip
->next
= all_sam3_chips
;
2322 all_sam3_chips
= pChip
;
2323 pChip
->target
= bank
->target
;
2324 /* assumption is this runs at 32khz */
2325 pChip
->cfg
.slow_freq
= 32768;
2329 switch (bank
->base
) {
2331 LOG_ERROR("Address 0x%08x invalid bank address (try 0x%08x or 0x%08x "
2332 "[at91sam3u series] or 0x%08x [at91sam3s series] or "
2333 "0x%08x [at91sam3n series])",
2334 ((unsigned int)(bank
->base
)),
2335 ((unsigned int)(FLASH_BANK0_BASE_U
)),
2336 ((unsigned int)(FLASH_BANK1_BASE_U
)),
2337 ((unsigned int)(FLASH_BANK_BASE_S
)),
2338 ((unsigned int)(FLASH_BANK_BASE_N
)));
2342 /* at91sam3u series */
2343 case FLASH_BANK0_BASE_U
:
2344 bank
->driver_priv
= &(pChip
->details
.bank
[0]);
2345 bank
->bank_number
= 0;
2346 pChip
->details
.bank
[0].pChip
= pChip
;
2347 pChip
->details
.bank
[0].pBank
= bank
;
2349 case FLASH_BANK1_BASE_U
:
2350 bank
->driver_priv
= &(pChip
->details
.bank
[1]);
2351 bank
->bank_number
= 1;
2352 pChip
->details
.bank
[1].pChip
= pChip
;
2353 pChip
->details
.bank
[1].pBank
= bank
;
2356 /* at91sam3s and at91sam3n series */
2357 case FLASH_BANK_BASE_S
:
2358 bank
->driver_priv
= &(pChip
->details
.bank
[0]);
2359 bank
->bank_number
= 0;
2360 pChip
->details
.bank
[0].pChip
= pChip
;
2361 pChip
->details
.bank
[0].pBank
= bank
;
2365 /* we initialize after probing. */
2369 static int sam3_GetDetails(struct sam3_bank_private
*pPrivate
)
2371 const struct sam3_chip_details
*pDetails
;
2372 struct sam3_chip
*pChip
;
2373 struct flash_bank
*saved_banks
[SAM3_MAX_FLASH_BANKS
];
2377 pDetails
= all_sam3_details
;
2378 while (pDetails
->name
) {
2379 /* Compare cidr without version bits */
2380 if (pDetails
->chipid_cidr
== (pPrivate
->pChip
->cfg
.CHIPID_CIDR
& 0xFFFFFFE0))
2385 if (pDetails
->name
== NULL
) {
2386 LOG_ERROR("SAM3 ChipID 0x%08x not found in table (perhaps you can this chip?)",
2387 (unsigned int)(pPrivate
->pChip
->cfg
.CHIPID_CIDR
));
2388 /* Help the victim, print details about the chip */
2389 LOG_INFO("SAM3 CHIPID_CIDR: 0x%08x decodes as follows",
2390 pPrivate
->pChip
->cfg
.CHIPID_CIDR
);
2391 sam3_explain_chipid_cidr(pPrivate
->pChip
);
2395 /* DANGER: THERE ARE DRAGONS HERE */
2397 /* get our pChip - it is going */
2398 /* to be over-written shortly */
2399 pChip
= pPrivate
->pChip
;
2401 /* Note that, in reality: */
2403 /* pPrivate = &(pChip->details.bank[0]) */
2404 /* or pPrivate = &(pChip->details.bank[1]) */
2407 /* save the "bank" pointers */
2408 for (x
= 0; x
< SAM3_MAX_FLASH_BANKS
; x
++)
2409 saved_banks
[x
] = pChip
->details
.bank
[x
].pBank
;
2411 /* Overwrite the "details" structure. */
2412 memcpy(&(pPrivate
->pChip
->details
),
2414 sizeof(pPrivate
->pChip
->details
));
2416 /* now fix the ghosted pointers */
2417 for (x
= 0; x
< SAM3_MAX_FLASH_BANKS
; x
++) {
2418 pChip
->details
.bank
[x
].pChip
= pChip
;
2419 pChip
->details
.bank
[x
].pBank
= saved_banks
[x
];
2422 /* update the *BANK*SIZE* */
2428 static int _sam3_probe(struct flash_bank
*bank
, int noise
)
2432 struct sam3_bank_private
*pPrivate
;
2435 LOG_DEBUG("Begin: Bank: %d, Noise: %d", bank
->bank_number
, noise
);
2436 if (bank
->target
->state
!= TARGET_HALTED
) {
2437 LOG_ERROR("Target not halted");
2438 return ERROR_TARGET_NOT_HALTED
;
2441 pPrivate
= get_sam3_bank_private(bank
);
2443 LOG_ERROR("Invalid/unknown bank number");
2447 r
= sam3_ReadAllRegs(pPrivate
->pChip
);
2452 if (pPrivate
->pChip
->probed
)
2453 r
= sam3_GetInfo(pPrivate
->pChip
);
2455 r
= sam3_GetDetails(pPrivate
);
2459 /* update the flash bank size */
2460 for (x
= 0; x
< SAM3_MAX_FLASH_BANKS
; x
++) {
2461 if (bank
->base
== pPrivate
->pChip
->details
.bank
[x
].base_address
) {
2462 bank
->size
= pPrivate
->pChip
->details
.bank
[x
].size_bytes
;
2467 if (bank
->sectors
== NULL
) {
2468 bank
->sectors
= calloc(pPrivate
->nsectors
, (sizeof((bank
->sectors
)[0])));
2469 if (bank
->sectors
== NULL
) {
2470 LOG_ERROR("No memory!");
2473 bank
->num_sectors
= pPrivate
->nsectors
;
2475 for (x
= 0; ((int)(x
)) < bank
->num_sectors
; x
++) {
2476 bank
->sectors
[x
].size
= pPrivate
->sector_size
;
2477 bank
->sectors
[x
].offset
= x
* (pPrivate
->sector_size
);
2478 /* mark as unknown */
2479 bank
->sectors
[x
].is_erased
= -1;
2480 bank
->sectors
[x
].is_protected
= -1;
2484 pPrivate
->probed
= 1;
2486 r
= sam3_protect_check(bank
);
2490 LOG_DEBUG("Bank = %d, nbanks = %d",
2491 pPrivate
->bank_number
, pPrivate
->pChip
->details
.n_banks
);
2492 if ((pPrivate
->bank_number
+ 1) == pPrivate
->pChip
->details
.n_banks
) {
2493 /* read unique id, */
2494 /* it appears to be associated with the *last* flash bank. */
2495 FLASHD_ReadUniqueID(pPrivate
);
2501 static int sam3_probe(struct flash_bank
*bank
)
2503 return _sam3_probe(bank
, 1);
2506 static int sam3_auto_probe(struct flash_bank
*bank
)
2508 return _sam3_probe(bank
, 0);
2511 static int sam3_erase(struct flash_bank
*bank
, int first
, int last
)
2513 struct sam3_bank_private
*pPrivate
;
2517 if (bank
->target
->state
!= TARGET_HALTED
) {
2518 LOG_ERROR("Target not halted");
2519 return ERROR_TARGET_NOT_HALTED
;
2522 r
= sam3_auto_probe(bank
);
2523 if (r
!= ERROR_OK
) {
2524 LOG_DEBUG("Here,r=%d", r
);
2528 pPrivate
= get_sam3_bank_private(bank
);
2529 if (!(pPrivate
->probed
))
2530 return ERROR_FLASH_BANK_NOT_PROBED
;
2532 if ((first
== 0) && ((last
+ 1) == ((int)(pPrivate
->nsectors
)))) {
2535 return FLASHD_EraseEntireBank(pPrivate
);
2537 LOG_INFO("sam3 auto-erases while programing (request ignored)");
2541 static int sam3_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
2543 struct sam3_bank_private
*pPrivate
;
2547 if (bank
->target
->state
!= TARGET_HALTED
) {
2548 LOG_ERROR("Target not halted");
2549 return ERROR_TARGET_NOT_HALTED
;
2552 pPrivate
= get_sam3_bank_private(bank
);
2553 if (!(pPrivate
->probed
))
2554 return ERROR_FLASH_BANK_NOT_PROBED
;
2557 r
= FLASHD_Lock(pPrivate
, (unsigned)(first
), (unsigned)(last
));
2559 r
= FLASHD_Unlock(pPrivate
, (unsigned)(first
), (unsigned)(last
));
2560 LOG_DEBUG("End: r=%d", r
);
2566 static int sam3_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
2568 if (bank
->target
->state
!= TARGET_HALTED
) {
2569 LOG_ERROR("Target not halted");
2570 return ERROR_TARGET_NOT_HALTED
;
2576 static int sam3_page_read(struct sam3_bank_private
*pPrivate
, unsigned pagenum
, uint8_t *buf
)
2581 adr
= pagenum
* pPrivate
->page_size
;
2582 adr
+= adr
+ pPrivate
->base_address
;
2584 r
= target_read_memory(pPrivate
->pChip
->target
,
2586 4, /* THIS*MUST*BE* in 32bit values */
2587 pPrivate
->page_size
/ 4,
2590 LOG_ERROR("SAM3: Flash program failed to read page phys address: 0x%08x",
2591 (unsigned int)(adr
));
2595 /* The code below is basically this: */
2597 /* arm-none-eabi-gcc -mthumb -mcpu = cortex-m3 -O9 -S ./foobar.c -o foobar.s */
2599 /* Only the *CPU* can write to the flash buffer. */
2600 /* the DAP cannot... so - we download this 28byte thing */
2601 /* Run the algorithm - (below) */
2602 /* to program the device */
2604 /* ======================================== */
2605 /* #include <stdint.h> */
2608 /* uint32_t *dst; */
2609 /* const uint32_t *src; */
2611 /* volatile uint32_t *base; */
2616 /* uint32_t sam3_function(struct foo *p) */
2618 /* volatile uint32_t *v; */
2620 /* const uint32_t *s; */
2635 /* v[ 1 ] = p->cmd; */
2638 /* } while (!(r&1)) */
2642 /* ======================================== */
2644 static const uint8_t
2645 sam3_page_write_opcodes
[] = {
2646 /* 24 0000 0446 mov r4, r0 */
2648 /* 25 0002 6168 ldr r1, [r4, #4] */
2650 /* 26 0004 0068 ldr r0, [r0, #0] */
2652 /* 27 0006 A268 ldr r2, [r4, #8] */
2654 /* 28 @ lr needed for prologue */
2656 /* 30 0008 51F8043B ldr r3, [r1], #4 */
2657 0x51, 0xf8, 0x04, 0x3b,
2658 /* 31 000c 12F1FF32 adds r2, r2, #-1 */
2659 0x12, 0xf1, 0xff, 0x32,
2660 /* 32 0010 40F8043B str r3, [r0], #4 */
2661 0x40, 0xf8, 0x04, 0x3b,
2662 /* 33 0014 F8D1 bne .L2 */
2664 /* 34 0016 E268 ldr r2, [r4, #12] */
2666 /* 35 0018 2369 ldr r3, [r4, #16] */
2668 /* 36 001a 5360 str r3, [r2, #4] */
2670 /* 37 001c 0832 adds r2, r2, #8 */
2673 /* 39 001e 1068 ldr r0, [r2, #0] */
2675 /* 40 0020 10F0010F tst r0, #1 */
2676 0x10, 0xf0, 0x01, 0x0f,
2677 /* 41 0024 FBD0 beq .L4 */
2679 0x00, 0xBE /* bkpt #0 */
2682 static int sam3_page_write(struct sam3_bank_private
*pPrivate
, unsigned pagenum
, uint8_t *buf
)
2686 uint32_t fmr
; /* EEFC Flash Mode Register */
2689 adr
= pagenum
* pPrivate
->page_size
;
2690 adr
+= (adr
+ pPrivate
->base_address
);
2692 /* Get flash mode register value */
2693 r
= target_read_u32(pPrivate
->pChip
->target
, pPrivate
->controller_address
, &fmr
);
2695 LOG_DEBUG("Error Read failed: read flash mode register");
2697 /* Clear flash wait state field */
2700 /* set FWS (flash wait states) field in the FMR (flash mode register) */
2701 fmr
|= (pPrivate
->flash_wait_states
<< 8);
2703 LOG_DEBUG("Flash Mode: 0x%08x", ((unsigned int)(fmr
)));
2704 r
= target_write_u32(pPrivate
->pBank
->target
, pPrivate
->controller_address
, fmr
);
2706 LOG_DEBUG("Error Write failed: set flash mode register");
2708 LOG_DEBUG("Wr Page %u @ phys address: 0x%08x", pagenum
, (unsigned int)(adr
));
2709 r
= target_write_memory(pPrivate
->pChip
->target
,
2711 4, /* THIS*MUST*BE* in 32bit values */
2712 pPrivate
->page_size
/ 4,
2714 if (r
!= ERROR_OK
) {
2715 LOG_ERROR("SAM3: Failed to write (buffer) page at phys address 0x%08x",
2716 (unsigned int)(adr
));
2720 r
= EFC_PerformCommand(pPrivate
,
2721 /* send Erase & Write Page */
2727 LOG_ERROR("SAM3: Error performing Erase & Write page @ phys address 0x%08x",
2728 (unsigned int)(adr
));
2729 if (status
& (1 << 2)) {
2730 LOG_ERROR("SAM3: Page @ Phys address 0x%08x is locked", (unsigned int)(adr
));
2733 if (status
& (1 << 1)) {
2734 LOG_ERROR("SAM3: Flash Command error @phys address 0x%08x", (unsigned int)(adr
));
2740 static int sam3_write(struct flash_bank
*bank
,
2749 unsigned page_offset
;
2750 struct sam3_bank_private
*pPrivate
;
2751 uint8_t *pagebuffer
;
2753 /* incase we bail further below, set this to null */
2756 /* ignore dumb requests */
2762 if (bank
->target
->state
!= TARGET_HALTED
) {
2763 LOG_ERROR("Target not halted");
2764 r
= ERROR_TARGET_NOT_HALTED
;
2768 pPrivate
= get_sam3_bank_private(bank
);
2769 if (!(pPrivate
->probed
)) {
2770 r
= ERROR_FLASH_BANK_NOT_PROBED
;
2774 if ((offset
+ count
) > pPrivate
->size_bytes
) {
2775 LOG_ERROR("Flash write error - past end of bank");
2776 LOG_ERROR(" offset: 0x%08x, count 0x%08x, BankEnd: 0x%08x",
2777 (unsigned int)(offset
),
2778 (unsigned int)(count
),
2779 (unsigned int)(pPrivate
->size_bytes
));
2784 pagebuffer
= malloc(pPrivate
->page_size
);
2786 LOG_ERROR("No memory for %d Byte page buffer", (int)(pPrivate
->page_size
));
2791 /* what page do we start & end in? */
2792 page_cur
= offset
/ pPrivate
->page_size
;
2793 page_end
= (offset
+ count
- 1) / pPrivate
->page_size
;
2795 LOG_DEBUG("Offset: 0x%08x, Count: 0x%08x", (unsigned int)(offset
), (unsigned int)(count
));
2796 LOG_DEBUG("Page start: %d, Page End: %d", (int)(page_cur
), (int)(page_end
));
2798 /* Special case: all one page */
2801 /* (1) non-aligned start */
2802 /* (2) body pages */
2803 /* (3) non-aligned end. */
2805 /* Handle special case - all one page. */
2806 if (page_cur
== page_end
) {
2807 LOG_DEBUG("Special case, all in one page");
2808 r
= sam3_page_read(pPrivate
, page_cur
, pagebuffer
);
2812 page_offset
= (offset
& (pPrivate
->page_size
-1));
2813 memcpy(pagebuffer
+ page_offset
,
2817 r
= sam3_page_write(pPrivate
, page_cur
, pagebuffer
);
2824 /* non-aligned start */
2825 page_offset
= offset
& (pPrivate
->page_size
- 1);
2827 LOG_DEBUG("Not-Aligned start");
2828 /* read the partial */
2829 r
= sam3_page_read(pPrivate
, page_cur
, pagebuffer
);
2833 /* over-write with new data */
2834 n
= (pPrivate
->page_size
- page_offset
);
2835 memcpy(pagebuffer
+ page_offset
,
2839 r
= sam3_page_write(pPrivate
, page_cur
, pagebuffer
);
2849 /* By checking that offset is correct here, we also
2850 fix a clang warning */
2851 assert(offset
% pPrivate
->page_size
== 0);
2853 /* intermediate large pages */
2854 /* also - the final *terminal* */
2855 /* if that terminal page is a full page */
2856 LOG_DEBUG("Full Page Loop: cur=%d, end=%d, count = 0x%08x",
2857 (int)page_cur
, (int)page_end
, (unsigned int)(count
));
2859 while ((page_cur
< page_end
) &&
2860 (count
>= pPrivate
->page_size
)) {
2861 r
= sam3_page_write(pPrivate
, page_cur
, buffer
);
2864 count
-= pPrivate
->page_size
;
2865 buffer
+= pPrivate
->page_size
;
2869 /* terminal partial page? */
2871 LOG_DEBUG("Terminal partial page, count = 0x%08x", (unsigned int)(count
));
2872 /* we have a partial page */
2873 r
= sam3_page_read(pPrivate
, page_cur
, pagebuffer
);
2876 /* data goes at start */
2877 memcpy(pagebuffer
, buffer
, count
);
2878 r
= sam3_page_write(pPrivate
, page_cur
, pagebuffer
);
2891 COMMAND_HANDLER(sam3_handle_info_command
)
2893 struct sam3_chip
*pChip
;
2894 pChip
= get_current_sam3(CMD_CTX
);
2901 /* bank0 must exist before we can do anything */
2902 if (pChip
->details
.bank
[0].pBank
== NULL
) {
2905 command_print(CMD_CTX
,
2906 "Please define bank %d via command: flash bank %s ... ",
2908 at91sam3_flash
.name
);
2912 /* if bank 0 is not probed, then probe it */
2913 if (!(pChip
->details
.bank
[0].probed
)) {
2914 r
= sam3_auto_probe(pChip
->details
.bank
[0].pBank
);
2918 /* above guarantees the "chip details" structure is valid */
2919 /* and thus, bank private areas are valid */
2920 /* and we have a SAM3 chip, what a concept! */
2922 /* auto-probe other banks, 0 done above */
2923 for (x
= 1; x
< SAM3_MAX_FLASH_BANKS
; x
++) {
2924 /* skip banks not present */
2925 if (!(pChip
->details
.bank
[x
].present
))
2928 if (pChip
->details
.bank
[x
].pBank
== NULL
)
2931 if (pChip
->details
.bank
[x
].probed
)
2934 r
= sam3_auto_probe(pChip
->details
.bank
[x
].pBank
);
2939 r
= sam3_GetInfo(pChip
);
2940 if (r
!= ERROR_OK
) {
2941 LOG_DEBUG("Sam3Info, Failed %d", r
);
2948 COMMAND_HANDLER(sam3_handle_gpnvm_command
)
2952 struct sam3_chip
*pChip
;
2954 pChip
= get_current_sam3(CMD_CTX
);
2958 if (pChip
->target
->state
!= TARGET_HALTED
) {
2959 LOG_ERROR("sam3 - target not halted");
2960 return ERROR_TARGET_NOT_HALTED
;
2963 if (pChip
->details
.bank
[0].pBank
== NULL
) {
2964 command_print(CMD_CTX
, "Bank0 must be defined first via: flash bank %s ...",
2965 at91sam3_flash
.name
);
2968 if (!pChip
->details
.bank
[0].probed
) {
2969 r
= sam3_auto_probe(pChip
->details
.bank
[0].pBank
);
2976 return ERROR_COMMAND_SYNTAX_ERROR
;
2985 if ((0 == strcmp(CMD_ARGV
[0], "show")) && (0 == strcmp(CMD_ARGV
[1], "all")))
2989 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], v32
);
2995 if (0 == strcmp("show", CMD_ARGV
[0])) {
2999 for (x
= 0; x
< pChip
->details
.n_gpnvms
; x
++) {
3000 r
= FLASHD_GetGPNVM(&(pChip
->details
.bank
[0]), x
, &v
);
3003 command_print(CMD_CTX
, "sam3-gpnvm%u: %u", x
, v
);
3007 if ((who
>= 0) && (((unsigned)(who
)) < pChip
->details
.n_gpnvms
)) {
3008 r
= FLASHD_GetGPNVM(&(pChip
->details
.bank
[0]), who
, &v
);
3009 command_print(CMD_CTX
, "sam3-gpnvm%u: %u", who
, v
);
3012 command_print(CMD_CTX
, "sam3-gpnvm invalid GPNVM: %u", who
);
3013 return ERROR_COMMAND_SYNTAX_ERROR
;
3018 command_print(CMD_CTX
, "Missing GPNVM number");
3019 return ERROR_COMMAND_SYNTAX_ERROR
;
3022 if (0 == strcmp("set", CMD_ARGV
[0]))
3023 r
= FLASHD_SetGPNVM(&(pChip
->details
.bank
[0]), who
);
3024 else if ((0 == strcmp("clr", CMD_ARGV
[0])) ||
3025 (0 == strcmp("clear", CMD_ARGV
[0]))) /* quietly accept both */
3026 r
= FLASHD_ClrGPNVM(&(pChip
->details
.bank
[0]), who
);
3028 command_print(CMD_CTX
, "Unknown command: %s", CMD_ARGV
[0]);
3029 r
= ERROR_COMMAND_SYNTAX_ERROR
;
3034 COMMAND_HANDLER(sam3_handle_slowclk_command
)
3036 struct sam3_chip
*pChip
;
3038 pChip
= get_current_sam3(CMD_CTX
);
3050 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], v
);
3052 /* absurd slow clock of 200Khz? */
3053 command_print(CMD_CTX
, "Absurd/illegal slow clock freq: %d\n", (int)(v
));
3054 return ERROR_COMMAND_SYNTAX_ERROR
;
3056 pChip
->cfg
.slow_freq
= v
;
3061 command_print(CMD_CTX
, "Too many parameters");
3062 return ERROR_COMMAND_SYNTAX_ERROR
;
3065 command_print(CMD_CTX
, "Slowclk freq: %d.%03dkhz",
3066 (int)(pChip
->cfg
.slow_freq
/ 1000),
3067 (int)(pChip
->cfg
.slow_freq
% 1000));
3071 static const struct command_registration at91sam3_exec_command_handlers
[] = {
3074 .handler
= sam3_handle_gpnvm_command
,
3075 .mode
= COMMAND_EXEC
,
3076 .usage
= "[('clr'|'set'|'show') bitnum]",
3077 .help
= "Without arguments, shows all bits in the gpnvm "
3078 "register. Otherwise, clears, sets, or shows one "
3079 "General Purpose Non-Volatile Memory (gpnvm) bit.",
3083 .handler
= sam3_handle_info_command
,
3084 .mode
= COMMAND_EXEC
,
3085 .help
= "Print information about the current at91sam3 chip"
3086 "and its flash configuration.",
3090 .handler
= sam3_handle_slowclk_command
,
3091 .mode
= COMMAND_EXEC
,
3092 .usage
= "[clock_hz]",
3093 .help
= "Display or set the slowclock frequency "
3094 "(default 32768 Hz).",
3096 COMMAND_REGISTRATION_DONE
3098 static const struct command_registration at91sam3_command_handlers
[] = {
3101 .mode
= COMMAND_ANY
,
3102 .help
= "at91sam3 flash command group",
3104 .chain
= at91sam3_exec_command_handlers
,
3106 COMMAND_REGISTRATION_DONE
3109 struct flash_driver at91sam3_flash
= {
3111 .commands
= at91sam3_command_handlers
,
3112 .flash_bank_command
= sam3_flash_bank_command
,
3113 .erase
= sam3_erase
,
3114 .protect
= sam3_protect
,
3115 .write
= sam3_write
,
3116 .read
= default_flash_read
,
3117 .probe
= sam3_probe
,
3118 .auto_probe
= sam3_auto_probe
,
3119 .erase_check
= sam3_erase_check
,
3120 .protect_check
= sam3_protect_check
,