21 #define FLC_ADDR 0x00000000
22 #define FLC_CLKDIV 0x00000004
23 #define FLC_CN 0x00000008
24 #define FLC_PR1E_ADDR 0x0000000C
25 #define FLC_PR2S_ADDR 0x00000010
26 #define FLC_PR2E_ADDR 0x00000014
27 #define FLC_PR3S_ADDR 0x00000018
28 #define FLC_PR3E_ADDR 0x0000001C
29 #define FLC_MD 0x00000020
30 #define FLC_INT 0x00000024
31 #define FLC_DATA0 0x00000030
32 #define FLC_DATA1 0x00000034
33 #define FLC_DATA2 0x00000038
34 #define FLC_DATA3 0x0000003C
35 #define FLC_BL_CTRL 0x00000170
36 #define FLC_PROT 0x00000300
38 #define ARM_PID_REG 0xE00FFFE0
39 #define MAX326XX_ID_REG 0x40000838
42 #define FLC_INT_AF 0x00000002
44 #define FLC_CN_UNLOCK_MASK 0xF0000000
45 #define FLC_CN_UNLOCK_VALUE 0x20000000
47 #define FLC_CN_PEND 0x01000000
48 #define FLC_CN_ERASE_CODE_MASK 0x0000FF00
49 #define FLC_CN_ERASE_CODE_PGE 0x00005500
50 #define FLC_CN_ERASE_CODE_ME 0x0000AA00
51 #define FLC_CN_32BIT 0x00000010
52 #define FLC_CN_PGE 0x00000004
53 #define FLC_CN_ME 0x00000002
54 #define FLC_CN_WR 0x00000001
55 #define FLC_CN_PGE 0x00000004
56 #define FLC_CN_ME 0x00000002
57 #define FLC_CN_WR 0x00000001
59 #define FLC_BL_CTRL_23 0x00020000
60 #define FLC_BL_CTRL_IFREN 0x00000001
62 #define MASK_FLASH_BUSY (0x048800E0 & ~0x04000000)
63 #define MASK_DISABLE_INTS (0xFFFFFCFC)
64 #define MASK_FLASH_UNLOCKED (0xF588FFEF & ~0x04000000)
65 #define MASK_FLASH_LOCK (0xF588FFEF & ~0x04000000)
66 #define MASK_FLASH_ERASE (0xF588FFEF & ~0x04000000)
67 #define MASK_FLASH_ERASED (0xF48800EB & ~0x04000000)
68 #define MASK_ACCESS_VIOLATIONS (0xFFFFFCFC)
69 #define MASK_FLASH_WRITE (0xF588FFEF & ~0x04000000)
70 #define MASK_WRITE_ALIGNED (0xF588FFEF & ~0x04000000)
71 #define MASK_WRITE_COMPLETE (0xF488FFEE & ~0x04000000)
72 #define MASK_WRITE_BURST (0xF588FFEF & ~0x04000000)
73 #define MASK_BURST_COMPLETE (0xF488FFEE & ~0x04000000)
74 #define MASK_WRITE_REMAINING (0xF588FFEF & ~0x04000000)
75 #define MASK_REMAINING_COMPLETE (0xF488FFEE & ~0x04000000)
76 #define MASK_MASS_ERASE (0xF588FFEF & ~0x04000000)
77 #define MASK_ERASE_COMPLETE (0xF48800ED & ~0x04000000)
79 #define ARM_PID_DEFAULT_CM3 0x0000B4C3
80 #define ARM_PID_DEFAULT_CM4 0x0000B4C4
81 #define MAX326XX_ID 0x0000004D
97 #include "../../../contrib/loaders/flash/max32xxx/max32xxx_write_arm.inc"
105 LOG_ERROR(
"incorrect flash bank max32xxx configuration: <base> <size> 0 0 <target> <FLC base> <sector size> <clkdiv> <options>");
162 LOG_ERROR(
"Read failure on FLC_BL_CTRL");
166 LOG_WARNING(
"FLC_BL_CTRL indicates BL mode 2 or mode 3.");
168 LOG_WARNING(
"Flash page 0 swapped out, attempting to swap back in for programming");
172 LOG_ERROR(
"Write failure on FLC_BL_CTRL");
177 LOG_ERROR(
"Read failure on FLC_BL_CTRL");
181 LOG_ERROR(
"Unable to swap flash page 0 back in. Writes to page 0 will fail.");
223 if (!
info->max326xx) {
224 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
225 bank->sectors[i].is_protected = -1;
231 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
235 if (temp_reg & (0x1 << i % 32))
236 bank->sectors[i].is_protected = 1;
238 bank->sectors[i].is_protected = 0;
246 uint32_t flash_cn, flash_int;
260 if (last < first || last >=
bank->num_sectors)
263 if (first == 0 && last == (
bank->num_sectors - 1))
273 for (
unsigned int banknr = first; banknr <= last; banknr++) {
275 if (
bank->sectors[banknr].is_protected == 1) {
276 LOG_WARNING(
"Flash sector %u is protected", banknr);
301 LOG_ERROR(
"Timed out waiting for flash page erase @ 0x%08" PRIx32,
302 (banknr *
info->sector_size));
309 LOG_ERROR(
"Error erasing flash page %i", banknr);
317 LOG_ERROR(
"All pages protected %u to %u", first, last);
329 unsigned int first,
unsigned int last)
346 if (last < first || last >=
bank->num_sectors)
350 for (
unsigned int page = first; page <= last; page++) {
354 temp_reg |= (0x1 << page % 32);
356 bank->sectors[page].is_protected = 1;
360 temp_reg &= ~(0x1 << page % 32);
362 bank->sectors[page].is_protected = 0;
370 uint32_t
offset, uint32_t len)
383 static const unsigned int buf_min = 128;
395 LOG_DEBUG(
"bank=%p buffer=%p offset=%08" PRIx32
" len=%08" PRIx32
"",
400 LOG_DEBUG(
"no working area for block memory writes");
413 LOG_DEBUG(
"retry target_alloc_working_area(%s, size=%" PRIu32
")",
461 write_algorithm->address,
466 LOG_ERROR(
"error %d executing max32xxx flash write algorithm", retval);
483 uint32_t flash_cn, flash_int;
485 uint32_t remaining =
count;
494 LOG_DEBUG(
"bank=%p buffer=%p offset=%08" PRIx32
" count=%08" PRIx32
"",
502 LOG_ERROR(
"offset size must be 32-bit aligned");
507 LOG_ERROR(
"offset size must be 128-bit aligned");
523 if (remaining >= 16) {
530 LOG_ERROR(
"Must use algorithm in working area for encryption");
533 LOG_DEBUG(
"writing flash word-at-a-time");
542 remaining -= remaining;
552 while (remaining >= 4) {
565 LOG_ERROR(
"Timed out waiting for flash write @ 0x%08" PRIx32,
583 while (remaining >= 16) {
600 LOG_ERROR(
"Timed out waiting for flash write @ 0x%08" PRIx32,
618 uint8_t last_word[4] = {0xFF, 0xFF, 0xFF, 0xFF};
621 while (remaining > 0) {
657 uint8_t last_words[16] = {0xFF, 0xFF, 0xFF, 0xFF,
658 0xFF, 0xFF, 0xFF, 0xFF,
659 0xFF, 0xFF, 0xFF, 0xFF,
660 0xFF, 0xFF, 0xFF, 0xFF};
664 while (remaining > 0) {
665 last_words[i++] = *
buffer;
686 }
while ((--retry > 0) && (flash_cn &
FLC_CN_PEND));
720 bank->num_sectors =
info->flash_size /
info->sector_size;
723 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
724 bank->sectors[i].offset = i *
info->sector_size;
725 bank->sectors[i].size =
info->sector_size;
726 bank->sectors[i].is_erased = -1;
727 bank->sectors[i].is_protected = -1;
731 info->max326xx =
false;
734 arm_pid = (arm_id[1] << 8) + arm_id[0];
738 uint32_t max326xx_id;
740 LOG_DEBUG(
"max326xx_id = 0x%" PRIx32, max326xx_id);
741 max326xx_id = ((max326xx_id & 0xFF000000) >> 24);
743 info->max326xx =
true;
749 LOG_WARNING(
"Flash protection not supported on this device");
759 uint32_t flash_cn, flash_int;
773 bool protected =
true;
774 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
775 if (
bank->sectors[i].is_protected == 1)
808 LOG_ERROR(
"Timed out waiting for flash mass erase");
873 if (sscanf(
CMD_ARGV[2],
"0x%" SCNx32, &len) != 1) {
880 if ((
addr + len) >=
info->flash_size)
888 if (len %
info->sector_size)
889 len = len +
info->sector_size - (len %
info->sector_size);
893 len =
addr + (len /
info->sector_size) - 1;
929 if (sscanf(
CMD_ARGV[2],
"0x%" SCNx32, &len) != 1) {
936 if ((
addr + len) >=
info->flash_size)
944 if (len %
info->sector_size)
945 len = len +
info->sector_size - (len %
info->sector_size);
949 len =
addr + (len /
info->sector_size) - 1;
978 LOG_WARNING(
"Error updating the protection array");
982 LOG_WARNING(
"s:<sector number> a:<address> p:<protection bit>");
983 for (
unsigned int i = 0; i <
bank->num_sectors; i += 4) {
984 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",
985 (i + 0), (i + 0) *
info->sector_size,
bank->sectors[(i + 0)].is_protected,
986 (i + 1), (i + 1) *
info->sector_size,
bank->sectors[(i + 1)].is_protected,
987 (i + 2), (i + 2) *
info->sector_size,
bank->sectors[(i + 2)].is_protected,
988 (i + 3), (i + 3) *
info->sector_size,
bank->sectors[(i + 3)].is_protected);
996 .
name =
"mass_erase",
997 .handler = max32xxx_handle_mass_erase_command,
1000 .help =
"mass erase flash",
1003 .name =
"protection_set",
1004 .handler = max32xxx_handle_protection_set_command,
1006 .usage =
"bank_id addr size",
1007 .help =
"set flash protection for address range",
1010 .name =
"protection_clr",
1011 .handler = max32xxx_handle_protection_clr_command,
1013 .usage =
"bank_id addr size",
1014 .help =
"clear flash protection for address range",
1017 .name =
"protection_check",
1018 .handler = max32xxx_handle_protection_check_command,
1021 .help =
"check flash protection",
1030 .help =
"max32xxx flash command group",
1040 .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
static const uint8_t write_code[]
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