1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
27 ***************************************************************************/
34 #include "minidriver.h"
35 #include "interface.h"
36 #include "interfaces.h"
37 #include <transport/transport.h>
45 * Holds support for configuring debug adapters from TCl scripts.
48 extern struct jtag_interface
*jtag_interface
;
49 const char * const jtag_only
[] = { "jtag", NULL
};
51 static int jim_adapter_name(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
54 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
56 /* return the name of the interface */
57 /* TCL code might need to know the exact type... */
58 /* FUTURE: we allow this as a means to "set" the interface. */
60 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
-1, "(no params)");
63 const char *name
= jtag_interface
? jtag_interface
->name
: NULL
;
64 Jim_SetResultString(goi
.interp
, name
? : "undefined", -1);
68 static int default_khz(int khz
, int *jtag_speed
)
70 LOG_ERROR("Translation from khz to jtag_speed not implemented");
74 static int default_speed_div(int speed
, int *khz
)
76 LOG_ERROR("Translation from jtag_speed to khz not implemented");
80 static int default_power_dropout(int *dropout
)
82 *dropout
= 0; /* by default we can't detect power dropout */
86 static int default_srst_asserted(int *srst_asserted
)
88 *srst_asserted
= 0; /* by default we can't detect srst asserted */
92 COMMAND_HANDLER(interface_transport_command
)
97 retval
= CALL_COMMAND_HANDLER(transport_list_parse
, &transports
);
98 if (retval
!= ERROR_OK
)
101 retval
= allow_transports(CMD_CTX
, (const char **)transports
);
103 if (retval
!= ERROR_OK
) {
104 for (unsigned i
= 0; transports
[i
]; i
++)
111 COMMAND_HANDLER(handle_interface_list_command
)
113 if (strcmp(CMD_NAME
, "interface_list") == 0 && CMD_ARGC
> 0)
114 return ERROR_COMMAND_SYNTAX_ERROR
;
116 command_print(CMD_CTX
, "The following debug interfaces are available:");
117 for (unsigned i
= 0; NULL
!= jtag_interfaces
[i
]; i
++) {
118 const char *name
= jtag_interfaces
[i
]->name
;
119 command_print(CMD_CTX
, "%u: %s", i
+ 1, name
);
125 COMMAND_HANDLER(handle_interface_command
)
129 /* check whether the interface is already configured */
130 if (jtag_interface
) {
131 LOG_WARNING("Interface already configured, ignoring");
135 /* interface name is a mandatory argument */
136 if (CMD_ARGC
!= 1 || CMD_ARGV
[0][0] == '\0')
137 return ERROR_COMMAND_SYNTAX_ERROR
;
139 for (unsigned i
= 0; NULL
!= jtag_interfaces
[i
]; i
++) {
140 if (strcmp(CMD_ARGV
[0], jtag_interfaces
[i
]->name
) != 0)
143 if (NULL
!= jtag_interfaces
[i
]->commands
) {
144 retval
= register_commands(CMD_CTX
, NULL
,
145 jtag_interfaces
[i
]->commands
);
146 if (ERROR_OK
!= retval
)
150 jtag_interface
= jtag_interfaces
[i
];
152 /* LEGACY SUPPORT ... adapter drivers must declare what
153 * transports they allow. Until they all do so, assume
154 * the legacy drivers are JTAG-only
156 if (!jtag_interface
->transports
)
157 LOG_WARNING("Adapter driver '%s' did not declare "
158 "which transports it allows; assuming "
159 "legacy JTAG-only", jtag_interface
->name
);
160 retval
= allow_transports(CMD_CTX
, jtag_interface
->transports
161 ? jtag_interface
->transports
: jtag_only
);
162 if (ERROR_OK
!= retval
)
165 if (jtag_interface
->khz
== NULL
)
166 jtag_interface
->khz
= default_khz
;
167 if (jtag_interface
->speed_div
== NULL
)
168 jtag_interface
->speed_div
= default_speed_div
;
169 if (jtag_interface
->power_dropout
== NULL
)
170 jtag_interface
->power_dropout
= default_power_dropout
;
171 if (jtag_interface
->srst_asserted
== NULL
)
172 jtag_interface
->srst_asserted
= default_srst_asserted
;
177 /* no valid interface was found (i.e. the configuration option,
178 * didn't match one of the compiled-in interfaces
180 LOG_ERROR("The specified debug interface was not found (%s)",
182 CALL_COMMAND_HANDLER(handle_interface_list_command
);
183 return ERROR_JTAG_INVALID_INTERFACE
;
186 COMMAND_HANDLER(handle_reset_config_command
)
191 /* Original versions cared about the order of these tokens:
192 * reset_config signals [combination [trst_type [srst_type]]]
193 * They also clobbered the previous configuration even on error.
195 * Here we don't care about the order, and only change values
196 * which have been explicitly specified.
198 for (; CMD_ARGC
; CMD_ARGC
--, CMD_ARGV
++) {
203 m
= RESET_SRST_NO_GATING
;
204 if (strcmp(*CMD_ARGV
, "srst_gates_jtag") == 0)
205 /* default: don't use JTAG while SRST asserted */;
206 else if (strcmp(*CMD_ARGV
, "srst_nogate") == 0)
207 tmp
= RESET_SRST_NO_GATING
;
211 LOG_ERROR("extra reset_config %s spec (%s)",
212 "gating", *CMD_ARGV
);
213 return ERROR_COMMAND_SYNTAX_ERROR
;
219 m
= RESET_HAS_TRST
| RESET_HAS_SRST
;
220 if (strcmp(*CMD_ARGV
, "none") == 0)
222 else if (strcmp(*CMD_ARGV
, "trst_only") == 0)
223 tmp
= RESET_HAS_TRST
;
224 else if (strcmp(*CMD_ARGV
, "srst_only") == 0)
225 tmp
= RESET_HAS_SRST
;
226 else if (strcmp(*CMD_ARGV
, "trst_and_srst") == 0)
227 tmp
= RESET_HAS_TRST
| RESET_HAS_SRST
;
231 LOG_ERROR("extra reset_config %s spec (%s)",
232 "signal", *CMD_ARGV
);
233 return ERROR_COMMAND_SYNTAX_ERROR
;
238 /* combination (options for broken wiring) */
239 m
= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
240 if (strcmp(*CMD_ARGV
, "separate") == 0)
241 /* separate reset lines - default */;
242 else if (strcmp(*CMD_ARGV
, "srst_pulls_trst") == 0)
243 tmp
|= RESET_SRST_PULLS_TRST
;
244 else if (strcmp(*CMD_ARGV
, "trst_pulls_srst") == 0)
245 tmp
|= RESET_TRST_PULLS_SRST
;
246 else if (strcmp(*CMD_ARGV
, "combined") == 0)
247 tmp
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
251 LOG_ERROR("extra reset_config %s spec (%s)",
252 "combination", *CMD_ARGV
);
253 return ERROR_COMMAND_SYNTAX_ERROR
;
258 /* trst_type (NOP without HAS_TRST) */
259 m
= RESET_TRST_OPEN_DRAIN
;
260 if (strcmp(*CMD_ARGV
, "trst_open_drain") == 0)
261 tmp
|= RESET_TRST_OPEN_DRAIN
;
262 else if (strcmp(*CMD_ARGV
, "trst_push_pull") == 0)
263 /* push/pull from adapter - default */;
267 LOG_ERROR("extra reset_config %s spec (%s)",
268 "trst_type", *CMD_ARGV
);
269 return ERROR_COMMAND_SYNTAX_ERROR
;
274 /* srst_type (NOP without HAS_SRST) */
275 m
= RESET_SRST_PUSH_PULL
;
276 if (strcmp(*CMD_ARGV
, "srst_push_pull") == 0)
277 tmp
|= RESET_SRST_PUSH_PULL
;
278 else if (strcmp(*CMD_ARGV
, "srst_open_drain") == 0)
279 /* open drain from adapter - default */;
283 LOG_ERROR("extra reset_config %s spec (%s)",
284 "srst_type", *CMD_ARGV
);
285 return ERROR_COMMAND_SYNTAX_ERROR
;
290 /* connect_type - only valid when srst_nogate */
291 m
= RESET_CNCT_UNDER_SRST
;
292 if (strcmp(*CMD_ARGV
, "connect_assert_srst") == 0)
293 tmp
|= RESET_CNCT_UNDER_SRST
;
294 else if (strcmp(*CMD_ARGV
, "connect_deassert_srst") == 0)
295 /* connect normally - default */;
299 LOG_ERROR("extra reset_config %s spec (%s)",
300 "connect_type", *CMD_ARGV
);
301 return ERROR_COMMAND_SYNTAX_ERROR
;
306 /* caller provided nonsense; fail */
307 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV
);
308 return ERROR_COMMAND_SYNTAX_ERROR
;
311 /* Remember the bits which were specified (mask)
312 * and their new values (new_cfg).
318 /* clear previous values of those bits, save new values */
320 int old_cfg
= jtag_get_reset_config();
324 jtag_set_reset_config(new_cfg
);
326 new_cfg
= jtag_get_reset_config();
329 * Display the (now-)current reset mode
333 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
334 switch (new_cfg
& (RESET_HAS_TRST
| RESET_HAS_SRST
)) {
336 modes
[0] = "srst_only";
339 modes
[0] = "trst_only";
341 case RESET_TRST_AND_SRST
:
342 modes
[0] = "trst_and_srst";
349 /* normally SRST and TRST are decoupled; but bugs happen ... */
350 switch (new_cfg
& (RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
)) {
351 case RESET_SRST_PULLS_TRST
:
352 modes
[1] = "srst_pulls_trst";
354 case RESET_TRST_PULLS_SRST
:
355 modes
[1] = "trst_pulls_srst";
357 case RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
:
358 modes
[1] = "combined";
361 modes
[1] = "separate";
365 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
366 if (new_cfg
& RESET_HAS_TRST
) {
367 if (new_cfg
& RESET_TRST_OPEN_DRAIN
)
368 modes
[3] = " trst_open_drain";
370 modes
[3] = " trst_push_pull";
374 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
375 if (new_cfg
& RESET_HAS_SRST
) {
376 if (new_cfg
& RESET_SRST_NO_GATING
)
377 modes
[2] = " srst_nogate";
379 modes
[2] = " srst_gates_jtag";
381 if (new_cfg
& RESET_SRST_PUSH_PULL
)
382 modes
[4] = " srst_push_pull";
384 modes
[4] = " srst_open_drain";
386 if (new_cfg
& RESET_CNCT_UNDER_SRST
)
387 modes
[5] = " connect_assert_srst";
389 modes
[5] = " connect_deassert_srst";
396 command_print(CMD_CTX
, "%s %s%s%s%s%s",
398 modes
[2], modes
[3], modes
[4], modes
[5]);
403 COMMAND_HANDLER(handle_adapter_nsrst_delay_command
)
406 return ERROR_COMMAND_SYNTAX_ERROR
;
409 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
411 jtag_set_nsrst_delay(delay
);
413 command_print(CMD_CTX
, "adapter_nsrst_delay: %u", jtag_get_nsrst_delay());
417 COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command
)
420 return ERROR_COMMAND_SYNTAX_ERROR
;
423 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], width
);
425 jtag_set_nsrst_assert_width(width
);
427 command_print(CMD_CTX
, "adapter_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
431 COMMAND_HANDLER(handle_adapter_khz_command
)
434 return ERROR_COMMAND_SYNTAX_ERROR
;
436 int retval
= ERROR_OK
;
439 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
441 retval
= jtag_config_khz(khz
);
442 if (ERROR_OK
!= retval
)
446 int cur_speed
= jtag_get_speed_khz();
447 retval
= jtag_get_speed_readable(&cur_speed
);
448 if (ERROR_OK
!= retval
)
452 command_print(CMD_CTX
, "adapter speed: %d kHz", cur_speed
);
454 command_print(CMD_CTX
, "adapter speed: RCLK - adaptive");
459 static const struct command_registration interface_command_handlers
[] = {
461 .name
= "adapter_khz",
462 .handler
= handle_adapter_khz_command
,
464 .help
= "With an argument, change to the specified maximum "
465 "jtag speed. For JTAG, 0 KHz signifies adaptive "
467 "With or without argument, display current setting.",
471 .name
= "adapter_name",
473 .jim_handler
= jim_adapter_name
,
474 .help
= "Returns the name of the currently "
475 "selected adapter (driver)",
478 .name
= "adapter_nsrst_delay",
479 .handler
= handle_adapter_nsrst_delay_command
,
481 .help
= "delay after deasserting SRST in ms",
482 .usage
= "[milliseconds]",
485 .name
= "adapter_nsrst_assert_width",
486 .handler
= handle_adapter_nsrst_assert_width_command
,
488 .help
= "delay after asserting SRST in ms",
489 .usage
= "[milliseconds]",
493 .handler
= handle_interface_command
,
494 .mode
= COMMAND_CONFIG
,
495 .help
= "Select a debug adapter interface (driver)",
496 .usage
= "driver_name",
499 .name
= "interface_transports",
500 .handler
= interface_transport_command
,
501 .mode
= COMMAND_CONFIG
,
502 .help
= "Declare transports the interface supports.",
503 .usage
= "transport ... ",
506 .name
= "interface_list",
507 .handler
= handle_interface_list_command
,
509 .help
= "List all built-in debug adapter interfaces (drivers)",
512 .name
= "reset_config",
513 .handler
= handle_reset_config_command
,
515 .help
= "configure adapter reset behavior",
516 .usage
= "[none|trst_only|srst_only|trst_and_srst] "
517 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
518 "[srst_gates_jtag|srst_nogate] "
519 "[trst_push_pull|trst_open_drain] "
520 "[srst_push_pull|srst_open_drain] "
521 "[connect_deassert_srst|connect_assert_srst]",
523 COMMAND_REGISTRATION_DONE
527 * Register the commands which deal with arbitrary debug adapter drivers.
529 * @todo Remove internal assumptions that all debug adapters use JTAG for
530 * transport. Various types and data structures are not named generically.
532 int interface_register_commands(struct command_context
*ctx
)
534 return register_commands(ctx
, NULL
, interface_command_handlers
);