20 #define FLC_ADDR 0x00000000
21 #define FLC_CLKDIV 0x00000004
22 #define FLC_CN 0x00000008
23 #define FLC_PR1E_ADDR 0x0000000C
24 #define FLC_PR2S_ADDR 0x00000010
25 #define FLC_PR2E_ADDR 0x00000014
26 #define FLC_PR3S_ADDR 0x00000018
27 #define FLC_PR3E_ADDR 0x0000001C
28 #define FLC_MD 0x00000020
29 #define FLC_INT 0x00000024
30 #define FLC_DATA0 0x00000030
31 #define FLC_DATA1 0x00000034
32 #define FLC_DATA2 0x00000038
33 #define FLC_DATA3 0x0000003C
34 #define FLC_BL_CTRL 0x00000170
35 #define FLC_PROT 0x00000300
37 #define ARM_PID_REG 0xE00FFFE0
38 #define MAX326XX_ID_REG 0x40000838
41 #define FLC_INT_AF 0x00000002
43 #define FLC_CN_UNLOCK_MASK 0xF0000000
44 #define FLC_CN_UNLOCK_VALUE 0x20000000
46 #define FLC_CN_PEND 0x01000000
47 #define FLC_CN_ERASE_CODE_MASK 0x0000FF00
48 #define FLC_CN_ERASE_CODE_PGE 0x00005500
49 #define FLC_CN_ERASE_CODE_ME 0x0000AA00
50 #define FLC_CN_32BIT 0x00000010
51 #define FLC_CN_PGE 0x00000004
52 #define FLC_CN_ME 0x00000002
53 #define FLC_CN_WR 0x00000001
54 #define FLC_CN_PGE 0x00000004
55 #define FLC_CN_ME 0x00000002
56 #define FLC_CN_WR 0x00000001
58 #define FLC_BL_CTRL_23 0x00020000
59 #define FLC_BL_CTRL_IFREN 0x00000001
61 #define MASK_FLASH_BUSY (0x048800E0 & ~0x04000000)
62 #define MASK_DISABLE_INTS (0xFFFFFCFC)
63 #define MASK_FLASH_UNLOCKED (0xF588FFEF & ~0x04000000)
64 #define MASK_FLASH_LOCK (0xF588FFEF & ~0x04000000)
65 #define MASK_FLASH_ERASE (0xF588FFEF & ~0x04000000)
66 #define MASK_FLASH_ERASED (0xF48800EB & ~0x04000000)
67 #define MASK_ACCESS_VIOLATIONS (0xFFFFFCFC)
68 #define MASK_FLASH_WRITE (0xF588FFEF & ~0x04000000)
69 #define MASK_WRITE_ALIGNED (0xF588FFEF & ~0x04000000)
70 #define MASK_WRITE_COMPLETE (0xF488FFEE & ~0x04000000)
71 #define MASK_WRITE_BURST (0xF588FFEF & ~0x04000000)
72 #define MASK_BURST_COMPLETE (0xF488FFEE & ~0x04000000)
73 #define MASK_WRITE_REMAINING (0xF588FFEF & ~0x04000000)
74 #define MASK_REMAINING_COMPLETE (0xF488FFEE & ~0x04000000)
75 #define MASK_MASS_ERASE (0xF588FFEF & ~0x04000000)
76 #define MASK_ERASE_COMPLETE (0xF48800ED & ~0x04000000)
78 #define ARM_PID_DEFAULT_CM3 0x0000B4C3
79 #define ARM_PID_DEFAULT_CM4 0x0000B4C4
80 #define MAX326XX_ID 0x0000004D
82 #define OPTIONS_128 0x01
83 #define OPTIONS_ENC 0x02
84 #define OPTIONS_AUTH 0x04
85 #define OPTIONS_COUNT 0x08
86 #define OPTIONS_INTER 0x10
87 #define OPTIONS_RELATIVE_XOR 0x20
88 #define OPTIONS_KEYSIZE 0x40
104 #include "../../../contrib/loaders/flash/max32xxx/max32xxx_write_arm.inc"
112 LOG_ERROR(
"incorrect flash bank max32xxx configuration: <base> <size> 0 0 <target> <FLC base> <sector size> <clkdiv> <options>");
169 LOG_ERROR(
"Read failure on FLC_BL_CTRL");
173 LOG_WARNING(
"FLC_BL_CTRL indicates BL mode 2 or mode 3.");
175 LOG_WARNING(
"Flash page 0 swapped out, attempting to swap back in for programming");
179 LOG_ERROR(
"Write failure on FLC_BL_CTRL");
184 LOG_ERROR(
"Read failure on FLC_BL_CTRL");
188 LOG_ERROR(
"Unable to swap flash page 0 back in. Writes to page 0 will fail.");
230 if (!
info->max326xx) {
231 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
232 bank->sectors[i].is_protected = -1;
238 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
242 if (temp_reg & (0x1 << i % 32))
243 bank->sectors[i].is_protected = 1;
245 bank->sectors[i].is_protected = 0;
253 uint32_t flash_cn, flash_int;
267 if (last < first || last >=
bank->num_sectors)
270 if (first == 0 && last == (
bank->num_sectors - 1))
280 for (
unsigned int banknr = first; banknr <= last; banknr++) {
282 if (
bank->sectors[banknr].is_protected == 1) {
283 LOG_WARNING(
"Flash sector %u is protected", banknr);
308 LOG_ERROR(
"Timed out waiting for flash page erase @ 0x%08" PRIx32,
309 (banknr *
info->sector_size));
316 LOG_ERROR(
"Error erasing flash page %i", banknr);
324 LOG_ERROR(
"All pages protected %u to %u", first, last);
336 unsigned int first,
unsigned int last)
353 if (last < first || last >=
bank->num_sectors)
357 for (
unsigned int page = first; page <= last; page++) {
361 temp_reg |= (0x1 << page % 32);
363 bank->sectors[page].is_protected = 1;
367 temp_reg &= ~(0x1 << page % 32);
369 bank->sectors[page].is_protected = 0;
377 uint32_t
offset, uint32_t len)
390 static const unsigned int buf_min = 128;
402 LOG_DEBUG(
"bank=%p buffer=%p offset=%08" PRIx32
" len=%08" PRIx32
"",
407 LOG_DEBUG(
"no working area for block memory writes");
420 LOG_DEBUG(
"retry target_alloc_working_area(%s, size=%" PRIu32
")",
468 write_algorithm->address,
473 LOG_ERROR(
"error %d executing max32xxx flash write algorithm", retval);
490 uint32_t flash_cn, flash_int;
492 uint32_t remaining =
count;
501 LOG_DEBUG(
"bank=%p buffer=%p offset=%08" PRIx32
" count=%08" PRIx32
"",
509 LOG_ERROR(
"offset size must be 32-bit aligned");
514 LOG_ERROR(
"offset size must be 128-bit aligned");
530 if (remaining >= 16) {
537 LOG_ERROR(
"Must use algorithm in working area for encryption");
540 LOG_DEBUG(
"writing flash word-at-a-time");
549 remaining -= remaining;
559 while (remaining >= 4) {
572 LOG_ERROR(
"Timed out waiting for flash write @ 0x%08" PRIx32,
590 while (remaining >= 16) {
607 LOG_ERROR(
"Timed out waiting for flash write @ 0x%08" PRIx32,
625 uint8_t last_word[4] = {0xFF, 0xFF, 0xFF, 0xFF};
628 while (remaining > 0) {
664 uint8_t last_words[16] = {0xFF, 0xFF, 0xFF, 0xFF,
665 0xFF, 0xFF, 0xFF, 0xFF,
666 0xFF, 0xFF, 0xFF, 0xFF,
667 0xFF, 0xFF, 0xFF, 0xFF};
671 while (remaining > 0) {
672 last_words[i++] = *
buffer;
693 }
while ((--retry > 0) && (flash_cn &
FLC_CN_PEND));
727 bank->num_sectors =
info->flash_size /
info->sector_size;
730 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
731 bank->sectors[i].offset = i *
info->sector_size;
732 bank->sectors[i].size =
info->sector_size;
733 bank->sectors[i].is_erased = -1;
734 bank->sectors[i].is_protected = -1;
738 info->max326xx =
false;
741 arm_pid = (arm_id[1] << 8) + arm_id[0];
745 uint32_t max326xx_id;
747 LOG_DEBUG(
"max326xx_id = 0x%" PRIx32, max326xx_id);
748 max326xx_id = ((max326xx_id & 0xFF000000) >> 24);
750 info->max326xx =
true;
756 LOG_WARNING(
"Flash protection not supported on this device");
766 uint32_t flash_cn, flash_int;
780 bool protected =
true;
781 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
782 if (
bank->sectors[i].is_protected == 1)
815 LOG_ERROR(
"Timed out waiting for flash mass erase");
880 if (sscanf(
CMD_ARGV[2],
"0x%" SCNx32, &len) != 1) {
887 if ((
addr + len) >=
info->flash_size)
895 if (len %
info->sector_size)
896 len = len +
info->sector_size - (len %
info->sector_size);
900 len =
addr + (len /
info->sector_size) - 1;
936 if (sscanf(
CMD_ARGV[2],
"0x%" SCNx32, &len) != 1) {
943 if ((
addr + len) >=
info->flash_size)
951 if (len %
info->sector_size)
952 len = len +
info->sector_size - (len %
info->sector_size);
956 len =
addr + (len /
info->sector_size) - 1;
985 LOG_WARNING(
"Error updating the protection array");
989 LOG_WARNING(
"s:<sector number> a:<address> p:<protection bit>");
990 for (
unsigned int i = 0; i <
bank->num_sectors; i += 4) {
991 LOG_WARNING(
"s:%03d a:0x%06x p:%d | s:%03d a:0x%06x p:%d | s:%03d a:0x%06x p:%d | s:%03d a:0x%06x p:%d",
992 (i + 0), (i + 0) *
info->sector_size,
bank->sectors[(i + 0)].is_protected,
993 (i + 1), (i + 1) *
info->sector_size,
bank->sectors[(i + 1)].is_protected,
994 (i + 2), (i + 2) *
info->sector_size,
bank->sectors[(i + 2)].is_protected,
995 (i + 3), (i + 3) *
info->sector_size,
bank->sectors[(i + 3)].is_protected);
1003 .
name =
"mass_erase",
1004 .handler = max32xxx_handle_mass_erase_command,
1007 .help =
"mass erase flash",
1010 .name =
"protection_set",
1011 .handler = max32xxx_handle_protection_set_command,
1013 .usage =
"bank_id addr size",
1014 .help =
"set flash protection for address range",
1017 .name =
"protection_clr",
1018 .handler = max32xxx_handle_protection_clr_command,
1020 .usage =
"bank_id addr size",
1021 .help =
"clear flash protection for address range",
1024 .name =
"protection_check",
1025 .handler = max32xxx_handle_protection_check_command,
1028 .help =
"check flash protection",
1037 .help =
"max32xxx flash command group",
1047 .flash_bank_command = max32xxx_flash_bank_command,
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
void destroy_reg_param(struct reg_param *param)
void init_mem_param(struct mem_param *param, uint32_t address, uint32_t size, enum param_direction direction)
#define ARMV7M_COMMON_MAGIC
Support functions to access arbitrary bits in a byte array.
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,...)
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 CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#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.
uint32_t buffer_size
Size of dw_spi_program::buffer.
uint32_t address
Starting address. Sector aligned.
#define ERROR_FLASH_OPER_UNSUPPORTED
#define ERROR_FLASH_BANK_INVALID
#define ERROR_FLASH_SECTOR_INVALID
#define ERROR_FLASH_BANK_NOT_PROBED
#define ERROR_FLASH_OPERATION_FAILED
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
#define ERROR_FLASH_DST_OUT_OF_BANK
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_DEBUG(expr ...)
static int max32xxx_flash_op_post(struct flash_bank *bank)
static int max32xxx_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t len)
static int max32xxx_probe(struct flash_bank *bank)
static int max32xxx_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static const struct command_registration max32xxx_exec_command_handlers[]
static const struct command_registration max32xxx_command_handlers[]
#define FLC_CN_UNLOCK_MASK
static const uint8_t write_code_arm[]
static int max32xxx_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
static int max32xxx_mass_erase(struct flash_bank *bank)
#define FLC_CN_ERASE_CODE_ME
static int max32xxx_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static bool max32xxx_flash_busy(uint32_t flash_cn)
COMMAND_HANDLER(max32xxx_handle_mass_erase_command)
#define ARM_PID_DEFAULT_CM3
#define ARM_PID_DEFAULT_CM4
const struct flash_driver max32xxx_flash
static int get_info(struct flash_bank *bank, struct command_invocation *cmd)
#define FLC_CN_UNLOCK_VALUE
#define FLC_CN_ERASE_CODE_PGE
static int max32xxx_flash_op_pre(struct flash_bank *bank)
static int max32xxx_protect_check(struct flash_bank *bank)
FLASH_BANK_COMMAND_HANDLER(max32xxx_flash_bank_command)
#define FLC_BL_CTRL_IFREN
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...
const char * usage
a string listing the options and arguments, required or optional
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 clkdiv_value
const char * name
Name of this type of target.
struct target_type * type
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)
const char * target_type_name(const struct target *target)
Get the target type name.
#define ERROR_TARGET_NOT_HALTED
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
static struct ublast_lowlevel_priv info