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