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 /* 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!!!!
117 enum tap_state end_state
= TAP_TLR
;
118 enum tap_state cur_state
= TAP_TLR
;
122 jtag_command_t
*jtag_command_queue
= NULL
;
123 jtag_command_t
**last_comand_pointer
= &jtag_command_queue
;
124 jtag_device_t
*jtag_devices
= NULL
;
125 int jtag_num_devices
= 0;
126 int jtag_ir_scan_size
= 0;
127 enum reset_types jtag_reset_config
= RESET_NONE
;
128 enum tap_state cmd_queue_end_state
= TAP_TLR
;
129 enum tap_state cmd_queue_cur_state
= TAP_TLR
;
131 int jtag_verify_capture_ir
= 1;
133 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
134 int jtag_nsrst_delay
= 0; /* default to no nSRST delay */
135 int jtag_ntrst_delay
= 0; /* default to no nTRST delay */
137 /* maximum number of JTAG devices expected in the chain
139 #define JTAG_MAX_CHAIN_SIZE 20
141 /* callbacks to inform high-level handlers about JTAG state changes */
142 jtag_event_callback_t
*jtag_event_callbacks
;
145 static int speed1
= 0, speed2
= 0;
146 /* flag if the kHz speed was defined */
147 static int hasKHz
= 0;
149 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
152 #if BUILD_ECOSBOARD == 1
153 extern jtag_interface_t eCosBoard_interface
;
156 #if BUILD_PARPORT == 1
157 extern jtag_interface_t parport_interface
;
161 extern jtag_interface_t dummy_interface
;
164 #if BUILD_FT2232_FTD2XX == 1
165 extern jtag_interface_t ft2232_interface
;
168 #if BUILD_FT2232_LIBFTDI == 1
169 extern jtag_interface_t ft2232_interface
;
172 #if BUILD_AMTJTAGACCEL == 1
173 extern jtag_interface_t amt_jtagaccel_interface
;
176 #if BUILD_EP93XX == 1
177 extern jtag_interface_t ep93xx_interface
;
180 #if BUILD_AT91RM9200 == 1
181 extern jtag_interface_t at91rm9200_interface
;
184 #if BUILD_GW16012 == 1
185 extern jtag_interface_t gw16012_interface
;
188 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
189 extern jtag_interface_t presto_interface
;
192 #if BUILD_USBPROG == 1
193 extern jtag_interface_t usbprog_interface
;
196 jtag_interface_t
*jtag_interfaces
[] = {
197 #if BUILD_ECOSBOARD == 1
198 &eCosBoard_interface
,
200 #if BUILD_PARPORT == 1
206 #if BUILD_FT2232_FTD2XX == 1
209 #if BUILD_FT2232_LIBFTDI == 1
212 #if BUILD_AMTJTAGACCEL == 1
213 &amt_jtagaccel_interface
,
215 #if BUILD_EP93XX == 1
218 #if BUILD_AT91RM9200 == 1
219 &at91rm9200_interface
,
221 #if BUILD_GW16012 == 1
224 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
227 #if BUILD_USBPROG == 1
233 jtag_interface_t
*jtag
= NULL
;
236 jtag_interface_t
*jtag_interface
= NULL
;
238 int jtag_speed_post_reset
= 0;
241 /* forward declarations */
242 void jtag_add_pathmove(int num_states
, enum tap_state
*path
);
243 void jtag_add_runtest(int num_cycles
, enum tap_state endstate
);
244 void jtag_add_end_state(enum tap_state endstate
);
245 void jtag_add_sleep(u32 us
);
246 int jtag_execute_queue(void);
247 int jtag_cancel_queue(void);
250 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
251 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
252 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
253 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
254 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
255 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
256 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
258 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
260 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
261 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
262 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
263 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
264 int handle_drscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
266 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
268 int jtag_register_event_callback(int (*callback
)(enum jtag_event event
, void *priv
), void *priv
)
270 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
272 if (callback
== NULL
)
274 return ERROR_INVALID_ARGUMENTS
;
279 while ((*callbacks_p
)->next
)
280 callbacks_p
= &((*callbacks_p
)->next
);
281 callbacks_p
= &((*callbacks_p
)->next
);
284 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
285 (*callbacks_p
)->callback
= callback
;
286 (*callbacks_p
)->priv
= priv
;
287 (*callbacks_p
)->next
= NULL
;
292 int jtag_unregister_event_callback(int (*callback
)(enum jtag_event event
, void *priv
))
294 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
296 if (callback
== NULL
)
298 return ERROR_INVALID_ARGUMENTS
;
303 jtag_event_callback_t
**next
= &((*callbacks_p
)->next
);
304 if ((*callbacks_p
)->callback
== callback
)
307 *callbacks_p
= *next
;
315 int jtag_call_event_callbacks(enum jtag_event event
)
317 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
319 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
323 callback
->callback(event
, callback
->priv
);
324 callback
= callback
->next
;
330 /* returns a pointer to the pointer of the last command in queue
331 * this may be a pointer to the root pointer (jtag_command_queue)
332 * or to the next member of the last but one command
334 jtag_command_t
** jtag_get_last_command_p(void)
336 /* jtag_command_t *cmd = jtag_command_queue;
342 return &jtag_command_queue;
346 return last_comand_pointer
;
349 /* returns a pointer to the n-th device in the scan chain */
350 jtag_device_t
* jtag_get_device(int num
)
352 jtag_device_t
*device
= jtag_devices
;
359 device
= device
->next
;
363 LOG_ERROR("jtag device number %d not defined", num
);
367 void* cmd_queue_alloc(size_t size
)
369 cmd_queue_page_t
**p_page
= &cmd_queue_pages
;
375 while ((*p_page
)->next
)
376 p_page
= &((*p_page
)->next
);
377 if (CMD_QUEUE_PAGE_SIZE
- (*p_page
)->used
< size
)
378 p_page
= &((*p_page
)->next
);
383 *p_page
= malloc(sizeof(cmd_queue_page_t
));
385 (*p_page
)->address
= malloc(CMD_QUEUE_PAGE_SIZE
);
386 (*p_page
)->next
= NULL
;
389 offset
= (*p_page
)->used
;
390 (*p_page
)->used
+= size
;
392 t
=(u8
*)((*p_page
)->address
);
396 void cmd_queue_free()
398 cmd_queue_page_t
*page
= cmd_queue_pages
;
402 cmd_queue_page_t
*last
= page
;
408 cmd_queue_pages
= NULL
;
411 static void jtag_prelude1()
415 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
416 jtag_error
=ERROR_JTAG_TRST_ASSERTED
;
420 if (cmd_queue_end_state
== TAP_TLR
)
421 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
424 static void jtag_prelude(enum tap_state state
)
429 jtag_add_end_state(state
);
431 cmd_queue_cur_state
= cmd_queue_end_state
;
434 void jtag_add_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
440 retval
=interface_jtag_add_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
441 if (retval
!=ERROR_OK
)
445 int MINIDRIVER(interface_jtag_add_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
447 jtag_command_t
**last_cmd
;
448 jtag_device_t
*device
;
453 last_cmd
= jtag_get_last_command_p();
455 /* allocate memory for a new list member */
456 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
457 (*last_cmd
)->next
= NULL
;
458 last_comand_pointer
= &((*last_cmd
)->next
);
459 (*last_cmd
)->type
= JTAG_SCAN
;
461 /* allocate memory for ir scan command */
462 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
463 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
464 (*last_cmd
)->cmd
.scan
->num_fields
= jtag_num_devices
; /* one field per device */
465 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(jtag_num_devices
* sizeof(scan_field_t
));
466 (*last_cmd
)->cmd
.scan
->end_state
= state
;
468 for (i
= 0; i
< jtag_num_devices
; i
++)
471 device
= jtag_get_device(i
);
472 scan_size
= device
->ir_length
;
473 (*last_cmd
)->cmd
.scan
->fields
[i
].device
= i
;
474 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= scan_size
;
475 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= NULL
;
476 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= NULL
; /* disable verification by default */
478 /* search the list */
479 for (j
= 0; j
< num_fields
; j
++)
481 if (i
== fields
[j
].device
)
484 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
485 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
487 if (jtag_verify_capture_ir
)
489 if (fields
[j
].in_handler
==NULL
)
491 jtag_set_check_value((*last_cmd
)->cmd
.scan
->fields
+i
, device
->expected
, device
->expected_mask
, NULL
);
494 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= fields
[j
].in_handler
;
495 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= fields
[j
].in_handler_priv
;
496 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= device
->expected
;
497 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= device
->expected_mask
;
508 /* if a device isn't listed, set it to BYPASS */
509 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_set_ones(cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
510 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= NULL
;
515 /* update device information */
516 buf_cpy((*last_cmd
)->cmd
.scan
->fields
[i
].out_value
, jtag_get_device(i
)->cur_instr
, scan_size
);
522 void jtag_add_plain_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
528 retval
=interface_jtag_add_plain_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
529 if (retval
!=ERROR_OK
)
533 int MINIDRIVER(interface_jtag_add_plain_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
536 jtag_command_t
**last_cmd
;
538 last_cmd
= jtag_get_last_command_p();
540 /* allocate memory for a new list member */
541 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
542 (*last_cmd
)->next
= NULL
;
543 last_comand_pointer
= &((*last_cmd
)->next
);
544 (*last_cmd
)->type
= JTAG_SCAN
;
546 /* allocate memory for ir scan command */
547 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
548 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
549 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
550 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
551 (*last_cmd
)->cmd
.scan
->end_state
= state
;
553 for (i
= 0; i
< num_fields
; i
++)
555 int num_bits
= fields
[i
].num_bits
;
556 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
557 (*last_cmd
)->cmd
.scan
->fields
[i
].device
= fields
[i
].device
;
558 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
559 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
560 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
561 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
562 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
563 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
564 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= NULL
;
565 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= NULL
;
570 void jtag_add_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
576 retval
=interface_jtag_add_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
577 if (retval
!=ERROR_OK
)
581 int MINIDRIVER(interface_jtag_add_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
584 int bypass_devices
= 0;
588 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
589 jtag_device_t
*device
= jtag_devices
;
591 /* count devices in bypass */
596 device
= device
->next
;
599 /* allocate memory for a new list member */
600 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
601 last_comand_pointer
= &((*last_cmd
)->next
);
602 (*last_cmd
)->next
= NULL
;
603 (*last_cmd
)->type
= JTAG_SCAN
;
605 /* allocate memory for dr scan command */
606 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
607 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
608 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
609 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
610 (*last_cmd
)->cmd
.scan
->end_state
= state
;
612 for (i
= 0; i
< jtag_num_devices
; i
++)
615 (*last_cmd
)->cmd
.scan
->fields
[field_count
].device
= i
;
617 for (j
= 0; j
< num_fields
; j
++)
619 if (i
== fields
[j
].device
)
622 scan_size
= fields
[j
].num_bits
;
623 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
624 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
625 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
626 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= fields
[j
].in_value
;
627 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= fields
[j
].in_check_value
;
628 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= fields
[j
].in_check_mask
;
629 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= fields
[j
].in_handler
;
630 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= fields
[j
].in_handler_priv
;
635 #ifdef _DEBUG_JTAG_IO_
636 /* if a device isn't listed, the BYPASS register should be selected */
637 if (!jtag_get_device(i
)->bypass
)
639 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
643 /* program the scan field to 1 bit length, and ignore it's value */
644 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
645 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
646 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
647 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
648 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
649 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
650 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
651 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
655 #ifdef _DEBUG_JTAG_IO_
656 /* if a device is listed, the BYPASS register must not be selected */
657 if (jtag_get_device(i
)->bypass
)
659 LOG_ERROR("BUG: scan data for a device in BYPASS");
668 void MINIDRIVER(interface_jtag_add_dr_out
)(int device_num
,
672 enum tap_state end_state
)
677 int bypass_devices
= 0;
679 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
680 jtag_device_t
*device
= jtag_devices
;
681 /* count devices in bypass */
686 device
= device
->next
;
689 /* allocate memory for a new list member */
690 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
691 last_comand_pointer
= &((*last_cmd
)->next
);
692 (*last_cmd
)->next
= NULL
;
693 (*last_cmd
)->type
= JTAG_SCAN
;
695 /* allocate memory for dr scan command */
696 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
697 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
698 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
699 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
700 (*last_cmd
)->cmd
.scan
->end_state
= end_state
;
702 for (i
= 0; i
< jtag_num_devices
; i
++)
704 (*last_cmd
)->cmd
.scan
->fields
[field_count
].device
= i
;
709 #ifdef _DEBUG_JTAG_IO_
710 /* if a device is listed, the BYPASS register must not be selected */
711 if (jtag_get_device(i
)->bypass
)
713 LOG_ERROR("BUG: scan data for a device in BYPASS");
717 for (j
= 0; j
< num_fields
; j
++)
720 scan_size
= num_bits
[j
];
721 buf_set_u32(out_value
, 0, scan_size
, value
[j
]);
722 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
723 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
724 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
725 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
726 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
727 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
728 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
729 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
733 #ifdef _DEBUG_JTAG_IO_
734 /* if a device isn't listed, the BYPASS register should be selected */
735 if (!jtag_get_device(i
)->bypass
)
737 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
741 /* program the scan field to 1 bit length, and ignore it's value */
742 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
743 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
744 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
745 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
746 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
747 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
748 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
749 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
757 void jtag_add_plain_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
763 retval
=interface_jtag_add_plain_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
764 if (retval
!=ERROR_OK
)
768 int MINIDRIVER(interface_jtag_add_plain_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
771 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
773 /* allocate memory for a new list member */
774 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
775 last_comand_pointer
= &((*last_cmd
)->next
);
776 (*last_cmd
)->next
= NULL
;
777 (*last_cmd
)->type
= JTAG_SCAN
;
779 /* allocate memory for scan command */
780 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
781 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
782 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
783 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
784 (*last_cmd
)->cmd
.scan
->end_state
= state
;
786 for (i
= 0; i
< num_fields
; i
++)
788 int num_bits
= fields
[i
].num_bits
;
789 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
790 (*last_cmd
)->cmd
.scan
->fields
[i
].device
= fields
[i
].device
;
791 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
792 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
793 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
794 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
795 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
796 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
797 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= fields
[i
].in_handler
;
798 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= fields
[i
].in_handler_priv
;
806 jtag_prelude(TAP_TLR
);
809 retval
=interface_jtag_add_tlr();
810 if (retval
!=ERROR_OK
)
814 int MINIDRIVER(interface_jtag_add_tlr
)()
816 enum tap_state state
= TAP_TLR
;
817 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
819 /* allocate memory for a new list member */
820 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
821 last_comand_pointer
= &((*last_cmd
)->next
);
822 (*last_cmd
)->next
= NULL
;
823 (*last_cmd
)->type
= JTAG_STATEMOVE
;
825 (*last_cmd
)->cmd
.statemove
= cmd_queue_alloc(sizeof(statemove_command_t
));
826 (*last_cmd
)->cmd
.statemove
->end_state
= state
;
832 void jtag_add_pathmove(int num_states
, enum tap_state
*path
)
834 enum tap_state cur_state
=cmd_queue_cur_state
;
838 /* the last state has to be a stable state */
839 if (tap_move_map
[path
[num_states
- 1]] == -1)
841 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
845 for (i
=0; i
<num_states
; i
++)
847 if ((tap_transitions
[cur_state
].low
!= path
[i
])&&
848 (tap_transitions
[cur_state
].high
!= path
[i
]))
850 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings
[cur_state
], tap_state_strings
[path
[i
]]);
858 cmd_queue_cur_state
= path
[num_states
- 1];
860 retval
=interface_jtag_add_pathmove(num_states
, path
);
861 if (retval
!=ERROR_OK
)
866 int MINIDRIVER(interface_jtag_add_pathmove
)(int num_states
, enum tap_state
*path
)
868 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
871 /* allocate memory for a new list member */
872 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
873 last_comand_pointer
= &((*last_cmd
)->next
);
874 (*last_cmd
)->next
= NULL
;
875 (*last_cmd
)->type
= JTAG_PATHMOVE
;
877 (*last_cmd
)->cmd
.pathmove
= cmd_queue_alloc(sizeof(pathmove_command_t
));
878 (*last_cmd
)->cmd
.pathmove
->num_states
= num_states
;
879 (*last_cmd
)->cmd
.pathmove
->path
= cmd_queue_alloc(sizeof(enum tap_state
) * num_states
);
881 for (i
= 0; i
< num_states
; i
++)
882 (*last_cmd
)->cmd
.pathmove
->path
[i
] = path
[i
];
887 int MINIDRIVER(interface_jtag_add_runtest
)(int num_cycles
, enum tap_state state
)
889 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
891 /* allocate memory for a new list member */
892 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
893 (*last_cmd
)->next
= NULL
;
894 last_comand_pointer
= &((*last_cmd
)->next
);
895 (*last_cmd
)->type
= JTAG_RUNTEST
;
897 (*last_cmd
)->cmd
.runtest
= cmd_queue_alloc(sizeof(runtest_command_t
));
898 (*last_cmd
)->cmd
.runtest
->num_cycles
= num_cycles
;
899 (*last_cmd
)->cmd
.runtest
->end_state
= state
;
904 void jtag_add_runtest(int num_cycles
, enum tap_state state
)
910 /* executed by sw or hw fifo */
911 retval
=interface_jtag_add_runtest(num_cycles
, cmd_queue_end_state
);
912 if (retval
!=ERROR_OK
)
916 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
918 int trst_with_tlr
= 0;
921 /* FIX!!! there are *many* different cases here. A better
922 * approach is needed for legal combinations of transitions...
924 if ((jtag_reset_config
& RESET_HAS_SRST
)&&
925 (jtag_reset_config
& RESET_HAS_TRST
)&&
926 ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0)&&
927 ((jtag_reset_config
& RESET_TRST_PULLS_SRST
)==0))
929 if (((req_tlr_or_trst
&&!jtag_trst
)||
930 (!req_tlr_or_trst
&&jtag_trst
))&&
931 ((req_srst
&&!jtag_srst
)||
932 (!req_srst
&&jtag_srst
)))
934 LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
938 /* Make sure that jtag_reset_config allows the requested reset */
939 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
940 if (((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && (req_srst
== 1)) && (!req_tlr_or_trst
))
942 LOG_ERROR("BUG: requested reset would assert trst");
943 jtag_error
=ERROR_FAIL
;
947 /* if TRST pulls SRST, we reset with TAP T-L-R */
948 if (((jtag_reset_config
& RESET_TRST_PULLS_SRST
) && (req_tlr_or_trst
)) && (req_srst
== 0))
953 if (req_srst
&& !(jtag_reset_config
& RESET_HAS_SRST
))
955 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
956 jtag_error
=ERROR_FAIL
;
962 if (!trst_with_tlr
&& (jtag_reset_config
& RESET_HAS_TRST
))
974 jtag_srst
= req_srst
;
976 retval
= interface_jtag_add_reset(jtag_trst
, jtag_srst
);
977 if (retval
!=ERROR_OK
)
985 LOG_DEBUG("SRST line asserted");
989 LOG_DEBUG("SRST line released");
990 if (jtag_nsrst_delay
)
991 jtag_add_sleep(jtag_nsrst_delay
* 1000);
996 LOG_DEBUG("JTAG reset with tms instead of TRST");
997 jtag_add_end_state(TAP_TLR
);
999 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1005 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1006 * and inform possible listeners about this
1008 LOG_DEBUG("TRST line asserted");
1009 cmd_queue_cur_state
= TAP_TLR
;
1010 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1014 if (jtag_ntrst_delay
)
1015 jtag_add_sleep(jtag_ntrst_delay
* 1000);
1019 int MINIDRIVER(interface_jtag_add_reset
)(int req_trst
, int req_srst
)
1021 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1023 /* allocate memory for a new list member */
1024 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1025 (*last_cmd
)->next
= NULL
;
1026 last_comand_pointer
= &((*last_cmd
)->next
);
1027 (*last_cmd
)->type
= JTAG_RESET
;
1029 (*last_cmd
)->cmd
.reset
= cmd_queue_alloc(sizeof(reset_command_t
));
1030 (*last_cmd
)->cmd
.reset
->trst
= req_trst
;
1031 (*last_cmd
)->cmd
.reset
->srst
= req_srst
;
1037 void jtag_add_end_state(enum tap_state state
)
1039 cmd_queue_end_state
= state
;
1040 if ((cmd_queue_end_state
== TAP_SD
)||(cmd_queue_end_state
== TAP_SD
))
1042 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1046 int MINIDRIVER(interface_jtag_add_sleep
)(u32 us
)
1048 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1050 /* allocate memory for a new list member */
1051 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1052 (*last_cmd
)->next
= NULL
;
1053 last_comand_pointer
= &((*last_cmd
)->next
);
1054 (*last_cmd
)->type
= JTAG_SLEEP
;
1056 (*last_cmd
)->cmd
.sleep
= cmd_queue_alloc(sizeof(sleep_command_t
));
1057 (*last_cmd
)->cmd
.sleep
->us
= us
;
1062 void jtag_add_sleep(u32 us
)
1064 int retval
=interface_jtag_add_sleep(us
);
1065 if (retval
!=ERROR_OK
)
1070 int jtag_scan_size(scan_command_t
*cmd
)
1075 /* count bits in scan command */
1076 for (i
= 0; i
< cmd
->num_fields
; i
++)
1078 bit_count
+= cmd
->fields
[i
].num_bits
;
1084 int jtag_build_buffer(scan_command_t
*cmd
, u8
**buffer
)
1089 bit_count
= jtag_scan_size(cmd
);
1090 *buffer
= malloc(CEIL(bit_count
, 8));
1094 for (i
= 0; i
< cmd
->num_fields
; i
++)
1096 if (cmd
->fields
[i
].out_value
)
1098 #ifdef _DEBUG_JTAG_IO_
1099 char* char_buf
= buf_to_str(cmd
->fields
[i
].out_value
, (cmd
->fields
[i
].num_bits
> 64) ? 64 : cmd
->fields
[i
].num_bits
, 16);
1101 buf_set_buf(cmd
->fields
[i
].out_value
, 0, *buffer
, bit_count
, cmd
->fields
[i
].num_bits
);
1102 #ifdef _DEBUG_JTAG_IO_
1103 LOG_DEBUG("fields[%i].out_value: 0x%s", i
, char_buf
);
1108 bit_count
+= cmd
->fields
[i
].num_bits
;
1115 int jtag_read_buffer(u8
*buffer
, scan_command_t
*cmd
)
1121 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1124 for (i
= 0; i
< cmd
->num_fields
; i
++)
1126 /* if neither in_value nor in_handler
1127 * are specified we don't have to examine this field
1129 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1131 int num_bits
= cmd
->fields
[i
].num_bits
;
1132 u8
*captured
= buf_set_buf(buffer
, bit_count
, malloc(CEIL(num_bits
, 8)), 0, num_bits
);
1134 #ifdef _DEBUG_JTAG_IO_
1137 char_buf
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1138 LOG_DEBUG("fields[%i].in_value: 0x%s", i
, char_buf
);
1142 if (cmd
->fields
[i
].in_value
)
1144 buf_cpy(captured
, cmd
->fields
[i
].in_value
, num_bits
);
1146 if (cmd
->fields
[i
].in_handler
)
1148 if (cmd
->fields
[i
].in_handler(cmd
->fields
[i
].in_value
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1150 LOG_WARNING("in_handler reported a failed check");
1151 retval
= ERROR_JTAG_QUEUE_FAILED
;
1156 /* no in_value specified, but a handler takes care of the scanned data */
1157 if (cmd
->fields
[i
].in_handler
&& (!cmd
->fields
[i
].in_value
))
1159 if (cmd
->fields
[i
].in_handler(captured
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1161 /* We're going to call the error:handler later, but if the in_handler
1162 * reported an error we report this failure upstream
1164 LOG_WARNING("in_handler reported a failed check");
1165 retval
= ERROR_JTAG_QUEUE_FAILED
;
1171 bit_count
+= cmd
->fields
[i
].num_bits
;
1177 int jtag_check_value(u8
*captured
, void *priv
, scan_field_t
*field
)
1179 int retval
= ERROR_OK
;
1180 int num_bits
= field
->num_bits
;
1182 int compare_failed
= 0;
1184 if (field
->in_check_mask
)
1185 compare_failed
= buf_cmp_mask(captured
, field
->in_check_value
, field
->in_check_mask
, num_bits
);
1187 compare_failed
= buf_cmp(captured
, field
->in_check_value
, num_bits
);
1191 /* An error handler could have caught the failing check
1192 * only report a problem when there wasn't a handler, or if the handler
1193 * acknowledged the error
1197 char *captured_char
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1198 char *in_check_value_char
= buf_to_str(field
->in_check_value
, (num_bits
> 64) ? 64 : num_bits
, 16);
1200 if (field
->in_check_mask
)
1202 char *in_check_mask_char
;
1203 in_check_mask_char
= buf_to_str(field
->in_check_mask
, (num_bits
> 64) ? 64 : num_bits
, 16);
1204 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
);
1205 free(in_check_mask_char
);
1209 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
);
1212 free(captured_char
);
1213 free(in_check_value_char
);
1215 retval
= ERROR_JTAG_QUEUE_FAILED
;
1223 set up checking of this field using the in_handler. The values passed in must be valid until
1224 after jtag_execute() has completed.
1226 void jtag_set_check_value(scan_field_t
*field
, u8
*value
, u8
*mask
, error_handler_t
*in_error_handler
)
1229 field
->in_handler
= jtag_check_value
;
1231 field
->in_handler
= NULL
; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1232 field
->in_handler_priv
= NULL
; /* this will be filled in at the invocation site to point to the field duplicate */
1233 field
->in_check_value
= value
;
1234 field
->in_check_mask
= mask
;
1237 enum scan_type
jtag_scan_type(scan_command_t
*cmd
)
1242 for (i
= 0; i
< cmd
->num_fields
; i
++)
1244 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1246 if (cmd
->fields
[i
].out_value
)
1253 int MINIDRIVER(interface_jtag_execute_queue
)(void)
1257 retval
= jtag
->execute_queue();
1261 jtag_command_queue
= NULL
;
1262 last_comand_pointer
= &jtag_command_queue
;
1267 int jtag_execute_queue(void)
1269 int retval
=interface_jtag_execute_queue();
1270 if (retval
==ERROR_OK
)
1274 jtag_error
=ERROR_OK
;
1278 int jtag_reset_callback(enum jtag_event event
, void *priv
)
1280 jtag_device_t
*device
= priv
;
1284 if (event
== JTAG_TRST_ASSERTED
)
1286 buf_set_ones(device
->cur_instr
, device
->ir_length
);
1293 void jtag_sleep(u32 us
)
1298 /* Try to examine chain layout according to IEEE 1149.1 §12
1300 int jtag_examine_chain()
1302 jtag_device_t
*device
= jtag_devices
;
1304 u8 idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
1307 int device_count
= 0;
1308 u8 zero_check
= 0x0;
1309 u8 one_check
= 0xff;
1312 field
.num_bits
= sizeof(idcode_buffer
) * 8;
1313 field
.out_value
= idcode_buffer
;
1314 field
.out_mask
= NULL
;
1315 field
.in_value
= idcode_buffer
;
1316 field
.in_check_value
= NULL
;
1317 field
.in_check_mask
= NULL
;
1318 field
.in_handler
= NULL
;
1319 field
.in_handler_priv
= NULL
;
1321 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
1323 buf_set_u32(idcode_buffer
, i
* 32, 32, 0x000000FF);
1326 jtag_add_plain_dr_scan(1, &field
, TAP_TLR
);
1327 jtag_execute_queue();
1329 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
* 4; i
++)
1331 zero_check
|= idcode_buffer
[i
];
1332 one_check
&= idcode_buffer
[i
];
1335 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1336 if ((zero_check
== 0x00) || (one_check
== 0xff))
1338 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1339 return ERROR_JTAG_INIT_FAILED
;
1342 for (bit_count
= 0; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;)
1344 u32 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1345 if ((idcode
& 1) == 0)
1347 /* LSB must not be 0, this indicates a device in bypass */
1358 if (idcode
== 0x000000FF)
1360 /* End of chain (invalid manufacturer ID) */
1366 device
->idcode
= idcode
;
1367 device
= device
->next
;
1371 manufacturer
= (idcode
& 0xffe) >> 1;
1372 part
= (idcode
& 0xffff000) >> 12;
1373 version
= (idcode
& 0xf0000000) >> 28;
1375 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1376 idcode
, manufacturer
, part
, version
);
1382 /* see if number of discovered devices matches configuration */
1383 if (device_count
!= jtag_num_devices
)
1385 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1386 device_count
, jtag_num_devices
);
1387 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1388 return ERROR_JTAG_INIT_FAILED
;
1394 int jtag_validate_chain()
1396 jtag_device_t
*device
= jtag_devices
;
1397 int total_ir_length
= 0;
1404 total_ir_length
+= device
->ir_length
;
1405 device
= device
->next
;
1408 total_ir_length
+= 2;
1409 ir_test
= malloc(CEIL(total_ir_length
, 8));
1410 buf_set_ones(ir_test
, total_ir_length
);
1413 field
.num_bits
= total_ir_length
;
1414 field
.out_value
= ir_test
;
1415 field
.out_mask
= NULL
;
1416 field
.in_value
= ir_test
;
1417 field
.in_check_value
= NULL
;
1418 field
.in_check_mask
= NULL
;
1419 field
.in_handler
= NULL
;
1420 field
.in_handler_priv
= NULL
;
1422 jtag_add_plain_ir_scan(1, &field
, TAP_TLR
);
1423 jtag_execute_queue();
1425 device
= jtag_devices
;
1428 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x1)
1430 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1431 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1434 return ERROR_JTAG_INIT_FAILED
;
1436 chain_pos
+= device
->ir_length
;
1437 device
= device
->next
;
1440 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x3)
1442 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1443 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1446 return ERROR_JTAG_INIT_FAILED
;
1454 int jtag_register_commands(struct command_context_s
*cmd_ctx
)
1456 register_command(cmd_ctx
, NULL
, "interface", handle_interface_command
,
1457 COMMAND_CONFIG
, NULL
);
1458 register_command(cmd_ctx
, NULL
, "jtag_speed", handle_jtag_speed_command
,
1459 COMMAND_ANY
, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1460 register_command(cmd_ctx
, NULL
, "jtag_khz", handle_jtag_khz_command
,
1461 COMMAND_ANY
, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1462 register_command(cmd_ctx
, NULL
, "jtag_device", handle_jtag_device_command
,
1463 COMMAND_CONFIG
, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1464 register_command(cmd_ctx
, NULL
, "reset_config", handle_reset_config_command
,
1465 COMMAND_CONFIG
, NULL
);
1466 register_command(cmd_ctx
, NULL
, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command
,
1467 COMMAND_ANY
, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1468 register_command(cmd_ctx
, NULL
, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command
,
1469 COMMAND_ANY
, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1471 register_command(cmd_ctx
, NULL
, "scan_chain", handle_scan_chain_command
,
1472 COMMAND_EXEC
, "print current scan chain configuration");
1474 register_command(cmd_ctx
, NULL
, "endstate", handle_endstate_command
,
1475 COMMAND_EXEC
, "finish JTAG operations in <tap_state>");
1476 register_command(cmd_ctx
, NULL
, "jtag_reset", handle_jtag_reset_command
,
1477 COMMAND_EXEC
, "toggle reset lines <trst> <srst>");
1478 register_command(cmd_ctx
, NULL
, "runtest", handle_runtest_command
,
1479 COMMAND_EXEC
, "move to Run-Test/Idle, and execute <num_cycles>");
1480 register_command(cmd_ctx
, NULL
, "irscan", handle_irscan_command
,
1481 COMMAND_EXEC
, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1482 register_command(cmd_ctx
, NULL
, "drscan", handle_drscan_command
,
1483 COMMAND_EXEC
, "execute DR scan <device> <var> [dev2] [var2] ...");
1485 register_command(cmd_ctx
, NULL
, "verify_ircapture", handle_verify_ircapture_command
,
1486 COMMAND_ANY
, "verify value captured during Capture-IR <enable|disable>");
1490 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
1495 if (!jtag_interface
)
1497 /* nothing was previously specified by "interface" command */
1498 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1499 return ERROR_JTAG_INVALID_INTERFACE
;
1503 /*stay on "reset speed"*/
1504 jtag_interface
->khz(speed1
, &jtag_speed
);
1505 jtag_interface
->khz(speed2
, &jtag_speed_post_reset
);
1509 if (jtag_interface
->init() != ERROR_OK
)
1510 return ERROR_JTAG_INIT_FAILED
;
1514 jtag
= jtag_interface
;
1518 static int jtag_init_inner(struct command_context_s
*cmd_ctx
)
1520 int validate_tries
= 0;
1521 jtag_device_t
*device
;
1524 LOG_DEBUG("Init JTAG chain");
1526 device
= jtag_devices
;
1527 jtag_ir_scan_size
= 0;
1528 jtag_num_devices
= 0;
1529 while (device
!= NULL
)
1531 jtag_ir_scan_size
+= device
->ir_length
;
1533 device
= device
->next
;
1537 if ((retval
=jtag_execute_queue())!=ERROR_OK
)
1540 /* examine chain first, as this could discover the real chain layout */
1541 if (jtag_examine_chain() != ERROR_OK
)
1543 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1546 while (jtag_validate_chain() != ERROR_OK
)
1550 if (validate_tries
> 5)
1552 LOG_ERROR("Could not validate JTAG chain, exit");
1553 return ERROR_JTAG_INVALID_INTERFACE
;
1561 int jtag_init_reset(struct command_context_s
*cmd_ctx
)
1565 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1568 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / tms");
1570 /* Reset can happen after a power cycle.
1572 * Ideally we would only assert TRST or run tms before the target reset.
1574 * However w/srst_pulls_trst, trst is asserted together with the target
1575 * reset whether we want it or not.
1577 * NB! Some targets have JTAG circuitry disabled until a
1578 * trst & srst has been asserted.
1580 * NB! here we assume nsrst/ntrst delay are sufficient!
1582 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1585 jtag_add_reset(1, 0); /* TMS or TRST */
1586 if (jtag_reset_config
& RESET_HAS_SRST
)
1588 jtag_add_reset(1, 1);
1589 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0)
1590 jtag_add_reset(0, 1);
1592 jtag_add_reset(0, 0);
1593 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
1596 /* Check that we can communication on the JTAG chain + eventually we want to
1597 * be able to perform enumeration only after OpenOCD has started
1598 * telnet and GDB server
1600 * That would allow users to more easily perform any magic they need to before
1603 return jtag_init_inner(cmd_ctx
);
1606 int jtag_init(struct command_context_s
*cmd_ctx
)
1609 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1611 if (jtag_init_inner(cmd_ctx
)==ERROR_OK
)
1615 return jtag_init_reset(cmd_ctx
);
1619 static int default_khz(int khz
, int *jtag_speed
)
1621 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1625 static int default_speedDiv(int speed
, int *khz
)
1627 LOG_ERROR("Translation from jtag_speed to khz not implemented");
1631 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1635 /* check whether the interface is already configured */
1638 LOG_WARNING("Interface already configured, ignoring");
1642 /* interface name is a mandatory argument */
1643 if (argc
< 1 || args
[0][0] == '\0')
1645 return ERROR_COMMAND_SYNTAX_ERROR
;
1648 for (i
=0; jtag_interfaces
[i
]; i
++)
1650 if (strcmp(args
[0], jtag_interfaces
[i
]->name
) == 0)
1652 if (jtag_interfaces
[i
]->register_commands(cmd_ctx
) != ERROR_OK
)
1655 jtag_interface
= jtag_interfaces
[i
];
1657 if (jtag_interface
->khz
== NULL
)
1659 jtag_interface
->khz
= default_khz
;
1661 if (jtag_interface
->speedDiv
== NULL
)
1663 jtag_interface
->speedDiv
= default_speedDiv
;
1669 /* no valid interface was found (i.e. the configuration option,
1670 * didn't match one of the compiled-in interfaces
1672 LOG_ERROR("No valid jtag interface found (%s)", args
[0]);
1673 LOG_ERROR("compiled-in jtag interfaces:");
1674 for (i
= 0; jtag_interfaces
[i
]; i
++)
1676 LOG_ERROR("%i: %s", i
, jtag_interfaces
[i
]->name
);
1679 return ERROR_JTAG_INVALID_INTERFACE
;
1682 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1684 jtag_device_t
**last_device_p
= &jtag_devices
;
1688 while ((*last_device_p
)->next
)
1689 last_device_p
= &((*last_device_p
)->next
);
1690 last_device_p
= &((*last_device_p
)->next
);
1696 *last_device_p
= malloc(sizeof(jtag_device_t
));
1697 (*last_device_p
)->ir_length
= strtoul(args
[0], NULL
, 0);
1699 (*last_device_p
)->expected
= malloc((*last_device_p
)->ir_length
);
1700 buf_set_u32((*last_device_p
)->expected
, 0, (*last_device_p
)->ir_length
, strtoul(args
[1], NULL
, 0));
1701 (*last_device_p
)->expected_mask
= malloc((*last_device_p
)->ir_length
);
1702 buf_set_u32((*last_device_p
)->expected_mask
, 0, (*last_device_p
)->ir_length
, strtoul(args
[2], NULL
, 0));
1704 (*last_device_p
)->cur_instr
= malloc((*last_device_p
)->ir_length
);
1705 (*last_device_p
)->bypass
= 1;
1706 buf_set_ones((*last_device_p
)->cur_instr
, (*last_device_p
)->ir_length
);
1708 (*last_device_p
)->next
= NULL
;
1710 jtag_register_event_callback(jtag_reset_callback
, (*last_device_p
));
1717 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1719 jtag_device_t
*device
= jtag_devices
;
1720 int device_count
= 0;
1724 u32 expected
, expected_mask
, cur_instr
;
1725 expected
= buf_get_u32(device
->expected
, 0, device
->ir_length
);
1726 expected_mask
= buf_get_u32(device
->expected_mask
, 0, device
->ir_length
);
1727 cur_instr
= buf_get_u32(device
->cur_instr
, 0, device
->ir_length
);
1728 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
);
1729 device
= device
->next
;
1736 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1739 return ERROR_COMMAND_SYNTAX_ERROR
;
1743 if (strcmp(args
[0], "none") == 0)
1744 jtag_reset_config
= RESET_NONE
;
1745 else if (strcmp(args
[0], "trst_only") == 0)
1746 jtag_reset_config
= RESET_HAS_TRST
;
1747 else if (strcmp(args
[0], "srst_only") == 0)
1748 jtag_reset_config
= RESET_HAS_SRST
;
1749 else if (strcmp(args
[0], "trst_and_srst") == 0)
1750 jtag_reset_config
= RESET_TRST_AND_SRST
;
1753 LOG_ERROR("invalid reset_config argument, defaulting to none");
1754 jtag_reset_config
= RESET_NONE
;
1755 return ERROR_INVALID_ARGUMENTS
;
1761 if (strcmp(args
[1], "separate") == 0)
1763 /* seperate reset lines - default */
1766 if (strcmp(args
[1], "srst_pulls_trst") == 0)
1767 jtag_reset_config
|= RESET_SRST_PULLS_TRST
;
1768 else if (strcmp(args
[1], "trst_pulls_srst") == 0)
1769 jtag_reset_config
|= RESET_TRST_PULLS_SRST
;
1770 else if (strcmp(args
[1], "combined") == 0)
1771 jtag_reset_config
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
1774 LOG_ERROR("invalid reset_config argument, defaulting to none");
1775 jtag_reset_config
= RESET_NONE
;
1776 return ERROR_INVALID_ARGUMENTS
;
1783 if (strcmp(args
[2], "trst_open_drain") == 0)
1784 jtag_reset_config
|= RESET_TRST_OPEN_DRAIN
;
1785 else if (strcmp(args
[2], "trst_push_pull") == 0)
1786 jtag_reset_config
&= ~RESET_TRST_OPEN_DRAIN
;
1789 LOG_ERROR("invalid reset_config argument, defaulting to none");
1790 jtag_reset_config
= RESET_NONE
;
1791 return ERROR_INVALID_ARGUMENTS
;
1797 if (strcmp(args
[3], "srst_push_pull") == 0)
1798 jtag_reset_config
|= RESET_SRST_PUSH_PULL
;
1799 else if (strcmp(args
[3], "srst_open_drain") == 0)
1800 jtag_reset_config
&= ~RESET_SRST_PUSH_PULL
;
1803 LOG_ERROR("invalid reset_config argument, defaulting to none");
1804 jtag_reset_config
= RESET_NONE
;
1805 return ERROR_INVALID_ARGUMENTS
;
1812 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1816 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1821 jtag_nsrst_delay
= strtoul(args
[0], NULL
, 0);
1827 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1831 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1836 jtag_ntrst_delay
= strtoul(args
[0], NULL
, 0);
1842 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1848 if ((argc
<1) || (argc
>2))
1849 return ERROR_COMMAND_SYNTAX_ERROR
;
1851 LOG_DEBUG("handle jtag speed");
1854 cur_speed
= jtag_speed
= jtag_speed_post_reset
= strtoul(args
[0], NULL
, 0);
1856 cur_speed
= jtag_speed_post_reset
= strtoul(args
[1], NULL
, 0);
1858 /* this command can be called during CONFIG,
1859 * in which case jtag isn't initialized */
1862 jtag
->speedDiv(jtag_speed
, &speed1
);
1863 jtag
->speedDiv(jtag_speed_post_reset
, &speed2
);
1864 jtag
->speed(cur_speed
);
1867 command_print(cmd_ctx
, "jtag_speed: %d, %d", jtag_speed
, jtag_speed_post_reset
);
1872 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1874 LOG_DEBUG("handle jtag khz");
1877 return ERROR_COMMAND_SYNTAX_ERROR
;
1883 speed1
= speed2
= strtoul(args
[0], NULL
, 0);
1885 speed2
= strtoul(args
[1], NULL
, 0);
1890 LOG_DEBUG("have interface set up");
1891 int speed_div1
, speed_div2
;
1892 if (jtag
->khz(speed1
, &speed_div1
)!=ERROR_OK
)
1894 if (jtag
->khz(speed2
, &speed_div2
)!=ERROR_OK
)
1898 cur_speed
= jtag_speed
= jtag_speed_post_reset
= speed_div1
;
1900 cur_speed
= jtag_speed_post_reset
= speed_div2
;
1902 jtag
->speed(cur_speed
);
1908 command_print(cmd_ctx
, "jtag_khz: %d, %d", speed1
, speed2
);
1914 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1916 enum tap_state state
;
1920 return ERROR_COMMAND_SYNTAX_ERROR
;
1924 for (state
= 0; state
< 16; state
++)
1926 if (strcmp(args
[0], tap_state_strings
[state
]) == 0)
1928 jtag_add_end_state(state
);
1929 jtag_execute_queue();
1933 command_print(cmd_ctx
, "current endstate: %s", tap_state_strings
[cmd_queue_end_state
]);
1938 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1945 return ERROR_COMMAND_SYNTAX_ERROR
;
1948 if (args
[0][0] == '1')
1950 else if (args
[0][0] == '0')
1954 return ERROR_COMMAND_SYNTAX_ERROR
;
1957 if (args
[1][0] == '1')
1959 else if (args
[1][0] == '0')
1963 return ERROR_COMMAND_SYNTAX_ERROR
;
1966 if (jtag_interface_init(cmd_ctx
) != ERROR_OK
)
1967 return ERROR_JTAG_INIT_FAILED
;
1969 jtag_add_reset(trst
, srst
);
1970 jtag_execute_queue();
1975 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1979 return ERROR_COMMAND_SYNTAX_ERROR
;
1982 jtag_add_runtest(strtol(args
[0], NULL
, 0), -1);
1983 jtag_execute_queue();
1990 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1993 scan_field_t
*fields
;
1995 if ((argc
< 2) || (argc
% 2))
1997 return ERROR_COMMAND_SYNTAX_ERROR
;
2000 fields
= malloc(sizeof(scan_field_t
) * argc
/ 2);
2002 for (i
= 0; i
< argc
/ 2; i
++)
2004 int device
= strtoul(args
[i
*2], NULL
, 0);
2005 int field_size
= jtag_get_device(device
)->ir_length
;
2006 fields
[i
].device
= device
;
2007 fields
[i
].out_value
= malloc(CEIL(field_size
, 8));
2008 buf_set_u32(fields
[i
].out_value
, 0, field_size
, strtoul(args
[i
*2+1], NULL
, 0));
2009 fields
[i
].out_mask
= NULL
;
2010 fields
[i
].in_value
= NULL
;
2011 fields
[i
].in_check_mask
= NULL
;
2012 fields
[i
].in_handler
= NULL
;
2013 fields
[i
].in_handler_priv
= NULL
;
2016 jtag_add_ir_scan(argc
/ 2, fields
, -1);
2017 jtag_execute_queue();
2019 for (i
= 0; i
< argc
/ 2; i
++)
2020 free(fields
[i
].out_value
);
2027 int handle_drscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2029 scan_field_t
*fields
;
2031 int field_count
= 0;
2035 if ((argc
< 2) || (argc
% 2))
2037 return ERROR_COMMAND_SYNTAX_ERROR
;
2040 for (i
= 0; i
< argc
; i
+=2)
2042 var
= get_var_by_namenum(args
[i
+1]);
2045 num_fields
+= var
->num_fields
;
2049 command_print(cmd_ctx
, "variable %s doesn't exist", args
[i
+1]);
2054 fields
= malloc(sizeof(scan_field_t
) * num_fields
);
2056 for (i
= 0; i
< argc
; i
+=2)
2058 var
= get_var_by_namenum(args
[i
+1]);
2060 for (j
= 0; j
< var
->num_fields
; j
++)
2062 fields
[field_count
].device
= strtol(args
[i
], NULL
, 0);
2063 fields
[field_count
].num_bits
= var
->fields
[j
].num_bits
;
2064 fields
[field_count
].out_value
= malloc(CEIL(var
->fields
[j
].num_bits
, 8));
2065 buf_set_u32(fields
[field_count
].out_value
, 0, var
->fields
[j
].num_bits
, var
->fields
[j
].value
);
2066 fields
[field_count
].out_mask
= NULL
;
2067 fields
[field_count
].in_value
= fields
[field_count
].out_value
;
2068 fields
[field_count
].in_check_mask
= NULL
;
2069 fields
[field_count
].in_check_value
= NULL
;
2070 fields
[field_count
].in_handler
= field_le_to_host
;
2071 fields
[field_count
++].in_handler_priv
= &(var
->fields
[j
]);
2075 jtag_add_dr_scan(num_fields
, fields
, -1);
2076 jtag_execute_queue();
2078 for (i
= 0; i
< argc
/ 2; i
++)
2079 free(fields
[i
].out_value
);
2086 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2090 if (strcmp(args
[0], "enable") == 0)
2092 jtag_verify_capture_ir
= 1;
2094 else if (strcmp(args
[0], "disable") == 0)
2096 jtag_verify_capture_ir
= 0;
2099 return ERROR_COMMAND_SYNTAX_ERROR
;
2101 } else if (argc
!= 0)
2103 return ERROR_COMMAND_SYNTAX_ERROR
;
2106 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)