flash/nor/stm32h7x: remove stm32x_options.protection2
[openocd.git] / src / flash / nor / stm32h7x.c
index 90bc8f393d207817f3c4f824e7b66dc741cca889..1d128f3be1841b0511eb7b5ba6992d6501e7d25b 100644 (file)
 #define FLASH_SR        0x10
 #define FLASH_CCR       0x14
 #define FLASH_OPTCR     0x18
-#define FLASH_OPTCUR    0x1C
-#define FLASH_OPTPRG    0x20
+#define FLASH_OPTSR_CUR 0x1C
+#define FLASH_OPTSR_PRG 0x20
 #define FLASH_OPTCCR    0x24
-#define FLASH_WPSNCUR   0x38
-#define FLASH_WPSNPRG   0x3C
+#define FLASH_WPSN_CUR  0x38
+#define FLASH_WPSN_PRG  0x3C
 
 
 /* FLASH_CR register bits */
 #define FLASH_LOCK     (1 << 0)
 #define FLASH_PG       (1 << 1)
 #define FLASH_SER      (1 << 2)
-#define FLASH_BER_CMD  (1 << 3)
+#define FLASH_BER      (1 << 3)
 #define FLASH_PSIZE_8  (0 << 4)
 #define FLASH_PSIZE_16 (1 << 4)
 #define FLASH_PSIZE_32 (2 << 4)
@@ -102,8 +102,7 @@ struct stm32h7x_rev {
 
 struct stm32x_options {
        uint8_t RDP;
-       uint32_t protection;  /* bank1 WRP */
-       uint32_t protection2; /* bank2 WRP */
+       uint32_t protection;  /* bank sectors's write protection (WPSN register) */
        uint8_t user_options;
        uint8_t user2_options;
        uint8_t user3_options;
@@ -115,7 +114,6 @@ struct stm32h7x_part_info {
        const struct stm32h7x_rev *revs;
        size_t num_revs;
        unsigned int page_size;
-       unsigned int pages_per_sector;
        uint16_t max_flash_size_kb;
        uint8_t has_dual_bank;
        uint16_t first_bank_size_kb; /* Used when has_dual_bank is true */
@@ -141,7 +139,7 @@ static const struct stm32h7x_part_info stm32h7x_parts[] = {
        .id                                     = 0x450,
        .revs                           = stm32_450_revs,
        .num_revs                       = ARRAY_SIZE(stm32_450_revs),
-       .device_str                     = "STM32H7xx 2M",
+       .device_str                     = "STM32H74x/75x",
        .page_size                      = 128,  /* 128 KB */
        .max_flash_size_kb      = 2048,
        .first_bank_size_kb     = 1024,
@@ -308,13 +306,11 @@ static int stm32x_lock_reg(struct flash_bank *bank)
 static int stm32x_read_options(struct flash_bank *bank)
 {
        uint32_t optiondata;
-       struct stm32h7x_flash_bank *stm32x_info = NULL;
+       struct stm32h7x_flash_bank *stm32x_info = bank->driver_priv;
        struct target *target = bank->target;
 
-       stm32x_info = bank->driver_priv;
-
        /* read current option bytes */
-       int retval = target_read_u32(target, FLASH_REG_BASE_B0 + FLASH_OPTCUR, &optiondata);
+       int retval = target_read_u32(target, FLASH_REG_BASE_B0 + FLASH_OPTSR_CUR, &optiondata);
        if (retval != ERROR_OK)
                return retval;
 
@@ -328,28 +324,20 @@ static int stm32x_read_options(struct flash_bank *bank)
                LOG_INFO("Device Security Bit Set");
 
        /* read current WPSN option bytes */
-       retval = target_read_u32(target, FLASH_REG_BASE_B0 + FLASH_WPSNCUR, &optiondata);
+       retval = target_read_u32(target, stm32x_get_flash_reg(bank, FLASH_WPSN_CUR), &optiondata);
        if (retval != ERROR_OK)
                return retval;
        stm32x_info->option_bytes.protection = optiondata & 0xff;
 
-       /* read current WPSN2 option bytes */
-       retval = target_read_u32(target, FLASH_REG_BASE_B1 + FLASH_WPSNCUR, &optiondata);
-       if (retval != ERROR_OK)
-               return retval;
-       stm32x_info->option_bytes.protection2 = optiondata & 0xff;
-
        return ERROR_OK;
 }
 
 static int stm32x_write_options(struct flash_bank *bank)
 {
-       struct stm32h7x_flash_bank *stm32x_info = NULL;
+       struct stm32h7x_flash_bank *stm32x_info = bank->driver_priv;
        struct target *target = bank->target;
        uint32_t optiondata;
 
-       stm32x_info = bank->driver_priv;
-
        int retval = stm32x_unlock_option_reg(bank);
        if (retval != ERROR_OK)
                return retval;
@@ -361,19 +349,13 @@ static int stm32x_write_options(struct flash_bank *bank)
        optiondata |= (stm32x_info->option_bytes.user3_options & 0xa3) << 24;
 
        /* program options */
-       retval = target_write_u32(target, FLASH_REG_BASE_B0 + FLASH_OPTPRG, optiondata);
+       retval = target_write_u32(target, FLASH_REG_BASE_B0 + FLASH_OPTSR_PRG, optiondata);
        if (retval != ERROR_OK)
                return retval;
 
        optiondata = stm32x_info->option_bytes.protection & 0xff;
        /* Program protection WPSNPRG */
-       retval = target_write_u32(target, FLASH_REG_BASE_B0 + FLASH_WPSNPRG, optiondata);
-       if (retval != ERROR_OK)
-               return retval;
-
-       optiondata = stm32x_info->option_bytes.protection2 & 0xff;
-       /* Program protection WPSNPRG2 */
-       retval = target_write_u32(target, FLASH_REG_BASE_B1 + FLASH_WPSNPRG, optiondata);
+       retval = target_write_u32(target, stm32x_get_flash_reg(bank, FLASH_WPSN_PRG), optiondata);
        if (retval != ERROR_OK)
                return retval;
 
@@ -427,17 +409,7 @@ static int stm32x_protect_check(struct flash_bank *bank)
        }
 
        for (int i = 0; i < bank->num_sectors; i++) {
-               if (stm32x_info->flash_base == FLASH_REG_BASE_B0) {
-                       if (stm32x_info->option_bytes.protection & (1 << i))
-                               bank->sectors[i].is_protected = 0;
-                       else
-                               bank->sectors[i].is_protected = 1;
-               } else {
-                       if (stm32x_info->option_bytes.protection2 & (1 << i))
-                               bank->sectors[i].is_protected = 0;
-                       else
-                               bank->sectors[i].is_protected = 1;
-               }
+               bank->sectors[i].is_protected = stm32x_info->option_bytes.protection & (1 << i) ? 0 : 1;
        }
        return ERROR_OK;
 }
@@ -516,21 +488,14 @@ static int stm32x_protect(struct flash_bank *bank, int set, int first, int last)
        }
 
        for (int i = first; i <= last; i++) {
-               if (stm32x_info->flash_base == FLASH_REG_BASE_B0) {
-                       if (set)
-                               stm32x_info->option_bytes.protection &= ~(1 << i);
-                       else
-                               stm32x_info->option_bytes.protection |= (1 << i);
-               } else {
-                       if (set)
-                               stm32x_info->option_bytes.protection2 &= ~(1 << i);
-                       else
-                               stm32x_info->option_bytes.protection2 |= (1 << i);
-               }
+               if (set)
+                       stm32x_info->option_bytes.protection &= ~(1 << i);
+               else
+                       stm32x_info->option_bytes.protection |= (1 << i);
        }
 
-       LOG_INFO("stm32x_protect, option_bytes written WRP1 0x%x , WRP2 0x%x",
-         (stm32x_info->option_bytes.protection & 0xff), (stm32x_info->option_bytes.protection2 & 0xff));
+       LOG_DEBUG("stm32x_protect, option_bytes written WPSN 0x%x",
+         (stm32x_info->option_bytes.protection & 0xff));
 
        retval = stm32x_write_options(bank);
        if (retval != ERROR_OK)
@@ -952,7 +917,7 @@ COMMAND_HANDLER(stm32x_handle_lock_command)
        }
 
        if (stm32x_read_options(bank) != ERROR_OK) {
-               command_print(CMD_CTX, "%s failed to read options",
+               command_print(CMD, "%s failed to read options",
                              bank->driver->name);
                return ERROR_OK;
        }
@@ -960,11 +925,11 @@ COMMAND_HANDLER(stm32x_handle_lock_command)
        stm32x_info->option_bytes.RDP = 0;
 
        if (stm32x_write_options(bank) != ERROR_OK) {
-               command_print(CMD_CTX, "%s failed to lock device",
+               command_print(CMD, "%s failed to lock device",
                              bank->driver->name);
                return ERROR_OK;
        }
-       command_print(CMD_CTX, "%s locked", bank->driver->name);
+       command_print(CMD, "%s locked", bank->driver->name);
 
        return ERROR_OK;
 }
@@ -998,7 +963,7 @@ COMMAND_HANDLER(stm32x_handle_unlock_command)
        }
 
        if (stm32x_read_options(bank) != ERROR_OK) {
-               command_print(CMD_CTX, "%s failed to read options", bank->driver->name);
+               command_print(CMD, "%s failed to read options", bank->driver->name);
                return ERROR_OK;
        }
 
@@ -1007,10 +972,10 @@ COMMAND_HANDLER(stm32x_handle_unlock_command)
        stm32x_info->option_bytes.RDP = 0xAA;
 
        if (stm32x_write_options(bank) != ERROR_OK) {
-               command_print(CMD_CTX, "%s failed to unlock device", bank->driver->name);
+               command_print(CMD, "%s failed to unlock device", bank->driver->name);
                return ERROR_OK;
        }
-       command_print(CMD_CTX, "%s unlocked.\n", bank->driver->name);
+       command_print(CMD, "%s unlocked.\n", bank->driver->name);
 
        return ERROR_OK;
 }
@@ -1030,12 +995,12 @@ static int stm32x_mass_erase(struct flash_bank *bank)
                return retval;
 
        /* mass erase flash memory bank */
-       retval = target_write_u32(target, stm32x_get_flash_reg(bank, FLASH_CR), FLASH_BER_CMD | FLASH_PSIZE_64);
+       retval = target_write_u32(target, stm32x_get_flash_reg(bank, FLASH_CR), FLASH_BER | FLASH_PSIZE_64);
        if (retval != ERROR_OK)
                return retval;
 
        retval = target_write_u32(target, stm32x_get_flash_reg(bank, FLASH_CR),
-                                                         FLASH_BER_CMD | FLASH_PSIZE_64 | FLASH_START);
+                                                         FLASH_BER | FLASH_PSIZE_64 | FLASH_START);
        if (retval != ERROR_OK)
                return retval;
 
@@ -1056,7 +1021,7 @@ COMMAND_HANDLER(stm32x_handle_mass_erase_command)
        int i;
 
        if (CMD_ARGC < 1) {
-               command_print(CMD_CTX, "stm32h7x mass_erase <bank>");
+               command_print(CMD, "stm32h7x mass_erase <bank>");
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
@@ -1071,9 +1036,9 @@ COMMAND_HANDLER(stm32x_handle_mass_erase_command)
                for (i = 0; i < bank->num_sectors; i++)
                        bank->sectors[i].is_erased = 1;
 
-               command_print(CMD_CTX, "stm32h7x mass erase complete");
+               command_print(CMD, "stm32h7x mass erase complete");
        } else {
-               command_print(CMD_CTX, "stm32h7x mass erase failed");
+               command_print(CMD, "stm32h7x mass erase failed");
        }
 
        return retval;

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)