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