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