openocd: src: fix incorrect SPDX tags
[openocd.git] / src / flash / nor / ambiqmicro.c
index 28dc42d8272f8c3903962a4e4bc1471a556c83c0..4c4a642d5fa4d181967744420b21799197be0024 100644 (file)
@@ -1,3 +1,5 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+
 /******************************************************************************
  *
  * @file ambiqmicro.c
  * Copyright (c) 2015-2016, Ambiq Micro, Inc.
  *
  * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * 3. Neither the name of the copyright holder nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
  *****************************************************************************/
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -123,7 +98,7 @@ static struct {
        uint8_t class;
        uint8_t partno;
        const char *partname;
-} ambiqmicroParts[6] = {
+} ambiqmicro_parts[6] = {
        {0xFF, 0x00, "Unknown"},
        {0x01, 0x00, "Apollo"},
        {0x02, 0x00, "Apollo2"},
@@ -132,7 +107,7 @@ static struct {
        {0x05, 0x00, "Apollo"},
 };
 
-static char *ambiqmicroClassname[6] = {
+static char *ambiqmicro_classname[6] = {
        "Unknown", "Apollo", "Apollo2", "Unknown", "Unknown", "Apollo"
 };
 
@@ -161,10 +136,9 @@ FLASH_BANK_COMMAND_HANDLER(ambiqmicro_flash_bank_command)
        return ERROR_OK;
 }
 
-static int get_ambiqmicro_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_ambiqmicro_info(struct flash_bank *bank, struct command_invocation *cmd)
 {
        struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv;
-       int printed;
        char *classname;
 
        if (!ambiqmicro_info->probed) {
@@ -173,21 +147,17 @@ static int get_ambiqmicro_info(struct flash_bank *bank, char *buf, int buf_size)
        }
 
        /* Check class name in range. */
-       if (ambiqmicro_info->target_class < sizeof(ambiqmicroClassname))
-               classname = ambiqmicroClassname[ambiqmicro_info->target_class];
+       if (ambiqmicro_info->target_class < sizeof(ambiqmicro_classname))
+               classname = ambiqmicro_classname[ambiqmicro_info->target_class];
        else
-               classname = ambiqmicroClassname[0];
+               classname = ambiqmicro_classname[0];
 
-       printed = snprintf(buf,
-               buf_size,
-               "\nAmbiq Micro information: Chip is "
+       command_print_sameline(cmd, "\nAmbiq Micro information: Chip is "
                "class %d (%s) %s\n",
                ambiqmicro_info->target_class,
                classname,
                ambiqmicro_info->target_name);
 
-       if ((printed < 0))
-               return ERROR_BUF_TOO_SMALL;
        return ERROR_OK;
 }
 
@@ -200,24 +170,24 @@ static int ambiqmicro_read_part_info(struct flash_bank *bank)
 {
        struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv;
        struct target *target = bank->target;
-       uint32_t PartNum = 0;
+       uint32_t part_num = 0;
        int retval;
 
        /*
         * Read Part Number.
         */
-       retval = target_read_u32(target, 0x40020000, &PartNum);
+       retval = target_read_u32(target, 0x40020000, &part_num);
        if (retval != ERROR_OK) {
-               LOG_ERROR("status(0x%x):Could not read PartNum.\n", retval);
-               /* Set PartNum to default device */
-               PartNum = 0;
+               LOG_ERROR("status(0x%x):Could not read part_num.\n", retval);
+               /* Set part_num to default device */
+               part_num = 0;
        }
-       LOG_DEBUG("Part number: 0x%x", PartNum);
+       LOG_DEBUG("Part number: 0x%" PRIx32, part_num);
 
        /*
         * Determine device class.
         */
-       ambiqmicro_info->target_class = (PartNum & 0xFF000000) >> 24;
+       ambiqmicro_info->target_class = (part_num & 0xFF000000) >> 24;
 
        switch (ambiqmicro_info->target_class) {
                case 1:         /* 1 - Apollo */
@@ -225,9 +195,9 @@ static int ambiqmicro_read_part_info(struct flash_bank *bank)
                        bank->base = bank->bank_number * 0x40000;
                        ambiqmicro_info->pagesize = 2048;
                        ambiqmicro_info->flshsiz =
-                       apollo_flash_size[(PartNum & 0x00F00000) >> 20];
+                       apollo_flash_size[(part_num & 0x00F00000) >> 20];
                        ambiqmicro_info->sramsiz =
-                       apollo_sram_size[(PartNum & 0x000F0000) >> 16];
+                       apollo_sram_size[(part_num & 0x000F0000) >> 16];
                        ambiqmicro_info->num_pages = ambiqmicro_info->flshsiz /
                        ambiqmicro_info->pagesize;
                        if (ambiqmicro_info->num_pages > 128) {
@@ -253,14 +223,14 @@ static int ambiqmicro_read_part_info(struct flash_bank *bank)
 
        }
 
-       if (ambiqmicro_info->target_class < ARRAY_SIZE(ambiqmicroParts))
+       if (ambiqmicro_info->target_class < ARRAY_SIZE(ambiqmicro_parts))
                ambiqmicro_info->target_name =
-                       ambiqmicroParts[ambiqmicro_info->target_class].partname;
+                       ambiqmicro_parts[ambiqmicro_info->target_class].partname;
        else
                ambiqmicro_info->target_name =
-                       ambiqmicroParts[0].partname;
+                       ambiqmicro_parts[0].partname;
 
-       LOG_DEBUG("num_pages: %d, pagesize: %d, flash: %d, sram: %d",
+       LOG_DEBUG("num_pages: %" PRIu32 ", pagesize: %" PRIu32 ", flash: %" PRIu32 ", sram: %" PRIu32,
                ambiqmicro_info->num_pages,
                ambiqmicro_info->pagesize,
                ambiqmicro_info->flshsiz,
@@ -304,7 +274,7 @@ static int check_flash_status(struct target *target, uint32_t address)
        }
        /* target flash failed, unknown cause. */
        if (retflash != 0) {
-               LOG_ERROR("Flash not happy: status(0x%x)", retflash);
+               LOG_ERROR("Flash not happy: status(0x%" PRIx32 ")", retflash);
                return ERROR_FLASH_OPERATION_FAILED;
        }
        return ERROR_OK;
@@ -432,7 +402,7 @@ static int ambiqmicro_erase(struct flash_bank *bank, unsigned int first,
 {
        struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv;
        struct target *target = bank->target;
-       uint32_t retval = ERROR_OK;
+       int retval;
 
        if (bank->target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
@@ -615,7 +585,7 @@ static int ambiqmicro_write_block(struct flash_bank *bank,
                        break;
                }
 
-               LOG_DEBUG("address = 0x%08x", address);
+               LOG_DEBUG("address = 0x%08" PRIx32, address);
 
                retval = ambiqmicro_exec_command(target, FLASH_PROGRAM_MAIN_FROM_SRAM, 0x1000000c);
                CHECK_STATUS(retval, "error executing ambiqmicro flash write algorithm");
@@ -641,7 +611,7 @@ static int ambiqmicro_write_block(struct flash_bank *bank,
 static int ambiqmicro_write(struct flash_bank *bank, const uint8_t *buffer,
        uint32_t offset, uint32_t count)
 {
-       uint32_t retval;
+       int retval;
 
        /* try using a block write */
        retval = ambiqmicro_write_block(bank, buffer, offset, count);
@@ -654,7 +624,7 @@ static int ambiqmicro_write(struct flash_bank *bank, const uint8_t *buffer,
 static int ambiqmicro_probe(struct flash_bank *bank)
 {
        struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv;
-       uint32_t retval;
+       int retval;
 
        /* If this is a ambiqmicro chip, it has flash; probe() is just
         * to figure out how much is present.  Only do it once.
@@ -698,7 +668,7 @@ static int ambiqmicro_otp_program(struct flash_bank *bank,
 {
        struct target *target = NULL;
        struct ambiqmicro_flash_bank *ambiqmicro_info = NULL;
-       uint32_t retval = ERROR_OK;
+       int retval;
 
        ambiqmicro_info = bank->driver_priv;
        target = bank->target;
@@ -757,7 +727,7 @@ static int ambiqmicro_otp_program(struct flash_bank *bank,
        /*
         * Program OTP.
         */
-       LOG_INFO("Programming OTP offset 0x%08x", offset);
+       LOG_INFO("Programming OTP offset 0x%08" PRIx32, offset);
 
        /*
         * passed pc, addr = ROM function, handle breakpoints, not debugging.
@@ -778,17 +748,13 @@ COMMAND_HANDLER(ambiqmicro_handle_mass_erase_command)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
        struct flash_bank *bank;
-       uint32_t retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
+       if (retval != ERROR_OK)
                return retval;
 
-       if (ambiqmicro_mass_erase(bank) == ERROR_OK) {
-               /* set all sectors as erased */
-               for (unsigned int i = 0; i < bank->num_sectors; i++)
-                       bank->sectors[i].is_erased = 1;
-
+       if (ambiqmicro_mass_erase(bank) == ERROR_OK)
                command_print(CMD, "ambiqmicro mass erase complete");
-       else
+       else
                command_print(CMD, "ambiqmicro mass erase failed");
 
        return ERROR_OK;
@@ -798,7 +764,7 @@ COMMAND_HANDLER(ambiqmicro_handle_page_erase_command)
 {
        struct flash_bank *bank;
        uint32_t first, last;
-       uint32_t retval;
+       int retval;
 
        if (CMD_ARGC < 3)
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -807,7 +773,7 @@ COMMAND_HANDLER(ambiqmicro_handle_page_erase_command)
        COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
 
        retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
        if (ambiqmicro_erase(bank, first, last) == ERROR_OK)
@@ -826,7 +792,7 @@ COMMAND_HANDLER(ambiqmicro_handle_program_otp_command)
 {
        struct flash_bank *bank;
        uint32_t offset, count;
-       uint32_t retval;
+       int retval;
 
        if (CMD_ARGC < 3)
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -834,7 +800,7 @@ COMMAND_HANDLER(ambiqmicro_handle_program_otp_command)
        COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], offset);
        COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
 
-       command_print(CMD, "offset=0x%08x count=%d", offset, count);
+       command_print(CMD, "offset=0x%08" PRIx32 " count=%" PRIu32, offset, count);
 
        CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
 

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)