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