openocd: fix SPDX tag format for files .c
[fw/openocd] / src / jtag / aice / aice_interface.c
index bad3c3df87768dd27c7b42b4d10ab38b34c6f3a7..89f82a0aec006d3e4a106568f455f6bbfd5722ea 100644 (file)
@@ -1,37 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2013 by Andes Technology                                *
  *   Hsiangkai Wang <hkwang@andestech.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, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
+#include <jtag/adapter.h>
 #include <jtag/interface.h>
 #include <jtag/commands.h>
 #include <transport/transport.h>
 #include <target/target.h>
 #include <jtag/aice/aice_transport.h>
-#include <jtag/drivers/libusb_common.h>
 #include "aice_usb.h"
 
 #define AICE_KHZ_TO_SPEED_MAP_SIZE     16
-static int aice_khz_to_speed_map[AICE_KHZ_TO_SPEED_MAP_SIZE] = {
+static const int aice_khz_to_speed_map[AICE_KHZ_TO_SPEED_MAP_SIZE] = {
        30000,
        15000,
        7500,
@@ -50,24 +37,28 @@ static int aice_khz_to_speed_map[AICE_KHZ_TO_SPEED_MAP_SIZE] = {
        375,
 };
 
-static struct aice_port_s aice;
+static const struct aice_port *aice_port;
+static struct aice_port_param_s param;
+static uint32_t retry_times;
+static uint32_t count_to_check_dbger;
 
 /***************************************************************************/
 /* External interface implementation */
-#define AICE_MAX_TARGET_ID_CODES       0x10
-static uint32_t aice_target_id_codes[AICE_MAX_TARGET_ID_CODES];
+static uint32_t aice_target_id_codes[AICE_MAX_NUM_CORE];
 static uint8_t aice_num_of_target_id_codes;
 
 /***************************************************************************/
 /* AICE operations */
-int aice_init_target(struct target *t)
+int aice_init_targets(void)
 {
        int res;
+       struct target *target;
+       struct aice_port_s *aice;
 
-       LOG_DEBUG("aice_init_target");
+       LOG_DEBUG("aice_init_targets");
 
        if (aice_num_of_target_id_codes == 0) {
-               res = aice.port->api->idcode(aice_target_id_codes, &aice_num_of_target_id_codes);
+               res = aice_port->api->idcode(aice_target_id_codes, &aice_num_of_target_id_codes);
                if (res != ERROR_OK) {
                        LOG_ERROR("<-- TARGET ERROR! Failed to identify AndesCore "
                                        "JTAG Manufacture ID in the JTAG scan chain. "
@@ -76,30 +67,36 @@ int aice_init_target(struct target *t)
                }
        }
 
-       t->tap->idcode = aice_target_id_codes[t->tap->abs_chain_position];
+       for (target = all_targets; target; target = target->next) {
+               target->tap->idcode = aice_target_id_codes[target->tap->abs_chain_position];
 
-       unsigned ii, limit = t->tap->expected_ids_cnt;
-       int found = 0;
+               unsigned ii, limit = target->tap->expected_ids_cnt;
+               int found = 0;
 
-       for (ii = 0; ii < limit; ii++) {
-               uint32_t expected = t->tap->expected_ids[ii];
+               for (ii = 0; ii < limit; ii++) {
+                       uint32_t expected = target->tap->expected_ids[ii];
 
-               /* treat "-expected-id 0" as a "don't-warn" wildcard */
-               if (!expected || (t->tap->idcode == expected)) {
-                       found = 1;
-                       break;
+                       /* treat "-expected-id 0" as a "don't-warn" wildcard */
+                       if (!expected || (target->tap->idcode == expected)) {
+                               found = 1;
+                               break;
+                       }
                }
-       }
 
-       if (found == 0) {
-               LOG_ERROR
-                       ("aice_init_target: target not found: idcode: %x ",
-                        t->tap->idcode);
-               return ERROR_FAIL;
-       }
+               if (found == 0) {
+                       LOG_ERROR
+                               ("aice_init_targets: target not found: idcode: %" PRIx32,
+                                target->tap->idcode);
+                       return ERROR_FAIL;
+               }
 
-       t->tap->priv = &aice;
-       t->tap->hasidcode = 1;
+               aice = calloc(1, sizeof(struct aice_port_s));
+               aice->port = aice_port;
+               aice->coreid = target->tap->abs_chain_position;
+
+               target->tap->priv = aice;
+               target->tap->hasidcode = 1;
+       }
 
        return ERROR_OK;
 }
@@ -114,14 +111,14 @@ int aice_init_target(struct target *t)
  */
 static int aice_init(void)
 {
-       if (ERROR_OK != aice.port->api->open(&(aice.param))) {
+       if (aice_port->api->open(&param) != ERROR_OK) {
                LOG_ERROR("Cannot find AICE Interface! Please check "
                                "connection and permissions.");
                return ERROR_JTAG_INIT_FAILED;
        }
 
-       aice.port->api->set_retry_times(aice.retry_times);
-       aice.port->api->set_count_to_check_dbger(aice.count_to_check_dbger);
+       aice_port->api->set_retry_times(retry_times);
+       aice_port->api->set_count_to_check_dbger(count_to_check_dbger);
 
        LOG_INFO("AICE JTAG Interface ready");
 
@@ -133,7 +130,7 @@ static int aice_init(void)
  */
 static int aice_quit(void)
 {
-       aice.port->api->close();
+       aice_port->api->close();
        return ERROR_OK;
 }
 
@@ -142,11 +139,11 @@ static int aice_execute_reset(struct jtag_command *cmd)
        static int last_trst;
        int retval = ERROR_OK;
 
-       DEBUG_JTAG_IO("reset trst: %i", cmd->cmd.reset->trst);
+       LOG_DEBUG_IO("reset trst: %d", cmd->cmd.reset->trst);
 
        if (cmd->cmd.reset->trst != last_trst) {
                if (cmd->cmd.reset->trst)
-                       retval = aice.port->api->reset();
+                       retval = aice_port->api->reset();
 
                last_trst = cmd->cmd.reset->trst;
        }
@@ -191,7 +188,7 @@ static int aice_execute_queue(void)
 /* set jtag frequency(base frequency/frequency divider) to your jtag adapter */
 static int aice_speed(int speed)
 {
-       return aice.port->api->set_jtag_clock(speed);
+       return aice_port->api->set_jtag_clock(speed);
 }
 
 /* convert jtag adapter frequency(base frequency/frequency divider) to
@@ -210,7 +207,7 @@ static int aice_khz(int khz, int *jtag_speed)
        int i;
        for (i = 0 ; i < AICE_KHZ_TO_SPEED_MAP_SIZE ; i++) {
                if (khz == aice_khz_to_speed_map[i]) {
-                       if (8 <= i)
+                       if (i >= 8)
                                *jtag_speed = i | AICE_TCK_CONTROL_TCK3048;
                        else
                                *jtag_speed = i;
@@ -231,16 +228,40 @@ static int aice_khz(int khz, int *jtag_speed)
        return ERROR_OK;
 }
 
+int aice_scan_jtag_chain(void)
+{
+       LOG_DEBUG("=== %s ===", __func__);
+       uint8_t num_of_idcode = 0;
+       struct target *target;
+
+       int res = aice_port->api->idcode(aice_target_id_codes, &num_of_idcode);
+       if (res != ERROR_OK) {
+               LOG_ERROR("<-- TARGET ERROR! Failed to identify AndesCore "
+                                       "JTAG Manufacture ID in the JTAG scan chain. "
+                                       "Failed to access EDM registers. -->");
+               return res;
+       }
+
+       for (unsigned int i = 0; i < num_of_idcode; i++)
+               LOG_DEBUG("id_codes[%u] = 0x%" PRIx32, i, aice_target_id_codes[i]);
+
+       /* Update tap idcode */
+       for (target = all_targets; target; target = target->next)
+               target->tap->idcode = aice_target_id_codes[target->tap->abs_chain_position];
+
+       return ERROR_OK;
+}
+
 /***************************************************************************/
 /* Command handlers */
 COMMAND_HANDLER(aice_handle_aice_info_command)
 {
        LOG_DEBUG("aice_handle_aice_info_command");
 
-       command_print(CMD_CTX, "Description: %s", aice.param.device_desc);
-       command_print(CMD_CTX, "Serial number: %s", aice.param.serial);
-       if (strncmp(aice.port->name, "aice_pipe", 9) == 0)
-               command_print(CMD_CTX, "Adapter: %s", aice.param.adapter_name);
+       command_print(CMD, "Description: %s", param.device_desc);
+       command_print(CMD, "Serial number: %s", adapter_get_required_serial());
+       if (strncmp(aice_port->name, "aice_pipe", 9) == 0)
+               command_print(CMD, "Adapter: %s", param.adapter_name);
 
        return ERROR_OK;
 }
@@ -256,7 +277,7 @@ COMMAND_HANDLER(aice_handle_aice_port_command)
 
        for (const struct aice_port *l = aice_port_get_list(); l->name; l++) {
                if (strcmp(l->name, CMD_ARGV[0]) == 0) {
-                       aice.port = l;
+                       aice_port = l;
                        return ERROR_OK;
                }
        }
@@ -270,25 +291,13 @@ COMMAND_HANDLER(aice_handle_aice_desc_command)
        LOG_DEBUG("aice_handle_aice_desc_command");
 
        if (CMD_ARGC == 1)
-               aice.param.device_desc = strdup(CMD_ARGV[0]);
+               param.device_desc = strdup(CMD_ARGV[0]);
        else
                LOG_ERROR("expected exactly one argument to aice desc <description>");
 
        return ERROR_OK;
 }
 
-COMMAND_HANDLER(aice_handle_aice_serial_command)
-{
-       LOG_DEBUG("aice_handle_aice_serial_command");
-
-       if (CMD_ARGC == 1)
-               aice.param.serial = strdup(CMD_ARGV[0]);
-       else
-               LOG_ERROR("expected exactly one argument to aice serial <serial-number>");
-
-       return ERROR_OK;
-}
-
 COMMAND_HANDLER(aice_handle_aice_vid_pid_command)
 {
        LOG_DEBUG("aice_handle_aice_vid_pid_command");
@@ -298,8 +307,8 @@ COMMAND_HANDLER(aice_handle_aice_vid_pid_command)
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], aice.param.vid);
-       COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], aice.param.pid);
+       COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], param.vid);
+       COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], param.pid);
 
        return ERROR_OK;
 }
@@ -309,7 +318,7 @@ COMMAND_HANDLER(aice_handle_aice_adapter_command)
        LOG_DEBUG("aice_handle_aice_adapter_command");
 
        if (CMD_ARGC == 1)
-               aice.param.adapter_name = strdup(CMD_ARGV[0]);
+               param.adapter_name = strdup(CMD_ARGV[0]);
        else
                LOG_ERROR("expected exactly one argument to aice adapter <adapter-name>");
 
@@ -321,7 +330,7 @@ COMMAND_HANDLER(aice_handle_aice_retry_times_command)
        LOG_DEBUG("aice_handle_aice_retry_times_command");
 
        if (CMD_ARGC == 1)
-               COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], aice.retry_times);
+               COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], retry_times);
        else
                LOG_ERROR("expected exactly one argument to aice retry_times <num_of_retry>");
 
@@ -333,7 +342,7 @@ COMMAND_HANDLER(aice_handle_aice_count_to_check_dbger_command)
        LOG_DEBUG("aice_handle_aice_count_to_check_dbger_command");
 
        if (CMD_ARGC == 1)
-               COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], aice.count_to_check_dbger);
+               COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], count_to_check_dbger);
        else
                LOG_ERROR("expected exactly one argument to aice count_to_check_dbger "
                                "<count_of_checking>");
@@ -346,7 +355,7 @@ COMMAND_HANDLER(aice_handle_aice_custom_srst_script_command)
        LOG_DEBUG("aice_handle_aice_custom_srst_script_command");
 
        if (CMD_ARGC > 0) {
-               aice.port->api->set_custom_srst_script(CMD_ARGV[0]);
+               aice_port->api->set_custom_srst_script(CMD_ARGV[0]);
                return ERROR_OK;
        }
 
@@ -358,7 +367,7 @@ COMMAND_HANDLER(aice_handle_aice_custom_trst_script_command)
        LOG_DEBUG("aice_handle_aice_custom_trst_script_command");
 
        if (CMD_ARGC > 0) {
-               aice.port->api->set_custom_trst_script(CMD_ARGV[0]);
+               aice_port->api->set_custom_trst_script(CMD_ARGV[0]);
                return ERROR_OK;
        }
 
@@ -370,7 +379,7 @@ COMMAND_HANDLER(aice_handle_aice_custom_restart_script_command)
        LOG_DEBUG("aice_handle_aice_custom_restart_script_command");
 
        if (CMD_ARGC > 0) {
-               aice.port->api->set_custom_restart_script(CMD_ARGV[0]);
+               aice_port->api->set_custom_restart_script(CMD_ARGV[0]);
                return ERROR_OK;
        }
 
@@ -381,7 +390,7 @@ COMMAND_HANDLER(aice_handle_aice_reset_command)
 {
        LOG_DEBUG("aice_handle_aice_reset_command");
 
-       return aice.port->api->reset();
+       return aice_port->api->reset();
 }
 
 
@@ -391,83 +400,76 @@ static const struct command_registration aice_subcommand_handlers[] = {
                .handler = &aice_handle_aice_info_command,
                .mode = COMMAND_EXEC,
                .help = "show aice info",
-               .usage = "aice info",
+               .usage = "",
        },
        {
                .name = "port",
                .handler = &aice_handle_aice_port_command,
                .mode = COMMAND_CONFIG,
                .help = "set the port of the AICE",
-               .usage = "aice port ['aice_pipe'|'aice_usb']",
+               .usage = "['aice_pipe'|'aice_usb']",
        },
        {
                .name = "desc",
                .handler = &aice_handle_aice_desc_command,
                .mode = COMMAND_CONFIG,
                .help = "set the aice device description",
-               .usage = "aice desc [desciption string]",
-       },
-       {
-               .name = "serial",
-               .handler = &aice_handle_aice_serial_command,
-               .mode = COMMAND_CONFIG,
-               .help = "set the serial number of the AICE device",
-               .usage = "aice serial [serial string]",
+               .usage = "[description string]",
        },
        {
                .name = "vid_pid",
                .handler = &aice_handle_aice_vid_pid_command,
                .mode = COMMAND_CONFIG,
                .help = "the vendor and product ID of the AICE device",
-               .usage = "aice vid_pid (vid pid)*",
+               .usage = "(vid pid)*",
        },
        {
                .name = "adapter",
                .handler = &aice_handle_aice_adapter_command,
                .mode = COMMAND_CONFIG,
                .help = "set the file name of adapter",
-               .usage = "aice adapter [adapter name]",
+               .usage = "[adapter name]",
        },
        {
                .name = "retry_times",
                .handler = &aice_handle_aice_retry_times_command,
                .mode = COMMAND_CONFIG,
                .help = "set retry times as AICE timeout",
-               .usage = "aice retry_times num_of_retry",
+               .usage = "num_of_retry",
        },
        {
                .name = "count_to_check_dbger",
                .handler = &aice_handle_aice_count_to_check_dbger_command,
                .mode = COMMAND_CONFIG,
                .help = "set retry times as checking $DBGER status",
-               .usage = "aice count_to_check_dbger count_of_checking",
+               .usage = "count_of_checking",
        },
        {
                .name = "custom_srst_script",
                .handler = &aice_handle_aice_custom_srst_script_command,
                .mode = COMMAND_CONFIG,
-               .usage = "custom_srst_script script_file_name",
+               .usage = "script_file_name",
                .help = "set custom srst script",
        },
        {
                .name = "custom_trst_script",
                .handler = &aice_handle_aice_custom_trst_script_command,
                .mode = COMMAND_CONFIG,
-               .usage = "custom_trst_script script_file_name",
+               .usage = "script_file_name",
                .help = "set custom trst script",
        },
        {
                .name = "custom_restart_script",
                .handler = &aice_handle_aice_custom_restart_script_command,
                .mode = COMMAND_CONFIG,
-               .usage = "custom_restart_script script_file_name",
+               .usage = "script_file_name",
                .help = "set custom restart script",
        },
        {
                .name = "reset",
                .handler = &aice_handle_aice_reset_command,
                .mode = COMMAND_EXEC,
-               .usage = "aice reset",
+               .usage = "",
                .help = "reset AICE",
        },
        COMMAND_REGISTRATION_DONE
@@ -478,7 +480,7 @@ static const struct command_registration aice_command_handlers[] = {
                .name = "aice",
                .mode = COMMAND_ANY,
                .help = "perform aice management",
-               .usage = "aice [subcommand]",
+               .usage = "[subcommand]",
                .chain = aice_subcommand_handlers,
        },
        COMMAND_REGISTRATION_DONE
@@ -486,15 +488,20 @@ static const struct command_registration aice_command_handlers[] = {
 /***************************************************************************/
 /* End of Command handlers */
 
-struct jtag_interface aice_interface = {
+static struct jtag_interface aice_interface = {
+       .execute_queue = aice_execute_queue,
+};
+
+struct adapter_driver aice_adapter_driver = {
        .name = "aice",
-       .commands = aice_command_handlers,
        .transports = aice_transports,
+       .commands = aice_command_handlers,
+
        .init = aice_init,
        .quit = aice_quit,
-       .execute_queue = aice_execute_queue,
        .speed = aice_speed,            /* set interface speed */
-       .speed_div = aice_speed_div,    /* return readable value */
        .khz = aice_khz,                /* convert khz to interface speed value */
-};
+       .speed_div = aice_speed_div,    /* return readable value */
 
+       .jtag_ops = &aice_interface,
+};