1 /***************************************************************************
\r
2 * Copyright (C) 2005 by Dominic Rath *
\r
3 * Dominic.Rath@gmx.de *
\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
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
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 #ifdef HAVE_CONFIG_H
\r
24 #include "replacements.h"
\r
28 #include "command.h"
\r
30 #include "interpreter.h"
\r
36 char* tap_state_strings[16] =
\r
39 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
\r
41 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
\r
44 typedef struct cmd_queue_page_s
\r
48 struct cmd_queue_page_s *next;
\r
51 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
\r
52 static cmd_queue_page_t *cmd_queue_pages = NULL;
\r
54 /* tap_move[i][j]: tap movement command to go from state i to state j
\r
55 * 0: Test-Logic-Reset
\r
62 * SD->SD and SI->SI have to be caught in interface specific code
\r
66 /* TLR RTI SD PD SI PI */
\r
67 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
\r
68 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
\r
69 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
\r
70 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
\r
71 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
\r
72 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
\r
75 int tap_move_map[16] = {
\r
76 0, -1, -1, 2, -1, 3, -1, -1,
\r
77 1, -1, -1, 4, -1, 5, -1, -1
\r
80 tap_transition_t tap_transitions[16] =
\r
82 {TAP_TLR, TAP_RTI}, /* TLR */
\r
83 {TAP_SIS, TAP_CD}, /* SDS */
\r
84 {TAP_E1D, TAP_SD}, /* CD */
\r
85 {TAP_E1D, TAP_SD}, /* SD */
\r
86 {TAP_UD, TAP_PD}, /* E1D */
\r
87 {TAP_E2D, TAP_PD}, /* PD */
\r
88 {TAP_UD, TAP_SD}, /* E2D */
\r
89 {TAP_SDS, TAP_RTI}, /* UD */
\r
90 {TAP_SDS, TAP_RTI}, /* RTI */
\r
91 {TAP_TLR, TAP_CI}, /* SIS */
\r
92 {TAP_E1I, TAP_SI}, /* CI */
\r
93 {TAP_E1I, TAP_SI}, /* SI */
\r
94 {TAP_UI, TAP_PI}, /* E1I */
\r
95 {TAP_E2I, TAP_PI}, /* PI */
\r
96 {TAP_UI, TAP_SI}, /* E2I */
\r
97 {TAP_SDS, TAP_RTI} /* UI */
\r
100 char* jtag_event_strings[] =
\r
108 enum tap_state end_state = TAP_TLR;
\r
109 enum tap_state cur_state = TAP_TLR;
\r
113 jtag_command_t *jtag_command_queue = NULL;
\r
114 jtag_command_t **last_comand_pointer = &jtag_command_queue;
\r
115 jtag_device_t *jtag_devices = NULL;
\r
116 int jtag_num_devices = 0;
\r
117 int jtag_ir_scan_size = 0;
\r
118 enum reset_types jtag_reset_config = RESET_NONE;
\r
119 enum tap_state cmd_queue_end_state = TAP_TLR;
\r
120 enum tap_state cmd_queue_cur_state = TAP_TLR;
\r
122 int jtag_verify_capture_ir = 1;
\r
124 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
\r
125 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
\r
126 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
\r
128 /* maximum number of JTAG devices expected in the chain
\r
130 #define JTAG_MAX_CHAIN_SIZE 20
\r
132 /* callbacks to inform high-level handlers about JTAG state changes */
\r
133 jtag_event_callback_t *jtag_event_callbacks;
\r
135 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
\r
137 #if BUILD_PARPORT == 1
\r
138 extern jtag_interface_t parport_interface;
\r
141 #if BUILD_FT2232_FTD2XX == 1
\r
142 extern jtag_interface_t ft2232_interface;
\r
145 #if BUILD_FT2232_LIBFTDI == 1
\r
146 extern jtag_interface_t ft2232_interface;
\r
149 #if BUILD_AMTJTAGACCEL == 1
\r
150 extern jtag_interface_t amt_jtagaccel_interface;
\r
153 #if BUILD_EP93XX == 1
\r
154 extern jtag_interface_t ep93xx_interface;
\r
157 #if BUILD_AT91RM9200 == 1
\r
158 extern jtag_interface_t at91rm9200_interface;
\r
161 #if BUILD_GW16012 == 1
\r
162 extern jtag_interface_t gw16012_interface;
\r
165 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
\r
166 extern jtag_interface_t presto_interface;
\r
169 #if BUILD_USBPROG == 1
\r
170 extern jtag_interface_t usbprog_interface;
\r
173 jtag_interface_t *jtag_interfaces[] = {
\r
174 #if BUILD_PARPORT == 1
\r
175 &parport_interface,
\r
177 #if BUILD_FT2232_FTD2XX == 1
\r
180 #if BUILD_FT2232_LIBFTDI == 1
\r
183 #if BUILD_AMTJTAGACCEL == 1
\r
184 &amt_jtagaccel_interface,
\r
186 #if BUILD_EP93XX == 1
\r
189 #if BUILD_AT91RM9200 == 1
\r
190 &at91rm9200_interface,
\r
192 #if BUILD_GW16012 == 1
\r
193 &gw16012_interface,
\r
195 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
\r
198 #if BUILD_USBPROG == 1
\r
199 &usbprog_interface,
\r
204 jtag_interface_t *jtag = NULL;
\r
206 /* configuration */
\r
207 char* jtag_interface = NULL;
\r
208 int jtag_speed = -1;
\r
211 /* forward declarations */
\r
212 int jtag_add_statemove(enum tap_state endstate);
\r
213 int jtag_add_pathmove(int num_states, enum tap_state *path);
\r
214 int jtag_add_runtest(int num_cycles, enum tap_state endstate);
\r
215 int jtag_add_reset(int trst, int srst);
\r
216 int jtag_add_end_state(enum tap_state endstate);
\r
217 int jtag_add_sleep(u32 us);
\r
218 int jtag_execute_queue(void);
\r
219 int jtag_cancel_queue(void);
\r
221 /* jtag commands */
\r
222 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
223 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
224 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
225 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
226 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
227 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
229 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
231 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
232 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
233 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
234 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
235 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
236 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
238 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
240 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
\r
242 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
\r
244 if (callback == NULL)
\r
246 return ERROR_INVALID_ARGUMENTS;
\r
251 while ((*callbacks_p)->next)
\r
252 callbacks_p = &((*callbacks_p)->next);
\r
253 callbacks_p = &((*callbacks_p)->next);
\r
256 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
\r
257 (*callbacks_p)->callback = callback;
\r
258 (*callbacks_p)->priv = priv;
\r
259 (*callbacks_p)->next = NULL;
\r
264 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
\r
266 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
\r
268 if (callback == NULL)
\r
270 return ERROR_INVALID_ARGUMENTS;
\r
273 while (*callbacks_p)
\r
275 jtag_event_callback_t **next = &((*callbacks_p)->next);
\r
276 if ((*callbacks_p)->callback == callback)
\r
278 free(*callbacks_p);
\r
279 *callbacks_p = *next;
\r
281 callbacks_p = next;
\r
287 int jtag_call_event_callbacks(enum jtag_event event)
\r
289 jtag_event_callback_t *callback = jtag_event_callbacks;
\r
291 DEBUG("jtag event: %s", jtag_event_strings[event]);
\r
295 callback->callback(event, callback->priv);
\r
296 callback = callback->next;
\r
302 /* returns a pointer to the pointer of the last command in queue
\r
303 * this may be a pointer to the root pointer (jtag_command_queue)
\r
304 * or to the next member of the last but one command
\r
306 jtag_command_t** jtag_get_last_command_p(void)
\r
308 /* jtag_command_t *cmd = jtag_command_queue;
\r
314 return &jtag_command_queue;
\r
316 return &cmd->next;*/
\r
318 return last_comand_pointer;
\r
321 /* returns a pointer to the n-th device in the scan chain */
\r
322 jtag_device_t* jtag_get_device(int num)
\r
324 jtag_device_t *device = jtag_devices;
\r
331 device = device->next;
\r
335 ERROR("jtag device number %d not defined", num);
\r
339 void* cmd_queue_alloc(size_t size)
\r
341 cmd_queue_page_t **p_page = &cmd_queue_pages;
\r
346 while ((*p_page)->next)
\r
347 p_page = &((*p_page)->next);
\r
348 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
\r
349 p_page = &((*p_page)->next);
\r
354 *p_page = malloc(sizeof(cmd_queue_page_t));
\r
355 (*p_page)->used = 0;
\r
356 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
\r
357 (*p_page)->next = NULL;
\r
360 offset = (*p_page)->used;
\r
361 (*p_page)->used += size;
\r
363 u8 *t=(u8 *)((*p_page)->address);
\r
367 void cmd_queue_free()
\r
369 cmd_queue_page_t *page = cmd_queue_pages;
\r
373 cmd_queue_page_t *last = page;
\r
374 free(page->address);
\r
379 cmd_queue_pages = NULL;
\r
382 int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
384 jtag_command_t **last_cmd;
\r
385 jtag_device_t *device;
\r
389 if (jtag_trst == 1)
\r
391 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
\r
392 return ERROR_JTAG_TRST_ASSERTED;
\r
395 last_cmd = jtag_get_last_command_p();
\r
397 /* allocate memory for a new list member */
\r
398 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
399 (*last_cmd)->next = NULL;
\r
400 last_comand_pointer = &((*last_cmd)->next);
\r
401 (*last_cmd)->type = JTAG_SCAN;
\r
403 /* allocate memory for ir scan command */
\r
404 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
\r
405 (*last_cmd)->cmd.scan->ir_scan = 1;
\r
406 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
\r
407 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
\r
408 (*last_cmd)->cmd.scan->end_state = state;
\r
411 cmd_queue_end_state = state;
\r
413 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
\r
414 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
\r
416 if (cmd_queue_end_state == TAP_TLR)
\r
417 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
\r
419 cmd_queue_cur_state = cmd_queue_end_state;
\r
421 for (i = 0; i < jtag_num_devices; i++)
\r
424 device = jtag_get_device(i);
\r
425 scan_size = device->ir_length;
\r
426 (*last_cmd)->cmd.scan->fields[i].device = i;
\r
427 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
\r
428 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
\r
429 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
\r
431 /* search the list */
\r
432 for (j = 0; j < num_fields; j++)
\r
434 if (i == fields[j].device)
\r
437 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
\r
438 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
\r
440 if (jtag_verify_capture_ir)
\r
442 if (fields[j].in_handler==NULL)
\r
444 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
\r
447 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
\r
448 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
\r
449 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
\r
450 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
\r
454 device->bypass = 0;
\r
461 /* if a device isn't listed, set it to BYPASS */
\r
462 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
\r
463 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
\r
464 device->bypass = 1;
\r
468 /* update device information */
\r
469 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
\r
475 int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
477 jtag_command_t **last_cmd;
\r
480 if (jtag_trst == 1)
\r
482 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
\r
483 return ERROR_JTAG_TRST_ASSERTED;
\r
486 last_cmd = jtag_get_last_command_p();
\r
488 /* allocate memory for a new list member */
\r
489 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
490 (*last_cmd)->next = NULL;
\r
491 last_comand_pointer = &((*last_cmd)->next);
\r
492 (*last_cmd)->type = JTAG_SCAN;
\r
494 /* allocate memory for ir scan command */
\r
495 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
\r
496 (*last_cmd)->cmd.scan->ir_scan = 1;
\r
497 (*last_cmd)->cmd.scan->num_fields = num_fields;
\r
498 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
\r
499 (*last_cmd)->cmd.scan->end_state = state;
\r
502 cmd_queue_end_state = state;
\r
504 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
\r
505 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
\r
507 if (cmd_queue_end_state == TAP_TLR)
\r
508 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
\r
510 cmd_queue_cur_state = cmd_queue_end_state;
\r
512 for (i = 0; i < num_fields; i++)
\r
514 int num_bits = fields[i].num_bits;
\r
515 int num_bytes = CEIL(fields[i].num_bits, 8);
\r
516 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
\r
517 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
\r
518 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
\r
519 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
\r
520 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
\r
521 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
\r
522 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
\r
523 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
\r
524 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
\r
529 int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
532 int bypass_devices = 0;
\r
533 int field_count = 0;
\r
534 jtag_command_t **last_cmd = jtag_get_last_command_p();
\r
535 jtag_device_t *device = jtag_devices;
\r
538 if (jtag_trst == 1)
\r
540 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
\r
541 return ERROR_JTAG_TRST_ASSERTED;
\r
544 /* count devices in bypass */
\r
547 if (device->bypass)
\r
549 device = device->next;
\r
552 /* allocate memory for a new list member */
\r
553 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
554 last_comand_pointer = &((*last_cmd)->next);
\r
555 (*last_cmd)->next = NULL;
\r
556 (*last_cmd)->type = JTAG_SCAN;
\r
558 /* allocate memory for dr scan command */
\r
559 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
\r
560 (*last_cmd)->cmd.scan->ir_scan = 0;
\r
561 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
\r
562 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
\r
563 (*last_cmd)->cmd.scan->end_state = state;
\r
566 cmd_queue_end_state = state;
\r
568 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
\r
569 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
\r
571 if (cmd_queue_end_state == TAP_TLR)
\r
572 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
\r
574 cmd_queue_cur_state = cmd_queue_end_state;
\r
576 for (i = 0; i < jtag_num_devices; i++)
\r
579 (*last_cmd)->cmd.scan->fields[field_count].device = i;
\r
581 for (j = 0; j < num_fields; j++)
\r
583 if (i == fields[j].device)
\r
586 scan_size = fields[j].num_bits;
\r
587 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
\r
588 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
\r
589 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
\r
590 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
\r
591 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
\r
592 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
\r
593 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
\r
594 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
\r
599 /* if a device isn't listed, the BYPASS register should be selected */
\r
600 if (!jtag_get_device(i)->bypass)
\r
602 ERROR("BUG: no scan data for a device not in BYPASS");
\r
606 /* program the scan field to 1 bit length, and ignore it's value */
\r
607 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
\r
608 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
\r
609 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
\r
610 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
\r
611 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
\r
612 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
\r
613 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
\r
614 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
\r
618 /* if a device is listed, the BYPASS register must not be selected */
\r
619 if (jtag_get_device(i)->bypass)
\r
621 WARNING("scan data for a device in BYPASS");
\r
629 int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
632 jtag_command_t **last_cmd = jtag_get_last_command_p();
\r
634 if (jtag_trst == 1)
\r
636 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
\r
637 return ERROR_JTAG_TRST_ASSERTED;
\r
640 /* allocate memory for a new list member */
\r
641 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
642 last_comand_pointer = &((*last_cmd)->next);
\r
643 (*last_cmd)->next = NULL;
\r
644 (*last_cmd)->type = JTAG_SCAN;
\r
646 /* allocate memory for scan command */
\r
647 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
\r
648 (*last_cmd)->cmd.scan->ir_scan = 0;
\r
649 (*last_cmd)->cmd.scan->num_fields = num_fields;
\r
650 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
\r
651 (*last_cmd)->cmd.scan->end_state = state;
\r
654 cmd_queue_end_state = state;
\r
656 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
\r
657 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
\r
659 if (cmd_queue_end_state == TAP_TLR)
\r
660 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
\r
662 cmd_queue_cur_state = cmd_queue_end_state;
\r
664 for (i = 0; i < num_fields; i++)
\r
666 int num_bits = fields[i].num_bits;
\r
667 int num_bytes = CEIL(fields[i].num_bits, 8);
\r
668 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
\r
669 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
\r
670 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
\r
671 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
\r
672 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
\r
673 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
\r
674 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
\r
675 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
\r
676 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
\r
681 int jtag_add_statemove(enum tap_state state)
\r
683 jtag_command_t **last_cmd = jtag_get_last_command_p();
\r
685 if (jtag_trst == 1)
\r
687 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
\r
688 return ERROR_JTAG_TRST_ASSERTED;
\r
691 /* allocate memory for a new list member */
\r
692 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
693 last_comand_pointer = &((*last_cmd)->next);
\r
694 (*last_cmd)->next = NULL;
\r
695 (*last_cmd)->type = JTAG_STATEMOVE;
\r
697 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
\r
698 (*last_cmd)->cmd.statemove->end_state = state;
\r
701 cmd_queue_end_state = state;
\r
703 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
\r
704 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
\r
706 if (cmd_queue_end_state == TAP_TLR)
\r
707 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
\r
709 cmd_queue_cur_state = cmd_queue_end_state;
\r
714 int jtag_add_pathmove(int num_states, enum tap_state *path)
\r
716 jtag_command_t **last_cmd = jtag_get_last_command_p();
\r
719 if (jtag_trst == 1)
\r
721 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
\r
722 return ERROR_JTAG_TRST_ASSERTED;
\r
725 /* the last state has to be a stable state */
\r
726 if (tap_move_map[path[num_states - 1]] == -1)
\r
728 ERROR("TAP path doesn't finish in a stable state");
\r
729 return ERROR_JTAG_NOT_IMPLEMENTED;
\r
732 /* allocate memory for a new list member */
\r
733 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
734 last_comand_pointer = &((*last_cmd)->next);
\r
735 (*last_cmd)->next = NULL;
\r
736 (*last_cmd)->type = JTAG_PATHMOVE;
\r
738 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
\r
739 (*last_cmd)->cmd.pathmove->num_states = num_states;
\r
740 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
\r
742 for (i = 0; i < num_states; i++)
\r
743 (*last_cmd)->cmd.pathmove->path[i] = path[i];
\r
745 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
\r
746 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
\r
748 if (cmd_queue_end_state == TAP_TLR)
\r
749 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
\r
751 cmd_queue_cur_state = path[num_states - 1];
\r
756 int jtag_add_runtest(int num_cycles, enum tap_state state)
\r
758 jtag_command_t **last_cmd = jtag_get_last_command_p();
\r
760 if (jtag_trst == 1)
\r
762 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
\r
763 return ERROR_JTAG_TRST_ASSERTED;
\r
766 /* allocate memory for a new list member */
\r
767 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
768 (*last_cmd)->next = NULL;
\r
769 last_comand_pointer = &((*last_cmd)->next);
\r
770 (*last_cmd)->type = JTAG_RUNTEST;
\r
772 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
\r
773 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
\r
774 (*last_cmd)->cmd.runtest->end_state = state;
\r
777 cmd_queue_end_state = state;
\r
779 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
\r
780 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
\r
782 if (cmd_queue_end_state == TAP_TLR)
\r
783 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
\r
785 cmd_queue_cur_state = cmd_queue_end_state;
\r
790 int jtag_add_reset(int req_trst, int req_srst)
\r
792 int trst_with_tms = 0;
\r
794 jtag_command_t **last_cmd = jtag_get_last_command_p();
\r
796 if (req_trst == -1)
\r
797 req_trst = jtag_trst;
\r
799 if (req_srst == -1)
\r
800 req_srst = jtag_srst;
\r
802 /* Make sure that jtag_reset_config allows the requested reset */
\r
803 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
\r
804 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
\r
805 return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
\r
807 /* if TRST pulls SRST, we reset with TAP T-L-R */
\r
808 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
\r
814 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
\r
816 ERROR("requested nSRST assertion, but the current configuration doesn't support this");
\r
817 return ERROR_JTAG_RESET_CANT_SRST;
\r
820 if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
\r
826 /* allocate memory for a new list member */
\r
827 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
828 (*last_cmd)->next = NULL;
\r
829 last_comand_pointer = &((*last_cmd)->next);
\r
830 (*last_cmd)->type = JTAG_RESET;
\r
832 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
\r
833 (*last_cmd)->cmd.reset->trst = req_trst;
\r
834 (*last_cmd)->cmd.reset->srst = req_srst;
\r
836 jtag_trst = req_trst;
\r
837 jtag_srst = req_srst;
\r
841 jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
\r
845 jtag_call_event_callbacks(JTAG_SRST_RELEASED);
\r
846 if (jtag_nsrst_delay)
\r
847 jtag_add_sleep(jtag_nsrst_delay * 1000);
\r
852 last_cmd = &((*last_cmd)->next);
\r
854 /* allocate memory for a new list member */
\r
855 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
856 (*last_cmd)->next = NULL;
\r
857 last_comand_pointer = &((*last_cmd)->next);
\r
858 (*last_cmd)->type = JTAG_STATEMOVE;
\r
860 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
\r
861 (*last_cmd)->cmd.statemove->end_state = TAP_TLR;
\r
863 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
\r
864 cmd_queue_cur_state = TAP_TLR;
\r
865 cmd_queue_end_state = TAP_TLR;
\r
873 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
\r
874 * and inform possible listeners about this
\r
876 cmd_queue_cur_state = TAP_TLR;
\r
877 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
\r
881 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
\r
882 * but we might want to add a delay to give the TAP time to settle
\r
884 if (jtag_ntrst_delay)
\r
885 jtag_add_sleep(jtag_ntrst_delay * 1000);
\r
892 int jtag_add_end_state(enum tap_state state)
\r
894 jtag_command_t **last_cmd = jtag_get_last_command_p();
\r
896 /* allocate memory for a new list member */
\r
897 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
898 (*last_cmd)->next = NULL;
\r
899 last_comand_pointer = &((*last_cmd)->next);
\r
900 (*last_cmd)->type = JTAG_END_STATE;
\r
902 (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
\r
903 (*last_cmd)->cmd.end_state->end_state = state;
\r
906 cmd_queue_end_state = state;
\r
911 int jtag_add_sleep(u32 us)
\r
913 jtag_command_t **last_cmd = jtag_get_last_command_p();
\r
915 /* allocate memory for a new list member */
\r
916 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
\r
917 (*last_cmd)->next = NULL;
\r
918 last_comand_pointer = &((*last_cmd)->next);
\r
919 (*last_cmd)->type = JTAG_SLEEP;
\r
921 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
\r
922 (*last_cmd)->cmd.sleep->us = us;
\r
927 int jtag_scan_size(scan_command_t *cmd)
\r
932 /* count bits in scan command */
\r
933 for (i = 0; i < cmd->num_fields; i++)
\r
935 bit_count += cmd->fields[i].num_bits;
\r
941 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
\r
946 bit_count = jtag_scan_size(cmd);
\r
947 *buffer = malloc(CEIL(bit_count, 8));
\r
951 for (i = 0; i < cmd->num_fields; i++)
\r
953 if (cmd->fields[i].out_value)
\r
955 #ifdef _DEBUG_JTAG_IO_
\r
956 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
\r
958 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
\r
959 #ifdef _DEBUG_JTAG_IO_
\r
960 DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
\r
965 bit_count += cmd->fields[i].num_bits;
\r
972 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
\r
978 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
\r
981 for (i = 0; i < cmd->num_fields; i++)
\r
983 /* if neither in_value nor in_handler
\r
984 * are specified we don't have to examine this field
\r
986 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
\r
988 int num_bits = cmd->fields[i].num_bits;
\r
989 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
\r
991 #ifdef _DEBUG_JTAG_IO_
\r
994 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
\r
995 DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
\r
999 if (cmd->fields[i].in_value)
\r
1001 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
\r
1003 if (cmd->fields[i].in_handler)
\r
1005 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
\r
1007 WARNING("in_handler reported a failed check");
\r
1008 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1013 /* no in_value specified, but a handler takes care of the scanned data */
\r
1014 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
\r
1016 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
\r
1018 /* We're going to call the error:handler later, but if the in_handler
\r
1019 * reported an error we report this failure upstream
\r
1021 WARNING("in_handler reported a failed check");
\r
1022 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1028 bit_count += cmd->fields[i].num_bits;
\r
1034 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
\r
1036 int retval = ERROR_OK;
\r
1037 int num_bits = field->num_bits;
\r
1039 int compare_failed = 0;
\r
1041 if (field->in_check_mask)
\r
1042 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
\r
1044 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
\r
1046 if (compare_failed)
\r
1048 /* An error handler could have caught the failing check
\r
1049 * only report a problem when there wasn't a handler, or if the handler
\r
1050 * acknowledged the error
\r
1052 if (compare_failed)
\r
1054 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
\r
1055 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
\r
1057 if (field->in_check_mask)
\r
1059 char *in_check_mask_char;
\r
1060 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
\r
1061 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);
\r
1062 free(in_check_mask_char);
\r
1066 WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
\r
1069 free(captured_char);
\r
1070 free(in_check_value_char);
\r
1072 retval = ERROR_JTAG_QUEUE_FAILED;
\r
1080 set up checking of this field using the in_handler. The values passed in must be valid until
\r
1081 after jtag_execute() has completed.
\r
1083 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
\r
1086 field->in_handler = jtag_check_value;
\r
1088 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
\r
1089 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
\r
1090 field->in_check_value = value;
\r
1091 field->in_check_mask = mask;
\r
1094 enum scan_type jtag_scan_type(scan_command_t *cmd)
\r
1099 for (i = 0; i < cmd->num_fields; i++)
\r
1101 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
\r
1103 if (cmd->fields[i].out_value)
\r
1110 int jtag_execute_queue(void)
\r
1114 retval = jtag->execute_queue();
\r
1118 jtag_command_queue = NULL;
\r
1119 last_comand_pointer = &jtag_command_queue;
\r
1124 int jtag_reset_callback(enum jtag_event event, void *priv)
\r
1126 jtag_device_t *device = priv;
\r
1130 if (event == JTAG_TRST_ASSERTED)
\r
1132 buf_set_ones(device->cur_instr, device->ir_length);
\r
1133 device->bypass = 1;
\r
1139 void jtag_sleep(u32 us)
\r
1144 /* Try to examine chain layout according to IEEE 1149.1 §12
\r
1146 int jtag_examine_chain()
\r
1148 jtag_device_t *device = jtag_devices;
\r
1149 scan_field_t field;
\r
1150 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
\r
1153 int device_count = 0;
\r
1154 u8 zero_check = 0x0;
\r
1155 u8 one_check = 0xff;
\r
1158 field.num_bits = sizeof(idcode_buffer) * 8;
\r
1159 field.out_value = idcode_buffer;
\r
1160 field.out_mask = NULL;
\r
1161 field.in_value = idcode_buffer;
\r
1162 field.in_check_value = NULL;
\r
1163 field.in_check_mask = NULL;
\r
1164 field.in_handler = NULL;
\r
1165 field.in_handler_priv = NULL;
\r
1167 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
\r
1169 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
\r
1172 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
\r
1173 jtag_execute_queue();
\r
1175 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
\r
1177 zero_check |= idcode_buffer[i];
\r
1178 one_check &= idcode_buffer[i];
\r
1181 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
\r
1182 if ((zero_check == 0x00) || (one_check == 0xff))
\r
1184 ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
\r
1185 return ERROR_JTAG_INIT_FAILED;
\r
1188 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
\r
1190 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
\r
1191 if ((idcode & 1) == 0)
\r
1193 /* LSB must not be 0, this indicates a device in bypass */
\r
1204 if (idcode == 0x000000FF)
\r
1206 /* End of chain (invalid manufacturer ID) */
\r
1212 device->idcode = idcode;
\r
1213 device = device->next;
\r
1217 manufacturer = (idcode & 0xffe) >> 1;
\r
1218 part = (idcode & 0xffff000) >> 12;
\r
1219 version = (idcode & 0xf0000000) >> 28;
\r
1221 INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
\r
1222 idcode, manufacturer, part, version);
\r
1228 /* see if number of discovered devices matches configuration */
\r
1229 if (device_count != jtag_num_devices)
\r
1231 ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
\r
1232 device_count, jtag_num_devices);
\r
1233 ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
\r
1234 return ERROR_JTAG_INIT_FAILED;
\r
1240 int jtag_validate_chain()
\r
1242 jtag_device_t *device = jtag_devices;
\r
1243 int total_ir_length = 0;
\r
1244 u8 *ir_test = NULL;
\r
1245 scan_field_t field;
\r
1246 int chain_pos = 0;
\r
1250 total_ir_length += device->ir_length;
\r
1251 device = device->next;
\r
1254 total_ir_length += 2;
\r
1255 ir_test = malloc(CEIL(total_ir_length, 8));
\r
1256 buf_set_ones(ir_test, total_ir_length);
\r
1259 field.num_bits = total_ir_length;
\r
1260 field.out_value = ir_test;
\r
1261 field.out_mask = NULL;
\r
1262 field.in_value = ir_test;
\r
1263 field.in_check_value = NULL;
\r
1264 field.in_check_mask = NULL;
\r
1265 field.in_handler = NULL;
\r
1266 field.in_handler_priv = NULL;
\r
1268 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
\r
1269 jtag_execute_queue();
\r
1271 device = jtag_devices;
\r
1274 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
\r
1276 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
\r
1277 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
\r
1280 return ERROR_JTAG_INIT_FAILED;
\r
1282 chain_pos += device->ir_length;
\r
1283 device = device->next;
\r
1286 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
\r
1288 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
\r
1289 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
\r
1292 return ERROR_JTAG_INIT_FAILED;
\r
1300 int jtag_register_commands(struct command_context_s *cmd_ctx)
\r
1302 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
\r
1303 COMMAND_CONFIG, NULL);
\r
1304 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
\r
1305 COMMAND_ANY, "set jtag speed (if supported) <speed>");
\r
1306 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
\r
1307 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
\r
1308 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
\r
1309 COMMAND_CONFIG, NULL);
\r
1310 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
\r
1311 COMMAND_CONFIG, NULL);
\r
1312 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
\r
1313 COMMAND_CONFIG, NULL);
\r
1315 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
\r
1316 COMMAND_EXEC, "print current scan chain configuration");
\r
1318 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
\r
1319 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
\r
1320 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
\r
1321 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
\r
1322 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
\r
1323 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
\r
1324 register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
\r
1325 COMMAND_EXEC, "move to current endstate or [tap_state]");
\r
1326 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
\r
1327 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
\r
1328 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
\r
1329 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
\r
1331 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
\r
1332 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
\r
1336 int jtag_init(struct command_context_s *cmd_ctx)
\r
1338 int i, validate_tries = 0;
\r
1342 if (jtag_speed == -1)
\r
1345 if (jtag_interface && (jtag_interface[0] != 0))
\r
1346 /* configuration var 'jtag_interface' is set, and not empty */
\r
1347 for (i = 0; jtag_interfaces[i]; i++)
\r
1349 if (strcmp(jtag_interface, jtag_interfaces[i]->name) == 0)
\r
1351 jtag_device_t *device;
\r
1352 device = jtag_devices;
\r
1354 if (jtag_interfaces[i]->init() != ERROR_OK)
\r
1355 return ERROR_JTAG_INIT_FAILED;
\r
1356 jtag = jtag_interfaces[i];
\r
1358 jtag_ir_scan_size = 0;
\r
1359 jtag_num_devices = 0;
\r
1360 while (device != NULL)
\r
1362 jtag_ir_scan_size += device->ir_length;
\r
1363 jtag_num_devices++;
\r
1364 device = device->next;
\r
1367 jtag_add_statemove(TAP_TLR);
\r
1368 jtag_execute_queue();
\r
1370 /* examine chain first, as this could discover the real chain layout */
\r
1371 if (jtag_examine_chain() != ERROR_OK)
\r
1373 ERROR("trying to validate configured JTAG chain anyway...");
\r
1376 while (jtag_validate_chain() != ERROR_OK)
\r
1379 if (validate_tries > 5)
\r
1381 ERROR("Could not validate JTAG chain, exit");
\r
1383 return ERROR_JTAG_INVALID_INTERFACE;
\r
1392 /* no valid interface was found (i.e. the configuration option,
\r
1393 * didn't match one of the compiled-in interfaces
\r
1395 ERROR("No valid jtag interface found (%s)", jtag_interface);
\r
1396 ERROR("compiled-in jtag interfaces:");
\r
1397 for (i = 0; jtag_interfaces[i]; i++)
\r
1399 ERROR("%i: %s", i, jtag_interfaces[i]->name);
\r
1403 return ERROR_JTAG_INVALID_INTERFACE;
\r
1406 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1410 /* only if the configuration var isn't overwritten from cmdline */
\r
1411 if (!jtag_interface)
\r
1413 if (args[0] && (args[0][0] != 0))
\r
1415 for (i=0; jtag_interfaces[i]; i++)
\r
1417 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
\r
1419 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
\r
1422 jtag_interface = jtag_interfaces[i]->name;
\r
1429 /* remember the requested interface name, so we can complain about it later */
\r
1430 jtag_interface = strdup(args[0]);
\r
1431 DEBUG("'interface' command didn't specify a valid interface");
\r
1437 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1439 jtag_device_t **last_device_p = &jtag_devices;
\r
1441 if (*last_device_p)
\r
1443 while ((*last_device_p)->next)
\r
1444 last_device_p = &((*last_device_p)->next);
\r
1445 last_device_p = &((*last_device_p)->next);
\r
1451 *last_device_p = malloc(sizeof(jtag_device_t));
\r
1452 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
\r
1454 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
\r
1455 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
\r
1456 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
\r
1457 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
\r
1459 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
\r
1460 (*last_device_p)->bypass = 1;
\r
1461 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
\r
1463 (*last_device_p)->next = NULL;
\r
1465 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
\r
1467 jtag_num_devices++;
\r
1472 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1474 jtag_device_t *device = jtag_devices;
\r
1475 int device_count = 0;
\r
1479 u32 expected, expected_mask, cur_instr;
\r
1480 expected = buf_get_u32(device->expected, 0, device->ir_length);
\r
1481 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
\r
1482 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
\r
1483 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);
\r
1484 device = device->next;
\r
1491 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1495 if (strcmp(args[0], "none") == 0)
\r
1496 jtag_reset_config = RESET_NONE;
\r
1497 else if (strcmp(args[0], "trst_only") == 0)
\r
1498 jtag_reset_config = RESET_HAS_TRST;
\r
1499 else if (strcmp(args[0], "srst_only") == 0)
\r
1500 jtag_reset_config = RESET_HAS_SRST;
\r
1501 else if (strcmp(args[0], "trst_and_srst") == 0)
\r
1502 jtag_reset_config = RESET_TRST_AND_SRST;
\r
1505 ERROR("invalid reset_config argument, defaulting to none");
\r
1506 jtag_reset_config = RESET_NONE;
\r
1507 return ERROR_INVALID_ARGUMENTS;
\r
1513 if (strcmp(args[1], "srst_pulls_trst") == 0)
\r
1514 jtag_reset_config |= RESET_SRST_PULLS_TRST;
\r
1515 else if (strcmp(args[1], "trst_pulls_srst") == 0)
\r
1516 jtag_reset_config |= RESET_TRST_PULLS_SRST;
\r
1517 else if (strcmp(args[1], "combined") == 0)
\r
1518 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
\r
1519 else if (strcmp(args[1], "separate") == 0)
\r
1520 jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
\r
1523 ERROR("invalid reset_config argument, defaulting to none");
\r
1524 jtag_reset_config = RESET_NONE;
\r
1525 return ERROR_INVALID_ARGUMENTS;
\r
1531 if (strcmp(args[2], "trst_open_drain") == 0)
\r
1532 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
\r
1533 else if (strcmp(args[2], "trst_push_pull") == 0)
\r
1534 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
\r
1537 ERROR("invalid reset_config argument, defaulting to none");
\r
1538 jtag_reset_config = RESET_NONE;
\r
1539 return ERROR_INVALID_ARGUMENTS;
\r
1545 if (strcmp(args[3], "srst_push_pull") == 0)
\r
1546 jtag_reset_config |= RESET_SRST_PUSH_PULL;
\r
1547 else if (strcmp(args[3], "srst_open_drain") == 0)
\r
1548 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
\r
1551 ERROR("invalid reset_config argument, defaulting to none");
\r
1552 jtag_reset_config = RESET_NONE;
\r
1553 return ERROR_INVALID_ARGUMENTS;
\r
1560 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1564 ERROR("jtag_nsrst_delay <ms> command takes one required argument");
\r
1569 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
\r
1575 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1579 ERROR("jtag_ntrst_delay <ms> command takes one required argument");
\r
1584 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
\r
1590 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1593 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
\r
1597 /* this command can be called during CONFIG,
\r
1598 * in which case jtag isn't initialized */
\r
1600 jtag->speed(strtoul(args[0], NULL, 0));
\r
1602 jtag_speed = strtoul(args[0], NULL, 0);
\r
1608 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1610 enum tap_state state;
\r
1614 return ERROR_COMMAND_SYNTAX_ERROR;
\r
1618 for (state = 0; state < 16; state++)
\r
1620 if (strcmp(args[0], tap_state_strings[state]) == 0)
\r
1622 jtag_add_end_state(state);
\r
1623 jtag_execute_queue();
\r
1627 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
\r
1632 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1640 return ERROR_COMMAND_SYNTAX_ERROR;
\r
1644 if (args[0][0] == '1')
\r
1646 else if (args[0][0] == '0')
\r
1650 return ERROR_COMMAND_SYNTAX_ERROR;
\r
1653 if (args[1][0] == '1')
\r
1655 else if (args[1][0] == '0')
\r
1659 return ERROR_COMMAND_SYNTAX_ERROR;
\r
1662 if ((retval = jtag_add_reset(trst, srst)) != ERROR_OK)
\r
1666 case ERROR_JTAG_RESET_WOULD_ASSERT_TRST:
\r
1667 command_print(cmd_ctx, "requested reset would assert trst\nif this is acceptable, use jtag_reset 1 %c", args[1][0]);
\r
1669 case ERROR_JTAG_RESET_CANT_SRST:
\r
1670 command_print(cmd_ctx, "can't assert srst because the current reset_config doesn't support it");
\r
1673 command_print(cmd_ctx, "unknown error");
\r
1676 jtag_execute_queue();
\r
1681 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1685 return ERROR_COMMAND_SYNTAX_ERROR;
\r
1688 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
\r
1689 jtag_execute_queue();
\r
1695 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1697 enum tap_state state;
\r
1702 for (state = 0; state < 16; state++)
\r
1704 if (strcmp(args[0], tap_state_strings[state]) == 0)
\r
1711 jtag_add_statemove(state);
\r
1712 jtag_execute_queue();
\r
1718 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1721 scan_field_t *fields;
\r
1723 if ((argc < 2) || (argc % 2))
\r
1725 return ERROR_COMMAND_SYNTAX_ERROR;
\r
1728 fields = malloc(sizeof(scan_field_t) * argc / 2);
\r
1730 for (i = 0; i < argc / 2; i++)
\r
1732 int device = strtoul(args[i*2], NULL, 0);
\r
1733 int field_size = jtag_get_device(device)->ir_length;
\r
1734 fields[i].device = device;
\r
1735 fields[i].out_value = malloc(CEIL(field_size, 8));
\r
1736 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
\r
1737 fields[i].out_mask = NULL;
\r
1738 fields[i].in_value = NULL;
\r
1739 fields[i].in_check_mask = NULL;
\r
1740 fields[i].in_handler = NULL;
\r
1741 fields[i].in_handler_priv = NULL;
\r
1744 jtag_add_ir_scan(argc / 2, fields, -1);
\r
1745 jtag_execute_queue();
\r
1747 for (i = 0; i < argc / 2; i++)
\r
1748 free(fields[i].out_value);
\r
1755 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1757 scan_field_t *fields;
\r
1758 int num_fields = 0;
\r
1759 int field_count = 0;
\r
1763 if ((argc < 2) || (argc % 2))
\r
1765 return ERROR_COMMAND_SYNTAX_ERROR;
\r
1768 for (i = 0; i < argc; i+=2)
\r
1770 var = get_var_by_namenum(args[i+1]);
\r
1773 num_fields += var->num_fields;
\r
1777 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
\r
1782 fields = malloc(sizeof(scan_field_t) * num_fields);
\r
1784 for (i = 0; i < argc; i+=2)
\r
1786 var = get_var_by_namenum(args[i+1]);
\r
1788 for (j = 0; j < var->num_fields; j++)
\r
1790 fields[field_count].device = strtol(args[i], NULL, 0);
\r
1791 fields[field_count].num_bits = var->fields[j].num_bits;
\r
1792 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
\r
1793 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
\r
1794 fields[field_count].out_mask = NULL;
\r
1795 fields[field_count].in_value = fields[field_count].out_value;
\r
1796 fields[field_count].in_check_mask = NULL;
\r
1797 fields[field_count].in_check_value = NULL;
\r
1798 fields[field_count].in_handler = field_le_to_host;
\r
1799 fields[field_count++].in_handler_priv = &(var->fields[j]);
\r
1803 jtag_add_dr_scan(num_fields, fields, -1);
\r
1804 jtag_execute_queue();
\r
1806 for (i = 0; i < argc / 2; i++)
\r
1807 free(fields[i].out_value);
\r
1814 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
1818 if (strcmp(args[0], "enable") == 0)
\r
1820 jtag_verify_capture_ir = 1;
\r
1822 else if (strcmp(args[0], "disable") == 0)
\r
1824 jtag_verify_capture_ir = 0;
\r
1827 return ERROR_COMMAND_SYNTAX_ERROR;
\r
1829 } else if (argc != 0)
\r
1831 return ERROR_COMMAND_SYNTAX_ERROR;
\r
1834 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
\r