jtag newtap change & huge manual update
[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 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "replacements.h"
28
29 #include "jtag.h"
30
31 #include "command.h"
32 #include "log.h"
33
34 #include "stdlib.h"
35 #include "string.h"
36 #include <unistd.h>
37
38 /* note that this is not marked as static as it must be available from outside jtag.c for those
39 that implement the jtag_xxx() minidriver layer
40 */
41 int jtag_error=ERROR_OK;
42
43
44 char* tap_state_strings[16] =
45 {
46 "tlr",
47 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
48 "rti",
49 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
50 };
51
52 typedef struct cmd_queue_page_s
53 {
54 void *address;
55 size_t used;
56 struct cmd_queue_page_s *next;
57 } cmd_queue_page_t;
58
59 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
60 static cmd_queue_page_t *cmd_queue_pages = NULL;
61
62 /* tap_move[i][j]: tap movement command to go from state i to state j
63 * 0: Test-Logic-Reset
64 * 1: Run-Test/Idle
65 * 2: Shift-DR
66 * 3: Pause-DR
67 * 4: Shift-IR
68 * 5: Pause-IR
69 *
70 * SD->SD and SI->SI have to be caught in interface specific code
71 */
72 u8 tap_move[6][6] =
73 {
74 /* TLR RTI SD PD SI PI */
75 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
76 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
77 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
78 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
79 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
80 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
81 };
82
83 int tap_move_map[16] = {
84 0, -1, -1, 2, -1, 3, -1, -1,
85 1, -1, -1, 4, -1, 5, -1, -1
86 };
87
88 tap_transition_t tap_transitions[16] =
89 {
90 {TAP_TLR, TAP_RTI}, /* TLR */
91 {TAP_SIS, TAP_CD}, /* SDS */
92 {TAP_E1D, TAP_SD}, /* CD */
93 {TAP_E1D, TAP_SD}, /* SD */
94 {TAP_UD, TAP_PD}, /* E1D */
95 {TAP_E2D, TAP_PD}, /* PD */
96 {TAP_UD, TAP_SD}, /* E2D */
97 {TAP_SDS, TAP_RTI}, /* UD */
98 {TAP_SDS, TAP_RTI}, /* RTI */
99 {TAP_TLR, TAP_CI}, /* SIS */
100 {TAP_E1I, TAP_SI}, /* CI */
101 {TAP_E1I, TAP_SI}, /* SI */
102 {TAP_UI, TAP_PI}, /* E1I */
103 {TAP_E2I, TAP_PI}, /* PI */
104 {TAP_UI, TAP_SI}, /* E2I */
105 {TAP_SDS, TAP_RTI} /* UI */
106 };
107
108 char* jtag_event_strings[] =
109 {
110 "JTAG controller reset (TLR or TRST)"
111 };
112
113 /* kludge!!!! these are just global variables that the
114 * interface use internally. They really belong
115 * inside the drivers, but we don't want to break
116 * linking the drivers!!!!
117 */
118 enum tap_state end_state = TAP_TLR;
119 enum tap_state cur_state = TAP_TLR;
120 int jtag_trst = 0;
121 int jtag_srst = 0;
122
123 jtag_command_t *jtag_command_queue = NULL;
124 jtag_command_t **last_comand_pointer = &jtag_command_queue;
125 static jtag_tap_t *jtag_all_taps = NULL;
126
127 enum reset_types jtag_reset_config = RESET_NONE;
128 enum tap_state cmd_queue_end_state = TAP_TLR;
129 enum tap_state cmd_queue_cur_state = TAP_TLR;
130
131 int jtag_verify_capture_ir = 1;
132
133 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
134 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
135 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
136
137 /* maximum number of JTAG devices expected in the chain
138 */
139 #define JTAG_MAX_CHAIN_SIZE 20
140
141 /* callbacks to inform high-level handlers about JTAG state changes */
142 jtag_event_callback_t *jtag_event_callbacks;
143
144 /* speed in kHz*/
145 static int speed_khz = 0;
146 /* flag if the kHz speed was defined */
147 static int hasKHz = 0;
148
149 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
150 */
151
152 #if BUILD_ECOSBOARD == 1
153 extern jtag_interface_t zy1000_interface;
154 #endif
155
156 #if BUILD_PARPORT == 1
157 extern jtag_interface_t parport_interface;
158 #endif
159
160 #if BUILD_DUMMY == 1
161 extern jtag_interface_t dummy_interface;
162 #endif
163
164 #if BUILD_FT2232_FTD2XX == 1
165 extern jtag_interface_t ft2232_interface;
166 #endif
167
168 #if BUILD_FT2232_LIBFTDI == 1
169 extern jtag_interface_t ft2232_interface;
170 #endif
171
172 #if BUILD_AMTJTAGACCEL == 1
173 extern jtag_interface_t amt_jtagaccel_interface;
174 #endif
175
176 #if BUILD_EP93XX == 1
177 extern jtag_interface_t ep93xx_interface;
178 #endif
179
180 #if BUILD_AT91RM9200 == 1
181 extern jtag_interface_t at91rm9200_interface;
182 #endif
183
184 #if BUILD_GW16012 == 1
185 extern jtag_interface_t gw16012_interface;
186 #endif
187
188 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
189 extern jtag_interface_t presto_interface;
190 #endif
191
192 #if BUILD_USBPROG == 1
193 extern jtag_interface_t usbprog_interface;
194 #endif
195
196 #if BUILD_JLINK == 1
197 extern jtag_interface_t jlink_interface;
198 #endif
199
200 jtag_interface_t *jtag_interfaces[] = {
201 #if BUILD_ECOSBOARD == 1
202 &zy1000_interface,
203 #endif
204 #if BUILD_PARPORT == 1
205 &parport_interface,
206 #endif
207 #if BUILD_DUMMY == 1
208 &dummy_interface,
209 #endif
210 #if BUILD_FT2232_FTD2XX == 1
211 &ft2232_interface,
212 #endif
213 #if BUILD_FT2232_LIBFTDI == 1
214 &ft2232_interface,
215 #endif
216 #if BUILD_AMTJTAGACCEL == 1
217 &amt_jtagaccel_interface,
218 #endif
219 #if BUILD_EP93XX == 1
220 &ep93xx_interface,
221 #endif
222 #if BUILD_AT91RM9200 == 1
223 &at91rm9200_interface,
224 #endif
225 #if BUILD_GW16012 == 1
226 &gw16012_interface,
227 #endif
228 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
229 &presto_interface,
230 #endif
231 #if BUILD_USBPROG == 1
232 &usbprog_interface,
233 #endif
234 #if BUILD_JLINK == 1
235 &jlink_interface,
236 #endif
237 NULL,
238 };
239
240 jtag_interface_t *jtag = NULL;
241
242 /* configuration */
243 jtag_interface_t *jtag_interface = NULL;
244 int jtag_speed = 0;
245
246
247
248 /* forward declarations */
249 void jtag_add_pathmove(int num_states, enum tap_state *path);
250 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
251 void jtag_add_end_state(enum tap_state endstate);
252 void jtag_add_sleep(u32 us);
253 int jtag_execute_queue(void);
254
255
256 /* jtag commands */
257 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
258 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
259 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
260 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
261 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
262 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
263 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
264
265 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
266
267 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
268 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
269 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
270 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
271 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
272
273 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
274
275
276 jtag_tap_t *jtag_AllTaps(void)
277 {
278 return jtag_all_taps;
279 };
280
281 int
282 jtag_NumTotalTaps(void)
283 {
284 jtag_tap_t *t;
285 int n;
286
287 n = 0;
288 t = jtag_AllTaps();
289 while(t){
290 n++;
291 t = t->next_tap;
292 }
293 return n;
294 }
295
296 int
297 jtag_NumEnabledTaps(void)
298 {
299 jtag_tap_t *t;
300 int n;
301
302 n = 0;
303 t = jtag_AllTaps();
304 while(t){
305 if( t->enabled ){
306 n++;
307 }
308 t = t->next_tap;
309 }
310 return n;
311 }
312
313 jtag_tap_t *
314 jtag_NextEnabledTap( jtag_tap_t *p )
315 {
316 if( p == NULL ){
317 // start at the head of list
318 p = jtag_AllTaps();
319 } else {
320 // start *after* this one
321 p = p->next_tap;
322 }
323 while( p ){
324 if( p->enabled ){
325 break;
326 } else {
327 p = p->next_tap;
328 }
329 }
330 return p;
331 }
332
333 jtag_tap_t *jtag_TapByString( const char *s )
334 {
335 jtag_tap_t *t;
336 char *cp;
337
338 t = jtag_AllTaps();
339 // try name first
340 while(t){
341 if( 0 == strcmp( t->dotted_name, s ) ){
342 break;
343 } else {
344 t = t->next_tap;
345 }
346 }
347 // backup plan is by number
348 if( t == NULL ){
349 /* ok - is "s" a number? */
350 int n;
351 n = strtol( s, &cp, 0 );
352 if( (s != cp) && (*cp == 0) ){
353 /* Then it is... */
354 t = jtag_TapByAbsPosition(n);
355 }
356 }
357 return t;
358 }
359
360 jtag_tap_t *
361 jtag_TapByJimObj( Jim_Interp *interp, Jim_Obj *o )
362 {
363 jtag_tap_t *t;
364 const char *cp;
365
366 cp = Jim_GetString( o, NULL );
367 if(cp == NULL){
368 cp = "(unknown)";
369 t = NULL;
370 } else {
371 t = jtag_TapByString( cp );
372 }
373 if( t == NULL ){
374 Jim_SetResult_sprintf(interp,"Tap: %s is unknown", cp );
375 }
376 return t;
377 }
378
379 /* returns a pointer to the n-th device in the scan chain */
380 jtag_tap_t *
381 jtag_TapByAbsPosition( int n )
382 {
383 int orig_n;
384 jtag_tap_t *t;
385
386 orig_n = n;
387 t = jtag_AllTaps();
388
389 while( t && (n > 0)) {
390 n--;
391 t = t->next_tap;
392 }
393 return t;
394 }
395
396
397 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
398 {
399 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
400
401 if (callback == NULL)
402 {
403 return ERROR_INVALID_ARGUMENTS;
404 }
405
406 if (*callbacks_p)
407 {
408 while ((*callbacks_p)->next)
409 callbacks_p = &((*callbacks_p)->next);
410 callbacks_p = &((*callbacks_p)->next);
411 }
412
413 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
414 (*callbacks_p)->callback = callback;
415 (*callbacks_p)->priv = priv;
416 (*callbacks_p)->next = NULL;
417
418 return ERROR_OK;
419 }
420
421 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
422 {
423 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
424
425 if (callback == NULL)
426 {
427 return ERROR_INVALID_ARGUMENTS;
428 }
429
430 while (*callbacks_p)
431 {
432 jtag_event_callback_t **next = &((*callbacks_p)->next);
433 if ((*callbacks_p)->callback == callback)
434 {
435 free(*callbacks_p);
436 *callbacks_p = *next;
437 }
438 callbacks_p = next;
439 }
440
441 return ERROR_OK;
442 }
443
444 int jtag_call_event_callbacks(enum jtag_event event)
445 {
446 jtag_event_callback_t *callback = jtag_event_callbacks;
447
448 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
449
450 while (callback)
451 {
452 callback->callback(event, callback->priv);
453 callback = callback->next;
454 }
455
456 return ERROR_OK;
457 }
458
459 /* returns a pointer to the pointer of the last command in queue
460 * this may be a pointer to the root pointer (jtag_command_queue)
461 * or to the next member of the last but one command
462 */
463 jtag_command_t** jtag_get_last_command_p(void)
464 {
465 /* jtag_command_t *cmd = jtag_command_queue;
466
467 if (cmd)
468 while (cmd->next)
469 cmd = cmd->next;
470 else
471 return &jtag_command_queue;
472
473 return &cmd->next;*/
474
475 return last_comand_pointer;
476 }
477
478
479 void* cmd_queue_alloc(size_t size)
480 {
481 cmd_queue_page_t **p_page = &cmd_queue_pages;
482 int offset;
483 u8 *t;
484
485 if (*p_page)
486 {
487 while ((*p_page)->next)
488 p_page = &((*p_page)->next);
489 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
490 p_page = &((*p_page)->next);
491 }
492
493 if (!*p_page)
494 {
495 *p_page = malloc(sizeof(cmd_queue_page_t));
496 (*p_page)->used = 0;
497 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
498 (*p_page)->next = NULL;
499 }
500
501 offset = (*p_page)->used;
502 (*p_page)->used += size;
503
504 t=(u8 *)((*p_page)->address);
505 return t + offset;
506 }
507
508 void cmd_queue_free(void)
509 {
510 cmd_queue_page_t *page = cmd_queue_pages;
511
512 while (page)
513 {
514 cmd_queue_page_t *last = page;
515 free(page->address);
516 page = page->next;
517 free(last);
518 }
519
520 cmd_queue_pages = NULL;
521 }
522
523 static void jtag_prelude1(void)
524 {
525 if (jtag_trst == 1)
526 {
527 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
528 jtag_error=ERROR_JTAG_TRST_ASSERTED;
529 return;
530 }
531
532 if (cmd_queue_end_state == TAP_TLR)
533 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
534 }
535
536 static void jtag_prelude(enum tap_state state)
537 {
538 jtag_prelude1();
539
540 if (state != -1)
541 jtag_add_end_state(state);
542
543 cmd_queue_cur_state = cmd_queue_end_state;
544 }
545
546 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
547 {
548 int retval;
549
550 jtag_prelude(state);
551
552 retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
553 if (retval!=ERROR_OK)
554 jtag_error=retval;
555 }
556
557 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
558 {
559 jtag_command_t **last_cmd;
560 jtag_tap_t *tap;
561 int j;
562 int x;
563 int nth_tap;
564 int scan_size = 0;
565
566
567 last_cmd = jtag_get_last_command_p();
568
569 /* allocate memory for a new list member */
570 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
571 (*last_cmd)->next = NULL;
572 last_comand_pointer = &((*last_cmd)->next);
573 (*last_cmd)->type = JTAG_SCAN;
574
575 /* allocate memory for ir scan command */
576 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
577 (*last_cmd)->cmd.scan->ir_scan = 1;
578 x = jtag_NumEnabledTaps();
579 (*last_cmd)->cmd.scan->num_fields = x; /* one field per device */
580 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(x * sizeof(scan_field_t));
581 (*last_cmd)->cmd.scan->end_state = state;
582
583 nth_tap = -1;
584 tap = NULL;
585 for(;;){
586 int found = 0;
587
588 // do this here so it is not forgotten
589 tap = jtag_NextEnabledTap(tap);
590 if( tap == NULL ){
591 break;
592 }
593 nth_tap++;
594 scan_size = tap->ir_length;
595 (*last_cmd)->cmd.scan->fields[nth_tap].tap = tap;
596 (*last_cmd)->cmd.scan->fields[nth_tap].num_bits = scan_size;
597 (*last_cmd)->cmd.scan->fields[nth_tap].in_value = NULL;
598 (*last_cmd)->cmd.scan->fields[nth_tap].in_handler = NULL; /* disable verification by default */
599
600 /* search the list */
601 for (j = 0; j < num_fields; j++)
602 {
603 if (tap == fields[j].tap)
604 {
605 found = 1;
606 (*last_cmd)->cmd.scan->fields[nth_tap].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
607 (*last_cmd)->cmd.scan->fields[nth_tap].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
608
609 if (jtag_verify_capture_ir)
610 {
611 if (fields[j].in_handler==NULL)
612 {
613 jtag_set_check_value((*last_cmd)->cmd.scan->fields+nth_tap, tap->expected, tap->expected_mask, NULL);
614 } else
615 {
616 (*last_cmd)->cmd.scan->fields[nth_tap].in_handler = fields[j].in_handler;
617 (*last_cmd)->cmd.scan->fields[nth_tap].in_handler_priv = fields[j].in_handler_priv;
618 (*last_cmd)->cmd.scan->fields[nth_tap].in_check_value = tap->expected;
619 (*last_cmd)->cmd.scan->fields[nth_tap].in_check_mask = tap->expected_mask;
620 }
621 }
622
623 tap->bypass = 0;
624 break;
625 }
626 }
627
628 if (!found)
629 {
630 /* if a tap isn't listed, set it to BYPASS */
631 (*last_cmd)->cmd.scan->fields[nth_tap].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
632 (*last_cmd)->cmd.scan->fields[nth_tap].out_mask = NULL;
633 tap->bypass = 1;
634
635 }
636
637 /* update device information */
638 buf_cpy((*last_cmd)->cmd.scan->fields[nth_tap].out_value, tap->cur_instr, scan_size);
639 }
640
641 return ERROR_OK;
642 }
643
644 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
645 {
646 int retval;
647
648 jtag_prelude(state);
649
650 retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
651 if (retval!=ERROR_OK)
652 jtag_error=retval;
653 }
654
655 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
656 {
657 int i;
658 jtag_command_t **last_cmd;
659
660 last_cmd = jtag_get_last_command_p();
661
662 /* allocate memory for a new list member */
663 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
664 (*last_cmd)->next = NULL;
665 last_comand_pointer = &((*last_cmd)->next);
666 (*last_cmd)->type = JTAG_SCAN;
667
668 /* allocate memory for ir scan command */
669 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
670 (*last_cmd)->cmd.scan->ir_scan = 1;
671 (*last_cmd)->cmd.scan->num_fields = num_fields;
672 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
673 (*last_cmd)->cmd.scan->end_state = state;
674
675 for( i = 0 ; i < num_fields ; i++ ){
676 int num_bits = fields[i].num_bits;
677 int num_bytes = CEIL(fields[i].num_bits, 8);
678 (*last_cmd)->cmd.scan->fields[i].tap = fields[i].tap;
679 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
680 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
681 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
682 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
683 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
684 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
685 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
686 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
687 }
688 return ERROR_OK;
689 }
690
691 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
692 {
693 int retval;
694
695 jtag_prelude(state);
696
697 retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
698 if (retval!=ERROR_OK)
699 jtag_error=retval;
700 }
701
702 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
703 {
704 int j;
705 int nth_tap;
706 int bypass_devices = 0;
707 int field_count = 0;
708 int scan_size;
709
710 jtag_command_t **last_cmd = jtag_get_last_command_p();
711 jtag_tap_t *tap;
712
713 /* count devices in bypass */
714 tap = NULL;
715 bypass_devices = 0;
716 for(;;){
717 tap = jtag_NextEnabledTap(tap);
718 if( tap == NULL ){
719 break;
720 }
721 if( tap->bypass ){
722 bypass_devices++;
723 }
724 }
725
726 /* allocate memory for a new list member */
727 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
728 last_comand_pointer = &((*last_cmd)->next);
729 (*last_cmd)->next = NULL;
730 (*last_cmd)->type = JTAG_SCAN;
731
732 /* allocate memory for dr scan command */
733 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
734 (*last_cmd)->cmd.scan->ir_scan = 0;
735 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
736 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
737 (*last_cmd)->cmd.scan->end_state = state;
738
739 tap = NULL;
740 nth_tap = -1;
741 for(;;){
742 nth_tap++;
743 tap = jtag_NextEnabledTap(tap);
744 if( tap == NULL ){
745 break;
746 }
747 int found = 0;
748 (*last_cmd)->cmd.scan->fields[field_count].tap = tap;
749
750 for (j = 0; j < num_fields; j++)
751 {
752 if (tap == fields[j].tap)
753 {
754 found = 1;
755 scan_size = fields[j].num_bits;
756 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
757 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
758 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
759 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
760 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
761 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
762 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
763 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
764 }
765 }
766 if (!found)
767 {
768 #ifdef _DEBUG_JTAG_IO_
769 /* if a device isn't listed, the BYPASS register should be selected */
770 if (! tap->bypass)
771 {
772 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
773 exit(-1);
774 }
775 #endif
776 /* program the scan field to 1 bit length, and ignore it's value */
777 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
778 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
779 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
780 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
781 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
782 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
783 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
784 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
785 }
786 else
787 {
788 #ifdef _DEBUG_JTAG_IO_
789 /* if a device is listed, the BYPASS register must not be selected */
790 if (tap->bypass)
791 {
792 LOG_ERROR("BUG: scan data for a device in BYPASS");
793 exit(-1);
794 }
795 #endif
796 }
797 }
798 return ERROR_OK;
799 }
800
801 void MINIDRIVER(interface_jtag_add_dr_out)(jtag_tap_t *target_tap,
802 int num_fields,
803 const int *num_bits,
804 const u32 *value,
805 enum tap_state end_state)
806 {
807 int nth_tap;
808 int field_count = 0;
809 int scan_size;
810 int bypass_devices = 0;
811
812 jtag_command_t **last_cmd = jtag_get_last_command_p();
813 jtag_tap_t *tap;
814
815 /* count devices in bypass */
816 tap = NULL;
817 bypass_devices = 0;
818 for(;;){
819 tap = jtag_NextEnabledTap(tap);
820 if( tap == NULL ){
821 break;
822 }
823 if( tap->bypass ){
824 bypass_devices++;
825 }
826 }
827
828 /* allocate memory for a new list member */
829 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
830 last_comand_pointer = &((*last_cmd)->next);
831 (*last_cmd)->next = NULL;
832 (*last_cmd)->type = JTAG_SCAN;
833
834 /* allocate memory for dr scan command */
835 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
836 (*last_cmd)->cmd.scan->ir_scan = 0;
837 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
838 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
839 (*last_cmd)->cmd.scan->end_state = end_state;
840
841 tap = NULL;
842 nth_tap = -1;
843 for(;;){
844 tap = jtag_NextEnabledTap(tap);
845 if( tap == NULL ){
846 break;
847 }
848 nth_tap++;
849 (*last_cmd)->cmd.scan->fields[field_count].tap = tap;
850
851 if (tap == target_tap)
852 {
853 int j;
854 #ifdef _DEBUG_JTAG_IO_
855 /* if a device is listed, the BYPASS register must not be selected */
856 if (tap->bypass)
857 {
858 LOG_ERROR("BUG: scan data for a device in BYPASS");
859 exit(-1);
860 }
861 #endif
862 for (j = 0; j < num_fields; j++)
863 {
864 u8 out_value[4];
865 scan_size = num_bits[j];
866 buf_set_u32(out_value, 0, scan_size, value[j]);
867 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
868 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
869 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
870 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
871 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
872 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
873 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
874 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
875 }
876 } else
877 {
878 #ifdef _DEBUG_JTAG_IO_
879 /* if a device isn't listed, the BYPASS register should be selected */
880 if (! tap->bypass)
881 {
882 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
883 exit(-1);
884 }
885 #endif
886 /* program the scan field to 1 bit length, and ignore it's value */
887 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
888 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
889 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
890 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
891 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
892 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
893 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
894 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
895 }
896 }
897 }
898
899 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
900 {
901 int retval;
902
903 jtag_prelude(state);
904
905 retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
906 if (retval!=ERROR_OK)
907 jtag_error=retval;
908 }
909
910 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
911 {
912 int i;
913 jtag_command_t **last_cmd = jtag_get_last_command_p();
914
915 /* allocate memory for a new list member */
916 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
917 last_comand_pointer = &((*last_cmd)->next);
918 (*last_cmd)->next = NULL;
919 (*last_cmd)->type = JTAG_SCAN;
920
921 /* allocate memory for scan command */
922 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
923 (*last_cmd)->cmd.scan->ir_scan = 0;
924 (*last_cmd)->cmd.scan->num_fields = num_fields;
925 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
926 (*last_cmd)->cmd.scan->end_state = state;
927
928 for (i = 0; i < num_fields; i++)
929 {
930 int num_bits = fields[i].num_bits;
931 int num_bytes = CEIL(fields[i].num_bits, 8);
932 (*last_cmd)->cmd.scan->fields[i].tap = fields[i].tap;
933 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
934 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
935 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
936 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
937 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
938 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
939 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
940 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
941 }
942
943 return ERROR_OK;
944 }
945
946 void jtag_add_tlr(void)
947 {
948 jtag_prelude(TAP_TLR);
949
950 int retval;
951 retval=interface_jtag_add_tlr();
952 if (retval!=ERROR_OK)
953 jtag_error=retval;
954 }
955
956 int MINIDRIVER(interface_jtag_add_tlr)()
957 {
958 enum tap_state state = TAP_TLR;
959 jtag_command_t **last_cmd = jtag_get_last_command_p();
960
961 /* allocate memory for a new list member */
962 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
963 last_comand_pointer = &((*last_cmd)->next);
964 (*last_cmd)->next = NULL;
965 (*last_cmd)->type = JTAG_STATEMOVE;
966
967 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
968 (*last_cmd)->cmd.statemove->end_state = state;
969
970
971 return ERROR_OK;
972 }
973
974 void jtag_add_pathmove(int num_states, enum tap_state *path)
975 {
976 enum tap_state cur_state=cmd_queue_cur_state;
977 int i;
978 int retval;
979
980 /* the last state has to be a stable state */
981 if (tap_move_map[path[num_states - 1]] == -1)
982 {
983 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
984 exit(-1);
985 }
986
987 for (i=0; i<num_states; i++)
988 {
989 if (path[i] == TAP_TLR)
990 {
991 LOG_ERROR("BUG: TAP_TLR is not a valid state for pathmove sequences");
992 exit(-1);
993 }
994 if ((tap_transitions[cur_state].low != path[i])&&
995 (tap_transitions[cur_state].high != path[i]))
996 {
997 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
998 exit(-1);
999 }
1000 cur_state = path[i];
1001 }
1002
1003 jtag_prelude1();
1004
1005
1006 retval=interface_jtag_add_pathmove(num_states, path);
1007 cmd_queue_cur_state = path[num_states - 1];
1008 if (retval!=ERROR_OK)
1009 jtag_error=retval;
1010 }
1011
1012 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
1013 {
1014 jtag_command_t **last_cmd = jtag_get_last_command_p();
1015 int i;
1016
1017 /* allocate memory for a new list member */
1018 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1019 last_comand_pointer = &((*last_cmd)->next);
1020 (*last_cmd)->next = NULL;
1021 (*last_cmd)->type = JTAG_PATHMOVE;
1022
1023 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
1024 (*last_cmd)->cmd.pathmove->num_states = num_states;
1025 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
1026
1027 for (i = 0; i < num_states; i++)
1028 (*last_cmd)->cmd.pathmove->path[i] = path[i];
1029
1030 return ERROR_OK;
1031 }
1032
1033 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
1034 {
1035 jtag_command_t **last_cmd = jtag_get_last_command_p();
1036
1037 /* allocate memory for a new list member */
1038 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1039 (*last_cmd)->next = NULL;
1040 last_comand_pointer = &((*last_cmd)->next);
1041 (*last_cmd)->type = JTAG_RUNTEST;
1042
1043 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
1044 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
1045 (*last_cmd)->cmd.runtest->end_state = state;
1046
1047 return ERROR_OK;
1048 }
1049
1050 void jtag_add_runtest(int num_cycles, enum tap_state state)
1051 {
1052 int retval;
1053
1054 jtag_prelude(state);
1055
1056 /* executed by sw or hw fifo */
1057 retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
1058 if (retval!=ERROR_OK)
1059 jtag_error=retval;
1060 }
1061
1062 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
1063 {
1064 int trst_with_tlr = 0;
1065 int retval;
1066
1067 /* FIX!!! there are *many* different cases here. A better
1068 * approach is needed for legal combinations of transitions...
1069 */
1070 if ((jtag_reset_config & RESET_HAS_SRST)&&
1071 (jtag_reset_config & RESET_HAS_TRST)&&
1072 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
1073 {
1074 if (((req_tlr_or_trst&&!jtag_trst)||
1075 (!req_tlr_or_trst&&jtag_trst))&&
1076 ((req_srst&&!jtag_srst)||
1077 (!req_srst&&jtag_srst)))
1078 {
1079 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
1080 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
1081 }
1082 }
1083
1084 /* Make sure that jtag_reset_config allows the requested reset */
1085 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
1086 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
1087 {
1088 LOG_ERROR("BUG: requested reset would assert trst");
1089 jtag_error=ERROR_FAIL;
1090 return;
1091 }
1092
1093 /* if TRST pulls SRST, we reset with TAP T-L-R */
1094 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
1095 {
1096 trst_with_tlr = 1;
1097 }
1098
1099 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
1100 {
1101 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
1102 jtag_error=ERROR_FAIL;
1103 return;
1104 }
1105
1106 if (req_tlr_or_trst)
1107 {
1108 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
1109 {
1110 jtag_trst = 1;
1111 } else
1112 {
1113 trst_with_tlr = 1;
1114 }
1115 } else
1116 {
1117 jtag_trst = 0;
1118 }
1119
1120 jtag_srst = req_srst;
1121
1122 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
1123 if (retval!=ERROR_OK)
1124 {
1125 jtag_error=retval;
1126 return;
1127 }
1128
1129 if (jtag_srst)
1130 {
1131 LOG_DEBUG("SRST line asserted");
1132 }
1133 else
1134 {
1135 LOG_DEBUG("SRST line released");
1136 if (jtag_nsrst_delay)
1137 jtag_add_sleep(jtag_nsrst_delay * 1000);
1138 }
1139
1140 if (trst_with_tlr)
1141 {
1142 LOG_DEBUG("JTAG reset with TLR instead of TRST");
1143 jtag_add_end_state(TAP_TLR);
1144 jtag_add_tlr();
1145 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1146 return;
1147 }
1148
1149 if (jtag_trst)
1150 {
1151 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1152 * and inform possible listeners about this
1153 */
1154 LOG_DEBUG("TRST line asserted");
1155 cmd_queue_cur_state = TAP_TLR;
1156 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1157 }
1158 else
1159 {
1160 if (jtag_ntrst_delay)
1161 jtag_add_sleep(jtag_ntrst_delay * 1000);
1162 }
1163 }
1164
1165 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1166 {
1167 jtag_command_t **last_cmd = jtag_get_last_command_p();
1168
1169 /* allocate memory for a new list member */
1170 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1171 (*last_cmd)->next = NULL;
1172 last_comand_pointer = &((*last_cmd)->next);
1173 (*last_cmd)->type = JTAG_RESET;
1174
1175 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1176 (*last_cmd)->cmd.reset->trst = req_trst;
1177 (*last_cmd)->cmd.reset->srst = req_srst;
1178
1179 return ERROR_OK;
1180 }
1181
1182 void jtag_add_end_state(enum tap_state state)
1183 {
1184 cmd_queue_end_state = state;
1185 if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SI))
1186 {
1187 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1188 }
1189 }
1190
1191 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1192 {
1193 jtag_command_t **last_cmd = jtag_get_last_command_p();
1194
1195 /* allocate memory for a new list member */
1196 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1197 (*last_cmd)->next = NULL;
1198 last_comand_pointer = &((*last_cmd)->next);
1199 (*last_cmd)->type = JTAG_SLEEP;
1200
1201 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1202 (*last_cmd)->cmd.sleep->us = us;
1203
1204 return ERROR_OK;
1205 }
1206
1207 void jtag_add_sleep(u32 us)
1208 {
1209 keep_alive(); /* we might be running on a very slow JTAG clk */
1210 int retval=interface_jtag_add_sleep(us);
1211 if (retval!=ERROR_OK)
1212 jtag_error=retval;
1213 return;
1214 }
1215
1216 int jtag_scan_size(scan_command_t *cmd)
1217 {
1218 int bit_count = 0;
1219 int i;
1220
1221 /* count bits in scan command */
1222 for (i = 0; i < cmd->num_fields; i++)
1223 {
1224 bit_count += cmd->fields[i].num_bits;
1225 }
1226
1227 return bit_count;
1228 }
1229
1230 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1231 {
1232 int bit_count = 0;
1233 int i;
1234
1235 bit_count = jtag_scan_size(cmd);
1236 *buffer = malloc(CEIL(bit_count, 8));
1237
1238 bit_count = 0;
1239
1240 for (i = 0; i < cmd->num_fields; i++)
1241 {
1242 if (cmd->fields[i].out_value)
1243 {
1244 #ifdef _DEBUG_JTAG_IO_
1245 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1246 #endif
1247 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1248 #ifdef _DEBUG_JTAG_IO_
1249 LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1250 free(char_buf);
1251 #endif
1252 }
1253
1254 bit_count += cmd->fields[i].num_bits;
1255 }
1256
1257 return bit_count;
1258 }
1259
1260 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1261 {
1262 int i;
1263 int bit_count = 0;
1264 int retval;
1265
1266 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1267 retval = ERROR_OK;
1268
1269 for (i = 0; i < cmd->num_fields; i++)
1270 {
1271 /* if neither in_value nor in_handler
1272 * are specified we don't have to examine this field
1273 */
1274 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1275 {
1276 int num_bits = cmd->fields[i].num_bits;
1277 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1278
1279 #ifdef _DEBUG_JTAG_IO_
1280 char *char_buf;
1281
1282 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1283 LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1284 free(char_buf);
1285 #endif
1286
1287 if (cmd->fields[i].in_value)
1288 {
1289 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1290
1291 if (cmd->fields[i].in_handler)
1292 {
1293 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1294 {
1295 LOG_WARNING("in_handler reported a failed check");
1296 retval = ERROR_JTAG_QUEUE_FAILED;
1297 }
1298 }
1299 }
1300
1301 /* no in_value specified, but a handler takes care of the scanned data */
1302 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1303 {
1304 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1305 {
1306 /* We're going to call the error:handler later, but if the in_handler
1307 * reported an error we report this failure upstream
1308 */
1309 LOG_WARNING("in_handler reported a failed check");
1310 retval = ERROR_JTAG_QUEUE_FAILED;
1311 }
1312 }
1313
1314 free(captured);
1315 }
1316 bit_count += cmd->fields[i].num_bits;
1317 }
1318
1319 return retval;
1320 }
1321
1322 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1323 {
1324 int retval = ERROR_OK;
1325 int num_bits = field->num_bits;
1326
1327 int compare_failed = 0;
1328
1329 if (field->in_check_mask)
1330 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1331 else
1332 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1333
1334 if (compare_failed){
1335 /* An error handler could have caught the failing check
1336 * only report a problem when there wasn't a handler, or if the handler
1337 * acknowledged the error
1338 */
1339 LOG_WARNING("TAP %s:",
1340 (field->tap == NULL) ? "(unknown)" : field->tap->dotted_name );
1341 if (compare_failed)
1342 {
1343 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1344 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1345
1346 if (field->in_check_mask)
1347 {
1348 char *in_check_mask_char;
1349 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1350 LOG_WARNING("value captured during scan didn't pass the requested check:");
1351 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
1352 captured_char, in_check_value_char, in_check_mask_char);
1353 free(in_check_mask_char);
1354 }
1355 else
1356 {
1357 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);
1358 }
1359
1360 free(captured_char);
1361 free(in_check_value_char);
1362
1363 retval = ERROR_JTAG_QUEUE_FAILED;
1364 }
1365
1366 }
1367 return retval;
1368 }
1369
1370 /*
1371 set up checking of this field using the in_handler. The values passed in must be valid until
1372 after jtag_execute() has completed.
1373 */
1374 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1375 {
1376 if (value)
1377 field->in_handler = jtag_check_value;
1378 else
1379 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1380 field->in_handler_priv = NULL;
1381 field->in_check_value = value;
1382 field->in_check_mask = mask;
1383 }
1384
1385 enum scan_type jtag_scan_type(scan_command_t *cmd)
1386 {
1387 int i;
1388 int type = 0;
1389
1390 for (i = 0; i < cmd->num_fields; i++)
1391 {
1392 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1393 type |= SCAN_IN;
1394 if (cmd->fields[i].out_value)
1395 type |= SCAN_OUT;
1396 }
1397
1398 return type;
1399 }
1400
1401 int MINIDRIVER(interface_jtag_execute_queue)(void)
1402 {
1403 int retval;
1404
1405 if (jtag==NULL)
1406 {
1407 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1408 return ERROR_FAIL;
1409 }
1410
1411 retval = jtag->execute_queue();
1412
1413 cmd_queue_free();
1414
1415 jtag_command_queue = NULL;
1416 last_comand_pointer = &jtag_command_queue;
1417
1418 return retval;
1419 }
1420
1421 int jtag_execute_queue(void)
1422 {
1423 int retval=interface_jtag_execute_queue();
1424 if (retval==ERROR_OK)
1425 {
1426 retval=jtag_error;
1427 }
1428 jtag_error=ERROR_OK;
1429 return retval;
1430 }
1431
1432 int jtag_reset_callback(enum jtag_event event, void *priv)
1433 {
1434 jtag_tap_t *tap = priv;
1435
1436 LOG_DEBUG("-");
1437
1438 if (event == JTAG_TRST_ASSERTED)
1439 {
1440 buf_set_ones(tap->cur_instr, tap->ir_length);
1441 tap->bypass = 1;
1442 }
1443
1444 return ERROR_OK;
1445 }
1446
1447 void jtag_sleep(u32 us)
1448 {
1449 alive_sleep(us/1000);
1450 }
1451
1452 /* Try to examine chain layout according to IEEE 1149.1 §12
1453 */
1454 int jtag_examine_chain(void)
1455 {
1456 jtag_tap_t *tap;
1457 scan_field_t field;
1458 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1459 int i;
1460 int bit_count;
1461 int device_count = 0;
1462 u8 zero_check = 0x0;
1463 u8 one_check = 0xff;
1464
1465 field.tap = NULL;
1466 field.num_bits = sizeof(idcode_buffer) * 8;
1467 field.out_value = idcode_buffer;
1468 field.out_mask = NULL;
1469 field.in_value = idcode_buffer;
1470 field.in_check_value = NULL;
1471 field.in_check_mask = NULL;
1472 field.in_handler = NULL;
1473 field.in_handler_priv = NULL;
1474
1475 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1476 {
1477 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1478 }
1479
1480 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1481 jtag_execute_queue();
1482
1483 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1484 {
1485 zero_check |= idcode_buffer[i];
1486 one_check &= idcode_buffer[i];
1487 }
1488
1489 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1490 if ((zero_check == 0x00) || (one_check == 0xff))
1491 {
1492 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1493 return ERROR_JTAG_INIT_FAILED;
1494 }
1495
1496 // point at the 1st tap
1497 tap = jtag_NextEnabledTap(NULL);
1498 if( tap == NULL ){
1499 LOG_ERROR("JTAG: No taps enabled?");
1500 return ERROR_JTAG_INIT_FAILED;
1501 }
1502
1503 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1504 {
1505 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1506 if ((idcode & 1) == 0)
1507 {
1508 /* LSB must not be 0, this indicates a device in bypass */
1509 LOG_WARNING("Tap/Device does not have IDCODE");
1510 idcode=0;
1511
1512 bit_count += 1;
1513 }
1514 else
1515 {
1516 u32 manufacturer;
1517 u32 part;
1518 u32 version;
1519
1520 if (idcode == 0x000000FF)
1521 {
1522 int unexpected=0;
1523 /* End of chain (invalid manufacturer ID)
1524 *
1525 * The JTAG examine is the very first thing that happens
1526 *
1527 * A single JTAG device requires only 64 bits to be read back correctly.
1528 *
1529 * The code below adds a check that the rest of the data scanned (640 bits)
1530 * are all as expected. This helps diagnose/catch problems with the JTAG chain
1531 *
1532 * earlier and gives more helpful/explicit error messages.
1533 */
1534 for (bit_count += 32; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;bit_count += 32)
1535 {
1536 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1537 if (unexpected||(idcode != 0x000000FF))
1538 {
1539 LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count, idcode);
1540 unexpected = 1;
1541 }
1542 }
1543
1544 break;
1545 }
1546
1547 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1548 manufacturer = EXTRACT_MFG(idcode);
1549 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1550 part = EXTRACT_PART(idcode);
1551 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1552 version = EXTRACT_VER(idcode);
1553
1554 LOG_INFO("JTAG tap: %s tap/device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1555 ((tap != NULL) ? (tap->dotted_name) : "(not-named)"),
1556 idcode, manufacturer, part, version);
1557
1558 bit_count += 32;
1559 }
1560 if (tap)
1561 {
1562 tap->idcode = idcode;
1563 if( tap->expected_id ){
1564 if( tap->idcode != tap->expected_id ){
1565 LOG_ERROR("ERROR: Tap: %s - Expected id: 0x%08x, Got: 0x%08x",
1566 tap->dotted_name,
1567 tap->expected_id,
1568 idcode );
1569 LOG_ERROR("ERROR: expected: mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x",
1570 EXTRACT_MFG( tap->expected_id ),
1571 EXTRACT_PART( tap->expected_id ),
1572 EXTRACT_VER( tap->expected_id ) );
1573 LOG_ERROR("ERROR: got: mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x",
1574 EXTRACT_MFG( tap->idcode ),
1575 EXTRACT_PART( tap->idcode ),
1576 EXTRACT_VER( tap->idcode ) );
1577 } else {
1578 LOG_INFO("JTAG Tap/device matched");
1579 }
1580 } else {
1581 #if 0
1582 LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
1583 tap->idcode);
1584 #endif
1585 }
1586 tap = jtag_NextEnabledTap(tap);
1587 }
1588 device_count++;
1589 }
1590
1591 /* see if number of discovered devices matches configuration */
1592 if (device_count != jtag_NumEnabledTaps())
1593 {
1594 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
1595 device_count, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
1596 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1597 return ERROR_JTAG_INIT_FAILED;
1598 }
1599
1600 return ERROR_OK;
1601 }
1602
1603 int jtag_validate_chain(void)
1604 {
1605 jtag_tap_t *tap;
1606 int total_ir_length = 0;
1607 u8 *ir_test = NULL;
1608 scan_field_t field;
1609 int chain_pos = 0;
1610
1611 tap = NULL;
1612 total_ir_length = 0;
1613 for(;;){
1614 tap = jtag_NextEnabledTap(tap);
1615 if( tap == NULL ){
1616 break;
1617 }
1618 total_ir_length += tap->ir_length;
1619 }
1620
1621 total_ir_length += 2;
1622 ir_test = malloc(CEIL(total_ir_length, 8));
1623 buf_set_ones(ir_test, total_ir_length);
1624
1625 field.tap = NULL;
1626 field.num_bits = total_ir_length;
1627 field.out_value = ir_test;
1628 field.out_mask = NULL;
1629 field.in_value = ir_test;
1630 field.in_check_value = NULL;
1631 field.in_check_mask = NULL;
1632 field.in_handler = NULL;
1633 field.in_handler_priv = NULL;
1634
1635 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1636 jtag_execute_queue();
1637
1638 tap = NULL;
1639 chain_pos = 0;
1640 for(;;){
1641 tap = jtag_NextEnabledTap(tap);
1642 if( tap == NULL ){
1643 break;
1644 }
1645
1646
1647 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1648 {
1649 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1650 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1651 free(cbuf);
1652 free(ir_test);
1653 return ERROR_JTAG_INIT_FAILED;
1654 }
1655 chain_pos += tap->ir_length;
1656 }
1657
1658 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1659 {
1660 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1661 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1662 free(cbuf);
1663 free(ir_test);
1664 return ERROR_JTAG_INIT_FAILED;
1665 }
1666
1667 free(ir_test);
1668
1669 return ERROR_OK;
1670 }
1671
1672
1673 static int
1674 jim_newtap_cmd( Jim_GetOptInfo *goi )
1675 {
1676 jtag_tap_t *pTap;
1677 jtag_tap_t **ppTap;
1678 jim_wide w;
1679 int x;
1680 int e;
1681 int reqbits;
1682 Jim_Nvp *n;
1683 char *cp;
1684 const Jim_Nvp opts[] = {
1685 #define NTAP_OPT_IRLEN 0
1686 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
1687 #define NTAP_OPT_IRMASK 1
1688 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
1689 #define NTAP_OPT_IRCAPTURE 2
1690 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
1691 #define NTAP_OPT_ENABLED 3
1692 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
1693 #define NTAP_OPT_DISABLED 4
1694 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
1695 #define NTAP_OPT_EXPECTED_ID 5
1696 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
1697 { .name = NULL , .value = -1 },
1698 };
1699
1700
1701 pTap = malloc( sizeof(jtag_tap_t) );
1702 memset( pTap, 0, sizeof(*pTap) );
1703 if( !pTap ){
1704 Jim_SetResult_sprintf( goi->interp, "no memory");
1705 return JIM_ERR;
1706 }
1707 //
1708 // we expect CHIP + TAP + OPTIONS
1709 //
1710 if( goi->argc < 3 ){
1711 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
1712 return JIM_ERR;
1713 }
1714 Jim_GetOpt_String( goi, &cp, NULL );
1715 pTap->chip = strdup(cp);
1716
1717 Jim_GetOpt_String( goi, &cp, NULL );
1718 pTap->tapname = strdup(cp);
1719
1720 // name + dot + name + null
1721 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
1722 cp = malloc( x );
1723 sprintf( cp, "%s.%s", pTap->chip, pTap->tapname );
1724 pTap->dotted_name = cp;
1725
1726 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
1727 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
1728
1729
1730 // default is enabled
1731 pTap->enabled = 1;
1732
1733 // deal with options
1734 #define NTREQ_IRLEN 1
1735 #define NTREQ_IRCAPTURE 2
1736 #define NTREQ_IRMASK 4
1737
1738 // clear them as we find them
1739 reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
1740
1741 while( goi->argc ){
1742 e = Jim_GetOpt_Nvp( goi, opts, &n );
1743 if( e != JIM_OK ){
1744 Jim_GetOpt_NvpUnknown( goi, opts, 0 );
1745 return e;
1746 }
1747 LOG_DEBUG("Processing option: %s", n->name );
1748 switch( n->value ){
1749 case NTAP_OPT_ENABLED:
1750 pTap->enabled = 1;
1751 break;
1752 case NTAP_OPT_DISABLED:
1753 pTap->enabled = 0;
1754 break;
1755 case NTAP_OPT_EXPECTED_ID:
1756 e = Jim_GetOpt_Wide( goi, &w );
1757 pTap->expected_id = w;
1758 break;
1759 case NTAP_OPT_IRLEN:
1760 case NTAP_OPT_IRMASK:
1761 case NTAP_OPT_IRCAPTURE:
1762 e = Jim_GetOpt_Wide( goi, &w );
1763 if( e != JIM_OK ){
1764 Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
1765 return e;
1766 }
1767 if( (w < 0) || (w > 0xffff) ){
1768 // wacky value
1769 Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
1770 n->name, (int)(w), (int)(w));
1771 return JIM_ERR;
1772 }
1773 switch(n->value){
1774 case NTAP_OPT_IRLEN:
1775 pTap->ir_length = w;
1776 reqbits &= (~(NTREQ_IRLEN));
1777 break;
1778 case NTAP_OPT_IRMASK:
1779 pTap->ir_capture_mask = w;
1780 reqbits &= (~(NTREQ_IRMASK));
1781 break;
1782 case NTAP_OPT_IRCAPTURE:
1783 pTap->ir_capture_value = w;
1784 reqbits &= (~(NTREQ_IRCAPTURE));
1785 break;
1786 }
1787 } // switch(n->value)
1788 } // while( goi->argc )
1789
1790 // Did we get all the options?
1791 if( reqbits ){
1792 // no
1793 Jim_SetResult_sprintf( goi->interp,
1794 "newtap: %s missing required parameters",
1795 pTap->dotted_name);
1796 // fixme: Tell user what is missing :-(
1797 // no memory leaks pelase
1798 free(((void *)(pTap->chip)));
1799 free(((void *)(pTap->tapname)));
1800 free(((void *)(pTap->dotted_name)));
1801 free(((void *)(pTap)));
1802 return JIM_ERR;
1803 }
1804
1805 pTap->expected = malloc( pTap->ir_length );
1806 pTap->expected_mask = malloc( pTap->ir_length );
1807 pTap->cur_instr = malloc( pTap->ir_length );
1808
1809 buf_set_u32( pTap->expected,
1810 0,
1811 pTap->ir_length,
1812 pTap->ir_capture_value );
1813 buf_set_u32( pTap->expected_mask,
1814 0,
1815 pTap->ir_length,
1816 pTap->ir_capture_mask );
1817 buf_set_ones( pTap->cur_instr,
1818 pTap->ir_length );
1819
1820 pTap->bypass = 1;
1821
1822
1823 jtag_register_event_callback(jtag_reset_callback, pTap );
1824
1825 ppTap = &(jtag_all_taps);
1826 while( (*ppTap) != NULL ){
1827 ppTap = &((*ppTap)->next_tap);
1828 }
1829 *ppTap = pTap;
1830 {
1831 static int n_taps = 0;
1832 pTap->abs_chain_position = n_taps++;
1833 }
1834 LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
1835 (*ppTap)->dotted_name,
1836 (*ppTap)->abs_chain_position,
1837 (*ppTap)->ir_length,
1838 (*ppTap)->ir_capture_value,
1839 (*ppTap)->ir_capture_mask );
1840
1841
1842 return ERROR_OK;
1843 }
1844
1845
1846 static int
1847 jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
1848 {
1849 Jim_GetOptInfo goi;
1850 int e;
1851 Jim_Nvp *n;
1852 struct command_context_s *context;
1853
1854 enum {
1855 JTAG_CMD_INTERFACE,
1856 JTAG_CMD_INIT_RESET,
1857 JTAG_CMD_NEWTAP,
1858 JTAG_CMD_TAPENABLE,
1859 JTAG_CMD_TAPDISABLE,
1860 JTAG_CMD_TAPISENABLED
1861 };
1862
1863 const Jim_Nvp jtag_cmds[] = {
1864 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
1865 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
1866 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
1867 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
1868 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
1869 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
1870
1871 { .name = NULL, .value = -1 },
1872 };
1873
1874 context = Jim_GetAssocData(interp, "context");
1875 // go past the command
1876 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
1877
1878 e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
1879 if( e != JIM_OK ){
1880 Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
1881 return e;
1882 }
1883 Jim_SetEmptyResult( goi.interp );
1884 switch( n->value ){
1885 case JTAG_CMD_INTERFACE:
1886 // return the name of the interface
1887 // TCL code might need to know the exact type...
1888 // FUTURE: we allow this as a means to "set" the interface.
1889 if( goi.argc != 0 ){
1890 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
1891 return JIM_ERR;
1892 }
1893 Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
1894 return JIM_OK;
1895 case JTAG_CMD_INIT_RESET:
1896 if( goi.argc != 0 ){
1897 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
1898 return JIM_ERR;
1899 }
1900 e = jtag_init_reset(context);
1901 if( e != ERROR_OK ){
1902 Jim_SetResult_sprintf( goi.interp, "error: %d", e);
1903 return JIM_ERR;
1904 }
1905 return JIM_OK;
1906 case JTAG_CMD_NEWTAP:
1907 return jim_newtap_cmd( &goi );
1908 break;
1909 case JTAG_CMD_TAPISENABLED:
1910 case JTAG_CMD_TAPENABLE:
1911 case JTAG_CMD_TAPDISABLE:
1912 if( goi.argc != 1 ){
1913 Jim_SetResultString( goi.interp, "Too many parameters",-1 );
1914 return JIM_ERR;
1915 }
1916
1917 {
1918 jtag_tap_t *t;
1919 t = jtag_TapByJimObj( goi.interp, goi.argv[0] );
1920 if( t == NULL ){
1921 return JIM_ERR;
1922 }
1923 switch( n->value ){
1924 case JTAG_CMD_TAPISENABLED:
1925 // below
1926 break;
1927 case JTAG_CMD_TAPENABLE:
1928 e = 1;
1929 t->enabled = e;
1930 break;
1931 case JTAG_CMD_TAPDISABLE:
1932 e = 0;
1933 t->enabled = e;
1934 break;
1935 }
1936 Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
1937 return JIM_OK;
1938 }
1939 }
1940
1941
1942 return JIM_ERR;
1943 }
1944
1945 int jtag_register_commands(struct command_context_s *cmd_ctx)
1946 {
1947 register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
1948
1949 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1950 COMMAND_CONFIG, "try to configure interface");
1951 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1952 COMMAND_ANY, "set jtag speed (if supported)");
1953 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1954 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1955 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1956 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1957 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1958 COMMAND_CONFIG, NULL);
1959 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1960 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1961 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1962 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1963
1964 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1965 COMMAND_EXEC, "print current scan chain configuration");
1966
1967 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1968 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1969 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1970 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1971 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1972 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1973 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1974 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1975 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
1976
1977 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1978 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1979 return ERROR_OK;
1980 }
1981
1982 int jtag_interface_init(struct command_context_s *cmd_ctx)
1983 {
1984 if (jtag)
1985 return ERROR_OK;
1986
1987 if (!jtag_interface)
1988 {
1989 /* nothing was previously specified by "interface" command */
1990 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1991 return ERROR_JTAG_INVALID_INTERFACE;
1992 }
1993 if(hasKHz)
1994 {
1995 jtag_interface->khz(speed_khz, &jtag_speed);
1996 hasKHz = 0;
1997 }
1998
1999 if (jtag_interface->init() != ERROR_OK)
2000 return ERROR_JTAG_INIT_FAILED;
2001
2002
2003
2004 jtag = jtag_interface;
2005 return ERROR_OK;
2006 }
2007
2008 static int jtag_init_inner(struct command_context_s *cmd_ctx)
2009 {
2010 jtag_tap_t *tap;
2011 int retval;
2012
2013 LOG_DEBUG("Init JTAG chain");
2014
2015
2016 tap = jtag_NextEnabledTap(NULL);
2017 if( tap == NULL ){
2018 LOG_ERROR("There are no enabled taps?");
2019 return ERROR_JTAG_INIT_FAILED;
2020 }
2021
2022 jtag_add_tlr();
2023 if ((retval=jtag_execute_queue())!=ERROR_OK)
2024 return retval;
2025
2026 /* examine chain first, as this could discover the real chain layout */
2027 if (jtag_examine_chain() != ERROR_OK)
2028 {
2029 LOG_ERROR("trying to validate configured JTAG chain anyway...");
2030 }
2031
2032 if (jtag_validate_chain() != ERROR_OK)
2033 {
2034 LOG_ERROR("Could not validate JTAG chain, continuing anyway...");
2035 }
2036
2037 return ERROR_OK;
2038 }
2039
2040 int jtag_init_reset(struct command_context_s *cmd_ctx)
2041 {
2042 int retval;
2043
2044 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
2045 return retval;
2046
2047 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
2048
2049 /* Reset can happen after a power cycle.
2050 *
2051 * Ideally we would only assert TRST or run TLR before the target reset.
2052 *
2053 * However w/srst_pulls_trst, trst is asserted together with the target
2054 * reset whether we want it or not.
2055 *
2056 * NB! Some targets have JTAG circuitry disabled until a
2057 * trst & srst has been asserted.
2058 *
2059 * NB! here we assume nsrst/ntrst delay are sufficient!
2060 *
2061 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
2062 *
2063 */
2064 jtag_add_reset(1, 0); /* TLR or TRST */
2065 if (jtag_reset_config & RESET_HAS_SRST)
2066 {
2067 jtag_add_reset(1, 1);
2068 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
2069 jtag_add_reset(0, 1);
2070 }
2071 jtag_add_reset(0, 0);
2072 if ((retval = jtag_execute_queue()) != ERROR_OK)
2073 return retval;
2074
2075 /* Check that we can communication on the JTAG chain + eventually we want to
2076 * be able to perform enumeration only after OpenOCD has started
2077 * telnet and GDB server
2078 *
2079 * That would allow users to more easily perform any magic they need to before
2080 * reset happens.
2081 */
2082 return jtag_init_inner(cmd_ctx);
2083 }
2084
2085 int jtag_init(struct command_context_s *cmd_ctx)
2086 {
2087 int retval;
2088 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
2089 return retval;
2090 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
2091 {
2092 return ERROR_OK;
2093 }
2094 return jtag_init_reset(cmd_ctx);
2095 }
2096
2097 static int default_khz(int khz, int *jtag_speed)
2098 {
2099 LOG_ERROR("Translation from khz to jtag_speed not implemented");
2100 return ERROR_FAIL;
2101 }
2102
2103 static int default_speed_div(int speed, int *khz)
2104 {
2105 LOG_ERROR("Translation from jtag_speed to khz not implemented");
2106 return ERROR_FAIL;
2107 }
2108
2109 static int default_power_dropout(int *dropout)
2110 {
2111 *dropout=0; /* by default we can't detect power dropout */
2112 return ERROR_OK;
2113 }
2114
2115 static int default_srst_asserted(int *srst_asserted)
2116 {
2117 *srst_asserted=0; /* by default we can't detect srst asserted */
2118 return ERROR_OK;
2119 }
2120
2121 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2122 {
2123 int i;
2124 int retval;
2125
2126 /* check whether the interface is already configured */
2127 if (jtag_interface)
2128 {
2129 LOG_WARNING("Interface already configured, ignoring");
2130 return ERROR_OK;
2131 }
2132
2133 /* interface name is a mandatory argument */
2134 if (argc < 1 || args[0][0] == '\0')
2135 {
2136 return ERROR_COMMAND_SYNTAX_ERROR;
2137 }
2138
2139 for (i=0; jtag_interfaces[i]; i++)
2140 {
2141 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
2142 {
2143 if ((retval = jtag_interfaces[i]->register_commands(cmd_ctx)) != ERROR_OK)
2144 {
2145 return retval;
2146 }
2147
2148 jtag_interface = jtag_interfaces[i];
2149
2150 if (jtag_interface->khz == NULL)
2151 {
2152 jtag_interface->khz = default_khz;
2153 }
2154 if (jtag_interface->speed_div == NULL)
2155 {
2156 jtag_interface->speed_div = default_speed_div;
2157 }
2158 if (jtag_interface->power_dropout == NULL)
2159 {
2160 jtag_interface->power_dropout = default_power_dropout;
2161 }
2162 if (jtag_interface->srst_asserted == NULL)
2163 {
2164 jtag_interface->srst_asserted = default_srst_asserted;
2165 }
2166
2167 return ERROR_OK;
2168 }
2169 }
2170
2171 /* no valid interface was found (i.e. the configuration option,
2172 * didn't match one of the compiled-in interfaces
2173 */
2174 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
2175 LOG_ERROR("compiled-in jtag interfaces:");
2176 for (i = 0; jtag_interfaces[i]; i++)
2177 {
2178 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
2179 }
2180
2181 return ERROR_JTAG_INVALID_INTERFACE;
2182 }
2183
2184 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2185 {
2186 int e;
2187 char buf[1024];
2188 Jim_Obj *newargs[ 10 ];
2189 //
2190 // CONVERT SYNTAX
2191 //
2192 // argv[-1] = command
2193 // argv[ 0] = ir length
2194 // argv[ 1] = ir capture
2195 // argv[ 2] = ir mask
2196 // argv[ 3] = not actually used by anything but in the docs
2197
2198 if( argc < 4 ){
2199 command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
2200 return ERROR_OK;
2201 }
2202 command_print( cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
2203 command_print( cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
2204 args[0],
2205 args[1],
2206 args[2] );
2207 command_print( cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundryscan(len5)");
2208 command_print( cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
2209 command_print( cmd_ctx, "jtag newtap stm32 boundry ....., and the tap: \"stm32.boundery\"");
2210 command_print( cmd_ctx, "And then refer to the taps by the dotted name.");
2211
2212
2213
2214 newargs[0] = Jim_NewStringObj( interp, "jtag", -1 );
2215 newargs[1] = Jim_NewStringObj( interp, "newtap", -1 );
2216 sprintf( buf, "chip%d", jtag_NumTotalTaps() );
2217 newargs[2] = Jim_NewStringObj( interp, buf, -1 );
2218 sprintf( buf, "tap%d", jtag_NumTotalTaps() );
2219 newargs[3] = Jim_NewStringObj( interp, buf, -1 );
2220 newargs[4] = Jim_NewStringObj( interp, "-irlen", -1 );
2221 newargs[5] = Jim_NewStringObj( interp, args[0], -1 );
2222 newargs[6] = Jim_NewStringObj( interp, "-ircapture", -1 );
2223 newargs[7] = Jim_NewStringObj( interp, args[1], -1 );
2224 newargs[8] = Jim_NewStringObj( interp, "-irmask", -1 );
2225 newargs[9] = Jim_NewStringObj( interp, args[2], -1 );
2226
2227 command_print( cmd_ctx, "NEW COMMAND:");
2228 sprintf( buf, "%s %s %s %s %s %s %s %s %s %s",
2229 Jim_GetString( newargs[0], NULL ),
2230 Jim_GetString( newargs[1], NULL ),
2231 Jim_GetString( newargs[2], NULL ),
2232 Jim_GetString( newargs[3], NULL ),
2233 Jim_GetString( newargs[4], NULL ),
2234 Jim_GetString( newargs[5], NULL ),
2235 Jim_GetString( newargs[6], NULL ),
2236 Jim_GetString( newargs[7], NULL ),
2237 Jim_GetString( newargs[8], NULL ),
2238 Jim_GetString( newargs[9], NULL ) );
2239
2240
2241
2242 e = jim_jtag_command( interp, 10, newargs );
2243 if( e != JIM_OK ){
2244 command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
2245 }
2246 return e;
2247 }
2248
2249
2250 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2251 {
2252 jtag_tap_t *tap;
2253
2254 tap = jtag_all_taps;
2255 command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
2256 command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
2257
2258 while( tap ){
2259 u32 expected, expected_mask, cur_instr;
2260 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
2261 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
2262 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
2263 command_print(cmd_ctx,
2264 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
2265 tap->abs_chain_position,
2266 tap->dotted_name,
2267 tap->enabled ? 'Y' : 'n',
2268 tap->idcode,
2269 tap->expected_id,
2270 tap->ir_length,
2271 expected,
2272 expected_mask,
2273 cur_instr);
2274 tap = tap->next_tap;
2275 }
2276
2277 return ERROR_OK;
2278 }
2279
2280 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2281 {
2282 if (argc < 1)
2283 return ERROR_COMMAND_SYNTAX_ERROR;
2284
2285 if (argc >= 1)
2286 {
2287 if (strcmp(args[0], "none") == 0)
2288 jtag_reset_config = RESET_NONE;
2289 else if (strcmp(args[0], "trst_only") == 0)
2290 jtag_reset_config = RESET_HAS_TRST;
2291 else if (strcmp(args[0], "srst_only") == 0)
2292 jtag_reset_config = RESET_HAS_SRST;
2293 else if (strcmp(args[0], "trst_and_srst") == 0)
2294 jtag_reset_config = RESET_TRST_AND_SRST;
2295 else
2296 {
2297 LOG_ERROR("(1) invalid reset_config argument (%s), defaulting to none", args[0]);
2298 jtag_reset_config = RESET_NONE;
2299 return ERROR_INVALID_ARGUMENTS;
2300 }
2301 }
2302
2303 if (argc >= 2)
2304 {
2305 if (strcmp(args[1], "separate") == 0)
2306 {
2307 /* seperate reset lines - default */
2308 } else
2309 {
2310 if (strcmp(args[1], "srst_pulls_trst") == 0)
2311 jtag_reset_config |= RESET_SRST_PULLS_TRST;
2312 else if (strcmp(args[1], "trst_pulls_srst") == 0)
2313 jtag_reset_config |= RESET_TRST_PULLS_SRST;
2314 else if (strcmp(args[1], "combined") == 0)
2315 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
2316 else
2317 {
2318 LOG_ERROR("(2) invalid reset_config argument (%s), defaulting to none", args[1]);
2319 jtag_reset_config = RESET_NONE;
2320 return ERROR_INVALID_ARGUMENTS;
2321 }
2322 }
2323 }
2324
2325 if (argc >= 3)
2326 {
2327 if (strcmp(args[2], "trst_open_drain") == 0)
2328 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
2329 else if (strcmp(args[2], "trst_push_pull") == 0)
2330 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
2331 else
2332 {
2333 LOG_ERROR("(3) invalid reset_config argument (%s) defaulting to none", args[2] );
2334 jtag_reset_config = RESET_NONE;
2335 return ERROR_INVALID_ARGUMENTS;
2336 }
2337 }
2338
2339 if (argc >= 4)
2340 {
2341 if (strcmp(args[3], "srst_push_pull") == 0)
2342 jtag_reset_config |= RESET_SRST_PUSH_PULL;
2343 else if (strcmp(args[3], "srst_open_drain") == 0)
2344 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
2345 else
2346 {
2347 LOG_ERROR("(4) invalid reset_config argument (%s), defaulting to none", args[3]);
2348 jtag_reset_config = RESET_NONE;
2349 return ERROR_INVALID_ARGUMENTS;
2350 }
2351 }
2352
2353 return ERROR_OK;
2354 }
2355
2356 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2357 {
2358 if (argc < 1)
2359 {
2360 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
2361 exit(-1);
2362 }
2363 else
2364 {
2365 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
2366 }
2367
2368 return ERROR_OK;
2369 }
2370
2371 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2372 {
2373 if (argc < 1)
2374 {
2375 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
2376 exit(-1);
2377 }
2378 else
2379 {
2380 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
2381 }
2382
2383 return ERROR_OK;
2384 }
2385
2386 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2387 {
2388 int retval=ERROR_OK;
2389
2390 if (argc == 1)
2391 {
2392 LOG_DEBUG("handle jtag speed");
2393
2394 int cur_speed = 0;
2395 cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
2396
2397 /* this command can be called during CONFIG,
2398 * in which case jtag isn't initialized */
2399 if (jtag)
2400 {
2401 retval=jtag->speed(cur_speed);
2402 }
2403 } else if (argc == 0)
2404 {
2405 } else
2406 {
2407 return ERROR_COMMAND_SYNTAX_ERROR;
2408 }
2409 command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
2410
2411 return retval;
2412 }
2413
2414 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2415 {
2416 int retval=ERROR_OK;
2417 LOG_DEBUG("handle jtag khz");
2418
2419 if(argc == 1)
2420 {
2421 speed_khz = strtoul(args[0], NULL, 0);
2422 if (jtag != NULL)
2423 {
2424 int cur_speed = 0;
2425 LOG_DEBUG("have interface set up");
2426 int speed_div1;
2427 if ((retval=jtag->khz(speed_khz, &speed_div1))!=ERROR_OK)
2428 {
2429 speed_khz = 0;
2430 return retval;
2431 }
2432
2433 cur_speed = jtag_speed = speed_div1;
2434
2435 retval=jtag->speed(cur_speed);
2436 } else
2437 {
2438 hasKHz = 1;
2439 }
2440 } else if (argc==0)
2441 {
2442 } else
2443 {
2444 return ERROR_COMMAND_SYNTAX_ERROR;
2445 }
2446
2447 if (jtag!=NULL)
2448 {
2449 if ((retval=jtag->speed_div(jtag_speed, &speed_khz))!=ERROR_OK)
2450 return retval;
2451 }
2452
2453 if (speed_khz==0)
2454 {
2455 command_print(cmd_ctx, "RCLK - adaptive");
2456 } else
2457 {
2458 command_print(cmd_ctx, "%d kHz", speed_khz);
2459 }
2460 return retval;
2461
2462 }
2463
2464 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2465 {
2466 enum tap_state state;
2467
2468 if (argc < 1)
2469 {
2470 return ERROR_COMMAND_SYNTAX_ERROR;
2471 }
2472 else
2473 {
2474 for (state = 0; state < 16; state++)
2475 {
2476 if (strcmp(args[0], tap_state_strings[state]) == 0)
2477 {
2478 jtag_add_end_state(state);
2479 jtag_execute_queue();
2480 }
2481 }
2482 }
2483 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
2484
2485 return ERROR_OK;
2486 }
2487
2488 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2489 {
2490 int trst = -1;
2491 int srst = -1;
2492
2493 if (argc < 2)
2494 {
2495 return ERROR_COMMAND_SYNTAX_ERROR;
2496 }
2497
2498 if (args[0][0] == '1')
2499 trst = 1;
2500 else if (args[0][0] == '0')
2501 trst = 0;
2502 else
2503 {
2504 return ERROR_COMMAND_SYNTAX_ERROR;
2505 }
2506
2507 if (args[1][0] == '1')
2508 srst = 1;
2509 else if (args[1][0] == '0')
2510 srst = 0;
2511 else
2512 {
2513 return ERROR_COMMAND_SYNTAX_ERROR;
2514 }
2515
2516 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
2517 return ERROR_JTAG_INIT_FAILED;
2518
2519 jtag_add_reset(trst, srst);
2520 jtag_execute_queue();
2521
2522 return ERROR_OK;
2523 }
2524
2525 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2526 {
2527 if (argc < 1)
2528 {
2529 return ERROR_COMMAND_SYNTAX_ERROR;
2530 }
2531
2532 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
2533 jtag_execute_queue();
2534
2535 return ERROR_OK;
2536
2537 }
2538
2539 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2540 {
2541 int i;
2542 scan_field_t *fields;
2543 jtag_tap_t *tap;
2544
2545 if ((argc < 2) || (argc % 2))
2546 {
2547 return ERROR_COMMAND_SYNTAX_ERROR;
2548 }
2549
2550 fields = malloc(sizeof(scan_field_t) * argc / 2);
2551
2552 for (i = 0; i < argc / 2; i++)
2553 {
2554 tap = jtag_TapByString( args[i*2] );
2555 if (tap==NULL)
2556 {
2557 command_print( cmd_ctx, "Tap: %s unknown", args[i*2] );
2558 return ERROR_FAIL;
2559 }
2560 int field_size = tap->ir_length;
2561 fields[i].tap = tap;
2562 fields[i].out_value = malloc(CEIL(field_size, 8));
2563 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
2564 fields[i].out_mask = NULL;
2565 fields[i].in_value = NULL;
2566 fields[i].in_check_mask = NULL;
2567 fields[i].in_handler = NULL;
2568 fields[i].in_handler_priv = NULL;
2569 }
2570
2571 jtag_add_ir_scan(argc / 2, fields, -1);
2572 jtag_execute_queue();
2573
2574 for (i = 0; i < argc / 2; i++)
2575 free(fields[i].out_value);
2576
2577 free (fields);
2578
2579 return ERROR_OK;
2580 }
2581
2582 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2583 {
2584 int retval;
2585 scan_field_t *fields;
2586 int num_fields;
2587 int field_count = 0;
2588 int i, e;
2589 jtag_tap_t *tap;
2590
2591 /* args[1] = device
2592 * args[2] = num_bits
2593 * args[3] = hex string
2594 * ... repeat num bits and hex string ...
2595 */
2596 if ((argc < 4) || ((argc % 2)!=0))
2597 {
2598 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
2599 return JIM_ERR;
2600 }
2601
2602 for (i = 2; i < argc; i+=2)
2603 {
2604 long bits;
2605
2606 e = Jim_GetLong(interp, args[i], &bits);
2607 if (e != JIM_OK)
2608 return e;
2609 }
2610
2611 tap = jtag_TapByJimObj( interp, args[1] );
2612 if( tap == NULL ){
2613 return JIM_ERR;
2614 }
2615
2616 num_fields=(argc-2)/2;
2617 fields = malloc(sizeof(scan_field_t) * num_fields);
2618 for (i = 2; i < argc; i+=2)
2619 {
2620 long bits;
2621 int len;
2622 const char *str;
2623
2624 Jim_GetLong(interp, args[i], &bits);
2625 str = Jim_GetString(args[i+1], &len);
2626
2627
2628 fields[field_count].tap = tap;
2629 fields[field_count].num_bits = bits;
2630 fields[field_count].out_value = malloc(CEIL(bits, 8));
2631 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
2632 fields[field_count].out_mask = NULL;
2633 fields[field_count].in_value = fields[field_count].out_value;
2634 fields[field_count].in_check_mask = NULL;
2635 fields[field_count].in_check_value = NULL;
2636 fields[field_count].in_handler = NULL;
2637 fields[field_count++].in_handler_priv = NULL;
2638 }
2639
2640 jtag_add_dr_scan(num_fields, fields, -1);
2641 retval = jtag_execute_queue();
2642 if (retval != ERROR_OK)
2643 {
2644 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
2645 return JIM_ERR;
2646 }
2647
2648 field_count=0;
2649 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
2650 for (i = 2; i < argc; i+=2)
2651 {
2652 long bits;
2653 char *str;
2654
2655 Jim_GetLong(interp, args[i], &bits);
2656 str = buf_to_str(fields[field_count].in_value, bits, 16);
2657 free(fields[field_count].out_value);
2658
2659 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
2660 free(str);
2661 field_count++;
2662 }
2663
2664 Jim_SetResult(interp, list);
2665
2666 free(fields);
2667
2668 return JIM_OK;
2669 }
2670
2671 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2672 {
2673 if (argc == 1)
2674 {
2675 if (strcmp(args[0], "enable") == 0)
2676 {
2677 jtag_verify_capture_ir = 1;
2678 }
2679 else if (strcmp(args[0], "disable") == 0)
2680 {
2681 jtag_verify_capture_ir = 0;
2682 } else
2683 {
2684 return ERROR_COMMAND_SYNTAX_ERROR;
2685 }
2686 } else if (argc != 0)
2687 {
2688 return ERROR_COMMAND_SYNTAX_ERROR;
2689 }
2690
2691 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
2692
2693 return ERROR_OK;
2694 }
2695
2696
2697 int jtag_power_dropout(int *dropout)
2698 {
2699 return jtag->power_dropout(dropout);
2700 }
2701
2702 int jtag_srst_asserted(int *srst_asserted)
2703 {
2704 return jtag->srst_asserted(srst_asserted);
2705 }
2706