23 #define DEVICEID0_DEVICEID0 (0x400490C0)
24 #define DEVICEID0_DEVICEID1 (0x400490D0)
25 #define DEVICEID0_DEVICEID2 (0x400490E0)
26 #define DEVICEID0_DEVICEID3 (0x400490F0)
29 #define CPUID_CHECK_VALUE (0x410FC230)
30 #define CPUID_CHECK_VALUE_MASK (0xFF0FFFF0)
33 #define FLASH_BASE_ADDRESS (0x00000000)
34 #define LOCK_WORD_ADDRESS (0x0003FFFC)
36 #define LOCK_WORD_MCU_UNLOCKED (0xFFFFFFFF)
38 #define LOCK_WORD_MCU_UNLOCKED_BY_FIRMWARE (0x00000000)
41 #define FLASHCTRL0_CONFIG_ALL (0x4002E000)
42 #define FLASHCTRL0_CONFIG_SET (0x4002E004)
43 #define FLASHCTRL0_CONFIG_CLR (0x4002E008)
44 #define FLASHCTRL0_CONFIG_ERASEEN_MASK (0x00040000)
45 #define FLASHCTRL0_CONFIG_BUSYF_MASK (0x00100000)
47 #define FLASHCTRL0_WRADDR (0x4002E0A0)
48 #define FLASHCTRL0_WRDATA (0x4002E0B0)
50 #define FLASHCTRL0_KEY (0x4002E0C0)
51 #define FLASHCTRL0_KEY_INITIAL_UNLOCK (0x000000A5)
52 #define FLASHCTRL0_KEY_SINGLE_UNLOCK (0x000000F1)
53 #define FLASHCTRL0_KEY_MULTIPLE_UNLOCK (0x000000F2)
54 #define FLASHCTRL0_KEY_MULTIPLE_LOCK (0x0000005A)
56 #define FLASH_BUSY_TIMEOUT (100)
59 #define RSTSRC0_RESETEN_ALL (0x4002D060)
60 #define RSTSRC0_RESETEN_SET (0x4002D064)
61 #define RSTSRC0_RESETEN_CLR (0x4002D068)
62 #define RSTSRC0_RESETEN_VMONREN_MASK (0x00000004)
63 #define RSTSRC0_RESETEN_SWREN_MASK (0x00000040)
66 #define VMON0_CONTROL_ALL (0x4002F000)
67 #define VMON0_CONTROL_SET (0x4002F004)
68 #define VMON0_CONTROL_CLR (0x4002F008)
69 #define VMON0_CONTROL_VMONEN_MASK (0x80000000)
72 #define CLKCTRL0_APBCLKG0_ALL (0x4002D020)
73 #define CLKCTRL0_APBCLKG0_SET (0x4002D024)
74 #define CLKCTRL0_APBCLKG0_CLR (0x4002D028)
75 #define CLKCTRL0_APBCLKG0_FLCTRLCEN_MASK (0x40000000)
78 #define WDTIMER0_CONTROL_ALL (0x40030000)
79 #define WDTIMER0_CONTROL_SET (0x40030004)
80 #define WDTIMER0_CONTROL_CLR (0x40030008)
81 #define WDTIMER0_CONTROL_DBGMD_MASK (0x00000002)
83 #define WDTIMER0_STATUS_ALL (0x40030010)
84 #define WDTIMER0_STATUS_SET (0x40030014)
85 #define WDTIMER0_STATUS_CLR (0x40030018)
86 #define WDTIMER0_STATUS_KEYSTS_MASK (0x00000001)
87 #define WDTIMER0_STATUS_PRIVSTS_MASK (0x00000002)
89 #define WDTIMER0_THRESHOLD (0x40030020)
91 #define WDTIMER0_WDTKEY (0x40030030)
92 #define WDTIMER0_KEY_ATTN (0x000000A5)
93 #define WDTIMER0_KEY_WRITE (0x000000F1)
94 #define WDTIMER0_KEY_RESET (0x000000CC)
95 #define WDTIMER0_KEY_DISABLE (0x000000DD)
96 #define WDTIMER0_KEY_START (0x000000EE)
97 #define WDTIMER0_KEY_LOCK (0x000000FF)
100 #define SIM3X_AP (0x0A)
102 #define SIM3X_AP_CTRL1 (0x00)
103 #define SIM3X_AP_CTRL2 (0x04)
104 #define SIM3X_AP_LOCK (0x08)
105 #define SIM3X_AP_CRC (0x0C)
107 #define SIM3X_AP_INIT_STAT (0x10)
108 #define SIM3X_AP_DAP_IN (0x14)
109 #define SIM3X_AP_DAP_OUT (0x18)
111 #define SIM3X_AP_ID (0xFC)
114 #define SIM3X_AP_CTRL1_MASS_ERASE_REQ (0x00000001)
115 #define SIM3X_AP_CTRL1_RESET_REQ (0x00000008)
117 #define SIM3X_AP_INIT_STAT_LOCK (0x00000004)
119 #define SIM3X_AP_ID_VALUE (0x2430002)
121 #define SIM3X_FLASH_PAGE_SIZE 1024
266 LOG_ERROR(
"timed out waiting for FLASHCTRL0_CONFIG_BUSYF");
296 for (
unsigned int i = first; i <= last; i++) {
325 LOG_ERROR(
"timed out waiting for FLASHCTRL0_CONFIG_BUSYF");
333 uint32_t buffer_size = 16384;
343 static const uint8_t sim3x_flash_write_code[] = {
346 0xC0, 0xF8, 0xC0, 0x60,
350 0xC0, 0xF8, 0xC0, 0x60,
363 0x16, 0xF4, 0x80, 0x1F,
367 0xC0, 0xF8, 0xA0, 0x40,
371 0xC0, 0xF8, 0xB0, 0x60,
391 0xC0, 0xF8, 0xC0, 0x60,
396 0x16, 0xF4, 0x80, 0x1F,
405 LOG_WARNING(
"no working area available, can't do block memory writes");
410 sizeof(sim3x_flash_write_code), sim3x_flash_write_code);
418 if (buffer_size <= 256) {
424 LOG_WARNING(
"no large enough working area available, can't do block memory writes");
449 LOG_ERROR(
"flash write failed at address 0x%"PRIx32,
470 uint8_t *new_buffer =
NULL;
495 LOG_ERROR(
"offset 0x%" PRIx32
" breaks required 2-byte alignment",
offset);
500 uint32_t old_count =
count;
502 new_buffer = malloc(
count);
505 LOG_ERROR(
"odd number of bytes to write and no memory "
506 "for padding buffer");
509 LOG_INFO(
"odd number of bytes to write (%" PRIu32
"), extending to %" PRIu32
510 " and padding with 0xff", old_count,
count);
512 new_buffer[
count - 1] = 0xff;
556 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
563 unsigned int first,
unsigned int last)
566 uint8_t lock_word[4];
578 if (first != 0 || last !=
bank->num_sectors - 1) {
579 LOG_ERROR(
"Flash does not support finer granularity");
587 LOG_INFO(
"Flash is already locked");
634 char part_num_string[4];
644 if (device_id != 0x00004D33)
652 part_num_string[0] = device_id >> 16;
653 part_num_string[1] = device_id >> 8;
654 part_num_string[2] = device_id;
655 part_num_string[3] = 0;
660 if (!isalpha(device_id >> 24) || part_number < 100 || part_number > 999)
745 if (((cpuid >> 4) & 0xfff) != 0xc23) {
755 LOG_ERROR(
"Failed to parse info from MCU");
759 LOG_WARNING(
"Failed to read info from MCU, using info from flash bank parameters");
763 LOG_ERROR(
"Flash size not set in the flash bank command");
804 bank->sectors[i].is_erased = -1;
874 LOG_DEBUG(
"DAP: failed to queue a write request");
899 LOG_DEBUG(
"DAP: failed to queue a read request");
943 LOG_ERROR(
"mass_erase can't be used by this debug interface");
988 LOG_INFO(
"Target can't be unlocked by this debug interface");
996 LOG_ERROR(
"Target is not ARM Cortex-M3 or is already locked");
1020 LOG_INFO(
"Target is already locked");
1023 LOG_ERROR(
"Target doesn't seem to be locked but memory was not read correct");
1035 uint8_t lock_word[4];
1048 LOG_INFO(
"Target is successfully locked");
1052 LOG_ERROR(
"Target is unlocked by firmware and can't by locked again without the lock page erase or mass erase");
1055 LOG_ERROR(
"Unexpected lock word value");
1059 LOG_INFO(
"Maybe this isn't a SiM3x MCU");
1067 .
name =
"mass_erase",
1069 .help =
"Erase the complete flash",
1071 .handler = sim3x_mass_erase,
1076 .help =
"Locks the flash. Unlock by mass erase",
1078 .handler = sim3x_lock,
1087 .help =
"sim3x flash command group",
1097 .flash_bank_command = sim3x_flash_bank_command,
void init_reg_param(struct reg_param *param, char *reg_name, uint32_t size, enum param_direction direction)
void destroy_reg_param(struct reg_param *param)
struct adiv5_ap * dap_get_ap(struct adiv5_dap *dap, uint64_t ap_num)
int dap_put_ap(struct adiv5_ap *ap)
This defines formats and data structures used to talk to ADIv5 entities.
static int dap_queue_ap_write(struct adiv5_ap *ap, unsigned reg, uint32_t data)
Queue an AP register write.
static int dap_queue_ap_read(struct adiv5_ap *ap, unsigned reg, uint32_t *data)
Queue an AP register read.
static int dap_run(struct adiv5_dap *dap)
Perform all queued DAP operations, and clear any errors posted in the CTRL_STAT register when they ar...
#define ARMV7M_COMMON_MAGIC
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
#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 CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
static struct cortex_m_common * target_to_cm(struct target *target)
#define ERROR_FLASH_OPERATION_FAILED
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
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.
void alive_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
#define WDTIMER0_CONTROL_SET
#define FLASHCTRL0_CONFIG_SET
#define CPUID_CHECK_VALUE
static int ap_write_register(struct adiv5_dap *dap, unsigned reg, uint32_t value)
reg 31:8 - no effect reg 7:4 - bank reg 3:2 - register reg 1:0 - no effect
#define LOCK_WORD_ADDRESS
#define CLKCTRL0_APBCLKG0_SET
static int sim3x_auto_probe(struct flash_bank *bank)
static int sim3x_flash_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
const struct flash_driver sim3x_flash
static int sim3x_init(struct flash_bank *bank)
static int ap_read_register(struct adiv5_dap *dap, unsigned reg, uint32_t *result)
#define VMON0_CONTROL_VMONEN_MASK
#define FLASHCTRL0_KEY_INITIAL_UNLOCK
static int sim3x_flash_info(struct flash_bank *bank, struct command_invocation *cmd)
static const struct command_registration sim3x_command_handlers[]
#define LOCK_WORD_MCU_UNLOCKED
#define FLASHCTRL0_WRADDR
#define RSTSRC0_RESETEN_SET
#define RSTSRC0_RESETEN_VMONREN_MASK
static int ap_poll_register(struct adiv5_dap *dap, unsigned reg, uint32_t mask, uint32_t value, int timeout)
#define SIM3X_FLASH_PAGE_SIZE
static int sim3x_flash_protect_check(struct flash_bank *bank)
#define FLASHCTRL0_WRDATA
#define DEVICEID0_DEVICEID0
FLASH_BANK_COMMAND_HANDLER(sim3x_flash_bank_command)
#define WDTIMER0_CONTROL_DBGMD_MASK
#define FLASHCTRL0_CONFIG_BUSYF_MASK
static const struct command_registration sim3x_exec_command_handlers[]
#define LOCK_WORD_MCU_UNLOCKED_BY_FIRMWARE
#define CPUID_CHECK_VALUE_MASK
static int sim3x_write_block(struct flash_bank *bank, const uint8_t *buf, uint32_t offset, uint32_t count)
#define SIM3X_AP_CTRL1_MASS_ERASE_REQ
#define WDTIMER0_KEY_DISABLE
COMMAND_HANDLER(sim3x_mass_erase)
static int sim3x_read_deviceid(struct flash_bank *bank)
#define FLASHCTRL0_CONFIG_CLR
static int sim3x_parse_part_info(struct sim3x_info *sim3x_info)
#define FLASHCTRL0_CONFIG_ERASEEN_MASK
static int sim3x_read_info(struct flash_bank *bank)
#define DEVICEID0_DEVICEID1
static int sim3x_flash_lock_check(struct flash_bank *bank)
#define VMON0_CONTROL_SET
#define SIM3X_AP_INIT_STAT_LOCK
#define SIM3X_AP_CTRL1_RESET_REQ
#define FLASHCTRL0_CONFIG_ALL
#define FLASH_BASE_ADDRESS
static int sim3x_erase_page(struct flash_bank *bank, uint32_t addr)
static int sim3x_probe(struct flash_bank *bank)
static int sim3x_flash_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
#define SIM3X_AP_ID_VALUE
static int sim3x_flash_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
#define FLASH_BUSY_TIMEOUT
#define SIM3X_AP_INIT_STAT
#define FLASHCTRL0_KEY_SINGLE_UNLOCK
#define WDTIMER0_KEY_ATTN
#define WDTIMER0_KEY_WRITE
#define CLKCTRL0_APBCLKG0_FLCTRLCEN_MASK
#define DEVICEID0_DEVICEID2
This represents an ARM Debug Interface (v5) Access Port (AP).
struct adiv5_dap * dap
DAP this AP belongs to.
This represents an ARM Debug Interface (v5) Debug Access Port (DAP).
struct adiv5_dap * dap
For targets conforming to ARM Debug Interface v5, this handle references the Debug Access Port (DAP) ...
unsigned int common_magic
When run_command is called, a new instance will be created on the stack, filled with the proper value...
struct armv7m_common armv7m
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.
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
int target_run_flash_async_algorithm(struct target *target, const uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
Streams data to a circular buffer on target intended for consumption by code running asynchronously o...
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
struct target * get_current_target(struct command_context *cmd_ctx)
#define ERROR_TARGET_NOT_HALTED
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE