back out Don Porges workaround for some device with manufcaturer # 0x1a2
[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 /* Try to examine chain layout according to IEEE 1149.1 Â§12
1292  */
1293 int jtag_examine_chain()
1294 {
1295         jtag_device_t *device = jtag_devices;
1296         scan_field_t field;
1297         u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1298         int i;
1299         int bit_count;
1300         int device_count = 0;
1301         u8 zero_check = 0x0;
1302         u8 one_check = 0xff;
1303         
1304         field.device = 0;
1305         field.num_bits = sizeof(idcode_buffer) * 8;
1306         field.out_value = idcode_buffer;
1307         field.out_mask = NULL;
1308         field.in_value = idcode_buffer;
1309         field.in_check_value = NULL;
1310         field.in_check_mask = NULL;
1311         field.in_handler = NULL;
1312         field.in_handler_priv = NULL;
1313         
1314         for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1315         {
1316                 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1317         }
1318         
1319         jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1320         jtag_execute_queue();
1321         
1322         for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1323         {
1324                 zero_check |= idcode_buffer[i];
1325                 one_check &= idcode_buffer[i];
1326         }
1327         
1328         /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1329         if ((zero_check == 0x00) || (one_check == 0xff))
1330         {
1331                 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1332                 return ERROR_JTAG_INIT_FAILED;
1333         }
1334         
1335         for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1336         {
1337                 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1338                 if ((idcode & 1) == 0)
1339                 {
1340                         /* LSB must not be 0, this indicates a device in bypass */
1341                         device_count++;
1342                         
1343                         bit_count += 1;
1344                 }
1345                 else
1346                 {
1347                         u32 manufacturer;
1348                         u32 part;
1349                         u32 version;
1350                         
1351                         if (idcode == 0x000000FF)
1352                         {
1353                                 /* End of chain (invalid manufacturer ID) */
1354                                 break;
1355                         }
1356                         
1357                         if (device)
1358                         {
1359                                 device->idcode = idcode;
1360                                 device = device->next;
1361                         }
1362                         device_count++;
1363                         
1364                         manufacturer = (idcode & 0xffe) >> 1;
1365                         part = (idcode & 0xffff000) >> 12;
1366                         version = (idcode & 0xf0000000) >> 28;
1367
1368                         LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)", 
1369                                 idcode, manufacturer, part, version);
1370                         
1371                         bit_count += 32;
1372                 }
1373         }
1374         
1375         /* see if number of discovered devices matches configuration */
1376         if (device_count != jtag_num_devices)
1377         {
1378                 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)", 
1379                                 device_count, jtag_num_devices);
1380                 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1381                 return ERROR_JTAG_INIT_FAILED;
1382         }
1383         
1384         return ERROR_OK;
1385 }
1386
1387 int jtag_validate_chain()
1388 {
1389         jtag_device_t *device = jtag_devices;
1390         int total_ir_length = 0;
1391         u8 *ir_test = NULL;
1392         scan_field_t field;
1393         int chain_pos = 0;
1394         
1395         while (device)
1396         {
1397                 total_ir_length += device->ir_length;
1398                 device = device->next;
1399         }
1400         
1401         total_ir_length += 2;
1402         ir_test = malloc(CEIL(total_ir_length, 8));
1403         buf_set_ones(ir_test, total_ir_length);
1404         
1405         field.device = 0;
1406         field.num_bits = total_ir_length;
1407         field.out_value = ir_test;
1408         field.out_mask = NULL;
1409         field.in_value = ir_test;
1410         field.in_check_value = NULL;
1411         field.in_check_mask = NULL;
1412         field.in_handler = NULL;
1413         field.in_handler_priv = NULL;
1414         
1415         jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1416         jtag_execute_queue();
1417         
1418         device = jtag_devices;
1419         while (device)
1420         {
1421                 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1422                 {
1423                         char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1424                         LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1425                         free(cbuf);
1426                         free(ir_test);
1427                         return ERROR_JTAG_INIT_FAILED;
1428                 }
1429                 chain_pos += device->ir_length;
1430                 device = device->next;
1431         }
1432         
1433         if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1434         {
1435                 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1436                 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1437                 free(cbuf);
1438                 free(ir_test);
1439                 return ERROR_JTAG_INIT_FAILED;
1440         }
1441         
1442         free(ir_test);
1443         
1444         return ERROR_OK;
1445 }
1446
1447 int jtag_register_commands(struct command_context_s *cmd_ctx)
1448 {
1449         register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1450                 COMMAND_CONFIG, NULL);
1451         register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1452                 COMMAND_ANY, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1453         register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1454                 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1455         register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1456                 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1457         register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1458                 COMMAND_CONFIG, NULL);
1459         register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1460                 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1461         register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1462                 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1463                 
1464         register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1465                 COMMAND_EXEC, "print current scan chain configuration");
1466
1467         register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1468                 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1469         register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1470                 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1471         register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1472                 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1473         register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1474                 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1475         register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1476                 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1477
1478         register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1479                 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1480         return ERROR_OK;
1481 }
1482
1483 int jtag_interface_init(struct command_context_s *cmd_ctx)
1484 {
1485         if (!jtag_interface)
1486         {
1487                 /* nothing was previously specified by "interface" command */
1488                 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1489                 return ERROR_JTAG_INVALID_INTERFACE;
1490         }
1491         if(hasKHz)
1492         {
1493                 /*stay on "reset speed"*/
1494                 if (jtag_interface->khz(speed1, &speed1) == ERROR_OK)
1495                         jtag_speed = speed1;
1496                 if (jtag_interface->khz(speed2, &speed2) == ERROR_OK)
1497                         jtag_speed_post_reset = speed2;
1498                 hasKHz = 0;
1499         }
1500
1501         if (jtag_interface->init() != ERROR_OK)
1502                 return ERROR_JTAG_INIT_FAILED;
1503
1504         
1505         
1506         jtag = jtag_interface;
1507         return ERROR_OK;
1508 }
1509
1510 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1511 {
1512         int validate_tries = 0;
1513         jtag_device_t *device;
1514
1515         LOG_DEBUG("-");
1516         
1517         if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1518                 return ERROR_JTAG_INIT_FAILED;
1519
1520         device = jtag_devices;
1521         jtag_ir_scan_size = 0;
1522         jtag_num_devices = 0;
1523         while (device != NULL)
1524         {
1525                 jtag_ir_scan_size += device->ir_length;
1526                 jtag_num_devices++;
1527                 device = device->next;
1528         }
1529         
1530         jtag_add_tlr();
1531         jtag_execute_queue();
1532
1533         /* examine chain first, as this could discover the real chain layout */
1534         if (jtag_examine_chain() != ERROR_OK)
1535         {
1536                 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1537         }
1538         
1539         while (jtag_validate_chain() != ERROR_OK)
1540         {
1541                 validate_tries++;
1542                 
1543                 if (validate_tries > 5)
1544                 {
1545                         LOG_ERROR("Could not validate JTAG chain, exit");
1546                         return ERROR_JTAG_INVALID_INTERFACE;
1547                 }
1548                 usleep(10000);
1549         }
1550         
1551         return ERROR_OK;
1552 }
1553
1554 int jtag_init_reset(struct command_context_s *cmd_ctx)
1555 {
1556         int retval;
1557         LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / tms");
1558
1559         /* Reset can happen after a power cycle.
1560          * 
1561          * Ideally we would only assert TRST or run tms before the target reset.
1562          * 
1563          * However w/srst_pulls_trst, trst is asserted together with the target
1564          * reset whether we want it or not.
1565          * 
1566          * NB! Some targets have JTAG circuitry disabled until a 
1567          * trst & srst has been asserted.
1568          * 
1569          * NB! here we assume nsrst/ntrst delay are sufficient!
1570          * 
1571          * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1572          * 
1573          */
1574         jtag_add_reset(1, 0); /* TMS or TRST */
1575         if (jtag_reset_config & RESET_HAS_SRST)
1576         {
1577                 jtag_add_reset(1, 1);
1578                 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
1579                         jtag_add_reset(0, 1);
1580         }
1581         jtag_add_reset(0, 0);
1582         if ((retval = jtag_execute_queue()) != ERROR_OK)
1583                 return retval;
1584         
1585         /* Check that we can communication on the JTAG chain + eventually we want to
1586          * be able to perform enumeration only after OpenOCD has started 
1587          * telnet and GDB server
1588          * 
1589          * That would allow users to more easily perform any magic they need to before
1590          * reset happens.
1591          */
1592         return jtag_init_inner(cmd_ctx);
1593 }
1594
1595 int jtag_init(struct command_context_s *cmd_ctx)
1596 {
1597         if (jtag_init_inner(cmd_ctx)==ERROR_OK)
1598         {
1599                 return ERROR_OK;
1600         }
1601         return jtag_init_reset(cmd_ctx);
1602 }
1603
1604
1605 static int default_khz(int khz, int *jtag_speed)
1606 {
1607         LOG_ERROR("Translation from khz to jtag_speed not implemented");
1608         return ERROR_FAIL;
1609 }
1610
1611 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1612 {
1613         int i;
1614
1615         /* check whether the interface is already configured */
1616         if (jtag_interface)
1617         {
1618                 LOG_WARNING("Interface already configured, ignoring");
1619                 return ERROR_OK;
1620         }
1621
1622         /* interface name is a mandatory argument */
1623         if (argc < 1 || args[0][0] == '\0')
1624         {
1625                 return ERROR_COMMAND_SYNTAX_ERROR;
1626         }
1627
1628         for (i=0; jtag_interfaces[i]; i++)
1629         {
1630                 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1631                 {
1632                         if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1633                                 exit(-1);
1634
1635                         jtag_interface = jtag_interfaces[i];
1636                         
1637                         if (jtag_interface->khz == NULL)
1638                         {
1639                                 jtag_interface->khz = default_khz;
1640                         }
1641                         return ERROR_OK;
1642                 }
1643         }
1644
1645         /* no valid interface was found (i.e. the configuration option,
1646          * didn't match one of the compiled-in interfaces
1647          */
1648         LOG_ERROR("No valid jtag interface found (%s)", args[0]);
1649         LOG_ERROR("compiled-in jtag interfaces:");
1650         for (i = 0; jtag_interfaces[i]; i++)
1651         {
1652                 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
1653         }
1654
1655         return ERROR_JTAG_INVALID_INTERFACE;
1656 }
1657
1658 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1659 {
1660         jtag_device_t **last_device_p = &jtag_devices;
1661
1662         if (*last_device_p)
1663         {
1664                 while ((*last_device_p)->next)
1665                         last_device_p = &((*last_device_p)->next);
1666                 last_device_p = &((*last_device_p)->next);
1667         }
1668
1669         if (argc < 3)
1670                 return ERROR_OK;
1671
1672         *last_device_p = malloc(sizeof(jtag_device_t));
1673         (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1674
1675         (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1676         buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1677         (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1678         buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1679
1680         (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1681         (*last_device_p)->bypass = 1;
1682         buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1683         
1684         (*last_device_p)->next = NULL;
1685         
1686         jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1687         
1688         jtag_num_devices++;
1689         
1690         return ERROR_OK;
1691 }
1692
1693 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1694 {
1695         jtag_device_t *device = jtag_devices;
1696         int device_count = 0;
1697         
1698         while (device)
1699         {
1700                 u32 expected, expected_mask, cur_instr;
1701                 expected = buf_get_u32(device->expected, 0, device->ir_length);
1702                 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1703                 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1704                 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);
1705                 device = device->next;
1706                 device_count++;
1707         }
1708
1709         return ERROR_OK;
1710 }
1711
1712 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1713 {
1714         if (argc < 1)
1715                 return ERROR_COMMAND_SYNTAX_ERROR;
1716         
1717         if (argc >= 1)
1718         {
1719                 if (strcmp(args[0], "none") == 0)
1720                         jtag_reset_config = RESET_NONE;
1721                 else if (strcmp(args[0], "trst_only") == 0)
1722                         jtag_reset_config = RESET_HAS_TRST;
1723                 else if (strcmp(args[0], "srst_only") == 0)
1724                         jtag_reset_config = RESET_HAS_SRST;
1725                 else if (strcmp(args[0], "trst_and_srst") == 0)
1726                         jtag_reset_config = RESET_TRST_AND_SRST;
1727                 else
1728                 {
1729                         LOG_ERROR("invalid reset_config argument, defaulting to none");
1730                         jtag_reset_config = RESET_NONE;
1731                         return ERROR_INVALID_ARGUMENTS;
1732                 }
1733         }
1734         
1735         if (argc >= 2)
1736         {
1737                 if (strcmp(args[1], "separate") == 0)
1738                 {
1739                         /* seperate reset lines - default */
1740                 } else
1741                 {
1742                         if (strcmp(args[1], "srst_pulls_trst") == 0)
1743                                 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1744                         else if (strcmp(args[1], "trst_pulls_srst") == 0)
1745                                 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1746                         else if (strcmp(args[1], "combined") == 0)
1747                                 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1748                         else
1749                         {
1750                                 LOG_ERROR("invalid reset_config argument, defaulting to none");
1751                                 jtag_reset_config = RESET_NONE;
1752                                 return ERROR_INVALID_ARGUMENTS;
1753                         }
1754                 }
1755         }
1756         
1757         if (argc >= 3)
1758         {
1759                 if (strcmp(args[2], "trst_open_drain") == 0)
1760                         jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1761                 else if (strcmp(args[2], "trst_push_pull") == 0)
1762                         jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1763                 else
1764                 {
1765                         LOG_ERROR("invalid reset_config argument, defaulting to none");
1766                         jtag_reset_config = RESET_NONE;
1767                         return ERROR_INVALID_ARGUMENTS;
1768                 }
1769         }
1770
1771         if (argc >= 4)
1772         {
1773                 if (strcmp(args[3], "srst_push_pull") == 0)
1774                         jtag_reset_config |= RESET_SRST_PUSH_PULL;
1775                 else if (strcmp(args[3], "srst_open_drain") == 0)
1776                         jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1777                 else
1778                 {
1779                         LOG_ERROR("invalid reset_config argument, defaulting to none");
1780                         jtag_reset_config = RESET_NONE;
1781                         return ERROR_INVALID_ARGUMENTS;
1782                 }
1783         }
1784         
1785         return ERROR_OK;
1786 }
1787
1788 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1789 {
1790         if (argc < 1)
1791         {
1792                 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1793                 exit(-1);
1794         }
1795         else
1796         {
1797                 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1798         }
1799         
1800         return ERROR_OK;
1801 }
1802
1803 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1804 {
1805         if (argc < 1)
1806         {
1807                 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1808                 exit(-1);
1809         }
1810         else
1811         {
1812                 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1813         }
1814         
1815         return ERROR_OK;
1816 }
1817
1818 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1819 {
1820         int cur_speed = 0;
1821         
1822         if (argc != 0)
1823         {
1824                 if ((argc<1) || (argc>2))
1825                         return ERROR_COMMAND_SYNTAX_ERROR;
1826                 
1827                 LOG_DEBUG("handle jtag speed");
1828                 
1829                 if (argc >= 1)
1830                         cur_speed = jtag_speed = jtag_speed_post_reset = strtoul(args[0], NULL, 0);
1831                 if (argc == 2)
1832                         cur_speed = jtag_speed_post_reset = strtoul(args[1], NULL, 0);
1833                         
1834                 /* this command can be called during CONFIG, 
1835                  * in which case jtag isn't initialized */
1836                 if (jtag)
1837                         jtag->speed(cur_speed);
1838         }               
1839         command_print(cmd_ctx, "jtag_speed: %d, %d", jtag_speed, jtag_speed_post_reset);
1840         
1841         return ERROR_OK;
1842 }
1843
1844 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1845 {
1846         LOG_DEBUG("handle jtag khz");
1847         
1848         if ((argc<1) || (argc>2))
1849                 return ERROR_COMMAND_SYNTAX_ERROR;
1850
1851         if (argc >= 1)
1852                 speed1 = speed2 = strtoul(args[0], NULL, 0);
1853         if (argc == 2)
1854                 speed2 = strtoul(args[1], NULL, 0);
1855
1856         if (jtag != NULL)
1857         {
1858                 int cur_speed = 0;
1859                 LOG_DEBUG("have interface set up");
1860                 int speed_div1, speed_div2;
1861                 if (jtag->khz(speed1, &speed_div1)!=ERROR_OK)
1862                         return ERROR_OK;
1863                 if (jtag->khz(speed2, &speed_div2)!=ERROR_OK)
1864                         return ERROR_OK;
1865
1866                 if (argc >= 1)
1867                         cur_speed = jtag_speed = jtag_speed_post_reset = speed_div1;
1868                 if (argc == 2)
1869                         cur_speed = jtag_speed_post_reset = speed_div2;
1870
1871                 jtag->speed(cur_speed);
1872         } else
1873         {
1874                 hasKHz = 1;
1875         }
1876         
1877         return ERROR_OK;
1878 }
1879
1880
1881 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1882 {
1883         enum tap_state state;
1884
1885         if (argc < 1)
1886         {
1887                 return ERROR_COMMAND_SYNTAX_ERROR;
1888         }
1889         else
1890         {
1891                 for (state = 0; state < 16; state++)
1892                 {
1893                         if (strcmp(args[0], tap_state_strings[state]) == 0)
1894                         {
1895                                 jtag_add_end_state(state);
1896                                 jtag_execute_queue();
1897                         }
1898                 }
1899         }
1900         command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
1901         
1902         return ERROR_OK;
1903 }
1904
1905 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1906 {
1907         int trst = -1;
1908         int srst = -1;
1909         
1910         if (argc < 2)
1911         {
1912                 return ERROR_COMMAND_SYNTAX_ERROR;
1913         }
1914
1915         if (args[0][0] == '1')
1916                 trst = 1;
1917         else if (args[0][0] == '0')
1918                 trst = 0;
1919         else
1920         {
1921                 return ERROR_COMMAND_SYNTAX_ERROR;
1922         }
1923
1924         if (args[1][0] == '1')
1925                 srst = 1;
1926         else if (args[1][0] == '0')
1927                 srst = 0;
1928         else
1929         {
1930                 return ERROR_COMMAND_SYNTAX_ERROR;
1931         }
1932
1933         if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1934                 return ERROR_JTAG_INIT_FAILED;
1935
1936         jtag_add_reset(trst, srst);
1937         jtag_execute_queue();
1938
1939         return ERROR_OK;
1940 }
1941
1942 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1943 {
1944         if (argc < 1)
1945         {
1946                 return ERROR_COMMAND_SYNTAX_ERROR;
1947         }
1948
1949         jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1950         jtag_execute_queue();
1951
1952         return ERROR_OK;
1953
1954 }
1955
1956
1957 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1958 {
1959         int i;
1960         scan_field_t *fields;
1961         
1962         if ((argc < 2) || (argc % 2))
1963         {
1964                 return ERROR_COMMAND_SYNTAX_ERROR;
1965         }
1966
1967         fields = malloc(sizeof(scan_field_t) * argc / 2);
1968         
1969         for (i = 0; i < argc / 2; i++)
1970         {
1971                 int device = strtoul(args[i*2], NULL, 0);
1972                 int field_size = jtag_get_device(device)->ir_length;
1973                 fields[i].device = device;
1974                 fields[i].out_value = malloc(CEIL(field_size, 8));
1975                 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1976                 fields[i].out_mask = NULL;
1977                 fields[i].in_value = NULL;
1978                 fields[i].in_check_mask = NULL;
1979                 fields[i].in_handler = NULL;
1980                 fields[i].in_handler_priv = NULL;
1981         }
1982
1983         jtag_add_ir_scan(argc / 2, fields, -1);
1984         jtag_execute_queue();
1985
1986         for (i = 0; i < argc / 2; i++)
1987                 free(fields[i].out_value);
1988
1989         free (fields);
1990
1991         return ERROR_OK;
1992 }
1993
1994 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1995 {
1996         scan_field_t *fields;
1997         int num_fields = 0;
1998         int field_count = 0;
1999         var_t *var;
2000         int i, j;
2001         
2002         if ((argc < 2) || (argc % 2))
2003         {
2004                 return ERROR_COMMAND_SYNTAX_ERROR;
2005         }
2006
2007         for (i = 0; i < argc; i+=2)
2008         {
2009                 var = get_var_by_namenum(args[i+1]);
2010                 if (var)
2011                 {
2012                         num_fields += var->num_fields;
2013                 }
2014                 else
2015                 {
2016                         command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
2017                         return ERROR_OK;
2018                 }
2019         }
2020
2021         fields = malloc(sizeof(scan_field_t) * num_fields);
2022
2023         for (i = 0; i < argc; i+=2)
2024         {
2025                 var = get_var_by_namenum(args[i+1]);
2026         
2027                 for (j = 0; j < var->num_fields; j++)
2028                 {
2029                         fields[field_count].device = strtol(args[i], NULL, 0);
2030                         fields[field_count].num_bits = var->fields[j].num_bits;
2031                         fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
2032                         buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
2033                         fields[field_count].out_mask = NULL;
2034                         fields[field_count].in_value = fields[field_count].out_value;
2035                         fields[field_count].in_check_mask = NULL;
2036                         fields[field_count].in_check_value = NULL;
2037                         fields[field_count].in_handler = field_le_to_host;
2038                         fields[field_count++].in_handler_priv = &(var->fields[j]);
2039                 }
2040         }
2041
2042         jtag_add_dr_scan(num_fields, fields, -1);
2043         jtag_execute_queue();
2044         
2045         for (i = 0; i < argc / 2; i++)
2046                 free(fields[i].out_value);
2047
2048         free(fields);
2049
2050         return ERROR_OK;
2051 }
2052
2053 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2054 {
2055         if (argc == 1)
2056         {
2057                 if (strcmp(args[0], "enable") == 0)
2058                 {
2059                         jtag_verify_capture_ir = 1;
2060                 }
2061                 else if (strcmp(args[0], "disable") == 0)
2062                 {
2063                         jtag_verify_capture_ir = 0;
2064                 } else
2065                 {
2066                         return ERROR_COMMAND_SYNTAX_ERROR;
2067                 }
2068         } else if (argc != 0)
2069         {
2070                 return ERROR_COMMAND_SYNTAX_ERROR;
2071         }
2072         
2073         command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
2074         
2075         return ERROR_OK;
2076 }