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