1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2013 by Andes Technology *
5 * Hsiangkai Wang <hkwang@andestech.com> *
6 ***************************************************************************/
12 #include <jtag/adapter.h>
13 #include <jtag/interface.h>
14 #include <jtag/commands.h>
15 #include <transport/transport.h>
16 #include <target/target.h>
17 #include <jtag/aice/aice_transport.h>
20 #define AICE_KHZ_TO_SPEED_MAP_SIZE 16
21 static const int aice_khz_to_speed_map[AICE_KHZ_TO_SPEED_MAP_SIZE] = {
40 static const struct aice_port *aice_port;
41 static struct aice_port_param_s param;
42 static uint32_t retry_times;
43 static uint32_t count_to_check_dbger;
45 /***************************************************************************/
46 /* External interface implementation */
47 static uint32_t aice_target_id_codes[AICE_MAX_NUM_CORE];
48 static uint8_t aice_num_of_target_id_codes;
50 /***************************************************************************/
52 int aice_init_targets(void)
55 struct target *target;
56 struct aice_port_s *aice;
58 LOG_DEBUG("aice_init_targets");
60 if (aice_num_of_target_id_codes == 0) {
61 res = aice_port->api->idcode(aice_target_id_codes, &aice_num_of_target_id_codes);
62 if (res != ERROR_OK) {
63 LOG_ERROR("<-- TARGET ERROR! Failed to identify AndesCore "
64 "JTAG Manufacture ID in the JTAG scan chain. "
65 "Failed to access EDM registers. -->");
70 for (target = all_targets; target; target = target->next) {
71 target->tap->idcode = aice_target_id_codes[target->tap->abs_chain_position];
73 unsigned ii, limit = target->tap->expected_ids_cnt;
76 for (ii = 0; ii < limit; ii++) {
77 uint32_t expected = target->tap->expected_ids[ii];
79 /* treat "-expected-id 0" as a "don't-warn" wildcard */
80 if (!expected || (target->tap->idcode == expected)) {
88 ("aice_init_targets: target not found: idcode: %" PRIx32,
93 aice = calloc(1, sizeof(struct aice_port_s));
94 aice->port = aice_port;
95 aice->coreid = target->tap->abs_chain_position;
97 target->tap->priv = aice;
98 target->tap->hasidcode = 1;
104 /***************************************************************************/
105 /* End of External interface implementation */
109 * 2. get/show version number
112 static int aice_init(void)
114 if (aice_port->api->open(¶m) != ERROR_OK) {
115 LOG_ERROR("Cannot find AICE Interface! Please check "
116 "connection and permissions.");
117 return ERROR_JTAG_INIT_FAILED;
120 aice_port->api->set_retry_times(retry_times);
121 aice_port->api->set_count_to_check_dbger(count_to_check_dbger);
123 LOG_INFO("AICE JTAG Interface ready");
128 /* cleanup aice resource
131 static int aice_quit(void)
133 aice_port->api->close();
137 static int aice_execute_reset(struct jtag_command *cmd)
139 static int last_trst;
140 int retval = ERROR_OK;
142 LOG_DEBUG_IO("reset trst: %d", cmd->cmd.reset->trst);
144 if (cmd->cmd.reset->trst != last_trst) {
145 if (cmd->cmd.reset->trst)
146 retval = aice_port->api->reset();
148 last_trst = cmd->cmd.reset->trst;
154 static int aice_execute_command(struct jtag_command *cmd)
160 retval = aice_execute_reset(cmd);
169 /* aice has no need to implement jtag execution model
171 static int aice_execute_queue(void)
173 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
179 if (aice_execute_command(cmd) != ERROR_OK)
180 retval = ERROR_JTAG_QUEUE_FAILED;
188 /* set jtag frequency(base frequency/frequency divider) to your jtag adapter */
189 static int aice_speed(int speed)
191 return aice_port->api->set_jtag_clock(speed);
194 /* convert jtag adapter frequency(base frequency/frequency divider) to
195 * human readable KHz value */
196 static int aice_speed_div(int speed, int *khz)
198 *khz = aice_khz_to_speed_map[speed];
203 /* convert human readable KHz value to jtag adapter frequency
204 * (base frequency/frequency divider) */
205 static int aice_khz(int khz, int *jtag_speed)
208 for (i = 0 ; i < AICE_KHZ_TO_SPEED_MAP_SIZE ; i++) {
209 if (khz == aice_khz_to_speed_map[i]) {
211 *jtag_speed = i | AICE_TCK_CONTROL_TCK3048;
218 if (i == AICE_KHZ_TO_SPEED_MAP_SIZE) {
219 LOG_INFO("No support the jtag clock: %d", khz);
220 LOG_INFO("Supported jtag clocks are:");
222 for (i = 0 ; i < AICE_KHZ_TO_SPEED_MAP_SIZE ; i++)
223 LOG_INFO("* %d", aice_khz_to_speed_map[i]);
231 int aice_scan_jtag_chain(void)
233 LOG_DEBUG("=== %s ===", __func__);
234 uint8_t num_of_idcode = 0;
235 struct target *target;
237 int res = aice_port->api->idcode(aice_target_id_codes, &num_of_idcode);
238 if (res != ERROR_OK) {
239 LOG_ERROR("<-- TARGET ERROR! Failed to identify AndesCore "
240 "JTAG Manufacture ID in the JTAG scan chain. "
241 "Failed to access EDM registers. -->");
245 for (unsigned int i = 0; i < num_of_idcode; i++)
246 LOG_DEBUG("id_codes[%u] = 0x%" PRIx32, i, aice_target_id_codes[i]);
248 /* Update tap idcode */
249 for (target = all_targets; target; target = target->next)
250 target->tap->idcode = aice_target_id_codes[target->tap->abs_chain_position];
255 /***************************************************************************/
256 /* Command handlers */
257 COMMAND_HANDLER(aice_handle_aice_info_command)
259 LOG_DEBUG("aice_handle_aice_info_command");
261 command_print(CMD, "Description: %s", param.device_desc);
262 command_print(CMD, "Serial number: %s", adapter_get_required_serial());
263 if (strncmp(aice_port->name, "aice_pipe", 9) == 0)
264 command_print(CMD, "Adapter: %s", param.adapter_name);
269 COMMAND_HANDLER(aice_handle_aice_port_command)
271 LOG_DEBUG("aice_handle_aice_port_command");
274 LOG_ERROR("Need exactly one argument to 'aice port'");
275 return ERROR_COMMAND_SYNTAX_ERROR;
278 for (const struct aice_port *l = aice_port_get_list(); l->name; l++) {
279 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
285 LOG_ERROR("No AICE port '%s' found", CMD_ARGV[0]);
289 COMMAND_HANDLER(aice_handle_aice_desc_command)
291 LOG_DEBUG("aice_handle_aice_desc_command");
294 param.device_desc = strdup(CMD_ARGV[0]);
296 LOG_ERROR("expected exactly one argument to aice desc <description>");
301 COMMAND_HANDLER(aice_handle_aice_vid_pid_command)
303 LOG_DEBUG("aice_handle_aice_vid_pid_command");
306 LOG_WARNING("ignoring extra IDs in aice vid_pid (maximum is 1 pair)");
307 return ERROR_COMMAND_SYNTAX_ERROR;
310 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], param.vid);
311 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], param.pid);
316 COMMAND_HANDLER(aice_handle_aice_adapter_command)
318 LOG_DEBUG("aice_handle_aice_adapter_command");
321 param.adapter_name = strdup(CMD_ARGV[0]);
323 LOG_ERROR("expected exactly one argument to aice adapter <adapter-name>");
328 COMMAND_HANDLER(aice_handle_aice_retry_times_command)
330 LOG_DEBUG("aice_handle_aice_retry_times_command");
333 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], retry_times);
335 LOG_ERROR("expected exactly one argument to aice retry_times <num_of_retry>");
340 COMMAND_HANDLER(aice_handle_aice_count_to_check_dbger_command)
342 LOG_DEBUG("aice_handle_aice_count_to_check_dbger_command");
345 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], count_to_check_dbger);
347 LOG_ERROR("expected exactly one argument to aice count_to_check_dbger "
348 "<count_of_checking>");
353 COMMAND_HANDLER(aice_handle_aice_custom_srst_script_command)
355 LOG_DEBUG("aice_handle_aice_custom_srst_script_command");
358 aice_port->api->set_custom_srst_script(CMD_ARGV[0]);
365 COMMAND_HANDLER(aice_handle_aice_custom_trst_script_command)
367 LOG_DEBUG("aice_handle_aice_custom_trst_script_command");
370 aice_port->api->set_custom_trst_script(CMD_ARGV[0]);
377 COMMAND_HANDLER(aice_handle_aice_custom_restart_script_command)
379 LOG_DEBUG("aice_handle_aice_custom_restart_script_command");
382 aice_port->api->set_custom_restart_script(CMD_ARGV[0]);
389 COMMAND_HANDLER(aice_handle_aice_reset_command)
391 LOG_DEBUG("aice_handle_aice_reset_command");
393 return aice_port->api->reset();
397 static const struct command_registration aice_subcommand_handlers[] = {
400 .handler = &aice_handle_aice_info_command,
401 .mode = COMMAND_EXEC,
402 .help = "show aice info",
407 .handler = &aice_handle_aice_port_command,
408 .mode = COMMAND_CONFIG,
409 .help = "set the port of the AICE",
410 .usage = "['aice_pipe'|'aice_usb']",
414 .handler = &aice_handle_aice_desc_command,
415 .mode = COMMAND_CONFIG,
416 .help = "set the aice device description",
417 .usage = "[description string]",
421 .handler = &aice_handle_aice_vid_pid_command,
422 .mode = COMMAND_CONFIG,
423 .help = "the vendor and product ID of the AICE device",
424 .usage = "(vid pid)*",
428 .handler = &aice_handle_aice_adapter_command,
429 .mode = COMMAND_CONFIG,
430 .help = "set the file name of adapter",
431 .usage = "[adapter name]",
434 .name = "retry_times",
435 .handler = &aice_handle_aice_retry_times_command,
436 .mode = COMMAND_CONFIG,
437 .help = "set retry times as AICE timeout",
438 .usage = "num_of_retry",
441 .name = "count_to_check_dbger",
442 .handler = &aice_handle_aice_count_to_check_dbger_command,
443 .mode = COMMAND_CONFIG,
444 .help = "set retry times as checking $DBGER status",
445 .usage = "count_of_checking",
448 .name = "custom_srst_script",
449 .handler = &aice_handle_aice_custom_srst_script_command,
450 .mode = COMMAND_CONFIG,
451 .usage = "script_file_name",
452 .help = "set custom srst script",
455 .name = "custom_trst_script",
456 .handler = &aice_handle_aice_custom_trst_script_command,
457 .mode = COMMAND_CONFIG,
458 .usage = "script_file_name",
459 .help = "set custom trst script",
462 .name = "custom_restart_script",
463 .handler = &aice_handle_aice_custom_restart_script_command,
464 .mode = COMMAND_CONFIG,
465 .usage = "script_file_name",
466 .help = "set custom restart script",
470 .handler = &aice_handle_aice_reset_command,
471 .mode = COMMAND_EXEC,
473 .help = "reset AICE",
475 COMMAND_REGISTRATION_DONE
478 static const struct command_registration aice_command_handlers[] = {
482 .help = "perform aice management",
483 .usage = "[subcommand]",
484 .chain = aice_subcommand_handlers,
486 COMMAND_REGISTRATION_DONE
488 /***************************************************************************/
489 /* End of Command handlers */
491 static struct jtag_interface aice_interface = {
492 .execute_queue = aice_execute_queue,
495 struct adapter_driver aice_adapter_driver = {
497 .transports = aice_transports,
498 .commands = aice_command_handlers,
502 .speed = aice_speed, /* set interface speed */
503 .khz = aice_khz, /* convert khz to interface speed value */
504 .speed_div = aice_speed_div, /* return readable value */
506 .jtag_ops = &aice_interface,