a2709937dbda2beb64017e9cebf188f9e7cff4b4
[openocd.git] / src / jtag / jtag.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 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU General Public License *
23 * along with this program; if not, write to the *
24 * Free Software Foundation, Inc., *
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
26 ***************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "jtag.h"
32
33 #ifdef HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
36
37
38 int jtag_flush_queue_count; /* count # of flushes for profiling / debugging purposes */
39
40 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
41 int in_num_fields, scan_field_t *in_fields, tap_state_t state);
42
43 /* note that this is not marked as static as it must be available from outside jtag.c for those
44 that implement the jtag_xxx() minidriver layer
45 */
46 int jtag_error=ERROR_OK;
47
48 typedef struct cmd_queue_page_s
49 {
50 void *address;
51 size_t used;
52 struct cmd_queue_page_s *next;
53 } cmd_queue_page_t;
54
55 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
56 static cmd_queue_page_t *cmd_queue_pages = NULL;
57
58 char* jtag_event_strings[] =
59 {
60 "JTAG controller reset (RESET or TRST)"
61 };
62
63 const Jim_Nvp nvp_jtag_tap_event[] = {
64 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
65 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
66
67 { .name = NULL, .value = -1 }
68 };
69
70 int jtag_trst = 0;
71 int jtag_srst = 0;
72
73 #ifndef HAVE_JTAG_MINIDRIVER_H
74 struct jtag_callback_entry
75 {
76 struct jtag_callback_entry *next;
77
78 jtag_callback_t callback;
79 u8 *in;
80 jtag_callback_data_t data1;
81 jtag_callback_data_t data2;
82 jtag_callback_data_t data3;
83 };
84
85
86 static struct jtag_callback_entry *jtag_callback_queue_head = NULL;
87 static struct jtag_callback_entry *jtag_callback_queue_tail = NULL;
88 #endif
89
90
91 jtag_command_t *jtag_command_queue = NULL;
92 jtag_command_t **last_command_pointer = &jtag_command_queue;
93 static jtag_tap_t *jtag_all_taps = NULL;
94
95 enum reset_types jtag_reset_config = RESET_NONE;
96 tap_state_t cmd_queue_end_state = TAP_RESET;
97 tap_state_t cmd_queue_cur_state = TAP_RESET;
98
99 int jtag_verify_capture_ir = 1;
100 int jtag_verify = 1;
101
102 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
103 static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
104 static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
105
106 /* maximum number of JTAG devices expected in the chain
107 */
108 #define JTAG_MAX_CHAIN_SIZE 20
109
110 /* callbacks to inform high-level handlers about JTAG state changes */
111 jtag_event_callback_t *jtag_event_callbacks;
112
113 /* speed in kHz*/
114 static int speed_khz = 0;
115 /* flag if the kHz speed was defined */
116 static int hasKHz = 0;
117
118 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
119 */
120
121 #if BUILD_ECOSBOARD == 1
122 extern jtag_interface_t zy1000_interface;
123 #endif
124
125 #if BUILD_PARPORT == 1
126 extern jtag_interface_t parport_interface;
127 #endif
128
129 #if BUILD_DUMMY == 1
130 extern jtag_interface_t dummy_interface;
131 #endif
132
133 #if BUILD_FT2232_FTD2XX == 1
134 extern jtag_interface_t ft2232_interface;
135 #endif
136
137 #if BUILD_FT2232_LIBFTDI == 1
138 extern jtag_interface_t ft2232_interface;
139 #endif
140
141 #if BUILD_AMTJTAGACCEL == 1
142 extern jtag_interface_t amt_jtagaccel_interface;
143 #endif
144
145 #if BUILD_EP93XX == 1
146 extern jtag_interface_t ep93xx_interface;
147 #endif
148
149 #if BUILD_AT91RM9200 == 1
150 extern jtag_interface_t at91rm9200_interface;
151 #endif
152
153 #if BUILD_GW16012 == 1
154 extern jtag_interface_t gw16012_interface;
155 #endif
156
157 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
158 extern jtag_interface_t presto_interface;
159 #endif
160
161 #if BUILD_USBPROG == 1
162 extern jtag_interface_t usbprog_interface;
163 #endif
164
165 #if BUILD_JLINK == 1
166 extern jtag_interface_t jlink_interface;
167 #endif
168
169 #if BUILD_VSLLINK == 1
170 extern jtag_interface_t vsllink_interface;
171 #endif
172
173 #if BUILD_RLINK == 1
174 extern jtag_interface_t rlink_interface;
175 #endif
176
177 #if BUILD_ARMJTAGEW == 1
178 extern jtag_interface_t armjtagew_interface;
179 #endif
180
181 jtag_interface_t *jtag_interfaces[] = {
182 #if BUILD_ECOSBOARD == 1
183 &zy1000_interface,
184 #endif
185 #if BUILD_PARPORT == 1
186 &parport_interface,
187 #endif
188 #if BUILD_DUMMY == 1
189 &dummy_interface,
190 #endif
191 #if BUILD_FT2232_FTD2XX == 1
192 &ft2232_interface,
193 #endif
194 #if BUILD_FT2232_LIBFTDI == 1
195 &ft2232_interface,
196 #endif
197 #if BUILD_AMTJTAGACCEL == 1
198 &amt_jtagaccel_interface,
199 #endif
200 #if BUILD_EP93XX == 1
201 &ep93xx_interface,
202 #endif
203 #if BUILD_AT91RM9200 == 1
204 &at91rm9200_interface,
205 #endif
206 #if BUILD_GW16012 == 1
207 &gw16012_interface,
208 #endif
209 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
210 &presto_interface,
211 #endif
212 #if BUILD_USBPROG == 1
213 &usbprog_interface,
214 #endif
215 #if BUILD_JLINK == 1
216 &jlink_interface,
217 #endif
218 #if BUILD_VSLLINK == 1
219 &vsllink_interface,
220 #endif
221 #if BUILD_RLINK == 1
222 &rlink_interface,
223 #endif
224 #if BUILD_ARMJTAGEW == 1
225 &armjtagew_interface,
226 #endif
227 NULL,
228 };
229
230 jtag_interface_t *jtag = NULL;
231
232 /* configuration */
233 static jtag_interface_t *jtag_interface = NULL;
234 int jtag_speed = 0;
235
236 /* forward declarations */
237 //void jtag_add_pathmove(int num_states, tap_state_t *path);
238 //void jtag_add_runtest(int num_cycles, tap_state_t endstate);
239 //void jtag_add_end_state(tap_state_t endstate);
240 //void jtag_add_sleep(u32 us);
241 //int jtag_execute_queue(void);
242 static tap_state_t tap_state_by_name(const char *name);
243
244 /* jtag commands */
245 static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
247 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
249 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
250 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
251 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
252
253 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
254
255 static int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
256 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
257 static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
258 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
259 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
260 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
261
262 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
263 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
264 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
265
266 jtag_tap_t *jtag_AllTaps(void)
267 {
268 return jtag_all_taps;
269 };
270
271 int jtag_NumTotalTaps(void)
272 {
273 jtag_tap_t *t;
274 int n;
275
276 n = 0;
277 t = jtag_AllTaps();
278 while(t){
279 n++;
280 t = t->next_tap;
281 }
282 return n;
283 }
284
285 int jtag_NumEnabledTaps(void)
286 {
287 jtag_tap_t *t;
288 int n;
289
290 n = 0;
291 t = jtag_AllTaps();
292 while(t){
293 if( t->enabled ){
294 n++;
295 }
296 t = t->next_tap;
297 }
298 return n;
299 }
300
301 jtag_tap_t *jtag_TapByString( const char *s )
302 {
303 jtag_tap_t *t;
304 char *cp;
305
306 t = jtag_AllTaps();
307 /* try name first */
308 while(t){
309 if( 0 == strcmp( t->dotted_name, s ) ){
310 break;
311 } else {
312 t = t->next_tap;
313 }
314 }
315 /* backup plan is by number */
316 if( t == NULL ){
317 /* ok - is "s" a number? */
318 int n;
319 n = strtol( s, &cp, 0 );
320 if( (s != cp) && (*cp == 0) ){
321 /* Then it is... */
322 t = jtag_TapByAbsPosition(n);
323 }
324 }
325 return t;
326 }
327
328 jtag_tap_t * jtag_TapByJimObj( Jim_Interp *interp, Jim_Obj *o )
329 {
330 jtag_tap_t *t;
331 const char *cp;
332
333 cp = Jim_GetString( o, NULL );
334 if(cp == NULL){
335 cp = "(unknown)";
336 t = NULL;
337 } else {
338 t = jtag_TapByString( cp );
339 }
340 if( t == NULL ){
341 Jim_SetResult_sprintf(interp,"Tap: %s is unknown", cp );
342 }
343 return t;
344 }
345
346 /* returns a pointer to the n-th device in the scan chain */
347 jtag_tap_t * jtag_TapByAbsPosition( int n )
348 {
349 int orig_n;
350 jtag_tap_t *t;
351
352 orig_n = n;
353 t = jtag_AllTaps();
354
355 while( t && (n > 0)) {
356 n--;
357 t = t->next_tap;
358 }
359 return t;
360 }
361
362 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
363 {
364 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
365
366 if (callback == NULL)
367 {
368 return ERROR_INVALID_ARGUMENTS;
369 }
370
371 if (*callbacks_p)
372 {
373 while ((*callbacks_p)->next)
374 callbacks_p = &((*callbacks_p)->next);
375 callbacks_p = &((*callbacks_p)->next);
376 }
377
378 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
379 (*callbacks_p)->callback = callback;
380 (*callbacks_p)->priv = priv;
381 (*callbacks_p)->next = NULL;
382
383 return ERROR_OK;
384 }
385
386 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
387 {
388 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
389
390 if (callback == NULL)
391 {
392 return ERROR_INVALID_ARGUMENTS;
393 }
394
395 while (*callbacks_p)
396 {
397 jtag_event_callback_t **next = &((*callbacks_p)->next);
398 if ((*callbacks_p)->callback == callback)
399 {
400 free(*callbacks_p);
401 *callbacks_p = *next;
402 }
403 callbacks_p = next;
404 }
405
406 return ERROR_OK;
407 }
408
409 int jtag_call_event_callbacks(enum jtag_event event)
410 {
411 jtag_event_callback_t *callback = jtag_event_callbacks;
412
413 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
414
415 while (callback)
416 {
417 callback->callback(event, callback->priv);
418 callback = callback->next;
419 }
420
421 return ERROR_OK;
422 }
423
424 /* returns a pointer to the pointer of the last command in queue
425 * this may be a pointer to the root pointer (jtag_command_queue)
426 * or to the next member of the last but one command
427 */
428 jtag_command_t** jtag_get_last_command_p(void)
429 {
430 /* jtag_command_t *cmd = jtag_command_queue;
431
432 if (cmd)
433 while (cmd->next)
434 cmd = cmd->next;
435 else
436 return &jtag_command_queue;
437
438 return &cmd->next;*/
439
440 return last_command_pointer;
441 }
442
443
444 void jtag_queue_command(jtag_command_t * cmd)
445 {
446 jtag_command_t **last_cmd;
447
448 last_cmd = jtag_get_last_command_p();
449
450 *last_cmd = cmd;
451
452 (*last_cmd)->next = NULL;
453
454 last_command_pointer = &((*last_cmd)->next);
455 }
456
457
458 void* cmd_queue_alloc(size_t size)
459 {
460 cmd_queue_page_t **p_page = &cmd_queue_pages;
461 int offset;
462 u8 *t;
463
464 /*
465 * WARNING:
466 * We align/round the *SIZE* per below
467 * so that all pointers returned by
468 * this function are reasonably well
469 * aligned.
470 *
471 * If we did not, then an "odd-length" request would cause the
472 * *next* allocation to be at an *odd* address, and because
473 * this function has the same type of api as malloc() - we
474 * must also return pointers that have the same type of
475 * alignment.
476 *
477 * What I do not/have is a reasonable portable means
478 * to align by...
479 *
480 * The solution here, is based on these suggestions.
481 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
482 *
483 */
484 union worse_case_align {
485 int i;
486 long l;
487 float f;
488 void *v;
489 };
490 #define ALIGN_SIZE (sizeof(union worse_case_align))
491
492 /* The alignment process. */
493 size = (size + ALIGN_SIZE -1) & (~(ALIGN_SIZE-1));
494 /* Done... */
495
496 if (*p_page)
497 {
498 while ((*p_page)->next)
499 p_page = &((*p_page)->next);
500 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
501 p_page = &((*p_page)->next);
502 }
503
504 if (!*p_page)
505 {
506 *p_page = malloc(sizeof(cmd_queue_page_t));
507 (*p_page)->used = 0;
508 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
509 (*p_page)->next = NULL;
510 }
511
512 offset = (*p_page)->used;
513 (*p_page)->used += size;
514
515 t=(u8 *)((*p_page)->address);
516 return t + offset;
517 }
518
519 void cmd_queue_free(void)
520 {
521 cmd_queue_page_t *page = cmd_queue_pages;
522
523 while (page)
524 {
525 cmd_queue_page_t *last = page;
526 free(page->address);
527 page = page->next;
528 free(last);
529 }
530
531 cmd_queue_pages = NULL;
532 }
533
534 /**
535 * Copy a scan_field_t for insertion into the queue.
536 *
537 * This allocates a new copy of out_value using cmd_queue_alloc.
538 */
539 static void cmd_queue_scan_field_clone(scan_field_t * dst, const scan_field_t * src)
540 {
541 dst->tap = src->tap;
542 dst->num_bits = src->num_bits;
543 dst->out_value = buf_cpy(src->out_value, cmd_queue_alloc(CEIL(src->num_bits, 8)), src->num_bits);
544 dst->in_value = src->in_value;
545 }
546
547
548 static void jtag_prelude1(void)
549 {
550 if (jtag_trst == 1)
551 {
552 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
553 jtag_error=ERROR_JTAG_TRST_ASSERTED;
554 return;
555 }
556
557 if (cmd_queue_end_state == TAP_RESET)
558 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
559 }
560
561 static void jtag_prelude(tap_state_t state)
562 {
563 jtag_prelude1();
564
565 if (state != TAP_INVALID)
566 jtag_add_end_state(state);
567
568 cmd_queue_cur_state = cmd_queue_end_state;
569 }
570
571 void jtag_add_ir_scan_noverify(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
572 {
573 int retval;
574 jtag_prelude(state);
575
576 retval=interface_jtag_add_ir_scan(in_num_fields, in_fields, cmd_queue_end_state);
577 if (retval!=ERROR_OK)
578 jtag_error=retval;
579
580 }
581
582
583 /**
584 * Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
585 *
586 * If the input field list contains an instruction value for a TAP then that is used
587 * otherwise the TAP is set to bypass.
588 *
589 * TAPs for which no fields are passed are marked as bypassed for subsequent DR SCANs.
590 *
591 */
592 void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
593 {
594 if (jtag_verify&&jtag_verify_capture_ir)
595 {
596 /* 8 x 32 bit id's is enough for all invoations */
597
598 for (int j = 0; j < in_num_fields; j++)
599 {
600 in_fields[j].check_value=NULL;
601 in_fields[j].check_mask=NULL;
602 /* if we are to run a verification of the ir scan, we need to get the input back.
603 * We may have to allocate space if the caller didn't ask for the input back.
604 */
605 in_fields[j].check_value=in_fields[j].tap->expected;
606 in_fields[j].check_mask=in_fields[j].tap->expected_mask;
607 }
608 jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
609 } else
610 {
611 jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
612 }
613 }
614
615 /**
616 * see jtag_add_ir_scan()
617 *
618 */
619 int MINIDRIVER(interface_jtag_add_ir_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
620 {
621 int nth_tap;
622
623 int num_taps = jtag_NumEnabledTaps();
624
625 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
626 scan_command_t * scan = cmd_queue_alloc(sizeof(scan_command_t));
627 scan_field_t * out_fields = cmd_queue_alloc(num_taps * sizeof(scan_field_t));
628
629 jtag_queue_command(cmd);
630
631 cmd->type = JTAG_SCAN;
632 cmd->cmd.scan = scan;
633
634 scan->ir_scan = true;
635 scan->num_fields = num_taps; /* one field per device */
636 scan->fields = out_fields;
637 scan->end_state = state;
638
639 nth_tap = -1;
640
641 for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
642 {
643 int found = 0;
644
645 nth_tap++;
646
647 assert(nth_tap < num_taps);
648
649 size_t scan_size = tap->ir_length;
650
651 /* search the list */
652 for (int j = 0; j < in_num_fields; j++)
653 {
654 if (tap == in_fields[j].tap)
655 {
656 found = 1;
657
658 tap->bypass = 0;
659
660 cmd_queue_scan_field_clone(scan->fields + nth_tap, in_fields + j);
661
662 break;
663 }
664 }
665
666 if (!found)
667 {
668 /* if a tap isn't listed, set it to BYPASS */
669 tap->bypass = 1;
670
671 scan->fields[nth_tap].tap = tap;
672 scan->fields[nth_tap].num_bits = scan_size;
673 scan->fields[nth_tap].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
674 scan->fields[nth_tap].in_value = NULL; /* do not collect input for tap's in bypass */
675 }
676
677 /* update device information */
678 buf_cpy(scan->fields[nth_tap].out_value, tap->cur_instr, scan_size);
679 }
680
681 assert(nth_tap == (num_taps - 1));
682
683 return ERROR_OK;
684 }
685
686 /**
687 * Duplicate the scan fields passed into the function into an IR SCAN command
688 *
689 * This function assumes that the caller handles extra fields for bypassed TAPs
690 *
691 */
692 void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
693 {
694 int retval;
695
696 jtag_prelude(state);
697
698 retval=interface_jtag_add_plain_ir_scan(in_num_fields, in_fields, cmd_queue_end_state);
699 if (retval!=ERROR_OK)
700 jtag_error=retval;
701 }
702
703
704 /**
705 * see jtag_add_plain_ir_scan()
706 *
707 */
708 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
709 {
710
711 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
712 scan_command_t * scan = cmd_queue_alloc(sizeof(scan_command_t));
713 scan_field_t * out_fields = cmd_queue_alloc(in_num_fields * sizeof(scan_field_t));
714
715 jtag_queue_command(cmd);
716
717 cmd->type = JTAG_SCAN;
718 cmd->cmd.scan = scan;
719
720 scan->ir_scan = true;
721 scan->num_fields = in_num_fields;
722 scan->fields = out_fields;
723 scan->end_state = state;
724
725 for (int i = 0; i < in_num_fields; i++)
726 cmd_queue_scan_field_clone(out_fields + i, in_fields + i);
727
728 return ERROR_OK;
729 }
730
731
732
733 int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits);
734
735 static int jtag_check_value_mask_callback(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
736 {
737 return jtag_check_value_inner(in, (u8 *)data1, (u8 *)data2, (int)data3);
738 }
739
740 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
741 int in_num_fields, scan_field_t *in_fields, tap_state_t state)
742 {
743 for (int i = 0; i < in_num_fields; i++)
744 {
745 in_fields[i].allocated = 0;
746 in_fields[i].modified = 0;
747 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value == NULL))
748 {
749 in_fields[i].modified = 1;
750 /* we need storage space... */
751 #ifdef HAVE_JTAG_MINIDRIVER_H
752 if (in_fields[i].num_bits <= 32)
753 {
754 /* This is enough space and we're executing this synchronously */
755 in_fields[i].in_value = in_fields[i].intmp;
756 } else
757 {
758 in_fields[i].in_value = (u8 *)malloc(CEIL(in_fields[i].num_bits, 8));
759 in_fields[i].allocated = 1;
760 }
761 #else
762 in_fields[i].in_value = (u8 *)cmd_queue_alloc(CEIL(in_fields[i].num_bits, 8));
763 #endif
764 }
765 }
766
767 jtag_add_scan(in_num_fields, in_fields, state);
768
769 for (int i = 0; i < in_num_fields; i++)
770 {
771 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
772 {
773 /* this is synchronous for a minidriver */
774 jtag_add_callback4(jtag_check_value_mask_callback, in_fields[i].in_value,
775 (jtag_callback_data_t)in_fields[i].check_value,
776 (jtag_callback_data_t)in_fields[i].check_mask,
777 (jtag_callback_data_t)in_fields[i].num_bits);
778 }
779 if (in_fields[i].allocated)
780 {
781 free(in_fields[i].in_value);
782 }
783 if (in_fields[i].modified)
784 {
785 in_fields[i].in_value = NULL;
786 }
787 }
788 }
789
790 void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
791 {
792 if (jtag_verify)
793 {
794 jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
795 } else
796 {
797 jtag_add_dr_scan(in_num_fields, in_fields, state);
798 }
799 }
800
801
802 /**
803 * Generate a DR SCAN using the fields passed to the function
804 *
805 * For not bypassed TAPs the function checks in_fields and uses fields specified there.
806 * For bypassed TAPs the function generates a dummy 1bit field.
807 *
808 * The bypass status of TAPs is set by jtag_add_ir_scan().
809 *
810 */
811 void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
812 {
813 int retval;
814
815 jtag_prelude(state);
816
817 retval=interface_jtag_add_dr_scan(in_num_fields, in_fields, cmd_queue_end_state);
818 if (retval!=ERROR_OK)
819 jtag_error=retval;
820 }
821
822
823 /**
824 * see jtag_add_dr_scan()
825 *
826 */
827 int MINIDRIVER(interface_jtag_add_dr_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
828 {
829 int j;
830 int field_count = 0;
831
832 /* count devices in bypass */
833
834 size_t bypass_devices = 0;
835
836 for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
837 {
838 if (tap->bypass)
839 bypass_devices++;
840 }
841
842 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
843 scan_command_t * scan = cmd_queue_alloc(sizeof(scan_command_t));
844 scan_field_t * out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(scan_field_t));
845
846 jtag_queue_command(cmd);
847
848 cmd->type = JTAG_SCAN;
849 cmd->cmd.scan = scan;
850
851 scan->ir_scan = false;
852 scan->num_fields = in_num_fields + bypass_devices;
853 scan->fields = out_fields;
854 scan->end_state = state;
855
856 for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
857 {
858 int found = 0;
859
860 for (j = 0; j < in_num_fields; j++)
861 {
862 if (tap == in_fields[j].tap)
863 {
864 found = 1;
865
866 cmd_queue_scan_field_clone(scan->fields + field_count, in_fields + j);
867
868 field_count++;
869 }
870 }
871 if (!found)
872 {
873 #ifdef _DEBUG_JTAG_IO_
874 /* if a device isn't listed, the BYPASS register should be selected */
875 if (! tap->bypass)
876 {
877 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
878 exit(-1);
879 }
880 #endif
881 /* program the scan field to 1 bit length, and ignore it's value */
882 scan->fields[field_count].tap = tap;
883 scan->fields[field_count].num_bits = 1;
884 scan->fields[field_count].out_value = NULL;
885 scan->fields[field_count].in_value = NULL;
886 field_count++;
887 }
888 else
889 {
890 #ifdef _DEBUG_JTAG_IO_
891 /* if a device is listed, the BYPASS register must not be selected */
892 if (tap->bypass)
893 {
894 LOG_ERROR("BUG: scan data for a device in BYPASS");
895 exit(-1);
896 }
897 #endif
898 }
899 }
900
901 /* field_count represents the true number of fields setup*/
902 scan->num_fields = field_count;
903 return ERROR_OK;
904 }
905
906
907
908 /**
909 * Generate a DR SCAN using the array of output values passed to the function
910 *
911 * This function assumes that the parameter target_tap specifies the one TAP
912 * that is not bypassed. All other TAPs must be bypassed and the function will
913 * generate a dummy 1bit field for them.
914 *
915 * For the target_tap a sequence of output-only fields will be generated where
916 * each field has the size num_bits and the field's values are taken from
917 * the array value.
918 *
919 * The bypass status of TAPs is set by jtag_add_ir_scan().
920 *
921 */
922 void MINIDRIVER(interface_jtag_add_dr_out)(jtag_tap_t *target_tap,
923 int in_num_fields,
924 const int *num_bits,
925 const u32 *value,
926 tap_state_t end_state)
927 {
928 int field_count = 0;
929
930 /* count devices in bypass */
931
932 size_t bypass_devices = 0;
933
934 for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
935 {
936 if (tap->bypass)
937 bypass_devices++;
938 }
939
940
941 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
942 scan_command_t * scan = cmd_queue_alloc(sizeof(scan_command_t));
943 scan_field_t * out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(scan_field_t));
944
945 jtag_queue_command(cmd);
946
947 cmd->type = JTAG_SCAN;
948 cmd->cmd.scan = scan;
949
950 scan->ir_scan = false;
951 scan->num_fields = in_num_fields + bypass_devices;
952 scan->fields = out_fields;
953 scan->end_state = end_state;
954
955 for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
956 {
957 scan->fields[field_count].tap = tap;
958
959 if (tap == target_tap)
960 {
961 #ifdef _DEBUG_JTAG_IO_
962 /* if a device is listed, the BYPASS register must not be selected */
963 if (tap->bypass)
964 {
965 LOG_ERROR("BUG: scan data for a device in BYPASS");
966 exit(-1);
967 }
968 #endif
969 for (int j = 0; j < in_num_fields; j++)
970 {
971 u8 out_value[4];
972 size_t scan_size = num_bits[j];
973 buf_set_u32(out_value, 0, scan_size, value[j]);
974 scan->fields[field_count].num_bits = scan_size;
975 scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
976 scan->fields[field_count].in_value = NULL;
977 field_count++;
978 }
979 } else
980 {
981 #ifdef _DEBUG_JTAG_IO_
982 /* if a device isn't listed, the BYPASS register should be selected */
983 if (! tap->bypass)
984 {
985 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
986 exit(-1);
987 }
988 #endif
989 /* program the scan field to 1 bit length, and ignore it's value */
990 scan->fields[field_count].num_bits = 1;
991 scan->fields[field_count].out_value = NULL;
992 scan->fields[field_count].in_value = NULL;
993 field_count++;
994 }
995 }
996 }
997
998
999 /**
1000 * Duplicate the scan fields passed into the function into a DR SCAN command
1001 *
1002 * This function assumes that the caller handles extra fields for bypassed TAPs
1003 *
1004 */
1005 void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
1006 {
1007 int retval;
1008
1009 jtag_prelude(state);
1010
1011 retval=interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, cmd_queue_end_state);
1012 if (retval!=ERROR_OK)
1013 jtag_error=retval;
1014 }
1015
1016
1017 /**
1018 * see jtag_add_plain_dr_scan()
1019 *
1020 */
1021 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
1022 {
1023 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1024 scan_command_t * scan = cmd_queue_alloc(sizeof(scan_command_t));
1025 scan_field_t * out_fields = cmd_queue_alloc(in_num_fields * sizeof(scan_field_t));
1026
1027 jtag_queue_command(cmd);
1028
1029 cmd->type = JTAG_SCAN;
1030 cmd->cmd.scan = scan;
1031
1032 scan->ir_scan = false;
1033 scan->num_fields = in_num_fields;
1034 scan->fields = out_fields;
1035 scan->end_state = state;
1036
1037 for (int i = 0; i < in_num_fields; i++)
1038 cmd_queue_scan_field_clone(out_fields + i, in_fields + i);
1039
1040 return ERROR_OK;
1041 }
1042
1043
1044 void jtag_add_tlr(void)
1045 {
1046 jtag_prelude(TAP_RESET);
1047
1048 int retval;
1049 retval=interface_jtag_add_tlr();
1050 if (retval!=ERROR_OK)
1051 jtag_error=retval;
1052 }
1053
1054 int MINIDRIVER(interface_jtag_add_tlr)(void)
1055 {
1056 tap_state_t state = TAP_RESET;
1057
1058 /* allocate memory for a new list member */
1059 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1060
1061 jtag_queue_command(cmd);
1062
1063 cmd->type = JTAG_STATEMOVE;
1064
1065 cmd->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
1066 cmd->cmd.statemove->end_state = state;
1067
1068 return ERROR_OK;
1069 }
1070
1071 void jtag_add_pathmove(int num_states, const tap_state_t *path)
1072 {
1073 tap_state_t cur_state = cmd_queue_cur_state;
1074 int i;
1075 int retval;
1076
1077 /* the last state has to be a stable state */
1078 if (!tap_is_state_stable(path[num_states - 1]))
1079 {
1080 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
1081 exit(-1);
1082 }
1083
1084 for (i=0; i<num_states; i++)
1085 {
1086 if (path[i] == TAP_RESET)
1087 {
1088 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
1089 exit(-1);
1090 }
1091
1092 if ( tap_state_transition(cur_state, true) != path[i]
1093 && tap_state_transition(cur_state, false) != path[i])
1094 {
1095 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[i]));
1096 exit(-1);
1097 }
1098 cur_state = path[i];
1099 }
1100
1101 jtag_prelude1();
1102
1103 retval = interface_jtag_add_pathmove(num_states, path);
1104 cmd_queue_cur_state = path[num_states - 1];
1105 if (retval!=ERROR_OK)
1106 jtag_error=retval;
1107 }
1108
1109 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, const tap_state_t *path)
1110 {
1111 /* allocate memory for a new list member */
1112 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1113
1114 jtag_queue_command(cmd);
1115
1116 cmd->type = JTAG_PATHMOVE;
1117
1118 cmd->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
1119 cmd->cmd.pathmove->num_states = num_states;
1120 cmd->cmd.pathmove->path = cmd_queue_alloc(sizeof(tap_state_t) * num_states);
1121
1122 for (int i = 0; i < num_states; i++)
1123 cmd->cmd.pathmove->path[i] = path[i];
1124
1125 return ERROR_OK;
1126 }
1127
1128 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, tap_state_t state)
1129 {
1130 /* allocate memory for a new list member */
1131 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1132
1133 jtag_queue_command(cmd);
1134
1135 cmd->type = JTAG_RUNTEST;
1136
1137 cmd->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
1138 cmd->cmd.runtest->num_cycles = num_cycles;
1139 cmd->cmd.runtest->end_state = state;
1140
1141 return ERROR_OK;
1142 }
1143
1144 void jtag_add_runtest(int num_cycles, tap_state_t state)
1145 {
1146 int retval;
1147
1148 jtag_prelude(state);
1149
1150 /* executed by sw or hw fifo */
1151 retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
1152 if (retval!=ERROR_OK)
1153 jtag_error=retval;
1154 }
1155
1156
1157 int MINIDRIVER(interface_jtag_add_clocks)( int num_cycles )
1158 {
1159 /* allocate memory for a new list member */
1160 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1161
1162 jtag_queue_command(cmd);
1163
1164 cmd->type = JTAG_STABLECLOCKS;
1165
1166 cmd->cmd.stableclocks = cmd_queue_alloc(sizeof(stableclocks_command_t));
1167 cmd->cmd.stableclocks->num_cycles = num_cycles;
1168
1169 return ERROR_OK;
1170 }
1171
1172 void jtag_add_clocks( int num_cycles )
1173 {
1174 int retval;
1175
1176 if( !tap_is_state_stable(cmd_queue_cur_state) )
1177 {
1178 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
1179 tap_state_name(cmd_queue_cur_state) );
1180 jtag_error = ERROR_JTAG_NOT_STABLE_STATE;
1181 return;
1182 }
1183
1184 if( num_cycles > 0 )
1185 {
1186 jtag_prelude1();
1187
1188 retval = interface_jtag_add_clocks(num_cycles);
1189 if (retval != ERROR_OK)
1190 jtag_error=retval;
1191 }
1192 }
1193
1194 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
1195 {
1196 int trst_with_tlr = 0;
1197 int retval;
1198
1199 /* FIX!!! there are *many* different cases here. A better
1200 * approach is needed for legal combinations of transitions...
1201 */
1202 if ((jtag_reset_config & RESET_HAS_SRST)&&
1203 (jtag_reset_config & RESET_HAS_TRST)&&
1204 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
1205 {
1206 if (((req_tlr_or_trst&&!jtag_trst)||
1207 (!req_tlr_or_trst&&jtag_trst))&&
1208 ((req_srst&&!jtag_srst)||
1209 (!req_srst&&jtag_srst)))
1210 {
1211 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
1212 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
1213 }
1214 }
1215
1216 /* Make sure that jtag_reset_config allows the requested reset */
1217 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
1218 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
1219 {
1220 LOG_ERROR("BUG: requested reset would assert trst");
1221 jtag_error=ERROR_FAIL;
1222 return;
1223 }
1224
1225 /* if TRST pulls SRST, we reset with TAP T-L-R */
1226 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
1227 {
1228 trst_with_tlr = 1;
1229 }
1230
1231 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
1232 {
1233 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
1234 jtag_error=ERROR_FAIL;
1235 return;
1236 }
1237
1238 if (req_tlr_or_trst)
1239 {
1240 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
1241 {
1242 jtag_trst = 1;
1243 } else
1244 {
1245 trst_with_tlr = 1;
1246 }
1247 } else
1248 {
1249 jtag_trst = 0;
1250 }
1251
1252 jtag_srst = req_srst;
1253
1254 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
1255 if (retval!=ERROR_OK)
1256 {
1257 jtag_error=retval;
1258 return;
1259 }
1260
1261 if (jtag_srst)
1262 {
1263 LOG_DEBUG("SRST line asserted");
1264 }
1265 else
1266 {
1267 LOG_DEBUG("SRST line released");
1268 if (jtag_nsrst_delay)
1269 jtag_add_sleep(jtag_nsrst_delay * 1000);
1270 }
1271
1272 if (trst_with_tlr)
1273 {
1274 LOG_DEBUG("JTAG reset with RESET instead of TRST");
1275 jtag_add_end_state(TAP_RESET);
1276 jtag_add_tlr();
1277 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1278 return;
1279 }
1280
1281 if (jtag_trst)
1282 {
1283 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1284 * and inform possible listeners about this
1285 */
1286 LOG_DEBUG("TRST line asserted");
1287 cmd_queue_cur_state = TAP_RESET;
1288 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1289 }
1290 else
1291 {
1292 if (jtag_ntrst_delay)
1293 jtag_add_sleep(jtag_ntrst_delay * 1000);
1294 }
1295 }
1296
1297 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1298 {
1299 /* allocate memory for a new list member */
1300 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1301
1302 jtag_queue_command(cmd);
1303
1304 cmd->type = JTAG_RESET;
1305
1306 cmd->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1307 cmd->cmd.reset->trst = req_trst;
1308 cmd->cmd.reset->srst = req_srst;
1309
1310 return ERROR_OK;
1311 }
1312
1313 void jtag_add_end_state(tap_state_t state)
1314 {
1315 cmd_queue_end_state = state;
1316 if ((cmd_queue_end_state == TAP_DRSHIFT)||(cmd_queue_end_state == TAP_IRSHIFT))
1317 {
1318 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
1319 }
1320 }
1321
1322 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1323 {
1324 /* allocate memory for a new list member */
1325 jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1326
1327 jtag_queue_command(cmd);
1328
1329 cmd->type = JTAG_SLEEP;
1330
1331 cmd->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1332 cmd->cmd.sleep->us = us;
1333
1334 return ERROR_OK;
1335 }
1336
1337 void jtag_add_sleep(u32 us)
1338 {
1339 keep_alive(); /* we might be running on a very slow JTAG clk */
1340 int retval=interface_jtag_add_sleep(us);
1341 if (retval!=ERROR_OK)
1342 jtag_error=retval;
1343 return;
1344 }
1345
1346 int jtag_scan_size(const scan_command_t *cmd)
1347 {
1348 int bit_count = 0;
1349 int i;
1350
1351 /* count bits in scan command */
1352 for (i = 0; i < cmd->num_fields; i++)
1353 {
1354 bit_count += cmd->fields[i].num_bits;
1355 }
1356
1357 return bit_count;
1358 }
1359
1360 int jtag_build_buffer(const scan_command_t *cmd, u8 **buffer)
1361 {
1362 int bit_count = 0;
1363 int i;
1364
1365 bit_count = jtag_scan_size(cmd);
1366 *buffer = calloc(1,CEIL(bit_count, 8));
1367
1368 bit_count = 0;
1369
1370 #ifdef _DEBUG_JTAG_IO_
1371 LOG_DEBUG("%s num_fields: %i", cmd->ir_scan ? "IRSCAN" : "DRSCAN", cmd->num_fields);
1372 #endif
1373
1374 for (i = 0; i < cmd->num_fields; i++)
1375 {
1376 if (cmd->fields[i].out_value)
1377 {
1378 #ifdef _DEBUG_JTAG_IO_
1379 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : cmd->fields[i].num_bits, 16);
1380 #endif
1381 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1382 #ifdef _DEBUG_JTAG_IO_
1383 LOG_DEBUG("fields[%i].out_value[%i]: 0x%s", i, cmd->fields[i].num_bits, char_buf);
1384 free(char_buf);
1385 #endif
1386 }
1387 else
1388 {
1389 #ifdef _DEBUG_JTAG_IO_
1390 LOG_DEBUG("fields[%i].out_value[%i]: NULL", i, cmd->fields[i].num_bits);
1391 #endif
1392 }
1393
1394 bit_count += cmd->fields[i].num_bits;
1395 }
1396
1397 #ifdef _DEBUG_JTAG_IO_
1398 //LOG_DEBUG("bit_count totalling: %i", bit_count );
1399 #endif
1400
1401 return bit_count;
1402 }
1403
1404 int jtag_read_buffer(u8 *buffer, const scan_command_t *cmd)
1405 {
1406 int i;
1407 int bit_count = 0;
1408 int retval;
1409
1410 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1411 retval = ERROR_OK;
1412
1413 for (i = 0; i < cmd->num_fields; i++)
1414 {
1415 /* if neither in_value nor in_handler
1416 * are specified we don't have to examine this field
1417 */
1418 if (cmd->fields[i].in_value)
1419 {
1420 int num_bits = cmd->fields[i].num_bits;
1421 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1422
1423 #ifdef _DEBUG_JTAG_IO_
1424 char *char_buf = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
1425 LOG_DEBUG("fields[%i].in_value[%i]: 0x%s", i, num_bits, char_buf);
1426 free(char_buf);
1427 #endif
1428
1429 if (cmd->fields[i].in_value)
1430 {
1431 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1432 }
1433
1434 free(captured);
1435 }
1436 bit_count += cmd->fields[i].num_bits;
1437 }
1438
1439 return retval;
1440 }
1441
1442 static const char *jtag_tap_name(const jtag_tap_t *tap)
1443 {
1444 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
1445 }
1446
1447 int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
1448 {
1449 int retval = ERROR_OK;
1450
1451 int compare_failed = 0;
1452
1453 if (in_check_mask)
1454 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
1455 else
1456 compare_failed = buf_cmp(captured, in_check_value, num_bits);
1457
1458 if (compare_failed){
1459 /* An error handler could have caught the failing check
1460 * only report a problem when there wasn't a handler, or if the handler
1461 * acknowledged the error
1462 */
1463 /*
1464 LOG_WARNING("TAP %s:",
1465 jtag_tap_name(field->tap));
1466 */
1467 if (compare_failed)
1468 {
1469 char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
1470 char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
1471
1472 if (in_check_mask)
1473 {
1474 char *in_check_mask_char;
1475 in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
1476 LOG_WARNING("value captured during scan didn't pass the requested check:");
1477 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
1478 captured_char, in_check_value_char, in_check_mask_char);
1479 free(in_check_mask_char);
1480 }
1481 else
1482 {
1483 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);
1484 }
1485
1486 free(captured_char);
1487 free(in_check_value_char);
1488
1489 retval = ERROR_JTAG_QUEUE_FAILED;
1490 }
1491
1492 }
1493 return retval;
1494 }
1495
1496 void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
1497 {
1498 assert(field->in_value != NULL);
1499
1500 if (value==NULL)
1501 {
1502 /* no checking to do */
1503 return;
1504 }
1505
1506 jtag_execute_queue_noclear();
1507
1508 int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
1509 jtag_set_error(retval);
1510 }
1511
1512
1513
1514 enum scan_type jtag_scan_type(const scan_command_t *cmd)
1515 {
1516 int i;
1517 int type = 0;
1518
1519 for (i = 0; i < cmd->num_fields; i++)
1520 {
1521 if (cmd->fields[i].in_value)
1522 type |= SCAN_IN;
1523 if (cmd->fields[i].out_value)
1524 type |= SCAN_OUT;
1525 }
1526
1527 return type;
1528 }
1529
1530
1531 #ifndef HAVE_JTAG_MINIDRIVER_H
1532 /* add callback to end of queue */
1533 void jtag_add_callback4(jtag_callback_t callback, u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
1534 {
1535 struct jtag_callback_entry *entry=cmd_queue_alloc(sizeof(struct jtag_callback_entry));
1536
1537 entry->next=NULL;
1538 entry->callback=callback;
1539 entry->in=in;
1540 entry->data1=data1;
1541 entry->data2=data2;
1542 entry->data3=data3;
1543
1544 if (jtag_callback_queue_head==NULL)
1545 {
1546 jtag_callback_queue_head=entry;
1547 jtag_callback_queue_tail=entry;
1548 } else
1549 {
1550 jtag_callback_queue_tail->next=entry;
1551 jtag_callback_queue_tail=entry;
1552 }
1553 }
1554
1555
1556 static int jtag_convert_to_callback4(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
1557 {
1558 ((jtag_callback1_t)data1)(in);
1559 return ERROR_OK;
1560 }
1561
1562 void jtag_add_callback(jtag_callback1_t callback, u8 *in)
1563 {
1564 jtag_add_callback4(jtag_convert_to_callback4, in, (jtag_callback_data_t)callback, 0, 0);
1565 }
1566 #endif
1567
1568 #ifndef HAVE_JTAG_MINIDRIVER_H
1569
1570 int interface_jtag_execute_queue(void)
1571 {
1572 int retval;
1573
1574 if (jtag==NULL)
1575 {
1576 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1577 return ERROR_FAIL;
1578 }
1579
1580 retval = jtag->execute_queue();
1581
1582 if (retval == ERROR_OK)
1583 {
1584 struct jtag_callback_entry *entry;
1585 for (entry=jtag_callback_queue_head; entry!=NULL; entry=entry->next)
1586 {
1587 retval=entry->callback(entry->in, entry->data1, entry->data2, entry->data3);
1588 if (retval!=ERROR_OK)
1589 break;
1590 }
1591 }
1592
1593 cmd_queue_free();
1594
1595 jtag_callback_queue_head = NULL;
1596 jtag_callback_queue_tail = NULL;
1597
1598 jtag_command_queue = NULL;
1599 last_command_pointer = &jtag_command_queue;
1600
1601 return retval;
1602 }
1603 #endif
1604
1605 void jtag_execute_queue_noclear(void)
1606 {
1607 /* each flush can take as much as 1-2ms on high bandwidth low latency interfaces.
1608 * E.g. a JTAG over TCP/IP or USB....
1609 */
1610 jtag_flush_queue_count++;
1611
1612 int retval=interface_jtag_execute_queue();
1613 /* we keep the first error */
1614 if ((jtag_error==ERROR_OK)&&(retval!=ERROR_OK))
1615 {
1616 jtag_error=retval;
1617 }
1618 }
1619
1620 int jtag_execute_queue(void)
1621 {
1622 int retval;
1623 jtag_execute_queue_noclear();
1624 retval=jtag_error;
1625 jtag_error=ERROR_OK;
1626 return retval;
1627 }
1628
1629 int jtag_reset_callback(enum jtag_event event, void *priv)
1630 {
1631 jtag_tap_t *tap = priv;
1632
1633 LOG_DEBUG("-");
1634
1635 if (event == JTAG_TRST_ASSERTED)
1636 {
1637 buf_set_ones(tap->cur_instr, tap->ir_length);
1638 tap->bypass = 1;
1639 }
1640
1641 return ERROR_OK;
1642 }
1643
1644 void jtag_sleep(u32 us)
1645 {
1646 alive_sleep(us/1000);
1647 }
1648
1649 /* Try to examine chain layout according to IEEE 1149.1 §12
1650 */
1651 int jtag_examine_chain(void)
1652 {
1653 jtag_tap_t *tap;
1654 scan_field_t field;
1655 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1656 int i;
1657 int bit_count;
1658 int device_count = 0;
1659 u8 zero_check = 0x0;
1660 u8 one_check = 0xff;
1661
1662 field.tap = NULL;
1663 field.num_bits = sizeof(idcode_buffer) * 8;
1664 field.out_value = idcode_buffer;
1665
1666 field.in_value = idcode_buffer;
1667
1668
1669
1670
1671 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1672 {
1673 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1674 }
1675
1676 jtag_add_plain_dr_scan(1, &field, TAP_RESET);
1677 jtag_execute_queue();
1678
1679 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1680 {
1681 zero_check |= idcode_buffer[i];
1682 one_check &= idcode_buffer[i];
1683 }
1684
1685 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1686 if ((zero_check == 0x00) || (one_check == 0xff))
1687 {
1688 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1689 return ERROR_JTAG_INIT_FAILED;
1690 }
1691
1692 /* point at the 1st tap */
1693 tap = jtag_NextEnabledTap(NULL);
1694 if( tap == NULL ){
1695 LOG_ERROR("JTAG: No taps enabled?");
1696 return ERROR_JTAG_INIT_FAILED;
1697 }
1698
1699 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1700 {
1701 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1702 if ((idcode & 1) == 0)
1703 {
1704 /* LSB must not be 0, this indicates a device in bypass */
1705 LOG_WARNING("Tap/Device does not have IDCODE");
1706 idcode=0;
1707
1708 bit_count += 1;
1709 }
1710 else
1711 {
1712 u32 manufacturer;
1713 u32 part;
1714 u32 version;
1715
1716 /* some devices, such as AVR will output all 1's instead of TDI
1717 input value at end of chain. */
1718 if ((idcode == 0x000000FF)||(idcode == 0xFFFFFFFF))
1719 {
1720 int unexpected=0;
1721 /* End of chain (invalid manufacturer ID)
1722 *
1723 * The JTAG examine is the very first thing that happens
1724 *
1725 * A single JTAG device requires only 64 bits to be read back correctly.
1726 *
1727 * The code below adds a check that the rest of the data scanned (640 bits)
1728 * are all as expected. This helps diagnose/catch problems with the JTAG chain
1729 *
1730 * earlier and gives more helpful/explicit error messages.
1731 */
1732 for (bit_count += 32; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;bit_count += 32)
1733 {
1734 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1735 if (unexpected||((idcode != 0x000000FF)&&(idcode != 0xFFFFFFFF)))
1736 {
1737 LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count, idcode);
1738 unexpected = 1;
1739 }
1740 }
1741
1742 break;
1743 }
1744
1745 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1746 manufacturer = EXTRACT_MFG(idcode);
1747 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1748 part = EXTRACT_PART(idcode);
1749 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1750 version = EXTRACT_VER(idcode);
1751
1752 LOG_INFO("JTAG tap: %s tap/device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1753 ((tap != NULL) ? (tap->dotted_name) : "(not-named)"),
1754 idcode, manufacturer, part, version);
1755
1756 bit_count += 32;
1757 }
1758 if (tap)
1759 {
1760 tap->idcode = idcode;
1761
1762 if (tap->expected_ids_cnt > 0) {
1763 /* Loop over the expected identification codes and test for a match */
1764 u8 ii;
1765 for (ii = 0; ii < tap->expected_ids_cnt; ii++) {
1766 if( tap->idcode == tap->expected_ids[ii] ){
1767 break;
1768 }
1769 }
1770
1771 /* If none of the expected ids matched, log an error */
1772 if (ii == tap->expected_ids_cnt) {
1773 LOG_ERROR("JTAG tap: %s got: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1774 tap->dotted_name,
1775 idcode,
1776 EXTRACT_MFG( tap->idcode ),
1777 EXTRACT_PART( tap->idcode ),
1778 EXTRACT_VER( tap->idcode ) );
1779 for (ii = 0; ii < tap->expected_ids_cnt; ii++) {
1780 LOG_ERROR("JTAG tap: %s expected %hhu of %hhu: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1781 tap->dotted_name,
1782 ii + 1,
1783 tap->expected_ids_cnt,
1784 tap->expected_ids[ii],
1785 EXTRACT_MFG( tap->expected_ids[ii] ),
1786 EXTRACT_PART( tap->expected_ids[ii] ),
1787 EXTRACT_VER( tap->expected_ids[ii] ) );
1788 }
1789
1790 return ERROR_JTAG_INIT_FAILED;
1791 } else {
1792 LOG_INFO("JTAG Tap/device matched");
1793 }
1794 } else {
1795 #if 0
1796 LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
1797 tap->idcode);
1798 #endif
1799 }
1800 tap = jtag_NextEnabledTap(tap);
1801 }
1802 device_count++;
1803 }
1804
1805 /* see if number of discovered devices matches configuration */
1806 if (device_count != jtag_NumEnabledTaps())
1807 {
1808 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
1809 device_count, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
1810 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1811 return ERROR_JTAG_INIT_FAILED;
1812 }
1813
1814 return ERROR_OK;
1815 }
1816
1817 int jtag_validate_chain(void)
1818 {
1819 jtag_tap_t *tap;
1820 int total_ir_length = 0;
1821 u8 *ir_test = NULL;
1822 scan_field_t field;
1823 int chain_pos = 0;
1824
1825 tap = NULL;
1826 total_ir_length = 0;
1827 for(;;){
1828 tap = jtag_NextEnabledTap(tap);
1829 if( tap == NULL ){
1830 break;
1831 }
1832 total_ir_length += tap->ir_length;
1833 }
1834
1835 total_ir_length += 2;
1836 ir_test = malloc(CEIL(total_ir_length, 8));
1837 buf_set_ones(ir_test, total_ir_length);
1838
1839 field.tap = NULL;
1840 field.num_bits = total_ir_length;
1841 field.out_value = ir_test;
1842 field.in_value = ir_test;
1843
1844
1845 jtag_add_plain_ir_scan(1, &field, TAP_RESET);
1846 jtag_execute_queue();
1847
1848 tap = NULL;
1849 chain_pos = 0;
1850 int val;
1851 for(;;){
1852 tap = jtag_NextEnabledTap(tap);
1853 if( tap == NULL ){
1854 break;
1855 }
1856
1857 val = buf_get_u32(ir_test, chain_pos, 2);
1858 if (val != 0x1)
1859 {
1860 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1861 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);
1862 free(cbuf);
1863 free(ir_test);
1864 return ERROR_JTAG_INIT_FAILED;
1865 }
1866 chain_pos += tap->ir_length;
1867 }
1868
1869 val = buf_get_u32(ir_test, chain_pos, 2);
1870 if (val != 0x3)
1871 {
1872 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1873 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);
1874 free(cbuf);
1875 free(ir_test);
1876 return ERROR_JTAG_INIT_FAILED;
1877 }
1878
1879 free(ir_test);
1880
1881 return ERROR_OK;
1882 }
1883
1884 enum jtag_tap_cfg_param {
1885 JCFG_EVENT
1886 };
1887
1888 static Jim_Nvp nvp_config_opts[] = {
1889 { .name = "-event", .value = JCFG_EVENT },
1890
1891 { .name = NULL, .value = -1 }
1892 };
1893
1894 static int jtag_tap_configure_cmd( Jim_GetOptInfo *goi, jtag_tap_t * tap)
1895 {
1896 Jim_Nvp *n;
1897 Jim_Obj *o;
1898 int e;
1899
1900 /* parse config or cget options */
1901 while (goi->argc > 0) {
1902 Jim_SetEmptyResult (goi->interp);
1903
1904 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
1905 if (e != JIM_OK) {
1906 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
1907 return e;
1908 }
1909
1910 switch (n->value) {
1911 case JCFG_EVENT:
1912 if (goi->argc == 0) {
1913 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ..." );
1914 return JIM_ERR;
1915 }
1916
1917 e = Jim_GetOpt_Nvp( goi, nvp_jtag_tap_event, &n );
1918 if (e != JIM_OK) {
1919 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
1920 return e;
1921 }
1922
1923 if (goi->isconfigure) {
1924 if (goi->argc != 1) {
1925 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
1926 return JIM_ERR;
1927 }
1928 } else {
1929 if (goi->argc != 0) {
1930 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
1931 return JIM_ERR;
1932 }
1933 }
1934
1935 {
1936 jtag_tap_event_action_t *jteap;
1937
1938 jteap = tap->event_action;
1939 /* replace existing? */
1940 while (jteap) {
1941 if (jteap->event == (enum jtag_tap_event)n->value) {
1942 break;
1943 }
1944 jteap = jteap->next;
1945 }
1946
1947 if (goi->isconfigure) {
1948 if (jteap == NULL) {
1949 /* create new */
1950 jteap = calloc(1, sizeof (*jteap));
1951 }
1952 jteap->event = n->value;
1953 Jim_GetOpt_Obj( goi, &o);
1954 if (jteap->body) {
1955 Jim_DecrRefCount(interp, jteap->body);
1956 }
1957 jteap->body = Jim_DuplicateObj(goi->interp, o);
1958 Jim_IncrRefCount(jteap->body);
1959
1960 /* add to head of event list */
1961 jteap->next = tap->event_action;
1962 tap->event_action = jteap;
1963 Jim_SetEmptyResult(goi->interp);
1964 } else {
1965 /* get */
1966 if (jteap == NULL) {
1967 Jim_SetEmptyResult(goi->interp);
1968 } else {
1969 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
1970 }
1971 }
1972 }
1973 /* loop for more */
1974 break;
1975 }
1976 } /* while (goi->argc) */
1977
1978 return JIM_OK;
1979 }
1980
1981 static int jim_newtap_cmd( Jim_GetOptInfo *goi )
1982 {
1983 jtag_tap_t *pTap;
1984 jtag_tap_t **ppTap;
1985 jim_wide w;
1986 int x;
1987 int e;
1988 int reqbits;
1989 Jim_Nvp *n;
1990 char *cp;
1991 const Jim_Nvp opts[] = {
1992 #define NTAP_OPT_IRLEN 0
1993 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
1994 #define NTAP_OPT_IRMASK 1
1995 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
1996 #define NTAP_OPT_IRCAPTURE 2
1997 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
1998 #define NTAP_OPT_ENABLED 3
1999 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
2000 #define NTAP_OPT_DISABLED 4
2001 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
2002 #define NTAP_OPT_EXPECTED_ID 5
2003 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
2004 { .name = NULL , .value = -1 },
2005 };
2006
2007 pTap = malloc( sizeof(jtag_tap_t) );
2008 memset( pTap, 0, sizeof(*pTap) );
2009 if( !pTap ){
2010 Jim_SetResult_sprintf( goi->interp, "no memory");
2011 return JIM_ERR;
2012 }
2013 /*
2014 * we expect CHIP + TAP + OPTIONS
2015 * */
2016 if( goi->argc < 3 ){
2017 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
2018 return JIM_ERR;
2019 }
2020 Jim_GetOpt_String( goi, &cp, NULL );
2021 pTap->chip = strdup(cp);
2022
2023 Jim_GetOpt_String( goi, &cp, NULL );
2024 pTap->tapname = strdup(cp);
2025
2026 /* name + dot + name + null */
2027 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
2028 cp = malloc( x );
2029 sprintf( cp, "%s.%s", pTap->chip, pTap->tapname );
2030 pTap->dotted_name = cp;
2031
2032 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
2033 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
2034
2035 /* default is enabled */
2036 pTap->enabled = 1;
2037
2038 /* deal with options */
2039 #define NTREQ_IRLEN 1
2040 #define NTREQ_IRCAPTURE 2
2041 #define NTREQ_IRMASK 4
2042
2043 /* clear them as we find them */
2044 reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
2045
2046 while( goi->argc ){
2047 e = Jim_GetOpt_Nvp( goi, opts, &n );
2048 if( e != JIM_OK ){
2049 Jim_GetOpt_NvpUnknown( goi, opts, 0 );
2050 return e;
2051 }
2052 LOG_DEBUG("Processing option: %s", n->name );
2053 switch( n->value ){
2054 case NTAP_OPT_ENABLED:
2055 pTap->enabled = 1;
2056 break;
2057 case NTAP_OPT_DISABLED:
2058 pTap->enabled = 0;
2059 break;
2060 case NTAP_OPT_EXPECTED_ID:
2061 {
2062 u32 *new_expected_ids;
2063
2064 e = Jim_GetOpt_Wide( goi, &w );
2065 if( e != JIM_OK) {
2066 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
2067 return e;
2068 }
2069
2070 new_expected_ids = malloc(sizeof(u32) * (pTap->expected_ids_cnt + 1));
2071 if (new_expected_ids == NULL) {
2072 Jim_SetResult_sprintf( goi->interp, "no memory");
2073 return JIM_ERR;
2074 }
2075
2076 memcpy(new_expected_ids, pTap->expected_ids, sizeof(u32) * pTap->expected_ids_cnt);
2077
2078 new_expected_ids[pTap->expected_ids_cnt] = w;
2079
2080 free(pTap->expected_ids);
2081 pTap->expected_ids = new_expected_ids;
2082 pTap->expected_ids_cnt++;
2083 break;
2084 }
2085 case NTAP_OPT_IRLEN:
2086 case NTAP_OPT_IRMASK:
2087 case NTAP_OPT_IRCAPTURE:
2088 e = Jim_GetOpt_Wide( goi, &w );
2089 if( e != JIM_OK ){
2090 Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
2091 return e;
2092 }
2093 if( (w < 0) || (w > 0xffff) ){
2094 /* wacky value */
2095 Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
2096 n->name, (int)(w), (int)(w));
2097 return JIM_ERR;
2098 }
2099 switch(n->value){
2100 case NTAP_OPT_IRLEN:
2101 pTap->ir_length = w;
2102 reqbits &= (~(NTREQ_IRLEN));
2103 break;
2104 case NTAP_OPT_IRMASK:
2105 pTap->ir_capture_mask = w;
2106 reqbits &= (~(NTREQ_IRMASK));
2107 break;
2108 case NTAP_OPT_IRCAPTURE:
2109 pTap->ir_capture_value = w;
2110 reqbits &= (~(NTREQ_IRCAPTURE));
2111 break;
2112 }
2113 } /* switch(n->value) */
2114 } /* while( goi->argc ) */
2115
2116 /* Did we get all the options? */
2117 if( reqbits ){
2118 // no
2119 Jim_SetResult_sprintf( goi->interp,
2120 "newtap: %s missing required parameters",
2121 pTap->dotted_name);
2122 /* TODO: Tell user what is missing :-( */
2123 /* no memory leaks pelase */
2124 free(((void *)(pTap->expected_ids)));
2125 free(((void *)(pTap->chip)));
2126 free(((void *)(pTap->tapname)));
2127 free(((void *)(pTap->dotted_name)));
2128 free(((void *)(pTap)));
2129 return JIM_ERR;
2130 }
2131
2132 pTap->expected = malloc( pTap->ir_length );
2133 pTap->expected_mask = malloc( pTap->ir_length );
2134 pTap->cur_instr = malloc( pTap->ir_length );
2135
2136 buf_set_u32( pTap->expected,
2137 0,
2138 pTap->ir_length,
2139 pTap->ir_capture_value );
2140 buf_set_u32( pTap->expected_mask,
2141 0,
2142 pTap->ir_length,
2143 pTap->ir_capture_mask );
2144 buf_set_ones( pTap->cur_instr,
2145 pTap->ir_length );
2146
2147 pTap->bypass = 1;
2148
2149 jtag_register_event_callback(jtag_reset_callback, pTap );
2150
2151 ppTap = &(jtag_all_taps);
2152 while( (*ppTap) != NULL ){
2153 ppTap = &((*ppTap)->next_tap);
2154 }
2155 *ppTap = pTap;
2156 {
2157 static int n_taps = 0;
2158 pTap->abs_chain_position = n_taps++;
2159 }
2160 LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
2161 (*ppTap)->dotted_name,
2162 (*ppTap)->abs_chain_position,
2163 (*ppTap)->ir_length,
2164 (*ppTap)->ir_capture_value,
2165 (*ppTap)->ir_capture_mask );
2166
2167 return ERROR_OK;
2168 }
2169
2170 static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
2171 {
2172 Jim_GetOptInfo goi;
2173 int e;
2174 Jim_Nvp *n;
2175 Jim_Obj *o;
2176 struct command_context_s *context;
2177
2178 enum {
2179 JTAG_CMD_INTERFACE,
2180 JTAG_CMD_INIT_RESET,
2181 JTAG_CMD_NEWTAP,
2182 JTAG_CMD_TAPENABLE,
2183 JTAG_CMD_TAPDISABLE,
2184 JTAG_CMD_TAPISENABLED,
2185 JTAG_CMD_CONFIGURE,
2186 JTAG_CMD_CGET
2187 };
2188
2189 const Jim_Nvp jtag_cmds[] = {
2190 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
2191 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
2192 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
2193 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
2194 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
2195 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
2196 { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
2197 { .name = "cget" , .value = JTAG_CMD_CGET },
2198
2199 { .name = NULL, .value = -1 },
2200 };
2201
2202 context = Jim_GetAssocData(interp, "context");
2203 /* go past the command */
2204 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
2205
2206 e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
2207 if( e != JIM_OK ){
2208 Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
2209 return e;
2210 }
2211 Jim_SetEmptyResult( goi.interp );
2212 switch( n->value ){
2213 case JTAG_CMD_INTERFACE:
2214 /* return the name of the interface */
2215 /* TCL code might need to know the exact type... */
2216 /* FUTURE: we allow this as a means to "set" the interface. */
2217 if( goi.argc != 0 ){
2218 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
2219 return JIM_ERR;
2220 }
2221 Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
2222 return JIM_OK;
2223 case JTAG_CMD_INIT_RESET:
2224 if( goi.argc != 0 ){
2225 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
2226 return JIM_ERR;
2227 }
2228 e = jtag_init_reset(context);
2229 if( e != ERROR_OK ){
2230 Jim_SetResult_sprintf( goi.interp, "error: %d", e);
2231 return JIM_ERR;
2232 }
2233 return JIM_OK;
2234 case JTAG_CMD_NEWTAP:
2235 return jim_newtap_cmd( &goi );
2236 break;
2237 case JTAG_CMD_TAPISENABLED:
2238 case JTAG_CMD_TAPENABLE:
2239 case JTAG_CMD_TAPDISABLE:
2240 if( goi.argc != 1 ){
2241 Jim_SetResultString( goi.interp, "Too many parameters",-1 );
2242 return JIM_ERR;
2243 }
2244
2245 {
2246 jtag_tap_t *t;
2247 t = jtag_TapByJimObj( goi.interp, goi.argv[0] );
2248 if( t == NULL ){
2249 return JIM_ERR;
2250 }
2251 switch( n->value ){
2252 case JTAG_CMD_TAPISENABLED:
2253 e = t->enabled;
2254 break;
2255 case JTAG_CMD_TAPENABLE:
2256 jtag_tap_handle_event( t, JTAG_TAP_EVENT_ENABLE);
2257 e = 1;
2258 t->enabled = e;
2259 break;
2260 case JTAG_CMD_TAPDISABLE:
2261 jtag_tap_handle_event( t, JTAG_TAP_EVENT_DISABLE);
2262 e = 0;
2263 t->enabled = e;
2264 break;
2265 }
2266 Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
2267 return JIM_OK;
2268 }
2269 break;
2270
2271 case JTAG_CMD_CGET:
2272 if( goi.argc < 2 ){
2273 Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ...");
2274 return JIM_ERR;
2275 }
2276
2277 {
2278 jtag_tap_t *t;
2279
2280 Jim_GetOpt_Obj(&goi, &o);
2281 t = jtag_TapByJimObj( goi.interp, o );
2282 if( t == NULL ){
2283 return JIM_ERR;
2284 }
2285
2286 goi.isconfigure = 0;
2287 return jtag_tap_configure_cmd( &goi, t);
2288 }
2289 break;
2290
2291 case JTAG_CMD_CONFIGURE:
2292 if( goi.argc < 3 ){
2293 Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ?VALUE? ...");
2294 return JIM_ERR;
2295 }
2296
2297 {
2298 jtag_tap_t *t;
2299
2300 Jim_GetOpt_Obj(&goi, &o);
2301 t = jtag_TapByJimObj( goi.interp, o );
2302 if( t == NULL ){
2303 return JIM_ERR;
2304 }
2305
2306 goi.isconfigure = 1;
2307 return jtag_tap_configure_cmd( &goi, t);
2308 }
2309 }
2310
2311 return JIM_ERR;
2312 }
2313
2314 int jtag_register_commands(struct command_context_s *cmd_ctx)
2315 {
2316 register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
2317
2318 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
2319 COMMAND_CONFIG, "try to configure interface");
2320 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
2321 COMMAND_ANY, "set jtag speed (if supported)");
2322 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
2323 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
2324 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
2325 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
2326 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
2327 COMMAND_ANY,
2328 "[none/trst_only/srst_only/trst_and_srst] [srst_pulls_trst/trst_pulls_srst] [combined/separate] [trst_push_pull/trst_open_drain] [srst_push_pull/srst_open_drain]");
2329 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
2330 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
2331 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
2332 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
2333
2334 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
2335 COMMAND_EXEC, "print current scan chain configuration");
2336
2337 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
2338 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
2339 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
2340 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
2341 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
2342 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
2343 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
2344 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
2345 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
2346 register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
2347
2348 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
2349 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
2350 register_command(cmd_ctx, NULL, "verify_jtag", handle_verify_jtag_command,
2351 COMMAND_ANY, "verify value capture <enable|disable>");
2352 register_command(cmd_ctx, NULL, "tms_sequence", handle_tms_sequence_command,
2353 COMMAND_ANY, "choose short(default) or long tms_sequence <short|long>");
2354 return ERROR_OK;
2355 }
2356
2357 int jtag_interface_init(struct command_context_s *cmd_ctx)
2358 {
2359 if (jtag)
2360 return ERROR_OK;
2361
2362 if (!jtag_interface)
2363 {
2364 /* nothing was previously specified by "interface" command */
2365 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
2366 return ERROR_JTAG_INVALID_INTERFACE;
2367 }
2368 if(hasKHz)
2369 {
2370 jtag_interface->khz(speed_khz, &jtag_speed);
2371 hasKHz = 0;
2372 }
2373
2374 if (jtag_interface->init() != ERROR_OK)
2375 return ERROR_JTAG_INIT_FAILED;
2376
2377 jtag = jtag_interface;
2378 return ERROR_OK;
2379 }
2380
2381 static int jtag_init_inner(struct command_context_s *cmd_ctx)
2382 {
2383 jtag_tap_t *tap;
2384 int retval;
2385
2386 LOG_DEBUG("Init JTAG chain");
2387
2388 tap = jtag_NextEnabledTap(NULL);
2389 if( tap == NULL ){
2390 LOG_ERROR("There are no enabled taps?");
2391 return ERROR_JTAG_INIT_FAILED;
2392 }
2393
2394 jtag_add_tlr();
2395 if ((retval=jtag_execute_queue())!=ERROR_OK)
2396 return retval;
2397
2398 /* examine chain first, as this could discover the real chain layout */
2399 if (jtag_examine_chain() != ERROR_OK)
2400 {
2401 LOG_ERROR("trying to validate configured JTAG chain anyway...");
2402 }
2403
2404 if (jtag_validate_chain() != ERROR_OK)
2405 {
2406 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
2407 }
2408
2409 return ERROR_OK;
2410 }
2411
2412 int jtag_init_reset(struct command_context_s *cmd_ctx)
2413 {
2414 int retval;
2415
2416 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
2417 return retval;
2418
2419 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
2420
2421 /* Reset can happen after a power cycle.
2422 *
2423 * Ideally we would only assert TRST or run RESET before the target reset.
2424 *
2425 * However w/srst_pulls_trst, trst is asserted together with the target
2426 * reset whether we want it or not.
2427 *
2428 * NB! Some targets have JTAG circuitry disabled until a
2429 * trst & srst has been asserted.
2430 *
2431 * NB! here we assume nsrst/ntrst delay are sufficient!
2432 *
2433 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
2434 *
2435 */
2436 jtag_add_reset(1, 0); /* RESET or TRST */
2437 if (jtag_reset_config & RESET_HAS_SRST)
2438 {
2439 jtag_add_reset(1, 1);
2440 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
2441 jtag_add_reset(0, 1);
2442 }
2443 jtag_add_reset(0, 0);
2444 if ((retval = jtag_execute_queue()) != ERROR_OK)
2445 return retval;
2446
2447 /* Check that we can communication on the JTAG chain + eventually we want to
2448 * be able to perform enumeration only after OpenOCD has started
2449 * telnet and GDB server
2450 *
2451 * That would allow users to more easily perform any magic they need to before
2452 * reset happens.
2453 */
2454 return jtag_init_inner(cmd_ctx);
2455 }
2456
2457 int jtag_init(struct command_context_s *cmd_ctx)
2458 {
2459 int retval;
2460 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
2461 return retval;
2462 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
2463 {
2464 return ERROR_OK;
2465 }
2466 return jtag_init_reset(cmd_ctx);
2467 }
2468
2469 static int default_khz(int khz, int *jtag_speed)
2470 {
2471 LOG_ERROR("Translation from khz to jtag_speed not implemented");
2472 return ERROR_FAIL;
2473 }
2474
2475 static int default_speed_div(int speed, int *khz)
2476 {
2477 LOG_ERROR("Translation from jtag_speed to khz not implemented");
2478 return ERROR_FAIL;
2479 }
2480
2481 static int default_power_dropout(int *dropout)
2482 {
2483 *dropout=0; /* by default we can't detect power dropout */
2484 return ERROR_OK;
2485 }
2486
2487 static int default_srst_asserted(int *srst_asserted)
2488 {
2489 *srst_asserted=0; /* by default we can't detect srst asserted */
2490 return ERROR_OK;
2491 }
2492
2493 static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2494 {
2495 int i;
2496 int retval;
2497
2498 /* check whether the interface is already configured */
2499 if (jtag_interface)
2500 {
2501 LOG_WARNING("Interface already configured, ignoring");
2502 return ERROR_OK;
2503 }
2504
2505 /* interface name is a mandatory argument */
2506 if (argc < 1 || args[0][0] == '\0')
2507 {
2508 return ERROR_COMMAND_SYNTAX_ERROR;
2509 }
2510
2511 for (i=0; jtag_interfaces[i]; i++)
2512 {
2513 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
2514 {
2515 if ((retval = jtag_interfaces[i]->register_commands(cmd_ctx)) != ERROR_OK)
2516 {
2517 return retval;
2518 }
2519
2520 jtag_interface = jtag_interfaces[i];
2521
2522 if (jtag_interface->khz == NULL)
2523 {
2524 jtag_interface->khz = default_khz;
2525 }
2526 if (jtag_interface->speed_div == NULL)
2527 {
2528 jtag_interface->speed_div = default_speed_div;
2529 }
2530 if (jtag_interface->power_dropout == NULL)
2531 {
2532 jtag_interface->power_dropout = default_power_dropout;
2533 }
2534 if (jtag_interface->srst_asserted == NULL)
2535 {
2536 jtag_interface->srst_asserted = default_srst_asserted;
2537 }
2538
2539 return ERROR_OK;
2540 }
2541 }
2542
2543 /* no valid interface was found (i.e. the configuration option,
2544 * didn't match one of the compiled-in interfaces
2545 */
2546 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
2547 LOG_ERROR("compiled-in jtag interfaces:");
2548 for (i = 0; jtag_interfaces[i]; i++)
2549 {
2550 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
2551 }
2552
2553 return ERROR_JTAG_INVALID_INTERFACE;
2554 }
2555
2556 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2557 {
2558 int e;
2559 char buf[1024];
2560 Jim_Obj *newargs[ 10 ];
2561 /*
2562 * CONVERT SYNTAX
2563 * argv[-1] = command
2564 * argv[ 0] = ir length
2565 * argv[ 1] = ir capture
2566 * argv[ 2] = ir mask
2567 * argv[ 3] = not actually used by anything but in the docs
2568 */
2569
2570 if( argc < 4 ){
2571 command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
2572 return ERROR_OK;
2573 }
2574 command_print( cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
2575 command_print( cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
2576 args[0],
2577 args[1],
2578 args[2] );
2579 command_print( cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
2580 command_print( cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
2581 command_print( cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
2582 command_print( cmd_ctx, "And then refer to the taps by the dotted name.");
2583
2584 newargs[0] = Jim_NewStringObj( interp, "jtag", -1 );
2585 newargs[1] = Jim_NewStringObj( interp, "newtap", -1 );
2586 sprintf( buf, "chip%d", jtag_NumTotalTaps() );
2587 newargs[2] = Jim_NewStringObj( interp, buf, -1 );
2588 sprintf( buf, "tap%d", jtag_NumTotalTaps() );
2589 newargs[3] = Jim_NewStringObj( interp, buf, -1 );
2590 newargs[4] = Jim_NewStringObj( interp, "-irlen", -1 );
2591 newargs[5] = Jim_NewStringObj( interp, args[0], -1 );
2592 newargs[6] = Jim_NewStringObj( interp, "-ircapture", -1 );
2593 newargs[7] = Jim_NewStringObj( interp, args[1], -1 );
2594 newargs[8] = Jim_NewStringObj( interp, "-irmask", -1 );
2595 newargs[9] = Jim_NewStringObj( interp, args[2], -1 );
2596
2597 command_print( cmd_ctx, "NEW COMMAND:");
2598 sprintf( buf, "%s %s %s %s %s %s %s %s %s %s",
2599 Jim_GetString( newargs[0], NULL ),
2600 Jim_GetString( newargs[1], NULL ),
2601 Jim_GetString( newargs[2], NULL ),
2602 Jim_GetString( newargs[3], NULL ),
2603 Jim_GetString( newargs[4], NULL ),
2604 Jim_GetString( newargs[5], NULL ),
2605 Jim_GetString( newargs[6], NULL ),
2606 Jim_GetString( newargs[7], NULL ),
2607 Jim_GetString( newargs[8], NULL ),
2608 Jim_GetString( newargs[9], NULL ) );
2609
2610 e = jim_jtag_command( interp, 10, newargs );
2611 if( e != JIM_OK ){
2612 command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
2613 }
2614 return e;
2615 }
2616
2617 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2618 {
2619 jtag_tap_t *tap;
2620
2621 tap = jtag_all_taps;
2622 command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
2623 command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
2624
2625 while( tap ){
2626 u32 expected, expected_mask, cur_instr, ii;
2627 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
2628 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
2629 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
2630
2631 command_print(cmd_ctx,
2632 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
2633 tap->abs_chain_position,
2634 tap->dotted_name,
2635 tap->enabled ? 'Y' : 'n',
2636 tap->idcode,
2637 (tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
2638 tap->ir_length,
2639 expected,
2640 expected_mask,
2641 cur_instr);
2642
2643 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
2644 command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
2645 tap->expected_ids[ii]);
2646 }
2647
2648 tap = tap->next_tap;
2649 }
2650
2651 return ERROR_OK;
2652 }
2653
2654 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2655 {
2656 if (argc < 1)
2657 return ERROR_COMMAND_SYNTAX_ERROR;
2658
2659 if (argc >= 1)
2660 {
2661 if (strcmp(args[0], "none") == 0)
2662 jtag_reset_config = RESET_NONE;
2663 else if (strcmp(args[0], "trst_only") == 0)
2664 jtag_reset_config = RESET_HAS_TRST;
2665 else if (strcmp(args[0], "srst_only") == 0)
2666 jtag_reset_config = RESET_HAS_SRST;
2667 else if (strcmp(args[0], "trst_and_srst") == 0)
2668 jtag_reset_config = RESET_TRST_AND_SRST;
2669 else
2670 {
2671 LOG_ERROR("(1) invalid reset_config argument (%s), defaulting to none", args[0]);
2672 jtag_reset_config = RESET_NONE;
2673 return ERROR_INVALID_ARGUMENTS;
2674 }
2675 }
2676
2677 if (argc >= 2)
2678 {
2679 if (strcmp(args[1], "separate") == 0)
2680 {
2681 /* seperate reset lines - default */
2682 } else
2683 {
2684 if (strcmp(args[1], "srst_pulls_trst") == 0)
2685 jtag_reset_config |= RESET_SRST_PULLS_TRST;
2686 else if (strcmp(args[1], "trst_pulls_srst") == 0)
2687 jtag_reset_config |= RESET_TRST_PULLS_SRST;
2688 else if (strcmp(args[1], "combined") == 0)
2689 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
2690 else
2691 {
2692 LOG_ERROR("(2) invalid reset_config argument (%s), defaulting to none", args[1]);
2693 jtag_reset_config = RESET_NONE;
2694 return ERROR_INVALID_ARGUMENTS;
2695 }
2696 }
2697 }
2698
2699 if (argc >= 3)
2700 {
2701 if (strcmp(args[2], "trst_open_drain") == 0)
2702 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
2703 else if (strcmp(args[2], "trst_push_pull") == 0)
2704 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
2705 else
2706 {
2707 LOG_ERROR("(3) invalid reset_config argument (%s) defaulting to none", args[2] );
2708 jtag_reset_config = RESET_NONE;
2709 return ERROR_INVALID_ARGUMENTS;
2710 }
2711 }
2712
2713 if (argc >= 4)
2714 {
2715 if (strcmp(args[3], "srst_push_pull") == 0)
2716 jtag_reset_config |= RESET_SRST_PUSH_PULL;
2717 else if (strcmp(args[3], "srst_open_drain") == 0)
2718 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
2719 else
2720 {
2721 LOG_ERROR("(4) invalid reset_config argument (%s), defaulting to none", args[3]);
2722 jtag_reset_config = RESET_NONE;
2723 return ERROR_INVALID_ARGUMENTS;
2724 }
2725 }
2726
2727 return ERROR_OK;
2728 }
2729
2730 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2731 {
2732 if (argc < 1)
2733 {
2734 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
2735 exit(-1);
2736 }
2737 else
2738 {
2739 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
2740 }
2741
2742 return ERROR_OK;
2743 }
2744
2745 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2746 {
2747 if (argc < 1)
2748 {
2749 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
2750 exit(-1);
2751 }
2752 else
2753 {
2754 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
2755 }
2756
2757 return ERROR_OK;
2758 }
2759
2760 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2761 {
2762 int retval=ERROR_OK;
2763
2764 if (argc == 1)
2765 {
2766 LOG_DEBUG("handle jtag speed");
2767
2768 int cur_speed = 0;
2769 cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
2770
2771 /* this command can be called during CONFIG,
2772 * in which case jtag isn't initialized */
2773 if (jtag)
2774 {
2775 retval=jtag->speed(cur_speed);
2776 }
2777 } else if (argc == 0)
2778 {
2779 } else
2780 {
2781 return ERROR_COMMAND_SYNTAX_ERROR;
2782 }
2783 command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
2784
2785 return retval;
2786 }
2787
2788 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2789 {
2790 int retval=ERROR_OK;
2791 LOG_DEBUG("handle jtag khz");
2792
2793 if(argc == 1)
2794 {
2795 speed_khz = strtoul(args[0], NULL, 0);
2796 if (jtag != NULL)
2797 {
2798 int cur_speed = 0;
2799 LOG_DEBUG("have interface set up");
2800 int speed_div1;
2801 if ((retval=jtag->khz(speed_khz, &speed_div1))!=ERROR_OK)
2802 {
2803 speed_khz = 0;
2804 return retval;
2805 }
2806
2807 cur_speed = jtag_speed = speed_div1;
2808
2809 retval=jtag->speed(cur_speed);
2810 } else
2811 {
2812 hasKHz = 1;
2813 }
2814 } else if (argc==0)
2815 {
2816 } else
2817 {
2818 return ERROR_COMMAND_SYNTAX_ERROR;
2819 }
2820
2821 if (jtag!=NULL)
2822 {
2823 if ((retval=jtag->speed_div(jtag_speed, &speed_khz))!=ERROR_OK)
2824 return retval;
2825 }
2826
2827 if (speed_khz==0)
2828 {
2829 command_print(cmd_ctx, "RCLK - adaptive");
2830 } else
2831 {
2832 command_print(cmd_ctx, "%d kHz", speed_khz);
2833 }
2834 return retval;
2835
2836 }
2837
2838 static int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2839 {
2840 tap_state_t state;
2841
2842 if (argc < 1)
2843 {
2844 return ERROR_COMMAND_SYNTAX_ERROR;
2845 }
2846 else
2847 {
2848 state = tap_state_by_name( args[0] );
2849 if( state < 0 ){
2850 command_print( cmd_ctx, "Invalid state name: %s\n", args[0] );
2851 return ERROR_COMMAND_SYNTAX_ERROR;
2852 }
2853 jtag_add_end_state(state);
2854 jtag_execute_queue();
2855 }
2856 command_print(cmd_ctx, "current endstate: %s", tap_state_name(cmd_queue_end_state));
2857
2858 return ERROR_OK;
2859 }
2860
2861 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2862 {
2863 int trst = -1;
2864 int srst = -1;
2865
2866 if (argc < 2)
2867 {
2868 return ERROR_COMMAND_SYNTAX_ERROR;
2869 }
2870
2871 if (args[0][0] == '1')
2872 trst = 1;
2873 else if (args[0][0] == '0')
2874 trst = 0;
2875 else
2876 {
2877 return ERROR_COMMAND_SYNTAX_ERROR;
2878 }
2879
2880 if (args[1][0] == '1')
2881 srst = 1;
2882 else if (args[1][0] == '0')
2883 srst = 0;
2884 else
2885 {
2886 return ERROR_COMMAND_SYNTAX_ERROR;
2887 }
2888
2889 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
2890 return ERROR_JTAG_INIT_FAILED;
2891
2892 jtag_add_reset(trst, srst);
2893 jtag_execute_queue();
2894
2895 return ERROR_OK;
2896 }
2897
2898 static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2899 {
2900 if (argc < 1)
2901 {
2902 return ERROR_COMMAND_SYNTAX_ERROR;
2903 }
2904
2905 jtag_add_runtest(strtol(args[0], NULL, 0), TAP_INVALID);
2906 jtag_execute_queue();
2907
2908 return ERROR_OK;
2909
2910 }
2911
2912 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2913 {
2914 int i;
2915 scan_field_t *fields;
2916 jtag_tap_t *tap;
2917 tap_state_t endstate;
2918
2919 if ((argc < 2) || (argc % 2))
2920 {
2921 return ERROR_COMMAND_SYNTAX_ERROR;
2922 }
2923
2924 /* optional "-endstate" */
2925 /* "statename" */
2926 /* at the end of the arguments. */
2927 /* assume none. */
2928 endstate = cmd_queue_end_state;
2929 if( argc >= 4 ){
2930 /* have at least one pair of numbers. */
2931 /* is last pair the magic text? */
2932 if( 0 == strcmp( "-endstate", args[ argc - 2 ] ) ){
2933 const char *cpA;
2934 const char *cpS;
2935 cpA = args[ argc-1 ];
2936 for( endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++ ){
2937 cpS = tap_state_name( endstate );
2938 if( 0 == strcmp( cpA, cpS ) ){
2939 break;
2940 }
2941 }
2942 if( endstate >= TAP_NUM_STATES ){
2943 return ERROR_COMMAND_SYNTAX_ERROR;
2944 } else {
2945 /* found - remove the last 2 args */
2946 argc -= 2;
2947 }
2948 }
2949 }
2950
2951 int num_fields = argc / 2;
2952
2953 fields = malloc(sizeof(scan_field_t) * num_fields);
2954
2955 for (i = 0; i < num_fields; i++)
2956 {
2957 tap = jtag_TapByString( args[i*2] );
2958 if (tap==NULL)
2959 {
2960 command_print( cmd_ctx, "Tap: %s unknown", args[i*2] );
2961 return ERROR_FAIL;
2962 }
2963 int field_size = tap->ir_length;
2964 fields[i].tap = tap;
2965 fields[i].num_bits = field_size;
2966 fields[i].out_value = malloc(CEIL(field_size, 8));
2967 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
2968 fields[i].in_value = NULL;
2969 }
2970
2971 /* did we have an endstate? */
2972 jtag_add_ir_scan(num_fields, fields, endstate);
2973
2974 int retval=jtag_execute_queue();
2975
2976 for (i = 0; i < num_fields; i++)
2977 free(fields[i].out_value);
2978
2979 free (fields);
2980
2981 return retval;
2982 }
2983
2984 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2985 {
2986 int retval;
2987 scan_field_t *fields;
2988 int num_fields;
2989 int field_count = 0;
2990 int i, e;
2991 jtag_tap_t *tap;
2992 tap_state_t endstate;
2993
2994 /* args[1] = device
2995 * args[2] = num_bits
2996 * args[3] = hex string
2997 * ... repeat num bits and hex string ...
2998 *
2999 * .. optionally:
3000 * args[N-2] = "-endstate"
3001 * args[N-1] = statename
3002 */
3003 if ((argc < 4) || ((argc % 2)!=0))
3004 {
3005 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
3006 return JIM_ERR;
3007 }
3008
3009 /* assume no endstate */
3010 endstate = cmd_queue_end_state;
3011 /* validate arguments as numbers */
3012 e = JIM_OK;
3013 for (i = 2; i < argc; i+=2)
3014 {
3015 long bits;
3016 const char *cp;
3017
3018 e = Jim_GetLong(interp, args[i], &bits);
3019 /* If valid - try next arg */
3020 if( e == JIM_OK ){
3021 continue;
3022 }
3023
3024 /* Not valid.. are we at the end? */
3025 if ( ((i+2) != argc) ){
3026 /* nope, then error */
3027 return e;
3028 }
3029
3030 /* it could be: "-endstate FOO" */
3031
3032 /* get arg as a string. */
3033 cp = Jim_GetString( args[i], NULL );
3034 /* is it the magic? */
3035 if( 0 == strcmp( "-endstate", cp ) ){
3036 /* is the statename valid? */
3037 cp = Jim_GetString( args[i+1], NULL );
3038
3039 /* see if it is a valid state name */
3040 endstate = tap_state_by_name(cp);
3041 if( endstate < 0 ){
3042 /* update the error message */
3043 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp );
3044 } else {
3045 /* valid - so clear the error */
3046 e = JIM_OK;
3047 /* and remove the last 2 args */
3048 argc -= 2;
3049 }
3050 }
3051
3052 /* Still an error? */
3053 if( e != JIM_OK ){
3054 return e; /* too bad */
3055 }
3056 } /* validate args */
3057
3058 tap = jtag_TapByJimObj( interp, args[1] );
3059 if( tap == NULL ){
3060 return JIM_ERR;
3061 }
3062
3063 num_fields=(argc-2)/2;
3064 fields = malloc(sizeof(scan_field_t) * num_fields);
3065 for (i = 2; i < argc; i+=2)
3066 {
3067 long bits;
3068 int len;
3069 const char *str;
3070
3071 Jim_GetLong(interp, args[i], &bits);
3072 str = Jim_GetString(args[i+1], &len);
3073
3074 fields[field_count].tap = tap;
3075 fields[field_count].num_bits = bits;
3076 fields[field_count].out_value = malloc(CEIL(bits, 8));
3077 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
3078 fields[field_count].in_value = fields[field_count].out_value;
3079 field_count++;
3080 }
3081
3082 jtag_add_dr_scan(num_fields, fields, endstate);
3083
3084 retval = jtag_execute_queue();
3085 if (retval != ERROR_OK)
3086 {
3087 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
3088 return JIM_ERR;
3089 }
3090
3091 field_count=0;
3092 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
3093 for (i = 2; i < argc; i+=2)
3094 {
3095 long bits;
3096 char *str;
3097
3098 Jim_GetLong(interp, args[i], &bits);
3099 str = buf_to_str(fields[field_count].in_value, bits, 16);
3100 free(fields[field_count].out_value);
3101
3102 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
3103 free(str);
3104 field_count++;
3105 }
3106
3107 Jim_SetResult(interp, list);
3108
3109 free(fields);
3110
3111 return JIM_OK;
3112 }
3113
3114
3115 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
3116 {
3117 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_flush_queue_count));
3118
3119 return JIM_OK;
3120 }
3121
3122
3123 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3124 {
3125 if (argc == 1)
3126 {
3127 if (strcmp(args[0], "enable") == 0)
3128 {
3129 jtag_verify_capture_ir = 1;
3130 }
3131 else if (strcmp(args[0], "disable") == 0)
3132 {
3133 jtag_verify_capture_ir = 0;
3134 } else
3135 {
3136 return ERROR_COMMAND_SYNTAX_ERROR;
3137 }
3138 } else if (argc != 0)
3139 {
3140 return ERROR_COMMAND_SYNTAX_ERROR;
3141 }
3142
3143 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
3144
3145 return ERROR_OK;
3146 }
3147
3148 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3149 {
3150 if (argc == 1)
3151 {
3152 if (strcmp(args[0], "enable") == 0)
3153 {
3154 jtag_verify = 1;
3155 }
3156 else if (strcmp(args[0], "disable") == 0)
3157 {
3158 jtag_verify = 0;
3159 } else
3160 {
3161 return ERROR_COMMAND_SYNTAX_ERROR;
3162 }
3163 } else if (argc != 0)
3164 {
3165 return ERROR_COMMAND_SYNTAX_ERROR;
3166 }
3167
3168 command_print(cmd_ctx, "verify jtag capture is %s", (jtag_verify) ? "enabled": "disabled");
3169
3170 return ERROR_OK;
3171 }
3172
3173
3174 int jtag_power_dropout(int *dropout)
3175 {
3176 return jtag->power_dropout(dropout);
3177 }
3178
3179 int jtag_srst_asserted(int *srst_asserted)
3180 {
3181 return jtag->srst_asserted(srst_asserted);
3182 }
3183
3184 void jtag_tap_handle_event( jtag_tap_t * tap, enum jtag_tap_event e)
3185 {
3186 jtag_tap_event_action_t * jteap;
3187 int done;
3188
3189 jteap = tap->event_action;
3190
3191 done = 0;
3192 while (jteap) {
3193 if (jteap->event == e) {
3194 done = 1;
3195 LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
3196 tap->dotted_name,
3197 e,
3198 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
3199 Jim_GetString(jteap->body, NULL) );
3200 if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
3201 Jim_PrintErrorMessage(interp);
3202 }
3203 }
3204
3205 jteap = jteap->next;
3206 }
3207
3208 if (!done) {
3209 LOG_DEBUG( "event %d %s - no action",
3210 e,
3211 Jim_Nvp_value2name_simple( nvp_jtag_tap_event, e)->name);
3212 }
3213 }
3214
3215 /*-----<Cable Helper API>---------------------------------------*/
3216
3217 /* these Cable Helper API functions are all documented in the jtag.h header file,
3218 using a Doxygen format. And since Doxygen's configuration file "Doxyfile",
3219 is setup to prefer its docs in the header file, no documentation is here, for
3220 if it were, it would have to be doubly maintained.
3221 */
3222
3223 /**
3224 * @see tap_set_state() and tap_get_state() accessors.
3225 * Actual name is not important since accessors hide it.
3226 */
3227 static tap_state_t state_follower = TAP_RESET;
3228
3229 void tap_set_state_impl( tap_state_t new_state )
3230 {
3231 /* this is the state we think the TAPs are in now, was cur_state */
3232 state_follower = new_state;
3233 }
3234
3235 tap_state_t tap_get_state()
3236 {
3237 return state_follower;
3238 }
3239
3240 /**
3241 * @see tap_set_end_state() and tap_get_end_state() accessors.
3242 * Actual name is not important because accessors hide it.
3243 */
3244 static tap_state_t end_state_follower = TAP_RESET;
3245
3246 void tap_set_end_state( tap_state_t new_end_state )
3247 {
3248 /* this is the state we think the TAPs will be in at completion of the
3249 current TAP operation, was end_state
3250 */
3251 end_state_follower = new_end_state;
3252 }
3253
3254 tap_state_t tap_get_end_state()
3255 {
3256 return end_state_follower;
3257 }
3258
3259
3260 int tap_move_ndx( tap_state_t astate )
3261 {
3262 /* given a stable state, return the index into the tms_seqs[] array within tap_get_tms_path() */
3263
3264 int ndx;
3265
3266 switch( astate )
3267 {
3268 case TAP_RESET: ndx = 0; break;
3269 case TAP_DRSHIFT: ndx = 2; break;
3270 case TAP_DRPAUSE: ndx = 3; break;
3271 case TAP_IDLE: ndx = 1; break;
3272 case TAP_IRSHIFT: ndx = 4; break;
3273 case TAP_IRPAUSE: ndx = 5; break;
3274 default:
3275 LOG_ERROR( "fatal: unstable state \"%s\" used in tap_move_ndx()", tap_state_name(astate) );
3276 exit(1);
3277 }
3278
3279 return ndx;
3280 }
3281
3282
3283 /* tap_move[i][j]: tap movement command to go from state i to state j
3284 * 0: Test-Logic-Reset
3285 * 1: Run-Test/Idle
3286 * 2: Shift-DR
3287 * 3: Pause-DR
3288 * 4: Shift-IR
3289 * 5: Pause-IR
3290 *
3291 * DRSHIFT->DRSHIFT and IRSHIFT->IRSHIFT have to be caught in interface specific code
3292 */
3293 struct tms_sequences
3294 {
3295 u8 bits;
3296 u8 bit_count;
3297
3298 };
3299
3300 /*
3301 * These macros allow us to specify TMS state transitions by bits rather than hex bytes.
3302 * Read the bits from LSBit first to MSBit last (right-to-left).
3303 */
3304 #define HEX__(n) 0x##n##LU
3305
3306 #define B8__(x) \
3307 (((x) & 0x0000000FLU)?(1<<0):0) \
3308 +(((x) & 0x000000F0LU)?(1<<1):0) \
3309 +(((x) & 0x00000F00LU)?(1<<2):0) \
3310 +(((x) & 0x0000F000LU)?(1<<3):0) \
3311 +(((x) & 0x000F0000LU)?(1<<4):0) \
3312 +(((x) & 0x00F00000LU)?(1<<5):0) \
3313 +(((x) & 0x0F000000LU)?(1<<6):0) \
3314 +(((x) & 0xF0000000LU)?(1<<7):0)
3315
3316 #define B8(bits,count) { ((u8)B8__(HEX__(bits))), (count) }
3317
3318 static const struct tms_sequences old_tms_seqs[6][6] = /* [from_state_ndx][to_state_ndx] */
3319 {
3320 /* value clocked to TMS to move from one of six stable states to another.
3321 * N.B. OOCD clocks TMS from LSB first, so read these right-to-left.
3322 * N.B. These values are tightly bound to the table in tap_get_tms_path_len().
3323 * N.B. Reset only needs to be 0b11111, but in JLink an even byte of 1's is more stable.
3324 * These extra ones cause no TAP state problem, because we go into reset and stay in reset.
3325 */
3326
3327
3328
3329 /* to state: */
3330 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ /* from state: */
3331 { B8(1111111,7), B8(0000000,7), B8(0010111,7), B8(0001010,7), B8(0011011,7), B8(0010110,7) }, /* RESET */
3332 { B8(1111111,7), B8(0000000,7), B8(0100101,7), B8(0000101,7), B8(0101011,7), B8(0001011,7) }, /* IDLE */
3333 { B8(1111111,7), B8(0110001,7), B8(0000000,7), B8(0000001,7), B8(0001111,7), B8(0101111,7) }, /* DRSHIFT */
3334 { B8(1111111,7), B8(0110000,7), B8(0100000,7), B8(0010111,7), B8(0011110,7), B8(0101111,7) }, /* DRPAUSE */
3335 { B8(1111111,7), B8(0110001,7), B8(0000111,7), B8(0010111,7), B8(0000000,7), B8(0000001,7) }, /* IRSHIFT */
3336 { B8(1111111,7), B8(0110000,7), B8(0011100,7), B8(0010111,7), B8(0011110,7), B8(0101111,7) }, /* IRPAUSE */
3337 };
3338
3339
3340
3341 static const struct tms_sequences short_tms_seqs[6][6] = /* [from_state_ndx][to_state_ndx] */
3342 {
3343 /* this is the table submitted by Jeff Williams on 3/30/2009 with this comment:
3344
3345 OK, I added Peter's version of the state table, and it works OK for
3346 me on MC1322x. I've recreated the jlink portion of patch with this
3347 new state table. His changes to my state table are pretty minor in
3348 terms of total transitions, but Peter feels that his version fixes
3349 some long-standing problems.
3350 Jeff
3351
3352 I added the bit count into the table, reduced RESET column to 7 bits from 8.
3353 Dick
3354
3355 state specific comments:
3356 ------------------------
3357 *->RESET tried the 5 bit reset and it gave me problems, 7 bits seems to
3358 work better on ARM9 with ft2232 driver. (Dick)
3359
3360 RESET->DRSHIFT add 1 extra clock cycles in the RESET state before advancing.
3361 needed on ARM9 with ft2232 driver. (Dick)
3362
3363 RESET->IRSHIFT add 1 extra clock cycles in the RESET state before advancing.
3364 needed on ARM9 with ft2232 driver. (Dick)
3365 */
3366
3367 /* to state: */
3368 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ /* from state: */
3369 { B8(1111111,7), B8(0000000,7), B8(0010111,7), B8(0001010,7), B8(0011011,7), B8(0010110,7) }, /* RESET */
3370 { B8(1111111,7), B8(0000000,7), B8(001,3), B8(0101,4), B8(0011,4), B8(01011,5) }, /* IDLE */
3371 { B8(1111111,7), B8(011,3), B8(00111,5), B8(01,2), B8(001111,6), B8(0101111,7) }, /* DRSHIFT */
3372 { B8(1111111,7), B8(011,3), B8(01,2), B8(0,1), B8(001111,6), B8(0101111,7) }, /* DRPAUSE */
3373 { B8(1111111,7), B8(011,3), B8(00111,5), B8(010111,6), B8(001111,6), B8(01,2) }, /* IRSHIFT */
3374 { B8(1111111,7), B8(011,3), B8(00111,5), B8(010111,6), B8(01,2), B8(0,1) } /* IRPAUSE */
3375
3376 };
3377
3378 typedef const struct tms_sequences tms_table[6][6];
3379
3380 static tms_table *tms_seqs=&short_tms_seqs;
3381
3382 int tap_get_tms_path( tap_state_t from, tap_state_t to )
3383 {
3384 return (*tms_seqs)[tap_move_ndx(from)][tap_move_ndx(to)].bits;
3385 }
3386
3387
3388 int tap_get_tms_path_len( tap_state_t from, tap_state_t to )
3389 {
3390 return (*tms_seqs)[tap_move_ndx(from)][tap_move_ndx(to)].bit_count;
3391 }
3392
3393
3394 bool tap_is_state_stable(tap_state_t astate)
3395 {
3396 bool is_stable;
3397
3398 /* A switch() is used because it is symbol dependent
3399 (not value dependent like an array), and can also check bounds.
3400 */
3401 switch( astate )
3402 {
3403 case TAP_RESET:
3404 case TAP_IDLE:
3405 case TAP_DRSHIFT:
3406 case TAP_DRPAUSE:
3407 case TAP_IRSHIFT:
3408 case TAP_IRPAUSE:
3409 is_stable = true;
3410 break;
3411 default:
3412 is_stable = false;
3413 }
3414
3415 return is_stable;
3416 }
3417
3418 tap_state_t tap_state_transition(tap_state_t cur_state, bool tms)
3419 {
3420 tap_state_t new_state;
3421
3422 /* A switch is used because it is symbol dependent and not value dependent
3423 like an array. Also it can check for out of range conditions.
3424 */
3425
3426 if (tms)
3427 {
3428 switch (cur_state)
3429 {
3430 case TAP_RESET:
3431 new_state = cur_state;
3432 break;
3433 case TAP_IDLE:
3434 case TAP_DRUPDATE:
3435 case TAP_IRUPDATE:
3436 new_state = TAP_DRSELECT;
3437 break;
3438 case TAP_DRSELECT:
3439 new_state = TAP_IRSELECT;
3440 break;
3441 case TAP_DRCAPTURE:
3442 case TAP_DRSHIFT:
3443 new_state = TAP_DREXIT1;
3444 break;
3445 case TAP_DREXIT1:
3446 case TAP_DREXIT2:
3447 new_state = TAP_DRUPDATE;
3448 break;
3449 case TAP_DRPAUSE:
3450 new_state = TAP_DREXIT2;
3451 break;
3452 case TAP_IRSELECT:
3453 new_state = TAP_RESET;
3454 break;
3455 case TAP_IRCAPTURE:
3456 case TAP_IRSHIFT:
3457 new_state = TAP_IREXIT1;
3458 break;
3459 case TAP_IREXIT1:
3460 case TAP_IREXIT2:
3461 new_state = TAP_IRUPDATE;
3462 break;
3463 case TAP_IRPAUSE:
3464 new_state = TAP_IREXIT2;
3465 break;
3466 default:
3467 LOG_ERROR( "fatal: invalid argument cur_state=%d", cur_state );
3468 exit(1);
3469 break;
3470 }
3471 }
3472 else
3473 {
3474 switch (cur_state)
3475 {
3476 case TAP_RESET:
3477 case TAP_IDLE:
3478 case TAP_DRUPDATE:
3479 case TAP_IRUPDATE:
3480 new_state = TAP_IDLE;
3481 break;
3482 case TAP_DRSELECT:
3483 new_state = TAP_DRCAPTURE;
3484 break;
3485 case TAP_DRCAPTURE:
3486 case TAP_DRSHIFT:
3487 case TAP_DREXIT2:
3488 new_state = TAP_DRSHIFT;
3489 break;
3490 case TAP_DREXIT1:
3491 case TAP_DRPAUSE:
3492 new_state = TAP_DRPAUSE;
3493 break;
3494 case TAP_IRSELECT:
3495 new_state = TAP_IRCAPTURE;
3496 break;
3497 case TAP_IRCAPTURE:
3498 case TAP_IRSHIFT:
3499 case TAP_IREXIT2:
3500 new_state = TAP_IRSHIFT;
3501 break;
3502 case TAP_IREXIT1:
3503 case TAP_IRPAUSE:
3504 new_state = TAP_IRPAUSE;
3505 break;
3506 default:
3507 LOG_ERROR( "fatal: invalid argument cur_state=%d", cur_state );
3508 exit(1);
3509 break;
3510 }
3511 }
3512
3513 return new_state;
3514 }
3515
3516 const char* tap_state_name(tap_state_t state)
3517 {
3518 const char* ret;
3519
3520 switch( state )
3521 {
3522 case TAP_RESET: ret = "RESET"; break;
3523 case TAP_IDLE: ret = "RUN/IDLE"; break;
3524 case TAP_DRSELECT: ret = "DRSELECT"; break;
3525 case TAP_DRCAPTURE: ret = "DRCAPTURE"; break;
3526 case TAP_DRSHIFT: ret = "DRSHIFT"; break;
3527 case TAP_DREXIT1: ret = "DREXIT1"; break;
3528 case TAP_DRPAUSE: ret = "DRPAUSE"; break;
3529 case TAP_DREXIT2: ret = "DREXIT2"; break;
3530 case TAP_DRUPDATE: ret = "DRUPDATE"; break;
3531 case TAP_IRSELECT: ret = "IRSELECT"; break;
3532 case TAP_IRCAPTURE: ret = "IRCAPTURE"; break;
3533 case TAP_IRSHIFT: ret = "IRSHIFT"; break;
3534 case TAP_IREXIT1: ret = "IREXIT1"; break;
3535 case TAP_IRPAUSE: ret = "IRPAUSE"; break;
3536 case TAP_IREXIT2: ret = "IREXIT2"; break;
3537 case TAP_IRUPDATE: ret = "IRUPDATE"; break;
3538 default: ret = "???";
3539 }
3540
3541 return ret;
3542 }
3543
3544 static tap_state_t tap_state_by_name( const char *name )
3545 {
3546 tap_state_t x;
3547
3548 for( x = 0 ; x < TAP_NUM_STATES ; x++ ){
3549 /* be nice to the human */
3550 if( 0 == strcasecmp( name, tap_state_name(x) ) ){
3551 return x;
3552 }
3553 }
3554 /* not found */
3555 return TAP_INVALID;
3556 }
3557
3558 #ifdef _DEBUG_JTAG_IO_
3559
3560 #define JTAG_DEBUG_STATE_APPEND(buf, len, bit) \
3561 do { buf[len] = bit ? '1' : '0'; } while(0)
3562 #define JTAG_DEBUG_STATE_PRINT(a, b, astr, bstr) \
3563 DEBUG_JTAG_IO("TAP/SM: %9s -> %5s\tTMS: %s\tTDI: %s", \
3564 tap_state_name(a), tap_state_name(b), astr, bstr)
3565
3566 tap_state_t jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf,
3567 unsigned tap_bits, tap_state_t next_state)
3568 {
3569 const u8 *tms_buffer;
3570 const u8 *tdi_buffer;
3571 unsigned tap_bytes;
3572 unsigned cur_byte;
3573 unsigned cur_bit;
3574
3575 unsigned tap_out_bits;
3576 char tms_str[33];
3577 char tdi_str[33];
3578
3579 tap_state_t last_state;
3580
3581 // set startstate (and possibly last, if tap_bits == 0)
3582 last_state = next_state;
3583 DEBUG_JTAG_IO("TAP/SM: START state: %s", tap_state_name(next_state));
3584
3585 tms_buffer = (const u8 *)tms_buf;
3586 tdi_buffer = (const u8 *)tdi_buf;
3587
3588 tap_bytes = TAP_SCAN_BYTES(tap_bits);
3589 DEBUG_JTAG_IO("TAP/SM: TMS bits: %u (bytes: %u)", tap_bits, tap_bytes);
3590
3591 tap_out_bits = 0;
3592 for(cur_byte = 0; cur_byte < tap_bytes; cur_byte++)
3593 {
3594 for(cur_bit = 0; cur_bit < 8; cur_bit++)
3595 {
3596 // make sure we do not run off the end of the buffers
3597 unsigned tap_bit = cur_byte * 8 + cur_bit;
3598 if (tap_bit == tap_bits)
3599 break;
3600
3601 // check and save TMS bit
3602 tap_bit = !!(tms_buffer[cur_byte] & (1 << cur_bit));
3603 JTAG_DEBUG_STATE_APPEND(tms_str, tap_out_bits, tap_bit);
3604
3605 // use TMS bit to find the next TAP state
3606 next_state = tap_state_transition(last_state, tap_bit);
3607
3608 // check and store TDI bit
3609 tap_bit = !!(tdi_buffer[cur_byte] & (1 << cur_bit));
3610 JTAG_DEBUG_STATE_APPEND(tdi_str, tap_out_bits, tap_bit);
3611
3612 // increment TAP bits
3613 tap_out_bits++;
3614
3615 // Only show TDO bits on state transitions, or
3616 // after some number of bits in the same state.
3617 if ((next_state == last_state) && (tap_out_bits < 32))
3618 continue;
3619
3620 // terminate strings and display state transition
3621 tms_str[tap_out_bits] = tdi_str[tap_out_bits] = 0;
3622 JTAG_DEBUG_STATE_PRINT(last_state, next_state, tms_str, tdi_str);
3623
3624 // reset state
3625 last_state = next_state;
3626 tap_out_bits = 0;
3627 }
3628 }
3629
3630 if (tap_out_bits)
3631 {
3632 // terminate strings and display state transition
3633 tms_str[tap_out_bits] = tdi_str[tap_out_bits] = 0;
3634 JTAG_DEBUG_STATE_PRINT(last_state, next_state, tms_str, tdi_str);
3635 }
3636
3637 DEBUG_JTAG_IO("TAP/SM: FINAL state: %s", tap_state_name(next_state));
3638
3639 return next_state;
3640 }
3641 #endif // _DEBUG_JTAG_IO_
3642
3643 #ifndef HAVE_JTAG_MINIDRIVER_H
3644 void jtag_alloc_in_value32(scan_field_t *field)
3645 {
3646 field->in_value=(u8 *)cmd_queue_alloc(4);
3647 }
3648 #endif
3649
3650 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3651 {
3652 if (argc == 1)
3653 {
3654 if (strcmp(args[0], "short") == 0)
3655 {
3656 tms_seqs=&short_tms_seqs;
3657 }
3658 else if (strcmp(args[0], "long") == 0)
3659 {
3660 tms_seqs=&old_tms_seqs;
3661 } else
3662 {
3663 return ERROR_COMMAND_SYNTAX_ERROR;
3664 }
3665 } else if (argc != 0)
3666 {
3667 return ERROR_COMMAND_SYNTAX_ERROR;
3668 }
3669
3670 command_print(cmd_ctx, "tms sequence is %s", (tms_seqs==&short_tms_seqs) ? "short": "long");
3671
3672 return ERROR_OK;
3673 }
3674
3675 /*-----</Cable Helper API>--------------------------------------*/

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)