1 /***************************************************************************
2 * Copyright (C) 2013 by Andes Technology *
3 * Hsiangkai Wang <hkwang@andestech.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
23 #include <jtag/adapter.h>
24 #include <jtag/interface.h>
25 #include <jtag/commands.h>
26 #include <transport/transport.h>
27 #include <target/target.h>
28 #include <jtag/aice/aice_transport.h>
31 #define AICE_KHZ_TO_SPEED_MAP_SIZE 16
32 static const int aice_khz_to_speed_map[AICE_KHZ_TO_SPEED_MAP_SIZE] = {
51 static const struct aice_port *aice_port;
52 static struct aice_port_param_s param;
53 static uint32_t retry_times;
54 static uint32_t count_to_check_dbger;
56 /***************************************************************************/
57 /* External interface implementation */
58 static uint32_t aice_target_id_codes[AICE_MAX_NUM_CORE];
59 static uint8_t aice_num_of_target_id_codes;
61 /***************************************************************************/
63 int aice_init_targets(void)
66 struct target *target;
67 struct aice_port_s *aice;
69 LOG_DEBUG("aice_init_targets");
71 if (aice_num_of_target_id_codes == 0) {
72 res = aice_port->api->idcode(aice_target_id_codes, &aice_num_of_target_id_codes);
73 if (res != ERROR_OK) {
74 LOG_ERROR("<-- TARGET ERROR! Failed to identify AndesCore "
75 "JTAG Manufacture ID in the JTAG scan chain. "
76 "Failed to access EDM registers. -->");
81 for (target = all_targets; target; target = target->next) {
82 target->tap->idcode = aice_target_id_codes[target->tap->abs_chain_position];
84 unsigned ii, limit = target->tap->expected_ids_cnt;
87 for (ii = 0; ii < limit; ii++) {
88 uint32_t expected = target->tap->expected_ids[ii];
90 /* treat "-expected-id 0" as a "don't-warn" wildcard */
91 if (!expected || (target->tap->idcode == expected)) {
99 ("aice_init_targets: target not found: idcode: %" PRIx32,
100 target->tap->idcode);
104 aice = calloc(1, sizeof(struct aice_port_s));
105 aice->port = aice_port;
106 aice->coreid = target->tap->abs_chain_position;
108 target->tap->priv = aice;
109 target->tap->hasidcode = 1;
115 /***************************************************************************/
116 /* End of External interface implementation */
120 * 2. get/show version number
123 static int aice_init(void)
125 if (aice_port->api->open(¶m) != ERROR_OK) {
126 LOG_ERROR("Cannot find AICE Interface! Please check "
127 "connection and permissions.");
128 return ERROR_JTAG_INIT_FAILED;
131 aice_port->api->set_retry_times(retry_times);
132 aice_port->api->set_count_to_check_dbger(count_to_check_dbger);
134 LOG_INFO("AICE JTAG Interface ready");
139 /* cleanup aice resource
142 static int aice_quit(void)
144 aice_port->api->close();
148 static int aice_execute_reset(struct jtag_command *cmd)
150 static int last_trst;
151 int retval = ERROR_OK;
153 LOG_DEBUG_IO("reset trst: %d", cmd->cmd.reset->trst);
155 if (cmd->cmd.reset->trst != last_trst) {
156 if (cmd->cmd.reset->trst)
157 retval = aice_port->api->reset();
159 last_trst = cmd->cmd.reset->trst;
165 static int aice_execute_command(struct jtag_command *cmd)
171 retval = aice_execute_reset(cmd);
180 /* aice has no need to implement jtag execution model
182 static int aice_execute_queue(void)
184 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
190 if (aice_execute_command(cmd) != ERROR_OK)
191 retval = ERROR_JTAG_QUEUE_FAILED;
199 /* set jtag frequency(base frequency/frequency divider) to your jtag adapter */
200 static int aice_speed(int speed)
202 return aice_port->api->set_jtag_clock(speed);
205 /* convert jtag adapter frequency(base frequency/frequency divider) to
206 * human readable KHz value */
207 static int aice_speed_div(int speed, int *khz)
209 *khz = aice_khz_to_speed_map[speed];
214 /* convert human readable KHz value to jtag adapter frequency
215 * (base frequency/frequency divider) */
216 static int aice_khz(int khz, int *jtag_speed)
219 for (i = 0 ; i < AICE_KHZ_TO_SPEED_MAP_SIZE ; i++) {
220 if (khz == aice_khz_to_speed_map[i]) {
222 *jtag_speed = i | AICE_TCK_CONTROL_TCK3048;
229 if (i == AICE_KHZ_TO_SPEED_MAP_SIZE) {
230 LOG_INFO("No support the jtag clock: %d", khz);
231 LOG_INFO("Supported jtag clocks are:");
233 for (i = 0 ; i < AICE_KHZ_TO_SPEED_MAP_SIZE ; i++)
234 LOG_INFO("* %d", aice_khz_to_speed_map[i]);
242 int aice_scan_jtag_chain(void)
244 LOG_DEBUG("=== %s ===", __func__);
245 uint8_t num_of_idcode = 0;
246 struct target *target;
248 int res = aice_port->api->idcode(aice_target_id_codes, &num_of_idcode);
249 if (res != ERROR_OK) {
250 LOG_ERROR("<-- TARGET ERROR! Failed to identify AndesCore "
251 "JTAG Manufacture ID in the JTAG scan chain. "
252 "Failed to access EDM registers. -->");
256 for (unsigned int i = 0; i < num_of_idcode; i++)
257 LOG_DEBUG("id_codes[%u] = 0x%" PRIx32, i, aice_target_id_codes[i]);
259 /* Update tap idcode */
260 for (target = all_targets; target; target = target->next)
261 target->tap->idcode = aice_target_id_codes[target->tap->abs_chain_position];
266 /***************************************************************************/
267 /* Command handlers */
268 COMMAND_HANDLER(aice_handle_aice_info_command)
270 LOG_DEBUG("aice_handle_aice_info_command");
272 command_print(CMD, "Description: %s", param.device_desc);
273 command_print(CMD, "Serial number: %s", adapter_get_required_serial());
274 if (strncmp(aice_port->name, "aice_pipe", 9) == 0)
275 command_print(CMD, "Adapter: %s", param.adapter_name);
280 COMMAND_HANDLER(aice_handle_aice_port_command)
282 LOG_DEBUG("aice_handle_aice_port_command");
285 LOG_ERROR("Need exactly one argument to 'aice port'");
286 return ERROR_COMMAND_SYNTAX_ERROR;
289 for (const struct aice_port *l = aice_port_get_list(); l->name; l++) {
290 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
296 LOG_ERROR("No AICE port '%s' found", CMD_ARGV[0]);
300 COMMAND_HANDLER(aice_handle_aice_desc_command)
302 LOG_DEBUG("aice_handle_aice_desc_command");
305 param.device_desc = strdup(CMD_ARGV[0]);
307 LOG_ERROR("expected exactly one argument to aice desc <description>");
312 COMMAND_HANDLER(aice_handle_aice_vid_pid_command)
314 LOG_DEBUG("aice_handle_aice_vid_pid_command");
317 LOG_WARNING("ignoring extra IDs in aice vid_pid (maximum is 1 pair)");
318 return ERROR_COMMAND_SYNTAX_ERROR;
321 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], param.vid);
322 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], param.pid);
327 COMMAND_HANDLER(aice_handle_aice_adapter_command)
329 LOG_DEBUG("aice_handle_aice_adapter_command");
332 param.adapter_name = strdup(CMD_ARGV[0]);
334 LOG_ERROR("expected exactly one argument to aice adapter <adapter-name>");
339 COMMAND_HANDLER(aice_handle_aice_retry_times_command)
341 LOG_DEBUG("aice_handle_aice_retry_times_command");
344 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], retry_times);
346 LOG_ERROR("expected exactly one argument to aice retry_times <num_of_retry>");
351 COMMAND_HANDLER(aice_handle_aice_count_to_check_dbger_command)
353 LOG_DEBUG("aice_handle_aice_count_to_check_dbger_command");
356 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], count_to_check_dbger);
358 LOG_ERROR("expected exactly one argument to aice count_to_check_dbger "
359 "<count_of_checking>");
364 COMMAND_HANDLER(aice_handle_aice_custom_srst_script_command)
366 LOG_DEBUG("aice_handle_aice_custom_srst_script_command");
369 aice_port->api->set_custom_srst_script(CMD_ARGV[0]);
376 COMMAND_HANDLER(aice_handle_aice_custom_trst_script_command)
378 LOG_DEBUG("aice_handle_aice_custom_trst_script_command");
381 aice_port->api->set_custom_trst_script(CMD_ARGV[0]);
388 COMMAND_HANDLER(aice_handle_aice_custom_restart_script_command)
390 LOG_DEBUG("aice_handle_aice_custom_restart_script_command");
393 aice_port->api->set_custom_restart_script(CMD_ARGV[0]);
400 COMMAND_HANDLER(aice_handle_aice_reset_command)
402 LOG_DEBUG("aice_handle_aice_reset_command");
404 return aice_port->api->reset();
408 static const struct command_registration aice_subcommand_handlers[] = {
411 .handler = &aice_handle_aice_info_command,
412 .mode = COMMAND_EXEC,
413 .help = "show aice info",
418 .handler = &aice_handle_aice_port_command,
419 .mode = COMMAND_CONFIG,
420 .help = "set the port of the AICE",
421 .usage = "['aice_pipe'|'aice_usb']",
425 .handler = &aice_handle_aice_desc_command,
426 .mode = COMMAND_CONFIG,
427 .help = "set the aice device description",
428 .usage = "[description string]",
432 .handler = &aice_handle_aice_vid_pid_command,
433 .mode = COMMAND_CONFIG,
434 .help = "the vendor and product ID of the AICE device",
435 .usage = "(vid pid)*",
439 .handler = &aice_handle_aice_adapter_command,
440 .mode = COMMAND_CONFIG,
441 .help = "set the file name of adapter",
442 .usage = "[adapter name]",
445 .name = "retry_times",
446 .handler = &aice_handle_aice_retry_times_command,
447 .mode = COMMAND_CONFIG,
448 .help = "set retry times as AICE timeout",
449 .usage = "num_of_retry",
452 .name = "count_to_check_dbger",
453 .handler = &aice_handle_aice_count_to_check_dbger_command,
454 .mode = COMMAND_CONFIG,
455 .help = "set retry times as checking $DBGER status",
456 .usage = "count_of_checking",
459 .name = "custom_srst_script",
460 .handler = &aice_handle_aice_custom_srst_script_command,
461 .mode = COMMAND_CONFIG,
462 .usage = "script_file_name",
463 .help = "set custom srst script",
466 .name = "custom_trst_script",
467 .handler = &aice_handle_aice_custom_trst_script_command,
468 .mode = COMMAND_CONFIG,
469 .usage = "script_file_name",
470 .help = "set custom trst script",
473 .name = "custom_restart_script",
474 .handler = &aice_handle_aice_custom_restart_script_command,
475 .mode = COMMAND_CONFIG,
476 .usage = "script_file_name",
477 .help = "set custom restart script",
481 .handler = &aice_handle_aice_reset_command,
482 .mode = COMMAND_EXEC,
484 .help = "reset AICE",
486 COMMAND_REGISTRATION_DONE
489 static const struct command_registration aice_command_handlers[] = {
493 .help = "perform aice management",
494 .usage = "[subcommand]",
495 .chain = aice_subcommand_handlers,
497 COMMAND_REGISTRATION_DONE
499 /***************************************************************************/
500 /* End of Command handlers */
502 static struct jtag_interface aice_interface = {
503 .execute_queue = aice_execute_queue,
506 struct adapter_driver aice_adapter_driver = {
508 .transports = aice_transports,
509 .commands = aice_command_handlers,
513 .speed = aice_speed, /* set interface speed */
514 .khz = aice_khz, /* convert khz to interface speed value */
515 .speed_div = aice_speed_div, /* return readable value */
517 .jtag_ops = &aice_interface,