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 target_read_u32(target
, STM32_FLASH_OBR
, &optiondata
);
120 stm32x_info
->option_bytes
.user_options
= (uint16_t)0xFFF8 | ((optiondata
>> 2) & 0x07);
121 stm32x_info
->option_bytes
.RDP
= (optiondata
& (1 << OPT_READOUT
)) ? 0xFFFF : 0x5AA5;
123 if (optiondata
& (1 << OPT_READOUT
))
124 LOG_INFO("Device Security Bit Set");
126 /* each bit refers to a 4bank protection */
127 target_read_u32(target
, STM32_FLASH_WRPR
, &optiondata
);
129 stm32x_info
->option_bytes
.protection
[0] = (uint16_t)optiondata
;
130 stm32x_info
->option_bytes
.protection
[1] = (uint16_t)(optiondata
>> 8);
131 stm32x_info
->option_bytes
.protection
[2] = (uint16_t)(optiondata
>> 16);
132 stm32x_info
->option_bytes
.protection
[3] = (uint16_t)(optiondata
>> 24);
137 static int stm32x_erase_options(struct flash_bank
*bank
)
139 struct stm32x_flash_bank
*stm32x_info
= NULL
;
140 struct target
*target
= bank
->target
;
142 stm32x_info
= bank
->driver_priv
;
144 /* read current options */
145 stm32x_read_options(bank
);
147 /* unlock flash registers */
148 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
149 if (retval
!= ERROR_OK
)
152 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
153 if (retval
!= ERROR_OK
)
156 /* unlock option flash registers */
157 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY1
);
158 if (retval
!= ERROR_OK
)
160 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY2
);
161 if (retval
!= ERROR_OK
)
164 /* erase option bytes */
165 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTER
| FLASH_OPTWRE
);
166 if (retval
!= ERROR_OK
)
168 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTER
| FLASH_STRT
| FLASH_OPTWRE
);
169 if (retval
!= ERROR_OK
)
172 retval
= stm32x_wait_status_busy(bank
, 10);
173 if (retval
!= ERROR_OK
)
176 /* clear readout protection and complementary option bytes
177 * this will also force a device unlock if set */
178 stm32x_info
->option_bytes
.RDP
= 0x5AA5;
183 static int stm32x_write_options(struct flash_bank
*bank
)
185 struct stm32x_flash_bank
*stm32x_info
= NULL
;
186 struct target
*target
= bank
->target
;
188 stm32x_info
= bank
->driver_priv
;
190 /* unlock flash registers */
191 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
192 if (retval
!= ERROR_OK
)
194 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
195 if (retval
!= ERROR_OK
)
198 /* unlock option flash registers */
199 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY1
);
200 if (retval
!= ERROR_OK
)
202 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY2
);
203 if (retval
!= ERROR_OK
)
206 /* program option bytes */
207 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTPG
| FLASH_OPTWRE
);
208 if (retval
!= ERROR_OK
)
211 /* write user option byte */
212 retval
= target_write_u16(target
, STM32_OB_USER
, stm32x_info
->option_bytes
.user_options
);
213 if (retval
!= ERROR_OK
)
216 retval
= stm32x_wait_status_busy(bank
, 10);
217 if (retval
!= ERROR_OK
)
220 /* write protection byte 1 */
221 retval
= target_write_u16(target
, STM32_OB_WRP0
, stm32x_info
->option_bytes
.protection
[0]);
222 if (retval
!= ERROR_OK
)
225 retval
= stm32x_wait_status_busy(bank
, 10);
226 if (retval
!= ERROR_OK
)
229 /* write protection byte 2 */
230 retval
= target_write_u16(target
, STM32_OB_WRP1
, stm32x_info
->option_bytes
.protection
[1]);
231 if (retval
!= ERROR_OK
)
234 retval
= stm32x_wait_status_busy(bank
, 10);
235 if (retval
!= ERROR_OK
)
238 /* write protection byte 3 */
239 retval
= target_write_u16(target
, STM32_OB_WRP2
, stm32x_info
->option_bytes
.protection
[2]);
240 if (retval
!= ERROR_OK
)
243 retval
= stm32x_wait_status_busy(bank
, 10);
244 if (retval
!= ERROR_OK
)
247 /* write protection byte 4 */
248 retval
= target_write_u16(target
, STM32_OB_WRP3
, stm32x_info
->option_bytes
.protection
[3]);
249 if (retval
!= ERROR_OK
)
252 retval
= stm32x_wait_status_busy(bank
, 10);
253 if (retval
!= ERROR_OK
)
256 /* write readout protection bit */
257 retval
= target_write_u16(target
, STM32_OB_RDP
, stm32x_info
->option_bytes
.RDP
);
258 if (retval
!= ERROR_OK
)
261 retval
= stm32x_wait_status_busy(bank
, 10);
262 if (retval
!= ERROR_OK
)
265 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
266 if (retval
!= ERROR_OK
)
272 static int stm32x_protect_check(struct flash_bank
*bank
)
274 struct target
*target
= bank
->target
;
275 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
282 if (target
->state
!= TARGET_HALTED
)
284 LOG_ERROR("Target not halted");
285 return ERROR_TARGET_NOT_HALTED
;
288 /* medium density - each bit refers to a 4bank protection
289 * high density - each bit refers to a 2bank protection */
290 target_read_u32(target
, STM32_FLASH_WRPR
, &protection
);
292 /* medium density - each protection bit is for 4 * 1K pages
293 * high density - each protection bit is for 2 * 2K pages */
294 num_bits
= (bank
->num_sectors
/ stm32x_info
->ppage_size
);
296 if (stm32x_info
->ppage_size
== 2)
298 /* high density flash/connectivity line protection */
302 if (protection
& (1 << 31))
305 /* bit 31 controls sector 62 - 255 protection for high density
306 * bit 31 controls sector 62 - 127 protection for connectivity line */
307 for (s
= 62; s
< bank
->num_sectors
; s
++)
309 bank
->sectors
[s
].is_protected
= set
;
312 if (bank
->num_sectors
> 61)
315 for (i
= 0; i
< num_bits
; i
++)
319 if (protection
& (1 << i
))
322 for (s
= 0; s
< stm32x_info
->ppage_size
; s
++)
323 bank
->sectors
[(i
* stm32x_info
->ppage_size
) + s
].is_protected
= set
;
328 /* low/medium density flash protection */
329 for (i
= 0; i
< num_bits
; i
++)
333 if (protection
& (1 << i
))
336 for (s
= 0; s
< stm32x_info
->ppage_size
; s
++)
337 bank
->sectors
[(i
* stm32x_info
->ppage_size
) + s
].is_protected
= set
;
344 static int stm32x_erase(struct flash_bank
*bank
, int first
, int last
)
346 struct target
*target
= bank
->target
;
349 if (bank
->target
->state
!= TARGET_HALTED
)
351 LOG_ERROR("Target not halted");
352 return ERROR_TARGET_NOT_HALTED
;
355 if ((first
== 0) && (last
== (bank
->num_sectors
- 1)))
357 return stm32x_mass_erase(bank
);
360 /* unlock flash registers */
361 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
362 if (retval
!= ERROR_OK
)
364 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
365 if (retval
!= ERROR_OK
)
368 for (i
= first
; i
<= last
; i
++)
370 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PER
);
371 if (retval
!= ERROR_OK
)
373 retval
= target_write_u32(target
, STM32_FLASH_AR
, bank
->base
+ bank
->sectors
[i
].offset
);
374 if (retval
!= ERROR_OK
)
376 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PER
| FLASH_STRT
);
377 if (retval
!= ERROR_OK
)
380 retval
= stm32x_wait_status_busy(bank
, 100);
381 if (retval
!= ERROR_OK
)
384 bank
->sectors
[i
].is_erased
= 1;
387 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
388 if (retval
!= ERROR_OK
)
394 static int stm32x_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
396 struct stm32x_flash_bank
*stm32x_info
= NULL
;
397 struct target
*target
= bank
->target
;
398 uint16_t prot_reg
[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
403 stm32x_info
= bank
->driver_priv
;
405 if (target
->state
!= TARGET_HALTED
)
407 LOG_ERROR("Target not halted");
408 return ERROR_TARGET_NOT_HALTED
;
411 if ((first
&& (first
% stm32x_info
->ppage_size
)) || ((last
+ 1) &&
412 (last
+ 1) % stm32x_info
->ppage_size
))
414 LOG_WARNING("Error: start and end sectors must be on a %d sector boundary",
415 stm32x_info
->ppage_size
);
416 return ERROR_FLASH_SECTOR_INVALID
;
419 /* medium density - each bit refers to a 4bank protection
420 * high density - each bit refers to a 2bank protection */
421 target_read_u32(target
, STM32_FLASH_WRPR
, &protection
);
423 prot_reg
[0] = (uint16_t)protection
;
424 prot_reg
[1] = (uint16_t)(protection
>> 8);
425 prot_reg
[2] = (uint16_t)(protection
>> 16);
426 prot_reg
[3] = (uint16_t)(protection
>> 24);
428 if (stm32x_info
->ppage_size
== 2)
430 /* high density flash */
432 /* bit 7 controls sector 62 - 255 protection */
436 prot_reg
[3] &= ~(1 << 7);
438 prot_reg
[3] |= (1 << 7);
446 for (i
= first
; i
<= last
; i
++)
448 reg
= (i
/ stm32x_info
->ppage_size
) / 8;
449 bit
= (i
/ stm32x_info
->ppage_size
) - (reg
* 8);
452 prot_reg
[reg
] &= ~(1 << bit
);
454 prot_reg
[reg
] |= (1 << bit
);
459 /* medium density flash */
460 for (i
= first
; i
<= last
; i
++)
462 reg
= (i
/ stm32x_info
->ppage_size
) / 8;
463 bit
= (i
/ stm32x_info
->ppage_size
) - (reg
* 8);
466 prot_reg
[reg
] &= ~(1 << bit
);
468 prot_reg
[reg
] |= (1 << bit
);
472 if ((status
= stm32x_erase_options(bank
)) != ERROR_OK
)
475 stm32x_info
->option_bytes
.protection
[0] = prot_reg
[0];
476 stm32x_info
->option_bytes
.protection
[1] = prot_reg
[1];
477 stm32x_info
->option_bytes
.protection
[2] = prot_reg
[2];
478 stm32x_info
->option_bytes
.protection
[3] = prot_reg
[3];
480 return stm32x_write_options(bank
);
483 static int stm32x_write_block(struct flash_bank
*bank
, uint8_t *buffer
,
484 uint32_t offset
, uint32_t count
)
486 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
487 struct target
*target
= bank
->target
;
488 uint32_t buffer_size
= 16384;
489 struct working_area
*source
;
490 uint32_t address
= bank
->base
+ offset
;
491 struct reg_param reg_params
[4];
492 struct armv7m_algorithm armv7m_info
;
493 int retval
= ERROR_OK
;
495 /* see contib/loaders/flash/stm32x.s for src */
497 static const uint8_t stm32x_flash_write_code
[] = {
499 0xDF, 0xF8, 0x24, 0x40, /* ldr r4, STM32_FLASH_CR */
500 0x09, 0x4D, /* ldr r5, STM32_FLASH_SR */
501 0x4F, 0xF0, 0x01, 0x03, /* mov r3, #1 */
502 0x23, 0x60, /* str r3, [r4, #0] */
503 0x30, 0xF8, 0x02, 0x3B, /* ldrh r3, [r0], #2 */
504 0x21, 0xF8, 0x02, 0x3B, /* strh r3, [r1], #2 */
506 0x2B, 0x68, /* ldr r3, [r5, #0] */
507 0x13, 0xF0, 0x01, 0x0F, /* tst r3, #0x01 */
508 0xFB, 0xD0, /* beq busy */
509 0x13, 0xF0, 0x14, 0x0F, /* tst r3, #0x14 */
510 0x01, 0xD1, /* bne exit */
511 0x01, 0x3A, /* subs r2, r2, #1 */
512 0xED, 0xD1, /* bne write */
514 0x00, 0xBE, /* bkpt #0 */
515 0x10, 0x20, 0x02, 0x40, /* STM32_FLASH_CR: .word 0x40022010 */
516 0x0C, 0x20, 0x02, 0x40 /* STM32_FLASH_SR: .word 0x4002200C */
519 /* flash write code */
520 if (target_alloc_working_area(target
, sizeof(stm32x_flash_write_code
),
521 &stm32x_info
->write_algorithm
) != ERROR_OK
)
523 LOG_WARNING("no working area available, can't do block memory writes");
524 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
527 if ((retval
= target_write_buffer(target
, stm32x_info
->write_algorithm
->address
,
528 sizeof(stm32x_flash_write_code
),
529 (uint8_t*)stm32x_flash_write_code
)) != ERROR_OK
)
533 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
536 if (buffer_size
<= 256)
538 /* if we already allocated the writing code, but failed to get a
539 * buffer, free the algorithm */
540 if (stm32x_info
->write_algorithm
)
541 target_free_working_area(target
, stm32x_info
->write_algorithm
);
543 LOG_WARNING("no large enough working area available, can't do block memory writes");
544 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
548 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
549 armv7m_info
.core_mode
= ARMV7M_MODE_ANY
;
551 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
552 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
553 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
554 init_reg_param(®_params
[3], "r3", 32, PARAM_IN
);
558 uint32_t thisrun_count
= (count
> (buffer_size
/ 2)) ?
559 (buffer_size
/ 2) : count
;
561 if ((retval
= target_write_buffer(target
, source
->address
,
562 thisrun_count
* 2, buffer
)) != ERROR_OK
)
565 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
566 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
567 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
);
569 if ((retval
= target_run_algorithm(target
, 0, NULL
, 4, reg_params
,
570 stm32x_info
->write_algorithm
->address
,
572 10000, &armv7m_info
)) != ERROR_OK
)
574 LOG_ERROR("error executing stm32x flash write algorithm");
578 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_PGERR
)
580 LOG_ERROR("flash memory not erased before writing");
581 /* Clear but report errors */
582 target_write_u32(target
, STM32_FLASH_SR
, FLASH_PGERR
);
587 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_WRPRTERR
)
589 LOG_ERROR("flash memory write protected");
590 /* Clear but report errors */
591 target_write_u32(target
, STM32_FLASH_SR
, FLASH_WRPRTERR
);
596 buffer
+= thisrun_count
* 2;
597 address
+= thisrun_count
* 2;
598 count
-= thisrun_count
;
601 target_free_working_area(target
, source
);
602 target_free_working_area(target
, stm32x_info
->write_algorithm
);
604 destroy_reg_param(®_params
[0]);
605 destroy_reg_param(®_params
[1]);
606 destroy_reg_param(®_params
[2]);
607 destroy_reg_param(®_params
[3]);
612 static int stm32x_write(struct flash_bank
*bank
, uint8_t *buffer
,
613 uint32_t offset
, uint32_t count
)
615 struct target
*target
= bank
->target
;
616 uint32_t words_remaining
= (count
/ 2);
617 uint32_t bytes_remaining
= (count
& 0x00000001);
618 uint32_t address
= bank
->base
+ offset
;
619 uint32_t bytes_written
= 0;
622 if (bank
->target
->state
!= TARGET_HALTED
)
624 LOG_ERROR("Target not halted");
625 return ERROR_TARGET_NOT_HALTED
;
630 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
631 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
634 /* unlock flash registers */
635 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
636 if (retval
!= ERROR_OK
)
638 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
639 if (retval
!= ERROR_OK
)
642 /* multiple half words (2-byte) to be programmed? */
643 if (words_remaining
> 0)
645 /* try using a block write */
646 if ((retval
= stm32x_write_block(bank
, buffer
, offset
, words_remaining
)) != ERROR_OK
)
648 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
650 /* if block write failed (no sufficient working area),
651 * we use normal (slow) single dword accesses */
652 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
657 buffer
+= words_remaining
* 2;
658 address
+= words_remaining
* 2;
663 while (words_remaining
> 0)
666 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint16_t));
668 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PG
);
669 if (retval
!= ERROR_OK
)
671 retval
= target_write_u16(target
, address
, value
);
672 if (retval
!= ERROR_OK
)
675 retval
= stm32x_wait_status_busy(bank
, 5);
676 if (retval
!= ERROR_OK
)
686 uint16_t value
= 0xffff;
687 memcpy(&value
, buffer
+ bytes_written
, bytes_remaining
);
689 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PG
);
690 if (retval
!= ERROR_OK
)
692 retval
= target_write_u16(target
, address
, value
);
693 if (retval
!= ERROR_OK
)
696 retval
= stm32x_wait_status_busy(bank
, 5);
697 if (retval
!= ERROR_OK
)
701 return target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
704 static int stm32x_probe(struct flash_bank
*bank
)
706 struct target
*target
= bank
->target
;
707 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
713 stm32x_info
->probed
= 0;
715 /* read stm32 device id register */
716 target_read_u32(target
, 0xE0042000, &device_id
);
717 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
719 /* get flash size from target */
720 if (target_read_u16(target
, 0x1FFFF7E0, &num_pages
) != ERROR_OK
)
722 /* failed reading flash size, default to max target family */
726 if ((device_id
& 0x7ff) == 0x410)
728 /* medium density - we have 1k pages
729 * 4 pages for a protection area */
731 stm32x_info
->ppage_size
= 4;
733 /* check for early silicon */
734 if (num_pages
== 0xffff)
736 /* number of sectors incorrect on revA */
737 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
741 else if ((device_id
& 0x7ff) == 0x412)
743 /* low 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 32k flash");
756 else if ((device_id
& 0x7ff) == 0x414)
758 /* high density - we have 2k pages
759 * 2 pages for a protection area */
761 stm32x_info
->ppage_size
= 2;
763 /* check for early silicon */
764 if (num_pages
== 0xffff)
766 /* number of sectors incorrect on revZ */
767 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 512k flash");
771 else if ((device_id
& 0x7ff) == 0x418)
773 /* connectivity line 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 256k flash");
786 else if ((device_id
& 0x7ff) == 0x420)
788 /* value line density - we have 1k pages
789 * 4 pages for a protection area */
791 stm32x_info
->ppage_size
= 4;
793 /* check for early silicon */
794 if (num_pages
== 0xffff)
796 /* number of sectors may be incorrrect on early silicon */
797 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
803 LOG_WARNING("Cannot identify target as a STM32 family.");
807 LOG_INFO("flash size = %dkbytes", num_pages
);
809 /* calculate numbers of pages */
810 num_pages
/= (page_size
/ 1024);
815 bank
->sectors
= NULL
;
818 bank
->base
= 0x08000000;
819 bank
->size
= (num_pages
* page_size
);
820 bank
->num_sectors
= num_pages
;
821 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
823 for (i
= 0; i
< num_pages
; i
++)
825 bank
->sectors
[i
].offset
= i
* page_size
;
826 bank
->sectors
[i
].size
= page_size
;
827 bank
->sectors
[i
].is_erased
= -1;
828 bank
->sectors
[i
].is_protected
= 1;
831 stm32x_info
->probed
= 1;
836 static int stm32x_auto_probe(struct flash_bank
*bank
)
838 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
839 if (stm32x_info
->probed
)
841 return stm32x_probe(bank
);
845 COMMAND_HANDLER(stm32x_handle_part_id_command
)
851 static int get_stm32x_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
853 struct target
*target
= bank
->target
;
857 /* read stm32 device id register */
858 target_read_u32(target
, 0xE0042000, &device_id
);
860 if ((device_id
& 0x7ff) == 0x410)
862 printed
= snprintf(buf
, buf_size
, "stm32x (Medium Density) - Rev: ");
866 switch (device_id
>> 16)
869 snprintf(buf
, buf_size
, "A");
873 snprintf(buf
, buf_size
, "B");
877 snprintf(buf
, buf_size
, "Z");
881 snprintf(buf
, buf_size
, "Y");
885 snprintf(buf
, buf_size
, "unknown");
889 else if ((device_id
& 0x7ff) == 0x412)
891 printed
= snprintf(buf
, buf_size
, "stm32x (Low Density) - Rev: ");
895 switch (device_id
>> 16)
898 snprintf(buf
, buf_size
, "A");
902 snprintf(buf
, buf_size
, "unknown");
906 else if ((device_id
& 0x7ff) == 0x414)
908 printed
= snprintf(buf
, buf_size
, "stm32x (High Density) - Rev: ");
912 switch (device_id
>> 16)
915 snprintf(buf
, buf_size
, "A");
919 snprintf(buf
, buf_size
, "Z");
923 snprintf(buf
, buf_size
, "unknown");
927 else if ((device_id
& 0x7ff) == 0x418)
929 printed
= snprintf(buf
, buf_size
, "stm32x (Connectivity) - Rev: ");
933 switch (device_id
>> 16)
936 snprintf(buf
, buf_size
, "A");
940 snprintf(buf
, buf_size
, "Z");
944 snprintf(buf
, buf_size
, "unknown");
948 else if ((device_id
& 0x7ff) == 0x420)
950 printed
= snprintf(buf
, buf_size
, "stm32x (Value) - Rev: ");
954 switch (device_id
>> 16)
957 snprintf(buf
, buf_size
, "A");
961 snprintf(buf
, buf_size
, "Z");
965 snprintf(buf
, buf_size
, "unknown");
971 snprintf(buf
, buf_size
, "Cannot identify target as a stm32x\n");
978 COMMAND_HANDLER(stm32x_handle_lock_command
)
980 struct target
*target
= NULL
;
981 struct stm32x_flash_bank
*stm32x_info
= NULL
;
985 command_print(CMD_CTX
, "stm32x lock <bank>");
989 struct flash_bank
*bank
;
990 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
991 if (ERROR_OK
!= retval
)
994 stm32x_info
= bank
->driver_priv
;
996 target
= bank
->target
;
998 if (target
->state
!= TARGET_HALTED
)
1000 LOG_ERROR("Target not halted");
1001 return ERROR_TARGET_NOT_HALTED
;
1004 if (stm32x_erase_options(bank
) != ERROR_OK
)
1006 command_print(CMD_CTX
, "stm32x failed to erase options");
1010 /* set readout protection */
1011 stm32x_info
->option_bytes
.RDP
= 0;
1013 if (stm32x_write_options(bank
) != ERROR_OK
)
1015 command_print(CMD_CTX
, "stm32x failed to lock device");
1019 command_print(CMD_CTX
, "stm32x locked");
1024 COMMAND_HANDLER(stm32x_handle_unlock_command
)
1026 struct target
*target
= NULL
;
1027 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1031 command_print(CMD_CTX
, "stm32x unlock <bank>");
1035 struct flash_bank
*bank
;
1036 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1037 if (ERROR_OK
!= retval
)
1040 stm32x_info
= bank
->driver_priv
;
1042 target
= bank
->target
;
1044 if (target
->state
!= TARGET_HALTED
)
1046 LOG_ERROR("Target not halted");
1047 return ERROR_TARGET_NOT_HALTED
;
1050 if (stm32x_erase_options(bank
) != ERROR_OK
)
1052 command_print(CMD_CTX
, "stm32x failed to unlock device");
1056 if (stm32x_write_options(bank
) != ERROR_OK
)
1058 command_print(CMD_CTX
, "stm32x failed to lock device");
1062 command_print(CMD_CTX
, "stm32x unlocked.\n"
1063 "INFO: a reset or power cycle is required "
1064 "for the new settings to take effect.");
1069 COMMAND_HANDLER(stm32x_handle_options_read_command
)
1071 uint32_t optionbyte
;
1072 struct target
*target
= NULL
;
1073 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1077 command_print(CMD_CTX
, "stm32x options_read <bank>");
1081 struct flash_bank
*bank
;
1082 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1083 if (ERROR_OK
!= retval
)
1086 stm32x_info
= bank
->driver_priv
;
1088 target
= bank
->target
;
1090 if (target
->state
!= TARGET_HALTED
)
1092 LOG_ERROR("Target not halted");
1093 return ERROR_TARGET_NOT_HALTED
;
1096 target_read_u32(target
, STM32_FLASH_OBR
, &optionbyte
);
1097 command_print(CMD_CTX
, "Option Byte: 0x%" PRIx32
"", optionbyte
);
1099 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_ERROR
, 1))
1100 command_print(CMD_CTX
, "Option Byte Complement Error");
1102 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_READOUT
, 1))
1103 command_print(CMD_CTX
, "Readout Protection On");
1105 command_print(CMD_CTX
, "Readout Protection Off");
1107 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDWDGSW
, 1))
1108 command_print(CMD_CTX
, "Software Watchdog");
1110 command_print(CMD_CTX
, "Hardware Watchdog");
1112 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDRSTSTOP
, 1))
1113 command_print(CMD_CTX
, "Stop: No reset generated");
1115 command_print(CMD_CTX
, "Stop: Reset generated");
1117 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDRSTSTDBY
, 1))
1118 command_print(CMD_CTX
, "Standby: No reset generated");
1120 command_print(CMD_CTX
, "Standby: Reset generated");
1125 COMMAND_HANDLER(stm32x_handle_options_write_command
)
1127 struct target
*target
= NULL
;
1128 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1129 uint16_t optionbyte
= 0xF8;
1133 command_print(CMD_CTX
, "stm32x options_write <bank> <SWWDG | HWWDG> <RSTSTNDBY | NORSTSTNDBY> <RSTSTOP | NORSTSTOP>");
1137 struct flash_bank
*bank
;
1138 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1139 if (ERROR_OK
!= retval
)
1142 stm32x_info
= bank
->driver_priv
;
1144 target
= bank
->target
;
1146 if (target
->state
!= TARGET_HALTED
)
1148 LOG_ERROR("Target not halted");
1149 return ERROR_TARGET_NOT_HALTED
;
1152 /* REVISIT: ignores some options which we will display...
1153 * and doesn't insist on the specified syntax.
1157 if (strcmp(CMD_ARGV
[1], "SWWDG") == 0)
1159 optionbyte
|= (1 << 0);
1161 else /* REVISIT must be "HWWDG" then ... */
1163 optionbyte
&= ~(1 << 0);
1166 /* OPT_RDRSTSTDBY */
1167 if (strcmp(CMD_ARGV
[2], "NORSTSTNDBY") == 0)
1169 optionbyte
|= (1 << 1);
1171 else /* REVISIT must be "RSTSTNDBY" then ... */
1173 optionbyte
&= ~(1 << 1);
1177 if (strcmp(CMD_ARGV
[3], "NORSTSTOP") == 0)
1179 optionbyte
|= (1 << 2);
1181 else /* REVISIT must be "RSTSTOP" then ... */
1183 optionbyte
&= ~(1 << 2);
1186 if (stm32x_erase_options(bank
) != ERROR_OK
)
1188 command_print(CMD_CTX
, "stm32x failed to erase options");
1192 stm32x_info
->option_bytes
.user_options
= optionbyte
;
1194 if (stm32x_write_options(bank
) != ERROR_OK
)
1196 command_print(CMD_CTX
, "stm32x failed to write options");
1200 command_print(CMD_CTX
, "stm32x write options complete.\n"
1201 "INFO: a reset or power cycle is required "
1202 "for the new settings to take effect.");
1207 static int stm32x_mass_erase(struct flash_bank
*bank
)
1209 struct target
*target
= bank
->target
;
1211 if (target
->state
!= TARGET_HALTED
)
1213 LOG_ERROR("Target not halted");
1214 return ERROR_TARGET_NOT_HALTED
;
1217 /* unlock option flash registers */
1218 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
1219 if (retval
!= ERROR_OK
)
1221 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
1222 if (retval
!= ERROR_OK
)
1225 /* mass erase flash memory */
1226 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_MER
);
1227 if (retval
!= ERROR_OK
)
1229 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_MER
| FLASH_STRT
);
1230 if (retval
!= ERROR_OK
)
1233 retval
= stm32x_wait_status_busy(bank
, 100);
1234 if (retval
!= ERROR_OK
)
1237 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
1238 if (retval
!= ERROR_OK
)
1244 COMMAND_HANDLER(stm32x_handle_mass_erase_command
)
1250 command_print(CMD_CTX
, "stm32x mass_erase <bank>");
1254 struct flash_bank
*bank
;
1255 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1256 if (ERROR_OK
!= retval
)
1259 retval
= stm32x_mass_erase(bank
);
1260 if (retval
== ERROR_OK
)
1262 /* set all sectors as erased */
1263 for (i
= 0; i
< bank
->num_sectors
; i
++)
1265 bank
->sectors
[i
].is_erased
= 1;
1268 command_print(CMD_CTX
, "stm32x mass erase complete");
1272 command_print(CMD_CTX
, "stm32x mass erase failed");
1278 static const struct command_registration stm32x_exec_command_handlers
[] = {
1281 .handler
= stm32x_handle_lock_command
,
1282 .mode
= COMMAND_EXEC
,
1284 .help
= "Lock entire flash device.",
1288 .handler
= stm32x_handle_unlock_command
,
1289 .mode
= COMMAND_EXEC
,
1291 .help
= "Unlock entire protected flash device.",
1294 .name
= "mass_erase",
1295 .handler
= stm32x_handle_mass_erase_command
,
1296 .mode
= COMMAND_EXEC
,
1298 .help
= "Erase entire flash device.",
1301 .name
= "options_read",
1302 .handler
= stm32x_handle_options_read_command
,
1303 .mode
= COMMAND_EXEC
,
1305 .help
= "Read and display device option byte.",
1308 .name
= "options_write",
1309 .handler
= stm32x_handle_options_write_command
,
1310 .mode
= COMMAND_EXEC
,
1311 .usage
= "bank_id ('SWWDG'|'HWWDG') "
1312 "('RSTSTNDBY'|'NORSTSTNDBY') "
1313 "('RSTSTOP'|'NORSTSTOP')",
1314 .help
= "Replace bits in device option byte.",
1316 COMMAND_REGISTRATION_DONE
1319 static const struct command_registration stm32x_command_handlers
[] = {
1322 .mode
= COMMAND_ANY
,
1323 .help
= "stm32x flash command group",
1324 .chain
= stm32x_exec_command_handlers
,
1326 COMMAND_REGISTRATION_DONE
1329 struct flash_driver stm32x_flash
= {
1331 .commands
= stm32x_command_handlers
,
1332 .flash_bank_command
= stm32x_flash_bank_command
,
1333 .erase
= stm32x_erase
,
1334 .protect
= stm32x_protect
,
1335 .write
= stm32x_write
,
1336 .read
= default_flash_read
,
1337 .probe
= stm32x_probe
,
1338 .auto_probe
= stm32x_auto_probe
,
1339 .erase_check
= default_flash_mem_blank_check
,
1340 .protect_check
= stm32x_protect_check
,
1341 .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)