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