flash/stm32l4x: fix scan-build warnings
[openocd.git] / src / flash / nor / kinetis.c
index 45046d6072463f80054c4ae69a99784c1ca23756..edb4eb58fedddc8fac483a0d33e9769745eecf04 100644 (file)
@@ -38,6 +38,7 @@
 #include <helper/time_support.h>
 #include <target/target_type.h>
 #include <target/algorithm.h>
+#include <target/arm_adi_v5.h>
 #include <target/armv7m.h>
 #include <target/cortex_m.h>
 
 
 #define MSCM_OCMDR0    0x40001400
 #define FMC_PFB01CR    0x4001f004
-#define FTFx_FSTAT     0x40020000
-#define FTFx_FCNFG     0x40020001
-#define FTFx_FCCOB3    0x40020004
-#define FTFx_FPROT3    0x40020010
-#define FTFx_FDPROT    0x40020017
+#define FTFX_FSTAT     0x40020000
+#define FTFX_FCNFG     0x40020001
+#define FTFX_FCCOB3    0x40020004
+#define FTFX_FPROT3    0x40020010
+#define FTFX_FDPROT    0x40020017
 #define SIM_BASE       0x40047000
 #define SIM_BASE_KL28  0x40074000
 #define SIM_COPC       0x40048100
 #define PM_CTRL_RUNM_RUN       0x00
 
 /* Commands */
-#define FTFx_CMD_BLOCKSTAT  0x00
-#define FTFx_CMD_SECTSTAT   0x01
-#define FTFx_CMD_LWORDPROG  0x06
-#define FTFx_CMD_SECTERASE  0x09
-#define FTFx_CMD_SECTWRITE  0x0b
-#define FTFx_CMD_MASSERASE  0x44
-#define FTFx_CMD_PGMPART    0x80
-#define FTFx_CMD_SETFLEXRAM 0x81
+#define FTFX_CMD_BLOCKSTAT  0x00
+#define FTFX_CMD_SECTSTAT   0x01
+#define FTFX_CMD_LWORDPROG  0x06
+#define FTFX_CMD_SECTERASE  0x09
+#define FTFX_CMD_SECTWRITE  0x0b
+#define FTFX_CMD_MASSERASE  0x44
+#define FTFX_CMD_PGMPART    0x80
+#define FTFX_CMD_SETFLEXRAM 0x81
 
 /* The older Kinetis K series uses the following SDID layout :
  * Bit 31-16 : 0
 
 /* The field originally named DIEID has new name/meaning on KE1x */
 #define KINETIS_SDID_PROJECTID_MASK  KINETIS_SDID_DIEID_MASK
-#define KINETIS_SDID_PROJECTID_KE1xF 0x00000080
-#define KINETIS_SDID_PROJECTID_KE1xZ 0x00000100
+#define KINETIS_SDID_PROJECTID_KE1XF 0x00000080
+#define KINETIS_SDID_PROJECTID_KE1XZ 0x00000100
 
 struct kinetis_flash_bank {
        struct kinetis_chip *k_chip;
@@ -882,9 +883,9 @@ FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
 
        k_chip = kinetis_get_chip(target);
 
-       if (k_chip == NULL) {
+       if (!k_chip) {
                k_chip = calloc(sizeof(struct kinetis_chip), 1);
-               if (k_chip == NULL) {
+               if (!k_chip) {
                        LOG_ERROR("No memory");
                        return ERROR_FAIL;
                }
@@ -915,11 +916,11 @@ FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
 static void kinetis_free_driver_priv(struct flash_bank *bank)
 {
        struct kinetis_flash_bank *k_bank = bank->driver_priv;
-       if (k_bank == NULL)
+       if (!k_bank)
                return;
 
        struct kinetis_chip *k_chip = k_bank->k_chip;
-       if (k_chip == NULL)
+       if (!k_chip)
                return;
 
        k_chip->num_banks--;
@@ -985,7 +986,7 @@ static int kinetis_create_missing_banks(struct kinetis_chip *k_chip)
                }
 
                bank = calloc(sizeof(struct flash_bank), 1);
-               if (bank == NULL)
+               if (!bank)
                        return ERROR_FAIL;
 
                bank->target = k_chip->target;
@@ -1174,7 +1175,7 @@ COMMAND_HANDLER(kinetis_disable_wdog_handler)
        struct target *target = get_current_target(CMD_CTX);
        struct kinetis_chip *k_chip = kinetis_get_chip(target);
 
-       if (k_chip == NULL)
+       if (!k_chip)
                return ERROR_FAIL;
 
        if (CMD_ARGC > 0)
@@ -1209,7 +1210,7 @@ static int kinetis_ftfx_decode_error(uint8_t fstat)
 static int kinetis_ftfx_clear_error(struct target *target)
 {
        /* reset error flags */
-       return target_write_u8(target, FTFx_FSTAT, 0x70);
+       return target_write_u8(target, FTFX_FSTAT, 0x70);
 }
 
 
@@ -1220,7 +1221,7 @@ static int kinetis_ftfx_prepare(struct target *target)
 
        /* wait until busy */
        for (unsigned int i = 0; i < 50; i++) {
-               result = target_read_u8(target, FTFx_FSTAT, &fstat);
+               result = target_read_u8(target, FTFX_FSTAT, &fstat);
                if (result != ERROR_OK)
                        return result;
 
@@ -1300,7 +1301,7 @@ static int kinetis_write_block(struct flash_bank *bank, const uint8_t *buffer,
        buf_set_u32(reg_params[1].value, 0, 32, wcount);
        buf_set_u32(reg_params[2].value, 0, 32, source->address);
        buf_set_u32(reg_params[3].value, 0, 32, source->address + source->size);
-       buf_set_u32(reg_params[4].value, 0, 32, FTFx_FSTAT);
+       buf_set_u32(reg_params[4].value, 0, 32, FTFX_FSTAT);
 
        retval = target_run_flash_async_algorithm(target, buffer, wcount, 4,
                                                0, NULL,
@@ -1314,12 +1315,12 @@ static int kinetis_write_block(struct flash_bank *bank, const uint8_t *buffer,
 
                LOG_ERROR("Error writing flash at %08" PRIx32, end_address);
 
-               retval = target_read_u8(target, FTFx_FSTAT, &fstat);
+               retval = target_read_u8(target, FTFX_FSTAT, &fstat);
                if (retval == ERROR_OK) {
                        retval = kinetis_ftfx_decode_error(fstat);
 
                        /* reset error flags */
-                       target_write_u8(target, FTFx_FSTAT, 0x70);
+                       target_write_u8(target, FTFX_FSTAT, 0x70);
                }
        } else if (retval != ERROR_OK)
                LOG_ERROR("Error executing kinetis Flash programming algorithm");
@@ -1369,7 +1370,7 @@ static int kinetis_protect_check(struct flash_bank *bank)
        if (k_bank->flash_class == FC_PFLASH) {
 
                /* read protection register */
-               result = target_read_u32(bank->target, FTFx_FPROT3, &fprot);
+               result = target_read_u32(bank->target, FTFX_FPROT3, &fprot);
                if (result != ERROR_OK)
                        return result;
 
@@ -1379,7 +1380,7 @@ static int kinetis_protect_check(struct flash_bank *bank)
                uint8_t fdprot;
 
                /* read protection register */
-               result = target_read_u8(bank->target, FTFx_FDPROT, &fdprot);
+               result = target_read_u8(bank->target, FTFX_FDPROT, &fdprot);
                if (result != ERROR_OK)
                        return result;
 
@@ -1428,7 +1429,7 @@ static int kinetis_fill_fcf(struct flash_bank *bank, uint8_t *fcf)
                k_bank = &(k_chip->banks[bank_idx]);
                bank_iter = k_bank->bank;
 
-               if (bank_iter == NULL) {
+               if (!bank_iter) {
                        LOG_WARNING("Missing bank %u configuration, FCF protection flags may be incomplete", bank_idx);
                        continue;
                }
@@ -1475,18 +1476,18 @@ static int kinetis_ftfx_command(struct target *target, uint8_t fcmd, uint32_t fa
        uint8_t fstat;
        int64_t ms_timeout = timeval_ms() + 250;
 
-       result = target_write_memory(target, FTFx_FCCOB3, 4, 3, command);
+       result = target_write_memory(target, FTFX_FCCOB3, 4, 3, command);
        if (result != ERROR_OK)
                return result;
 
        /* start command */
-       result = target_write_u8(target, FTFx_FSTAT, 0x80);
+       result = target_write_u8(target, FTFX_FSTAT, 0x80);
        if (result != ERROR_OK)
                return result;
 
        /* wait for done */
        do {
-               result = target_read_u8(target, FTFx_FSTAT, &fstat);
+               result = target_read_u8(target, FTFX_FSTAT, &fstat);
 
                if (result != ERROR_OK)
                        return result;
@@ -1538,7 +1539,7 @@ static int kinetis_check_run_mode(struct kinetis_chip *k_chip)
        uint8_t pmstat;
        struct target *target;
 
-       if (k_chip == NULL) {
+       if (!k_chip) {
                LOG_ERROR("Chip not probed.");
                return ERROR_FAIL;
        }
@@ -1641,7 +1642,7 @@ static int kinetis_erase(struct flash_bank *bank, unsigned int first,
         */
        for (unsigned int i = first; i <= last; i++) {
                /* set command and sector address */
-               result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTERASE, k_bank->prog_base + bank->sectors[i].offset,
+               result = kinetis_ftfx_command(bank->target, FTFX_CMD_SECTERASE, k_bank->prog_base + bank->sectors[i].offset,
                                0, 0, 0, 0,  0, 0, 0, 0,  NULL);
 
                if (result != ERROR_OK) {
@@ -1679,7 +1680,7 @@ static int kinetis_make_ram_ready(struct target *target)
        uint8_t ftfx_fcnfg;
 
        /* check if ram ready */
-       result = target_read_u8(target, FTFx_FCNFG, &ftfx_fcnfg);
+       result = target_read_u8(target, FTFX_FCNFG, &ftfx_fcnfg);
        if (result != ERROR_OK)
                return result;
 
@@ -1687,13 +1688,13 @@ static int kinetis_make_ram_ready(struct target *target)
                return ERROR_OK;        /* ram ready */
 
        /* make flex ram available */
-       result = kinetis_ftfx_command(target, FTFx_CMD_SETFLEXRAM, 0x00ff0000,
+       result = kinetis_ftfx_command(target, FTFX_CMD_SETFLEXRAM, 0x00ff0000,
                                 0, 0, 0, 0,  0, 0, 0, 0,  NULL);
        if (result != ERROR_OK)
                return ERROR_FLASH_OPERATION_FAILED;
 
        /* check again */
-       result = target_read_u8(target, FTFx_FCNFG, &ftfx_fcnfg);
+       result = target_read_u8(target, FTFX_FCNFG, &ftfx_fcnfg);
        if (result != ERROR_OK)
                return result;
 
@@ -1766,7 +1767,7 @@ static int kinetis_write_sections(struct flash_bank *bank, const uint8_t *buffer
                }
 
                /* execute section-write command */
-               result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTWRITE,
+               result = kinetis_ftfx_command(bank->target, FTFX_CMD_SECTWRITE,
                                k_bank->prog_base + offset - align_begin,
                                chunk_count>>8, chunk_count, 0, 0,
                                0, 0, 0, 0,  &ftfx_fstat);
@@ -1840,7 +1841,7 @@ static int kinetis_write_inner(struct flash_bank *bank, const uint8_t *buffer,
                        uint32_t old_count = count;
                        count = (old_count | 3) + 1;
                        new_buffer = malloc(count);
-                       if (new_buffer == NULL) {
+                       if (!new_buffer) {
                                LOG_ERROR("odd number of bytes to write and no memory "
                                        "for padding buffer");
                                return ERROR_FAIL;
@@ -1869,7 +1870,7 @@ static int kinetis_write_inner(struct flash_bank *bank, const uint8_t *buffer,
 
                                LOG_DEBUG("write longword @ %08" PRIx32, (uint32_t)(bank->base + offset));
 
-                               result = kinetis_ftfx_command(bank->target, FTFx_CMD_LWORDPROG, k_bank->prog_base + offset,
+                               result = kinetis_ftfx_command(bank->target, FTFX_CMD_LWORDPROG, k_bank->prog_base + offset,
                                                buffer[3], buffer[2], buffer[1], buffer[0],
                                                0, 0, 0, 0,  &ftfx_fstat);
 
@@ -2405,8 +2406,8 @@ static int kinetis_probe_chip(struct kinetis_chip *k_chip)
                        k_chip->watchdog_type = KINETIS_WDOG32_KE1X;
                        switch (k_chip->sim_sdid &
                                (KINETIS_SDID_FAMILYID_MASK | KINETIS_SDID_SUBFAMID_MASK | KINETIS_SDID_PROJECTID_MASK)) {
-                       case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX4 | KINETIS_SDID_PROJECTID_KE1xZ:
-                       case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX5 | KINETIS_SDID_PROJECTID_KE1xZ:
+                       case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX4 | KINETIS_SDID_PROJECTID_KE1XZ:
+                       case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX5 | KINETIS_SDID_PROJECTID_KE1XZ:
                                /* KE1xZ: FTFE, 2kB sectors */
                                k_chip->pflash_sector_size = 2<<10;
                                k_chip->nvm_sector_size = 2<<10;
@@ -2420,9 +2421,9 @@ static int kinetis_probe_chip(struct kinetis_chip *k_chip)
                                         familyid, subfamid, cpu_mhz / 10);
                                break;
 
-                       case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX4 | KINETIS_SDID_PROJECTID_KE1xF:
-                       case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX6 | KINETIS_SDID_PROJECTID_KE1xF:
-                       case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX8 | KINETIS_SDID_PROJECTID_KE1xF:
+                       case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX4 | KINETIS_SDID_PROJECTID_KE1XF:
+                       case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX6 | KINETIS_SDID_PROJECTID_KE1XF:
+                       case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX8 | KINETIS_SDID_PROJECTID_KE1XF:
                                /* KE1xF: FTFE, 4kB sectors */
                                k_chip->pflash_sector_size = 4<<10;
                                k_chip->nvm_sector_size = 2<<10;
@@ -2827,7 +2828,7 @@ static int kinetis_blank_check(struct flash_bank *bank)
 
                if (use_block_cmd) {
                        /* check if whole bank is blank */
-                       result = kinetis_ftfx_command(bank->target, FTFx_CMD_BLOCKSTAT, k_bank->prog_base,
+                       result = kinetis_ftfx_command(bank->target, FTFX_CMD_BLOCKSTAT, k_bank->prog_base,
                                                         0, 0, 0, 0,  0, 0, 0, 0, &ftfx_fstat);
 
                        if (result != ERROR_OK)
@@ -2840,7 +2841,7 @@ static int kinetis_blank_check(struct flash_bank *bank)
                        /* the whole bank is not erased, check sector-by-sector */
                        for (unsigned int i = 0; i < bank->num_sectors; i++) {
                                /* normal margin */
-                               result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTSTAT,
+                               result = kinetis_ftfx_command(bank->target, FTFX_CMD_SECTSTAT,
                                                k_bank->prog_base + bank->sectors[i].offset,
                                                1, 0, 0, 0,  0, 0, 0, 0, &ftfx_fstat);
 
@@ -2890,13 +2891,13 @@ COMMAND_HANDLER(kinetis_nvm_partition)
                else if (strcmp(CMD_ARGV[0], "eebkp") == 0)
                        sz_type = EEBKP_SIZE;
 
-               par = strtoul(CMD_ARGV[1], NULL, 10);
+               COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[1], par);
                while (par >> (log2 + 3))
                        log2++;
        }
        switch (sz_type) {
        case SHOW_INFO:
-               if (k_chip == NULL) {
+               if (!k_chip) {
                        LOG_ERROR("Chip not probed.");
                        return ERROR_FAIL;
                }
@@ -2945,11 +2946,13 @@ COMMAND_HANDLER(kinetis_nvm_partition)
                break;
        }
 
-       if (CMD_ARGC == 3)
-               ee1 = ee2 = strtoul(CMD_ARGV[2], NULL, 10) / 2;
-       else if (CMD_ARGC >= 4) {
-               ee1 = strtoul(CMD_ARGV[2], NULL, 10);
-               ee2 = strtoul(CMD_ARGV[3], NULL, 10);
+       if (CMD_ARGC == 3) {
+               unsigned long eex;
+               COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[2], eex);
+               ee1 = ee2 = eex / 2;
+       } else if (CMD_ARGC >= 4) {
+               COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[2], ee1);
+               COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[3], ee2);
        }
 
        enable = ee1 + ee2 > 0;
@@ -2992,7 +2995,7 @@ COMMAND_HANDLER(kinetis_nvm_partition)
        if (result != ERROR_OK)
                return result;
 
-       result = kinetis_ftfx_command(target, FTFx_CMD_PGMPART, load_flex_ram,
+       result = kinetis_ftfx_command(target, FTFX_CMD_PGMPART, load_flex_ram,
                                      ee_size_code, flex_nvm_partition_code, 0, 0,
                                      0, 0, 0, 0,  NULL);
        if (result != ERROR_OK)
@@ -3044,7 +3047,7 @@ COMMAND_HANDLER(kinetis_fopt_handler)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
        if (CMD_ARGC == 1) {
-               fcf_fopt = (uint8_t)strtoul(CMD_ARGV[0], NULL, 0);
+               COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], fcf_fopt);
        } else {
                command_print(CMD, "FCF_FOPT 0x%02" PRIx8, fcf_fopt);
        }

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)