1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
30 #include "interpreter.h"
37 /* note that this is not marked as static as it must be available from outside jtag.c for those
38 that implement the jtag_xxx() minidriver layer
40 int jtag_error
=ERROR_OK
;
43 char* tap_state_strings
[16] =
46 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
48 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
51 typedef struct cmd_queue_page_s
55 struct cmd_queue_page_s
*next
;
58 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
59 static cmd_queue_page_t
*cmd_queue_pages
= NULL
;
61 /* tap_move[i][j]: tap movement command to go from state i to state j
69 * SD->SD and SI->SI have to be caught in interface specific code
73 /* TLR RTI SD PD SI PI */
74 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
75 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
76 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
77 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
78 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
79 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
82 int tap_move_map
[16] = {
83 0, -1, -1, 2, -1, 3, -1, -1,
84 1, -1, -1, 4, -1, 5, -1, -1
87 tap_transition_t tap_transitions
[16] =
89 {TAP_TLR
, TAP_RTI
}, /* TLR */
90 {TAP_SIS
, TAP_CD
}, /* SDS */
91 {TAP_E1D
, TAP_SD
}, /* CD */
92 {TAP_E1D
, TAP_SD
}, /* SD */
93 {TAP_UD
, TAP_PD
}, /* E1D */
94 {TAP_E2D
, TAP_PD
}, /* PD */
95 {TAP_UD
, TAP_SD
}, /* E2D */
96 {TAP_SDS
, TAP_RTI
}, /* UD */
97 {TAP_SDS
, TAP_RTI
}, /* RTI */
98 {TAP_TLR
, TAP_CI
}, /* SIS */
99 {TAP_E1I
, TAP_SI
}, /* CI */
100 {TAP_E1I
, TAP_SI
}, /* SI */
101 {TAP_UI
, TAP_PI
}, /* E1I */
102 {TAP_E2I
, TAP_PI
}, /* PI */
103 {TAP_UI
, TAP_SI
}, /* E2I */
104 {TAP_SDS
, TAP_RTI
} /* UI */
107 char* jtag_event_strings
[] =
109 "JTAG controller reset(tms or TRST)"
112 enum tap_state end_state
= TAP_TLR
;
113 enum tap_state cur_state
= TAP_TLR
;
117 jtag_command_t
*jtag_command_queue
= NULL
;
118 jtag_command_t
**last_comand_pointer
= &jtag_command_queue
;
119 jtag_device_t
*jtag_devices
= NULL
;
120 int jtag_num_devices
= 0;
121 int jtag_ir_scan_size
= 0;
122 enum reset_types jtag_reset_config
= RESET_NONE
;
123 enum tap_state cmd_queue_end_state
= TAP_TLR
;
124 enum tap_state cmd_queue_cur_state
= TAP_TLR
;
126 int jtag_verify_capture_ir
= 1;
128 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
129 int jtag_nsrst_delay
= 0; /* default to no nSRST delay */
130 int jtag_ntrst_delay
= 0; /* default to no nTRST delay */
132 /* maximum number of JTAG devices expected in the chain
134 #define JTAG_MAX_CHAIN_SIZE 20
136 /* callbacks to inform high-level handlers about JTAG state changes */
137 jtag_event_callback_t
*jtag_event_callbacks
;
139 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
142 #if BUILD_ECOSBOARD == 1
143 extern jtag_interface_t eCosBoard_interface
;
146 #if BUILD_PARPORT == 1
147 extern jtag_interface_t parport_interface
;
150 #if BUILD_FT2232_FTD2XX == 1
151 extern jtag_interface_t ft2232_interface
;
154 #if BUILD_FT2232_LIBFTDI == 1
155 extern jtag_interface_t ft2232_interface
;
158 #if BUILD_AMTJTAGACCEL == 1
159 extern jtag_interface_t amt_jtagaccel_interface
;
162 #if BUILD_EP93XX == 1
163 extern jtag_interface_t ep93xx_interface
;
166 #if BUILD_AT91RM9200 == 1
167 extern jtag_interface_t at91rm9200_interface
;
170 #if BUILD_GW16012 == 1
171 extern jtag_interface_t gw16012_interface
;
174 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
175 extern jtag_interface_t presto_interface
;
178 #if BUILD_USBPROG == 1
179 extern jtag_interface_t usbprog_interface
;
182 jtag_interface_t
*jtag_interfaces
[] = {
183 #if BUILD_ECOSBOARD == 1
184 &eCosBoard_interface
,
186 #if BUILD_PARPORT == 1
189 #if BUILD_FT2232_FTD2XX == 1
192 #if BUILD_FT2232_LIBFTDI == 1
195 #if BUILD_AMTJTAGACCEL == 1
196 &amt_jtagaccel_interface
,
198 #if BUILD_EP93XX == 1
201 #if BUILD_AT91RM9200 == 1
202 &at91rm9200_interface
,
204 #if BUILD_GW16012 == 1
207 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
210 #if BUILD_USBPROG == 1
216 jtag_interface_t
*jtag
= NULL
;
219 jtag_interface_t
*jtag_interface
= NULL
;
221 int jtag_speed_post_reset
= 0;
224 /* forward declarations */
225 void jtag_add_pathmove(int num_states
, enum tap_state
*path
);
226 void jtag_add_runtest(int num_cycles
, enum tap_state endstate
);
227 void jtag_add_reset(int trst
, int srst
);
228 void jtag_add_end_state(enum tap_state endstate
);
229 void jtag_add_sleep(u32 us
);
230 int jtag_execute_queue(void);
231 int jtag_cancel_queue(void);
234 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
235 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
236 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
237 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
238 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
239 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
240 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
242 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
244 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
245 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
246 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
247 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
248 int handle_drscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
250 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
252 int jtag_register_event_callback(int (*callback
)(enum jtag_event event
, void *priv
), void *priv
)
254 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
256 if (callback
== NULL
)
258 return ERROR_INVALID_ARGUMENTS
;
263 while ((*callbacks_p
)->next
)
264 callbacks_p
= &((*callbacks_p
)->next
);
265 callbacks_p
= &((*callbacks_p
)->next
);
268 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
269 (*callbacks_p
)->callback
= callback
;
270 (*callbacks_p
)->priv
= priv
;
271 (*callbacks_p
)->next
= NULL
;
276 int jtag_unregister_event_callback(int (*callback
)(enum jtag_event event
, void *priv
))
278 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
280 if (callback
== NULL
)
282 return ERROR_INVALID_ARGUMENTS
;
287 jtag_event_callback_t
**next
= &((*callbacks_p
)->next
);
288 if ((*callbacks_p
)->callback
== callback
)
291 *callbacks_p
= *next
;
299 int jtag_call_event_callbacks(enum jtag_event event
)
301 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
303 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
307 callback
->callback(event
, callback
->priv
);
308 callback
= callback
->next
;
314 /* returns a pointer to the pointer of the last command in queue
315 * this may be a pointer to the root pointer (jtag_command_queue)
316 * or to the next member of the last but one command
318 jtag_command_t
** jtag_get_last_command_p(void)
320 /* jtag_command_t *cmd = jtag_command_queue;
326 return &jtag_command_queue;
330 return last_comand_pointer
;
333 /* returns a pointer to the n-th device in the scan chain */
334 jtag_device_t
* jtag_get_device(int num
)
336 jtag_device_t
*device
= jtag_devices
;
343 device
= device
->next
;
347 LOG_ERROR("jtag device number %d not defined", num
);
351 void* cmd_queue_alloc(size_t size
)
353 cmd_queue_page_t
**p_page
= &cmd_queue_pages
;
358 while ((*p_page
)->next
)
359 p_page
= &((*p_page
)->next
);
360 if (CMD_QUEUE_PAGE_SIZE
- (*p_page
)->used
< size
)
361 p_page
= &((*p_page
)->next
);
366 *p_page
= malloc(sizeof(cmd_queue_page_t
));
368 (*p_page
)->address
= malloc(CMD_QUEUE_PAGE_SIZE
);
369 (*p_page
)->next
= NULL
;
372 offset
= (*p_page
)->used
;
373 (*p_page
)->used
+= size
;
375 u8
*t
=(u8
*)((*p_page
)->address
);
379 void cmd_queue_free()
381 cmd_queue_page_t
*page
= cmd_queue_pages
;
385 cmd_queue_page_t
*last
= page
;
391 cmd_queue_pages
= NULL
;
394 static void jtag_prelude1()
398 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
399 jtag_error
=ERROR_JTAG_TRST_ASSERTED
;
403 if (cmd_queue_end_state
== TAP_TLR
)
404 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
407 static void jtag_prelude(enum tap_state state
)
412 jtag_add_end_state(state
);
414 cmd_queue_cur_state
= cmd_queue_end_state
;
417 void jtag_add_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
421 int retval
=interface_jtag_add_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
422 if (retval
!=ERROR_OK
)
426 int MINIDRIVER(interface_jtag_add_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
428 jtag_command_t
**last_cmd
;
429 jtag_device_t
*device
;
434 last_cmd
= jtag_get_last_command_p();
436 /* allocate memory for a new list member */
437 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
438 (*last_cmd
)->next
= NULL
;
439 last_comand_pointer
= &((*last_cmd
)->next
);
440 (*last_cmd
)->type
= JTAG_SCAN
;
442 /* allocate memory for ir scan command */
443 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
444 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
445 (*last_cmd
)->cmd
.scan
->num_fields
= jtag_num_devices
; /* one field per device */
446 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(jtag_num_devices
* sizeof(scan_field_t
));
447 (*last_cmd
)->cmd
.scan
->end_state
= state
;
449 for (i
= 0; i
< jtag_num_devices
; i
++)
452 device
= jtag_get_device(i
);
453 scan_size
= device
->ir_length
;
454 (*last_cmd
)->cmd
.scan
->fields
[i
].device
= i
;
455 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= scan_size
;
456 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= NULL
;
457 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= NULL
; /* disable verification by default */
459 /* search the list */
460 for (j
= 0; j
< num_fields
; j
++)
462 if (i
== fields
[j
].device
)
465 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
466 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
468 if (jtag_verify_capture_ir
)
470 if (fields
[j
].in_handler
==NULL
)
472 jtag_set_check_value((*last_cmd
)->cmd
.scan
->fields
+i
, device
->expected
, device
->expected_mask
, NULL
);
475 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= fields
[j
].in_handler
;
476 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= fields
[j
].in_handler_priv
;
477 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= device
->expected
;
478 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= device
->expected_mask
;
489 /* if a device isn't listed, set it to BYPASS */
490 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_set_ones(cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
491 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= NULL
;
496 /* update device information */
497 buf_cpy((*last_cmd
)->cmd
.scan
->fields
[i
].out_value
, jtag_get_device(i
)->cur_instr
, scan_size
);
503 void jtag_add_plain_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
507 int retval
=interface_jtag_add_plain_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
508 if (retval
!=ERROR_OK
)
512 int MINIDRIVER(interface_jtag_add_plain_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
515 jtag_command_t
**last_cmd
;
517 last_cmd
= jtag_get_last_command_p();
519 /* allocate memory for a new list member */
520 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
521 (*last_cmd
)->next
= NULL
;
522 last_comand_pointer
= &((*last_cmd
)->next
);
523 (*last_cmd
)->type
= JTAG_SCAN
;
525 /* allocate memory for ir scan command */
526 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
527 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
528 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
529 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
530 (*last_cmd
)->cmd
.scan
->end_state
= state
;
532 for (i
= 0; i
< num_fields
; i
++)
534 int num_bits
= fields
[i
].num_bits
;
535 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
536 (*last_cmd
)->cmd
.scan
->fields
[i
].device
= fields
[i
].device
;
537 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
538 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
539 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
540 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
541 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
542 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
543 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= NULL
;
544 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= NULL
;
549 void jtag_add_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
553 int retval
=interface_jtag_add_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
554 if (retval
!=ERROR_OK
)
558 int MINIDRIVER(interface_jtag_add_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
561 int bypass_devices
= 0;
565 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
566 jtag_device_t
*device
= jtag_devices
;
568 /* count devices in bypass */
573 device
= device
->next
;
576 /* allocate memory for a new list member */
577 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
578 last_comand_pointer
= &((*last_cmd
)->next
);
579 (*last_cmd
)->next
= NULL
;
580 (*last_cmd
)->type
= JTAG_SCAN
;
582 /* allocate memory for dr scan command */
583 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
584 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
585 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
586 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
587 (*last_cmd
)->cmd
.scan
->end_state
= state
;
589 for (i
= 0; i
< jtag_num_devices
; i
++)
592 (*last_cmd
)->cmd
.scan
->fields
[field_count
].device
= i
;
594 for (j
= 0; j
< num_fields
; j
++)
596 if (i
== fields
[j
].device
)
599 scan_size
= fields
[j
].num_bits
;
600 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
601 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
602 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
603 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= fields
[j
].in_value
;
604 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= fields
[j
].in_check_value
;
605 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= fields
[j
].in_check_mask
;
606 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= fields
[j
].in_handler
;
607 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= fields
[j
].in_handler_priv
;
612 #ifdef _DEBUG_JTAG_IO_
613 /* if a device isn't listed, the BYPASS register should be selected */
614 if (!jtag_get_device(i
)->bypass
)
616 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
620 /* program the scan field to 1 bit length, and ignore it's value */
621 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
622 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
623 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
624 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
625 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
626 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
627 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
628 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
632 #ifdef _DEBUG_JTAG_IO_
633 /* if a device is listed, the BYPASS register must not be selected */
634 if (jtag_get_device(i
)->bypass
)
636 LOG_ERROR("BUG: scan data for a device in BYPASS");
645 void MINIDRIVER(interface_jtag_add_dr_out
)(int device_num
,
649 enum tap_state end_state
)
654 int bypass_devices
= 0;
656 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
657 jtag_device_t
*device
= jtag_devices
;
658 /* count devices in bypass */
663 device
= device
->next
;
666 /* allocate memory for a new list member */
667 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
668 last_comand_pointer
= &((*last_cmd
)->next
);
669 (*last_cmd
)->next
= NULL
;
670 (*last_cmd
)->type
= JTAG_SCAN
;
672 /* allocate memory for dr scan command */
673 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
674 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
675 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
676 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
677 (*last_cmd
)->cmd
.scan
->end_state
= end_state
;
679 for (i
= 0; i
< jtag_num_devices
; i
++)
681 (*last_cmd
)->cmd
.scan
->fields
[field_count
].device
= i
;
686 #ifdef _DEBUG_JTAG_IO_
687 /* if a device is listed, the BYPASS register must not be selected */
688 if (jtag_get_device(i
)->bypass
)
690 LOG_ERROR("BUG: scan data for a device in BYPASS");
694 for (j
= 0; j
< num_fields
; j
++)
697 scan_size
= num_bits
[j
];
698 buf_set_u32(out_value
, 0, scan_size
, value
[j
]);
699 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
700 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
701 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
702 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
703 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
704 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
705 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
706 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
710 #ifdef _DEBUG_JTAG_IO_
711 /* if a device isn't listed, the BYPASS register should be selected */
712 if (!jtag_get_device(i
)->bypass
)
714 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
718 /* program the scan field to 1 bit length, and ignore it's value */
719 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
720 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
721 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
722 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
723 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
724 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
725 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
726 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
734 void jtag_add_plain_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
738 int retval
=interface_jtag_add_plain_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
739 if (retval
!=ERROR_OK
)
743 int MINIDRIVER(interface_jtag_add_plain_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
746 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
748 /* allocate memory for a new list member */
749 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
750 last_comand_pointer
= &((*last_cmd
)->next
);
751 (*last_cmd
)->next
= NULL
;
752 (*last_cmd
)->type
= JTAG_SCAN
;
754 /* allocate memory for scan command */
755 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
756 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
757 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
758 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
759 (*last_cmd
)->cmd
.scan
->end_state
= state
;
761 for (i
= 0; i
< num_fields
; i
++)
763 int num_bits
= fields
[i
].num_bits
;
764 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
765 (*last_cmd
)->cmd
.scan
->fields
[i
].device
= fields
[i
].device
;
766 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
767 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
768 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
769 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
770 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
771 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
772 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= fields
[i
].in_handler
;
773 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= fields
[i
].in_handler_priv
;
781 jtag_prelude(TAP_TLR
);
784 retval
=interface_jtag_add_tms();
785 if (retval
!=ERROR_OK
)
789 int MINIDRIVER(interface_jtag_add_tms
)()
791 enum tap_state state
= TAP_TLR
;
792 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
794 /* allocate memory for a new list member */
795 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
796 last_comand_pointer
= &((*last_cmd
)->next
);
797 (*last_cmd
)->next
= NULL
;
798 (*last_cmd
)->type
= JTAG_STATEMOVE
;
800 (*last_cmd
)->cmd
.statemove
= cmd_queue_alloc(sizeof(statemove_command_t
));
801 (*last_cmd
)->cmd
.statemove
->end_state
= state
;
807 void jtag_add_pathmove(int num_states
, enum tap_state
*path
)
809 /* the last state has to be a stable state */
810 if (tap_move_map
[path
[num_states
- 1]] == -1)
812 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
816 enum tap_state cur_state
=cmd_queue_cur_state
;
818 for (i
=0; i
<num_states
; i
++)
820 if ((tap_transitions
[cur_state
].low
!= path
[i
])&&
821 (tap_transitions
[cur_state
].high
!= path
[i
]))
823 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings
[cur_state
], tap_state_strings
[path
[i
]]);
831 cmd_queue_cur_state
= path
[num_states
- 1];
833 int retval
=interface_jtag_add_pathmove(num_states
, path
);
834 if (retval
!=ERROR_OK
)
839 int MINIDRIVER(interface_jtag_add_pathmove
)(int num_states
, enum tap_state
*path
)
841 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
844 /* allocate memory for a new list member */
845 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
846 last_comand_pointer
= &((*last_cmd
)->next
);
847 (*last_cmd
)->next
= NULL
;
848 (*last_cmd
)->type
= JTAG_PATHMOVE
;
850 (*last_cmd
)->cmd
.pathmove
= cmd_queue_alloc(sizeof(pathmove_command_t
));
851 (*last_cmd
)->cmd
.pathmove
->num_states
= num_states
;
852 (*last_cmd
)->cmd
.pathmove
->path
= cmd_queue_alloc(sizeof(enum tap_state
) * num_states
);
854 for (i
= 0; i
< num_states
; i
++)
855 (*last_cmd
)->cmd
.pathmove
->path
[i
] = path
[i
];
860 int MINIDRIVER(interface_jtag_add_runtest
)(int num_cycles
, enum tap_state state
)
862 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
864 /* allocate memory for a new list member */
865 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
866 (*last_cmd
)->next
= NULL
;
867 last_comand_pointer
= &((*last_cmd
)->next
);
868 (*last_cmd
)->type
= JTAG_RUNTEST
;
870 (*last_cmd
)->cmd
.runtest
= cmd_queue_alloc(sizeof(runtest_command_t
));
871 (*last_cmd
)->cmd
.runtest
->num_cycles
= num_cycles
;
872 (*last_cmd
)->cmd
.runtest
->end_state
= state
;
877 void jtag_add_runtest(int num_cycles
, enum tap_state state
)
881 /* executed by sw or hw fifo */
882 int retval
=interface_jtag_add_runtest(num_cycles
, cmd_queue_end_state
);
883 if (retval
!=ERROR_OK
)
887 void jtag_add_reset(int req_trst
, int req_srst
)
889 int trst_with_tms
= 0;
892 /* Make sure that jtag_reset_config allows the requested reset */
893 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
894 if (((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && (req_srst
== 1)) && (req_trst
== 0))
896 LOG_ERROR("BUG: requested reset would assert trst");
897 jtag_error
=ERROR_FAIL
;
901 /* if TRST pulls SRST, we reset with TAP T-L-R */
902 if (((jtag_reset_config
& RESET_TRST_PULLS_SRST
) && (req_trst
== 1)) && (req_srst
== 0))
908 if (req_srst
&& !(jtag_reset_config
& RESET_HAS_SRST
))
910 LOG_ERROR("BUG: requested nSRST assertion, but the current configuration doesn't support this");
911 jtag_error
=ERROR_FAIL
;
915 if (req_trst
&& !(jtag_reset_config
& RESET_HAS_TRST
))
921 jtag_trst
= req_trst
;
922 jtag_srst
= req_srst
;
924 retval
= interface_jtag_add_reset(req_trst
, req_srst
);
925 if (retval
!=ERROR_OK
)
933 LOG_DEBUG("SRST line asserted");
937 LOG_DEBUG("SRST line released");
938 if (jtag_nsrst_delay
)
939 jtag_add_sleep(jtag_nsrst_delay
* 1000);
944 LOG_DEBUG("JTAG reset with tms instead of TRST");
945 jtag_add_end_state(TAP_TLR
);
947 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
953 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
954 * and inform possible listeners about this
956 LOG_DEBUG("TRST line asserted");
957 cmd_queue_cur_state
= TAP_TLR
;
958 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
962 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
963 * but we might want to add a delay to give the TAP time to settle
965 LOG_DEBUG("Now in TAP_TLR - Test-Logic-Reset(either due to TRST line asserted or tms reset)");
966 if (jtag_ntrst_delay
)
967 jtag_add_sleep(jtag_ntrst_delay
* 1000);
971 int MINIDRIVER(interface_jtag_add_reset
)(int req_trst
, int req_srst
)
973 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
975 /* allocate memory for a new list member */
976 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
977 (*last_cmd
)->next
= NULL
;
978 last_comand_pointer
= &((*last_cmd
)->next
);
979 (*last_cmd
)->type
= JTAG_RESET
;
981 (*last_cmd
)->cmd
.reset
= cmd_queue_alloc(sizeof(reset_command_t
));
982 (*last_cmd
)->cmd
.reset
->trst
= req_trst
;
983 (*last_cmd
)->cmd
.reset
->srst
= req_srst
;
989 void jtag_add_end_state(enum tap_state state
)
991 cmd_queue_end_state
= state
;
992 if ((cmd_queue_end_state
== TAP_SD
)||(cmd_queue_end_state
== TAP_SD
))
994 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
998 int MINIDRIVER(interface_jtag_add_sleep
)(u32 us
)
1000 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1002 /* allocate memory for a new list member */
1003 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1004 (*last_cmd
)->next
= NULL
;
1005 last_comand_pointer
= &((*last_cmd
)->next
);
1006 (*last_cmd
)->type
= JTAG_SLEEP
;
1008 (*last_cmd
)->cmd
.sleep
= cmd_queue_alloc(sizeof(sleep_command_t
));
1009 (*last_cmd
)->cmd
.sleep
->us
= us
;
1014 void jtag_add_sleep(u32 us
)
1016 int retval
=interface_jtag_add_sleep(us
);
1017 if (retval
!=ERROR_OK
)
1022 int jtag_scan_size(scan_command_t
*cmd
)
1027 /* count bits in scan command */
1028 for (i
= 0; i
< cmd
->num_fields
; i
++)
1030 bit_count
+= cmd
->fields
[i
].num_bits
;
1036 int jtag_build_buffer(scan_command_t
*cmd
, u8
**buffer
)
1041 bit_count
= jtag_scan_size(cmd
);
1042 *buffer
= malloc(CEIL(bit_count
, 8));
1046 for (i
= 0; i
< cmd
->num_fields
; i
++)
1048 if (cmd
->fields
[i
].out_value
)
1050 #ifdef _DEBUG_JTAG_IO_
1051 char* char_buf
= buf_to_str(cmd
->fields
[i
].out_value
, (cmd
->fields
[i
].num_bits
> 64) ? 64 : cmd
->fields
[i
].num_bits
, 16);
1053 buf_set_buf(cmd
->fields
[i
].out_value
, 0, *buffer
, bit_count
, cmd
->fields
[i
].num_bits
);
1054 #ifdef _DEBUG_JTAG_IO_
1055 LOG_DEBUG("fields[%i].out_value: 0x%s", i
, char_buf
);
1060 bit_count
+= cmd
->fields
[i
].num_bits
;
1067 int jtag_read_buffer(u8
*buffer
, scan_command_t
*cmd
)
1073 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1076 for (i
= 0; i
< cmd
->num_fields
; i
++)
1078 /* if neither in_value nor in_handler
1079 * are specified we don't have to examine this field
1081 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1083 int num_bits
= cmd
->fields
[i
].num_bits
;
1084 u8
*captured
= buf_set_buf(buffer
, bit_count
, malloc(CEIL(num_bits
, 8)), 0, num_bits
);
1086 #ifdef _DEBUG_JTAG_IO_
1089 char_buf
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1090 LOG_DEBUG("fields[%i].in_value: 0x%s", i
, char_buf
);
1094 if (cmd
->fields
[i
].in_value
)
1096 buf_cpy(captured
, cmd
->fields
[i
].in_value
, num_bits
);
1098 if (cmd
->fields
[i
].in_handler
)
1100 if (cmd
->fields
[i
].in_handler(cmd
->fields
[i
].in_value
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1102 LOG_WARNING("in_handler reported a failed check");
1103 retval
= ERROR_JTAG_QUEUE_FAILED
;
1108 /* no in_value specified, but a handler takes care of the scanned data */
1109 if (cmd
->fields
[i
].in_handler
&& (!cmd
->fields
[i
].in_value
))
1111 if (cmd
->fields
[i
].in_handler(captured
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1113 /* We're going to call the error:handler later, but if the in_handler
1114 * reported an error we report this failure upstream
1116 LOG_WARNING("in_handler reported a failed check");
1117 retval
= ERROR_JTAG_QUEUE_FAILED
;
1123 bit_count
+= cmd
->fields
[i
].num_bits
;
1129 int jtag_check_value(u8
*captured
, void *priv
, scan_field_t
*field
)
1131 int retval
= ERROR_OK
;
1132 int num_bits
= field
->num_bits
;
1134 int compare_failed
= 0;
1136 if (field
->in_check_mask
)
1137 compare_failed
= buf_cmp_mask(captured
, field
->in_check_value
, field
->in_check_mask
, num_bits
);
1139 compare_failed
= buf_cmp(captured
, field
->in_check_value
, num_bits
);
1143 /* An error handler could have caught the failing check
1144 * only report a problem when there wasn't a handler, or if the handler
1145 * acknowledged the error
1149 char *captured_char
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1150 char *in_check_value_char
= buf_to_str(field
->in_check_value
, (num_bits
> 64) ? 64 : num_bits
, 16);
1152 if (field
->in_check_mask
)
1154 char *in_check_mask_char
;
1155 in_check_mask_char
= buf_to_str(field
->in_check_mask
, (num_bits
> 64) ? 64 : num_bits
, 16);
1156 LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s check_mask: 0x%s", captured_char
, in_check_value_char
, in_check_mask_char
);
1157 free(in_check_mask_char
);
1161 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
);
1164 free(captured_char
);
1165 free(in_check_value_char
);
1167 retval
= ERROR_JTAG_QUEUE_FAILED
;
1175 set up checking of this field using the in_handler. The values passed in must be valid until
1176 after jtag_execute() has completed.
1178 void jtag_set_check_value(scan_field_t
*field
, u8
*value
, u8
*mask
, error_handler_t
*in_error_handler
)
1181 field
->in_handler
= jtag_check_value
;
1183 field
->in_handler
= NULL
; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1184 field
->in_handler_priv
= NULL
; /* this will be filled in at the invocation site to point to the field duplicate */
1185 field
->in_check_value
= value
;
1186 field
->in_check_mask
= mask
;
1189 enum scan_type
jtag_scan_type(scan_command_t
*cmd
)
1194 for (i
= 0; i
< cmd
->num_fields
; i
++)
1196 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1198 if (cmd
->fields
[i
].out_value
)
1205 int MINIDRIVER(interface_jtag_execute_queue
)(void)
1209 retval
= jtag
->execute_queue();
1213 jtag_command_queue
= NULL
;
1214 last_comand_pointer
= &jtag_command_queue
;
1219 int jtag_execute_queue(void)
1221 int retval
=interface_jtag_execute_queue();
1222 if (retval
==ERROR_OK
)
1226 jtag_error
=ERROR_OK
;
1230 int jtag_reset_callback(enum jtag_event event
, void *priv
)
1232 jtag_device_t
*device
= priv
;
1236 if (event
== JTAG_TRST_ASSERTED
)
1238 buf_set_ones(device
->cur_instr
, device
->ir_length
);
1245 void jtag_sleep(u32 us
)
1250 /* Try to examine chain layout according to IEEE 1149.1 §12
1252 int jtag_examine_chain()
1254 jtag_device_t
*device
= jtag_devices
;
1256 u8 idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
1259 int device_count
= 0;
1260 u8 zero_check
= 0x0;
1261 u8 one_check
= 0xff;
1264 field
.num_bits
= sizeof(idcode_buffer
) * 8;
1265 field
.out_value
= idcode_buffer
;
1266 field
.out_mask
= NULL
;
1267 field
.in_value
= idcode_buffer
;
1268 field
.in_check_value
= NULL
;
1269 field
.in_check_mask
= NULL
;
1270 field
.in_handler
= NULL
;
1271 field
.in_handler_priv
= NULL
;
1273 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
1275 buf_set_u32(idcode_buffer
, i
* 32, 32, 0x000000FF);
1278 jtag_add_plain_dr_scan(1, &field
, TAP_TLR
);
1279 jtag_execute_queue();
1281 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
* 4; i
++)
1283 zero_check
|= idcode_buffer
[i
];
1284 one_check
&= idcode_buffer
[i
];
1287 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1288 if ((zero_check
== 0x00) || (one_check
== 0xff))
1290 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1291 return ERROR_JTAG_INIT_FAILED
;
1294 for (bit_count
= 0; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;)
1296 u32 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1297 if ((idcode
& 1) == 0)
1299 /* LSB must not be 0, this indicates a device in bypass */
1310 if (idcode
== 0x000000FF)
1312 /* End of chain (invalid manufacturer ID) */
1318 device
->idcode
= idcode
;
1319 device
= device
->next
;
1323 manufacturer
= (idcode
& 0xffe) >> 1;
1324 part
= (idcode
& 0xffff000) >> 12;
1325 version
= (idcode
& 0xf0000000) >> 28;
1327 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1328 idcode
, manufacturer
, part
, version
);
1334 /* see if number of discovered devices matches configuration */
1335 if (device_count
!= jtag_num_devices
)
1337 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1338 device_count
, jtag_num_devices
);
1339 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1340 return ERROR_JTAG_INIT_FAILED
;
1346 int jtag_validate_chain()
1348 jtag_device_t
*device
= jtag_devices
;
1349 int total_ir_length
= 0;
1356 total_ir_length
+= device
->ir_length
;
1357 device
= device
->next
;
1360 total_ir_length
+= 2;
1361 ir_test
= malloc(CEIL(total_ir_length
, 8));
1362 buf_set_ones(ir_test
, total_ir_length
);
1365 field
.num_bits
= total_ir_length
;
1366 field
.out_value
= ir_test
;
1367 field
.out_mask
= NULL
;
1368 field
.in_value
= ir_test
;
1369 field
.in_check_value
= NULL
;
1370 field
.in_check_mask
= NULL
;
1371 field
.in_handler
= NULL
;
1372 field
.in_handler_priv
= NULL
;
1374 jtag_add_plain_ir_scan(1, &field
, TAP_TLR
);
1375 jtag_execute_queue();
1377 device
= jtag_devices
;
1380 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x1)
1382 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1383 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1386 return ERROR_JTAG_INIT_FAILED
;
1388 chain_pos
+= device
->ir_length
;
1389 device
= device
->next
;
1392 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x3)
1394 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1395 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1398 return ERROR_JTAG_INIT_FAILED
;
1406 int jtag_register_commands(struct command_context_s
*cmd_ctx
)
1408 register_command(cmd_ctx
, NULL
, "interface", handle_interface_command
,
1409 COMMAND_CONFIG
, NULL
);
1410 register_command(cmd_ctx
, NULL
, "jtag_speed", handle_jtag_speed_command
,
1411 COMMAND_ANY
, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1412 register_command(cmd_ctx
, NULL
, "jtag_khz", handle_jtag_khz_command
,
1413 COMMAND_ANY
, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1414 register_command(cmd_ctx
, NULL
, "jtag_device", handle_jtag_device_command
,
1415 COMMAND_CONFIG
, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1416 register_command(cmd_ctx
, NULL
, "reset_config", handle_reset_config_command
,
1417 COMMAND_CONFIG
, NULL
);
1418 register_command(cmd_ctx
, NULL
, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command
,
1419 COMMAND_CONFIG
, NULL
);
1420 register_command(cmd_ctx
, NULL
, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command
,
1421 COMMAND_CONFIG
, NULL
);
1423 register_command(cmd_ctx
, NULL
, "scan_chain", handle_scan_chain_command
,
1424 COMMAND_EXEC
, "print current scan chain configuration");
1426 register_command(cmd_ctx
, NULL
, "endstate", handle_endstate_command
,
1427 COMMAND_EXEC
, "finish JTAG operations in <tap_state>");
1428 register_command(cmd_ctx
, NULL
, "jtag_reset", handle_jtag_reset_command
,
1429 COMMAND_EXEC
, "toggle reset lines <trst> <srst>");
1430 register_command(cmd_ctx
, NULL
, "runtest", handle_runtest_command
,
1431 COMMAND_EXEC
, "move to Run-Test/Idle, and execute <num_cycles>");
1432 register_command(cmd_ctx
, NULL
, "irscan", handle_irscan_command
,
1433 COMMAND_EXEC
, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1434 register_command(cmd_ctx
, NULL
, "drscan", handle_drscan_command
,
1435 COMMAND_EXEC
, "execute DR scan <device> <var> [dev2] [var2] ...");
1437 register_command(cmd_ctx
, NULL
, "verify_ircapture", handle_verify_ircapture_command
,
1438 COMMAND_ANY
, "verify value captured during Capture-IR <enable|disable>");
1442 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
1444 if (!jtag_interface
)
1446 /* nothing was previously specified by "interface" command */
1447 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1448 return ERROR_JTAG_INVALID_INTERFACE
;
1451 if (jtag_interface
->init() != ERROR_OK
)
1452 return ERROR_JTAG_INIT_FAILED
;
1454 jtag
= jtag_interface
;
1458 int jtag_init(struct command_context_s
*cmd_ctx
)
1460 int validate_tries
= 0;
1461 jtag_device_t
*device
;
1465 if (!jtag
&& jtag_interface_init(cmd_ctx
) != ERROR_OK
)
1466 return ERROR_JTAG_INIT_FAILED
;
1468 device
= jtag_devices
;
1469 jtag_ir_scan_size
= 0;
1470 jtag_num_devices
= 0;
1471 while (device
!= NULL
)
1473 jtag_ir_scan_size
+= device
->ir_length
;
1475 device
= device
->next
;
1479 jtag_execute_queue();
1481 /* examine chain first, as this could discover the real chain layout */
1482 if (jtag_examine_chain() != ERROR_OK
)
1484 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1487 while (jtag_validate_chain() != ERROR_OK
)
1490 if (validate_tries
> 5)
1492 LOG_ERROR("Could not validate JTAG chain, exit");
1493 return ERROR_JTAG_INVALID_INTERFACE
;
1502 static int default_khz(int khz
, int *jtag_speed
)
1504 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1508 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1512 /* check whether the interface is already configured */
1515 LOG_WARNING("Interface already configured, ignoring");
1519 /* interface name is a mandatory argument */
1520 if (argc
< 1 || args
[0][0] == '\0')
1522 return ERROR_COMMAND_SYNTAX_ERROR
;
1525 for (i
=0; jtag_interfaces
[i
]; i
++)
1527 if (strcmp(args
[0], jtag_interfaces
[i
]->name
) == 0)
1529 if (jtag_interfaces
[i
]->register_commands(cmd_ctx
) != ERROR_OK
)
1532 jtag_interface
= jtag_interfaces
[i
];
1534 if (jtag_interface
->khz
== NULL
)
1536 jtag_interface
->khz
= default_khz
;
1542 /* no valid interface was found (i.e. the configuration option,
1543 * didn't match one of the compiled-in interfaces
1545 LOG_ERROR("No valid jtag interface found (%s)", args
[0]);
1546 LOG_ERROR("compiled-in jtag interfaces:");
1547 for (i
= 0; jtag_interfaces
[i
]; i
++)
1549 LOG_ERROR("%i: %s", i
, jtag_interfaces
[i
]->name
);
1552 return ERROR_JTAG_INVALID_INTERFACE
;
1555 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1557 jtag_device_t
**last_device_p
= &jtag_devices
;
1561 while ((*last_device_p
)->next
)
1562 last_device_p
= &((*last_device_p
)->next
);
1563 last_device_p
= &((*last_device_p
)->next
);
1569 *last_device_p
= malloc(sizeof(jtag_device_t
));
1570 (*last_device_p
)->ir_length
= strtoul(args
[0], NULL
, 0);
1572 (*last_device_p
)->expected
= malloc((*last_device_p
)->ir_length
);
1573 buf_set_u32((*last_device_p
)->expected
, 0, (*last_device_p
)->ir_length
, strtoul(args
[1], NULL
, 0));
1574 (*last_device_p
)->expected_mask
= malloc((*last_device_p
)->ir_length
);
1575 buf_set_u32((*last_device_p
)->expected_mask
, 0, (*last_device_p
)->ir_length
, strtoul(args
[2], NULL
, 0));
1577 (*last_device_p
)->cur_instr
= malloc((*last_device_p
)->ir_length
);
1578 (*last_device_p
)->bypass
= 1;
1579 buf_set_ones((*last_device_p
)->cur_instr
, (*last_device_p
)->ir_length
);
1581 (*last_device_p
)->next
= NULL
;
1583 jtag_register_event_callback(jtag_reset_callback
, (*last_device_p
));
1590 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1592 jtag_device_t
*device
= jtag_devices
;
1593 int device_count
= 0;
1597 u32 expected
, expected_mask
, cur_instr
;
1598 expected
= buf_get_u32(device
->expected
, 0, device
->ir_length
);
1599 expected_mask
= buf_get_u32(device
->expected_mask
, 0, device
->ir_length
);
1600 cur_instr
= buf_get_u32(device
->cur_instr
, 0, device
->ir_length
);
1601 command_print(cmd_ctx
, "%i: idcode: 0x%8.8x ir length %i, ir capture 0x%x, ir mask 0x%x, current instruction 0x%x", device_count
, device
->idcode
, device
->ir_length
, expected
, expected_mask
, cur_instr
);
1602 device
= device
->next
;
1609 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1613 if (strcmp(args
[0], "none") == 0)
1614 jtag_reset_config
= RESET_NONE
;
1615 else if (strcmp(args
[0], "trst_only") == 0)
1616 jtag_reset_config
= RESET_HAS_TRST
;
1617 else if (strcmp(args
[0], "srst_only") == 0)
1618 jtag_reset_config
= RESET_HAS_SRST
;
1619 else if (strcmp(args
[0], "trst_and_srst") == 0)
1620 jtag_reset_config
= RESET_TRST_AND_SRST
;
1623 LOG_ERROR("invalid reset_config argument, defaulting to none");
1624 jtag_reset_config
= RESET_NONE
;
1625 return ERROR_INVALID_ARGUMENTS
;
1631 if (strcmp(args
[1], "srst_pulls_trst") == 0)
1632 jtag_reset_config
|= RESET_SRST_PULLS_TRST
;
1633 else if (strcmp(args
[1], "trst_pulls_srst") == 0)
1634 jtag_reset_config
|= RESET_TRST_PULLS_SRST
;
1635 else if (strcmp(args
[1], "combined") == 0)
1636 jtag_reset_config
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
1637 else if (strcmp(args
[1], "separate") == 0)
1638 jtag_reset_config
&= ~(RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
);
1641 LOG_ERROR("invalid reset_config argument, defaulting to none");
1642 jtag_reset_config
= RESET_NONE
;
1643 return ERROR_INVALID_ARGUMENTS
;
1649 if (strcmp(args
[2], "trst_open_drain") == 0)
1650 jtag_reset_config
|= RESET_TRST_OPEN_DRAIN
;
1651 else if (strcmp(args
[2], "trst_push_pull") == 0)
1652 jtag_reset_config
&= ~RESET_TRST_OPEN_DRAIN
;
1655 LOG_ERROR("invalid reset_config argument, defaulting to none");
1656 jtag_reset_config
= RESET_NONE
;
1657 return ERROR_INVALID_ARGUMENTS
;
1663 if (strcmp(args
[3], "srst_push_pull") == 0)
1664 jtag_reset_config
|= RESET_SRST_PUSH_PULL
;
1665 else if (strcmp(args
[3], "srst_open_drain") == 0)
1666 jtag_reset_config
&= ~RESET_SRST_PUSH_PULL
;
1669 LOG_ERROR("invalid reset_config argument, defaulting to none");
1670 jtag_reset_config
= RESET_NONE
;
1671 return ERROR_INVALID_ARGUMENTS
;
1678 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1682 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1687 jtag_nsrst_delay
= strtoul(args
[0], NULL
, 0);
1693 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1697 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1702 jtag_ntrst_delay
= strtoul(args
[0], NULL
, 0);
1708 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1711 if ((argc
<1) || (argc
>2))
1712 return ERROR_COMMAND_SYNTAX_ERROR
;
1715 cur_speed
= jtag_speed
= jtag_speed_post_reset
= strtoul(args
[0], NULL
, 0);
1717 cur_speed
= jtag_speed_post_reset
= strtoul(args
[1], NULL
, 0);
1719 /* this command can be called during CONFIG,
1720 * in which case jtag isn't initialized */
1722 jtag
->speed(cur_speed
);
1727 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1731 if ((argc
<1) || (argc
>2))
1732 return ERROR_COMMAND_SYNTAX_ERROR
;
1736 LOG_ERROR("Interface not selected yet");
1737 return ERROR_COMMAND_SYNTAX_ERROR
;
1741 speed1
= strtoul(args
[0], NULL
, 0);
1743 speed2
= strtoul(args
[1], NULL
, 0);
1745 if (jtag
->khz(speed1
, &speed1
)!=ERROR_OK
)
1748 if (jtag
->khz(speed2
, &speed2
)!=ERROR_OK
)
1752 cur_speed
= jtag_speed
= jtag_speed_post_reset
= speed1
;
1755 cur_speed
= jtag_speed_post_reset
= speed2
;
1757 jtag
->speed(cur_speed
);
1763 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1765 enum tap_state state
;
1769 return ERROR_COMMAND_SYNTAX_ERROR
;
1773 for (state
= 0; state
< 16; state
++)
1775 if (strcmp(args
[0], tap_state_strings
[state
]) == 0)
1777 jtag_add_end_state(state
);
1778 jtag_execute_queue();
1782 command_print(cmd_ctx
, "current endstate: %s", tap_state_strings
[end_state
]);
1787 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1794 return ERROR_COMMAND_SYNTAX_ERROR
;
1797 if (args
[0][0] == '1')
1799 else if (args
[0][0] == '0')
1803 return ERROR_COMMAND_SYNTAX_ERROR
;
1806 if (args
[1][0] == '1')
1808 else if (args
[1][0] == '0')
1812 return ERROR_COMMAND_SYNTAX_ERROR
;
1815 if (!jtag
&& jtag_interface_init(cmd_ctx
) != ERROR_OK
)
1816 return ERROR_JTAG_INIT_FAILED
;
1818 jtag_add_reset(trst
, srst
);
1819 jtag_execute_queue();
1824 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1828 return ERROR_COMMAND_SYNTAX_ERROR
;
1831 jtag_add_runtest(strtol(args
[0], NULL
, 0), -1);
1832 jtag_execute_queue();
1839 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1842 scan_field_t
*fields
;
1844 if ((argc
< 2) || (argc
% 2))
1846 return ERROR_COMMAND_SYNTAX_ERROR
;
1849 fields
= malloc(sizeof(scan_field_t
) * argc
/ 2);
1851 for (i
= 0; i
< argc
/ 2; i
++)
1853 int device
= strtoul(args
[i
*2], NULL
, 0);
1854 int field_size
= jtag_get_device(device
)->ir_length
;
1855 fields
[i
].device
= device
;
1856 fields
[i
].out_value
= malloc(CEIL(field_size
, 8));
1857 buf_set_u32(fields
[i
].out_value
, 0, field_size
, strtoul(args
[i
*2+1], NULL
, 0));
1858 fields
[i
].out_mask
= NULL
;
1859 fields
[i
].in_value
= NULL
;
1860 fields
[i
].in_check_mask
= NULL
;
1861 fields
[i
].in_handler
= NULL
;
1862 fields
[i
].in_handler_priv
= NULL
;
1865 jtag_add_ir_scan(argc
/ 2, fields
, -1);
1866 jtag_execute_queue();
1868 for (i
= 0; i
< argc
/ 2; i
++)
1869 free(fields
[i
].out_value
);
1876 int handle_drscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1878 scan_field_t
*fields
;
1880 int field_count
= 0;
1884 if ((argc
< 2) || (argc
% 2))
1886 return ERROR_COMMAND_SYNTAX_ERROR
;
1889 for (i
= 0; i
< argc
; i
+=2)
1891 var
= get_var_by_namenum(args
[i
+1]);
1894 num_fields
+= var
->num_fields
;
1898 command_print(cmd_ctx
, "variable %s doesn't exist", args
[i
+1]);
1903 fields
= malloc(sizeof(scan_field_t
) * num_fields
);
1905 for (i
= 0; i
< argc
; i
+=2)
1907 var
= get_var_by_namenum(args
[i
+1]);
1909 for (j
= 0; j
< var
->num_fields
; j
++)
1911 fields
[field_count
].device
= strtol(args
[i
], NULL
, 0);
1912 fields
[field_count
].num_bits
= var
->fields
[j
].num_bits
;
1913 fields
[field_count
].out_value
= malloc(CEIL(var
->fields
[j
].num_bits
, 8));
1914 buf_set_u32(fields
[field_count
].out_value
, 0, var
->fields
[j
].num_bits
, var
->fields
[j
].value
);
1915 fields
[field_count
].out_mask
= NULL
;
1916 fields
[field_count
].in_value
= fields
[field_count
].out_value
;
1917 fields
[field_count
].in_check_mask
= NULL
;
1918 fields
[field_count
].in_check_value
= NULL
;
1919 fields
[field_count
].in_handler
= field_le_to_host
;
1920 fields
[field_count
++].in_handler_priv
= &(var
->fields
[j
]);
1924 jtag_add_dr_scan(num_fields
, fields
, -1);
1925 jtag_execute_queue();
1927 for (i
= 0; i
< argc
/ 2; i
++)
1928 free(fields
[i
].out_value
);
1935 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1939 if (strcmp(args
[0], "enable") == 0)
1941 jtag_verify_capture_ir
= 1;
1943 else if (strcmp(args
[0], "disable") == 0)
1945 jtag_verify_capture_ir
= 0;
1948 return ERROR_COMMAND_SYNTAX_ERROR
;
1950 } else if (argc
!= 0)
1952 return ERROR_COMMAND_SYNTAX_ERROR
;
1955 command_print(cmd_ctx
, "verify Capture-IR is %s", (jtag_verify_capture_ir
) ? "enabled": "disabled");
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)