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