28 #define FLASH_REG_BASE_B0 0x40022000
29 #define FLASH_REG_BASE_B1 0x40022040
31 #define STM32_FLASH_ACR 0x00
32 #define STM32_FLASH_KEYR 0x04
33 #define STM32_FLASH_OPTKEYR 0x08
34 #define STM32_FLASH_SR 0x0C
35 #define STM32_FLASH_CR 0x10
36 #define STM32_FLASH_AR 0x14
37 #define STM32_FLASH_OBR 0x1C
38 #define STM32_FLASH_WRPR 0x20
43 #define STM32_FLASH_ACR_B0 0x40022000
44 #define STM32_FLASH_KEYR_B0 0x40022004
45 #define STM32_FLASH_OPTKEYR_B0 0x40022008
46 #define STM32_FLASH_SR_B0 0x4002200C
47 #define STM32_FLASH_CR_B0 0x40022010
48 #define STM32_FLASH_AR_B0 0x40022014
49 #define STM32_FLASH_OBR_B0 0x4002201C
50 #define STM32_FLASH_WRPR_B0 0x40022020
54 #define STM32_OB_RDP 0x1FFFF800
55 #define STM32_OB_USER 0x1FFFF802
56 #define STM32_OB_DATA0 0x1FFFF804
57 #define STM32_OB_DATA1 0x1FFFF806
58 #define STM32_OB_WRP0 0x1FFFF808
59 #define STM32_OB_WRP1 0x1FFFF80A
60 #define STM32_OB_WRP2 0x1FFFF80C
61 #define STM32_OB_WRP3 0x1FFFF80E
65 #define FLASH_PG (1 << 0)
66 #define FLASH_PER (1 << 1)
67 #define FLASH_MER (1 << 2)
68 #define FLASH_OPTPG (1 << 4)
69 #define FLASH_OPTER (1 << 5)
70 #define FLASH_STRT (1 << 6)
71 #define FLASH_LOCK (1 << 7)
72 #define FLASH_OPTWRE (1 << 9)
73 #define FLASH_OBL_LAUNCH (1 << 13)
77 #define FLASH_BSY (1 << 0)
78 #define FLASH_PGERR (1 << 2)
79 #define FLASH_WRPRTERR (1 << 4)
80 #define FLASH_EOP (1 << 5)
87 #define OPT_RDRSTSTOP 3
88 #define OPT_RDRSTSTDBY 4
93 #define KEY1 0x45670123
94 #define KEY2 0xCDEF89AB
98 #define FLASH_WRITE_TIMEOUT 10
99 #define FLASH_ERASE_TIMEOUT 100
125 uint32_t
address, uint32_t hwords_count);
138 bank->driver_priv = stm32x_info;
139 stm32x_info->
probed =
false;
146 bank->write_start_alignment =
bank->write_end_alignment = 2;
178 LOG_ERROR(
"timed out waiting for flash");
190 LOG_ERROR(
"stm32x device programming failed / flash not erased");
212 LOG_ERROR(
"Option byte operations must use bank 0");
223 uint32_t option_bytes;
295 stm32x_info =
bank->driver_priv;
318 uint8_t opt_bytes[16];
361 for (
unsigned int i = 0; i <
bank->num_prot_blocks; i++)
362 bank->prot_blocks[i].is_protected = (
protection & (1 << i)) ? 0 : 1;
377 if ((first == 0) && (last == (
bank->num_sectors - 1)))
388 for (
unsigned int i = first; i <= last; i++) {
393 bank->base +
bank->sectors[i].offset);
432 LOG_ERROR(
"stm32x failed to erase options");
436 for (
unsigned int i = first; i <= last; i++) {
447 uint32_t
address, uint32_t hwords_count)
457 static const uint8_t stm32x_flash_write_code[] = {
458 #include "../../../contrib/loaders/flash/stm32/stm32f1x.inc"
464 LOG_WARNING(
"no working area available, can't do block memory writes");
469 sizeof(stm32x_flash_write_code), stm32x_flash_write_code);
489 LOG_WARNING(
"no large enough working area available, can't do block memory writes");
531 LOG_ERROR(
"flash write failed just before address 0x%"PRIx32,
535 for (
unsigned int i = 0; i <
ARRAY_SIZE(reg_params); i++)
545 uint32_t
address, uint32_t hwords_count)
551 static const uint8_t gd32vf103_flash_write_code[] = {
552 #include "../../../contrib/loaders/flash/gd32vf103/gd32vf103.inc"
558 LOG_WARNING(
"no working area available, can't do block memory writes");
563 sizeof(gd32vf103_flash_write_code), gd32vf103_flash_write_code);
577 LOG_WARNING(
"no large enough working area available, can't do block memory writes");
591 while (hwords_count > 0) {
592 uint32_t thisrun_hwords =
source->size / 2;
595 if (thisrun_hwords > hwords_count)
596 thisrun_hwords = hwords_count;
600 thisrun_hwords * 2,
buffer);
617 write_algorithm->
address +
sizeof(gd32vf103_flash_write_code) - 4,
618 10000, &algorithm_info);
622 write_algorithm->
address, retval);
631 LOG_ERROR(
"flash write failed at address 0x%"PRIx32,
637 buffer += thisrun_hwords * 2;
639 hwords_count -= thisrun_hwords;
642 for (
unsigned int i = 0; i <
ARRAY_SIZE(reg_params); i++)
656 const uint8_t *
buffer, uint32_t
address, uint32_t hwords_count)
678 LOG_WARNING(
"couldn't use block writes, falling back to single memory accesses");
680 while (hwords_count > 0) {
711 assert(
count % 2 == 0);
721 goto reset_pg_and_lock;
726 goto reset_pg_and_lock;
754 addr->device_id = 0x40015800;
755 addr->flash_size = 0x1FFFF7CC;
758 addr->device_id = 0xE0042000;
759 addr->flash_size = 0x1FFFF7E0;
762 addr->device_id = 0xE0042000;
763 addr->flash_size = 0x1FFFF7CC;
766 addr->device_id = 0x40015800;
767 addr->flash_size = 0x1FFFF7E0;
776 addr->device_id = 0xE0042000;
777 addr->flash_size = 0x1FFFF7E0;
782 LOG_ERROR(
"Cannot identify target as a stm32x");
814 uint16_t flash_size_in_kb;
815 uint16_t max_flash_size_in_kb;
816 uint32_t dbgmcu_idcode;
818 uint32_t base_address = 0x08000000;
820 stm32x_info->
probed =
false;
833 LOG_INFO(
"device id = 0x%08" PRIx32, dbgmcu_idcode);
835 uint16_t device_id = dbgmcu_idcode & 0xfff;
836 uint16_t rev_id = dbgmcu_idcode >> 16;
843 max_flash_size_in_kb = 64;
853 max_flash_size_in_kb = 32;
862 max_flash_size_in_kb = 128;
871 max_flash_size_in_kb = 256;
880 max_flash_size_in_kb = 128;
893 max_flash_size_in_kb = 64;
906 max_flash_size_in_kb = 64;
914 max_flash_size_in_kb = 32;
919 max_flash_size_in_kb = 512;
924 max_flash_size_in_kb = 256;
929 max_flash_size_in_kb = 1024;
935 max_flash_size_in_kb = 128;
940 max_flash_size_in_kb = 512;
945 max_flash_size_in_kb = 256;
954 max_flash_size_in_kb = 512;
963 max_flash_size_in_kb = 256;
973 max_flash_size_in_kb = 64;
980 LOG_WARNING(
"Cannot identify target as a STM32 family.");
989 if (retval !=
ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
990 LOG_WARNING(
"STM32 flash size failed, probe inaccurate - assuming %dk flash",
991 max_flash_size_in_kb);
992 flash_size_in_kb = max_flash_size_in_kb;
997 if (
bank->base != 0x08080000) {
999 flash_size_in_kb = 512;
1001 flash_size_in_kb -= 512;
1004 base_address = 0x08080000;
1011 LOG_INFO(
"ignoring flash probed value, using configured bank size");
1015 LOG_INFO(
"flash size = %d KiB", flash_size_in_kb);
1018 assert(flash_size_in_kb != 0xffff);
1021 int num_pages = flash_size_in_kb * 1024 /
page_size;
1024 assert(num_pages > 0);
1026 free(
bank->sectors);
1029 free(
bank->prot_blocks);
1032 bank->base = base_address;
1035 bank->num_sectors = num_pages;
1041 int num_prot_blocks = num_pages / stm32x_info->
ppage_size;
1042 if (num_prot_blocks > 32)
1043 num_prot_blocks = 32;
1045 bank->num_prot_blocks = num_prot_blocks;
1047 if (!
bank->prot_blocks)
1050 if (num_prot_blocks == 32)
1053 stm32x_info->
probed =
true;
1075 const char *rev_str =
NULL;
1090 uint32_t dbgmcu_idcode;
1097 uint16_t device_id = dbgmcu_idcode & 0xfff;
1098 uint16_t rev_id = dbgmcu_idcode >> 16;
1099 const char *device_str;
1100 const char *rev_str =
NULL;
1102 switch (device_id) {
1104 device_str =
"STM32F10x (Medium Density)";
1112 device_str =
"GD32F1x0";
1116 device_str =
"GD32F3x0";
1120 device_str =
"GD32VF103";
1124 device_str =
"GD32E23x";
1142 device_str =
"STM32F10x (Low Density)";
1152 device_str =
"STM32F10x (High Density)";
1170 device_str =
"STM32F10x (Connectivity)";
1184 device_str =
"STM32F100 (Low/Medium Density)";
1198 device_str =
"STM32F302xB/C";
1220 device_str =
"STM32F100 (High Density)";
1234 device_str =
"STM32F10x (XL Density)";
1244 device_str =
"STM32F37x";
1258 device_str =
"STM32F33x";
1268 device_str =
"STM32F302x6/8";
1282 device_str =
"STM32F03x";
1287 device_str =
"STM32F05x";
1292 device_str =
"STM32F04x";
1297 device_str =
"STM32F303xD/E";
1306 device_str =
"STM32F07x";
1311 device_str =
"STM32F09x";
1341 stm32x_info =
bank->driver_priv;
1406 "INFO: a reset or power cycle is required "
1407 "for the new settings to take effect.");
1426 stm32x_info =
bank->driver_priv;
1456 (optionbyte & (1 <<
OPT_READOUT)) ?
"on" :
"off");
1462 (optionbyte & (1 <<
OPT_RDWDGSW)) ?
"soft" :
"hard");
1493 stm32x_info =
bank->driver_priv;
1519 if (strcmp(
"SWWDG",
CMD_ARGV[0]) == 0)
1520 optionbyte |= (1 << 0);
1521 else if (strcmp(
"HWWDG",
CMD_ARGV[0]) == 0)
1522 optionbyte &= ~(1 << 0);
1523 else if (strcmp(
"NORSTSTOP",
CMD_ARGV[0]) == 0)
1524 optionbyte |= (1 << 1);
1525 else if (strcmp(
"RSTSTOP",
CMD_ARGV[0]) == 0)
1526 optionbyte &= ~(1 << 1);
1527 else if (strcmp(
"NORSTSTNDBY",
CMD_ARGV[0]) == 0)
1528 optionbyte |= (1 << 2);
1529 else if (strcmp(
"RSTSTNDBY",
CMD_ARGV[0]) == 0)
1530 optionbyte &= ~(1 << 2);
1531 else if (strcmp(
"USEROPT",
CMD_ARGV[0]) == 0) {
1538 if (strcmp(
"BOOT0",
CMD_ARGV[0]) == 0)
1539 optionbyte |= (1 << 3);
1540 else if (strcmp(
"BOOT1",
CMD_ARGV[0]) == 0)
1541 optionbyte &= ~(1 << 3);
1564 "INFO: %spower cycle is required "
1565 "for the new settings to take effect.",
1567 ?
"'stm32f1x options_load' command or " :
"");
1585 LOG_ERROR(
"Command not applicable to stm32f1x devices - power cycle is "
1586 "required instead.");
1678 .handler = stm32x_handle_lock_command,
1681 .help =
"Lock entire flash device.",
1685 .handler = stm32x_handle_unlock_command,
1688 .help =
"Unlock entire protected flash device.",
1691 .name =
"mass_erase",
1692 .handler = stm32x_handle_mass_erase_command,
1695 .help =
"Erase entire flash device.",
1698 .name =
"options_read",
1699 .handler = stm32x_handle_options_read_command,
1702 .help =
"Read and display device option bytes.",
1705 .name =
"options_write",
1706 .handler = stm32x_handle_options_write_command,
1708 .usage =
"bank_id ('SWWDG'|'HWWDG') "
1709 "('RSTSTNDBY'|'NORSTSTNDBY') "
1710 "('RSTSTOP'|'NORSTSTOP') ('USEROPT' user_data)",
1711 .help =
"Replace bits in device option bytes.",
1714 .name =
"options_load",
1715 .handler = stm32x_handle_options_load_command,
1718 .help =
"Force re-load of device option bytes.",
1727 .help =
"stm32f1x flash command group",
1737 .flash_bank_command = stm32x_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)
static bool is_arm(struct arm *arm)
static struct arm * target_to_arm(const struct target *target)
Convert target handle to generic ARM target state handle.
#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,...)
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 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 COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
static enum cortex_m_impl_part cortex_m_get_impl_part(struct target *target)
@ CORTEX_M_PARTNO_INVALID
uint64_t buffer
Pointer to data buffer to send over SPI.
uint32_t page_size
Page size.
uint32_t buffer_size
Size of dw_spi_program::buffer.
uint32_t address
Starting address. Sector aligned.
#define ERROR_FLASH_PROTECTED
#define ERROR_FLASH_OPERATION_FAILED
struct flash_sector * alloc_block_array(uint32_t offset, uint32_t size, unsigned int num_blocks)
Allocate and fill an array of sectors or protection blocks.
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 RISCV_COMMON_MAGIC
target_addr_t addr
Start address to search for the control block.
static int stm32x_get_property_addr(struct target *target, struct stm32x_property_addr *addr)
static int stm32x_auto_probe(struct flash_bank *bank)
static const struct command_registration stm32f1x_exec_command_handlers[]
static int stm32x_probe(struct flash_bank *bank)
#define FLASH_ERASE_TIMEOUT
static const struct command_registration stm32f1x_command_handlers[]
static int get_stm32x_info(struct flash_bank *bank, struct command_invocation *cmd)
#define STM32_FLASH_OBR_B0
static int stm32x_get_flash_status(struct flash_bank *bank, uint32_t *status)
static int stm32x_get_flash_reg(struct flash_bank *bank, uint32_t reg)
static int stm32x_check_operation_supported(struct flash_bank *bank)
static int stm32x_get_flash_size(struct flash_bank *bank, uint16_t *flash_size_in_kb)
static int stm32x_erase_options(struct flash_bank *bank)
FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command)
static int stm32x_read_options(struct flash_bank *bank)
#define STM32_FLASH_WRPR_B0
#define STM32_FLASH_OPTKEYR_B0
static int stm32x_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static int stm32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
#define FLASH_REG_BASE_B0
#define STM32_FLASH_KEYR_B0
static int stm32x_write_options(struct flash_bank *bank)
COMMAND_HANDLER(stm32x_handle_lock_command)
static int stm32x_write_block_riscv(struct flash_bank *bank, const uint8_t *buffer, uint32_t address, uint32_t hwords_count)
static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t address, uint32_t hwords_count)
Writes a block to flash either using target algorithm or use fallback, host controlled halfword-by-ha...
static int stm32x_wait_status_busy(struct flash_bank *bank, int timeout)
static int stm32x_mass_erase(struct flash_bank *bank)
static int stm32x_write_block_async(struct flash_bank *bank, const uint8_t *buffer, uint32_t address, uint32_t hwords_count)
static int stm32x_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static const char * get_stm32f0_revision(uint16_t rev_id)
static int stm32x_protect_check(struct flash_bank *bank)
const struct flash_driver stm32f1x_flash
#define FLASH_REG_BASE_B1
static int stm32x_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
#define STM32_FLASH_CR_B0
Represents a generic ARM core, with standard application registers.
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...
Architecture info for RISC-V algorithms.
unsigned int common_magic
struct stm32x_options option_bytes
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
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.
unsigned int target_address_bits(struct target *target)
Return the number of address bits this target supports.
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
uint32_t target_get_working_area_avail(struct target *target)
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_read_u16(struct target *target, target_addr_t address, uint16_t *value)
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 bool target_was_examined(const struct target *target)
#define ERROR_TARGET_NOT_EXAMINED
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.