- /* get flash size from target. */
- retval = target_read_u16(target, F_SIZE, &flash_size_in_kb);
- if (retval != ERROR_OK) {
- LOG_WARNING("failed reading flash size, default to max target family");
- /* failed reading flash size, default to max target family */
- flash_size_in_kb = 0xffff;
- }
-
- /* some variants read 0 for flash size register
- * use a max flash size as a default */
- if (flash_size_in_kb == 0)
- flash_size_in_kb = 0xffff;
-
- if ((device_id & 0xfff) == 0x416) {
- /* check for early silicon */
- if (flash_size_in_kb == 0xffff) {
- /* number of sectors may be incorrect on early silicon */
- LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash");
- flash_size_in_kb = 128;
- }
- } else if ((device_id & 0xfff) == 0x436) {
- /* check for early silicon */
- if (flash_size_in_kb == 0xffff) {
- /* number of sectors may be incorrect on early silicon */
- LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 384k flash");
+ /* set max flash size depending on family */
+ switch (device_id & 0xfff) {
+ case 0x416:
+ max_flash_size_in_kb = 128;
+ break;
+ case 0x427:
+ /* single bank, high density */
+ max_flash_size_in_kb = 256;
+ break;
+ case 0x436:
+ /* According to ST, the devices with id 0x436 have dual bank flash and comes with
+ * a total flash size of 384k or 256kb. However, the first bank is always 192kb,
+ * and second one holds the rest. The reason is that the 256kb version is actually
+ * the same physical flash but only the first 256kb are verified.
+ */
+ max_flash_size_in_kb = 384;
+ first_bank_size_in_kb = 192;
+ stm32lx_info->has_dual_banks = true;
+ break;
+ case 0x437:
+ /* Dual bank, high density */
+ max_flash_size_in_kb = 512;
+ first_bank_size_in_kb = 192;
+ stm32lx_info->has_dual_banks = true;
+ break;
+ default:
+ LOG_WARNING("Cannot identify target as a STM32L family.");
+ return ERROR_FAIL;
+ }
+
+ /* Get the flash size from target. 0x427 and 0x436 devices use a
+ * different location for the Flash Size register, please see RM0038 r8 or
+ * newer. */
+ if ((device_id & 0xfff) == 0x427 || (device_id & 0xfff) == 0x436 ||
+ (device_id & 0xfff) == 0x437)
+ retval = target_read_u16(target, F_SIZE_MP, &flash_size_in_kb);
+ else
+ retval = target_read_u16(target, F_SIZE, &flash_size_in_kb);
+
+ /* 0x436 devices report their flash size as a 0 or 1 code indicating 384K
+ * or 256K, respectively. Please see RM0038 r8 or newer and refer to
+ * section 30.1.1. */
+ if (retval == ERROR_OK && (device_id & 0xfff) == 0x436) {
+ if (flash_size_in_kb == 0)