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