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, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ****************************************************************************/
27 /* Some of the the lower level code was based on code supplied by
28 * ATMEL under this copyright. */
30 /* BEGIN ATMEL COPYRIGHT */
31 /* ----------------------------------------------------------------------------
32 * ATMEL Microcontroller Software Support
33 * ----------------------------------------------------------------------------
34 * Copyright (c) 2009, Atmel Corporation
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
41 * - Redistributions of source code must retain the above copyright notice,
42 * this list of conditions and the disclaimer below.
44 * Atmel's name may not be used to endorse or promote products derived from
45 * this software without specific prior written permission.
47 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
49 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
50 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
53 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
54 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
56 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 * ----------------------------------------------------------------------------
59 /* END ATMEL COPYRIGHT */
66 #include <helper/time_support.h>
68 #define REG_NAME_WIDTH (12)
70 /* at91sam4s series (has always one flash bank)*/
71 #define FLASH_BANK_BASE_S 0x00400000
73 #define AT91C_EFC_FCMD_GETD (0x0) /* (EFC) Get Flash Descriptor */
74 #define AT91C_EFC_FCMD_WP (0x1) /* (EFC) Write Page */
75 #define AT91C_EFC_FCMD_WPL (0x2) /* (EFC) Write Page and Lock */
76 #define AT91C_EFC_FCMD_EWP (0x3) /* (EFC) Erase Page and Write Page */
77 #define AT91C_EFC_FCMD_EWPL (0x4) /* (EFC) Erase Page and Write Page
79 #define AT91C_EFC_FCMD_EA (0x5) /* (EFC) Erase All */
80 /* cmd6 is not present int he at91sam4u4/2/1 data sheet table 19-2 */
81 /* #define AT91C_EFC_FCMD_EPL (0x6) // (EFC) Erase plane? */
82 #define AT91C_EFC_FCMD_EPA (0x7) /* (EFC) Erase pages */
83 #define AT91C_EFC_FCMD_SLB (0x8) /* (EFC) Set Lock Bit */
84 #define AT91C_EFC_FCMD_CLB (0x9) /* (EFC) Clear Lock Bit */
85 #define AT91C_EFC_FCMD_GLB (0xA) /* (EFC) Get Lock Bit */
86 #define AT91C_EFC_FCMD_SFB (0xB) /* (EFC) Set Fuse Bit */
87 #define AT91C_EFC_FCMD_CFB (0xC) /* (EFC) Clear Fuse Bit */
88 #define AT91C_EFC_FCMD_GFB (0xD) /* (EFC) Get Fuse Bit */
89 #define AT91C_EFC_FCMD_STUI (0xE) /* (EFC) Start Read Unique ID */
90 #define AT91C_EFC_FCMD_SPUI (0xF) /* (EFC) Stop Read Unique ID */
92 #define offset_EFC_FMR 0
93 #define offset_EFC_FCR 4
94 #define offset_EFC_FSR 8
95 #define offset_EFC_FRR 12
97 extern struct flash_driver at91sam4_flash
;
99 static float _tomhz(uint32_t freq_hz
)
103 f
= ((float)(freq_hz
)) / 1000000.0;
107 /* How the chip is configured. */
109 uint32_t unique_id
[4];
113 uint32_t mainosc_freq
;
123 #define SAM4_CHIPID_CIDR (0x400E0740)
124 uint32_t CHIPID_CIDR
;
125 #define SAM4_CHIPID_EXID (0x400E0744)
126 uint32_t CHIPID_EXID
;
128 #define SAM4_PMC_BASE (0x400E0400)
129 #define SAM4_PMC_SCSR (SAM4_PMC_BASE + 0x0008)
131 #define SAM4_PMC_PCSR (SAM4_PMC_BASE + 0x0018)
133 #define SAM4_CKGR_UCKR (SAM4_PMC_BASE + 0x001c)
135 #define SAM4_CKGR_MOR (SAM4_PMC_BASE + 0x0020)
137 #define SAM4_CKGR_MCFR (SAM4_PMC_BASE + 0x0024)
139 #define SAM4_CKGR_PLLAR (SAM4_PMC_BASE + 0x0028)
141 #define SAM4_PMC_MCKR (SAM4_PMC_BASE + 0x0030)
143 #define SAM4_PMC_PCK0 (SAM4_PMC_BASE + 0x0040)
145 #define SAM4_PMC_PCK1 (SAM4_PMC_BASE + 0x0044)
147 #define SAM4_PMC_PCK2 (SAM4_PMC_BASE + 0x0048)
149 #define SAM4_PMC_SR (SAM4_PMC_BASE + 0x0068)
151 #define SAM4_PMC_IMR (SAM4_PMC_BASE + 0x006c)
153 #define SAM4_PMC_FSMR (SAM4_PMC_BASE + 0x0070)
155 #define SAM4_PMC_FSPR (SAM4_PMC_BASE + 0x0074)
159 struct sam4_bank_private
{
161 /* DANGER: THERE ARE DRAGONS HERE.. */
162 /* NOTE: If you add more 'ghost' pointers */
163 /* be aware that you must *manually* update */
164 /* these pointers in the function sam4_GetDetails() */
165 /* See the comment "Here there be dragons" */
167 /* so we can find the chip we belong to */
168 struct sam4_chip
*pChip
;
169 /* so we can find the orginal bank pointer */
170 struct flash_bank
*pBank
;
171 unsigned bank_number
;
172 uint32_t controller_address
;
173 uint32_t base_address
;
174 uint32_t flash_wait_states
;
178 unsigned sector_size
;
182 struct sam4_chip_details
{
183 /* THERE ARE DRAGONS HERE.. */
184 /* note: If you add pointers here */
185 /* becareful about them as they */
186 /* may need to be updated inside */
187 /* the function: "sam4_GetDetails() */
188 /* which copy/overwrites the */
189 /* 'runtime' copy of this structure */
190 uint32_t chipid_cidr
;
194 #define SAM4_N_NVM_BITS 3
195 unsigned gpnvm
[SAM4_N_NVM_BITS
];
196 unsigned total_flash_size
;
197 unsigned total_sram_size
;
199 #define SAM4_MAX_FLASH_BANKS 2
200 /* these are "initialized" from the global const data */
201 struct sam4_bank_private bank
[SAM4_MAX_FLASH_BANKS
];
205 struct sam4_chip
*next
;
208 /* this is "initialized" from the global const structure */
209 struct sam4_chip_details details
;
210 struct target
*target
;
215 struct sam4_reg_list
{
216 uint32_t address
; size_t struct_offset
; const char *name
;
217 void (*explain_func
)(struct sam4_chip
*pInfo
);
220 static struct sam4_chip
*all_sam4_chips
;
222 static struct sam4_chip
*get_current_sam4(struct command_context
*cmd_ctx
)
225 static struct sam4_chip
*p
;
227 t
= get_current_target(cmd_ctx
);
229 command_print(cmd_ctx
, "No current target?");
235 /* this should not happen */
236 /* the command is not registered until the chip is created? */
237 command_print(cmd_ctx
, "No SAM4 chips exist?");
246 command_print(cmd_ctx
, "Cannot find SAM4 chip?");
250 /*The actual sector size of the SAM4S flash memory is 65536 bytes. 16 sectors for a 1024KB device*/
251 /*The lockregions are 8KB per lock reqion, with a 1024KB device having 128 lock reqions. */
252 /*For the best results, nsectors are thus set to the amount of lock regions, and the sector_size*/
253 /*set to the lock region size. Page erases are used to erase 8KB sections when programming*/
255 /* these are used to *initialize* the "pChip->details" structure. */
256 static const struct sam4_chip_details all_sam4_details
[] = {
257 /* Start at91sam4s* series */
258 /*atsam4s16c - LQFP100/BGA100*/
260 .chipid_cidr
= 0x28AC0CE0,
261 .name
= "at91sam4s16c",
262 .total_flash_size
= 1024 * 1024,
263 .total_sram_size
= 128 * 1024,
273 .base_address
= FLASH_BANK_BASE_S
,
274 .controller_address
= 0x400e0a00,
275 .flash_wait_states
= 6, /* workaround silicon bug */
277 .size_bytes
= 1024 * 1024,
291 /*atsam4s16b - LQFP64/QFN64*/
293 .chipid_cidr
= 0x289C0CE0,
294 .name
= "at91sam4s16b",
295 .total_flash_size
= 1024 * 1024,
296 .total_sram_size
= 128 * 1024,
306 .base_address
= FLASH_BANK_BASE_S
,
307 .controller_address
= 0x400e0a00,
308 .flash_wait_states
= 6, /* workaround silicon bug */
310 .size_bytes
= 1024 * 1024,
324 /*atsam4s16a - LQFP48/QFN48*/
326 .chipid_cidr
= 0x288C0CE0,
327 .name
= "at91sam4s16a",
328 .total_flash_size
= 1024 * 1024,
329 .total_sram_size
= 128 * 1024,
339 .base_address
= FLASH_BANK_BASE_S
,
340 .controller_address
= 0x400e0a00,
341 .flash_wait_states
= 6, /* workaround silicon bug */
343 .size_bytes
= 1024 * 1024,
357 /*atsam4s8c - LQFP100/BGA100*/
359 .chipid_cidr
= 0x28AC0AE0,
360 .name
= "at91sam4s8c",
361 .total_flash_size
= 512 * 1024,
362 .total_sram_size
= 128 * 1024,
372 .base_address
= FLASH_BANK_BASE_S
,
373 .controller_address
= 0x400e0a00,
374 .flash_wait_states
= 6, /* workaround silicon bug */
376 .size_bytes
= 512 * 1024,
390 /*atsam4s8b - LQFP64/BGA64*/
392 .chipid_cidr
= 0x289C0AE0,
393 .name
= "at91sam4s8b",
394 .total_flash_size
= 512 * 1024,
395 .total_sram_size
= 128 * 1024,
405 .base_address
= FLASH_BANK_BASE_S
,
406 .controller_address
= 0x400e0a00,
407 .flash_wait_states
= 6, /* workaround silicon bug */
409 .size_bytes
= 512 * 1024,
423 /*atsam4s8a - LQFP48/BGA48*/
425 .chipid_cidr
= 0x288C0AE0,
426 .name
= "at91sam4s8a",
427 .total_flash_size
= 512 * 1024,
428 .total_sram_size
= 128 * 1024,
438 .base_address
= FLASH_BANK_BASE_S
,
439 .controller_address
= 0x400e0a00,
440 .flash_wait_states
= 6, /* workaround silicon bug */
442 .size_bytes
= 512 * 1024,
464 /***********************************************************************
465 **********************************************************************
466 **********************************************************************
467 **********************************************************************
468 **********************************************************************
469 **********************************************************************/
470 /* *ATMEL* style code - from the SAM4 driver code */
473 * Get the current status of the EEFC and
474 * the value of some status bits (LOCKE, PROGE).
475 * @param pPrivate - info about the bank
476 * @param v - result goes here
478 static int EFC_GetStatus(struct sam4_bank_private
*pPrivate
, uint32_t *v
)
481 r
= target_read_u32(pPrivate
->pChip
->target
,
482 pPrivate
->controller_address
+ offset_EFC_FSR
,
484 LOG_DEBUG("Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
486 ((unsigned int)((*v
>> 2) & 1)),
487 ((unsigned int)((*v
>> 1) & 1)),
488 ((unsigned int)((*v
>> 0) & 1)));
494 * Get the result of the last executed command.
495 * @param pPrivate - info about the bank
496 * @param v - result goes here
498 static int EFC_GetResult(struct sam4_bank_private
*pPrivate
, uint32_t *v
)
502 r
= target_read_u32(pPrivate
->pChip
->target
,
503 pPrivate
->controller_address
+ offset_EFC_FRR
,
507 LOG_DEBUG("Result: 0x%08x", ((unsigned int)(rv
)));
511 static int EFC_StartCommand(struct sam4_bank_private
*pPrivate
,
512 unsigned command
, unsigned argument
)
521 /* Check command & argument */
524 case AT91C_EFC_FCMD_WP
:
525 case AT91C_EFC_FCMD_WPL
:
526 case AT91C_EFC_FCMD_EWP
:
527 case AT91C_EFC_FCMD_EWPL
:
528 /* case AT91C_EFC_FCMD_EPL: */
529 case AT91C_EFC_FCMD_EPA
:
530 case AT91C_EFC_FCMD_SLB
:
531 case AT91C_EFC_FCMD_CLB
:
532 n
= (pPrivate
->size_bytes
/ pPrivate
->page_size
);
534 LOG_ERROR("*BUG*: Embedded flash has only %u pages", (unsigned)(n
));
537 case AT91C_EFC_FCMD_SFB
:
538 case AT91C_EFC_FCMD_CFB
:
539 if (argument
>= pPrivate
->pChip
->details
.n_gpnvms
) {
540 LOG_ERROR("*BUG*: Embedded flash has only %d GPNVMs",
541 pPrivate
->pChip
->details
.n_gpnvms
);
545 case AT91C_EFC_FCMD_GETD
:
546 case AT91C_EFC_FCMD_EA
:
547 case AT91C_EFC_FCMD_GLB
:
548 case AT91C_EFC_FCMD_GFB
:
549 case AT91C_EFC_FCMD_STUI
:
550 case AT91C_EFC_FCMD_SPUI
:
552 LOG_ERROR("Argument is meaningless for cmd: %d", command
);
555 LOG_ERROR("Unknown command %d", command
);
559 if (command
== AT91C_EFC_FCMD_SPUI
) {
560 /* this is a very special situation. */
561 /* Situation (1) - error/retry - see below */
562 /* And we are being called recursively */
563 /* Situation (2) - normal, finished reading unique id */
565 /* it should be "ready" */
566 EFC_GetStatus(pPrivate
, &v
);
568 /* then it is ready */
572 /* we have done this before */
573 /* the controller is not responding. */
574 LOG_ERROR("flash controller(%d) is not ready! Error",
575 pPrivate
->bank_number
);
579 LOG_ERROR("Flash controller(%d) is not ready, attempting reset",
580 pPrivate
->bank_number
);
581 /* we do that by issuing the *STOP* command */
582 EFC_StartCommand(pPrivate
, AT91C_EFC_FCMD_SPUI
, 0);
583 /* above is recursive, and further recursion is blocked by */
584 /* if (command == AT91C_EFC_FCMD_SPUI) above */
590 v
= (0x5A << 24) | (argument
<< 8) | command
;
591 LOG_DEBUG("Command: 0x%08x", ((unsigned int)(v
)));
592 r
= target_write_u32(pPrivate
->pBank
->target
,
593 pPrivate
->controller_address
+ offset_EFC_FCR
, v
);
595 LOG_DEBUG("Error Write failed");
600 * Performs the given command and wait until its completion (or an error).
601 * @param pPrivate - info about the bank
602 * @param command - Command to perform.
603 * @param argument - Optional command argument.
604 * @param status - put command status bits here
606 static int EFC_PerformCommand(struct sam4_bank_private
*pPrivate
,
614 long long ms_now
, ms_end
;
620 r
= EFC_StartCommand(pPrivate
, command
, argument
);
624 ms_end
= 10000 + timeval_ms();
627 r
= EFC_GetStatus(pPrivate
, &v
);
630 ms_now
= timeval_ms();
631 if (ms_now
> ms_end
) {
633 LOG_ERROR("Command timeout");
636 } while ((v
& 1) == 0);
646 * Read the unique ID.
647 * @param pPrivate - info about the bank
648 * The unique ID is stored in the 'pPrivate' structure.
650 static int FLASHD_ReadUniqueID(struct sam4_bank_private
*pPrivate
)
656 pPrivate
->pChip
->cfg
.unique_id
[0] = 0;
657 pPrivate
->pChip
->cfg
.unique_id
[1] = 0;
658 pPrivate
->pChip
->cfg
.unique_id
[2] = 0;
659 pPrivate
->pChip
->cfg
.unique_id
[3] = 0;
662 r
= EFC_StartCommand(pPrivate
, AT91C_EFC_FCMD_STUI
, 0);
666 for (x
= 0; x
< 4; x
++) {
667 r
= target_read_u32(pPrivate
->pChip
->target
,
668 pPrivate
->pBank
->base
+ (x
* 4),
672 pPrivate
->pChip
->cfg
.unique_id
[x
] = v
;
675 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_SPUI
, 0, NULL
);
676 LOG_DEBUG("End: R=%d, id = 0x%08x, 0x%08x, 0x%08x, 0x%08x",
678 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[0]),
679 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[1]),
680 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[2]),
681 (unsigned int)(pPrivate
->pChip
->cfg
.unique_id
[3]));
687 * Erases the entire flash.
688 * @param pPrivate - the info about the bank.
690 static int FLASHD_EraseEntireBank(struct sam4_bank_private
*pPrivate
)
693 return EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_EA
, 0, NULL
);
697 * Erases the entire flash.
698 * @param pPrivate - the info about the bank.
700 static int FLASHD_ErasePages(struct sam4_bank_private
*pPrivate
,
725 return EFC_PerformCommand(pPrivate
,
726 /* send Erase Page */
728 (firstPage
<< 2) | erasePages
,
733 * Gets current GPNVM state.
734 * @param pPrivate - info about the bank.
735 * @param gpnvm - GPNVM bit index.
736 * @param puthere - result stored here.
738 /* ------------------------------------------------------------------------------ */
739 static int FLASHD_GetGPNVM(struct sam4_bank_private
*pPrivate
, unsigned gpnvm
, unsigned *puthere
)
745 if (pPrivate
->bank_number
!= 0) {
746 LOG_ERROR("GPNVM only works with Bank0");
750 if (gpnvm
>= pPrivate
->pChip
->details
.n_gpnvms
) {
751 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
752 gpnvm
, pPrivate
->pChip
->details
.n_gpnvms
);
756 /* Get GPNVMs status */
757 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_GFB
, 0, NULL
);
763 r
= EFC_GetResult(pPrivate
, &v
);
766 /* Check if GPNVM is set */
767 /* get the bit and make it a 0/1 */
768 *puthere
= (v
>> gpnvm
) & 1;
775 * Clears the selected GPNVM bit.
776 * @param pPrivate info about the bank
777 * @param gpnvm GPNVM index.
778 * @returns 0 if successful; otherwise returns an error code.
780 static int FLASHD_ClrGPNVM(struct sam4_bank_private
*pPrivate
, unsigned gpnvm
)
786 if (pPrivate
->bank_number
!= 0) {
787 LOG_ERROR("GPNVM only works with Bank0");
791 if (gpnvm
>= pPrivate
->pChip
->details
.n_gpnvms
) {
792 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
793 gpnvm
, pPrivate
->pChip
->details
.n_gpnvms
);
797 r
= FLASHD_GetGPNVM(pPrivate
, gpnvm
, &v
);
799 LOG_DEBUG("Failed: %d", r
);
802 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_CFB
, gpnvm
, NULL
);
803 LOG_DEBUG("End: %d", r
);
808 * Sets the selected GPNVM bit.
809 * @param pPrivate info about the bank
810 * @param gpnvm GPNVM index.
812 static int FLASHD_SetGPNVM(struct sam4_bank_private
*pPrivate
, unsigned gpnvm
)
817 if (pPrivate
->bank_number
!= 0) {
818 LOG_ERROR("GPNVM only works with Bank0");
822 if (gpnvm
>= pPrivate
->pChip
->details
.n_gpnvms
) {
823 LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
824 gpnvm
, pPrivate
->pChip
->details
.n_gpnvms
);
828 r
= FLASHD_GetGPNVM(pPrivate
, gpnvm
, &v
);
836 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_SFB
, gpnvm
, NULL
);
842 * Returns a bit field (at most 64) of locked regions within a page.
843 * @param pPrivate info about the bank
844 * @param v where to store locked bits
846 static int FLASHD_GetLockBits(struct sam4_bank_private
*pPrivate
, uint32_t *v
)
850 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_GLB
, 0, NULL
);
852 EFC_GetResult(pPrivate
, v
);
853 EFC_GetResult(pPrivate
, v
);
854 EFC_GetResult(pPrivate
, v
);
855 r
= EFC_GetResult(pPrivate
, v
);
857 LOG_DEBUG("End: %d", r
);
862 * Unlocks all the regions in the given address range.
863 * @param pPrivate info about the bank
864 * @param start_sector first sector to unlock
865 * @param end_sector last (inclusive) to unlock
868 static int FLASHD_Unlock(struct sam4_bank_private
*pPrivate
,
869 unsigned start_sector
,
875 uint32_t pages_per_sector
;
877 pages_per_sector
= pPrivate
->sector_size
/ pPrivate
->page_size
;
879 /* Unlock all pages */
880 while (start_sector
<= end_sector
) {
881 pg
= start_sector
* pages_per_sector
;
883 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_CLB
, pg
, &status
);
894 * @param pPrivate - info about the bank
895 * @param start_sector - first sector to lock
896 * @param end_sector - last sector (inclusive) to lock
898 static int FLASHD_Lock(struct sam4_bank_private
*pPrivate
,
899 unsigned start_sector
,
904 uint32_t pages_per_sector
;
907 pages_per_sector
= pPrivate
->sector_size
/ pPrivate
->page_size
;
910 while (start_sector
<= end_sector
) {
911 pg
= start_sector
* pages_per_sector
;
913 r
= EFC_PerformCommand(pPrivate
, AT91C_EFC_FCMD_SLB
, pg
, &status
);
921 /****** END SAM4 CODE ********/
923 /* begin helpful debug code */
924 /* print the fieldname, the field value, in dec & hex, and return field value */
925 static uint32_t sam4_reg_fieldname(struct sam4_chip
*pChip
,
935 /* extract the field */
937 v
= v
& ((1 << width
)-1);
946 /* show the basics */
947 LOG_USER_N("\t%*s: %*d [0x%0*x] ",
948 REG_NAME_WIDTH
, regname
,
954 static const char _unknown
[] = "unknown";
955 static const char *const eproc_names
[] = {
974 #define nvpsize2 nvpsize /* these two tables are identical */
975 static const char *const nvpsize
[] = {
983 "128K bytes", /* 7 */
985 "256K bytes", /* 9 */
986 "512K bytes", /* 10 */
988 "1024K bytes", /* 12 */
990 "2048K bytes", /* 14 */
994 static const char *const sramsize
[] = {
999 "112K Bytes", /* 4 */
1001 "80K Bytes", /* 6 */
1002 "160K Bytes", /* 7 */
1004 "16K Bytes", /* 9 */
1005 "32K Bytes", /* 10 */
1006 "64K Bytes", /* 11 */
1007 "128K Bytes", /* 12 */
1008 "256K Bytes", /* 13 */
1009 "96K Bytes", /* 14 */
1010 "512K Bytes", /* 15 */
1014 static const struct archnames
{ unsigned value
; const char *name
; } archnames
[] = {
1015 { 0x19, "AT91SAM9xx Series" },
1016 { 0x29, "AT91SAM9XExx Series" },
1017 { 0x34, "AT91x34 Series" },
1018 { 0x37, "CAP7 Series" },
1019 { 0x39, "CAP9 Series" },
1020 { 0x3B, "CAP11 Series" },
1021 { 0x40, "AT91x40 Series" },
1022 { 0x42, "AT91x42 Series" },
1023 { 0x55, "AT91x55 Series" },
1024 { 0x60, "AT91SAM7Axx Series" },
1025 { 0x61, "AT91SAM7AQxx Series" },
1026 { 0x63, "AT91x63 Series" },
1027 { 0x70, "AT91SAM7Sxx Series" },
1028 { 0x71, "AT91SAM7XCxx Series" },
1029 { 0x72, "AT91SAM7SExx Series" },
1030 { 0x73, "AT91SAM7Lxx Series" },
1031 { 0x75, "AT91SAM7Xxx Series" },
1032 { 0x76, "AT91SAM7SLxx Series" },
1033 { 0x80, "ATSAM3UxC Series (100-pin version)" },
1034 { 0x81, "ATSAM3UxE Series (144-pin version)" },
1035 { 0x83, "ATSAM3A/SAM4A xC Series (100-pin version)"},
1036 { 0x84, "ATSAM3X/SAM4X xC Series (100-pin version)"},
1037 { 0x85, "ATSAM3X/SAM4X xE Series (144-pin version)"},
1038 { 0x86, "ATSAM3X/SAM4X xG Series (208/217-pin version)" },
1039 { 0x88, "ATSAM3S/SAM4S xA Series (48-pin version)" },
1040 { 0x89, "ATSAM3S/SAM4S xB Series (64-pin version)" },
1041 { 0x8A, "ATSAM3S/SAM4S xC Series (100-pin version)"},
1042 { 0x92, "AT91x92 Series" },
1043 { 0x93, "ATSAM3NxA Series (48-pin version)" },
1044 { 0x94, "ATSAM3NxB Series (64-pin version)" },
1045 { 0x95, "ATSAM3NxC Series (100-pin version)" },
1046 { 0x98, "ATSAM3SDxA Series (48-pin version)" },
1047 { 0x99, "ATSAM3SDxB Series (64-pin version)" },
1048 { 0x9A, "ATSAM3SDxC Series (100-pin version)" },
1049 { 0xA5, "ATSAM5A" },
1050 { 0xF0, "AT75Cxx Series" },
1054 static const char *const nvptype
[] = {
1056 "romless or onchip flash", /* 1 */
1057 "embedded flash memory",/* 2 */
1058 "rom(nvpsiz) + embedded flash (nvpsiz2)", /* 3 */
1059 "sram emulating flash", /* 4 */
1065 static const char *_yes_or_no(uint32_t v
)
1073 static const char *const _rc_freq
[] = {
1074 "4 MHz", "8 MHz", "12 MHz", "reserved"
1077 static void sam4_explain_ckgr_mor(struct sam4_chip
*pChip
)
1082 v
= sam4_reg_fieldname(pChip
, "MOSCXTEN", pChip
->cfg
.CKGR_MOR
, 0, 1);
1083 LOG_USER("(main xtal enabled: %s)", _yes_or_no(v
));
1084 v
= sam4_reg_fieldname(pChip
, "MOSCXTBY", pChip
->cfg
.CKGR_MOR
, 1, 1);
1085 LOG_USER("(main osc bypass: %s)", _yes_or_no(v
));
1086 rcen
= sam4_reg_fieldname(pChip
, "MOSCRCEN", pChip
->cfg
.CKGR_MOR
, 3, 1);
1087 LOG_USER("(onchip RC-OSC enabled: %s)", _yes_or_no(rcen
));
1088 v
= sam4_reg_fieldname(pChip
, "MOSCRCF", pChip
->cfg
.CKGR_MOR
, 4, 3);
1089 LOG_USER("(onchip RC-OSC freq: %s)", _rc_freq
[v
]);
1091 pChip
->cfg
.rc_freq
= 0;
1095 pChip
->cfg
.rc_freq
= 0;
1098 pChip
->cfg
.rc_freq
= 4 * 1000 * 1000;
1101 pChip
->cfg
.rc_freq
= 8 * 1000 * 1000;
1104 pChip
->cfg
.rc_freq
= 12 * 1000 * 1000;
1109 v
= sam4_reg_fieldname(pChip
, "MOSCXTST", pChip
->cfg
.CKGR_MOR
, 8, 8);
1110 LOG_USER("(startup clks, time= %f uSecs)",
1111 ((float)(v
* 1000000)) / ((float)(pChip
->cfg
.slow_freq
)));
1112 v
= sam4_reg_fieldname(pChip
, "MOSCSEL", pChip
->cfg
.CKGR_MOR
, 24, 1);
1113 LOG_USER("(mainosc source: %s)",
1114 v
? "external xtal" : "internal RC");
1116 v
= sam4_reg_fieldname(pChip
, "CFDEN", pChip
->cfg
.CKGR_MOR
, 25, 1);
1117 LOG_USER("(clock failure enabled: %s)",
1121 static void sam4_explain_chipid_cidr(struct sam4_chip
*pChip
)
1127 sam4_reg_fieldname(pChip
, "Version", pChip
->cfg
.CHIPID_CIDR
, 0, 5);
1130 v
= sam4_reg_fieldname(pChip
, "EPROC", pChip
->cfg
.CHIPID_CIDR
, 5, 3);
1131 LOG_USER("%s", eproc_names
[v
]);
1133 v
= sam4_reg_fieldname(pChip
, "NVPSIZE", pChip
->cfg
.CHIPID_CIDR
, 8, 4);
1134 LOG_USER("%s", nvpsize
[v
]);
1136 v
= sam4_reg_fieldname(pChip
, "NVPSIZE2", pChip
->cfg
.CHIPID_CIDR
, 12, 4);
1137 LOG_USER("%s", nvpsize2
[v
]);
1139 v
= sam4_reg_fieldname(pChip
, "SRAMSIZE", pChip
->cfg
.CHIPID_CIDR
, 16, 4);
1140 LOG_USER("%s", sramsize
[v
]);
1142 v
= sam4_reg_fieldname(pChip
, "ARCH", pChip
->cfg
.CHIPID_CIDR
, 20, 8);
1144 for (x
= 0; archnames
[x
].name
; x
++) {
1145 if (v
== archnames
[x
].value
) {
1146 cp
= archnames
[x
].name
;
1153 v
= sam4_reg_fieldname(pChip
, "NVPTYP", pChip
->cfg
.CHIPID_CIDR
, 28, 3);
1154 LOG_USER("%s", nvptype
[v
]);
1156 v
= sam4_reg_fieldname(pChip
, "EXTID", pChip
->cfg
.CHIPID_CIDR
, 31, 1);
1157 LOG_USER("(exists: %s)", _yes_or_no(v
));
1160 static void sam4_explain_ckgr_mcfr(struct sam4_chip
*pChip
)
1164 v
= sam4_reg_fieldname(pChip
, "MAINFRDY", pChip
->cfg
.CKGR_MCFR
, 16, 1);
1165 LOG_USER("(main ready: %s)", _yes_or_no(v
));
1167 v
= sam4_reg_fieldname(pChip
, "MAINF", pChip
->cfg
.CKGR_MCFR
, 0, 16);
1169 v
= (v
* pChip
->cfg
.slow_freq
) / 16;
1170 pChip
->cfg
.mainosc_freq
= v
;
1172 LOG_USER("(%3.03f Mhz (%d.%03dkhz slowclk)",
1174 pChip
->cfg
.slow_freq
/ 1000,
1175 pChip
->cfg
.slow_freq
% 1000);
1178 static void sam4_explain_ckgr_plla(struct sam4_chip
*pChip
)
1180 uint32_t mula
, diva
;
1182 diva
= sam4_reg_fieldname(pChip
, "DIVA", pChip
->cfg
.CKGR_PLLAR
, 0, 8);
1184 mula
= sam4_reg_fieldname(pChip
, "MULA", pChip
->cfg
.CKGR_PLLAR
, 16, 11);
1186 pChip
->cfg
.plla_freq
= 0;
1188 LOG_USER("\tPLLA Freq: (Disabled,mula = 0)");
1190 LOG_USER("\tPLLA Freq: (Disabled,diva = 0)");
1191 else if (diva
== 1) {
1192 pChip
->cfg
.plla_freq
= (pChip
->cfg
.mainosc_freq
* (mula
+ 1));
1193 LOG_USER("\tPLLA Freq: %3.03f MHz",
1194 _tomhz(pChip
->cfg
.plla_freq
));
1198 static void sam4_explain_mckr(struct sam4_chip
*pChip
)
1200 uint32_t css
, pres
, fin
= 0;
1202 const char *cp
= NULL
;
1204 css
= sam4_reg_fieldname(pChip
, "CSS", pChip
->cfg
.PMC_MCKR
, 0, 2);
1207 fin
= pChip
->cfg
.slow_freq
;
1211 fin
= pChip
->cfg
.mainosc_freq
;
1215 fin
= pChip
->cfg
.plla_freq
;
1219 if (pChip
->cfg
.CKGR_UCKR
& (1 << 16)) {
1220 fin
= 480 * 1000 * 1000;
1224 cp
= "upll (*ERROR* UPLL is disabled)";
1232 LOG_USER("%s (%3.03f Mhz)",
1235 pres
= sam4_reg_fieldname(pChip
, "PRES", pChip
->cfg
.PMC_MCKR
, 4, 3);
1236 switch (pres
& 0x07) {
1239 cp
= "selected clock";
1273 LOG_USER("(%s)", cp
);
1275 /* sam4 has a *SINGLE* clock - */
1276 /* other at91 series parts have divisors for these. */
1277 pChip
->cfg
.cpu_freq
= fin
;
1278 pChip
->cfg
.mclk_freq
= fin
;
1279 pChip
->cfg
.fclk_freq
= fin
;
1280 LOG_USER("\t\tResult CPU Freq: %3.03f",
1285 static struct sam4_chip
*target2sam4(struct target
*pTarget
)
1287 struct sam4_chip
*pChip
;
1289 if (pTarget
== NULL
)
1292 pChip
= all_sam4_chips
;
1294 if (pChip
->target
== pTarget
)
1295 break; /* return below */
1297 pChip
= pChip
->next
;
1303 static uint32_t *sam4_get_reg_ptr(struct sam4_cfg
*pCfg
, const struct sam4_reg_list
*pList
)
1305 /* this function exists to help */
1306 /* keep funky offsetof() errors */
1307 /* and casting from causing bugs */
1309 /* By using prototypes - we can detect what would */
1310 /* be casting errors. */
1312 return (uint32_t *)(void *)(((char *)(pCfg
)) + pList
->struct_offset
);
1316 #define SAM4_ENTRY(NAME, FUNC) { .address = SAM4_ ## NAME, .struct_offset = offsetof( \
1318 NAME), # NAME, FUNC }
1319 static const struct sam4_reg_list sam4_all_regs
[] = {
1320 SAM4_ENTRY(CKGR_MOR
, sam4_explain_ckgr_mor
),
1321 SAM4_ENTRY(CKGR_MCFR
, sam4_explain_ckgr_mcfr
),
1322 SAM4_ENTRY(CKGR_PLLAR
, sam4_explain_ckgr_plla
),
1323 SAM4_ENTRY(CKGR_UCKR
, NULL
),
1324 SAM4_ENTRY(PMC_FSMR
, NULL
),
1325 SAM4_ENTRY(PMC_FSPR
, NULL
),
1326 SAM4_ENTRY(PMC_IMR
, NULL
),
1327 SAM4_ENTRY(PMC_MCKR
, sam4_explain_mckr
),
1328 SAM4_ENTRY(PMC_PCK0
, NULL
),
1329 SAM4_ENTRY(PMC_PCK1
, NULL
),
1330 SAM4_ENTRY(PMC_PCK2
, NULL
),
1331 SAM4_ENTRY(PMC_PCSR
, NULL
),
1332 SAM4_ENTRY(PMC_SCSR
, NULL
),
1333 SAM4_ENTRY(PMC_SR
, NULL
),
1334 SAM4_ENTRY(CHIPID_CIDR
, sam4_explain_chipid_cidr
),
1335 SAM4_ENTRY(CHIPID_EXID
, NULL
),
1336 /* TERMINATE THE LIST */
1341 static struct sam4_bank_private
*get_sam4_bank_private(struct flash_bank
*bank
)
1343 return (struct sam4_bank_private
*)(bank
->driver_priv
);
1347 * Given a pointer to where it goes in the structure,
1348 * determine the register name, address from the all registers table.
1350 static const struct sam4_reg_list
*sam4_GetReg(struct sam4_chip
*pChip
, uint32_t *goes_here
)
1352 const struct sam4_reg_list
*pReg
;
1354 pReg
= &(sam4_all_regs
[0]);
1355 while (pReg
->name
) {
1356 uint32_t *pPossible
;
1358 /* calculate where this one go.. */
1359 /* it is "possibly" this register. */
1361 pPossible
= ((uint32_t *)(void *)(((char *)(&(pChip
->cfg
))) + pReg
->struct_offset
));
1363 /* well? Is it this register */
1364 if (pPossible
== goes_here
) {
1372 /* This is *TOTAL*PANIC* - we are totally screwed. */
1373 LOG_ERROR("INVALID SAM4 REGISTER");
1377 static int sam4_ReadThisReg(struct sam4_chip
*pChip
, uint32_t *goes_here
)
1379 const struct sam4_reg_list
*pReg
;
1382 pReg
= sam4_GetReg(pChip
, goes_here
);
1386 r
= target_read_u32(pChip
->target
, pReg
->address
, goes_here
);
1387 if (r
!= ERROR_OK
) {
1388 LOG_ERROR("Cannot read SAM4 register: %s @ 0x%08x, Err: %d",
1389 pReg
->name
, (unsigned)(pReg
->address
), r
);
1394 static int sam4_ReadAllRegs(struct sam4_chip
*pChip
)
1397 const struct sam4_reg_list
*pReg
;
1399 pReg
= &(sam4_all_regs
[0]);
1400 while (pReg
->name
) {
1401 r
= sam4_ReadThisReg(pChip
,
1402 sam4_get_reg_ptr(&(pChip
->cfg
), pReg
));
1403 if (r
!= ERROR_OK
) {
1404 LOG_ERROR("Cannot read SAM4 registere: %s @ 0x%08x, Error: %d",
1405 pReg
->name
, ((unsigned)(pReg
->address
)), r
);
1414 static int sam4_GetInfo(struct sam4_chip
*pChip
)
1416 const struct sam4_reg_list
*pReg
;
1419 pReg
= &(sam4_all_regs
[0]);
1420 while (pReg
->name
) {
1421 /* display all regs */
1422 LOG_DEBUG("Start: %s", pReg
->name
);
1423 regval
= *sam4_get_reg_ptr(&(pChip
->cfg
), pReg
);
1424 LOG_USER("%*s: [0x%08x] -> 0x%08x",
1429 if (pReg
->explain_func
)
1430 (*(pReg
->explain_func
))(pChip
);
1431 LOG_DEBUG("End: %s", pReg
->name
);
1434 LOG_USER(" rc-osc: %3.03f MHz", _tomhz(pChip
->cfg
.rc_freq
));
1435 LOG_USER(" mainosc: %3.03f MHz", _tomhz(pChip
->cfg
.mainosc_freq
));
1436 LOG_USER(" plla: %3.03f MHz", _tomhz(pChip
->cfg
.plla_freq
));
1437 LOG_USER(" cpu-freq: %3.03f MHz", _tomhz(pChip
->cfg
.cpu_freq
));
1438 LOG_USER("mclk-freq: %3.03f MHz", _tomhz(pChip
->cfg
.mclk_freq
));
1440 LOG_USER(" UniqueId: 0x%08x 0x%08x 0x%08x 0x%08x",
1441 pChip
->cfg
.unique_id
[0],
1442 pChip
->cfg
.unique_id
[1],
1443 pChip
->cfg
.unique_id
[2],
1444 pChip
->cfg
.unique_id
[3]);
1449 static int sam4_protect_check(struct flash_bank
*bank
)
1452 uint32_t v
[4] = {0};
1454 struct sam4_bank_private
*pPrivate
;
1457 if (bank
->target
->state
!= TARGET_HALTED
) {
1458 LOG_ERROR("Target not halted");
1459 return ERROR_TARGET_NOT_HALTED
;
1462 pPrivate
= get_sam4_bank_private(bank
);
1464 LOG_ERROR("no private for this bank?");
1467 if (!(pPrivate
->probed
))
1468 return ERROR_FLASH_BANK_NOT_PROBED
;
1470 r
= FLASHD_GetLockBits(pPrivate
, v
);
1471 if (r
!= ERROR_OK
) {
1472 LOG_DEBUG("Failed: %d", r
);
1476 for (x
= 0; x
< pPrivate
->nsectors
; x
++)
1477 bank
->sectors
[x
].is_protected
= (!!(v
[x
>> 5] & (1 << (x
% 32))));
1482 FLASH_BANK_COMMAND_HANDLER(sam4_flash_bank_command
)
1484 struct sam4_chip
*pChip
;
1486 pChip
= all_sam4_chips
;
1488 /* is this an existing chip? */
1490 if (pChip
->target
== bank
->target
)
1492 pChip
= pChip
->next
;
1496 /* this is a *NEW* chip */
1497 pChip
= calloc(1, sizeof(struct sam4_chip
));
1499 LOG_ERROR("NO RAM!");
1502 pChip
->target
= bank
->target
;
1503 /* insert at head */
1504 pChip
->next
= all_sam4_chips
;
1505 all_sam4_chips
= pChip
;
1506 pChip
->target
= bank
->target
;
1507 /* assumption is this runs at 32khz */
1508 pChip
->cfg
.slow_freq
= 32768;
1512 switch (bank
->base
) {
1514 LOG_ERROR("Address 0x%08x invalid bank address (try 0x%08x"
1515 "[at91sam4s series] )",
1516 ((unsigned int)(bank
->base
)),
1517 ((unsigned int)(FLASH_BANK_BASE_S
)));
1521 /* at91sam4s series only has bank 0*/
1522 case FLASH_BANK_BASE_S
:
1523 bank
->driver_priv
= &(pChip
->details
.bank
[0]);
1524 bank
->bank_number
= 0;
1525 pChip
->details
.bank
[0].pChip
= pChip
;
1526 pChip
->details
.bank
[0].pBank
= bank
;
1530 /* we initialize after probing. */
1534 static int sam4_GetDetails(struct sam4_bank_private
*pPrivate
)
1536 const struct sam4_chip_details
*pDetails
;
1537 struct sam4_chip
*pChip
;
1538 struct flash_bank
*saved_banks
[SAM4_MAX_FLASH_BANKS
];
1542 pDetails
= all_sam4_details
;
1543 while (pDetails
->name
) {
1544 /* Compare cidr without version bits */
1545 if (pDetails
->chipid_cidr
== (pPrivate
->pChip
->cfg
.CHIPID_CIDR
& 0xFFFFFFE0))
1550 if (pDetails
->name
== NULL
) {
1551 LOG_ERROR("SAM4 ChipID 0x%08x not found in table (perhaps you can ID this chip?)",
1552 (unsigned int)(pPrivate
->pChip
->cfg
.CHIPID_CIDR
));
1553 /* Help the victim, print details about the chip */
1554 LOG_INFO("SAM4 CHIPID_CIDR: 0x%08x decodes as follows",
1555 pPrivate
->pChip
->cfg
.CHIPID_CIDR
);
1556 sam4_explain_chipid_cidr(pPrivate
->pChip
);
1560 /* DANGER: THERE ARE DRAGONS HERE */
1562 /* get our pChip - it is going */
1563 /* to be over-written shortly */
1564 pChip
= pPrivate
->pChip
;
1566 /* Note that, in reality: */
1568 /* pPrivate = &(pChip->details.bank[0]) */
1569 /* or pPrivate = &(pChip->details.bank[1]) */
1572 /* save the "bank" pointers */
1573 for (x
= 0; x
< SAM4_MAX_FLASH_BANKS
; x
++)
1574 saved_banks
[x
] = pChip
->details
.bank
[x
].pBank
;
1576 /* Overwrite the "details" structure. */
1577 memcpy(&(pPrivate
->pChip
->details
),
1579 sizeof(pPrivate
->pChip
->details
));
1581 /* now fix the ghosted pointers */
1582 for (x
= 0; x
< SAM4_MAX_FLASH_BANKS
; x
++) {
1583 pChip
->details
.bank
[x
].pChip
= pChip
;
1584 pChip
->details
.bank
[x
].pBank
= saved_banks
[x
];
1587 /* update the *BANK*SIZE* */
1593 static int _sam4_probe(struct flash_bank
*bank
, int noise
)
1597 struct sam4_bank_private
*pPrivate
;
1600 LOG_DEBUG("Begin: Bank: %d, Noise: %d", bank
->bank_number
, noise
);
1601 if (bank
->target
->state
!= TARGET_HALTED
) {
1602 LOG_ERROR("Target not halted");
1603 return ERROR_TARGET_NOT_HALTED
;
1606 pPrivate
= get_sam4_bank_private(bank
);
1608 LOG_ERROR("Invalid/unknown bank number");
1612 r
= sam4_ReadAllRegs(pPrivate
->pChip
);
1617 if (pPrivate
->pChip
->probed
)
1618 r
= sam4_GetInfo(pPrivate
->pChip
);
1620 r
= sam4_GetDetails(pPrivate
);
1624 /* update the flash bank size */
1625 for (x
= 0; x
< SAM4_MAX_FLASH_BANKS
; x
++) {
1626 if (bank
->base
== pPrivate
->pChip
->details
.bank
[x
].base_address
) {
1627 bank
->size
= pPrivate
->pChip
->details
.bank
[x
].size_bytes
;
1632 if (bank
->sectors
== NULL
) {
1633 bank
->sectors
= calloc(pPrivate
->nsectors
, (sizeof((bank
->sectors
)[0])));
1634 if (bank
->sectors
== NULL
) {
1635 LOG_ERROR("No memory!");
1638 bank
->num_sectors
= pPrivate
->nsectors
;
1640 for (x
= 0; ((int)(x
)) < bank
->num_sectors
; x
++) {
1641 bank
->sectors
[x
].size
= pPrivate
->sector_size
;
1642 bank
->sectors
[x
].offset
= x
* (pPrivate
->sector_size
);
1643 /* mark as unknown */
1644 bank
->sectors
[x
].is_erased
= -1;
1645 bank
->sectors
[x
].is_protected
= -1;
1649 pPrivate
->probed
= 1;
1651 r
= sam4_protect_check(bank
);
1655 LOG_DEBUG("Bank = %d, nbanks = %d",
1656 pPrivate
->bank_number
, pPrivate
->pChip
->details
.n_banks
);
1657 if ((pPrivate
->bank_number
+ 1) == pPrivate
->pChip
->details
.n_banks
) {
1658 /* read unique id, */
1659 /* it appears to be associated with the *last* flash bank. */
1660 FLASHD_ReadUniqueID(pPrivate
);
1666 static int sam4_probe(struct flash_bank
*bank
)
1668 return _sam4_probe(bank
, 1);
1671 static int sam4_auto_probe(struct flash_bank
*bank
)
1673 return _sam4_probe(bank
, 0);
1676 static int sam4_erase(struct flash_bank
*bank
, int first
, int last
)
1678 struct sam4_bank_private
*pPrivate
;
1682 /*16 pages equals 8KB - Same size as a lock region*/
1687 if (bank
->target
->state
!= TARGET_HALTED
) {
1688 LOG_ERROR("Target not halted");
1689 return ERROR_TARGET_NOT_HALTED
;
1692 r
= sam4_auto_probe(bank
);
1693 if (r
!= ERROR_OK
) {
1694 LOG_DEBUG("Here,r=%d", r
);
1698 pPrivate
= get_sam4_bank_private(bank
);
1699 if (!(pPrivate
->probed
))
1700 return ERROR_FLASH_BANK_NOT_PROBED
;
1702 if ((first
== 0) && ((last
+ 1) == ((int)(pPrivate
->nsectors
)))) {
1705 return FLASHD_EraseEntireBank(pPrivate
);
1707 LOG_INFO("sam4 does not auto-erase while programing (Erasing relevant sectors)");
1708 LOG_INFO("sam4 First: 0x%08x Last: 0x%08x", (unsigned int)(first
), (unsigned int)(last
));
1709 for (i
= first
; i
<= last
; i
++) {
1710 /*16 pages equals 8KB - Same size as a lock region*/
1711 r
= FLASHD_ErasePages(pPrivate
, (i
* pageCount
), pageCount
, &status
);
1712 LOG_INFO("Erasing sector: 0x%08x", (unsigned int)(i
));
1714 LOG_ERROR("SAM4: Error performing Erase page @ lock region number %d",
1716 if (status
& (1 << 2)) {
1717 LOG_ERROR("SAM4: Lock Reqion %d is locked", (unsigned int)(i
));
1720 if (status
& (1 << 1)) {
1721 LOG_ERROR("SAM4: Flash Command error @lock region %d", (unsigned int)(i
));
1729 static int sam4_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
1731 struct sam4_bank_private
*pPrivate
;
1735 if (bank
->target
->state
!= TARGET_HALTED
) {
1736 LOG_ERROR("Target not halted");
1737 return ERROR_TARGET_NOT_HALTED
;
1740 pPrivate
= get_sam4_bank_private(bank
);
1741 if (!(pPrivate
->probed
))
1742 return ERROR_FLASH_BANK_NOT_PROBED
;
1745 r
= FLASHD_Lock(pPrivate
, (unsigned)(first
), (unsigned)(last
));
1747 r
= FLASHD_Unlock(pPrivate
, (unsigned)(first
), (unsigned)(last
));
1748 LOG_DEBUG("End: r=%d", r
);
1754 static int sam4_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
1756 if (bank
->target
->state
!= TARGET_HALTED
) {
1757 LOG_ERROR("Target not halted");
1758 return ERROR_TARGET_NOT_HALTED
;
1764 static int sam4_page_read(struct sam4_bank_private
*pPrivate
, unsigned pagenum
, uint8_t *buf
)
1769 adr
= pagenum
* pPrivate
->page_size
;
1770 adr
= adr
+ pPrivate
->base_address
;
1772 r
= target_read_memory(pPrivate
->pChip
->target
,
1774 4, /* THIS*MUST*BE* in 32bit values */
1775 pPrivate
->page_size
/ 4,
1778 LOG_ERROR("SAM4: Flash program failed to read page phys address: 0x%08x",
1779 (unsigned int)(adr
));
1783 /* The code below is basically this: */
1785 /* arm-none-eabi-gcc -mthumb -mcpu = cortex-m3 -O9 -S ./foobar.c -o foobar.s */
1787 /* Only the *CPU* can write to the flash buffer. */
1788 /* the DAP cannot... so - we download this 28byte thing */
1789 /* Run the algorithm - (below) */
1790 /* to program the device */
1792 /* ======================================== */
1793 /* #include <stdint.h> */
1796 /* uint32_t *dst; */
1797 /* const uint32_t *src; */
1799 /* volatile uint32_t *base; */
1804 /* uint32_t sam4_function(struct foo *p) */
1806 /* volatile uint32_t *v; */
1808 /* const uint32_t *s; */
1823 /* v[ 1 ] = p->cmd; */
1826 /* } while (!(r&1)) */
1830 /* ======================================== */
1832 static const uint8_t
1833 sam4_page_write_opcodes
[] = {
1834 /* 24 0000 0446 mov r4, r0 */
1836 /* 25 0002 6168 ldr r1, [r4, #4] */
1838 /* 26 0004 0068 ldr r0, [r0, #0] */
1840 /* 27 0006 A268 ldr r2, [r4, #8] */
1842 /* 28 @ lr needed for prologue */
1844 /* 30 0008 51F8043B ldr r3, [r1], #4 */
1845 0x51, 0xf8, 0x04, 0x3b,
1846 /* 31 000c 12F1FF32 adds r2, r2, #-1 */
1847 0x12, 0xf1, 0xff, 0x32,
1848 /* 32 0010 40F8043B str r3, [r0], #4 */
1849 0x40, 0xf8, 0x04, 0x3b,
1850 /* 33 0014 F8D1 bne .L2 */
1852 /* 34 0016 E268 ldr r2, [r4, #12] */
1854 /* 35 0018 2369 ldr r3, [r4, #16] */
1856 /* 36 001a 5360 str r3, [r2, #4] */
1858 /* 37 001c 0832 adds r2, r2, #8 */
1861 /* 39 001e 1068 ldr r0, [r2, #0] */
1863 /* 40 0020 10F0010F tst r0, #1 */
1864 0x10, 0xf0, 0x01, 0x0f,
1865 /* 41 0024 FBD0 beq .L4 */
1867 0x00, 0xBE /* bkpt #0 */
1870 static int sam4_page_write(struct sam4_bank_private
*pPrivate
, unsigned pagenum
, uint8_t *buf
)
1874 uint32_t fmr
; /* EEFC Flash Mode Register */
1877 adr
= pagenum
* pPrivate
->page_size
;
1878 adr
= (adr
+ pPrivate
->base_address
);
1880 /* Get flash mode register value */
1881 r
= target_read_u32(pPrivate
->pChip
->target
, pPrivate
->controller_address
, &fmr
);
1883 LOG_DEBUG("Error Read failed: read flash mode register");
1885 /* Clear flash wait state field */
1888 /* set FWS (flash wait states) field in the FMR (flash mode register) */
1889 fmr
|= (pPrivate
->flash_wait_states
<< 8);
1891 LOG_DEBUG("Flash Mode: 0x%08x", ((unsigned int)(fmr
)));
1892 r
= target_write_u32(pPrivate
->pBank
->target
, pPrivate
->controller_address
, fmr
);
1894 LOG_DEBUG("Error Write failed: set flash mode register");
1896 /* 1st sector 8kBytes - page 0 - 15*/
1897 /* 2nd sector 8kBytes - page 16 - 30*/
1898 /* 3rd sector 48kBytes - page 31 - 127*/
1899 LOG_DEBUG("Wr Page %u @ phys address: 0x%08x", pagenum
, (unsigned int)(adr
));
1900 r
= target_write_memory(pPrivate
->pChip
->target
,
1902 4, /* THIS*MUST*BE* in 32bit values */
1903 pPrivate
->page_size
/ 4,
1905 if (r
!= ERROR_OK
) {
1906 LOG_ERROR("SAM4: Failed to write (buffer) page at phys address 0x%08x",
1907 (unsigned int)(adr
));
1911 r
= EFC_PerformCommand(pPrivate
,
1912 /* send Erase & Write Page */
1913 AT91C_EFC_FCMD_WP
, /*AT91C_EFC_FCMD_EWP only works on first two 8kb sectors*/
1918 LOG_ERROR("SAM4: Error performing Write page @ phys address 0x%08x",
1919 (unsigned int)(adr
));
1920 if (status
& (1 << 2)) {
1921 LOG_ERROR("SAM4: Page @ Phys address 0x%08x is locked", (unsigned int)(adr
));
1924 if (status
& (1 << 1)) {
1925 LOG_ERROR("SAM4: Flash Command error @phys address 0x%08x", (unsigned int)(adr
));
1931 static int sam4_write(struct flash_bank
*bank
,
1940 unsigned page_offset
;
1941 struct sam4_bank_private
*pPrivate
;
1942 uint8_t *pagebuffer
;
1944 /* incase we bail further below, set this to null */
1947 /* ignore dumb requests */
1953 if (bank
->target
->state
!= TARGET_HALTED
) {
1954 LOG_ERROR("Target not halted");
1955 r
= ERROR_TARGET_NOT_HALTED
;
1959 pPrivate
= get_sam4_bank_private(bank
);
1960 if (!(pPrivate
->probed
)) {
1961 r
= ERROR_FLASH_BANK_NOT_PROBED
;
1965 if ((offset
+ count
) > pPrivate
->size_bytes
) {
1966 LOG_ERROR("Flash write error - past end of bank");
1967 LOG_ERROR(" offset: 0x%08x, count 0x%08x, BankEnd: 0x%08x",
1968 (unsigned int)(offset
),
1969 (unsigned int)(count
),
1970 (unsigned int)(pPrivate
->size_bytes
));
1975 pagebuffer
= malloc(pPrivate
->page_size
);
1977 LOG_ERROR("No memory for %d Byte page buffer", (int)(pPrivate
->page_size
));
1982 /* what page do we start & end in? */
1983 page_cur
= offset
/ pPrivate
->page_size
;
1984 page_end
= (offset
+ count
- 1) / pPrivate
->page_size
;
1986 LOG_DEBUG("Offset: 0x%08x, Count: 0x%08x", (unsigned int)(offset
), (unsigned int)(count
));
1987 LOG_DEBUG("Page start: %d, Page End: %d", (int)(page_cur
), (int)(page_end
));
1989 /* Special case: all one page */
1992 /* (1) non-aligned start */
1993 /* (2) body pages */
1994 /* (3) non-aligned end. */
1996 /* Handle special case - all one page. */
1997 if (page_cur
== page_end
) {
1998 LOG_DEBUG("Special case, all in one page");
1999 r
= sam4_page_read(pPrivate
, page_cur
, pagebuffer
);
2003 page_offset
= (offset
& (pPrivate
->page_size
-1));
2004 memcpy(pagebuffer
+ page_offset
,
2008 r
= sam4_page_write(pPrivate
, page_cur
, pagebuffer
);
2015 /* non-aligned start */
2016 page_offset
= offset
& (pPrivate
->page_size
- 1);
2018 LOG_DEBUG("Not-Aligned start");
2019 /* read the partial */
2020 r
= sam4_page_read(pPrivate
, page_cur
, pagebuffer
);
2024 /* over-write with new data */
2025 n
= (pPrivate
->page_size
- page_offset
);
2026 memcpy(pagebuffer
+ page_offset
,
2030 r
= sam4_page_write(pPrivate
, page_cur
, pagebuffer
);
2040 /* By checking that offset is correct here, we also
2041 fix a clang warning */
2042 assert(offset
% pPrivate
->page_size
== 0);
2044 /* intermediate large pages */
2045 /* also - the final *terminal* */
2046 /* if that terminal page is a full page */
2047 LOG_DEBUG("Full Page Loop: cur=%d, end=%d, count = 0x%08x",
2048 (int)page_cur
, (int)page_end
, (unsigned int)(count
));
2050 while ((page_cur
< page_end
) &&
2051 (count
>= pPrivate
->page_size
)) {
2052 r
= sam4_page_write(pPrivate
, page_cur
, buffer
);
2055 count
-= pPrivate
->page_size
;
2056 buffer
+= pPrivate
->page_size
;
2060 /* terminal partial page? */
2062 LOG_DEBUG("Terminal partial page, count = 0x%08x", (unsigned int)(count
));
2063 /* we have a partial page */
2064 r
= sam4_page_read(pPrivate
, page_cur
, pagebuffer
);
2067 /* data goes at start */
2068 memcpy(pagebuffer
, buffer
, count
);
2069 r
= sam4_page_write(pPrivate
, page_cur
, pagebuffer
);
2081 COMMAND_HANDLER(sam4_handle_info_command
)
2083 struct sam4_chip
*pChip
;
2084 pChip
= get_current_sam4(CMD_CTX
);
2091 /* bank0 must exist before we can do anything */
2092 if (pChip
->details
.bank
[0].pBank
== NULL
) {
2095 command_print(CMD_CTX
,
2096 "Please define bank %d via command: flash bank %s ... ",
2098 at91sam4_flash
.name
);
2102 /* if bank 0 is not probed, then probe it */
2103 if (!(pChip
->details
.bank
[0].probed
)) {
2104 r
= sam4_auto_probe(pChip
->details
.bank
[0].pBank
);
2108 /* above guarantees the "chip details" structure is valid */
2109 /* and thus, bank private areas are valid */
2110 /* and we have a SAM4 chip, what a concept! */
2112 /* auto-probe other banks, 0 done above */
2113 for (x
= 1; x
< SAM4_MAX_FLASH_BANKS
; x
++) {
2114 /* skip banks not present */
2115 if (!(pChip
->details
.bank
[x
].present
))
2118 if (pChip
->details
.bank
[x
].pBank
== NULL
)
2121 if (pChip
->details
.bank
[x
].probed
)
2124 r
= sam4_auto_probe(pChip
->details
.bank
[x
].pBank
);
2129 r
= sam4_GetInfo(pChip
);
2130 if (r
!= ERROR_OK
) {
2131 LOG_DEBUG("Sam4Info, Failed %d", r
);
2138 COMMAND_HANDLER(sam4_handle_gpnvm_command
)
2142 struct sam4_chip
*pChip
;
2144 pChip
= get_current_sam4(CMD_CTX
);
2148 if (pChip
->target
->state
!= TARGET_HALTED
) {
2149 LOG_ERROR("sam4 - target not halted");
2150 return ERROR_TARGET_NOT_HALTED
;
2153 if (pChip
->details
.bank
[0].pBank
== NULL
) {
2154 command_print(CMD_CTX
, "Bank0 must be defined first via: flash bank %s ...",
2155 at91sam4_flash
.name
);
2158 if (!pChip
->details
.bank
[0].probed
) {
2159 r
= sam4_auto_probe(pChip
->details
.bank
[0].pBank
);
2166 return ERROR_COMMAND_SYNTAX_ERROR
;
2175 if ((0 == strcmp(CMD_ARGV
[0], "show")) && (0 == strcmp(CMD_ARGV
[1], "all")))
2179 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], v32
);
2185 if (0 == strcmp("show", CMD_ARGV
[0])) {
2189 for (x
= 0; x
< pChip
->details
.n_gpnvms
; x
++) {
2190 r
= FLASHD_GetGPNVM(&(pChip
->details
.bank
[0]), x
, &v
);
2193 command_print(CMD_CTX
, "sam4-gpnvm%u: %u", x
, v
);
2197 if ((who
>= 0) && (((unsigned)(who
)) < pChip
->details
.n_gpnvms
)) {
2198 r
= FLASHD_GetGPNVM(&(pChip
->details
.bank
[0]), who
, &v
);
2199 command_print(CMD_CTX
, "sam4-gpnvm%u: %u", who
, v
);
2202 command_print(CMD_CTX
, "sam4-gpnvm invalid GPNVM: %u", who
);
2203 return ERROR_COMMAND_SYNTAX_ERROR
;
2208 command_print(CMD_CTX
, "Missing GPNVM number");
2209 return ERROR_COMMAND_SYNTAX_ERROR
;
2212 if (0 == strcmp("set", CMD_ARGV
[0]))
2213 r
= FLASHD_SetGPNVM(&(pChip
->details
.bank
[0]), who
);
2214 else if ((0 == strcmp("clr", CMD_ARGV
[0])) ||
2215 (0 == strcmp("clear", CMD_ARGV
[0]))) /* quietly accept both */
2216 r
= FLASHD_ClrGPNVM(&(pChip
->details
.bank
[0]), who
);
2218 command_print(CMD_CTX
, "Unknown command: %s", CMD_ARGV
[0]);
2219 r
= ERROR_COMMAND_SYNTAX_ERROR
;
2224 COMMAND_HANDLER(sam4_handle_slowclk_command
)
2226 struct sam4_chip
*pChip
;
2228 pChip
= get_current_sam4(CMD_CTX
);
2240 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], v
);
2242 /* absurd slow clock of 200Khz? */
2243 command_print(CMD_CTX
, "Absurd/illegal slow clock freq: %d\n", (int)(v
));
2244 return ERROR_COMMAND_SYNTAX_ERROR
;
2246 pChip
->cfg
.slow_freq
= v
;
2251 command_print(CMD_CTX
, "Too many parameters");
2252 return ERROR_COMMAND_SYNTAX_ERROR
;
2255 command_print(CMD_CTX
, "Slowclk freq: %d.%03dkhz",
2256 (int)(pChip
->cfg
.slow_freq
/ 1000),
2257 (int)(pChip
->cfg
.slow_freq
% 1000));
2261 static const struct command_registration at91sam4_exec_command_handlers
[] = {
2264 .handler
= sam4_handle_gpnvm_command
,
2265 .mode
= COMMAND_EXEC
,
2266 .usage
= "[('clr'|'set'|'show') bitnum]",
2267 .help
= "Without arguments, shows all bits in the gpnvm "
2268 "register. Otherwise, clears, sets, or shows one "
2269 "General Purpose Non-Volatile Memory (gpnvm) bit.",
2273 .handler
= sam4_handle_info_command
,
2274 .mode
= COMMAND_EXEC
,
2275 .help
= "Print information about the current at91sam4 chip"
2276 "and its flash configuration.",
2280 .handler
= sam4_handle_slowclk_command
,
2281 .mode
= COMMAND_EXEC
,
2282 .usage
= "[clock_hz]",
2283 .help
= "Display or set the slowclock frequency "
2284 "(default 32768 Hz).",
2286 COMMAND_REGISTRATION_DONE
2288 static const struct command_registration at91sam4_command_handlers
[] = {
2291 .mode
= COMMAND_ANY
,
2292 .help
= "at91sam4 flash command group",
2294 .chain
= at91sam4_exec_command_handlers
,
2296 COMMAND_REGISTRATION_DONE
2299 struct flash_driver at91sam4_flash
= {
2301 .commands
= at91sam4_command_handlers
,
2302 .flash_bank_command
= sam4_flash_bank_command
,
2303 .erase
= sam4_erase
,
2304 .protect
= sam4_protect
,
2305 .write
= sam4_write
,
2306 .read
= default_flash_read
,
2307 .probe
= sam4_probe
,
2308 .auto_probe
= sam4_auto_probe
,
2309 .erase_check
= default_flash_blank_check
,
2310 .protect_check
= sam4_protect_check
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)