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