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

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)