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, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
25 #include <jtag/interface.h>
26 #include <jtag/commands.h>
27 #include <transport/transport.h>
28 #include <target/target.h>
29 #include <jtag/aice/aice_transport.h>
30 #include <jtag/drivers/libusb_common.h>
33 #define AICE_KHZ_TO_SPEED_MAP_SIZE 16
34 static int aice_khz_to_speed_map[AICE_KHZ_TO_SPEED_MAP_SIZE] = {
53 static struct aice_port_s aice;
55 /***************************************************************************/
56 /* External interface implementation */
57 #define AICE_MAX_TARGET_ID_CODES 0x10
58 static uint32_t aice_target_id_codes[AICE_MAX_TARGET_ID_CODES];
59 static uint8_t aice_num_of_target_id_codes;
61 /***************************************************************************/
63 int aice_init_target(struct target *t)
67 LOG_DEBUG("aice_init_target");
69 if (aice_num_of_target_id_codes == 0) {
70 res = aice.port->api->idcode(aice_target_id_codes, &aice_num_of_target_id_codes);
71 if (res != ERROR_OK) {
72 LOG_ERROR("<-- TARGET ERROR! Failed to identify AndesCore "
73 "JTAG Manufacture ID in the JTAG scan chain. "
74 "Failed to access EDM registers. -->");
79 t->tap->idcode = aice_target_id_codes[t->tap->abs_chain_position];
81 unsigned ii, limit = t->tap->expected_ids_cnt;
84 for (ii = 0; ii < limit; ii++) {
85 uint32_t expected = t->tap->expected_ids[ii];
87 /* treat "-expected-id 0" as a "don't-warn" wildcard */
88 if (!expected || (t->tap->idcode == expected)) {
96 ("aice_init_target: target not found: idcode: %x ",
101 t->tap->priv = &aice;
102 t->tap->hasidcode = 1;
107 /***************************************************************************/
108 /* End of External interface implementation */
112 * 2. get/show version number
115 static int aice_init(void)
117 if (ERROR_OK != aice.port->api->open(&(aice.param))) {
118 LOG_ERROR("Cannot find AICE Interface! Please check "
119 "connection and permissions.");
120 return ERROR_JTAG_INIT_FAILED;
123 aice.port->api->set_retry_times(aice.retry_times);
124 aice.port->api->set_count_to_check_dbger(aice.count_to_check_dbger);
126 LOG_INFO("AICE JTAG Interface ready");
131 /* cleanup aice resource
134 static int aice_quit(void)
136 aice.port->api->close();
140 static int aice_execute_reset(struct jtag_command *cmd)
142 static int last_trst;
143 int retval = ERROR_OK;
145 DEBUG_JTAG_IO("reset trst: %i", cmd->cmd.reset->trst);
147 if (cmd->cmd.reset->trst != last_trst) {
148 if (cmd->cmd.reset->trst)
149 retval = aice.port->api->reset();
151 last_trst = cmd->cmd.reset->trst;
157 static int aice_execute_command(struct jtag_command *cmd)
163 retval = aice_execute_reset(cmd);
172 /* aice has no need to implement jtag execution model
174 static int aice_execute_queue(void)
176 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
182 if (aice_execute_command(cmd) != ERROR_OK)
183 retval = ERROR_JTAG_QUEUE_FAILED;
191 /* set jtag frequency(base frequency/frequency divider) to your jtag adapter */
192 static int aice_speed(int speed)
194 return aice.port->api->set_jtag_clock(speed);
197 /* convert jtag adapter frequency(base frequency/frequency divider) to
198 * human readable KHz value */
199 static int aice_speed_div(int speed, int *khz)
201 *khz = aice_khz_to_speed_map[speed];
206 /* convert human readable KHz value to jtag adapter frequency
207 * (base frequency/frequency divider) */
208 static int aice_khz(int khz, int *jtag_speed)
211 for (i = 0 ; i < AICE_KHZ_TO_SPEED_MAP_SIZE ; i++) {
212 if (khz == aice_khz_to_speed_map[i]) {
214 *jtag_speed = i | AICE_TCK_CONTROL_TCK3048;
221 if (i == AICE_KHZ_TO_SPEED_MAP_SIZE) {
222 LOG_INFO("No support the jtag clock: %d", khz);
223 LOG_INFO("Supported jtag clocks are:");
225 for (i = 0 ; i < AICE_KHZ_TO_SPEED_MAP_SIZE ; i++)
226 LOG_INFO("* %d", aice_khz_to_speed_map[i]);
234 /***************************************************************************/
235 /* Command handlers */
236 COMMAND_HANDLER(aice_handle_aice_info_command)
238 LOG_DEBUG("aice_handle_aice_info_command");
240 command_print(CMD_CTX, "Description: %s", aice.param.device_desc);
241 command_print(CMD_CTX, "Serial number: %s", aice.param.serial);
242 if (strncmp(aice.port->name, "aice_pipe", 9) == 0)
243 command_print(CMD_CTX, "Adapter: %s", aice.param.adapter_name);
248 COMMAND_HANDLER(aice_handle_aice_port_command)
250 LOG_DEBUG("aice_handle_aice_port_command");
253 LOG_ERROR("Need exactly one argument to 'aice port'");
254 return ERROR_COMMAND_SYNTAX_ERROR;
257 for (const struct aice_port *l = aice_port_get_list(); l->name; l++) {
258 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
264 LOG_ERROR("No AICE port '%s' found", CMD_ARGV[0]);
268 COMMAND_HANDLER(aice_handle_aice_desc_command)
270 LOG_DEBUG("aice_handle_aice_desc_command");
273 aice.param.device_desc = strdup(CMD_ARGV[0]);
275 LOG_ERROR("expected exactly one argument to aice desc <description>");
280 COMMAND_HANDLER(aice_handle_aice_serial_command)
282 LOG_DEBUG("aice_handle_aice_serial_command");
285 aice.param.serial = strdup(CMD_ARGV[0]);
287 LOG_ERROR("expected exactly one argument to aice serial <serial-number>");
292 COMMAND_HANDLER(aice_handle_aice_vid_pid_command)
294 LOG_DEBUG("aice_handle_aice_vid_pid_command");
297 LOG_WARNING("ignoring extra IDs in aice vid_pid (maximum is 1 pair)");
298 return ERROR_COMMAND_SYNTAX_ERROR;
301 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], aice.param.vid);
302 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], aice.param.pid);
307 COMMAND_HANDLER(aice_handle_aice_adapter_command)
309 LOG_DEBUG("aice_handle_aice_adapter_command");
312 aice.param.adapter_name = strdup(CMD_ARGV[0]);
314 LOG_ERROR("expected exactly one argument to aice adapter <adapter-name>");
319 COMMAND_HANDLER(aice_handle_aice_retry_times_command)
321 LOG_DEBUG("aice_handle_aice_retry_times_command");
324 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], aice.retry_times);
326 LOG_ERROR("expected exactly one argument to aice retry_times <num_of_retry>");
331 COMMAND_HANDLER(aice_handle_aice_count_to_check_dbger_command)
333 LOG_DEBUG("aice_handle_aice_count_to_check_dbger_command");
336 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], aice.count_to_check_dbger);
338 LOG_ERROR("expected exactly one argument to aice count_to_check_dbger "
339 "<count_of_checking>");
344 COMMAND_HANDLER(aice_handle_aice_custom_srst_script_command)
346 LOG_DEBUG("aice_handle_aice_custom_srst_script_command");
349 aice.port->api->set_custom_srst_script(CMD_ARGV[0]);
356 COMMAND_HANDLER(aice_handle_aice_custom_trst_script_command)
358 LOG_DEBUG("aice_handle_aice_custom_trst_script_command");
361 aice.port->api->set_custom_trst_script(CMD_ARGV[0]);
368 COMMAND_HANDLER(aice_handle_aice_custom_restart_script_command)
370 LOG_DEBUG("aice_handle_aice_custom_restart_script_command");
373 aice.port->api->set_custom_restart_script(CMD_ARGV[0]);
380 COMMAND_HANDLER(aice_handle_aice_reset_command)
382 LOG_DEBUG("aice_handle_aice_reset_command");
384 return aice.port->api->reset();
388 static const struct command_registration aice_subcommand_handlers[] = {
391 .handler = &aice_handle_aice_info_command,
392 .mode = COMMAND_EXEC,
393 .help = "show aice info",
394 .usage = "aice info",
398 .handler = &aice_handle_aice_port_command,
399 .mode = COMMAND_CONFIG,
400 .help = "set the port of the AICE",
401 .usage = "aice port ['aice_pipe'|'aice_usb']",
405 .handler = &aice_handle_aice_desc_command,
406 .mode = COMMAND_CONFIG,
407 .help = "set the aice device description",
408 .usage = "aice desc [desciption string]",
412 .handler = &aice_handle_aice_serial_command,
413 .mode = COMMAND_CONFIG,
414 .help = "set the serial number of the AICE device",
415 .usage = "aice serial [serial string]",
419 .handler = &aice_handle_aice_vid_pid_command,
420 .mode = COMMAND_CONFIG,
421 .help = "the vendor and product ID of the AICE device",
422 .usage = "aice vid_pid (vid pid)*",
426 .handler = &aice_handle_aice_adapter_command,
427 .mode = COMMAND_CONFIG,
428 .help = "set the file name of adapter",
429 .usage = "aice adapter [adapter name]",
432 .name = "retry_times",
433 .handler = &aice_handle_aice_retry_times_command,
434 .mode = COMMAND_CONFIG,
435 .help = "set retry times as AICE timeout",
436 .usage = "aice retry_times num_of_retry",
439 .name = "count_to_check_dbger",
440 .handler = &aice_handle_aice_count_to_check_dbger_command,
441 .mode = COMMAND_CONFIG,
442 .help = "set retry times as checking $DBGER status",
443 .usage = "aice count_to_check_dbger count_of_checking",
446 .name = "custom_srst_script",
447 .handler = &aice_handle_aice_custom_srst_script_command,
448 .mode = COMMAND_CONFIG,
449 .usage = "custom_srst_script script_file_name",
450 .help = "set custom srst script",
453 .name = "custom_trst_script",
454 .handler = &aice_handle_aice_custom_trst_script_command,
455 .mode = COMMAND_CONFIG,
456 .usage = "custom_trst_script script_file_name",
457 .help = "set custom trst script",
460 .name = "custom_restart_script",
461 .handler = &aice_handle_aice_custom_restart_script_command,
462 .mode = COMMAND_CONFIG,
463 .usage = "custom_restart_script script_file_name",
464 .help = "set custom restart script",
468 .handler = &aice_handle_aice_reset_command,
469 .mode = COMMAND_EXEC,
470 .usage = "aice reset",
471 .help = "reset AICE",
473 COMMAND_REGISTRATION_DONE
476 static const struct command_registration aice_command_handlers[] = {
480 .help = "perform aice management",
481 .usage = "aice [subcommand]",
482 .chain = aice_subcommand_handlers,
484 COMMAND_REGISTRATION_DONE
486 /***************************************************************************/
487 /* End of Command handlers */
489 struct jtag_interface aice_interface = {
491 .commands = aice_command_handlers,
492 .transports = aice_transports,
495 .execute_queue = aice_execute_queue,
496 .speed = aice_speed, /* set interface speed */
497 .speed_div = aice_speed_div, /* return readable value */
498 .khz = aice_khz, /* convert khz to interface speed value */