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