25 #define EM357_FLASH_ACR 0x40008000
26 #define EM357_FLASH_KEYR 0x40008004
27 #define EM357_FLASH_OPTKEYR 0x40008008
28 #define EM357_FLASH_SR 0x4000800C
29 #define EM357_FLASH_CR 0x40008010
30 #define EM357_FLASH_AR 0x40008014
31 #define EM357_FLASH_OBR 0x4000801C
32 #define EM357_FLASH_WRPR 0x40008020
34 #define EM357_FPEC_CLK 0x4000402c
37 #define EM357_OB_RDP 0x08040800
38 #define EM357_OB_WRP0 0x08040808
39 #define EM357_OB_WRP1 0x0804080A
40 #define EM357_OB_WRP2 0x0804080C
44 #define FLASH_PG (1 << 0)
45 #define FLASH_PER (1 << 1)
46 #define FLASH_MER (1 << 2)
47 #define FLASH_OPTPG (1 << 4)
48 #define FLASH_OPTER (1 << 5)
49 #define FLASH_STRT (1 << 6)
50 #define FLASH_LOCK (1 << 7)
51 #define FLASH_OPTWRE (1 << 9)
55 #define FLASH_BSY (1 << 0)
56 #define FLASH_PGERR (1 << 2)
57 #define FLASH_WRPRTERR (1 << 4)
58 #define FLASH_EOP (1 << 5)
67 #define KEY1 0x45670123
68 #define KEY2 0xCDEF89AB
94 bank->driver_priv = em357_info;
96 em357_info->
probed =
false;
122 LOG_ERROR(
"timed out waiting for flash");
134 LOG_ERROR(
"em357 device programming failed");
154 em357_info =
bank->driver_priv;
165 LOG_INFO(
"Device Security Bit Set");
184 em357_info =
bank->driver_priv;
230 em357_info =
bank->driver_priv;
323 for (i = 0; i < num_bits; i++) {
329 bank->sectors[(i * em357_info->
ppage_size) + s].is_protected = set;
345 if ((first == 0) && (last == (
bank->num_sectors - 1)))
359 for (
unsigned int i = first; i <= last; i++) {
364 bank->base +
bank->sectors[i].offset);
388 uint16_t prot_reg[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
393 em357_info =
bank->driver_priv;
401 LOG_WARNING(
"aligned start protect sector to a %d sector boundary",
403 first = first - (first % em357_info->
ppage_size);
405 if (((last + 1) % em357_info->
ppage_size) != 0) {
406 LOG_WARNING(
"aligned end protect sector to a %d sector boundary",
409 last = last - (last % em357_info->
ppage_size);
422 for (
unsigned int i = first; i <= last; i++) {
428 prot_reg[
reg] &= ~(1 <<
bit);
430 prot_reg[
reg] |= (1 <<
bit);
448 uint32_t buffer_size = 16384;
459 static const uint8_t em357_flash_write_code[] = {
469 0x30, 0xf8, 0x02, 0x3b,
470 0x21, 0xf8, 0x02, 0x3b,
474 0x13, 0xf0, 0x01, 0x0f,
476 0x13, 0xf0, 0x14, 0x0f,
482 0x00, 0x80, 0x00, 0x40,
488 LOG_WARNING(
"no working area available, can't do block memory writes");
493 sizeof(em357_flash_write_code), em357_flash_write_code);
500 if (buffer_size <= 256) {
506 "no large enough working area available, can't do block memory writes");
520 uint32_t thisrun_count = (
count > (buffer_size / 2)) ?
521 (buffer_size / 2) :
count;
529 buf_set_u32(reg_params[2].value, 0, 32, thisrun_count);
533 write_algorithm->
address, 0, 10000, &armv7m_info);
535 LOG_ERROR(
"error executing em357 flash write algorithm");
540 LOG_ERROR(
"flash memory not erased before writing");
548 LOG_ERROR(
"flash memory write protected");
555 buffer += thisrun_count * 2;
556 address += thisrun_count * 2;
557 count -= thisrun_count;
575 uint32_t words_remaining = (
count / 2);
576 uint32_t bytes_remaining = (
count & 0x00000001);
578 uint32_t bytes_written = 0;
602 if (words_remaining > 0) {
610 "couldn't use block writes, falling back to single memory accesses");
613 buffer += words_remaining * 2;
614 address += words_remaining * 2;
622 while (words_remaining > 0) {
624 memcpy(&value,
buffer + bytes_written,
sizeof(uint16_t));
642 if (bytes_remaining) {
643 uint16_t value = 0xffff;
644 memcpy(&value,
buffer + bytes_written, bytes_remaining);
668 uint32_t base_address = 0x08000000;
670 em357_info->
probed =
false;
672 switch (
bank->size) {
699 LOG_WARNING(
"No size specified for em357 flash driver, assuming 192k!");
712 LOG_INFO(
"flash size = %d KiB", num_pages * page_size / 1024);
716 bank->base = base_address;
717 bank->size = (num_pages * page_size);
718 bank->num_sectors = num_pages;
721 for (i = 0; i < num_pages; i++) {
722 bank->sectors[i].offset = i * page_size;
723 bank->sectors[i].size = page_size;
724 bank->sectors[i].is_erased = -1;
725 bank->sectors[i].is_protected = 1;
728 em357_info->
probed =
true;
754 em357_info =
bank->driver_priv;
811 "INFO: a reset or power cycle is required "
812 "for the new settings to take effect.");
879 .handler = em357_handle_lock_command,
881 .help =
"Lock entire flash device.",
886 .handler = em357_handle_unlock_command,
888 .help =
"Unlock entire protected flash device.",
891 .name =
"mass_erase",
893 .handler = em357_handle_mass_erase_command,
895 .help =
"Erase entire flash device.",
904 .help =
"em357 flash command group",
914 .flash_bank_command = em357_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)
#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 int first, unsigned int 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 int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
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 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 COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
static const struct command_registration em357_exec_command_handlers[]
static int em357_probe(struct flash_bank *bank)
static int em357_mass_erase(struct flash_bank *bank)
const struct flash_driver em357_flash
COMMAND_HANDLER(em357_handle_lock_command)
#define EM357_FLASH_OPTKEYR
static const struct command_registration em357_command_handlers[]
static int em357_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static int em357_get_flash_status(struct flash_bank *bank, uint32_t *status)
FLASH_BANK_COMMAND_HANDLER(em357_flash_bank_command)
static int em357_erase_options(struct flash_bank *bank)
static int em357_protect_check(struct flash_bank *bank)
static int em357_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static int em357_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
static int em357_auto_probe(struct flash_bank *bank)
static int em357_read_options(struct flash_bank *bank)
static int em357_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static int em357_write_options(struct flash_bank *bank)
static int em357_wait_status_busy(struct flash_bank *bank, int timeout)
#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 ...)
static uint32_t bit(uint32_t value, unsigned int b)
unsigned int common_magic
struct em357_options option_bytes
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.
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
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_read_u32(struct target *target, target_addr_t address, uint32_t *value)
#define ERROR_TARGET_NOT_HALTED
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE