flash/stm32l4x: fix auto-probe when RDP is promoted from 0 to 0.5
authorTarek BOCHKATI <tarek.bouchkati@gmail.com>
Mon, 28 Feb 2022 12:55:26 +0000 (13:55 +0100)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 19 Mar 2022 09:13:55 +0000 (09:13 +0000)
Considering this use case: (using STM32 L5 or U5)
 1- first probe : TZEN enabled, RDP level 0
    flash_regs_base |= STM32L5_REGS_SEC_OFFSET => 0x50022000
 2- the user promotes the RDP to level 0.5
 3- the second probe, fails to read OPTR using secure flags_regs_base:
    used OPTR address is 0x50022040

Step 3 fails because when RDP is level 0.5, we should use Non-Secure
flash registers.
To fix this, always use NS flash regs to read OPTR in probe functions.

Fixes: 80d323c6e82b (flash/stm32l4x: introduce auto-probe when OPTR is changed)
Change-Id: I296aa633972b0c410b927488c999584a07b912d3
Signed-off-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/6864
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
Tested-by: jenkins
src/flash/nor/stm32l4x.c

index fd0338899f461293098f2f3de4d8fa743e8f5211..7b35a0635392fa61fd21a923be2a957910f43152 100644 (file)
@@ -1757,7 +1757,7 @@ static int stm32l4_probe(struct flash_bank *bank)
         * Ask the flash infrastructure to ensure required alignment */
        bank->write_start_alignment = bank->write_end_alignment = stm32l4_info->data_width;
 
-       /* initialise the flash registers layout */
+       /* Initialize the flash registers layout */
        if (part_info->flags & F_HAS_L5_FLASH_REGS)
                stm32l4_info->flash_regs = stm32l5_ns_flash_regs;
        else
@@ -1770,7 +1770,7 @@ static int stm32l4_probe(struct flash_bank *bank)
 
        stm32l4_sync_rdp_tzen(bank);
 
-       /* for devices with trustzone, use flash secure registers when TZEN=1 and RDP is LEVEL_0 */
+       /* for devices with TrustZone, use flash secure registers when TZEN=1 and RDP is LEVEL_0 */
        if (stm32l4_info->tzen && (stm32l4_info->rdp == RDP_LEVEL_0)) {
                if (part_info->flags & F_HAS_L5_FLASH_REGS) {
                        stm32l4_info->flash_regs_base |= STM32L5_REGS_SEC_OFFSET;
@@ -2046,8 +2046,19 @@ static int stm32l4_auto_probe(struct flash_bank *bank)
        if (stm32l4_info->probed) {
                uint32_t optr_cur;
 
+               /* save flash_regs_base */
+               uint32_t saved_flash_regs_base = stm32l4_info->flash_regs_base;
+
+               /* for devices with TrustZone, use NS flash registers to read OPTR */
+               if (stm32l4_info->part_info->flags & F_HAS_L5_FLASH_REGS)
+                       stm32l4_info->flash_regs_base &= ~STM32L5_REGS_SEC_OFFSET;
+
                /* 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);
+
+               /* restore saved flash_regs_base */
+               stm32l4_info->flash_regs_base = saved_flash_regs_base;
+
                if (retval != ERROR_OK)
                        return retval;