#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;
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;
};
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;
}
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;
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;
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",
/* 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;
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);
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.");
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);
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");
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");
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");
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;
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;
}
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;
}
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.
*/
optionbyte &= ~(1 << 0);
}
- /* OPT_RDRSTSTDBY */
- if (strcmp(CMD_ARGV[2], "NORSTSTNDBY") == 0)
+ /* OPT_RDRSTSTOP */
+ if (strcmp(CMD_ARGV[2], "NORSTSTOP") == 0)
{
optionbyte |= (1 << 1);
}
optionbyte &= ~(1 << 1);
}
- /* OPT_RDRSTSTOP */
- if (strcmp(CMD_ARGV[3], "NORSTSTOP") == 0)
+ /* OPT_RDRSTSTDBY */
+ if (strcmp(CMD_ARGV[3], "NORSTSTNDBY") == 0)
{
optionbyte |= (1 << 2);
}
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");