1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2011 by Clement Burin des Roziers *
9 * clement.burin-des-roziers@hikob.com *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
30 #include <helper/binarybuffer.h>
31 #include <target/algorithm.h>
32 #include <target/armv7m.h>
33 #include <target/cortex_m.h>
35 /* stm32lx flash register locations */
37 #define FLASH_ACR 0x00
38 #define FLASH_PECR 0x04
39 #define FLASH_PDKEYR 0x08
40 #define FLASH_PEKEYR 0x0C
41 #define FLASH_PRGKEYR 0x10
42 #define FLASH_OPTKEYR 0x14
44 #define FLASH_OBR 0x1C
45 #define FLASH_WRPR 0x20
48 #define FLASH_ACR__LATENCY (1<<0)
49 #define FLASH_ACR__PRFTEN (1<<1)
50 #define FLASH_ACR__ACC64 (1<<2)
51 #define FLASH_ACR__SLEEP_PD (1<<3)
52 #define FLASH_ACR__RUN_PD (1<<4)
55 #define FLASH_PECR__PELOCK (1<<0)
56 #define FLASH_PECR__PRGLOCK (1<<1)
57 #define FLASH_PECR__OPTLOCK (1<<2)
58 #define FLASH_PECR__PROG (1<<3)
59 #define FLASH_PECR__DATA (1<<4)
60 #define FLASH_PECR__FTDW (1<<8)
61 #define FLASH_PECR__ERASE (1<<9)
62 #define FLASH_PECR__FPRG (1<<10)
63 #define FLASH_PECR__EOPIE (1<<16)
64 #define FLASH_PECR__ERRIE (1<<17)
65 #define FLASH_PECR__OBL_LAUNCH (1<<18)
68 #define FLASH_SR__BSY (1<<0)
69 #define FLASH_SR__EOP (1<<1)
70 #define FLASH_SR__ENDHV (1<<2)
71 #define FLASH_SR__READY (1<<3)
72 #define FLASH_SR__WRPERR (1<<8)
73 #define FLASH_SR__PGAERR (1<<9)
74 #define FLASH_SR__SIZERR (1<<10)
75 #define FLASH_SR__OPTVERR (1<<11)
78 #define PEKEY1 0x89ABCDEF
79 #define PEKEY2 0x02030405
80 #define PRGKEY1 0x8C9DAEBF
81 #define PRGKEY2 0x13141516
82 #define OPTKEY1 0xFBEAD9C8
83 #define OPTKEY2 0x24252627
86 #define DBGMCU_IDCODE 0xE0042000
87 #define DBGMCU_IDCODE_L0 0x40015800
90 #define FLASH_SECTOR_SIZE 4096
91 #define FLASH_BANK0_ADDRESS 0x08000000
94 #define OPTION_BYTES_ADDRESS 0x1FF80000
96 #define OPTION_BYTE_0_PR1 0xFFFF0000
97 #define OPTION_BYTE_0_PR0 0xFF5500AA
99 static int stm32lx_unlock_program_memory(struct flash_bank *bank);
100 static int stm32lx_lock_program_memory(struct flash_bank *bank);
101 static int stm32lx_enable_write_half_page(struct flash_bank *bank);
102 static int stm32lx_erase_sector(struct flash_bank *bank, int sector);
103 static int stm32lx_wait_until_bsy_clear(struct flash_bank *bank);
104 static int stm32lx_lock(struct flash_bank *bank);
105 static int stm32lx_unlock(struct flash_bank *bank);
106 static int stm32lx_mass_erase(struct flash_bank *bank);
107 static int stm32lx_wait_until_bsy_clear_timeout(struct flash_bank *bank, int timeout);
108 static int stm32lx_update_part_info(struct flash_bank *bank, uint16_t flash_size_in_kb);
115 struct stm32lx_part_info {
117 const char *device_str;
118 const struct stm32lx_rev *revs;
120 unsigned int page_size;
121 unsigned int pages_per_sector;
122 uint16_t max_flash_size_kb;
123 uint16_t first_bank_size_kb; /* used when has_dual_banks is true */
126 uint32_t flash_base; /* Flash controller registers location */
127 uint32_t fsize_base; /* Location of FSIZE register */
130 struct stm32lx_flash_bank {
133 uint32_t user_bank_size;
136 struct stm32lx_part_info part_info;
139 static const struct stm32lx_rev stm32_416_revs[] = {
140 { 0x1000, "A" }, { 0x1008, "Y" }, { 0x1038, "W" }, { 0x1078, "V" },
142 static const struct stm32lx_rev stm32_417_revs[] = {
143 { 0x1000, "A" }, { 0x1008, "Z" }, { 0x1018, "Y" }, { 0x1038, "X" }
145 static const struct stm32lx_rev stm32_425_revs[] = {
146 { 0x1000, "A" }, { 0x2000, "B" }, { 0x2008, "Y" },
148 static const struct stm32lx_rev stm32_427_revs[] = {
149 { 0x1000, "A" }, { 0x1018, "Y" }, { 0x1038, "X" }, { 0x10f8, "V" },
151 static const struct stm32lx_rev stm32_429_revs[] = {
152 { 0x1000, "A" }, { 0x1018, "Z" },
154 static const struct stm32lx_rev stm32_436_revs[] = {
155 { 0x1000, "A" }, { 0x1008, "Z" }, { 0x1018, "Y" },
157 static const struct stm32lx_rev stm32_437_revs[] = {
160 static const struct stm32lx_rev stm32_447_revs[] = {
161 { 0x1000, "A" }, { 0x2000, "B" }, { 0x2008, "Z" },
163 static const struct stm32lx_rev stm32_457_revs[] = {
164 { 0x1000, "A" }, { 0x1008, "Z" },
167 static const struct stm32lx_part_info stm32lx_parts[] = {
170 .revs = stm32_416_revs,
171 .num_revs = ARRAY_SIZE(stm32_416_revs),
172 .device_str = "STM32L1xx (Cat.1 - Low/Medium Density)",
174 .pages_per_sector = 16,
175 .max_flash_size_kb = 128,
176 .has_dual_banks = false,
177 .flash_base = 0x40023C00,
178 .fsize_base = 0x1FF8004C,
182 .revs = stm32_417_revs,
183 .num_revs = ARRAY_SIZE(stm32_417_revs),
184 .device_str = "STM32L0xx (Cat. 3)",
186 .pages_per_sector = 32,
187 .max_flash_size_kb = 64,
188 .has_dual_banks = false,
189 .flash_base = 0x40022000,
190 .fsize_base = 0x1FF8007C,
194 .revs = stm32_425_revs,
195 .num_revs = ARRAY_SIZE(stm32_425_revs),
196 .device_str = "STM32L0xx (Cat. 2)",
198 .pages_per_sector = 32,
199 .max_flash_size_kb = 32,
200 .has_dual_banks = false,
201 .flash_base = 0x40022000,
202 .fsize_base = 0x1FF8007C,
206 .revs = stm32_427_revs,
207 .num_revs = ARRAY_SIZE(stm32_427_revs),
208 .device_str = "STM32L1xx (Cat.3 - Medium+ Density)",
210 .pages_per_sector = 16,
211 .max_flash_size_kb = 256,
212 .has_dual_banks = false,
213 .flash_base = 0x40023C00,
214 .fsize_base = 0x1FF800CC,
218 .revs = stm32_429_revs,
219 .num_revs = ARRAY_SIZE(stm32_429_revs),
220 .device_str = "STM32L1xx (Cat.2)",
222 .pages_per_sector = 16,
223 .max_flash_size_kb = 128,
224 .has_dual_banks = false,
225 .flash_base = 0x40023C00,
226 .fsize_base = 0x1FF8004C,
230 .revs = stm32_436_revs,
231 .num_revs = ARRAY_SIZE(stm32_436_revs),
232 .device_str = "STM32L1xx (Cat.4/Cat.3 - Medium+/High Density)",
234 .pages_per_sector = 16,
235 .max_flash_size_kb = 384,
236 .first_bank_size_kb = 192,
237 .has_dual_banks = true,
238 .flash_base = 0x40023C00,
239 .fsize_base = 0x1FF800CC,
243 .revs = stm32_437_revs,
244 .num_revs = ARRAY_SIZE(stm32_437_revs),
245 .device_str = "STM32L1xx (Cat.5/Cat.6)",
247 .pages_per_sector = 16,
248 .max_flash_size_kb = 512,
249 .first_bank_size_kb = 0, /* determined in runtime */
250 .has_dual_banks = true,
251 .flash_base = 0x40023C00,
252 .fsize_base = 0x1FF800CC,
256 .revs = stm32_447_revs,
257 .num_revs = ARRAY_SIZE(stm32_447_revs),
258 .device_str = "STM32L0xx (Cat.5)",
260 .pages_per_sector = 32,
261 .max_flash_size_kb = 192,
262 .first_bank_size_kb = 0, /* determined in runtime */
263 .has_dual_banks = false, /* determined in runtime */
264 .flash_base = 0x40022000,
265 .fsize_base = 0x1FF8007C,
269 .revs = stm32_457_revs,
270 .num_revs = ARRAY_SIZE(stm32_457_revs),
271 .device_str = "STM32L0xx (Cat.1)",
273 .pages_per_sector = 32,
274 .max_flash_size_kb = 16,
275 .has_dual_banks = false,
276 .flash_base = 0x40022000,
277 .fsize_base = 0x1FF8007C,
281 /* flash bank stm32lx <base> <size> 0 0 <target#>
283 FLASH_BANK_COMMAND_HANDLER(stm32lx_flash_bank_command)
285 struct stm32lx_flash_bank *stm32lx_info;
287 return ERROR_COMMAND_SYNTAX_ERROR;
289 /* Create the bank structure */
290 stm32lx_info = calloc(1, sizeof(*stm32lx_info));
292 /* Check allocation */
293 if (stm32lx_info == NULL) {
294 LOG_ERROR("failed to allocate bank structure");
298 bank->driver_priv = stm32lx_info;
300 stm32lx_info->probed = false;
301 stm32lx_info->user_bank_size = bank->size;
303 /* the stm32l erased value is 0x00 */
304 bank->default_padded_value = bank->erased_value = 0x00;
309 COMMAND_HANDLER(stm32lx_handle_mass_erase_command)
312 return ERROR_COMMAND_SYNTAX_ERROR;
314 struct flash_bank *bank;
315 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
316 if (ERROR_OK != retval)
319 retval = stm32lx_mass_erase(bank);
320 if (retval == ERROR_OK) {
321 /* set all sectors as erased */
322 for (unsigned int i = 0; i < bank->num_sectors; i++)
323 bank->sectors[i].is_erased = 1;
325 command_print(CMD, "stm32lx mass erase complete");
327 command_print(CMD, "stm32lx mass erase failed");
333 COMMAND_HANDLER(stm32lx_handle_lock_command)
336 return ERROR_COMMAND_SYNTAX_ERROR;
338 struct flash_bank *bank;
339 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
340 if (ERROR_OK != retval)
343 retval = stm32lx_lock(bank);
345 if (retval == ERROR_OK)
346 command_print(CMD, "STM32Lx locked, takes effect after power cycle.");
348 command_print(CMD, "STM32Lx lock failed");
353 COMMAND_HANDLER(stm32lx_handle_unlock_command)
356 return ERROR_COMMAND_SYNTAX_ERROR;
358 struct flash_bank *bank;
359 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
360 if (ERROR_OK != retval)
363 retval = stm32lx_unlock(bank);
365 if (retval == ERROR_OK)
366 command_print(CMD, "STM32Lx unlocked, takes effect after power cycle.");
368 command_print(CMD, "STM32Lx unlock failed");
373 static int stm32lx_protect_check(struct flash_bank *bank)
376 struct target *target = bank->target;
377 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
382 * Read the WRPR word, and check each bit (corresponding to each
385 retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_WRPR,
387 if (retval != ERROR_OK)
390 for (unsigned int i = 0; i < bank->num_sectors; i++) {
392 bank->sectors[i].is_protected = 1;
394 bank->sectors[i].is_protected = 0;
399 static int stm32lx_erase(struct flash_bank *bank, unsigned int first,
405 * It could be possible to do a mass erase if all sectors must be
406 * erased, but it is not implemented yet.
409 if (bank->target->state != TARGET_HALTED) {
410 LOG_ERROR("Target not halted");
411 return ERROR_TARGET_NOT_HALTED;
415 * Loop over the selected sectors and erase them
417 for (unsigned int i = first; i <= last; i++) {
418 retval = stm32lx_erase_sector(bank, i);
419 if (retval != ERROR_OK)
421 bank->sectors[i].is_erased = 1;
426 static int stm32lx_write_half_pages(struct flash_bank *bank, const uint8_t *buffer,
427 uint32_t offset, uint32_t count)
429 struct target *target = bank->target;
430 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
432 uint32_t hp_nb = stm32lx_info->part_info.page_size / 2;
433 uint32_t buffer_size = 16384;
434 struct working_area *write_algorithm;
435 struct working_area *source;
436 uint32_t address = bank->base + offset;
438 struct reg_param reg_params[3];
439 struct armv7m_algorithm armv7m_info;
441 int retval = ERROR_OK;
443 static const uint8_t stm32lx_flash_write_code[] = {
444 #include "../../../contrib/loaders/flash/stm32/stm32lx.inc"
447 /* Make sure we're performing a half-page aligned write. */
449 LOG_ERROR("The byte count must be %" PRIu32 "B-aligned but count is %" PRIu32 "B)", hp_nb, count);
453 /* flash write code */
454 if (target_alloc_working_area(target, sizeof(stm32lx_flash_write_code),
455 &write_algorithm) != ERROR_OK) {
456 LOG_DEBUG("no working area for block memory writes");
457 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
460 /* Write the flashing code */
461 retval = target_write_buffer(target,
462 write_algorithm->address,
463 sizeof(stm32lx_flash_write_code),
464 stm32lx_flash_write_code);
465 if (retval != ERROR_OK) {
466 target_free_working_area(target, write_algorithm);
470 /* Allocate half pages memory */
471 while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) {
472 if (buffer_size > 1024)
477 if (buffer_size <= stm32lx_info->part_info.page_size) {
478 /* we already allocated the writing code, but failed to get a
479 * buffer, free the algorithm */
480 target_free_working_area(target, write_algorithm);
482 LOG_WARNING("no large enough working area available, can't do block memory writes");
483 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
487 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
488 armv7m_info.core_mode = ARM_MODE_THREAD;
489 init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
490 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
491 init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
493 /* Enable half-page write */
494 retval = stm32lx_enable_write_half_page(bank);
495 if (retval != ERROR_OK) {
496 target_free_working_area(target, source);
497 target_free_working_area(target, write_algorithm);
499 destroy_reg_param(®_params[0]);
500 destroy_reg_param(®_params[1]);
501 destroy_reg_param(®_params[2]);
505 struct armv7m_common *armv7m = target_to_armv7m(target);
506 if (armv7m == NULL) {
508 /* something is very wrong if armv7m is NULL */
509 LOG_ERROR("unable to get armv7m target");
513 /* save any DEMCR flags and configure target to catch any Hard Faults */
514 uint32_t demcr_save = armv7m->demcr;
515 armv7m->demcr = VC_HARDERR;
517 /* Loop while there are bytes to write */
520 this_count = (count > buffer_size) ? buffer_size : count;
522 /* Write the next half pages */
523 retval = target_write_buffer(target, source->address, this_count, buffer);
524 if (retval != ERROR_OK)
527 /* 4: Store useful information in the registers */
528 /* the destination address of the copy (R0) */
529 buf_set_u32(reg_params[0].value, 0, 32, address);
530 /* The source address of the copy (R1) */
531 buf_set_u32(reg_params[1].value, 0, 32, source->address);
532 /* The length of the copy (R2) */
533 buf_set_u32(reg_params[2].value, 0, 32, this_count / 4);
535 /* 5: Execute the bunch of code */
536 retval = target_run_algorithm(target, 0, NULL,
537 ARRAY_SIZE(reg_params), reg_params,
538 write_algorithm->address, 0, 10000, &armv7m_info);
539 if (retval != ERROR_OK)
542 /* check for Hard Fault */
543 if (armv7m->exception_number == 3)
546 /* 6: Wait while busy */
547 retval = stm32lx_wait_until_bsy_clear(bank);
548 if (retval != ERROR_OK)
551 buffer += this_count;
552 address += this_count;
556 /* restore previous flags */
557 armv7m->demcr = demcr_save;
559 if (armv7m->exception_number == 3) {
561 /* the stm32l15x devices seem to have an issue when blank.
562 * if a ram loader is executed on a blank device it will
563 * Hard Fault, this issue does not happen for a already programmed device.
564 * A related issue is described in the stm32l151xx errata (Doc ID 17721 Rev 6 - 2.1.3).
565 * The workaround of handling the Hard Fault exception does work, but makes the
566 * loader more complicated, as a compromise we manually write the pages, programming time
567 * is reduced by 50% using this slower method.
570 LOG_WARNING("Couldn't use loader, falling back to page memory writes");
574 this_count = (count > hp_nb) ? hp_nb : count;
576 /* Write the next half pages */
577 retval = target_write_buffer(target, address, this_count, buffer);
578 if (retval != ERROR_OK)
581 /* Wait while busy */
582 retval = stm32lx_wait_until_bsy_clear(bank);
583 if (retval != ERROR_OK)
586 buffer += this_count;
587 address += this_count;
592 if (retval == ERROR_OK)
593 retval = stm32lx_lock_program_memory(bank);
595 target_free_working_area(target, source);
596 target_free_working_area(target, write_algorithm);
598 destroy_reg_param(®_params[0]);
599 destroy_reg_param(®_params[1]);
600 destroy_reg_param(®_params[2]);
605 static int stm32lx_write(struct flash_bank *bank, const uint8_t *buffer,
606 uint32_t offset, uint32_t count)
608 struct target *target = bank->target;
609 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
611 uint32_t hp_nb = stm32lx_info->part_info.page_size / 2;
612 uint32_t halfpages_number;
613 uint32_t bytes_remaining = 0;
614 uint32_t address = bank->base + offset;
615 uint32_t bytes_written = 0;
618 if (bank->target->state != TARGET_HALTED) {
619 LOG_ERROR("Target not halted");
620 return ERROR_TARGET_NOT_HALTED;
624 LOG_ERROR("offset 0x%" PRIx32 " breaks required 4-byte alignment", offset);
625 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
628 retval = stm32lx_unlock_program_memory(bank);
629 if (retval != ERROR_OK)
632 /* first we need to write any unaligned head bytes up to
633 * the next 128 byte page */
636 bytes_remaining = MIN(count, hp_nb - (offset % hp_nb));
638 while (bytes_remaining > 0) {
639 uint8_t value[4] = {0xff, 0xff, 0xff, 0xff};
641 /* copy remaining bytes into the write buffer */
642 uint32_t bytes_to_write = MIN(4, bytes_remaining);
643 memcpy(value, buffer + bytes_written, bytes_to_write);
645 retval = target_write_buffer(target, address, 4, value);
646 if (retval != ERROR_OK)
647 goto reset_pg_and_lock;
649 bytes_written += bytes_to_write;
650 bytes_remaining -= bytes_to_write;
653 retval = stm32lx_wait_until_bsy_clear(bank);
654 if (retval != ERROR_OK)
655 goto reset_pg_and_lock;
658 offset += bytes_written;
659 count -= bytes_written;
661 /* this should always pass this check here */
662 assert((offset % hp_nb) == 0);
664 /* calculate half pages */
665 halfpages_number = count / hp_nb;
667 if (halfpages_number) {
668 retval = stm32lx_write_half_pages(bank, buffer + bytes_written, offset, hp_nb * halfpages_number);
669 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
670 /* attempt slow memory writes */
671 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
672 halfpages_number = 0;
674 if (retval != ERROR_OK)
679 /* write any remaining bytes */
680 uint32_t page_bytes_written = hp_nb * halfpages_number;
681 bytes_written += page_bytes_written;
682 address += page_bytes_written;
683 bytes_remaining = count - page_bytes_written;
685 retval = stm32lx_unlock_program_memory(bank);
686 if (retval != ERROR_OK)
689 while (bytes_remaining > 0) {
690 uint8_t value[4] = {0xff, 0xff, 0xff, 0xff};
692 /* copy remaining bytes into the write buffer */
693 uint32_t bytes_to_write = MIN(4, bytes_remaining);
694 memcpy(value, buffer + bytes_written, bytes_to_write);
696 retval = target_write_buffer(target, address, 4, value);
697 if (retval != ERROR_OK)
698 goto reset_pg_and_lock;
700 bytes_written += bytes_to_write;
701 bytes_remaining -= bytes_to_write;
704 retval = stm32lx_wait_until_bsy_clear(bank);
705 if (retval != ERROR_OK)
706 goto reset_pg_and_lock;
710 retval2 = stm32lx_lock_program_memory(bank);
711 if (retval == ERROR_OK)
717 static int stm32lx_read_id_code(struct target *target, uint32_t *id)
719 struct armv7m_common *armv7m = target_to_armv7m(target);
721 if (armv7m->arm.is_armv6m == true)
722 retval = target_read_u32(target, DBGMCU_IDCODE_L0, id);
724 /* read stm32 device id register */
725 retval = target_read_u32(target, DBGMCU_IDCODE, id);
729 static int stm32lx_probe(struct flash_bank *bank)
731 struct target *target = bank->target;
732 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
733 uint16_t flash_size_in_kb;
735 uint32_t base_address = FLASH_BANK0_ADDRESS;
736 uint32_t second_bank_base;
739 stm32lx_info->probed = false;
741 int retval = stm32lx_read_id_code(bank->target, &device_id);
742 if (retval != ERROR_OK)
745 stm32lx_info->idcode = device_id;
747 LOG_DEBUG("device id = 0x%08" PRIx32 "", device_id);
749 for (n = 0; n < ARRAY_SIZE(stm32lx_parts); n++) {
750 if ((device_id & 0xfff) == stm32lx_parts[n].id) {
751 stm32lx_info->part_info = stm32lx_parts[n];
756 if (n == ARRAY_SIZE(stm32lx_parts)) {
757 LOG_ERROR("Cannot identify target as an STM32 L0 or L1 family device.");
760 LOG_INFO("Device: %s", stm32lx_info->part_info.device_str);
763 stm32lx_info->flash_base = stm32lx_info->part_info.flash_base;
765 /* Get the flash size from target. */
766 retval = target_read_u16(target, stm32lx_info->part_info.fsize_base,
769 /* 0x436 devices report their flash size as a 0 or 1 code indicating 384K
770 * or 256K, respectively. Please see RM0038 r8 or newer and refer to
772 if (retval == ERROR_OK && (device_id & 0xfff) == 0x436) {
773 if (flash_size_in_kb == 0)
774 flash_size_in_kb = 384;
775 else if (flash_size_in_kb == 1)
776 flash_size_in_kb = 256;
779 /* 0x429 devices only use the lowest 8 bits of the flash size register */
780 if (retval == ERROR_OK && (device_id & 0xfff) == 0x429) {
781 flash_size_in_kb &= 0xff;
784 /* Failed reading flash size or flash size invalid (early silicon),
785 * default to max target family */
786 if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
787 LOG_WARNING("STM32L flash size failed, probe inaccurate - assuming %dk flash",
788 stm32lx_info->part_info.max_flash_size_kb);
789 flash_size_in_kb = stm32lx_info->part_info.max_flash_size_kb;
790 } else if (flash_size_in_kb > stm32lx_info->part_info.max_flash_size_kb) {
791 LOG_WARNING("STM32L probed flash size assumed incorrect since FLASH_SIZE=%dk > %dk, - assuming %dk flash",
792 flash_size_in_kb, stm32lx_info->part_info.max_flash_size_kb,
793 stm32lx_info->part_info.max_flash_size_kb);
794 flash_size_in_kb = stm32lx_info->part_info.max_flash_size_kb;
797 /* Overwrite default dual-bank configuration */
798 retval = stm32lx_update_part_info(bank, flash_size_in_kb);
799 if (retval != ERROR_OK)
802 if (stm32lx_info->part_info.has_dual_banks) {
803 /* Use the configured base address to determine if this is the first or second flash bank.
804 * Verify that the base address is reasonably correct and determine the flash bank size
806 second_bank_base = base_address +
807 stm32lx_info->part_info.first_bank_size_kb * 1024;
808 if (bank->base == second_bank_base || !bank->base) {
809 /* This is the second bank */
810 base_address = second_bank_base;
811 flash_size_in_kb = flash_size_in_kb -
812 stm32lx_info->part_info.first_bank_size_kb;
813 } else if (bank->base == base_address) {
814 /* This is the first bank */
815 flash_size_in_kb = stm32lx_info->part_info.first_bank_size_kb;
817 LOG_WARNING("STM32L flash bank base address config is incorrect. "
818 TARGET_ADDR_FMT " but should rather be 0x%" PRIx32
820 bank->base, base_address, second_bank_base);
823 LOG_INFO("STM32L flash has dual banks. Bank (%u) size is %dkb, base address is 0x%" PRIx32,
824 bank->bank_number, flash_size_in_kb, base_address);
826 LOG_INFO("STM32L flash size is %dkb, base address is 0x%" PRIx32, flash_size_in_kb, base_address);
829 /* if the user sets the size manually then ignore the probed value
830 * this allows us to work around devices that have a invalid flash size register value */
831 if (stm32lx_info->user_bank_size) {
832 flash_size_in_kb = stm32lx_info->user_bank_size / 1024;
833 LOG_INFO("ignoring flash probed value, using configured bank size: %dkbytes", flash_size_in_kb);
836 /* calculate numbers of sectors (4kB per sector) */
837 unsigned int num_sectors = (flash_size_in_kb * 1024) / FLASH_SECTOR_SIZE;
841 bank->size = flash_size_in_kb * 1024;
842 bank->base = base_address;
843 bank->num_sectors = num_sectors;
844 bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
845 if (bank->sectors == NULL) {
846 LOG_ERROR("failed to allocate bank sectors");
850 for (unsigned int i = 0; i < num_sectors; i++) {
851 bank->sectors[i].offset = i * FLASH_SECTOR_SIZE;
852 bank->sectors[i].size = FLASH_SECTOR_SIZE;
853 bank->sectors[i].is_erased = -1;
854 bank->sectors[i].is_protected = -1;
857 stm32lx_info->probed = true;
862 static int stm32lx_auto_probe(struct flash_bank *bank)
864 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
866 if (stm32lx_info->probed)
869 return stm32lx_probe(bank);
872 /* This method must return a string displaying information about the bank */
873 static int stm32lx_get_info(struct flash_bank *bank, struct command_invocation *cmd)
875 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
876 const struct stm32lx_part_info *info = &stm32lx_info->part_info;
877 uint16_t rev_id = stm32lx_info->idcode >> 16;
878 const char *rev_str = NULL;
880 if (!stm32lx_info->probed) {
881 int retval = stm32lx_probe(bank);
882 if (retval != ERROR_OK) {
883 command_print_sameline(cmd, "Unable to find bank information.");
888 for (unsigned int i = 0; i < info->num_revs; i++)
889 if (rev_id == info->revs[i].rev)
890 rev_str = info->revs[i].str;
892 if (rev_str != NULL) {
893 command_print_sameline(cmd, "%s - Rev: %s", info->device_str, rev_str);
895 command_print_sameline(cmd, "%s - Rev: unknown (0x%04x)", info->device_str, rev_id);
901 static const struct command_registration stm32lx_exec_command_handlers[] = {
903 .name = "mass_erase",
904 .handler = stm32lx_handle_mass_erase_command,
905 .mode = COMMAND_EXEC,
907 .help = "Erase entire flash device. including available EEPROM",
911 .handler = stm32lx_handle_lock_command,
912 .mode = COMMAND_EXEC,
914 .help = "Increase the readout protection to Level 1.",
918 .handler = stm32lx_handle_unlock_command,
919 .mode = COMMAND_EXEC,
921 .help = "Lower the readout protection from Level 1 to 0.",
923 COMMAND_REGISTRATION_DONE
926 static const struct command_registration stm32lx_command_handlers[] = {
930 .help = "stm32lx flash command group",
932 .chain = stm32lx_exec_command_handlers,
934 COMMAND_REGISTRATION_DONE
937 const struct flash_driver stm32lx_flash = {
939 .commands = stm32lx_command_handlers,
940 .flash_bank_command = stm32lx_flash_bank_command,
941 .erase = stm32lx_erase,
942 .write = stm32lx_write,
943 .read = default_flash_read,
944 .probe = stm32lx_probe,
945 .auto_probe = stm32lx_auto_probe,
946 .erase_check = default_flash_blank_check,
947 .protect_check = stm32lx_protect_check,
948 .info = stm32lx_get_info,
949 .free_driver_priv = default_flash_free_driver_priv,
952 /* Static methods implementation */
953 static int stm32lx_unlock_program_memory(struct flash_bank *bank)
955 struct target *target = bank->target;
956 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
961 * Unlocking the program memory is done by unlocking the PECR,
962 * then by writing the 2 PRGKEY to the PRGKEYR register
965 /* check flash is not already unlocked */
966 retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
968 if (retval != ERROR_OK)
971 if ((reg32 & FLASH_PECR__PRGLOCK) == 0)
974 /* To unlock the PECR write the 2 PEKEY to the PEKEYR register */
975 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR,
977 if (retval != ERROR_OK)
980 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR,
982 if (retval != ERROR_OK)
985 /* Make sure it worked */
986 retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
988 if (retval != ERROR_OK)
991 if (reg32 & FLASH_PECR__PELOCK) {
992 LOG_ERROR("PELOCK is not cleared :(");
993 return ERROR_FLASH_OPERATION_FAILED;
996 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PRGKEYR,
998 if (retval != ERROR_OK)
1000 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PRGKEYR,
1002 if (retval != ERROR_OK)
1005 /* Make sure it worked */
1006 retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1008 if (retval != ERROR_OK)
1011 if (reg32 & FLASH_PECR__PRGLOCK) {
1012 LOG_ERROR("PRGLOCK is not cleared :(");
1013 return ERROR_FLASH_OPERATION_FAILED;
1019 static int stm32lx_enable_write_half_page(struct flash_bank *bank)
1021 struct target *target = bank->target;
1022 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1027 * Unlock the program memory, then set the FPRG bit in the PECR register.
1029 retval = stm32lx_unlock_program_memory(bank);
1030 if (retval != ERROR_OK)
1033 retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1035 if (retval != ERROR_OK)
1038 reg32 |= FLASH_PECR__FPRG;
1039 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1041 if (retval != ERROR_OK)
1044 retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1046 if (retval != ERROR_OK)
1049 reg32 |= FLASH_PECR__PROG;
1050 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1056 static int stm32lx_lock_program_memory(struct flash_bank *bank)
1058 struct target *target = bank->target;
1059 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1063 /* To lock the program memory, simply set the lock bit and lock PECR */
1065 retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1067 if (retval != ERROR_OK)
1070 reg32 |= FLASH_PECR__PRGLOCK;
1071 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1073 if (retval != ERROR_OK)
1076 retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1078 if (retval != ERROR_OK)
1081 reg32 |= FLASH_PECR__PELOCK;
1082 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1084 if (retval != ERROR_OK)
1090 static int stm32lx_erase_sector(struct flash_bank *bank, int sector)
1092 struct target *target = bank->target;
1093 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1098 * To erase a sector (i.e. stm32lx_info->part_info.pages_per_sector pages),
1099 * first unlock the memory, loop over the pages of this sector
1100 * and write 0x0 to its first word.
1103 retval = stm32lx_unlock_program_memory(bank);
1104 if (retval != ERROR_OK)
1107 for (int page = 0; page < (int)stm32lx_info->part_info.pages_per_sector;
1109 reg32 = FLASH_PECR__PROG | FLASH_PECR__ERASE;
1110 retval = target_write_u32(target,
1111 stm32lx_info->flash_base + FLASH_PECR, reg32);
1112 if (retval != ERROR_OK)
1115 retval = stm32lx_wait_until_bsy_clear(bank);
1116 if (retval != ERROR_OK)
1119 uint32_t addr = bank->base + bank->sectors[sector].offset + (page
1120 * stm32lx_info->part_info.page_size);
1121 retval = target_write_u32(target, addr, 0x0);
1122 if (retval != ERROR_OK)
1125 retval = stm32lx_wait_until_bsy_clear(bank);
1126 if (retval != ERROR_OK)
1130 retval = stm32lx_lock_program_memory(bank);
1131 if (retval != ERROR_OK)
1137 static inline int stm32lx_get_flash_status(struct flash_bank *bank, uint32_t *status)
1139 struct target *target = bank->target;
1140 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1142 return target_read_u32(target, stm32lx_info->flash_base + FLASH_SR, status);
1145 static int stm32lx_wait_until_bsy_clear(struct flash_bank *bank)
1147 return stm32lx_wait_until_bsy_clear_timeout(bank, 100);
1150 static int stm32lx_unlock_options_bytes(struct flash_bank *bank)
1152 struct target *target = bank->target;
1153 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1158 * Unlocking the options bytes is done by unlocking the PECR,
1159 * then by writing the 2 FLASH_PEKEYR to the FLASH_OPTKEYR register
1162 /* check flash is not already unlocked */
1163 retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR, ®32);
1164 if (retval != ERROR_OK)
1167 if ((reg32 & FLASH_PECR__OPTLOCK) == 0)
1170 if ((reg32 & FLASH_PECR__PELOCK) != 0) {
1172 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR, PEKEY1);
1173 if (retval != ERROR_OK)
1176 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR, PEKEY2);
1177 if (retval != ERROR_OK)
1181 /* To unlock the PECR write the 2 OPTKEY to the FLASH_OPTKEYR register */
1182 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_OPTKEYR, OPTKEY1);
1183 if (retval != ERROR_OK)
1186 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_OPTKEYR, OPTKEY2);
1187 if (retval != ERROR_OK)
1193 static int stm32lx_wait_until_bsy_clear_timeout(struct flash_bank *bank, int timeout)
1195 struct target *target = bank->target;
1196 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1198 int retval = ERROR_OK;
1200 /* wait for busy to clear */
1202 retval = stm32lx_get_flash_status(bank, &status);
1203 if (retval != ERROR_OK)
1206 LOG_DEBUG("status: 0x%" PRIx32 "", status);
1207 if ((status & FLASH_SR__BSY) == 0)
1210 if (timeout-- <= 0) {
1211 LOG_ERROR("timed out waiting for flash");
1217 if (status & FLASH_SR__WRPERR) {
1218 LOG_ERROR("access denied / write protected");
1219 retval = ERROR_FAIL;
1222 if (status & FLASH_SR__PGAERR) {
1223 LOG_ERROR("invalid program address");
1224 retval = ERROR_FAIL;
1227 /* Clear but report errors */
1228 if (status & FLASH_SR__OPTVERR) {
1229 /* If this operation fails, we ignore it and report the original retval */
1230 target_write_u32(target, stm32lx_info->flash_base + FLASH_SR, status & FLASH_SR__OPTVERR);
1236 static int stm32lx_obl_launch(struct flash_bank *bank)
1238 struct target *target = bank->target;
1239 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1242 /* This will fail as the target gets immediately rebooted */
1243 target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1244 FLASH_PECR__OBL_LAUNCH);
1248 target_halt(target);
1249 retval = target_poll(target);
1250 } while (--tries > 0 &&
1251 (retval != ERROR_OK || target->state != TARGET_HALTED));
1253 return tries ? ERROR_OK : ERROR_FAIL;
1256 static int stm32lx_lock(struct flash_bank *bank)
1259 struct target *target = bank->target;
1261 if (target->state != TARGET_HALTED) {
1262 LOG_ERROR("Target not halted");
1263 return ERROR_TARGET_NOT_HALTED;
1266 retval = stm32lx_unlock_options_bytes(bank);
1267 if (retval != ERROR_OK)
1270 /* set the RDP protection level to 1 */
1271 retval = target_write_u32(target, OPTION_BYTES_ADDRESS, OPTION_BYTE_0_PR1);
1272 if (retval != ERROR_OK)
1278 static int stm32lx_unlock(struct flash_bank *bank)
1281 struct target *target = bank->target;
1283 if (target->state != TARGET_HALTED) {
1284 LOG_ERROR("Target not halted");
1285 return ERROR_TARGET_NOT_HALTED;
1288 retval = stm32lx_unlock_options_bytes(bank);
1289 if (retval != ERROR_OK)
1292 /* set the RDP protection level to 0 */
1293 retval = target_write_u32(target, OPTION_BYTES_ADDRESS, OPTION_BYTE_0_PR0);
1294 if (retval != ERROR_OK)
1297 retval = stm32lx_wait_until_bsy_clear_timeout(bank, 30000);
1298 if (retval != ERROR_OK)
1304 static int stm32lx_mass_erase(struct flash_bank *bank)
1307 struct target *target = bank->target;
1308 struct stm32lx_flash_bank *stm32lx_info = NULL;
1311 if (target->state != TARGET_HALTED) {
1312 LOG_ERROR("Target not halted");
1313 return ERROR_TARGET_NOT_HALTED;
1316 stm32lx_info = bank->driver_priv;
1318 retval = stm32lx_lock(bank);
1319 if (retval != ERROR_OK)
1322 retval = stm32lx_obl_launch(bank);
1323 if (retval != ERROR_OK)
1326 retval = stm32lx_unlock(bank);
1327 if (retval != ERROR_OK)
1330 retval = stm32lx_obl_launch(bank);
1331 if (retval != ERROR_OK)
1334 retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR, ®32);
1335 if (retval != ERROR_OK)
1338 retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR, reg32 | FLASH_PECR__OPTLOCK);
1339 if (retval != ERROR_OK)
1345 static int stm32lx_update_part_info(struct flash_bank *bank, uint16_t flash_size_in_kb)
1347 struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1349 switch (stm32lx_info->part_info.id) {
1350 case 0x447: /* STM32L0xx (Cat.5) devices */
1351 if (flash_size_in_kb == 192 || flash_size_in_kb == 128) {
1352 stm32lx_info->part_info.first_bank_size_kb = flash_size_in_kb / 2;
1353 stm32lx_info->part_info.has_dual_banks = true;
1356 case 0x437: /* STM32L1xx (Cat.5/Cat.6) */
1357 stm32lx_info->part_info.first_bank_size_kb = flash_size_in_kb / 2;