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, Jim Norris *
9 * (at91sam3x* & at91sam4 support)* *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
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 /* at91sam4s/at91sam4e series (has always one flash bank)*/
69 #define FLASH_BANK_BASE_S 0x00400000
71 /* at91sam4sd series (two one flash banks), first bank address */
72 #define FLASH_BANK0_BASE_SD FLASH_BANK_BASE_S
73 /* at91sam4sd16x, second bank address */
74 #define FLASH_BANK1_BASE_1024K_SD (FLASH_BANK0_BASE_SD+(1024*1024/2))
75 /* at91sam4sd32x, second bank address */
76 #define FLASH_BANK1_BASE_2048K_SD (FLASH_BANK0_BASE_SD+(2048*1024/2))
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 then Lock */
83 #define AT91C_EFC_FCMD_EA (0x5) /* (EFC) Erase All */
84 /* cmd6 is not present in the at91sam4u4/2/1 data sheet table 19-2 */
85 /* #define AT91C_EFC_FCMD_EPL (0x6) // (EFC) Erase plane? */
86 #define AT91C_EFC_FCMD_EPA (0x7) /* (EFC) Erase pages */
87 #define AT91C_EFC_FCMD_SLB (0x8) /* (EFC) Set Lock Bit */
88 #define AT91C_EFC_FCMD_CLB (0x9) /* (EFC) Clear Lock Bit */
89 #define AT91C_EFC_FCMD_GLB (0xA) /* (EFC) Get Lock Bit */
90 #define AT91C_EFC_FCMD_SFB (0xB) /* (EFC) Set Fuse Bit */
91 #define AT91C_EFC_FCMD_CFB (0xC) /* (EFC) Clear Fuse Bit */
92 #define AT91C_EFC_FCMD_GFB (0xD) /* (EFC) Get Fuse Bit */
93 #define AT91C_EFC_FCMD_STUI (0xE) /* (EFC) Start Read Unique ID */
94 #define AT91C_EFC_FCMD_SPUI (0xF) /* (EFC) Stop Read Unique ID */
96 #define offset_EFC_FMR 0
97 #define offset_EFC_FCR 4
98 #define offset_EFC_FSR 8
99 #define offset_EFC_FRR 12
101 extern struct flash_driver at91sam4_flash;
103 static float _tomhz(uint32_t freq_hz)
107 f = ((float)(freq_hz)) / 1000000.0;
111 /* How the chip is configured. */
113 uint32_t unique_id[4];
117 uint32_t mainosc_freq;
127 #define SAM4_CHIPID_CIDR (0x400E0740)
128 uint32_t CHIPID_CIDR;
129 #define SAM4_CHIPID_EXID (0x400E0744)
130 uint32_t CHIPID_EXID;
132 #define SAM4_PMC_BASE (0x400E0400)
133 #define SAM4_PMC_SCSR (SAM4_PMC_BASE + 0x0008)
135 #define SAM4_PMC_PCSR (SAM4_PMC_BASE + 0x0018)
137 #define SAM4_CKGR_UCKR (SAM4_PMC_BASE + 0x001c)
139 #define SAM4_CKGR_MOR (SAM4_PMC_BASE + 0x0020)
141 #define SAM4_CKGR_MCFR (SAM4_PMC_BASE + 0x0024)
143 #define SAM4_CKGR_PLLAR (SAM4_PMC_BASE + 0x0028)
145 #define SAM4_PMC_MCKR (SAM4_PMC_BASE + 0x0030)
147 #define SAM4_PMC_PCK0 (SAM4_PMC_BASE + 0x0040)
149 #define SAM4_PMC_PCK1 (SAM4_PMC_BASE + 0x0044)
151 #define SAM4_PMC_PCK2 (SAM4_PMC_BASE + 0x0048)
153 #define SAM4_PMC_SR (SAM4_PMC_BASE + 0x0068)
155 #define SAM4_PMC_IMR (SAM4_PMC_BASE + 0x006c)
157 #define SAM4_PMC_FSMR (SAM4_PMC_BASE + 0x0070)
159 #define SAM4_PMC_FSPR (SAM4_PMC_BASE + 0x0074)
163 struct sam4_bank_private {
165 /* DANGER: THERE ARE DRAGONS HERE.. */
166 /* NOTE: If you add more 'ghost' pointers */
167 /* be aware that you must *manually* update */
168 /* these pointers in the function sam4_GetDetails() */
169 /* See the comment "Here there be dragons" */
171 /* so we can find the chip we belong to */
172 struct sam4_chip *pChip;
173 /* so we can find the original bank pointer */
174 struct flash_bank *pBank;
175 unsigned bank_number;
176 uint32_t controller_address;
177 uint32_t base_address;
178 uint32_t flash_wait_states;
182 unsigned sector_size;
186 struct sam4_chip_details {
187 /* THERE ARE DRAGONS HERE.. */
188 /* note: If you add pointers here */
189 /* be careful about them as they */
190 /* may need to be updated inside */
191 /* the function: "sam4_GetDetails() */
192 /* which copy/overwrites the */
193 /* 'runtime' copy of this structure */
194 uint32_t chipid_cidr;
198 #define SAM4_N_NVM_BITS 3
199 unsigned gpnvm[SAM4_N_NVM_BITS];
200 unsigned total_flash_size;
201 unsigned total_sram_size;
203 #define SAM4_MAX_FLASH_BANKS 2
204 /* these are "initialized" from the global const data */
205 struct sam4_bank_private bank[SAM4_MAX_FLASH_BANKS];
209 struct sam4_chip *next;
212 /* this is "initialized" from the global const structure */
213 struct sam4_chip_details details;
214 struct target *target;
219 struct sam4_reg_list {
220 uint32_t address; size_t struct_offset; const char *name;
221 void (*explain_func)(struct sam4_chip *pInfo);
224 static struct sam4_chip *all_sam4_chips;
226 static struct sam4_chip *get_current_sam4(struct command_context *cmd_ctx)
229 static struct sam4_chip *p;
231 t = get_current_target(cmd_ctx);
233 command_print(cmd_ctx, "No current target?");
239 /* this should not happen */
240 /* the command is not registered until the chip is created? */
241 command_print(cmd_ctx, "No SAM4 chips exist?");
250 command_print(cmd_ctx, "Cannot find SAM4 chip?");
254 /*The actual sector size of the SAM4S flash memory is 65536 bytes. 16 sectors for a 1024KB device*/
255 /*The lockregions are 8KB per lock region, with a 1024KB device having 128 lock regions. */
256 /*For the best results, nsectors are thus set to the amount of lock regions, and the sector_size*/
257 /*set to the lock region size. Page erases are used to erase 8KB sections when programming*/
259 /* these are used to *initialize* the "pChip->details" structure. */
260 static const struct sam4_chip_details all_sam4_details[] = {
262 /* Start at91sam4e* series */
263 /*atsam4e16e - LQFP144/LFBGA144*/
265 .chipid_cidr = 0xA3CC0CE0,
266 .name = "at91sam4e16e",
267 .total_flash_size = 1024 * 1024,
268 .total_sram_size = 128 * 1024,
278 .base_address = FLASH_BANK_BASE_S,
279 .controller_address = 0x400e0a00,
280 .flash_wait_states = 6, /* workaround silicon bug */
282 .size_bytes = 1024 * 1024,
297 /* Start at91sam4n* series */
298 /*atsam4n8a - LQFP48/QFN48*/
300 .chipid_cidr = 0x293B0AE0,
301 .name = "at91sam4n8a",
302 .total_flash_size = 512 * 1024,
303 .total_sram_size = 64 * 1024,
313 .base_address = FLASH_BANK_BASE_S,
314 .controller_address = 0x400e0a00,
315 .flash_wait_states = 6, /* workaround silicon bug */
317 .size_bytes = 512 * 1024,
331 /*atsam4n8b - LQFP64/QFN64*/
333 .chipid_cidr = 0x294B0AE0,
334 .name = "at91sam4n8b",
335 .total_flash_size = 512 * 1024,
336 .total_sram_size = 64 * 1024,
346 .base_address = FLASH_BANK_BASE_S,
347 .controller_address = 0x400e0a00,
348 .flash_wait_states = 6, /* workaround silicon bug */
350 .size_bytes = 512 * 1024,
364 /*atsam4n8c - LQFP100/TFBGA100/VFBGA100*/
366 .chipid_cidr = 0x295B0AE0,
367 .name = "at91sam4n8c",
368 .total_flash_size = 512 * 1024,
369 .total_sram_size = 64 * 1024,
379 .base_address = FLASH_BANK_BASE_S,
380 .controller_address = 0x400e0a00,
381 .flash_wait_states = 6, /* workaround silicon bug */
383 .size_bytes = 512 * 1024,
397 /*atsam4n16b - LQFP64/QFN64*/
399 .chipid_cidr = 0x29460CE0,
400 .name = "at91sam4n16b",
401 .total_flash_size = 1024 * 1024,
402 .total_sram_size = 80 * 1024,
412 .base_address = FLASH_BANK_BASE_S,
413 .controller_address = 0x400e0a00,
414 .flash_wait_states = 6, /* workaround silicon bug */
416 .size_bytes = 1024 * 1024,
430 /*atsam4n16c - LQFP100/TFBGA100/VFBGA100*/
432 .chipid_cidr = 0x29560CE0,
433 .name = "at91sam4n16c",
434 .total_flash_size = 1024 * 1024,
435 .total_sram_size = 80 * 1024,
445 .base_address = FLASH_BANK_BASE_S,
446 .controller_address = 0x400e0a00,
447 .flash_wait_states = 6, /* workaround silicon bug */
449 .size_bytes = 1024 * 1024,
464 /* Start at91sam4s* series */
465 /*atsam4s16c - LQFP100/BGA100*/
467 .chipid_cidr = 0x28AC0CE0,
468 .name = "at91sam4s16c",
469 .total_flash_size = 1024 * 1024,
470 .total_sram_size = 128 * 1024,
480 .base_address = FLASH_BANK_BASE_S,
481 .controller_address = 0x400e0a00,
482 .flash_wait_states = 6, /* workaround silicon bug */
484 .size_bytes = 1024 * 1024,
498 /*atsam4s16b - LQFP64/QFN64*/
500 .chipid_cidr = 0x289C0CE0,
501 .name = "at91sam4s16b",
502 .total_flash_size = 1024 * 1024,
503 .total_sram_size = 128 * 1024,
513 .base_address = FLASH_BANK_BASE_S,
514 .controller_address = 0x400e0a00,
515 .flash_wait_states = 6, /* workaround silicon bug */
517 .size_bytes = 1024 * 1024,
531 /*atsam4sa16b - LQFP64/QFN64*/
533 .chipid_cidr = 0x28970CE0,
534 .name = "at91sam4sa16b",
535 .total_flash_size = 1024 * 1024,
536 .total_sram_size = 160 * 1024,
546 .base_address = FLASH_BANK_BASE_S,
547 .controller_address = 0x400e0a00,
548 .flash_wait_states = 6, /* workaround silicon bug */
550 .size_bytes = 1024 * 1024,
564 /*atsam4s16a - LQFP48/QFN48*/
566 .chipid_cidr = 0x288C0CE0,
567 .name = "at91sam4s16a",
568 .total_flash_size = 1024 * 1024,
569 .total_sram_size = 128 * 1024,
579 .base_address = FLASH_BANK_BASE_S,
580 .controller_address = 0x400e0a00,
581 .flash_wait_states = 6, /* workaround silicon bug */
583 .size_bytes = 1024 * 1024,
597 /*atsam4s8c - LQFP100/BGA100*/
599 .chipid_cidr = 0x28AC0AE0,
600 .name = "at91sam4s8c",
601 .total_flash_size = 512 * 1024,
602 .total_sram_size = 128 * 1024,
612 .base_address = FLASH_BANK_BASE_S,
613 .controller_address = 0x400e0a00,
614 .flash_wait_states = 6, /* workaround silicon bug */
616 .size_bytes = 512 * 1024,
630 /*atsam4s8b - LQFP64/BGA64*/
632 .chipid_cidr = 0x289C0AE0,
633 .name = "at91sam4s8b",
634 .total_flash_size = 512 * 1024,
635 .total_sram_size = 128 * 1024,
645 .base_address = FLASH_BANK_BASE_S,
646 .controller_address = 0x400e0a00,
647 .flash_wait_states = 6, /* workaround silicon bug */
649 .size_bytes = 512 * 1024,
663 /*atsam4s8a - LQFP48/BGA48*/
665 .chipid_cidr = 0x288C0AE0,
666 .name = "at91sam4s8a",
667 .total_flash_size = 512 * 1024,
668 .total_sram_size = 128 * 1024,
678 .base_address = FLASH_BANK_BASE_S,
679 .controller_address = 0x400e0a00,
680 .flash_wait_states = 6, /* workaround silicon bug */
682 .size_bytes = 512 * 1024,
697 /*atsam4s4a - LQFP48/BGA48*/
699 .chipid_cidr = 0x288b09e0,
700 .name = "at91sam4s4a",
701 .total_flash_size = 256 * 1024,
702 .total_sram_size = 64 * 1024,
712 .base_address = FLASH_BANK_BASE_S,
713 .controller_address = 0x400e0a00,
714 .flash_wait_states = 6, /* workaround silicon bug */
716 .size_bytes = 256 * 1024,
733 .chipid_cidr = 0x29a70ee0,
734 .name = "at91sam4sd32c",
735 .total_flash_size = 2048 * 1024,
736 .total_sram_size = 160 * 1024,
747 .base_address = FLASH_BANK0_BASE_SD,
748 .controller_address = 0x400e0a00,
749 .flash_wait_states = 6, /* workaround silicon bug */
751 .size_bytes = 1024 * 1024,
763 .base_address = FLASH_BANK1_BASE_2048K_SD,
764 .controller_address = 0x400e0c00,
765 .flash_wait_states = 6, /* workaround silicon bug */
767 .size_bytes = 1024 * 1024,
777 .chipid_cidr = 0x29a70ce0,
778 .name = "at91sam4sd16c",
779 .total_flash_size = 1024 * 1024,
780 .total_sram_size = 160 * 1024,
791 .base_address = FLASH_BANK0_BASE_SD,
792 .controller_address = 0x400e0a00,
793 .flash_wait_states = 6, /* workaround silicon bug */
795 .size_bytes = 512 * 1024,
807 .base_address = FLASH_BANK1_BASE_1024K_SD,
808 .controller_address = 0x400e0c00,
809 .flash_wait_states = 6, /* workaround silicon bug */
811 .size_bytes = 512 * 1024,
821 .chipid_cidr = 0x28a70ce0,
822 .name = "at91sam4sa16c",
823 .total_flash_size = 1024 * 1024,
824 .total_sram_size = 160 * 1024,
835 .base_address = FLASH_BANK0_BASE_SD,
836 .controller_address = 0x400e0a00,
837 .flash_wait_states = 6, /* workaround silicon bug */
839 .size_bytes = 512 * 1024,
851 .base_address = FLASH_BANK1_BASE_1024K_SD,
852 .controller_address = 0x400e0c00,
853 .flash_wait_states = 6, /* workaround silicon bug */
855 .size_bytes = 512 * 1024,
865 .chipid_cidr = 0x247e0ae0,
866 .name = "at91samg53n19",
867 .total_flash_size = 512 * 1024,
868 .total_sram_size = 96 * 1024,
879 .base_address = FLASH_BANK_BASE_S,
880 .controller_address = 0x400e0a00,
881 .flash_wait_states = 6, /* workaround silicon bug */
883 .size_bytes = 512 * 1024,
906 /***********************************************************************
907 **********************************************************************
908 **********************************************************************
909 **********************************************************************
910 **********************************************************************
911 **********************************************************************/
912 /* *ATMEL* style code - from the SAM4 driver code */
915 * Get the current status of the EEFC and
916 * the value of some status bits (LOCKE, PROGE).
917 * @param pPrivate - info about the bank
918 * @param v - result goes here
920 static int EFC_GetStatus(struct sam4_bank_private *pPrivate, uint32_t *v)
923 r = target_read_u32(pPrivate->pChip->target,
924 pPrivate->controller_address + offset_EFC_FSR,
926 LOG_DEBUG("Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
928 ((unsigned int)((*v >> 2) & 1)),
929 ((unsigned int)((*v >> 1) & 1)),
930 ((unsigned int)((*v >> 0) & 1)));
936 * Get the result of the last executed command.
937 * @param pPrivate - info about the bank
938 * @param v - result goes here
940 static int EFC_GetResult(struct sam4_bank_private *pPrivate, uint32_t *v)
944 r = target_read_u32(pPrivate->pChip->target,
945 pPrivate->controller_address + offset_EFC_FRR,
949 LOG_DEBUG("Result: 0x%08x", ((unsigned int)(rv)));
953 static int EFC_StartCommand(struct sam4_bank_private *pPrivate,
954 unsigned command, unsigned argument)
963 /* Check command & argument */
966 case AT91C_EFC_FCMD_WP:
967 case AT91C_EFC_FCMD_WPL:
968 case AT91C_EFC_FCMD_EWP:
969 case AT91C_EFC_FCMD_EWPL:
970 /* case AT91C_EFC_FCMD_EPL: */
971 case AT91C_EFC_FCMD_EPA:
972 case AT91C_EFC_FCMD_SLB:
973 case AT91C_EFC_FCMD_CLB:
974 n = (pPrivate->size_bytes / pPrivate->page_size);
976 LOG_ERROR("*BUG*: Embedded flash has only %u pages", (unsigned)(n));
979 case AT91C_EFC_FCMD_SFB:
980 case AT91C_EFC_FCMD_CFB:
981 if (argument >= pPrivate->pChip->details.n_gpnvms) {
982 LOG_ERROR("*BUG*: Embedded flash has only %d GPNVMs",
983 pPrivate->pChip->details.n_gpnvms);
987 case AT91C_EFC_FCMD_GETD:
988 case AT91C_EFC_FCMD_EA:
989 case AT91C_EFC_FCMD_GLB:
990 case AT91C_EFC_FCMD_GFB:
991 case AT91C_EFC_FCMD_STUI:
992 case AT91C_EFC_FCMD_SPUI:
994 LOG_ERROR("Argument is meaningless for cmd: %d", command);
997 LOG_ERROR("Unknown command %d", command);
1001 if (command == AT91C_EFC_FCMD_SPUI) {
1002 /* this is a very special situation. */
1003 /* Situation (1) - error/retry - see below */
1004 /* And we are being called recursively */
1005 /* Situation (2) - normal, finished reading unique id */
1007 /* it should be "ready" */
1008 EFC_GetStatus(pPrivate, &v);
1010 /* then it is ready */
1014 /* we have done this before */
1015 /* the controller is not responding. */
1016 LOG_ERROR("flash controller(%d) is not ready! Error",
1017 pPrivate->bank_number);
1021 LOG_ERROR("Flash controller(%d) is not ready, attempting reset",
1022 pPrivate->bank_number);
1023 /* we do that by issuing the *STOP* command */
1024 EFC_StartCommand(pPrivate, AT91C_EFC_FCMD_SPUI, 0);
1025 /* above is recursive, and further recursion is blocked by */
1026 /* if (command == AT91C_EFC_FCMD_SPUI) above */
1032 v = (0x5A << 24) | (argument << 8) | command;
1033 LOG_DEBUG("Command: 0x%08x", ((unsigned int)(v)));
1034 r = target_write_u32(pPrivate->pBank->target,
1035 pPrivate->controller_address + offset_EFC_FCR, v);
1037 LOG_DEBUG("Error Write failed");
1042 * Performs the given command and wait until its completion (or an error).
1043 * @param pPrivate - info about the bank
1044 * @param command - Command to perform.
1045 * @param argument - Optional command argument.
1046 * @param status - put command status bits here
1048 static int EFC_PerformCommand(struct sam4_bank_private *pPrivate,
1056 int64_t ms_now, ms_end;
1062 r = EFC_StartCommand(pPrivate, command, argument);
1066 ms_end = 10000 + timeval_ms();
1069 r = EFC_GetStatus(pPrivate, &v);
1072 ms_now = timeval_ms();
1073 if (ms_now > ms_end) {
1075 LOG_ERROR("Command timeout");
1078 } while ((v & 1) == 0);
1082 *status = (v & 0x6);
1088 * Read the unique ID.
1089 * @param pPrivate - info about the bank
1090 * The unique ID is stored in the 'pPrivate' structure.
1092 static int FLASHD_ReadUniqueID(struct sam4_bank_private *pPrivate)
1098 pPrivate->pChip->cfg.unique_id[0] = 0;
1099 pPrivate->pChip->cfg.unique_id[1] = 0;
1100 pPrivate->pChip->cfg.unique_id[2] = 0;
1101 pPrivate->pChip->cfg.unique_id[3] = 0;
1104 r = EFC_StartCommand(pPrivate, AT91C_EFC_FCMD_STUI, 0);
1108 for (x = 0; x < 4; x++) {
1109 r = target_read_u32(pPrivate->pChip->target,
1110 pPrivate->pBank->base + (x * 4),
1114 pPrivate->pChip->cfg.unique_id[x] = v;
1117 r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_SPUI, 0, NULL);
1118 LOG_DEBUG("End: R=%d, id = 0x%08x, 0x%08x, 0x%08x, 0x%08x",
1120 (unsigned int)(pPrivate->pChip->cfg.unique_id[0]),
1121 (unsigned int)(pPrivate->pChip->cfg.unique_id[1]),
1122 (unsigned int)(pPrivate->pChip->cfg.unique_id[2]),
1123 (unsigned int)(pPrivate->pChip->cfg.unique_id[3]));
1129 * Erases the entire flash.
1130 * @param pPrivate - the info about the bank.
1132 static int FLASHD_EraseEntireBank(struct sam4_bank_private *pPrivate)
1135 return EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_EA, 0, NULL);
1139 * Erases the entire flash.
1140 * @param pPrivate - the info about the bank.
1142 static int FLASHD_ErasePages(struct sam4_bank_private *pPrivate,
1167 /* AT91C_EFC_FCMD_EPA
1168 * According to the datasheet FARG[15:2] defines the page from which
1169 * the erase will start.This page must be modulo 4, 8, 16 or 32
1170 * according to the number of pages to erase. FARG[1:0] defines the
1171 * number of pages to be erased. Previously (firstpage << 2) was used
1172 * to conform to this, seems it should not be shifted...
1174 return EFC_PerformCommand(pPrivate,
1175 /* send Erase Page */
1177 (firstPage) | erasePages,
1182 * Gets current GPNVM state.
1183 * @param pPrivate - info about the bank.
1184 * @param gpnvm - GPNVM bit index.
1185 * @param puthere - result stored here.
1187 /* ------------------------------------------------------------------------------ */
1188 static int FLASHD_GetGPNVM(struct sam4_bank_private *pPrivate, unsigned gpnvm, unsigned *puthere)
1194 if (pPrivate->bank_number != 0) {
1195 LOG_ERROR("GPNVM only works with Bank0");
1199 if (gpnvm >= pPrivate->pChip->details.n_gpnvms) {
1200 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1201 gpnvm, pPrivate->pChip->details.n_gpnvms);
1205 /* Get GPNVMs status */
1206 r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_GFB, 0, NULL);
1207 if (r != ERROR_OK) {
1208 LOG_ERROR("Failed");
1212 r = EFC_GetResult(pPrivate, &v);
1215 /* Check if GPNVM is set */
1216 /* get the bit and make it a 0/1 */
1217 *puthere = (v >> gpnvm) & 1;
1224 * Clears the selected GPNVM bit.
1225 * @param pPrivate info about the bank
1226 * @param gpnvm GPNVM index.
1227 * @returns 0 if successful; otherwise returns an error code.
1229 static int FLASHD_ClrGPNVM(struct sam4_bank_private *pPrivate, unsigned gpnvm)
1235 if (pPrivate->bank_number != 0) {
1236 LOG_ERROR("GPNVM only works with Bank0");
1240 if (gpnvm >= pPrivate->pChip->details.n_gpnvms) {
1241 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1242 gpnvm, pPrivate->pChip->details.n_gpnvms);
1246 r = FLASHD_GetGPNVM(pPrivate, gpnvm, &v);
1247 if (r != ERROR_OK) {
1248 LOG_DEBUG("Failed: %d", r);
1251 r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_CFB, gpnvm, NULL);
1252 LOG_DEBUG("End: %d", r);
1257 * Sets the selected GPNVM bit.
1258 * @param pPrivate info about the bank
1259 * @param gpnvm GPNVM index.
1261 static int FLASHD_SetGPNVM(struct sam4_bank_private *pPrivate, unsigned gpnvm)
1266 if (pPrivate->bank_number != 0) {
1267 LOG_ERROR("GPNVM only works with Bank0");
1271 if (gpnvm >= pPrivate->pChip->details.n_gpnvms) {
1272 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
1273 gpnvm, pPrivate->pChip->details.n_gpnvms);
1277 r = FLASHD_GetGPNVM(pPrivate, gpnvm, &v);
1285 r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_SFB, gpnvm, NULL);
1291 * Returns a bit field (at most 64) of locked regions within a page.
1292 * @param pPrivate info about the bank
1293 * @param v where to store locked bits
1295 static int FLASHD_GetLockBits(struct sam4_bank_private *pPrivate, uint32_t *v)
1299 r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_GLB, 0, NULL);
1300 if (r == ERROR_OK) {
1301 EFC_GetResult(pPrivate, v);
1302 EFC_GetResult(pPrivate, v);
1303 EFC_GetResult(pPrivate, v);
1304 r = EFC_GetResult(pPrivate, v);
1306 LOG_DEBUG("End: %d", r);
1311 * Unlocks all the regions in the given address range.
1312 * @param pPrivate info about the bank
1313 * @param start_sector first sector to unlock
1314 * @param end_sector last (inclusive) to unlock
1317 static int FLASHD_Unlock(struct sam4_bank_private *pPrivate,
1318 unsigned start_sector,
1319 unsigned end_sector)
1324 uint32_t pages_per_sector;
1326 pages_per_sector = pPrivate->sector_size / pPrivate->page_size;
1328 /* Unlock all pages */
1329 while (start_sector <= end_sector) {
1330 pg = start_sector * pages_per_sector;
1332 r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_CLB, pg, &status);
1343 * @param pPrivate - info about the bank
1344 * @param start_sector - first sector to lock
1345 * @param end_sector - last sector (inclusive) to lock
1347 static int FLASHD_Lock(struct sam4_bank_private *pPrivate,
1348 unsigned start_sector,
1349 unsigned end_sector)
1353 uint32_t pages_per_sector;
1356 pages_per_sector = pPrivate->sector_size / pPrivate->page_size;
1358 /* Lock all pages */
1359 while (start_sector <= end_sector) {
1360 pg = start_sector * pages_per_sector;
1362 r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_SLB, pg, &status);
1370 /****** END SAM4 CODE ********/
1372 /* begin helpful debug code */
1373 /* print the fieldname, the field value, in dec & hex, and return field value */
1374 static uint32_t sam4_reg_fieldname(struct sam4_chip *pChip,
1375 const char *regname,
1384 /* extract the field */
1386 v = v & ((1 << width)-1);
1395 /* show the basics */
1396 LOG_USER_N("\t%*s: %*" PRId32 " [0x%0*" PRIx32 "] ",
1397 REG_NAME_WIDTH, regname,
1403 static const char _unknown[] = "unknown";
1404 static const char *const eproc_names[] = {
1408 "Cortex-M3", /* 3 */
1410 "arm926ejs", /* 5 */
1411 "Cortex-A5", /* 6 */
1412 "Cortex-M4", /* 7 */
1423 #define nvpsize2 nvpsize /* these two tables are identical */
1424 static const char *const nvpsize[] = {
1427 "16K bytes", /* 2 */
1428 "32K bytes", /* 3 */
1430 "64K bytes", /* 5 */
1432 "128K bytes", /* 7 */
1434 "256K bytes", /* 9 */
1435 "512K bytes", /* 10 */
1437 "1024K bytes", /* 12 */
1439 "2048K bytes", /* 14 */
1443 static const char *const sramsize[] = {
1444 "48K Bytes", /* 0 */
1448 "112K Bytes", /* 4 */
1450 "80K Bytes", /* 6 */
1451 "160K Bytes", /* 7 */
1453 "16K Bytes", /* 9 */
1454 "32K Bytes", /* 10 */
1455 "64K Bytes", /* 11 */
1456 "128K Bytes", /* 12 */
1457 "256K Bytes", /* 13 */
1458 "96K Bytes", /* 14 */
1459 "512K Bytes", /* 15 */
1463 static const struct archnames { unsigned value; const char *name; } archnames[] = {
1464 { 0x19, "AT91SAM9xx Series" },
1465 { 0x29, "AT91SAM9XExx Series" },
1466 { 0x34, "AT91x34 Series" },
1467 { 0x37, "CAP7 Series" },
1468 { 0x39, "CAP9 Series" },
1469 { 0x3B, "CAP11 Series" },
1470 { 0x3C, "ATSAM4E" },
1471 { 0x40, "AT91x40 Series" },
1472 { 0x42, "AT91x42 Series" },
1473 { 0x43, "SAMG51 Series"
1475 { 0x47, "SAMG53 Series"
1477 { 0x55, "AT91x55 Series" },
1478 { 0x60, "AT91SAM7Axx Series" },
1479 { 0x61, "AT91SAM7AQxx Series" },
1480 { 0x63, "AT91x63 Series" },
1481 { 0x70, "AT91SAM7Sxx Series" },
1482 { 0x71, "AT91SAM7XCxx Series" },
1483 { 0x72, "AT91SAM7SExx Series" },
1484 { 0x73, "AT91SAM7Lxx Series" },
1485 { 0x75, "AT91SAM7Xxx Series" },
1486 { 0x76, "AT91SAM7SLxx Series" },
1487 { 0x80, "ATSAM3UxC Series (100-pin version)" },
1488 { 0x81, "ATSAM3UxE Series (144-pin version)" },
1489 { 0x83, "ATSAM3A/SAM4A xC Series (100-pin version)"},
1490 { 0x84, "ATSAM3X/SAM4X xC Series (100-pin version)"},
1491 { 0x85, "ATSAM3X/SAM4X xE Series (144-pin version)"},
1492 { 0x86, "ATSAM3X/SAM4X xG Series (208/217-pin version)" },
1493 { 0x88, "ATSAM3S/SAM4S xA Series (48-pin version)" },
1494 { 0x89, "ATSAM3S/SAM4S xB Series (64-pin version)" },
1495 { 0x8A, "ATSAM3S/SAM4S xC Series (100-pin version)"},
1496 { 0x92, "AT91x92 Series" },
1497 { 0x93, "ATSAM3NxA Series (48-pin version)" },
1498 { 0x94, "ATSAM3NxB Series (64-pin version)" },
1499 { 0x95, "ATSAM3NxC Series (100-pin version)" },
1500 { 0x98, "ATSAM3SDxA Series (48-pin version)" },
1501 { 0x99, "ATSAM3SDxB Series (64-pin version)" },
1502 { 0x9A, "ATSAM3SDxC Series (100-pin version)" },
1503 { 0xA5, "ATSAM5A" },
1504 { 0xF0, "AT75Cxx Series" },
1508 static const char *const nvptype[] = {
1510 "romless or onchip flash", /* 1 */
1511 "embedded flash memory",/* 2 */
1512 "rom(nvpsiz) + embedded flash (nvpsiz2)", /* 3 */
1513 "sram emulating flash", /* 4 */
1519 static const char *_yes_or_no(uint32_t v)
1527 static const char *const _rc_freq[] = {
1528 "4 MHz", "8 MHz", "12 MHz", "reserved"
1531 static void sam4_explain_ckgr_mor(struct sam4_chip *pChip)
1536 v = sam4_reg_fieldname(pChip, "MOSCXTEN", pChip->cfg.CKGR_MOR, 0, 1);
1537 LOG_USER("(main xtal enabled: %s)", _yes_or_no(v));
1538 v = sam4_reg_fieldname(pChip, "MOSCXTBY", pChip->cfg.CKGR_MOR, 1, 1);
1539 LOG_USER("(main osc bypass: %s)", _yes_or_no(v));
1540 rcen = sam4_reg_fieldname(pChip, "MOSCRCEN", pChip->cfg.CKGR_MOR, 3, 1);
1541 LOG_USER("(onchip RC-OSC enabled: %s)", _yes_or_no(rcen));
1542 v = sam4_reg_fieldname(pChip, "MOSCRCF", pChip->cfg.CKGR_MOR, 4, 3);
1543 LOG_USER("(onchip RC-OSC freq: %s)", _rc_freq[v]);
1545 pChip->cfg.rc_freq = 0;
1549 pChip->cfg.rc_freq = 0;
1552 pChip->cfg.rc_freq = 4 * 1000 * 1000;
1555 pChip->cfg.rc_freq = 8 * 1000 * 1000;
1558 pChip->cfg.rc_freq = 12 * 1000 * 1000;
1563 v = sam4_reg_fieldname(pChip, "MOSCXTST", pChip->cfg.CKGR_MOR, 8, 8);
1564 LOG_USER("(startup clks, time= %f uSecs)",
1565 ((float)(v * 1000000)) / ((float)(pChip->cfg.slow_freq)));
1566 v = sam4_reg_fieldname(pChip, "MOSCSEL", pChip->cfg.CKGR_MOR, 24, 1);
1567 LOG_USER("(mainosc source: %s)",
1568 v ? "external xtal" : "internal RC");
1570 v = sam4_reg_fieldname(pChip, "CFDEN", pChip->cfg.CKGR_MOR, 25, 1);
1571 LOG_USER("(clock failure enabled: %s)",
1575 static void sam4_explain_chipid_cidr(struct sam4_chip *pChip)
1581 sam4_reg_fieldname(pChip, "Version", pChip->cfg.CHIPID_CIDR, 0, 5);
1584 v = sam4_reg_fieldname(pChip, "EPROC", pChip->cfg.CHIPID_CIDR, 5, 3);
1585 LOG_USER("%s", eproc_names[v]);
1587 v = sam4_reg_fieldname(pChip, "NVPSIZE", pChip->cfg.CHIPID_CIDR, 8, 4);
1588 LOG_USER("%s", nvpsize[v]);
1590 v = sam4_reg_fieldname(pChip, "NVPSIZE2", pChip->cfg.CHIPID_CIDR, 12, 4);
1591 LOG_USER("%s", nvpsize2[v]);
1593 v = sam4_reg_fieldname(pChip, "SRAMSIZE", pChip->cfg.CHIPID_CIDR, 16, 4);
1594 LOG_USER("%s", sramsize[v]);
1596 v = sam4_reg_fieldname(pChip, "ARCH", pChip->cfg.CHIPID_CIDR, 20, 8);
1598 for (x = 0; archnames[x].name; x++) {
1599 if (v == archnames[x].value) {
1600 cp = archnames[x].name;
1607 v = sam4_reg_fieldname(pChip, "NVPTYP", pChip->cfg.CHIPID_CIDR, 28, 3);
1608 LOG_USER("%s", nvptype[v]);
1610 v = sam4_reg_fieldname(pChip, "EXTID", pChip->cfg.CHIPID_CIDR, 31, 1);
1611 LOG_USER("(exists: %s)", _yes_or_no(v));
1614 static void sam4_explain_ckgr_mcfr(struct sam4_chip *pChip)
1618 v = sam4_reg_fieldname(pChip, "MAINFRDY", pChip->cfg.CKGR_MCFR, 16, 1);
1619 LOG_USER("(main ready: %s)", _yes_or_no(v));
1621 v = sam4_reg_fieldname(pChip, "MAINF", pChip->cfg.CKGR_MCFR, 0, 16);
1623 v = (v * pChip->cfg.slow_freq) / 16;
1624 pChip->cfg.mainosc_freq = v;
1626 LOG_USER("(%3.03f Mhz (%" PRIu32 ".%03" PRIu32 "khz slowclk)",
1628 (uint32_t)(pChip->cfg.slow_freq / 1000),
1629 (uint32_t)(pChip->cfg.slow_freq % 1000));
1632 static void sam4_explain_ckgr_plla(struct sam4_chip *pChip)
1634 uint32_t mula, diva;
1636 diva = sam4_reg_fieldname(pChip, "DIVA", pChip->cfg.CKGR_PLLAR, 0, 8);
1638 mula = sam4_reg_fieldname(pChip, "MULA", pChip->cfg.CKGR_PLLAR, 16, 11);
1640 pChip->cfg.plla_freq = 0;
1642 LOG_USER("\tPLLA Freq: (Disabled,mula = 0)");
1644 LOG_USER("\tPLLA Freq: (Disabled,diva = 0)");
1645 else if (diva >= 1) {
1646 pChip->cfg.plla_freq = (pChip->cfg.mainosc_freq * (mula + 1) / diva);
1647 LOG_USER("\tPLLA Freq: %3.03f MHz",
1648 _tomhz(pChip->cfg.plla_freq));
1652 static void sam4_explain_mckr(struct sam4_chip *pChip)
1654 uint32_t css, pres, fin = 0;
1656 const char *cp = NULL;
1658 css = sam4_reg_fieldname(pChip, "CSS", pChip->cfg.PMC_MCKR, 0, 2);
1661 fin = pChip->cfg.slow_freq;
1665 fin = pChip->cfg.mainosc_freq;
1669 fin = pChip->cfg.plla_freq;
1673 if (pChip->cfg.CKGR_UCKR & (1 << 16)) {
1674 fin = 480 * 1000 * 1000;
1678 cp = "upll (*ERROR* UPLL is disabled)";
1686 LOG_USER("%s (%3.03f Mhz)",
1689 pres = sam4_reg_fieldname(pChip, "PRES", pChip->cfg.PMC_MCKR, 4, 3);
1690 switch (pres & 0x07) {
1693 cp = "selected clock";
1727 LOG_USER("(%s)", cp);
1729 /* sam4 has a *SINGLE* clock - */
1730 /* other at91 series parts have divisors for these. */
1731 pChip->cfg.cpu_freq = fin;
1732 pChip->cfg.mclk_freq = fin;
1733 pChip->cfg.fclk_freq = fin;
1734 LOG_USER("\t\tResult CPU Freq: %3.03f",
1739 static struct sam4_chip *target2sam4(struct target *pTarget)
1741 struct sam4_chip *pChip;
1743 if (pTarget == NULL)
1746 pChip = all_sam4_chips;
1748 if (pChip->target == pTarget)
1749 break; /* return below */
1751 pChip = pChip->next;
1757 static uint32_t *sam4_get_reg_ptr(struct sam4_cfg *pCfg, const struct sam4_reg_list *pList)
1759 /* this function exists to help */
1760 /* keep funky offsetof() errors */
1761 /* and casting from causing bugs */
1763 /* By using prototypes - we can detect what would */
1764 /* be casting errors. */
1766 return (uint32_t *)(void *)(((char *)(pCfg)) + pList->struct_offset);
1770 #define SAM4_ENTRY(NAME, FUNC) { .address = SAM4_ ## NAME, .struct_offset = offsetof( \
1772 NAME), # NAME, FUNC }
1773 static const struct sam4_reg_list sam4_all_regs[] = {
1774 SAM4_ENTRY(CKGR_MOR, sam4_explain_ckgr_mor),
1775 SAM4_ENTRY(CKGR_MCFR, sam4_explain_ckgr_mcfr),
1776 SAM4_ENTRY(CKGR_PLLAR, sam4_explain_ckgr_plla),
1777 SAM4_ENTRY(CKGR_UCKR, NULL),
1778 SAM4_ENTRY(PMC_FSMR, NULL),
1779 SAM4_ENTRY(PMC_FSPR, NULL),
1780 SAM4_ENTRY(PMC_IMR, NULL),
1781 SAM4_ENTRY(PMC_MCKR, sam4_explain_mckr),
1782 SAM4_ENTRY(PMC_PCK0, NULL),
1783 SAM4_ENTRY(PMC_PCK1, NULL),
1784 SAM4_ENTRY(PMC_PCK2, NULL),
1785 SAM4_ENTRY(PMC_PCSR, NULL),
1786 SAM4_ENTRY(PMC_SCSR, NULL),
1787 SAM4_ENTRY(PMC_SR, NULL),
1788 SAM4_ENTRY(CHIPID_CIDR, sam4_explain_chipid_cidr),
1789 SAM4_ENTRY(CHIPID_EXID, NULL),
1790 /* TERMINATE THE LIST */
1795 static struct sam4_bank_private *get_sam4_bank_private(struct flash_bank *bank)
1797 return bank->driver_priv;
1801 * Given a pointer to where it goes in the structure,
1802 * determine the register name, address from the all registers table.
1804 static const struct sam4_reg_list *sam4_GetReg(struct sam4_chip *pChip, uint32_t *goes_here)
1806 const struct sam4_reg_list *pReg;
1808 pReg = &(sam4_all_regs[0]);
1809 while (pReg->name) {
1810 uint32_t *pPossible;
1812 /* calculate where this one go.. */
1813 /* it is "possibly" this register. */
1815 pPossible = ((uint32_t *)(void *)(((char *)(&(pChip->cfg))) + pReg->struct_offset));
1817 /* well? Is it this register */
1818 if (pPossible == goes_here) {
1826 /* This is *TOTAL*PANIC* - we are totally screwed. */
1827 LOG_ERROR("INVALID SAM4 REGISTER");
1831 static int sam4_ReadThisReg(struct sam4_chip *pChip, uint32_t *goes_here)
1833 const struct sam4_reg_list *pReg;
1836 pReg = sam4_GetReg(pChip, goes_here);
1840 r = target_read_u32(pChip->target, pReg->address, goes_here);
1841 if (r != ERROR_OK) {
1842 LOG_ERROR("Cannot read SAM4 register: %s @ 0x%08x, Err: %d",
1843 pReg->name, (unsigned)(pReg->address), r);
1848 static int sam4_ReadAllRegs(struct sam4_chip *pChip)
1851 const struct sam4_reg_list *pReg;
1853 pReg = &(sam4_all_regs[0]);
1854 while (pReg->name) {
1855 r = sam4_ReadThisReg(pChip,
1856 sam4_get_reg_ptr(&(pChip->cfg), pReg));
1857 if (r != ERROR_OK) {
1858 LOG_ERROR("Cannot read SAM4 register: %s @ 0x%08x, Error: %d",
1859 pReg->name, ((unsigned)(pReg->address)), r);
1868 static int sam4_GetInfo(struct sam4_chip *pChip)
1870 const struct sam4_reg_list *pReg;
1873 pReg = &(sam4_all_regs[0]);
1874 while (pReg->name) {
1875 /* display all regs */
1876 LOG_DEBUG("Start: %s", pReg->name);
1877 regval = *sam4_get_reg_ptr(&(pChip->cfg), pReg);
1878 LOG_USER("%*s: [0x%08" PRIx32 "] -> 0x%08" PRIx32,
1883 if (pReg->explain_func)
1884 (*(pReg->explain_func))(pChip);
1885 LOG_DEBUG("End: %s", pReg->name);
1888 LOG_USER(" rc-osc: %3.03f MHz", _tomhz(pChip->cfg.rc_freq));
1889 LOG_USER(" mainosc: %3.03f MHz", _tomhz(pChip->cfg.mainosc_freq));
1890 LOG_USER(" plla: %3.03f MHz", _tomhz(pChip->cfg.plla_freq));
1891 LOG_USER(" cpu-freq: %3.03f MHz", _tomhz(pChip->cfg.cpu_freq));
1892 LOG_USER("mclk-freq: %3.03f MHz", _tomhz(pChip->cfg.mclk_freq));
1894 LOG_USER(" UniqueId: 0x%08" PRIx32 " 0x%08" PRIx32 " 0x%08" PRIx32 " 0x%08"PRIx32,
1895 pChip->cfg.unique_id[0],
1896 pChip->cfg.unique_id[1],
1897 pChip->cfg.unique_id[2],
1898 pChip->cfg.unique_id[3]);
1903 static int sam4_protect_check(struct flash_bank *bank)
1906 uint32_t v[4] = {0};
1908 struct sam4_bank_private *pPrivate;
1911 if (bank->target->state != TARGET_HALTED) {
1912 LOG_ERROR("Target not halted");
1913 return ERROR_TARGET_NOT_HALTED;
1916 pPrivate = get_sam4_bank_private(bank);
1918 LOG_ERROR("no private for this bank?");
1921 if (!(pPrivate->probed))
1922 return ERROR_FLASH_BANK_NOT_PROBED;
1924 r = FLASHD_GetLockBits(pPrivate, v);
1925 if (r != ERROR_OK) {
1926 LOG_DEBUG("Failed: %d", r);
1930 for (x = 0; x < pPrivate->nsectors; x++)
1931 bank->sectors[x].is_protected = (!!(v[x >> 5] & (1 << (x % 32))));
1936 FLASH_BANK_COMMAND_HANDLER(sam4_flash_bank_command)
1938 struct sam4_chip *pChip;
1940 pChip = all_sam4_chips;
1942 /* is this an existing chip? */
1944 if (pChip->target == bank->target)
1946 pChip = pChip->next;
1950 /* this is a *NEW* chip */
1951 pChip = calloc(1, sizeof(struct sam4_chip));
1953 LOG_ERROR("NO RAM!");
1956 pChip->target = bank->target;
1957 /* insert at head */
1958 pChip->next = all_sam4_chips;
1959 all_sam4_chips = pChip;
1960 pChip->target = bank->target;
1961 /* assumption is this runs at 32khz */
1962 pChip->cfg.slow_freq = 32768;
1966 switch (bank->base) {
1968 LOG_ERROR("Address 0x%08x invalid bank address (try 0x%08x"
1969 "[at91sam4s series] )",
1970 ((unsigned int)(bank->base)),
1971 ((unsigned int)(FLASH_BANK_BASE_S)));
1975 /* at91sam4s series only has bank 0*/
1976 /* at91sam4sd series has the same address for bank 0 (FLASH_BANK0_BASE_SD)*/
1977 case FLASH_BANK_BASE_S:
1978 bank->driver_priv = &(pChip->details.bank[0]);
1979 bank->bank_number = 0;
1980 pChip->details.bank[0].pChip = pChip;
1981 pChip->details.bank[0].pBank = bank;
1984 /* Bank 1 of at91sam4sd series */
1985 case FLASH_BANK1_BASE_1024K_SD:
1986 case FLASH_BANK1_BASE_2048K_SD:
1987 bank->driver_priv = &(pChip->details.bank[1]);
1988 bank->bank_number = 1;
1989 pChip->details.bank[1].pChip = pChip;
1990 pChip->details.bank[1].pBank = bank;
1994 /* we initialize after probing. */
1998 static int sam4_GetDetails(struct sam4_bank_private *pPrivate)
2000 const struct sam4_chip_details *pDetails;
2001 struct sam4_chip *pChip;
2002 struct flash_bank *saved_banks[SAM4_MAX_FLASH_BANKS];
2006 pDetails = all_sam4_details;
2007 while (pDetails->name) {
2008 /* Compare cidr without version bits */
2009 if (pDetails->chipid_cidr == (pPrivate->pChip->cfg.CHIPID_CIDR & 0xFFFFFFE0))
2014 if (pDetails->name == NULL) {
2015 LOG_ERROR("SAM4 ChipID 0x%08x not found in table (perhaps you can ID this chip?)",
2016 (unsigned int)(pPrivate->pChip->cfg.CHIPID_CIDR));
2017 /* Help the victim, print details about the chip */
2018 LOG_INFO("SAM4 CHIPID_CIDR: 0x%08" PRIx32 " decodes as follows",
2019 pPrivate->pChip->cfg.CHIPID_CIDR);
2020 sam4_explain_chipid_cidr(pPrivate->pChip);
2024 /* DANGER: THERE ARE DRAGONS HERE */
2026 /* get our pChip - it is going */
2027 /* to be over-written shortly */
2028 pChip = pPrivate->pChip;
2030 /* Note that, in reality: */
2032 /* pPrivate = &(pChip->details.bank[0]) */
2033 /* or pPrivate = &(pChip->details.bank[1]) */
2036 /* save the "bank" pointers */
2037 for (x = 0; x < SAM4_MAX_FLASH_BANKS; x++)
2038 saved_banks[x] = pChip->details.bank[x].pBank;
2040 /* Overwrite the "details" structure. */
2041 memcpy(&(pPrivate->pChip->details),
2043 sizeof(pPrivate->pChip->details));
2045 /* now fix the ghosted pointers */
2046 for (x = 0; x < SAM4_MAX_FLASH_BANKS; x++) {
2047 pChip->details.bank[x].pChip = pChip;
2048 pChip->details.bank[x].pBank = saved_banks[x];
2051 /* update the *BANK*SIZE* */
2057 static int _sam4_probe(struct flash_bank *bank, int noise)
2061 struct sam4_bank_private *pPrivate;
2064 LOG_DEBUG("Begin: Bank: %d, Noise: %d", bank->bank_number, noise);
2065 if (bank->target->state != TARGET_HALTED) {
2066 LOG_ERROR("Target not halted");
2067 return ERROR_TARGET_NOT_HALTED;
2070 pPrivate = get_sam4_bank_private(bank);
2072 LOG_ERROR("Invalid/unknown bank number");
2076 r = sam4_ReadAllRegs(pPrivate->pChip);
2081 if (pPrivate->pChip->probed)
2082 r = sam4_GetInfo(pPrivate->pChip);
2084 r = sam4_GetDetails(pPrivate);
2088 /* update the flash bank size */
2089 for (x = 0; x < SAM4_MAX_FLASH_BANKS; x++) {
2090 if (bank->base == pPrivate->pChip->details.bank[x].base_address) {
2091 bank->size = pPrivate->pChip->details.bank[x].size_bytes;
2096 if (bank->sectors == NULL) {
2097 bank->sectors = calloc(pPrivate->nsectors, (sizeof((bank->sectors)[0])));
2098 if (bank->sectors == NULL) {
2099 LOG_ERROR("No memory!");
2102 bank->num_sectors = pPrivate->nsectors;
2104 for (x = 0; ((int)(x)) < bank->num_sectors; x++) {
2105 bank->sectors[x].size = pPrivate->sector_size;
2106 bank->sectors[x].offset = x * (pPrivate->sector_size);
2107 /* mark as unknown */
2108 bank->sectors[x].is_erased = -1;
2109 bank->sectors[x].is_protected = -1;
2113 pPrivate->probed = 1;
2115 r = sam4_protect_check(bank);
2119 LOG_DEBUG("Bank = %d, nbanks = %d",
2120 pPrivate->bank_number, pPrivate->pChip->details.n_banks);
2121 if ((pPrivate->bank_number + 1) == pPrivate->pChip->details.n_banks) {
2122 /* read unique id, */
2123 /* it appears to be associated with the *last* flash bank. */
2124 FLASHD_ReadUniqueID(pPrivate);
2130 static int sam4_probe(struct flash_bank *bank)
2132 return _sam4_probe(bank, 1);
2135 static int sam4_auto_probe(struct flash_bank *bank)
2137 return _sam4_probe(bank, 0);
2140 static int sam4_erase(struct flash_bank *bank, int first, int last)
2142 struct sam4_bank_private *pPrivate;
2146 /*16 pages equals 8KB - Same size as a lock region*/
2151 if (bank->target->state != TARGET_HALTED) {
2152 LOG_ERROR("Target not halted");
2153 return ERROR_TARGET_NOT_HALTED;
2156 r = sam4_auto_probe(bank);
2157 if (r != ERROR_OK) {
2158 LOG_DEBUG("Here,r=%d", r);
2162 pPrivate = get_sam4_bank_private(bank);
2163 if (!(pPrivate->probed))
2164 return ERROR_FLASH_BANK_NOT_PROBED;
2166 if ((first == 0) && ((last + 1) == ((int)(pPrivate->nsectors)))) {
2169 return FLASHD_EraseEntireBank(pPrivate);
2171 LOG_INFO("sam4 does not auto-erase while programming (Erasing relevant sectors)");
2172 LOG_INFO("sam4 First: 0x%08x Last: 0x%08x", (unsigned int)(first), (unsigned int)(last));
2173 for (i = first; i <= last; i++) {
2174 /*16 pages equals 8KB - Same size as a lock region*/
2175 r = FLASHD_ErasePages(pPrivate, (i * pageCount), pageCount, &status);
2176 LOG_INFO("Erasing sector: 0x%08x", (unsigned int)(i));
2178 LOG_ERROR("SAM4: Error performing Erase page @ lock region number %d",
2180 if (status & (1 << 2)) {
2181 LOG_ERROR("SAM4: Lock Region %d is locked", (unsigned int)(i));
2184 if (status & (1 << 1)) {
2185 LOG_ERROR("SAM4: Flash Command error @lock region %d", (unsigned int)(i));
2193 static int sam4_protect(struct flash_bank *bank, int set, int first, int last)
2195 struct sam4_bank_private *pPrivate;
2199 if (bank->target->state != TARGET_HALTED) {
2200 LOG_ERROR("Target not halted");
2201 return ERROR_TARGET_NOT_HALTED;
2204 pPrivate = get_sam4_bank_private(bank);
2205 if (!(pPrivate->probed))
2206 return ERROR_FLASH_BANK_NOT_PROBED;
2209 r = FLASHD_Lock(pPrivate, (unsigned)(first), (unsigned)(last));
2211 r = FLASHD_Unlock(pPrivate, (unsigned)(first), (unsigned)(last));
2212 LOG_DEBUG("End: r=%d", r);
2218 static int sam4_page_read(struct sam4_bank_private *pPrivate, unsigned pagenum, uint8_t *buf)
2223 adr = pagenum * pPrivate->page_size;
2224 adr = adr + pPrivate->base_address;
2226 r = target_read_memory(pPrivate->pChip->target,
2228 4, /* THIS*MUST*BE* in 32bit values */
2229 pPrivate->page_size / 4,
2232 LOG_ERROR("SAM4: Flash program failed to read page phys address: 0x%08x",
2233 (unsigned int)(adr));
2237 static int sam4_page_write(struct sam4_bank_private *pPrivate, unsigned pagenum, const uint8_t *buf)
2241 uint32_t fmr; /* EEFC Flash Mode Register */
2244 adr = pagenum * pPrivate->page_size;
2245 adr = (adr + pPrivate->base_address);
2247 /* Get flash mode register value */
2248 r = target_read_u32(pPrivate->pChip->target, pPrivate->controller_address, &fmr);
2250 LOG_DEBUG("Error Read failed: read flash mode register");
2252 /* Clear flash wait state field */
2255 /* set FWS (flash wait states) field in the FMR (flash mode register) */
2256 fmr |= (pPrivate->flash_wait_states << 8);
2258 LOG_DEBUG("Flash Mode: 0x%08x", ((unsigned int)(fmr)));
2259 r = target_write_u32(pPrivate->pBank->target, pPrivate->controller_address, fmr);
2261 LOG_DEBUG("Error Write failed: set flash mode register");
2263 /* 1st sector 8kBytes - page 0 - 15*/
2264 /* 2nd sector 8kBytes - page 16 - 30*/
2265 /* 3rd sector 48kBytes - page 31 - 127*/
2266 LOG_DEBUG("Wr Page %u @ phys address: 0x%08x", pagenum, (unsigned int)(adr));
2267 r = target_write_memory(pPrivate->pChip->target,
2269 4, /* THIS*MUST*BE* in 32bit values */
2270 pPrivate->page_size / 4,
2272 if (r != ERROR_OK) {
2273 LOG_ERROR("SAM4: Failed to write (buffer) page at phys address 0x%08x",
2274 (unsigned int)(adr));
2278 r = EFC_PerformCommand(pPrivate,
2279 /* send Erase & Write Page */
2280 AT91C_EFC_FCMD_WP, /*AT91C_EFC_FCMD_EWP only works on first two 8kb sectors*/
2285 LOG_ERROR("SAM4: Error performing Write page @ phys address 0x%08x",
2286 (unsigned int)(adr));
2287 if (status & (1 << 2)) {
2288 LOG_ERROR("SAM4: Page @ Phys address 0x%08x is locked", (unsigned int)(adr));
2291 if (status & (1 << 1)) {
2292 LOG_ERROR("SAM4: Flash Command error @phys address 0x%08x", (unsigned int)(adr));
2298 static int sam4_write(struct flash_bank *bank,
2299 const uint8_t *buffer,
2307 unsigned page_offset;
2308 struct sam4_bank_private *pPrivate;
2309 uint8_t *pagebuffer;
2311 /* incase we bail further below, set this to null */
2314 /* ignore dumb requests */
2320 if (bank->target->state != TARGET_HALTED) {
2321 LOG_ERROR("Target not halted");
2322 r = ERROR_TARGET_NOT_HALTED;
2326 pPrivate = get_sam4_bank_private(bank);
2327 if (!(pPrivate->probed)) {
2328 r = ERROR_FLASH_BANK_NOT_PROBED;
2332 if ((offset + count) > pPrivate->size_bytes) {
2333 LOG_ERROR("Flash write error - past end of bank");
2334 LOG_ERROR(" offset: 0x%08x, count 0x%08x, BankEnd: 0x%08x",
2335 (unsigned int)(offset),
2336 (unsigned int)(count),
2337 (unsigned int)(pPrivate->size_bytes));
2342 pagebuffer = malloc(pPrivate->page_size);
2344 LOG_ERROR("No memory for %d Byte page buffer", (int)(pPrivate->page_size));
2349 /* what page do we start & end in? */
2350 page_cur = offset / pPrivate->page_size;
2351 page_end = (offset + count - 1) / pPrivate->page_size;
2353 LOG_DEBUG("Offset: 0x%08x, Count: 0x%08x", (unsigned int)(offset), (unsigned int)(count));
2354 LOG_DEBUG("Page start: %d, Page End: %d", (int)(page_cur), (int)(page_end));
2356 /* Special case: all one page */
2359 /* (1) non-aligned start */
2360 /* (2) body pages */
2361 /* (3) non-aligned end. */
2363 /* Handle special case - all one page. */
2364 if (page_cur == page_end) {
2365 LOG_DEBUG("Special case, all in one page");
2366 r = sam4_page_read(pPrivate, page_cur, pagebuffer);
2370 page_offset = (offset & (pPrivate->page_size-1));
2371 memcpy(pagebuffer + page_offset,
2375 r = sam4_page_write(pPrivate, page_cur, pagebuffer);
2382 /* non-aligned start */
2383 page_offset = offset & (pPrivate->page_size - 1);
2385 LOG_DEBUG("Not-Aligned start");
2386 /* read the partial */
2387 r = sam4_page_read(pPrivate, page_cur, pagebuffer);
2391 /* over-write with new data */
2392 n = (pPrivate->page_size - page_offset);
2393 memcpy(pagebuffer + page_offset,
2397 r = sam4_page_write(pPrivate, page_cur, pagebuffer);
2407 /* By checking that offset is correct here, we also
2408 fix a clang warning */
2409 assert(offset % pPrivate->page_size == 0);
2411 /* intermediate large pages */
2412 /* also - the final *terminal* */
2413 /* if that terminal page is a full page */
2414 LOG_DEBUG("Full Page Loop: cur=%d, end=%d, count = 0x%08x",
2415 (int)page_cur, (int)page_end, (unsigned int)(count));
2417 while ((page_cur < page_end) &&
2418 (count >= pPrivate->page_size)) {
2419 r = sam4_page_write(pPrivate, page_cur, buffer);
2422 count -= pPrivate->page_size;
2423 buffer += pPrivate->page_size;
2427 /* terminal partial page? */
2429 LOG_DEBUG("Terminal partial page, count = 0x%08x", (unsigned int)(count));
2430 /* we have a partial page */
2431 r = sam4_page_read(pPrivate, page_cur, pagebuffer);
2434 /* data goes at start */
2435 memcpy(pagebuffer, buffer, count);
2436 r = sam4_page_write(pPrivate, page_cur, pagebuffer);
2448 COMMAND_HANDLER(sam4_handle_info_command)
2450 struct sam4_chip *pChip;
2451 pChip = get_current_sam4(CMD_CTX);
2458 /* bank0 must exist before we can do anything */
2459 if (pChip->details.bank[0].pBank == NULL) {
2462 command_print(CMD_CTX,
2463 "Please define bank %d via command: flash bank %s ... ",
2465 at91sam4_flash.name);
2469 /* if bank 0 is not probed, then probe it */
2470 if (!(pChip->details.bank[0].probed)) {
2471 r = sam4_auto_probe(pChip->details.bank[0].pBank);
2475 /* above guarantees the "chip details" structure is valid */
2476 /* and thus, bank private areas are valid */
2477 /* and we have a SAM4 chip, what a concept! */
2479 /* auto-probe other banks, 0 done above */
2480 for (x = 1; x < SAM4_MAX_FLASH_BANKS; x++) {
2481 /* skip banks not present */
2482 if (!(pChip->details.bank[x].present))
2485 if (pChip->details.bank[x].pBank == NULL)
2488 if (pChip->details.bank[x].probed)
2491 r = sam4_auto_probe(pChip->details.bank[x].pBank);
2496 r = sam4_GetInfo(pChip);
2497 if (r != ERROR_OK) {
2498 LOG_DEBUG("Sam4Info, Failed %d", r);
2505 COMMAND_HANDLER(sam4_handle_gpnvm_command)
2509 struct sam4_chip *pChip;
2511 pChip = get_current_sam4(CMD_CTX);
2515 if (pChip->target->state != TARGET_HALTED) {
2516 LOG_ERROR("sam4 - target not halted");
2517 return ERROR_TARGET_NOT_HALTED;
2520 if (pChip->details.bank[0].pBank == NULL) {
2521 command_print(CMD_CTX, "Bank0 must be defined first via: flash bank %s ...",
2522 at91sam4_flash.name);
2525 if (!pChip->details.bank[0].probed) {
2526 r = sam4_auto_probe(pChip->details.bank[0].pBank);
2533 return ERROR_COMMAND_SYNTAX_ERROR;
2542 if ((0 == strcmp(CMD_ARGV[0], "show")) && (0 == strcmp(CMD_ARGV[1], "all")))
2546 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], v32);
2552 if (0 == strcmp("show", CMD_ARGV[0])) {
2556 for (x = 0; x < pChip->details.n_gpnvms; x++) {
2557 r = FLASHD_GetGPNVM(&(pChip->details.bank[0]), x, &v);
2560 command_print(CMD_CTX, "sam4-gpnvm%u: %u", x, v);
2564 if ((who >= 0) && (((unsigned)(who)) < pChip->details.n_gpnvms)) {
2565 r = FLASHD_GetGPNVM(&(pChip->details.bank[0]), who, &v);
2566 command_print(CMD_CTX, "sam4-gpnvm%u: %u", who, v);
2569 command_print(CMD_CTX, "sam4-gpnvm invalid GPNVM: %u", who);
2570 return ERROR_COMMAND_SYNTAX_ERROR;
2575 command_print(CMD_CTX, "Missing GPNVM number");
2576 return ERROR_COMMAND_SYNTAX_ERROR;
2579 if (0 == strcmp("set", CMD_ARGV[0]))
2580 r = FLASHD_SetGPNVM(&(pChip->details.bank[0]), who);
2581 else if ((0 == strcmp("clr", CMD_ARGV[0])) ||
2582 (0 == strcmp("clear", CMD_ARGV[0]))) /* quietly accept both */
2583 r = FLASHD_ClrGPNVM(&(pChip->details.bank[0]), who);
2585 command_print(CMD_CTX, "Unknown command: %s", CMD_ARGV[0]);
2586 r = ERROR_COMMAND_SYNTAX_ERROR;
2591 COMMAND_HANDLER(sam4_handle_slowclk_command)
2593 struct sam4_chip *pChip;
2595 pChip = get_current_sam4(CMD_CTX);
2607 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], v);
2609 /* absurd slow clock of 200Khz? */
2610 command_print(CMD_CTX, "Absurd/illegal slow clock freq: %d\n", (int)(v));
2611 return ERROR_COMMAND_SYNTAX_ERROR;
2613 pChip->cfg.slow_freq = v;
2618 command_print(CMD_CTX, "Too many parameters");
2619 return ERROR_COMMAND_SYNTAX_ERROR;
2622 command_print(CMD_CTX, "Slowclk freq: %d.%03dkhz",
2623 (int)(pChip->cfg.slow_freq / 1000),
2624 (int)(pChip->cfg.slow_freq % 1000));
2628 static const struct command_registration at91sam4_exec_command_handlers[] = {
2631 .handler = sam4_handle_gpnvm_command,
2632 .mode = COMMAND_EXEC,
2633 .usage = "[('clr'|'set'|'show') bitnum]",
2634 .help = "Without arguments, shows all bits in the gpnvm "
2635 "register. Otherwise, clears, sets, or shows one "
2636 "General Purpose Non-Volatile Memory (gpnvm) bit.",
2640 .handler = sam4_handle_info_command,
2641 .mode = COMMAND_EXEC,
2642 .help = "Print information about the current at91sam4 chip"
2643 "and its flash configuration.",
2647 .handler = sam4_handle_slowclk_command,
2648 .mode = COMMAND_EXEC,
2649 .usage = "[clock_hz]",
2650 .help = "Display or set the slowclock frequency "
2651 "(default 32768 Hz).",
2653 COMMAND_REGISTRATION_DONE
2655 static const struct command_registration at91sam4_command_handlers[] = {
2658 .mode = COMMAND_ANY,
2659 .help = "at91sam4 flash command group",
2661 .chain = at91sam4_exec_command_handlers,
2663 COMMAND_REGISTRATION_DONE
2666 struct flash_driver at91sam4_flash = {
2668 .commands = at91sam4_command_handlers,
2669 .flash_bank_command = sam4_flash_bank_command,
2670 .erase = sam4_erase,
2671 .protect = sam4_protect,
2672 .write = sam4_write,
2673 .read = default_flash_read,
2674 .probe = sam4_probe,
2675 .auto_probe = sam4_auto_probe,
2676 .erase_check = default_flash_blank_check,
2677 .protect_check = sam4_protect_check,