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