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