flash Kinetis: refactoring ftfx commands and numerous minor changes
[openocd.git] / src / flash / nor / kinetis.c
index 0f6397581feeaf2f5b68d00a75c17f0efac15c3a..f91dda4fca8d731f0ca5bffb76c0d95ae20b08fb 100644 (file)
@@ -35,6 +35,7 @@
 #include "jtag/interface.h"
 #include "imp.h"
 #include <helper/binarybuffer.h>
+#include <helper/time_support.h>
 #include <target/target_type.h>
 #include <target/algorithm.h>
 #include <target/armv7m.h>
@@ -227,6 +228,8 @@ struct kinetis_flash_bank {
        } flash_support;
 };
 
+#define MDM_AP                 1
+
 #define MDM_REG_STAT           0x00
 #define MDM_REG_CTRL           0x04
 #define MDM_REG_ID             0xfc
@@ -254,14 +257,14 @@ struct kinetis_flash_bank {
 #define MDM_CTRL_VLLSX_DBG_ACK (1<<6)
 #define MDM_CTRL_VLLSX_STAT_ACK        (1<<7)
 
-#define MDM_ACCESS_TIMEOUT     3000 /* iterations */
+#define MDM_ACCESS_TIMEOUT     500 /* msec */
 
 static int kinetis_mdm_write_register(struct adiv5_dap *dap, unsigned reg, uint32_t value)
 {
        int retval;
        LOG_DEBUG("MDM_REG[0x%02x] <- %08" PRIX32, reg, value);
 
-       retval = dap_queue_ap_write(dap_ap(dap, 1), reg, value);
+       retval = dap_queue_ap_write(dap_ap(dap, MDM_AP), reg, value);
        if (retval != ERROR_OK) {
                LOG_DEBUG("MDM: failed to queue a write request");
                return retval;
@@ -281,7 +284,7 @@ static int kinetis_mdm_read_register(struct adiv5_dap *dap, unsigned reg, uint32
 {
        int retval;
 
-       retval = dap_queue_ap_read(dap_ap(dap, 1), reg, result);
+       retval = dap_queue_ap_read(dap_ap(dap, MDM_AP), reg, result);
        if (retval != ERROR_OK) {
                LOG_DEBUG("MDM: failed to queue a read request");
                return retval;
@@ -297,11 +300,12 @@ static int kinetis_mdm_read_register(struct adiv5_dap *dap, unsigned reg, uint32
        return ERROR_OK;
 }
 
-static int kinetis_mdm_poll_register(struct adiv5_dap *dap, unsigned reg, uint32_t mask, uint32_t value)
+static int kinetis_mdm_poll_register(struct adiv5_dap *dap, unsigned reg,
+                       uint32_t mask, uint32_t value, uint32_t timeout_ms)
 {
        uint32_t val;
        int retval;
-       int timeout = MDM_ACCESS_TIMEOUT;
+       int64_t ms_timeout = timeval_ms() + timeout_ms;
 
        do {
                retval = kinetis_mdm_read_register(dap, reg, &val);
@@ -309,7 +313,7 @@ static int kinetis_mdm_poll_register(struct adiv5_dap *dap, unsigned reg, uint32
                        return retval;
 
                alive_sleep(1);
-       } while (timeout--);
+       } while (timeval_ms() < ms_timeout);
 
        LOG_DEBUG("MDM: polling timed out");
        return ERROR_FAIL;
@@ -329,6 +333,7 @@ COMMAND_HANDLER(kinetis_mdm_halt)
        int retval;
        int tries = 0;
        uint32_t stat;
+       int64_t ms_timeout = timeval_ms() + MDM_ACCESS_TIMEOUT;
 
        if (!dap) {
                LOG_ERROR("Cannot perform halt with a high-level adapter");
@@ -355,7 +360,7 @@ COMMAND_HANDLER(kinetis_mdm_halt)
                                == (MDM_STAT_FREADY | MDM_STAT_SYSRES))
                        break;
 
-               if (tries > MDM_ACCESS_TIMEOUT) {
+               if (timeval_ms() >= ms_timeout) {
                        LOG_ERROR("MDM: halt timed out");
                        return ERROR_FAIL;
                }
@@ -401,7 +406,7 @@ COMMAND_HANDLER(kinetis_mdm_reset)
                return retval;
        }
 
-       retval = kinetis_mdm_poll_register(dap, MDM_REG_STAT, MDM_STAT_SYSRES, 0);
+       retval = kinetis_mdm_poll_register(dap, MDM_REG_STAT, MDM_STAT_SYSRES, 0, 500);
        if (retval != ERROR_OK) {
                LOG_ERROR("MDM: failed to assert reset");
                return retval;
@@ -457,42 +462,61 @@ COMMAND_HANDLER(kinetis_mdm_mass_erase)
        }
 
        /*
-        * ... Read the MDM-AP status register Mass Erase Enable bit to
-        * determine if the mass erase command is enabled. If Mass Erase
-        * Enable = 0, then mass erase is disabled and the processor
-        * cannot be erased or unsecured. If Mass Erase Enable = 1, then
-        * the mass erase command can be used...
+        * ... Read the MDM-AP status register repeatedly and wait for
+        * stable conditions suitable for mass erase:
+        * - mass erase is enabled
+        * - flash is ready
+        * - reset is finished
+        *
+        * Mass erase is started as soon as all conditions are met in 32
+        * subsequent status reads.
+        *
+        * In case of not stable conditions (RESET/WDOG loop in secured device)
+        * the user is asked for manual pressing of RESET button
+        * as a last resort.
         */
-       uint32_t stat;
+       int cnt_mass_erase_disabled = 0;
+       int cnt_ready = 0;
+       int64_t ms_start = timeval_ms();
+       bool man_reset_requested = false;
 
-       retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &stat);
-       if (retval != ERROR_OK) {
-               LOG_ERROR("MDM: failed to read MDM_REG_STAT");
-               goto deassert_reset_and_exit;
-       }
+       do {
+               uint32_t stat = 0;
+               int64_t ms_elapsed = timeval_ms() - ms_start;
 
-       if (!(stat & MDM_STAT_FMEEN)) {
-               LOG_ERROR("MDM: mass erase is disabled");
-               goto deassert_reset_and_exit;
-       }
+               if (!man_reset_requested && ms_elapsed > 100) {
+                       LOG_INFO("MDM: Press RESET button now if possible.");
+                       man_reset_requested = true;
+               }
 
-       if ((stat & MDM_STAT_SYSSEC) && !(jtag_get_reset_config() & RESET_HAS_SRST)) {
-               LOG_ERROR("Mass erase of a secured MCU is not possible without hardware reset.");
-               LOG_INFO("Connect SRST and use 'reset_config srst_only'.");
-               goto deassert_reset_and_exit;
-       }
+               if (ms_elapsed > 3000) {
+                       LOG_ERROR("MDM: waiting for mass erase conditions timed out.");
+                       LOG_INFO("Mass erase of a secured MCU is not possible without hardware reset.");
+                       LOG_INFO("Connect SRST, use 'reset_config srst_only' and retry.");
+                       goto deassert_reset_and_exit;
+               }
+               retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &stat);
+               if (retval != ERROR_OK) {
+                       cnt_ready = 0;
+                       continue;
+               }
 
-       /*
-        * ... Read the MDM-AP status register until the Flash Ready bit sets
-        * and System Reset is asserted...
-        */
-       retval = kinetis_mdm_poll_register(dap, MDM_REG_STAT,
-                                          MDM_STAT_FREADY | MDM_STAT_SYSRES,
-                                          MDM_STAT_FREADY);
-       if (retval != ERROR_OK) {
-               LOG_ERROR("MDM: flash ready / system reset timeout");
-               goto deassert_reset_and_exit;
-       }
+               if (!(stat & MDM_STAT_FMEEN)) {
+                       cnt_ready = 0;
+                       cnt_mass_erase_disabled++;
+                       if (cnt_mass_erase_disabled > 10) {
+                               LOG_ERROR("MDM: mass erase is disabled");
+                               goto deassert_reset_and_exit;
+                       }
+                       continue;
+               }
+
+               if ((stat & (MDM_STAT_FREADY | MDM_STAT_SYSRES)) == MDM_STAT_FREADY)
+                       cnt_ready++;
+               else
+                       cnt_ready = 0;
+
+       } while (cnt_ready < 32);
 
        /*
         * ... Write the MDM-AP control register to set the Flash Mass
@@ -508,8 +532,10 @@ COMMAND_HANDLER(kinetis_mdm_mass_erase)
        /*
         * ... Read the MDM-AP control register until the Flash Mass
         * Erase in Progress bit clears...
+        * Data sheed defines erase time <3.6 sec/512kB flash block.
+        * The biggest device has 4 pflash blocks => timeout 16 sec.
         */
-       retval = kinetis_mdm_poll_register(dap, MDM_REG_CTRL, MDM_CTRL_FMEIP, 0);
+       retval = kinetis_mdm_poll_register(dap, MDM_REG_CTRL, MDM_CTRL_FMEIP, 0, 16000);
        if (retval != ERROR_OK) {
                LOG_ERROR("MDM: mass erase timeout");
                goto deassert_reset_and_exit;
@@ -575,9 +601,12 @@ COMMAND_HANDLER(kinetis_check_flash_security_status)
        retval = kinetis_mdm_read_register(dap, MDM_REG_ID, &val);
        if (retval != ERROR_OK) {
                LOG_ERROR("MDM: failed to read ID register");
-               goto fail;
+               return ERROR_OK;
        }
 
+       if (val == 0)
+               return ERROR_OK;
+
        bool found = false;
        for (size_t i = 0; i < ARRAY_SIZE(kinetis_known_mdm_ids); i++) {
                if (val == kinetis_known_mdm_ids[i]) {
@@ -589,17 +618,6 @@ COMMAND_HANDLER(kinetis_check_flash_security_status)
        if (!found)
                LOG_WARNING("MDM: unknown ID %08" PRIX32, val);
 
-       /*
-        * ... Read the MDM-AP status register until the Flash Ready bit sets...
-        */
-       retval = kinetis_mdm_poll_register(dap, MDM_REG_STAT,
-                                          MDM_STAT_FREADY,
-                                          MDM_STAT_FREADY);
-       if (retval != ERROR_OK) {
-               LOG_ERROR("MDM: flash ready timeout");
-               goto fail;
-       }
-
        /*
         * ... Read the System Security bit to determine if security is enabled.
         * If System Security = 0, then proceed. If System Security = 1, then
@@ -610,33 +628,40 @@ COMMAND_HANDLER(kinetis_check_flash_security_status)
        retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &val);
        if (retval != ERROR_OK) {
                LOG_ERROR("MDM: failed to read MDM_REG_STAT");
-               goto fail;
+               return ERROR_OK;
        }
 
-       if ((val & (MDM_STAT_SYSSEC | MDM_STAT_CORE_HALTED)) == MDM_STAT_SYSSEC) {
-               LOG_WARNING("MDM: Secured MCU state detected however it may be a false alarm");
-               LOG_WARNING("MDM: Halting target to detect secured state reliably");
+       /*
+        * System Security bit is also active for short time during reset.
+        * If a MCU has blank flash and runs in RESET/WDOG loop,
+        * System Security bit is active most of time!
+        * We should observe Flash Ready bit and read status several times
+        * to avoid false detection of secured MCU
+        */
+       int secured_score = 0, flash_not_ready_score = 0;
 
-               retval = target_halt(target);
-               if (retval == ERROR_OK)
-                       retval = target_wait_state(target, TARGET_HALTED, 100);
+       if ((val & (MDM_STAT_SYSSEC | MDM_STAT_FREADY)) != MDM_STAT_FREADY) {
+               uint32_t stats[32];
+               int i;
 
-               if (retval != ERROR_OK) {
-                       LOG_WARNING("MDM: Target not halted, trying reset halt");
-                       target->reset_halt = true;
-                       target->type->assert_reset(target);
-                       target->type->deassert_reset(target);
+               for (i = 0; i < 32; i++) {
+                       stats[i] = MDM_STAT_FREADY;
+                       dap_queue_ap_read(dap_ap(dap, MDM_AP), MDM_REG_STAT, &stats[i]);
                }
-
-               /* re-read status */
-               retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &val);
+               retval = dap_run(dap);
                if (retval != ERROR_OK) {
-                       LOG_ERROR("MDM: failed to read MDM_REG_STAT");
-                       goto fail;
+                       LOG_DEBUG("MDM: dap_run failed when validating secured state");
+                       return ERROR_OK;
+               }
+               for (i = 0; i < 32; i++) {
+                       if (stats[i] & MDM_STAT_SYSSEC)
+                               secured_score++;
+                       if (!(stats[i] & MDM_STAT_FREADY))
+                               flash_not_ready_score++;
                }
        }
 
-       if (val & MDM_STAT_SYSSEC) {
+       if (flash_not_ready_score <= 8 && secured_score > 24) {
                jtag_poll_set_enabled(false);
 
                LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
@@ -648,17 +673,22 @@ COMMAND_HANDLER(kinetis_check_flash_security_status)
                LOG_WARNING("**** command, power cycle the MCU and restart OpenOCD.        ****");
                LOG_WARNING("****                                                          ****");
                LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
+
+       } else if (flash_not_ready_score > 24) {
+               jtag_poll_set_enabled(false);
+               LOG_WARNING("**** Your Kinetis MCU is probably locked-up in RESET/WDOG loop. ****");
+               LOG_WARNING("**** Common reason is a blank flash (at least a reset vector).  ****");
+               LOG_WARNING("**** Issue 'kinetis mdm halt' command or if SRST is connected   ****");
+               LOG_WARNING("**** and configured, use 'reset halt'                           ****");
+               LOG_WARNING("**** If MCU cannot be halted, it is likely secured and running  ****");
+               LOG_WARNING("**** in RESET/WDOG loop. Issue 'kinetis mdm mass_erase'         ****");
+
        } else {
                LOG_INFO("MDM: Chip is unsecured. Continuing.");
                jtag_poll_set_enabled(true);
        }
 
        return ERROR_OK;
-
-fail:
-       LOG_ERROR("MDM: Failed to check security status of the MCU. Cannot proceed further");
-       jtag_poll_set_enabled(false);
-       return retval;
 }
 
 FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
@@ -764,6 +794,54 @@ COMMAND_HANDLER(kinetis_disable_wdog_handler)
 }
 
 
+static int kinetis_ftfx_decode_error(uint8_t fstat)
+{
+       if (fstat & 0x20) {
+               LOG_ERROR("Flash operation failed, illegal command");
+               return ERROR_FLASH_OPER_UNSUPPORTED;
+
+       } else if (fstat & 0x10)
+               LOG_ERROR("Flash operation failed, protection violated");
+
+       else if (fstat & 0x40)
+               LOG_ERROR("Flash operation failed, read collision");
+
+       else if (fstat & 0x80)
+               return ERROR_OK;
+
+       else
+               LOG_ERROR("Flash operation timed out");
+
+       return ERROR_FLASH_OPERATION_FAILED;
+}
+
+
+static int kinetis_ftfx_prepare(struct target *target)
+{
+       int result, i;
+       uint8_t fstat;
+
+       /* wait until busy */
+       for (i = 0; i < 50; i++) {
+               result = target_read_u8(target, FTFx_FSTAT, &fstat);
+               if (result != ERROR_OK)
+                       return result;
+
+               if (fstat & 0x80)
+                       break;
+       }
+
+       if ((fstat & 0x80) == 0) {
+               LOG_ERROR("Flash controller is busy");
+               return ERROR_FLASH_OPERATION_FAILED;
+       }
+       if (fstat != 0x80) {
+               /* reset error flags */
+               result = target_write_u8(target, FTFx_FSTAT, 0x70);
+       }
+       return result;
+}
+
 /* Kinetis Program-LongWord Microcodes */
 static const uint8_t kinetis_flash_write_code[] = {
        /* Params:
@@ -856,14 +934,6 @@ static int kinetis_write_block(struct flash_bank *bank, const uint8_t *buffer,
        if (buffer_size < (target->working_area_size/2))
                buffer_size = (target->working_area_size/2);
 
-       LOG_INFO("Kinetis: FLASH Write ...");
-
-       /* check code alignment */
-       if (offset & 0x1) {
-               LOG_WARNING("offset 0x%" PRIx32 " breaks required 2-byte alignment", offset);
-               return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
-       }
-
        /* allocate working area with flash programming code */
        if (target_alloc_working_area(target, sizeof(kinetis_flash_write_code),
                        &write_algorithm) != ERROR_OK) {
@@ -957,23 +1027,19 @@ static int kinetis_protect_check(struct flash_bank *bank)
        }
 
        if (kinfo->flash_class == FC_PFLASH) {
-               uint8_t buffer[4];
 
                /* read protection register */
-               result = target_read_memory(bank->target, FTFx_FPROT3, 1, 4, buffer);
-
+               result = target_read_u32(bank->target, FTFx_FPROT3, &fprot);
                if (result != ERROR_OK)
                        return result;
 
-               fprot = target_buffer_get_u32(bank->target, buffer);
                /* Every bit protects 1/32 of the full flash (not necessarily just this bank) */
 
        } else if (kinfo->flash_class == FC_FLEX_NVM) {
                uint8_t fdprot;
 
                /* read protection register */
-               result = target_read_memory(bank->target, FTFx_FDPROT, 1, 1, &fdprot);
-
+               result = target_read_u8(bank->target, FTFx_FDPROT, &fdprot);
                if (result != ERROR_OK)
                        return result;
 
@@ -1010,62 +1076,41 @@ static int kinetis_ftfx_command(struct target *target, uint8_t fcmd, uint32_t fa
        uint8_t command[12] = {faddr & 0xff, (faddr >> 8) & 0xff, (faddr >> 16) & 0xff, fcmd,
                        fccob7, fccob6, fccob5, fccob4,
                        fccobb, fccoba, fccob9, fccob8};
-       int result, i;
-       uint8_t buffer;
-
-       /* wait for done */
-       for (i = 0; i < 50; i++) {
-               result =
-                       target_read_memory(target, FTFx_FSTAT, 1, 1, &buffer);
-
-               if (result != ERROR_OK)
-                       return result;
-
-               if (buffer & 0x80)
-                       break;
-
-               buffer = 0x00;
-       }
-
-       if (buffer != 0x80) {
-               /* reset error flags */
-               buffer = 0x30;
-               result =
-                       target_write_memory(target, FTFx_FSTAT, 1, 1, &buffer);
-               if (result != ERROR_OK)
-                       return result;
-       }
+       int result;
+       uint8_t fstat;
+       int64_t ms_timeout = timeval_ms() + 250;
 
        result = target_write_memory(target, FTFx_FCCOB3, 4, 3, command);
-
        if (result != ERROR_OK)
                return result;
 
        /* start command */
-       buffer = 0x80;
-       result = target_write_memory(target, FTFx_FSTAT, 1, 1, &buffer);
+       result = target_write_u8(target, FTFx_FSTAT, 0x80);
        if (result != ERROR_OK)
                return result;
 
        /* wait for done */
-       for (i = 0; i < 240; i++) { /* Need longtime for "Mass Erase" Command Nemui Changed */
-               result =
-                       target_read_memory(target, FTFx_FSTAT, 1, 1, ftfx_fstat);
+       do {
+               result = target_read_u8(target, FTFx_FSTAT, &fstat);
 
                if (result != ERROR_OK)
                        return result;
 
-               if (*ftfx_fstat & 0x80)
+               if (fstat & 0x80)
                        break;
-       }
 
-       if ((*ftfx_fstat & 0xf0) != 0x80) {
-               LOG_ERROR
-                       ("ftfx command failed FSTAT: %02X FCCOB: %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X",
-                        *ftfx_fstat, command[3], command[2], command[1], command[0],
+       } while (timeval_ms() < ms_timeout);
+
+       if (ftfx_fstat)
+               *ftfx_fstat = fstat;
+
+       if ((fstat & 0xf0) != 0x80) {
+               LOG_DEBUG("ftfx command failed FSTAT: %02X FCCOB: %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X",
+                        fstat, command[3], command[2], command[1], command[0],
                         command[7], command[6], command[5], command[4],
                         command[11], command[10], command[9], command[8]);
-               return ERROR_FLASH_OPERATION_FAILED;
+
+               return kinetis_ftfx_decode_error(fstat);
        }
 
        return ERROR_OK;
@@ -1135,6 +1180,11 @@ static int kinetis_erase(struct flash_bank *bank, int first, int last)
        if (result != ERROR_OK)
                return result;
 
+       /* reset error flags */
+       result = kinetis_ftfx_prepare(bank->target);
+       if (result != ERROR_OK)
+               return result;
+
        if ((first > bank->num_sectors) || (last > bank->num_sectors))
                return ERROR_FLASH_OPERATION_FAILED;
 
@@ -1144,10 +1194,9 @@ static int kinetis_erase(struct flash_bank *bank, int first, int last)
         * block.  Should be quicker.
         */
        for (i = first; i <= last; i++) {
-               uint8_t ftfx_fstat;
                /* set command and sector address */
                result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTERASE, kinfo->prog_base + bank->sectors[i].offset,
-                               0, 0, 0, 0,  0, 0, 0, 0,  &ftfx_fstat);
+                               0, 0, 0, 0,  0, 0, 0, 0,  NULL);
 
                if (result != ERROR_OK) {
                        LOG_WARNING("erase sector %d failed", i);
@@ -1170,11 +1219,10 @@ static int kinetis_erase(struct flash_bank *bank, int first, int last)
 static int kinetis_make_ram_ready(struct target *target)
 {
        int result;
-       uint8_t ftfx_fstat;
        uint8_t ftfx_fcnfg;
 
        /* check if ram ready */
-       result = target_read_memory(target, FTFx_FCNFG, 1, 1, &ftfx_fcnfg);
+       result = target_read_u8(target, FTFx_FCNFG, &ftfx_fcnfg);
        if (result != ERROR_OK)
                return result;
 
@@ -1183,12 +1231,12 @@ static int kinetis_make_ram_ready(struct target *target)
 
        /* make flex ram available */
        result = kinetis_ftfx_command(target, FTFx_CMD_SETFLEXRAM, 0x00ff0000,
-                                0, 0, 0, 0,  0, 0, 0, 0,  &ftfx_fstat);
+                                0, 0, 0, 0,  0, 0, 0, 0,  NULL);
        if (result != ERROR_OK)
                return ERROR_FLASH_OPERATION_FAILED;
 
        /* check again */
-       result = target_read_memory(target, FTFx_FCNFG, 1, 1, &ftfx_fcnfg);
+       result = target_read_u8(target, FTFx_FCNFG, &ftfx_fcnfg);
        if (result != ERROR_OK)
                return result;
 
@@ -1201,15 +1249,20 @@ static int kinetis_make_ram_ready(struct target *target)
 static int kinetis_write(struct flash_bank *bank, const uint8_t *buffer,
                         uint32_t offset, uint32_t count)
 {
-       unsigned int i, result, fallback = 0;
+       unsigned int i;
+       int result, fallback = 0;
        uint32_t wc;
        struct kinetis_flash_bank *kinfo = bank->driver_priv;
-       uint8_t *new_buffer = NULL;
 
        result = kinetis_check_run_mode(bank->target);
        if (result != ERROR_OK)
                return result;
 
+       /* reset error flags */
+       result = kinetis_ftfx_prepare(bank->target);
+       if (result != ERROR_OK)
+               return result;
+
        if (!(kinfo->flash_support & FS_PROGRAM_SECTOR)) {
                /* fallback to longword write */
                fallback = 1;
@@ -1222,7 +1275,7 @@ static int kinetis_write(struct flash_bank *bank, const uint8_t *buffer,
                }
        }
 
-       LOG_DEBUG("flash write @08%" PRIX32, offset);
+       LOG_DEBUG("flash write @08%" PRIx32, bank->base + offset);
 
 
        /* program section command */
@@ -1306,8 +1359,16 @@ static int kinetis_write(struct flash_bank *bank, const uint8_t *buffer,
                                return ERROR_FLASH_OPERATION_FAILED;
                }
        }
-       /* program longword command, not supported in "SF3" devices */
        else if (kinfo->flash_support & FS_PROGRAM_LONGWORD) {
+               /* program longword command, not supported in FTFE */
+               uint8_t *new_buffer = NULL;
+
+               /* check word alignment */
+               if (offset & 0x3) {
+                       LOG_ERROR("offset 0x%" PRIx32 " breaks the required alignment", offset);
+                       return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
+               }
+
                if (count & 0x3) {
                        uint32_t old_count = count;
                        count = (old_count | 3) + 1;
@@ -1319,7 +1380,7 @@ static int kinetis_write(struct flash_bank *bank, const uint8_t *buffer,
                        }
                        LOG_INFO("odd number of bytes to write (%" PRIu32 "), extending to %" PRIu32 " "
                                "and padding with 0xff", old_count, count);
-                       memset(new_buffer, 0xff, count);
+                       memset(new_buffer + old_count, 0xff, count - old_count);
                        buffer = memcpy(new_buffer, buffer, old_count);
                }
 
@@ -1328,39 +1389,47 @@ static int kinetis_write(struct flash_bank *bank, const uint8_t *buffer,
                kinetis_disable_wdog(bank->target, kinfo->sim_sdid);
 
                /* try using a block write */
-               int retval = kinetis_write_block(bank, buffer, offset, words_remaining);
+               result = kinetis_write_block(bank, buffer, offset, words_remaining);
 
-               if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
+               if (result == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
                        /* if block write failed (no sufficient working area),
                         * we use normal (slow) single word accesses */
                        LOG_WARNING("couldn't use block writes, falling back to single "
                                "memory accesses");
 
-                       for (i = 0; i < count; i += 4) {
+                       while (words_remaining) {
                                uint8_t ftfx_fstat;
 
-                               LOG_DEBUG("write longword @ %08" PRIX32, (uint32_t)(offset + i));
+                               LOG_DEBUG("write longword @ %08" PRIx32, (uint32_t)(bank->base + offset));
 
-                               uint8_t padding[4] = {0xff, 0xff, 0xff, 0xff};
-                               memcpy(padding, buffer + i, MIN(4, count-i));
-
-                               result = kinetis_ftfx_command(bank->target, FTFx_CMD_LWORDPROG, kinfo->prog_base + offset + i,
-                                               padding[3], padding[2], padding[1], padding[0],
+                               result = kinetis_ftfx_command(bank->target, FTFx_CMD_LWORDPROG, kinfo->prog_base + offset,
+                                               buffer[3], buffer[2], buffer[1], buffer[0],
                                                0, 0, 0, 0,  &ftfx_fstat);
 
-                               if (result != ERROR_OK)
-                                       return ERROR_FLASH_OPERATION_FAILED;
+                               if (result != ERROR_OK) {
+                                       LOG_ERROR("Error writing longword at %08" PRIx32, bank->base + offset);
+                                       break;
+                               }
+
+                               if (ftfx_fstat & 0x01)
+                                       LOG_ERROR("Flash write error at %08" PRIx32, bank->base + offset);
+
+                               buffer += 4;
+                               offset += 4;
+                               words_remaining--;
                        }
                }
+               free(new_buffer);
        } else {
                LOG_ERROR("Flash write strategy not implemented");
                return ERROR_FLASH_OPERATION_FAILED;
        }
 
        kinetis_invalidate_flash_cache(bank);
-       return ERROR_OK;
+       return result;
 }
 
+
 static int kinetis_probe(struct flash_bank *bank)
 {
        int result, i;
@@ -1864,6 +1933,11 @@ static int kinetis_blank_check(struct flash_bank *bank)
        if (result != ERROR_OK)
                return result;
 
+       /* reset error flags */
+       result = kinetis_ftfx_prepare(bank->target);
+       if (result != ERROR_OK)
+               return result;
+
        if (kinfo->flash_class == FC_PFLASH || kinfo->flash_class == FC_FLEX_NVM) {
                bool block_dirty = false;
                uint8_t ftfx_fstat;
@@ -1921,7 +1995,6 @@ COMMAND_HANDLER(kinetis_nvm_partition)
        unsigned long par, log2 = 0, ee1 = 0, ee2 = 0;
        enum { SHOW_INFO, DF_SIZE, EEBKP_SIZE } sz_type = SHOW_INFO;
        bool enable;
-       uint8_t ftfx_fstat;
        uint8_t load_flex_ram = 1;
        uint8_t ee_size_code = 0x3f;
        uint8_t flex_nvm_partition_code = 0;
@@ -2030,9 +2103,14 @@ COMMAND_HANDLER(kinetis_nvm_partition)
        if (result != ERROR_OK)
                return result;
 
+       /* reset error flags */
+       result = kinetis_ftfx_prepare(target);
+       if (result != ERROR_OK)
+               return result;
+
        result = kinetis_ftfx_command(target, FTFx_CMD_PGMPART, load_flex_ram,
                                      ee_size_code, flex_nvm_partition_code, 0, 0,
-                                     0, 0, 0, 0,  &ftfx_fstat);
+                                     0, 0, 0, 0,  NULL);
        if (result != ERROR_OK)
                return result;
 

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)