flash/stm32l4x: introduce auto-probe when OPTR is changed
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 */