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