flash/stm32f2x: add stm32f7 revision Z identification
[openocd.git] / src / flash / nor / stm32f2x.c
index 0e4abb533c6ff672f50a20d752e64be57c957120..b46fb07869f0fba8610e771bfba4a5774a730fc9 100644 (file)
 #define FLASH_PSIZE_16 (1 << 8)
 #define FLASH_PSIZE_32 (2 << 8)
 #define FLASH_PSIZE_64 (3 << 8)
-/* The sector number encoding is not straight binary for dual bank flash.
- * Warning: evaluates the argument multiple times */
-#define FLASH_SNB(a)   ((((a) >= 12) ? 0x10 | ((a) - 12) : (a)) << 3)
+/* The sector number encoding is not straight binary for dual bank flash. */
+#define FLASH_SNB(a)   ((a) << 3)
 #define FLASH_LOCK     (1 << 31)
 
 /* FLASH_SR register bits */
@@ -253,6 +252,8 @@ static int stm32x_wait_status_busy(struct flash_bank *bank, int timeout)
 
        /* Clear but report errors */
        if (status & FLASH_ERROR) {
+               if (retval == ERROR_OK)
+                       retval = ERROR_FAIL;
                /* If this operation fails, we ignore it and report the original
                 * retval
                 */
@@ -489,6 +490,7 @@ static int stm32x_protect_check(struct flash_bank *bank)
 
 static int stm32x_erase(struct flash_bank *bank, int first, int last)
 {
+       struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
        struct target *target = bank->target;
        int i;
 
@@ -516,8 +518,14 @@ static int stm32x_erase(struct flash_bank *bank, int first, int last)
         */
 
        for (i = first; i <= last; i++) {
+               int snb;
+               if (stm32x_info->has_large_mem && i >= 12)
+                       snb = (i - 12) | 0x10;
+               else
+                       snb = i;
+
                retval = target_write_u32(target,
-                               stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_SER | FLASH_SNB(i) | FLASH_STRT);
+                               stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_SER | FLASH_SNB(snb) | FLASH_STRT);
                if (retval != ERROR_OK)
                        return retval;
 
@@ -578,45 +586,8 @@ static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer,
        struct armv7m_algorithm armv7m_info;
        int retval = ERROR_OK;
 
-       /* see contrib/loaders/flash/stm32f2x.S for src */
-
        static const uint8_t stm32x_flash_write_code[] = {
-                                                                       /* wait_fifo: */
-               0xD0, 0xF8, 0x00, 0x80,         /* ldr          r8, [r0, #0] */
-               0xB8, 0xF1, 0x00, 0x0F,         /* cmp          r8, #0 */
-               0x1A, 0xD0,                                     /* beq          exit */
-               0x47, 0x68,                                     /* ldr          r7, [r0, #4] */
-               0x47, 0x45,                                     /* cmp          r7, r8 */
-               0xF7, 0xD0,                                     /* beq          wait_fifo */
-
-               0xDF, 0xF8, 0x34, 0x60,         /* ldr          r6, STM32_PROG16 */
-               0x26, 0x61,                                     /* str          r6, [r4, #STM32_FLASH_CR_OFFSET] */
-               0x37, 0xF8, 0x02, 0x6B,         /* ldrh         r6, [r7], #0x02 */
-               0x22, 0xF8, 0x02, 0x6B,         /* strh         r6, [r2], #0x02 */
-               0xBF, 0xF3, 0x4F, 0x8F,         /* dsb          sy */
-                                                                       /* busy: */
-               0xE6, 0x68,                                     /* ldr          r6, [r4, #STM32_FLASH_SR_OFFSET] */
-               0x16, 0xF4, 0x80, 0x3F,         /* tst          r6, #0x10000 */
-               0xFB, 0xD1,                                     /* bne          busy */
-               0x16, 0xF0, 0xF0, 0x0F,         /* tst          r6, #0xf0 */
-               0x07, 0xD1,                                     /* bne          error */
-
-               0x8F, 0x42,                                     /* cmp          r7, r1 */
-               0x28, 0xBF,                                     /* it           cs */
-               0x00, 0xF1, 0x08, 0x07,         /* addcs        r7, r0, #8 */
-               0x47, 0x60,                                     /* str          r7, [r0, #4] */
-               0x01, 0x3B,                                     /* subs         r3, r3, #1 */
-               0x13, 0xB1,                                     /* cbz          r3, exit */
-               0xDF, 0xE7,                                     /* b            wait_fifo */
-                                                                       /* error: */
-               0x00, 0x21,                                     /* movs         r1, #0 */
-               0x41, 0x60,                                     /* str          r1, [r0, #4] */
-                                                                       /* exit: */
-               0x30, 0x46,                                     /* mov          r0, r6 */
-               0x00, 0xBE,                                     /* bkpt         #0x00 */
-
-               /* <STM32_PROG16>: */
-               0x01, 0x01, 0x00, 0x00,         /* .word        0x00000101 */
+#include "../../../contrib/loaders/flash/stm32/stm32f2x.inc"
        };
 
        if (target_alloc_working_area(target, sizeof(stm32x_flash_write_code),
@@ -628,8 +599,10 @@ static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer,
        retval = target_write_buffer(target, write_algorithm->address,
                        sizeof(stm32x_flash_write_code),
                        stm32x_flash_write_code);
-       if (retval != ERROR_OK)
+       if (retval != ERROR_OK) {
+               target_free_working_area(target, write_algorithm);
                return retval;
+       }
 
        /* memory buffer */
        while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) {
@@ -1047,7 +1020,8 @@ static int stm32x_probe(struct flash_bank *bank)
                if (device_id == 0x451) {
                        for (i = 0; i < num_prot_blocks; i++) {
                                bank->prot_blocks[i].offset = bank->sectors[i << 1].offset;
-                               bank->prot_blocks[i].size = bank->sectors[i << 1].size << 1;
+                               bank->prot_blocks[i].size = bank->sectors[i << 1].size
+                                               + bank->sectors[(i << 1) + 1].size;
                        }
                }
        } else {
@@ -1216,6 +1190,9 @@ static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
                case 0x1000:
                        rev_str = "A";
                        break;
+               case 0x1001:
+                       rev_str = "Z";
+                       break;
                }
                break;
 
@@ -1321,7 +1298,7 @@ COMMAND_HANDLER(stm32x_handle_unlock_command)
         * this will also force a device unlock if set */
        stm32x_info->option_bytes.RDP = 0xAA;
        if (stm32x_info->has_optcr2_pcrop) {
-               stm32x_info->option_bytes.optcr2_pcrop = OPTCR2_PCROP_RDP | (~1 << bank->num_sectors);
+               stm32x_info->option_bytes.optcr2_pcrop = OPTCR2_PCROP_RDP | (~1U << bank->num_sectors);
        }
 
        if (stm32x_write_options(bank) != ERROR_OK) {
@@ -1627,4 +1604,5 @@ struct flash_driver stm32f2x_flash = {
        .erase_check = default_flash_blank_check,
        .protect_check = stm32x_protect_check,
        .info = get_stm32x_info,
+       .free_driver_priv = default_flash_free_driver_priv,
 };

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)