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 (TLR 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
;
197 extern jtag_interface_t jlink_interface
;
200 jtag_interface_t
*jtag_interfaces
[] = {
201 #if BUILD_ECOSBOARD == 1
202 &eCosBoard_interface
,
204 #if BUILD_PARPORT == 1
210 #if BUILD_FT2232_FTD2XX == 1
213 #if BUILD_FT2232_LIBFTDI == 1
216 #if BUILD_AMTJTAGACCEL == 1
217 &amt_jtagaccel_interface
,
219 #if BUILD_EP93XX == 1
222 #if BUILD_AT91RM9200 == 1
223 &at91rm9200_interface
,
225 #if BUILD_GW16012 == 1
228 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
231 #if BUILD_USBPROG == 1
240 jtag_interface_t
*jtag
= NULL
;
243 jtag_interface_t
*jtag_interface
= NULL
;
245 int jtag_speed_post_reset
= 0;
248 /* forward declarations */
249 void jtag_add_pathmove(int num_states
, enum tap_state
*path
);
250 void jtag_add_runtest(int num_cycles
, enum tap_state endstate
);
251 void jtag_add_end_state(enum tap_state endstate
);
252 void jtag_add_sleep(u32 us
);
253 int jtag_execute_queue(void);
254 int jtag_cancel_queue(void);
257 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
258 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
259 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
260 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
261 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
262 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
263 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
265 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
267 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
268 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
269 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
270 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
271 int handle_drscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
273 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
275 int jtag_register_event_callback(int (*callback
)(enum jtag_event event
, void *priv
), void *priv
)
277 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
279 if (callback
== NULL
)
281 return ERROR_INVALID_ARGUMENTS
;
286 while ((*callbacks_p
)->next
)
287 callbacks_p
= &((*callbacks_p
)->next
);
288 callbacks_p
= &((*callbacks_p
)->next
);
291 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
292 (*callbacks_p
)->callback
= callback
;
293 (*callbacks_p
)->priv
= priv
;
294 (*callbacks_p
)->next
= NULL
;
299 int jtag_unregister_event_callback(int (*callback
)(enum jtag_event event
, void *priv
))
301 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
303 if (callback
== NULL
)
305 return ERROR_INVALID_ARGUMENTS
;
310 jtag_event_callback_t
**next
= &((*callbacks_p
)->next
);
311 if ((*callbacks_p
)->callback
== callback
)
314 *callbacks_p
= *next
;
322 int jtag_call_event_callbacks(enum jtag_event event
)
324 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
326 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
330 callback
->callback(event
, callback
->priv
);
331 callback
= callback
->next
;
337 /* returns a pointer to the pointer of the last command in queue
338 * this may be a pointer to the root pointer (jtag_command_queue)
339 * or to the next member of the last but one command
341 jtag_command_t
** jtag_get_last_command_p(void)
343 /* jtag_command_t *cmd = jtag_command_queue;
349 return &jtag_command_queue;
353 return last_comand_pointer
;
356 /* returns a pointer to the n-th device in the scan chain */
357 jtag_device_t
* jtag_get_device(int num
)
359 jtag_device_t
*device
= jtag_devices
;
366 device
= device
->next
;
370 LOG_ERROR("jtag device number %d not defined", num
);
374 void* cmd_queue_alloc(size_t size
)
376 cmd_queue_page_t
**p_page
= &cmd_queue_pages
;
382 while ((*p_page
)->next
)
383 p_page
= &((*p_page
)->next
);
384 if (CMD_QUEUE_PAGE_SIZE
- (*p_page
)->used
< size
)
385 p_page
= &((*p_page
)->next
);
390 *p_page
= malloc(sizeof(cmd_queue_page_t
));
392 (*p_page
)->address
= malloc(CMD_QUEUE_PAGE_SIZE
);
393 (*p_page
)->next
= NULL
;
396 offset
= (*p_page
)->used
;
397 (*p_page
)->used
+= size
;
399 t
=(u8
*)((*p_page
)->address
);
403 void cmd_queue_free()
405 cmd_queue_page_t
*page
= cmd_queue_pages
;
409 cmd_queue_page_t
*last
= page
;
415 cmd_queue_pages
= NULL
;
418 static void jtag_prelude1()
422 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
423 jtag_error
=ERROR_JTAG_TRST_ASSERTED
;
427 if (cmd_queue_end_state
== TAP_TLR
)
428 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
431 static void jtag_prelude(enum tap_state state
)
436 jtag_add_end_state(state
);
438 cmd_queue_cur_state
= cmd_queue_end_state
;
441 void jtag_add_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
447 retval
=interface_jtag_add_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
448 if (retval
!=ERROR_OK
)
452 int MINIDRIVER(interface_jtag_add_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
454 jtag_command_t
**last_cmd
;
455 jtag_device_t
*device
;
460 last_cmd
= jtag_get_last_command_p();
462 /* allocate memory for a new list member */
463 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
464 (*last_cmd
)->next
= NULL
;
465 last_comand_pointer
= &((*last_cmd
)->next
);
466 (*last_cmd
)->type
= JTAG_SCAN
;
468 /* allocate memory for ir scan command */
469 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
470 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
471 (*last_cmd
)->cmd
.scan
->num_fields
= jtag_num_devices
; /* one field per device */
472 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(jtag_num_devices
* sizeof(scan_field_t
));
473 (*last_cmd
)->cmd
.scan
->end_state
= state
;
475 for (i
= 0; i
< jtag_num_devices
; i
++)
478 device
= jtag_get_device(i
);
479 scan_size
= device
->ir_length
;
480 (*last_cmd
)->cmd
.scan
->fields
[i
].device
= i
;
481 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= scan_size
;
482 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= NULL
;
483 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= NULL
; /* disable verification by default */
485 /* search the list */
486 for (j
= 0; j
< num_fields
; j
++)
488 if (i
== fields
[j
].device
)
491 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
492 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
494 if (jtag_verify_capture_ir
)
496 if (fields
[j
].in_handler
==NULL
)
498 jtag_set_check_value((*last_cmd
)->cmd
.scan
->fields
+i
, device
->expected
, device
->expected_mask
, NULL
);
501 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= fields
[j
].in_handler
;
502 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= fields
[j
].in_handler_priv
;
503 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= device
->expected
;
504 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= device
->expected_mask
;
515 /* if a device isn't listed, set it to BYPASS */
516 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_set_ones(cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
517 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= NULL
;
522 /* update device information */
523 buf_cpy((*last_cmd
)->cmd
.scan
->fields
[i
].out_value
, jtag_get_device(i
)->cur_instr
, scan_size
);
529 void jtag_add_plain_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
535 retval
=interface_jtag_add_plain_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
536 if (retval
!=ERROR_OK
)
540 int MINIDRIVER(interface_jtag_add_plain_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
543 jtag_command_t
**last_cmd
;
545 last_cmd
= jtag_get_last_command_p();
547 /* allocate memory for a new list member */
548 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
549 (*last_cmd
)->next
= NULL
;
550 last_comand_pointer
= &((*last_cmd
)->next
);
551 (*last_cmd
)->type
= JTAG_SCAN
;
553 /* allocate memory for ir scan command */
554 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
555 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
556 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
557 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
558 (*last_cmd
)->cmd
.scan
->end_state
= state
;
560 for (i
= 0; i
< num_fields
; i
++)
562 int num_bits
= fields
[i
].num_bits
;
563 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
564 (*last_cmd
)->cmd
.scan
->fields
[i
].device
= fields
[i
].device
;
565 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
566 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
567 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
568 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
569 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
570 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
571 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= NULL
;
572 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= NULL
;
577 void jtag_add_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
583 retval
=interface_jtag_add_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
584 if (retval
!=ERROR_OK
)
588 int MINIDRIVER(interface_jtag_add_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
591 int bypass_devices
= 0;
595 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
596 jtag_device_t
*device
= jtag_devices
;
598 /* count devices in bypass */
603 device
= device
->next
;
606 /* allocate memory for a new list member */
607 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
608 last_comand_pointer
= &((*last_cmd
)->next
);
609 (*last_cmd
)->next
= NULL
;
610 (*last_cmd
)->type
= JTAG_SCAN
;
612 /* allocate memory for dr scan command */
613 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
614 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
615 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
616 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
617 (*last_cmd
)->cmd
.scan
->end_state
= state
;
619 for (i
= 0; i
< jtag_num_devices
; i
++)
622 (*last_cmd
)->cmd
.scan
->fields
[field_count
].device
= i
;
624 for (j
= 0; j
< num_fields
; j
++)
626 if (i
== fields
[j
].device
)
629 scan_size
= fields
[j
].num_bits
;
630 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
631 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
632 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
633 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= fields
[j
].in_value
;
634 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= fields
[j
].in_check_value
;
635 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= fields
[j
].in_check_mask
;
636 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= fields
[j
].in_handler
;
637 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= fields
[j
].in_handler_priv
;
642 #ifdef _DEBUG_JTAG_IO_
643 /* if a device isn't listed, the BYPASS register should be selected */
644 if (!jtag_get_device(i
)->bypass
)
646 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
650 /* program the scan field to 1 bit length, and ignore it's value */
651 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
652 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
653 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
654 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
655 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
656 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
657 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
658 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
662 #ifdef _DEBUG_JTAG_IO_
663 /* if a device is listed, the BYPASS register must not be selected */
664 if (jtag_get_device(i
)->bypass
)
666 LOG_ERROR("BUG: scan data for a device in BYPASS");
675 void MINIDRIVER(interface_jtag_add_dr_out
)(int device_num
,
679 enum tap_state end_state
)
684 int bypass_devices
= 0;
686 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
687 jtag_device_t
*device
= jtag_devices
;
688 /* count devices in bypass */
693 device
= device
->next
;
696 /* allocate memory for a new list member */
697 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
698 last_comand_pointer
= &((*last_cmd
)->next
);
699 (*last_cmd
)->next
= NULL
;
700 (*last_cmd
)->type
= JTAG_SCAN
;
702 /* allocate memory for dr scan command */
703 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
704 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
705 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
706 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
707 (*last_cmd
)->cmd
.scan
->end_state
= end_state
;
709 for (i
= 0; i
< jtag_num_devices
; i
++)
711 (*last_cmd
)->cmd
.scan
->fields
[field_count
].device
= i
;
716 #ifdef _DEBUG_JTAG_IO_
717 /* if a device is listed, the BYPASS register must not be selected */
718 if (jtag_get_device(i
)->bypass
)
720 LOG_ERROR("BUG: scan data for a device in BYPASS");
724 for (j
= 0; j
< num_fields
; j
++)
727 scan_size
= num_bits
[j
];
728 buf_set_u32(out_value
, 0, scan_size
, value
[j
]);
729 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
730 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
731 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
732 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
733 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
734 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
735 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
736 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
740 #ifdef _DEBUG_JTAG_IO_
741 /* if a device isn't listed, the BYPASS register should be selected */
742 if (!jtag_get_device(i
)->bypass
)
744 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
748 /* program the scan field to 1 bit length, and ignore it's value */
749 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
750 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
751 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
752 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
753 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
754 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
755 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
756 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
764 void jtag_add_plain_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
770 retval
=interface_jtag_add_plain_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
771 if (retval
!=ERROR_OK
)
775 int MINIDRIVER(interface_jtag_add_plain_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
778 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
780 /* allocate memory for a new list member */
781 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
782 last_comand_pointer
= &((*last_cmd
)->next
);
783 (*last_cmd
)->next
= NULL
;
784 (*last_cmd
)->type
= JTAG_SCAN
;
786 /* allocate memory for scan command */
787 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
788 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
789 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
790 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
791 (*last_cmd
)->cmd
.scan
->end_state
= state
;
793 for (i
= 0; i
< num_fields
; i
++)
795 int num_bits
= fields
[i
].num_bits
;
796 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
797 (*last_cmd
)->cmd
.scan
->fields
[i
].device
= fields
[i
].device
;
798 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
799 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
800 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
801 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
802 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
803 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
804 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= fields
[i
].in_handler
;
805 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= fields
[i
].in_handler_priv
;
813 jtag_prelude(TAP_TLR
);
816 retval
=interface_jtag_add_tlr();
817 if (retval
!=ERROR_OK
)
821 int MINIDRIVER(interface_jtag_add_tlr
)()
823 enum tap_state state
= TAP_TLR
;
824 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
826 /* allocate memory for a new list member */
827 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
828 last_comand_pointer
= &((*last_cmd
)->next
);
829 (*last_cmd
)->next
= NULL
;
830 (*last_cmd
)->type
= JTAG_STATEMOVE
;
832 (*last_cmd
)->cmd
.statemove
= cmd_queue_alloc(sizeof(statemove_command_t
));
833 (*last_cmd
)->cmd
.statemove
->end_state
= state
;
839 void jtag_add_pathmove(int num_states
, enum tap_state
*path
)
841 enum tap_state cur_state
=cmd_queue_cur_state
;
845 /* the last state has to be a stable state */
846 if (tap_move_map
[path
[num_states
- 1]] == -1)
848 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
852 for (i
=0; i
<num_states
; i
++)
854 if ((tap_transitions
[cur_state
].low
!= path
[i
])&&
855 (tap_transitions
[cur_state
].high
!= path
[i
]))
857 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings
[cur_state
], tap_state_strings
[path
[i
]]);
865 cmd_queue_cur_state
= path
[num_states
- 1];
867 retval
=interface_jtag_add_pathmove(num_states
, path
);
868 if (retval
!=ERROR_OK
)
873 int MINIDRIVER(interface_jtag_add_pathmove
)(int num_states
, enum tap_state
*path
)
875 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
878 /* allocate memory for a new list member */
879 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
880 last_comand_pointer
= &((*last_cmd
)->next
);
881 (*last_cmd
)->next
= NULL
;
882 (*last_cmd
)->type
= JTAG_PATHMOVE
;
884 (*last_cmd
)->cmd
.pathmove
= cmd_queue_alloc(sizeof(pathmove_command_t
));
885 (*last_cmd
)->cmd
.pathmove
->num_states
= num_states
;
886 (*last_cmd
)->cmd
.pathmove
->path
= cmd_queue_alloc(sizeof(enum tap_state
) * num_states
);
888 for (i
= 0; i
< num_states
; i
++)
889 (*last_cmd
)->cmd
.pathmove
->path
[i
] = path
[i
];
894 int MINIDRIVER(interface_jtag_add_runtest
)(int num_cycles
, enum tap_state state
)
896 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
898 /* allocate memory for a new list member */
899 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
900 (*last_cmd
)->next
= NULL
;
901 last_comand_pointer
= &((*last_cmd
)->next
);
902 (*last_cmd
)->type
= JTAG_RUNTEST
;
904 (*last_cmd
)->cmd
.runtest
= cmd_queue_alloc(sizeof(runtest_command_t
));
905 (*last_cmd
)->cmd
.runtest
->num_cycles
= num_cycles
;
906 (*last_cmd
)->cmd
.runtest
->end_state
= state
;
911 void jtag_add_runtest(int num_cycles
, enum tap_state state
)
917 /* executed by sw or hw fifo */
918 retval
=interface_jtag_add_runtest(num_cycles
, cmd_queue_end_state
);
919 if (retval
!=ERROR_OK
)
923 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
925 int trst_with_tlr
= 0;
928 /* FIX!!! there are *many* different cases here. A better
929 * approach is needed for legal combinations of transitions...
931 if ((jtag_reset_config
& RESET_HAS_SRST
)&&
932 (jtag_reset_config
& RESET_HAS_TRST
)&&
933 ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0))
935 if (((req_tlr_or_trst
&&!jtag_trst
)||
936 (!req_tlr_or_trst
&&jtag_trst
))&&
937 ((req_srst
&&!jtag_srst
)||
938 (!req_srst
&&jtag_srst
)))
940 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
941 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
945 /* Make sure that jtag_reset_config allows the requested reset */
946 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
947 if (((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && (req_srst
== 1)) && (!req_tlr_or_trst
))
949 LOG_ERROR("BUG: requested reset would assert trst");
950 jtag_error
=ERROR_FAIL
;
954 /* if TRST pulls SRST, we reset with TAP T-L-R */
955 if (((jtag_reset_config
& RESET_TRST_PULLS_SRST
) && (req_tlr_or_trst
)) && (req_srst
== 0))
960 if (req_srst
&& !(jtag_reset_config
& RESET_HAS_SRST
))
962 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
963 jtag_error
=ERROR_FAIL
;
969 if (!trst_with_tlr
&& (jtag_reset_config
& RESET_HAS_TRST
))
981 jtag_srst
= req_srst
;
983 retval
= interface_jtag_add_reset(jtag_trst
, jtag_srst
);
984 if (retval
!=ERROR_OK
)
992 LOG_DEBUG("SRST line asserted");
996 LOG_DEBUG("SRST line released");
997 if (jtag_nsrst_delay
)
998 jtag_add_sleep(jtag_nsrst_delay
* 1000);
1003 LOG_DEBUG("JTAG reset with TLR instead of TRST");
1004 jtag_add_end_state(TAP_TLR
);
1006 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1012 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1013 * and inform possible listeners about this
1015 LOG_DEBUG("TRST line asserted");
1016 cmd_queue_cur_state
= TAP_TLR
;
1017 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1021 if (jtag_ntrst_delay
)
1022 jtag_add_sleep(jtag_ntrst_delay
* 1000);
1026 int MINIDRIVER(interface_jtag_add_reset
)(int req_trst
, int req_srst
)
1028 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1030 /* allocate memory for a new list member */
1031 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1032 (*last_cmd
)->next
= NULL
;
1033 last_comand_pointer
= &((*last_cmd
)->next
);
1034 (*last_cmd
)->type
= JTAG_RESET
;
1036 (*last_cmd
)->cmd
.reset
= cmd_queue_alloc(sizeof(reset_command_t
));
1037 (*last_cmd
)->cmd
.reset
->trst
= req_trst
;
1038 (*last_cmd
)->cmd
.reset
->srst
= req_srst
;
1044 void jtag_add_end_state(enum tap_state state
)
1046 cmd_queue_end_state
= state
;
1047 if ((cmd_queue_end_state
== TAP_SD
)||(cmd_queue_end_state
== TAP_SD
))
1049 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1053 int MINIDRIVER(interface_jtag_add_sleep
)(u32 us
)
1055 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1057 /* allocate memory for a new list member */
1058 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1059 (*last_cmd
)->next
= NULL
;
1060 last_comand_pointer
= &((*last_cmd
)->next
);
1061 (*last_cmd
)->type
= JTAG_SLEEP
;
1063 (*last_cmd
)->cmd
.sleep
= cmd_queue_alloc(sizeof(sleep_command_t
));
1064 (*last_cmd
)->cmd
.sleep
->us
= us
;
1069 void jtag_add_sleep(u32 us
)
1071 int retval
=interface_jtag_add_sleep(us
);
1072 if (retval
!=ERROR_OK
)
1077 int jtag_scan_size(scan_command_t
*cmd
)
1082 /* count bits in scan command */
1083 for (i
= 0; i
< cmd
->num_fields
; i
++)
1085 bit_count
+= cmd
->fields
[i
].num_bits
;
1091 int jtag_build_buffer(scan_command_t
*cmd
, u8
**buffer
)
1096 bit_count
= jtag_scan_size(cmd
);
1097 *buffer
= malloc(CEIL(bit_count
, 8));
1101 for (i
= 0; i
< cmd
->num_fields
; i
++)
1103 if (cmd
->fields
[i
].out_value
)
1105 #ifdef _DEBUG_JTAG_IO_
1106 char* char_buf
= buf_to_str(cmd
->fields
[i
].out_value
, (cmd
->fields
[i
].num_bits
> 64) ? 64 : cmd
->fields
[i
].num_bits
, 16);
1108 buf_set_buf(cmd
->fields
[i
].out_value
, 0, *buffer
, bit_count
, cmd
->fields
[i
].num_bits
);
1109 #ifdef _DEBUG_JTAG_IO_
1110 LOG_DEBUG("fields[%i].out_value: 0x%s", i
, char_buf
);
1115 bit_count
+= cmd
->fields
[i
].num_bits
;
1122 int jtag_read_buffer(u8
*buffer
, scan_command_t
*cmd
)
1128 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1131 for (i
= 0; i
< cmd
->num_fields
; i
++)
1133 /* if neither in_value nor in_handler
1134 * are specified we don't have to examine this field
1136 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1138 int num_bits
= cmd
->fields
[i
].num_bits
;
1139 u8
*captured
= buf_set_buf(buffer
, bit_count
, malloc(CEIL(num_bits
, 8)), 0, num_bits
);
1141 #ifdef _DEBUG_JTAG_IO_
1144 char_buf
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1145 LOG_DEBUG("fields[%i].in_value: 0x%s", i
, char_buf
);
1149 if (cmd
->fields
[i
].in_value
)
1151 buf_cpy(captured
, cmd
->fields
[i
].in_value
, num_bits
);
1153 if (cmd
->fields
[i
].in_handler
)
1155 if (cmd
->fields
[i
].in_handler(cmd
->fields
[i
].in_value
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1157 LOG_WARNING("in_handler reported a failed check");
1158 retval
= ERROR_JTAG_QUEUE_FAILED
;
1163 /* no in_value specified, but a handler takes care of the scanned data */
1164 if (cmd
->fields
[i
].in_handler
&& (!cmd
->fields
[i
].in_value
))
1166 if (cmd
->fields
[i
].in_handler(captured
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1168 /* We're going to call the error:handler later, but if the in_handler
1169 * reported an error we report this failure upstream
1171 LOG_WARNING("in_handler reported a failed check");
1172 retval
= ERROR_JTAG_QUEUE_FAILED
;
1178 bit_count
+= cmd
->fields
[i
].num_bits
;
1184 int jtag_check_value(u8
*captured
, void *priv
, scan_field_t
*field
)
1186 int retval
= ERROR_OK
;
1187 int num_bits
= field
->num_bits
;
1189 int compare_failed
= 0;
1191 if (field
->in_check_mask
)
1192 compare_failed
= buf_cmp_mask(captured
, field
->in_check_value
, field
->in_check_mask
, num_bits
);
1194 compare_failed
= buf_cmp(captured
, field
->in_check_value
, num_bits
);
1198 /* An error handler could have caught the failing check
1199 * only report a problem when there wasn't a handler, or if the handler
1200 * acknowledged the error
1204 char *captured_char
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1205 char *in_check_value_char
= buf_to_str(field
->in_check_value
, (num_bits
> 64) ? 64 : num_bits
, 16);
1207 if (field
->in_check_mask
)
1209 char *in_check_mask_char
;
1210 in_check_mask_char
= buf_to_str(field
->in_check_mask
, (num_bits
> 64) ? 64 : num_bits
, 16);
1211 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
);
1212 free(in_check_mask_char
);
1216 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
);
1219 free(captured_char
);
1220 free(in_check_value_char
);
1222 retval
= ERROR_JTAG_QUEUE_FAILED
;
1230 set up checking of this field using the in_handler. The values passed in must be valid until
1231 after jtag_execute() has completed.
1233 void jtag_set_check_value(scan_field_t
*field
, u8
*value
, u8
*mask
, error_handler_t
*in_error_handler
)
1236 field
->in_handler
= jtag_check_value
;
1238 field
->in_handler
= NULL
; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1239 field
->in_handler_priv
= NULL
;
1240 field
->in_check_value
= value
;
1241 field
->in_check_mask
= mask
;
1244 enum scan_type
jtag_scan_type(scan_command_t
*cmd
)
1249 for (i
= 0; i
< cmd
->num_fields
; i
++)
1251 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1253 if (cmd
->fields
[i
].out_value
)
1260 int MINIDRIVER(interface_jtag_execute_queue
)(void)
1266 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1270 retval
= jtag
->execute_queue();
1274 jtag_command_queue
= NULL
;
1275 last_comand_pointer
= &jtag_command_queue
;
1280 int jtag_execute_queue(void)
1282 int retval
=interface_jtag_execute_queue();
1283 if (retval
==ERROR_OK
)
1287 jtag_error
=ERROR_OK
;
1291 int jtag_reset_callback(enum jtag_event event
, void *priv
)
1293 jtag_device_t
*device
= priv
;
1297 if (event
== JTAG_TRST_ASSERTED
)
1299 buf_set_ones(device
->cur_instr
, device
->ir_length
);
1306 void jtag_sleep(u32 us
)
1311 /* Try to examine chain layout according to IEEE 1149.1 §12
1313 int jtag_examine_chain()
1315 jtag_device_t
*device
= jtag_devices
;
1317 u8 idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
1320 int device_count
= 0;
1321 u8 zero_check
= 0x0;
1322 u8 one_check
= 0xff;
1325 field
.num_bits
= sizeof(idcode_buffer
) * 8;
1326 field
.out_value
= idcode_buffer
;
1327 field
.out_mask
= NULL
;
1328 field
.in_value
= idcode_buffer
;
1329 field
.in_check_value
= NULL
;
1330 field
.in_check_mask
= NULL
;
1331 field
.in_handler
= NULL
;
1332 field
.in_handler_priv
= NULL
;
1334 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
1336 buf_set_u32(idcode_buffer
, i
* 32, 32, 0x000000FF);
1339 jtag_add_plain_dr_scan(1, &field
, TAP_TLR
);
1340 jtag_execute_queue();
1342 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
* 4; i
++)
1344 zero_check
|= idcode_buffer
[i
];
1345 one_check
&= idcode_buffer
[i
];
1348 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1349 if ((zero_check
== 0x00) || (one_check
== 0xff))
1351 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1352 return ERROR_JTAG_INIT_FAILED
;
1355 for (bit_count
= 0; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;)
1357 u32 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1358 if ((idcode
& 1) == 0)
1360 /* LSB must not be 0, this indicates a device in bypass */
1371 if (idcode
== 0x000000FF)
1373 /* End of chain (invalid manufacturer ID) */
1379 device
->idcode
= idcode
;
1380 device
= device
->next
;
1384 manufacturer
= (idcode
& 0xffe) >> 1;
1385 part
= (idcode
& 0xffff000) >> 12;
1386 version
= (idcode
& 0xf0000000) >> 28;
1388 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1389 idcode
, manufacturer
, part
, version
);
1395 /* see if number of discovered devices matches configuration */
1396 if (device_count
!= jtag_num_devices
)
1398 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1399 device_count
, jtag_num_devices
);
1400 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1401 return ERROR_JTAG_INIT_FAILED
;
1407 int jtag_validate_chain()
1409 jtag_device_t
*device
= jtag_devices
;
1410 int total_ir_length
= 0;
1417 total_ir_length
+= device
->ir_length
;
1418 device
= device
->next
;
1421 total_ir_length
+= 2;
1422 ir_test
= malloc(CEIL(total_ir_length
, 8));
1423 buf_set_ones(ir_test
, total_ir_length
);
1426 field
.num_bits
= total_ir_length
;
1427 field
.out_value
= ir_test
;
1428 field
.out_mask
= NULL
;
1429 field
.in_value
= ir_test
;
1430 field
.in_check_value
= NULL
;
1431 field
.in_check_mask
= NULL
;
1432 field
.in_handler
= NULL
;
1433 field
.in_handler_priv
= NULL
;
1435 jtag_add_plain_ir_scan(1, &field
, TAP_TLR
);
1436 jtag_execute_queue();
1438 device
= jtag_devices
;
1441 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x1)
1443 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1444 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1447 return ERROR_JTAG_INIT_FAILED
;
1449 chain_pos
+= device
->ir_length
;
1450 device
= device
->next
;
1453 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x3)
1455 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1456 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1459 return ERROR_JTAG_INIT_FAILED
;
1467 int jtag_register_commands(struct command_context_s
*cmd_ctx
)
1469 register_command(cmd_ctx
, NULL
, "interface", handle_interface_command
,
1470 COMMAND_CONFIG
, NULL
);
1471 register_command(cmd_ctx
, NULL
, "jtag_speed", handle_jtag_speed_command
,
1472 COMMAND_ANY
, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1473 register_command(cmd_ctx
, NULL
, "jtag_khz", handle_jtag_khz_command
,
1474 COMMAND_ANY
, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1475 register_command(cmd_ctx
, NULL
, "jtag_device", handle_jtag_device_command
,
1476 COMMAND_CONFIG
, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1477 register_command(cmd_ctx
, NULL
, "reset_config", handle_reset_config_command
,
1478 COMMAND_CONFIG
, NULL
);
1479 register_command(cmd_ctx
, NULL
, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command
,
1480 COMMAND_ANY
, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1481 register_command(cmd_ctx
, NULL
, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command
,
1482 COMMAND_ANY
, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1484 register_command(cmd_ctx
, NULL
, "scan_chain", handle_scan_chain_command
,
1485 COMMAND_EXEC
, "print current scan chain configuration");
1487 register_command(cmd_ctx
, NULL
, "endstate", handle_endstate_command
,
1488 COMMAND_EXEC
, "finish JTAG operations in <tap_state>");
1489 register_command(cmd_ctx
, NULL
, "jtag_reset", handle_jtag_reset_command
,
1490 COMMAND_EXEC
, "toggle reset lines <trst> <srst>");
1491 register_command(cmd_ctx
, NULL
, "runtest", handle_runtest_command
,
1492 COMMAND_EXEC
, "move to Run-Test/Idle, and execute <num_cycles>");
1493 register_command(cmd_ctx
, NULL
, "irscan", handle_irscan_command
,
1494 COMMAND_EXEC
, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1495 register_command(cmd_ctx
, NULL
, "drscan", handle_drscan_command
,
1496 COMMAND_EXEC
, "execute DR scan <device> <var> [dev2] [var2] ...");
1498 register_command(cmd_ctx
, NULL
, "verify_ircapture", handle_verify_ircapture_command
,
1499 COMMAND_ANY
, "verify value captured during Capture-IR <enable|disable>");
1503 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
1508 if (!jtag_interface
)
1510 /* nothing was previously specified by "interface" command */
1511 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1512 return ERROR_JTAG_INVALID_INTERFACE
;
1516 /*stay on "reset speed"*/
1517 jtag_interface
->khz(speed1
, &jtag_speed
);
1518 jtag_interface
->khz(speed2
, &jtag_speed_post_reset
);
1522 if (jtag_interface
->init() != ERROR_OK
)
1523 return ERROR_JTAG_INIT_FAILED
;
1527 jtag
= jtag_interface
;
1531 static int jtag_init_inner(struct command_context_s
*cmd_ctx
)
1533 int validate_tries
= 0;
1534 jtag_device_t
*device
;
1537 LOG_DEBUG("Init JTAG chain");
1539 device
= jtag_devices
;
1540 jtag_ir_scan_size
= 0;
1541 jtag_num_devices
= 0;
1542 while (device
!= NULL
)
1544 jtag_ir_scan_size
+= device
->ir_length
;
1546 device
= device
->next
;
1550 if ((retval
=jtag_execute_queue())!=ERROR_OK
)
1553 /* examine chain first, as this could discover the real chain layout */
1554 if (jtag_examine_chain() != ERROR_OK
)
1556 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1559 while (jtag_validate_chain() != ERROR_OK
)
1563 if (validate_tries
> 5)
1565 LOG_ERROR("Could not validate JTAG chain, exit");
1566 return ERROR_JTAG_INVALID_INTERFACE
;
1574 int jtag_init_reset(struct command_context_s
*cmd_ctx
)
1578 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1581 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
1583 /* Reset can happen after a power cycle.
1585 * Ideally we would only assert TRST or run TLR before the target reset.
1587 * However w/srst_pulls_trst, trst is asserted together with the target
1588 * reset whether we want it or not.
1590 * NB! Some targets have JTAG circuitry disabled until a
1591 * trst & srst has been asserted.
1593 * NB! here we assume nsrst/ntrst delay are sufficient!
1595 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1598 jtag_add_reset(1, 0); /* TLR or TRST */
1599 if (jtag_reset_config
& RESET_HAS_SRST
)
1601 jtag_add_reset(1, 1);
1602 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0)
1603 jtag_add_reset(0, 1);
1605 jtag_add_reset(0, 0);
1606 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
1609 /* Check that we can communication on the JTAG chain + eventually we want to
1610 * be able to perform enumeration only after OpenOCD has started
1611 * telnet and GDB server
1613 * That would allow users to more easily perform any magic they need to before
1616 return jtag_init_inner(cmd_ctx
);
1619 int jtag_init(struct command_context_s
*cmd_ctx
)
1622 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1624 if (jtag_init_inner(cmd_ctx
)==ERROR_OK
)
1628 return jtag_init_reset(cmd_ctx
);
1632 static int default_khz(int khz
, int *jtag_speed
)
1634 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1638 static int default_speed_div(int speed
, int *khz
)
1643 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1647 /* check whether the interface is already configured */
1650 LOG_WARNING("Interface already configured, ignoring");
1654 /* interface name is a mandatory argument */
1655 if (argc
< 1 || args
[0][0] == '\0')
1657 return ERROR_COMMAND_SYNTAX_ERROR
;
1660 for (i
=0; jtag_interfaces
[i
]; i
++)
1662 if (strcmp(args
[0], jtag_interfaces
[i
]->name
) == 0)
1664 if (jtag_interfaces
[i
]->register_commands(cmd_ctx
) != ERROR_OK
)
1667 jtag_interface
= jtag_interfaces
[i
];
1669 if (jtag_interface
->khz
== NULL
)
1671 jtag_interface
->khz
= default_khz
;
1673 if (jtag_interface
->speed_div
== NULL
)
1675 jtag_interface
->speed_div
= default_speed_div
;
1681 /* no valid interface was found (i.e. the configuration option,
1682 * didn't match one of the compiled-in interfaces
1684 LOG_ERROR("No valid jtag interface found (%s)", args
[0]);
1685 LOG_ERROR("compiled-in jtag interfaces:");
1686 for (i
= 0; jtag_interfaces
[i
]; i
++)
1688 LOG_ERROR("%i: %s", i
, jtag_interfaces
[i
]->name
);
1691 return ERROR_JTAG_INVALID_INTERFACE
;
1694 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1696 jtag_device_t
**last_device_p
= &jtag_devices
;
1700 while ((*last_device_p
)->next
)
1701 last_device_p
= &((*last_device_p
)->next
);
1702 last_device_p
= &((*last_device_p
)->next
);
1708 *last_device_p
= malloc(sizeof(jtag_device_t
));
1709 (*last_device_p
)->ir_length
= strtoul(args
[0], NULL
, 0);
1711 (*last_device_p
)->expected
= malloc((*last_device_p
)->ir_length
);
1712 buf_set_u32((*last_device_p
)->expected
, 0, (*last_device_p
)->ir_length
, strtoul(args
[1], NULL
, 0));
1713 (*last_device_p
)->expected_mask
= malloc((*last_device_p
)->ir_length
);
1714 buf_set_u32((*last_device_p
)->expected_mask
, 0, (*last_device_p
)->ir_length
, strtoul(args
[2], NULL
, 0));
1716 (*last_device_p
)->cur_instr
= malloc((*last_device_p
)->ir_length
);
1717 (*last_device_p
)->bypass
= 1;
1718 buf_set_ones((*last_device_p
)->cur_instr
, (*last_device_p
)->ir_length
);
1720 (*last_device_p
)->next
= NULL
;
1722 jtag_register_event_callback(jtag_reset_callback
, (*last_device_p
));
1729 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1731 jtag_device_t
*device
= jtag_devices
;
1732 int device_count
= 0;
1736 u32 expected
, expected_mask
, cur_instr
;
1737 expected
= buf_get_u32(device
->expected
, 0, device
->ir_length
);
1738 expected_mask
= buf_get_u32(device
->expected_mask
, 0, device
->ir_length
);
1739 cur_instr
= buf_get_u32(device
->cur_instr
, 0, device
->ir_length
);
1740 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
);
1741 device
= device
->next
;
1748 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1751 return ERROR_COMMAND_SYNTAX_ERROR
;
1755 if (strcmp(args
[0], "none") == 0)
1756 jtag_reset_config
= RESET_NONE
;
1757 else if (strcmp(args
[0], "trst_only") == 0)
1758 jtag_reset_config
= RESET_HAS_TRST
;
1759 else if (strcmp(args
[0], "srst_only") == 0)
1760 jtag_reset_config
= RESET_HAS_SRST
;
1761 else if (strcmp(args
[0], "trst_and_srst") == 0)
1762 jtag_reset_config
= RESET_TRST_AND_SRST
;
1765 LOG_ERROR("invalid reset_config argument, defaulting to none");
1766 jtag_reset_config
= RESET_NONE
;
1767 return ERROR_INVALID_ARGUMENTS
;
1773 if (strcmp(args
[1], "separate") == 0)
1775 /* seperate reset lines - default */
1778 if (strcmp(args
[1], "srst_pulls_trst") == 0)
1779 jtag_reset_config
|= RESET_SRST_PULLS_TRST
;
1780 else if (strcmp(args
[1], "trst_pulls_srst") == 0)
1781 jtag_reset_config
|= RESET_TRST_PULLS_SRST
;
1782 else if (strcmp(args
[1], "combined") == 0)
1783 jtag_reset_config
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
1786 LOG_ERROR("invalid reset_config argument, defaulting to none");
1787 jtag_reset_config
= RESET_NONE
;
1788 return ERROR_INVALID_ARGUMENTS
;
1795 if (strcmp(args
[2], "trst_open_drain") == 0)
1796 jtag_reset_config
|= RESET_TRST_OPEN_DRAIN
;
1797 else if (strcmp(args
[2], "trst_push_pull") == 0)
1798 jtag_reset_config
&= ~RESET_TRST_OPEN_DRAIN
;
1801 LOG_ERROR("invalid reset_config argument, defaulting to none");
1802 jtag_reset_config
= RESET_NONE
;
1803 return ERROR_INVALID_ARGUMENTS
;
1809 if (strcmp(args
[3], "srst_push_pull") == 0)
1810 jtag_reset_config
|= RESET_SRST_PUSH_PULL
;
1811 else if (strcmp(args
[3], "srst_open_drain") == 0)
1812 jtag_reset_config
&= ~RESET_SRST_PUSH_PULL
;
1815 LOG_ERROR("invalid reset_config argument, defaulting to none");
1816 jtag_reset_config
= RESET_NONE
;
1817 return ERROR_INVALID_ARGUMENTS
;
1824 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1828 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1833 jtag_nsrst_delay
= strtoul(args
[0], NULL
, 0);
1839 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1843 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1848 jtag_ntrst_delay
= strtoul(args
[0], NULL
, 0);
1854 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1860 if ((argc
<1) || (argc
>2))
1861 return ERROR_COMMAND_SYNTAX_ERROR
;
1863 LOG_DEBUG("handle jtag speed");
1866 cur_speed
= jtag_speed
= jtag_speed_post_reset
= strtoul(args
[0], NULL
, 0);
1868 cur_speed
= jtag_speed_post_reset
= strtoul(args
[1], NULL
, 0);
1870 /* this command can be called during CONFIG,
1871 * in which case jtag isn't initialized */
1874 jtag
->speed_div(jtag_speed
, &speed1
);
1875 jtag
->speed_div(jtag_speed_post_reset
, &speed2
);
1876 jtag
->speed(cur_speed
);
1879 command_print(cmd_ctx
, "jtag_speed: %d, %d", jtag_speed
, jtag_speed_post_reset
);
1884 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1886 LOG_DEBUG("handle jtag khz");
1889 return ERROR_COMMAND_SYNTAX_ERROR
;
1895 speed1
= speed2
= strtoul(args
[0], NULL
, 0);
1897 speed2
= strtoul(args
[1], NULL
, 0);
1902 LOG_DEBUG("have interface set up");
1903 int speed_div1
, speed_div2
;
1904 if (jtag
->khz(speed1
, &speed_div1
)!=ERROR_OK
)
1906 speed1
= speed2
= 0;
1909 if (jtag
->khz(speed2
, &speed_div2
)!=ERROR_OK
)
1911 speed1
= speed2
= 0;
1916 cur_speed
= jtag_speed
= jtag_speed_post_reset
= speed_div1
;
1918 cur_speed
= jtag_speed_post_reset
= speed_div2
;
1920 jtag
->speed(cur_speed
);
1926 command_print(cmd_ctx
, "jtag_khz: %d, %d", speed1
, speed2
);
1932 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1934 enum tap_state state
;
1938 return ERROR_COMMAND_SYNTAX_ERROR
;
1942 for (state
= 0; state
< 16; state
++)
1944 if (strcmp(args
[0], tap_state_strings
[state
]) == 0)
1946 jtag_add_end_state(state
);
1947 jtag_execute_queue();
1951 command_print(cmd_ctx
, "current endstate: %s", tap_state_strings
[cmd_queue_end_state
]);
1956 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1963 return ERROR_COMMAND_SYNTAX_ERROR
;
1966 if (args
[0][0] == '1')
1968 else if (args
[0][0] == '0')
1972 return ERROR_COMMAND_SYNTAX_ERROR
;
1975 if (args
[1][0] == '1')
1977 else if (args
[1][0] == '0')
1981 return ERROR_COMMAND_SYNTAX_ERROR
;
1984 if (jtag_interface_init(cmd_ctx
) != ERROR_OK
)
1985 return ERROR_JTAG_INIT_FAILED
;
1987 jtag_add_reset(trst
, srst
);
1988 jtag_execute_queue();
1993 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1997 return ERROR_COMMAND_SYNTAX_ERROR
;
2000 jtag_add_runtest(strtol(args
[0], NULL
, 0), -1);
2001 jtag_execute_queue();
2008 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2011 scan_field_t
*fields
;
2013 if ((argc
< 2) || (argc
% 2))
2015 return ERROR_COMMAND_SYNTAX_ERROR
;
2018 fields
= malloc(sizeof(scan_field_t
) * argc
/ 2);
2020 for (i
= 0; i
< argc
/ 2; i
++)
2022 int device
= strtoul(args
[i
*2], NULL
, 0);
2023 int field_size
= jtag_get_device(device
)->ir_length
;
2024 fields
[i
].device
= device
;
2025 fields
[i
].out_value
= malloc(CEIL(field_size
, 8));
2026 buf_set_u32(fields
[i
].out_value
, 0, field_size
, strtoul(args
[i
*2+1], NULL
, 0));
2027 fields
[i
].out_mask
= NULL
;
2028 fields
[i
].in_value
= NULL
;
2029 fields
[i
].in_check_mask
= NULL
;
2030 fields
[i
].in_handler
= NULL
;
2031 fields
[i
].in_handler_priv
= NULL
;
2034 jtag_add_ir_scan(argc
/ 2, fields
, -1);
2035 jtag_execute_queue();
2037 for (i
= 0; i
< argc
/ 2; i
++)
2038 free(fields
[i
].out_value
);
2045 int handle_drscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2047 scan_field_t
*fields
;
2049 int field_count
= 0;
2053 if ((argc
< 2) || (argc
% 2))
2055 return ERROR_COMMAND_SYNTAX_ERROR
;
2058 for (i
= 0; i
< argc
; i
+=2)
2060 var
= get_var_by_namenum(args
[i
+1]);
2063 num_fields
+= var
->num_fields
;
2067 command_print(cmd_ctx
, "variable %s doesn't exist", args
[i
+1]);
2072 fields
= malloc(sizeof(scan_field_t
) * num_fields
);
2074 for (i
= 0; i
< argc
; i
+=2)
2076 var
= get_var_by_namenum(args
[i
+1]);
2078 for (j
= 0; j
< var
->num_fields
; j
++)
2080 fields
[field_count
].device
= strtol(args
[i
], NULL
, 0);
2081 fields
[field_count
].num_bits
= var
->fields
[j
].num_bits
;
2082 fields
[field_count
].out_value
= malloc(CEIL(var
->fields
[j
].num_bits
, 8));
2083 buf_set_u32(fields
[field_count
].out_value
, 0, var
->fields
[j
].num_bits
, var
->fields
[j
].value
);
2084 fields
[field_count
].out_mask
= NULL
;
2085 fields
[field_count
].in_value
= fields
[field_count
].out_value
;
2086 fields
[field_count
].in_check_mask
= NULL
;
2087 fields
[field_count
].in_check_value
= NULL
;
2088 fields
[field_count
].in_handler
= field_le_to_host
;
2089 fields
[field_count
++].in_handler_priv
= &(var
->fields
[j
]);
2093 jtag_add_dr_scan(num_fields
, fields
, -1);
2094 jtag_execute_queue();
2096 for (i
= 0; i
< argc
/ 2; i
++)
2097 free(fields
[i
].out_value
);
2104 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2108 if (strcmp(args
[0], "enable") == 0)
2110 jtag_verify_capture_ir
= 1;
2112 else if (strcmp(args
[0], "disable") == 0)
2114 jtag_verify_capture_ir
= 0;
2117 return ERROR_COMMAND_SYNTAX_ERROR
;
2119 } else if (argc
!= 0)
2121 return ERROR_COMMAND_SYNTAX_ERROR
;
2124 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)