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