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