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 ***************************************************************************/
28 #include <helper/binarybuffer.h>
29 #include <target/algorithm.h>
30 #include <target/armv7m.h>
33 /* stm32x register locations */
35 #define STM32_FLASH_ACR 0x40022000
36 #define STM32_FLASH_KEYR 0x40022004
37 #define STM32_FLASH_OPTKEYR 0x40022008
38 #define STM32_FLASH_SR 0x4002200C
39 #define STM32_FLASH_CR 0x40022010
40 #define STM32_FLASH_AR 0x40022014
41 #define STM32_FLASH_OBR 0x4002201C
42 #define STM32_FLASH_WRPR 0x40022020
44 /* option byte location */
46 #define STM32_OB_RDP 0x1FFFF800
47 #define STM32_OB_USER 0x1FFFF802
48 #define STM32_OB_DATA0 0x1FFFF804
49 #define STM32_OB_DATA1 0x1FFFF806
50 #define STM32_OB_WRP0 0x1FFFF808
51 #define STM32_OB_WRP1 0x1FFFF80A
52 #define STM32_OB_WRP2 0x1FFFF80C
53 #define STM32_OB_WRP3 0x1FFFF80E
55 /* FLASH_CR register bits */
57 #define FLASH_PG (1 << 0)
58 #define FLASH_PER (1 << 1)
59 #define FLASH_MER (1 << 2)
60 #define FLASH_OPTPG (1 << 4)
61 #define FLASH_OPTER (1 << 5)
62 #define FLASH_STRT (1 << 6)
63 #define FLASH_LOCK (1 << 7)
64 #define FLASH_OPTWRE (1 << 9)
66 /* FLASH_SR register bits */
68 #define FLASH_BSY (1 << 0)
69 #define FLASH_PGERR (1 << 2)
70 #define FLASH_WRPRTERR (1 << 4)
71 #define FLASH_EOP (1 << 5)
73 /* STM32_FLASH_OBR bit definitions (reading) */
78 #define OPT_RDRSTSTOP 3
79 #define OPT_RDRSTSTDBY 4
81 /* register unlock keys */
83 #define KEY1 0x45670123
84 #define KEY2 0xCDEF89AB
90 uint16_t user_options
;
91 uint16_t protection
[4];
94 struct stm32x_flash_bank
96 struct stm32x_options option_bytes
;
97 struct working_area
*write_algorithm
;
102 struct stm32x_mem_layout
{
103 uint32_t sector_start
;
104 uint32_t sector_size
;
107 static int stm32x_mass_erase(struct flash_bank
*bank
);
109 /* flash bank stm32x <base> <size> 0 0 <target#>
111 FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command
)
113 struct stm32x_flash_bank
*stm32x_info
;
117 LOG_WARNING("incomplete flash_bank stm32x configuration");
118 return ERROR_FLASH_BANK_INVALID
;
121 stm32x_info
= malloc(sizeof(struct stm32x_flash_bank
));
122 bank
->driver_priv
= stm32x_info
;
124 stm32x_info
->write_algorithm
= NULL
;
125 stm32x_info
->probed
= 0;
130 static int stm32x_get_flash_status(struct flash_bank
*bank
, uint32_t *status
)
132 struct target
*target
= bank
->target
;
133 return target_read_u32(target
, STM32_FLASH_SR
, status
);
136 static int stm32x_wait_status_busy(struct flash_bank
*bank
, int timeout
)
138 struct target
*target
= bank
->target
;
140 int retval
= ERROR_OK
;
142 /* wait for busy to clear */
145 retval
= stm32x_get_flash_status(bank
, &status
);
146 if (retval
!= ERROR_OK
)
148 LOG_DEBUG("status: 0x%" PRIx32
"", status
);
149 if ((status
& FLASH_BSY
) == 0)
153 LOG_ERROR("timed out waiting for flash");
159 if (status
& FLASH_WRPRTERR
)
161 LOG_ERROR("stm32x device protected");
165 if (status
& FLASH_PGERR
)
167 LOG_ERROR("stm32x device programming failed");
171 /* Clear but report errors */
172 if (status
& (FLASH_WRPRTERR
| FLASH_PGERR
))
174 /* If this operation fails, we ignore it and report the original
177 target_write_u32(target
, STM32_FLASH_SR
, FLASH_WRPRTERR
| FLASH_PGERR
);
182 static int stm32x_read_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 /* read current option bytes */
191 int retval
= target_read_u32(target
, STM32_FLASH_OBR
, &optiondata
);
192 if (retval
!= ERROR_OK
)
195 stm32x_info
->option_bytes
.user_options
= (uint16_t)0xFFF8 | ((optiondata
>> 2) & 0x07);
196 stm32x_info
->option_bytes
.RDP
= (optiondata
& (1 << OPT_READOUT
)) ? 0xFFFF : 0x5AA5;
198 if (optiondata
& (1 << OPT_READOUT
))
199 LOG_INFO("Device Security Bit Set");
201 /* each bit refers to a 4bank protection */
202 retval
= target_read_u32(target
, STM32_FLASH_WRPR
, &optiondata
);
203 if (retval
!= ERROR_OK
)
206 stm32x_info
->option_bytes
.protection
[0] = (uint16_t)optiondata
;
207 stm32x_info
->option_bytes
.protection
[1] = (uint16_t)(optiondata
>> 8);
208 stm32x_info
->option_bytes
.protection
[2] = (uint16_t)(optiondata
>> 16);
209 stm32x_info
->option_bytes
.protection
[3] = (uint16_t)(optiondata
>> 24);
214 static int stm32x_erase_options(struct flash_bank
*bank
)
216 struct stm32x_flash_bank
*stm32x_info
= NULL
;
217 struct target
*target
= bank
->target
;
219 stm32x_info
= bank
->driver_priv
;
221 /* read current options */
222 stm32x_read_options(bank
);
224 /* unlock flash registers */
225 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
226 if (retval
!= ERROR_OK
)
229 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
230 if (retval
!= ERROR_OK
)
233 /* unlock option flash registers */
234 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY1
);
235 if (retval
!= ERROR_OK
)
237 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY2
);
238 if (retval
!= ERROR_OK
)
241 /* erase option bytes */
242 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTER
| FLASH_OPTWRE
);
243 if (retval
!= ERROR_OK
)
245 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTER
| FLASH_STRT
| FLASH_OPTWRE
);
246 if (retval
!= ERROR_OK
)
249 retval
= stm32x_wait_status_busy(bank
, 10);
250 if (retval
!= ERROR_OK
)
253 /* clear readout protection and complementary option bytes
254 * this will also force a device unlock if set */
255 stm32x_info
->option_bytes
.RDP
= 0x5AA5;
260 static int stm32x_write_options(struct flash_bank
*bank
)
262 struct stm32x_flash_bank
*stm32x_info
= NULL
;
263 struct target
*target
= bank
->target
;
265 stm32x_info
= bank
->driver_priv
;
267 /* unlock flash registers */
268 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
269 if (retval
!= ERROR_OK
)
271 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
272 if (retval
!= ERROR_OK
)
275 /* unlock option flash registers */
276 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY1
);
277 if (retval
!= ERROR_OK
)
279 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY2
);
280 if (retval
!= ERROR_OK
)
283 /* program option bytes */
284 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTPG
| FLASH_OPTWRE
);
285 if (retval
!= ERROR_OK
)
288 /* write user option byte */
289 retval
= target_write_u16(target
, STM32_OB_USER
, stm32x_info
->option_bytes
.user_options
);
290 if (retval
!= ERROR_OK
)
293 retval
= stm32x_wait_status_busy(bank
, 10);
294 if (retval
!= ERROR_OK
)
297 /* write protection byte 1 */
298 retval
= target_write_u16(target
, STM32_OB_WRP0
, stm32x_info
->option_bytes
.protection
[0]);
299 if (retval
!= ERROR_OK
)
302 retval
= stm32x_wait_status_busy(bank
, 10);
303 if (retval
!= ERROR_OK
)
306 /* write protection byte 2 */
307 retval
= target_write_u16(target
, STM32_OB_WRP1
, stm32x_info
->option_bytes
.protection
[1]);
308 if (retval
!= ERROR_OK
)
311 retval
= stm32x_wait_status_busy(bank
, 10);
312 if (retval
!= ERROR_OK
)
315 /* write protection byte 3 */
316 retval
= target_write_u16(target
, STM32_OB_WRP2
, stm32x_info
->option_bytes
.protection
[2]);
317 if (retval
!= ERROR_OK
)
320 retval
= stm32x_wait_status_busy(bank
, 10);
321 if (retval
!= ERROR_OK
)
324 /* write protection byte 4 */
325 retval
= target_write_u16(target
, STM32_OB_WRP3
, stm32x_info
->option_bytes
.protection
[3]);
326 if (retval
!= ERROR_OK
)
329 retval
= stm32x_wait_status_busy(bank
, 10);
330 if (retval
!= ERROR_OK
)
333 /* write readout protection bit */
334 retval
= target_write_u16(target
, STM32_OB_RDP
, stm32x_info
->option_bytes
.RDP
);
335 if (retval
!= ERROR_OK
)
338 retval
= stm32x_wait_status_busy(bank
, 10);
339 if (retval
!= ERROR_OK
)
342 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
343 if (retval
!= ERROR_OK
)
349 static int stm32x_protect_check(struct flash_bank
*bank
)
351 struct target
*target
= bank
->target
;
352 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
359 if (target
->state
!= TARGET_HALTED
)
361 LOG_ERROR("Target not halted");
362 return ERROR_TARGET_NOT_HALTED
;
365 /* medium density - each bit refers to a 4bank protection
366 * high density - each bit refers to a 2bank protection */
367 int retval
= target_read_u32(target
, STM32_FLASH_WRPR
, &protection
);
368 if (retval
!= ERROR_OK
)
371 /* medium density - each protection bit is for 4 * 1K pages
372 * high density - each protection bit is for 2 * 2K pages */
373 num_bits
= (bank
->num_sectors
/ stm32x_info
->ppage_size
);
375 if (stm32x_info
->ppage_size
== 2)
377 /* high density flash/connectivity line protection */
381 if (protection
& (1 << 31))
384 /* bit 31 controls sector 62 - 255 protection for high density
385 * bit 31 controls sector 62 - 127 protection for connectivity line */
386 for (s
= 62; s
< bank
->num_sectors
; s
++)
388 bank
->sectors
[s
].is_protected
= set
;
391 if (bank
->num_sectors
> 61)
394 for (i
= 0; i
< num_bits
; i
++)
398 if (protection
& (1 << i
))
401 for (s
= 0; s
< stm32x_info
->ppage_size
; s
++)
402 bank
->sectors
[(i
* stm32x_info
->ppage_size
) + s
].is_protected
= set
;
407 /* low/medium density flash protection */
408 for (i
= 0; i
< num_bits
; i
++)
412 if (protection
& (1 << i
))
415 for (s
= 0; s
< stm32x_info
->ppage_size
; s
++)
416 bank
->sectors
[(i
* stm32x_info
->ppage_size
) + s
].is_protected
= set
;
423 static int stm32x_erase(struct flash_bank
*bank
, int first
, int last
)
425 struct target
*target
= bank
->target
;
428 if (bank
->target
->state
!= TARGET_HALTED
)
430 LOG_ERROR("Target not halted");
431 return ERROR_TARGET_NOT_HALTED
;
434 if ((first
== 0) && (last
== (bank
->num_sectors
- 1)))
436 return stm32x_mass_erase(bank
);
439 /* unlock flash registers */
440 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
441 if (retval
!= ERROR_OK
)
443 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
444 if (retval
!= ERROR_OK
)
447 for (i
= first
; i
<= last
; i
++)
449 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PER
);
450 if (retval
!= ERROR_OK
)
452 retval
= target_write_u32(target
, STM32_FLASH_AR
, bank
->base
+ bank
->sectors
[i
].offset
);
453 if (retval
!= ERROR_OK
)
455 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PER
| FLASH_STRT
);
456 if (retval
!= ERROR_OK
)
459 retval
= stm32x_wait_status_busy(bank
, 100);
460 if (retval
!= ERROR_OK
)
463 bank
->sectors
[i
].is_erased
= 1;
466 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
467 if (retval
!= ERROR_OK
)
473 static int stm32x_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
475 struct stm32x_flash_bank
*stm32x_info
= NULL
;
476 struct target
*target
= bank
->target
;
477 uint16_t prot_reg
[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
482 stm32x_info
= bank
->driver_priv
;
484 if (target
->state
!= TARGET_HALTED
)
486 LOG_ERROR("Target not halted");
487 return ERROR_TARGET_NOT_HALTED
;
490 if ((first
% stm32x_info
->ppage_size
) != 0)
492 LOG_WARNING("aligned start protect sector to a %d sector boundary",
493 stm32x_info
->ppage_size
);
494 first
= first
- (first
% stm32x_info
->ppage_size
);
496 if (((last
+ 1) % stm32x_info
->ppage_size
) != 0)
498 LOG_WARNING("aligned end protect sector to a %d sector boundary",
499 stm32x_info
->ppage_size
);
501 last
= last
- (last
% stm32x_info
->ppage_size
);
505 /* medium density - each bit refers to a 4bank protection
506 * high density - each bit refers to a 2bank protection */
507 int retval
= target_read_u32(target
, STM32_FLASH_WRPR
, &protection
);
508 if (retval
!= ERROR_OK
)
511 prot_reg
[0] = (uint16_t)protection
;
512 prot_reg
[1] = (uint16_t)(protection
>> 8);
513 prot_reg
[2] = (uint16_t)(protection
>> 16);
514 prot_reg
[3] = (uint16_t)(protection
>> 24);
516 if (stm32x_info
->ppage_size
== 2)
518 /* high density flash */
520 /* bit 7 controls sector 62 - 255 protection */
524 prot_reg
[3] &= ~(1 << 7);
526 prot_reg
[3] |= (1 << 7);
534 for (i
= first
; i
<= last
; i
++)
536 reg
= (i
/ stm32x_info
->ppage_size
) / 8;
537 bit
= (i
/ stm32x_info
->ppage_size
) - (reg
* 8);
540 prot_reg
[reg
] &= ~(1 << bit
);
542 prot_reg
[reg
] |= (1 << bit
);
547 /* medium density flash */
548 for (i
= first
; i
<= last
; i
++)
550 reg
= (i
/ stm32x_info
->ppage_size
) / 8;
551 bit
= (i
/ stm32x_info
->ppage_size
) - (reg
* 8);
554 prot_reg
[reg
] &= ~(1 << bit
);
556 prot_reg
[reg
] |= (1 << bit
);
560 if ((status
= stm32x_erase_options(bank
)) != ERROR_OK
)
563 stm32x_info
->option_bytes
.protection
[0] = prot_reg
[0];
564 stm32x_info
->option_bytes
.protection
[1] = prot_reg
[1];
565 stm32x_info
->option_bytes
.protection
[2] = prot_reg
[2];
566 stm32x_info
->option_bytes
.protection
[3] = prot_reg
[3];
568 return stm32x_write_options(bank
);
571 static int stm32x_write_block(struct flash_bank
*bank
, uint8_t *buffer
,
572 uint32_t offset
, uint32_t count
)
574 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
575 struct target
*target
= bank
->target
;
576 uint32_t buffer_size
= 16384;
577 struct working_area
*source
;
578 uint32_t address
= bank
->base
+ offset
;
579 struct reg_param reg_params
[4];
580 struct armv7m_algorithm armv7m_info
;
581 int retval
= ERROR_OK
;
583 /* see contib/loaders/flash/stm32x.s for src */
585 static const uint8_t stm32x_flash_write_code
[] = {
586 /* #define STM32_FLASH_CR_OFFSET 0x10 */
587 /* #define STM32_FLASH_SR_OFFSET 0x0C */
589 0xdf, 0xf8, 0x20, 0x40, /* ldr r4, STM32_FLASH_BASE */
590 /* write_half_word: */
591 0x01, 0x23, /* movs r3, #0x01 */
592 0x23, 0x61, /* str r3, [r4, #STM32_FLASH_CR_OFFSET] */
593 0x30, 0xf8, 0x02, 0x3b, /* ldrh r3, [r0], #0x02 */
594 0x21, 0xf8, 0x02, 0x3b, /* strh r3, [r1], #0x02 */
596 0xe3, 0x68, /* ldr r3, [r4, #STM32_FLASH_SR_OFFSET] */
597 0x13, 0xf0, 0x01, 0x0f, /* tst r3, #0x01 */
598 0xfb, 0xd0, /* beq busy */
599 0x13, 0xf0, 0x14, 0x0f, /* tst r3, #0x14 */
600 0x01, 0xd1, /* bne exit */
601 0x01, 0x3a, /* subs r2, r2, #0x01 */
602 0xf0, 0xd1, /* bne write_half_word */
604 0x00, 0xbe, /* bkpt #0x00 */
605 0x00, 0x20, 0x02, 0x40, /* STM32_FLASH_BASE: .word 0x40022000 */
608 /* flash write code */
609 if (target_alloc_working_area(target
, sizeof(stm32x_flash_write_code
),
610 &stm32x_info
->write_algorithm
) != ERROR_OK
)
612 LOG_WARNING("no working area available, can't do block memory writes");
613 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
616 if ((retval
= target_write_buffer(target
, stm32x_info
->write_algorithm
->address
,
617 sizeof(stm32x_flash_write_code
),
618 (uint8_t*)stm32x_flash_write_code
)) != ERROR_OK
)
622 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
625 if (buffer_size
<= 256)
627 /* if we already allocated the writing code, but failed to get a
628 * buffer, free the algorithm */
629 if (stm32x_info
->write_algorithm
)
630 target_free_working_area(target
, stm32x_info
->write_algorithm
);
632 LOG_WARNING("no large enough working area available, can't do block memory writes");
633 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
637 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
638 armv7m_info
.core_mode
= ARMV7M_MODE_ANY
;
640 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
641 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
642 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
643 init_reg_param(®_params
[3], "r3", 32, PARAM_IN
);
647 uint32_t thisrun_count
= (count
> (buffer_size
/ 2)) ?
648 (buffer_size
/ 2) : count
;
650 if ((retval
= target_write_buffer(target
, source
->address
,
651 thisrun_count
* 2, buffer
)) != ERROR_OK
)
654 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
655 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
656 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
);
658 if ((retval
= target_run_algorithm(target
, 0, NULL
, 4, reg_params
,
659 stm32x_info
->write_algorithm
->address
,
661 10000, &armv7m_info
)) != ERROR_OK
)
663 LOG_ERROR("error executing stm32x flash write algorithm");
667 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_PGERR
)
669 LOG_ERROR("flash memory not erased before writing");
670 /* Clear but report errors */
671 target_write_u32(target
, STM32_FLASH_SR
, FLASH_PGERR
);
676 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_WRPRTERR
)
678 LOG_ERROR("flash memory write protected");
679 /* Clear but report errors */
680 target_write_u32(target
, STM32_FLASH_SR
, FLASH_WRPRTERR
);
685 buffer
+= thisrun_count
* 2;
686 address
+= thisrun_count
* 2;
687 count
-= thisrun_count
;
690 target_free_working_area(target
, source
);
691 target_free_working_area(target
, stm32x_info
->write_algorithm
);
693 destroy_reg_param(®_params
[0]);
694 destroy_reg_param(®_params
[1]);
695 destroy_reg_param(®_params
[2]);
696 destroy_reg_param(®_params
[3]);
701 static int stm32x_write(struct flash_bank
*bank
, uint8_t *buffer
,
702 uint32_t offset
, uint32_t count
)
704 struct target
*target
= bank
->target
;
705 uint32_t words_remaining
= (count
/ 2);
706 uint32_t bytes_remaining
= (count
& 0x00000001);
707 uint32_t address
= bank
->base
+ offset
;
708 uint32_t bytes_written
= 0;
711 if (bank
->target
->state
!= TARGET_HALTED
)
713 LOG_ERROR("Target not halted");
714 return ERROR_TARGET_NOT_HALTED
;
719 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
720 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
723 /* unlock flash registers */
724 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
725 if (retval
!= ERROR_OK
)
727 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
728 if (retval
!= ERROR_OK
)
731 /* multiple half words (2-byte) to be programmed? */
732 if (words_remaining
> 0)
734 /* try using a block write */
735 if ((retval
= stm32x_write_block(bank
, buffer
, offset
, words_remaining
)) != ERROR_OK
)
737 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
739 /* if block write failed (no sufficient working area),
740 * we use normal (slow) single dword accesses */
741 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
746 buffer
+= words_remaining
* 2;
747 address
+= words_remaining
* 2;
752 if ((retval
!= ERROR_OK
) && (retval
!= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
))
755 while (words_remaining
> 0)
758 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint16_t));
760 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PG
);
761 if (retval
!= ERROR_OK
)
763 retval
= target_write_u16(target
, address
, value
);
764 if (retval
!= ERROR_OK
)
767 retval
= stm32x_wait_status_busy(bank
, 5);
768 if (retval
!= ERROR_OK
)
778 uint16_t value
= 0xffff;
779 memcpy(&value
, buffer
+ bytes_written
, bytes_remaining
);
781 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_PG
);
782 if (retval
!= ERROR_OK
)
784 retval
= target_write_u16(target
, address
, value
);
785 if (retval
!= ERROR_OK
)
788 retval
= stm32x_wait_status_busy(bank
, 5);
789 if (retval
!= ERROR_OK
)
793 return target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
796 static int stm32x_probe(struct flash_bank
*bank
)
798 struct target
*target
= bank
->target
;
799 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
805 stm32x_info
->probed
= 0;
807 /* read stm32 device id register */
808 int retval
= target_read_u32(target
, 0xE0042000, &device_id
);
809 if (retval
!= ERROR_OK
)
811 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
813 /* get flash size from target. */
814 retval
= target_read_u16(target
, 0x1FFFF7E0, &num_pages
);
815 if (retval
!= ERROR_OK
)
817 LOG_WARNING("failed reading flash size, default to max target family");
818 /* failed reading flash size, default to max target family */
822 if ((device_id
& 0x7ff) == 0x410)
824 /* medium density - we have 1k pages
825 * 4 pages for a protection area */
827 stm32x_info
->ppage_size
= 4;
829 /* check for early silicon */
830 if (num_pages
== 0xffff)
832 /* number of sectors incorrect on revA */
833 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
837 else if ((device_id
& 0x7ff) == 0x412)
839 /* low density - we have 1k pages
840 * 4 pages for a protection area */
842 stm32x_info
->ppage_size
= 4;
844 /* check for early silicon */
845 if (num_pages
== 0xffff)
847 /* number of sectors incorrect on revA */
848 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 32k flash");
852 else if ((device_id
& 0x7ff) == 0x414)
854 /* high density - we have 2k pages
855 * 2 pages for a protection area */
857 stm32x_info
->ppage_size
= 2;
859 /* check for early silicon */
860 if (num_pages
== 0xffff)
862 /* number of sectors incorrect on revZ */
863 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 512k flash");
867 else if ((device_id
& 0x7ff) == 0x418)
869 /* connectivity line density - we have 2k pages
870 * 2 pages for a protection area */
872 stm32x_info
->ppage_size
= 2;
874 /* check for early silicon */
875 if (num_pages
== 0xffff)
877 /* number of sectors incorrect on revZ */
878 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash");
882 else if ((device_id
& 0x7ff) == 0x420)
884 /* value line density - we have 1k pages
885 * 4 pages for a protection area */
887 stm32x_info
->ppage_size
= 4;
889 /* check for early silicon */
890 if (num_pages
== 0xffff)
892 /* number of sectors may be incorrrect on early silicon */
893 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
899 LOG_WARNING("Cannot identify target as a STM32 family.");
903 LOG_INFO("flash size = %dkbytes", num_pages
);
905 /* calculate numbers of pages */
906 num_pages
/= (page_size
/ 1024);
911 bank
->sectors
= NULL
;
914 bank
->base
= 0x08000000;
915 bank
->size
= (num_pages
* page_size
);
916 bank
->num_sectors
= num_pages
;
917 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
919 for (i
= 0; i
< num_pages
; i
++)
921 bank
->sectors
[i
].offset
= i
* page_size
;
922 bank
->sectors
[i
].size
= page_size
;
923 bank
->sectors
[i
].is_erased
= -1;
924 bank
->sectors
[i
].is_protected
= 1;
927 stm32x_info
->probed
= 1;
932 static int stm32x_auto_probe(struct flash_bank
*bank
)
934 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
935 if (stm32x_info
->probed
)
937 return stm32x_probe(bank
);
941 COMMAND_HANDLER(stm32x_handle_part_id_command
)
947 static int get_stm32x_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
949 struct target
*target
= bank
->target
;
953 /* read stm32 device id register */
954 int retval
= target_read_u32(target
, 0xE0042000, &device_id
);
955 if (retval
!= ERROR_OK
)
958 if ((device_id
& 0x7ff) == 0x410)
960 printed
= snprintf(buf
, buf_size
, "stm32x (Medium Density) - Rev: ");
964 switch (device_id
>> 16)
967 snprintf(buf
, buf_size
, "A");
971 snprintf(buf
, buf_size
, "B");
975 snprintf(buf
, buf_size
, "Z");
979 snprintf(buf
, buf_size
, "Y");
983 snprintf(buf
, buf_size
, "unknown");
987 else if ((device_id
& 0x7ff) == 0x412)
989 printed
= snprintf(buf
, buf_size
, "stm32x (Low Density) - Rev: ");
993 switch (device_id
>> 16)
996 snprintf(buf
, buf_size
, "A");
1000 snprintf(buf
, buf_size
, "unknown");
1004 else if ((device_id
& 0x7ff) == 0x414)
1006 printed
= snprintf(buf
, buf_size
, "stm32x (High Density) - Rev: ");
1008 buf_size
-= printed
;
1010 switch (device_id
>> 16)
1013 snprintf(buf
, buf_size
, "A");
1017 snprintf(buf
, buf_size
, "Z");
1021 snprintf(buf
, buf_size
, "unknown");
1025 else if ((device_id
& 0x7ff) == 0x418)
1027 printed
= snprintf(buf
, buf_size
, "stm32x (Connectivity) - Rev: ");
1029 buf_size
-= printed
;
1031 switch (device_id
>> 16)
1034 snprintf(buf
, buf_size
, "A");
1038 snprintf(buf
, buf_size
, "Z");
1042 snprintf(buf
, buf_size
, "unknown");
1046 else if ((device_id
& 0x7ff) == 0x420)
1048 printed
= snprintf(buf
, buf_size
, "stm32x (Value) - Rev: ");
1050 buf_size
-= printed
;
1052 switch (device_id
>> 16)
1055 snprintf(buf
, buf_size
, "A");
1059 snprintf(buf
, buf_size
, "Z");
1063 snprintf(buf
, buf_size
, "unknown");
1069 snprintf(buf
, buf_size
, "Cannot identify target as a stm32x\n");
1076 COMMAND_HANDLER(stm32x_handle_lock_command
)
1078 struct target
*target
= NULL
;
1079 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1083 command_print(CMD_CTX
, "stm32x lock <bank>");
1087 struct flash_bank
*bank
;
1088 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1089 if (ERROR_OK
!= retval
)
1092 stm32x_info
= bank
->driver_priv
;
1094 target
= bank
->target
;
1096 if (target
->state
!= TARGET_HALTED
)
1098 LOG_ERROR("Target not halted");
1099 return ERROR_TARGET_NOT_HALTED
;
1102 if (stm32x_erase_options(bank
) != ERROR_OK
)
1104 command_print(CMD_CTX
, "stm32x failed to erase options");
1108 /* set readout protection */
1109 stm32x_info
->option_bytes
.RDP
= 0;
1111 if (stm32x_write_options(bank
) != ERROR_OK
)
1113 command_print(CMD_CTX
, "stm32x failed to lock device");
1117 command_print(CMD_CTX
, "stm32x locked");
1122 COMMAND_HANDLER(stm32x_handle_unlock_command
)
1124 struct target
*target
= NULL
;
1125 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1129 command_print(CMD_CTX
, "stm32x unlock <bank>");
1133 struct flash_bank
*bank
;
1134 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1135 if (ERROR_OK
!= retval
)
1138 stm32x_info
= bank
->driver_priv
;
1140 target
= bank
->target
;
1142 if (target
->state
!= TARGET_HALTED
)
1144 LOG_ERROR("Target not halted");
1145 return ERROR_TARGET_NOT_HALTED
;
1148 if (stm32x_erase_options(bank
) != ERROR_OK
)
1150 command_print(CMD_CTX
, "stm32x failed to unlock device");
1154 if (stm32x_write_options(bank
) != ERROR_OK
)
1156 command_print(CMD_CTX
, "stm32x failed to lock device");
1160 command_print(CMD_CTX
, "stm32x unlocked.\n"
1161 "INFO: a reset or power cycle is required "
1162 "for the new settings to take effect.");
1167 COMMAND_HANDLER(stm32x_handle_options_read_command
)
1169 uint32_t optionbyte
;
1170 struct target
*target
= NULL
;
1171 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1175 command_print(CMD_CTX
, "stm32x options_read <bank>");
1179 struct flash_bank
*bank
;
1180 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1181 if (ERROR_OK
!= retval
)
1184 stm32x_info
= bank
->driver_priv
;
1186 target
= bank
->target
;
1188 if (target
->state
!= TARGET_HALTED
)
1190 LOG_ERROR("Target not halted");
1191 return ERROR_TARGET_NOT_HALTED
;
1194 retval
= target_read_u32(target
, STM32_FLASH_OBR
, &optionbyte
);
1195 if (retval
!= ERROR_OK
)
1197 command_print(CMD_CTX
, "Option Byte: 0x%" PRIx32
"", optionbyte
);
1199 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_ERROR
, 1))
1200 command_print(CMD_CTX
, "Option Byte Complement Error");
1202 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_READOUT
, 1))
1203 command_print(CMD_CTX
, "Readout Protection On");
1205 command_print(CMD_CTX
, "Readout Protection Off");
1207 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDWDGSW
, 1))
1208 command_print(CMD_CTX
, "Software Watchdog");
1210 command_print(CMD_CTX
, "Hardware Watchdog");
1212 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDRSTSTOP
, 1))
1213 command_print(CMD_CTX
, "Stop: No reset generated");
1215 command_print(CMD_CTX
, "Stop: Reset generated");
1217 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDRSTSTDBY
, 1))
1218 command_print(CMD_CTX
, "Standby: No reset generated");
1220 command_print(CMD_CTX
, "Standby: Reset generated");
1225 COMMAND_HANDLER(stm32x_handle_options_write_command
)
1227 struct target
*target
= NULL
;
1228 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1229 uint16_t optionbyte
= 0xF8;
1233 command_print(CMD_CTX
, "stm32x options_write <bank> <SWWDG | HWWDG> <RSTSTNDBY | NORSTSTNDBY> <RSTSTOP | NORSTSTOP>");
1237 struct flash_bank
*bank
;
1238 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1239 if (ERROR_OK
!= retval
)
1242 stm32x_info
= bank
->driver_priv
;
1244 target
= bank
->target
;
1246 if (target
->state
!= TARGET_HALTED
)
1248 LOG_ERROR("Target not halted");
1249 return ERROR_TARGET_NOT_HALTED
;
1252 /* REVISIT: ignores some options which we will display...
1253 * and doesn't insist on the specified syntax.
1257 if (strcmp(CMD_ARGV
[1], "SWWDG") == 0)
1259 optionbyte
|= (1 << 0);
1261 else /* REVISIT must be "HWWDG" then ... */
1263 optionbyte
&= ~(1 << 0);
1266 /* OPT_RDRSTSTDBY */
1267 if (strcmp(CMD_ARGV
[2], "NORSTSTNDBY") == 0)
1269 optionbyte
|= (1 << 1);
1271 else /* REVISIT must be "RSTSTNDBY" then ... */
1273 optionbyte
&= ~(1 << 1);
1277 if (strcmp(CMD_ARGV
[3], "NORSTSTOP") == 0)
1279 optionbyte
|= (1 << 2);
1281 else /* REVISIT must be "RSTSTOP" then ... */
1283 optionbyte
&= ~(1 << 2);
1286 if (stm32x_erase_options(bank
) != ERROR_OK
)
1288 command_print(CMD_CTX
, "stm32x failed to erase options");
1292 stm32x_info
->option_bytes
.user_options
= optionbyte
;
1294 if (stm32x_write_options(bank
) != ERROR_OK
)
1296 command_print(CMD_CTX
, "stm32x failed to write options");
1300 command_print(CMD_CTX
, "stm32x write options complete.\n"
1301 "INFO: a reset or power cycle is required "
1302 "for the new settings to take effect.");
1307 static int stm32x_mass_erase(struct flash_bank
*bank
)
1309 struct target
*target
= bank
->target
;
1311 if (target
->state
!= TARGET_HALTED
)
1313 LOG_ERROR("Target not halted");
1314 return ERROR_TARGET_NOT_HALTED
;
1317 /* unlock option flash registers */
1318 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
1319 if (retval
!= ERROR_OK
)
1321 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
1322 if (retval
!= ERROR_OK
)
1325 /* mass erase flash memory */
1326 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_MER
);
1327 if (retval
!= ERROR_OK
)
1329 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_MER
| FLASH_STRT
);
1330 if (retval
!= ERROR_OK
)
1333 retval
= stm32x_wait_status_busy(bank
, 100);
1334 if (retval
!= ERROR_OK
)
1337 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
1338 if (retval
!= ERROR_OK
)
1344 COMMAND_HANDLER(stm32x_handle_mass_erase_command
)
1350 command_print(CMD_CTX
, "stm32x mass_erase <bank>");
1354 struct flash_bank
*bank
;
1355 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1356 if (ERROR_OK
!= retval
)
1359 retval
= stm32x_mass_erase(bank
);
1360 if (retval
== ERROR_OK
)
1362 /* set all sectors as erased */
1363 for (i
= 0; i
< bank
->num_sectors
; i
++)
1365 bank
->sectors
[i
].is_erased
= 1;
1368 command_print(CMD_CTX
, "stm32x mass erase complete");
1372 command_print(CMD_CTX
, "stm32x mass erase failed");
1378 static const struct command_registration stm32x_exec_command_handlers
[] = {
1381 .handler
= stm32x_handle_lock_command
,
1382 .mode
= COMMAND_EXEC
,
1384 .help
= "Lock entire flash device.",
1388 .handler
= stm32x_handle_unlock_command
,
1389 .mode
= COMMAND_EXEC
,
1391 .help
= "Unlock entire protected flash device.",
1394 .name
= "mass_erase",
1395 .handler
= stm32x_handle_mass_erase_command
,
1396 .mode
= COMMAND_EXEC
,
1398 .help
= "Erase entire flash device.",
1401 .name
= "options_read",
1402 .handler
= stm32x_handle_options_read_command
,
1403 .mode
= COMMAND_EXEC
,
1405 .help
= "Read and display device option byte.",
1408 .name
= "options_write",
1409 .handler
= stm32x_handle_options_write_command
,
1410 .mode
= COMMAND_EXEC
,
1411 .usage
= "bank_id ('SWWDG'|'HWWDG') "
1412 "('RSTSTNDBY'|'NORSTSTNDBY') "
1413 "('RSTSTOP'|'NORSTSTOP')",
1414 .help
= "Replace bits in device option byte.",
1416 COMMAND_REGISTRATION_DONE
1419 static const struct command_registration stm32x_command_handlers
[] = {
1422 .mode
= COMMAND_ANY
,
1423 .help
= "stm32x flash command group",
1424 .chain
= stm32x_exec_command_handlers
,
1426 COMMAND_REGISTRATION_DONE
1429 struct flash_driver stm32x_flash
= {
1431 .commands
= stm32x_command_handlers
,
1432 .flash_bank_command
= stm32x_flash_bank_command
,
1433 .erase
= stm32x_erase
,
1434 .protect
= stm32x_protect
,
1435 .write
= stm32x_write
,
1436 .read
= default_flash_read
,
1437 .probe
= stm32x_probe
,
1438 .auto_probe
= stm32x_auto_probe
,
1439 .erase_check
= default_flash_mem_blank_check
,
1440 .protect_check
= stm32x_protect_check
,
1441 .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)