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