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

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)