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