OpenOCD
jtag.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 * *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
9 ***************************************************************************/
10 
11 #ifndef OPENOCD_JTAG_JTAG_H
12 #define OPENOCD_JTAG_JTAG_H
13 
14 #include <helper/binarybuffer.h>
15 #include <helper/command.h>
16 #include <helper/log.h>
17 #include <helper/replacements.h>
18 
19 #ifndef DEBUG_JTAG_IOZ
20 #define DEBUG_JTAG_IOZ 64
21 #endif
22 
23 /*-----</Macros>-------------------------------------------------*/
24 
37 typedef enum tap_state {
39 
40  /* Proper ARM recommended numbers */
41  TAP_DREXIT2 = 0x0,
42  TAP_DREXIT1 = 0x1,
43  TAP_DRSHIFT = 0x2,
44  TAP_DRPAUSE = 0x3,
45  TAP_IRSELECT = 0x4,
46  TAP_DRUPDATE = 0x5,
48  TAP_DRSELECT = 0x7,
49  TAP_IREXIT2 = 0x8,
50  TAP_IREXIT1 = 0x9,
51  TAP_IRSHIFT = 0xa,
52  TAP_IRPAUSE = 0xb,
53  TAP_IDLE = 0xc,
54  TAP_IRUPDATE = 0xd,
56  TAP_RESET = 0x0f,
58 
62 #define SRST_DEASSERT 0
63 #define SRST_ASSERT 1
64 #define TRST_DEASSERT 0
65 #define TRST_ASSERT 1
66 
71 const char *tap_state_name(tap_state_t state);
72 
75 
78 
87 struct scan_field {
89  unsigned int num_bits;
91  const uint8_t *out_value;
93  uint8_t *in_value;
94 
96  uint8_t *check_value;
98  uint8_t *check_mask;
99 };
100 
101 struct jtag_tap {
102  char *chip;
103  char *tapname;
104  char *dotted_name;
105  unsigned int abs_chain_position;
109  bool enabled;
110  unsigned int ir_length;
112  uint8_t *expected;
113  uint32_t ir_capture_mask;
114  uint8_t *expected_mask;
115  uint32_t idcode;
119 
121  uint32_t *expected_ids;
124 
127 
130 
132  uint8_t *cur_instr;
134  bool bypass;
135 
137  uint64_t ir_bypass_value;
138 
140 
142  /* private pointer to support none-jtag specific functions */
143  void *priv;
144 };
145 
146 void jtag_tap_init(struct jtag_tap *tap);
147 void jtag_tap_free(struct jtag_tap *tap);
148 
149 struct jtag_tap *jtag_all_taps(void);
150 const char *jtag_tap_name(const struct jtag_tap *tap);
151 struct jtag_tap *jtag_tap_by_string(const char *dotted_name);
152 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *obj);
153 struct jtag_tap *jtag_tap_by_position(unsigned int abs_position);
154 struct jtag_tap *jtag_tap_next_enabled(struct jtag_tap *p);
155 unsigned int jtag_tap_count_enabled(void);
156 
157 /*
158  * - TRST_ASSERTED triggers two sets of callbacks, after operations to
159  * reset the scan chain -- via TMS+TCK signaling, or deasserting the
160  * nTRST signal -- are queued:
161  *
162  * + Callbacks in C code fire first, patching internal state
163  * + Then post-reset event scripts fire ... activating JTAG circuits
164  * via TCK cycles, exiting SWD mode via TMS sequences, etc
165  *
166  * During those callbacks, scan chain contents have not been validated.
167  * JTAG operations that address a specific TAP (primarily DR/IR scans)
168  * must *not* be queued.
169  *
170  * - TAP_EVENT_SETUP is reported after TRST_ASSERTED, and after the scan
171  * chain has been validated. JTAG operations including scans that
172  * target specific TAPs may be performed.
173  *
174  * - TAP_EVENT_ENABLE and TAP_EVENT_DISABLE implement TAP activation and
175  * deactivation outside the core using scripted code that understands
176  * the specific JTAG router type. They might be triggered indirectly
177  * from EVENT_SETUP operations.
178  */
184 };
185 
188  enum jtag_event event;
190  Jim_Interp *interp;
192  Jim_Obj *body;
193  /* next action in linked list */
195 };
196 
208 typedef int (*jtag_event_handler_t)(enum jtag_event event, void *priv);
209 
212 
214 
216  RESET_NONE = 0x0,
225  RESET_CNCT_UNDER_SRST = 0x80
226 };
227 
230 
231 void jtag_set_nsrst_delay(unsigned int delay);
232 unsigned int jtag_get_nsrst_delay(void);
233 
234 void jtag_set_ntrst_delay(unsigned int delay);
235 unsigned int jtag_get_ntrst_delay(void);
236 
237 void jtag_set_nsrst_assert_width(unsigned int delay);
238 unsigned int jtag_get_nsrst_assert_width(void);
239 
240 void jtag_set_ntrst_assert_width(unsigned int delay);
241 unsigned int jtag_get_ntrst_assert_width(void);
242 
244 int jtag_get_trst(void);
246 int jtag_get_srst(void);
247 
249 void jtag_set_verify(bool enable);
251 bool jtag_will_verify(void);
252 
254 void jtag_set_verify_capture_ir(bool enable);
256 bool jtag_will_verify_capture_ir(void);
257 
259 void jtag_set_flush_queue_sleep(int ms);
260 
265 int jtag_init(struct command_context *cmd_ctx);
266 
268 int jtag_init_reset(struct command_context *cmd_ctx);
269 int jtag_register_commands(struct command_context *cmd_ctx);
270 int jtag_init_inner(struct command_context *cmd_ctx);
271 
297 void jtag_add_ir_scan(struct jtag_tap *tap,
298  struct scan_field *fields, tap_state_t endstate);
303 void jtag_add_ir_scan_noverify(struct jtag_tap *tap,
304  const struct scan_field *fields, tap_state_t state);
310 void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
311  tap_state_t endstate);
312 
319 void jtag_add_dr_scan(struct jtag_tap *tap, int num_fields,
320  const struct scan_field *fields, tap_state_t endstate);
322 void jtag_add_dr_scan_check(struct jtag_tap *tap, int num_fields,
323  struct scan_field *fields, tap_state_t endstate);
329 void jtag_add_plain_dr_scan(int num_bits,
330  const uint8_t *out_bits, uint8_t *in_bits, tap_state_t endstate);
331 
336 typedef intptr_t jtag_callback_data_t;
337 
346 typedef void (*jtag_callback1_t)(jtag_callback_data_t data0);
347 
350 
351 
380  jtag_callback_data_t data1,
381  jtag_callback_data_t data2,
382  jtag_callback_data_t data3);
383 
388 void jtag_add_tlr(void);
389 
438 void jtag_add_pathmove(unsigned int num_states, const tap_state_t *path);
439 
449 int jtag_add_statemove(tap_state_t goal_state);
450 
461 void jtag_add_runtest(unsigned int num_cycles, tap_state_t endstate);
462 
486 void jtag_add_reset(int req_tlr_or_trst, int srst);
487 
488 void jtag_add_sleep(uint32_t us);
489 
490 int jtag_add_tms_seq(unsigned int nbits, const uint8_t *seq, enum tap_state t);
491 
497 void jtag_add_clocks(unsigned int num_cycles);
498 
519 int jtag_execute_queue(void);
520 
522 void jtag_execute_queue_noclear(void);
523 
525 unsigned int jtag_get_flush_queue_count(void);
526 
528 void jtag_notify_event(enum jtag_event);
529 
530 /* can be implemented by hw + sw */
531 int jtag_power_dropout(int *dropout);
533 
534 /* JTAG support functions */
535 
544 void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask);
545 
546 void jtag_sleep(uint32_t us);
547 
548 /*
549  * The JTAG subsystem defines a number of error codes,
550  * using codes between -100 and -199.
551  */
552 #define ERROR_JTAG_INIT_FAILED (-100)
553 #define ERROR_JTAG_INVALID_INTERFACE (-101)
554 #define ERROR_JTAG_NOT_IMPLEMENTED (-102)
555 #define ERROR_JTAG_TRST_ASSERTED (-103)
556 #define ERROR_JTAG_QUEUE_FAILED (-104)
557 #define ERROR_JTAG_NOT_STABLE_STATE (-105)
558 #define ERROR_JTAG_DEVICE_ERROR (-107)
559 #define ERROR_JTAG_STATE_INVALID (-108)
560 #define ERROR_JTAG_TRANSITION_INVALID (-109)
561 #define ERROR_JTAG_INIT_SOFT_FAIL (-110)
562 
569 void jtag_set_error(int error);
570 
576 bool is_jtag_poll_safe(void);
577 
581 bool jtag_poll_get_enabled(void);
582 
586 void jtag_poll_set_enabled(bool value);
587 
594 bool jtag_poll_mask(void);
595 
599 void jtag_poll_unmask(bool saved);
600 
601 #include <jtag/minidriver.h>
602 
603 __COMMAND_HANDLER(handle_jtag_newtap);
604 
605 #endif /* OPENOCD_JTAG_JTAG_H */
const char * name
Definition: armv4_5.c:76
Support functions to access arbitrary bits in a byte array.
int mask
Definition: esirisc.c:1741
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
void jtag_set_error(int error)
Set the current JTAG core execution error, unless one was set by a previous call previously.
Definition: jtag/core.c:125
int jtag_register_commands(struct command_context *cmd_ctx)
Definition: jtag/tcl.c:1209
void jtag_poll_unmask(bool saved)
Restore saved mask for polling.
Definition: jtag/core.c:183
void jtag_add_ir_scan_noverify(struct jtag_tap *tap, const struct scan_field *fields, tap_state_t state)
The same as jtag_add_ir_scan except no verification is performed out the output values.
Definition: jtag/core.c:362
int jtag_init_inner(struct command_context *cmd_ctx)
Definition: jtag/core.c:1509
void jtag_execute_queue_noclear(void)
same as jtag_execute_queue() but does not clear the error flag
Definition: jtag/core.c:1024
int(* jtag_event_handler_t)(enum jtag_event event, void *priv)
Defines the function signature required for JTAG event callback functions, which are added with jtag_...
Definition: jtag.h:208
void jtag_add_pathmove(unsigned int num_states, const tap_state_t *path)
Application code must assume that interfaces will implement transitions between states with different...
Definition: jtag/core.c:523
int(* jtag_callback_t)(jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
Defines the interface of the JTAG callback mechanism.
Definition: jtag.h:379
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_IRCAPTURE
Definition: jtag.h:55
@ TAP_RESET
Definition: jtag.h:56
@ TAP_DRCAPTURE
Definition: jtag.h:47
@ TAP_DRSELECT
Definition: jtag.h:48
@ TAP_DREXIT1
Definition: jtag.h:42
@ TAP_IREXIT1
Definition: jtag.h:50
@ TAP_DRPAUSE
Definition: jtag.h:44
@ TAP_IRSELECT
Definition: jtag.h:45
@ TAP_IRUPDATE
Definition: jtag.h:54
@ TAP_IREXIT2
Definition: jtag.h:49
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_DREXIT2
Definition: jtag.h:41
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
@ TAP_IRPAUSE
Definition: jtag.h:52
@ TAP_DRUPDATE
Definition: jtag.h:46
@ TAP_INVALID
Definition: jtag.h:38
int jtag_unregister_event_callback(jtag_event_handler_t f, void *x)
Definition: jtag/core.c:309
unsigned int jtag_get_ntrst_assert_width(void)
Definition: jtag/core.c:1787
int jtag_get_trst(void)
Definition: jtag/core.c:1749
const char * tap_state_name(tap_state_t state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
Definition: interface.c:344
unsigned int jtag_tap_count_enabled(void)
Definition: jtag/core.c:206
int jtag_init(struct command_context *cmd_ctx)
Initialize JTAG chain using only a RESET reset.
Definition: jtag/core.c:1670
void jtag_poll_set_enabled(bool value)
Assign flag reporting whether JTAG polling is disallowed.
Definition: jtag/core.c:171
int jtag_srst_asserted(int *srst_asserted)
Definition: jtag/core.c:1731
unsigned int jtag_get_nsrst_delay(void)
Definition: jtag/core.c:1762
struct jtag_tap * jtag_tap_by_string(const char *dotted_name)
Definition: jtag/core.c:243
void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t endstate)
Scan out the bits in ir scan mode.
Definition: jtag/core.c:471
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1043
int jtag_add_tms_seq(unsigned int nbits, const uint8_t *seq, enum tap_state t)
If supported by the underlying adapter, this clocks a raw bit sequence onto TMS for switching between...
Definition: jtag/core.c:508
void jtag_add_clocks(unsigned int num_cycles)
Function jtag_add_clocks first checks that the state in which the clocks are to be issued is stable,...
Definition: jtag/core.c:605
void jtag_add_dr_scan(struct jtag_tap *tap, int num_fields, const struct scan_field *fields, tap_state_t endstate)
Generate a DR SCAN using the fields passed to the function.
Definition: jtag/core.c:457
void jtag_set_flush_queue_sleep(int ms)
Set ms to sleep after jtag_execute_queue() flushes queue.
Definition: jtag/core.c:120
bool is_jtag_poll_safe(void)
Return true if it's safe for a background polling task to access the JTAG scan chain.
Definition: jtag/core.c:148
const char * jtag_tap_name(const struct jtag_tap *tap)
Definition: jtag/core.c:282
struct jtag_tap * jtag_all_taps(void)
Definition: jtag/core.c:190
jtag_event
Definition: jtag.h:179
@ JTAG_TAP_EVENT_ENABLE
Definition: jtag.h:182
@ JTAG_TAP_EVENT_SETUP
Definition: jtag.h:181
@ JTAG_TRST_ASSERTED
Definition: jtag.h:180
@ JTAG_TAP_EVENT_DISABLE
Definition: jtag.h:183
int jtag_power_dropout(int *dropout)
Definition: jtag/core.c:1716
tap_state_t cmd_queue_cur_state
The current TAP state of the pending JTAG command queue.
Definition: jtag/core.c:90
int jtag_add_statemove(tap_state_t goal_state)
jtag_add_statemove() moves from the current state to goal_state.
Definition: jtag/core.c:557
unsigned int jtag_get_nsrst_assert_width(void)
Definition: jtag/core.c:1779
void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
A simpler version of jtag_add_callback4().
struct jtag_tap * jtag_tap_by_position(unsigned int abs_position)
Definition: jtag/core.c:233
void jtag_set_nsrst_assert_width(unsigned int delay)
Definition: jtag/core.c:1775
int jtag_init_reset(struct command_context *cmd_ctx)
reset, then initialize JTAG chain
Definition: jtag/core.c:1606
void jtag_add_reset(int req_tlr_or_trst, int srst)
A reset of the TAP state machine can be requested.
Definition: jtag/core.c:764
struct jtag_tap * jtag_tap_next_enabled(struct jtag_tap *p)
Definition: jtag/core.c:271
void jtag_add_tlr(void)
Run a TAP_RESET reset where the end state is TAP_RESET, regardless of the start state.
Definition: jtag/core.c:484
bool jtag_poll_get_enabled(void)
Return flag reporting whether JTAG polling is disallowed.
Definition: jtag/core.c:166
bool jtag_will_verify(void)
Definition: jtag/core.c:1701
int jtag_call_event_callbacks(enum jtag_event event)
Definition: jtag/core.c:330
int jtag_get_srst(void)
Definition: jtag/core.c:1753
reset_types
Definition: jtag.h:215
@ RESET_NONE
Definition: jtag.h:216
@ RESET_SRST_NO_GATING
Definition: jtag.h:224
@ RESET_HAS_SRST
Definition: jtag.h:218
@ RESET_HAS_TRST
Definition: jtag.h:217
@ RESET_TRST_PULLS_SRST
Definition: jtag.h:221
@ RESET_CNCT_UNDER_SRST
Definition: jtag.h:225
@ RESET_SRST_PULLS_TRST
Definition: jtag.h:220
@ RESET_TRST_OPEN_DRAIN
Definition: jtag.h:222
@ RESET_TRST_AND_SRST
Definition: jtag.h:219
@ RESET_SRST_PUSH_PULL
Definition: jtag.h:223
void jtag_set_ntrst_delay(unsigned int delay)
Definition: jtag/core.c:1766
void jtag_set_verify_capture_ir(bool enable)
Enable or disable verification of IR scan checking.
Definition: jtag/core.c:1706
unsigned int jtag_get_ntrst_delay(void)
Definition: jtag/core.c:1770
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
void jtag_add_sleep(uint32_t us)
Definition: jtag/core.c:876
bool jtag_will_verify_capture_ir(void)
Definition: jtag/core.c:1711
void jtag_tap_free(struct jtag_tap *tap)
Definition: jtag/core.c:1487
unsigned int jtag_get_flush_queue_count(void)
Definition: jtag/core.c:1038
int jtag_register_event_callback(jtag_event_handler_t f, void *x)
Definition: jtag/core.c:288
__COMMAND_HANDLER(handle_jtag_newtap)
Definition: jtag/tcl.c:514
tap_state_t tap_state_by_name(const char *name)
Provides user-friendly name lookup of TAP states.
Definition: interface.c:355
void jtag_add_ir_scan(struct jtag_tap *tap, struct scan_field *fields, tap_state_t endstate)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:380
struct jtag_tap * jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *obj)
Definition: jtag/tcl.c:53
void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
Execute jtag queue and check value with an optional mask.
Definition: jtag/core.c:923
void jtag_set_nsrst_delay(unsigned int delay)
Definition: jtag/core.c:1758
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1068
void jtag_add_dr_scan_check(struct jtag_tap *tap, int num_fields, struct scan_field *fields, tap_state_t endstate)
A version of jtag_add_dr_scan() that uses the check_value/mask fields.
Definition: jtag/core.c:445
void jtag_add_runtest(unsigned int num_cycles, tap_state_t endstate)
Goes to TAP_IDLE (if we're not already there), cycle precisely num_cycles in the TAP_IDLE state,...
Definition: jtag/core.c:598
void jtag_tap_init(struct jtag_tap *tap)
Definition: jtag/core.c:1453
void jtag_set_reset_config(enum reset_types type)
Definition: jtag/core.c:1744
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1740
void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t endstate)
Scan out the bits in ir scan mode.
Definition: jtag/core.c:398
void jtag_notify_event(enum jtag_event)
Report Tcl event to all TAPs.
Definition: jtag/tcl.c:783
void jtag_set_ntrst_assert_width(unsigned int delay)
Definition: jtag/core.c:1783
intptr_t jtag_callback_data_t
Defines the type of data passed to the jtag_callback_t interface.
Definition: jtag.h:336
bool jtag_poll_mask(void)
Mask (disable) polling and return the current mask status that should be feed to jtag_poll_unmask() t...
Definition: jtag/core.c:176
void(* jtag_callback1_t)(jtag_callback_data_t data0)
Defines a simple JTAG callback that can allow conversions on data scanned in from an interface.
Definition: jtag.h:346
void jtag_set_verify(bool enable)
Enable or disable data scan verification checking.
Definition: jtag/core.c:1696
struct jtag_tap_event_action * next
Definition: jtag.h:194
enum jtag_event event
The event for which this action will be triggered.
Definition: jtag.h:188
Jim_Obj * body
Contains a script to 'eval' when the event is triggered.
Definition: jtag.h:192
Jim_Interp * interp
The interpreter to use for evaluating the body.
Definition: jtag.h:190
Definition: jtag.h:101
uint32_t ir_capture_value
Definition: jtag.h:111
bool bypass
Bypass register selected.
Definition: jtag.h:134
uint8_t * expected_mask
Capture-IR expected mask.
Definition: jtag.h:114
char * chip
Definition: jtag.h:102
bool ignore_version
Flag saying whether to ignore version field in expected_ids[].
Definition: jtag.h:126
bool disabled_after_reset
Is this TAP disabled after JTAG reset?
Definition: jtag.h:107
struct jtag_tap_event_action * event_action
Definition: jtag.h:139
uint8_t * cur_instr
current instruction
Definition: jtag.h:132
void * priv
Definition: jtag.h:143
unsigned int ir_length
size of instruction register
Definition: jtag.h:110
uint8_t * expected
Capture-IR expected value.
Definition: jtag.h:112
uint32_t ir_capture_mask
Definition: jtag.h:113
uint8_t expected_ids_cnt
Number of expected identification codes.
Definition: jtag.h:123
bool has_idcode
not all devices have idcode, we'll discover this during chain examination
Definition: jtag.h:118
unsigned int abs_chain_position
Definition: jtag.h:105
char * tapname
Definition: jtag.h:103
bool ignore_bypass
Flag saying whether to ignore the bypass bit in the code.
Definition: jtag.h:129
bool enabled
Is this TAP currently enabled?
Definition: jtag.h:109
uint64_t ir_bypass_value
Bypass instruction value.
Definition: jtag.h:137
uint32_t * expected_ids
Array of expected identification codes.
Definition: jtag.h:121
struct jtag_tap * next_tap
Definition: jtag.h:141
uint32_t idcode
device identification code
Definition: jtag.h:115
char * dotted_name
Definition: jtag.h:104
This structure defines a single scan field in the scan.
Definition: jtag.h:87
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
uint8_t * check_value
The value used to check the data scanned out.
Definition: jtag.h:96
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
uint8_t * check_mask
The mask to go with check_value.
Definition: jtag.h:98
static int srst_asserted
Definition: target.c:2849
uint8_t state[4]
Definition: vdebug.c:21