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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)