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 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
29 #include <helper/binarybuffer.h>
30 #include <target/algorithm.h>
31 #include <target/armv7m.h>
34 static int stm32x_mass_erase(struct flash_bank
*bank
);
36 /* flash bank stm32x <base> <size> 0 0 <target#>
38 FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command
)
40 struct stm32x_flash_bank
*stm32x_info
;
44 LOG_WARNING("incomplete flash_bank stm32x configuration");
45 return ERROR_FLASH_BANK_INVALID
;
48 stm32x_info
= malloc(sizeof(struct stm32x_flash_bank
));
49 bank
->driver_priv
= stm32x_info
;
51 stm32x_info
->write_algorithm
= NULL
;
52 stm32x_info
->probed
= 0;
57 static int stm32x_get_flash_status(struct flash_bank
*bank
, uint32_t *status
)
59 struct target
*target
= bank
->target
;
60 return target_read_u32(target
, STM32_FLASH_SR
, status
);
63 static int stm32x_wait_status_busy(struct flash_bank
*bank
, int timeout
)
65 struct target
*target
= bank
->target
;
67 int retval
= ERROR_OK
;
69 /* wait for busy to clear */
72 retval
= stm32x_get_flash_status(bank
, &status
);
73 if (retval
!= ERROR_OK
)
75 LOG_DEBUG("status: 0x%" PRIx32
"", status
);
76 if ((status
& FLASH_BSY
) == 0)
80 LOG_ERROR("timed out waiting for flash");
86 if (status
& FLASH_WRPRTERR
)
88 LOG_ERROR("stm32x device protected");
92 if (status
& FLASH_PGERR
)
94 LOG_ERROR("stm32x device programming failed");
98 /* Clear but report errors */
99 if (status
& (FLASH_WRPRTERR
| FLASH_PGERR
))
101 /* If this operation fails, we ignore it and report the original
104 target_write_u32(target
, STM32_FLASH_SR
, FLASH_WRPRTERR
| FLASH_PGERR
);
109 static int stm32x_read_options(struct flash_bank
*bank
)
112 struct stm32x_flash_bank
*stm32x_info
= NULL
;
113 struct target
*target
= bank
->target
;
115 stm32x_info
= bank
->driver_priv
;
117 /* read current option bytes */
118 int retval
= target_read_u32(target
, STM32_FLASH_OBR
, &optiondata
);
119 if (retval
!= ERROR_OK
)
122 stm32x_info
->option_bytes
.user_options
= (uint16_t)0xFFF8 | ((optiondata
>> 2) & 0x07);
123 stm32x_info
->option_bytes
.RDP
= (optiondata
& (1 << OPT_READOUT
)) ? 0xFFFF : 0x5AA5;
125 if (optiondata
& (1 << OPT_READOUT
))
126 LOG_INFO("Device Security Bit Set");
128 /* each bit refers to a 4bank protection */
129 retval
= target_read_u32(target
, STM32_FLASH_WRPR
, &optiondata
);
130 if (retval
!= ERROR_OK
)
133 stm32x_info
->option_bytes
.protection
[0] = (uint16_t)optiondata
;
134 stm32x_info
->option_bytes
.protection
[1] = (uint16_t)(optiondata
>> 8);
135 stm32x_info
->option_bytes
.protection
[2] = (uint16_t)(optiondata
>> 16);
136 stm32x_info
->option_bytes
.protection
[3] = (uint16_t)(optiondata
>> 24);
141 static int stm32x_erase_options(struct flash_bank
*bank
)
143 struct stm32x_flash_bank
*stm32x_info
= NULL
;
144 struct target
*target
= bank
->target
;
146 stm32x_info
= bank
->driver_priv
;
148 /* read current options */
149 stm32x_read_options(bank
);
151 /* unlock flash registers */
152 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
153 if (retval
!= ERROR_OK
)
156 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
157 if (retval
!= ERROR_OK
)
160 /* unlock option flash registers */
161 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY1
);
162 if (retval
!= ERROR_OK
)
164 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY2
);
165 if (retval
!= ERROR_OK
)
168 /* erase option bytes */
169 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTER
| FLASH_OPTWRE
);
170 if (retval
!= ERROR_OK
)
172 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTER
| FLASH_STRT
| FLASH_OPTWRE
);
173 if (retval
!= ERROR_OK
)
176 retval
= stm32x_wait_status_busy(bank
, 10);
177 if (retval
!= ERROR_OK
)
180 /* clear readout protection and complementary option bytes
181 * this will also force a device unlock if set */
182 stm32x_info
->option_bytes
.RDP
= 0x5AA5;
187 static int stm32x_write_options(struct flash_bank
*bank
)
189 struct stm32x_flash_bank
*stm32x_info
= NULL
;
190 struct target
*target
= bank
->target
;
192 stm32x_info
= bank
->driver_priv
;
194 /* unlock flash registers */
195 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
196 if (retval
!= ERROR_OK
)
198 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
199 if (retval
!= ERROR_OK
)
202 /* unlock option flash registers */
203 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY1
);
204 if (retval
!= ERROR_OK
)
206 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY2
);
207 if (retval
!= ERROR_OK
)
210 /* program option bytes */
211 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTPG
| FLASH_OPTWRE
);
212 if (retval
!= ERROR_OK
)
215 /* write user option byte */
216 retval
= target_write_u16(target
, STM32_OB_USER
, stm32x_info
->option_bytes
.user_options
);
217 if (retval
!= ERROR_OK
)
220 retval
= stm32x_wait_status_busy(bank
, 10);
221 if (retval
!= ERROR_OK
)
224 /* write protection byte 1 */
225 retval
= target_write_u16(target
, STM32_OB_WRP0
, stm32x_info
->option_bytes
.protection
[0]);
226 if (retval
!= ERROR_OK
)
229 retval
= stm32x_wait_status_busy(bank
, 10);
230 if (retval
!= ERROR_OK
)
233 /* write protection byte 2 */
234 retval
= target_write_u16(target
, STM32_OB_WRP1
, stm32x_info
->option_bytes
.protection
[1]);
235 if (retval
!= ERROR_OK
)
238 retval
= stm32x_wait_status_busy(bank
, 10);
239 if (retval
!= ERROR_OK
)
242 /* write protection byte 3 */
243 retval
= target_write_u16(target
, STM32_OB_WRP2
, stm32x_info
->option_bytes
.protection
[2]);
244 if (retval
!= ERROR_OK
)
247 retval
= stm32x_wait_status_busy(bank
, 10);
248 if (retval
!= ERROR_OK
)
251 /* write protection byte 4 */
252 retval
= target_write_u16(target
, STM32_OB_WRP3
, stm32x_info
->option_bytes
.protection
[3]);
253 if (retval
!= ERROR_OK
)
256 retval
= stm32x_wait_status_busy(bank
, 10);
257 if (retval
!= ERROR_OK
)
260 /* write readout protection bit */
261 retval
= target_write_u16(target
, STM32_OB_RDP
, stm32x_info
->option_bytes
.RDP
);
262 if (retval
!= ERROR_OK
)
265 retval
= stm32x_wait_status_busy(bank
, 10);
266 if (retval
!= ERROR_OK
)
269 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
270 if (retval
!= ERROR_OK
)
276 static int stm32x_protect_check(struct flash_bank
*bank
)
278 struct target
*target
= bank
->target
;
279 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
286 if (target
->state
!= TARGET_HALTED
)
288 LOG_ERROR("Target not halted");
289 return ERROR_TARGET_NOT_HALTED
;
292 /* medium density - each bit refers to a 4bank protection
293 * high density - each bit refers to a 2bank protection */
294 int retval
= target_read_u32(target
, STM32_FLASH_WRPR
, &protection
);
295 if (retval
!= ERROR_OK
)
298 /* medium density - each protection bit is for 4 * 1K pages
299 * high density - each protection bit is for 2 * 2K pages */
300 num_bits
= (bank
->num_sectors
/ stm32x_info
->ppage_size
);
302 if (stm32x_info
->ppage_size
== 2)
304 /* high density flash/connectivity line protection */
308 if (protection
& (1 << 31))
311 /* bit 31 controls sector 62 - 255 protection for high density
312 * bit 31 controls sector 62 - 127 protection for connectivity line */
313 for (s
= 62; s
< bank
->num_sectors
; s
++)
315 bank
->sectors
[s
].is_protected
= set
;
318 if (bank
->num_sectors
> 61)
321 for (i
= 0; i
< num_bits
; i
++)
325 if (protection
& (1 << i
))
328 for (s
= 0; s
< stm32x_info
->ppage_size
; s
++)
329 bank
->sectors
[(i
* stm32x_info
->ppage_size
) + s
].is_protected
= set
;
334 /* low/medium density flash protection */
335 for (i
= 0; i
< num_bits
; i
++)
339 if (protection
& (1 << i
))
342 for (s
= 0; s
< stm32x_info
->ppage_size
; s
++)
343 bank
->sectors
[(i
* stm32x_info
->ppage_size
) + s
].is_protected
= set
;
350 static int stm32x_erase(struct flash_bank
*bank
, int first
, int last
)
352 struct target
*target
= bank
->target
;
355 if (bank
->target
->state
!= TARGET_HALTED
)
357 LOG_ERROR("Target not halted");
358 return ERROR_TARGET_NOT_HALTED
;
361 if ((first
== 0) && (last
== (bank
->num_sectors
- 1)))
363 return stm32x_mass_erase(bank
);
366 /* unlock flash registers */
367 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
368 if (retval
!= ERROR_OK
)
370 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
371 if (retval
!= ERROR_OK
)
374 for (i
= first
; i
<= last
; i
++)
376 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PER
);
377 if (retval
!= ERROR_OK
)
379 retval
= target_write_u32(target
, STM32_FLASH_AR
, bank
->base
+ bank
->sectors
[i
].offset
);
380 if (retval
!= ERROR_OK
)
382 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PER
| FLASH_STRT
);
383 if (retval
!= ERROR_OK
)
386 retval
= stm32x_wait_status_busy(bank
, 100);
387 if (retval
!= ERROR_OK
)
390 bank
->sectors
[i
].is_erased
= 1;
393 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
394 if (retval
!= ERROR_OK
)
400 static int stm32x_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
402 struct stm32x_flash_bank
*stm32x_info
= NULL
;
403 struct target
*target
= bank
->target
;
404 uint16_t prot_reg
[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
409 stm32x_info
= bank
->driver_priv
;
411 if (target
->state
!= TARGET_HALTED
)
413 LOG_ERROR("Target not halted");
414 return ERROR_TARGET_NOT_HALTED
;
417 if ((first
&& (first
% stm32x_info
->ppage_size
)) || ((last
+ 1) &&
418 (last
+ 1) % stm32x_info
->ppage_size
))
420 LOG_WARNING("Error: start and end sectors must be on a %d sector boundary",
421 stm32x_info
->ppage_size
);
422 return ERROR_FLASH_SECTOR_INVALID
;
425 /* medium density - each bit refers to a 4bank protection
426 * high density - each bit refers to a 2bank protection */
427 int retval
= target_read_u32(target
, STM32_FLASH_WRPR
, &protection
);
428 if (retval
!= ERROR_OK
)
431 prot_reg
[0] = (uint16_t)protection
;
432 prot_reg
[1] = (uint16_t)(protection
>> 8);
433 prot_reg
[2] = (uint16_t)(protection
>> 16);
434 prot_reg
[3] = (uint16_t)(protection
>> 24);
436 if (stm32x_info
->ppage_size
== 2)
438 /* high density flash */
440 /* bit 7 controls sector 62 - 255 protection */
444 prot_reg
[3] &= ~(1 << 7);
446 prot_reg
[3] |= (1 << 7);
454 for (i
= first
; i
<= last
; i
++)
456 reg
= (i
/ stm32x_info
->ppage_size
) / 8;
457 bit
= (i
/ stm32x_info
->ppage_size
) - (reg
* 8);
460 prot_reg
[reg
] &= ~(1 << bit
);
462 prot_reg
[reg
] |= (1 << bit
);
467 /* medium density flash */
468 for (i
= first
; i
<= last
; i
++)
470 reg
= (i
/ stm32x_info
->ppage_size
) / 8;
471 bit
= (i
/ stm32x_info
->ppage_size
) - (reg
* 8);
474 prot_reg
[reg
] &= ~(1 << bit
);
476 prot_reg
[reg
] |= (1 << bit
);
480 if ((status
= stm32x_erase_options(bank
)) != ERROR_OK
)
483 stm32x_info
->option_bytes
.protection
[0] = prot_reg
[0];
484 stm32x_info
->option_bytes
.protection
[1] = prot_reg
[1];
485 stm32x_info
->option_bytes
.protection
[2] = prot_reg
[2];
486 stm32x_info
->option_bytes
.protection
[3] = prot_reg
[3];
488 return stm32x_write_options(bank
);
491 static int stm32x_write_block(struct flash_bank
*bank
, uint8_t *buffer
,
492 uint32_t offset
, uint32_t count
)
494 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
495 struct target
*target
= bank
->target
;
496 uint32_t buffer_size
= 16384;
497 struct working_area
*source
;
498 uint32_t address
= bank
->base
+ offset
;
499 struct reg_param reg_params
[4];
500 struct armv7m_algorithm armv7m_info
;
501 int retval
= ERROR_OK
;
503 /* see contib/loaders/flash/stm32x.s for src */
505 static const uint8_t stm32x_flash_write_code
[] = {
506 /* #define STM32_FLASH_CR_OFFSET 0x10 */
507 /* #define STM32_FLASH_SR_OFFSET 0x0C */
509 0xdf, 0xf8, 0x20, 0x40, /* ldr r4, STM32_FLASH_BASE */
510 /* write_half_word: */
511 0x01, 0x23, /* movs r3, #0x01 */
512 0x23, 0x61, /* str r3, [r4, #STM32_FLASH_CR_OFFSET] */
513 0x30, 0xf8, 0x02, 0x3b, /* ldrh r3, [r0], #0x02 */
514 0x21, 0xf8, 0x02, 0x3b, /* strh r3, [r1], #0x02 */
516 0xe3, 0x68, /* ldr r3, [r4, #STM32_FLASH_SR_OFFSET] */
517 0x13, 0xf0, 0x01, 0x0f, /* tst r3, #0x01 */
518 0xfb, 0xd0, /* beq busy */
519 0x13, 0xf0, 0x14, 0x0f, /* tst r3, #0x14 */
520 0x01, 0xd1, /* bne exit */
521 0x01, 0x3a, /* subs r2, r2, #0x01 */
522 0xf0, 0xd1, /* bne write_half_word */
524 0x00, 0xbe, /* bkpt #0x00 */
525 0x00, 0x20, 0x02, 0x40, /* STM32_FLASH_BASE: .word 0x40022000 */
528 /* flash write code */
529 if (target_alloc_working_area(target
, sizeof(stm32x_flash_write_code
),
530 &stm32x_info
->write_algorithm
) != ERROR_OK
)
532 LOG_WARNING("no working area available, can't do block memory writes");
533 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
536 if ((retval
= target_write_buffer(target
, stm32x_info
->write_algorithm
->address
,
537 sizeof(stm32x_flash_write_code
),
538 (uint8_t*)stm32x_flash_write_code
)) != ERROR_OK
)
542 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
545 if (buffer_size
<= 256)
547 /* if we already allocated the writing code, but failed to get a
548 * buffer, free the algorithm */
549 if (stm32x_info
->write_algorithm
)
550 target_free_working_area(target
, stm32x_info
->write_algorithm
);
552 LOG_WARNING("no large enough working area available, can't do block memory writes");
553 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
557 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
558 armv7m_info
.core_mode
= ARMV7M_MODE_ANY
;
560 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
561 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
562 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
563 init_reg_param(®_params
[3], "r3", 32, PARAM_IN
);
567 uint32_t thisrun_count
= (count
> (buffer_size
/ 2)) ?
568 (buffer_size
/ 2) : count
;
570 if ((retval
= target_write_buffer(target
, source
->address
,
571 thisrun_count
* 2, buffer
)) != ERROR_OK
)
574 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
575 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
576 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
);
578 if ((retval
= target_run_algorithm(target
, 0, NULL
, 4, reg_params
,
579 stm32x_info
->write_algorithm
->address
,
581 10000, &armv7m_info
)) != ERROR_OK
)
583 LOG_ERROR("error executing stm32x flash write algorithm");
587 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_PGERR
)
589 LOG_ERROR("flash memory not erased before writing");
590 /* Clear but report errors */
591 target_write_u32(target
, STM32_FLASH_SR
, FLASH_PGERR
);
596 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_WRPRTERR
)
598 LOG_ERROR("flash memory write protected");
599 /* Clear but report errors */
600 target_write_u32(target
, STM32_FLASH_SR
, FLASH_WRPRTERR
);
605 buffer
+= thisrun_count
* 2;
606 address
+= thisrun_count
* 2;
607 count
-= thisrun_count
;
610 target_free_working_area(target
, source
);
611 target_free_working_area(target
, stm32x_info
->write_algorithm
);
613 destroy_reg_param(®_params
[0]);
614 destroy_reg_param(®_params
[1]);
615 destroy_reg_param(®_params
[2]);
616 destroy_reg_param(®_params
[3]);
621 static int stm32x_write(struct flash_bank
*bank
, uint8_t *buffer
,
622 uint32_t offset
, uint32_t count
)
624 struct target
*target
= bank
->target
;
625 uint32_t words_remaining
= (count
/ 2);
626 uint32_t bytes_remaining
= (count
& 0x00000001);
627 uint32_t address
= bank
->base
+ offset
;
628 uint32_t bytes_written
= 0;
631 if (bank
->target
->state
!= TARGET_HALTED
)
633 LOG_ERROR("Target not halted");
634 return ERROR_TARGET_NOT_HALTED
;
639 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
640 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
643 /* unlock flash registers */
644 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
645 if (retval
!= ERROR_OK
)
647 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
648 if (retval
!= ERROR_OK
)
651 /* multiple half words (2-byte) to be programmed? */
652 if (words_remaining
> 0)
654 /* try using a block write */
655 if ((retval
= stm32x_write_block(bank
, buffer
, offset
, words_remaining
)) != ERROR_OK
)
657 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
659 /* if block write failed (no sufficient working area),
660 * we use normal (slow) single dword accesses */
661 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
666 buffer
+= words_remaining
* 2;
667 address
+= words_remaining
* 2;
672 if ((retval
!= ERROR_OK
) && (retval
!= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
))
675 while (words_remaining
> 0)
678 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint16_t));
680 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PG
);
681 if (retval
!= ERROR_OK
)
683 retval
= target_write_u16(target
, address
, value
);
684 if (retval
!= ERROR_OK
)
687 retval
= stm32x_wait_status_busy(bank
, 5);
688 if (retval
!= ERROR_OK
)
698 uint16_t value
= 0xffff;
699 memcpy(&value
, buffer
+ bytes_written
, bytes_remaining
);
701 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PG
);
702 if (retval
!= ERROR_OK
)
704 retval
= target_write_u16(target
, address
, value
);
705 if (retval
!= ERROR_OK
)
708 retval
= stm32x_wait_status_busy(bank
, 5);
709 if (retval
!= ERROR_OK
)
713 return target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
716 static int stm32x_probe(struct flash_bank
*bank
)
718 struct target
*target
= bank
->target
;
719 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
725 stm32x_info
->probed
= 0;
727 /* read stm32 device id register */
728 int retval
= target_read_u32(target
, 0xE0042000, &device_id
);
729 if (retval
!= ERROR_OK
)
731 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
733 /* get flash size from target. */
734 retval
= target_read_u16(target
, 0x1FFFF7E0, &num_pages
);
735 if (retval
!= ERROR_OK
)
737 LOG_WARNING("failed reading flash size, default to max target family");
738 /* failed reading flash size, default to max target family */
742 if ((device_id
& 0x7ff) == 0x410)
744 /* medium density - we have 1k pages
745 * 4 pages for a protection area */
747 stm32x_info
->ppage_size
= 4;
749 /* check for early silicon */
750 if (num_pages
== 0xffff)
752 /* number of sectors incorrect on revA */
753 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
757 else if ((device_id
& 0x7ff) == 0x412)
759 /* low density - we have 1k pages
760 * 4 pages for a protection area */
762 stm32x_info
->ppage_size
= 4;
764 /* check for early silicon */
765 if (num_pages
== 0xffff)
767 /* number of sectors incorrect on revA */
768 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 32k flash");
772 else if ((device_id
& 0x7ff) == 0x414)
774 /* high density - we have 2k pages
775 * 2 pages for a protection area */
777 stm32x_info
->ppage_size
= 2;
779 /* check for early silicon */
780 if (num_pages
== 0xffff)
782 /* number of sectors incorrect on revZ */
783 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 512k flash");
787 else if ((device_id
& 0x7ff) == 0x418)
789 /* connectivity line density - we have 2k pages
790 * 2 pages for a protection area */
792 stm32x_info
->ppage_size
= 2;
794 /* check for early silicon */
795 if (num_pages
== 0xffff)
797 /* number of sectors incorrect on revZ */
798 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash");
802 else if ((device_id
& 0x7ff) == 0x420)
804 /* value line density - we have 1k pages
805 * 4 pages for a protection area */
807 stm32x_info
->ppage_size
= 4;
809 /* check for early silicon */
810 if (num_pages
== 0xffff)
812 /* number of sectors may be incorrrect on early silicon */
813 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
819 LOG_WARNING("Cannot identify target as a STM32 family.");
823 LOG_INFO("flash size = %dkbytes", num_pages
);
825 /* calculate numbers of pages */
826 num_pages
/= (page_size
/ 1024);
831 bank
->sectors
= NULL
;
834 bank
->base
= 0x08000000;
835 bank
->size
= (num_pages
* page_size
);
836 bank
->num_sectors
= num_pages
;
837 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
839 for (i
= 0; i
< num_pages
; i
++)
841 bank
->sectors
[i
].offset
= i
* page_size
;
842 bank
->sectors
[i
].size
= page_size
;
843 bank
->sectors
[i
].is_erased
= -1;
844 bank
->sectors
[i
].is_protected
= 1;
847 stm32x_info
->probed
= 1;
852 static int stm32x_auto_probe(struct flash_bank
*bank
)
854 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
855 if (stm32x_info
->probed
)
857 return stm32x_probe(bank
);
861 COMMAND_HANDLER(stm32x_handle_part_id_command
)
867 static int get_stm32x_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
869 struct target
*target
= bank
->target
;
873 /* read stm32 device id register */
874 int retval
= target_read_u32(target
, 0xE0042000, &device_id
);
875 if (retval
!= ERROR_OK
)
878 if ((device_id
& 0x7ff) == 0x410)
880 printed
= snprintf(buf
, buf_size
, "stm32x (Medium Density) - Rev: ");
884 switch (device_id
>> 16)
887 snprintf(buf
, buf_size
, "A");
891 snprintf(buf
, buf_size
, "B");
895 snprintf(buf
, buf_size
, "Z");
899 snprintf(buf
, buf_size
, "Y");
903 snprintf(buf
, buf_size
, "unknown");
907 else if ((device_id
& 0x7ff) == 0x412)
909 printed
= snprintf(buf
, buf_size
, "stm32x (Low Density) - Rev: ");
913 switch (device_id
>> 16)
916 snprintf(buf
, buf_size
, "A");
920 snprintf(buf
, buf_size
, "unknown");
924 else if ((device_id
& 0x7ff) == 0x414)
926 printed
= snprintf(buf
, buf_size
, "stm32x (High Density) - Rev: ");
930 switch (device_id
>> 16)
933 snprintf(buf
, buf_size
, "A");
937 snprintf(buf
, buf_size
, "Z");
941 snprintf(buf
, buf_size
, "unknown");
945 else if ((device_id
& 0x7ff) == 0x418)
947 printed
= snprintf(buf
, buf_size
, "stm32x (Connectivity) - Rev: ");
951 switch (device_id
>> 16)
954 snprintf(buf
, buf_size
, "A");
958 snprintf(buf
, buf_size
, "Z");
962 snprintf(buf
, buf_size
, "unknown");
966 else if ((device_id
& 0x7ff) == 0x420)
968 printed
= snprintf(buf
, buf_size
, "stm32x (Value) - Rev: ");
972 switch (device_id
>> 16)
975 snprintf(buf
, buf_size
, "A");
979 snprintf(buf
, buf_size
, "Z");
983 snprintf(buf
, buf_size
, "unknown");
989 snprintf(buf
, buf_size
, "Cannot identify target as a stm32x\n");
996 COMMAND_HANDLER(stm32x_handle_lock_command
)
998 struct target
*target
= NULL
;
999 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1003 command_print(CMD_CTX
, "stm32x lock <bank>");
1007 struct flash_bank
*bank
;
1008 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1009 if (ERROR_OK
!= retval
)
1012 stm32x_info
= bank
->driver_priv
;
1014 target
= bank
->target
;
1016 if (target
->state
!= TARGET_HALTED
)
1018 LOG_ERROR("Target not halted");
1019 return ERROR_TARGET_NOT_HALTED
;
1022 if (stm32x_erase_options(bank
) != ERROR_OK
)
1024 command_print(CMD_CTX
, "stm32x failed to erase options");
1028 /* set readout protection */
1029 stm32x_info
->option_bytes
.RDP
= 0;
1031 if (stm32x_write_options(bank
) != ERROR_OK
)
1033 command_print(CMD_CTX
, "stm32x failed to lock device");
1037 command_print(CMD_CTX
, "stm32x locked");
1042 COMMAND_HANDLER(stm32x_handle_unlock_command
)
1044 struct target
*target
= NULL
;
1045 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1049 command_print(CMD_CTX
, "stm32x unlock <bank>");
1053 struct flash_bank
*bank
;
1054 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1055 if (ERROR_OK
!= retval
)
1058 stm32x_info
= bank
->driver_priv
;
1060 target
= bank
->target
;
1062 if (target
->state
!= TARGET_HALTED
)
1064 LOG_ERROR("Target not halted");
1065 return ERROR_TARGET_NOT_HALTED
;
1068 if (stm32x_erase_options(bank
) != ERROR_OK
)
1070 command_print(CMD_CTX
, "stm32x failed to unlock device");
1074 if (stm32x_write_options(bank
) != ERROR_OK
)
1076 command_print(CMD_CTX
, "stm32x failed to lock device");
1080 command_print(CMD_CTX
, "stm32x unlocked.\n"
1081 "INFO: a reset or power cycle is required "
1082 "for the new settings to take effect.");
1087 COMMAND_HANDLER(stm32x_handle_options_read_command
)
1089 uint32_t optionbyte
;
1090 struct target
*target
= NULL
;
1091 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1095 command_print(CMD_CTX
, "stm32x options_read <bank>");
1099 struct flash_bank
*bank
;
1100 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1101 if (ERROR_OK
!= retval
)
1104 stm32x_info
= bank
->driver_priv
;
1106 target
= bank
->target
;
1108 if (target
->state
!= TARGET_HALTED
)
1110 LOG_ERROR("Target not halted");
1111 return ERROR_TARGET_NOT_HALTED
;
1114 retval
= target_read_u32(target
, STM32_FLASH_OBR
, &optionbyte
);
1115 if (retval
!= ERROR_OK
)
1117 command_print(CMD_CTX
, "Option Byte: 0x%" PRIx32
"", optionbyte
);
1119 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_ERROR
, 1))
1120 command_print(CMD_CTX
, "Option Byte Complement Error");
1122 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_READOUT
, 1))
1123 command_print(CMD_CTX
, "Readout Protection On");
1125 command_print(CMD_CTX
, "Readout Protection Off");
1127 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDWDGSW
, 1))
1128 command_print(CMD_CTX
, "Software Watchdog");
1130 command_print(CMD_CTX
, "Hardware Watchdog");
1132 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDRSTSTOP
, 1))
1133 command_print(CMD_CTX
, "Stop: No reset generated");
1135 command_print(CMD_CTX
, "Stop: Reset generated");
1137 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDRSTSTDBY
, 1))
1138 command_print(CMD_CTX
, "Standby: No reset generated");
1140 command_print(CMD_CTX
, "Standby: Reset generated");
1145 COMMAND_HANDLER(stm32x_handle_options_write_command
)
1147 struct target
*target
= NULL
;
1148 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1149 uint16_t optionbyte
= 0xF8;
1153 command_print(CMD_CTX
, "stm32x options_write <bank> <SWWDG | HWWDG> <RSTSTNDBY | NORSTSTNDBY> <RSTSTOP | NORSTSTOP>");
1157 struct flash_bank
*bank
;
1158 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1159 if (ERROR_OK
!= retval
)
1162 stm32x_info
= bank
->driver_priv
;
1164 target
= bank
->target
;
1166 if (target
->state
!= TARGET_HALTED
)
1168 LOG_ERROR("Target not halted");
1169 return ERROR_TARGET_NOT_HALTED
;
1172 /* REVISIT: ignores some options which we will display...
1173 * and doesn't insist on the specified syntax.
1177 if (strcmp(CMD_ARGV
[1], "SWWDG") == 0)
1179 optionbyte
|= (1 << 0);
1181 else /* REVISIT must be "HWWDG" then ... */
1183 optionbyte
&= ~(1 << 0);
1186 /* OPT_RDRSTSTDBY */
1187 if (strcmp(CMD_ARGV
[2], "NORSTSTNDBY") == 0)
1189 optionbyte
|= (1 << 1);
1191 else /* REVISIT must be "RSTSTNDBY" then ... */
1193 optionbyte
&= ~(1 << 1);
1197 if (strcmp(CMD_ARGV
[3], "NORSTSTOP") == 0)
1199 optionbyte
|= (1 << 2);
1201 else /* REVISIT must be "RSTSTOP" then ... */
1203 optionbyte
&= ~(1 << 2);
1206 if (stm32x_erase_options(bank
) != ERROR_OK
)
1208 command_print(CMD_CTX
, "stm32x failed to erase options");
1212 stm32x_info
->option_bytes
.user_options
= optionbyte
;
1214 if (stm32x_write_options(bank
) != ERROR_OK
)
1216 command_print(CMD_CTX
, "stm32x failed to write options");
1220 command_print(CMD_CTX
, "stm32x write options complete.\n"
1221 "INFO: a reset or power cycle is required "
1222 "for the new settings to take effect.");
1227 static int stm32x_mass_erase(struct flash_bank
*bank
)
1229 struct target
*target
= bank
->target
;
1231 if (target
->state
!= TARGET_HALTED
)
1233 LOG_ERROR("Target not halted");
1234 return ERROR_TARGET_NOT_HALTED
;
1237 /* unlock option flash registers */
1238 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
1239 if (retval
!= ERROR_OK
)
1241 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
1242 if (retval
!= ERROR_OK
)
1245 /* mass erase flash memory */
1246 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_MER
);
1247 if (retval
!= ERROR_OK
)
1249 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_MER
| FLASH_STRT
);
1250 if (retval
!= ERROR_OK
)
1253 retval
= stm32x_wait_status_busy(bank
, 100);
1254 if (retval
!= ERROR_OK
)
1257 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
1258 if (retval
!= ERROR_OK
)
1264 COMMAND_HANDLER(stm32x_handle_mass_erase_command
)
1270 command_print(CMD_CTX
, "stm32x mass_erase <bank>");
1274 struct flash_bank
*bank
;
1275 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1276 if (ERROR_OK
!= retval
)
1279 retval
= stm32x_mass_erase(bank
);
1280 if (retval
== ERROR_OK
)
1282 /* set all sectors as erased */
1283 for (i
= 0; i
< bank
->num_sectors
; i
++)
1285 bank
->sectors
[i
].is_erased
= 1;
1288 command_print(CMD_CTX
, "stm32x mass erase complete");
1292 command_print(CMD_CTX
, "stm32x mass erase failed");
1298 static const struct command_registration stm32x_exec_command_handlers
[] = {
1301 .handler
= stm32x_handle_lock_command
,
1302 .mode
= COMMAND_EXEC
,
1304 .help
= "Lock entire flash device.",
1308 .handler
= stm32x_handle_unlock_command
,
1309 .mode
= COMMAND_EXEC
,
1311 .help
= "Unlock entire protected flash device.",
1314 .name
= "mass_erase",
1315 .handler
= stm32x_handle_mass_erase_command
,
1316 .mode
= COMMAND_EXEC
,
1318 .help
= "Erase entire flash device.",
1321 .name
= "options_read",
1322 .handler
= stm32x_handle_options_read_command
,
1323 .mode
= COMMAND_EXEC
,
1325 .help
= "Read and display device option byte.",
1328 .name
= "options_write",
1329 .handler
= stm32x_handle_options_write_command
,
1330 .mode
= COMMAND_EXEC
,
1331 .usage
= "bank_id ('SWWDG'|'HWWDG') "
1332 "('RSTSTNDBY'|'NORSTSTNDBY') "
1333 "('RSTSTOP'|'NORSTSTOP')",
1334 .help
= "Replace bits in device option byte.",
1336 COMMAND_REGISTRATION_DONE
1339 static const struct command_registration stm32x_command_handlers
[] = {
1342 .mode
= COMMAND_ANY
,
1343 .help
= "stm32x flash command group",
1344 .chain
= stm32x_exec_command_handlers
,
1346 COMMAND_REGISTRATION_DONE
1349 struct flash_driver stm32x_flash
= {
1351 .commands
= stm32x_command_handlers
,
1352 .flash_bank_command
= stm32x_flash_bank_command
,
1353 .erase
= stm32x_erase
,
1354 .protect
= stm32x_protect
,
1355 .write
= stm32x_write
,
1356 .read
= default_flash_read
,
1357 .probe
= stm32x_probe
,
1358 .auto_probe
= stm32x_auto_probe
,
1359 .erase_check
= default_flash_mem_blank_check
,
1360 .protect_check
= stm32x_protect_check
,
1361 .info
= get_stm32x_info
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)