- retired unused jtag events. The code was incorrect
[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 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
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 (state != -1)
404                 cmd_queue_end_state = state;
405
406         if (cmd_queue_end_state == TAP_TLR)
407                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
408         
409         cmd_queue_cur_state = cmd_queue_end_state;
410         
411         int retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
412         if (retval!=ERROR_OK)
413                 jtag_error=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 void 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                 jtag_error=ERROR_JTAG_TRST_ASSERTED;
499                 return;
500         }
501
502         if (state != -1)
503                 cmd_queue_end_state = state;
504
505         if (cmd_queue_end_state == TAP_TLR)
506                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
507                 
508         cmd_queue_cur_state = cmd_queue_end_state;
509         
510         int retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
511         if (retval!=ERROR_OK)
512                 jtag_error=retval;
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 void 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                 jtag_error=ERROR_JTAG_TRST_ASSERTED;
558                 return;
559         }
560
561         if (state != -1)
562                 cmd_queue_end_state = state;
563
564         if (cmd_queue_end_state == TAP_TLR)
565                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
566                         
567         cmd_queue_cur_state = cmd_queue_end_state;
568
569         int retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
570         if (retval!=ERROR_OK)
571                 jtag_error=retval;
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                                 ERROR("BUG: scan data for a device in BYPASS");
653                                 exit(-1);
654                         }
655 #endif
656                 }
657         }
658         return ERROR_OK;
659 }
660
661 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num, 
662                 int num_fields,
663                 int *num_bits,
664                 u32 *value,
665                 enum tap_state end_state)
666 {
667         int i;
668         int field_count = 0;
669         int scan_size;
670         int bypass_devices = 0;
671
672         jtag_command_t **last_cmd = jtag_get_last_command_p();
673         jtag_device_t *device = jtag_devices;
674         /* count devices in bypass */
675         while (device)
676         {
677                 if (device->bypass)
678                         bypass_devices++;
679                 device = device->next;
680         }
681         
682         /* allocate memory for a new list member */
683         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
684         last_comand_pointer = &((*last_cmd)->next);
685         (*last_cmd)->next = NULL;
686         (*last_cmd)->type = JTAG_SCAN;
687
688         /* allocate memory for dr scan command */
689         (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
690         (*last_cmd)->cmd.scan->ir_scan = 0;
691         (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
692         (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
693         (*last_cmd)->cmd.scan->end_state = end_state;
694         
695         for (i = 0; i < jtag_num_devices; i++)
696         {
697                 (*last_cmd)->cmd.scan->fields[field_count].device = i;
698         
699                 if (i == device_num)
700                 {
701                         int j;
702 #ifdef _DEBUG_JTAG_IO_
703                         /* if a device is listed, the BYPASS register must not be selected */
704                         if (jtag_get_device(i)->bypass)
705                         {
706                                 ERROR("BUG: scan data for a device in BYPASS");
707                                 exit(-1);
708                         }
709 #endif
710                         for (j = 0; j < num_fields; j++)
711                         {
712                                 u8 out_value[4];
713                                 scan_size = num_bits[j];
714                                 buf_set_u32(out_value, 0, scan_size, value[j]);
715                                 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
716                                 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
717                                 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
718                                 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
719                                 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
720                                 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
721                                 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
722                                 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
723                         }
724                 } else
725                 {
726 #ifdef _DEBUG_JTAG_IO_
727                         /* if a device isn't listed, the BYPASS register should be selected */
728                         if (!jtag_get_device(i)->bypass)
729                         {
730                                 ERROR("BUG: no scan data for a device not in BYPASS");
731                                 exit(-1);
732                         }
733 #endif  
734                         /* program the scan field to 1 bit length, and ignore it's value */
735                         (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
736                         (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
737                         (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
738                         (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
739                         (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
740                         (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
741                         (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
742                         (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
743                 }
744         }
745 }
746
747
748
749
750 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
751 {
752         if (jtag_trst == 1)
753         {
754                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
755                 jtag_error=ERROR_JTAG_TRST_ASSERTED;
756                 return;
757         }
758
759         if (state != -1)
760                 cmd_queue_end_state = state;
761
762         if (cmd_queue_end_state == TAP_TLR)
763                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
764                         
765         cmd_queue_cur_state = cmd_queue_end_state;
766
767         int retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
768         if (retval!=ERROR_OK)
769                 jtag_error=retval;
770 }
771
772 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
773 {
774         int i;
775         jtag_command_t **last_cmd = jtag_get_last_command_p();
776         
777         /* allocate memory for a new list member */
778         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
779         last_comand_pointer = &((*last_cmd)->next);
780         (*last_cmd)->next = NULL;
781         (*last_cmd)->type = JTAG_SCAN;
782
783         /* allocate memory for scan command */
784         (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
785         (*last_cmd)->cmd.scan->ir_scan = 0;
786         (*last_cmd)->cmd.scan->num_fields = num_fields;
787         (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
788         (*last_cmd)->cmd.scan->end_state = state;
789         
790         for (i = 0; i < num_fields; i++)
791         {
792                 int num_bits = fields[i].num_bits;
793                 int num_bytes = CEIL(fields[i].num_bits, 8);
794                 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
795                 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
796                 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
797                 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
798                 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
799                 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
800                 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
801                 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
802                 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
803         }
804
805         return ERROR_OK;
806 }
807 void jtag_add_statemove(enum tap_state state)
808 {
809         if (jtag_trst == 1)
810         {
811                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
812                 jtag_error=ERROR_JTAG_TRST_ASSERTED;
813                 return;
814         }
815
816         if (state != -1)
817                 cmd_queue_end_state = state;
818
819         if (cmd_queue_end_state == TAP_TLR)
820                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
821                         
822         cmd_queue_cur_state = cmd_queue_end_state;
823
824         int retval;
825         retval=interface_jtag_add_statemove(cmd_queue_end_state);
826         if (retval!=ERROR_OK)
827                 jtag_error=retval;
828 }
829
830 int MINIDRIVER(interface_jtag_add_statemove)(enum tap_state state)
831 {
832         jtag_command_t **last_cmd = jtag_get_last_command_p();
833         
834         /* allocate memory for a new list member */
835         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
836         last_comand_pointer = &((*last_cmd)->next);
837         (*last_cmd)->next = NULL;
838         (*last_cmd)->type = JTAG_STATEMOVE;
839
840         (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
841         (*last_cmd)->cmd.statemove->end_state = state;
842         
843         
844         return ERROR_OK;
845 }
846
847 void jtag_add_pathmove(int num_states, enum tap_state *path)
848 {
849         if (jtag_trst == 1)
850         {
851                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
852                 jtag_error=ERROR_JTAG_TRST_ASSERTED;
853                 return;
854         }
855         
856         if (cmd_queue_end_state == TAP_TLR)
857                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
858         
859         /* the last state has to be a stable state */
860         if (tap_move_map[path[num_states - 1]] == -1)
861         {
862                 ERROR("TAP path doesn't finish in a stable state");
863                 jtag_error=ERROR_JTAG_NOT_IMPLEMENTED;
864                 return;
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         int retval=interface_jtag_add_pathmove(num_states, path);
883         if (retval!=ERROR_OK)
884                 jtag_error=retval;
885 }
886
887
888 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
889 {
890         jtag_command_t **last_cmd = jtag_get_last_command_p();
891         int i;
892         
893         /* allocate memory for a new list member */
894         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
895         last_comand_pointer = &((*last_cmd)->next);
896         (*last_cmd)->next = NULL;
897         (*last_cmd)->type = JTAG_PATHMOVE;
898
899         (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
900         (*last_cmd)->cmd.pathmove->num_states = num_states;
901         (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
902         
903         for (i = 0; i < num_states; i++)
904                 (*last_cmd)->cmd.pathmove->path[i] = path[i];
905         
906         return ERROR_OK;
907 }
908
909 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
910 {
911         jtag_command_t **last_cmd = jtag_get_last_command_p();
912         
913         /* allocate memory for a new list member */
914         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
915         (*last_cmd)->next = NULL;
916         last_comand_pointer = &((*last_cmd)->next);
917         (*last_cmd)->type = JTAG_RUNTEST;
918
919         (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
920         (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
921         (*last_cmd)->cmd.runtest->end_state = state;
922         
923         return ERROR_OK;
924 }
925
926 void jtag_add_runtest(int num_cycles, enum tap_state state)
927 {
928         if (jtag_trst == 1)
929         {
930                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
931                 jtag_error=ERROR_JTAG_TRST_ASSERTED;
932                 return;
933         }
934         
935         if (state != -1)
936                 cmd_queue_end_state = state;
937
938         if (cmd_queue_end_state == TAP_TLR)
939                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
940                         
941         cmd_queue_cur_state = cmd_queue_end_state;
942         
943         /* executed by sw or hw fifo */
944         int retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
945         if (retval!=ERROR_OK)
946                 jtag_error=retval;
947 }
948
949 int jtag_add_reset(int req_trst, int req_srst)
950 {
951         int trst_with_tms = 0;
952         int retval;
953         
954         if (req_trst == -1)
955                 req_trst = jtag_trst;
956         
957         if (req_srst == -1)
958                 req_srst = jtag_srst;
959
960         /* Make sure that jtag_reset_config allows the requested reset */
961         /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
962         if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
963         {
964                 ERROR("requested reset would assert trst");
965                 return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
966         }
967                 
968         /* if TRST pulls SRST, we reset with TAP T-L-R */
969         if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
970         {
971                 req_trst = 0;
972                 trst_with_tms = 1;
973         }
974         
975         if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
976         {
977                 ERROR("requested nSRST assertion, but the current configuration doesn't support this");
978                 return ERROR_JTAG_RESET_CANT_SRST;
979         }
980         
981         if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
982         {
983                 req_trst = 0;
984                 trst_with_tms = 1;
985         }
986
987         jtag_trst = req_trst;
988         jtag_srst = req_srst;
989
990         retval = interface_jtag_add_reset(req_trst, req_srst);
991         if (retval!=ERROR_OK)
992         {
993                 jtag_error=retval;
994                 return retval;
995         }
996
997         if (jtag_srst)
998         {
999                 DEBUG("SRST line asserted");
1000         }
1001         else
1002         {
1003                 DEBUG("SRST line released");
1004                 if (jtag_nsrst_delay)
1005                         jtag_add_sleep(jtag_nsrst_delay * 1000);
1006         }
1007         
1008         if (trst_with_tms)
1009         {
1010                 DEBUG("JTAG reset with tms instead of TRST");
1011                 jtag_add_end_state(TAP_TLR);
1012                 jtag_add_statemove(TAP_TLR);
1013                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1014                 return ERROR_OK;
1015         }
1016         
1017         if (jtag_trst)
1018         {
1019                 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1020                  * and inform possible listeners about this
1021                  */
1022                 DEBUG("TRST line asserted");
1023                 cmd_queue_cur_state = TAP_TLR;
1024                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1025         }
1026         else
1027         {
1028                 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
1029                  * but we might want to add a delay to give the TAP time to settle
1030                  */
1031                 DEBUG("Now in TAP_TLR - Test-Logic-Reset(either due to TRST line asserted or tms reset)");
1032                 if (jtag_ntrst_delay)
1033                         jtag_add_sleep(jtag_ntrst_delay * 1000);
1034         }
1035         return ERROR_OK;
1036 }
1037
1038 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1039 {
1040         jtag_command_t **last_cmd = jtag_get_last_command_p();
1041
1042         /* allocate memory for a new list member */
1043         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1044         (*last_cmd)->next = NULL;
1045         last_comand_pointer = &((*last_cmd)->next);
1046         (*last_cmd)->type = JTAG_RESET;
1047
1048         (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1049         (*last_cmd)->cmd.reset->trst = req_trst;
1050         (*last_cmd)->cmd.reset->srst = req_srst;
1051
1052         
1053         return ERROR_OK;
1054 }
1055
1056 void jtag_add_end_state(enum tap_state state)
1057 {
1058         cmd_queue_end_state = state;
1059 }
1060
1061 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1062 {
1063         jtag_command_t **last_cmd = jtag_get_last_command_p();
1064         
1065         /* allocate memory for a new list member */
1066         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1067         (*last_cmd)->next = NULL;
1068         last_comand_pointer = &((*last_cmd)->next);
1069         (*last_cmd)->type = JTAG_SLEEP;
1070
1071         (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1072         (*last_cmd)->cmd.sleep->us = us;
1073         
1074         return ERROR_OK;
1075 }
1076
1077 void jtag_add_sleep(u32 us)
1078 {
1079         int retval=interface_jtag_add_sleep(us);
1080         if (retval!=ERROR_OK)
1081                 jtag_error=retval;
1082         return;
1083 }
1084
1085 int jtag_scan_size(scan_command_t *cmd)
1086 {
1087         int bit_count = 0;
1088         int i;
1089
1090         /* count bits in scan command */
1091         for (i = 0; i < cmd->num_fields; i++)
1092         {
1093                 bit_count += cmd->fields[i].num_bits;
1094         }
1095
1096         return bit_count;
1097 }
1098
1099 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1100 {
1101         int bit_count = 0;
1102         int i;
1103         
1104         bit_count = jtag_scan_size(cmd);
1105         *buffer = malloc(CEIL(bit_count, 8));
1106         
1107         bit_count = 0;
1108
1109         for (i = 0; i < cmd->num_fields; i++)
1110         {
1111                 if (cmd->fields[i].out_value)
1112                 {
1113 #ifdef _DEBUG_JTAG_IO_
1114                         char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1115 #endif
1116                         buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1117 #ifdef _DEBUG_JTAG_IO_
1118                         DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1119                         free(char_buf);
1120 #endif
1121                 }
1122                 
1123                 bit_count += cmd->fields[i].num_bits;
1124         }
1125
1126         return bit_count;
1127
1128 }
1129
1130 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1131 {
1132         int i;
1133         int bit_count = 0;
1134         int retval;
1135         
1136         /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1137         retval = ERROR_OK;
1138         
1139         for (i = 0; i < cmd->num_fields; i++)
1140         {
1141                 /* if neither in_value nor in_handler
1142                  * are specified we don't have to examine this field
1143                  */
1144                 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1145                 {
1146                         int num_bits = cmd->fields[i].num_bits;
1147                         u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1148                         
1149 #ifdef _DEBUG_JTAG_IO_
1150                         char *char_buf;
1151
1152                         char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1153                         DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1154                         free(char_buf);
1155 #endif
1156                         
1157                         if (cmd->fields[i].in_value)
1158                         {
1159                                 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1160                                 
1161                                 if (cmd->fields[i].in_handler)
1162                                 {
1163                                         if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1164                                         {
1165                                                 WARNING("in_handler reported a failed check");
1166                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1167                                         }
1168                                 }
1169                         }
1170                         
1171                         /* no in_value specified, but a handler takes care of the scanned data */
1172                         if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1173                         {
1174                                 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1175                                 {
1176                                         /* We're going to call the error:handler later, but if the in_handler
1177                                          * reported an error we report this failure upstream
1178                                          */
1179                                         WARNING("in_handler reported a failed check");
1180                                         retval = ERROR_JTAG_QUEUE_FAILED;
1181                                 }
1182                         }
1183
1184                         free(captured);
1185                 }
1186                 bit_count += cmd->fields[i].num_bits;
1187         }
1188
1189         return retval;
1190 }
1191
1192 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1193 {
1194         int retval = ERROR_OK;
1195         int num_bits = field->num_bits;
1196         
1197         int compare_failed = 0;
1198         
1199         if (field->in_check_mask)
1200                 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1201         else
1202                 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1203         
1204         if (compare_failed)
1205                 {
1206                 /* An error handler could have caught the failing check
1207                  * only report a problem when there wasn't a handler, or if the handler
1208                  * acknowledged the error
1209                  */ 
1210                 if (compare_failed)
1211                 {
1212                         char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1213                         char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1214
1215                         if (field->in_check_mask)
1216                         {
1217                                 char *in_check_mask_char;
1218                                 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1219                                 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);
1220                                 free(in_check_mask_char);
1221                         }
1222                         else
1223                         {
1224                                 WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
1225                         }
1226
1227                         free(captured_char);
1228                         free(in_check_value_char);
1229                         
1230                         retval = ERROR_JTAG_QUEUE_FAILED;
1231                 }
1232                 
1233         }
1234         return retval;
1235 }
1236
1237 /* 
1238   set up checking of this field using the in_handler. The values passed in must be valid until
1239   after jtag_execute() has completed.
1240  */
1241 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1242 {
1243         if (value)
1244                 field->in_handler = jtag_check_value;
1245         else
1246                 field->in_handler = NULL;       /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1247         field->in_handler_priv = NULL;  /* this will be filled in at the invocation site to point to the field duplicate */ 
1248         field->in_check_value = value;
1249         field->in_check_mask = mask;
1250 }
1251
1252 enum scan_type jtag_scan_type(scan_command_t *cmd)
1253 {
1254         int i;
1255         int type = 0;
1256         
1257         for (i = 0; i < cmd->num_fields; i++)
1258         {
1259                 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1260                         type |= SCAN_IN;
1261                 if (cmd->fields[i].out_value)
1262                         type |= SCAN_OUT;
1263         }
1264
1265         return type;
1266 }
1267
1268 int MINIDRIVER(interface_jtag_execute_queue)(void)
1269 {
1270         int retval;
1271
1272         retval = jtag->execute_queue();
1273         
1274         cmd_queue_free();
1275
1276         jtag_command_queue = NULL;
1277         last_comand_pointer = &jtag_command_queue;
1278
1279         return retval;
1280 }
1281
1282 int jtag_execute_queue(void)
1283 {
1284         int retval=interface_jtag_execute_queue();
1285         if (retval==ERROR_OK)
1286         {
1287                 retval=jtag_error;
1288         }
1289         jtag_error=ERROR_OK;
1290         return retval;
1291 }
1292
1293 int jtag_reset_callback(enum jtag_event event, void *priv)
1294 {
1295         jtag_device_t *device = priv;
1296
1297         DEBUG("-");
1298         
1299         if (event == JTAG_TRST_ASSERTED)
1300         {
1301                 buf_set_ones(device->cur_instr, device->ir_length);
1302                 device->bypass = 1;
1303         }
1304         
1305         return ERROR_OK;
1306 }
1307
1308 void jtag_sleep(u32 us)
1309 {
1310         usleep(us);
1311 }
1312
1313 /* Try to examine chain layout according to IEEE 1149.1 Â§12
1314  */
1315 int jtag_examine_chain()
1316 {
1317         jtag_device_t *device = jtag_devices;
1318         scan_field_t field;
1319         u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1320         int i;
1321         int bit_count;
1322         int device_count = 0;
1323         u8 zero_check = 0x0;
1324         u8 one_check = 0xff;
1325         
1326         field.device = 0;
1327         field.num_bits = sizeof(idcode_buffer) * 8;
1328         field.out_value = idcode_buffer;
1329         field.out_mask = NULL;
1330         field.in_value = idcode_buffer;
1331         field.in_check_value = NULL;
1332         field.in_check_mask = NULL;
1333         field.in_handler = NULL;
1334         field.in_handler_priv = NULL;
1335         
1336         for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1337         {
1338                 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1339         }
1340         
1341         jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1342         jtag_execute_queue();
1343         
1344         for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1345         {
1346                 zero_check |= idcode_buffer[i];
1347                 one_check &= idcode_buffer[i];
1348         }
1349         
1350         /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1351         if ((zero_check == 0x00) || (one_check == 0xff))
1352         {
1353                 ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1354                 return ERROR_JTAG_INIT_FAILED;
1355         }
1356         
1357         for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1358         {
1359                 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1360                 if ((idcode & 1) == 0)
1361                 {
1362                         /* LSB must not be 0, this indicates a device in bypass */
1363                         device_count++;
1364                         
1365                         bit_count += 1;
1366                 }
1367                 else
1368                 {
1369                         u32 manufacturer;
1370                         u32 part;
1371                         u32 version;
1372                         
1373                         if (idcode == 0x000000FF)
1374                         {
1375                                 /* End of chain (invalid manufacturer ID) */
1376                                 break;
1377                         }
1378                         
1379                         if (device)
1380                         {
1381                                 device->idcode = idcode;
1382                                 device = device->next;
1383                         }
1384                         device_count++;
1385                         
1386                         manufacturer = (idcode & 0xffe) >> 1;
1387                         part = (idcode & 0xffff000) >> 12;
1388                         version = (idcode & 0xf0000000) >> 28;
1389
1390                         INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)", 
1391                                 idcode, manufacturer, part, version);
1392                         
1393                         bit_count += 32;
1394                 }
1395         }
1396         
1397         /* see if number of discovered devices matches configuration */
1398         if (device_count != jtag_num_devices)
1399         {
1400                 ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)", 
1401                                 device_count, jtag_num_devices);
1402                 ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1403                 return ERROR_JTAG_INIT_FAILED;
1404         }
1405         
1406         return ERROR_OK;
1407 }
1408
1409 int jtag_validate_chain()
1410 {
1411         jtag_device_t *device = jtag_devices;
1412         int total_ir_length = 0;
1413         u8 *ir_test = NULL;
1414         scan_field_t field;
1415         int chain_pos = 0;
1416         
1417         while (device)
1418         {
1419                 total_ir_length += device->ir_length;
1420                 device = device->next;
1421         }
1422         
1423         total_ir_length += 2;
1424         ir_test = malloc(CEIL(total_ir_length, 8));
1425         buf_set_ones(ir_test, total_ir_length);
1426         
1427         field.device = 0;
1428         field.num_bits = total_ir_length;
1429         field.out_value = ir_test;
1430         field.out_mask = NULL;
1431         field.in_value = ir_test;
1432         field.in_check_value = NULL;
1433         field.in_check_mask = NULL;
1434         field.in_handler = NULL;
1435         field.in_handler_priv = NULL;
1436         
1437         jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1438         jtag_execute_queue();
1439         
1440         device = jtag_devices;
1441         while (device)
1442         {
1443                 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1444                 {
1445                         char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1446                         ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1447                         free(cbuf);
1448                         free(ir_test);
1449                         return ERROR_JTAG_INIT_FAILED;
1450                 }
1451                 chain_pos += device->ir_length;
1452                 device = device->next;
1453         }
1454         
1455         if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1456         {
1457                 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1458                 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1459                 free(cbuf);
1460                 free(ir_test);
1461                 return ERROR_JTAG_INIT_FAILED;
1462         }
1463         
1464         free(ir_test);
1465         
1466         return ERROR_OK;
1467 }
1468
1469 int jtag_register_commands(struct command_context_s *cmd_ctx)
1470 {
1471         register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1472                 COMMAND_CONFIG, NULL);
1473         register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1474                 COMMAND_ANY, "set jtag speed (if supported) <speed>");
1475         register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1476                 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1477         register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1478                 COMMAND_CONFIG, NULL);
1479         register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1480                 COMMAND_CONFIG, NULL);
1481         register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1482                 COMMAND_CONFIG, NULL);
1483                 
1484         register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1485                 COMMAND_EXEC, "print current scan chain configuration");
1486
1487         register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1488                 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1489         register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1490                 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1491         register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1492                 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1493         register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1494                 COMMAND_EXEC, "move to current endstate or [tap_state]");
1495         register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1496                 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1497         register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1498                 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1499
1500         register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1501                 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1502         return ERROR_OK;
1503 }
1504
1505 int jtag_interface_init(struct command_context_s *cmd_ctx)
1506 {
1507         if (!jtag_interface)
1508         {
1509                 /* nothing was previously specified by "interface" command */
1510                 ERROR("JTAG interface has to be specified, see \"interface\" command");
1511                 return ERROR_JTAG_INVALID_INTERFACE;
1512         }
1513
1514         if (jtag_interface->init() != ERROR_OK)
1515                 return ERROR_JTAG_INIT_FAILED;
1516
1517         jtag = jtag_interface;
1518         return ERROR_OK;
1519 }
1520
1521 int jtag_init(struct command_context_s *cmd_ctx)
1522 {
1523         int validate_tries = 0;
1524         jtag_device_t *device;
1525
1526         DEBUG("-");
1527         
1528         if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1529                 return ERROR_JTAG_INIT_FAILED;
1530
1531         device = jtag_devices;
1532         jtag_ir_scan_size = 0;
1533         jtag_num_devices = 0;
1534         while (device != NULL)
1535         {
1536                 jtag_ir_scan_size += device->ir_length;
1537                 jtag_num_devices++;
1538                 device = device->next;
1539         }
1540         
1541         jtag_add_statemove(TAP_TLR);
1542         jtag_execute_queue();
1543
1544         /* examine chain first, as this could discover the real chain layout */
1545         if (jtag_examine_chain() != ERROR_OK)
1546         {
1547                 ERROR("trying to validate configured JTAG chain anyway...");
1548         }
1549         
1550         while (jtag_validate_chain() != ERROR_OK)
1551         {
1552                 validate_tries++;
1553                 if (validate_tries > 5)
1554                 {
1555                         ERROR("Could not validate JTAG chain, exit");
1556                         return ERROR_JTAG_INVALID_INTERFACE;
1557                 }
1558                 usleep(10000);
1559         }
1560
1561         return ERROR_OK;
1562 }
1563
1564
1565 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1566 {
1567         int i;
1568
1569         /* check whether the interface is already configured */
1570         if (jtag_interface)
1571         {
1572                 WARNING("Interface already configured, ignoring");
1573                 return ERROR_OK;
1574         }
1575
1576         /* interface name is a mandatory argument */
1577         if (argc < 1 || args[0][0] == '\0')
1578         {
1579                 return ERROR_COMMAND_SYNTAX_ERROR;
1580         }
1581
1582         for (i=0; jtag_interfaces[i]; i++)
1583         {
1584                 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1585                 {
1586                         if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1587                                 exit(-1);
1588
1589                         jtag_interface = jtag_interfaces[i];
1590                         return ERROR_OK;
1591                 }
1592         }
1593
1594         /* no valid interface was found (i.e. the configuration option,
1595          * didn't match one of the compiled-in interfaces
1596          */
1597         ERROR("No valid jtag interface found (%s)", args[0]);
1598         ERROR("compiled-in jtag interfaces:");
1599         for (i = 0; jtag_interfaces[i]; i++)
1600         {
1601                 ERROR("%i: %s", i, jtag_interfaces[i]->name);
1602         }
1603
1604         return ERROR_JTAG_INVALID_INTERFACE;
1605 }
1606
1607 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1608 {
1609         jtag_device_t **last_device_p = &jtag_devices;
1610
1611         if (*last_device_p)
1612         {
1613                 while ((*last_device_p)->next)
1614                         last_device_p = &((*last_device_p)->next);
1615                 last_device_p = &((*last_device_p)->next);
1616         }
1617
1618         if (argc < 3)
1619                 return ERROR_OK;
1620
1621         *last_device_p = malloc(sizeof(jtag_device_t));
1622         (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1623
1624         (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1625         buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1626         (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1627         buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1628
1629         (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1630         (*last_device_p)->bypass = 1;
1631         buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1632         
1633         (*last_device_p)->next = NULL;
1634         
1635         jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1636         
1637         jtag_num_devices++;
1638         
1639         return ERROR_OK;
1640 }
1641
1642 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1643 {
1644         jtag_device_t *device = jtag_devices;
1645         int device_count = 0;
1646         
1647         while (device)
1648         {
1649                 u32 expected, expected_mask, cur_instr;
1650                 expected = buf_get_u32(device->expected, 0, device->ir_length);
1651                 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1652                 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1653                 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);
1654                 device = device->next;
1655                 device_count++;
1656         }
1657
1658         return ERROR_OK;
1659 }
1660
1661 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1662 {
1663         if (argc >= 1)
1664         {
1665                 if (strcmp(args[0], "none") == 0)
1666                         jtag_reset_config = RESET_NONE;
1667                 else if (strcmp(args[0], "trst_only") == 0)
1668                         jtag_reset_config = RESET_HAS_TRST;
1669                 else if (strcmp(args[0], "srst_only") == 0)
1670                         jtag_reset_config = RESET_HAS_SRST;
1671                 else if (strcmp(args[0], "trst_and_srst") == 0)
1672                         jtag_reset_config = RESET_TRST_AND_SRST;
1673                 else
1674                 {
1675                         ERROR("invalid reset_config argument, defaulting to none");
1676                         jtag_reset_config = RESET_NONE;
1677                         return ERROR_INVALID_ARGUMENTS;
1678                 }
1679         }
1680         
1681         if (argc >= 2)
1682         {
1683                 if (strcmp(args[1], "srst_pulls_trst") == 0)
1684                         jtag_reset_config |= RESET_SRST_PULLS_TRST;
1685                 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1686                         jtag_reset_config |= RESET_TRST_PULLS_SRST;
1687                 else if (strcmp(args[1], "combined") == 0)
1688                         jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1689                 else if (strcmp(args[1], "separate") == 0)
1690                         jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
1691                 else
1692                 {
1693                         ERROR("invalid reset_config argument, defaulting to none");
1694                         jtag_reset_config = RESET_NONE;
1695                         return ERROR_INVALID_ARGUMENTS;
1696                 }
1697         }
1698         
1699         if (argc >= 3)
1700         {
1701                 if (strcmp(args[2], "trst_open_drain") == 0)
1702                         jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1703                 else if (strcmp(args[2], "trst_push_pull") == 0)
1704                         jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1705                 else
1706                 {
1707                         ERROR("invalid reset_config argument, defaulting to none");
1708                         jtag_reset_config = RESET_NONE;
1709                         return ERROR_INVALID_ARGUMENTS;
1710                 }
1711         }
1712
1713         if (argc >= 4)
1714         {
1715                 if (strcmp(args[3], "srst_push_pull") == 0)
1716                         jtag_reset_config |= RESET_SRST_PUSH_PULL;
1717                 else if (strcmp(args[3], "srst_open_drain") == 0)
1718                         jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1719                 else
1720                 {
1721                         ERROR("invalid reset_config argument, defaulting to none");
1722                         jtag_reset_config = RESET_NONE;
1723                         return ERROR_INVALID_ARGUMENTS;
1724                 }
1725         }
1726         
1727         return ERROR_OK;
1728 }
1729
1730 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1731 {
1732         if (argc < 1)
1733         {
1734                 ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1735                 exit(-1);
1736         }
1737         else
1738         {
1739                 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1740         }
1741         
1742         return ERROR_OK;
1743 }
1744
1745 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1746 {
1747         if (argc < 1)
1748         {
1749                 ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1750                 exit(-1);
1751         }
1752         else
1753         {
1754                 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1755         }
1756         
1757         return ERROR_OK;
1758 }
1759
1760 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1761 {
1762         if (argc == 0)
1763                 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
1764
1765         if (argc > 0)
1766         {
1767                 jtag_speed = strtoul(args[0], NULL, 0);
1768                 /* this command can be called during CONFIG, 
1769                  * in which case jtag isn't initialized */
1770                 if (jtag)
1771                         jtag->speed(jtag_speed);
1772         }
1773
1774         return ERROR_OK;
1775 }
1776
1777 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1778 {
1779         enum tap_state state;
1780
1781         if (argc < 1)
1782         {
1783                 return ERROR_COMMAND_SYNTAX_ERROR;
1784         }
1785         else
1786         {
1787                 for (state = 0; state < 16; state++)
1788                 {
1789                         if (strcmp(args[0], tap_state_strings[state]) == 0)
1790                         {
1791                                 jtag_add_end_state(state);
1792                                 jtag_execute_queue();
1793                         }
1794                 }
1795         }
1796         command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
1797         
1798         return ERROR_OK;
1799 }
1800
1801 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1802 {
1803         int trst = -1;
1804         int srst = -1;
1805         
1806         if (argc < 2)
1807         {
1808                 return ERROR_COMMAND_SYNTAX_ERROR;
1809         }
1810
1811         if (args[0][0] == '1')
1812                 trst = 1;
1813         else if (args[0][0] == '0')
1814                 trst = 0;
1815         else
1816         {
1817                 return ERROR_COMMAND_SYNTAX_ERROR;
1818         }
1819
1820         if (args[1][0] == '1')
1821                 srst = 1;
1822         else if (args[1][0] == '0')
1823                 srst = 0;
1824         else
1825         {
1826                 return ERROR_COMMAND_SYNTAX_ERROR;
1827         }
1828
1829         if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1830                 return ERROR_JTAG_INIT_FAILED;
1831
1832         jtag_add_reset(trst, srst);
1833         jtag_execute_queue();
1834
1835         return ERROR_OK;
1836 }
1837
1838 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1839 {
1840         if (argc < 1)
1841         {
1842                 return ERROR_COMMAND_SYNTAX_ERROR;
1843         }
1844
1845         jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1846         jtag_execute_queue();
1847
1848         return ERROR_OK;
1849
1850 }
1851
1852 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1853 {
1854         enum tap_state state;
1855
1856         state = -1;
1857         if (argc == 1)
1858         {
1859                 for (state = 0; state < 16; state++)
1860                 {
1861                         if (strcmp(args[0], tap_state_strings[state]) == 0)
1862                         {
1863                                 break;
1864                         }
1865                 }
1866         }
1867
1868         jtag_add_statemove(state);
1869         jtag_execute_queue();
1870
1871         return ERROR_OK;
1872
1873 }
1874
1875 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1876 {
1877         int i;
1878         scan_field_t *fields;
1879         
1880         if ((argc < 2) || (argc % 2))
1881         {
1882                 return ERROR_COMMAND_SYNTAX_ERROR;
1883         }
1884
1885         fields = malloc(sizeof(scan_field_t) * argc / 2);
1886         
1887         for (i = 0; i < argc / 2; i++)
1888         {
1889                 int device = strtoul(args[i*2], NULL, 0);
1890                 int field_size = jtag_get_device(device)->ir_length;
1891                 fields[i].device = device;
1892                 fields[i].out_value = malloc(CEIL(field_size, 8));
1893                 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1894                 fields[i].out_mask = NULL;
1895                 fields[i].in_value = NULL;
1896                 fields[i].in_check_mask = NULL;
1897                 fields[i].in_handler = NULL;
1898                 fields[i].in_handler_priv = NULL;
1899         }
1900
1901         jtag_add_ir_scan(argc / 2, fields, -1);
1902         jtag_execute_queue();
1903
1904         for (i = 0; i < argc / 2; i++)
1905                 free(fields[i].out_value);
1906
1907         free (fields);
1908
1909         return ERROR_OK;
1910 }
1911
1912 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1913 {
1914         scan_field_t *fields;
1915         int num_fields = 0;
1916         int field_count = 0;
1917         var_t *var;
1918         int i, j;
1919         
1920         if ((argc < 2) || (argc % 2))
1921         {
1922                 return ERROR_COMMAND_SYNTAX_ERROR;
1923         }
1924
1925         for (i = 0; i < argc; i+=2)
1926         {
1927                 var = get_var_by_namenum(args[i+1]);
1928                 if (var)
1929                 {
1930                         num_fields += var->num_fields;
1931                 }
1932                 else
1933                 {
1934                         command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1935                         return ERROR_OK;
1936                 }
1937         }
1938
1939         fields = malloc(sizeof(scan_field_t) * num_fields);
1940
1941         for (i = 0; i < argc; i+=2)
1942         {
1943                 var = get_var_by_namenum(args[i+1]);
1944         
1945                 for (j = 0; j < var->num_fields; j++)
1946                 {
1947                         fields[field_count].device = strtol(args[i], NULL, 0);
1948                         fields[field_count].num_bits = var->fields[j].num_bits;
1949                         fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1950                         buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1951                         fields[field_count].out_mask = NULL;
1952                         fields[field_count].in_value = fields[field_count].out_value;
1953                         fields[field_count].in_check_mask = NULL;
1954                         fields[field_count].in_check_value = NULL;
1955                         fields[field_count].in_handler = field_le_to_host;
1956                         fields[field_count++].in_handler_priv = &(var->fields[j]);
1957                 }
1958         }
1959
1960         jtag_add_dr_scan(num_fields, fields, -1);
1961         jtag_execute_queue();
1962         
1963         for (i = 0; i < argc / 2; i++)
1964                 free(fields[i].out_value);
1965
1966         free(fields);
1967
1968         return ERROR_OK;
1969 }
1970
1971 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1972 {
1973         if (argc == 1)
1974         {
1975                 if (strcmp(args[0], "enable") == 0)
1976                 {
1977                         jtag_verify_capture_ir = 1;
1978                 }
1979                 else if (strcmp(args[0], "disable") == 0)
1980                 {
1981                         jtag_verify_capture_ir = 0;
1982                 } else
1983                 {
1984                         return ERROR_COMMAND_SYNTAX_ERROR;
1985                 }
1986         } else if (argc != 0)
1987         {
1988                 return ERROR_COMMAND_SYNTAX_ERROR;
1989         }
1990         
1991         command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
1992         
1993         return ERROR_OK;
1994 }