flash/stm32l4x: introduce auto-probe when OPTR is changed 35/5935/14
authorTarek BOCHKATI <tarek.bouchkati@gmail.com>
Tue, 10 Nov 2020 18:15:54 +0000 (19:15 +0100)
committerOleksij Rempel <linux@rempel-privat.de>
Thu, 26 Aug 2021 06:18:52 +0000 (06:18 +0000)
auto re-probing is ensured by having optr cache set in the
last probe operation.

this will help to detect if flash options have been modified by the
running application or by the user using direct register access.

Change-Id: I05cd7ab9e83a7fc26ac6cff175b3c11b0efa2eb5
Signed-off-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/5935
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-by: Oleksij Rempel <linux@rempel-privat.de>
Tested-by: jenkins
src/flash/nor/stm32l4x.c

index 6370d17becca58f0d19c26feaabe96ea01628a31..06d4ac1eaaf6c98048da083bc62d1054e06b4dfc 100644 (file)
@@ -209,6 +209,7 @@ struct stm32l4_flash_bank {
        bool otp_enabled;
        enum stm32l4_rdp rdp;
        bool tzen;
+       uint32_t optr;
 };
 
 enum stm32_bank_id {
@@ -620,16 +621,16 @@ static inline bool stm32l4_otp_is_enabled(struct flash_bank *bank)
        return stm32l4_info->otp_enabled;
 }
 
-static void stm32l4_sync_rdp_tzen(struct flash_bank *bank, uint32_t optr_value)
+static void stm32l4_sync_rdp_tzen(struct flash_bank *bank)
 {
        struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
 
        bool tzen = false;
 
        if (stm32l4_info->part_info->flags & F_HAS_TZ)
-               tzen = (optr_value & FLASH_TZEN) != 0;
+               tzen = (stm32l4_info->optr & FLASH_TZEN) != 0;
 
-       uint32_t rdp = optr_value & FLASH_RDP_MASK;
+       uint32_t rdp = stm32l4_info->optr & FLASH_RDP_MASK;
 
        /* for devices without TrustZone:
         *   RDP level 0 and 2 values are to 0xAA and 0xCC
@@ -1396,7 +1397,6 @@ static int stm32l4_probe(struct flash_bank *bank)
        struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
        const struct stm32l4_part_info *part_info;
        uint16_t flash_size_kb = 0xffff;
-       uint32_t options;
 
        stm32l4_info->probed = false;
 
@@ -1429,11 +1429,11 @@ static int stm32l4_probe(struct flash_bank *bank)
        stm32l4_info->flash_regs = stm32l4_info->part_info->default_flash_regs;
 
        /* read flash option register */
-       retval = stm32l4_read_flash_reg_by_index(bank, STM32_FLASH_OPTR_INDEX, &options);
+       retval = stm32l4_read_flash_reg_by_index(bank, STM32_FLASH_OPTR_INDEX, &stm32l4_info->optr);
        if (retval != ERROR_OK)
                return retval;
 
-       stm32l4_sync_rdp_tzen(bank, options);
+       stm32l4_sync_rdp_tzen(bank);
 
        if (part_info->flags & F_HAS_TZ)
                LOG_INFO("TZEN = %d : TrustZone %s by option bytes",
@@ -1515,7 +1515,7 @@ static int stm32l4_probe(struct flash_bank *bank)
                stm32l4_info->bank1_sectors = num_pages;
 
                /* check DUAL_BANK bit[21] if the flash is less than 1M */
-               if (flash_size_kb == 1024 || (options & BIT(21))) {
+               if (flash_size_kb == 1024 || (stm32l4_info->optr & BIT(21))) {
                        stm32l4_info->dual_bank_mode = true;
                        stm32l4_info->bank1_sectors = num_pages / 2;
                }
@@ -1541,7 +1541,7 @@ static int stm32l4_probe(struct flash_bank *bank)
                page_size_kb = 4;
                num_pages = flash_size_kb / page_size_kb;
                stm32l4_info->bank1_sectors = num_pages;
-               if (options & BIT(22)) {
+               if (stm32l4_info->optr & BIT(22)) {
                        stm32l4_info->dual_bank_mode = true;
                        page_size_kb = 2;
                        num_pages = flash_size_kb / page_size_kb;
@@ -1565,8 +1565,8 @@ static int stm32l4_probe(struct flash_bank *bank)
                num_pages = flash_size_kb / page_size_kb;
                stm32l4_info->bank1_sectors = num_pages;
                use_dbank_bit = flash_size_kb == part_info->max_flash_size_kb;
-               if ((use_dbank_bit && (options & BIT(22))) ||
-                       (!use_dbank_bit && (options & BIT(21)))) {
+               if ((use_dbank_bit && (stm32l4_info->optr & BIT(22))) ||
+                       (!use_dbank_bit && (stm32l4_info->optr & BIT(21)))) {
                        stm32l4_info->dual_bank_mode = true;
                        page_size_kb = 4;
                        num_pages = flash_size_kb / page_size_kb;
@@ -1582,8 +1582,8 @@ static int stm32l4_probe(struct flash_bank *bank)
                num_pages = flash_size_kb / page_size_kb;
                stm32l4_info->bank1_sectors = num_pages;
                use_dbank_bit = flash_size_kb == part_info->max_flash_size_kb;
-               if ((use_dbank_bit && (options & BIT(22))) ||
-                       (!use_dbank_bit && (options & BIT(21)))) {
+               if ((use_dbank_bit && (stm32l4_info->optr & BIT(22))) ||
+                       (!use_dbank_bit && (stm32l4_info->optr & BIT(21)))) {
                        stm32l4_info->dual_bank_mode = true;
                        page_size_kb = 2;
                        num_pages = flash_size_kb / page_size_kb;
@@ -1660,8 +1660,17 @@ static int stm32l4_probe(struct flash_bank *bank)
 static int stm32l4_auto_probe(struct flash_bank *bank)
 {
        struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
-       if (stm32l4_info->probed)
-               return ERROR_OK;
+       if (stm32l4_info->probed) {
+               uint32_t optr_cur;
+
+               /* read flash option register and re-probe if optr value is changed */
+               int retval = stm32l4_read_flash_reg_by_index(bank, STM32_FLASH_OPTR_INDEX, &optr_cur);
+               if (retval != ERROR_OK)
+                       return retval;
+
+               if (stm32l4_info->optr == optr_cur)
+                       return ERROR_OK;
+       }
 
        return stm32l4_probe(bank);
 }
@@ -1827,12 +1836,11 @@ COMMAND_HANDLER(stm32l4_handle_trustzone_command)
                return ERROR_FAIL;
        }
 
-       uint32_t optr;
-       retval = stm32l4_read_flash_reg_by_index(bank, STM32_FLASH_OPTR_INDEX, &optr);
+       retval = stm32l4_read_flash_reg_by_index(bank, STM32_FLASH_OPTR_INDEX, &stm32l4_info->optr);
        if (retval != ERROR_OK)
                return retval;
 
-       stm32l4_sync_rdp_tzen(bank, optr);
+       stm32l4_sync_rdp_tzen(bank);
 
        if (CMD_ARGC == 1) {
                /* only display the TZEN value */

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)