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