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 Øyvind Harboe *
9 * oyvind.harboe@zylin.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, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
32 #include <helper/binarybuffer.h>
33 #include <target/algorithm.h>
34 #include <target/armv7m.h>
36 /* Regarding performance:
38 * Short story - it might be best to leave the performance at
41 * You may see a jump in speed if you change to using
42 * 32bit words for the block programming.
44 * Its a shame you cannot use the double word as its
45 * even faster - but you require external VPP for that mode.
47 * Having said all that 16bit writes give us the widest vdd
48 * operating range, so may be worth adding a note to that effect.
52 /* Danger!!!! The STM32F1x and STM32F2x series actually have
53 * quite different flash controllers.
55 * What's more scary is that the names of the registers and their
56 * addresses are the same, but the actual bits and what they do are
57 * can be very different.
59 * To reduce testing complexity and dangers of regressions,
60 * a seperate file is used for stm32fx2x.
62 * 1mByte part with 4 x 16, 1 x 64, 7 x 128kBytes sectors
64 * What's the protection page size???
66 * Tested with STM3220F-EVAL board.
68 * STM32F21xx series for reference.
71 * http://www.st.com/internet/mcu/product/250192.jsp
74 * www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/
75 * PROGRAMMING_MANUAL/CD00233952.pdf
77 * STM32F1x series - notice that this code was copy, pasted and knocked
78 * into a stm32f2x driver, so in case something has been converted or
79 * bugs haven't been fixed, here are the original manuals:
81 * RM0008 - Reference manual
83 * RM0042, the Flash programming manual for low-, medium- high-density and
84 * connectivity line STM32F10x devices
86 * PM0068, the Flash programming manual for XL-density STM32F10x devices.
90 /* Erase time can be as high as 1000ms, 10x this and it's toast... */
91 #define FLASH_ERASE_TIMEOUT 10000
92 #define FLASH_WRITE_TIMEOUT 5
94 #define STM32_FLASH_BASE 0x40023c00
95 #define STM32_FLASH_ACR 0x40023c00
96 #define STM32_FLASH_KEYR 0x40023c04
97 #define STM32_FLASH_OPTKEYR 0x40023c08
98 #define STM32_FLASH_SR 0x40023c0C
99 #define STM32_FLASH_CR 0x40023c10
100 #define STM32_FLASH_OPTCR 0x40023c14
101 #define STM32_FLASH_OBR 0x40023c1C
103 /* option byte location */
105 #define STM32_OB_RDP 0x1FFFF800
106 #define STM32_OB_USER 0x1FFFF802
107 #define STM32_OB_DATA0 0x1FFFF804
108 #define STM32_OB_DATA1 0x1FFFF806
109 #define STM32_OB_WRP0 0x1FFFF808
110 #define STM32_OB_WRP1 0x1FFFF80A
111 #define STM32_OB_WRP2 0x1FFFF80C
112 #define STM32_OB_WRP3 0x1FFFF80E
114 /* FLASH_CR register bits */
116 #define FLASH_PG (1 << 0)
117 #define FLASH_SER (1 << 1)
118 #define FLASH_MER (1 << 2)
119 #define FLASH_STRT (1 << 16)
120 #define FLASH_PSIZE_8 (0 << 8)
121 #define FLASH_PSIZE_16 (1 << 8)
122 #define FLASH_PSIZE_32 (2 << 8)
123 #define FLASH_PSIZE_64 (3 << 8)
124 #define FLASH_SNB(a) ((a) << 3)
125 #define FLASH_LOCK (1 << 31)
127 /* FLASH_SR register bits */
129 #define FLASH_BSY (1 << 16)
130 #define FLASH_PGSERR (1 << 7) /* Programming sequence error */
131 #define FLASH_PGPERR (1 << 6) /* Programming parallelism error */
132 #define FLASH_PGAERR (1 << 5) /* Programming alignment error */
133 #define FLASH_WRPERR (1 << 4) /* Write protection error */
134 #define FLASH_OPERR (1 << 1) /* Operation error */
136 #define FLASH_ERROR (FLASH_PGSERR | FLASH_PGPERR | FLASH_PGAERR | FLASH_WRPERR | FLASH_OPERR)
138 /* STM32_FLASH_OBR bit definitions (reading) */
141 #define OPT_READOUT 1
142 #define OPT_RDWDGSW 2
143 #define OPT_RDRSTSTOP 3
144 #define OPT_RDRSTSTDBY 4
145 #define OPT_BFB2 5 /* dual flash bank only */
147 /* register unlock keys */
149 #define KEY1 0x45670123
150 #define KEY2 0xCDEF89AB
152 struct stm32x_flash_bank
{
153 struct working_area
*write_algorithm
;
158 /* flash bank stm32x <base> <size> 0 0 <target#>
160 FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command
)
162 struct stm32x_flash_bank
*stm32x_info
;
165 return ERROR_COMMAND_SYNTAX_ERROR
;
167 stm32x_info
= malloc(sizeof(struct stm32x_flash_bank
));
168 bank
->driver_priv
= stm32x_info
;
170 stm32x_info
->write_algorithm
= NULL
;
171 stm32x_info
->probed
= 0;
176 static inline int stm32x_get_flash_reg(struct flash_bank
*bank
, uint32_t reg
)
181 static inline int stm32x_get_flash_status(struct flash_bank
*bank
, uint32_t *status
)
183 struct target
*target
= bank
->target
;
184 return target_read_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_SR
), status
);
187 static int stm32x_wait_status_busy(struct flash_bank
*bank
, int timeout
)
189 struct target
*target
= bank
->target
;
191 int retval
= ERROR_OK
;
193 /* wait for busy to clear */
195 retval
= stm32x_get_flash_status(bank
, &status
);
196 if (retval
!= ERROR_OK
)
198 LOG_DEBUG("status: 0x%" PRIx32
"", status
);
199 if ((status
& FLASH_BSY
) == 0)
201 if (timeout
-- <= 0) {
202 LOG_ERROR("timed out waiting for flash");
209 if (status
& FLASH_WRPERR
) {
210 LOG_ERROR("stm32x device protected");
214 /* Clear but report errors */
215 if (status
& FLASH_ERROR
) {
216 /* If this operation fails, we ignore it and report the original
219 target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_SR
),
220 status
& FLASH_ERROR
);
225 static int stm32x_unlock_reg(struct target
*target
)
229 /* first check if not already unlocked
230 * otherwise writing on STM32_FLASH_KEYR will fail
232 int retval
= target_read_u32(target
, STM32_FLASH_CR
, &ctrl
);
233 if (retval
!= ERROR_OK
)
236 if ((ctrl
& FLASH_LOCK
) == 0)
239 /* unlock flash registers */
240 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
241 if (retval
!= ERROR_OK
)
244 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
245 if (retval
!= ERROR_OK
)
248 retval
= target_read_u32(target
, STM32_FLASH_CR
, &ctrl
);
249 if (retval
!= ERROR_OK
)
252 if (ctrl
& FLASH_LOCK
) {
253 LOG_ERROR("flash not unlocked STM32_FLASH_CR: %x", ctrl
);
254 return ERROR_TARGET_FAILURE
;
260 static int stm32x_protect_check(struct flash_bank
*bank
)
265 static int stm32x_erase(struct flash_bank
*bank
, int first
, int last
)
267 struct target
*target
= bank
->target
;
270 if (bank
->target
->state
!= TARGET_HALTED
) {
271 LOG_ERROR("Target not halted");
272 return ERROR_TARGET_NOT_HALTED
;
276 retval
= stm32x_unlock_reg(target
);
277 if (retval
!= ERROR_OK
)
282 To erase a sector, follow the procedure below:
283 1. Check that no Flash memory operation is ongoing by checking the BSY bit in the
285 2. Set the SER bit and select the sector (out of the 12 sectors in the main memory block)
286 you wish to erase (SNB) in the FLASH_CR register
287 3. Set the STRT bit in the FLASH_CR register
288 4. Wait for the BSY bit to be cleared
291 for (i
= first
; i
<= last
; i
++) {
292 retval
= target_write_u32(target
,
293 stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_SER
| FLASH_SNB(i
) | FLASH_STRT
);
294 if (retval
!= ERROR_OK
)
297 retval
= stm32x_wait_status_busy(bank
, FLASH_ERASE_TIMEOUT
);
298 if (retval
!= ERROR_OK
)
301 bank
->sectors
[i
].is_erased
= 1;
304 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_LOCK
);
305 if (retval
!= ERROR_OK
)
311 static int stm32x_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
316 static int stm32x_write_block(struct flash_bank
*bank
, uint8_t *buffer
,
317 uint32_t offset
, uint32_t count
)
319 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
320 struct target
*target
= bank
->target
;
321 uint32_t buffer_size
= 16384;
322 struct working_area
*source
;
323 uint32_t address
= bank
->base
+ offset
;
324 struct reg_param reg_params
[5];
325 struct armv7m_algorithm armv7m_info
;
326 int retval
= ERROR_OK
;
328 /* see contrib/loaders/flash/stm32f2x.S for src */
330 static const uint16_t stm32x_flash_write_code_16
[] = {
331 /* 00000000 <write>: */
332 0x4b07, /* ldr r3, [pc, #28] (20 <STM32_PROG16>) */
333 0x6123, /* str r3, [r4, #16] */
334 0xf830, 0x3b02, /* ldrh.w r3, [r0], #2 */
335 0xf821, 0x3b02, /* strh.w r3, [r1], #2 */
337 /* 0000000c <busy>: */
338 0x68e3, /* ldr r3, [r4, #12] */
339 0xf413, 0x3f80, /* tst.w r3, #65536 ; 0x10000 */
340 0xd0fb, /* beq.n c <busy> */
341 0xf013, 0x0ff0, /* tst.w r3, #240 ; 0xf0 */
342 0xd101, /* bne.n 1e <exit> */
343 0x3a01, /* subs r2, #1 */
344 0xd1f0, /* bne.n 0 <write> */
345 /* 0000001e <exit>: */
346 0xbe00, /* bkpt 0x0000 */
348 /* 00000020 <STM32_PROG16>: */
349 0x0101, 0x0000, /* .word 0x00000101 */
353 uint8_t stm32x_flash_write_code
[sizeof(stm32x_flash_write_code_16
)*2];
354 for (unsigned i
= 0; i
< sizeof(stm32x_flash_write_code_16
) / 2; i
++) {
355 stm32x_flash_write_code
[i
*2 + 0] = stm32x_flash_write_code_16
[i
] & 0xff;
356 stm32x_flash_write_code
[i
*2 + 1] = (stm32x_flash_write_code_16
[i
] >> 8) & 0xff;
359 if (target_alloc_working_area(target
, sizeof(stm32x_flash_write_code
),
360 &stm32x_info
->write_algorithm
) != ERROR_OK
) {
361 LOG_WARNING("no working area available, can't do block memory writes");
362 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
365 retval
= target_write_buffer(target
, stm32x_info
->write_algorithm
->address
,
366 sizeof(stm32x_flash_write_code
),
367 (uint8_t *)stm32x_flash_write_code
);
368 if (retval
!= ERROR_OK
)
372 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
) {
374 if (buffer_size
<= 256) {
375 /* if we already allocated the writing code, but failed to get a
376 * buffer, free the algorithm */
377 if (stm32x_info
->write_algorithm
)
378 target_free_working_area(target
, stm32x_info
->write_algorithm
);
380 LOG_WARNING("no large enough working area available, can't do block memory writes");
381 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
385 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
386 armv7m_info
.core_mode
= ARMV7M_MODE_ANY
;
388 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
389 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
390 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
391 init_reg_param(®_params
[3], "r3", 32, PARAM_IN_OUT
);
392 init_reg_param(®_params
[4], "r4", 32, PARAM_OUT
);
395 uint32_t thisrun_count
= (count
> (buffer_size
/ 2)) ?
396 (buffer_size
/ 2) : count
;
398 retval
= target_write_buffer(target
, source
->address
, thisrun_count
* 2, buffer
);
399 if (retval
!= ERROR_OK
)
402 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
403 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
404 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
);
405 /* R3 is a return value only */
406 buf_set_u32(reg_params
[4].value
, 0, 32, STM32_FLASH_BASE
);
408 retval
= target_run_algorithm(target
, 0, NULL
,
409 sizeof(reg_params
) / sizeof(*reg_params
),
411 stm32x_info
->write_algorithm
->address
,
413 10000, &armv7m_info
);
414 if (retval
!= ERROR_OK
) {
415 LOG_ERROR("error executing stm32x flash write algorithm");
419 uint32_t error
= buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_ERROR
;
421 if (error
& FLASH_WRPERR
)
422 LOG_ERROR("flash memory write protected");
425 LOG_ERROR("flash write failed = %08x", error
);
426 /* Clear but report errors */
427 target_write_u32(target
, STM32_FLASH_SR
, error
);
432 buffer
+= thisrun_count
* 2;
433 address
+= thisrun_count
* 2;
434 count
-= thisrun_count
;
437 target_free_working_area(target
, source
);
438 target_free_working_area(target
, stm32x_info
->write_algorithm
);
440 destroy_reg_param(®_params
[0]);
441 destroy_reg_param(®_params
[1]);
442 destroy_reg_param(®_params
[2]);
443 destroy_reg_param(®_params
[3]);
444 destroy_reg_param(®_params
[4]);
449 static int stm32x_write(struct flash_bank
*bank
, uint8_t *buffer
,
450 uint32_t offset
, uint32_t count
)
452 struct target
*target
= bank
->target
;
453 uint32_t words_remaining
= (count
/ 2);
454 uint32_t bytes_remaining
= (count
& 0x00000001);
455 uint32_t address
= bank
->base
+ offset
;
456 uint32_t bytes_written
= 0;
459 if (bank
->target
->state
!= TARGET_HALTED
) {
460 LOG_ERROR("Target not halted");
461 return ERROR_TARGET_NOT_HALTED
;
465 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
466 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
469 retval
= stm32x_unlock_reg(target
);
470 if (retval
!= ERROR_OK
)
473 /* multiple half words (2-byte) to be programmed? */
474 if (words_remaining
> 0) {
475 /* try using a block write */
476 retval
= stm32x_write_block(bank
, buffer
, offset
, words_remaining
);
477 if (retval
!= ERROR_OK
) {
478 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
) {
479 /* if block write failed (no sufficient working area),
480 * we use normal (slow) single dword accesses */
481 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
484 buffer
+= words_remaining
* 2;
485 address
+= words_remaining
* 2;
490 if ((retval
!= ERROR_OK
) && (retval
!= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
))
495 The Flash memory programming sequence is as follows:
496 1. Check that no main Flash memory operation is ongoing by checking the BSY bit in the
498 2. Set the PG bit in the FLASH_CR register
499 3. Perform the data write operation(s) to the desired memory address (inside main
500 memory block or OTP area):
501 – – Half-word access in case of x16 parallelism
502 – Word access in case of x32 parallelism
505 Byte access in case of x8 parallelism
506 Double word access in case of x64 parallelism
507 Wait for the BSY bit to be cleared
509 while (words_remaining
> 0) {
511 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint16_t));
513 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
),
514 FLASH_PG
| FLASH_PSIZE_16
);
515 if (retval
!= ERROR_OK
)
518 retval
= target_write_u16(target
, address
, value
);
519 if (retval
!= ERROR_OK
)
522 retval
= stm32x_wait_status_busy(bank
, FLASH_WRITE_TIMEOUT
);
523 if (retval
!= ERROR_OK
)
531 if (bytes_remaining
) {
532 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
),
533 FLASH_PG
| FLASH_PSIZE_8
);
534 if (retval
!= ERROR_OK
)
536 retval
= target_write_u8(target
, address
, buffer
[bytes_written
]);
537 if (retval
!= ERROR_OK
)
540 retval
= stm32x_wait_status_busy(bank
, FLASH_WRITE_TIMEOUT
);
541 if (retval
!= ERROR_OK
)
545 return target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
548 static void setup_sector(struct flash_bank
*bank
, int start
, int num
, int size
)
550 for (int i
= start
; i
< (start
+ num
) ; i
++) {
551 bank
->sectors
[i
].offset
= bank
->size
;
552 bank
->sectors
[i
].size
= size
;
553 bank
->size
+= bank
->sectors
[i
].size
;
557 static int stm32x_get_device_id(struct flash_bank
*bank
, uint32_t *device_id
)
559 /* this checks for a stm32f4x errata issue where a
560 * stm32f2x DBGMCU_IDCODE is incorrectly returned.
561 * If the issue is detected target is forced to stm32f4x Rev A.
562 * Only effects Rev A silicon */
564 struct target
*target
= bank
->target
;
567 /* read stm32 device id register */
568 int retval
= target_read_u32(target
, 0xE0042000, device_id
);
569 if (retval
!= ERROR_OK
)
572 if ((*device_id
& 0xfff) == 0x411) {
573 /* read CPUID reg to check core type */
574 retval
= target_read_u32(target
, 0xE000ED00, &cpuid
);
575 if (retval
!= ERROR_OK
)
578 /* check for cortex_m4 */
579 if (((cpuid
>> 4) & 0xFFF) == 0xC24) {
580 *device_id
&= ~((0xFFFF << 16) | 0xfff);
581 *device_id
|= (0x1000 << 16) | 0x413;
582 LOG_INFO("stm32f4x errata detected - fixing incorrect MCU_IDCODE");
588 static int stm32x_probe(struct flash_bank
*bank
)
590 struct target
*target
= bank
->target
;
591 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
593 uint16_t flash_size_in_kb
;
595 uint32_t base_address
= 0x08000000;
597 stm32x_info
->probed
= 0;
599 /* read stm32 device id register */
600 int retval
= stm32x_get_device_id(bank
, &device_id
);
601 if (retval
!= ERROR_OK
)
603 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
605 /* get flash size from target. */
606 retval
= target_read_u16(target
, 0x1FFF7A22, &flash_size_in_kb
);
607 if (retval
!= ERROR_OK
) {
608 LOG_WARNING("failed reading flash size, default to max target family");
609 /* failed reading flash size, default to max target family */
610 flash_size_in_kb
= 0xffff;
613 if ((device_id
& 0xfff) == 0x411) {
614 /* check for early silicon */
615 if (flash_size_in_kb
== 0xffff) {
616 /* number of sectors may be incorrrect on early silicon */
617 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash");
618 flash_size_in_kb
= 1024;
620 } else if ((device_id
& 0xfff) == 0x413) {
621 /* check for early silicon */
622 if (flash_size_in_kb
== 0xffff) {
623 /* number of sectors may be incorrrect on early silicon */
624 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash");
625 flash_size_in_kb
= 1024;
628 LOG_WARNING("Cannot identify target as a STM32 family.");
632 LOG_INFO("flash size = %dkbytes", flash_size_in_kb
);
634 /* did we assign flash size? */
635 assert(flash_size_in_kb
!= 0xffff);
637 /* calculate numbers of pages */
638 int num_pages
= (flash_size_in_kb
/ 128) + 4;
640 /* check that calculation result makes sense */
641 assert(num_pages
> 0);
645 bank
->sectors
= NULL
;
648 bank
->base
= base_address
;
649 bank
->num_sectors
= num_pages
;
650 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
654 setup_sector(bank
, 0, 4, 16 * 1024);
655 setup_sector(bank
, 4, 1, 64 * 1024);
658 setup_sector(bank
, 4 + 1, num_pages
- 5, 128 * 1024);
660 for (i
= 0; i
< num_pages
; i
++) {
661 bank
->sectors
[i
].is_erased
= -1;
662 bank
->sectors
[i
].is_protected
= 0;
665 stm32x_info
->probed
= 1;
670 static int stm32x_auto_probe(struct flash_bank
*bank
)
672 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
673 if (stm32x_info
->probed
)
675 return stm32x_probe(bank
);
678 static int get_stm32x_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
683 /* read stm32 device id register */
684 int retval
= stm32x_get_device_id(bank
, &device_id
);
685 if (retval
!= ERROR_OK
)
688 if ((device_id
& 0xfff) == 0x411) {
689 printed
= snprintf(buf
, buf_size
, "stm32f2x - Rev: ");
693 switch (device_id
>> 16) {
695 snprintf(buf
, buf_size
, "A");
699 snprintf(buf
, buf_size
, "B");
703 snprintf(buf
, buf_size
, "Z");
707 snprintf(buf
, buf_size
, "Y");
711 snprintf(buf
, buf_size
, "unknown");
714 } else if ((device_id
& 0xfff) == 0x413) {
715 printed
= snprintf(buf
, buf_size
, "stm32f4x - Rev: ");
719 switch (device_id
>> 16) {
721 snprintf(buf
, buf_size
, "A");
725 snprintf(buf
, buf_size
, "Z");
729 snprintf(buf
, buf_size
, "unknown");
733 snprintf(buf
, buf_size
, "Cannot identify target as a stm32x\n");
740 static int stm32x_mass_erase(struct flash_bank
*bank
)
743 struct target
*target
= bank
->target
;
745 if (target
->state
!= TARGET_HALTED
) {
746 LOG_ERROR("Target not halted");
747 return ERROR_TARGET_NOT_HALTED
;
750 retval
= stm32x_unlock_reg(target
);
751 if (retval
!= ERROR_OK
)
754 /* mass erase flash memory */
755 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_MER
);
756 if (retval
!= ERROR_OK
)
758 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
),
759 FLASH_MER
| FLASH_STRT
);
760 if (retval
!= ERROR_OK
)
763 retval
= stm32x_wait_status_busy(bank
, 30000);
764 if (retval
!= ERROR_OK
)
767 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_LOCK
);
768 if (retval
!= ERROR_OK
)
774 COMMAND_HANDLER(stm32x_handle_mass_erase_command
)
779 command_print(CMD_CTX
, "stm32x mass_erase <bank>");
780 return ERROR_COMMAND_SYNTAX_ERROR
;
783 struct flash_bank
*bank
;
784 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
785 if (ERROR_OK
!= retval
)
788 retval
= stm32x_mass_erase(bank
);
789 if (retval
== ERROR_OK
) {
790 /* set all sectors as erased */
791 for (i
= 0; i
< bank
->num_sectors
; i
++)
792 bank
->sectors
[i
].is_erased
= 1;
794 command_print(CMD_CTX
, "stm32x mass erase complete");
796 command_print(CMD_CTX
, "stm32x mass erase failed");
802 static const struct command_registration stm32x_exec_command_handlers
[] = {
804 .name
= "mass_erase",
805 .handler
= stm32x_handle_mass_erase_command
,
806 .mode
= COMMAND_EXEC
,
808 .help
= "Erase entire flash device.",
810 COMMAND_REGISTRATION_DONE
813 static const struct command_registration stm32x_command_handlers
[] = {
817 .help
= "stm32f2x flash command group",
819 .chain
= stm32x_exec_command_handlers
,
821 COMMAND_REGISTRATION_DONE
824 struct flash_driver stm32f2x_flash
= {
826 .commands
= stm32x_command_handlers
,
827 .flash_bank_command
= stm32x_flash_bank_command
,
828 .erase
= stm32x_erase
,
829 .protect
= stm32x_protect
,
830 .write
= stm32x_write
,
831 .read
= default_flash_read
,
832 .probe
= stm32x_probe
,
833 .auto_probe
= stm32x_auto_probe
,
834 .erase_check
= default_flash_mem_blank_check
,
835 .protect_check
= stm32x_protect_check
,
836 .info
= get_stm32x_info
,