- prepare OpenOCD for branching, created ./trunk/
[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 #include "config.h"
21 #include "jtag.h"
22
23 #include "command.h"
24 #include "log.h"
25 #include "interpreter.h"
26
27 #include "stdlib.h"
28 #include "string.h"
29 #include <unistd.h>
30
31 char* tap_state_strings[16] =
32 {
33         "tlr", 
34         "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
35         "rti",
36         "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
37 };
38
39 typedef struct cmd_queue_page_s
40 {
41         void *address;
42         size_t used;
43         struct cmd_queue_page_s *next;
44 } cmd_queue_page_t;
45
46 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
47 static cmd_queue_page_t *cmd_queue_pages = NULL;
48
49 /* tap_move[i][j]: tap movement command to go from state i to state j
50  * 0: Test-Logic-Reset
51  * 1: Run-Test/Idle
52  * 2: Shift-DR
53  * 3: Pause-DR
54  * 4: Shift-IR
55  * 5: Pause-IR
56  */
57 u8 tap_move[6][6] =
58 {
59 /*        TLR   RTI   SD    PD    SI    PI             */
60         {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16},   /* TLR */
61         {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b},   /* RTI */
62         {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f},   /* SD  */
63         {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f},   /* PD  */
64         {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01},   /* SI  */
65         {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f}    /* PI  */
66 };
67
68 int tap_move_map[16] = {
69         0, -1, -1,  2, -1,  3, -1, -1,
70         1, -1, -1,  4, -1,  5, -1, -1
71 };
72
73 tap_transition_t tap_transitions[16] =
74 {
75         {TAP_TLR, TAP_RTI},             /* TLR */
76         {TAP_SIS, TAP_CD},              /* SDS */
77         {TAP_E1D, TAP_SD},              /* CD  */
78         {TAP_E1D, TAP_SD},              /* SD  */
79         {TAP_UD,  TAP_PD},              /* E1D */
80         {TAP_E2D, TAP_PD},              /* PD  */
81         {TAP_UD,  TAP_SD},              /* E2D */
82         {TAP_SDS, TAP_RTI},             /* UD  */
83         {TAP_SDS, TAP_RTI},             /* RTI */
84         {TAP_TLR, TAP_CI},              /* SIS */
85         {TAP_E1I, TAP_SI},              /* CI  */
86         {TAP_E1I, TAP_SI},              /* SI  */
87         {TAP_UI,  TAP_PI},              /* E1I */
88         {TAP_E2I, TAP_PI},              /* PI  */
89         {TAP_UI,  TAP_SI},              /* E2I */
90         {TAP_SDS, TAP_RTI}              /* UI  */
91 };
92
93 enum tap_state end_state = TAP_TLR;
94 enum tap_state cur_state = TAP_TLR;
95 int jtag_trst = 0;
96 int jtag_srst = 0;
97
98 jtag_command_t *jtag_command_queue = NULL;
99 jtag_command_t **last_comand_pointer = &jtag_command_queue;
100 jtag_device_t *jtag_devices = NULL;
101 int jtag_num_devices = 0;
102 int jtag_ir_scan_size = 0;
103 enum reset_types jtag_reset_config = RESET_NONE;
104 enum tap_state cmd_queue_end_state = TAP_TLR;
105 enum tap_state cmd_queue_cur_state = TAP_TLR;
106
107 int jtag_verify_capture_ir = 1;
108
109 /* callbacks to inform high-level handlers about JTAG state changes */
110 jtag_event_callback_t *jtag_event_callbacks;
111
112 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
113  */
114 #if BUILD_PARPORT == 1
115         extern jtag_interface_t parport_interface;
116 #endif
117
118 #if BUILD_FTDI2232 == 1
119         extern jtag_interface_t ftdi2232_interface;
120 #endif
121
122 #if BUILD_FTD2XX == 1
123         extern jtag_interface_t ftd2xx_interface;
124 #endif
125
126 #if BUILD_AMTJTAGACCEL == 1
127         extern jtag_interface_t amt_jtagaccel_interface;
128 #endif
129
130 #if BUILD_EP93XX == 1
131         extern jtag_interface_t ep93xx_interface;
132 #endif
133
134 jtag_interface_t *jtag_interfaces[] = {
135 #if BUILD_PARPORT == 1
136         &parport_interface,
137 #endif
138 #if BUILD_FTDI2232 == 1
139         &ftdi2232_interface,
140 #endif
141 #if BUILD_FTD2XX == 1
142         &ftd2xx_interface,
143 #endif
144 #if BUILD_AMTJTAGACCEL == 1
145         &amt_jtagaccel_interface,
146 #endif
147 #if BUILD_EP93XX == 1
148         &ep93xx_interface,
149 #endif
150         NULL,
151 };
152
153 jtag_interface_t *jtag = NULL;
154
155 /* configuration */
156 char* jtag_interface = NULL;
157 int jtag_speed = -1;
158
159 /* forward declarations */
160
161 /* jtag commands */
162 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
163 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
164 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
165 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
166
167 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
168
169 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
170 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
171 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
172 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
173 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
174 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
175
176 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
177
178 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
179 {
180         jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
181         
182         if (callback == NULL)
183         {
184                 return ERROR_INVALID_ARGUMENTS;
185         }
186         
187         if (*callbacks_p)
188         {
189                 while ((*callbacks_p)->next)
190                         callbacks_p = &((*callbacks_p)->next);
191                 callbacks_p = &((*callbacks_p)->next);
192         }
193         
194         (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
195         (*callbacks_p)->callback = callback;
196         (*callbacks_p)->priv = priv;
197         (*callbacks_p)->next = NULL;
198         
199         return ERROR_OK;
200 }
201
202 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
203 {
204         jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
205         
206         if (callback == NULL)
207         {
208                 return ERROR_INVALID_ARGUMENTS;
209         }
210                 
211         while (*callbacks_p)
212         {
213                 jtag_event_callback_t **next = &((*callbacks_p)->next);
214                 if ((*callbacks_p)->callback == callback)
215                 {
216                         free(*callbacks_p);
217                         *callbacks_p = *next;
218                 }
219                 callbacks_p = next;
220         }
221         
222         return ERROR_OK;
223 }
224
225 int jtag_call_event_callbacks(enum jtag_event event)
226 {
227         jtag_event_callback_t *callback = jtag_event_callbacks;
228         
229         DEBUG("jtag event: %i", event);
230         
231         while (callback)
232         {
233                 callback->callback(event, callback->priv);
234                 callback = callback->next;
235         }
236         
237         return ERROR_OK;
238 }
239
240 /* returns a pointer to the pointer of the last command in queue
241  * this may be a pointer to the root pointer (jtag_command_queue)
242  * or to the next member of the last but one command
243  */
244 jtag_command_t** jtag_get_last_command_p(void)
245 {
246 /*      jtag_command_t *cmd = jtag_command_queue;
247         
248         if (cmd)
249                 while (cmd->next)
250                         cmd = cmd->next;
251         else
252                 return &jtag_command_queue;
253         
254         return &cmd->next;*/
255         
256         return last_comand_pointer;
257 }
258
259 /* returns a pointer to the n-th device in the scan chain */
260 jtag_device_t* jtag_get_device(int num)
261 {
262         jtag_device_t *device = jtag_devices;
263         int i = 0;
264
265         while (device)
266         {
267                 if (num == i)
268                         return device;
269                 device = device->next;
270                 i++;
271         }
272
273         return NULL;
274 }
275
276 void* cmd_queue_alloc(size_t size)
277 {
278         cmd_queue_page_t **p_page = &cmd_queue_pages;
279         int offset;
280
281         if (*p_page)
282         {
283                 while ((*p_page)->next)
284                         p_page = &((*p_page)->next);
285                 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
286                         p_page = &((*p_page)->next);
287         }
288
289         if (!*p_page)
290         {
291                 *p_page = malloc(sizeof(cmd_queue_page_t));
292                 (*p_page)->used = 0;
293                 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
294                 (*p_page)->next = NULL;
295         }
296
297         offset = (*p_page)->used;
298         (*p_page)->used += size;
299         
300         return ((*p_page)->address) + offset;
301 }
302
303 void cmd_queue_free()
304 {
305         cmd_queue_page_t *page = cmd_queue_pages;
306
307         while (page)
308         {
309                 cmd_queue_page_t *last = page;
310                 free(page->address);
311                 page = page->next;
312                 free(last);
313         }
314
315         cmd_queue_pages = NULL;
316 }
317
318 int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
319 {
320         jtag_command_t **last_cmd;
321         jtag_device_t *device;
322         int i, j;
323         int scan_size = 0;
324         /*      int changed = 0; */
325
326         if (jtag_trst == 1)
327         {
328                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
329                 return ERROR_JTAG_TRST_ASSERTED;
330         }
331
332         /*
333         for (i=0; i<num_fields; i++)
334         {
335                 device = jtag_get_device(fields[i].device);
336                 if (device)
337                 {
338                         if (buf_cmp(device->cur_instr, fields[i].out_value, device->ir_length))
339                                 changed = 1;
340                 }
341                 else
342                 {
343                         ERROR("inexistant device specified for ir scan");
344                         return ERROR_INVALID_ARGUMENTS;
345                 }
346         }
347
348         if (!changed)
349                 return ERROR_OK;
350         */
351         
352         last_cmd = jtag_get_last_command_p();
353         
354         /* allocate memory for a new list member */
355         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
356         (*last_cmd)->next = NULL;
357         last_comand_pointer = &((*last_cmd)->next);
358         (*last_cmd)->type = JTAG_SCAN;
359
360         /* allocate memory for ir scan command */
361         (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
362         (*last_cmd)->cmd.scan->ir_scan = 1;
363         (*last_cmd)->cmd.scan->num_fields = jtag_num_devices;   /* one field per device */
364         (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
365         (*last_cmd)->cmd.scan->end_state = state;
366         
367         if (state != -1)
368                 cmd_queue_end_state = state;
369
370         if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
371                 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
372         
373         if (cmd_queue_end_state == TAP_TLR)
374                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
375         
376         cmd_queue_cur_state = cmd_queue_end_state;
377                 
378         for (i=0; i < jtag_num_devices; i++)
379         {
380                 int found = 0;
381                 device = jtag_get_device(i);
382                 scan_size = device->ir_length;
383                 (*last_cmd)->cmd.scan->fields[i].device = i;
384                 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
385                 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
386                 if (jtag_verify_capture_ir)
387                 {
388                         (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(device->expected, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
389                         (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(device->expected_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
390                 }
391                 else
392                 {
393                         (*last_cmd)->cmd.scan->fields[i].in_check_value = NULL;
394                         (*last_cmd)->cmd.scan->fields[i].in_check_mask = NULL;
395                 }                       
396                 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
397                 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
398
399                 /* search the list */
400                 for (j=0; j < num_fields; j++)
401                 {
402                         if (i == fields[j].device)
403                         {
404                                 found = 1;
405                                 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
406                                 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
407                                 
408                                 device->bypass = 0;
409                                 break;
410                         }
411                 }
412         
413                 if (!found)
414                 {
415                         /* if a device isn't listed, set it to BYPASS */
416                         (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
417                         (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
418                         device->bypass = 1;
419                 
420                 }
421                 
422                 /* update device information */
423                 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
424         }
425         
426         return ERROR_OK;
427 }
428
429 int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
430 {
431         jtag_command_t **last_cmd;
432         int i;
433
434         if (jtag_trst == 1)
435         {
436                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
437                 return ERROR_JTAG_TRST_ASSERTED;
438         }
439
440         last_cmd = jtag_get_last_command_p();
441         
442         /* allocate memory for a new list member */
443         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
444         (*last_cmd)->next = NULL;
445         last_comand_pointer = &((*last_cmd)->next);
446         (*last_cmd)->type = JTAG_SCAN;
447
448         /* allocate memory for ir scan command */
449         (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
450         (*last_cmd)->cmd.scan->ir_scan = 1;
451         (*last_cmd)->cmd.scan->num_fields = num_fields;
452         (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
453         (*last_cmd)->cmd.scan->end_state = state;
454
455         if (state != -1)
456                 cmd_queue_end_state = state;
457
458         if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
459                 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
460         
461         if (cmd_queue_end_state == TAP_TLR)
462                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
463                 
464         cmd_queue_cur_state = cmd_queue_end_state;
465         
466         for (i = 0; i < num_fields; i++)
467         {
468                 int num_bits = fields[i].num_bits;
469                 int num_bytes = CEIL(fields[i].num_bits, 8);
470                 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
471                 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
472                 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
473                 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
474                 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
475                 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(fields[i].in_check_value, cmd_queue_alloc(num_bytes), num_bits);
476                 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(fields[i].in_check_mask, cmd_queue_alloc(num_bytes), num_bits);
477                 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
478                 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
479         }
480         return ERROR_OK;
481 }
482
483 int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
484 {
485         int i, j;
486         int bypass_devices = 0;
487         int field_count = 0;
488         jtag_command_t **last_cmd = jtag_get_last_command_p();
489         jtag_device_t *device = jtag_devices;
490         int scan_size;
491
492         if (jtag_trst == 1)
493         {
494                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
495                 return ERROR_JTAG_TRST_ASSERTED;
496         }
497
498         /* count devices in bypass */
499         while (device)
500         {
501                 if (device->bypass)
502                         bypass_devices++;
503                 device = device->next;
504         }
505         
506         /* allocate memory for a new list member */
507         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
508         last_comand_pointer = &((*last_cmd)->next);
509         (*last_cmd)->next = NULL;
510         (*last_cmd)->type = JTAG_SCAN;
511
512         /* allocate memory for dr scan command */
513         (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
514         (*last_cmd)->cmd.scan->ir_scan = 0;
515         (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
516         (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
517         (*last_cmd)->cmd.scan->end_state = state;
518
519         if (state != -1)
520                 cmd_queue_end_state = state;
521
522         if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
523                 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
524         
525         if (cmd_queue_end_state == TAP_TLR)
526                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
527                         
528         cmd_queue_cur_state = cmd_queue_end_state;
529         
530         for (i=0; i < jtag_num_devices; i++)
531         {
532                 int found = 0;
533                 (*last_cmd)->cmd.scan->fields[field_count].device = i;
534         
535                 for (j=0; j < num_fields; j++)
536                 {
537                         if (i == fields[j].device)
538                         {
539                                 found = 1;
540                                 scan_size = fields[j].num_bits;
541                                 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
542                                 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
543                                 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
544                                 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
545                                 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = buf_cpy(fields[j].in_check_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
546                                 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = buf_cpy(fields[j].in_check_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
547                                 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
548                                 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
549                         }
550                 }
551                 if (!found)
552                 {
553                         /* if a device isn't listed, the BYPASS register should be selected */
554                         if (!jtag_get_device(i)->bypass)
555                         {
556                                 ERROR("BUG: no scan data for a device not in BYPASS");
557                                 exit(-1);
558                         }
559         
560                         /* program the scan field to 1 bit length, and ignore it's value */
561                         (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
562                         (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
563                         (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
564                         (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
565                         (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
566                         (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
567                         (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
568                         (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
569                 }
570                 else
571                 {
572                         /* if a device is listed, the BYPASS register must not be selected */
573                         if (jtag_get_device(i)->bypass)
574                         {
575                                 ERROR("BUG: scan data for a device in BYPASS");
576                                 exit(-1);
577                         }
578                 }
579         }
580         return ERROR_OK;
581 }
582
583 int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
584 {
585         int i;
586         jtag_command_t **last_cmd = jtag_get_last_command_p();
587         
588         if (jtag_trst == 1)
589         {
590                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
591                 return ERROR_JTAG_TRST_ASSERTED;
592         }
593
594         /* allocate memory for a new list member */
595         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
596         last_comand_pointer = &((*last_cmd)->next);
597         (*last_cmd)->next = NULL;
598         (*last_cmd)->type = JTAG_SCAN;
599
600         /* allocate memory for scan command */
601         (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
602         (*last_cmd)->cmd.scan->ir_scan = 0;
603         (*last_cmd)->cmd.scan->num_fields = num_fields;
604         (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
605         (*last_cmd)->cmd.scan->end_state = state;
606         
607         if (state != -1)
608                 cmd_queue_end_state = state;
609
610         if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
611                 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
612         
613         if (cmd_queue_end_state == TAP_TLR)
614                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
615                         
616         cmd_queue_cur_state = cmd_queue_end_state;
617         
618         for (i = 0; i < num_fields; i++)
619         {
620                 int num_bits = fields[i].num_bits;
621                 int num_bytes = CEIL(fields[i].num_bits, 8);
622                 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
623                 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
624                 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
625                 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
626                 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
627                 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(fields[i].in_check_value, cmd_queue_alloc(num_bytes), num_bits);
628                 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(fields[i].in_check_mask, cmd_queue_alloc(num_bytes), num_bits);
629                 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
630                 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
631         }
632
633         return ERROR_OK;
634 }
635 int jtag_add_statemove(enum tap_state state)
636 {
637         jtag_command_t **last_cmd = jtag_get_last_command_p();
638         
639         if (jtag_trst == 1)
640         {
641                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
642                 return ERROR_JTAG_TRST_ASSERTED;
643         }
644
645         /* allocate memory for a new list member */
646         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
647         last_comand_pointer = &((*last_cmd)->next);
648         (*last_cmd)->next = NULL;
649         (*last_cmd)->type = JTAG_STATEMOVE;
650
651         (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
652         (*last_cmd)->cmd.statemove->end_state = state;
653         
654         if (state != -1)
655                 cmd_queue_end_state = state;
656
657         if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
658                 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
659         
660         if (cmd_queue_end_state == TAP_TLR)
661                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
662                         
663         cmd_queue_cur_state = cmd_queue_end_state;
664         
665         return ERROR_OK;
666 }
667
668 int jtag_add_pathmove(int num_states, enum tap_state *path)
669 {
670         jtag_command_t **last_cmd = jtag_get_last_command_p();
671         int i;
672         
673         if (jtag_trst == 1)
674         {
675                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
676                 return ERROR_JTAG_TRST_ASSERTED;
677         }
678         
679         /* the last state has to be a stable state */
680         if (tap_move_map[path[num_states - 1]] == -1)
681         {
682                 ERROR("TAP path doesn't finish in a stable state");
683                 return ERROR_JTAG_NOT_IMPLEMENTED;
684         }
685         
686         if (jtag->support_statemove)
687         {
688                 /* allocate memory for a new list member */
689                 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
690                 last_comand_pointer = &((*last_cmd)->next);
691                 (*last_cmd)->next = NULL;
692                 (*last_cmd)->type = JTAG_RUNTEST;
693         
694                 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
695                 (*last_cmd)->cmd.pathmove->num_states = num_states;
696                 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
697                 
698                 for (i = 0; i < num_states; i++)
699                         (*last_cmd)->cmd.pathmove->path[i] = path[i];
700         }
701         else
702         {
703                 /* validate the desired path, and see if it fits a default path */
704                 int begin = 0;
705                 int end = 0;
706                 int j;
707                 
708                 for (i = 0; i < num_states; i++)
709                 {
710                         for (j = i; j < num_states; j++)
711                         {
712                                 if (tap_move_map[path[j]] != -1)
713                                 {       
714                                         end = j;
715                                         break;
716                                 }
717                         }
718                         
719                         if (begin - end <= 7)   /* a default path spans no more than 7 states */
720                         {
721                                 jtag_add_statemove(path[end]);
722                         }
723                         else
724                         {
725                                 ERROR("encountered a TAP path that can't be fulfilled by default paths");       
726                                 return ERROR_JTAG_NOT_IMPLEMENTED;
727                         }
728                         
729                         i = end;
730                 }
731         }
732
733         if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
734                 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
735         
736         if (cmd_queue_end_state == TAP_TLR)
737                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
738         
739         cmd_queue_cur_state = path[num_states - 1];
740         
741         return ERROR_OK;
742 }
743
744 int jtag_add_runtest(int num_cycles, enum tap_state state)
745 {
746         jtag_command_t **last_cmd = jtag_get_last_command_p();
747         
748         if (jtag_trst == 1)
749         {
750                 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
751                 return ERROR_JTAG_TRST_ASSERTED;
752         }
753
754         /* allocate memory for a new list member */
755         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
756         (*last_cmd)->next = NULL;
757         last_comand_pointer = &((*last_cmd)->next);
758         (*last_cmd)->type = JTAG_RUNTEST;
759
760         (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
761         (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
762         (*last_cmd)->cmd.runtest->end_state = state;
763         
764         if (state != -1)
765                 cmd_queue_end_state = state;
766
767         if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
768                 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
769         
770         if (cmd_queue_end_state == TAP_TLR)
771                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
772                         
773         cmd_queue_cur_state = cmd_queue_end_state;
774         
775         return ERROR_OK;
776 }
777
778 int jtag_add_reset(int req_trst, int req_srst)
779 {
780         int trst_with_tms = 0;
781         
782         jtag_command_t **last_cmd = jtag_get_last_command_p();
783         
784         if (req_trst == -1)
785                 req_trst = jtag_trst;
786         
787         if (req_srst == -1)
788                 req_srst = jtag_srst;
789
790         /* Make sure that jtag_reset_config allows the requested reset */
791         /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
792         if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
793                 return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
794                 
795         /* if TRST pulls SRST, we reset with TAP T-L-R */
796         if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
797         {
798                 req_trst = 0;
799                 trst_with_tms = 1;
800         }
801         
802         if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
803                 return ERROR_JTAG_RESET_CANT_SRST;
804         
805         if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
806         {
807                 req_trst = 0;
808                 trst_with_tms = 1;
809         }
810         
811         /* allocate memory for a new list member */
812         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
813         (*last_cmd)->next = NULL;
814         last_comand_pointer = &((*last_cmd)->next);
815         (*last_cmd)->type = JTAG_RESET;
816
817         (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
818         (*last_cmd)->cmd.reset->trst = req_trst;
819         (*last_cmd)->cmd.reset->srst = req_srst;
820
821         jtag_trst = req_trst;
822         jtag_srst = req_srst;
823
824         if (jtag_srst)
825                 jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
826         else
827                 jtag_call_event_callbacks(JTAG_SRST_RELEASED);
828         
829         if (trst_with_tms)
830         {
831                 last_cmd = &((*last_cmd)->next);
832                 
833                 /* allocate memory for a new list member */
834                 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
835                 (*last_cmd)->next = NULL;
836                 last_comand_pointer = &((*last_cmd)->next);
837                 (*last_cmd)->type = JTAG_STATEMOVE;
838
839                 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
840                 (*last_cmd)->cmd.statemove->end_state = TAP_TLR;
841                 
842                 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
843                 cmd_queue_cur_state = TAP_TLR;
844                 cmd_queue_end_state = TAP_TLR;
845                 
846                 return ERROR_OK;
847         }
848         else
849         {
850                 if (jtag_trst)
851                 {
852                         cmd_queue_cur_state = TAP_TLR;
853                         jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
854                 }
855         }
856
857         return ERROR_OK;
858 }
859
860 int jtag_add_end_state(enum tap_state state)
861 {
862         jtag_command_t **last_cmd = jtag_get_last_command_p();
863         
864         /* allocate memory for a new list member */
865         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
866         (*last_cmd)->next = NULL;
867         last_comand_pointer = &((*last_cmd)->next);
868         (*last_cmd)->type = JTAG_END_STATE;
869
870         (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
871         (*last_cmd)->cmd.end_state->end_state = state;
872
873         if (state != -1)
874                 cmd_queue_end_state = state;
875         
876         return ERROR_OK;
877 }
878
879 int jtag_add_sleep(u32 us)
880 {
881         jtag_command_t **last_cmd = jtag_get_last_command_p();
882         
883         /* allocate memory for a new list member */
884         *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
885         (*last_cmd)->next = NULL;
886         last_comand_pointer = &((*last_cmd)->next);
887         (*last_cmd)->type = JTAG_SLEEP;
888
889         (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
890         (*last_cmd)->cmd.sleep->us = us;
891         
892         return ERROR_OK;
893 }
894
895 int jtag_scan_size(scan_command_t *cmd)
896 {
897         int bit_count = 0;
898         int i;
899
900         /* count bits in scan command */
901         for (i=0; i<cmd->num_fields; i++)
902         {
903                 bit_count += cmd->fields[i].num_bits;
904         }
905
906         return bit_count;
907 }
908
909 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
910 {
911         int bit_count = 0;
912         int i;
913         
914         bit_count = jtag_scan_size(cmd);
915         *buffer = malloc(CEIL(bit_count, 8));
916         
917         bit_count = 0;
918
919         for (i = 0; i < cmd->num_fields; i++)
920         {
921                 if (cmd->fields[i].out_value)
922                 {
923                         char* char_buf = buf_to_char(cmd->fields[i].out_value, cmd->fields[i].num_bits);
924                         buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
925 #ifdef _DEBUG_JTAG_IO_
926                         DEBUG("fields[%i].out_value: %s", i, char_buf);
927 #endif
928                         free(char_buf);
929                 }
930                 
931                 bit_count += cmd->fields[i].num_bits;
932         }
933
934         return bit_count;
935
936 }
937
938 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
939 {
940         int i;
941         int bit_count = 0;
942         int retval = ERROR_OK;
943
944         for (i=0; i < cmd->num_fields; i++)
945         {
946                 /* if neither in_value nor in_check_value are specified we don't have to examine this field */
947                 if (cmd->fields[i].in_value || cmd->fields[i].in_check_value)
948                 {
949                         int num_bits = cmd->fields[i].num_bits;
950
951                         if (cmd->fields[i].in_value)
952                         {
953                                 char *char_buf;
954                                 buf_set_buf(buffer, bit_count, cmd->fields[i].in_value, 0, num_bits);
955                                 char_buf = buf_to_char(cmd->fields[i].in_value, num_bits);
956 #ifdef _DEBUG_JTAG_IO_
957                                 DEBUG("fields[%i].in_value: %s", i, char_buf);
958 #endif
959                                 free(char_buf);
960                                 if (cmd->fields[i].in_handler)
961                                 {
962                                         if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv) != ERROR_OK)
963                                         {
964                                                 /* TODO: error reporting */
965                                                 WARNING("in_handler reported a failed check");
966                                                 retval = ERROR_JTAG_QUEUE_FAILED;
967                                         }
968                                 }
969                         }
970
971                         if (cmd->fields[i].in_check_value)
972                         {
973                                 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
974                                 if ((cmd->fields[i].in_check_mask && buf_cmp_mask(captured, cmd->fields[i].in_check_value, cmd->fields[i].in_check_mask, num_bits))
975                                         || (!cmd->fields[i].in_check_mask && buf_cmp(captured, cmd->fields[i].in_check_mask, num_bits)))
976                                 {
977                                         char *captured_char = buf_to_char(captured, num_bits);
978                                         char *in_check_value_char = buf_to_char(cmd->fields[i].in_check_value, num_bits);
979                                         char *in_check_mask_char = buf_to_char(cmd->fields[i].in_check_mask, num_bits);
980                                         /* TODO: error reporting */
981                                         WARNING("value captured during scan didn't pass the requested check: captured: %s check_value: %s check_mask: %s", captured_char, in_check_value_char, in_check_mask_char);
982                                         retval = ERROR_JTAG_QUEUE_FAILED;
983                                         free(captured_char);
984                                         free(in_check_value_char);
985                                         free(in_check_mask_char);
986                                 }
987                                 free(captured);
988                         }
989                 }
990                 bit_count += cmd->fields[i].num_bits;
991         }
992
993         return retval;
994 }
995
996 enum scan_type jtag_scan_type(scan_command_t *cmd)
997 {
998         int i;
999         int type = 0;
1000         
1001         for (i=0; i < cmd->num_fields; i++)
1002         {
1003                 if (cmd->fields[i].in_check_value || cmd->fields[i].in_value)
1004                         type |= SCAN_IN;
1005                 if (cmd->fields[i].out_value)
1006                         type |= SCAN_OUT;
1007         }
1008
1009         return type;
1010 }
1011
1012 int jtag_execute_queue(void)
1013 {
1014         int retval;
1015
1016         retval = jtag->execute_queue();
1017         
1018         cmd_queue_free();
1019
1020         jtag_command_queue = NULL;
1021         last_comand_pointer = &jtag_command_queue;
1022
1023         return retval;
1024 }
1025
1026 int jtag_cancel_queue(void)
1027 {
1028         cmd_queue_free();
1029         jtag_command_queue = NULL;
1030         last_comand_pointer = &jtag_command_queue;
1031
1032         return ERROR_OK;
1033 }
1034
1035 int jtag_reset_callback(enum jtag_event event, void *priv)
1036 {
1037         jtag_device_t *device = priv;
1038
1039         DEBUG("");
1040         
1041         if (event == JTAG_TRST_ASSERTED)
1042         {
1043                 buf_set_ones(device->cur_instr, device->ir_length);
1044                 device->bypass = 1;
1045         }
1046         
1047         return ERROR_OK;
1048 }
1049
1050 void jtag_sleep(u32 us)
1051 {
1052         usleep(us);
1053 }
1054
1055 int jtag_validate_chain()
1056 {
1057         jtag_device_t *device = jtag_devices;
1058         int total_ir_length = 0;
1059         u8 *ir_test = NULL;
1060         scan_field_t field;
1061         int chain_pos = 0;
1062         
1063         while (device)
1064         {
1065                 total_ir_length += device->ir_length;
1066                 device = device->next;
1067         }
1068         
1069         total_ir_length += 2;
1070         ir_test = malloc(CEIL(total_ir_length, 8));
1071         buf_set_ones(ir_test, total_ir_length);
1072         
1073         field.device = 0;
1074         field.num_bits = total_ir_length;
1075         field.out_value = ir_test;
1076         field.out_mask = NULL;
1077         field.in_value = ir_test;
1078         field.in_check_value = NULL;
1079         field.in_check_mask = NULL;
1080         field.in_handler = NULL;
1081         field.in_handler_priv = NULL;
1082         
1083         jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1084         jtag_execute_queue();
1085         
1086         device = jtag_devices;
1087         while (device)
1088         {
1089                 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1090                 {
1091                         ERROR("Error validating JTAG scan chain, IR mismatch");
1092                         exit(-1);
1093                 }
1094                 chain_pos += device->ir_length;
1095                 device = device->next;
1096         }
1097         
1098         if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1099         {
1100                 ERROR("Error validating JTAG scan chain, IR mismatch");
1101                 exit(-1);
1102         }
1103         
1104         free(ir_test);
1105         
1106         return ERROR_OK;
1107 }
1108
1109 int jtag_register_commands(struct command_context_s *cmd_ctx)
1110 {
1111         register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1112                 COMMAND_CONFIG, NULL);
1113         register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1114                 COMMAND_ANY, "set jtag speed (if supported) <speed>");
1115         register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1116                 COMMAND_CONFIG, NULL);
1117         register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1118                 COMMAND_CONFIG, NULL);
1119
1120         register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1121                 COMMAND_EXEC, "print current scan chain configuration");
1122
1123         register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1124                 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1125         register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1126                 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1127         register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1128                 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1129         register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1130                 COMMAND_EXEC, "move to current endstate or [tap_state]");
1131         register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1132                 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1133         register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1134                 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1135
1136         register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1137                 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1138         return ERROR_OK;
1139 }
1140
1141 int jtag_init(struct command_context_s *cmd_ctx)
1142 {
1143         int i;
1144         
1145         DEBUG("");
1146
1147         if (jtag_speed == -1)
1148                 jtag_speed = 0;
1149         
1150         if (jtag_interface && (jtag_interface[0] != 0))
1151                 /* configuration var 'jtag_interface' is set, and not empty */
1152                 for (i = 0; jtag_interfaces[i]; i++)
1153                 {
1154                         if (strcmp(jtag_interface, jtag_interfaces[i]->name) == 0)
1155                         {
1156                                 jtag_device_t *device;
1157                                 device = jtag_devices;
1158         
1159                                 if (jtag_interfaces[i]->init() != ERROR_OK)
1160                                         return ERROR_JTAG_INIT_FAILED;
1161                                 jtag = jtag_interfaces[i];
1162
1163                                 jtag_ir_scan_size = 0;
1164                                 jtag_num_devices = 0;
1165                                 while (device != NULL)
1166                                 {
1167                                         jtag_ir_scan_size += device->ir_length;
1168                                         jtag_num_devices++;
1169                                         device = device->next;
1170                                 }
1171                                 
1172                                 jtag_add_statemove(TAP_TLR);
1173                                 jtag_execute_queue();
1174                                 
1175                                 jtag_validate_chain();
1176                                 
1177                                 return ERROR_OK;
1178                         }
1179                 }
1180         
1181         /* no valid interface was found (i.e. the configuration option,
1182          * didn't match one of the compiled-in interfaces
1183          */
1184         ERROR("No valid jtag interface found (%s)", jtag_interface);
1185         jtag = NULL;
1186         return ERROR_JTAG_INVALID_INTERFACE;
1187 }
1188
1189 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1190 {
1191         int i;
1192         
1193         /* only if the configuration var isn't overwritten from cmdline */
1194         if (!jtag_interface)
1195         {
1196                 if (args[0] && (args[0][0] != 0))
1197                 {
1198                         for (i=0; jtag_interfaces[i]; i++)
1199                         {
1200                                 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1201                                 {
1202                                         if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1203                                                 exit(-1);
1204                                 
1205                                         jtag_interface = jtag_interfaces[i]->name;
1206                 
1207                                         return ERROR_OK;
1208                                 }
1209                         }
1210                 }
1211                 
1212                 /* remember the requested interface name, so we can complain about it later */
1213                 jtag_interface = strdup(args[0]);
1214                 DEBUG("'interface' command didn't specify a valid interface");
1215         }
1216         
1217         return ERROR_OK;
1218 }
1219
1220 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1221 {
1222         jtag_device_t **last_device_p = &jtag_devices;
1223
1224         if (*last_device_p)
1225         {
1226                 while ((*last_device_p)->next)
1227                         last_device_p = &((*last_device_p)->next);
1228                 last_device_p = &((*last_device_p)->next);
1229         }
1230
1231         if (argc < 3)
1232                 return ERROR_OK;
1233
1234         *last_device_p = malloc(sizeof(jtag_device_t));
1235         (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1236
1237         (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1238         buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1239         (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1240         buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1241
1242         (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1243         (*last_device_p)->bypass = 1;
1244         buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1245         
1246         (*last_device_p)->next = NULL;
1247         
1248         jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1249         
1250         jtag_num_devices++;
1251
1252         return ERROR_OK;
1253 }
1254
1255 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1256 {
1257         jtag_device_t *device = jtag_devices;
1258         int device_count = 0;
1259         
1260         while (device)
1261         {
1262                 u32 expected, expected_mask, cur_instr;
1263                 expected = buf_get_u32(device->expected, 0, device->ir_length);
1264                 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1265                 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1266                 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);
1267                 device = device->next;
1268                 device_count++;
1269         }
1270
1271         return ERROR_OK;
1272 }
1273
1274 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1275 {
1276         if (argc >= 1)
1277         {
1278                 if (strcmp(args[0], "none") == 0)
1279                         jtag_reset_config = RESET_NONE;
1280                 else if (strcmp(args[0], "trst_only") == 0)
1281                         jtag_reset_config = RESET_HAS_TRST;
1282                 else if (strcmp(args[0], "srst_only") == 0)
1283                         jtag_reset_config = RESET_HAS_SRST;
1284                 else if (strcmp(args[0], "trst_and_srst") == 0)
1285                         jtag_reset_config = RESET_TRST_AND_SRST;
1286                 else
1287                 {
1288                         ERROR("invalid reset_config argument");
1289                         exit(-1);
1290                 }
1291         }
1292         
1293         if (argc >= 2)
1294         {
1295                 if (strcmp(args[1], "srst_pulls_trst") == 0)
1296                         jtag_reset_config |= RESET_SRST_PULLS_TRST;
1297                 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1298                         jtag_reset_config |= RESET_TRST_PULLS_SRST;
1299                 else if (strcmp(args[1], "combined") == 0)
1300                         jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1301                 else if (strcmp(args[1], "separate") == 0)
1302                         jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
1303                 else
1304                 {
1305                         ERROR("invalid reset_config argument");
1306                         exit(-1);
1307                 }
1308         }
1309         
1310         if (argc >= 3)
1311         {
1312                 if (strcmp(args[2], "trst_open_drain") == 0)
1313                         jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1314                 else if (strcmp(args[2], "trst_push_pull") == 0)
1315                         jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1316                 else
1317                 {
1318                         ERROR("invalid reset_config argument");
1319                         exit(-1);
1320                 }
1321         }
1322
1323         if (argc >= 4)
1324         {
1325                 if (strcmp(args[3], "srst_push_pull") == 0)
1326                         jtag_reset_config |= RESET_SRST_PUSH_PULL;
1327                 else if (strcmp(args[3], "srst_open_drain") == 0)
1328                         jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1329                 else
1330                 {
1331                         ERROR("invalid reset_config argument");
1332                         exit(-1);
1333                 }
1334         }
1335         
1336         return ERROR_OK;
1337 }
1338
1339 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1340 {
1341         if (argc == 0)
1342                 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
1343
1344         if (argc > 0)
1345         {
1346                 /* this command can be called during CONFIG, 
1347                  * in which case jtag isn't initialized */
1348                 if (jtag)
1349                         jtag->speed(strtoul(args[0], NULL, 0));
1350                 else
1351                         jtag_speed = strtoul(args[0], NULL, 0);
1352         }
1353
1354         return ERROR_OK;
1355 }
1356
1357 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1358 {
1359         enum tap_state state;
1360
1361         if (argc < 1)
1362         {
1363                 command_print(cmd_ctx, "usage: endstate <tap_state>");
1364                 return ERROR_OK;
1365         }
1366
1367         for (state = 0; state < 16; state++)
1368         {
1369                 if (strcmp(args[0], tap_state_strings[state]) == 0)
1370                 {
1371                         jtag_add_end_state(state);
1372                         jtag_execute_queue();
1373                 }
1374         }
1375         
1376         return ERROR_OK;
1377 }
1378
1379 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1380 {
1381         int trst = -1;
1382         int srst = -1;
1383         char *usage = "usage: jtag_reset <trst> <srst>";
1384         int retval;
1385         
1386         if (argc < 1)
1387         {
1388                 command_print(cmd_ctx, usage);
1389                 return ERROR_OK;
1390         }
1391
1392         if (args[0][0] == '1')
1393                 trst = 1;
1394         else if (args[0][0] == '0')
1395                 trst = 0;
1396         else
1397         {
1398                 command_print(cmd_ctx, usage);
1399                 return ERROR_OK;
1400         }
1401
1402         if (args[1][0] == '1')
1403                 srst = 1;
1404         else if (args[1][0] == '0')
1405                 srst = 0;
1406         else
1407         {
1408                 command_print(cmd_ctx, usage);
1409                 return ERROR_OK;
1410         }
1411
1412         if ((retval = jtag_add_reset(trst, srst)) != ERROR_OK)
1413         {
1414                 switch (retval)
1415                 {
1416                         case ERROR_JTAG_RESET_WOULD_ASSERT_TRST:
1417                                 command_print(cmd_ctx, "requested reset would assert trst\nif this is acceptable, use jtag_reset 1 %c", args[1][0]);
1418                                 break;
1419                         case ERROR_JTAG_RESET_CANT_SRST:
1420                                 command_print(cmd_ctx, "can't assert srst because the current reset_config doesn't support it");
1421                                 break;
1422                         default:
1423                                 command_print(cmd_ctx, "unknown error");
1424                 }
1425         }
1426         jtag_execute_queue();
1427
1428         return ERROR_OK;
1429 }
1430
1431 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1432 {
1433         if (argc < 1)
1434         {
1435                 command_print(cmd_ctx, "usage: runtest <num_cycles>");
1436                 return ERROR_OK;
1437         }
1438
1439         jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1440         jtag_execute_queue();
1441
1442         return ERROR_OK;
1443
1444 }
1445
1446 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1447 {
1448         enum tap_state state;
1449
1450         state = -1;
1451         if (argc == 1)
1452         {
1453                 for (state = 0; state < 16; state++)
1454                 {
1455                         if (strcmp(args[0], tap_state_strings[state]) == 0)
1456                         {
1457                                 break;
1458                         }
1459                 }
1460         }
1461
1462         jtag_add_statemove(state);
1463         jtag_execute_queue();
1464
1465         return ERROR_OK;
1466
1467 }
1468
1469 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1470 {
1471         int i;
1472         scan_field_t *fields;
1473         
1474         if ((argc < 2) || (argc % 2))
1475         {
1476                 command_print(cmd_ctx, "usage: irscan <device> <instr> [dev2] [instr2] ...");
1477                 return ERROR_OK;
1478         }
1479
1480         fields = malloc(sizeof(scan_field_t) * argc / 2);
1481         
1482         for (i = 0; i < argc / 2; i++)
1483         {
1484                 int device = strtoul(args[i*2], NULL, 0);
1485                 int field_size = jtag_get_device(device)->ir_length;
1486                 fields[i].device = device;
1487                 fields[i].out_value = malloc(CEIL(field_size, 8));
1488                 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1489                 fields[i].out_mask = NULL;
1490                 fields[i].in_value = NULL;
1491                 fields[i].in_check_mask = NULL;
1492                 fields[i].in_handler = NULL;
1493                 fields[i].in_handler_priv = NULL;
1494         }
1495
1496         jtag_add_ir_scan(argc / 2, fields, -1);
1497         jtag_execute_queue();
1498
1499         for (i = 0; i < argc / 2; i++)
1500                 free(fields[i].out_value);
1501
1502         free (fields);
1503
1504         return ERROR_OK;
1505 }
1506
1507 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1508 {
1509         scan_field_t *fields;
1510         int num_fields = 0;
1511         int field_count = 0;
1512         var_t *var;
1513         int i, j;
1514         
1515         if ((argc < 2) || (argc % 2))
1516         {
1517                 command_print(cmd_ctx, "usage: drscan <device> <var> [dev2] [var2]");
1518                 return ERROR_OK;
1519         }
1520
1521         for (i = 0; i < argc; i+=2)
1522         {
1523                 var = get_var_by_namenum(args[i+1]);
1524                 if (var)
1525                 {
1526                         num_fields += var->num_fields;
1527                 }
1528                 else
1529                 {
1530                         command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1531                         return ERROR_OK;
1532                 }
1533         }
1534
1535         fields = malloc(sizeof(scan_field_t) * num_fields);
1536
1537         for (i = 0; i < argc; i+=2)
1538         {
1539                 var = get_var_by_namenum(args[i+1]);
1540         
1541                 for (j = 0; j < var->num_fields; j++)
1542                 {
1543                         fields[field_count].device = strtol(args[i], NULL, 0);
1544                         fields[field_count].num_bits = var->fields[j].num_bits;
1545                         fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1546                         buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1547                         fields[field_count].out_mask = NULL;
1548                         fields[field_count].in_value = fields[field_count].out_value;
1549                         fields[field_count].in_check_mask = NULL;
1550                         fields[field_count].in_check_value = NULL;
1551                         fields[field_count].in_handler = field_le_to_host;
1552                         fields[field_count++].in_handler_priv = &(var->fields[j]);
1553                 }
1554         }
1555
1556         jtag_add_dr_scan(num_fields, fields, -1);
1557         jtag_execute_queue();
1558         
1559         for (i = 0; i < argc / 2; i++)
1560                 free(fields[i].out_value);
1561
1562         free(fields);
1563
1564         return ERROR_OK;
1565 }
1566
1567 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1568 {
1569         if (argc == 0)
1570         {
1571                 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
1572                 return ERROR_OK;
1573         }
1574         
1575         if (strcmp(args[0], "enable") == 0)
1576         {
1577                 jtag_verify_capture_ir = 1;
1578         }
1579         else if (strcmp(args[0], "disable") == 0)
1580         {
1581                 jtag_verify_capture_ir = 0;
1582         }
1583         
1584         return ERROR_OK;
1585 }