Minor behavior fixes for the two JTAG reset events (C/internal,
[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         assert(end_state != TAP_INVALID);
475
476         cmd_queue_cur_state = end_state;
477
478         interface_jtag_add_dr_out(tap,
479                         num_fields, num_bits, value,
480                         end_state);
481 }
482
483 void jtag_add_tlr(void)
484 {
485         jtag_prelude(TAP_RESET);
486         jtag_set_error(interface_jtag_add_tlr());
487
488         /* NOTE: order here matches TRST path in jtag_add_reset() */
489         jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
490         jtag_notify_reset();
491 }
492
493 void jtag_add_pathmove(int num_states, const tap_state_t *path)
494 {
495         tap_state_t cur_state = cmd_queue_cur_state;
496
497         /* the last state has to be a stable state */
498         if (!tap_is_state_stable(path[num_states - 1]))
499         {
500                 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
501                 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
502                 return;
503         }
504
505         for (int i = 0; i < num_states; i++)
506         {
507                 if (path[i] == TAP_RESET)
508                 {
509                         LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
510                         jtag_set_error(ERROR_JTAG_STATE_INVALID);
511                         return;
512                 }
513
514                 if (tap_state_transition(cur_state, true)  != path[i]
515                   && tap_state_transition(cur_state, false) != path[i])
516                 {
517                         LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
518                                         tap_state_name(cur_state), tap_state_name(path[i]));
519                         jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
520                         return;
521                 }
522                 cur_state = path[i];
523         }
524
525         jtag_checks();
526
527         jtag_set_error(interface_jtag_add_pathmove(num_states, path));
528         cmd_queue_cur_state = path[num_states - 1];
529 }
530
531 int jtag_add_statemove(tap_state_t goal_state)
532 {
533         tap_state_t cur_state = cmd_queue_cur_state;
534
535         LOG_DEBUG("cur_state=%s goal_state=%s",
536                 tap_state_name(cur_state),
537                 tap_state_name(goal_state));
538
539
540         if (goal_state == cur_state)
541                 ;       /* nothing to do */
542         else if (goal_state == TAP_RESET)
543         {
544                 jtag_add_tlr();
545         }
546         else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state))
547         {
548                 unsigned tms_bits  = tap_get_tms_path(cur_state, goal_state);
549                 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
550                 tap_state_t moves[8];
551                 assert(tms_count < DIM(moves));
552
553                 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
554                 {
555                         bool bit = tms_bits & 1;
556
557                         cur_state = tap_state_transition(cur_state, bit);
558                         moves[i] = cur_state;
559                 }
560
561                 jtag_add_pathmove(tms_count, moves);
562         }
563         else if (tap_state_transition(cur_state, true)  == goal_state
564                 ||   tap_state_transition(cur_state, false) == goal_state)
565         {
566                 jtag_add_pathmove(1, &goal_state);
567         }
568
569         else
570                 return ERROR_FAIL;
571
572         return ERROR_OK;
573 }
574
575 void jtag_add_runtest(int num_cycles, tap_state_t state)
576 {
577         jtag_prelude(state);
578         jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
579 }
580
581
582 void jtag_add_clocks(int num_cycles)
583 {
584         if (!tap_is_state_stable(cmd_queue_cur_state))
585         {
586                  LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
587                                  tap_state_name(cmd_queue_cur_state));
588                  jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
589                  return;
590         }
591
592         if (num_cycles > 0)
593         {
594                 jtag_checks();
595                 jtag_set_error(interface_jtag_add_clocks(num_cycles));
596         }
597 }
598
599 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
600 {
601         int trst_with_tlr = 0;
602         int new_srst = 0;
603         int new_trst = 0;
604
605         /* Without SRST, we must use target-specific JTAG operations
606          * on each target; callers should not be requesting SRST when
607          * that signal doesn't exist.
608          *
609          * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
610          * can kick in even if the JTAG adapter can't drive TRST.
611          */
612         if (req_srst) {
613                 if (!(jtag_reset_config & RESET_HAS_SRST)) {
614                         LOG_ERROR("BUG: can't assert SRST");
615                         jtag_set_error(ERROR_FAIL);
616                         return;
617                 }
618                 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
619                                 && !req_tlr_or_trst) {
620                         LOG_ERROR("BUG: can't assert only SRST");
621                         jtag_set_error(ERROR_FAIL);
622                         return;
623                 }
624                 new_srst = 1;
625         }
626
627         /* JTAG reset (entry to TAP_RESET state) can always be achieved
628          * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
629          * state first.  TRST accelerates it, and bypasses those states.
630          *
631          * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
632          * can kick in even if the JTAG adapter can't drive SRST.
633          */
634         if (req_tlr_or_trst) {
635                 if (!(jtag_reset_config & RESET_HAS_TRST))
636                         trst_with_tlr = 1;
637                 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
638                                 && !req_srst)
639                         trst_with_tlr = 1;
640                 else
641                         new_trst = 1;
642         }
643
644         /* Maybe change TRST and/or SRST signal state */
645         if (jtag_srst != new_srst || jtag_trst != new_trst) {
646                 int retval;
647
648                 retval = interface_jtag_add_reset(new_trst, new_srst);
649                 if (retval != ERROR_OK)
650                         jtag_set_error(retval);
651                 else
652                         retval = jtag_execute_queue();
653
654                 if (retval != ERROR_OK) {
655                         LOG_ERROR("TRST/SRST error %d", retval);
656                         return;
657                 }
658         }
659
660         /* SRST resets everything hooked up to that signal */
661         if (jtag_srst != new_srst) {
662                 jtag_srst = new_srst;
663                 if (jtag_srst)
664                         LOG_DEBUG("SRST line asserted");
665                 else {
666                         LOG_DEBUG("SRST line released");
667                         if (jtag_nsrst_delay)
668                                 jtag_add_sleep(jtag_nsrst_delay * 1000);
669                 }
670         }
671
672         /* Maybe enter the JTAG TAP_RESET state ...
673          *  - using only TMS, TCK, and the JTAG state machine
674          *  - or else more directly, using TRST
675          *
676          * TAP_RESET should be invisible to non-debug parts of the system.
677          */
678         if (trst_with_tlr) {
679                 LOG_DEBUG("JTAG reset with TLR instead of TRST");
680                 jtag_set_end_state(TAP_RESET);
681                 jtag_add_tlr();
682
683         } else if (jtag_trst != new_trst) {
684                 jtag_trst = new_trst;
685                 if (jtag_trst) {
686                         LOG_DEBUG("TRST line asserted");
687                         tap_set_state(TAP_RESET);
688                 } else {
689                         LOG_DEBUG("TRST line released");
690                         if (jtag_ntrst_delay)
691                                 jtag_add_sleep(jtag_ntrst_delay * 1000);
692
693                         /* We just asserted nTRST, so we're now in TAP_RESET.
694                          * Inform possible listeners about this, now that
695                          * JTAG instructions and data can be shifted.  This
696                          * sequence must match jtag_add_tlr().
697                          */
698                         jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
699                         jtag_notify_reset();
700                 }
701         }
702 }
703
704 tap_state_t jtag_set_end_state(tap_state_t state)
705 {
706         if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
707         {
708                 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
709         }
710
711         if (state != TAP_INVALID)
712                 cmd_queue_end_state = state;
713         return cmd_queue_end_state;
714 }
715
716 tap_state_t jtag_get_end_state(void)
717 {
718         return cmd_queue_end_state;
719 }
720
721 void jtag_add_sleep(uint32_t us)
722 {
723         /// @todo Here, keep_alive() appears to be a layering violation!!!
724         keep_alive();
725         jtag_set_error(interface_jtag_add_sleep(us));
726 }
727
728 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
729                 uint8_t *in_check_mask, int num_bits)
730 {
731         int retval = ERROR_OK;
732
733         int compare_failed = 0;
734
735         if (in_check_mask)
736                 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
737         else
738                 compare_failed = buf_cmp(captured, in_check_value, num_bits);
739
740         if (compare_failed) {
741                 char *captured_str, *in_check_value_str;
742                 int bits = (num_bits > DEBUG_JTAG_IOZ)
743                                 ? DEBUG_JTAG_IOZ
744                                 : num_bits;
745
746                 /* NOTE:  we've lost diagnostic context here -- 'which tap' */
747
748                 captured_str = buf_to_str(captured, bits, 16);
749                 in_check_value_str = buf_to_str(in_check_value, bits, 16);
750
751                 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
752                                 captured_str);
753                 LOG_WARNING(" check_value: 0x%s", in_check_value_str);
754
755                 free(captured_str);
756                 free(in_check_value_str);
757
758                 if (in_check_mask) {
759                         char *in_check_mask_str;
760
761                         in_check_mask_str = buf_to_str(in_check_mask, bits, 16);
762                         LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
763                         free(in_check_mask_str);
764                 }
765
766                 retval = ERROR_JTAG_QUEUE_FAILED;
767         }
768         return retval;
769 }
770
771 void jtag_check_value_mask(scan_field_t *field, uint8_t *value, uint8_t *mask)
772 {
773         assert(field->in_value != NULL);
774
775         if (value == NULL)
776         {
777                 /* no checking to do */
778                 return;
779         }
780
781         jtag_execute_queue_noclear();
782
783         int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
784         jtag_set_error(retval);
785 }
786
787
788
789 int default_interface_jtag_execute_queue(void)
790 {
791         if (NULL == jtag)
792         {
793                 LOG_ERROR("No JTAG interface configured yet.  "
794                         "Issue 'init' command in startup scripts "
795                         "before communicating with targets.");
796                 return ERROR_FAIL;
797         }
798
799         return jtag->execute_queue();
800 }
801
802 void jtag_execute_queue_noclear(void)
803 {
804         jtag_flush_queue_count++;
805         jtag_set_error(interface_jtag_execute_queue());
806 }
807
808 int jtag_get_flush_queue_count(void)
809 {
810         return jtag_flush_queue_count;
811 }
812
813 int jtag_execute_queue(void)
814 {
815         jtag_execute_queue_noclear();
816         return jtag_error_clear();
817 }
818
819 static int jtag_reset_callback(enum jtag_event event, void *priv)
820 {
821         jtag_tap_t *tap = priv;
822
823         LOG_DEBUG("TAP %s event %s", tap->dotted_name,
824                         jtag_event_strings[event]);
825
826         if (event == JTAG_TRST_ASSERTED)
827         {
828                 tap->enabled = !tap->disabled_after_reset;
829
830                 /* current instruction is either BYPASS or IDCODE */
831                 buf_set_ones(tap->cur_instr, tap->ir_length);
832                 tap->bypass = 1;
833         }
834
835         return ERROR_OK;
836 }
837
838 void jtag_sleep(uint32_t us)
839 {
840         alive_sleep(us/1000);
841 }
842
843 /// maximum number of JTAG devices expected in the chain
844 #define JTAG_MAX_CHAIN_SIZE 20
845
846 #define EXTRACT_MFG(X)  (((X) & 0xffe) >> 1)
847 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
848 #define EXTRACT_VER(X)  (((X) & 0xf0000000) >> 28)
849
850 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
851 {
852         scan_field_t field = {
853                         .tap = NULL,
854                         .num_bits = num_idcode * 32,
855                         .out_value = idcode_buffer,
856                         .in_value = idcode_buffer,
857                 };
858
859         // initialize to the end of chain ID value
860         for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
861                 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
862
863         jtag_add_plain_dr_scan(1, &field, TAP_DRPAUSE);
864         jtag_add_tlr();
865         return jtag_execute_queue();
866 }
867
868 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
869 {
870         uint8_t zero_check = 0x0;
871         uint8_t one_check = 0xff;
872
873         for (unsigned i = 0; i < count * 4; i++)
874         {
875                 zero_check |= idcodes[i];
876                 one_check &= idcodes[i];
877         }
878
879         /* if there wasn't a single non-zero bit or if all bits were one,
880          * the scan is not valid */
881         if (zero_check == 0x00 || one_check == 0xff)
882         {
883                 LOG_ERROR("JTAG communication failure: check connection, "
884                         "JTAG interface, target power etc.");
885                 return false;
886         }
887         return true;
888 }
889
890 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
891                 const char *name, uint32_t idcode)
892 {
893         log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
894                                   "JTAG tap: %s %16.16s: 0x%08x "
895                                   "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
896                                   name, msg,
897                                   (unsigned int)idcode,
898                                   (unsigned int)EXTRACT_MFG(idcode),
899                                   (unsigned int)EXTRACT_PART(idcode),
900                                   (unsigned int)EXTRACT_VER(idcode));
901 }
902
903 static bool jtag_idcode_is_final(uint32_t idcode)
904 {
905                 return idcode == 0x000000FF || idcode == 0xFFFFFFFF;
906 }
907
908 /**
909  * This helper checks that remaining bits in the examined chain data are
910  * all as expected, but a single JTAG device requires only 64 bits to be
911  * read back correctly.  This can help identify and diagnose problems
912  * with the JTAG chain earlier, gives more helpful/explicit error messages.
913  */
914 static void jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
915 {
916         bool triggered = false;
917         for (; count < max - 31; count += 32)
918         {
919                 uint32_t idcode = buf_get_u32(idcodes, count, 32);
920                 // do not trigger the warning if the data looks good
921                 if (!triggered && jtag_idcode_is_final(idcode))
922                         continue;
923                 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
924                                         count, (unsigned int)idcode);
925                 triggered = true;
926         }
927 }
928
929 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
930 {
931         if (0 == tap->expected_ids_cnt)
932         {
933                 /// @todo Enable LOG_INFO to ask for reports about unknown TAP IDs.
934 #if 0
935                 LOG_INFO("Uknown JTAG TAP ID: 0x%08x", tap->idcode)
936                 LOG_INFO("Please report the chip name and reported ID code to the openocd project");
937 #endif
938                 return true;
939         }
940
941         /* Loop over the expected identification codes and test for a match */
942         uint8_t ii;
943         for (ii = 0; ii < tap->expected_ids_cnt; ii++)
944         {
945                 if (tap->idcode == tap->expected_ids[ii])
946                         break;
947         }
948
949         /* If none of the expected ids matched, log an error */
950         if (ii != tap->expected_ids_cnt)
951         {
952                 LOG_DEBUG("JTAG Tap/device matched");
953                 return true;
954         }
955         jtag_examine_chain_display(LOG_LVL_ERROR, "got",
956                         tap->dotted_name, tap->idcode);
957         for (ii = 0; ii < tap->expected_ids_cnt; ii++)
958         {
959                 char msg[32];
960                 snprintf(msg, sizeof(msg), "expected %hhu of %hhu",
961                                 ii + 1, tap->expected_ids_cnt);
962                 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
963                                 tap->dotted_name, tap->expected_ids[ii]);
964         }
965         return false;
966 }
967
968 /* Try to examine chain layout according to IEEE 1149.1 Â§12
969  */
970 static int jtag_examine_chain(void)
971 {
972         uint8_t idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
973         unsigned device_count = 0;
974
975         jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
976
977         if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
978                 return ERROR_JTAG_INIT_FAILED;
979
980         /* point at the 1st tap */
981         jtag_tap_t *tap = jtag_tap_next_enabled(NULL);
982         if (tap == NULL)
983         {
984                 LOG_ERROR("JTAG: No taps enabled?");
985                 return ERROR_JTAG_INIT_FAILED;
986         }
987
988         for (unsigned bit_count = 0;
989                         tap && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;
990                         tap = jtag_tap_next_enabled(tap))
991         {
992                 uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
993
994                 if ((idcode & 1) == 0)
995                 {
996                         /* LSB must not be 0, this indicates a device in bypass */
997                         LOG_WARNING("Tap/Device does not have IDCODE");
998                         idcode = 0;
999                         tap->hasidcode = false;
1000
1001                         bit_count += 1;
1002                 }
1003                 else
1004                 {
1005                         tap->hasidcode = true;
1006
1007                         /*
1008                          * End of chain (invalid manufacturer ID) some devices, such
1009                          * as AVR will output all 1's instead of TDI input value at
1010                          * end of chain.
1011                          */
1012                         if (jtag_idcode_is_final(idcode))
1013                         {
1014                                 jtag_examine_chain_end(idcode_buffer,
1015                                                 bit_count + 32, JTAG_MAX_CHAIN_SIZE * 32);
1016                                 break;
1017                         }
1018
1019                         jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found",
1020                                         tap ? tap->dotted_name : "(not-named)",
1021                                         idcode);
1022
1023                         bit_count += 32;
1024                 }
1025                 device_count++;
1026                 if (!tap)
1027                         continue;
1028
1029                 tap->idcode = idcode;
1030
1031                 // ensure the TAP ID does matches what was expected
1032                 if (!jtag_examine_chain_match_tap(tap))
1033                         return ERROR_JTAG_INIT_FAILED;
1034         }
1035
1036         /* see if number of discovered devices matches configuration */
1037         if (device_count != jtag_tap_count_enabled())
1038         {
1039                 LOG_ERROR("number of discovered devices in JTAG chain (%i) "
1040                                 "does not match (enabled) configuration (%i), total taps: %d",
1041                                 device_count, jtag_tap_count_enabled(), jtag_tap_count());
1042                 LOG_ERROR("check the config file and ensure proper JTAG communication"
1043                                 " (connections, speed, ...)");
1044                 return ERROR_JTAG_INIT_FAILED;
1045         }
1046
1047         return ERROR_OK;
1048 }
1049
1050 int jtag_validate_chain(void)
1051 {
1052         jtag_tap_t *tap;
1053         int total_ir_length = 0;
1054         uint8_t *ir_test = NULL;
1055         scan_field_t field;
1056         int chain_pos = 0;
1057
1058         tap = NULL;
1059         total_ir_length = 0;
1060         for (;;) {
1061                 tap = jtag_tap_next_enabled(tap);
1062                 if (tap == NULL) {
1063                         break;
1064                 }
1065                 total_ir_length += tap->ir_length;
1066         }
1067
1068         total_ir_length += 2;
1069         ir_test = malloc(CEIL(total_ir_length, 8));
1070         buf_set_ones(ir_test, total_ir_length);
1071
1072         field.tap = NULL;
1073         field.num_bits = total_ir_length;
1074         field.out_value = ir_test;
1075         field.in_value = ir_test;
1076
1077
1078         jtag_add_plain_ir_scan(1, &field, TAP_IRPAUSE);
1079         jtag_add_tlr();
1080
1081         int retval;
1082         retval = jtag_execute_queue();
1083         if (retval != ERROR_OK)
1084                 return retval;
1085
1086         tap = NULL;
1087         chain_pos = 0;
1088         int val;
1089         for (;;) {
1090                 tap = jtag_tap_next_enabled(tap);
1091                 if (tap == NULL) {
1092                         break;
1093                 }
1094
1095                 val = buf_get_u32(ir_test, chain_pos, 2);
1096                 /* Only fail this check if we have IDCODE for this device */
1097                 if ((val != 0x1)&&(tap->hasidcode))
1098                 {
1099                         char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1100                         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);
1101                         free(cbuf);
1102                         free(ir_test);
1103                         return ERROR_JTAG_INIT_FAILED;
1104                 }
1105                 chain_pos += tap->ir_length;
1106         }
1107
1108         val = buf_get_u32(ir_test, chain_pos, 2);
1109         if (val != 0x3)
1110         {
1111                 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1112                 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);
1113                 free(cbuf);
1114                 free(ir_test);
1115                 return ERROR_JTAG_INIT_FAILED;
1116         }
1117
1118         free(ir_test);
1119
1120         return ERROR_OK;
1121 }
1122
1123
1124 void jtag_tap_init(jtag_tap_t *tap)
1125 {
1126         assert(0 != tap->ir_length);
1127
1128         tap->expected = malloc(tap->ir_length);
1129         tap->expected_mask = malloc(tap->ir_length);
1130         tap->cur_instr = malloc(tap->ir_length);
1131
1132         /// @todo cope sanely with ir_length bigger than 32 bits
1133         buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
1134         buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
1135         buf_set_ones(tap->cur_instr, tap->ir_length);
1136
1137         // place TAP in bypass mode
1138         tap->bypass = 1;
1139         // register the reset callback for the TAP
1140         jtag_register_event_callback(&jtag_reset_callback, tap);
1141
1142         LOG_DEBUG("Created Tap: %s @ abs position %d, "
1143                         "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1144                                 tap->abs_chain_position, tap->ir_length,
1145                           (unsigned int)(tap->ir_capture_value), (unsigned int)(tap->ir_capture_mask));
1146         jtag_tap_add(tap);
1147 }
1148
1149 void jtag_tap_free(jtag_tap_t *tap)
1150 {
1151         jtag_unregister_event_callback(&jtag_reset_callback, tap);
1152
1153         /// @todo is anything missing? no memory leaks please
1154         free((void *)tap->expected_ids);
1155         free((void *)tap->chip);
1156         free((void *)tap->tapname);
1157         free((void *)tap->dotted_name);
1158         free(tap);
1159 }
1160
1161 int jtag_interface_init(struct command_context_s *cmd_ctx)
1162 {
1163         if (jtag)
1164                 return ERROR_OK;
1165
1166         if (!jtag_interface)
1167         {
1168                 /* nothing was previously specified by "interface" command */
1169                 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1170                 return ERROR_JTAG_INVALID_INTERFACE;
1171         }
1172
1173         jtag = jtag_interface;
1174         if (jtag_interface->init() != ERROR_OK)
1175         {
1176                 jtag = NULL;
1177                 return ERROR_JTAG_INIT_FAILED;
1178         }
1179
1180         int requested_khz = jtag_get_speed_khz();
1181         int actual_khz = requested_khz;
1182         int retval = jtag_get_speed_readable(&actual_khz);
1183         if (ERROR_OK != retval)
1184                 LOG_INFO("interface specific clock speed value %d", jtag_get_speed());
1185         else if (actual_khz)
1186         {
1187                 if ((CLOCK_MODE_RCLK == clock_mode)
1188                         || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz))
1189                 {
1190                         LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1191                                 , actual_khz);
1192                 }
1193                 else
1194                         LOG_INFO("clock speed %d kHz", actual_khz);
1195         }
1196         else
1197                 LOG_INFO("RCLK (adaptive clock speed)");
1198
1199         return ERROR_OK;
1200 }
1201
1202 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1203 {
1204         jtag_tap_t *tap;
1205         int retval;
1206
1207         LOG_DEBUG("Init JTAG chain");
1208
1209         tap = jtag_tap_next_enabled(NULL);
1210         if (tap == NULL) {
1211                 LOG_ERROR("There are no enabled taps?");
1212                 return ERROR_JTAG_INIT_FAILED;
1213         }
1214
1215         jtag_add_tlr();
1216         if ((retval = jtag_execute_queue()) != ERROR_OK)
1217                 return retval;
1218
1219         /* examine chain first, as this could discover the real chain layout */
1220         if (jtag_examine_chain() != ERROR_OK)
1221         {
1222                 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1223         }
1224
1225         if (jtag_validate_chain() != ERROR_OK)
1226         {
1227                 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
1228         }
1229
1230         return ERROR_OK;
1231 }
1232
1233 int jtag_interface_quit(void)
1234 {
1235         if (!jtag || !jtag->quit)
1236                 return ERROR_OK;
1237
1238         // close the JTAG interface
1239         int result = jtag->quit();
1240         if (ERROR_OK != result)
1241                 LOG_ERROR("failed: %d", result);
1242
1243         return ERROR_OK;
1244 }
1245
1246
1247 int jtag_init_reset(struct command_context_s *cmd_ctx)
1248 {
1249         int retval;
1250
1251         if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1252                 return retval;
1253
1254         LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
1255
1256         /* Reset can happen after a power cycle.
1257          *
1258          * Ideally we would only assert TRST or run TLR before the target reset.
1259          *
1260          * However w/srst_pulls_trst, trst is asserted together with the target
1261          * reset whether we want it or not.
1262          *
1263          * NB! Some targets have JTAG circuitry disabled until a
1264          * trst & srst has been asserted.
1265          *
1266          * NB! here we assume nsrst/ntrst delay are sufficient!
1267          *
1268          * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1269          *
1270          */
1271         jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1272         if (jtag_reset_config & RESET_HAS_SRST)
1273         {
1274                 jtag_add_reset(1, 1);
1275                 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1276                         jtag_add_reset(0, 1);
1277         }
1278         jtag_add_reset(0, 0);
1279         if ((retval = jtag_execute_queue()) != ERROR_OK)
1280                 return retval;
1281
1282         /* Check that we can communication on the JTAG chain + eventually we want to
1283          * be able to perform enumeration only after OpenOCD has started
1284          * telnet and GDB server
1285          *
1286          * That would allow users to more easily perform any magic they need to before
1287          * reset happens.
1288          */
1289         return jtag_init_inner(cmd_ctx);
1290 }
1291
1292 int jtag_init(struct command_context_s *cmd_ctx)
1293 {
1294         int retval;
1295         if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1296                 return retval;
1297         if (jtag_init_inner(cmd_ctx) == ERROR_OK)
1298         {
1299                 return ERROR_OK;
1300         }
1301         return jtag_init_reset(cmd_ctx);
1302 }
1303
1304 unsigned jtag_get_speed_khz(void)
1305 {
1306         return speed_khz;
1307 }
1308
1309 static int jtag_khz_to_speed(unsigned khz, int* speed)
1310 {
1311         LOG_DEBUG("convert khz to interface specific speed value");
1312         speed_khz = khz;
1313         if (jtag != NULL)
1314         {
1315                 LOG_DEBUG("have interface set up");
1316                 int speed_div1;
1317                 int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
1318                 if (ERROR_OK != retval)
1319                 {
1320                         return retval;
1321                 }
1322                 *speed = speed_div1;
1323         }
1324         return ERROR_OK;
1325 }
1326
1327 static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int* speed)
1328 {
1329         int retval = jtag_khz_to_speed(0, speed);
1330         if ((ERROR_OK != retval) && fallback_speed_khz)
1331         {
1332                 LOG_DEBUG("trying fallback speed...");
1333                 retval = jtag_khz_to_speed(fallback_speed_khz, speed);
1334         }
1335         return retval;
1336 }
1337
1338 static int jtag_set_speed(int speed)
1339 {
1340         jtag_speed = speed;
1341         /* this command can be called during CONFIG,
1342          * in which case jtag isn't initialized */
1343         return jtag ? jtag->speed(speed) : ERROR_OK;
1344 }
1345
1346 int jtag_config_speed(int speed)
1347 {
1348         LOG_DEBUG("handle jtag speed");
1349         clock_mode = CLOCK_MODE_SPEED;
1350         return jtag_set_speed(speed);
1351 }
1352
1353 int jtag_config_khz(unsigned khz)
1354 {
1355         LOG_DEBUG("handle jtag khz");
1356         clock_mode = CLOCK_MODE_KHZ;
1357         int speed = 0;
1358         int retval = jtag_khz_to_speed(khz, &speed);
1359         return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1360 }
1361
1362 int jtag_config_rclk(unsigned fallback_speed_khz)
1363 {
1364         LOG_DEBUG("handle jtag rclk");
1365         clock_mode = CLOCK_MODE_RCLK;
1366         rclk_fallback_speed_khz = fallback_speed_khz;
1367         int speed = 0;
1368         int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
1369         return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1370 }
1371
1372 int jtag_get_speed(void)
1373 {
1374         int speed;
1375         switch(clock_mode)
1376         {
1377                 case CLOCK_MODE_SPEED:
1378                         speed = jtag_speed;
1379                         break;
1380                 case CLOCK_MODE_KHZ:
1381                         jtag_khz_to_speed(jtag_get_speed_khz(), &speed);
1382                         break;
1383                 case CLOCK_MODE_RCLK:
1384                         jtag_rclk_to_speed(rclk_fallback_speed_khz, &speed);
1385                         break;
1386                 default:
1387                         LOG_ERROR("BUG: unknown jtag clock mode");
1388                         speed = 0;
1389                         break;
1390         }
1391         return speed;
1392 }
1393
1394 int jtag_get_speed_readable(int *khz)
1395 {
1396         return jtag ? jtag->speed_div(jtag_get_speed(), khz) : ERROR_OK;
1397 }
1398
1399 void jtag_set_verify(bool enable)
1400 {
1401         jtag_verify = enable;
1402 }
1403
1404 bool jtag_will_verify()
1405 {
1406         return jtag_verify;
1407 }
1408
1409 void jtag_set_verify_capture_ir(bool enable)
1410 {
1411         jtag_verify_capture_ir = enable;
1412 }
1413
1414 bool jtag_will_verify_capture_ir()
1415 {
1416         return jtag_verify_capture_ir;
1417 }
1418
1419 int jtag_power_dropout(int *dropout)
1420 {
1421         return jtag->power_dropout(dropout);
1422 }
1423
1424 int jtag_srst_asserted(int *srst_asserted)
1425 {
1426         return jtag->srst_asserted(srst_asserted);
1427 }
1428
1429 enum reset_types jtag_get_reset_config(void)
1430 {
1431         return jtag_reset_config;
1432 }
1433 void jtag_set_reset_config(enum reset_types type)
1434 {
1435         jtag_reset_config = type;
1436 }
1437
1438 int jtag_get_trst(void)
1439 {
1440         return jtag_trst;
1441 }
1442 int jtag_get_srst(void)
1443 {
1444         return jtag_srst;
1445 }
1446
1447 void jtag_set_nsrst_delay(unsigned delay)
1448 {
1449         jtag_nsrst_delay = delay;
1450 }
1451 unsigned jtag_get_nsrst_delay(void)
1452 {
1453         return jtag_nsrst_delay;
1454 }
1455 void jtag_set_ntrst_delay(unsigned delay)
1456 {
1457         jtag_ntrst_delay = delay;
1458 }
1459 unsigned jtag_get_ntrst_delay(void)
1460 {
1461         return jtag_ntrst_delay;
1462 }