26 #define MD_RST_CLK 0x40020000
27 #define MD_PER_CLOCK (MD_RST_CLK + 0x1C)
28 #define MD_PER_CLOCK_EEPROM (1 << 3)
29 #define MD_PER_CLOCK_RST_CLK (1 << 4)
31 #define FLASH_REG_BASE 0x40018000
32 #define FLASH_CMD (FLASH_REG_BASE + 0x00)
33 #define FLASH_ADR (FLASH_REG_BASE + 0x04)
34 #define FLASH_DI (FLASH_REG_BASE + 0x08)
35 #define FLASH_DO (FLASH_REG_BASE + 0x0C)
36 #define FLASH_KEY (FLASH_REG_BASE + 0x10)
38 #define FLASH_NVSTR (1 << 13)
39 #define FLASH_PROG (1 << 12)
40 #define FLASH_MAS1 (1 << 11)
41 #define FLASH_ERASE (1 << 10)
42 #define FLASH_IFREN (1 << 9)
43 #define FLASH_SE (1 << 8)
44 #define FLASH_YE (1 << 7)
45 #define FLASH_XE (1 << 6)
46 #define FLASH_RD (1 << 2)
47 #define FLASH_WR (1 << 1)
48 #define FLASH_CON (1 << 0)
49 #define FLASH_DELAY_MASK (7 << 3)
51 #define KEY 0x8AAA5551
70 bank->driver_priv = mdr_info;
90 for (i = 0; i < mdr_info->
sec_count; i++) {
123 uint32_t flash_cmd, cur_per_clock;
134 if (!(cur_per_clock & 0x10)) {
135 LOG_ERROR(
"Target needs reset before flash operations");
149 goto reset_pg_and_lock;
157 goto reset_pg_and_lock;
159 if ((first == 0) && (last == (
bank->num_sectors - 1)) &&
162 goto reset_pg_and_lock;
166 for (
unsigned int i = first; i <= last; i++) {
167 for (j = 0; j < mdr_info->
sec_count; j++) {
170 goto reset_pg_and_lock;
175 goto reset_pg_and_lock;
179 goto reset_pg_and_lock;
183 goto reset_pg_and_lock;
187 goto reset_pg_and_lock;
208 uint32_t buffer_size = 16384;
217 static const uint8_t mdr32fx_flash_write_code[] = {
218 0x07, 0x68, 0x16, 0x68, 0x00, 0x2e, 0x2e, 0xd0, 0x55, 0x68, 0xb5, 0x42,
219 0xf9, 0xd0, 0x2e, 0x68, 0x44, 0x60, 0x86, 0x60, 0x17, 0x4e, 0x37, 0x43,
220 0x07, 0x60, 0x05, 0x26, 0x00, 0xf0, 0x25, 0xf8, 0x15, 0x4e, 0x37, 0x43,
221 0x07, 0x60, 0x0d, 0x26, 0x00, 0xf0, 0x1f, 0xf8, 0x80, 0x26, 0x37, 0x43,
222 0x07, 0x60, 0x3d, 0x26, 0x00, 0xf0, 0x19, 0xf8, 0x80, 0x26, 0xb7, 0x43,
223 0x07, 0x60, 0x0f, 0x4e, 0xb7, 0x43, 0x07, 0x60, 0x05, 0x26, 0x00, 0xf0,
224 0x10, 0xf8, 0x0d, 0x4e, 0xb7, 0x43, 0x07, 0x60, 0x04, 0x35, 0x04, 0x34,
225 0x9d, 0x42, 0x01, 0xd3, 0x15, 0x46, 0x08, 0x35, 0x55, 0x60, 0x01, 0x39,
226 0x00, 0x29, 0x00, 0xd0, 0xcd, 0xe7, 0x30, 0x46, 0x00, 0xbe, 0x01, 0x3e,
227 0x00, 0x2e, 0xfc, 0xd1, 0x70, 0x47, 0x00, 0x00, 0x40, 0x10, 0x00, 0x00,
228 0x00, 0x20, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00
234 LOG_WARNING(
"no working area available, can't do block memory writes");
239 sizeof(mdr32fx_flash_write_code), mdr32fx_flash_write_code);
247 if (buffer_size <= 256) {
252 LOG_WARNING(
"no large enough working area available, can't do block memory writes");
280 LOG_ERROR(
"flash write failed at address 0x%"PRIx32,
300 uint8_t *new_buffer =
NULL;
308 LOG_ERROR(
"offset 0x%" PRIx32
" breaks required 4-byte alignment",
offset);
318 new_buffer = malloc(
count + rem);
320 LOG_ERROR(
"odd number of bytes to write and no memory for padding buffer");
323 LOG_INFO(
"odd number of bytes to write, padding with 0xff");
326 new_buffer[
count++] = 0xff;
329 uint32_t flash_cmd, cur_per_clock;
338 LOG_ERROR(
"Target needs reset before flash operations");
353 goto reset_pg_and_lock;
361 goto reset_pg_and_lock;
369 LOG_WARNING(
"Can't use block writes, falling back to single memory accesses");
372 unsigned int page_mask = page_size - 1;
375 unsigned int cur_page =
offset & ~page_mask;
376 unsigned int bytes_to_write = cur_page + page_size -
offset;
377 if (
count < bytes_to_write)
378 bytes_to_write =
count;
382 for (i = 0; i < mdr_info->
sec_count; i++) {
385 goto reset_pg_and_lock;
391 goto reset_pg_and_lock;
396 goto reset_pg_and_lock;
399 (((
offset + j + i*4) & ~page_mask) == cur_page) &&
403 memcpy(&value,
buffer + j + i*4,
sizeof(uint32_t));
406 goto reset_pg_and_lock;
410 goto reset_pg_and_lock;
415 goto reset_pg_and_lock;
419 goto reset_pg_and_lock;
424 goto reset_pg_and_lock;
429 goto reset_pg_and_lock;
434 count -= bytes_to_write;
477 LOG_ERROR(
"offset 0x%" PRIx32
" breaks required 4-byte alignment",
offset);
482 LOG_ERROR(
"count 0x%" PRIx32
" breaks required 4-byte alignment",
count);
486 uint32_t flash_cmd, cur_per_clock;
494 LOG_ERROR(
"Target needs reset before flash operations");
515 goto reset_pg_and_lock;
517 for (uint32_t i = 0; i <
count; i += 4) {
520 goto reset_pg_and_lock;
525 goto reset_pg_and_lock;
530 goto reset_pg_and_lock;
536 goto reset_pg_and_lock;
569 bank->sectors[i].offset = i * page_size;
570 bank->sectors[i].size = page_size;
571 bank->sectors[i].is_erased = -1;
572 bank->sectors[i].is_protected = 0;
592 mdr_info->
mem_type ?
"info memory" :
"main memory");
599 .usage =
"flash bank <name> mdr <base> <size> 0 0 <target#> <type> <page_count> <sec_count>\n"
600 "<type>: 0 for main memory, 1 for info memory",
601 .flash_bank_command = mdr_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_sameline(struct command_invocation *cmd, const char *format,...)
#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 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.
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define MD_PER_CLOCK_RST_CLK
static int mdr_probe(struct flash_bank *bank)
static int mdr_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static int mdr_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
static int mdr_auto_probe(struct flash_bank *bank)
#define MD_PER_CLOCK_EEPROM
static int mdr_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static int mdr_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
FLASH_BANK_COMMAND_HANDLER(mdr_flash_bank_command)
static int mdr_mass_erase(struct flash_bank *bank)
static int get_mdr_info(struct flash_bank *bank, struct command_invocation *cmd)
const struct flash_driver mdr_flash
unsigned int common_magic
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.
int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
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)
#define ERROR_TARGET_NOT_HALTED
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE