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