]> git.sur5r.net Git - openocd/blob - src/jtag/core.c
tap post reset event added. Allows omap3530 to send 100 runtest idle tickle's after...
[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 int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value, uint8_t *in_check_mask, int num_bits);
389
390 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)
391 {
392         return jtag_check_value_inner((uint8_t *)data0, (uint8_t *)data1, (uint8_t *)data2, (int)data3);
393 }
394
395 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
396                 int in_num_fields, scan_field_t *in_fields, tap_state_t state)
397 {
398         for (int i = 0; i < in_num_fields; i++)
399         {
400                 struct scan_field_s *field = &in_fields[i];
401                 field->allocated = 0;
402                 field->modified = 0;
403                 if (field->check_value || field->in_value)
404                         continue;
405                 interface_jtag_add_scan_check_alloc(field);
406                 field->modified = 1;
407         }
408
409         jtag_add_scan(in_num_fields, in_fields, state);
410
411         for (int i = 0; i < in_num_fields; i++)
412         {
413                 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
414                 {
415                         /* this is synchronous for a minidriver */
416                         jtag_add_callback4(jtag_check_value_mask_callback, (jtag_callback_data_t)in_fields[i].in_value,
417                                 (jtag_callback_data_t)in_fields[i].check_value,
418                                 (jtag_callback_data_t)in_fields[i].check_mask,
419                                 (jtag_callback_data_t)in_fields[i].num_bits);
420                 }
421                 if (in_fields[i].allocated)
422                 {
423                         free(in_fields[i].in_value);
424                 }
425                 if (in_fields[i].modified)
426                 {
427                         in_fields[i].in_value = NULL;
428                 }
429         }
430 }
431
432 void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
433 {
434         if (jtag_verify)
435         {
436                 jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
437         } else
438         {
439                 jtag_add_dr_scan(in_num_fields, in_fields, state);
440         }
441 }
442
443
444 void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
445                 tap_state_t state)
446 {
447         assert(state != TAP_RESET);
448         
449         jtag_prelude(state);
450
451         int retval;
452         retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
453         jtag_set_error(retval);
454 }
455
456 void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
457                 tap_state_t state)
458 {
459         assert(state != TAP_RESET);
460         
461         jtag_prelude(state);
462
463         int retval;
464         retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
465         jtag_set_error(retval);
466 }
467
468 void jtag_add_dr_out(jtag_tap_t* tap,
469                 int num_fields, const int* num_bits, const uint32_t* value,
470                 tap_state_t end_state)
471 {
472         assert(end_state != TAP_RESET);
473         
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         jtag_notify_reset();
489
490         jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
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                         /* we just asserted nTRST, so we're now in TAP_RESET;
687                          * inform possible listeners about this
688                          *
689                          * REVISIT asserting TRST is less significant than
690                          * being in TAP_RESET ... both entries (TRST, TLR)
691                          * should trigger a callback.
692                          */
693                         LOG_DEBUG("TRST line asserted");
694                         tap_set_state(TAP_RESET);
695                         jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
696                 } else {
697                         LOG_DEBUG("TRST line released");
698                         if (jtag_ntrst_delay)
699                                 jtag_add_sleep(jtag_ntrst_delay * 1000);
700
701                         jtag_notify_reset();
702                 }
703         }
704 }
705
706 tap_state_t jtag_set_end_state(tap_state_t state)
707 {
708         if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
709         {
710                 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
711         }
712
713         if (state != TAP_INVALID)
714                 cmd_queue_end_state = state;
715         return cmd_queue_end_state;
716 }
717
718 tap_state_t jtag_get_end_state(void)
719 {
720         return cmd_queue_end_state;
721 }
722
723 void jtag_add_sleep(uint32_t us)
724 {
725         /// @todo Here, keep_alive() appears to be a layering violation!!!
726         keep_alive();
727         jtag_set_error(interface_jtag_add_sleep(us));
728 }
729
730 int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value, uint8_t *in_check_mask, int num_bits)
731 {
732         int retval = ERROR_OK;
733
734         int compare_failed = 0;
735
736         if (in_check_mask)
737                 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
738         else
739                 compare_failed = buf_cmp(captured, in_check_value, num_bits);
740
741         if (compare_failed) {
742                 /* An error handler could have caught the failing check
743                  * only report a problem when there wasn't a handler, or if the handler
744                  * acknowledged the error
745                  */
746                 /*
747                 LOG_WARNING("TAP %s:",
748                                         jtag_tap_name(field->tap));
749                                         */
750                 if (compare_failed)
751                 {
752                         char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
753                         char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
754
755                         if (in_check_mask)
756                         {
757                                 char *in_check_mask_char;
758                                 in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
759                                 LOG_WARNING("value captured during scan didn't pass the requested check:");
760                                 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
761                                                         captured_char, in_check_value_char, in_check_mask_char);
762                                 free(in_check_mask_char);
763                         }
764                         else
765                         {
766                                 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);
767                         }
768
769                         free(captured_char);
770                         free(in_check_value_char);
771
772                         retval = ERROR_JTAG_QUEUE_FAILED;
773                 }
774
775         }
776         return retval;
777 }
778
779 void jtag_check_value_mask(scan_field_t *field, uint8_t *value, uint8_t *mask)
780 {
781         assert(field->in_value != NULL);
782
783         if (value == NULL)
784         {
785                 /* no checking to do */
786                 return;
787         }
788
789         jtag_execute_queue_noclear();
790
791         int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
792         jtag_set_error(retval);
793 }
794
795
796
797 int default_interface_jtag_execute_queue(void)
798 {
799         if (NULL == jtag)
800         {
801                 LOG_ERROR("No JTAG interface configured yet.  "
802                         "Issue 'init' command in startup scripts "
803                         "before communicating with targets.");
804                 return ERROR_FAIL;
805         }
806
807         return jtag->execute_queue();
808 }
809
810 void jtag_execute_queue_noclear(void)
811 {
812         jtag_flush_queue_count++;
813         jtag_set_error(interface_jtag_execute_queue());
814 }
815
816 int jtag_get_flush_queue_count(void)
817 {
818         return jtag_flush_queue_count;
819 }
820
821 int jtag_execute_queue(void)
822 {
823         jtag_execute_queue_noclear();
824         return jtag_error_clear();
825 }
826
827 static int jtag_reset_callback(enum jtag_event event, void *priv)
828 {
829         jtag_tap_t *tap = priv;
830
831         LOG_DEBUG("-");
832
833         if (event == JTAG_TRST_ASSERTED)
834         {
835                 tap->enabled = !tap->disabled_after_reset;
836
837                 /* current instruction is either BYPASS or IDCODE */
838                 buf_set_ones(tap->cur_instr, tap->ir_length);
839                 tap->bypass = 1;
840         }
841
842         return ERROR_OK;
843 }
844
845 void jtag_sleep(uint32_t us)
846 {
847         alive_sleep(us/1000);
848 }
849
850 /// maximum number of JTAG devices expected in the chain
851 #define JTAG_MAX_CHAIN_SIZE 20
852
853 #define EXTRACT_MFG(X)  (((X) & 0xffe) >> 1)
854 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
855 #define EXTRACT_VER(X)  (((X) & 0xf0000000) >> 28)
856
857 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
858 {
859         scan_field_t field = {
860                         .tap = NULL,
861                         .num_bits = num_idcode * 32,
862                         .out_value = idcode_buffer,
863                         .in_value = idcode_buffer,
864                 };
865
866         // initialize to the end of chain ID value
867         for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
868                 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
869
870         jtag_add_plain_dr_scan(1, &field, TAP_DRPAUSE);
871         jtag_add_tlr();
872         return jtag_execute_queue();
873 }
874
875 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
876 {
877         uint8_t zero_check = 0x0;
878         uint8_t one_check = 0xff;
879
880         for (unsigned i = 0; i < count * 4; i++)
881         {
882                 zero_check |= idcodes[i];
883                 one_check &= idcodes[i];
884         }
885
886         /* if there wasn't a single non-zero bit or if all bits were one,
887          * the scan is not valid */
888         if (zero_check == 0x00 || one_check == 0xff)
889         {
890                 LOG_ERROR("JTAG communication failure: check connection, "
891                         "JTAG interface, target power etc.");
892                 return false;
893         }
894         return true;
895 }
896
897 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
898                 const char *name, uint32_t idcode)
899 {
900         log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
901                                   "JTAG tap: %s %16.16s: 0x%08x "
902                                   "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
903                                   name, msg,
904                                   (unsigned int)idcode,
905                                   (unsigned int)EXTRACT_MFG(idcode),
906                                   (unsigned int)EXTRACT_PART(idcode),
907                                   (unsigned int)EXTRACT_VER(idcode));
908 }
909
910 static bool jtag_idcode_is_final(uint32_t idcode)
911 {
912                 return idcode == 0x000000FF || idcode == 0xFFFFFFFF;
913 }
914
915 /**
916  * This helper checks that remaining bits in the examined chain data are
917  * all as expected, but a single JTAG device requires only 64 bits to be
918  * read back correctly.  This can help identify and diagnose problems
919  * with the JTAG chain earlier, gives more helpful/explicit error messages.
920  */
921 static void jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
922 {
923         bool triggered = false;
924         for (; count < max - 31; count += 32)
925         {
926                 uint32_t idcode = buf_get_u32(idcodes, count, 32);
927                 // do not trigger the warning if the data looks good
928                 if (!triggered && jtag_idcode_is_final(idcode))
929                         continue;
930                 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
931                                         count, (unsigned int)idcode);
932                 triggered = true;
933         }
934 }
935
936 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
937 {
938         if (0 == tap->expected_ids_cnt)
939         {
940                 /// @todo Enable LOG_INFO to ask for reports about unknown TAP IDs.
941 #if 0
942                 LOG_INFO("Uknown JTAG TAP ID: 0x%08x", tap->idcode)
943                 LOG_INFO("Please report the chip name and reported ID code to the openocd project");
944 #endif
945                 return true;
946         }
947
948         /* Loop over the expected identification codes and test for a match */
949         uint8_t ii;
950         for (ii = 0; ii < tap->expected_ids_cnt; ii++)
951         {
952                 if (tap->idcode == tap->expected_ids[ii])
953                         break;
954         }
955
956         /* If none of the expected ids matched, log an error */
957         if (ii != tap->expected_ids_cnt)
958         {
959                 LOG_DEBUG("JTAG Tap/device matched");
960                 return true;
961         }
962         jtag_examine_chain_display(LOG_LVL_ERROR, "got",
963                         tap->dotted_name, tap->idcode);
964         for (ii = 0; ii < tap->expected_ids_cnt; ii++)
965         {
966                 char msg[32];
967                 snprintf(msg, sizeof(msg), "expected %hhu of %hhu",
968                                 ii + 1, tap->expected_ids_cnt);
969                 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
970                                 tap->dotted_name, tap->expected_ids[ii]);
971         }
972         return false;
973 }
974
975 /* Try to examine chain layout according to IEEE 1149.1 Â§12
976  */
977 int jtag_examine_chain(void)
978 {
979         uint8_t idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
980         unsigned device_count = 0;
981
982         jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
983
984         if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
985                 return ERROR_JTAG_INIT_FAILED;
986
987         /* point at the 1st tap */
988         jtag_tap_t *tap = jtag_tap_next_enabled(NULL);
989         if (tap == NULL)
990         {
991                 LOG_ERROR("JTAG: No taps enabled?");
992                 return ERROR_JTAG_INIT_FAILED;
993         }
994
995         for (unsigned bit_count = 0;
996                         tap && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;
997                         tap = jtag_tap_next_enabled(tap))
998         {
999                 uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1000
1001                 if ((idcode & 1) == 0)
1002                 {
1003                         /* LSB must not be 0, this indicates a device in bypass */
1004                         LOG_WARNING("Tap/Device does not have IDCODE");
1005                         idcode = 0;
1006                         tap->hasidcode = false;
1007
1008                         bit_count += 1;
1009                 }
1010                 else
1011                 {
1012                         tap->hasidcode = true;
1013
1014                         /*
1015                          * End of chain (invalid manufacturer ID) some devices, such
1016                          * as AVR will output all 1's instead of TDI input value at
1017                          * end of chain.
1018                          */
1019                         if (jtag_idcode_is_final(idcode))
1020                         {
1021                                 jtag_examine_chain_end(idcode_buffer,
1022                                                 bit_count + 32, JTAG_MAX_CHAIN_SIZE * 32);
1023                                 break;
1024                         }
1025
1026                         jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found",
1027                                         tap ? tap->dotted_name : "(not-named)",
1028                                         idcode);
1029
1030                         bit_count += 32;
1031                 }
1032                 device_count++;
1033                 if (!tap)
1034                         continue;
1035
1036                 tap->idcode = idcode;
1037
1038                 // ensure the TAP ID does matches what was expected
1039                 if (!jtag_examine_chain_match_tap(tap))
1040                         return ERROR_JTAG_INIT_FAILED;
1041         }
1042
1043         /* see if number of discovered devices matches configuration */
1044         if (device_count != jtag_tap_count_enabled())
1045         {
1046                 LOG_ERROR("number of discovered devices in JTAG chain (%i) "
1047                                 "does not match (enabled) configuration (%i), total taps: %d",
1048                                 device_count, jtag_tap_count_enabled(), jtag_tap_count());
1049                 LOG_ERROR("check the config file and ensure proper JTAG communication"
1050                                 " (connections, speed, ...)");
1051                 return ERROR_JTAG_INIT_FAILED;
1052         }
1053
1054         return ERROR_OK;
1055 }
1056
1057 int jtag_validate_chain(void)
1058 {
1059         jtag_tap_t *tap;
1060         int total_ir_length = 0;
1061         uint8_t *ir_test = NULL;
1062         scan_field_t field;
1063         int chain_pos = 0;
1064
1065         tap = NULL;
1066         total_ir_length = 0;
1067         for (;;) {
1068                 tap = jtag_tap_next_enabled(tap);
1069                 if (tap == NULL) {
1070                         break;
1071                 }
1072                 total_ir_length += tap->ir_length;
1073         }
1074
1075         total_ir_length += 2;
1076         ir_test = malloc(CEIL(total_ir_length, 8));
1077         buf_set_ones(ir_test, total_ir_length);
1078
1079         field.tap = NULL;
1080         field.num_bits = total_ir_length;
1081         field.out_value = ir_test;
1082         field.in_value = ir_test;
1083
1084
1085         jtag_add_plain_ir_scan(1, &field, TAP_IRPAUSE);
1086         jtag_add_tlr();
1087
1088         int retval;
1089         retval = jtag_execute_queue();
1090         if (retval != ERROR_OK)
1091                 return retval;
1092
1093         tap = NULL;
1094         chain_pos = 0;
1095         int val;
1096         for (;;) {
1097                 tap = jtag_tap_next_enabled(tap);
1098                 if (tap == NULL) {
1099                         break;
1100                 }
1101
1102                 val = buf_get_u32(ir_test, chain_pos, 2);
1103                 /* Only fail this check if we have IDCODE for this device */
1104                 if ((val != 0x1)&&(tap->hasidcode))
1105                 {
1106                         char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1107                         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);
1108                         free(cbuf);
1109                         free(ir_test);
1110                         return ERROR_JTAG_INIT_FAILED;
1111                 }
1112                 chain_pos += tap->ir_length;
1113         }
1114
1115         val = buf_get_u32(ir_test, chain_pos, 2);
1116         if (val != 0x3)
1117         {
1118                 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1119                 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);
1120                 free(cbuf);
1121                 free(ir_test);
1122                 return ERROR_JTAG_INIT_FAILED;
1123         }
1124
1125         free(ir_test);
1126
1127         return ERROR_OK;
1128 }
1129
1130
1131 void jtag_tap_init(jtag_tap_t *tap)
1132 {
1133         assert(0 != tap->ir_length);
1134
1135         tap->expected = malloc(tap->ir_length);
1136         tap->expected_mask = malloc(tap->ir_length);
1137         tap->cur_instr = malloc(tap->ir_length);
1138
1139         /// @todo cope sanely with ir_length bigger than 32 bits
1140         buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
1141         buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
1142         buf_set_ones(tap->cur_instr, tap->ir_length);
1143
1144         // place TAP in bypass mode
1145         tap->bypass = 1;
1146         // register the reset callback for the TAP
1147         jtag_register_event_callback(&jtag_reset_callback, tap);
1148
1149         LOG_DEBUG("Created Tap: %s @ abs position %d, "
1150                         "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1151                                 tap->abs_chain_position, tap->ir_length,
1152                           (unsigned int)(tap->ir_capture_value), (unsigned int)(tap->ir_capture_mask));
1153         jtag_tap_add(tap);
1154 }
1155
1156 void jtag_tap_free(jtag_tap_t *tap)
1157 {
1158         jtag_unregister_event_callback(&jtag_reset_callback, tap);
1159
1160         /// @todo is anything missing? no memory leaks please
1161         free((void *)tap->expected_ids);
1162         free((void *)tap->chip);
1163         free((void *)tap->tapname);
1164         free((void *)tap->dotted_name);
1165         free(tap);
1166 }
1167
1168 int jtag_interface_init(struct command_context_s *cmd_ctx)
1169 {
1170         if (jtag)
1171                 return ERROR_OK;
1172
1173         if (!jtag_interface)
1174         {
1175                 /* nothing was previously specified by "interface" command */
1176                 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1177                 return ERROR_JTAG_INVALID_INTERFACE;
1178         }
1179
1180         jtag = jtag_interface;
1181         if (jtag_interface->init() != ERROR_OK)
1182         {
1183                 jtag = NULL;
1184                 return ERROR_JTAG_INIT_FAILED;
1185         }
1186
1187         int requested_khz = jtag_get_speed_khz();
1188         int actual_khz = requested_khz;
1189         int retval = jtag_get_speed_readable(&actual_khz);
1190         if (ERROR_OK != retval)
1191                 LOG_INFO("interface specific clock speed value %d", jtag_get_speed());
1192         else if (actual_khz)
1193         {
1194                 if ((CLOCK_MODE_RCLK == clock_mode)
1195                         || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz))
1196                 {
1197                         LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1198                                 , actual_khz);
1199                 }
1200                 else
1201                         LOG_INFO("clock speed %d kHz", actual_khz);
1202         }
1203         else
1204                 LOG_INFO("RCLK (adaptive clock speed)");
1205
1206         return ERROR_OK;
1207 }
1208
1209 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1210 {
1211         jtag_tap_t *tap;
1212         int retval;
1213
1214         LOG_DEBUG("Init JTAG chain");
1215
1216         tap = jtag_tap_next_enabled(NULL);
1217         if (tap == NULL) {
1218                 LOG_ERROR("There are no enabled taps?");
1219                 return ERROR_JTAG_INIT_FAILED;
1220         }
1221
1222         jtag_add_tlr();
1223         if ((retval = jtag_execute_queue()) != ERROR_OK)
1224                 return retval;
1225
1226         /* examine chain first, as this could discover the real chain layout */
1227         if (jtag_examine_chain() != ERROR_OK)
1228         {
1229                 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1230         }
1231
1232         if (jtag_validate_chain() != ERROR_OK)
1233         {
1234                 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
1235         }
1236
1237         return ERROR_OK;
1238 }
1239
1240 int jtag_interface_quit(void)
1241 {
1242         if (!jtag || !jtag->quit)
1243                 return ERROR_OK;
1244
1245         // close the JTAG interface
1246         int result = jtag->quit();
1247         if (ERROR_OK != result)
1248                 LOG_ERROR("failed: %d", result);
1249
1250         return ERROR_OK;
1251 }
1252
1253
1254 int jtag_init_reset(struct command_context_s *cmd_ctx)
1255 {
1256         int retval;
1257
1258         if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1259                 return retval;
1260
1261         LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
1262
1263         /* Reset can happen after a power cycle.
1264          *
1265          * Ideally we would only assert TRST or run TLR before the target reset.
1266          *
1267          * However w/srst_pulls_trst, trst is asserted together with the target
1268          * reset whether we want it or not.
1269          *
1270          * NB! Some targets have JTAG circuitry disabled until a
1271          * trst & srst has been asserted.
1272          *
1273          * NB! here we assume nsrst/ntrst delay are sufficient!
1274          *
1275          * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1276          *
1277          */
1278         jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1279         if (jtag_reset_config & RESET_HAS_SRST)
1280         {
1281                 jtag_add_reset(1, 1);
1282                 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1283                         jtag_add_reset(0, 1);
1284         }
1285         jtag_add_reset(0, 0);
1286         if ((retval = jtag_execute_queue()) != ERROR_OK)
1287                 return retval;
1288
1289         /* Check that we can communication on the JTAG chain + eventually we want to
1290          * be able to perform enumeration only after OpenOCD has started
1291          * telnet and GDB server
1292          *
1293          * That would allow users to more easily perform any magic they need to before
1294          * reset happens.
1295          */
1296         return jtag_init_inner(cmd_ctx);
1297 }
1298
1299 int jtag_init(struct command_context_s *cmd_ctx)
1300 {
1301         int retval;
1302         if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1303                 return retval;
1304         if (jtag_init_inner(cmd_ctx) == ERROR_OK)
1305         {
1306                 return ERROR_OK;
1307         }
1308         return jtag_init_reset(cmd_ctx);
1309 }
1310
1311 unsigned jtag_get_speed_khz(void)
1312 {
1313         return speed_khz;
1314 }
1315
1316 static int jtag_khz_to_speed(unsigned khz, int* speed)
1317 {
1318         LOG_DEBUG("convert khz to interface specific speed value");
1319         speed_khz = khz;
1320         if (jtag != NULL)
1321         {
1322                 LOG_DEBUG("have interface set up");
1323                 int speed_div1;
1324                 int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
1325                 if (ERROR_OK != retval)
1326                 {
1327                         return retval;
1328                 }
1329                 *speed = speed_div1;
1330         }
1331         return ERROR_OK;
1332 }
1333
1334 static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int* speed)
1335 {
1336         int retval = jtag_khz_to_speed(0, speed);
1337         if ((ERROR_OK != retval) && fallback_speed_khz)
1338         {
1339                 LOG_DEBUG("trying fallback speed...");
1340                 retval = jtag_khz_to_speed(fallback_speed_khz, speed);
1341         }
1342         return retval;
1343 }
1344
1345 static int jtag_set_speed(int speed)
1346 {
1347         jtag_speed = speed;
1348         /* this command can be called during CONFIG,
1349          * in which case jtag isn't initialized */
1350         return jtag ? jtag->speed(speed) : ERROR_OK;
1351 }
1352
1353 int jtag_config_speed(int speed)
1354 {
1355         LOG_DEBUG("handle jtag speed");
1356         clock_mode = CLOCK_MODE_SPEED;
1357         return jtag_set_speed(speed);
1358 }
1359
1360 int jtag_config_khz(unsigned khz)
1361 {
1362         LOG_DEBUG("handle jtag khz");
1363         clock_mode = CLOCK_MODE_KHZ;
1364         int speed = 0;
1365         int retval = jtag_khz_to_speed(khz, &speed);
1366         return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1367 }
1368
1369 int jtag_config_rclk(unsigned fallback_speed_khz)
1370 {
1371         LOG_DEBUG("handle jtag rclk");
1372         clock_mode = CLOCK_MODE_RCLK;
1373         rclk_fallback_speed_khz = fallback_speed_khz;
1374         int speed = 0;
1375         int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
1376         return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1377 }
1378
1379 int jtag_get_speed(void)
1380 {
1381         int speed;
1382         switch(clock_mode)
1383         {
1384                 case CLOCK_MODE_SPEED:
1385                         speed = jtag_speed;
1386                         break;
1387                 case CLOCK_MODE_KHZ:
1388                         jtag_khz_to_speed(jtag_get_speed_khz(), &speed);
1389                         break;
1390                 case CLOCK_MODE_RCLK:
1391                         jtag_rclk_to_speed(rclk_fallback_speed_khz, &speed);
1392                         break;
1393                 default:
1394                         LOG_ERROR("BUG: unknown jtag clock mode");
1395                         speed = 0;
1396                         break;
1397         }
1398         return speed;
1399 }
1400
1401 int jtag_get_speed_readable(int *khz)
1402 {
1403         return jtag ? jtag->speed_div(jtag_get_speed(), khz) : ERROR_OK;
1404 }
1405
1406 void jtag_set_verify(bool enable)
1407 {
1408         jtag_verify = enable;
1409 }
1410
1411 bool jtag_will_verify()
1412 {
1413         return jtag_verify;
1414 }
1415
1416 void jtag_set_verify_capture_ir(bool enable)
1417 {
1418         jtag_verify_capture_ir = enable;
1419 }
1420
1421 bool jtag_will_verify_capture_ir()
1422 {
1423         return jtag_verify_capture_ir;
1424 }
1425
1426 int jtag_power_dropout(int *dropout)
1427 {
1428         return jtag->power_dropout(dropout);
1429 }
1430
1431 int jtag_srst_asserted(int *srst_asserted)
1432 {
1433         return jtag->srst_asserted(srst_asserted);
1434 }
1435
1436 enum reset_types jtag_get_reset_config(void)
1437 {
1438         return jtag_reset_config;
1439 }
1440 void jtag_set_reset_config(enum reset_types type)
1441 {
1442         jtag_reset_config = type;
1443 }
1444
1445 int jtag_get_trst(void)
1446 {
1447         return jtag_trst;
1448 }
1449 int jtag_get_srst(void)
1450 {
1451         return jtag_srst;
1452 }
1453
1454 void jtag_set_nsrst_delay(unsigned delay)
1455 {
1456         jtag_nsrst_delay = delay;
1457 }
1458 unsigned jtag_get_nsrst_delay(void)
1459 {
1460         return jtag_nsrst_delay;
1461 }
1462 void jtag_set_ntrst_delay(unsigned delay)
1463 {
1464         jtag_ntrst_delay = delay;
1465 }
1466 unsigned jtag_get_ntrst_delay(void)
1467 {
1468         return jtag_ntrst_delay;
1469 }