openocd: fix SPDX tag format for files .c
[fw/openocd] / src / flash / nor / str9xpec.c
index da6c83e28bef83b29e393071e1e9176f16101a85..c39eb3aa84da81ab8964bb01d31c4138a4722592 100644 (file)
@@ -1,24 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   Dominic.Rath@gmx.de                                                   *
  *                                                                         *
  *   Copyright (C) 2008 by Spencer Oliver                                  *
  *   spen@spen-soft.co.uk                                                  *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   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.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -76,13 +63,14 @@ struct str9xpec_flash_controller {
        uint8_t options[8];
 };
 
-static int str9xpec_erase_area(struct flash_bank *bank, int first, int last);
+static int str9xpec_erase_area(struct flash_bank *bank, unsigned int first,
+               unsigned int last);
 static int str9xpec_set_address(struct flash_bank *bank, uint8_t sector);
 static int str9xpec_write_options(struct flash_bank *bank);
 
 static int str9xpec_set_instr(struct jtag_tap *tap, uint32_t new_instr, tap_state_t end_state)
 {
-       if (tap == NULL)
+       if (!tap)
                return ERROR_TARGET_INVALID;
 
        if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr) {
@@ -212,7 +200,7 @@ static int str9xpec_build_block_list(struct flash_bank *bank)
        struct str9xpec_flash_controller *str9xpec_info = bank->driver_priv;
 
        int i;
-       int num_sectors;
+       unsigned int num_sectors;
        int b0_sectors = 0, b1_sectors = 0;
        uint32_t offset = 0;
        int b1_size = 0x2000;
@@ -305,12 +293,12 @@ FLASH_BANK_COMMAND_HANDLER(str9xpec_flash_bank_command)
        return ERROR_OK;
 }
 
-static int str9xpec_blank_check(struct flash_bank *bank, int first, int last)
+static int str9xpec_blank_check(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
        struct scan_field field;
        uint8_t status;
        struct jtag_tap *tap;
-       int i;
        uint8_t *buffer = NULL;
 
        struct str9xpec_flash_controller *str9xpec_info = bank->driver_priv;
@@ -325,9 +313,9 @@ static int str9xpec_blank_check(struct flash_bank *bank, int first, int last)
 
        buffer = calloc(DIV_ROUND_UP(64, 8), 1);
 
-       LOG_DEBUG("blank check: first_bank: %i, last_bank: %i", first, last);
+       LOG_DEBUG("blank check: first_bank: %u, last_bank: %u", first, last);
 
-       for (i = first; i <= last; i++)
+       for (unsigned int i = first; i <= last; i++)
                buf_set_u32(buffer, str9xpec_info->sector_bits[i], 1, 1);
 
        /* execute ISC_BLANK_CHECK command */
@@ -350,7 +338,7 @@ static int str9xpec_blank_check(struct flash_bank *bank, int first, int last)
 
        status = str9xpec_isc_status(tap);
 
-       for (i = first; i <= last; i++) {
+       for (unsigned int i = first; i <= last; i++) {
                if (buf_get_u32(buffer, str9xpec_info->sector_bits[i], 1))
                        bank->sectors[i].is_erased = 0;
                else
@@ -369,13 +357,12 @@ static int str9xpec_blank_check(struct flash_bank *bank, int first, int last)
 static int str9xpec_protect_check(struct flash_bank *bank)
 {
        uint8_t status;
-       int i;
 
        struct str9xpec_flash_controller *str9xpec_info = bank->driver_priv;
 
        status = str9xpec_read_config(bank);
 
-       for (i = 0; i < bank->num_sectors; i++) {
+       for (unsigned int i = 0; i < bank->num_sectors; i++) {
                if (buf_get_u32(str9xpec_info->options, str9xpec_info->sector_bits[i], 1))
                        bank->sectors[i].is_protected = 1;
                else
@@ -387,12 +374,12 @@ static int str9xpec_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int str9xpec_erase_area(struct flash_bank *bank, int first, int last)
+static int str9xpec_erase_area(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
        struct scan_field field;
        uint8_t status;
        struct jtag_tap *tap;
-       int i;
        uint8_t *buffer = NULL;
 
        struct str9xpec_flash_controller *str9xpec_info = bank->driver_priv;
@@ -407,17 +394,17 @@ static int str9xpec_erase_area(struct flash_bank *bank, int first, int last)
 
        buffer = calloc(DIV_ROUND_UP(64, 8), 1);
 
-       LOG_DEBUG("erase: first_bank: %i, last_bank: %i", first, last);
+       LOG_DEBUG("erase: first_bank: %u, last_bank: %u", first, last);
 
        /* last bank: 0xFF signals a full erase (unlock complete device) */
        /* last bank: 0xFE signals a option byte erase */
        if (last == 0xFF) {
-               for (i = 0; i < 64; i++)
+               for (unsigned int i = 0; i < 64; i++)
                        buf_set_u32(buffer, i, 1, 1);
        } else if (last == 0xFE)
                buf_set_u32(buffer, 49, 1, 1);
        else {
-               for (i = first; i <= last; i++)
+               for (unsigned int i = first; i <= last; i++)
                        buf_set_u32(buffer, str9xpec_info->sector_bits[i], 1, 1);
        }
 
@@ -446,7 +433,8 @@ static int str9xpec_erase_area(struct flash_bank *bank, int first, int last)
        return status;
 }
 
-static int str9xpec_erase(struct flash_bank *bank, int first, int last)
+static int str9xpec_erase(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
        int status;
 
@@ -506,10 +494,10 @@ static int str9xpec_unlock_device(struct flash_bank *bank)
        return status;
 }
 
-static int str9xpec_protect(struct flash_bank *bank, int set, int first, int last)
+static int str9xpec_protect(struct flash_bank *bank, int set,
+               unsigned int first, unsigned int last)
 {
        uint8_t status;
-       int i;
 
        struct str9xpec_flash_controller *str9xpec_info = bank->driver_priv;
 
@@ -518,7 +506,7 @@ static int str9xpec_protect(struct flash_bank *bank, int set, int first, int las
        if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
                return ERROR_FLASH_OPERATION_FAILED;
 
-       LOG_DEBUG("protect: first_bank: %i, last_bank: %i", first, last);
+       LOG_DEBUG("protect: first_bank: %u, last_bank: %u", first, last);
 
        /* last bank: 0xFF signals a full device protect */
        if (last == 0xFF) {
@@ -529,7 +517,7 @@ static int str9xpec_protect(struct flash_bank *bank, int set, int first, int las
                        status = str9xpec_unlock_device(bank);
                }
        } else {
-               for (i = first; i <= last; i++) {
+               for (unsigned int i = first; i <= last; i++) {
                        if (set)
                                buf_set_u32(str9xpec_info->options, str9xpec_info->sector_bits[i], 1, 1);
                        else
@@ -565,7 +553,7 @@ static int str9xpec_set_address(struct flash_bank *bank, uint8_t sector)
        return ERROR_OK;
 }
 
-static int str9xpec_write(struct flash_bank *bank, uint8_t *buffer,
+static int str9xpec_write(struct flash_bank *bank, const uint8_t *buffer,
                uint32_t offset, uint32_t count)
 {
        struct str9xpec_flash_controller *str9xpec_info = bank->driver_priv;
@@ -577,9 +565,8 @@ static int str9xpec_write(struct flash_bank *bank, uint8_t *buffer,
        struct jtag_tap *tap;
        struct scan_field field;
        uint8_t *scanbuf;
-       int i;
-       int first_sector = 0;
-       int last_sector = 0;
+       unsigned int first_sector = 0;
+       unsigned int last_sector = 0;
 
        tap = str9xpec_info->tap;
 
@@ -594,7 +581,7 @@ static int str9xpec_write(struct flash_bank *bank, uint8_t *buffer,
                return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
        }
 
-       for (i = 0; i < bank->num_sectors; i++) {
+       for (unsigned int i = 0; i < bank->num_sectors; i++) {
                uint32_t sec_start = bank->sectors[i].offset;
                uint32_t sec_end = sec_start + bank->sectors[i].size;
 
@@ -623,7 +610,7 @@ static int str9xpec_write(struct flash_bank *bank, uint8_t *buffer,
 
        LOG_DEBUG("ISC_PROGRAM");
 
-       for (i = first_sector; i <= last_sector; i++) {
+       for (unsigned int i = first_sector; i <= last_sector; i++) {
                str9xpec_set_address(bank, str9xpec_info->sector_bits[i]);
 
                dwords_remaining = dwords_remaining < (bank->sectors[i].size/8)
@@ -729,7 +716,7 @@ COMMAND_HANDLER(str9xpec_handle_part_id_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        str9xpec_info = bank->driver_priv;
@@ -748,7 +735,7 @@ COMMAND_HANDLER(str9xpec_handle_part_id_command)
 
        idcode = buf_get_u32(buffer, 0, 32);
 
-       command_print(CMD_CTX, "str9xpec part id: 0x%8.8" PRIx32 "", idcode);
+       command_print(CMD, "str9xpec part id: 0x%8.8" PRIx32 "", idcode);
 
        free(buffer);
 
@@ -760,12 +747,6 @@ static int str9xpec_erase_check(struct flash_bank *bank)
        return str9xpec_blank_check(bank, 0, bank->num_sectors - 1);
 }
 
-static int get_str9xpec_info(struct flash_bank *bank, char *buf, int buf_size)
-{
-       snprintf(buf, buf_size, "str9xpec flash driver info");
-       return ERROR_OK;
-}
-
 COMMAND_HANDLER(str9xpec_handle_flash_options_read_command)
 {
        uint8_t status;
@@ -776,7 +757,7 @@ COMMAND_HANDLER(str9xpec_handle_flash_options_read_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        str9xpec_info = bank->driver_priv;
@@ -788,33 +769,33 @@ COMMAND_HANDLER(str9xpec_handle_flash_options_read_command)
 
        /* boot bank */
        if (buf_get_u32(str9xpec_info->options, STR9XPEC_OPT_CSMAPBIT, 1))
-               command_print(CMD_CTX, "CS Map: bank1");
+               command_print(CMD, "CS Map: bank1");
        else
-               command_print(CMD_CTX, "CS Map: bank0");
+               command_print(CMD, "CS Map: bank0");
 
        /* OTP lock */
        if (buf_get_u32(str9xpec_info->options, STR9XPEC_OPT_OTPBIT, 1))
-               command_print(CMD_CTX, "OTP Lock: OTP Locked");
+               command_print(CMD, "OTP Lock: OTP Locked");
        else
-               command_print(CMD_CTX, "OTP Lock: OTP Unlocked");
+               command_print(CMD, "OTP Lock: OTP Unlocked");
 
        /* LVD Threshold */
        if (buf_get_u32(str9xpec_info->options, STR9XPEC_OPT_LVDTHRESBIT, 1))
-               command_print(CMD_CTX, "LVD Threshold: 2.7v");
+               command_print(CMD, "LVD Threshold: 2.7v");
        else
-               command_print(CMD_CTX, "LVD Threshold: 2.4v");
+               command_print(CMD, "LVD Threshold: 2.4v");
 
        /* LVD reset warning */
        if (buf_get_u32(str9xpec_info->options, STR9XPEC_OPT_LVDWARNBIT, 1))
-               command_print(CMD_CTX, "LVD Reset Warning: VDD or VDDQ Inputs");
+               command_print(CMD, "LVD Reset Warning: VDD or VDDQ Inputs");
        else
-               command_print(CMD_CTX, "LVD Reset Warning: VDD Input Only");
+               command_print(CMD, "LVD Reset Warning: VDD Input Only");
 
        /* LVD reset select */
        if (buf_get_u32(str9xpec_info->options, STR9XPEC_OPT_LVDSELBIT, 1))
-               command_print(CMD_CTX, "LVD Reset Selection: VDD or VDDQ Inputs");
+               command_print(CMD, "LVD Reset Selection: VDD or VDDQ Inputs");
        else
-               command_print(CMD_CTX, "LVD Reset Selection: VDD Input Only");
+               command_print(CMD, "LVD Reset Selection: VDD Input Only");
 
        return ERROR_OK;
 }
@@ -885,7 +866,7 @@ COMMAND_HANDLER(str9xpec_handle_flash_options_write_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        status = str9xpec_write_options(bank);
@@ -893,7 +874,7 @@ COMMAND_HANDLER(str9xpec_handle_flash_options_write_command)
        if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
                return ERROR_FLASH_OPERATION_FAILED;
 
-       command_print(CMD_CTX, "str9xpec write options complete.\n"
+       command_print(CMD, "str9xpec write options complete.\n"
                        "INFO: a reset or power cycle is required "
                        "for the new settings to take effect.");
 
@@ -909,7 +890,7 @@ COMMAND_HANDLER(str9xpec_handle_flash_options_cmap_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        str9xpec_info = bank->driver_priv;
@@ -931,7 +912,7 @@ COMMAND_HANDLER(str9xpec_handle_flash_options_lvdthd_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        str9xpec_info = bank->driver_priv;
@@ -953,7 +934,7 @@ COMMAND_HANDLER(str9xpec_handle_flash_options_lvdsel_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        str9xpec_info = bank->driver_priv;
@@ -975,7 +956,7 @@ COMMAND_HANDLER(str9xpec_handle_flash_options_lvdwarn_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        str9xpec_info = bank->driver_priv;
@@ -997,7 +978,7 @@ COMMAND_HANDLER(str9xpec_handle_flash_lock_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        status = str9xpec_lock_device(bank);
@@ -1017,7 +998,7 @@ COMMAND_HANDLER(str9xpec_handle_flash_unlock_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        status = str9xpec_unlock_device(bank);
@@ -1025,7 +1006,7 @@ COMMAND_HANDLER(str9xpec_handle_flash_unlock_command)
        if ((status & ISC_STATUS_ERROR) != STR9XPEC_ISC_SUCCESS)
                return ERROR_FLASH_OPERATION_FAILED;
 
-       command_print(CMD_CTX, "str9xpec unlocked.\n"
+       command_print(CMD, "str9xpec unlocked.\n"
                        "INFO: a reset or power cycle is required "
                        "for the new settings to take effect.");
 
@@ -1044,28 +1025,28 @@ COMMAND_HANDLER(str9xpec_handle_flash_enable_turbo_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        str9xpec_info = bank->driver_priv;
 
        /* remove arm core from chain - enter turbo mode */
        tap0 = str9xpec_info->tap;
-       if (tap0 == NULL) {
+       if (!tap0) {
                /* things are *WRONG* */
-               command_print(CMD_CTX, "**STR9FLASH** (tap0) invalid chain?");
+               command_print(CMD, "**STR9FLASH** (tap0) invalid chain?");
                return ERROR_FAIL;
        }
        tap1 = tap0->next_tap;
-       if (tap1 == NULL) {
+       if (!tap1) {
                /* things are *WRONG* */
-               command_print(CMD_CTX, "**STR9FLASH** (tap1) invalid chain?");
+               command_print(CMD, "**STR9FLASH** (tap1) invalid chain?");
                return ERROR_FAIL;
        }
        tap2 = tap1->next_tap;
-       if (tap2 == NULL) {
+       if (!tap2) {
                /* things are *WRONG* */
-               command_print(CMD_CTX, "**STR9FLASH** (tap2) invalid chain?");
+               command_print(CMD, "**STR9FLASH** (tap2) invalid chain?");
                return ERROR_FAIL;
        }
 
@@ -1091,13 +1072,13 @@ COMMAND_HANDLER(str9xpec_handle_flash_disable_turbo_command)
 
        struct flash_bank *bank;
        int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        str9xpec_info = bank->driver_priv;
        tap = str9xpec_info->tap;
 
-       if (tap == NULL)
+       if (!tap)
                return ERROR_FAIL;
 
        /* exit turbo mode via RESET */
@@ -1185,9 +1166,10 @@ static const struct command_registration str9xpec_config_command_handlers[] = {
        },
        {
                .name = "part_id",
+               .usage = "<bank>",
                .handler = str9xpec_handle_part_id_command,
                .mode = COMMAND_EXEC,
-               .help = "print part id of str9xpec flash bank <num>",
+               .help = "print part id of str9xpec flash bank",
        },
        COMMAND_REGISTRATION_DONE
 };
@@ -1203,7 +1185,7 @@ static const struct command_registration str9xpec_command_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
-struct flash_driver str9xpec_flash = {
+const struct flash_driver str9xpec_flash = {
        .name = "str9xpec",
        .commands = str9xpec_command_handlers,
        .flash_bank_command = str9xpec_flash_bank_command,
@@ -1215,5 +1197,5 @@ struct flash_driver str9xpec_flash = {
        .auto_probe = str9xpec_probe,
        .erase_check = str9xpec_erase_check,
        .protect_check = str9xpec_protect_check,
-       .info = get_str9xpec_info,
+       .free_driver_priv = default_flash_free_driver_priv,
 };