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