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