openocd: src/flash: replace the GPL-2.0-or-later license tag
[fw/openocd] / src / flash / nor / atsame5.c
index baa86acd09eaeae278e66b451d336d1203d0484c..163d142e32d0986afab3ec999aa8a44abc27f003 100644 (file)
@@ -1,3 +1,5 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
 /***************************************************************************
  *   Copyright (C) 2017 by Tomas Vanek                                    *
  *   vanekt@fbl.cz                                                        *
@@ -5,19 +7,6 @@
  *   Based on at91samd.c                                                   *
  *   Copyright (C) 2013 by Andrey Yurovsky                                 *
  *   Andrey Yurovsky <yurovsky@gmail.com>                                  *
- *                                                                         *
- *   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 <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 #include "helper/binarybuffer.h"
 
 #include <helper/time_support.h>
+#include <jtag/jtag.h>
 #include <target/cortex_m.h>
 
 /* A note to prefixing.
- * Definitions and functions ingerited from at91samd.c without
- * any change retained the original prefix samd_ so they eventualy
+ * Definitions and functions inherited from at91samd.c without
+ * any change retained the original prefix samd_ so they eventually
  * may go to samd_common.h and .c
- * As currently there are olny 3 short functions identical with
+ * As currently there are only 3 short functions identical with
  * the original source, no common file was created. */
 
 #define SAME5_PAGES_PER_BLOCK  16
@@ -113,7 +103,7 @@ struct samd_part {
 };
 
 /* See SAM D5x/E5x Family Silicon Errata and Data Sheet Clarification
- * DS80000748B */
+ * DS80000748K */
 /* Known SAMD51 parts. */
 static const struct samd_part samd51_parts[] = {
        { 0x00, "SAMD51P20A", 1024, 256 },
@@ -134,6 +124,8 @@ static const struct samd_part same51_parts[] = {
        { 0x02, "SAME51J19A", 512, 192 },
        { 0x03, "SAME51J18A", 256, 128 },
        { 0x04, "SAME51J20A", 1024, 256 },
+       { 0x05, "SAME51G19A", 512, 192 },       /* New in rev D */
+       { 0x06, "SAME51G18A", 256, 128 },       /* New in rev D */
 };
 
 /* Known SAME53 parts. */
@@ -143,6 +135,9 @@ static const struct samd_part same53_parts[] = {
        { 0x04, "SAME53J20A", 1024, 256 },
        { 0x05, "SAME53J19A", 512, 192 },
        { 0x06, "SAME53J18A", 256, 128 },
+       { 0x55, "LAN9255/ZMX020", 1024, 256 },
+       { 0x56, "LAN9255/ZMX019", 512, 192 },
+       { 0x57, "LAN9255/ZMX018", 256, 128 },
 };
 
 /* Known SAME54 parts. */
@@ -218,7 +213,7 @@ static const struct samd_part *samd_find_part(uint32_t id)
 {
        uint8_t devsel = SAMD_GET_DEVSEL(id);
        const struct samd_family *family = samd_find_family(id);
-       if (family == NULL)
+       if (!family)
                return NULL;
 
        for (unsigned i = 0; i < family->num_parts; i++) {
@@ -231,7 +226,7 @@ static const struct samd_part *samd_find_part(uint32_t id)
 
 static int same5_protect_check(struct flash_bank *bank)
 {
-       int res, prot_block;
+       int res;
        uint32_t lock;
 
        res = target_read_u32(bank->target,
@@ -240,7 +235,7 @@ static int same5_protect_check(struct flash_bank *bank)
                return res;
 
        /* Lock bits are active-low */
-       for (prot_block = 0; prot_block < bank->num_prot_blocks; prot_block++)
+       for (unsigned int prot_block = 0; prot_block < bank->num_prot_blocks; prot_block++)
                bank->prot_blocks[prot_block].is_protected = !(lock & (1u<<prot_block));
 
        return ERROR_OK;
@@ -285,7 +280,7 @@ static int same5_probe(struct flash_bank *bank)
        }
 
        part = samd_find_part(id);
-       if (part == NULL) {
+       if (!part) {
                LOG_ERROR("Couldn't find part corresponding to DID %08" PRIx32, id);
                return ERROR_FAIL;
        }
@@ -569,10 +564,10 @@ static int same5_modify_user_row(struct target *target, uint32_t value,
                        buf_val, buf_mask, 0, 8);
 }
 
-static int same5_protect(struct flash_bank *bank, int set, int first_prot_bl, int last_prot_bl)
+static int same5_protect(struct flash_bank *bank, int set, unsigned int first,
+               unsigned int last)
 {
        int res = ERROR_OK;
-       int prot_block;
 
        /* We can issue lock/unlock region commands with the target running but
         * the settings won't persist unless we're able to modify the LOCK regions
@@ -582,7 +577,7 @@ static int same5_protect(struct flash_bank *bank, int set, int first_prot_bl, in
                return ERROR_TARGET_NOT_HALTED;
        }
 
-       for (prot_block = first_prot_bl; prot_block <= last_prot_bl; prot_block++) {
+       for (unsigned int prot_block = first; prot_block <= last; prot_block++) {
                if (set != bank->prot_blocks[prot_block].is_protected) {
                        /* Load an address that is within this protection block (we use offset 0) */
                        res = target_write_u32(bank->target,
@@ -606,7 +601,7 @@ static int same5_protect(struct flash_bank *bank, int set, int first_prot_bl, in
        const uint8_t unlock[4] = { 0xff, 0xff, 0xff, 0xff };
        uint8_t mask[4] = { 0, 0, 0, 0 };
 
-       buf_set_u32(mask, first_prot_bl, last_prot_bl + 1 - first_prot_bl, 0xffffffff);
+       buf_set_u32(mask, first, last + 1 - first, 0xffffffff);
 
        res = same5_modify_user_row_masked(bank->target,
                        set ? lock : unlock, mask, 8, 4);
@@ -621,9 +616,10 @@ exit:
        return res;
 }
 
-static int same5_erase(struct flash_bank *bank, int first_sect, int last_sect)
+static int same5_erase(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
-       int res, s;
+       int res;
        struct samd_info *chip = (struct samd_info *)bank->driver_priv;
 
        if (bank->target->state != TARGET_HALTED) {
@@ -636,7 +632,7 @@ static int same5_erase(struct flash_bank *bank, int first_sect, int last_sect)
                return ERROR_FLASH_BANK_NOT_PROBED;
 
        /* For each sector to be erased */
-       for (s = first_sect; s <= last_sect; s++) {
+       for (unsigned int s = first; s <= last; s++) {
                res = same5_erase_block(bank->target, bank->sectors[s].offset);
                if (res != ERROR_OK) {
                        LOG_ERROR("SAM: failed to erase sector %d at 0x%08" PRIx32, s, bank->sectors[s].offset);
@@ -730,9 +726,7 @@ static int same5_write(struct flash_bank *bank, const uint8_t *buffer,
        }
 
 free_pb:
-       if (pb)
-               free(pb);
-
+       free(pb);
        return res;
 }
 
@@ -741,7 +735,7 @@ FLASH_BANK_COMMAND_HANDLER(same5_flash_bank_command)
 {
        if (bank->base != SAMD_FLASH) {
                LOG_ERROR("Address " TARGET_ADDR_FMT " invalid bank address (try "
-                       "0x%08" PRIx32 "[same5] )", bank->base, SAMD_FLASH);
+                       "0x%08x[same5] )", bank->base, SAMD_FLASH);
                return ERROR_FAIL;
        }
 
@@ -796,11 +790,12 @@ COMMAND_HANDLER(same5_handle_userpage_command)
        }
 
        if (CMD_ARGC >= 1) {
-               uint64_t mask = NVMUSERROW_SAM_E5_D5_MASK;
-               uint64_t value = strtoull(CMD_ARGV[0], NULL, 0);
+               uint64_t value, mask = NVMUSERROW_SAM_E5_D5_MASK;
+               COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], value);
 
                if (CMD_ARGC == 2) {
-                       uint64_t mask_temp = strtoull(CMD_ARGV[1], NULL, 0);
+                       uint64_t mask_temp;
+                       COMMAND_PARSE_NUMBER(u64, CMD_ARGV[1], mask_temp);
                        mask &= mask_temp;
                }
 
@@ -836,7 +831,9 @@ COMMAND_HANDLER(same5_handle_bootloader_command)
                return ERROR_FAIL;
 
        if (CMD_ARGC >= 1) {
-               unsigned long size = strtoul(CMD_ARGV[0], NULL, 0);
+               unsigned long size;
+
+               COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[0], size);
                uint32_t code = (size + 8191) / 8192;
                if (code > 15) {
                        command_print(CMD, "Invalid bootloader size.  Please "