26 #define FLASH_ACR 0x00
27 #define FLASH_PECR 0x04
28 #define FLASH_PDKEYR 0x08
29 #define FLASH_PEKEYR 0x0C
30 #define FLASH_PRGKEYR 0x10
31 #define FLASH_OPTKEYR 0x14
33 #define FLASH_OBR 0x1C
34 #define FLASH_WRPR 0x20
37 #define FLASH_ACR__LATENCY (1<<0)
38 #define FLASH_ACR__PRFTEN (1<<1)
39 #define FLASH_ACR__ACC64 (1<<2)
40 #define FLASH_ACR__SLEEP_PD (1<<3)
41 #define FLASH_ACR__RUN_PD (1<<4)
44 #define FLASH_PECR__PELOCK (1<<0)
45 #define FLASH_PECR__PRGLOCK (1<<1)
46 #define FLASH_PECR__OPTLOCK (1<<2)
47 #define FLASH_PECR__PROG (1<<3)
48 #define FLASH_PECR__DATA (1<<4)
49 #define FLASH_PECR__FTDW (1<<8)
50 #define FLASH_PECR__ERASE (1<<9)
51 #define FLASH_PECR__FPRG (1<<10)
52 #define FLASH_PECR__EOPIE (1<<16)
53 #define FLASH_PECR__ERRIE (1<<17)
54 #define FLASH_PECR__OBL_LAUNCH (1<<18)
57 #define FLASH_SR__BSY (1<<0)
58 #define FLASH_SR__EOP (1<<1)
59 #define FLASH_SR__ENDHV (1<<2)
60 #define FLASH_SR__READY (1<<3)
61 #define FLASH_SR__WRPERR (1<<8)
62 #define FLASH_SR__PGAERR (1<<9)
63 #define FLASH_SR__SIZERR (1<<10)
64 #define FLASH_SR__OPTVERR (1<<11)
67 #define PEKEY1 0x89ABCDEF
68 #define PEKEY2 0x02030405
69 #define PRGKEY1 0x8C9DAEBF
70 #define PRGKEY2 0x13141516
71 #define OPTKEY1 0xFBEAD9C8
72 #define OPTKEY2 0x24252627
75 #define DBGMCU_IDCODE 0xE0042000
76 #define DBGMCU_IDCODE_L0 0x40015800
79 #define FLASH_SECTOR_SIZE 4096
80 #define FLASH_BANK0_ADDRESS 0x08000000
83 #define OPTION_BYTES_ADDRESS 0x1FF80000
85 #define OPTION_BYTE_0_PR1 0xFFFF0000
86 #define OPTION_BYTE_0_PR0 0xFF5500AA
130 { 0x1000,
"A" }, { 0x1008,
"Y" }, { 0x1038,
"W" }, { 0x1078,
"V" },
133 { 0x1000,
"A" }, { 0x1008,
"Z" }, { 0x1018,
"Y" }, { 0x1038,
"X" }
136 { 0x1000,
"A" }, { 0x2000,
"B" }, { 0x2008,
"Y" }, { 0x2018,
"1, X" },
139 { 0x1000,
"A" }, { 0x1018,
"Y" }, { 0x1038,
"X" }, { 0x10f8,
"V" },
142 { 0x1000,
"A" }, { 0x1018,
"Z" },
145 { 0x1000,
"A" }, { 0x1008,
"Z" }, { 0x1018,
"Y" }, { 0x1038,
"X" },
151 { 0x1000,
"A" }, { 0x2000,
"B" }, { 0x2008,
"Z" },
154 { 0x1000,
"A" }, { 0x1008,
"Z" },
162 .device_str =
"STM32L1xx (Cat.1 - Low/Medium Density)",
164 .pages_per_sector = 16,
165 .max_flash_size_kb = 128,
166 .has_dual_banks =
false,
167 .flash_base = 0x40023C00,
168 .fsize_base = 0x1FF8004C,
174 .device_str =
"STM32L0xx (Cat. 3)",
176 .pages_per_sector = 32,
177 .max_flash_size_kb = 64,
178 .has_dual_banks =
false,
179 .flash_base = 0x40022000,
180 .fsize_base = 0x1FF8007C,
186 .device_str =
"STM32L0xx (Cat. 2)",
188 .pages_per_sector = 32,
189 .max_flash_size_kb = 32,
190 .has_dual_banks =
false,
191 .flash_base = 0x40022000,
192 .fsize_base = 0x1FF8007C,
198 .device_str =
"STM32L1xx (Cat.3 - Medium+ Density)",
200 .pages_per_sector = 16,
201 .max_flash_size_kb = 256,
202 .has_dual_banks =
false,
203 .flash_base = 0x40023C00,
204 .fsize_base = 0x1FF800CC,
210 .device_str =
"STM32L1xx (Cat.2)",
212 .pages_per_sector = 16,
213 .max_flash_size_kb = 128,
214 .has_dual_banks =
false,
215 .flash_base = 0x40023C00,
216 .fsize_base = 0x1FF8004C,
222 .device_str =
"STM32L1xx (Cat.4/Cat.3 - Medium+/High Density)",
224 .pages_per_sector = 16,
225 .max_flash_size_kb = 384,
226 .first_bank_size_kb = 192,
227 .has_dual_banks =
true,
228 .flash_base = 0x40023C00,
229 .fsize_base = 0x1FF800CC,
235 .device_str =
"STM32L1xx (Cat.5/Cat.6)",
237 .pages_per_sector = 16,
238 .max_flash_size_kb = 512,
239 .first_bank_size_kb = 0,
240 .has_dual_banks =
true,
241 .flash_base = 0x40023C00,
242 .fsize_base = 0x1FF800CC,
248 .device_str =
"STM32L0xx (Cat.5)",
250 .pages_per_sector = 32,
251 .max_flash_size_kb = 192,
252 .first_bank_size_kb = 0,
253 .has_dual_banks =
false,
254 .flash_base = 0x40022000,
255 .fsize_base = 0x1FF8007C,
261 .device_str =
"STM32L0xx (Cat.1)",
263 .pages_per_sector = 32,
264 .max_flash_size_kb = 16,
265 .has_dual_banks =
false,
266 .flash_base = 0x40022000,
267 .fsize_base = 0x1FF8007C,
280 stm32lx_info = calloc(1,
sizeof(*stm32lx_info));
284 LOG_ERROR(
"failed to allocate bank structure");
288 bank->driver_priv = stm32lx_info;
290 stm32lx_info->
probed =
false;
294 bank->default_padded_value =
bank->erased_value = 0x00;
395 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
397 bank->sectors[i].is_protected = 1;
399 bank->sectors[i].is_protected = 0;
422 for (
unsigned int i = first; i <= last; i++) {
426 bank->sectors[i].is_erased = 1;
448 static const uint8_t stm32lx_flash_write_code[] = {
449 #include "../../../contrib/loaders/flash/stm32/stm32lx.inc"
454 LOG_ERROR(
"The offset must be %" PRIu32
"B-aligned but it is %" PRIi32
"B)", hp_nb,
offset);
458 LOG_ERROR(
"The byte count must be %" PRIu32
"B-aligned but count is %" PRIu32
"B)", hp_nb,
count);
465 LOG_DEBUG(
"no working area for block memory writes");
472 sizeof(stm32lx_flash_write_code),
473 stm32lx_flash_write_code);
486 if (buffer_size <= stm32lx_info->part_info.page_size) {
491 LOG_WARNING(
"no large enough working area available, can't do block memory writes");
525 LOG_ERROR(
"unable to get armv7m target");
530 uint32_t demcr_save = armv7m->
demcr;
549 buf_set_u32(reg_params[2].value, 0, 32, this_count / hp_nb);
558 write_algorithm->
address, 0, 10000, &armv7m_info);
577 armv7m->
demcr = demcr_save;
590 LOG_WARNING(
"Couldn't use loader, falling back to page memory writes");
634 uint32_t halfpages_number;
635 uint32_t bytes_remaining = 0;
637 uint32_t bytes_written = 0;
646 LOG_ERROR(
"offset 0x%" PRIx32
" breaks required 4-byte alignment",
offset);
660 while (bytes_remaining > 0) {
661 uint8_t value[4] = {0xff, 0xff, 0xff, 0xff};
664 uint32_t bytes_to_write =
MIN(4, bytes_remaining);
665 memcpy(value,
buffer + bytes_written, bytes_to_write);
669 goto reset_pg_and_lock;
671 bytes_written += bytes_to_write;
672 bytes_remaining -= bytes_to_write;
677 goto reset_pg_and_lock;
681 count -= bytes_written;
684 assert((
offset % hp_nb) == 0);
687 halfpages_number =
count / hp_nb;
689 if (halfpages_number) {
693 LOG_WARNING(
"couldn't use block writes, falling back to single memory accesses");
694 halfpages_number = 0;
702 uint32_t page_bytes_written = hp_nb * halfpages_number;
703 bytes_written += page_bytes_written;
705 bytes_remaining =
count - page_bytes_written;
711 while (bytes_remaining > 0) {
712 uint8_t value[4] = {0xff, 0xff, 0xff, 0xff};
715 uint32_t bytes_to_write =
MIN(4, bytes_remaining);
716 memcpy(value,
buffer + bytes_written, bytes_to_write);
720 goto reset_pg_and_lock;
722 bytes_written += bytes_to_write;
723 bytes_remaining -= bytes_to_write;
728 goto reset_pg_and_lock;
755 uint16_t flash_size_in_kb;
758 uint32_t second_bank_base;
761 stm32lx_info->
probed =
false;
767 stm32lx_info->
idcode = device_id;
769 LOG_DEBUG(
"device id = 0x%08" PRIx32, device_id);
779 LOG_ERROR(
"Cannot identify target as an STM32 L0 or L1 family device.");
794 if (retval ==
ERROR_OK && (device_id & 0xfff) == 0x436) {
795 if (flash_size_in_kb == 0)
796 flash_size_in_kb = 384;
797 else if (flash_size_in_kb == 1)
798 flash_size_in_kb = 256;
802 if (retval ==
ERROR_OK && (device_id & 0xfff) == 0x429) {
803 flash_size_in_kb &= 0xff;
808 if (retval !=
ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
809 LOG_WARNING(
"STM32L flash size failed, probe inaccurate - assuming %dk flash",
813 LOG_WARNING(
"STM32L probed flash size assumed incorrect since FLASH_SIZE=%dk > %dk, - assuming %dk flash",
828 second_bank_base = base_address +
830 if (
bank->base == second_bank_base || !
bank->base) {
832 base_address = second_bank_base;
833 flash_size_in_kb = flash_size_in_kb -
835 }
else if (
bank->base == base_address) {
839 LOG_WARNING(
"STM32L flash bank base address config is incorrect. "
842 bank->base, base_address, second_bank_base);
845 LOG_INFO(
"STM32L flash has dual banks. Bank (%u) size is %dkb, base address is 0x%" PRIx32,
846 bank->bank_number, flash_size_in_kb, base_address);
848 LOG_INFO(
"STM32L flash size is %dkb, base address is 0x%" PRIx32, flash_size_in_kb, base_address);
855 LOG_INFO(
"ignoring flash probed value, using configured bank size: %dkbytes", flash_size_in_kb);
863 bank->size = flash_size_in_kb * 1024;
864 bank->base = base_address;
865 bank->num_sectors = num_sectors;
867 if (!
bank->sectors) {
868 LOG_ERROR(
"failed to allocate bank sectors");
872 for (
unsigned int i = 0; i < num_sectors; i++) {
875 bank->sectors[i].is_erased = -1;
876 bank->sectors[i].is_protected = -1;
879 stm32lx_info->
probed =
true;
899 uint16_t rev_id = stm32lx_info->
idcode >> 16;
900 const char *rev_str =
NULL;
902 if (!stm32lx_info->
probed) {
910 for (
unsigned int i = 0; i <
info->num_revs; i++)
911 if (rev_id ==
info->revs[i].rev)
912 rev_str =
info->revs[i].str;
925 .
name =
"mass_erase",
926 .handler = stm32lx_handle_mass_erase_command,
929 .help =
"Erase entire flash device. including available EEPROM",
933 .handler = stm32lx_handle_lock_command,
936 .help =
"Increase the readout protection to Level 1.",
940 .handler = stm32lx_handle_unlock_command,
943 .help =
"Lower the readout protection from Level 1 to 0.",
946 .name =
"option_load",
947 .handler = stm32lx_handle_option_load_command,
950 .help =
"Force re-load of device options (will cause device reset).",
959 .help =
"stm32lx flash command group",
969 .flash_bank_command = stm32lx_flash_bank_command,
1148 uint32_t
addr =
bank->base +
bank->sectors[sector].offset + (page
1240 LOG_ERROR(
"timed out waiting for flash");
1247 LOG_ERROR(
"access denied / write protected");
1282 }
while (--tries > 0 &&
1348 stm32lx_info =
bank->driver_priv;
1383 if (flash_size_in_kb == 192 || flash_size_in_kb == 128) {
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 struct armv7m_common * target_to_armv7m(struct target *target)
#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 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.
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_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.
void alive_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
target_addr_t addr
Start address to search for the control block.
#define FLASH_SR__OPTVERR
static const struct stm32lx_rev stm32_425_revs[]
static int stm32lx_protect_check(struct flash_bank *bank)
static int stm32lx_lock_program_memory(struct flash_bank *bank)
static const struct stm32lx_rev stm32_447_revs[]
static const struct stm32lx_rev stm32_436_revs[]
static const struct stm32lx_rev stm32_437_revs[]
#define OPTION_BYTES_ADDRESS
static int stm32lx_update_part_info(struct flash_bank *bank, uint16_t flash_size_in_kb)
static int stm32lx_auto_probe(struct flash_bank *bank)
#define OPTION_BYTE_0_PR0
#define FLASH_PECR__OPTLOCK
#define FLASH_PECR__ERASE
static int stm32lx_read_id_code(struct target *target, uint32_t *id)
#define FLASH_BANK0_ADDRESS
static const struct stm32lx_rev stm32_416_revs[]
static int stm32lx_unlock_program_memory(struct flash_bank *bank)
static int stm32lx_probe(struct flash_bank *bank)
static const struct stm32lx_rev stm32_457_revs[]
static int stm32lx_erase_sector(struct flash_bank *bank, int sector)
static int stm32lx_mass_erase(struct flash_bank *bank)
static const struct command_registration stm32lx_exec_command_handlers[]
static const struct stm32lx_rev stm32_427_revs[]
static int stm32lx_write_half_pages(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static const struct stm32lx_rev stm32_429_revs[]
#define FLASH_PECR__OBL_LAUNCH
static const struct command_registration stm32lx_command_handlers[]
FLASH_BANK_COMMAND_HANDLER(stm32lx_flash_bank_command)
static int stm32lx_wait_until_bsy_clear(struct flash_bank *bank)
static const struct stm32lx_rev stm32_417_revs[]
static int stm32lx_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static const struct stm32lx_part_info stm32lx_parts[]
static int stm32lx_unlock(struct flash_bank *bank)
#define FLASH_PECR__PELOCK
static int stm32lx_get_flash_status(struct flash_bank *bank, uint32_t *status)
#define OPTION_BYTE_0_PR1
const struct flash_driver stm32lx_flash
static int stm32lx_obl_launch(struct flash_bank *bank)
COMMAND_HANDLER(stm32lx_handle_mass_erase_command)
static int stm32lx_unlock_options_bytes(struct flash_bank *bank)
static int stm32lx_wait_until_bsy_clear_timeout(struct flash_bank *bank, int timeout)
#define FLASH_SECTOR_SIZE
static int stm32lx_enable_write_half_page(struct flash_bank *bank)
static int stm32lx_lock(struct flash_bank *bank)
#define FLASH_PECR__PRGLOCK
static int stm32lx_get_info(struct flash_bank *bank, struct command_invocation *cmd)
static int stm32lx_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
enum arm_arch arch
ARM architecture version.
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.
struct stm32lx_part_info part_info
uint16_t first_bank_size_kb
unsigned int pages_per_sector
const struct stm32lx_rev * revs
uint16_t max_flash_size_kb
int target_halt(struct target *target)
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.
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_poll(struct target *target)
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_u16(struct target *target, target_addr_t address, uint16_t *value)
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
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
static struct ublast_lowlevel_priv info