flash/nor/nrf5: show RAM size on old nRF51 devices (rev 1, 2)
[openocd.git] / src / flash / nor / niietcm4.c
index 78467c5a231f318836dddfc150dbd1861efbef33..7b67bb8ad24d719e321c3b6b7a87a735bd5f5340 100644 (file)
@@ -13,8 +13,7 @@
  *   GNU General Public License for more details.                          *
  *                                                                         *
  *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.                                        *
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -155,7 +154,7 @@ static int niietcm4_opstatus_check(struct flash_bank *bank)
 {
        struct target *target = bank->target;
        int retval;
-       int timeout = 100;
+       int timeout = 5000;
 
        uint32_t flash_status;
        retval = target_read_u32(target, FCIS, &flash_status);
@@ -192,7 +191,7 @@ static int niietcm4_uopstatus_check(struct flash_bank *bank)
 {
        struct target *target = bank->target;
        int retval;
-       int timeout = 100;
+       int timeout = 5000;
 
        uint32_t uflash_status;
        retval = target_read_u32(target, UFCIS, &uflash_status);
@@ -231,7 +230,8 @@ static int niietcm4_uopstatus_check(struct flash_bank *bank)
 static int niietcm4_dump_uflash_page(struct flash_bank *bank, uint32_t *dump, int page_num, int mem_type)
 {
        struct target *target = bank->target;
-       int i, retval;
+       int i;
+       int retval = ERROR_OK;
 
        uint32_t uflash_cmd;
        if (mem_type == INFO_MEM_TYPE)
@@ -266,7 +266,8 @@ static int niietcm4_dump_uflash_page(struct flash_bank *bank, uint32_t *dump, in
 static int niietcm4_load_uflash_page(struct flash_bank *bank, uint32_t *dump, int page_num, int mem_type)
 {
        struct target *target = bank->target;
-       int i, retval;
+       int i;
+       int retval = ERROR_OK;
 
        uint32_t uflash_cmd;
        if (mem_type == INFO_MEM_TYPE)
@@ -424,9 +425,9 @@ COMMAND_HANDLER(niietcm4_handle_uflash_read_byte_command)
        retval = target_read_u32(target, UFMD, &uflash_data);
        if (retval != ERROR_OK)
                return retval;
-       command_print(CMD_CTX,  "Read userflash %s region:\n"
-                                                       "address = 0x%04x,\n"
-                                                       "value   = 0x%02x.", CMD_ARGV[0], uflash_addr, uflash_data);
+       command_print(CMD,  "Read userflash %s region:\n"
+                                               "address = 0x%04x,\n"
+                                               "value   = 0x%02x.", CMD_ARGV[0], uflash_addr, uflash_data);
        return retval;
 }
 
@@ -466,10 +467,10 @@ COMMAND_HANDLER(niietcm4_handle_uflash_write_byte_command)
 
        int page_num = uflash_addr/USERFLASH_PAGE_SIZE;
 
-       command_print(CMD_CTX, "Write userflash %s region:\n"
-                                                  "address = 0x%04x,\n"
-                                                  "value   = 0x%02x.\n"
-                                                  "Please wait ... ", CMD_ARGV[0], uflash_addr, uflash_data);
+       command_print(CMD, "Write userflash %s region:\n"
+                                          "address = 0x%04x,\n"
+                                          "value   = 0x%02x.\n"
+                                          "Please wait ... ", CMD_ARGV[0], uflash_addr, uflash_data);
        /* dump */
        uint32_t uflash_dump[USERFLASH_PAGE_SIZE];
        niietcm4_dump_uflash_page(bank, uflash_dump, page_num, mem_type);
@@ -482,7 +483,7 @@ COMMAND_HANDLER(niietcm4_handle_uflash_write_byte_command)
 
        /* write dump to userflash */
        niietcm4_load_uflash_page(bank, uflash_dump, page_num, mem_type);
-       command_print(CMD_CTX, "done!");
+       command_print(CMD, "done!");
        return retval;
 }
 
@@ -519,7 +520,7 @@ COMMAND_HANDLER(niietcm4_handle_uflash_full_erase_command)
        retval = niietcm4_uopstatus_check(bank);
        if (retval != ERROR_OK)
                return retval;
-       command_print(CMD_CTX, "Userflash full erase done!");
+       command_print(CMD, "Userflash full erase done!");
 
        return retval;
 }
@@ -562,7 +563,7 @@ COMMAND_HANDLER(niietcm4_handle_uflash_erase_command)
                        return retval;
        }
 
-       command_print(CMD_CTX, "Erase %s userflash pages %d through %d done!", CMD_ARGV[0], first, last);
+       command_print(CMD, "Erase %s userflash pages %d through %d done!", CMD_ARGV[0], first, last);
 
        return retval;
 }
@@ -620,9 +621,9 @@ COMMAND_HANDLER(niietcm4_handle_uflash_protect_check_command)
                        return retval;
 
                if (uflash_data & INFOWORD3_LOCK_IFB_UF)
-                       command_print(CMD_CTX, "All sectors of info userflash are not protected!");
+                       command_print(CMD, "All sectors of info userflash are not protected!");
                else
-                       command_print(CMD_CTX, "All sectors of info userflash are protected!");
+                       command_print(CMD, "All sectors of info userflash are protected!");
        } else {
                uflash_addr = UF_LOCK_ADDR;
                uflash_cmd = UFMC_MAGIC_KEY | UFMC_READ_IFB;
@@ -644,10 +645,10 @@ COMMAND_HANDLER(niietcm4_handle_uflash_protect_check_command)
 
                        for (j = 0; j < 8; j++) {
                                if (uflash_data & 0x1)
-                                       command_print(CMD_CTX, "Userflash sector #%03d: 0x%04x (0x100) is not protected!",
+                                       command_print(CMD, "Userflash sector #%03d: 0x%04x (0x100) is not protected!",
                                                                                        i*8+j, (i*8+j)*USERFLASH_PAGE_SIZE);
                                else
-                                       command_print(CMD_CTX, "Userflash sector #%03d: 0x%04x (0x100) is protected!",
+                                       command_print(CMD, "Userflash sector #%03d: 0x%04x (0x100) is protected!",
                                                                                        i*8+j, (i*8+j)*USERFLASH_PAGE_SIZE);
                                uflash_data = uflash_data >> 1;
                        }
@@ -692,11 +693,11 @@ COMMAND_HANDLER(niietcm4_handle_uflash_protect_command)
 
        int set;
        if (strcmp("on", CMD_ARGV[3]) == 0) {
-               command_print(CMD_CTX, "Try to enable %s userflash sectors %d through %d protection. Please wait ... ",
+               command_print(CMD, "Try to enable %s userflash sectors %d through %d protection. Please wait ... ",
                                                                CMD_ARGV[0], first, last);
                set = 1;
        } else if (strcmp("off", CMD_ARGV[3]) == 0) {
-               command_print(CMD_CTX, "Try to disable %s userflash sectors %d through %d protection. Please wait ... ",
+               command_print(CMD, "Try to disable %s userflash sectors %d through %d protection. Please wait ... ",
                                                                CMD_ARGV[0], first, last);
                set = 0;
        } else
@@ -706,7 +707,7 @@ COMMAND_HANDLER(niietcm4_handle_uflash_protect_command)
                if (retval != ERROR_OK)
                        return retval;
 
-       command_print(CMD_CTX, "done!");
+       command_print(CMD, "done!");
        return retval;
 }
 
@@ -732,10 +733,10 @@ COMMAND_HANDLER(niietcm4_handle_bflash_info_remap_command)
 
        int set;
        if (strcmp("on", CMD_ARGV[0]) == 0) {
-               command_print(CMD_CTX, "Try to enable bootflash info region remap. Please wait ...");
+               command_print(CMD, "Try to enable bootflash info region remap. Please wait ...");
                set = 1;
        } else if (strcmp("off", CMD_ARGV[0]) == 0) {
-               command_print(CMD_CTX, "Try to disable bootflash info region remap. Please wait ...");
+               command_print(CMD, "Try to disable bootflash info region remap. Please wait ...");
                set = 0;
        } else
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -755,7 +756,7 @@ COMMAND_HANDLER(niietcm4_handle_bflash_info_remap_command)
 
        /* write dump to userflash */
        niietcm4_load_uflash_page(bank, uflash_dump, 0, 1);
-       command_print(CMD_CTX, "done!");
+       command_print(CMD, "done!");
 
        return retval;
 }
@@ -813,11 +814,11 @@ COMMAND_HANDLER(niietcm4_handle_extmem_cfg_command)
        else
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       command_print(CMD_CTX,  "Try to configure external memory boot interface:\n"
-                                                       "port = %s\n"
-                                                       "pin  = %s\n"
-                                                       "func = %s\n"
-                                                       "Please wait ...", CMD_ARGV[0], CMD_ARGV[1], CMD_ARGV[2]);
+       command_print(CMD,  "Try to configure external memory boot interface:\n"
+                                               "port = %s\n"
+                                               "pin  = %s\n"
+                                               "func = %s\n"
+                                               "Please wait ...", CMD_ARGV[0], CMD_ARGV[1], CMD_ARGV[2]);
        /* dump */
        uint32_t uflash_dump[USERFLASH_PAGE_SIZE];
        niietcm4_dump_uflash_page(bank, uflash_dump, 0, 1);
@@ -832,7 +833,7 @@ COMMAND_HANDLER(niietcm4_handle_extmem_cfg_command)
 
        /* write dump to userflash */
        niietcm4_load_uflash_page(bank, uflash_dump, 0, 1);
-       command_print(CMD_CTX, "done!");
+       command_print(CMD, "done!");
 
        return retval;
 }
@@ -860,10 +861,10 @@ COMMAND_HANDLER(niietcm4_handle_extmem_boot_command)
        int set;
 
        if (strcmp("on", CMD_ARGV[0]) == 0) {
-               command_print(CMD_CTX, "Try to enable boot from external memory. Please wait ...");
+               command_print(CMD, "Try to enable boot from external memory. Please wait ...");
                set = 1;
        } else if (strcmp("off", CMD_ARGV[0]) == 0) {
-               command_print(CMD_CTX, "Try to disable boot from external memory. Please wait ...");
+               command_print(CMD, "Try to disable boot from external memory. Please wait ...");
                set = 0;
        } else
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -883,7 +884,7 @@ COMMAND_HANDLER(niietcm4_handle_extmem_boot_command)
 
        /* write dump to userflash */
        niietcm4_load_uflash_page(bank, uflash_dump, 0, 1);
-       command_print(CMD_CTX, "done!");
+       command_print(CMD, "done!");
 
        return retval;
 }
@@ -899,7 +900,7 @@ COMMAND_HANDLER(niietcm4_handle_service_mode_erase_command)
                return retval;
        struct target *target = bank->target;
 
-       command_print(CMD_CTX, "Try to perform service mode erase. Please wait ...");
+       command_print(CMD, "Try to perform service mode erase. Please wait ...");
 
        retval = target_write_u32(target, SERVICE_MODE_ERASE_ADDR, 1);
        if (retval != ERROR_OK)
@@ -922,7 +923,7 @@ COMMAND_HANDLER(niietcm4_handle_service_mode_erase_command)
                        }
                busy_sleep(1);  /* can use busy sleep for short times. */
        }
-       command_print(CMD_CTX, "done! All data erased.");
+       command_print(CMD, "done! All data erased.");
 
        return retval;
 }
@@ -937,7 +938,7 @@ COMMAND_HANDLER(niietcm4_handle_driver_info_command)
        if (retval != ERROR_OK)
                return retval;
 
-       command_print(CMD_CTX, "niietcm4 flash driver\n"
+       command_print(CMD, "niietcm4 flash driver\n"
                                                   "version: %d.%d\n"
                                                   "author: Bogdan Kolbov\n"
                                                   "mail: kolbov@niiet.ru",
@@ -1048,7 +1049,7 @@ FLASH_BANK_COMMAND_HANDLER(niietcm4_flash_bank_command)
 {
        struct niietcm4_flash_bank *niietcm4_info;
 
-       if (CMD_ARGC < 7)
+       if (CMD_ARGC < 6)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
        niietcm4_info = malloc(sizeof(struct niietcm4_flash_bank));
@@ -1299,7 +1300,7 @@ static int niietcm4_write_block(struct flash_bank *bank, const uint8_t *buffer,
                        &write_algorithm) != ERROR_OK) {
                LOG_WARNING("no working area available, can't do block memory writes");
                return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
-       };
+       }
 
        retval = target_write_buffer(target, write_algorithm->address,
                        sizeof(niietcm4_flash_write_code), niietcm4_flash_write_code);
@@ -1319,7 +1320,7 @@ static int niietcm4_write_block(struct flash_bank *bank, const uint8_t *buffer,
                        LOG_WARNING("no large enough working area available, can't do block memory writes");
                        return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
                }
-       };
+       }
 
        init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); /* write_cmd base (in), status (out) */
        init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);    /* count (128bit) */
@@ -1370,17 +1371,38 @@ static int niietcm4_write(struct flash_bank *bank, const uint8_t *buffer,
 {
        struct target *target = bank->target;
        struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
+       uint8_t *new_buffer = NULL;
 
        if (bank->target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
 
-       if (offset & 0x3) {
-               LOG_ERROR("offset 0x%" PRIx32 " breaks required 4-byte alignment", offset);
+       if (offset & 0xF) {
+               LOG_ERROR("offset 0x%" PRIx32 " breaks required 4-word alignment", offset);
                return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
        }
 
+       /* If there's an odd number of words, the data has to be padded. Duplicate
+        * the buffer and use the normal code path with a single block write since
+        * it's probably cheaper than to special case the last odd write using
+        * discrete accesses. */
+
+       int rem = count % 16;
+       if (rem) {
+               new_buffer = malloc(count + 16 - rem);
+               if (new_buffer == NULL) {
+                       LOG_ERROR("Odd number of words to write and no memory for padding buffer");
+                       return ERROR_FAIL;
+               }
+               LOG_INFO("Odd number of words to write, padding with 0xFFFFFFFF");
+               buffer = memcpy(new_buffer, buffer, count);
+               while (rem < 16) {
+                       new_buffer[count++] = 0xff;
+                       rem++;
+               }
+       }
+
        int retval;
 
        /* try using block write */
@@ -1406,46 +1428,47 @@ static int niietcm4_write(struct flash_bank *bank, const uint8_t *buffer,
                        flash_addr = offset + i;
                        retval = target_write_u32(target, FMA, flash_addr);
                        if (retval != ERROR_OK)
-                               return retval;
+                               goto free_buffer;
 
-                       /* if there's an odd number of bytes, the data has to be padded */
-                       uint8_t padding[16] = { 0xff, 0xff, 0xff, 0xff,
-                                                                       0xff, 0xff, 0xff, 0xff,
-                                                                       0xff, 0xff, 0xff, 0xff,
-                                                                       0xff, 0xff, 0xff, 0xff};
-                       memcpy(padding, buffer + i, MIN(16, count-i));
+                       /* Prepare data (4 words) */
+                       uint32_t value[4];
+                       memcpy(&value, buffer + i*16, 4*sizeof(uint32_t));
 
                        /* place in reg 16 bytes of data */
-                       flash_data = (padding[3]<<24) | (padding[2]<<16) | (padding[1]<<8) | padding[0];
+                       flash_data = value[0];
                        retval = target_write_u32(target, FMD1, flash_data);
                        if (retval != ERROR_OK)
-                               return retval;
-                       flash_data = (padding[7]<<24) | (padding[6]<<16) | (padding[5]<<8) | padding[4];
+                               goto free_buffer;
+                       flash_data = value[1];
                        retval = target_write_u32(target, FMD2, flash_data);
                        if (retval != ERROR_OK)
-                               return retval;
-                       flash_data = (padding[11]<<24) | (padding[10]<<16) | (padding[9]<<8) | padding[8];
+                               goto free_buffer;
+                       flash_data = value[2];
                        retval = target_write_u32(target, FMD3, flash_data);
                        if (retval != ERROR_OK)
-                               return retval;
-                       flash_data = (padding[15]<<24) | (padding[14]<<16) | (padding[13]<<8) | padding[12];
+                               goto free_buffer;
+                       flash_data = value[3];
                        retval = target_write_u32(target, FMD4, flash_data);
                        if (retval != ERROR_OK)
-                               return retval;
+                               goto free_buffer;
 
                        /* write start */
                        retval = target_write_u32(target, FMC, flash_cmd);
                        if (retval != ERROR_OK)
-                               return retval;
+                               goto free_buffer;
 
                        /* status check */
                        retval = niietcm4_opstatus_check(bank);
                        if (retval != ERROR_OK)
-                               return retval;
+                               goto free_buffer;
                }
 
        }
 
+free_buffer:
+       if (new_buffer)
+               free(new_buffer);
+
        return retval;
 }
 
@@ -1561,9 +1584,11 @@ static int niietcm4_probe_k1921vk01t(struct flash_bank *bank)
 
                char info_bootflash_addr_str[64];
                if (niietcm4_info->bflash_info_remap)
-                       snprintf(info_bootflash_addr_str, sizeof(info_bootflash_addr_str), "0x%08x base adress", bank->base);
+                       snprintf(info_bootflash_addr_str, sizeof(info_bootflash_addr_str),
+                                       TARGET_ADDR_FMT " base adress", bank->base);
                else
-                       snprintf(info_bootflash_addr_str, sizeof(info_bootflash_addr_str), "not maped to global adress space");
+                       snprintf(info_bootflash_addr_str, sizeof(info_bootflash_addr_str),
+                                       "not mapped to global adress space");
 
                snprintf(niietcm4_info->chip_brief,
                                sizeof(niietcm4_info->chip_brief),
@@ -1697,14 +1722,14 @@ static int niietcm4_auto_probe(struct flash_bank *bank)
 static int get_niietcm4_info(struct flash_bank *bank, char *buf, int buf_size)
 {
        struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
-       LOG_INFO("\nNIIET Cortex M4F %s\n%s", niietcm4_info->chip_name, niietcm4_info->chip_brief);
+       LOG_INFO("\nNIIET Cortex-M4F %s\n%s", niietcm4_info->chip_name, niietcm4_info->chip_brief);
        snprintf(buf, buf_size, " ");
 
        return ERROR_OK;
 }
 
 
-struct flash_driver niietcm4_flash = {
+const struct flash_driver niietcm4_flash = {
        .name = "niietcm4",
        .usage = "flash bank <name> niietcm4 <base> <size> 0 0 <target#>",
        .commands = niietcm4_command_handlers,
@@ -1718,4 +1743,5 @@ struct flash_driver niietcm4_flash = {
        .erase_check = default_flash_blank_check,
        .protect_check = niietcm4_protect_check,
        .info = get_niietcm4_info,
+       .free_driver_priv = default_flash_free_driver_priv,
 };

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)