X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fflash%2Fnor%2Fmsp432.c;h=e9e4be33a4d6e3f46d5a1e75f2c323fb9040bded;hp=d2417bc89b227b1ce525b06791216e040e65d205;hb=HEAD;hpb=62329444abc89ad3b37fbb4ebc2edfd1dee23351 diff --git a/src/flash/nor/msp432.c b/src/flash/nor/msp432.c index d2417bc89b..5e2935d02b 100644 --- a/src/flash/nor/msp432.c +++ b/src/flash/nor/msp432.c @@ -1,18 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + /*************************************************************************** * Copyright (C) 2018 by Texas Instruments, Inc. * - * * - * 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, see . * ***************************************************************************/ #ifdef HAVE_CONFIG_H @@ -56,6 +45,21 @@ struct msp432_bank { struct armv7m_algorithm armv7m_info; }; +/* Flash helper algorithm for MSP432P401x targets */ +static const uint8_t msp432p401x_algo[] = { +#include "../../../contrib/loaders/flash/msp432/msp432p401x_algo.inc" +}; + +/* Flash helper algorithm for MSP432P411x targets */ +static const uint8_t msp432p411x_algo[] = { +#include "../../../contrib/loaders/flash/msp432/msp432p411x_algo.inc" +}; + +/* Flash helper algorithm for MSP432E4x targets */ +static const uint8_t msp432e4x_algo[] = { +#include "../../../contrib/loaders/flash/msp432/msp432e4x_algo.inc" +}; + static int msp432_auto_probe(struct flash_bank *bank); static int msp432_device_type(uint32_t family_type, uint32_t device_id, @@ -63,7 +67,7 @@ static int msp432_device_type(uint32_t family_type, uint32_t device_id, { int device_type = MSP432_NO_TYPE; - if (MSP432E4 == family_type) { + if (family_type == MSP432E4) { /* MSP432E4 device family */ if (device_id == 0x180C0002) { @@ -191,7 +195,7 @@ static int msp432_exec_cmd(struct target *target, struct msp432_algo_params /* Write out parameters to target memory */ retval = target_write_buffer(target, ALGO_PARAMS_BASE_ADDR, sizeof(struct msp432_algo_params), (uint8_t *)algo_params); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; /* Write out command to target memory */ @@ -209,9 +213,9 @@ static int msp432_wait_return_code(struct target *target) int retval = ERROR_OK; start_ms = timeval_ms(); - while ((0 == return_code) || (FLASH_BUSY == return_code)) { + while ((return_code == 0) || (return_code == FLASH_BUSY)) { retval = target_read_u32(target, ALGO_RETURN_CODE_ADDR, &return_code); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; elapsed_ms = timeval_ms() - start_ms; @@ -221,7 +225,7 @@ static int msp432_wait_return_code(struct target *target) break; }; - if (FLASH_SUCCESS != return_code) { + if (return_code != FLASH_SUCCESS) { LOG_ERROR("msp432: Flash operation failed: %s", msp432_return_text(return_code)); return ERROR_FAIL; @@ -251,9 +255,9 @@ static int msp432_wait_inactive(struct target *target, uint32_t buffer) } start_ms = timeval_ms(); - while (BUFFER_INACTIVE != status_code) { + while (status_code != BUFFER_INACTIVE) { retval = target_read_u32(target, status_addr, &status_code); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; elapsed_ms = timeval_ms() - start_ms; @@ -263,7 +267,7 @@ static int msp432_wait_inactive(struct target *target, uint32_t buffer) break; }; - if (BUFFER_INACTIVE != status_code) { + if (status_code != BUFFER_INACTIVE) { LOG_ERROR( "msp432: Flash operation failed: buffer not written to flash"); return ERROR_FAIL; @@ -286,7 +290,7 @@ static int msp432_init(struct flash_bank *bank) /* Make sure we've probed the flash to get the device and size */ retval = msp432_auto_probe(bank); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; /* Choose appropriate flash helper algorithm */ @@ -315,41 +319,42 @@ static int msp432_init(struct flash_bank *bank) } /* Issue warnings if this is a device we may not be able to flash */ - if (MSP432P401X_GUESS == msp432_bank->device_type || - MSP432P411X_GUESS == msp432_bank->device_type) { + if (msp432_bank->device_type == MSP432P401X_GUESS || + msp432_bank->device_type == MSP432P411X_GUESS) { /* Explicit device type check failed. Report this. */ LOG_WARNING( "msp432: Unrecognized MSP432P4 Device ID and Hardware " - "Rev (%04X, %02X)", msp432_bank->device_id, + "Rev (%04" PRIX32 ", %02" PRIX32 ")", msp432_bank->device_id, msp432_bank->hardware_rev); - } else if (MSP432P401X_DEPR == msp432_bank->device_type) { + } else if (msp432_bank->device_type == MSP432P401X_DEPR) { LOG_WARNING( "msp432: MSP432P401x pre-production device (deprecated " "silicon)\n" SUPPORT_MESSAGE); - } else if (MSP432E4X_GUESS == msp432_bank->device_type) { + } else if (msp432_bank->device_type == MSP432E4X_GUESS) { /* Explicit device type check failed. Report this. */ LOG_WARNING( "msp432: Unrecognized MSP432E4 DID0 and DID1 values " - "(%08X, %08X)", msp432_bank->device_id, + "(%08" PRIX32 ", %08" PRIX32 ")", msp432_bank->device_id, msp432_bank->hardware_rev); } /* Check for working area to use for flash helper algorithm */ - if (NULL != msp432_bank->working_area) - target_free_working_area(target, msp432_bank->working_area); + target_free_working_area(target, msp432_bank->working_area); + msp432_bank->working_area = NULL; + retval = target_alloc_working_area(target, ALGO_WORKING_SIZE, &msp432_bank->working_area); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; /* Confirm the defined working address is the area we need to use */ - if (ALGO_BASE_ADDR != msp432_bank->working_area->address) + if (msp432_bank->working_area->address != ALGO_BASE_ADDR) return ERROR_TARGET_RESOURCE_NOT_AVAILABLE; /* Write flash helper algorithm into target memory */ retval = target_write_buffer(target, ALGO_BASE_ADDR, loader_size, loader_code); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; /* Initialize the ARMv7 specific info to run the algorithm */ @@ -362,7 +367,7 @@ static int msp432_init(struct flash_bank *bank) /* Write out parameters to target memory */ retval = target_write_buffer(target, ALGO_PARAMS_BASE_ADDR, sizeof(algo_params), (uint8_t *)&algo_params); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; /* Initialize stack pointer for flash helper algorithm */ @@ -370,10 +375,10 @@ static int msp432_init(struct flash_bank *bank) buf_set_u32(reg_params[0].value, 0, 32, ALGO_STACK_POINTER_ADDR); /* Begin executing the flash helper algorithm */ - retval = target_start_algorithm(target, 0, 0, 1, reg_params, + retval = target_start_algorithm(target, 0, NULL, 1, reg_params, algo_entry_addr, 0, &msp432_bank->armv7m_info); destroy_reg_param(®_params[0]); - if (ERROR_OK != retval) { + if (retval != ERROR_OK) { LOG_ERROR("msp432: Failed to start flash helper algorithm"); return retval; } @@ -385,7 +390,7 @@ static int msp432_init(struct flash_bank *bank) /* Issue the init command to the flash helper algorithm */ retval = msp432_exec_cmd(target, &algo_params, FLASH_INIT); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; retval = msp432_wait_return_code(target); @@ -406,7 +411,7 @@ static int msp432_quit(struct flash_bank *bank) /* Issue the exit command to the flash helper algorithm */ retval = msp432_exec_cmd(target, &algo_params, FLASH_EXIT); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; (void)msp432_wait_return_code(target); @@ -432,13 +437,13 @@ static int msp432_mass_erase(struct flash_bank *bank, bool all) int retval; - if (TARGET_HALTED != target->state) { + if (target->state != TARGET_HALTED) { LOG_ERROR("Target not halted"); return ERROR_TARGET_NOT_HALTED; } retval = msp432_init(bank); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; /* Initialize algorithm parameters to default values */ @@ -452,19 +457,19 @@ static int msp432_mass_erase(struct flash_bank *bank, bool all) /* Issue the mass erase command to the flash helper algorithm */ retval = msp432_exec_cmd(target, &algo_params, FLASH_MASS_ERASE); - if (ERROR_OK != retval) { + if (retval != ERROR_OK) { (void)msp432_quit(bank); return retval; } retval = msp432_wait_return_code(target); - if (ERROR_OK != retval) { + if (retval != ERROR_OK) { (void)msp432_quit(bank); return retval; } retval = msp432_quit(bank); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; return retval; @@ -489,9 +494,9 @@ COMMAND_HANDLER(msp432_mass_erase_command) all = false; } else if (2 == CMD_ARGC) { /* Check argument for how much to erase */ - if (0 == strcmp(CMD_ARGV[1], "main")) + if (strcmp(CMD_ARGV[1], "main") == 0) all = false; - else if (0 == strcmp(CMD_ARGV[1], "all")) + else if (strcmp(CMD_ARGV[1], "all") == 0) all = true; else return ERROR_COMMAND_SYNTAX_ERROR; @@ -501,16 +506,16 @@ COMMAND_HANDLER(msp432_mass_erase_command) msp432_bank = bank->driver_priv; - if (MSP432E4 == msp432_bank->family_type) { + if (msp432_bank->family_type == MSP432E4) { /* MSP432E4 does not have main vs info regions, ignore "all" */ all = false; } retval = msp432_mass_erase(bank, all); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; - if (MSP432E4 == msp432_bank->family_type) { + if (msp432_bank->family_type == MSP432E4) { /* MSP432E4 does not have main vs info regions */ LOG_INFO("msp432: Mass erase of flash is complete"); } else { @@ -537,15 +542,15 @@ COMMAND_HANDLER(msp432_bsl_command) msp432_bank = bank->driver_priv; - if (MSP432E4 == msp432_bank->family_type) { + if (msp432_bank->family_type == MSP432E4) { LOG_WARNING("msp432: MSP432E4 does not have a BSL region"); return ERROR_OK; } if (2 == CMD_ARGC) { - if (0 == strcmp(CMD_ARGV[1], "lock")) + if (strcmp(CMD_ARGV[1], "lock") == 0) msp432_bank->unlock_bsl = false; - else if (0 == strcmp(CMD_ARGV[1], "unlock")) + else if (strcmp(CMD_ARGV[1], "unlock") == 0) msp432_bank->unlock_bsl = true; else return ERROR_COMMAND_SYNTAX_ERROR; @@ -569,7 +574,7 @@ FLASH_BANK_COMMAND_HANDLER(msp432_flash_bank_command) /* Create shared private struct for flash banks */ msp432_bank = malloc(sizeof(struct msp432_bank)); - if (NULL == msp432_bank) + if (!msp432_bank) return ERROR_FAIL; /* Initialize private flash information */ @@ -597,12 +602,12 @@ static int msp432_erase(struct flash_bank *bank, unsigned int first, struct msp432_bank *msp432_bank = bank->driver_priv; struct msp432_algo_params algo_params; - bool is_main = FLASH_BASE == bank->base; - bool is_info = P4_FLASH_INFO_BASE == bank->base; + bool is_main = bank->base == FLASH_BASE; + bool is_info = bank->base == P4_FLASH_INFO_BASE; int retval; - if (TARGET_HALTED != target->state) { + if (target->state != TARGET_HALTED) { LOG_ERROR("Target not halted"); return ERROR_TARGET_NOT_HALTED; } @@ -614,7 +619,7 @@ static int msp432_erase(struct flash_bank *bank, unsigned int first, } retval = msp432_init(bank); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; /* Initialize algorithm parameters to default values */ @@ -646,20 +651,20 @@ static int msp432_erase(struct flash_bank *bank, unsigned int first, /* Issue the sector erase command to the flash helper algorithm */ retval = msp432_exec_cmd(target, &algo_params, FLASH_SECTOR_ERASE); - if (ERROR_OK != retval) { + if (retval != ERROR_OK) { (void)msp432_quit(bank); return retval; } retval = msp432_wait_return_code(target); - if (ERROR_OK != retval) { + if (retval != ERROR_OK) { (void)msp432_quit(bank); return retval; } } retval = msp432_quit(bank); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; return retval; @@ -676,11 +681,11 @@ static int msp432_write(struct flash_bank *bank, const uint8_t *buffer, long long start_ms; long long elapsed_ms; - bool is_info = P4_FLASH_INFO_BASE == bank->base; + bool is_info = bank->base == P4_FLASH_INFO_BASE; int retval; - if (TARGET_HALTED != target->state) { + if (target->state != TARGET_HALTED) { LOG_ERROR("Target not halted"); return ERROR_TARGET_NOT_HALTED; } @@ -705,7 +710,7 @@ static int msp432_write(struct flash_bank *bank, const uint8_t *buffer, if (offset < start) { uint32_t start_count = MIN(start - offset, count); retval = msp432_write(bank, buffer, offset, start_count); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; } /* Send a request for anything after read-only sectors */ @@ -723,7 +728,7 @@ static int msp432_write(struct flash_bank *bank, const uint8_t *buffer, } retval = msp432_init(bank); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; /* Initialize algorithm parameters to default values */ @@ -742,7 +747,7 @@ static int msp432_write(struct flash_bank *bank, const uint8_t *buffer, /* Set up flash helper algorithm to continuous flash mode */ retval = msp432_exec_cmd(target, &algo_params, FLASH_CONTINUOUS); - if (ERROR_OK != retval) { + if (retval != ERROR_OK) { (void)msp432_quit(bank); return retval; } @@ -758,7 +763,7 @@ static int msp432_write(struct flash_bank *bank, const uint8_t *buffer, /* Put next block of data to flash into buffer */ retval = target_write_buffer(target, ALGO_BUFFER1_ADDR, size, buffer); - if (ERROR_OK != retval) { + if (retval != ERROR_OK) { LOG_ERROR("Unable to write data to target memory"); (void)msp432_quit(bank); return ERROR_FLASH_OPERATION_FAILED; @@ -767,13 +772,13 @@ static int msp432_write(struct flash_bank *bank, const uint8_t *buffer, /* Signal the flash helper algorithm that data is ready to flash */ retval = target_write_u32(target, ALGO_BUFFER1_STATUS_ADDR, data_ready); - if (ERROR_OK != retval) { + if (retval != ERROR_OK) { (void)msp432_quit(bank); return ERROR_FLASH_OPERATION_FAILED; } retval = msp432_wait_inactive(target, 1); - if (ERROR_OK != retval) { + if (retval != ERROR_OK) { (void)msp432_quit(bank); return retval; } @@ -788,13 +793,13 @@ static int msp432_write(struct flash_bank *bank, const uint8_t *buffer, /* Confirm that the flash helper algorithm is finished */ retval = msp432_wait_return_code(target); - if (ERROR_OK != retval) { + if (retval != ERROR_OK) { (void)msp432_quit(bank); return retval; } retval = msp432_quit(bank); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; return retval; @@ -812,8 +817,8 @@ static int msp432_probe(struct flash_bank *bank) uint32_t size; unsigned int num_sectors; - bool is_main = FLASH_BASE == bank->base; - bool is_info = P4_FLASH_INFO_BASE == bank->base; + bool is_main = bank->base == FLASH_BASE; + bool is_info = bank->base == P4_FLASH_INFO_BASE; int retval; @@ -826,21 +831,21 @@ static int msp432_probe(struct flash_bank *bank) /* Read the flash size register to determine this is a P4 or not */ /* MSP432P4s will return the size of flash. MSP432E4s will return zero */ retval = target_read_u32(target, P4_FLASH_MAIN_SIZE_REG, &size); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; - if (0 == size) { + if (size == 0) { /* This is likely an MSP432E4 */ msp432_bank->family_type = MSP432E4; retval = target_read_u32(target, E4_DID0_REG, &device_id); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; msp432_bank->device_id = device_id; retval = target_read_u32(target, E4_DID1_REG, &hardware_rev); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; msp432_bank->hardware_rev = hardware_rev; @@ -849,13 +854,13 @@ static int msp432_probe(struct flash_bank *bank) msp432_bank->family_type = MSP432P4; retval = target_read_u32(target, P4_DEVICE_ID_REG, &device_id); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; msp432_bank->device_id = device_id & 0xFFFF; retval = target_read_u32(target, P4_HARDWARE_REV_REG, &hardware_rev); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; msp432_bank->hardware_rev = hardware_rev & 0xFF; @@ -864,11 +869,11 @@ static int msp432_probe(struct flash_bank *bank) msp432_bank->device_type = msp432_device_type(msp432_bank->family_type, msp432_bank->device_id, msp432_bank->hardware_rev); - if (MSP432P4 == msp432_bank->family_type) { + if (msp432_bank->family_type == MSP432P4) { /* Set up MSP432P4 specific flash parameters */ if (is_main) { retval = target_read_u32(target, P4_FLASH_MAIN_SIZE_REG, &size); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; sector_length = P4_SECTOR_LENGTH; @@ -878,7 +883,7 @@ static int msp432_probe(struct flash_bank *bank) msp432_bank->device_type == MSP432P411X_GUESS) { /* MSP432P411x has an info size register, use that for size */ retval = target_read_u32(target, P4_FLASH_INFO_SIZE_REG, &size); - if (ERROR_OK != retval) + if (retval != ERROR_OK) return retval; } else { /* All other MSP432P401x devices have fixed info region size */ @@ -907,7 +912,7 @@ static int msp432_probe(struct flash_bank *bank) if (num_sectors > 0) { bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors); - if (NULL == bank->sectors) + if (!bank->sectors) return ERROR_FAIL; } @@ -933,7 +938,7 @@ static int msp432_probe(struct flash_bank *bank) if (is_main && MSP432P4 == msp432_bank->family_type) { /* Create the info flash bank needed by MSP432P4 variants */ struct flash_bank *info = calloc(sizeof(struct flash_bank), 1); - if (NULL == info) + if (!info) return ERROR_FAIL; /* Create a name for the info bank, append "_1" to main name */ @@ -960,8 +965,8 @@ static int msp432_auto_probe(struct flash_bank *bank) { struct msp432_bank *msp432_bank = bank->driver_priv; - bool is_main = FLASH_BASE == bank->base; - bool is_info = P4_FLASH_INFO_BASE == bank->base; + bool is_main = bank->base == FLASH_BASE; + bool is_info = bank->base == P4_FLASH_INFO_BASE; int retval = ERROR_OK; @@ -975,60 +980,50 @@ static int msp432_auto_probe(struct flash_bank *bank) return retval; } -static int msp432_info(struct flash_bank *bank, char *buf, int buf_size) +static int msp432_info(struct flash_bank *bank, struct command_invocation *cmd) { struct msp432_bank *msp432_bank = bank->driver_priv; - int printed = 0; switch (msp432_bank->device_type) { case MSP432P401X_DEPR: - if (0xFFFF == msp432_bank->device_id) { + if (msp432_bank->device_id == 0xFFFF) { /* Very early pre-production silicon currently deprecated */ - printed = snprintf(buf, buf_size, - "MSP432P401x pre-production device (deprecated silicon)\n" + command_print_sameline(cmd, "MSP432P401x pre-production device (deprecated silicon)\n" SUPPORT_MESSAGE); } else { /* Revision A or B silicon, also deprecated */ - printed = snprintf(buf, buf_size, - "MSP432P401x Device Rev %c (deprecated silicon)\n" + command_print_sameline(cmd, "MSP432P401x Device Rev %c (deprecated silicon)\n" SUPPORT_MESSAGE, (char)msp432_bank->hardware_rev); } break; case MSP432P401X: - printed = snprintf(buf, buf_size, - "MSP432P401x Device Rev %c\n", + command_print_sameline(cmd, "MSP432P401x Device Rev %c\n", (char)msp432_bank->hardware_rev); break; case MSP432P411X: - printed = snprintf(buf, buf_size, - "MSP432P411x Device Rev %c\n", + command_print_sameline(cmd, "MSP432P411x Device Rev %c\n", (char)msp432_bank->hardware_rev); break; case MSP432E401Y: - printed = snprintf(buf, buf_size, "MSP432E401Y Device\n"); + command_print_sameline(cmd, "MSP432E401Y Device\n"); break; case MSP432E411Y: - printed = snprintf(buf, buf_size, "MSP432E411Y Device\n"); + command_print_sameline(cmd, "MSP432E411Y Device\n"); break; case MSP432E4X_GUESS: - printed = snprintf(buf, buf_size, - "Unrecognized MSP432E4 DID0 and DID1 IDs (%08X, %08X)", + command_print_sameline(cmd, + "Unrecognized MSP432E4 DID0 and DID1 IDs (%08" PRIX32 ", %08" PRIX32 ")", msp432_bank->device_id, msp432_bank->hardware_rev); break; case MSP432P401X_GUESS: case MSP432P411X_GUESS: default: - printed = snprintf(buf, buf_size, - "Unrecognized MSP432P4 Device ID and Hardware Rev (%04X, %02X)", + command_print_sameline(cmd, + "Unrecognized MSP432P4 Device ID and Hardware Rev (%04" PRIX32 ", %02" PRIX32 ")", msp432_bank->device_id, msp432_bank->hardware_rev); break; } - buf_size -= printed; - - if (0 > buf_size) - return ERROR_BUF_TOO_SMALL; - return ERROR_OK; } @@ -1040,7 +1035,7 @@ static int msp432_protect_check(struct flash_bank *bank) static void msp432_flash_free_driver_priv(struct flash_bank *bank) { - bool is_main = FLASH_BASE == bank->base; + bool is_main = bank->base == FLASH_BASE; /* A single private struct is shared between main and info banks */ /* Only free it on the call for main bank */