28 #define REG_NAME_WIDTH (12)
30 #define SAMV_EFC_FCMD_GETD (0x0)
31 #define SAMV_EFC_FCMD_WP (0x1)
32 #define SAMV_EFC_FCMD_WPL (0x2)
33 #define SAMV_EFC_FCMD_EWP (0x3)
34 #define SAMV_EFC_FCMD_EWPL (0x4)
35 #define SAMV_EFC_FCMD_EA (0x5)
36 #define SAMV_EFC_FCMD_EPA (0x7)
37 #define SAMV_EFC_FCMD_SLB (0x8)
38 #define SAMV_EFC_FCMD_CLB (0x9)
39 #define SAMV_EFC_FCMD_GLB (0xA)
40 #define SAMV_EFC_FCMD_SFB (0xB)
41 #define SAMV_EFC_FCMD_CFB (0xC)
42 #define SAMV_EFC_FCMD_GFB (0xD)
44 #define OFFSET_EFC_FMR 0
45 #define OFFSET_EFC_FCR 4
46 #define OFFSET_EFC_FSR 8
47 #define OFFSET_EFC_FRR 12
49 #define SAMV_CHIPID_CIDR (0x400E0940)
50 #define SAMV_NUM_GPNVM_BITS 9
51 #define SAMV_CONTROLLER_ADDR (0x400e0c00)
52 #define SAMV_SECTOR_SIZE 16384
53 #define SAMV_PAGE_SIZE 512
54 #define SAMV_FLASH_BASE 0x00400000
87 unsigned command,
unsigned argument)
92 LOG_ERROR(
"flash controller is not ready");
96 v = (0x5A << 24) | (argument << 8) |
command;
97 LOG_DEBUG(
"starting flash command: 0x%08x", (
unsigned int)(v));
109 int64_t ms_now, ms_end;
125 if (ms_now > ms_end) {
130 }
while ((v & 1) == 0);
139 int first_page,
int num_pages, uint32_t *
status)
168 first_page | erase_pages,
status);
190 *out = (v >> gpnvm) & 1;
236 unsigned start_sector,
unsigned end_sector)
241 uint32_t pages_per_sector;
245 while (start_sector <= end_sector) {
246 pg = start_sector * pages_per_sector;
256 unsigned start_sector,
unsigned end_sector)
260 uint32_t pages_per_sector;
265 while (start_sector <= end_sector) {
266 pg = start_sector * pages_per_sector;
290 for (
unsigned int x = 0; x <
bank->num_sectors; x++)
291 bank->sectors[x].is_protected = (!!(v[x >> 5] & (1 << (x % 32))));
300 bank->driver_priv = samv_info;
315 LOG_INFO(
"device id = 0x%08" PRIx32
"", device_id);
317 uint8_t
eproc = (device_id >> 5) & 0x7;
319 LOG_ERROR(
"unexpected eproc code: %d was expecting 0 (Cortex-M7)",
eproc);
323 uint8_t nvm_size_code = (device_id >> 8) & 0xf;
324 switch (nvm_size_code) {
326 bank->size = 512 * 1024;
329 bank->size = 1024 * 1024;
332 bank->size = 2048 * 1024;
335 LOG_ERROR(
"unrecognized flash size code: %d", nvm_size_code);
346 for (
unsigned int s = 0; s <
bank->num_sectors; s++) {
349 bank->sectors[s].is_erased = -1;
350 bank->sectors[s].is_protected = -1;
371 const int page_count = 32;
383 if ((first == 0) && ((last + 1) ==
bank->num_sectors))
386 LOG_INFO(
"erasing lock regions %u-%u...", first, last);
388 for (
unsigned int i = first; i <= last; i++) {
391 LOG_INFO(
"erasing lock region %u", i);
393 LOG_ERROR(
"error performing erase page @ lock region number %u", i);
395 LOG_ERROR(
"lock region %u is locked", i);
399 LOG_ERROR(
"flash command error @lock region %u", i);
424 unsigned page_num, uint8_t *buf)
429 LOG_ERROR(
"flash program failed to read page @ 0x%08x",
430 (
unsigned int)(
addr));
435 unsigned pagenum,
const uint8_t *buf)
441 LOG_DEBUG(
"write page %u at address 0x%08x", pagenum, (
unsigned int)
addr);
444 LOG_ERROR(
"failed to buffer page at 0x%08x", (
unsigned int)
addr);
450 LOG_ERROR(
"error performing write page at 0x%08x", (
unsigned int)
addr);
456 LOG_ERROR(
"flash command error at 0x%08x", (
unsigned int)
addr);
474 LOG_ERROR(
"flash write error - past end of bank");
475 LOG_ERROR(
" offset: 0x%08x, count 0x%08x, bank end: 0x%08x",
477 (
unsigned int)(
count),
478 (
unsigned int)(
bank->size));
486 LOG_DEBUG(
"offset: 0x%08x, count: 0x%08x",
488 LOG_DEBUG(
"page start: %d, page end: %d", (
int)(page_cur), (
int)(page_end));
497 uint32_t page_offset;
500 if (page_cur == page_end) {
501 LOG_DEBUG(
"special case, all in one page");
526 memcpy(pagebuffer + page_offset,
buffer, n);
542 LOG_DEBUG(
"full page loop: cur=%d, end=%d, count = 0x%08x",
543 (
int)page_cur, (
int)page_end, (
unsigned int)(
count));
556 LOG_DEBUG(
"final partial page, count = 0x%08x", (
unsigned int)(
count));
668 .handler = samv_handle_gpnvm_command,
670 .usage =
"[('clr'|'set'|'show') bitnum]",
671 .help =
"Without arguments, shows all bits in the gpnvm "
672 "register. Otherwise, clears, sets, or shows one "
673 "General Purpose Non-Volatile Memory (gpnvm) bit.",
682 .help =
"atsamv flash command group",
692 .flash_bank_command = samv_flash_bank_command,
static const char * eproc[8]
static int samv_efc_get_result(struct target *target, uint32_t *v)
static int samv_protect_check(struct flash_bank *bank)
static int samv_flash_lock(struct target *target, unsigned start_sector, unsigned end_sector)
static int samv_get_gpnvm(struct target *target, unsigned gpnvm, unsigned *out)
FLASH_BANK_COMMAND_HANDLER(samv_flash_bank_command)
static int samv_page_read(struct target *target, unsigned page_num, uint8_t *buf)
#define SAMV_EFC_FCMD_EPA
static int samv_flash_unlock(struct target *target, unsigned start_sector, unsigned end_sector)
static int samv_auto_probe(struct flash_bank *bank)
#define SAMV_EFC_FCMD_CFB
const struct flash_driver atsamv_flash
COMMAND_HANDLER(samv_handle_gpnvm_command)
static int samv_efc_perform_command(struct target *target, unsigned command, unsigned argument, uint32_t *status)
#define SAMV_EFC_FCMD_SLB
#define SAMV_EFC_FCMD_SFB
#define SAMV_EFC_FCMD_GFB
static int samv_clear_gpnvm(struct target *target, unsigned gpnvm)
static int samv_efc_start_command(struct target *target, unsigned command, unsigned argument)
static const struct command_registration atsamv_exec_command_handlers[]
static const struct command_registration atsamv_command_handlers[]
#define SAMV_CONTROLLER_ADDR
#define SAMV_EFC_FCMD_CLB
static int samv_set_gpnvm(struct target *target, unsigned gpnvm)
static int samv_get_info(struct flash_bank *bank, struct command_invocation *cmd)
static int samv_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
static int samv_page_write(struct target *target, unsigned pagenum, const uint8_t *buf)
#define SAMV_NUM_GPNVM_BITS
static int samv_erase_pages(struct target *target, int first_page, int num_pages, uint32_t *status)
static int samv_efc_get_status(struct target *target, uint32_t *v)
static int samv_get_device_id(struct flash_bank *bank, uint32_t *device_id)
static int samv_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static int samv_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
#define SAMV_EFC_FCMD_GLB
static int samv_probe(struct flash_bank *bank)
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
void command_print(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
struct flash_bank * get_flash_bank_by_num_noprobe(unsigned int num)
Returns the flash bank like get_flash_bank_by_num(), without probing.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Provides details of a flash bank, available either on-chip or through a major interface.
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Describes the geometry and status of a single flash sector within a flash bank.
unsigned gpnvm[SAMV_NUM_GPNVM_BITS]
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
#define ERROR_TARGET_NOT_HALTED