uptech2410
[openocd.git] / src / flash / nor / stm32x.c
index 8aae203625679cdad43e795ed8c704528b6b0598..3914d25c2f4765b86c2b550c1af2483c03686c81 100644 (file)
 #define OPT_RDWDGSW            2
 #define OPT_RDRSTSTOP  3
 #define OPT_RDRSTSTDBY 4
+#define OPT_BFB2               5       /* dual flash bank only */
 
 /* register unlock keys */
 
 #define KEY1                   0x45670123
 #define KEY2                   0xCDEF89AB
 
+/* we use an offset to access the second bank on dual flash devices
+ * strangely the protection of the second bank is done on the bank0 reg's */
+
+#define FLASH_OFFSET_B0        0x00
+#define FLASH_OFFSET_B1 0x40
+
 struct stm32x_options
 {
        uint16_t RDP;
@@ -96,9 +103,10 @@ struct stm32x_flash_bank
        int ppage_size;
        int probed;
 
+       bool has_dual_banks;
        /* used to access dual flash bank stm32xl
-        * 0x00 will address sector 0 flash
-        * 0x40 will address sector 1 flash */
+        * 0x00 will address bank 0 flash
+        * 0x40 will address bank 1 flash */
        int register_offset;
 };
 
@@ -121,7 +129,8 @@ FLASH_BANK_COMMAND_HANDLER(stm32x_flash_bank_command)
 
        stm32x_info->write_algorithm = NULL;
        stm32x_info->probed = 0;
-       stm32x_info->register_offset = 0x00;
+       stm32x_info->has_dual_banks = false;
+       stm32x_info->register_offset = FLASH_OFFSET_B0;
 
        return ERROR_OK;
 }
@@ -185,6 +194,21 @@ static int stm32x_wait_status_busy(struct flash_bank *bank, int timeout)
        return retval;
 }
 
+int stm32x_check_operation_supported(struct flash_bank *bank)
+{
+       struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
+
+       /* if we have a dual flash bank device then
+        * we need to perform option byte stuff on bank0 only */
+       if (stm32x_info->register_offset != FLASH_OFFSET_B0)
+       {
+               LOG_ERROR("Option Byte Operation's must use bank0");
+               return ERROR_FLASH_OPERATION_FAILED;
+       }
+
+       return ERROR_OK;
+}
+
 static int stm32x_read_options(struct flash_bank *bank)
 {
        uint32_t optiondata;
@@ -368,9 +392,13 @@ static int stm32x_protect_check(struct flash_bank *bank)
                return ERROR_TARGET_NOT_HALTED;
        }
 
+       int retval = stm32x_check_operation_supported(bank);
+       if (ERROR_OK != retval)
+               return retval;
+
        /* medium density - each bit refers to a 4bank protection
         * high density - each bit refers to a 2bank protection */
-       int retval = target_read_u32(target, STM32_FLASH_WRPR, &protection);
+       retval = target_read_u32(target, STM32_FLASH_WRPR, &protection);
        if (retval != ERROR_OK)
                return retval;
 
@@ -495,6 +523,10 @@ static int stm32x_protect(struct flash_bank *bank, int set, int first, int last)
                return ERROR_TARGET_NOT_HALTED;
        }
 
+       int retval = stm32x_check_operation_supported(bank);
+       if (ERROR_OK != retval)
+               return retval;
+
        if ((first % stm32x_info->ppage_size) != 0)
        {
                LOG_WARNING("aligned start protect sector to a %d sector boundary",
@@ -512,7 +544,7 @@ static int stm32x_protect(struct flash_bank *bank, int set, int first, int last)
 
        /* medium density - each bit refers to a 4bank protection
         * high density - each bit refers to a 2bank protection */
-       int retval = target_read_u32(target, STM32_FLASH_WRPR, &protection);
+       retval = target_read_u32(target, STM32_FLASH_WRPR, &protection);
        if (retval != ERROR_OK)
                return retval;
 
@@ -811,9 +843,10 @@ static int stm32x_probe(struct flash_bank *bank)
        uint16_t num_pages;
        uint32_t device_id;
        int page_size;
+       uint32_t base_address = 0x08000000;
 
        stm32x_info->probed = 0;
-       stm32x_info->register_offset = 0x00;
+       stm32x_info->register_offset = FLASH_OFFSET_B0;
 
        /* read stm32 device id register */
        int retval = target_read_u32(target, 0xE0042000, &device_id);
@@ -905,6 +938,36 @@ static int stm32x_probe(struct flash_bank *bank)
                        num_pages = 128;
                }
        }
+       else if ((device_id & 0x7ff) == 0x430)
+       {
+               /* xl line density - we have 2k pages
+                * 2 pages for a protection area */
+               page_size = 2048;
+               stm32x_info->ppage_size = 2;
+               stm32x_info->has_dual_banks = true;
+
+               /* check for early silicon */
+               if (num_pages == 0xffff)
+               {
+                       /* number of sectors may be incorrrect on early silicon */
+                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash");
+                       num_pages = 1024;
+               }
+
+               /* split reported size into matching bank */
+               if (bank->base != 0x08080000)
+               {
+                       /* bank 0 will be fixed 512k */
+                       num_pages = 512;
+               }
+               else
+               {
+                       num_pages -= 512;
+                       /* bank1 also uses a register offset */
+                       stm32x_info->register_offset = FLASH_OFFSET_B1;
+                       base_address = 0x08080000;
+               }
+       }
        else
        {
                LOG_WARNING("Cannot identify target as a STM32 family.");
@@ -922,7 +985,7 @@ static int stm32x_probe(struct flash_bank *bank)
                bank->sectors = NULL;
        }
 
-       bank->base = 0x08000000;
+       bank->base = base_address;
        bank->size = (num_pages * page_size);
        bank->num_sectors = num_pages;
        bank->sectors = malloc(sizeof(struct flash_sector) * num_pages);
@@ -1075,6 +1138,23 @@ static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
                                break;
                }
        }
+       else if ((device_id & 0x7ff) == 0x430)
+       {
+               printed = snprintf(buf, buf_size, "stm32x (XL) - Rev: ");
+               buf += printed;
+               buf_size -= printed;
+
+               switch (device_id >> 16)
+               {
+                       case 0x1000:
+                               snprintf(buf, buf_size, "A");
+                               break;
+
+                       default:
+                               snprintf(buf, buf_size, "unknown");
+                               break;
+               }
+       }
        else
        {
                snprintf(buf, buf_size, "Cannot identify target as a stm32x\n");
@@ -1110,6 +1190,10 @@ COMMAND_HANDLER(stm32x_handle_lock_command)
                return ERROR_TARGET_NOT_HALTED;
        }
 
+       retval = stm32x_check_operation_supported(bank);
+       if (ERROR_OK != retval)
+               return retval;
+
        if (stm32x_erase_options(bank) != ERROR_OK)
        {
                command_print(CMD_CTX, "stm32x failed to erase options");
@@ -1156,6 +1240,10 @@ COMMAND_HANDLER(stm32x_handle_unlock_command)
                return ERROR_TARGET_NOT_HALTED;
        }
 
+       retval = stm32x_check_operation_supported(bank);
+       if (ERROR_OK != retval)
+               return retval;
+
        if (stm32x_erase_options(bank) != ERROR_OK)
        {
                command_print(CMD_CTX, "stm32x failed to unlock device");
@@ -1202,6 +1290,10 @@ COMMAND_HANDLER(stm32x_handle_options_read_command)
                return ERROR_TARGET_NOT_HALTED;
        }
 
+       retval = stm32x_check_operation_supported(bank);
+       if (ERROR_OK != retval)
+               return retval;
+
        retval = target_read_u32(target, STM32_FLASH_OBR, &optionbyte);
        if (retval != ERROR_OK)
                return retval;
@@ -1230,6 +1322,14 @@ COMMAND_HANDLER(stm32x_handle_options_read_command)
        else
                command_print(CMD_CTX, "Standby: Reset generated");
 
+       if (stm32x_info->has_dual_banks)
+       {
+               if (buf_get_u32((uint8_t*)&optionbyte, OPT_BFB2, 1))
+                       command_print(CMD_CTX, "Boot: Bank 0");
+               else
+                       command_print(CMD_CTX, "Boot: Bank 1");
+       }
+
        return ERROR_OK;
 }
 
@@ -1241,7 +1341,8 @@ COMMAND_HANDLER(stm32x_handle_options_write_command)
 
        if (CMD_ARGC < 4)
        {
-               command_print(CMD_CTX, "stm32x options_write <bank> <SWWDG | HWWDG> <RSTSTNDBY | NORSTSTNDBY> <RSTSTOP | NORSTSTOP>");
+               command_print(CMD_CTX, "stm32x options_write <bank> <SWWDG | HWWDG> "
+                               "<RSTSTNDBY | NORSTSTNDBY> <RSTSTOP | NORSTSTOP> <BOOT0 | BOOT1>");
                return ERROR_OK;
        }
 
@@ -1260,6 +1361,10 @@ COMMAND_HANDLER(stm32x_handle_options_write_command)
                return ERROR_TARGET_NOT_HALTED;
        }
 
+       retval = stm32x_check_operation_supported(bank);
+       if (ERROR_OK != retval)
+               return retval;
+
        /* REVISIT: ignores some options which we will display...
         * and doesn't insist on the specified syntax.
         */
@@ -1274,8 +1379,8 @@ COMMAND_HANDLER(stm32x_handle_options_write_command)
                optionbyte &= ~(1 << 0);
        }
 
-       /* OPT_RDRSTSTDBY */
-       if (strcmp(CMD_ARGV[2], "NORSTSTNDBY") == 0)
+       /* OPT_RDRSTSTOP */
+       if (strcmp(CMD_ARGV[2], "NORSTSTOP") == 0)
        {
                optionbyte |= (1 << 1);
        }
@@ -1284,8 +1389,8 @@ COMMAND_HANDLER(stm32x_handle_options_write_command)
                optionbyte &= ~(1 << 1);
        }
 
-       /* OPT_RDRSTSTOP */
-       if (strcmp(CMD_ARGV[3], "NORSTSTOP") == 0)
+       /* OPT_RDRSTSTDBY */
+       if (strcmp(CMD_ARGV[3], "NORSTSTNDBY") == 0)
        {
                optionbyte |= (1 << 2);
        }
@@ -1294,6 +1399,19 @@ COMMAND_HANDLER(stm32x_handle_options_write_command)
                optionbyte &= ~(1 << 2);
        }
 
+       if (CMD_ARGC > 4 && stm32x_info->has_dual_banks)
+       {
+               /* OPT_BFB2 */
+               if (strcmp(CMD_ARGV[4], "BOOT0") == 0)
+               {
+                       optionbyte |= (1 << 3);
+               }
+               else
+               {
+                       optionbyte &= ~(1 << 3);
+               }
+       }
+
        if (stm32x_erase_options(bank) != ERROR_OK)
        {
                command_print(CMD_CTX, "stm32x failed to erase options");

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)