- disabled FT2232 debug output (that code should be fairly stable)
[openocd.git] / src / jtag / jtag.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "replacements.h"
25
26 #include "jtag.h"
27
28 #include "command.h"
29 #include "log.h"
30 #include "interpreter.h"
31
32 #include "stdlib.h"
33 #include "string.h"
34 #include <unistd.h>
35
36 char* tap_state_strings[16] =
37 {
38 "tlr",
39 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
40 "rti",
41 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
42 };
43
44 typedef struct cmd_queue_page_s
45 {
46 void *address;
47 size_t used;
48 struct cmd_queue_page_s *next;
49 } cmd_queue_page_t;
50
51 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
52 static cmd_queue_page_t *cmd_queue_pages = NULL;
53
54 /* tap_move[i][j]: tap movement command to go from state i to state j
55 * 0: Test-Logic-Reset
56 * 1: Run-Test/Idle
57 * 2: Shift-DR
58 * 3: Pause-DR
59 * 4: Shift-IR
60 * 5: Pause-IR
61 */
62 u8 tap_move[6][6] =
63 {
64 /* TLR RTI SD PD SI PI */
65 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
66 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
67 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
68 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
69 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
70 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
71 };
72
73 int tap_move_map[16] = {
74 0, -1, -1, 2, -1, 3, -1, -1,
75 1, -1, -1, 4, -1, 5, -1, -1
76 };
77
78 tap_transition_t tap_transitions[16] =
79 {
80 {TAP_TLR, TAP_RTI}, /* TLR */
81 {TAP_SIS, TAP_CD}, /* SDS */
82 {TAP_E1D, TAP_SD}, /* CD */
83 {TAP_E1D, TAP_SD}, /* SD */
84 {TAP_UD, TAP_PD}, /* E1D */
85 {TAP_E2D, TAP_PD}, /* PD */
86 {TAP_UD, TAP_SD}, /* E2D */
87 {TAP_SDS, TAP_RTI}, /* UD */
88 {TAP_SDS, TAP_RTI}, /* RTI */
89 {TAP_TLR, TAP_CI}, /* SIS */
90 {TAP_E1I, TAP_SI}, /* CI */
91 {TAP_E1I, TAP_SI}, /* SI */
92 {TAP_UI, TAP_PI}, /* E1I */
93 {TAP_E2I, TAP_PI}, /* PI */
94 {TAP_UI, TAP_SI}, /* E2I */
95 {TAP_SDS, TAP_RTI} /* UI */
96 };
97
98 enum tap_state end_state = TAP_TLR;
99 enum tap_state cur_state = TAP_TLR;
100 int jtag_trst = 0;
101 int jtag_srst = 0;
102
103 jtag_command_t *jtag_command_queue = NULL;
104 jtag_command_t **last_comand_pointer = &jtag_command_queue;
105 jtag_device_t *jtag_devices = NULL;
106 int jtag_num_devices = 0;
107 int jtag_ir_scan_size = 0;
108 enum reset_types jtag_reset_config = RESET_NONE;
109 enum tap_state cmd_queue_end_state = TAP_TLR;
110 enum tap_state cmd_queue_cur_state = TAP_TLR;
111
112 int jtag_verify_capture_ir = 1;
113
114 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
115 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
116 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
117
118 /* callbacks to inform high-level handlers about JTAG state changes */
119 jtag_event_callback_t *jtag_event_callbacks;
120
121 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
122 */
123 #if BUILD_PARPORT == 1
124 extern jtag_interface_t parport_interface;
125 #endif
126
127 #if BUILD_FT2232_FTD2XX == 1
128 extern jtag_interface_t ft2232_interface;
129 #endif
130
131 #if BUILD_FT2232_LIBFTDI == 1
132 extern jtag_interface_t ft2232_interface;
133 #endif
134
135 #if BUILD_AMTJTAGACCEL == 1
136 extern jtag_interface_t amt_jtagaccel_interface;
137 #endif
138
139 #if BUILD_EP93XX == 1
140 extern jtag_interface_t ep93xx_interface;
141 #endif
142
143 #if BUILD_AT91RM9200 == 1
144 extern jtag_interface_t at91rm9200_interface;
145 #endif
146
147 #if BUILD_GW16012 == 1
148 extern jtag_interface_t gw16012_interface;
149 #endif
150
151 jtag_interface_t *jtag_interfaces[] = {
152 #if BUILD_PARPORT == 1
153 &parport_interface,
154 #endif
155 #if BUILD_FT2232_FTD2XX == 1
156 &ft2232_interface,
157 #endif
158 #if BUILD_FT2232_LIBFTDI == 1
159 &ft2232_interface,
160 #endif
161 #if BUILD_AMTJTAGACCEL == 1
162 &amt_jtagaccel_interface,
163 #endif
164 #if BUILD_EP93XX == 1
165 &ep93xx_interface,
166 #endif
167 #if BUILD_AT91RM9200 == 1
168 &at91rm9200_interface,
169 #endif
170 #if BUILD_GW16012 == 1
171 &gw16012_interface,
172 #endif
173 NULL,
174 };
175
176 jtag_interface_t *jtag = NULL;
177
178 /* configuration */
179 char* jtag_interface = NULL;
180 int jtag_speed = -1;
181
182 /* forward declarations */
183
184 /* jtag commands */
185 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
186 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
187 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
188 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
189 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
190 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
191
192 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
193
194 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
195 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
196 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
197 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
198 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
199 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
200
201 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
202
203 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
204 {
205 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
206
207 if (callback == NULL)
208 {
209 return ERROR_INVALID_ARGUMENTS;
210 }
211
212 if (*callbacks_p)
213 {
214 while ((*callbacks_p)->next)
215 callbacks_p = &((*callbacks_p)->next);
216 callbacks_p = &((*callbacks_p)->next);
217 }
218
219 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
220 (*callbacks_p)->callback = callback;
221 (*callbacks_p)->priv = priv;
222 (*callbacks_p)->next = NULL;
223
224 return ERROR_OK;
225 }
226
227 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
228 {
229 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
230
231 if (callback == NULL)
232 {
233 return ERROR_INVALID_ARGUMENTS;
234 }
235
236 while (*callbacks_p)
237 {
238 jtag_event_callback_t **next = &((*callbacks_p)->next);
239 if ((*callbacks_p)->callback == callback)
240 {
241 free(*callbacks_p);
242 *callbacks_p = *next;
243 }
244 callbacks_p = next;
245 }
246
247 return ERROR_OK;
248 }
249
250 int jtag_call_event_callbacks(enum jtag_event event)
251 {
252 jtag_event_callback_t *callback = jtag_event_callbacks;
253
254 DEBUG("jtag event: %i", event);
255
256 while (callback)
257 {
258 callback->callback(event, callback->priv);
259 callback = callback->next;
260 }
261
262 return ERROR_OK;
263 }
264
265 /* returns a pointer to the pointer of the last command in queue
266 * this may be a pointer to the root pointer (jtag_command_queue)
267 * or to the next member of the last but one command
268 */
269 jtag_command_t** jtag_get_last_command_p(void)
270 {
271 /* jtag_command_t *cmd = jtag_command_queue;
272
273 if (cmd)
274 while (cmd->next)
275 cmd = cmd->next;
276 else
277 return &jtag_command_queue;
278
279 return &cmd->next;*/
280
281 return last_comand_pointer;
282 }
283
284 /* returns a pointer to the n-th device in the scan chain */
285 jtag_device_t* jtag_get_device(int num)
286 {
287 jtag_device_t *device = jtag_devices;
288 int i = 0;
289
290 while (device)
291 {
292 if (num == i)
293 return device;
294 device = device->next;
295 i++;
296 }
297
298 return NULL;
299 }
300
301 void* cmd_queue_alloc(size_t size)
302 {
303 cmd_queue_page_t **p_page = &cmd_queue_pages;
304 int offset;
305
306 if (*p_page)
307 {
308 while ((*p_page)->next)
309 p_page = &((*p_page)->next);
310 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
311 p_page = &((*p_page)->next);
312 }
313
314 if (!*p_page)
315 {
316 *p_page = malloc(sizeof(cmd_queue_page_t));
317 (*p_page)->used = 0;
318 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
319 (*p_page)->next = NULL;
320 }
321
322 offset = (*p_page)->used;
323 (*p_page)->used += size;
324
325 return ((*p_page)->address) + offset;
326 }
327
328 void cmd_queue_free()
329 {
330 cmd_queue_page_t *page = cmd_queue_pages;
331
332 while (page)
333 {
334 cmd_queue_page_t *last = page;
335 free(page->address);
336 page = page->next;
337 free(last);
338 }
339
340 cmd_queue_pages = NULL;
341 }
342
343 int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
344 {
345 jtag_command_t **last_cmd;
346 jtag_device_t *device;
347 int i, j;
348 int scan_size = 0;
349 /* int changed = 0; */
350
351 if (jtag_trst == 1)
352 {
353 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
354 return ERROR_JTAG_TRST_ASSERTED;
355 }
356
357 /*
358 for (i=0; i<num_fields; i++)
359 {
360 device = jtag_get_device(fields[i].device);
361 if (device)
362 {
363 if (buf_cmp(device->cur_instr, fields[i].out_value, device->ir_length))
364 changed = 1;
365 }
366 else
367 {
368 ERROR("inexistant device specified for ir scan");
369 return ERROR_INVALID_ARGUMENTS;
370 }
371 }
372
373 if (!changed)
374 return ERROR_OK;
375 */
376
377 last_cmd = jtag_get_last_command_p();
378
379 /* allocate memory for a new list member */
380 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
381 (*last_cmd)->next = NULL;
382 last_comand_pointer = &((*last_cmd)->next);
383 (*last_cmd)->type = JTAG_SCAN;
384
385 /* allocate memory for ir scan command */
386 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
387 (*last_cmd)->cmd.scan->ir_scan = 1;
388 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
389 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
390 (*last_cmd)->cmd.scan->end_state = state;
391
392 if (state != -1)
393 cmd_queue_end_state = state;
394
395 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
396 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
397
398 if (cmd_queue_end_state == TAP_TLR)
399 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
400
401 cmd_queue_cur_state = cmd_queue_end_state;
402
403 for (i=0; i < jtag_num_devices; i++)
404 {
405 int found = 0;
406 device = jtag_get_device(i);
407 scan_size = device->ir_length;
408 (*last_cmd)->cmd.scan->fields[i].device = i;
409 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
410 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
411 if (jtag_verify_capture_ir)
412 {
413 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(device->expected, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
414 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(device->expected_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
415 }
416 else
417 {
418 (*last_cmd)->cmd.scan->fields[i].in_check_value = NULL;
419 (*last_cmd)->cmd.scan->fields[i].in_check_mask = NULL;
420 }
421 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
422 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
423
424 /* search the list */
425 for (j=0; j < num_fields; j++)
426 {
427 if (i == fields[j].device)
428 {
429 found = 1;
430 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
431 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
432
433 device->bypass = 0;
434 break;
435 }
436 }
437
438 if (!found)
439 {
440 /* if a device isn't listed, set it to BYPASS */
441 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
442 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
443 device->bypass = 1;
444
445 }
446
447 /* update device information */
448 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
449 }
450
451 return ERROR_OK;
452 }
453
454 int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
455 {
456 jtag_command_t **last_cmd;
457 int i;
458
459 if (jtag_trst == 1)
460 {
461 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
462 return ERROR_JTAG_TRST_ASSERTED;
463 }
464
465 last_cmd = jtag_get_last_command_p();
466
467 /* allocate memory for a new list member */
468 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
469 (*last_cmd)->next = NULL;
470 last_comand_pointer = &((*last_cmd)->next);
471 (*last_cmd)->type = JTAG_SCAN;
472
473 /* allocate memory for ir scan command */
474 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
475 (*last_cmd)->cmd.scan->ir_scan = 1;
476 (*last_cmd)->cmd.scan->num_fields = num_fields;
477 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
478 (*last_cmd)->cmd.scan->end_state = state;
479
480 if (state != -1)
481 cmd_queue_end_state = state;
482
483 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
484 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
485
486 if (cmd_queue_end_state == TAP_TLR)
487 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
488
489 cmd_queue_cur_state = cmd_queue_end_state;
490
491 for (i = 0; i < num_fields; i++)
492 {
493 int num_bits = fields[i].num_bits;
494 int num_bytes = CEIL(fields[i].num_bits, 8);
495 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
496 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
497 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
498 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
499 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
500 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(fields[i].in_check_value, cmd_queue_alloc(num_bytes), num_bits);
501 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(fields[i].in_check_mask, cmd_queue_alloc(num_bytes), num_bits);
502 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
503 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
504 }
505 return ERROR_OK;
506 }
507
508 int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
509 {
510 int i, j;
511 int bypass_devices = 0;
512 int field_count = 0;
513 jtag_command_t **last_cmd = jtag_get_last_command_p();
514 jtag_device_t *device = jtag_devices;
515 int scan_size;
516
517 if (jtag_trst == 1)
518 {
519 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
520 return ERROR_JTAG_TRST_ASSERTED;
521 }
522
523 /* count devices in bypass */
524 while (device)
525 {
526 if (device->bypass)
527 bypass_devices++;
528 device = device->next;
529 }
530
531 /* allocate memory for a new list member */
532 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
533 last_comand_pointer = &((*last_cmd)->next);
534 (*last_cmd)->next = NULL;
535 (*last_cmd)->type = JTAG_SCAN;
536
537 /* allocate memory for dr scan command */
538 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
539 (*last_cmd)->cmd.scan->ir_scan = 0;
540 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
541 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
542 (*last_cmd)->cmd.scan->end_state = state;
543
544 if (state != -1)
545 cmd_queue_end_state = state;
546
547 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
548 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
549
550 if (cmd_queue_end_state == TAP_TLR)
551 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
552
553 cmd_queue_cur_state = cmd_queue_end_state;
554
555 for (i=0; i < jtag_num_devices; i++)
556 {
557 int found = 0;
558 (*last_cmd)->cmd.scan->fields[field_count].device = i;
559
560 for (j=0; j < num_fields; j++)
561 {
562 if (i == fields[j].device)
563 {
564 found = 1;
565 scan_size = fields[j].num_bits;
566 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
567 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
568 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
569 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
570 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = buf_cpy(fields[j].in_check_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
571 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = buf_cpy(fields[j].in_check_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
572 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
573 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
574 }
575 }
576 if (!found)
577 {
578 /* if a device isn't listed, the BYPASS register should be selected */
579 if (!jtag_get_device(i)->bypass)
580 {
581 ERROR("BUG: no scan data for a device not in BYPASS");
582 exit(-1);
583 }
584
585 /* program the scan field to 1 bit length, and ignore it's value */
586 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
587 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
588 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
589 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
590 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
591 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
592 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
593 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
594 }
595 else
596 {
597 /* if a device is listed, the BYPASS register must not be selected */
598 if (jtag_get_device(i)->bypass)
599 {
600 ERROR("BUG: scan data for a device in BYPASS");
601 exit(-1);
602 }
603 }
604 }
605 return ERROR_OK;
606 }
607
608 int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
609 {
610 int i;
611 jtag_command_t **last_cmd = jtag_get_last_command_p();
612
613 if (jtag_trst == 1)
614 {
615 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
616 return ERROR_JTAG_TRST_ASSERTED;
617 }
618
619 /* allocate memory for a new list member */
620 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
621 last_comand_pointer = &((*last_cmd)->next);
622 (*last_cmd)->next = NULL;
623 (*last_cmd)->type = JTAG_SCAN;
624
625 /* allocate memory for scan command */
626 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
627 (*last_cmd)->cmd.scan->ir_scan = 0;
628 (*last_cmd)->cmd.scan->num_fields = num_fields;
629 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
630 (*last_cmd)->cmd.scan->end_state = state;
631
632 if (state != -1)
633 cmd_queue_end_state = state;
634
635 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
636 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
637
638 if (cmd_queue_end_state == TAP_TLR)
639 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
640
641 cmd_queue_cur_state = cmd_queue_end_state;
642
643 for (i = 0; i < num_fields; i++)
644 {
645 int num_bits = fields[i].num_bits;
646 int num_bytes = CEIL(fields[i].num_bits, 8);
647 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
648 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
649 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
650 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
651 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
652 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(fields[i].in_check_value, cmd_queue_alloc(num_bytes), num_bits);
653 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(fields[i].in_check_mask, cmd_queue_alloc(num_bytes), num_bits);
654 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
655 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
656 }
657
658 return ERROR_OK;
659 }
660 int jtag_add_statemove(enum tap_state state)
661 {
662 jtag_command_t **last_cmd = jtag_get_last_command_p();
663
664 if (jtag_trst == 1)
665 {
666 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
667 return ERROR_JTAG_TRST_ASSERTED;
668 }
669
670 /* allocate memory for a new list member */
671 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
672 last_comand_pointer = &((*last_cmd)->next);
673 (*last_cmd)->next = NULL;
674 (*last_cmd)->type = JTAG_STATEMOVE;
675
676 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
677 (*last_cmd)->cmd.statemove->end_state = state;
678
679 if (state != -1)
680 cmd_queue_end_state = state;
681
682 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
683 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
684
685 if (cmd_queue_end_state == TAP_TLR)
686 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
687
688 cmd_queue_cur_state = cmd_queue_end_state;
689
690 return ERROR_OK;
691 }
692
693 int jtag_add_pathmove(int num_states, enum tap_state *path)
694 {
695 jtag_command_t **last_cmd = jtag_get_last_command_p();
696 int i;
697
698 if (jtag_trst == 1)
699 {
700 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
701 return ERROR_JTAG_TRST_ASSERTED;
702 }
703
704 /* the last state has to be a stable state */
705 if (tap_move_map[path[num_states - 1]] == -1)
706 {
707 ERROR("TAP path doesn't finish in a stable state");
708 return ERROR_JTAG_NOT_IMPLEMENTED;
709 }
710
711 if (jtag->support_pathmove)
712 {
713 /* allocate memory for a new list member */
714 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
715 last_comand_pointer = &((*last_cmd)->next);
716 (*last_cmd)->next = NULL;
717 (*last_cmd)->type = JTAG_PATHMOVE;
718
719 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
720 (*last_cmd)->cmd.pathmove->num_states = num_states;
721 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
722
723 for (i = 0; i < num_states; i++)
724 (*last_cmd)->cmd.pathmove->path[i] = path[i];
725 }
726 else
727 {
728 /* validate the desired path, and see if it fits a default path */
729 int begin = 0;
730 int end = 0;
731 int j;
732
733 for (i = 0; i < num_states; i++)
734 {
735 for (j = i; j < num_states; j++)
736 {
737 if (tap_move_map[path[j]] != -1)
738 {
739 end = j;
740 break;
741 }
742 }
743
744 if (begin - end <= 7) /* a default path spans no more than 7 states */
745 {
746 jtag_add_statemove(path[end]);
747 }
748 else
749 {
750 ERROR("encountered a TAP path that can't be fulfilled by default paths");
751 return ERROR_JTAG_NOT_IMPLEMENTED;
752 }
753
754 i = end;
755 }
756 }
757
758 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
759 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
760
761 if (cmd_queue_end_state == TAP_TLR)
762 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
763
764 cmd_queue_cur_state = path[num_states - 1];
765
766 return ERROR_OK;
767 }
768
769 int jtag_add_runtest(int num_cycles, enum tap_state state)
770 {
771 jtag_command_t **last_cmd = jtag_get_last_command_p();
772
773 if (jtag_trst == 1)
774 {
775 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
776 return ERROR_JTAG_TRST_ASSERTED;
777 }
778
779 /* allocate memory for a new list member */
780 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
781 (*last_cmd)->next = NULL;
782 last_comand_pointer = &((*last_cmd)->next);
783 (*last_cmd)->type = JTAG_RUNTEST;
784
785 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
786 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
787 (*last_cmd)->cmd.runtest->end_state = state;
788
789 if (state != -1)
790 cmd_queue_end_state = state;
791
792 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
793 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
794
795 if (cmd_queue_end_state == TAP_TLR)
796 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
797
798 cmd_queue_cur_state = cmd_queue_end_state;
799
800 return ERROR_OK;
801 }
802
803 int jtag_add_reset(int req_trst, int req_srst)
804 {
805 int trst_with_tms = 0;
806
807 jtag_command_t **last_cmd = jtag_get_last_command_p();
808
809 if (req_trst == -1)
810 req_trst = jtag_trst;
811
812 if (req_srst == -1)
813 req_srst = jtag_srst;
814
815 /* Make sure that jtag_reset_config allows the requested reset */
816 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
817 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
818 return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
819
820 /* if TRST pulls SRST, we reset with TAP T-L-R */
821 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
822 {
823 req_trst = 0;
824 trst_with_tms = 1;
825 }
826
827 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
828 {
829 ERROR("requested nSRST assertion, but the current configuration doesn't support this");
830 return ERROR_JTAG_RESET_CANT_SRST;
831 }
832
833 if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
834 {
835 req_trst = 0;
836 trst_with_tms = 1;
837 }
838
839 /* allocate memory for a new list member */
840 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
841 (*last_cmd)->next = NULL;
842 last_comand_pointer = &((*last_cmd)->next);
843 (*last_cmd)->type = JTAG_RESET;
844
845 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
846 (*last_cmd)->cmd.reset->trst = req_trst;
847 (*last_cmd)->cmd.reset->srst = req_srst;
848
849 jtag_trst = req_trst;
850 jtag_srst = req_srst;
851
852 if (jtag_srst)
853 {
854 jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
855 }
856 else
857 {
858 jtag_call_event_callbacks(JTAG_SRST_RELEASED);
859 if (jtag_nsrst_delay)
860 jtag_add_sleep(jtag_nsrst_delay * 1000);
861 }
862
863 if (trst_with_tms)
864 {
865 last_cmd = &((*last_cmd)->next);
866
867 /* allocate memory for a new list member */
868 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
869 (*last_cmd)->next = NULL;
870 last_comand_pointer = &((*last_cmd)->next);
871 (*last_cmd)->type = JTAG_STATEMOVE;
872
873 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
874 (*last_cmd)->cmd.statemove->end_state = TAP_TLR;
875
876 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
877 cmd_queue_cur_state = TAP_TLR;
878 cmd_queue_end_state = TAP_TLR;
879
880 return ERROR_OK;
881 }
882 else
883 {
884 if (jtag_trst)
885 {
886 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
887 * and inform possible listeners about this
888 */
889 cmd_queue_cur_state = TAP_TLR;
890 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
891 }
892 else
893 {
894 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
895 * but we might want to add a delay to give the TAP time to settle
896 */
897 if (jtag_ntrst_delay)
898 jtag_add_sleep(jtag_ntrst_delay * 1000);
899 }
900 }
901
902 return ERROR_OK;
903 }
904
905 int jtag_add_end_state(enum tap_state state)
906 {
907 jtag_command_t **last_cmd = jtag_get_last_command_p();
908
909 /* allocate memory for a new list member */
910 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
911 (*last_cmd)->next = NULL;
912 last_comand_pointer = &((*last_cmd)->next);
913 (*last_cmd)->type = JTAG_END_STATE;
914
915 (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
916 (*last_cmd)->cmd.end_state->end_state = state;
917
918 if (state != -1)
919 cmd_queue_end_state = state;
920
921 return ERROR_OK;
922 }
923
924 int jtag_add_sleep(u32 us)
925 {
926 jtag_command_t **last_cmd = jtag_get_last_command_p();
927
928 /* allocate memory for a new list member */
929 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
930 (*last_cmd)->next = NULL;
931 last_comand_pointer = &((*last_cmd)->next);
932 (*last_cmd)->type = JTAG_SLEEP;
933
934 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
935 (*last_cmd)->cmd.sleep->us = us;
936
937 return ERROR_OK;
938 }
939
940 int jtag_scan_size(scan_command_t *cmd)
941 {
942 int bit_count = 0;
943 int i;
944
945 /* count bits in scan command */
946 for (i=0; i<cmd->num_fields; i++)
947 {
948 bit_count += cmd->fields[i].num_bits;
949 }
950
951 return bit_count;
952 }
953
954 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
955 {
956 int bit_count = 0;
957 int i;
958
959 bit_count = jtag_scan_size(cmd);
960 *buffer = malloc(CEIL(bit_count, 8));
961
962 bit_count = 0;
963
964 for (i = 0; i < cmd->num_fields; i++)
965 {
966 if (cmd->fields[i].out_value)
967 {
968 #ifdef _DEBUG_JTAG_IO_
969 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
970 #endif
971 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
972 #ifdef _DEBUG_JTAG_IO_
973 DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
974 free(char_buf);
975 #endif
976 }
977
978 bit_count += cmd->fields[i].num_bits;
979 }
980
981 return bit_count;
982
983 }
984
985 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
986 {
987 int i;
988 int bit_count = 0;
989 int retval = ERROR_OK;
990
991 for (i=0; i < cmd->num_fields; i++)
992 {
993 /* if neither in_value, in_check_value nor in_handler
994 * are specified we don't have to examine this field
995 */
996 if (cmd->fields[i].in_value || cmd->fields[i].in_check_value || cmd->fields[i].in_handler)
997 {
998 int num_bits = cmd->fields[i].num_bits;
999 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1000 #ifdef _DEBUG_JTAG_IO_
1001 char *char_buf;
1002
1003 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1004 DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1005 free(char_buf);
1006 #endif
1007
1008
1009 if (cmd->fields[i].in_value)
1010 {
1011 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1012
1013 if (cmd->fields[i].in_handler)
1014 {
1015 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv) != ERROR_OK)
1016 {
1017 /* TODO: error reporting */
1018 WARNING("in_handler reported a failed check");
1019 retval = ERROR_JTAG_QUEUE_FAILED;
1020 }
1021 }
1022 }
1023
1024 /* no in_value specified, but a handler takes care of the scanned data */
1025 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1026 {
1027 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv) != ERROR_OK)
1028 {
1029 /* TODO: error reporting */
1030 WARNING("in_handler reported a failed check");
1031 retval = ERROR_JTAG_QUEUE_FAILED;
1032 }
1033
1034 }
1035
1036 if (cmd->fields[i].in_check_value)
1037 {
1038 if ((cmd->fields[i].in_check_mask && buf_cmp_mask(captured, cmd->fields[i].in_check_value, cmd->fields[i].in_check_mask, num_bits))
1039 || (!cmd->fields[i].in_check_mask && buf_cmp(captured, cmd->fields[i].in_check_mask, num_bits)))
1040 {
1041 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1042 char *in_check_value_char = buf_to_str(cmd->fields[i].in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1043 char *in_check_mask_char = buf_to_str(cmd->fields[i].in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1044 /* TODO: error reporting */
1045 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);
1046 retval = ERROR_JTAG_QUEUE_FAILED;
1047 free(captured_char);
1048 free(in_check_value_char);
1049 free(in_check_mask_char);
1050 }
1051 }
1052 free(captured);
1053 }
1054 bit_count += cmd->fields[i].num_bits;
1055 }
1056
1057 return retval;
1058 }
1059
1060 enum scan_type jtag_scan_type(scan_command_t *cmd)
1061 {
1062 int i;
1063 int type = 0;
1064
1065 for (i=0; i < cmd->num_fields; i++)
1066 {
1067 if (cmd->fields[i].in_check_value || cmd->fields[i].in_value || cmd->fields[i].in_handler)
1068 type |= SCAN_IN;
1069 if (cmd->fields[i].out_value)
1070 type |= SCAN_OUT;
1071 }
1072
1073 return type;
1074 }
1075
1076 int jtag_execute_queue(void)
1077 {
1078 int retval;
1079
1080 retval = jtag->execute_queue();
1081
1082 cmd_queue_free();
1083
1084 jtag_command_queue = NULL;
1085 last_comand_pointer = &jtag_command_queue;
1086
1087 return retval;
1088 }
1089
1090 int jtag_cancel_queue(void)
1091 {
1092 cmd_queue_free();
1093 jtag_command_queue = NULL;
1094 last_comand_pointer = &jtag_command_queue;
1095
1096 return ERROR_OK;
1097 }
1098
1099 int jtag_reset_callback(enum jtag_event event, void *priv)
1100 {
1101 jtag_device_t *device = priv;
1102
1103 DEBUG("");
1104
1105 if (event == JTAG_TRST_ASSERTED)
1106 {
1107 buf_set_ones(device->cur_instr, device->ir_length);
1108 device->bypass = 1;
1109 }
1110
1111 return ERROR_OK;
1112 }
1113
1114 void jtag_sleep(u32 us)
1115 {
1116 usleep(us);
1117 }
1118
1119 int jtag_validate_chain()
1120 {
1121 jtag_device_t *device = jtag_devices;
1122 int total_ir_length = 0;
1123 u8 *ir_test = NULL;
1124 scan_field_t field;
1125 int chain_pos = 0;
1126
1127 while (device)
1128 {
1129 total_ir_length += device->ir_length;
1130 device = device->next;
1131 }
1132
1133 total_ir_length += 2;
1134 ir_test = malloc(CEIL(total_ir_length, 8));
1135 buf_set_ones(ir_test, total_ir_length);
1136
1137 field.device = 0;
1138 field.num_bits = total_ir_length;
1139 field.out_value = ir_test;
1140 field.out_mask = NULL;
1141 field.in_value = ir_test;
1142 field.in_check_value = NULL;
1143 field.in_check_mask = NULL;
1144 field.in_handler = NULL;
1145 field.in_handler_priv = NULL;
1146
1147 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1148 jtag_execute_queue();
1149
1150 device = jtag_devices;
1151 while (device)
1152 {
1153 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1154 {
1155 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1156 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1157 free(cbuf);
1158 exit(-1);
1159 }
1160 chain_pos += device->ir_length;
1161 device = device->next;
1162 }
1163
1164 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1165 {
1166 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1167 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1168 free(cbuf);
1169 exit(-1);
1170 }
1171
1172 free(ir_test);
1173
1174 return ERROR_OK;
1175 }
1176
1177 int jtag_register_commands(struct command_context_s *cmd_ctx)
1178 {
1179 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1180 COMMAND_CONFIG, NULL);
1181 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1182 COMMAND_ANY, "set jtag speed (if supported) <speed>");
1183 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1184 COMMAND_CONFIG, NULL);
1185 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1186 COMMAND_CONFIG, NULL);
1187 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1188 COMMAND_CONFIG, NULL);
1189 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1190 COMMAND_CONFIG, NULL);
1191
1192 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1193 COMMAND_EXEC, "print current scan chain configuration");
1194
1195 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1196 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1197 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1198 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1199 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1200 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1201 register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1202 COMMAND_EXEC, "move to current endstate or [tap_state]");
1203 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1204 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1205 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1206 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1207
1208 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1209 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1210 return ERROR_OK;
1211 }
1212
1213 int jtag_init(struct command_context_s *cmd_ctx)
1214 {
1215 int i;
1216
1217 DEBUG("");
1218
1219 if (jtag_speed == -1)
1220 jtag_speed = 0;
1221
1222 if (jtag_interface && (jtag_interface[0] != 0))
1223 /* configuration var 'jtag_interface' is set, and not empty */
1224 for (i = 0; jtag_interfaces[i]; i++)
1225 {
1226 if (strcmp(jtag_interface, jtag_interfaces[i]->name) == 0)
1227 {
1228 jtag_device_t *device;
1229 device = jtag_devices;
1230
1231 if (jtag_interfaces[i]->init() != ERROR_OK)
1232 return ERROR_JTAG_INIT_FAILED;
1233 jtag = jtag_interfaces[i];
1234
1235 jtag_ir_scan_size = 0;
1236 jtag_num_devices = 0;
1237 while (device != NULL)
1238 {
1239 jtag_ir_scan_size += device->ir_length;
1240 jtag_num_devices++;
1241 device = device->next;
1242 }
1243
1244 jtag_add_statemove(TAP_TLR);
1245 jtag_execute_queue();
1246
1247 jtag_validate_chain();
1248
1249 return ERROR_OK;
1250 }
1251 }
1252
1253 /* no valid interface was found (i.e. the configuration option,
1254 * didn't match one of the compiled-in interfaces
1255 */
1256 ERROR("No valid jtag interface found (%s)", jtag_interface);
1257 ERROR("compiled-in jtag interfaces:");
1258 for (i = 0; jtag_interfaces[i]; i++)
1259 {
1260 ERROR("%i: %s", i, jtag_interfaces[i]->name);
1261 }
1262
1263 jtag = NULL;
1264 return ERROR_JTAG_INVALID_INTERFACE;
1265 }
1266
1267 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1268 {
1269 int i;
1270
1271 /* only if the configuration var isn't overwritten from cmdline */
1272 if (!jtag_interface)
1273 {
1274 if (args[0] && (args[0][0] != 0))
1275 {
1276 for (i=0; jtag_interfaces[i]; i++)
1277 {
1278 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1279 {
1280 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1281 exit(-1);
1282
1283 jtag_interface = jtag_interfaces[i]->name;
1284
1285 return ERROR_OK;
1286 }
1287 }
1288 }
1289
1290 /* remember the requested interface name, so we can complain about it later */
1291 jtag_interface = strdup(args[0]);
1292 DEBUG("'interface' command didn't specify a valid interface");
1293 }
1294
1295 return ERROR_OK;
1296 }
1297
1298 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1299 {
1300 jtag_device_t **last_device_p = &jtag_devices;
1301
1302 if (*last_device_p)
1303 {
1304 while ((*last_device_p)->next)
1305 last_device_p = &((*last_device_p)->next);
1306 last_device_p = &((*last_device_p)->next);
1307 }
1308
1309 if (argc < 3)
1310 return ERROR_OK;
1311
1312 *last_device_p = malloc(sizeof(jtag_device_t));
1313 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1314
1315 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1316 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1317 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1318 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1319
1320 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1321 (*last_device_p)->bypass = 1;
1322 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1323
1324 (*last_device_p)->next = NULL;
1325
1326 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1327
1328 jtag_num_devices++;
1329
1330 return ERROR_OK;
1331 }
1332
1333 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1334 {
1335 jtag_device_t *device = jtag_devices;
1336 int device_count = 0;
1337
1338 while (device)
1339 {
1340 u32 expected, expected_mask, cur_instr;
1341 expected = buf_get_u32(device->expected, 0, device->ir_length);
1342 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1343 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1344 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);
1345 device = device->next;
1346 device_count++;
1347 }
1348
1349 return ERROR_OK;
1350 }
1351
1352 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1353 {
1354 if (argc >= 1)
1355 {
1356 if (strcmp(args[0], "none") == 0)
1357 jtag_reset_config = RESET_NONE;
1358 else if (strcmp(args[0], "trst_only") == 0)
1359 jtag_reset_config = RESET_HAS_TRST;
1360 else if (strcmp(args[0], "srst_only") == 0)
1361 jtag_reset_config = RESET_HAS_SRST;
1362 else if (strcmp(args[0], "trst_and_srst") == 0)
1363 jtag_reset_config = RESET_TRST_AND_SRST;
1364 else
1365 {
1366 ERROR("invalid reset_config argument");
1367 exit(-1);
1368 }
1369 }
1370
1371 if (argc >= 2)
1372 {
1373 if (strcmp(args[1], "srst_pulls_trst") == 0)
1374 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1375 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1376 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1377 else if (strcmp(args[1], "combined") == 0)
1378 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1379 else if (strcmp(args[1], "separate") == 0)
1380 jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
1381 else
1382 {
1383 ERROR("invalid reset_config argument");
1384 exit(-1);
1385 }
1386 }
1387
1388 if (argc >= 3)
1389 {
1390 if (strcmp(args[2], "trst_open_drain") == 0)
1391 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1392 else if (strcmp(args[2], "trst_push_pull") == 0)
1393 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1394 else
1395 {
1396 ERROR("invalid reset_config argument");
1397 exit(-1);
1398 }
1399 }
1400
1401 if (argc >= 4)
1402 {
1403 if (strcmp(args[3], "srst_push_pull") == 0)
1404 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1405 else if (strcmp(args[3], "srst_open_drain") == 0)
1406 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1407 else
1408 {
1409 ERROR("invalid reset_config argument");
1410 exit(-1);
1411 }
1412 }
1413
1414 return ERROR_OK;
1415 }
1416
1417 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1418 {
1419 if (argc < 1)
1420 {
1421 ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1422 exit(-1);
1423 }
1424 else
1425 {
1426 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1427 }
1428
1429 return ERROR_OK;
1430 }
1431
1432 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1433 {
1434 if (argc < 1)
1435 {
1436 ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1437 exit(-1);
1438 }
1439 else
1440 {
1441 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1442 }
1443
1444 return ERROR_OK;
1445 }
1446
1447 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1448 {
1449 if (argc == 0)
1450 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
1451
1452 if (argc > 0)
1453 {
1454 /* this command can be called during CONFIG,
1455 * in which case jtag isn't initialized */
1456 if (jtag)
1457 jtag->speed(strtoul(args[0], NULL, 0));
1458 else
1459 jtag_speed = strtoul(args[0], NULL, 0);
1460 }
1461
1462 return ERROR_OK;
1463 }
1464
1465 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1466 {
1467 enum tap_state state;
1468
1469 if (argc < 1)
1470 {
1471 command_print(cmd_ctx, "usage: endstate <tap_state>");
1472 return ERROR_OK;
1473 }
1474
1475 for (state = 0; state < 16; state++)
1476 {
1477 if (strcmp(args[0], tap_state_strings[state]) == 0)
1478 {
1479 jtag_add_end_state(state);
1480 jtag_execute_queue();
1481 }
1482 }
1483
1484 return ERROR_OK;
1485 }
1486
1487 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1488 {
1489 int trst = -1;
1490 int srst = -1;
1491 char *usage = "usage: jtag_reset <trst> <srst>";
1492 int retval;
1493
1494 if (argc < 1)
1495 {
1496 command_print(cmd_ctx, usage);
1497 return ERROR_OK;
1498 }
1499
1500 if (args[0][0] == '1')
1501 trst = 1;
1502 else if (args[0][0] == '0')
1503 trst = 0;
1504 else
1505 {
1506 command_print(cmd_ctx, usage);
1507 return ERROR_OK;
1508 }
1509
1510 if (args[1][0] == '1')
1511 srst = 1;
1512 else if (args[1][0] == '0')
1513 srst = 0;
1514 else
1515 {
1516 command_print(cmd_ctx, usage);
1517 return ERROR_OK;
1518 }
1519
1520 if ((retval = jtag_add_reset(trst, srst)) != ERROR_OK)
1521 {
1522 switch (retval)
1523 {
1524 case ERROR_JTAG_RESET_WOULD_ASSERT_TRST:
1525 command_print(cmd_ctx, "requested reset would assert trst\nif this is acceptable, use jtag_reset 1 %c", args[1][0]);
1526 break;
1527 case ERROR_JTAG_RESET_CANT_SRST:
1528 command_print(cmd_ctx, "can't assert srst because the current reset_config doesn't support it");
1529 break;
1530 default:
1531 command_print(cmd_ctx, "unknown error");
1532 }
1533 }
1534 jtag_execute_queue();
1535
1536 return ERROR_OK;
1537 }
1538
1539 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1540 {
1541 if (argc < 1)
1542 {
1543 command_print(cmd_ctx, "usage: runtest <num_cycles>");
1544 return ERROR_OK;
1545 }
1546
1547 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1548 jtag_execute_queue();
1549
1550 return ERROR_OK;
1551
1552 }
1553
1554 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1555 {
1556 enum tap_state state;
1557
1558 state = -1;
1559 if (argc == 1)
1560 {
1561 for (state = 0; state < 16; state++)
1562 {
1563 if (strcmp(args[0], tap_state_strings[state]) == 0)
1564 {
1565 break;
1566 }
1567 }
1568 }
1569
1570 jtag_add_statemove(state);
1571 jtag_execute_queue();
1572
1573 return ERROR_OK;
1574
1575 }
1576
1577 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1578 {
1579 int i;
1580 scan_field_t *fields;
1581
1582 if ((argc < 2) || (argc % 2))
1583 {
1584 command_print(cmd_ctx, "usage: irscan <device> <instr> [dev2] [instr2] ...");
1585 return ERROR_OK;
1586 }
1587
1588 fields = malloc(sizeof(scan_field_t) * argc / 2);
1589
1590 for (i = 0; i < argc / 2; i++)
1591 {
1592 int device = strtoul(args[i*2], NULL, 0);
1593 int field_size = jtag_get_device(device)->ir_length;
1594 fields[i].device = device;
1595 fields[i].out_value = malloc(CEIL(field_size, 8));
1596 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1597 fields[i].out_mask = NULL;
1598 fields[i].in_value = NULL;
1599 fields[i].in_check_mask = NULL;
1600 fields[i].in_handler = NULL;
1601 fields[i].in_handler_priv = NULL;
1602 }
1603
1604 jtag_add_ir_scan(argc / 2, fields, -1);
1605 jtag_execute_queue();
1606
1607 for (i = 0; i < argc / 2; i++)
1608 free(fields[i].out_value);
1609
1610 free (fields);
1611
1612 return ERROR_OK;
1613 }
1614
1615 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1616 {
1617 scan_field_t *fields;
1618 int num_fields = 0;
1619 int field_count = 0;
1620 var_t *var;
1621 int i, j;
1622
1623 if ((argc < 2) || (argc % 2))
1624 {
1625 command_print(cmd_ctx, "usage: drscan <device> <var> [dev2] [var2]");
1626 return ERROR_OK;
1627 }
1628
1629 for (i = 0; i < argc; i+=2)
1630 {
1631 var = get_var_by_namenum(args[i+1]);
1632 if (var)
1633 {
1634 num_fields += var->num_fields;
1635 }
1636 else
1637 {
1638 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1639 return ERROR_OK;
1640 }
1641 }
1642
1643 fields = malloc(sizeof(scan_field_t) * num_fields);
1644
1645 for (i = 0; i < argc; i+=2)
1646 {
1647 var = get_var_by_namenum(args[i+1]);
1648
1649 for (j = 0; j < var->num_fields; j++)
1650 {
1651 fields[field_count].device = strtol(args[i], NULL, 0);
1652 fields[field_count].num_bits = var->fields[j].num_bits;
1653 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1654 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1655 fields[field_count].out_mask = NULL;
1656 fields[field_count].in_value = fields[field_count].out_value;
1657 fields[field_count].in_check_mask = NULL;
1658 fields[field_count].in_check_value = NULL;
1659 fields[field_count].in_handler = field_le_to_host;
1660 fields[field_count++].in_handler_priv = &(var->fields[j]);
1661 }
1662 }
1663
1664 jtag_add_dr_scan(num_fields, fields, -1);
1665 jtag_execute_queue();
1666
1667 for (i = 0; i < argc / 2; i++)
1668 free(fields[i].out_value);
1669
1670 free(fields);
1671
1672 return ERROR_OK;
1673 }
1674
1675 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1676 {
1677 if (argc == 0)
1678 {
1679 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
1680 return ERROR_OK;
1681 }
1682
1683 if (strcmp(args[0], "enable") == 0)
1684 {
1685 jtag_verify_capture_ir = 1;
1686 }
1687 else if (strcmp(args[0], "disable") == 0)
1688 {
1689 jtag_verify_capture_ir = 0;
1690 }
1691
1692 return ERROR_OK;
1693 }

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)