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
[] = {
507 0xDF, 0xF8, 0x24, 0x40, /* ldr r4, STM32_FLASH_CR */
508 0x09, 0x4D, /* ldr r5, STM32_FLASH_SR */
509 0x4F, 0xF0, 0x01, 0x03, /* mov r3, #1 */
510 0x23, 0x60, /* str r3, [r4, #0] */
511 0x30, 0xF8, 0x02, 0x3B, /* ldrh r3, [r0], #2 */
512 0x21, 0xF8, 0x02, 0x3B, /* strh r3, [r1], #2 */
514 0x2B, 0x68, /* ldr r3, [r5, #0] */
515 0x13, 0xF0, 0x01, 0x0F, /* tst r3, #0x01 */
516 0xFB, 0xD0, /* beq busy */
517 0x13, 0xF0, 0x14, 0x0F, /* tst r3, #0x14 */
518 0x01, 0xD1, /* bne exit */
519 0x01, 0x3A, /* subs r2, r2, #1 */
520 0xED, 0xD1, /* bne write */
522 0x00, 0xBE, /* bkpt #0 */
523 0x10, 0x20, 0x02, 0x40, /* STM32_FLASH_CR: .word 0x40022010 */
524 0x0C, 0x20, 0x02, 0x40 /* STM32_FLASH_SR: .word 0x4002200C */
527 /* flash write code */
528 if (target_alloc_working_area(target
, sizeof(stm32x_flash_write_code
),
529 &stm32x_info
->write_algorithm
) != ERROR_OK
)
531 LOG_WARNING("no working area available, can't do block memory writes");
532 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
535 if ((retval
= target_write_buffer(target
, stm32x_info
->write_algorithm
->address
,
536 sizeof(stm32x_flash_write_code
),
537 (uint8_t*)stm32x_flash_write_code
)) != ERROR_OK
)
541 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
544 if (buffer_size
<= 256)
546 /* if we already allocated the writing code, but failed to get a
547 * buffer, free the algorithm */
548 if (stm32x_info
->write_algorithm
)
549 target_free_working_area(target
, stm32x_info
->write_algorithm
);
551 LOG_WARNING("no large enough working area available, can't do block memory writes");
552 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
556 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
557 armv7m_info
.core_mode
= ARMV7M_MODE_ANY
;
559 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
560 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
561 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
562 init_reg_param(®_params
[3], "r3", 32, PARAM_IN
);
566 uint32_t thisrun_count
= (count
> (buffer_size
/ 2)) ?
567 (buffer_size
/ 2) : count
;
569 if ((retval
= target_write_buffer(target
, source
->address
,
570 thisrun_count
* 2, buffer
)) != ERROR_OK
)
573 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
574 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
575 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
);
577 if ((retval
= target_run_algorithm(target
, 0, NULL
, 4, reg_params
,
578 stm32x_info
->write_algorithm
->address
,
580 10000, &armv7m_info
)) != ERROR_OK
)
582 LOG_ERROR("error executing stm32x flash write algorithm");
586 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_PGERR
)
588 LOG_ERROR("flash memory not erased before writing");
589 /* Clear but report errors */
590 target_write_u32(target
, STM32_FLASH_SR
, FLASH_PGERR
);
595 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_WRPRTERR
)
597 LOG_ERROR("flash memory write protected");
598 /* Clear but report errors */
599 target_write_u32(target
, STM32_FLASH_SR
, FLASH_WRPRTERR
);
604 buffer
+= thisrun_count
* 2;
605 address
+= thisrun_count
* 2;
606 count
-= thisrun_count
;
609 target_free_working_area(target
, source
);
610 target_free_working_area(target
, stm32x_info
->write_algorithm
);
612 destroy_reg_param(®_params
[0]);
613 destroy_reg_param(®_params
[1]);
614 destroy_reg_param(®_params
[2]);
615 destroy_reg_param(®_params
[3]);
620 static int stm32x_write(struct flash_bank
*bank
, uint8_t *buffer
,
621 uint32_t offset
, uint32_t count
)
623 struct target
*target
= bank
->target
;
624 uint32_t words_remaining
= (count
/ 2);
625 uint32_t bytes_remaining
= (count
& 0x00000001);
626 uint32_t address
= bank
->base
+ offset
;
627 uint32_t bytes_written
= 0;
630 if (bank
->target
->state
!= TARGET_HALTED
)
632 LOG_ERROR("Target not halted");
633 return ERROR_TARGET_NOT_HALTED
;
638 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
639 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
642 /* unlock flash registers */
643 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
644 if (retval
!= ERROR_OK
)
646 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
647 if (retval
!= ERROR_OK
)
650 /* multiple half words (2-byte) to be programmed? */
651 if (words_remaining
> 0)
653 /* try using a block write */
654 if ((retval
= stm32x_write_block(bank
, buffer
, offset
, words_remaining
)) != ERROR_OK
)
656 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
658 /* if block write failed (no sufficient working area),
659 * we use normal (slow) single dword accesses */
660 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
665 buffer
+= words_remaining
* 2;
666 address
+= words_remaining
* 2;
671 if ((retval
!= ERROR_OK
) && (retval
!= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
))
674 while (words_remaining
> 0)
677 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint16_t));
679 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PG
);
680 if (retval
!= ERROR_OK
)
682 retval
= target_write_u16(target
, address
, value
);
683 if (retval
!= ERROR_OK
)
686 retval
= stm32x_wait_status_busy(bank
, 5);
687 if (retval
!= ERROR_OK
)
697 uint16_t value
= 0xffff;
698 memcpy(&value
, buffer
+ bytes_written
, bytes_remaining
);
700 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PG
);
701 if (retval
!= ERROR_OK
)
703 retval
= target_write_u16(target
, address
, value
);
704 if (retval
!= ERROR_OK
)
707 retval
= stm32x_wait_status_busy(bank
, 5);
708 if (retval
!= ERROR_OK
)
712 return target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
715 static int stm32x_probe(struct flash_bank
*bank
)
717 struct target
*target
= bank
->target
;
718 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
724 stm32x_info
->probed
= 0;
726 /* read stm32 device id register */
727 int retval
= target_read_u32(target
, 0xE0042000, &device_id
);
728 if (retval
!= ERROR_OK
)
730 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
732 /* get flash size from target. */
733 retval
= target_read_u16(target
, 0x1FFFF7E0, &num_pages
);
734 if (retval
!= ERROR_OK
)
736 LOG_WARNING("failed reading flash size, default to max target family");
737 /* failed reading flash size, default to max target family */
741 if ((device_id
& 0x7ff) == 0x410)
743 /* medium density - we have 1k pages
744 * 4 pages for a protection area */
746 stm32x_info
->ppage_size
= 4;
748 /* check for early silicon */
749 if (num_pages
== 0xffff)
751 /* number of sectors incorrect on revA */
752 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
756 else if ((device_id
& 0x7ff) == 0x412)
758 /* low density - we have 1k pages
759 * 4 pages for a protection area */
761 stm32x_info
->ppage_size
= 4;
763 /* check for early silicon */
764 if (num_pages
== 0xffff)
766 /* number of sectors incorrect on revA */
767 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 32k flash");
771 else if ((device_id
& 0x7ff) == 0x414)
773 /* high density - we have 2k pages
774 * 2 pages for a protection area */
776 stm32x_info
->ppage_size
= 2;
778 /* check for early silicon */
779 if (num_pages
== 0xffff)
781 /* number of sectors incorrect on revZ */
782 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 512k flash");
786 else if ((device_id
& 0x7ff) == 0x418)
788 /* connectivity line density - we have 2k pages
789 * 2 pages for a protection area */
791 stm32x_info
->ppage_size
= 2;
793 /* check for early silicon */
794 if (num_pages
== 0xffff)
796 /* number of sectors incorrect on revZ */
797 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash");
801 else if ((device_id
& 0x7ff) == 0x420)
803 /* value line density - we have 1k pages
804 * 4 pages for a protection area */
806 stm32x_info
->ppage_size
= 4;
808 /* check for early silicon */
809 if (num_pages
== 0xffff)
811 /* number of sectors may be incorrrect on early silicon */
812 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
818 LOG_WARNING("Cannot identify target as a STM32 family.");
822 LOG_INFO("flash size = %dkbytes", num_pages
);
824 /* calculate numbers of pages */
825 num_pages
/= (page_size
/ 1024);
830 bank
->sectors
= NULL
;
833 bank
->base
= 0x08000000;
834 bank
->size
= (num_pages
* page_size
);
835 bank
->num_sectors
= num_pages
;
836 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
838 for (i
= 0; i
< num_pages
; i
++)
840 bank
->sectors
[i
].offset
= i
* page_size
;
841 bank
->sectors
[i
].size
= page_size
;
842 bank
->sectors
[i
].is_erased
= -1;
843 bank
->sectors
[i
].is_protected
= 1;
846 stm32x_info
->probed
= 1;
851 static int stm32x_auto_probe(struct flash_bank
*bank
)
853 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
854 if (stm32x_info
->probed
)
856 return stm32x_probe(bank
);
860 COMMAND_HANDLER(stm32x_handle_part_id_command
)
866 static int get_stm32x_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
868 struct target
*target
= bank
->target
;
872 /* read stm32 device id register */
873 int retval
= target_read_u32(target
, 0xE0042000, &device_id
);
874 if (retval
!= ERROR_OK
)
877 if ((device_id
& 0x7ff) == 0x410)
879 printed
= snprintf(buf
, buf_size
, "stm32x (Medium Density) - Rev: ");
883 switch (device_id
>> 16)
886 snprintf(buf
, buf_size
, "A");
890 snprintf(buf
, buf_size
, "B");
894 snprintf(buf
, buf_size
, "Z");
898 snprintf(buf
, buf_size
, "Y");
902 snprintf(buf
, buf_size
, "unknown");
906 else if ((device_id
& 0x7ff) == 0x412)
908 printed
= snprintf(buf
, buf_size
, "stm32x (Low Density) - Rev: ");
912 switch (device_id
>> 16)
915 snprintf(buf
, buf_size
, "A");
919 snprintf(buf
, buf_size
, "unknown");
923 else if ((device_id
& 0x7ff) == 0x414)
925 printed
= snprintf(buf
, buf_size
, "stm32x (High Density) - Rev: ");
929 switch (device_id
>> 16)
932 snprintf(buf
, buf_size
, "A");
936 snprintf(buf
, buf_size
, "Z");
940 snprintf(buf
, buf_size
, "unknown");
944 else if ((device_id
& 0x7ff) == 0x418)
946 printed
= snprintf(buf
, buf_size
, "stm32x (Connectivity) - Rev: ");
950 switch (device_id
>> 16)
953 snprintf(buf
, buf_size
, "A");
957 snprintf(buf
, buf_size
, "Z");
961 snprintf(buf
, buf_size
, "unknown");
965 else if ((device_id
& 0x7ff) == 0x420)
967 printed
= snprintf(buf
, buf_size
, "stm32x (Value) - Rev: ");
971 switch (device_id
>> 16)
974 snprintf(buf
, buf_size
, "A");
978 snprintf(buf
, buf_size
, "Z");
982 snprintf(buf
, buf_size
, "unknown");
988 snprintf(buf
, buf_size
, "Cannot identify target as a stm32x\n");
995 COMMAND_HANDLER(stm32x_handle_lock_command
)
997 struct target
*target
= NULL
;
998 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1002 command_print(CMD_CTX
, "stm32x lock <bank>");
1006 struct flash_bank
*bank
;
1007 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1008 if (ERROR_OK
!= retval
)
1011 stm32x_info
= bank
->driver_priv
;
1013 target
= bank
->target
;
1015 if (target
->state
!= TARGET_HALTED
)
1017 LOG_ERROR("Target not halted");
1018 return ERROR_TARGET_NOT_HALTED
;
1021 if (stm32x_erase_options(bank
) != ERROR_OK
)
1023 command_print(CMD_CTX
, "stm32x failed to erase options");
1027 /* set readout protection */
1028 stm32x_info
->option_bytes
.RDP
= 0;
1030 if (stm32x_write_options(bank
) != ERROR_OK
)
1032 command_print(CMD_CTX
, "stm32x failed to lock device");
1036 command_print(CMD_CTX
, "stm32x locked");
1041 COMMAND_HANDLER(stm32x_handle_unlock_command
)
1043 struct target
*target
= NULL
;
1044 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1048 command_print(CMD_CTX
, "stm32x unlock <bank>");
1052 struct flash_bank
*bank
;
1053 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1054 if (ERROR_OK
!= retval
)
1057 stm32x_info
= bank
->driver_priv
;
1059 target
= bank
->target
;
1061 if (target
->state
!= TARGET_HALTED
)
1063 LOG_ERROR("Target not halted");
1064 return ERROR_TARGET_NOT_HALTED
;
1067 if (stm32x_erase_options(bank
) != ERROR_OK
)
1069 command_print(CMD_CTX
, "stm32x failed to unlock device");
1073 if (stm32x_write_options(bank
) != ERROR_OK
)
1075 command_print(CMD_CTX
, "stm32x failed to lock device");
1079 command_print(CMD_CTX
, "stm32x unlocked.\n"
1080 "INFO: a reset or power cycle is required "
1081 "for the new settings to take effect.");
1086 COMMAND_HANDLER(stm32x_handle_options_read_command
)
1088 uint32_t optionbyte
;
1089 struct target
*target
= NULL
;
1090 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1094 command_print(CMD_CTX
, "stm32x options_read <bank>");
1098 struct flash_bank
*bank
;
1099 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1100 if (ERROR_OK
!= retval
)
1103 stm32x_info
= bank
->driver_priv
;
1105 target
= bank
->target
;
1107 if (target
->state
!= TARGET_HALTED
)
1109 LOG_ERROR("Target not halted");
1110 return ERROR_TARGET_NOT_HALTED
;
1113 retval
= target_read_u32(target
, STM32_FLASH_OBR
, &optionbyte
);
1114 if (retval
!= ERROR_OK
)
1116 command_print(CMD_CTX
, "Option Byte: 0x%" PRIx32
"", optionbyte
);
1118 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_ERROR
, 1))
1119 command_print(CMD_CTX
, "Option Byte Complement Error");
1121 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_READOUT
, 1))
1122 command_print(CMD_CTX
, "Readout Protection On");
1124 command_print(CMD_CTX
, "Readout Protection Off");
1126 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDWDGSW
, 1))
1127 command_print(CMD_CTX
, "Software Watchdog");
1129 command_print(CMD_CTX
, "Hardware Watchdog");
1131 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDRSTSTOP
, 1))
1132 command_print(CMD_CTX
, "Stop: No reset generated");
1134 command_print(CMD_CTX
, "Stop: Reset generated");
1136 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDRSTSTDBY
, 1))
1137 command_print(CMD_CTX
, "Standby: No reset generated");
1139 command_print(CMD_CTX
, "Standby: Reset generated");
1144 COMMAND_HANDLER(stm32x_handle_options_write_command
)
1146 struct target
*target
= NULL
;
1147 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1148 uint16_t optionbyte
= 0xF8;
1152 command_print(CMD_CTX
, "stm32x options_write <bank> <SWWDG | HWWDG> <RSTSTNDBY | NORSTSTNDBY> <RSTSTOP | NORSTSTOP>");
1156 struct flash_bank
*bank
;
1157 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1158 if (ERROR_OK
!= retval
)
1161 stm32x_info
= bank
->driver_priv
;
1163 target
= bank
->target
;
1165 if (target
->state
!= TARGET_HALTED
)
1167 LOG_ERROR("Target not halted");
1168 return ERROR_TARGET_NOT_HALTED
;
1171 /* REVISIT: ignores some options which we will display...
1172 * and doesn't insist on the specified syntax.
1176 if (strcmp(CMD_ARGV
[1], "SWWDG") == 0)
1178 optionbyte
|= (1 << 0);
1180 else /* REVISIT must be "HWWDG" then ... */
1182 optionbyte
&= ~(1 << 0);
1185 /* OPT_RDRSTSTDBY */
1186 if (strcmp(CMD_ARGV
[2], "NORSTSTNDBY") == 0)
1188 optionbyte
|= (1 << 1);
1190 else /* REVISIT must be "RSTSTNDBY" then ... */
1192 optionbyte
&= ~(1 << 1);
1196 if (strcmp(CMD_ARGV
[3], "NORSTSTOP") == 0)
1198 optionbyte
|= (1 << 2);
1200 else /* REVISIT must be "RSTSTOP" then ... */
1202 optionbyte
&= ~(1 << 2);
1205 if (stm32x_erase_options(bank
) != ERROR_OK
)
1207 command_print(CMD_CTX
, "stm32x failed to erase options");
1211 stm32x_info
->option_bytes
.user_options
= optionbyte
;
1213 if (stm32x_write_options(bank
) != ERROR_OK
)
1215 command_print(CMD_CTX
, "stm32x failed to write options");
1219 command_print(CMD_CTX
, "stm32x write options complete.\n"
1220 "INFO: a reset or power cycle is required "
1221 "for the new settings to take effect.");
1226 static int stm32x_mass_erase(struct flash_bank
*bank
)
1228 struct target
*target
= bank
->target
;
1230 if (target
->state
!= TARGET_HALTED
)
1232 LOG_ERROR("Target not halted");
1233 return ERROR_TARGET_NOT_HALTED
;
1236 /* unlock option flash registers */
1237 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
1238 if (retval
!= ERROR_OK
)
1240 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
1241 if (retval
!= ERROR_OK
)
1244 /* mass erase flash memory */
1245 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_MER
);
1246 if (retval
!= ERROR_OK
)
1248 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_MER
| FLASH_STRT
);
1249 if (retval
!= ERROR_OK
)
1252 retval
= stm32x_wait_status_busy(bank
, 100);
1253 if (retval
!= ERROR_OK
)
1256 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
1257 if (retval
!= ERROR_OK
)
1263 COMMAND_HANDLER(stm32x_handle_mass_erase_command
)
1269 command_print(CMD_CTX
, "stm32x mass_erase <bank>");
1273 struct flash_bank
*bank
;
1274 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1275 if (ERROR_OK
!= retval
)
1278 retval
= stm32x_mass_erase(bank
);
1279 if (retval
== ERROR_OK
)
1281 /* set all sectors as erased */
1282 for (i
= 0; i
< bank
->num_sectors
; i
++)
1284 bank
->sectors
[i
].is_erased
= 1;
1287 command_print(CMD_CTX
, "stm32x mass erase complete");
1291 command_print(CMD_CTX
, "stm32x mass erase failed");
1297 static const struct command_registration stm32x_exec_command_handlers
[] = {
1300 .handler
= stm32x_handle_lock_command
,
1301 .mode
= COMMAND_EXEC
,
1303 .help
= "Lock entire flash device.",
1307 .handler
= stm32x_handle_unlock_command
,
1308 .mode
= COMMAND_EXEC
,
1310 .help
= "Unlock entire protected flash device.",
1313 .name
= "mass_erase",
1314 .handler
= stm32x_handle_mass_erase_command
,
1315 .mode
= COMMAND_EXEC
,
1317 .help
= "Erase entire flash device.",
1320 .name
= "options_read",
1321 .handler
= stm32x_handle_options_read_command
,
1322 .mode
= COMMAND_EXEC
,
1324 .help
= "Read and display device option byte.",
1327 .name
= "options_write",
1328 .handler
= stm32x_handle_options_write_command
,
1329 .mode
= COMMAND_EXEC
,
1330 .usage
= "bank_id ('SWWDG'|'HWWDG') "
1331 "('RSTSTNDBY'|'NORSTSTNDBY') "
1332 "('RSTSTOP'|'NORSTSTOP')",
1333 .help
= "Replace bits in device option byte.",
1335 COMMAND_REGISTRATION_DONE
1338 static const struct command_registration stm32x_command_handlers
[] = {
1341 .mode
= COMMAND_ANY
,
1342 .help
= "stm32x flash command group",
1343 .chain
= stm32x_exec_command_handlers
,
1345 COMMAND_REGISTRATION_DONE
1348 struct flash_driver stm32x_flash
= {
1350 .commands
= stm32x_command_handlers
,
1351 .flash_bank_command
= stm32x_flash_bank_command
,
1352 .erase
= stm32x_erase
,
1353 .protect
= stm32x_protect
,
1354 .write
= stm32x_write
,
1355 .read
= default_flash_read
,
1356 .probe
= stm32x_probe
,
1357 .auto_probe
= stm32x_auto_probe
,
1358 .erase_check
= default_flash_mem_blank_check
,
1359 .protect_check
= stm32x_protect_check
,
1360 .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)