flash: avoid checking for non NULL pointer to free it
[fw/openocd] / src / flash / nor / cc3220sf.c
index af45743b92f63895019184ecba6e677813c844fd..5427bd3a9b3bff6303ad52b99d79febedbb8a291 100644 (file)
@@ -106,7 +106,8 @@ FLASH_BANK_COMMAND_HANDLER(cc3220sf_flash_bank_command)
        return ERROR_OK;
 }
 
-static int cc3220sf_erase(struct flash_bank *bank, int first, int last)
+static int cc3220sf_erase(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
        struct target *target = bank->target;
        bool done;
@@ -129,7 +130,7 @@ static int cc3220sf_erase(struct flash_bank *bank, int first, int last)
        }
 
        /* Erase requested sectors one by one */
-       for (int i = first; i <= last; i++) {
+       for (unsigned int i = first; i <= last; i++) {
 
                /* Determine address of sector to erase */
                address = FLASH_BASE_ADDR + i * FLASH_SECTOR_SIZE;
@@ -173,12 +174,6 @@ static int cc3220sf_erase(struct flash_bank *bank, int first, int last)
        return retval;
 }
 
-static int cc3220sf_protect(struct flash_bank *bank, int set, int first,
-       int last)
-{
-       return ERROR_OK;
-}
-
 static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
        uint32_t offset, uint32_t count)
 {
@@ -369,6 +364,8 @@ static int cc3220sf_write(struct flash_bank *bank, const uint8_t *buffer,
                        LOG_ERROR("cc3220sf: Flash operation failed");
                        break;
                }
+
+               keep_alive();
        }
 
        /* Do one word write for any final bytes less than a full word */
@@ -435,24 +432,13 @@ static int cc3220sf_probe(struct flash_bank *bank)
 
        uint32_t base;
        uint32_t size;
-       int num_sectors;
-       int bank_id;
-
-       bank_id = bank->bank_number;
+       unsigned int num_sectors;
 
-       if (0 == bank_id) {
-               base = FLASH_BASE_ADDR;
-               size = FLASH_NUM_SECTORS * FLASH_SECTOR_SIZE;
-               num_sectors = FLASH_NUM_SECTORS;
-       } else {
-               /* Invalid bank number somehow */
-               return ERROR_FAIL;
-       }
+       base = FLASH_BASE_ADDR;
+       size = FLASH_NUM_SECTORS * FLASH_SECTOR_SIZE;
+       num_sectors = FLASH_NUM_SECTORS;
 
-       if (NULL != bank->sectors) {
-               free(bank->sectors);
-               bank->sectors = NULL;
-       }
+       free(bank->sectors);
 
        bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
        if (NULL == bank->sectors)
@@ -464,7 +450,7 @@ static int cc3220sf_probe(struct flash_bank *bank)
        bank->write_end_alignment = 0;
        bank->num_sectors = num_sectors;
 
-       for (int i = 0; i < num_sectors; i++) {
+       for (unsigned int i = 0; i < num_sectors; i++) {
                bank->sectors[i].offset = i * FLASH_SECTOR_SIZE;
                bank->sectors[i].size = FLASH_SECTOR_SIZE;
                bank->sectors[i].is_erased = -1;
@@ -485,22 +471,12 @@ static int cc3220sf_auto_probe(struct flash_bank *bank)
 
        int retval = ERROR_OK;
 
-       if (0 != bank->bank_number) {
-               /* Invalid bank number somehow */
-               return ERROR_FAIL;
-       }
-
        if (!cc3220sf_bank->probed)
                retval = cc3220sf_probe(bank);
 
        return retval;
 }
 
-static int cc3220sf_protect_check(struct flash_bank *bank)
-{
-       return ERROR_OK;
-}
-
 static int cc3220sf_info(struct flash_bank *bank, char *buf, int buf_size)
 {
        int printed;
@@ -513,17 +489,15 @@ static int cc3220sf_info(struct flash_bank *bank, char *buf, int buf_size)
        return ERROR_OK;
 }
 
-struct flash_driver cc3220sf_flash = {
+const struct flash_driver cc3220sf_flash = {
        .name = "cc3220sf",
        .flash_bank_command = cc3220sf_flash_bank_command,
        .erase = cc3220sf_erase,
-       .protect = cc3220sf_protect,
        .write = cc3220sf_write,
        .read = default_flash_read,
        .probe = cc3220sf_probe,
        .auto_probe = cc3220sf_auto_probe,
        .erase_check = default_flash_blank_check,
-       .protect_check = cc3220sf_protect_check,
        .info = cc3220sf_info,
        .free_driver_priv = default_flash_free_driver_priv,
 };