From Michael Bruck
[fw/openocd] / src / jtag / jtag.h
1 /***************************************************************************\r
2  *   Copyright (C) 2005 by Dominic Rath                                    *\r
3  *   Dominic.Rath@gmx.de                                                   *\r
4  *                                                                         *\r
5  *   This program is free software; you can redistribute it and/or modify  *\r
6  *   it under the terms of the GNU General Public License as published by  *\r
7  *   the Free Software Foundation; either version 2 of the License, or     *\r
8  *   (at your option) any later version.                                   *\r
9  *                                                                         *\r
10  *   This program is distributed in the hope that it will be useful,       *\r
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *\r
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *\r
13  *   GNU General Public License for more details.                          *\r
14  *                                                                         *\r
15  *   You should have received a copy of the GNU General Public License     *\r
16  *   along with this program; if not, write to the                         *\r
17  *   Free Software Foundation, Inc.,                                       *\r
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *\r
19  ***************************************************************************/\r
20 #ifndef JTAG_H\r
21 #define JTAG_H\r
22 \r
23 #include "types.h"\r
24 #include "binarybuffer.h"\r
25 \r
26 #include "command.h"\r
27 \r
28 #if 0\r
29 #define _DEBUG_JTAG_IO_\r
30 #endif\r
31 \r
32 /* Tap States\r
33  * TLR - Test-Logic-Reset, RTI - Run-Test/Idle, \r
34  * SDS - Select-DR-Scan, CD - Capture-DR, SD - Shift-DR, E1D - Exit1-DR,\r
35  * PD - Pause-DR, E2D - Exit2-DR, UD - Update-DR,\r
36  * SIS - Select-IR-Scan, CI - Capture-IR, SI - Shift-IR, E1I - Exit1-IR,\r
37  * PI - Pause-IR, E2I - Exit2-IR, UI - Update-IR \r
38  */\r
39 enum tap_state\r
40 {\r
41         TAP_TLR = 0x0, TAP_RTI = 0x8, \r
42         TAP_SDS = 0x1, TAP_CD = 0x2, TAP_SD = 0x3, TAP_E1D = 0x4, \r
43         TAP_PD = 0x5, TAP_E2D = 0x6, TAP_UD = 0x7,\r
44         TAP_SIS = 0x9, TAP_CI = 0xa, TAP_SI = 0xb, TAP_E1I = 0xc,\r
45         TAP_PI = 0xd, TAP_E2I = 0xe, TAP_UI = 0xf\r
46 };\r
47 \r
48 typedef struct tap_transition_s\r
49 {\r
50         enum tap_state high;\r
51         enum tap_state low;\r
52 } tap_transition_t;\r
53 \r
54 extern char* tap_state_strings[16];\r
55 extern int tap_move_map[16];    /* map 16 TAP states to 6 stable states */\r
56 extern u8 tap_move[6][6];               /* value scanned to TMS to move from one of six stable states to another */\r
57 extern tap_transition_t tap_transitions[16];    /* describe the TAP state diagram */\r
58 \r
59 extern enum tap_state end_state;                /* finish DR scans in dr_end_state */\r
60 extern enum tap_state cur_state;                /* current TAP state */\r
61 \r
62 extern enum tap_state cmd_queue_end_state;              /* finish DR scans in dr_end_state */\r
63 extern enum tap_state cmd_queue_cur_state;              /* current TAP state */\r
64 \r
65 #define TAP_MOVE(from, to) tap_move[tap_move_map[from]][tap_move_map[to]]\r
66 \r
67 typedef void * error_handler_t; /* Later on we can delete error_handler_t, but keep it for now to make patches more readable */\r
68 \r
69 struct scan_field_s;\r
70 typedef int (*in_handler_t)(u8 *in_value, void *priv, struct scan_field_s *field);\r
71 \r
72 typedef struct scan_field_s\r
73 {\r
74         int device;                     /* ordinal device number this instruction refers to */\r
75         int num_bits;           /* number of bits this field specifies (up to 32) */\r
76         u8 *out_value;          /* value to be scanned into the device */\r
77         u8 *out_mask;           /* only masked bits care */\r
78         u8 *in_value;           /* pointer to a 32-bit memory location to take data scanned out */\r
79         /* in_check_value/mask, in_handler_error_handler, in_handler_priv can be used by the in handler, otherwise they contain garbage  */\r
80         u8 *in_check_value;             /* used to validate scan results */ \r
81         u8 *in_check_mask;              /* check specified bits against check_value */\r
82         in_handler_t in_handler;            /* process received buffer using this handler */\r
83         void *in_handler_priv;  /* additional information for the in_handler */\r
84 } scan_field_t;\r
85 \r
86 enum scan_type\r
87 {\r
88         /* IN: from device to host, OUT: from host to device */\r
89         SCAN_IN = 1, SCAN_OUT = 2, SCAN_IO = 3\r
90 };\r
91 \r
92 typedef struct scan_command_s\r
93 {\r
94         int ir_scan;    /* instruction/not data scan */\r
95         int num_fields;         /* number of fields in *fields array */\r
96         scan_field_t *fields;   /* pointer to an array of data scan fields */\r
97         enum tap_state end_state;       /* TAP state in which JTAG commands should finish */\r
98 } scan_command_t;\r
99 \r
100 typedef struct statemove_command_s\r
101 {\r
102         enum tap_state end_state;       /* TAP state in which JTAG commands should finish */\r
103 } statemove_command_t;\r
104 \r
105 typedef struct pathmove_command_s\r
106 {\r
107         int num_states;                         /* number of states in *path */\r
108         enum tap_state *path;           /* states that have to be passed */\r
109 } pathmove_command_t;\r
110 \r
111 typedef struct runtest_command_s\r
112 {\r
113         int num_cycles;         /* number of cycles that should be spent in Run-Test/Idle */\r
114         enum tap_state end_state;       /* TAP state in which JTAG commands should finish */\r
115 } runtest_command_t;\r
116 \r
117 typedef struct reset_command_s\r
118 {\r
119         int trst;                       /* trst/srst 0: deassert, 1: assert, -1: don't change */\r
120         int srst;\r
121 } reset_command_t;\r
122 \r
123 typedef struct end_state_command_s\r
124 {\r
125         enum tap_state end_state;       /* TAP state in which JTAG commands should finish */\r
126 } end_state_command_t;\r
127 \r
128 typedef struct sleep_command_s\r
129 {\r
130         u32 us;         /* number of microseconds to sleep */\r
131 } sleep_command_t;\r
132 \r
133 typedef union jtag_command_container_u\r
134 {\r
135         scan_command_t *scan;\r
136         statemove_command_t *statemove;\r
137         pathmove_command_t *pathmove;\r
138         runtest_command_t *runtest;\r
139         reset_command_t *reset;\r
140         end_state_command_t *end_state;\r
141         sleep_command_t *sleep;\r
142 } jtag_command_container_t;\r
143 \r
144 enum jtag_command_type\r
145 {\r
146         JTAG_SCAN = 1,\r
147         JTAG_STATEMOVE = 2, JTAG_RUNTEST = 3,\r
148         JTAG_RESET = 4, JTAG_END_STATE = 5,\r
149         JTAG_PATHMOVE = 6, JTAG_SLEEP = 7\r
150 };\r
151 \r
152 typedef struct jtag_command_s\r
153 {\r
154         jtag_command_container_t cmd;\r
155         enum jtag_command_type type;\r
156         struct jtag_command_s *next;\r
157 } jtag_command_t;\r
158 \r
159 extern jtag_command_t *jtag_command_queue;\r
160 \r
161 typedef struct jtag_device_s\r
162 {\r
163         int ir_length;          /* size of instruction register */\r
164         u8 *expected;           /* Capture-IR expected value */\r
165         u8 *expected_mask;      /* Capture-IR expected mask */\r
166         u32 idcode;                     /* device identification code */\r
167         u8 *cur_instr;          /* current instruction */\r
168         int bypass;                     /* bypass register selected */\r
169         struct jtag_device_s *next;\r
170 } jtag_device_t;\r
171 \r
172 extern jtag_device_t *jtag_devices;\r
173 extern int jtag_num_devices;\r
174 extern int jtag_ir_scan_size;\r
175 \r
176 enum reset_line_mode\r
177 {\r
178         LINE_OPEN_DRAIN = 0x0,\r
179         LINE_PUSH_PULL = 0x1,\r
180 };\r
181 \r
182 typedef struct jtag_interface_s\r
183 {\r
184         char* name;\r
185         \r
186         /* queued command execution\r
187          */\r
188         int (*execute_queue)(void);\r
189         \r
190         /* interface initalization\r
191          */\r
192         int (*speed)(int speed);\r
193         int (*register_commands)(struct command_context_s *cmd_ctx);\r
194         int (*init)(void);\r
195         int (*quit)(void);\r
196         \r
197 } jtag_interface_t;\r
198 \r
199 enum jtag_event\r
200 {\r
201         JTAG_SRST_ASSERTED,\r
202         JTAG_TRST_ASSERTED,\r
203         JTAG_SRST_RELEASED,\r
204         JTAG_TRST_RELEASED,\r
205 };\r
206 \r
207 extern char* jtag_event_strings[];\r
208 \r
209 extern int jtag_trst;\r
210 extern int jtag_srst;\r
211 \r
212 typedef struct jtag_event_callback_s\r
213 {\r
214         int (*callback)(enum jtag_event event, void *priv);\r
215         void *priv;\r
216         struct jtag_event_callback_s *next;\r
217 } jtag_event_callback_t;\r
218 \r
219 extern jtag_event_callback_t *jtag_event_callbacks;\r
220 \r
221 extern jtag_interface_t *jtag;  /* global pointer to configured JTAG interface */\r
222 extern enum tap_state end_state;\r
223 extern enum tap_state cur_state;\r
224 \r
225 extern char* jtag_interface;\r
226 extern int jtag_speed;\r
227 \r
228 enum reset_types\r
229 {\r
230         RESET_NONE = 0x0, \r
231         RESET_HAS_TRST = 0x1, \r
232         RESET_HAS_SRST = 0x2, \r
233         RESET_TRST_AND_SRST = 0x3, \r
234         RESET_SRST_PULLS_TRST = 0x4,\r
235         RESET_TRST_PULLS_SRST = 0x8,\r
236         RESET_TRST_OPEN_DRAIN = 0x10,\r
237         RESET_SRST_PUSH_PULL = 0x20,\r
238 };\r
239 \r
240 extern enum reset_types jtag_reset_config;\r
241 \r
242 /* JTAG subsystem */\r
243 extern int jtag_init(struct command_context_s *cmd_ctx);\r
244 extern int jtag_register_commands(struct command_context_s *cmd_ctx);\r
245 \r
246 /* JTAG interface, can be implemented with a software or hardware fifo */\r
247 extern int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state endstate);
248 extern int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state endstate);
249 extern int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state endstate);
250 extern int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state endstate);
251 /* execute a state transition within the JTAG standard, but the exact path\r
252  * path that is taken is undefined. Many implementations use precisely\r
253  * 7 clocks to perform a transition, but it could be more or less\r
254  * than that.\r
255  *\r
256  * The following assertions are made about certain common state moves:\r
257  *\r
258  * - A state move from Pause-[ID]R to Pause-[ID]R should always go through \r
259  *   Update-[ID]R and Capture-[ID]R before returning to Pause-[ID]R, otherwise \r
260  *   there's no way force a register update, if you can't go to Run-Test/Idle for \r
261  *   some reason.\r
262  *\r
263  *   - A state move from Pause-[ID]R to Shift-[ID]R must not go through \r
264  *   Update-[ID]R.\r
265  *\r
266  *   - Run-Test/Idle must not be entered unless requested, because R-T/I may have \r
267  *   side effects.\r
268  */\r
269 extern int jtag_add_statemove(enum tap_state endstate);\r
270 /* A list of unambigious single clock state transitions, not\r
271  * all drivers can support this, but it is required for e.g.\r
272  * XScale and Xilinx support\r
273  */\r
274 extern int jtag_add_pathmove(int num_states, enum tap_state *path);\r
275 /* cycle precisely num_cycles in the TAP_RTI state */\r
276 extern int jtag_add_runtest(int num_cycles, enum tap_state endstate);\r
277 extern int jtag_add_reset(int trst, int srst);\r
278 extern int jtag_add_end_state(enum tap_state endstate);\r
279 extern int jtag_add_sleep(u32 us);\r
280 /*\r
281  * For software FIFO implementations, the queued commands can be executed \r
282  * during this call or earlier. A sw queue might decide to push out\r
283  * some of the jtag_add_xxx() operations once the queue is "big enough".\r
284  * \r
285  * This fn will return an error code if any of the prior jtag_add_xxx() \r
286  * calls caused a failure, e.g. check failure. Note that it does not\r
287  * matter if the operation was executed *before* jtag_execute_queue(),\r
288  * jtag_execute_queue() will still return an error code. \r
289  * \r
290  * All jtag_add_xxx() calls that have in_handler!=NULL will have been\r
291  * executed when this fn returns, but if what has been queued only \r
292  * clocks data out, without reading anything back, then JTAG could \r
293  * be running *after* jtag_execute_queue() returns. The API does \r
294  * not define a way to flush a hw FIFO that runs *after* \r
295  * jtag_execute_queue() returns. \r
296  * \r
297  * jtag_add_xxx() commands can either be executed immediately or \r
298  * at some time between the jtag_add_xxx() fn call and jtag_execute_queue().  \r
299  */\r
300 extern int jtag_execute_queue(void);\r
301 \r
302 /* JTAG support functions */\r
303 extern void jtag_set_check_value(scan_field_t *field, u8 *value,  u8 *mask, error_handler_t *in_error_handler);\r
304 extern enum scan_type jtag_scan_type(scan_command_t *cmd);\r
305 extern int jtag_scan_size(scan_command_t *cmd);\r
306 extern int jtag_read_buffer(u8 *buffer, scan_command_t *cmd);\r
307 extern int jtag_build_buffer(scan_command_t *cmd, u8 **buffer);\r
308 extern jtag_device_t* jtag_get_device(int num);\r
309 extern void jtag_sleep(u32 us);\r
310 extern int jtag_call_event_callbacks(enum jtag_event event);\r
311 extern int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv);\r
312 \r
313 extern int jtag_verify_capture_ir;\r
314 \r
315 \r
316 /* error codes\r
317  * JTAG subsystem uses codes between -100 and -199 */\r
318 \r
319 #define ERROR_JTAG_INIT_FAILED                  (-100)\r
320 #define ERROR_JTAG_INVALID_INTERFACE    (-101)\r
321 #define ERROR_JTAG_NOT_IMPLEMENTED              (-102)\r
322 #define ERROR_JTAG_TRST_ASSERTED                (-103)\r
323 #define ERROR_JTAG_QUEUE_FAILED                 (-104)\r
324 #define ERROR_JTAG_RESET_WOULD_ASSERT_TRST              (-105)\r
325 #define ERROR_JTAG_RESET_CANT_SRST                              (-106)\r
326 #define ERROR_JTAG_DEVICE_ERROR                 (-107)\r
327 #endif /* JTAG_H */\r