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