openocd: remove NULL comparisons with checkpatch [1/2]
[fw/openocd] / src / flash / nor / jtagspi.c
index f6e311ab820c22e25ab2adf36805ec374834b334..dc49fda6153ddf31e2b97c820cc58d5c42e48c7d 100644 (file)
@@ -30,7 +30,7 @@
 struct jtagspi_flash_bank {
        struct jtag_tap *tap;
        const struct flash_device *dev;
-       int probed;
+       bool probed;
        uint32_t ir;
 };
 
@@ -42,14 +42,14 @@ FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
        info = malloc(sizeof(struct jtagspi_flash_bank));
-       if (info == NULL) {
+       if (!info) {
                LOG_ERROR("no memory for flash bank info");
                return ERROR_FAIL;
        }
        bank->driver_priv = info;
 
        info->tap = NULL;
-       info->probed = 0;
+       info->probed = false;
        COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], info->ir);
 
        return ERROR_OK;
@@ -129,7 +129,7 @@ static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd,
        lenb = DIV_ROUND_UP(len, 8);
        data_buf = malloc(lenb);
        if (lenb > 0) {
-               if (data_buf == NULL) {
+               if (!data_buf) {
                        LOG_ERROR("no memory for spi buffer");
                        return ERROR_FAIL;
                }
@@ -153,12 +153,12 @@ static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd,
        jtagspi_set_ir(bank);
        /* passing from an IR scan to SHIFT-DR clears BYPASS registers */
        jtag_add_dr_scan(info->tap, n, fields, TAP_IDLE);
-       jtag_execute_queue();
+       int retval = jtag_execute_queue();
 
        if (is_read)
                flip_u8(data_buf, data, lenb);
        free(data_buf);
-       return ERROR_OK;
+       return retval;
 }
 
 static int jtagspi_probe(struct flash_bank *bank)
@@ -170,9 +170,9 @@ static int jtagspi_probe(struct flash_bank *bank)
 
        if (info->probed)
                free(bank->sectors);
-       info->probed = 0;
+       info->probed = false;
 
-       if (bank->target->tap == NULL) {
+       if (!bank->target->tap) {
                LOG_ERROR("Target has no JTAG tap");
                return ERROR_FAIL;
        }
@@ -211,12 +211,12 @@ static int jtagspi_probe(struct flash_bank *bank)
        /* create and fill sectors array */
        bank->num_sectors = info->dev->size_in_bytes / sectorsize;
        sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
-       if (sectors == NULL) {
+       if (!sectors) {
                LOG_ERROR("not enough memory");
                return ERROR_FAIL;
        }
 
-       for (int sector = 0; sector < bank->num_sectors; sector++) {
+       for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
                sectors[sector].offset = sector * sectorsize;
                sectors[sector].size = sectorsize;
                sectors[sector].is_erased = -1;
@@ -224,17 +224,20 @@ static int jtagspi_probe(struct flash_bank *bank)
        }
 
        bank->sectors = sectors;
-       info->probed = 1;
+       info->probed = true;
        return ERROR_OK;
 }
 
-static void jtagspi_read_status(struct flash_bank *bank, uint32_t *status)
+static int jtagspi_read_status(struct flash_bank *bank, uint32_t *status)
 {
        uint8_t buf;
-       if (jtagspi_cmd(bank, SPIFLASH_READ_STATUS, NULL, &buf, -8) == ERROR_OK) {
+       int err = jtagspi_cmd(bank, SPIFLASH_READ_STATUS, NULL, &buf, -8);
+       if (err == ERROR_OK) {
                *status = buf;
                /* LOG_DEBUG("status=0x%08" PRIx32, *status); */
        }
+
+       return err;
 }
 
 static int jtagspi_wait(struct flash_bank *bank, int timeout_ms)
@@ -245,7 +248,11 @@ static int jtagspi_wait(struct flash_bank *bank, int timeout_ms)
 
        do {
                dt = timeval_ms() - t0;
-               jtagspi_read_status(bank, &status);
+
+               int retval = jtagspi_read_status(bank, &status);
+               if (retval != ERROR_OK)
+                       return retval;
+
                if ((status & SPIFLASH_BSY_BIT) == 0) {
                        LOG_DEBUG("waited %" PRId64 " ms", dt);
                        return ERROR_OK;
@@ -262,7 +269,11 @@ static int jtagspi_write_enable(struct flash_bank *bank)
        uint32_t status;
 
        jtagspi_cmd(bank, SPIFLASH_WRITE_ENABLE, NULL, NULL, 0);
-       jtagspi_read_status(bank, &status);
+
+       int retval = jtagspi_read_status(bank, &status);
+       if (retval != ERROR_OK)
+               return retval;
+
        if ((status & SPIFLASH_WE_BIT) == 0) {
                LOG_ERROR("Cannot enable write to flash. Status=0x%08" PRIx32, status);
                return ERROR_FAIL;
@@ -288,7 +299,7 @@ static int jtagspi_bulk_erase(struct flash_bank *bank)
        return retval;
 }
 
-static int jtagspi_sector_erase(struct flash_bank *bank, int sector)
+static int jtagspi_sector_erase(struct flash_bank *bank, unsigned int sector)
 {
        struct jtagspi_flash_bank *info = bank->driver_priv;
        int retval;
@@ -299,19 +310,19 @@ static int jtagspi_sector_erase(struct flash_bank *bank, int sector)
                return retval;
        jtagspi_cmd(bank, info->dev->erase_cmd, &bank->sectors[sector].offset, NULL, 0);
        retval = jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT);
-       LOG_INFO("sector %d took %" PRId64 " ms", sector, timeval_ms() - t0);
+       LOG_INFO("sector %u took %" PRId64 " ms", sector, timeval_ms() - t0);
        return retval;
 }
 
-static int jtagspi_erase(struct flash_bank *bank, int first, int last)
+static int jtagspi_erase(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
-       int sector;
        struct jtagspi_flash_bank *info = bank->driver_priv;
        int retval = ERROR_OK;
 
-       LOG_DEBUG("erase from sector %d to sector %d", first, last);
+       LOG_DEBUG("erase from sector %u to sector %u", first, last);
 
-       if ((first < 0) || (last < first) || (last >= bank->num_sectors)) {
+       if ((last < first) || (last >= bank->num_sectors)) {
                LOG_ERROR("Flash sector invalid");
                return ERROR_FLASH_SECTOR_INVALID;
        }
@@ -321,9 +332,9 @@ static int jtagspi_erase(struct flash_bank *bank, int first, int last)
                return ERROR_FLASH_BANK_NOT_PROBED;
        }
 
-       for (sector = first; sector <= last; sector++) {
+       for (unsigned int sector = first; sector <= last; sector++) {
                if (bank->sectors[sector].is_protected) {
-                       LOG_ERROR("Flash sector %d protected", sector);
+                       LOG_ERROR("Flash sector %u protected", sector);
                        return ERROR_FAIL;
                }
        }
@@ -341,7 +352,7 @@ static int jtagspi_erase(struct flash_bank *bank, int first, int last)
        if (info->dev->erase_cmd == 0x00)
                return ERROR_FLASH_OPER_UNSUPPORTED;
 
-       for (sector = first; sector <= last; sector++) {
+       for (unsigned int sector = first; sector <= last; sector++) {
                retval = jtagspi_sector_erase(bank, sector);
                if (retval != ERROR_OK) {
                        LOG_ERROR("Sector erase failed.");
@@ -352,11 +363,10 @@ static int jtagspi_erase(struct flash_bank *bank, int first, int last)
        return retval;
 }
 
-static int jtagspi_protect(struct flash_bank *bank, int set, int first, int last)
+static int jtagspi_protect(struct flash_bank *bank, int set, unsigned int first,
+               unsigned int last)
 {
-       int sector;
-
-       for (sector = first; sector <= last; sector++)
+       for (unsigned int sector = first; sector <= last; sector++)
                bank->sectors[sector].is_protected = set;
        return ERROR_OK;
 }
@@ -411,16 +421,16 @@ static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_
        return ERROR_OK;
 }
 
-static int jtagspi_info(struct flash_bank *bank, char *buf, int buf_size)
+static int jtagspi_info(struct flash_bank *bank, struct command_invocation *cmd)
 {
        struct jtagspi_flash_bank *info = bank->driver_priv;
 
        if (!(info->probed)) {
-               snprintf(buf, buf_size, "\nJTAGSPI flash bank not probed yet\n");
+               command_print_sameline(cmd, "\nJTAGSPI flash bank not probed yet\n");
                return ERROR_OK;
        }
 
-       snprintf(buf, buf_size, "\nSPIFI flash information:\n"
+       command_print_sameline(cmd, "\nSPIFI flash information:\n"
                "  Device \'%s\' (ID 0x%08" PRIx32 ")\n",
                info->dev->name, info->dev->device_id);