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>
32 /* stm32x register locations */
34 #define STM32_FLASH_ACR 0x40022000
35 #define STM32_FLASH_KEYR 0x40022004
36 #define STM32_FLASH_OPTKEYR 0x40022008
37 #define STM32_FLASH_SR 0x4002200C
38 #define STM32_FLASH_CR 0x40022010
39 #define STM32_FLASH_AR 0x40022014
40 #define STM32_FLASH_OBR 0x4002201C
41 #define STM32_FLASH_WRPR 0x40022020
43 /* option byte location */
45 #define STM32_OB_RDP 0x1FFFF800
46 #define STM32_OB_USER 0x1FFFF802
47 #define STM32_OB_DATA0 0x1FFFF804
48 #define STM32_OB_DATA1 0x1FFFF806
49 #define STM32_OB_WRP0 0x1FFFF808
50 #define STM32_OB_WRP1 0x1FFFF80A
51 #define STM32_OB_WRP2 0x1FFFF80C
52 #define STM32_OB_WRP3 0x1FFFF80E
54 /* FLASH_CR register bits */
56 #define FLASH_PG (1 << 0)
57 #define FLASH_PER (1 << 1)
58 #define FLASH_MER (1 << 2)
59 #define FLASH_OPTPG (1 << 4)
60 #define FLASH_OPTER (1 << 5)
61 #define FLASH_STRT (1 << 6)
62 #define FLASH_LOCK (1 << 7)
63 #define FLASH_OPTWRE (1 << 9)
65 /* FLASH_SR register bits */
67 #define FLASH_BSY (1 << 0)
68 #define FLASH_PGERR (1 << 2)
69 #define FLASH_WRPRTERR (1 << 4)
70 #define FLASH_EOP (1 << 5)
72 /* STM32_FLASH_OBR bit definitions (reading) */
77 #define OPT_RDRSTSTOP 3
78 #define OPT_RDRSTSTDBY 4
80 /* register unlock keys */
82 #define KEY1 0x45670123
83 #define KEY2 0xCDEF89AB
88 uint16_t user_options
;
89 uint16_t protection
[4];
92 struct stm32x_flash_bank
94 struct stm32x_options option_bytes
;
95 struct working_area
*write_algorithm
;
99 /* used to access dual flash bank stm32xl
100 * 0x00 will address sector 0 flash
101 * 0x40 will address sector 1 flash */
105 static int stm32x_mass_erase(struct flash_bank
*bank
);
107 /* flash bank stm32x <base> <size> 0 0 <target#>
109 FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command
)
111 struct stm32x_flash_bank
*stm32x_info
;
115 LOG_WARNING("incomplete flash_bank stm32x configuration");
116 return ERROR_FLASH_BANK_INVALID
;
119 stm32x_info
= malloc(sizeof(struct stm32x_flash_bank
));
120 bank
->driver_priv
= stm32x_info
;
122 stm32x_info
->write_algorithm
= NULL
;
123 stm32x_info
->probed
= 0;
124 stm32x_info
->register_offset
= 0x00;
129 static inline int stm32x_get_flash_reg(struct flash_bank
*bank
, uint32_t reg
)
131 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
132 return reg
+ stm32x_info
->register_offset
;
135 static inline int stm32x_get_flash_status(struct flash_bank
*bank
, uint32_t *status
)
137 struct target
*target
= bank
->target
;
138 return target_read_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_SR
), status
);
141 static int stm32x_wait_status_busy(struct flash_bank
*bank
, int timeout
)
143 struct target
*target
= bank
->target
;
145 int retval
= ERROR_OK
;
147 /* wait for busy to clear */
150 retval
= stm32x_get_flash_status(bank
, &status
);
151 if (retval
!= ERROR_OK
)
153 LOG_DEBUG("status: 0x%" PRIx32
"", status
);
154 if ((status
& FLASH_BSY
) == 0)
158 LOG_ERROR("timed out waiting for flash");
164 if (status
& FLASH_WRPRTERR
)
166 LOG_ERROR("stm32x device protected");
170 if (status
& FLASH_PGERR
)
172 LOG_ERROR("stm32x device programming failed");
176 /* Clear but report errors */
177 if (status
& (FLASH_WRPRTERR
| FLASH_PGERR
))
179 /* If this operation fails, we ignore it and report the original
182 target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_SR
),
183 FLASH_WRPRTERR
| FLASH_PGERR
);
188 static int stm32x_read_options(struct flash_bank
*bank
)
191 struct stm32x_flash_bank
*stm32x_info
= NULL
;
192 struct target
*target
= bank
->target
;
194 stm32x_info
= bank
->driver_priv
;
196 /* read current option bytes */
197 int retval
= target_read_u32(target
, STM32_FLASH_OBR
, &optiondata
);
198 if (retval
!= ERROR_OK
)
201 stm32x_info
->option_bytes
.user_options
= (uint16_t)0xFFF8 | ((optiondata
>> 2) & 0x07);
202 stm32x_info
->option_bytes
.RDP
= (optiondata
& (1 << OPT_READOUT
)) ? 0xFFFF : 0x5AA5;
204 if (optiondata
& (1 << OPT_READOUT
))
205 LOG_INFO("Device Security Bit Set");
207 /* each bit refers to a 4bank protection */
208 retval
= target_read_u32(target
, STM32_FLASH_WRPR
, &optiondata
);
209 if (retval
!= ERROR_OK
)
212 stm32x_info
->option_bytes
.protection
[0] = (uint16_t)optiondata
;
213 stm32x_info
->option_bytes
.protection
[1] = (uint16_t)(optiondata
>> 8);
214 stm32x_info
->option_bytes
.protection
[2] = (uint16_t)(optiondata
>> 16);
215 stm32x_info
->option_bytes
.protection
[3] = (uint16_t)(optiondata
>> 24);
220 static int stm32x_erase_options(struct flash_bank
*bank
)
222 struct stm32x_flash_bank
*stm32x_info
= NULL
;
223 struct target
*target
= bank
->target
;
225 stm32x_info
= bank
->driver_priv
;
227 /* read current options */
228 stm32x_read_options(bank
);
230 /* unlock flash registers */
231 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
232 if (retval
!= ERROR_OK
)
235 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
236 if (retval
!= ERROR_OK
)
239 /* unlock option flash registers */
240 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY1
);
241 if (retval
!= ERROR_OK
)
243 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY2
);
244 if (retval
!= ERROR_OK
)
247 /* erase option bytes */
248 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTER
| FLASH_OPTWRE
);
249 if (retval
!= ERROR_OK
)
251 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTER
| FLASH_STRT
| FLASH_OPTWRE
);
252 if (retval
!= ERROR_OK
)
255 retval
= stm32x_wait_status_busy(bank
, 10);
256 if (retval
!= ERROR_OK
)
259 /* clear readout protection and complementary option bytes
260 * this will also force a device unlock if set */
261 stm32x_info
->option_bytes
.RDP
= 0x5AA5;
266 static int stm32x_write_options(struct flash_bank
*bank
)
268 struct stm32x_flash_bank
*stm32x_info
= NULL
;
269 struct target
*target
= bank
->target
;
271 stm32x_info
= bank
->driver_priv
;
273 /* unlock flash registers */
274 int retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY1
);
275 if (retval
!= ERROR_OK
)
277 retval
= target_write_u32(target
, STM32_FLASH_KEYR
, KEY2
);
278 if (retval
!= ERROR_OK
)
281 /* unlock option flash registers */
282 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY1
);
283 if (retval
!= ERROR_OK
)
285 retval
= target_write_u32(target
, STM32_FLASH_OPTKEYR
, KEY2
);
286 if (retval
!= ERROR_OK
)
289 /* program option bytes */
290 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_OPTPG
| FLASH_OPTWRE
);
291 if (retval
!= ERROR_OK
)
294 /* write user option byte */
295 retval
= target_write_u16(target
, STM32_OB_USER
, stm32x_info
->option_bytes
.user_options
);
296 if (retval
!= ERROR_OK
)
299 retval
= stm32x_wait_status_busy(bank
, 10);
300 if (retval
!= ERROR_OK
)
303 /* write protection byte 1 */
304 retval
= target_write_u16(target
, STM32_OB_WRP0
, stm32x_info
->option_bytes
.protection
[0]);
305 if (retval
!= ERROR_OK
)
308 retval
= stm32x_wait_status_busy(bank
, 10);
309 if (retval
!= ERROR_OK
)
312 /* write protection byte 2 */
313 retval
= target_write_u16(target
, STM32_OB_WRP1
, stm32x_info
->option_bytes
.protection
[1]);
314 if (retval
!= ERROR_OK
)
317 retval
= stm32x_wait_status_busy(bank
, 10);
318 if (retval
!= ERROR_OK
)
321 /* write protection byte 3 */
322 retval
= target_write_u16(target
, STM32_OB_WRP2
, stm32x_info
->option_bytes
.protection
[2]);
323 if (retval
!= ERROR_OK
)
326 retval
= stm32x_wait_status_busy(bank
, 10);
327 if (retval
!= ERROR_OK
)
330 /* write protection byte 4 */
331 retval
= target_write_u16(target
, STM32_OB_WRP3
, stm32x_info
->option_bytes
.protection
[3]);
332 if (retval
!= ERROR_OK
)
335 retval
= stm32x_wait_status_busy(bank
, 10);
336 if (retval
!= ERROR_OK
)
339 /* write readout protection bit */
340 retval
= target_write_u16(target
, STM32_OB_RDP
, stm32x_info
->option_bytes
.RDP
);
341 if (retval
!= ERROR_OK
)
344 retval
= stm32x_wait_status_busy(bank
, 10);
345 if (retval
!= ERROR_OK
)
348 retval
= target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
349 if (retval
!= ERROR_OK
)
355 static int stm32x_protect_check(struct flash_bank
*bank
)
357 struct target
*target
= bank
->target
;
358 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
365 if (target
->state
!= TARGET_HALTED
)
367 LOG_ERROR("Target not halted");
368 return ERROR_TARGET_NOT_HALTED
;
371 /* medium density - each bit refers to a 4bank protection
372 * high density - each bit refers to a 2bank protection */
373 int retval
= target_read_u32(target
, STM32_FLASH_WRPR
, &protection
);
374 if (retval
!= ERROR_OK
)
377 /* medium density - each protection bit is for 4 * 1K pages
378 * high density - each protection bit is for 2 * 2K pages */
379 num_bits
= (bank
->num_sectors
/ stm32x_info
->ppage_size
);
381 if (stm32x_info
->ppage_size
== 2)
383 /* high density flash/connectivity line protection */
387 if (protection
& (1 << 31))
390 /* bit 31 controls sector 62 - 255 protection for high density
391 * bit 31 controls sector 62 - 127 protection for connectivity line */
392 for (s
= 62; s
< bank
->num_sectors
; s
++)
394 bank
->sectors
[s
].is_protected
= set
;
397 if (bank
->num_sectors
> 61)
400 for (i
= 0; i
< num_bits
; i
++)
404 if (protection
& (1 << i
))
407 for (s
= 0; s
< stm32x_info
->ppage_size
; s
++)
408 bank
->sectors
[(i
* stm32x_info
->ppage_size
) + s
].is_protected
= set
;
413 /* low/medium density flash protection */
414 for (i
= 0; i
< num_bits
; i
++)
418 if (protection
& (1 << i
))
421 for (s
= 0; s
< stm32x_info
->ppage_size
; s
++)
422 bank
->sectors
[(i
* stm32x_info
->ppage_size
) + s
].is_protected
= set
;
429 static int stm32x_erase(struct flash_bank
*bank
, int first
, int last
)
431 struct target
*target
= bank
->target
;
434 if (bank
->target
->state
!= TARGET_HALTED
)
436 LOG_ERROR("Target not halted");
437 return ERROR_TARGET_NOT_HALTED
;
440 if ((first
== 0) && (last
== (bank
->num_sectors
- 1)))
442 return stm32x_mass_erase(bank
);
445 /* unlock flash registers */
446 int retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_KEYR
), KEY1
);
447 if (retval
!= ERROR_OK
)
449 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_KEYR
), KEY2
);
450 if (retval
!= ERROR_OK
)
453 for (i
= first
; i
<= last
; i
++)
455 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_PER
);
456 if (retval
!= ERROR_OK
)
458 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_AR
),
459 bank
->base
+ bank
->sectors
[i
].offset
);
460 if (retval
!= ERROR_OK
)
462 retval
= target_write_u32(target
,
463 stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_PER
| FLASH_STRT
);
464 if (retval
!= ERROR_OK
)
467 retval
= stm32x_wait_status_busy(bank
, 100);
468 if (retval
!= ERROR_OK
)
471 bank
->sectors
[i
].is_erased
= 1;
474 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_LOCK
);
475 if (retval
!= ERROR_OK
)
481 static int stm32x_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
483 struct stm32x_flash_bank
*stm32x_info
= NULL
;
484 struct target
*target
= bank
->target
;
485 uint16_t prot_reg
[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
490 stm32x_info
= bank
->driver_priv
;
492 if (target
->state
!= TARGET_HALTED
)
494 LOG_ERROR("Target not halted");
495 return ERROR_TARGET_NOT_HALTED
;
498 if ((first
% stm32x_info
->ppage_size
) != 0)
500 LOG_WARNING("aligned start protect sector to a %d sector boundary",
501 stm32x_info
->ppage_size
);
502 first
= first
- (first
% stm32x_info
->ppage_size
);
504 if (((last
+ 1) % stm32x_info
->ppage_size
) != 0)
506 LOG_WARNING("aligned end protect sector to a %d sector boundary",
507 stm32x_info
->ppage_size
);
509 last
= last
- (last
% stm32x_info
->ppage_size
);
513 /* medium density - each bit refers to a 4bank protection
514 * high density - each bit refers to a 2bank protection */
515 int retval
= target_read_u32(target
, STM32_FLASH_WRPR
, &protection
);
516 if (retval
!= ERROR_OK
)
519 prot_reg
[0] = (uint16_t)protection
;
520 prot_reg
[1] = (uint16_t)(protection
>> 8);
521 prot_reg
[2] = (uint16_t)(protection
>> 16);
522 prot_reg
[3] = (uint16_t)(protection
>> 24);
524 if (stm32x_info
->ppage_size
== 2)
526 /* high density flash */
528 /* bit 7 controls sector 62 - 255 protection */
532 prot_reg
[3] &= ~(1 << 7);
534 prot_reg
[3] |= (1 << 7);
542 for (i
= first
; i
<= last
; i
++)
544 reg
= (i
/ stm32x_info
->ppage_size
) / 8;
545 bit
= (i
/ stm32x_info
->ppage_size
) - (reg
* 8);
548 prot_reg
[reg
] &= ~(1 << bit
);
550 prot_reg
[reg
] |= (1 << bit
);
555 /* medium density flash */
556 for (i
= first
; i
<= last
; i
++)
558 reg
= (i
/ stm32x_info
->ppage_size
) / 8;
559 bit
= (i
/ stm32x_info
->ppage_size
) - (reg
* 8);
562 prot_reg
[reg
] &= ~(1 << bit
);
564 prot_reg
[reg
] |= (1 << bit
);
568 if ((status
= stm32x_erase_options(bank
)) != ERROR_OK
)
571 stm32x_info
->option_bytes
.protection
[0] = prot_reg
[0];
572 stm32x_info
->option_bytes
.protection
[1] = prot_reg
[1];
573 stm32x_info
->option_bytes
.protection
[2] = prot_reg
[2];
574 stm32x_info
->option_bytes
.protection
[3] = prot_reg
[3];
576 return stm32x_write_options(bank
);
579 static int stm32x_write_block(struct flash_bank
*bank
, uint8_t *buffer
,
580 uint32_t offset
, uint32_t count
)
582 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
583 struct target
*target
= bank
->target
;
584 uint32_t buffer_size
= 16384;
585 struct working_area
*source
;
586 uint32_t address
= bank
->base
+ offset
;
587 struct reg_param reg_params
[4];
588 struct armv7m_algorithm armv7m_info
;
589 int retval
= ERROR_OK
;
591 /* see contib/loaders/flash/stm32x.s for src */
593 static const uint8_t stm32x_flash_write_code
[] = {
594 /* #define STM32_FLASH_CR_OFFSET 0x10 */
595 /* #define STM32_FLASH_SR_OFFSET 0x0C */
597 0x08, 0x4c, /* ldr r4, STM32_FLASH_BASE */
598 0x1c, 0x44, /* add r4, r3 */
599 /* write_half_word: */
600 0x01, 0x23, /* movs r3, #0x01 */
601 0x23, 0x61, /* str r3, [r4, #STM32_FLASH_CR_OFFSET] */
602 0x30, 0xf8, 0x02, 0x3b, /* ldrh r3, [r0], #0x02 */
603 0x21, 0xf8, 0x02, 0x3b, /* strh r3, [r1], #0x02 */
605 0xe3, 0x68, /* ldr r3, [r4, #STM32_FLASH_SR_OFFSET] */
606 0x13, 0xf0, 0x01, 0x0f, /* tst r3, #0x01 */
607 0xfb, 0xd0, /* beq busy */
608 0x13, 0xf0, 0x14, 0x0f, /* tst r3, #0x14 */
609 0x01, 0xd1, /* bne exit */
610 0x01, 0x3a, /* subs r2, r2, #0x01 */
611 0xf0, 0xd1, /* bne write_half_word */
613 0x00, 0xbe, /* bkpt #0x00 */
614 0x00, 0x20, 0x02, 0x40, /* STM32_FLASH_BASE: .word 0x40022000 */
617 /* flash write code */
618 if (target_alloc_working_area(target
, sizeof(stm32x_flash_write_code
),
619 &stm32x_info
->write_algorithm
) != ERROR_OK
)
621 LOG_WARNING("no working area available, can't do block memory writes");
622 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
625 if ((retval
= target_write_buffer(target
, stm32x_info
->write_algorithm
->address
,
626 sizeof(stm32x_flash_write_code
),
627 (uint8_t*)stm32x_flash_write_code
)) != ERROR_OK
)
631 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
634 if (buffer_size
<= 256)
636 /* if we already allocated the writing code, but failed to get a
637 * buffer, free the algorithm */
638 if (stm32x_info
->write_algorithm
)
639 target_free_working_area(target
, stm32x_info
->write_algorithm
);
641 LOG_WARNING("no large enough working area available, can't do block memory writes");
642 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
646 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
647 armv7m_info
.core_mode
= ARMV7M_MODE_ANY
;
649 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
650 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
651 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
652 init_reg_param(®_params
[3], "r3", 32, PARAM_IN_OUT
);
656 uint32_t thisrun_count
= (count
> (buffer_size
/ 2)) ?
657 (buffer_size
/ 2) : count
;
659 if ((retval
= target_write_buffer(target
, source
->address
,
660 thisrun_count
* 2, buffer
)) != ERROR_OK
)
663 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
664 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
665 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
);
666 buf_set_u32(reg_params
[3].value
, 0, 32, stm32x_info
->register_offset
);
668 if ((retval
= target_run_algorithm(target
, 0, NULL
, 4, reg_params
,
669 stm32x_info
->write_algorithm
->address
,
671 10000, &armv7m_info
)) != ERROR_OK
)
673 LOG_ERROR("error executing stm32x flash write algorithm");
677 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_PGERR
)
679 LOG_ERROR("flash memory not erased before writing");
680 /* Clear but report errors */
681 target_write_u32(target
, STM32_FLASH_SR
, FLASH_PGERR
);
686 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_WRPRTERR
)
688 LOG_ERROR("flash memory write protected");
689 /* Clear but report errors */
690 target_write_u32(target
, STM32_FLASH_SR
, FLASH_WRPRTERR
);
695 buffer
+= thisrun_count
* 2;
696 address
+= thisrun_count
* 2;
697 count
-= thisrun_count
;
700 target_free_working_area(target
, source
);
701 target_free_working_area(target
, stm32x_info
->write_algorithm
);
703 destroy_reg_param(®_params
[0]);
704 destroy_reg_param(®_params
[1]);
705 destroy_reg_param(®_params
[2]);
706 destroy_reg_param(®_params
[3]);
711 static int stm32x_write(struct flash_bank
*bank
, uint8_t *buffer
,
712 uint32_t offset
, uint32_t count
)
714 struct target
*target
= bank
->target
;
715 uint32_t words_remaining
= (count
/ 2);
716 uint32_t bytes_remaining
= (count
& 0x00000001);
717 uint32_t address
= bank
->base
+ offset
;
718 uint32_t bytes_written
= 0;
721 if (bank
->target
->state
!= TARGET_HALTED
)
723 LOG_ERROR("Target not halted");
724 return ERROR_TARGET_NOT_HALTED
;
729 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
730 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
733 /* unlock flash registers */
734 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_KEYR
), KEY1
);
735 if (retval
!= ERROR_OK
)
737 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_KEYR
), KEY2
);
738 if (retval
!= ERROR_OK
)
741 /* multiple half words (2-byte) to be programmed? */
742 if (words_remaining
> 0)
744 /* try using a block write */
745 if ((retval
= stm32x_write_block(bank
, buffer
, offset
, words_remaining
)) != ERROR_OK
)
747 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
749 /* if block write failed (no sufficient working area),
750 * we use normal (slow) single dword accesses */
751 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
756 buffer
+= words_remaining
* 2;
757 address
+= words_remaining
* 2;
762 if ((retval
!= ERROR_OK
) && (retval
!= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
))
765 while (words_remaining
> 0)
768 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint16_t));
770 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_PG
);
771 if (retval
!= ERROR_OK
)
773 retval
= target_write_u16(target
, address
, value
);
774 if (retval
!= ERROR_OK
)
777 retval
= stm32x_wait_status_busy(bank
, 5);
778 if (retval
!= ERROR_OK
)
788 uint16_t value
= 0xffff;
789 memcpy(&value
, buffer
+ bytes_written
, bytes_remaining
);
791 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_PG
);
792 if (retval
!= ERROR_OK
)
794 retval
= target_write_u16(target
, address
, value
);
795 if (retval
!= ERROR_OK
)
798 retval
= stm32x_wait_status_busy(bank
, 5);
799 if (retval
!= ERROR_OK
)
803 return target_write_u32(target
, STM32_FLASH_CR
, FLASH_LOCK
);
806 static int stm32x_probe(struct flash_bank
*bank
)
808 struct target
*target
= bank
->target
;
809 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
815 stm32x_info
->probed
= 0;
816 stm32x_info
->register_offset
= 0x00;
818 /* read stm32 device id register */
819 int retval
= target_read_u32(target
, 0xE0042000, &device_id
);
820 if (retval
!= ERROR_OK
)
822 LOG_INFO("device id = 0x%08" PRIx32
"", device_id
);
824 /* get flash size from target. */
825 retval
= target_read_u16(target
, 0x1FFFF7E0, &num_pages
);
826 if (retval
!= ERROR_OK
)
828 LOG_WARNING("failed reading flash size, default to max target family");
829 /* failed reading flash size, default to max target family */
833 if ((device_id
& 0x7ff) == 0x410)
835 /* medium density - we have 1k pages
836 * 4 pages for a protection area */
838 stm32x_info
->ppage_size
= 4;
840 /* check for early silicon */
841 if (num_pages
== 0xffff)
843 /* number of sectors incorrect on revA */
844 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
848 else if ((device_id
& 0x7ff) == 0x412)
850 /* low density - we have 1k pages
851 * 4 pages for a protection area */
853 stm32x_info
->ppage_size
= 4;
855 /* check for early silicon */
856 if (num_pages
== 0xffff)
858 /* number of sectors incorrect on revA */
859 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 32k flash");
863 else if ((device_id
& 0x7ff) == 0x414)
865 /* high density - we have 2k pages
866 * 2 pages for a protection area */
868 stm32x_info
->ppage_size
= 2;
870 /* check for early silicon */
871 if (num_pages
== 0xffff)
873 /* number of sectors incorrect on revZ */
874 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 512k flash");
878 else if ((device_id
& 0x7ff) == 0x418)
880 /* connectivity line density - we have 2k pages
881 * 2 pages for a protection area */
883 stm32x_info
->ppage_size
= 2;
885 /* check for early silicon */
886 if (num_pages
== 0xffff)
888 /* number of sectors incorrect on revZ */
889 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash");
893 else if ((device_id
& 0x7ff) == 0x420)
895 /* value line density - we have 1k pages
896 * 4 pages for a protection area */
898 stm32x_info
->ppage_size
= 4;
900 /* check for early silicon */
901 if (num_pages
== 0xffff)
903 /* number of sectors may be incorrrect on early silicon */
904 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
908 else if ((device_id
& 0x7ff) == 0x430)
910 /* xl line density - we have 2k pages
911 * 2 pages for a protection area */
913 stm32x_info
->ppage_size
= 2;
915 /* check for early silicon */
916 if (num_pages
== 0xffff)
918 /* number of sectors may be incorrrect on early silicon */
919 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash");
923 /* split reported size into matching bank */
924 if (bank
->base
!= 0x08080000)
926 /* bank 0 will be fixed 512k */
932 /* bank1 also uses a register offset */
933 stm32x_info
->register_offset
= 0x40;
938 LOG_WARNING("Cannot identify target as a STM32 family.");
942 LOG_INFO("flash size = %dkbytes", num_pages
);
944 /* calculate numbers of pages */
945 num_pages
/= (page_size
/ 1024);
950 bank
->sectors
= NULL
;
953 bank
->size
= (num_pages
* page_size
);
954 bank
->num_sectors
= num_pages
;
955 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
957 for (i
= 0; i
< num_pages
; i
++)
959 bank
->sectors
[i
].offset
= i
* page_size
;
960 bank
->sectors
[i
].size
= page_size
;
961 bank
->sectors
[i
].is_erased
= -1;
962 bank
->sectors
[i
].is_protected
= 1;
965 stm32x_info
->probed
= 1;
970 static int stm32x_auto_probe(struct flash_bank
*bank
)
972 struct stm32x_flash_bank
*stm32x_info
= bank
->driver_priv
;
973 if (stm32x_info
->probed
)
975 return stm32x_probe(bank
);
979 COMMAND_HANDLER(stm32x_handle_part_id_command
)
985 static int get_stm32x_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
987 struct target
*target
= bank
->target
;
991 /* read stm32 device id register */
992 int retval
= target_read_u32(target
, 0xE0042000, &device_id
);
993 if (retval
!= ERROR_OK
)
996 if ((device_id
& 0x7ff) == 0x410)
998 printed
= snprintf(buf
, buf_size
, "stm32x (Medium Density) - Rev: ");
1000 buf_size
-= printed
;
1002 switch (device_id
>> 16)
1005 snprintf(buf
, buf_size
, "A");
1009 snprintf(buf
, buf_size
, "B");
1013 snprintf(buf
, buf_size
, "Z");
1017 snprintf(buf
, buf_size
, "Y");
1021 snprintf(buf
, buf_size
, "unknown");
1025 else if ((device_id
& 0x7ff) == 0x412)
1027 printed
= snprintf(buf
, buf_size
, "stm32x (Low Density) - Rev: ");
1029 buf_size
-= printed
;
1031 switch (device_id
>> 16)
1034 snprintf(buf
, buf_size
, "A");
1038 snprintf(buf
, buf_size
, "unknown");
1042 else if ((device_id
& 0x7ff) == 0x414)
1044 printed
= snprintf(buf
, buf_size
, "stm32x (High Density) - Rev: ");
1046 buf_size
-= printed
;
1048 switch (device_id
>> 16)
1051 snprintf(buf
, buf_size
, "A");
1055 snprintf(buf
, buf_size
, "Z");
1059 snprintf(buf
, buf_size
, "unknown");
1063 else if ((device_id
& 0x7ff) == 0x418)
1065 printed
= snprintf(buf
, buf_size
, "stm32x (Connectivity) - Rev: ");
1067 buf_size
-= printed
;
1069 switch (device_id
>> 16)
1072 snprintf(buf
, buf_size
, "A");
1076 snprintf(buf
, buf_size
, "Z");
1080 snprintf(buf
, buf_size
, "unknown");
1084 else if ((device_id
& 0x7ff) == 0x420)
1086 printed
= snprintf(buf
, buf_size
, "stm32x (Value) - Rev: ");
1088 buf_size
-= printed
;
1090 switch (device_id
>> 16)
1093 snprintf(buf
, buf_size
, "A");
1097 snprintf(buf
, buf_size
, "Z");
1101 snprintf(buf
, buf_size
, "unknown");
1105 else if ((device_id
& 0x7ff) == 0x430)
1107 printed
= snprintf(buf
, buf_size
, "stm32x (XL) - Rev: ");
1109 buf_size
-= printed
;
1111 switch (device_id
>> 16)
1114 snprintf(buf
, buf_size
, "A");
1118 snprintf(buf
, buf_size
, "unknown");
1124 snprintf(buf
, buf_size
, "Cannot identify target as a stm32x\n");
1131 COMMAND_HANDLER(stm32x_handle_lock_command
)
1133 struct target
*target
= NULL
;
1134 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1138 command_print(CMD_CTX
, "stm32x lock <bank>");
1142 struct flash_bank
*bank
;
1143 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1144 if (ERROR_OK
!= retval
)
1147 stm32x_info
= bank
->driver_priv
;
1149 target
= bank
->target
;
1151 if (target
->state
!= TARGET_HALTED
)
1153 LOG_ERROR("Target not halted");
1154 return ERROR_TARGET_NOT_HALTED
;
1157 if (stm32x_erase_options(bank
) != ERROR_OK
)
1159 command_print(CMD_CTX
, "stm32x failed to erase options");
1163 /* set readout protection */
1164 stm32x_info
->option_bytes
.RDP
= 0;
1166 if (stm32x_write_options(bank
) != ERROR_OK
)
1168 command_print(CMD_CTX
, "stm32x failed to lock device");
1172 command_print(CMD_CTX
, "stm32x locked");
1177 COMMAND_HANDLER(stm32x_handle_unlock_command
)
1179 struct target
*target
= NULL
;
1180 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1184 command_print(CMD_CTX
, "stm32x unlock <bank>");
1188 struct flash_bank
*bank
;
1189 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1190 if (ERROR_OK
!= retval
)
1193 stm32x_info
= bank
->driver_priv
;
1195 target
= bank
->target
;
1197 if (target
->state
!= TARGET_HALTED
)
1199 LOG_ERROR("Target not halted");
1200 return ERROR_TARGET_NOT_HALTED
;
1203 if (stm32x_erase_options(bank
) != ERROR_OK
)
1205 command_print(CMD_CTX
, "stm32x failed to unlock device");
1209 if (stm32x_write_options(bank
) != ERROR_OK
)
1211 command_print(CMD_CTX
, "stm32x failed to lock device");
1215 command_print(CMD_CTX
, "stm32x unlocked.\n"
1216 "INFO: a reset or power cycle is required "
1217 "for the new settings to take effect.");
1222 COMMAND_HANDLER(stm32x_handle_options_read_command
)
1224 uint32_t optionbyte
;
1225 struct target
*target
= NULL
;
1226 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1230 command_print(CMD_CTX
, "stm32x options_read <bank>");
1234 struct flash_bank
*bank
;
1235 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1236 if (ERROR_OK
!= retval
)
1239 stm32x_info
= bank
->driver_priv
;
1241 target
= bank
->target
;
1243 if (target
->state
!= TARGET_HALTED
)
1245 LOG_ERROR("Target not halted");
1246 return ERROR_TARGET_NOT_HALTED
;
1249 retval
= target_read_u32(target
, STM32_FLASH_OBR
, &optionbyte
);
1250 if (retval
!= ERROR_OK
)
1252 command_print(CMD_CTX
, "Option Byte: 0x%" PRIx32
"", optionbyte
);
1254 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_ERROR
, 1))
1255 command_print(CMD_CTX
, "Option Byte Complement Error");
1257 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_READOUT
, 1))
1258 command_print(CMD_CTX
, "Readout Protection On");
1260 command_print(CMD_CTX
, "Readout Protection Off");
1262 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDWDGSW
, 1))
1263 command_print(CMD_CTX
, "Software Watchdog");
1265 command_print(CMD_CTX
, "Hardware Watchdog");
1267 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDRSTSTOP
, 1))
1268 command_print(CMD_CTX
, "Stop: No reset generated");
1270 command_print(CMD_CTX
, "Stop: Reset generated");
1272 if (buf_get_u32((uint8_t*)&optionbyte
, OPT_RDRSTSTDBY
, 1))
1273 command_print(CMD_CTX
, "Standby: No reset generated");
1275 command_print(CMD_CTX
, "Standby: Reset generated");
1280 COMMAND_HANDLER(stm32x_handle_options_write_command
)
1282 struct target
*target
= NULL
;
1283 struct stm32x_flash_bank
*stm32x_info
= NULL
;
1284 uint16_t optionbyte
= 0xF8;
1288 command_print(CMD_CTX
, "stm32x options_write <bank> <SWWDG | HWWDG> <RSTSTNDBY | NORSTSTNDBY> <RSTSTOP | NORSTSTOP>");
1292 struct flash_bank
*bank
;
1293 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1294 if (ERROR_OK
!= retval
)
1297 stm32x_info
= bank
->driver_priv
;
1299 target
= bank
->target
;
1301 if (target
->state
!= TARGET_HALTED
)
1303 LOG_ERROR("Target not halted");
1304 return ERROR_TARGET_NOT_HALTED
;
1307 /* REVISIT: ignores some options which we will display...
1308 * and doesn't insist on the specified syntax.
1312 if (strcmp(CMD_ARGV
[1], "SWWDG") == 0)
1314 optionbyte
|= (1 << 0);
1316 else /* REVISIT must be "HWWDG" then ... */
1318 optionbyte
&= ~(1 << 0);
1321 /* OPT_RDRSTSTDBY */
1322 if (strcmp(CMD_ARGV
[2], "NORSTSTNDBY") == 0)
1324 optionbyte
|= (1 << 1);
1326 else /* REVISIT must be "RSTSTNDBY" then ... */
1328 optionbyte
&= ~(1 << 1);
1332 if (strcmp(CMD_ARGV
[3], "NORSTSTOP") == 0)
1334 optionbyte
|= (1 << 2);
1336 else /* REVISIT must be "RSTSTOP" then ... */
1338 optionbyte
&= ~(1 << 2);
1341 if (stm32x_erase_options(bank
) != ERROR_OK
)
1343 command_print(CMD_CTX
, "stm32x failed to erase options");
1347 stm32x_info
->option_bytes
.user_options
= optionbyte
;
1349 if (stm32x_write_options(bank
) != ERROR_OK
)
1351 command_print(CMD_CTX
, "stm32x failed to write options");
1355 command_print(CMD_CTX
, "stm32x write options complete.\n"
1356 "INFO: a reset or power cycle is required "
1357 "for the new settings to take effect.");
1362 static int stm32x_mass_erase(struct flash_bank
*bank
)
1364 struct target
*target
= bank
->target
;
1366 if (target
->state
!= TARGET_HALTED
)
1368 LOG_ERROR("Target not halted");
1369 return ERROR_TARGET_NOT_HALTED
;
1372 /* unlock option flash registers */
1373 int retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_KEYR
), KEY1
);
1374 if (retval
!= ERROR_OK
)
1376 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_KEYR
), KEY2
);
1377 if (retval
!= ERROR_OK
)
1380 /* mass erase flash memory */
1381 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_MER
);
1382 if (retval
!= ERROR_OK
)
1384 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_MER
| FLASH_STRT
);
1385 if (retval
!= ERROR_OK
)
1388 retval
= stm32x_wait_status_busy(bank
, 100);
1389 if (retval
!= ERROR_OK
)
1392 retval
= target_write_u32(target
, stm32x_get_flash_reg(bank
, STM32_FLASH_CR
), FLASH_LOCK
);
1393 if (retval
!= ERROR_OK
)
1399 COMMAND_HANDLER(stm32x_handle_mass_erase_command
)
1405 command_print(CMD_CTX
, "stm32x mass_erase <bank>");
1409 struct flash_bank
*bank
;
1410 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1411 if (ERROR_OK
!= retval
)
1414 retval
= stm32x_mass_erase(bank
);
1415 if (retval
== ERROR_OK
)
1417 /* set all sectors as erased */
1418 for (i
= 0; i
< bank
->num_sectors
; i
++)
1420 bank
->sectors
[i
].is_erased
= 1;
1423 command_print(CMD_CTX
, "stm32x mass erase complete");
1427 command_print(CMD_CTX
, "stm32x mass erase failed");
1433 static const struct command_registration stm32x_exec_command_handlers
[] = {
1436 .handler
= stm32x_handle_lock_command
,
1437 .mode
= COMMAND_EXEC
,
1439 .help
= "Lock entire flash device.",
1443 .handler
= stm32x_handle_unlock_command
,
1444 .mode
= COMMAND_EXEC
,
1446 .help
= "Unlock entire protected flash device.",
1449 .name
= "mass_erase",
1450 .handler
= stm32x_handle_mass_erase_command
,
1451 .mode
= COMMAND_EXEC
,
1453 .help
= "Erase entire flash device.",
1456 .name
= "options_read",
1457 .handler
= stm32x_handle_options_read_command
,
1458 .mode
= COMMAND_EXEC
,
1460 .help
= "Read and display device option byte.",
1463 .name
= "options_write",
1464 .handler
= stm32x_handle_options_write_command
,
1465 .mode
= COMMAND_EXEC
,
1466 .usage
= "bank_id ('SWWDG'|'HWWDG') "
1467 "('RSTSTNDBY'|'NORSTSTNDBY') "
1468 "('RSTSTOP'|'NORSTSTOP')",
1469 .help
= "Replace bits in device option byte.",
1471 COMMAND_REGISTRATION_DONE
1474 static const struct command_registration stm32x_command_handlers
[] = {
1477 .mode
= COMMAND_ANY
,
1478 .help
= "stm32x flash command group",
1479 .chain
= stm32x_exec_command_handlers
,
1481 COMMAND_REGISTRATION_DONE
1484 struct flash_driver stm32x_flash
= {
1486 .commands
= stm32x_command_handlers
,
1487 .flash_bank_command
= stm32x_flash_bank_command
,
1488 .erase
= stm32x_erase
,
1489 .protect
= stm32x_protect
,
1490 .write
= stm32x_write
,
1491 .read
= default_flash_read
,
1492 .probe
= stm32x_probe
,
1493 .auto_probe
= stm32x_auto_probe
,
1494 .erase_check
= default_flash_mem_blank_check
,
1495 .protect_check
= stm32x_protect_check
,
1496 .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)