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
85 unsigned int command,
unsigned int argument)
90 LOG_ERROR(
"flash controller is not ready");
94 v = (0x5A << 24) | (argument << 8) |
command;
95 LOG_DEBUG(
"starting flash command: 0x%08x", (
unsigned int)(v));
103 unsigned int command,
unsigned int argument, uint32_t *
status)
107 int64_t ms_now, ms_end;
123 if (ms_now > ms_end) {
128 }
while ((v & 1) == 0);
137 int first_page,
int num_pages, uint32_t *
status)
166 first_page | erase_pages,
status);
188 *out = (v >> gpnvm) & 1;
234 unsigned int start_sector,
unsigned int end_sector)
239 uint32_t pages_per_sector;
243 while (start_sector <= end_sector) {
244 pg = start_sector * pages_per_sector;
254 unsigned int start_sector,
unsigned int end_sector)
258 uint32_t pages_per_sector;
263 while (start_sector <= end_sector) {
264 pg = start_sector * pages_per_sector;
288 for (
unsigned int x = 0; x <
bank->num_sectors; x++)
289 bank->sectors[x].is_protected = (!!(v[x >> 5] & (1 << (x % 32))));
298 bank->driver_priv = samv_info;
313 LOG_INFO(
"device id = 0x%08" PRIx32
"", device_id);
315 uint8_t
eproc = (device_id >> 5) & 0x7;
317 LOG_ERROR(
"unexpected eproc code: %d was expecting 0 (Cortex-M7)",
eproc);
321 uint8_t nvm_size_code = (device_id >> 8) & 0xf;
322 switch (nvm_size_code) {
324 bank->size = 512 * 1024;
327 bank->size = 1024 * 1024;
330 bank->size = 2048 * 1024;
333 LOG_ERROR(
"unrecognized flash size code: %d", nvm_size_code);
344 for (
unsigned int s = 0; s <
bank->num_sectors; s++) {
347 bank->sectors[s].is_erased = -1;
348 bank->sectors[s].is_protected = -1;
369 const int page_count = 32;
381 if ((first == 0) && ((last + 1) ==
bank->num_sectors))
384 LOG_INFO(
"erasing lock regions %u-%u...", first, last);
386 for (
unsigned int i = first; i <= last; i++) {
389 LOG_INFO(
"erasing lock region %u", i);
391 LOG_ERROR(
"error performing erase page @ lock region number %u", i);
393 LOG_ERROR(
"lock region %u is locked", i);
397 LOG_ERROR(
"flash command error @lock region %u", i);
422 unsigned int page_num, uint8_t *buf)
427 LOG_ERROR(
"flash program failed to read page @ 0x%08x",
428 (
unsigned int)(
addr));
433 unsigned int pagenum,
const uint8_t *buf)
439 LOG_DEBUG(
"write page %u at address 0x%08x", pagenum, (
unsigned int)
addr);
442 LOG_ERROR(
"failed to buffer page at 0x%08x", (
unsigned int)
addr);
448 LOG_ERROR(
"error performing write page at 0x%08x", (
unsigned int)
addr);
454 LOG_ERROR(
"flash command error at 0x%08x", (
unsigned int)
addr);
472 LOG_ERROR(
"flash write error - past end of bank");
473 LOG_ERROR(
" offset: 0x%08x, count 0x%08x, bank end: 0x%08x",
475 (
unsigned int)(
count),
476 (
unsigned int)(
bank->size));
484 LOG_DEBUG(
"offset: 0x%08x, count: 0x%08x",
486 LOG_DEBUG(
"page start: %d, page end: %d", (
int)(page_cur), (
int)(page_end));
495 uint32_t page_offset;
498 if (page_cur == page_end) {
499 LOG_DEBUG(
"special case, all in one page");
524 memcpy(pagebuffer + page_offset,
buffer, n);
540 LOG_DEBUG(
"full page loop: cur=%d, end=%d, count = 0x%08x",
541 (
int)page_cur, (
int)page_end, (
unsigned int)(
count));
554 LOG_DEBUG(
"final partial page, count = 0x%08x", (
unsigned int)(
count));
666 .handler = samv_handle_gpnvm_command,
668 .usage =
"[('clr'|'set'|'show') bitnum]",
669 .help =
"Without arguments, shows all bits in the gpnvm "
670 "register. Otherwise, clears, sets, or shows one "
671 "General Purpose Non-Volatile Memory (gpnvm) bit.",
680 .help =
"atsamv flash command group",
690 .flash_bank_command = samv_flash_bank_command,
static const char * eproc[8]
static int samv_get_gpnvm(struct target *target, unsigned int gpnvm, unsigned int *out)
static int samv_efc_get_result(struct target *target, uint32_t *v)
static int samv_protect_check(struct flash_bank *bank)
FLASH_BANK_COMMAND_HANDLER(samv_flash_bank_command)
static int samv_flash_unlock(struct target *target, unsigned int start_sector, unsigned int end_sector)
#define SAMV_EFC_FCMD_EPA
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)
#define SAMV_EFC_FCMD_SLB
#define SAMV_EFC_FCMD_SFB
#define SAMV_EFC_FCMD_GFB
static int samv_page_read(struct target *target, unsigned int page_num, uint8_t *buf)
static const struct command_registration atsamv_exec_command_handlers[]
static int samv_efc_start_command(struct target *target, unsigned int command, unsigned int argument)
static const struct command_registration atsamv_command_handlers[]
static int samv_page_write(struct target *target, unsigned int pagenum, const uint8_t *buf)
static int samv_efc_perform_command(struct target *target, unsigned int command, unsigned int argument, uint32_t *status)
#define SAMV_CONTROLLER_ADDR
#define SAMV_EFC_FCMD_CLB
static int samv_clear_gpnvm(struct target *target, unsigned int 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)
#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)
static int samv_set_gpnvm(struct target *target, unsigned int gpnvm)
static int samv_flash_lock(struct target *target, unsigned int start_sector, unsigned int end_sector)
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 ...)
target_addr_t addr
Start address to search for the control block.
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 int 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