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