flash: fix stm32 flash driver typo's
[fw/openocd] / src / flash / nor / stm32f1x.c
index e9c2fb978cfa1f6fed460447de55c004b6ecb2c7..2a6604dc2a42833e0724adf96f89610dbfb46839 100644 (file)
@@ -604,17 +604,14 @@ static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer,
        /* see contrib/loaders/flash/stm32f1x.S for src */
 
        static const uint8_t stm32x_flash_write_code[] = {
-               /* #define STM32_FLASH_CR_OFFSET 0x10 */
                /* #define STM32_FLASH_SR_OFFSET 0x0C */
                /* wait_fifo: */
                        0x16, 0x68,   /* ldr   r6, [r2, #0] */
                        0x00, 0x2e,   /* cmp   r6, #0 */
-                       0x1a, 0xd0,   /* beq   exit */
+                       0x18, 0xd0,   /* beq   exit */
                        0x55, 0x68,   /* ldr   r5, [r2, #4] */
                        0xb5, 0x42,   /* cmp   r5, r6 */
                        0xf9, 0xd0,   /* beq   wait_fifo */
-                       0x01, 0x26,   /* movs  r6, #1 */
-                       0x06, 0x61,   /* str   r6, [r0, #STM32_FLASH_CR_OFFSET] */
                        0x2e, 0x88,   /* ldrh  r6, [r5, #0] */
                        0x26, 0x80,   /* strh  r6, [r4, #0] */
                        0x02, 0x35,   /* adds  r5, #2 */
@@ -636,7 +633,7 @@ static int stm32x_write_block(struct flash_bank *bank, uint8_t *buffer,
                        0x01, 0x39,   /* subs  r1, r1, #1 */
                        0x00, 0x29,   /* cmp   r1, #0 */
                        0x02, 0xd0,   /* beq   exit */
-                       0xe3, 0xe7,   /* b     wait_fifo */
+                       0xe5, 0xe7,   /* b     wait_fifo */
                /* error: */
                        0x00, 0x20,   /* movs  r0, #0 */
                        0x50, 0x60,   /* str   r0, [r2, #4] */
@@ -751,6 +748,10 @@ static int stm32x_write(struct flash_bank *bank, uint8_t *buffer,
        if (retval != ERROR_OK)
                return retval;
 
+       retval = target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_PG);
+       if (retval != ERROR_OK)
+               return retval;
+
        /* multiple half words (2-byte) to be programmed? */
        if (words_remaining > 0) {
                /* try using a block write */
@@ -769,22 +770,19 @@ static int stm32x_write(struct flash_bank *bank, uint8_t *buffer,
        }
 
        if ((retval != ERROR_OK) && (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE))
-               return retval;
+               goto reset_pg_and_lock;
 
        while (words_remaining > 0) {
                uint16_t value;
                memcpy(&value, buffer + bytes_written, sizeof(uint16_t));
 
-               retval = target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_PG);
-               if (retval != ERROR_OK)
-                       return retval;
                retval = target_write_u16(target, address, value);
                if (retval != ERROR_OK)
-                       return retval;
+                       goto reset_pg_and_lock;
 
                retval = stm32x_wait_status_busy(bank, 5);
                if (retval != ERROR_OK)
-                       return retval;
+                       goto reset_pg_and_lock;
 
                bytes_written += 2;
                words_remaining--;
@@ -795,19 +793,20 @@ static int stm32x_write(struct flash_bank *bank, uint8_t *buffer,
                uint16_t value = 0xffff;
                memcpy(&value, buffer + bytes_written, bytes_remaining);
 
-               retval = target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_PG);
-               if (retval != ERROR_OK)
-                       return retval;
                retval = target_write_u16(target, address, value);
                if (retval != ERROR_OK)
-                       return retval;
+                       goto reset_pg_and_lock;
 
                retval = stm32x_wait_status_busy(bank, 5);
                if (retval != ERROR_OK)
-                       return retval;
+                       goto reset_pg_and_lock;
        }
 
        return target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_LOCK);
+
+reset_pg_and_lock:
+       target_write_u32(target, stm32x_get_flash_reg(bank, STM32_FLASH_CR), FLASH_LOCK);
+       return retval;
 }
 
 static int stm32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
@@ -830,6 +829,9 @@ static int stm32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
        } else if (((cpuid >> 4) & 0xFFF) == 0xC23) {
                /* 0xC23 is M3 devices */
                device_id_register = 0xE0042000;
+       } else if (((cpuid >> 4) & 0xFFF) == 0xC24) {
+               /* 0xC24 is M4 devices */
+               device_id_register = 0xE0042000;
        } else {
                LOG_ERROR("Cannot identify target as a stm32x");
                return ERROR_FAIL;
@@ -843,9 +845,38 @@ static int stm32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
        return retval;
 }
 
-static int stm32x_probe(struct flash_bank *bank)
+static int stm32x_get_flash_size(struct flash_bank *bank, uint16_t *flash_size_in_kb)
 {
        struct target *target = bank->target;
+       uint32_t cpuid, flash_size_reg;
+
+       int retval = target_read_u32(target, 0xE000ED00, &cpuid);
+       if (retval != ERROR_OK)
+               return retval;
+
+       if (((cpuid >> 4) & 0xFFF) == 0xC20) {
+               /* 0xC20 is M0 devices */
+               flash_size_reg = 0x1FFFF7CC;
+       } else if (((cpuid >> 4) & 0xFFF) == 0xC23) {
+               /* 0xC23 is M3 devices */
+               flash_size_reg = 0x1FFFF7E0;
+       } else if (((cpuid >> 4) & 0xFFF) == 0xC24) {
+               /* 0xC24 is M4 devices */
+               flash_size_reg = 0x1FFFF7CC;
+       } else {
+               LOG_ERROR("Cannot identify target as a stm32x");
+               return ERROR_FAIL;
+       }
+
+       retval = target_read_u16(target, flash_size_reg, flash_size_in_kb);
+       if (retval != ERROR_OK)
+               return retval;
+
+       return retval;
+}
+
+static int stm32x_probe(struct flash_bank *bank)
+{
        struct stm32x_flash_bank *stm32x_info = bank->driver_priv;
        int i;
        uint16_t flash_size_in_kb;
@@ -853,7 +884,6 @@ static int stm32x_probe(struct flash_bank *bank)
        int page_size;
        uint32_t base_address = 0x08000000;
 
-
        stm32x_info->probed = 0;
        stm32x_info->register_base = FLASH_REG_BASE_B0;
 
@@ -865,13 +895,18 @@ static int stm32x_probe(struct flash_bank *bank)
        LOG_INFO("device id = 0x%08" PRIx32 "", device_id);
 
        /* get flash size from target. */
-       retval = target_read_u16(target, 0x1FFFF7E0, &flash_size_in_kb);
+       retval = stm32x_get_flash_size(bank, &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) == 0x410) {
                /* medium density - we have 1k pages
                 * 4 pages for a protection area */
@@ -928,10 +963,21 @@ static int stm32x_probe(struct flash_bank *bank)
 
                /* check for early silicon */
                if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors may be incorrrect on early silicon */
+                       /* 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) == 0x422) {
+               /* stm32f30x - we have 2k pages
+                * 2 pages for a protection area */
+               page_size = 2048;
+               stm32x_info->ppage_size = 2;
+
+               /* check for early silicon */
+               if (flash_size_in_kb == 0xffff) {
+                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash");
+                       flash_size_in_kb = 256;
+               }
        } else if ((device_id & 0xfff) == 0x428) {
                /* value line High density - we have 2k pages
                 * 4 pages for a protection area */
@@ -940,7 +986,7 @@ static int stm32x_probe(struct flash_bank *bank)
 
                /* check for early silicon */
                if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors may be incorrrect on early silicon */
+                       /* 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;
                }
@@ -953,7 +999,7 @@ static int stm32x_probe(struct flash_bank *bank)
 
                /* check for early silicon */
                if (flash_size_in_kb == 0xffff) {
-                       /* number of sectors may be incorrrect on early silicon */
+                       /* number of sectors may be incorrect on early silicon */
                        LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash");
                        flash_size_in_kb = 1024;
                }
@@ -968,6 +1014,17 @@ static int stm32x_probe(struct flash_bank *bank)
                        stm32x_info->register_base = FLASH_REG_BASE_B1;
                        base_address = 0x08080000;
                }
+       } else if ((device_id & 0xfff) == 0x432) {
+               /* stm32f37x - we have 2k pages
+                * 2 pages for a protection area */
+               page_size = 2048;
+               stm32x_info->ppage_size = 2;
+
+               /* check for early silicon */
+               if (flash_size_in_kb == 0xffff) {
+                       LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash");
+                       flash_size_in_kb = 256;
+               }
        } else if ((device_id & 0xfff) == 0x440) {
                /* stm32f0x - we have 1k pages
                 * 4 pages for a protection area */
@@ -1133,6 +1190,20 @@ static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
                                snprintf(buf, buf_size, "Z");
                                break;
 
+                       default:
+                               snprintf(buf, buf_size, "unknown");
+                               break;
+               }
+       } else if ((device_id & 0xfff) == 0x422) {
+               printed = snprintf(buf, buf_size, "stm32f30x - Rev: ");
+               buf += printed;
+               buf_size -= printed;
+
+               switch (device_id >> 16) {
+                       case 0x1000:
+                               snprintf(buf, buf_size, "1.0");
+                               break;
+
                        default:
                                snprintf(buf, buf_size, "unknown");
                                break;
@@ -1165,6 +1236,20 @@ static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
                                snprintf(buf, buf_size, "A");
                                break;
 
+                       default:
+                               snprintf(buf, buf_size, "unknown");
+                               break;
+               }
+       } else if ((device_id & 0xfff) == 0x432) {
+               printed = snprintf(buf, buf_size, "stm32f37x - Rev: ");
+               buf += printed;
+               buf_size -= printed;
+
+               switch (device_id >> 16) {
+                       case 0x1000:
+                               snprintf(buf, buf_size, "1.0");
+                               break;
+
                        default:
                                snprintf(buf, buf_size, "unknown");
                                break;
@@ -1176,7 +1261,11 @@ static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
 
                switch (device_id >> 16) {
                        case 0x1000:
-                               snprintf(buf, buf_size, "A");
+                               snprintf(buf, buf_size, "1.0");
+                               break;
+
+                       case 0x2000:
+                               snprintf(buf, buf_size, "2.0");
                                break;
 
                        default:
@@ -1540,7 +1629,7 @@ struct flash_driver stm32f1x_flash = {
        .read = default_flash_read,
        .probe = stm32x_probe,
        .auto_probe = stm32x_auto_probe,
-       .erase_check = default_flash_mem_blank_check,
+       .erase_check = default_flash_blank_check,
        .protect_check = stm32x_protect_check,
        .info = get_stm32x_info,
 };