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