flash/stm32*: Remove the halted check in protect_check
[openocd.git] / src / flash / nor / stm32f2x.c
blob1922a8c9afaadef910e5083005335083330a46ed
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * Copyright (C) 2011 Øyvind Harboe *
9 * oyvind.harboe@zylin.com *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
25 ***************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 #include "imp.h"
32 #include <helper/binarybuffer.h>
33 #include <target/algorithm.h>
34 #include <target/armv7m.h>
36 /* Regarding performance:
38 * Short story - it might be best to leave the performance at
39 * current levels.
41 * You may see a jump in speed if you change to using
42 * 32bit words for the block programming.
44 * Its a shame you cannot use the double word as its
45 * even faster - but you require external VPP for that mode.
47 * Having said all that 16bit writes give us the widest vdd
48 * operating range, so may be worth adding a note to that effect.
52 /* Danger!!!! The STM32F1x and STM32F2x series actually have
53 * quite different flash controllers.
55 * What's more scary is that the names of the registers and their
56 * addresses are the same, but the actual bits and what they do are
57 * can be very different.
59 * To reduce testing complexity and dangers of regressions,
60 * a seperate file is used for stm32fx2x.
62 * 1mByte part with 4 x 16, 1 x 64, 7 x 128kBytes sectors
64 * What's the protection page size???
66 * Tested with STM3220F-EVAL board.
68 * STM32F21xx series for reference.
70 * RM0033
71 * http://www.st.com/internet/mcu/product/250192.jsp
73 * PM0059
74 * www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/
75 * PROGRAMMING_MANUAL/CD00233952.pdf
77 * STM32F1x series - notice that this code was copy, pasted and knocked
78 * into a stm32f2x driver, so in case something has been converted or
79 * bugs haven't been fixed, here are the original manuals:
81 * RM0008 - Reference manual
83 * RM0042, the Flash programming manual for low-, medium- high-density and
84 * connectivity line STM32F10x devices
86 * PM0068, the Flash programming manual for XL-density STM32F10x devices.
90 /* Erase time can be as high as 1000ms, 10x this and it's toast... */
91 #define FLASH_ERASE_TIMEOUT 10000
92 #define FLASH_WRITE_TIMEOUT 5
94 #define STM32_FLASH_BASE 0x40023c00
95 #define STM32_FLASH_ACR 0x40023c00
96 #define STM32_FLASH_KEYR 0x40023c04
97 #define STM32_FLASH_OPTKEYR 0x40023c08
98 #define STM32_FLASH_SR 0x40023c0C
99 #define STM32_FLASH_CR 0x40023c10
100 #define STM32_FLASH_OPTCR 0x40023c14
101 #define STM32_FLASH_OPTCR1 0x40023c18
103 /* FLASH_CR register bits */
105 #define FLASH_PG (1 << 0)
106 #define FLASH_SER (1 << 1)
107 #define FLASH_MER (1 << 2)
108 #define FLASH_MER1 (1 << 15)
109 #define FLASH_STRT (1 << 16)
110 #define FLASH_PSIZE_8 (0 << 8)
111 #define FLASH_PSIZE_16 (1 << 8)
112 #define FLASH_PSIZE_32 (2 << 8)
113 #define FLASH_PSIZE_64 (3 << 8)
114 #define FLASH_SNB(a) ((a) << 3)
115 #define FLASH_LOCK (1 << 31)
117 /* FLASH_SR register bits */
119 #define FLASH_BSY (1 << 16)
120 #define FLASH_PGSERR (1 << 7) /* Programming sequence error */
121 #define FLASH_PGPERR (1 << 6) /* Programming parallelism error */
122 #define FLASH_PGAERR (1 << 5) /* Programming alignment error */
123 #define FLASH_WRPERR (1 << 4) /* Write protection error */
124 #define FLASH_OPERR (1 << 1) /* Operation error */
126 #define FLASH_ERROR (FLASH_PGSERR | FLASH_PGPERR | FLASH_PGAERR | FLASH_WRPERR | FLASH_OPERR)
128 /* STM32_FLASH_OPTCR register bits */
130 #define OPT_LOCK (1 << 0)
131 #define OPT_START (1 << 1)
133 /* STM32_FLASH_OBR bit definitions (reading) */
135 #define OPT_ERROR 0
136 #define OPT_READOUT 1
137 #define OPT_RDWDGSW 2
138 #define OPT_RDRSTSTOP 3
139 #define OPT_RDRSTSTDBY 4
140 #define OPT_BFB2 5 /* dual flash bank only */
142 /* register unlock keys */
144 #define KEY1 0x45670123
145 #define KEY2 0xCDEF89AB
147 /* option register unlock key */
148 #define OPTKEY1 0x08192A3B
149 #define OPTKEY2 0x4C5D6E7F
151 struct stm32x_options {
152 uint8_t RDP;
153 uint8_t user_options;
154 uint32_t protection;
157 struct stm32x_flash_bank {
158 struct stm32x_options option_bytes;
159 int probed;
160 bool has_large_mem; /* stm32f42x/stm32f43x family */
161 uint32_t user_bank_size;
164 /* flash bank stm32x <base> <size> 0 0 <target#>
166 FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command)
168 struct stm32x_flash_bank *stm32x_info;
170 if (CMD_ARGC < 6)
171 return ERROR_COMMAND_SYNTAX_ERROR;
173 stm32x_info = malloc(sizeof(struct stm32x_flash_bank));
174 bank->driver_priv = stm32x_info;
176 stm32x_info->probed = 0;
177 stm32x_info->user_bank_size = bank->size;
179 return ERROR_OK;
182 static inline int stm32x_get_flash_reg(struct flash_bank *bank, uint32_t reg)
184 return reg;
187 static inline int stm32x_get_flash_status(struct flash_bank *bank, uint32_t *status)
189 struct target *target = bank->target;
190 return target_read_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_SR), status);
193 static int stm32x_wait_status_busy(struct flash_bank *bank, int timeout)
195 struct target *target = bank->target;
196 uint32_t status;
197 int retval = ERROR_OK;
199 /* wait for busy to clear */
200 for (;;) {
201 retval = stm32x_get_flash_status(bank, &status);
202 if (retval != ERROR_OK)
203 return retval;
204 LOG_DEBUG("status: 0x%" PRIx32 "", status);
205 if ((status & FLASH_BSY) == 0)
206 break;
207 if (timeout-- <= 0) {
208 LOG_ERROR("timed out waiting for flash");
209 return ERROR_FAIL;
211 alive_sleep(1);
215 if (status & FLASH_WRPERR) {
216 LOG_ERROR("stm32x device protected");
217 retval = ERROR_FAIL;
220 /* Clear but report errors */
221 if (status & FLASH_ERROR) {
222 /* If this operation fails, we ignore it and report the original
223 * retval
225 target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_SR),
226 status & FLASH_ERROR);
228 return retval;
231 static int stm32x_unlock_reg(struct target *target)
233 uint32_t ctrl;
235 /* first check if not already unlocked
236 * otherwise writing on STM32_FLASH_KEYR will fail
238 int retval = target_read_u32(target, STM32_FLASH_CR, &ctrl);
239 if (retval != ERROR_OK)
240 return retval;
242 if ((ctrl & FLASH_LOCK) == 0)
243 return ERROR_OK;
245 /* unlock flash registers */
246 retval = target_write_u32(target, STM32_FLASH_KEYR, KEY1);
247 if (retval != ERROR_OK)
248 return retval;
250 retval = target_write_u32(target, STM32_FLASH_KEYR, KEY2);
251 if (retval != ERROR_OK)
252 return retval;
254 retval = target_read_u32(target, STM32_FLASH_CR, &ctrl);
255 if (retval != ERROR_OK)
256 return retval;
258 if (ctrl & FLASH_LOCK) {
259 LOG_ERROR("flash not unlocked STM32_FLASH_CR: %x", ctrl);
260 return ERROR_TARGET_FAILURE;
263 return ERROR_OK;
266 static int stm32x_unlock_option_reg(struct target *target)
268 uint32_t ctrl;
270 int retval = target_read_u32(target, STM32_FLASH_OPTCR, &ctrl);
271 if (retval != ERROR_OK)
272 return retval;
274 if ((ctrl & OPT_LOCK) == 0)
275 return ERROR_OK;
277 /* unlock option registers */
278 retval = target_write_u32(target, STM32_FLASH_OPTKEYR, OPTKEY1);
279 if (retval != ERROR_OK)
280 return retval;
282 retval = target_write_u32(target, STM32_FLASH_OPTKEYR, OPTKEY2);
283 if (retval != ERROR_OK)
284 return retval;
286 retval = target_read_u32(target, STM32_FLASH_OPTCR, &ctrl);
287 if (retval != ERROR_OK)
288 return retval;
290 if (ctrl & OPT_LOCK) {
291 LOG_ERROR("options not unlocked STM32_FLASH_OPTCR: %x", ctrl);
292 return ERROR_TARGET_FAILURE;
295 return ERROR_OK;
298 static int stm32x_read_options(struct flash_bank *bank)
300 uint32_t optiondata;
301 struct stm32x_flash_bank *stm32x_info = NULL;
302 struct target *target = bank->target;
304 stm32x_info = bank->driver_priv;
306 /* read current option bytes */
307 int retval = target_read_u32(target, STM32_FLASH_OPTCR, &optiondata);
308 if (retval != ERROR_OK)
309 return retval;
311 stm32x_info->option_bytes.user_options = optiondata & 0xec;
312 stm32x_info->option_bytes.RDP = (optiondata >> 8) & 0xff;
313 stm32x_info->option_bytes.protection = (optiondata >> 16) & 0xfff;
315 if (stm32x_info->has_large_mem) {
317 retval = target_read_u32(target, STM32_FLASH_OPTCR1, &optiondata);
318 if (retval != ERROR_OK)
319 return retval;
321 /* append protection bits */
322 stm32x_info->option_bytes.protection |= (optiondata >> 4) & 0x00fff000;
325 if (stm32x_info->option_bytes.RDP != 0xAA)
326 LOG_INFO("Device Security Bit Set");
328 return ERROR_OK;
331 static int stm32x_write_options(struct flash_bank *bank)
333 struct stm32x_flash_bank *stm32x_info = NULL;
334 struct target *target = bank->target;
335 uint32_t optiondata;
337 stm32x_info = bank->driver_priv;
339 int retval = stm32x_unlock_option_reg(target);
340 if (retval != ERROR_OK)
341 return retval;
343 /* rebuild option data */
344 optiondata = stm32x_info->option_bytes.user_options;
345 buf_set_u32(&optiondata, 8, 8, stm32x_info->option_bytes.RDP);
346 buf_set_u32(&optiondata, 16, 12, stm32x_info->option_bytes.protection);
348 /* program options */
349 retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata);
350 if (retval != ERROR_OK)
351 return retval;
353 if (stm32x_info->has_large_mem) {
355 uint32_t optiondata2 = 0;
356 buf_set_u32(&optiondata2, 16, 12, stm32x_info->option_bytes.protection >> 12);
357 retval = target_write_u32(target, STM32_FLASH_OPTCR1, optiondata2);
358 if (retval != ERROR_OK)
359 return retval;
362 /* start programming cycle */
363 retval = target_write_u32(target, STM32_FLASH_OPTCR, optiondata | OPT_START);
364 if (retval != ERROR_OK)
365 return retval;
367 /* wait for completion */
368 retval = stm32x_wait_status_busy(bank, FLASH_ERASE_TIMEOUT);
369 if (retval != ERROR_OK)
370 return retval;
372 /* relock registers */
373 retval = target_write_u32(target, STM32_FLASH_OPTCR, OPT_LOCK);
374 if (retval != ERROR_OK)
375 return retval;
377 return ERROR_OK;
380 static int stm32x_protect_check(struct flash_bank *bank)
382 struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
384 /* read write protection settings */
385 int retval = stm32x_read_options(bank);
386 if (retval != ERROR_OK) {
387 LOG_DEBUG("unable to read option bytes");
388 return retval;
391 for (int i = 0; i < bank->num_sectors; i++) {
392 if (stm32x_info->option_bytes.protection & (1 << i))
393 bank->sectors[i].is_protected = 0;
394 else
395 bank->sectors[i].is_protected = 1;
398 return ERROR_OK;
401 static int stm32x_erase(struct flash_bank *bank, int first, int last)
403 struct target *target = bank->target;
404 int i;
406 if (bank->target->state != TARGET_HALTED) {
407 LOG_ERROR("Target not halted");
408 return ERROR_TARGET_NOT_HALTED;
411 int retval;
412 retval = stm32x_unlock_reg(target);
413 if (retval != ERROR_OK)
414 return retval;
417 Sector Erase
418 To erase a sector, follow the procedure below:
419 1. Check that no Flash memory operation is ongoing by checking the BSY bit in the
420 FLASH_SR register
421 2. Set the SER bit and select the sector (out of the 12 sectors in the main memory block)
422 you wish to erase (SNB) in the FLASH_CR register
423 3. Set the STRT bit in the FLASH_CR register
424 4. Wait for the BSY bit to be cleared
427 for (i = first; i <= last; i++) {
428 retval = target_write_u32(target,
429 stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_SER | FLASH_SNB(i) | FLASH_STRT);
430 if (retval != ERROR_OK)
431 return retval;
433 retval = stm32x_wait_status_busy(bank, FLASH_ERASE_TIMEOUT);
434 if (retval != ERROR_OK)
435 return retval;
437 bank->sectors[i].is_erased = 1;
440 retval = target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_LOCK);
441 if (retval != ERROR_OK)
442 return retval;
444 return ERROR_OK;
447 static int stm32x_protect(struct flash_bank *bank, int set, int first, int last)
449 struct target *target = bank->target;
450 struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
452 if (target->state != TARGET_HALTED) {
453 LOG_ERROR("Target not halted");
454 return ERROR_TARGET_NOT_HALTED;
457 /* read protection settings */
458 int retval = stm32x_read_options(bank);
459 if (retval != ERROR_OK) {
460 LOG_DEBUG("unable to read option bytes");
461 return retval;
464 for (int i = first; i <= last; i++) {
466 if (set)
467 stm32x_info->option_bytes.protection &= ~(1 << i);
468 else
469 stm32x_info->option_bytes.protection |= (1 << i);
472 retval = stm32x_write_options(bank);
473 if (retval != ERROR_OK)
474 return retval;
476 return ERROR_OK;
479 static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer,
480 uint32_t offset, uint32_t count)
482 struct target *target = bank->target;
483 uint32_t buffer_size = 16384;
484 struct working_area *write_algorithm;
485 struct working_area *source;
486 uint32_t address = bank->base + offset;
487 struct reg_param reg_params[5];
488 struct armv7m_algorithm armv7m_info;
489 int retval = ERROR_OK;
491 /* see contrib/loaders/flash/stm32f2x.S for src */
493 static const uint8_t stm32x_flash_write_code[] = {
494 /* wait_fifo: */
495 0xD0, 0xF8, 0x00, 0x80, /* ldr r8, [r0, #0] */
496 0xB8, 0xF1, 0x00, 0x0F, /* cmp r8, #0 */
497 0x1A, 0xD0, /* beq exit */
498 0x47, 0x68, /* ldr r7, [r0, #4] */
499 0x47, 0x45, /* cmp r7, r8 */
500 0xF7, 0xD0, /* beq wait_fifo */
502 0xDF, 0xF8, 0x30, 0x60, /* ldr r6, STM32_PROG16 */
503 0x26, 0x61, /* str r6, [r4, #STM32_FLASH_CR_OFFSET] */
504 0x37, 0xF8, 0x02, 0x6B, /* ldrh r6, [r7], #0x02 */
505 0x22, 0xF8, 0x02, 0x6B, /* strh r6, [r2], #0x02 */
506 /* busy: */
507 0xE6, 0x68, /* ldr r6, [r4, #STM32_FLASH_SR_OFFSET] */
508 0x16, 0xF4, 0x80, 0x3F, /* tst r6, #0x10000 */
509 0xFB, 0xD1, /* bne busy */
510 0x16, 0xF0, 0xF0, 0x0F, /* tst r6, #0xf0 */
511 0x07, 0xD1, /* bne error */
513 0x8F, 0x42, /* cmp r7, r1 */
514 0x28, 0xBF, /* it cs */
515 0x00, 0xF1, 0x08, 0x07, /* addcs r7, r0, #8 */
516 0x47, 0x60, /* str r7, [r0, #4] */
517 0x01, 0x3B, /* subs r3, r3, #1 */
518 0x13, 0xB1, /* cbz r3, exit */
519 0xE1, 0xE7, /* b wait_fifo */
520 /* error: */
521 0x00, 0x21, /* movs r1, #0 */
522 0x41, 0x60, /* str r1, [r0, #4] */
523 /* exit: */
524 0x30, 0x46, /* mov r0, r6 */
525 0x00, 0xBE, /* bkpt #0x00 */
527 /* <STM32_PROG16>: */
528 0x01, 0x01, 0x00, 0x00, /* .word 0x00000101 */
531 if (target_alloc_working_area(target, sizeof(stm32x_flash_write_code),
532 &write_algorithm) != ERROR_OK) {
533 LOG_WARNING("no working area available, can't do block memory writes");
534 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
537 retval = target_write_buffer(target, write_algorithm->address,
538 sizeof(stm32x_flash_write_code),
539 (uint8_t *)stm32x_flash_write_code);
540 if (retval != ERROR_OK)
541 return retval;
543 /* memory buffer */
544 while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) {
545 buffer_size /= 2;
546 if (buffer_size <= 256) {
547 /* we already allocated the writing code, but failed to get a
548 * buffer, free the algorithm */
549 target_free_working_area(target, write_algorithm);
551 LOG_WARNING("no large enough working area available, can't do block memory writes");
552 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
556 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
557 armv7m_info.core_mode = ARM_MODE_THREAD;
559 init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); /* buffer start, status (out) */
560 init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); /* buffer end */
561 init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT); /* target address */
562 init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT); /* count (halfword-16bit) */
563 init_reg_param(&reg_params[4], "r4", 32, PARAM_OUT); /* flash base */
565 buf_set_u32(reg_params[0].value, 0, 32, source->address);
566 buf_set_u32(reg_params[1].value, 0, 32, source->address + source->size);
567 buf_set_u32(reg_params[2].value, 0, 32, address);
568 buf_set_u32(reg_params[3].value, 0, 32, count);
569 buf_set_u32(reg_params[4].value, 0, 32, STM32_FLASH_BASE);
571 retval = target_run_flash_async_algorithm(target, buffer, count, 2,
572 0, NULL,
573 5, reg_params,
574 source->address, source->size,
575 write_algorithm->address, 0,
576 &armv7m_info);
578 if (retval == ERROR_FLASH_OPERATION_FAILED) {
579 LOG_ERROR("error executing stm32x flash write algorithm");
581 uint32_t error = buf_get_u32(reg_params[0].value, 0, 32) & FLASH_ERROR;
583 if (error & FLASH_WRPERR)
584 LOG_ERROR("flash memory write protected");
586 if (error != 0) {
587 LOG_ERROR("flash write failed = %08x", error);
588 /* Clear but report errors */
589 target_write_u32(target, STM32_FLASH_SR, error);
590 retval = ERROR_FAIL;
594 target_free_working_area(target, source);
595 target_free_working_area(target, write_algorithm);
597 destroy_reg_param(&reg_params[0]);
598 destroy_reg_param(&reg_params[1]);
599 destroy_reg_param(&reg_params[2]);
600 destroy_reg_param(&reg_params[3]);
601 destroy_reg_param(&reg_params[4]);
603 return retval;
606 static int stm32x_write(struct flash_bank *bank, uint8_t *buffer,
607 uint32_t offset, uint32_t count)
609 struct target *target = bank->target;
610 uint32_t words_remaining = (count / 2);
611 uint32_t bytes_remaining = (count & 0x00000001);
612 uint32_t address = bank->base + offset;
613 uint32_t bytes_written = 0;
614 int retval;
616 if (bank->target->state != TARGET_HALTED) {
617 LOG_ERROR("Target not halted");
618 return ERROR_TARGET_NOT_HALTED;
621 if (offset & 0x1) {
622 LOG_WARNING("offset 0x%" PRIx32 " breaks required 2-byte alignment", offset);
623 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
626 retval = stm32x_unlock_reg(target);
627 if (retval != ERROR_OK)
628 return retval;
630 /* multiple half words (2-byte) to be programmed? */
631 if (words_remaining > 0) {
632 /* try using a block write */
633 retval = stm32x_write_block(bank, buffer, offset, words_remaining);
634 if (retval != ERROR_OK) {
635 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
636 /* if block write failed (no sufficient working area),
637 * we use normal (slow) single dword accesses */
638 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
640 } else {
641 buffer += words_remaining * 2;
642 address += words_remaining * 2;
643 words_remaining = 0;
647 if ((retval != ERROR_OK) && (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE))
648 return retval;
651 Standard programming
652 The Flash memory programming sequence is as follows:
653 1. Check that no main Flash memory operation is ongoing by checking the BSY bit in the
654 FLASH_SR register.
655 2. Set the PG bit in the FLASH_CR register
656 3. Perform the data write operation(s) to the desired memory address (inside main
657 memory block or OTP area):
658 – – Half-word access in case of x16 parallelism
659 – Word access in case of x32 parallelism
660 –
662 Byte access in case of x8 parallelism
663 Double word access in case of x64 parallelism
664 Wait for the BSY bit to be cleared
666 while (words_remaining > 0) {
667 uint16_t value;
668 memcpy(&value, buffer + bytes_written, sizeof(uint16_t));
670 retval = target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR),
671 FLASH_PG | FLASH_PSIZE_16);
672 if (retval != ERROR_OK)
673 return retval;
675 retval = target_write_u16(target, address, value);
676 if (retval != ERROR_OK)
677 return retval;
679 retval = stm32x_wait_status_busy(bank, FLASH_WRITE_TIMEOUT);
680 if (retval != ERROR_OK)
681 return retval;
683 bytes_written += 2;
684 words_remaining--;
685 address += 2;
688 if (bytes_remaining) {
689 retval = target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR),
690 FLASH_PG | FLASH_PSIZE_8);
691 if (retval != ERROR_OK)
692 return retval;
693 retval = target_write_u8(target, address, buffer[bytes_written]);
694 if (retval != ERROR_OK)
695 return retval;
697 retval = stm32x_wait_status_busy(bank, FLASH_WRITE_TIMEOUT);
698 if (retval != ERROR_OK)
699 return retval;
702 return target_write_u32(target, STM32_FLASH_CR, FLASH_LOCK);
705 static void setup_sector(struct flash_bank *bank, int start, int num, int size)
707 for (int i = start; i < (start + num) ; i++) {
708 assert(i < bank->num_sectors);
709 bank->sectors[i].offset = bank->size;
710 bank->sectors[i].size = size;
711 bank->size += bank->sectors[i].size;
715 static int stm32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
717 /* this checks for a stm32f4x errata issue where a
718 * stm32f2x DBGMCU_IDCODE is incorrectly returned.
719 * If the issue is detected target is forced to stm32f4x Rev A.
720 * Only effects Rev A silicon */
722 struct target *target = bank->target;
723 uint32_t cpuid;
725 /* read stm32 device id register */
726 int retval = target_read_u32(target, 0xE0042000, device_id);
727 if (retval != ERROR_OK)
728 return retval;
730 if ((*device_id & 0xfff) == 0x411) {
731 /* read CPUID reg to check core type */
732 retval = target_read_u32(target, 0xE000ED00, &cpuid);
733 if (retval != ERROR_OK)
734 return retval;
736 /* check for cortex_m4 */
737 if (((cpuid >> 4) & 0xFFF) == 0xC24) {
738 *device_id &= ~((0xFFFF << 16) | 0xfff);
739 *device_id |= (0x1000 << 16) | 0x413;
740 LOG_INFO("stm32f4x errata detected - fixing incorrect MCU_IDCODE");
743 return retval;
746 static int stm32x_probe(struct flash_bank *bank)
748 struct target *target = bank->target;
749 struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
750 int i;
751 uint16_t flash_size_in_kb;
752 uint16_t max_flash_size_in_kb;
753 uint32_t device_id;
754 uint32_t base_address = 0x08000000;
756 stm32x_info->probed = 0;
757 stm32x_info->has_large_mem = false;
759 /* read stm32 device id register */
760 int retval = stm32x_get_device_id(bank, &device_id);
761 if (retval != ERROR_OK)
762 return retval;
763 LOG_INFO("device id = 0x%08" PRIx32 "", device_id);
765 /* set max flash size depending on family */
766 switch (device_id & 0xfff) {
767 case 0x411:
768 case 0x413:
769 max_flash_size_in_kb = 1024;
770 break;
771 case 0x419:
772 max_flash_size_in_kb = 2048;
773 stm32x_info->has_large_mem = true;
774 break;
775 default:
776 LOG_WARNING("Cannot identify target as a STM32 family.");
777 return ERROR_FAIL;
780 /* get flash size from target. */
781 retval = target_read_u16(target, 0x1FFF7A22, &flash_size_in_kb);
783 /* failed reading flash size or flash size invalid (early silicon),
784 * default to max target family */
785 if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
786 LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %dk flash",
787 max_flash_size_in_kb);
788 flash_size_in_kb = max_flash_size_in_kb;
791 /* if the user sets the size manually then ignore the probed value
792 * this allows us to work around devices that have a invalid flash size register value */
793 if (stm32x_info->user_bank_size) {
794 LOG_INFO("ignoring flash probed value, using configured bank size");
795 flash_size_in_kb = stm32x_info->user_bank_size / 1024;
798 LOG_INFO("flash size = %dkbytes", flash_size_in_kb);
800 /* did we assign flash size? */
801 assert(flash_size_in_kb != 0xffff);
803 /* calculate numbers of pages */
804 int num_pages = (flash_size_in_kb / 128) + 4;
806 /* check for larger 2048 bytes devices */
807 if (stm32x_info->has_large_mem)
808 num_pages += 4;
810 /* check that calculation result makes sense */
811 assert(num_pages > 0);
813 if (bank->sectors) {
814 free(bank->sectors);
815 bank->sectors = NULL;
818 bank->base = base_address;
819 bank->num_sectors = num_pages;
820 bank->sectors = malloc(sizeof(struct flash_sector) * num_pages);
821 bank->size = 0;
823 /* fixed memory */
824 setup_sector(bank, 0, 4, 16 * 1024);
825 setup_sector(bank, 4, 1, 64 * 1024);
827 /* dynamic memory */
828 setup_sector(bank, 4 + 1, MIN(12, num_pages) - 5, 128 * 1024);
830 if (stm32x_info->has_large_mem) {
832 /* fixed memory for larger devices */
833 setup_sector(bank, 12, 4, 16 * 1024);
834 setup_sector(bank, 16, 1, 64 * 1024);
836 /* dynamic memory for larger devices */
837 setup_sector(bank, 16 + 1, num_pages - 5 - 12, 128 * 1024);
840 for (i = 0; i < num_pages; i++) {
841 bank->sectors[i].is_erased = -1;
842 bank->sectors[i].is_protected = 0;
845 stm32x_info->probed = 1;
847 return ERROR_OK;
850 static int stm32x_auto_probe(struct flash_bank *bank)
852 struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
853 if (stm32x_info->probed)
854 return ERROR_OK;
855 return stm32x_probe(bank);
858 static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
860 uint32_t device_id;
861 int printed;
863 /* read stm32 device id register */
864 int retval = stm32x_get_device_id(bank, &device_id);
865 if (retval != ERROR_OK)
866 return retval;
868 if ((device_id & 0xfff) == 0x411) {
869 printed = snprintf(buf, buf_size, "stm32f2x - Rev: ");
870 buf += printed;
871 buf_size -= printed;
873 switch (device_id >> 16) {
874 case 0x1000:
875 snprintf(buf, buf_size, "A");
876 break;
878 case 0x2000:
879 snprintf(buf, buf_size, "B");
880 break;
882 case 0x1001:
883 snprintf(buf, buf_size, "Z");
884 break;
886 case 0x2001:
887 snprintf(buf, buf_size, "Y");
888 break;
890 case 0x2003:
891 snprintf(buf, buf_size, "X");
892 break;
894 default:
895 snprintf(buf, buf_size, "unknown");
896 break;
898 } else if (((device_id & 0xfff) == 0x413) ||
899 ((device_id & 0xfff) == 0x419)) {
900 printed = snprintf(buf, buf_size, "stm32f4x - Rev: ");
901 buf += printed;
902 buf_size -= printed;
904 switch (device_id >> 16) {
905 case 0x1000:
906 snprintf(buf, buf_size, "A");
907 break;
909 case 0x1001:
910 snprintf(buf, buf_size, "Z");
911 break;
913 default:
914 snprintf(buf, buf_size, "unknown");
915 break;
917 } else {
918 snprintf(buf, buf_size, "Cannot identify target as a stm32x\n");
919 return ERROR_FAIL;
922 return ERROR_OK;
925 COMMAND_HANDLER(stm32x_handle_lock_command)
927 struct target *target = NULL;
928 struct stm32x_flash_bank *stm32x_info = NULL;
930 if (CMD_ARGC < 1)
931 return ERROR_COMMAND_SYNTAX_ERROR;
933 struct flash_bank *bank;
934 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
935 if (ERROR_OK != retval)
936 return retval;
938 stm32x_info = bank->driver_priv;
939 target = bank->target;
941 if (target->state != TARGET_HALTED) {
942 LOG_ERROR("Target not halted");
943 return ERROR_TARGET_NOT_HALTED;
946 if (stm32x_read_options(bank) != ERROR_OK) {
947 command_print(CMD_CTX, "%s failed to read options", bank->driver->name);
948 return ERROR_OK;
951 /* set readout protection */
952 stm32x_info->option_bytes.RDP = 0;
954 if (stm32x_write_options(bank) != ERROR_OK) {
955 command_print(CMD_CTX, "%s failed to lock device", bank->driver->name);
956 return ERROR_OK;
959 command_print(CMD_CTX, "%s locked", bank->driver->name);
961 return ERROR_OK;
964 COMMAND_HANDLER(stm32x_handle_unlock_command)
966 struct target *target = NULL;
967 struct stm32x_flash_bank *stm32x_info = NULL;
969 if (CMD_ARGC < 1)
970 return ERROR_COMMAND_SYNTAX_ERROR;
972 struct flash_bank *bank;
973 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
974 if (ERROR_OK != retval)
975 return retval;
977 stm32x_info = bank->driver_priv;
978 target = bank->target;
980 if (target->state != TARGET_HALTED) {
981 LOG_ERROR("Target not halted");
982 return ERROR_TARGET_NOT_HALTED;
985 if (stm32x_read_options(bank) != ERROR_OK) {
986 command_print(CMD_CTX, "%s failed to read options", bank->driver->name);
987 return ERROR_OK;
990 /* clear readout protection and complementary option bytes
991 * this will also force a device unlock if set */
992 stm32x_info->option_bytes.RDP = 0xAA;
994 if (stm32x_write_options(bank) != ERROR_OK) {
995 command_print(CMD_CTX, "%s failed to unlock device", bank->driver->name);
996 return ERROR_OK;
999 command_print(CMD_CTX, "%s unlocked.\n"
1000 "INFO: a reset or power cycle is required "
1001 "for the new settings to take effect.", bank->driver->name);
1003 return ERROR_OK;
1006 static int stm32x_mass_erase(struct flash_bank *bank)
1008 int retval;
1009 struct target *target = bank->target;
1010 struct stm32x_flash_bank *stm32x_info = NULL;
1012 if (target->state != TARGET_HALTED) {
1013 LOG_ERROR("Target not halted");
1014 return ERROR_TARGET_NOT_HALTED;
1017 stm32x_info = bank->driver_priv;
1019 retval = stm32x_unlock_reg(target);
1020 if (retval != ERROR_OK)
1021 return retval;
1023 /* mass erase flash memory */
1024 if (stm32x_info->has_large_mem)
1025 retval = target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_MER | FLASH_MER1);
1026 else
1027 retval = target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_MER);
1028 if (retval != ERROR_OK)
1029 return retval;
1030 retval = target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR),
1031 FLASH_MER | FLASH_STRT);
1032 if (retval != ERROR_OK)
1033 return retval;
1035 retval = stm32x_wait_status_busy(bank, 30000);
1036 if (retval != ERROR_OK)
1037 return retval;
1039 retval = target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_LOCK);
1040 if (retval != ERROR_OK)
1041 return retval;
1043 return ERROR_OK;
1046 COMMAND_HANDLER(stm32x_handle_mass_erase_command)
1048 int i;
1050 if (CMD_ARGC < 1) {
1051 command_print(CMD_CTX, "stm32x mass_erase <bank>");
1052 return ERROR_COMMAND_SYNTAX_ERROR;
1055 struct flash_bank *bank;
1056 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1057 if (ERROR_OK != retval)
1058 return retval;
1060 retval = stm32x_mass_erase(bank);
1061 if (retval == ERROR_OK) {
1062 /* set all sectors as erased */
1063 for (i = 0; i < bank->num_sectors; i++)
1064 bank->sectors[i].is_erased = 1;
1066 command_print(CMD_CTX, "stm32x mass erase complete");
1067 } else {
1068 command_print(CMD_CTX, "stm32x mass erase failed");
1071 return retval;
1074 static const struct command_registration stm32x_exec_command_handlers[] = {
1076 .name = "lock",
1077 .handler = stm32x_handle_lock_command,
1078 .mode = COMMAND_EXEC,
1079 .usage = "bank_id",
1080 .help = "Lock entire flash device.",
1083 .name = "unlock",
1084 .handler = stm32x_handle_unlock_command,
1085 .mode = COMMAND_EXEC,
1086 .usage = "bank_id",
1087 .help = "Unlock entire protected flash device.",
1090 .name = "mass_erase",
1091 .handler = stm32x_handle_mass_erase_command,
1092 .mode = COMMAND_EXEC,
1093 .usage = "bank_id",
1094 .help = "Erase entire flash device.",
1096 COMMAND_REGISTRATION_DONE
1099 static const struct command_registration stm32x_command_handlers[] = {
1101 .name = "stm32f2x",
1102 .mode = COMMAND_ANY,
1103 .help = "stm32f2x flash command group",
1104 .usage = "",
1105 .chain = stm32x_exec_command_handlers,
1107 COMMAND_REGISTRATION_DONE
1110 struct flash_driver stm32f2x_flash = {
1111 .name = "stm32f2x",
1112 .commands = stm32x_command_handlers,
1113 .flash_bank_command = stm32x_flash_bank_command,
1114 .erase = stm32x_erase,
1115 .protect = stm32x_protect,
1116 .write = stm32x_write,
1117 .read = default_flash_read,
1118 .probe = stm32x_probe,
1119 .auto_probe = stm32x_auto_probe,
1120 .erase_check = default_flash_blank_check,
1121 .protect_check = stm32x_protect_check,
1122 .info = get_stm32x_info,