adapter: run at default speed when clock speed not specified
[fw/openocd] / src / jtag / adapter.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de>
4  * Copyright (C) 2007-2010 Ã˜yvind Harboe <oyvind.harboe@zylin.com>
5  * Copyright (C) 2009 SoftPLC Corporation, http://softplc.com, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net>
7  * Copyright (C) 2018 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include "adapter.h"
15 #include "jtag.h"
16 #include "minidriver.h"
17 #include "interface.h"
18 #include "interfaces.h"
19 #include <transport/transport.h>
20
21 #ifdef HAVE_STRINGS_H
22 #include <strings.h>
23 #endif
24
25 /**
26  * @file
27  * Holds support for configuring debug adapters from TCl scripts.
28  */
29
30 struct adapter_driver *adapter_driver;
31 const char * const jtag_only[] = { "jtag", NULL };
32
33 enum adapter_clk_mode {
34         CLOCK_MODE_UNSELECTED = 0,
35         CLOCK_MODE_KHZ,
36         CLOCK_MODE_RCLK
37 };
38
39 #define DEFAULT_CLOCK_SPEED_KHZ         100U
40
41 /**
42  * Adapter configuration
43  */
44 static struct {
45         bool adapter_initialized;
46         char *usb_location;
47         char *serial;
48         enum adapter_clk_mode clock_mode;
49         int speed_khz;
50         int rclk_fallback_speed_khz;
51 } adapter_config;
52
53 bool is_adapter_initialized(void)
54 {
55         return adapter_config.adapter_initialized;
56 }
57
58 /**
59  * Do low-level setup like initializing registers, output signals,
60  * and clocking.
61  */
62 int adapter_init(struct command_context *cmd_ctx)
63 {
64         if (is_adapter_initialized())
65                 return ERROR_OK;
66
67         if (!adapter_driver) {
68                 /* nothing was previously specified by "adapter driver" command */
69                 LOG_ERROR("Debug Adapter has to be specified, "
70                         "see \"adapter driver\" command");
71                 return ERROR_JTAG_INVALID_INTERFACE;
72         }
73
74         int retval;
75
76         if (adapter_config.clock_mode == CLOCK_MODE_UNSELECTED) {
77                 LOG_WARNING("An adapter speed is not selected in the init scripts."
78                         " OpenOCD will try to run the adapter at the low speed (%d kHz)",
79                         DEFAULT_CLOCK_SPEED_KHZ);
80                 LOG_WARNING("To remove this warnings and achieve reasonable communication speed with the target,"
81                     " set \"adapter speed\" or \"jtag_rclk\" in the init scripts.");
82                 retval = adapter_config_khz(DEFAULT_CLOCK_SPEED_KHZ);
83                 if (retval != ERROR_OK)
84                         return ERROR_JTAG_INIT_FAILED;
85         }
86
87         retval = adapter_driver->init();
88         if (retval != ERROR_OK)
89                 return retval;
90         adapter_config.adapter_initialized = true;
91
92         if (!adapter_driver->speed) {
93                 LOG_INFO("This adapter doesn't support configurable speed");
94                 return ERROR_OK;
95         }
96
97         int requested_khz = adapter_get_speed_khz();
98         int actual_khz = requested_khz;
99         int speed_var = 0;
100         retval = adapter_get_speed(&speed_var);
101         if (retval != ERROR_OK)
102                 return retval;
103         retval = adapter_driver->speed(speed_var);
104         if (retval != ERROR_OK)
105                 return retval;
106         retval = adapter_get_speed_readable(&actual_khz);
107         if (retval != ERROR_OK)
108                 LOG_INFO("adapter-specific clock speed value %d", speed_var);
109         else if (actual_khz) {
110                 /* Adaptive clocking -- JTAG-specific */
111                 if ((adapter_config.clock_mode == CLOCK_MODE_RCLK)
112                                 || ((adapter_config.clock_mode == CLOCK_MODE_KHZ) && !requested_khz)) {
113                         LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
114                         , actual_khz);
115                 } else
116                         LOG_INFO("clock speed %d kHz", actual_khz);
117         } else
118                 LOG_INFO("RCLK (adaptive clock speed)");
119
120         return ERROR_OK;
121 }
122
123 int adapter_quit(void)
124 {
125         if (is_adapter_initialized() && adapter_driver->quit) {
126                 /* close the JTAG interface */
127                 int result = adapter_driver->quit();
128                 if (result != ERROR_OK)
129                         LOG_ERROR("failed: %d", result);
130         }
131
132         free(adapter_config.serial);
133         free(adapter_config.usb_location);
134
135         struct jtag_tap *t = jtag_all_taps();
136         while (t) {
137                 struct jtag_tap *n = t->next_tap;
138                 jtag_tap_free(t);
139                 t = n;
140         }
141
142         return ERROR_OK;
143 }
144
145 unsigned int adapter_get_speed_khz(void)
146 {
147         return adapter_config.speed_khz;
148 }
149
150 static int adapter_khz_to_speed(unsigned int khz, int *speed)
151 {
152         LOG_DEBUG("convert khz to adapter specific speed value");
153         adapter_config.speed_khz = khz;
154         if (!is_adapter_initialized())
155                 return ERROR_OK;
156         LOG_DEBUG("have adapter set up");
157         if (!adapter_driver->khz) {
158                 LOG_ERROR("Translation from khz to adapter speed not implemented");
159                 return ERROR_FAIL;
160         }
161         int speed_div1;
162         int retval = adapter_driver->khz(adapter_get_speed_khz(), &speed_div1);
163         if (retval != ERROR_OK)
164                 return retval;
165         *speed = speed_div1;
166         return ERROR_OK;
167 }
168
169 static int adapter_rclk_to_speed(unsigned int fallback_speed_khz, int *speed)
170 {
171         int retval = adapter_khz_to_speed(0, speed);
172         if ((retval != ERROR_OK) && fallback_speed_khz) {
173                 LOG_DEBUG("trying fallback speed...");
174                 retval = adapter_khz_to_speed(fallback_speed_khz, speed);
175         }
176         return retval;
177 }
178
179 static int adapter_set_speed(int speed)
180 {
181         /* this command can be called during CONFIG,
182          * in which case adapter isn't initialized */
183         return is_adapter_initialized() ? adapter_driver->speed(speed) : ERROR_OK;
184 }
185
186 int adapter_config_khz(unsigned int khz)
187 {
188         LOG_DEBUG("handle adapter khz");
189         adapter_config.clock_mode = CLOCK_MODE_KHZ;
190         int speed = 0;
191         int retval = adapter_khz_to_speed(khz, &speed);
192         return (retval != ERROR_OK) ? retval : adapter_set_speed(speed);
193 }
194
195 int adapter_config_rclk(unsigned int fallback_speed_khz)
196 {
197         LOG_DEBUG("handle adapter rclk");
198         adapter_config.clock_mode = CLOCK_MODE_RCLK;
199         adapter_config.rclk_fallback_speed_khz = fallback_speed_khz;
200         int speed = 0;
201         int retval = adapter_rclk_to_speed(fallback_speed_khz, &speed);
202         return (retval != ERROR_OK) ? retval : adapter_set_speed(speed);
203 }
204
205 int adapter_get_speed(int *speed)
206 {
207         switch (adapter_config.clock_mode) {
208                 case CLOCK_MODE_KHZ:
209                         adapter_khz_to_speed(adapter_get_speed_khz(), speed);
210                         break;
211                 case CLOCK_MODE_RCLK:
212                         adapter_rclk_to_speed(adapter_config.rclk_fallback_speed_khz, speed);
213                         break;
214                 default:
215                         LOG_ERROR("BUG: unknown adapter clock mode");
216                         return ERROR_FAIL;
217         }
218         return ERROR_OK;
219 }
220
221 int adapter_get_speed_readable(int *khz)
222 {
223         int speed_var = 0;
224         int retval = adapter_get_speed(&speed_var);
225         if (retval != ERROR_OK)
226                 return retval;
227         if (!is_adapter_initialized())
228                 return ERROR_OK;
229         if (!adapter_driver->speed_div) {
230                 LOG_ERROR("Translation from adapter speed to khz not implemented");
231                 return ERROR_FAIL;
232         }
233         return adapter_driver->speed_div(speed_var, khz);
234 }
235
236 const char *adapter_get_required_serial(void)
237 {
238         return adapter_config.serial;
239 }
240
241 /*
242  * 1 char: bus
243  * 2 * 7 chars: max 7 ports
244  * 1 char: test for overflow
245  * ------
246  * 16 chars
247  */
248 #define USB_MAX_LOCATION_LENGTH         16
249
250 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
251 static void adapter_usb_set_location(const char *location)
252 {
253         if (strnlen(location, USB_MAX_LOCATION_LENGTH) == USB_MAX_LOCATION_LENGTH)
254                 LOG_WARNING("usb location string is too long!!");
255
256         free(adapter_config.usb_location);
257
258         adapter_config.usb_location = strndup(location, USB_MAX_LOCATION_LENGTH);
259 }
260 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
261
262 const char *adapter_usb_get_location(void)
263 {
264         return adapter_config.usb_location;
265 }
266
267 bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len)
268 {
269         size_t path_step, string_length;
270         char *ptr, *loc;
271         bool equal = false;
272
273         if (!adapter_usb_get_location())
274                 return equal;
275
276         /* strtok need non const char */
277         loc = strndup(adapter_usb_get_location(), USB_MAX_LOCATION_LENGTH);
278         string_length = strnlen(loc, USB_MAX_LOCATION_LENGTH);
279
280         ptr = strtok(loc, "-");
281         if (!ptr) {
282                 LOG_WARNING("no '-' in usb path\n");
283                 goto done;
284         }
285
286         string_length -= strnlen(ptr, string_length);
287         /* check bus mismatch */
288         if (atoi(ptr) != dev_bus)
289                 goto done;
290
291         path_step = 0;
292         while (path_step < path_len) {
293                 ptr = strtok(NULL, ".");
294
295                 /* no more tokens in path */
296                 if (!ptr)
297                         break;
298
299                 /* path mismatch at some step */
300                 if (path_step < path_len && atoi(ptr) != port_path[path_step])
301                         break;
302
303                 path_step++;
304                 string_length -= strnlen(ptr, string_length) + 1;
305         };
306
307         /* walked the full path, all elements match */
308         if (path_step == path_len && !string_length)
309                 equal = true;
310
311 done:
312         free(loc);
313         return equal;
314 }
315
316 static int jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
317 {
318         struct jim_getopt_info goi;
319         jim_getopt_setup(&goi, interp, argc-1, argv + 1);
320
321         /* return the name of the interface */
322         /* TCL code might need to know the exact type... */
323         /* FUTURE: we allow this as a means to "set" the interface. */
324         if (goi.argc != 0) {
325                 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
326                 return JIM_ERR;
327         }
328         const char *name = adapter_driver ? adapter_driver->name : NULL;
329         Jim_SetResultString(goi.interp, name ? name : "undefined", -1);
330         return JIM_OK;
331 }
332
333 COMMAND_HANDLER(adapter_transports_command)
334 {
335         char **transports;
336         int retval;
337
338         retval = CALL_COMMAND_HANDLER(transport_list_parse, &transports);
339         if (retval != ERROR_OK)
340                 return retval;
341
342         retval = allow_transports(CMD_CTX, (const char **)transports);
343
344         if (retval != ERROR_OK) {
345                 for (unsigned i = 0; transports[i]; i++)
346                         free(transports[i]);
347                 free(transports);
348         }
349         return retval;
350 }
351
352 COMMAND_HANDLER(handle_adapter_list_command)
353 {
354         if (strcmp(CMD_NAME, "list") == 0 && CMD_ARGC > 0)
355                 return ERROR_COMMAND_SYNTAX_ERROR;
356
357         command_print(CMD, "The following debug adapters are available:");
358         for (unsigned i = 0; adapter_drivers[i]; i++) {
359                 const char *name = adapter_drivers[i]->name;
360                 command_print(CMD, "%u: %s", i + 1, name);
361         }
362
363         return ERROR_OK;
364 }
365
366 COMMAND_HANDLER(handle_adapter_driver_command)
367 {
368         int retval;
369
370         /* check whether the interface is already configured */
371         if (adapter_driver) {
372                 LOG_WARNING("Interface already configured, ignoring");
373                 return ERROR_OK;
374         }
375
376         /* interface name is a mandatory argument */
377         if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
378                 return ERROR_COMMAND_SYNTAX_ERROR;
379
380         for (unsigned i = 0; adapter_drivers[i]; i++) {
381                 if (strcmp(CMD_ARGV[0], adapter_drivers[i]->name) != 0)
382                         continue;
383
384                 if (adapter_drivers[i]->commands) {
385                         retval = register_commands(CMD_CTX, NULL, adapter_drivers[i]->commands);
386                         if (retval != ERROR_OK)
387                                 return retval;
388                 }
389
390                 adapter_driver = adapter_drivers[i];
391
392                 return allow_transports(CMD_CTX, adapter_driver->transports);
393         }
394
395         /* no valid interface was found (i.e. the configuration option,
396          * didn't match one of the compiled-in interfaces
397          */
398         LOG_ERROR("The specified debug interface was not found (%s)",
399                                 CMD_ARGV[0]);
400         CALL_COMMAND_HANDLER(handle_adapter_list_command);
401         return ERROR_JTAG_INVALID_INTERFACE;
402 }
403
404 COMMAND_HANDLER(handle_reset_config_command)
405 {
406         int new_cfg = 0;
407         int mask = 0;
408
409         /* Original versions cared about the order of these tokens:
410          *   reset_config signals [combination [trst_type [srst_type]]]
411          * They also clobbered the previous configuration even on error.
412          *
413          * Here we don't care about the order, and only change values
414          * which have been explicitly specified.
415          */
416         for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
417                 int tmp = 0;
418                 int m;
419
420                 /* gating */
421                 m = RESET_SRST_NO_GATING;
422                 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
423                         /* default: don't use JTAG while SRST asserted */;
424                 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
425                         tmp = RESET_SRST_NO_GATING;
426                 else
427                         m = 0;
428                 if (mask & m) {
429                         LOG_ERROR("extra reset_config %s spec (%s)",
430                                         "gating", *CMD_ARGV);
431                         return ERROR_COMMAND_SYNTAX_ERROR;
432                 }
433                 if (m)
434                         goto next;
435
436                 /* signals */
437                 m = RESET_HAS_TRST | RESET_HAS_SRST;
438                 if (strcmp(*CMD_ARGV, "none") == 0)
439                         tmp = RESET_NONE;
440                 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
441                         tmp = RESET_HAS_TRST;
442                 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
443                         tmp = RESET_HAS_SRST;
444                 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
445                         tmp = RESET_HAS_TRST | RESET_HAS_SRST;
446                 else
447                         m = 0;
448                 if (mask & m) {
449                         LOG_ERROR("extra reset_config %s spec (%s)",
450                                         "signal", *CMD_ARGV);
451                         return ERROR_COMMAND_SYNTAX_ERROR;
452                 }
453                 if (m)
454                         goto next;
455
456                 /* combination (options for broken wiring) */
457                 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
458                 if (strcmp(*CMD_ARGV, "separate") == 0)
459                         /* separate reset lines - default */;
460                 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
461                         tmp |= RESET_SRST_PULLS_TRST;
462                 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
463                         tmp |= RESET_TRST_PULLS_SRST;
464                 else if (strcmp(*CMD_ARGV, "combined") == 0)
465                         tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
466                 else
467                         m = 0;
468                 if (mask & m) {
469                         LOG_ERROR("extra reset_config %s spec (%s)",
470                                         "combination", *CMD_ARGV);
471                         return ERROR_COMMAND_SYNTAX_ERROR;
472                 }
473                 if (m)
474                         goto next;
475
476                 /* trst_type (NOP without HAS_TRST) */
477                 m = RESET_TRST_OPEN_DRAIN;
478                 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
479                         tmp |= RESET_TRST_OPEN_DRAIN;
480                 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
481                         /* push/pull from adapter - default */;
482                 else
483                         m = 0;
484                 if (mask & m) {
485                         LOG_ERROR("extra reset_config %s spec (%s)",
486                                         "trst_type", *CMD_ARGV);
487                         return ERROR_COMMAND_SYNTAX_ERROR;
488                 }
489                 if (m)
490                         goto next;
491
492                 /* srst_type (NOP without HAS_SRST) */
493                 m = RESET_SRST_PUSH_PULL;
494                 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
495                         tmp |= RESET_SRST_PUSH_PULL;
496                 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
497                         /* open drain from adapter - default */;
498                 else
499                         m = 0;
500                 if (mask & m) {
501                         LOG_ERROR("extra reset_config %s spec (%s)",
502                                         "srst_type", *CMD_ARGV);
503                         return ERROR_COMMAND_SYNTAX_ERROR;
504                 }
505                 if (m)
506                         goto next;
507
508                 /* connect_type - only valid when srst_nogate */
509                 m = RESET_CNCT_UNDER_SRST;
510                 if (strcmp(*CMD_ARGV, "connect_assert_srst") == 0)
511                         tmp |= RESET_CNCT_UNDER_SRST;
512                 else if (strcmp(*CMD_ARGV, "connect_deassert_srst") == 0)
513                         /* connect normally - default */;
514                 else
515                         m = 0;
516                 if (mask & m) {
517                         LOG_ERROR("extra reset_config %s spec (%s)",
518                                         "connect_type", *CMD_ARGV);
519                         return ERROR_COMMAND_SYNTAX_ERROR;
520                 }
521                 if (m)
522                         goto next;
523
524                 /* caller provided nonsense; fail */
525                 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
526                 return ERROR_COMMAND_SYNTAX_ERROR;
527
528 next:
529                 /* Remember the bits which were specified (mask)
530                  * and their new values (new_cfg).
531                  */
532                 mask |= m;
533                 new_cfg |= tmp;
534         }
535
536         /* clear previous values of those bits, save new values */
537         if (mask) {
538                 int old_cfg = jtag_get_reset_config();
539
540                 old_cfg &= ~mask;
541                 new_cfg |= old_cfg;
542                 jtag_set_reset_config(new_cfg);
543         } else
544                 new_cfg = jtag_get_reset_config();
545
546         /*
547          * Display the (now-)current reset mode
548          */
549         char *modes[6];
550
551         /* minimal JTAG has neither SRST nor TRST (so that's the default) */
552         switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
553                 case RESET_HAS_SRST:
554                         modes[0] = "srst_only";
555                         break;
556                 case RESET_HAS_TRST:
557                         modes[0] = "trst_only";
558                         break;
559                 case RESET_TRST_AND_SRST:
560                         modes[0] = "trst_and_srst";
561                         break;
562                 default:
563                         modes[0] = "none";
564                         break;
565         }
566
567         /* normally SRST and TRST are decoupled; but bugs happen ... */
568         switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
569                 case RESET_SRST_PULLS_TRST:
570                         modes[1] = "srst_pulls_trst";
571                         break;
572                 case RESET_TRST_PULLS_SRST:
573                         modes[1] = "trst_pulls_srst";
574                         break;
575                 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
576                         modes[1] = "combined";
577                         break;
578                 default:
579                         modes[1] = "separate";
580                         break;
581         }
582
583         /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
584         if (new_cfg & RESET_HAS_TRST) {
585                 if (new_cfg & RESET_TRST_OPEN_DRAIN)
586                         modes[3] = " trst_open_drain";
587                 else
588                         modes[3] = " trst_push_pull";
589         } else
590                 modes[3] = "";
591
592         /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
593         if (new_cfg & RESET_HAS_SRST) {
594                 if (new_cfg & RESET_SRST_NO_GATING)
595                         modes[2] = " srst_nogate";
596                 else
597                         modes[2] = " srst_gates_jtag";
598
599                 if (new_cfg & RESET_SRST_PUSH_PULL)
600                         modes[4] = " srst_push_pull";
601                 else
602                         modes[4] = " srst_open_drain";
603
604                 if (new_cfg & RESET_CNCT_UNDER_SRST)
605                         modes[5] = " connect_assert_srst";
606                 else
607                         modes[5] = " connect_deassert_srst";
608         } else {
609                 modes[2] = "";
610                 modes[4] = "";
611                 modes[5] = "";
612         }
613
614         command_print(CMD, "%s %s%s%s%s%s",
615                         modes[0], modes[1],
616                         modes[2], modes[3], modes[4], modes[5]);
617
618         return ERROR_OK;
619 }
620
621 COMMAND_HANDLER(handle_adapter_srst_delay_command)
622 {
623         if (CMD_ARGC > 1)
624                 return ERROR_COMMAND_SYNTAX_ERROR;
625         if (CMD_ARGC == 1) {
626                 unsigned delay;
627                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
628
629                 jtag_set_nsrst_delay(delay);
630         }
631         command_print(CMD, "adapter srst delay: %u", jtag_get_nsrst_delay());
632         return ERROR_OK;
633 }
634
635 COMMAND_HANDLER(handle_adapter_srst_pulse_width_command)
636 {
637         if (CMD_ARGC > 1)
638                 return ERROR_COMMAND_SYNTAX_ERROR;
639         if (CMD_ARGC == 1) {
640                 unsigned width;
641                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], width);
642
643                 jtag_set_nsrst_assert_width(width);
644         }
645         command_print(CMD, "adapter srst pulse_width: %u", jtag_get_nsrst_assert_width());
646         return ERROR_OK;
647 }
648
649 COMMAND_HANDLER(handle_adapter_speed_command)
650 {
651         if (CMD_ARGC > 1)
652                 return ERROR_COMMAND_SYNTAX_ERROR;
653
654         int retval = ERROR_OK;
655         if (CMD_ARGC == 1) {
656                 unsigned khz = 0;
657                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
658
659                 retval = adapter_config_khz(khz);
660                 if (retval != ERROR_OK)
661                         return retval;
662         }
663
664         int cur_speed = adapter_get_speed_khz();
665         retval = adapter_get_speed_readable(&cur_speed);
666         if (retval != ERROR_OK)
667                 return retval;
668
669         if (cur_speed)
670                 command_print(CMD, "adapter speed: %d kHz", cur_speed);
671         else
672                 command_print(CMD, "adapter speed: RCLK - adaptive");
673
674         return retval;
675 }
676
677 COMMAND_HANDLER(handle_adapter_serial_command)
678 {
679         if (CMD_ARGC != 1)
680                 return ERROR_COMMAND_SYNTAX_ERROR;
681
682         free(adapter_config.serial);
683         adapter_config.serial = strdup(CMD_ARGV[0]);
684         return ERROR_OK;
685 }
686
687 COMMAND_HANDLER(handle_adapter_reset_de_assert)
688 {
689         enum values {
690                 VALUE_UNDEFINED = -1,
691                 VALUE_DEASSERT  = 0,
692                 VALUE_ASSERT    = 1,
693         };
694         enum values value;
695         enum values srst = VALUE_UNDEFINED;
696         enum values trst = VALUE_UNDEFINED;
697         enum reset_types jtag_reset_config = jtag_get_reset_config();
698         char *signal;
699
700         if (CMD_ARGC == 0) {
701                 if (transport_is_jtag()) {
702                         if (jtag_reset_config & RESET_HAS_TRST)
703                                 signal = jtag_get_trst() ? "asserted" : "deasserted";
704                         else
705                                 signal = "not present";
706                         command_print(CMD, "trst %s", signal);
707                 }
708
709                 if (jtag_reset_config & RESET_HAS_SRST)
710                         signal = jtag_get_srst() ? "asserted" : "deasserted";
711                 else
712                         signal = "not present";
713                 command_print(CMD, "srst %s", signal);
714
715                 return ERROR_OK;
716         }
717
718         if (CMD_ARGC != 1 && CMD_ARGC != 3)
719                 return ERROR_COMMAND_SYNTAX_ERROR;
720
721         value = (strcmp(CMD_NAME, "assert") == 0) ? VALUE_ASSERT : VALUE_DEASSERT;
722         if (strcmp(CMD_ARGV[0], "srst") == 0)
723                 srst = value;
724         else if (strcmp(CMD_ARGV[0], "trst") == 0)
725                 trst = value;
726         else
727                 return ERROR_COMMAND_SYNTAX_ERROR;
728
729         if (CMD_ARGC == 3) {
730                 if (strcmp(CMD_ARGV[1], "assert") == 0)
731                         value = VALUE_ASSERT;
732                 else if (strcmp(CMD_ARGV[1], "deassert") == 0)
733                         value = VALUE_DEASSERT;
734                 else
735                         return ERROR_COMMAND_SYNTAX_ERROR;
736
737                 if (strcmp(CMD_ARGV[2], "srst") == 0 && srst == VALUE_UNDEFINED)
738                         srst = value;
739                 else if (strcmp(CMD_ARGV[2], "trst") == 0 && trst == VALUE_UNDEFINED)
740                         trst = value;
741                 else
742                         return ERROR_COMMAND_SYNTAX_ERROR;
743         }
744
745         if (trst == VALUE_UNDEFINED) {
746                 if (transport_is_jtag())
747                         trst = jtag_get_trst() ? VALUE_ASSERT : VALUE_DEASSERT;
748                 else
749                         trst = VALUE_DEASSERT; /* unused, safe value */
750         }
751
752         if (srst == VALUE_UNDEFINED) {
753                 if (jtag_reset_config & RESET_HAS_SRST)
754                         srst = jtag_get_srst() ? VALUE_ASSERT : VALUE_DEASSERT;
755                 else
756                         srst = VALUE_DEASSERT; /* unused, safe value */
757         }
758
759         if (trst == VALUE_ASSERT && !transport_is_jtag()) {
760                 LOG_ERROR("transport has no trst signal");
761                 return ERROR_FAIL;
762         }
763
764         if (srst == VALUE_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
765                 LOG_ERROR("adapter has no srst signal");
766                 return ERROR_FAIL;
767         }
768
769         return adapter_resets((trst == VALUE_DEASSERT) ? TRST_DEASSERT : TRST_ASSERT,
770                                                   (srst == VALUE_DEASSERT) ? SRST_DEASSERT : SRST_ASSERT);
771 }
772
773 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
774 COMMAND_HANDLER(handle_usb_location_command)
775 {
776         if (CMD_ARGC == 1)
777                 adapter_usb_set_location(CMD_ARGV[0]);
778
779         command_print(CMD, "adapter usb location: %s", adapter_usb_get_location());
780
781         return ERROR_OK;
782 }
783 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
784
785 static const struct command_registration adapter_usb_command_handlers[] = {
786 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
787         {
788                 .name = "location",
789                 .handler = &handle_usb_location_command,
790                 .mode = COMMAND_CONFIG,
791                 .help = "display or set the USB bus location of the USB device",
792                 .usage = "[<bus>-port[.port]...]",
793         },
794 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
795         COMMAND_REGISTRATION_DONE
796 };
797
798 static const struct command_registration adapter_srst_command_handlers[] = {
799         {
800                 .name = "delay",
801                 .handler = handle_adapter_srst_delay_command,
802                 .mode = COMMAND_ANY,
803                 .help = "delay after deasserting SRST in ms",
804                 .usage = "[milliseconds]",
805         },
806         {
807                 .name = "pulse_width",
808                 .handler = handle_adapter_srst_pulse_width_command,
809                 .mode = COMMAND_ANY,
810                 .help = "SRST assertion pulse width in ms",
811                 .usage = "[milliseconds]",
812         },
813         COMMAND_REGISTRATION_DONE
814 };
815
816 static const struct command_registration adapter_command_handlers[] = {
817         {
818                 .name = "driver",
819                 .handler = handle_adapter_driver_command,
820                 .mode = COMMAND_CONFIG,
821                 .help = "Select a debug adapter driver",
822                 .usage = "driver_name",
823         },
824         {
825                 .name = "speed",
826                 .handler = handle_adapter_speed_command,
827                 .mode = COMMAND_ANY,
828                 .help = "With an argument, change to the specified maximum "
829                         "jtag speed.  For JTAG, 0 KHz signifies adaptive "
830                         "clocking. "
831                         "With or without argument, display current setting.",
832                 .usage = "[khz]",
833         },
834         {
835                 .name = "serial",
836                 .handler = handle_adapter_serial_command,
837                 .mode = COMMAND_CONFIG,
838                 .help = "Set the serial number of the adapter",
839                 .usage = "serial_string",
840         },
841         {
842                 .name = "list",
843                 .handler = handle_adapter_list_command,
844                 .mode = COMMAND_ANY,
845                 .help = "List all built-in debug adapter drivers",
846                 .usage = "",
847         },
848         {
849                 .name = "name",
850                 .mode = COMMAND_ANY,
851                 .jim_handler = jim_adapter_name,
852                 .help = "Returns the name of the currently "
853                         "selected adapter (driver)",
854         },
855         {
856                 .name = "srst",
857                 .mode = COMMAND_ANY,
858                 .help = "srst adapter command group",
859                 .usage = "",
860                 .chain = adapter_srst_command_handlers,
861         },
862         {
863                 .name = "transports",
864                 .handler = adapter_transports_command,
865                 .mode = COMMAND_CONFIG,
866                 .help = "Declare transports the adapter supports.",
867                 .usage = "transport ...",
868         },
869         {
870                 .name = "usb",
871                 .mode = COMMAND_ANY,
872                 .help = "usb adapter command group",
873                 .usage = "",
874                 .chain = adapter_usb_command_handlers,
875         },
876         {
877                 .name = "assert",
878                 .handler = handle_adapter_reset_de_assert,
879                 .mode = COMMAND_EXEC,
880                 .help = "Controls SRST and TRST lines.",
881                 .usage = "|deassert [srst|trst [assert|deassert srst|trst]]",
882         },
883         {
884                 .name = "deassert",
885                 .handler = handle_adapter_reset_de_assert,
886                 .mode = COMMAND_EXEC,
887                 .help = "Controls SRST and TRST lines.",
888                 .usage = "|assert [srst|trst [deassert|assert srst|trst]]",
889         },
890         COMMAND_REGISTRATION_DONE
891 };
892
893 static const struct command_registration interface_command_handlers[] = {
894         {
895                 .name = "adapter",
896                 .mode = COMMAND_ANY,
897                 .help = "adapter command group",
898                 .usage = "",
899                 .chain = adapter_command_handlers,
900         },
901         {
902                 .name = "reset_config",
903                 .handler = handle_reset_config_command,
904                 .mode = COMMAND_ANY,
905                 .help = "configure adapter reset behavior",
906                 .usage = "[none|trst_only|srst_only|trst_and_srst] "
907                         "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
908                         "[srst_gates_jtag|srst_nogate] "
909                         "[trst_push_pull|trst_open_drain] "
910                         "[srst_push_pull|srst_open_drain] "
911                         "[connect_deassert_srst|connect_assert_srst]",
912         },
913         COMMAND_REGISTRATION_DONE
914 };
915
916 /**
917  * Register the commands which deal with arbitrary debug adapter drivers.
918  *
919  * @todo Remove internal assumptions that all debug adapters use JTAG for
920  * transport.  Various types and data structures are not named generically.
921  */
922 int adapter_register_commands(struct command_context *ctx)
923 {
924         return register_commands(ctx, NULL, interface_command_handlers);
925 }