1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
35 #include "minidriver.h"
36 #include "interface.h"
43 /// The number of JTAG queue flushes (for profiling and debugging purposes).
44 static int jtag_flush_queue_count
;
46 static void jtag_add_scan_check(void (*jtag_add_scan
)(int in_num_fields
, const scan_field_t
*in_fields
, tap_state_t state
),
47 int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
);
50 * The jtag_error variable is set when an error occurs while executing
51 * the queue. Application code may set this using jtag_set_error(),
52 * when an error occurs during processing that should be reported during
53 * jtag_execute_queue().
55 * Tts value may be checked with jtag_get_error() and cleared with
56 * jtag_error_clear(). This value is returned (and cleared) by
57 * jtag_execute_queue().
59 static int jtag_error
= ERROR_OK
;
61 static const char *jtag_event_strings
[] =
63 [JTAG_TRST_ASSERTED
] = "JTAG controller reset (RESET or TRST)",
64 [JTAG_TAP_EVENT_ENABLE
] = "TAP enabled",
65 [JTAG_TAP_EVENT_DISABLE
] = "TAP disabled",
68 static int jtag_trst
= 0;
69 static int jtag_srst
= 0;
72 * List all TAPs that have been created.
74 static jtag_tap_t
*__jtag_all_taps
= NULL
;
76 * The number of TAPs in the __jtag_all_taps list, used to track the
77 * assigned chain position to new TAPs
79 static unsigned jtag_num_taps
= 0;
81 static enum reset_types jtag_reset_config
= RESET_NONE
;
82 static tap_state_t cmd_queue_end_state
= TAP_RESET
;
83 tap_state_t cmd_queue_cur_state
= TAP_RESET
;
85 static bool jtag_verify_capture_ir
= true;
86 static int jtag_verify
= 1;
88 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
89 static int jtag_nsrst_delay
= 0; /* default to no nSRST delay */
90 static int jtag_ntrst_delay
= 0; /* default to no nTRST delay */
92 typedef struct jtag_event_callback_s
94 jtag_event_handler_t callback
;
96 struct jtag_event_callback_s
* next
;
97 } jtag_event_callback_t
;
99 /* callbacks to inform high-level handlers about JTAG state changes */
100 static jtag_event_callback_t
*jtag_event_callbacks
;
103 static int speed_khz
= 0;
104 /* flag if the kHz speed was defined */
105 static bool hasKHz
= false;
106 static int jtag_speed
= 0;
108 static struct jtag_interface_s
*jtag
= NULL
;
111 jtag_interface_t
*jtag_interface
= NULL
;
113 void jtag_set_error(int error
)
115 if ((error
== ERROR_OK
) || (jtag_error
!= ERROR_OK
))
119 int jtag_get_error(void)
123 int jtag_error_clear(void)
125 int temp
= jtag_error
;
126 jtag_error
= ERROR_OK
;
131 jtag_tap_t
*jtag_all_taps(void)
133 return __jtag_all_taps
;
136 unsigned jtag_tap_count(void)
138 return jtag_num_taps
;
141 unsigned jtag_tap_count_enabled(void)
143 jtag_tap_t
*t
= jtag_all_taps();
154 /// Append a new TAP to the chain of all taps.
155 void jtag_tap_add(struct jtag_tap_s
*t
)
157 t
->abs_chain_position
= jtag_num_taps
++;
159 jtag_tap_t
**tap
= &__jtag_all_taps
;
161 tap
= &(*tap
)->next_tap
;
165 jtag_tap_t
*jtag_tap_by_string(const char *s
)
167 /* try by name first */
168 jtag_tap_t
*t
= jtag_all_taps();
171 if (0 == strcmp(t
->dotted_name
, s
))
176 /* no tap found by name, so try to parse the name as a number */
178 if (parse_uint(s
, &n
) != ERROR_OK
)
181 return jtag_tap_by_position(n
);
184 jtag_tap_t
*jtag_tap_by_jim_obj(Jim_Interp
*interp
, Jim_Obj
*o
)
186 const char *cp
= Jim_GetString(o
, NULL
);
187 jtag_tap_t
*t
= cp
? jtag_tap_by_string(cp
) : NULL
;
191 Jim_SetResult_sprintf(interp
, "Tap '%s' could not be found", cp
);
195 /* returns a pointer to the n-th device in the scan chain */
196 jtag_tap_t
*jtag_tap_by_position(unsigned n
)
198 jtag_tap_t
*t
= jtag_all_taps();
206 jtag_tap_t
* jtag_tap_next_enabled(jtag_tap_t
* p
)
208 p
= p
? p
->next_tap
: jtag_all_taps();
218 const char *jtag_tap_name(const jtag_tap_t
*tap
)
220 return (tap
== NULL
) ? "(unknown)" : tap
->dotted_name
;
224 int jtag_register_event_callback(jtag_event_handler_t callback
, void *priv
)
226 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
228 if (callback
== NULL
)
230 return ERROR_INVALID_ARGUMENTS
;
235 while ((*callbacks_p
)->next
)
236 callbacks_p
= &((*callbacks_p
)->next
);
237 callbacks_p
= &((*callbacks_p
)->next
);
240 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
241 (*callbacks_p
)->callback
= callback
;
242 (*callbacks_p
)->priv
= priv
;
243 (*callbacks_p
)->next
= NULL
;
248 int jtag_unregister_event_callback(jtag_event_handler_t callback
, void *priv
)
250 jtag_event_callback_t
**callbacks_p
;
251 jtag_event_callback_t
**next
;
253 if (callback
== NULL
)
255 return ERROR_INVALID_ARGUMENTS
;
258 for (callbacks_p
= &jtag_event_callbacks
;
259 *callbacks_p
!= NULL
;
262 next
= &((*callbacks_p
)->next
);
264 if ((*callbacks_p
)->priv
!= priv
)
267 if ((*callbacks_p
)->callback
== callback
)
270 *callbacks_p
= *next
;
277 int jtag_call_event_callbacks(enum jtag_event event
)
279 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
281 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
285 jtag_event_callback_t
*next
;
287 /* callback may remove itself */
288 next
= callback
->next
;
289 callback
->callback(event
, callback
->priv
);
296 static void jtag_checks(void)
298 assert(jtag_trst
== 0);
301 static void jtag_prelude(tap_state_t state
)
305 assert(state
!=TAP_INVALID
);
307 cmd_queue_cur_state
= state
;
310 void jtag_alloc_in_value32(scan_field_t
*field
)
312 interface_jtag_alloc_in_value32(field
);
315 void jtag_add_ir_scan_noverify(int in_count
, const scan_field_t
*in_fields
,
320 int retval
= interface_jtag_add_ir_scan(in_count
, in_fields
, state
);
321 jtag_set_error(retval
);
325 void jtag_add_ir_scan(int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
327 if (jtag_verify
&&jtag_verify_capture_ir
)
329 /* 8 x 32 bit id's is enough for all invocations */
331 for (int j
= 0; j
< in_num_fields
; j
++)
333 /* if we are to run a verification of the ir scan, we need to get the input back.
334 * We may have to allocate space if the caller didn't ask for the input back.
336 in_fields
[j
].check_value
=in_fields
[j
].tap
->expected
;
337 in_fields
[j
].check_mask
=in_fields
[j
].tap
->expected_mask
;
339 jtag_add_scan_check(jtag_add_ir_scan_noverify
, in_num_fields
, in_fields
, state
);
342 jtag_add_ir_scan_noverify(in_num_fields
, in_fields
, state
);
346 void jtag_add_plain_ir_scan(int in_num_fields
, const scan_field_t
*in_fields
,
351 int retval
= interface_jtag_add_plain_ir_scan(
352 in_num_fields
, in_fields
, state
);
353 jtag_set_error(retval
);
356 void jtag_add_callback(jtag_callback1_t f
, uint8_t *in
)
358 interface_jtag_add_callback(f
, in
);
361 void jtag_add_callback4(jtag_callback_t f
, uint8_t *in
,
362 jtag_callback_data_t data1
, jtag_callback_data_t data2
,
363 jtag_callback_data_t data3
)
365 interface_jtag_add_callback4(f
, in
, data1
, data2
, data3
);
368 int jtag_check_value_inner(uint8_t *captured
, uint8_t *in_check_value
, uint8_t *in_check_mask
, int num_bits
);
370 static int jtag_check_value_mask_callback(uint8_t *in
, jtag_callback_data_t data1
, jtag_callback_data_t data2
, jtag_callback_data_t data3
)
372 return jtag_check_value_inner(in
, (uint8_t *)data1
, (uint8_t *)data2
, (int)data3
);
375 static void jtag_add_scan_check(void (*jtag_add_scan
)(int in_num_fields
, const scan_field_t
*in_fields
, tap_state_t state
),
376 int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
378 for (int i
= 0; i
< in_num_fields
; i
++)
380 struct scan_field_s
*field
= &in_fields
[i
];
381 field
->allocated
= 0;
383 if (field
->check_value
|| field
->in_value
)
385 interface_jtag_add_scan_check_alloc(field
);
389 jtag_add_scan(in_num_fields
, in_fields
, state
);
391 for (int i
= 0; i
< in_num_fields
; i
++)
393 if ((in_fields
[i
].check_value
!= NULL
) && (in_fields
[i
].in_value
!= NULL
))
395 /* this is synchronous for a minidriver */
396 jtag_add_callback4(jtag_check_value_mask_callback
, in_fields
[i
].in_value
,
397 (jtag_callback_data_t
)in_fields
[i
].check_value
,
398 (jtag_callback_data_t
)in_fields
[i
].check_mask
,
399 (jtag_callback_data_t
)in_fields
[i
].num_bits
);
401 if (in_fields
[i
].allocated
)
403 free(in_fields
[i
].in_value
);
405 if (in_fields
[i
].modified
)
407 in_fields
[i
].in_value
= NULL
;
412 void jtag_add_dr_scan_check(int in_num_fields
, scan_field_t
*in_fields
, tap_state_t state
)
416 jtag_add_scan_check(jtag_add_dr_scan
, in_num_fields
, in_fields
, state
);
419 jtag_add_dr_scan(in_num_fields
, in_fields
, state
);
424 void jtag_add_dr_scan(int in_num_fields
, const scan_field_t
*in_fields
,
430 retval
= interface_jtag_add_dr_scan(in_num_fields
, in_fields
, state
);
431 jtag_set_error(retval
);
434 void jtag_add_plain_dr_scan(int in_num_fields
, const scan_field_t
*in_fields
,
440 retval
= interface_jtag_add_plain_dr_scan(in_num_fields
, in_fields
, state
);
441 jtag_set_error(retval
);
444 void jtag_add_dr_out(jtag_tap_t
* tap
,
445 int num_fields
, const int* num_bits
, const u32
* value
,
446 tap_state_t end_state
)
448 assert(end_state
!= TAP_INVALID
);
450 cmd_queue_cur_state
= end_state
;
452 interface_jtag_add_dr_out(tap
,
453 num_fields
, num_bits
, value
,
457 void jtag_add_tlr(void)
459 jtag_prelude(TAP_RESET
);
460 jtag_set_error(interface_jtag_add_tlr());
461 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
464 void jtag_add_pathmove(int num_states
, const tap_state_t
*path
)
466 tap_state_t cur_state
= cmd_queue_cur_state
;
468 /* the last state has to be a stable state */
469 if (!tap_is_state_stable(path
[num_states
- 1]))
471 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
472 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE
);
476 for (int i
= 0; i
< num_states
; i
++)
478 if (path
[i
] == TAP_RESET
)
480 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
481 jtag_set_error(ERROR_JTAG_STATE_INVALID
);
485 if ( tap_state_transition(cur_state
, true) != path
[i
]
486 && tap_state_transition(cur_state
, false) != path
[i
])
488 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
489 tap_state_name(cur_state
), tap_state_name(path
[i
]));
490 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID
);
498 jtag_set_error(interface_jtag_add_pathmove(num_states
, path
));
499 cmd_queue_cur_state
= path
[num_states
- 1];
502 int jtag_add_statemove(tap_state_t goal_state
)
504 tap_state_t cur_state
= cmd_queue_cur_state
;
506 LOG_DEBUG( "cur_state=%s goal_state=%s",
507 tap_state_name(cur_state
),
508 tap_state_name(goal_state
) );
511 if (goal_state
==cur_state
)
512 ; /* nothing to do */
513 else if( goal_state
==TAP_RESET
)
517 else if( tap_is_state_stable(cur_state
) && tap_is_state_stable(goal_state
) )
519 unsigned tms_bits
= tap_get_tms_path(cur_state
, goal_state
);
520 unsigned tms_count
= tap_get_tms_path_len(cur_state
, goal_state
);
521 tap_state_t moves
[8];
522 assert(tms_count
< DIM(moves
));
524 for (unsigned i
= 0; i
< tms_count
; i
++, tms_bits
>>= 1)
526 bool bit
= tms_bits
& 1;
528 cur_state
= tap_state_transition(cur_state
, bit
);
529 moves
[i
] = cur_state
;
532 jtag_add_pathmove(tms_count
, moves
);
534 else if( tap_state_transition(cur_state
, true) == goal_state
535 || tap_state_transition(cur_state
, false) == goal_state
)
537 jtag_add_pathmove(1, &goal_state
);
546 void jtag_add_runtest(int num_cycles
, tap_state_t state
)
549 jtag_set_error(interface_jtag_add_runtest(num_cycles
, state
));
553 void jtag_add_clocks(int num_cycles
)
555 if (!tap_is_state_stable(cmd_queue_cur_state
))
557 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
558 tap_state_name(cmd_queue_cur_state
));
559 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE
);
566 jtag_set_error(interface_jtag_add_clocks(num_cycles
));
570 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
572 int trst_with_tlr
= 0;
574 /* FIX!!! there are *many* different cases here. A better
575 * approach is needed for legal combinations of transitions...
577 if ((jtag_reset_config
& RESET_HAS_SRST
)&&
578 (jtag_reset_config
& RESET_HAS_TRST
)&&
579 ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0))
581 if (((req_tlr_or_trst
&&!jtag_trst
)||
582 (!req_tlr_or_trst
&&jtag_trst
))&&
583 ((req_srst
&&!jtag_srst
)||
584 (!req_srst
&&jtag_srst
)))
586 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
587 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
591 /* Make sure that jtag_reset_config allows the requested reset */
592 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
593 if (((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && (req_srst
== 1)) && (!req_tlr_or_trst
))
595 LOG_ERROR("BUG: requested reset would assert trst");
596 jtag_set_error(ERROR_FAIL
);
600 /* if TRST pulls SRST, we reset with TAP T-L-R */
601 if (((jtag_reset_config
& RESET_TRST_PULLS_SRST
) && (req_tlr_or_trst
)) && (req_srst
== 0))
606 if (req_srst
&& !(jtag_reset_config
& RESET_HAS_SRST
))
608 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
609 jtag_set_error(ERROR_FAIL
);
615 if (!trst_with_tlr
&& (jtag_reset_config
& RESET_HAS_TRST
))
627 jtag_srst
= req_srst
;
629 int retval
= interface_jtag_add_reset(jtag_trst
, jtag_srst
);
630 if (retval
!= ERROR_OK
)
632 jtag_set_error(retval
);
635 jtag_execute_queue();
639 LOG_DEBUG("SRST line asserted");
643 LOG_DEBUG("SRST line released");
644 if (jtag_nsrst_delay
)
645 jtag_add_sleep(jtag_nsrst_delay
* 1000);
650 LOG_DEBUG("JTAG reset with RESET instead of TRST");
651 jtag_set_end_state(TAP_RESET
);
658 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
659 * and inform possible listeners about this
661 LOG_DEBUG("TRST line asserted");
662 tap_set_state(TAP_RESET
);
663 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
667 if (jtag_ntrst_delay
)
668 jtag_add_sleep(jtag_ntrst_delay
* 1000);
672 tap_state_t
jtag_set_end_state(tap_state_t state
)
674 if ((state
== TAP_DRSHIFT
)||(state
== TAP_IRSHIFT
))
676 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
679 if (state
!=TAP_INVALID
)
680 cmd_queue_end_state
= state
;
681 return cmd_queue_end_state
;
684 tap_state_t
jtag_get_end_state(void)
686 return cmd_queue_end_state
;
689 void jtag_add_sleep(u32 us
)
691 /// @todo Here, keep_alive() appears to be a layering violation!!!
693 jtag_set_error(interface_jtag_add_sleep(us
));
696 int jtag_check_value_inner(uint8_t *captured
, uint8_t *in_check_value
, uint8_t *in_check_mask
, int num_bits
)
698 int retval
= ERROR_OK
;
700 int compare_failed
= 0;
703 compare_failed
= buf_cmp_mask(captured
, in_check_value
, in_check_mask
, num_bits
);
705 compare_failed
= buf_cmp(captured
, in_check_value
, num_bits
);
708 /* An error handler could have caught the failing check
709 * only report a problem when there wasn't a handler, or if the handler
710 * acknowledged the error
713 LOG_WARNING("TAP %s:",
714 jtag_tap_name(field->tap));
718 char *captured_char
= buf_to_str(captured
, (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
, 16);
719 char *in_check_value_char
= buf_to_str(in_check_value
, (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
, 16);
723 char *in_check_mask_char
;
724 in_check_mask_char
= buf_to_str(in_check_mask
, (num_bits
> DEBUG_JTAG_IOZ
) ? DEBUG_JTAG_IOZ
: num_bits
, 16);
725 LOG_WARNING("value captured during scan didn't pass the requested check:");
726 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
727 captured_char
, in_check_value_char
, in_check_mask_char
);
728 free(in_check_mask_char
);
732 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
);
736 free(in_check_value_char
);
738 retval
= ERROR_JTAG_QUEUE_FAILED
;
745 void jtag_check_value_mask(scan_field_t
*field
, uint8_t *value
, uint8_t *mask
)
747 assert(field
->in_value
!= NULL
);
751 /* no checking to do */
755 jtag_execute_queue_noclear();
757 int retval
=jtag_check_value_inner(field
->in_value
, value
, mask
, field
->num_bits
);
758 jtag_set_error(retval
);
763 int default_interface_jtag_execute_queue(void)
767 LOG_ERROR("No JTAG interface configured yet. "
768 "Issue 'init' command in startup scripts "
769 "before communicating with targets.");
773 return jtag
->execute_queue();
776 void jtag_execute_queue_noclear(void)
778 jtag_flush_queue_count
++;
779 jtag_set_error(interface_jtag_execute_queue());
782 int jtag_get_flush_queue_count(void)
784 return jtag_flush_queue_count
;
787 int jtag_execute_queue(void)
789 jtag_execute_queue_noclear();
790 return jtag_error_clear();
793 static int jtag_reset_callback(enum jtag_event event
, void *priv
)
795 jtag_tap_t
*tap
= priv
;
799 if (event
== JTAG_TRST_ASSERTED
)
801 tap
->enabled
= !tap
->disabled_after_reset
;
803 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
810 void jtag_sleep(u32 us
)
812 alive_sleep(us
/1000);
815 /// maximum number of JTAG devices expected in the chain
816 #define JTAG_MAX_CHAIN_SIZE 20
818 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
819 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
820 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
822 static int jtag_examine_chain_execute(uint8_t *idcode_buffer
, unsigned num_idcode
)
824 scan_field_t field
= {
826 .num_bits
= num_idcode
* 32,
827 .out_value
= idcode_buffer
,
828 .in_value
= idcode_buffer
,
831 // initialize to the end of chain ID value
832 for (unsigned i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
833 buf_set_u32(idcode_buffer
, i
* 32, 32, 0x000000FF);
835 jtag_add_plain_dr_scan(1, &field
, TAP_RESET
);
836 return jtag_execute_queue();
839 static bool jtag_examine_chain_check(uint8_t *idcodes
, unsigned count
)
841 uint8_t zero_check
= 0x0;
842 uint8_t one_check
= 0xff;
844 for (unsigned i
= 0; i
< count
* 4; i
++)
846 zero_check
|= idcodes
[i
];
847 one_check
&= idcodes
[i
];
850 /* if there wasn't a single non-zero bit or if all bits were one,
851 * the scan is not valid */
852 if (zero_check
== 0x00 || one_check
== 0xff)
854 LOG_ERROR("JTAG communication failure: check connection, "
855 "JTAG interface, target power etc.");
861 static void jtag_examine_chain_display(enum log_levels level
, const char *msg
,
862 const char *name
, u32 idcode
)
864 log_printf_lf(level
, __FILE__
, __LINE__
, __FUNCTION__
,
865 "JTAG tap: %s %16.16s: 0x%08x "
866 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
868 EXTRACT_MFG(idcode
), EXTRACT_PART(idcode
), EXTRACT_VER(idcode
) );
871 static bool jtag_idcode_is_final(u32 idcode
)
873 return idcode
== 0x000000FF || idcode
== 0xFFFFFFFF;
877 * This helper checks that remaining bits in the examined chain data are
878 * all as expected, but a single JTAG device requires only 64 bits to be
879 * read back correctly. This can help identify and diagnose problems
880 * with the JTAG chain earlier, gives more helpful/explicit error messages.
882 static void jtag_examine_chain_end(uint8_t *idcodes
, unsigned count
, unsigned max
)
884 bool triggered
= false;
885 for ( ; count
< max
- 31; count
+= 32)
887 u32 idcode
= buf_get_u32(idcodes
, count
, 32);
888 // do not trigger the warning if the data looks good
889 if (!triggered
&& jtag_idcode_is_final(idcode
))
891 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
897 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s
*tap
)
899 if (0 == tap
->expected_ids_cnt
)
901 /// @todo Enable LOG_INFO to ask for reports about unknown TAP IDs.
903 LOG_INFO("Uknown JTAG TAP ID: 0x%08x", tap
->idcode
)
904 LOG_INFO("Please report the chip name and reported ID code to the openocd project");
909 /* Loop over the expected identification codes and test for a match */
911 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++)
913 if (tap
->idcode
== tap
->expected_ids
[ii
])
917 /* If none of the expected ids matched, log an error */
918 if (ii
!= tap
->expected_ids_cnt
)
920 LOG_INFO("JTAG Tap/device matched");
923 jtag_examine_chain_display(LOG_LVL_ERROR
, "got",
924 tap
->dotted_name
, tap
->idcode
);
925 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++)
928 snprintf(msg
, sizeof(msg
), "expected %hhu of %hhu",
929 ii
+ 1, tap
->expected_ids_cnt
);
930 jtag_examine_chain_display(LOG_LVL_ERROR
, msg
,
931 tap
->dotted_name
, tap
->expected_ids
[ii
]);
936 /* Try to examine chain layout according to IEEE 1149.1 §12
938 int jtag_examine_chain(void)
940 uint8_t idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
941 unsigned device_count
= 0;
943 jtag_examine_chain_execute(idcode_buffer
, JTAG_MAX_CHAIN_SIZE
);
945 if (!jtag_examine_chain_check(idcode_buffer
, JTAG_MAX_CHAIN_SIZE
))
946 return ERROR_JTAG_INIT_FAILED
;
948 /* point at the 1st tap */
949 jtag_tap_t
*tap
= jtag_tap_next_enabled(NULL
);
952 LOG_ERROR("JTAG: No taps enabled?");
953 return ERROR_JTAG_INIT_FAILED
;
956 for (unsigned bit_count
= 0; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;)
958 u32 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
959 if ((idcode
& 1) == 0)
961 /* LSB must not be 0, this indicates a device in bypass */
962 LOG_WARNING("Tap/Device does not have IDCODE");
970 * End of chain (invalid manufacturer ID) some devices, such
971 * as AVR will output all 1's instead of TDI input value at
974 if (jtag_idcode_is_final(idcode
))
976 jtag_examine_chain_end(idcode_buffer
,
977 bit_count
+ 32, JTAG_MAX_CHAIN_SIZE
* 32);
981 jtag_examine_chain_display(LOG_LVL_INFO
, "tap/device found",
982 tap
? tap
->dotted_name
: "(not-named)",
991 tap
->idcode
= idcode
;
993 // ensure the TAP ID does matches what was expected
994 if (!jtag_examine_chain_match_tap(tap
))
995 return ERROR_JTAG_INIT_FAILED
;
997 tap
= jtag_tap_next_enabled(tap
);
1000 /* see if number of discovered devices matches configuration */
1001 if (device_count
!= jtag_tap_count_enabled())
1003 LOG_ERROR("number of discovered devices in JTAG chain (%i) "
1004 "does not match (enabled) configuration (%i), total taps: %d",
1005 device_count
, jtag_tap_count_enabled(), jtag_tap_count());
1006 LOG_ERROR("check the config file and ensure proper JTAG communication"
1007 " (connections, speed, ...)");
1008 return ERROR_JTAG_INIT_FAILED
;
1014 int jtag_validate_chain(void)
1017 int total_ir_length
= 0;
1018 uint8_t *ir_test
= NULL
;
1023 total_ir_length
= 0;
1025 tap
= jtag_tap_next_enabled(tap
);
1029 total_ir_length
+= tap
->ir_length
;
1032 total_ir_length
+= 2;
1033 ir_test
= malloc(CEIL(total_ir_length
, 8));
1034 buf_set_ones(ir_test
, total_ir_length
);
1037 field
.num_bits
= total_ir_length
;
1038 field
.out_value
= ir_test
;
1039 field
.in_value
= ir_test
;
1042 jtag_add_plain_ir_scan(1, &field
, TAP_RESET
);
1043 jtag_execute_queue();
1049 tap
= jtag_tap_next_enabled(tap
);
1054 val
= buf_get_u32(ir_test
, chain_pos
, 2);
1057 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1058 LOG_ERROR("Could not validate JTAG scan chain, IR mismatch, scan returned 0x%s. tap=%s pos=%d expected 0x1 got %0x", cbuf
, jtag_tap_name(tap
), chain_pos
, val
);
1061 return ERROR_JTAG_INIT_FAILED
;
1063 chain_pos
+= tap
->ir_length
;
1066 val
= buf_get_u32(ir_test
, chain_pos
, 2);
1069 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1070 LOG_ERROR("Could not validate end of JTAG scan chain, IR mismatch, scan returned 0x%s. pos=%d expected 0x3 got %0x", cbuf
, chain_pos
, val
);
1073 return ERROR_JTAG_INIT_FAILED
;
1082 void jtag_tap_init(jtag_tap_t
*tap
)
1084 assert(0 != tap
->ir_length
);
1086 tap
->expected
= malloc(tap
->ir_length
);
1087 tap
->expected_mask
= malloc(tap
->ir_length
);
1088 tap
->cur_instr
= malloc(tap
->ir_length
);
1090 /// @todo cope sanely with ir_length bigger than 32 bits
1091 buf_set_u32(tap
->expected
, 0, tap
->ir_length
, tap
->ir_capture_value
);
1092 buf_set_u32(tap
->expected_mask
, 0, tap
->ir_length
, tap
->ir_capture_mask
);
1093 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1095 // place TAP in bypass mode
1097 // register the reset callback for the TAP
1098 jtag_register_event_callback(&jtag_reset_callback
, tap
);
1100 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1101 "irlen %d, capture: 0x%x mask: 0x%x", tap
->dotted_name
,
1102 tap
->abs_chain_position
, tap
->ir_length
,
1103 tap
->ir_capture_value
, tap
->ir_capture_mask
);
1107 void jtag_tap_free(jtag_tap_t
*tap
)
1109 jtag_unregister_event_callback(&jtag_reset_callback
, tap
);
1111 /// @todo is anything missing? no memory leaks please
1112 free((void *)tap
->expected_ids
);
1113 free((void *)tap
->chip
);
1114 free((void *)tap
->tapname
);
1115 free((void *)tap
->dotted_name
);
1119 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
1124 if (!jtag_interface
)
1126 /* nothing was previously specified by "interface" command */
1127 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1128 return ERROR_JTAG_INVALID_INTERFACE
;
1132 jtag_interface
->khz(jtag_get_speed_khz(), &jtag_speed
);
1136 if (jtag_interface
->init() != ERROR_OK
)
1137 return ERROR_JTAG_INIT_FAILED
;
1139 jtag
= jtag_interface
;
1143 static int jtag_init_inner(struct command_context_s
*cmd_ctx
)
1148 LOG_DEBUG("Init JTAG chain");
1150 tap
= jtag_tap_next_enabled(NULL
);
1152 LOG_ERROR("There are no enabled taps?");
1153 return ERROR_JTAG_INIT_FAILED
;
1157 if ((retval
=jtag_execute_queue())!=ERROR_OK
)
1160 /* examine chain first, as this could discover the real chain layout */
1161 if (jtag_examine_chain() != ERROR_OK
)
1163 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1166 if (jtag_validate_chain() != ERROR_OK
)
1168 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
1174 int jtag_interface_quit(void)
1176 if (!jtag
|| !jtag
->quit
)
1179 // close the JTAG interface
1180 int result
= jtag
->quit();
1181 if (ERROR_OK
!= result
)
1182 LOG_ERROR("failed: %d", result
);
1188 int jtag_init_reset(struct command_context_s
*cmd_ctx
)
1192 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1195 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
1197 /* Reset can happen after a power cycle.
1199 * Ideally we would only assert TRST or run RESET before the target reset.
1201 * However w/srst_pulls_trst, trst is asserted together with the target
1202 * reset whether we want it or not.
1204 * NB! Some targets have JTAG circuitry disabled until a
1205 * trst & srst has been asserted.
1207 * NB! here we assume nsrst/ntrst delay are sufficient!
1209 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1212 jtag_add_reset(1, 0); /* RESET or TRST */
1213 if (jtag_reset_config
& RESET_HAS_SRST
)
1215 jtag_add_reset(1, 1);
1216 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0)
1217 jtag_add_reset(0, 1);
1219 jtag_add_reset(0, 0);
1220 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
1223 /* Check that we can communication on the JTAG chain + eventually we want to
1224 * be able to perform enumeration only after OpenOCD has started
1225 * telnet and GDB server
1227 * That would allow users to more easily perform any magic they need to before
1230 return jtag_init_inner(cmd_ctx
);
1233 int jtag_init(struct command_context_s
*cmd_ctx
)
1236 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
1238 if (jtag_init_inner(cmd_ctx
)==ERROR_OK
)
1242 return jtag_init_reset(cmd_ctx
);
1245 void jtag_set_speed_khz(unsigned khz
)
1249 unsigned jtag_get_speed_khz(void)
1253 int jtag_config_khz(unsigned khz
)
1255 LOG_DEBUG("handle jtag khz");
1256 jtag_set_speed_khz(khz
);
1261 LOG_DEBUG("have interface set up");
1263 int retval
= jtag
->khz(jtag_get_speed_khz(), &speed_div1
);
1264 if (ERROR_OK
!= retval
)
1266 jtag_set_speed_khz(0);
1269 cur_speed
= speed_div1
;
1271 return jtag_set_speed(cur_speed
);
1274 int jtag_get_speed(void)
1279 int jtag_set_speed(int speed
)
1282 /* this command can be called during CONFIG,
1283 * in which case jtag isn't initialized */
1285 return jtag
? jtag
->speed(speed
) : ERROR_OK
;
1288 int jtag_get_speed_readable(int *speed
)
1290 return jtag
? jtag
->speed_div(jtag_get_speed(), speed
) : ERROR_OK
;
1294 void jtag_set_verify(bool enable
)
1296 jtag_verify
= enable
;
1299 bool jtag_will_verify()
1304 void jtag_set_verify_capture_ir(bool enable
)
1306 jtag_verify_capture_ir
= enable
;
1309 bool jtag_will_verify_capture_ir()
1311 return jtag_verify_capture_ir
;
1314 int jtag_power_dropout(int *dropout
)
1316 return jtag
->power_dropout(dropout
);
1319 int jtag_srst_asserted(int *srst_asserted
)
1321 return jtag
->srst_asserted(srst_asserted
);
1324 enum reset_types
jtag_get_reset_config(void)
1326 return jtag_reset_config
;
1328 void jtag_set_reset_config(enum reset_types type
)
1330 jtag_reset_config
= type
;
1333 int jtag_get_trst(void)
1337 int jtag_get_srst(void)
1342 void jtag_set_nsrst_delay(unsigned delay
)
1344 jtag_nsrst_delay
= delay
;
1346 unsigned jtag_get_nsrst_delay(void)
1348 return jtag_nsrst_delay
;
1350 void jtag_set_ntrst_delay(unsigned delay
)
1352 jtag_ntrst_delay
= delay
;
1354 unsigned jtag_get_ntrst_delay(void)
1356 return jtag_ntrst_delay
;