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