added t/nsrst_assert_width commands
[fw/openocd] / src / jtag / core.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   Copyright (C) 2009 SoftPLC Corporation                                *
9  *       http://softplc.com                                                *
10  *   dick@softplc.com                                                      *
11  *                                                                         *
12  *   Copyright (C) 2009 Zachary T Welch                                    *
13  *   zw@superlucidity.net                                                  *
14  *                                                                         *
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.                                   *
19  *                                                                         *
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.                          *
24  *                                                                         *
25  *   You should have received a copy of the GNU General Public License     *
26  *   along with this program; if not, write to the                         *
27  *   Free Software Foundation, Inc.,                                       *
28  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
29  ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "jtag.h"
35 #include "minidriver.h"
36 #include "interface.h"
37
38 #ifdef HAVE_STRINGS_H
39 #include <strings.h>
40 #endif
41
42
43 /// The number of JTAG queue flushes (for profiling and debugging purposes).
44 static int jtag_flush_queue_count;
45
46 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
47                 int in_num_fields, scan_field_t *in_fields, tap_state_t state);
48
49 /**
50  * The jtag_error variable is set when an error occurs while executing
51  * the queue.  Application code may set this using jtag_set_error(),
52  * when an error occurs during processing that should be reported during
53  * jtag_execute_queue().
54  *
55  * Tts value may be checked with jtag_get_error() and cleared with
56  * jtag_error_clear().  This value is returned (and cleared) by
57  * jtag_execute_queue().
58  */
59 static int jtag_error = ERROR_OK;
60
61 static const char *jtag_event_strings[] =
62 {
63         [JTAG_TRST_ASSERTED] = "TAP reset",
64         [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
65         [JTAG_TAP_EVENT_POST_RESET] = "TAP post reset",
66         [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
67 };
68
69 /*
70  * JTAG adapters must initialize with TRST and SRST de-asserted
71  * (they're negative logic, so that means *high*)
72  */
73 static int jtag_trst = 0;
74 static int jtag_srst = 0;
75
76 /**
77  * List all TAPs that have been created.
78  */
79 static jtag_tap_t *__jtag_all_taps = NULL;
80 /**
81  * The number of TAPs in the __jtag_all_taps list, used to track the
82  * assigned chain position to new TAPs
83  */
84 static unsigned jtag_num_taps = 0;
85
86 static enum reset_types jtag_reset_config = RESET_NONE;
87 static tap_state_t cmd_queue_end_state = TAP_RESET;
88 tap_state_t cmd_queue_cur_state = TAP_RESET;
89
90 static bool jtag_verify_capture_ir = true;
91 static int jtag_verify = 1;
92
93 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
94 static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
95 static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
96 static int jtag_nsrst_assert_width = 0; /* width of assertion */
97 static int jtag_ntrst_assert_width = 0; /* width of assertion */
98
99 typedef struct jtag_event_callback_s
100 {
101         jtag_event_handler_t          callback;
102         void*                         priv;
103         struct jtag_event_callback_s* next;
104 } jtag_event_callback_t;
105
106 /* callbacks to inform high-level handlers about JTAG state changes */
107 static jtag_event_callback_t *jtag_event_callbacks;
108
109 /* speed in kHz*/
110 static int speed_khz = 0;
111 /* speed to fallback to when RCLK is requested but not supported */
112 static int rclk_fallback_speed_khz = 0;
113 static enum {CLOCK_MODE_SPEED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
114 static int jtag_speed = 0;
115
116 static struct jtag_interface_s *jtag = NULL;
117
118 /* configuration */
119 jtag_interface_t *jtag_interface = NULL;
120
121 void jtag_set_error(int error)
122 {
123         if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
124                 return;
125         jtag_error = error;
126 }
127 int jtag_get_error(void)
128 {
129         return jtag_error;
130 }
131 int jtag_error_clear(void)
132 {
133         int temp = jtag_error;
134         jtag_error = ERROR_OK;
135         return temp;
136 }
137
138
139 jtag_tap_t *jtag_all_taps(void)
140 {
141         return __jtag_all_taps;
142 };
143
144 unsigned jtag_tap_count(void)
145 {
146         return jtag_num_taps;
147 }
148
149 unsigned jtag_tap_count_enabled(void)
150 {
151         jtag_tap_t *t = jtag_all_taps();
152         unsigned n = 0;
153         while (t)
154         {
155                 if (t->enabled)
156                         n++;
157                 t = t->next_tap;
158         }
159         return n;
160 }
161
162 /// Append a new TAP to the chain of all taps.
163 void jtag_tap_add(struct jtag_tap_s *t)
164 {
165         t->abs_chain_position = jtag_num_taps++;
166
167         jtag_tap_t **tap = &__jtag_all_taps;
168         while (*tap != NULL)
169                 tap = &(*tap)->next_tap;
170         *tap = t;
171 }
172
173 /* returns a pointer to the n-th device in the scan chain */
174 static inline jtag_tap_t *jtag_tap_by_position(unsigned n)
175 {
176         jtag_tap_t *t = jtag_all_taps();
177
178         while (t && n-- > 0)
179                 t = t->next_tap;
180
181         return t;
182 }
183
184 jtag_tap_t *jtag_tap_by_string(const char *s)
185 {
186         /* try by name first */
187         jtag_tap_t *t = jtag_all_taps();
188
189         while (t)
190         {
191                 if (0 == strcmp(t->dotted_name, s))
192                         return t;
193                 t = t->next_tap;
194         }
195
196         /* no tap found by name, so try to parse the name as a number */
197         unsigned n;
198         if (parse_uint(s, &n) != ERROR_OK)
199                 return NULL;
200
201         /* FIXME remove this numeric fallback code late June 2010, along
202          * with all info in the User's Guide that TAPs have numeric IDs.
203          * Also update "scan_chain" output to not display the numbers.
204          */
205         t = jtag_tap_by_position(n);
206         if (t)
207                 LOG_WARNING("Specify TAP '%s' by name, not number %u",
208                         t->dotted_name, n);
209
210         return t;
211 }
212
213 jtag_tap_t *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
214 {
215         const char *cp = Jim_GetString(o, NULL);
216         jtag_tap_t *t = cp ? jtag_tap_by_string(cp) : NULL;
217         if (NULL == cp)
218                 cp = "(unknown)";
219         if (NULL == t)
220                 Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
221         return t;
222 }
223
224 jtag_tap_t* jtag_tap_next_enabled(jtag_tap_t* p)
225 {
226         p = p ? p->next_tap : jtag_all_taps();
227         while (p)
228         {
229                 if (p->enabled)
230                         return p;
231                 p = p->next_tap;
232         }
233         return NULL;
234 }
235
236 const char *jtag_tap_name(const jtag_tap_t *tap)
237 {
238         return (tap == NULL) ? "(unknown)" : tap->dotted_name;
239 }
240
241
242 int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
243 {
244         jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
245
246         if (callback == NULL)
247         {
248                 return ERROR_INVALID_ARGUMENTS;
249         }
250
251         if (*callbacks_p)
252         {
253                 while ((*callbacks_p)->next)
254                         callbacks_p = &((*callbacks_p)->next);
255                 callbacks_p = &((*callbacks_p)->next);
256         }
257
258         (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
259         (*callbacks_p)->callback = callback;
260         (*callbacks_p)->priv = priv;
261         (*callbacks_p)->next = NULL;
262
263         return ERROR_OK;
264 }
265
266 int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
267 {
268         jtag_event_callback_t **callbacks_p;
269         jtag_event_callback_t **next;
270
271         if (callback == NULL)
272         {
273                 return ERROR_INVALID_ARGUMENTS;
274         }
275
276         for (callbacks_p = &jtag_event_callbacks;
277                         *callbacks_p != NULL;
278                         callbacks_p = next)
279         {
280                 next = &((*callbacks_p)->next);
281
282                 if ((*callbacks_p)->priv != priv)
283                         continue;
284
285                 if ((*callbacks_p)->callback == callback)
286                 {
287                         free(*callbacks_p);
288                         *callbacks_p = *next;
289                 }
290         }
291
292         return ERROR_OK;
293 }
294
295 int jtag_call_event_callbacks(enum jtag_event event)
296 {
297         jtag_event_callback_t *callback = jtag_event_callbacks;
298
299         LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
300
301         while (callback)
302         {
303                 jtag_event_callback_t *next;
304
305                 /* callback may remove itself */
306                 next = callback->next;
307                 callback->callback(event, callback->priv);
308                 callback = next;
309         }
310
311         return ERROR_OK;
312 }
313
314 static void jtag_checks(void)
315 {
316         assert(jtag_trst == 0);
317 }
318
319 static void jtag_prelude(tap_state_t state)
320 {
321         jtag_checks();
322
323         assert(state != TAP_INVALID);
324
325         cmd_queue_cur_state = state;
326 }
327
328 void jtag_alloc_in_value32(scan_field_t *field)
329 {
330         interface_jtag_alloc_in_value32(field);
331 }
332
333 void jtag_add_ir_scan_noverify(int in_count, const scan_field_t *in_fields,
334                 tap_state_t state)
335 {
336         jtag_prelude(state);
337
338         int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
339         jtag_set_error(retval);
340 }
341
342
343 void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
344 {
345         assert(state != TAP_RESET);
346
347         if (jtag_verify && jtag_verify_capture_ir)
348         {
349                 /* 8 x 32 bit id's is enough for all invocations */
350
351                 for (int j = 0; j < in_num_fields; j++)
352                 {
353                         /* if we are to run a verification of the ir scan, we need to get the input back.
354                          * We may have to allocate space if the caller didn't ask for the input back.
355                          */
356                         in_fields[j].check_value = in_fields[j].tap->expected;
357                         in_fields[j].check_mask = in_fields[j].tap->expected_mask;
358                 }
359                 jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
360         } else
361         {
362                 jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
363         }
364 }
365
366 void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields,
367                 tap_state_t state)
368 {
369         assert(state != TAP_RESET);
370
371         jtag_prelude(state);
372
373         int retval = interface_jtag_add_plain_ir_scan(
374                         in_num_fields, in_fields, state);
375         jtag_set_error(retval);
376 }
377
378 void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
379 {
380         interface_jtag_add_callback(f, data0);
381 }
382
383 void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0,
384                 jtag_callback_data_t data1, jtag_callback_data_t data2,
385                 jtag_callback_data_t data3)
386 {
387         interface_jtag_add_callback4(f, data0, data1, data2, data3);
388 }
389
390 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
391                 uint8_t *in_check_mask, int num_bits);
392
393 static int jtag_check_value_mask_callback(jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
394 {
395         return jtag_check_value_inner((uint8_t *)data0, (uint8_t *)data1, (uint8_t *)data2, (int)data3);
396 }
397
398 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
399                 int in_num_fields, scan_field_t *in_fields, tap_state_t state)
400 {
401         for (int i = 0; i < in_num_fields; i++)
402         {
403                 struct scan_field_s *field = &in_fields[i];
404                 field->allocated = 0;
405                 field->modified = 0;
406                 if (field->check_value || field->in_value)
407                         continue;
408                 interface_jtag_add_scan_check_alloc(field);
409                 field->modified = 1;
410         }
411
412         jtag_add_scan(in_num_fields, in_fields, state);
413
414         for (int i = 0; i < in_num_fields; i++)
415         {
416                 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
417                 {
418                         /* this is synchronous for a minidriver */
419                         jtag_add_callback4(jtag_check_value_mask_callback, (jtag_callback_data_t)in_fields[i].in_value,
420                                 (jtag_callback_data_t)in_fields[i].check_value,
421                                 (jtag_callback_data_t)in_fields[i].check_mask,
422                                 (jtag_callback_data_t)in_fields[i].num_bits);
423                 }
424                 if (in_fields[i].allocated)
425                 {
426                         free(in_fields[i].in_value);
427                 }
428                 if (in_fields[i].modified)
429                 {
430                         in_fields[i].in_value = NULL;
431                 }
432         }
433 }
434
435 void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
436 {
437         if (jtag_verify)
438         {
439                 jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
440         } else
441         {
442                 jtag_add_dr_scan(in_num_fields, in_fields, state);
443         }
444 }
445
446
447 void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
448                 tap_state_t state)
449 {
450         assert(state != TAP_RESET);
451
452         jtag_prelude(state);
453
454         int retval;
455         retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
456         jtag_set_error(retval);
457 }
458
459 void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
460                 tap_state_t state)
461 {
462         assert(state != TAP_RESET);
463
464         jtag_prelude(state);
465
466         int retval;
467         retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
468         jtag_set_error(retval);
469 }
470
471 void jtag_add_dr_out(jtag_tap_t* tap,
472                 int num_fields, const int* num_bits, const uint32_t* value,
473                 tap_state_t end_state)
474 {
475         assert(end_state != TAP_RESET);
476         assert(end_state != TAP_INVALID);
477
478         cmd_queue_cur_state = end_state;
479
480         interface_jtag_add_dr_out(tap,
481                         num_fields, num_bits, value,
482                         end_state);
483 }
484
485 void jtag_add_tlr(void)
486 {
487         jtag_prelude(TAP_RESET);
488         jtag_set_error(interface_jtag_add_tlr());
489
490         /* NOTE: order here matches TRST path in jtag_add_reset() */
491         jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
492         jtag_notify_reset();
493 }
494
495 void jtag_add_pathmove(int num_states, const tap_state_t *path)
496 {
497         tap_state_t cur_state = cmd_queue_cur_state;
498
499         /* the last state has to be a stable state */
500         if (!tap_is_state_stable(path[num_states - 1]))
501         {
502                 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
503                 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
504                 return;
505         }
506
507         for (int i = 0; i < num_states; i++)
508         {
509                 if (path[i] == TAP_RESET)
510                 {
511                         LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
512                         jtag_set_error(ERROR_JTAG_STATE_INVALID);
513                         return;
514                 }
515
516                 if (tap_state_transition(cur_state, true)  != path[i]
517                   && tap_state_transition(cur_state, false) != path[i])
518                 {
519                         LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
520                                         tap_state_name(cur_state), tap_state_name(path[i]));
521                         jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
522                         return;
523                 }
524                 cur_state = path[i];
525         }
526
527         jtag_checks();
528
529         jtag_set_error(interface_jtag_add_pathmove(num_states, path));
530         cmd_queue_cur_state = path[num_states - 1];
531 }
532
533 int jtag_add_statemove(tap_state_t goal_state)
534 {
535         tap_state_t cur_state = cmd_queue_cur_state;
536
537         LOG_DEBUG("cur_state=%s goal_state=%s",
538                 tap_state_name(cur_state),
539                 tap_state_name(goal_state));
540
541
542         if (goal_state == cur_state)
543                 ;       /* nothing to do */
544         else if (goal_state == TAP_RESET)
545         {
546                 jtag_add_tlr();
547         }
548         else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state))
549         {
550                 unsigned tms_bits  = tap_get_tms_path(cur_state, goal_state);
551                 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
552                 tap_state_t moves[8];
553                 assert(tms_count < DIM(moves));
554
555                 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
556                 {
557                         bool bit = tms_bits & 1;
558
559                         cur_state = tap_state_transition(cur_state, bit);
560                         moves[i] = cur_state;
561                 }
562
563                 jtag_add_pathmove(tms_count, moves);
564         }
565         else if (tap_state_transition(cur_state, true)  == goal_state
566                 ||   tap_state_transition(cur_state, false) == goal_state)
567         {
568                 jtag_add_pathmove(1, &goal_state);
569         }
570
571         else
572                 return ERROR_FAIL;
573
574         return ERROR_OK;
575 }
576
577 void jtag_add_runtest(int num_cycles, tap_state_t state)
578 {
579         jtag_prelude(state);
580         jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
581 }
582
583
584 void jtag_add_clocks(int num_cycles)
585 {
586         if (!tap_is_state_stable(cmd_queue_cur_state))
587         {
588                  LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
589                                  tap_state_name(cmd_queue_cur_state));
590                  jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
591                  return;
592         }
593
594         if (num_cycles > 0)
595         {
596                 jtag_checks();
597                 jtag_set_error(interface_jtag_add_clocks(num_cycles));
598         }
599 }
600
601 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
602 {
603         int trst_with_tlr = 0;
604         int new_srst = 0;
605         int new_trst = 0;
606
607         /* Without SRST, we must use target-specific JTAG operations
608          * on each target; callers should not be requesting SRST when
609          * that signal doesn't exist.
610          *
611          * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
612          * can kick in even if the JTAG adapter can't drive TRST.
613          */
614         if (req_srst) {
615                 if (!(jtag_reset_config & RESET_HAS_SRST)) {
616                         LOG_ERROR("BUG: can't assert SRST");
617                         jtag_set_error(ERROR_FAIL);
618                         return;
619                 }
620                 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
621                                 && !req_tlr_or_trst) {
622                         LOG_ERROR("BUG: can't assert only SRST");
623                         jtag_set_error(ERROR_FAIL);
624                         return;
625                 }
626                 new_srst = 1;
627         }
628
629         /* JTAG reset (entry to TAP_RESET state) can always be achieved
630          * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
631          * state first.  TRST accelerates it, and bypasses those states.
632          *
633          * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
634          * can kick in even if the JTAG adapter can't drive SRST.
635          */
636         if (req_tlr_or_trst) {
637                 if (!(jtag_reset_config & RESET_HAS_TRST))
638                         trst_with_tlr = 1;
639                 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
640                                 && !req_srst)
641                         trst_with_tlr = 1;
642                 else
643                         new_trst = 1;
644         }
645
646         /* Maybe change TRST and/or SRST signal state */
647         if (jtag_srst != new_srst || jtag_trst != new_trst) {
648                 int retval;
649
650                 retval = interface_jtag_add_reset(new_trst, new_srst);
651                 if (retval != ERROR_OK)
652                         jtag_set_error(retval);
653                 else
654                         retval = jtag_execute_queue();
655
656                 if (retval != ERROR_OK) {
657                         LOG_ERROR("TRST/SRST error %d", retval);
658                         return;
659                 }
660         }
661
662         /* SRST resets everything hooked up to that signal */
663         if (jtag_srst != new_srst) {
664                 jtag_srst = new_srst;
665                 if (jtag_srst)
666                 {
667                         LOG_DEBUG("SRST line asserted");
668                         if (jtag_nsrst_assert_width)
669                                 jtag_add_sleep(jtag_nsrst_assert_width * 1000);
670                 }
671                 else {
672                         LOG_DEBUG("SRST line released");
673                         if (jtag_nsrst_delay)
674                                 jtag_add_sleep(jtag_nsrst_delay * 1000);
675                 }
676         }
677
678         /* Maybe enter the JTAG TAP_RESET state ...
679          *  - using only TMS, TCK, and the JTAG state machine
680          *  - or else more directly, using TRST
681          *
682          * TAP_RESET should be invisible to non-debug parts of the system.
683          */
684         if (trst_with_tlr) {
685                 LOG_DEBUG("JTAG reset with TLR instead of TRST");
686                 jtag_set_end_state(TAP_RESET);
687                 jtag_add_tlr();
688
689         } else if (jtag_trst != new_trst) {
690                 jtag_trst = new_trst;
691                 if (jtag_trst) {
692                         LOG_DEBUG("TRST line asserted");
693                         tap_set_state(TAP_RESET);
694                         if (jtag_ntrst_assert_width)
695                                 jtag_add_sleep(jtag_ntrst_assert_width * 1000);
696                 } else {
697                         LOG_DEBUG("TRST line released");
698                         if (jtag_ntrst_delay)
699                                 jtag_add_sleep(jtag_ntrst_delay * 1000);
700
701                         /* We just asserted nTRST, so we're now in TAP_RESET.
702                          * Inform possible listeners about this, now that
703                          * JTAG instructions and data can be shifted.  This
704                          * sequence must match jtag_add_tlr().
705                          */
706                         jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
707                         jtag_notify_reset();
708                 }
709         }
710 }
711
712 tap_state_t jtag_set_end_state(tap_state_t state)
713 {
714         if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
715         {
716                 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
717         }
718
719         if (state != TAP_INVALID)
720                 cmd_queue_end_state = state;
721         return cmd_queue_end_state;
722 }
723
724 tap_state_t jtag_get_end_state(void)
725 {
726         return cmd_queue_end_state;
727 }
728
729 void jtag_add_sleep(uint32_t us)
730 {
731         /// @todo Here, keep_alive() appears to be a layering violation!!!
732         keep_alive();
733         jtag_set_error(interface_jtag_add_sleep(us));
734 }
735
736 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
737                 uint8_t *in_check_mask, int num_bits)
738 {
739         int retval = ERROR_OK;
740
741         int compare_failed = 0;
742
743         if (in_check_mask)
744                 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
745         else
746                 compare_failed = buf_cmp(captured, in_check_value, num_bits);
747
748         if (compare_failed) {
749                 char *captured_str, *in_check_value_str;
750                 int bits = (num_bits > DEBUG_JTAG_IOZ)
751                                 ? DEBUG_JTAG_IOZ
752                                 : num_bits;
753
754                 /* NOTE:  we've lost diagnostic context here -- 'which tap' */
755
756                 captured_str = buf_to_str(captured, bits, 16);
757                 in_check_value_str = buf_to_str(in_check_value, bits, 16);
758
759                 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
760                                 captured_str);
761                 LOG_WARNING(" check_value: 0x%s", in_check_value_str);
762
763                 free(captured_str);
764                 free(in_check_value_str);
765
766                 if (in_check_mask) {
767                         char *in_check_mask_str;
768
769                         in_check_mask_str = buf_to_str(in_check_mask, bits, 16);
770                         LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
771                         free(in_check_mask_str);
772                 }
773
774                 retval = ERROR_JTAG_QUEUE_FAILED;
775         }
776         return retval;
777 }
778
779 void jtag_check_value_mask(scan_field_t *field, uint8_t *value, uint8_t *mask)
780 {
781         assert(field->in_value != NULL);
782
783         if (value == NULL)
784         {
785                 /* no checking to do */
786                 return;
787         }
788
789         jtag_execute_queue_noclear();
790
791         int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
792         jtag_set_error(retval);
793 }
794
795
796
797 int default_interface_jtag_execute_queue(void)
798 {
799         if (NULL == jtag)
800         {
801                 LOG_ERROR("No JTAG interface configured yet.  "
802                         "Issue 'init' command in startup scripts "
803                         "before communicating with targets.");
804                 return ERROR_FAIL;
805         }
806
807         return jtag->execute_queue();
808 }
809
810 void jtag_execute_queue_noclear(void)
811 {
812         jtag_flush_queue_count++;
813         jtag_set_error(interface_jtag_execute_queue());
814 }
815
816 int jtag_get_flush_queue_count(void)
817 {
818         return jtag_flush_queue_count;
819 }
820
821 int jtag_execute_queue(void)
822 {
823         jtag_execute_queue_noclear();
824         return jtag_error_clear();
825 }
826
827 static int jtag_reset_callback(enum jtag_event event, void *priv)
828 {
829         jtag_tap_t *tap = priv;
830
831         if (event == JTAG_TRST_ASSERTED)
832         {
833                 tap->enabled = !tap->disabled_after_reset;
834
835                 /* current instruction is either BYPASS or IDCODE */
836                 buf_set_ones(tap->cur_instr, tap->ir_length);
837                 tap->bypass = 1;
838         }
839
840         return ERROR_OK;
841 }
842
843 void jtag_sleep(uint32_t us)
844 {
845         alive_sleep(us/1000);
846 }
847
848 /* Maximum number of enabled JTAG devices we expect in the scan chain,
849  * plus one (to detect garbage at the end).  Devices that don't support
850  * IDCODE take up fewer bits, possibly allowing a few more devices.
851  */
852 #define JTAG_MAX_CHAIN_SIZE 20
853
854 #define EXTRACT_MFG(X)  (((X) & 0xffe) >> 1)
855 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
856 #define EXTRACT_VER(X)  (((X) & 0xf0000000) >> 28)
857
858 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
859  * know that no valid TAP will have it as an IDCODE value.
860  */
861 #define END_OF_CHAIN_FLAG       0x000000ff
862
863 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
864 {
865         scan_field_t field = {
866                         .tap = NULL,
867                         .num_bits = num_idcode * 32,
868                         .out_value = idcode_buffer,
869                         .in_value = idcode_buffer,
870                 };
871
872         // initialize to the end of chain ID value
873         for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
874                 buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
875
876         jtag_add_plain_dr_scan(1, &field, TAP_DRPAUSE);
877         jtag_add_tlr();
878         return jtag_execute_queue();
879 }
880
881 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
882 {
883         uint8_t zero_check = 0x0;
884         uint8_t one_check = 0xff;
885
886         for (unsigned i = 0; i < count * 4; i++)
887         {
888                 zero_check |= idcodes[i];
889                 one_check &= idcodes[i];
890         }
891
892         /* if there wasn't a single non-zero bit or if all bits were one,
893          * the scan is not valid */
894         if (zero_check == 0x00 || one_check == 0xff)
895         {
896                 LOG_ERROR("JTAG scan chain interrogation failed: all %s",
897                                 (zero_check == 0x00) ? "zeroes" : "ones");
898                 LOG_ERROR("Check JTAG interface, timings, target power, etc.");
899                 return false;
900         }
901         return true;
902 }
903
904 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
905                 const char *name, uint32_t idcode)
906 {
907         log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
908                                   "JTAG tap: %s %16.16s: 0x%08x "
909                                   "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
910                                   name, msg,
911                                   (unsigned int)idcode,
912                                   (unsigned int)EXTRACT_MFG(idcode),
913                                   (unsigned int)EXTRACT_PART(idcode),
914                                   (unsigned int)EXTRACT_VER(idcode));
915 }
916
917 static bool jtag_idcode_is_final(uint32_t idcode)
918 {
919         /*
920          * Some devices, such as AVR8, will output all 1's instead
921          * of TDI input value at end of chain.  Allow those values
922          * instead of failing.
923          */
924         return idcode == END_OF_CHAIN_FLAG || idcode == 0xFFFFFFFF;
925 }
926
927 /**
928  * This helper checks that remaining bits in the examined chain data are
929  * all as expected, but a single JTAG device requires only 64 bits to be
930  * read back correctly.  This can help identify and diagnose problems
931  * with the JTAG chain earlier, gives more helpful/explicit error messages.
932  * Returns TRUE iff garbage was found.
933  */
934 static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
935 {
936         bool triggered = false;
937         for (; count < max - 31; count += 32)
938         {
939                 uint32_t idcode = buf_get_u32(idcodes, count, 32);
940                 // do not trigger the warning if the data looks good
941                 if (!triggered && jtag_idcode_is_final(idcode))
942                         continue;
943                 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
944                                         count, (unsigned int)idcode);
945                 triggered = true;
946         }
947         return triggered;
948 }
949
950 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
951 {
952         /* ignore expected BYPASS codes; warn otherwise */
953         if (0 == tap->expected_ids_cnt && !tap->idcode)
954                 return true;
955
956         /* Loop over the expected identification codes and test for a match */
957         uint8_t ii;
958         for (ii = 0; ii < tap->expected_ids_cnt; ii++)
959         {
960                 if (tap->idcode == tap->expected_ids[ii])
961                         return true;
962         }
963
964         /* If none of the expected ids matched, log an error */
965         jtag_examine_chain_display(LOG_LVL_ERROR, "UNEXPECTED",
966                         tap->dotted_name, tap->idcode);
967         for (ii = 0; ii < tap->expected_ids_cnt; ii++)
968         {
969                 char msg[32];
970                 snprintf(msg, sizeof(msg), "expected %hhu of %hhu",
971                                 ii + 1, tap->expected_ids_cnt);
972                 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
973                                 tap->dotted_name, tap->expected_ids[ii]);
974         }
975         return false;
976 }
977
978 /* Try to examine chain layout according to IEEE 1149.1 Â§12
979  * This is called a "blind interrogation" of the scan chain.
980  */
981 static int jtag_examine_chain(void)
982 {
983         uint8_t idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
984         unsigned bit_count;
985
986         /* DR scan to collect BYPASS or IDCODE register contents.
987          * Then make sure the scan data has both ones and zeroes.
988          */
989         jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
990         if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
991                 return ERROR_JTAG_INIT_FAILED;
992
993         /* point at the 1st tap */
994         jtag_tap_t *tap = jtag_tap_next_enabled(NULL);
995         if (tap == NULL)
996         {
997                 LOG_ERROR("JTAG: No taps enabled?");
998                 return ERROR_JTAG_INIT_FAILED;
999         }
1000
1001         for (bit_count = 0;
1002                         tap && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;
1003                         tap = jtag_tap_next_enabled(tap))
1004         {
1005                 uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1006
1007                 if ((idcode & 1) == 0)
1008                 {
1009                         /* LSB must not be 0, this indicates a device in bypass */
1010                         LOG_WARNING("TAP %s does not have IDCODE",
1011                                         tap->dotted_name);
1012                         idcode = 0;
1013                         tap->hasidcode = false;
1014
1015                         bit_count += 1;
1016                 }
1017                 else
1018                 {
1019                         /* Friendly devices support IDCODE */
1020                         tap->hasidcode = true;
1021                         jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found",
1022                                         tap->dotted_name, idcode);
1023
1024                         bit_count += 32;
1025                 }
1026                 tap->idcode = idcode;
1027
1028                 // ensure the TAP ID does matches what was expected
1029                 if (!jtag_examine_chain_match_tap(tap))
1030                         return ERROR_JTAG_INIT_FAILED;
1031         }
1032
1033         /* Fail if too many TAPs were enabled for us to verify them all. */
1034         if (tap) {
1035                 LOG_ERROR("Too many TAPs enabled; '%s' ignored.",
1036                                 tap->dotted_name);
1037                 return ERROR_JTAG_INIT_FAILED;
1038         }
1039
1040         /* After those IDCODE or BYPASS register values should be
1041          * only the data we fed into the scan chain.
1042          */
1043         if (jtag_examine_chain_end(idcode_buffer, bit_count,
1044                         8 * sizeof(idcode_buffer))) {
1045                 LOG_ERROR("double-check your JTAG setup (interface, "
1046                                 "speed, TAPs, ...)");
1047                 return ERROR_JTAG_INIT_FAILED;
1048         }
1049
1050         return ERROR_OK;
1051 }
1052
1053 /*
1054  * Validate the date loaded by entry to the Capture-IR state, to help
1055  * find errors related to scan chain configuration (wrong IR lengths)
1056  * or communication.
1057  */
1058 static int jtag_validate_ircapture(void)
1059 {
1060         jtag_tap_t *tap;
1061         int total_ir_length = 0;
1062         uint8_t *ir_test = NULL;
1063         scan_field_t field;
1064         int chain_pos = 0;
1065
1066         tap = NULL;
1067         total_ir_length = 0;
1068         for (;;) {
1069                 tap = jtag_tap_next_enabled(tap);
1070                 if (tap == NULL) {
1071                         break;
1072                 }
1073                 total_ir_length += tap->ir_length;
1074         }
1075
1076         total_ir_length += 2;
1077
1078         ir_test = malloc(CEIL(total_ir_length, 8));
1079         if (ir_test == NULL)
1080                 return ERROR_FAIL;
1081
1082         buf_set_ones(ir_test, total_ir_length);
1083
1084         field.tap = NULL;
1085         field.num_bits = total_ir_length;
1086         field.out_value = ir_test;
1087         field.in_value = ir_test;
1088
1089
1090         jtag_add_plain_ir_scan(1, &field, TAP_IRPAUSE);
1091         jtag_add_tlr();
1092
1093         int retval;
1094         retval = jtag_execute_queue();
1095         if (retval != ERROR_OK)
1096                 return retval;
1097
1098         tap = NULL;
1099         chain_pos = 0;
1100         int val;
1101         for (;;) {
1102                 tap = jtag_tap_next_enabled(tap);
1103                 if (tap == NULL) {
1104                         break;
1105                 }
1106
1107                 /* Validate the two LSBs, which must be 01 per JTAG spec.
1108                  * REVISIT we might be able to verify some MSBs too, using
1109                  * ircapture/irmask attributes.
1110                  */
1111                 val = buf_get_u32(ir_test, chain_pos, 2);
1112                 if (val != 1) {
1113                         char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1114
1115                         LOG_ERROR("%s: IR capture error; saw 0x%s not 0x..1",
1116                                         jtag_tap_name(tap), cbuf);
1117
1118                         /* Fail only if we have IDCODE for this device.
1119                          * REVISIT -- why not fail-always?
1120                          */
1121                         if (tap->hasidcode) {
1122                                 free(cbuf);
1123                                 free(ir_test);
1124                                 return ERROR_JTAG_INIT_FAILED;
1125                         }
1126                 }
1127                 chain_pos += tap->ir_length;
1128         }
1129
1130         /* verify the '11' sentinel we wrote is returned at the end */
1131         val = buf_get_u32(ir_test, chain_pos, 2);
1132         if (val != 0x3)
1133         {
1134                 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1135
1136                 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1137                                 chain_pos, cbuf);
1138                 free(cbuf);
1139                 free(ir_test);
1140                 return ERROR_JTAG_INIT_FAILED;
1141         }
1142
1143         free(ir_test);
1144
1145         return ERROR_OK;
1146 }
1147
1148
1149 void jtag_tap_init(jtag_tap_t *tap)
1150 {
1151         assert(0 != tap->ir_length);
1152
1153         /// @todo fix, this allocates one byte per bit for all three fields!
1154         tap->expected = malloc(tap->ir_length);
1155         tap->expected_mask = malloc(tap->ir_length);
1156         tap->cur_instr = malloc(tap->ir_length);
1157
1158         /// @todo cope sanely with ir_length bigger than 32 bits
1159         buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
1160         buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
1161         buf_set_ones(tap->cur_instr, tap->ir_length);
1162
1163         // place TAP in bypass mode
1164         tap->bypass = 1;
1165         // register the reset callback for the TAP
1166         jtag_register_event_callback(&jtag_reset_callback, tap);
1167
1168         LOG_DEBUG("Created Tap: %s @ abs position %d, "
1169                         "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1170                                 tap->abs_chain_position, tap->ir_length,
1171                                 (unsigned) tap->ir_capture_value,
1172                                 (unsigned) tap->ir_capture_mask);
1173         jtag_tap_add(tap);
1174 }
1175
1176 void jtag_tap_free(jtag_tap_t *tap)
1177 {
1178         jtag_unregister_event_callback(&jtag_reset_callback, tap);
1179
1180         /// @todo is anything missing? no memory leaks please
1181         free((void *)tap->expected);
1182         free((void *)tap->expected_ids);
1183         free((void *)tap->chip);
1184         free((void *)tap->tapname);
1185         free((void *)tap->dotted_name);
1186         free(tap);
1187 }
1188
1189 int jtag_interface_init(struct command_context_s *cmd_ctx)
1190 {
1191         if (jtag)
1192                 return ERROR_OK;
1193
1194         if (!jtag_interface)
1195         {
1196                 /* nothing was previously specified by "interface" command */
1197                 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1198                 return ERROR_JTAG_INVALID_INTERFACE;
1199         }
1200
1201         jtag = jtag_interface;
1202         if (jtag_interface->init() != ERROR_OK)
1203         {
1204                 jtag = NULL;
1205                 return ERROR_JTAG_INIT_FAILED;
1206         }
1207
1208         int requested_khz = jtag_get_speed_khz();
1209         int actual_khz = requested_khz;
1210         int retval = jtag_get_speed_readable(&actual_khz);
1211         if (ERROR_OK != retval)
1212                 LOG_INFO("interface specific clock speed value %d", jtag_get_speed());
1213         else if (actual_khz)
1214         {
1215                 if ((CLOCK_MODE_RCLK == clock_mode)
1216                         || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz))
1217                 {
1218                         LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1219                                 , actual_khz);
1220                 }
1221                 else
1222                         LOG_INFO("clock speed %d kHz", actual_khz);
1223         }
1224         else
1225                 LOG_INFO("RCLK (adaptive clock speed)");
1226
1227         return ERROR_OK;
1228 }
1229
1230 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1231 {
1232         jtag_tap_t *tap;
1233         int retval;
1234
1235         LOG_DEBUG("Init JTAG chain");
1236
1237         tap = jtag_tap_next_enabled(NULL);
1238         if (tap == NULL) {
1239                 LOG_ERROR("There are no enabled taps?");
1240                 return ERROR_JTAG_INIT_FAILED;
1241         }
1242
1243         jtag_add_tlr();
1244         if ((retval = jtag_execute_queue()) != ERROR_OK)
1245                 return retval;
1246
1247         /* examine chain first, as this could discover the real chain layout */
1248         if (jtag_examine_chain() != ERROR_OK)
1249         {
1250                 LOG_ERROR("Trying to use configured scan chain anyway...");
1251         }
1252
1253         if (jtag_validate_ircapture() != ERROR_OK)
1254         {
1255                 LOG_WARNING("Errors during IR capture, continuing anyway...");
1256         }
1257
1258         return ERROR_OK;
1259 }
1260
1261 int jtag_interface_quit(void)
1262 {
1263         if (!jtag || !jtag->quit)
1264                 return ERROR_OK;
1265
1266         // close the JTAG interface
1267         int result = jtag->quit();
1268         if (ERROR_OK != result)
1269                 LOG_ERROR("failed: %d", result);
1270
1271         return ERROR_OK;
1272 }
1273
1274
1275 int jtag_init_reset(struct command_context_s *cmd_ctx)
1276 {
1277         int retval;
1278
1279         if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1280                 return retval;
1281
1282         LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
1283
1284         /* Reset can happen after a power cycle.
1285          *
1286          * Ideally we would only assert TRST or run TLR before the target reset.
1287          *
1288          * However w/srst_pulls_trst, trst is asserted together with the target
1289          * reset whether we want it or not.
1290          *
1291          * NB! Some targets have JTAG circuitry disabled until a
1292          * trst & srst has been asserted.
1293          *
1294          * NB! here we assume nsrst/ntrst delay are sufficient!
1295          *
1296          * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1297          *
1298          */
1299         jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1300         if (jtag_reset_config & RESET_HAS_SRST)
1301         {
1302                 jtag_add_reset(1, 1);
1303                 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1304                         jtag_add_reset(0, 1);
1305         }
1306         jtag_add_reset(0, 0);
1307         if ((retval = jtag_execute_queue()) != ERROR_OK)
1308                 return retval;
1309
1310         /* Check that we can communication on the JTAG chain + eventually we want to
1311          * be able to perform enumeration only after OpenOCD has started
1312          * telnet and GDB server
1313          *
1314          * That would allow users to more easily perform any magic they need to before
1315          * reset happens.
1316          */
1317         return jtag_init_inner(cmd_ctx);
1318 }
1319
1320 int jtag_init(struct command_context_s *cmd_ctx)
1321 {
1322         int retval;
1323         if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1324                 return retval;
1325         if (jtag_init_inner(cmd_ctx) == ERROR_OK)
1326         {
1327                 return ERROR_OK;
1328         }
1329         return jtag_init_reset(cmd_ctx);
1330 }
1331
1332 unsigned jtag_get_speed_khz(void)
1333 {
1334         return speed_khz;
1335 }
1336
1337 static int jtag_khz_to_speed(unsigned khz, int* speed)
1338 {
1339         LOG_DEBUG("convert khz to interface specific speed value");
1340         speed_khz = khz;
1341         if (jtag != NULL)
1342         {
1343                 LOG_DEBUG("have interface set up");
1344                 int speed_div1;
1345                 int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
1346                 if (ERROR_OK != retval)
1347                 {
1348                         return retval;
1349                 }
1350                 *speed = speed_div1;
1351         }
1352         return ERROR_OK;
1353 }
1354
1355 static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int* speed)
1356 {
1357         int retval = jtag_khz_to_speed(0, speed);
1358         if ((ERROR_OK != retval) && fallback_speed_khz)
1359         {
1360                 LOG_DEBUG("trying fallback speed...");
1361                 retval = jtag_khz_to_speed(fallback_speed_khz, speed);
1362         }
1363         return retval;
1364 }
1365
1366 static int jtag_set_speed(int speed)
1367 {
1368         jtag_speed = speed;
1369         /* this command can be called during CONFIG,
1370          * in which case jtag isn't initialized */
1371         return jtag ? jtag->speed(speed) : ERROR_OK;
1372 }
1373
1374 int jtag_config_speed(int speed)
1375 {
1376         LOG_DEBUG("handle jtag speed");
1377         clock_mode = CLOCK_MODE_SPEED;
1378         return jtag_set_speed(speed);
1379 }
1380
1381 int jtag_config_khz(unsigned khz)
1382 {
1383         LOG_DEBUG("handle jtag khz");
1384         clock_mode = CLOCK_MODE_KHZ;
1385         int speed = 0;
1386         int retval = jtag_khz_to_speed(khz, &speed);
1387         return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1388 }
1389
1390 int jtag_config_rclk(unsigned fallback_speed_khz)
1391 {
1392         LOG_DEBUG("handle jtag rclk");
1393         clock_mode = CLOCK_MODE_RCLK;
1394         rclk_fallback_speed_khz = fallback_speed_khz;
1395         int speed = 0;
1396         int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
1397         return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1398 }
1399
1400 int jtag_get_speed(void)
1401 {
1402         int speed;
1403         switch(clock_mode)
1404         {
1405                 case CLOCK_MODE_SPEED:
1406                         speed = jtag_speed;
1407                         break;
1408                 case CLOCK_MODE_KHZ:
1409                         jtag_khz_to_speed(jtag_get_speed_khz(), &speed);
1410                         break;
1411                 case CLOCK_MODE_RCLK:
1412                         jtag_rclk_to_speed(rclk_fallback_speed_khz, &speed);
1413                         break;
1414                 default:
1415                         LOG_ERROR("BUG: unknown jtag clock mode");
1416                         speed = 0;
1417                         break;
1418         }
1419         return speed;
1420 }
1421
1422 int jtag_get_speed_readable(int *khz)
1423 {
1424         return jtag ? jtag->speed_div(jtag_get_speed(), khz) : ERROR_OK;
1425 }
1426
1427 void jtag_set_verify(bool enable)
1428 {
1429         jtag_verify = enable;
1430 }
1431
1432 bool jtag_will_verify()
1433 {
1434         return jtag_verify;
1435 }
1436
1437 void jtag_set_verify_capture_ir(bool enable)
1438 {
1439         jtag_verify_capture_ir = enable;
1440 }
1441
1442 bool jtag_will_verify_capture_ir()
1443 {
1444         return jtag_verify_capture_ir;
1445 }
1446
1447 int jtag_power_dropout(int *dropout)
1448 {
1449         return jtag->power_dropout(dropout);
1450 }
1451
1452 int jtag_srst_asserted(int *srst_asserted)
1453 {
1454         return jtag->srst_asserted(srst_asserted);
1455 }
1456
1457 enum reset_types jtag_get_reset_config(void)
1458 {
1459         return jtag_reset_config;
1460 }
1461 void jtag_set_reset_config(enum reset_types type)
1462 {
1463         jtag_reset_config = type;
1464 }
1465
1466 int jtag_get_trst(void)
1467 {
1468         return jtag_trst;
1469 }
1470 int jtag_get_srst(void)
1471 {
1472         return jtag_srst;
1473 }
1474
1475 void jtag_set_nsrst_delay(unsigned delay)
1476 {
1477         jtag_nsrst_delay = delay;
1478 }
1479 unsigned jtag_get_nsrst_delay(void)
1480 {
1481         return jtag_nsrst_delay;
1482 }
1483 void jtag_set_ntrst_delay(unsigned delay)
1484 {
1485         jtag_ntrst_delay = delay;
1486 }
1487 unsigned jtag_get_ntrst_delay(void)
1488 {
1489         return jtag_ntrst_delay;
1490 }
1491
1492
1493 void jtag_set_nsrst_assert_width(unsigned delay)
1494 {
1495         jtag_nsrst_assert_width = delay;
1496 }
1497 unsigned jtag_get_nsrst_assert_width(void)
1498 {
1499         return jtag_nsrst_assert_width;
1500 }
1501 void jtag_set_ntrst_assert_width(unsigned delay)
1502 {
1503         jtag_ntrst_assert_width = delay;
1504 }
1505 unsigned jtag_get_ntrst_assert_width(void)
1506 {
1507         return jtag_ntrst_assert_width;
1508 }