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