32 #define REG_NAME_WIDTH (12)
34 #define SAMV_EFC_FCMD_GETD (0x0)
35 #define SAMV_EFC_FCMD_WP (0x1)
36 #define SAMV_EFC_FCMD_WPL (0x2)
37 #define SAMV_EFC_FCMD_EWP (0x3)
38 #define SAMV_EFC_FCMD_EWPL (0x4)
39 #define SAMV_EFC_FCMD_EA (0x5)
40 #define SAMV_EFC_FCMD_EPA (0x7)
41 #define SAMV_EFC_FCMD_SLB (0x8)
42 #define SAMV_EFC_FCMD_CLB (0x9)
43 #define SAMV_EFC_FCMD_GLB (0xA)
44 #define SAMV_EFC_FCMD_SFB (0xB)
45 #define SAMV_EFC_FCMD_CFB (0xC)
46 #define SAMV_EFC_FCMD_GFB (0xD)
47 #define SAMV_EFC_FCMD_WUS (0x12)
48 #define SAMV_EFC_FCMD_EUS (0x13)
49 #define SAMV_EFC_FCMD_STUS (0x14)
50 #define SAMV_EFC_FCMD_SPUS (0x15)
52 #define SAMV_EFC_FMR_SCOD BIT(16)
54 #define SAMV_EFC_FSR_FRDY_SET 1
55 #define SAMV_EFC_FRS_FRDY_CLR 0
57 #define OFFSET_EFC_FMR 0
58 #define OFFSET_EFC_FCR 4
59 #define OFFSET_EFC_FSR 8
60 #define OFFSET_EFC_FRR 12
62 #define TIMEOUT_MS_FRS_CHANGE 10
63 #define TIMEOUT_MS_CMD_DEFAULT 50
64 #define TIMEOUT_MS_CMD_ERASE 24000
66 #define SAMV_CHIPID_CIDR (0x400E0940)
67 #define SAMV_NUM_GPNVM_BITS 9
68 #define SAMV_CONTROLLER_ADDR (0x400e0c00)
69 #define SAMV_SECTOR_SIZE 16384
70 #define SAMV_PAGE_SIZE 512
71 #define SAMV_FLASH_BASE 0x00400000
78 #define SAMV_FLASH_SIGNATURE_BASE 0x100000000
79 #define SAMV_FLASH_SIGNATURE_SIZE SAMV_PAGE_SIZE
105 int64_t ms_now, ms_end;
121 if (ms_now > ms_end) {
126 }
while ((v & 1) != desired);
151 uint8_t
command,
unsigned int argument)
155 v = (0x5A << 24) | (argument << 8) |
command;
156 LOG_DEBUG(
"starting flash command: 0x%08x", (
unsigned int)(v));
199 uint8_t stop_cmd, uint8_t *buf,
size_t read_size)
221 read_size /
sizeof(uint32_t), buf);
223 LOG_ERROR(
"flash program failed to read page @ 0x%" PRIx32
"",
addr);
244 int first_page,
int num_pages, uint32_t *
status)
273 first_page | erase_pages,
status);
295 *out = (v >> gpnvm) & 1;
346 LOG_ERROR(
"error performing user signature write");
352 unsigned int start_sector,
unsigned int end_sector)
357 uint32_t pages_per_sector;
361 while (start_sector <= end_sector) {
362 pg = start_sector * pages_per_sector;
372 unsigned int start_sector,
unsigned int end_sector)
376 uint32_t pages_per_sector;
381 while (start_sector <= end_sector) {
382 pg = start_sector * pages_per_sector;
406 for (
unsigned int x = 0; x <
bank->num_sectors; x++)
407 bank->sectors[x].is_protected = (!!(v[x >> 5] & (1 << (x % 32))));
415 bank->driver_priv = samv_info;
428 bank->num_sectors = 1;
438 LOG_INFO(
"device id = 0x%08" PRIx32, device_id);
440 uint8_t
eproc = (device_id >> 5) & 0x7;
442 LOG_ERROR(
"unexpected eproc code: %d was expecting 0 (Cortex-M7)",
eproc);
446 uint8_t nvm_size_code = (device_id >> 8) & 0xf;
447 switch (nvm_size_code) {
449 bank->size = 512 * 1024;
452 bank->size = 1024 * 1024;
455 bank->size = 2048 * 1024;
458 LOG_ERROR(
"unrecognized flash size code: %d", nvm_size_code);
469 for (
unsigned int s = 0; s <
bank->num_sectors; s++) {
472 bank->sectors[s].is_erased = -1;
473 bank->sectors[s].is_protected = -1;
494 const int page_count = 32;
509 if ((first == 0) && ((last + 1) ==
bank->num_sectors))
512 LOG_DEBUG(
"erasing lock regions %u-%u...", first, last);
514 for (
unsigned int i = first; i <= last; i++) {
519 LOG_ERROR(
"error performing erase page @ lock region number %u", i);
521 LOG_ERROR(
"lock region %u is locked", i);
525 LOG_ERROR(
"flash command error @lock region %u", i);
550 unsigned int page_num, uint8_t *buf)
555 LOG_ERROR(
"flash program failed to read page @ 0x%08" PRIx32
"",
583 unsigned int pagenum,
const uint8_t *buf)
597 LOG_ERROR(
"error performing user signature write");
603 unsigned int pagenum,
const uint8_t *buf)
609 LOG_DEBUG(
"write page %u at address 0x%08" PRIx32
"", pagenum,
addr);
618 LOG_ERROR(
"error performing write page at 0x%08" PRIx32
"",
addr);
631 target_addr_t base,
unsigned int pagenum,
const uint8_t *buf)
657 LOG_WARNING(
"Reads past end of flash. Extra data discarded.");
689 LOG_ERROR(
"flash write error - past end of bank");
690 LOG_ERROR(
" offset: 0x%08" PRIx32
", count 0x%08" PRIx32
", bank end: 0x%08" PRIx32
"",
702 LOG_DEBUG(
"page start: %" PRIu32
", page end: %" PRIu32
"", page_cur, page_end);
711 uint32_t page_offset;
714 if (page_cur == page_end) {
715 LOG_DEBUG(
"special case, all in one page");
740 memcpy(pagebuffer + page_offset,
buffer, n);
756 LOG_DEBUG(
"full page loop: cur=%" PRIu32
", end=%" PRIu32
", count = 0x%08" PRIx32
"",
757 page_cur, page_end,
count);
882 .handler = samv_handle_gpnvm_command,
884 .usage =
"[('clr'|'set'|'show') bitnum]",
885 .help =
"Without arguments, shows all bits in the gpnvm "
886 "register. Otherwise, clears, sets, or shows one "
887 "General Purpose Non-Volatile Memory (gpnvm) bit.",
896 .help =
"atsamv flash command group",
906 .flash_bank_command = samv_flash_bank_command,
static const char * eproc[8]
static int samv_efc_read_sequence(struct target *target, uint8_t start_cmd, uint8_t stop_cmd, uint8_t *buf, size_t read_size)
static int samv_get_gpnvm(struct target *target, unsigned int gpnvm, unsigned int *out)
#define SAMV_EFC_FCMD_STUS
#define SAMV_EFC_FMR_SCOD
static int samv_efc_get_result(struct target *target, uint32_t *v)
#define TIMEOUT_MS_CMD_DEFAULT
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)
static int samv_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
#define SAMV_EFC_FCMD_EPA
#define SAMV_EFC_FCMD_SPUS
static int samv_read_standard_page(struct target *target, unsigned int page_num, uint8_t *buf)
static int samv_auto_probe(struct flash_bank *bank)
#define SAMV_EFC_FCMD_CFB
static int samv_efc_get_mode(struct target *target, uint32_t *v)
const struct flash_driver atsamv_flash
#define TIMEOUT_MS_CMD_ERASE
COMMAND_HANDLER(samv_handle_gpnvm_command)
#define SAMV_EFC_FCMD_SLB
#define SAMV_EFC_FCMD_SFB
#define SAMV_EFC_FCMD_GFB
#define SAMV_FLASH_SIGNATURE_BASE
static const struct command_registration atsamv_exec_command_handlers[]
static int samv_erase_user_signature(struct target *target)
static int samv_efc_wait_status(struct target *target, uint8_t desired, int64_t timeout, uint32_t *status)
static int samv_write_standard_page(struct target *target, unsigned int pagenum, const uint8_t *buf)
static const struct command_registration atsamv_command_handlers[]
#define SAMV_CONTROLLER_ADDR
#define SAMV_EFC_FCMD_CLB
#define SAMV_EFC_FSR_FRDY_SET
#define SAMV_EFC_FRS_FRDY_CLR
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)
static int samv_efc_perform_command(struct target *target, uint8_t command, unsigned int argument, uint32_t *status)
#define SAMV_NUM_GPNVM_BITS
static int samv_page_write(struct target *target, target_addr_t base, unsigned int pagenum, const uint8_t *buf)
static int samv_efc_start_command(struct target *target, uint8_t command, unsigned int argument)
#define TIMEOUT_MS_FRS_CHANGE
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)
#define SAMV_EFC_FCMD_EUS
static int samv_page_read(struct target *target, target_addr_t base, unsigned int page_num, uint8_t *buf)
#define SAMV_EFC_FCMD_WUS
static int samv_write_user_signature(struct target *target, unsigned int pagenum, const uint8_t *buf)
static int samv_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
#define SAMV_EFC_FCMD_GLB
#define SAMV_FLASH_SIGNATURE_SIZE
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)
static int samv_read_user_signature(struct target *target, uint8_t *buf)
static int samv_efc_set_mode(struct target *target, uint32_t v)
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.
uint64_t buffer
Pointer to data buffer to send over SPI.
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.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
#define LOG_WARNING(expr ...)
#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