jtag.c 68 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
5
 *   Copyright (C) 2007,2008 yvind Harboe                                 *
6
 *   oyvind.harboe@zylin.com                                               *
ntfreak's avatar
ntfreak committed
7
8
 *                                                                         *
 *   Copyright (C) 2009 SoftPLC Corporation                                *
zwelch's avatar
zwelch committed
9
 *       http://softplc.com                                                *
ntfreak's avatar
ntfreak committed
10
 *   dick@softplc.com                                                      *
11
 *                                                                         *
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "jtag.h"
zwelch's avatar
zwelch committed
32
#include "minidriver.h"
33
#include "interface.h"
34

35
36
37
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
38

39

40
41
/// The number of JTAG queue flushes (for profiling and debugging purposes).
static int jtag_flush_queue_count;
42

43
static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
44
		int in_num_fields, scan_field_t *in_fields, tap_state_t state);
oharboe's avatar
oharboe committed
45

46
47
/* note that this is not marked as static as it must be available from outside jtag.c for those
   that implement the jtag_xxx() minidriver layer
oharboe's avatar
oharboe committed
48
*/
49
int jtag_error=ERROR_OK;
oharboe's avatar
oharboe committed
50

51
52
char* jtag_event_strings[] =
{
53
	"JTAG controller reset (RESET or TRST)"
54
55
};

56
57
58
59
60
61
62
const Jim_Nvp nvp_jtag_tap_event[] = {
	{ .value = JTAG_TAP_EVENT_ENABLE,       .name = "tap-enable" },
	{ .value = JTAG_TAP_EVENT_DISABLE,      .name = "tap-disable" },

	{ .name = NULL, .value = -1 }
};

63
64
65
int jtag_trst = 0;
int jtag_srst = 0;

zwelch's avatar
zwelch committed
66
67
68
/**
 * List all TAPs that have been created.
 */
69
static jtag_tap_t *__jtag_all_taps = NULL;
zwelch's avatar
zwelch committed
70
/**
71
 * The number of TAPs in the __jtag_all_taps list, used to track the
zwelch's avatar
zwelch committed
72
73
74
 * assigned chain position to new TAPs
 */
static int jtag_num_taps = 0;
75

76
enum reset_types jtag_reset_config = RESET_NONE;
77
78
tap_state_t cmd_queue_end_state = TAP_RESET;
tap_state_t cmd_queue_cur_state = TAP_RESET;
79
80

int jtag_verify_capture_ir = 1;
oharboe's avatar
oharboe committed
81
int jtag_verify = 1;
82
83

/* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
84
85
static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
86
87
88
89

/* callbacks to inform high-level handlers about JTAG state changes */
jtag_event_callback_t *jtag_event_callbacks;

oharboe's avatar
   
oharboe committed
90
/* speed in kHz*/
oharboe's avatar
   
oharboe committed
91
static int speed_khz = 0;
oharboe's avatar
   
oharboe committed
92
/* flag if the kHz speed was defined */
zwelch's avatar
zwelch committed
93
static bool hasKHz = false;
oharboe's avatar
   
oharboe committed
94

95
96
/* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
 */
97

98
#if BUILD_ECOSBOARD == 1
99
	extern jtag_interface_t zy1000_interface;
100
#endif
101

102
103
104
#if BUILD_MINIDUMMY == 1
	extern jtag_interface_t minidummy_interface;
#endif
105
106
107
#if BUILD_PARPORT == 1
	extern jtag_interface_t parport_interface;
#endif
108

109
110
111
#if BUILD_DUMMY == 1
	extern jtag_interface_t dummy_interface;
#endif
112

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#if BUILD_FT2232_FTD2XX == 1
	extern jtag_interface_t ft2232_interface;
#endif

#if BUILD_FT2232_LIBFTDI == 1
	extern jtag_interface_t ft2232_interface;
#endif

#if BUILD_AMTJTAGACCEL == 1
	extern jtag_interface_t amt_jtagaccel_interface;
#endif

#if BUILD_EP93XX == 1
	extern jtag_interface_t ep93xx_interface;
#endif

#if BUILD_AT91RM9200 == 1
	extern jtag_interface_t at91rm9200_interface;
#endif

#if BUILD_GW16012 == 1
	extern jtag_interface_t gw16012_interface;
#endif

#if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
	extern jtag_interface_t presto_interface;
#endif

#if BUILD_USBPROG == 1
	extern jtag_interface_t usbprog_interface;
#endif

145
146
147
148
#if BUILD_JLINK == 1
	extern jtag_interface_t jlink_interface;
#endif

duane's avatar
duane committed
149
150
151
152
#if BUILD_VSLLINK == 1
	extern jtag_interface_t vsllink_interface;
#endif

153
154
155
156
#if BUILD_RLINK == 1
	extern jtag_interface_t rlink_interface;
#endif

157
158
159
160
#if BUILD_ARMJTAGEW == 1
	extern jtag_interface_t armjtagew_interface;
#endif

161
jtag_interface_t *jtag_interfaces[] = {
162
#if BUILD_ECOSBOARD == 1
163
	&zy1000_interface,
164
#endif
165
166
167
#if BUILD_MINIDUMMY == 1
	&minidummy_interface,
#endif
168
169
170
#if BUILD_PARPORT == 1
	&parport_interface,
#endif
171
172
173
#if BUILD_DUMMY == 1
	&dummy_interface,
#endif
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#if BUILD_FT2232_FTD2XX == 1
	&ft2232_interface,
#endif
#if BUILD_FT2232_LIBFTDI == 1
	&ft2232_interface,
#endif
#if BUILD_AMTJTAGACCEL == 1
	&amt_jtagaccel_interface,
#endif
#if BUILD_EP93XX == 1
	&ep93xx_interface,
#endif
#if BUILD_AT91RM9200 == 1
	&at91rm9200_interface,
#endif
#if BUILD_GW16012 == 1
	&gw16012_interface,
#endif
#if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
	&presto_interface,
#endif
#if BUILD_USBPROG == 1
	&usbprog_interface,
197
198
199
#endif
#if BUILD_JLINK == 1
	&jlink_interface,
200
#endif
duane's avatar
duane committed
201
202
203
#if BUILD_VSLLINK == 1
	&vsllink_interface,
#endif
204
205
#if BUILD_RLINK == 1
	&rlink_interface,
206
207
208
#endif
#if BUILD_ARMJTAGEW == 1
	&armjtagew_interface,
209
210
211
212
#endif
	NULL,
};

213
struct jtag_interface_s *jtag = NULL;
214
215

/* configuration */
216
static jtag_interface_t *jtag_interface = NULL;
217
int jtag_speed = 0;
oharboe's avatar
   
oharboe committed
218

219
/* jtag commands */
220
221
222
223
224
225
226
static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
227

228
static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
229

230
231
232
233
234
static int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
235
static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
236

237
static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
oharboe's avatar
oharboe committed
238
static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
239
static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
240

241
jtag_tap_t *jtag_all_taps(void)
oharboe's avatar
oharboe committed
242
{
243
	return __jtag_all_taps;
244
245
};

246
int jtag_tap_count(void)
247
{
zwelch's avatar
zwelch committed
248
	return jtag_num_taps;
249
	}
250

251
unsigned jtag_tap_count_enabled(void)
252
253
{
	jtag_tap_t *t;
254
	unsigned n;
255
256

	n = 0;
257
	t = jtag_all_taps();
258
259
260
261
262
263
264
265
266
	while(t){
		if( t->enabled ){
			n++;
		}
		t = t->next_tap;
	}
	return n;
}

zwelch's avatar
zwelch committed
267
268
269
270
271
/// Append a new TAP to the chain of all taps.
static void jtag_tap_add(struct jtag_tap_s *t)
{
	t->abs_chain_position = jtag_num_taps++;

272
	jtag_tap_t **tap = &__jtag_all_taps;
zwelch's avatar
zwelch committed
273
274
275
276
277
	while(*tap != NULL)
		tap = &(*tap)->next_tap;
	*tap = t;
}

278
jtag_tap_t *jtag_tap_by_string( const char *s )
279
280
281
282
{
	jtag_tap_t *t;
	char *cp;

283
	t = jtag_all_taps();
ntfreak's avatar
ntfreak committed
284
	/* try name first */
285
286
287
288
289
290
291
	while(t){
		if( 0 == strcmp( t->dotted_name, s ) ){
			break;
		} else {
			t = t->next_tap;
		}
	}
ntfreak's avatar
ntfreak committed
292
	/* backup plan is by number */
293
294
295
296
297
298
	if( t == NULL ){
		/* ok - is "s" a number? */
		int n;
		n = strtol( s, &cp, 0 );
		if( (s != cp) && (*cp == 0) ){
			/* Then it is... */
299
			t = jtag_tap_by_abs_position(n);
300
301
302
303
304
		}
	}
	return t;
}

305
jtag_tap_t * jtag_tap_by_jim_obj( Jim_Interp *interp, Jim_Obj *o )
306
307
308
309
310
311
312
313
314
{
	jtag_tap_t *t;
	const char *cp;

	cp = Jim_GetString( o, NULL );
	if(cp == NULL){
		cp = "(unknown)";
		t = NULL;
	}  else {
315
		t = jtag_tap_by_string( cp );
oharboe's avatar
oharboe committed
316
	}
317
318
319
320
321
322
323
	if( t == NULL ){
		Jim_SetResult_sprintf(interp,"Tap: %s is unknown", cp );
	}
	return t;
}

/* returns a pointer to the n-th device in the scan chain */
324
jtag_tap_t * jtag_tap_by_abs_position( int n )
325
326
327
328
329
{
	int orig_n;
	jtag_tap_t *t;

	orig_n = n;
330
	t = jtag_all_taps();
oharboe's avatar
oharboe committed
331

332
333
334
	while( t && (n > 0)) {
		n--;
		t = t->next_tap;
335
	}
336
337
338
	return t;
}

zwelch's avatar
zwelch committed
339
340
341
342
343
344
const char *jtag_tap_name(const jtag_tap_t *tap)
{
	return (tap == NULL) ? "(unknown)" : tap->dotted_name;
}


345
346
347
int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
{
	jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
348

349
350
351
352
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
353

354
355
356
357
358
359
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
360

361
362
363
364
	(*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->priv = priv;
	(*callbacks_p)->next = NULL;
365

366
367
368
369
370
371
	return ERROR_OK;
}

int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
{
	jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
372

373
374
375
376
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
377

378
379
380
381
382
383
384
385
386
387
	while (*callbacks_p)
	{
		jtag_event_callback_t **next = &((*callbacks_p)->next);
		if ((*callbacks_p)->callback == callback)
		{
			free(*callbacks_p);
			*callbacks_p = *next;
		}
		callbacks_p = next;
	}
388

389
390
391
392
393
394
	return ERROR_OK;
}

int jtag_call_event_callbacks(enum jtag_event event)
{
	jtag_event_callback_t *callback = jtag_event_callbacks;
395

396
	LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
397

398
399
400
401
402
	while (callback)
	{
		callback->callback(event, callback->priv);
		callback = callback->next;
	}
403

404
405
406
	return ERROR_OK;
}

407
static void jtag_checks(void)
408
{
409
	assert(jtag_trst == 0);
410
411
}

412
static void jtag_prelude(tap_state_t state)
413
{
414
	jtag_checks();
415

416
417
	assert(state!=TAP_INVALID);

418
	cmd_queue_cur_state = state;
419
420
}

zwelch's avatar
zwelch committed
421
422
423
424
425
void jtag_alloc_in_value32(scan_field_t *field)
{
	interface_jtag_alloc_in_value32(field);
}

426
void jtag_add_ir_scan_noverify(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
427
{
oharboe's avatar
oharboe committed
428
	int retval;
429
	jtag_prelude(state);
430

431
	retval=interface_jtag_add_ir_scan(in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
432
433
	if (retval!=ERROR_OK)
		jtag_error=retval;
434
435
436
437

}


438
439
440
441
442
443
444
445
446
/**
 * Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
 *
 * If the input field list contains an instruction value for a TAP then that is used
 * otherwise the TAP is set to bypass.
 *
 * TAPs for which no fields are passed are marked as bypassed for subsequent DR SCANs.
 *
 */
447
void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
448
{
oharboe's avatar
oharboe committed
449
	if (jtag_verify&&jtag_verify_capture_ir)
450
	{
zwelch's avatar
zwelch committed
451
		/* 8 x 32 bit id's is enough for all invocations */
452
453

		for (int j = 0; j < in_num_fields; j++)
454
		{
oharboe's avatar
oharboe committed
455
456
457
			/* if we are to run a verification of the ir scan, we need to get the input back.
			 * We may have to allocate space if the caller didn't ask for the input back.
			 */
458
459
			in_fields[j].check_value=in_fields[j].tap->expected;
			in_fields[j].check_mask=in_fields[j].tap->expected_mask;
460
		}
461
		jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
462
463
	} else
	{
464
		jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
465
	}
oharboe's avatar
oharboe committed
466
467
}

468
469
470
471
472
473
/**
 * Duplicate the scan fields passed into the function into an IR SCAN command
 *
 * This function assumes that the caller handles extra fields for bypassed TAPs
 *
 */
474
void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
475
{
oharboe's avatar
oharboe committed
476
	int retval;
477

478
	jtag_prelude(state);
479

480
	retval=interface_jtag_add_plain_ir_scan(in_num_fields, in_fields, state);
481
482
	if (retval!=ERROR_OK)
		jtag_error=retval;
483
484
}

485
486
487
488
void jtag_add_callback(jtag_callback1_t f, u8 *in)
{
	interface_jtag_add_callback(f, in);
}
489

490
491
492
493
494
495
void jtag_add_callback4(jtag_callback_t f, u8 *in,
		jtag_callback_data_t data1, jtag_callback_data_t data2,
		jtag_callback_data_t data3)
{
	interface_jtag_add_callback4(f, in, data1, data2, data3);
}
496
497
498
499
500
501
502
503

int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits);

static int jtag_check_value_mask_callback(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
{
	return jtag_check_value_inner(in, (u8 *)data1, (u8 *)data2, (int)data3);
}

504
static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
505
		int in_num_fields, scan_field_t *in_fields, tap_state_t state)
506
{
507
	for (int i = 0; i < in_num_fields; i++)
508
	{
509
510
511
512
513
514
515
		struct scan_field_s *field = &in_fields[i];
		field->allocated = 0;
		field->modified = 0;
		if (field->check_value || field->in_value)
			continue;
		interface_jtag_add_scan_check_alloc(field);
		field->modified = 1;
516
517
	}

518
	jtag_add_scan(in_num_fields, in_fields, state);
519

520
	for (int i = 0; i < in_num_fields; i++)
521
	{
522
		if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
523
524
		{
			/* this is synchronous for a minidriver */
525
526
527
528
			jtag_add_callback4(jtag_check_value_mask_callback, in_fields[i].in_value,
				(jtag_callback_data_t)in_fields[i].check_value,
				(jtag_callback_data_t)in_fields[i].check_mask,
				(jtag_callback_data_t)in_fields[i].num_bits);
529
		}
530
		if (in_fields[i].allocated)
531
		{
532
			free(in_fields[i].in_value);
533
		}
534
		if (in_fields[i].modified)
535
		{
536
			in_fields[i].in_value = NULL;
537
538
		}
	}
539
}
540

541
void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
542
{
oharboe's avatar
oharboe committed
543
544
	if (jtag_verify)
	{
545
		jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
546
547
	} else
	{
548
		jtag_add_dr_scan(in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
549
	}
550
551
}

552
553
554
555
556
557
558
559
560
561

/**
 * Generate a DR SCAN using the fields passed to the function
 *
 * For not bypassed TAPs the function checks in_fields and uses fields specified there.
 * For bypassed TAPs the function generates a dummy 1bit field.
 *
 * The bypass status of TAPs is set by jtag_add_ir_scan().
 *
 */
562
void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
563
564
565
566
567
{
	int retval;

	jtag_prelude(state);

568
	retval=interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
569
570
571
572
573
574
575
576
577
578
579
580
	if (retval!=ERROR_OK)
		jtag_error=retval;
}



/**
 * Duplicate the scan fields passed into the function into a DR SCAN command
 *
 * This function assumes that the caller handles extra fields for bypassed TAPs
 *
 */
581
void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
582
{
oharboe's avatar
oharboe committed
583
	int retval;
584

585
	jtag_prelude(state);
586

587
	retval=interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
588
589
	if (retval!=ERROR_OK)
		jtag_error=retval;
590
591
}

zwelch's avatar
zwelch committed
592
593
594
595
void jtag_add_dr_out(jtag_tap_t* tap,
		int num_fields, const int* num_bits, const u32* value,
		tap_state_t end_state)
{
596
597
	assert(end_state != TAP_INVALID);

598
	cmd_queue_cur_state = end_state;
zwelch's avatar
zwelch committed
599
600
601

	interface_jtag_add_dr_out(tap,
			num_fields, num_bits, value,
602
			end_state);
zwelch's avatar
zwelch committed
603
}
604

605
void jtag_add_tlr(void)
606
{
607
	jtag_prelude(TAP_RESET);
608

609
	int retval;
610
	retval=interface_jtag_add_tlr();
611
612
	if (retval!=ERROR_OK)
		jtag_error=retval;
613
614
	
	jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
oharboe's avatar
oharboe committed
615
616
}

617
void jtag_add_pathmove(int num_states, const tap_state_t *path)
618
{
619
	tap_state_t cur_state = cmd_queue_cur_state;
oharboe's avatar
oharboe committed
620
621
622
	int i;
	int retval;

623
	/* the last state has to be a stable state */
624
	if (!tap_is_state_stable(path[num_states - 1]))
625
	{
626
		LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
627
		exit(-1);
628
	}
629
630
631

	for (i=0; i<num_states; i++)
	{
632
		if (path[i] == TAP_RESET)
633
		{
634
			LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
635
636
			exit(-1);
		}
637

638
639
		if ( tap_state_transition(cur_state, true)  != path[i]
		  && tap_state_transition(cur_state, false) != path[i])
640
		{
641
			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[i]));
642
643
644
645
			exit(-1);
		}
		cur_state = path[i];
	}
646

647
	jtag_checks();
648

649
	retval = interface_jtag_add_pathmove(num_states, path);
650
	cmd_queue_cur_state = path[num_states - 1];
651
652
	if (retval!=ERROR_OK)
		jtag_error=retval;
653
654
}

655
void jtag_add_runtest(int num_cycles, tap_state_t state)
oharboe's avatar
oharboe committed
656
{
oharboe's avatar
oharboe committed
657
	int retval;
658

659
	jtag_prelude(state);
660

oharboe's avatar
oharboe committed
661
	/* executed by sw or hw fifo */
662
	retval=interface_jtag_add_runtest(num_cycles, state);
663
664
	if (retval!=ERROR_OK)
		jtag_error=retval;
665
666
}

667
668
669
670
671

void jtag_add_clocks( int num_cycles )
{
	int retval;

672
	if( !tap_is_state_stable(cmd_queue_cur_state) )
673
674
	{
		 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
675
				 tap_state_name(cmd_queue_cur_state) );
676
677
678
		 jtag_error = ERROR_JTAG_NOT_STABLE_STATE;
		 return;
	}
679

680
681
	if( num_cycles > 0 )
	{
682
		jtag_checks();
683

684
685
		retval = interface_jtag_add_clocks(num_cycles);
		if (retval != ERROR_OK)
686
687
			jtag_error=retval;
	}
688
689
}

690
void jtag_add_reset(int req_tlr_or_trst, int req_srst)
691
{
692
	int trst_with_tlr = 0;
oharboe's avatar
oharboe committed
693
	int retval;
694

695
696
	/* FIX!!! there are *many* different cases here. A better
	 * approach is needed for legal combinations of transitions...
oharboe's avatar
oharboe committed
697
	 */
698
	if ((jtag_reset_config & RESET_HAS_SRST)&&
699
			(jtag_reset_config & RESET_HAS_TRST)&&
700
			((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
701
702
703
704
705
706
	{
		if (((req_tlr_or_trst&&!jtag_trst)||
				(!req_tlr_or_trst&&jtag_trst))&&
				((req_srst&&!jtag_srst)||
						(!req_srst&&jtag_srst)))
		{
oharboe's avatar
oharboe committed
707
			/* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
708
			//LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
709
710
		}
	}
711

712
713
	/* Make sure that jtag_reset_config allows the requested reset */
	/* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
714
	if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
oharboe's avatar
oharboe committed
715
	{
716
717
718
		LOG_ERROR("BUG: requested reset would assert trst");
		jtag_error=ERROR_FAIL;
		return;
oharboe's avatar
oharboe committed
719
	}
720

721
	/* if TRST pulls SRST, we reset with TAP T-L-R */
722
	if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
723
	{
724
		trst_with_tlr = 1;
725
	}
726

727
728
	if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
	{
729
		LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
730
731
		jtag_error=ERROR_FAIL;
		return;
732
	}
733

734
	if (req_tlr_or_trst)
735
	{
736
		if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
737
738
739
740
		{
			jtag_trst = 1;
		} else
		{
741
			trst_with_tlr = 1;
742
743
744
745
		}
	} else
	{
		jtag_trst = 0;
746
	}
747

748
749
	jtag_srst = req_srst;

750
	retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
751
752
753
	if (retval!=ERROR_OK)
	{
		jtag_error=retval;
754
		return;
755
	}
756
	jtag_execute_queue();
757

758
759
	if (jtag_srst)
	{
760
		LOG_DEBUG("SRST line asserted");
761
762
763
	}
	else
	{
764
		LOG_DEBUG("SRST line released");
765
766
767
		if (jtag_nsrst_delay)
			jtag_add_sleep(jtag_nsrst_delay * 1000);
	}
768

769
	if (trst_with_tlr)
770
	{
771
		LOG_DEBUG("JTAG reset with RESET instead of TRST");
772
		jtag_set_end_state(TAP_RESET);
773
		jtag_add_tlr();
774
		return;
775
	}
776

777
	if (jtag_trst)
oharboe's avatar
oharboe committed
778
	{
779
780
781
		/* we just asserted nTRST, so we're now in Test-Logic-Reset,
		 * and inform possible listeners about this
		 */
782
		LOG_DEBUG("TRST line asserted");
783
		tap_set_state(TAP_RESET);
784
785
786
787
788
789
		jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
	}
	else
	{
		if (jtag_ntrst_delay)
			jtag_add_sleep(jtag_ntrst_delay * 1000);
oharboe's avatar
oharboe committed
790
791
792
	}
}

793
tap_state_t jtag_set_end_state(tap_state_t state)
794
{
795
	if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
796
	{
797
		LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
798
	}
799
800
801
802

	if (state!=TAP_INVALID)
		cmd_queue_end_state = state;
	return cmd_queue_end_state;
803
804
}

805
806
807
808
809
tap_state_t jtag_get_end_state(void)
{
	return cmd_queue_end_state;
}

810
void jtag_add_sleep(u32 us)
811
{
oharboe's avatar
oharboe committed
812
	keep_alive(); /* we might be running on a very slow JTAG clk */
813
814
815
816
	int retval=interface_jtag_add_sleep(us);
	if (retval!=ERROR_OK)
		jtag_error=retval;
	return;
817
818
}

819
int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
820
821
{
	int retval = ERROR_OK;
822

823
	int compare_failed = 0;
824

oharboe's avatar
oharboe committed
825
	if (in_check_mask)
826
		compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
827
	else
828
		compare_failed = buf_cmp(captured, in_check_value, num_bits);
829

830
	if (compare_failed){
831
832
833
		/* An error handler could have caught the failing check
		 * only report a problem when there wasn't a handler, or if the handler
		 * acknowledged the error
834
		 */
835
		/*
836
		LOG_WARNING("TAP %s:",
oharboe's avatar
oharboe committed
837
					jtag_tap_name(field->tap));
838
					*/
839
840
		if (compare_failed)
		{
841
			char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
842
			char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
843

844
			if (in_check_mask)
845
846
			{
				char *in_check_mask_char;
847
				in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
848
				LOG_WARNING("value captured during scan didn't pass the requested check:");
oharboe's avatar
oharboe committed
849
				LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
850
							captured_char, in_check_value_char, in_check_mask_char);
851
852
853
854
				free(in_check_mask_char);
			}
			else
			{
855
				LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
856
857
858
859
			}

			free(captured_char);
			free(in_check_value_char);
860

861
862
			retval = ERROR_JTAG_QUEUE_FAILED;
		}
863

864
865
866
867
	}
	return retval;
}

868
869
void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
{
870
	assert(field->in_value != NULL);
871
872
873
874
875
876
877

	if (value==NULL)
	{
		/* no checking to do */
		return;
	}

878
	jtag_execute_queue_noclear();
879

880
	int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
881
882
883
884
885
	jtag_set_error(retval);
}



zwelch's avatar
zwelch committed
886
887
int default_interface_jtag_execute_queue(void)
{
zwelch's avatar
zwelch committed
888
889
890
891
892
893
894
895
	if (NULL == jtag)
	{
		LOG_ERROR("No JTAG interface configured yet.  "
			"Issue 'init' command in startup scripts "
			"before communicating with targets.");
		return ERROR_FAIL;
	}

zwelch's avatar
zwelch committed
896
897
898
	return jtag->execute_queue();
}

oharboe's avatar
oharboe committed
899
void jtag_execute_queue_noclear(void)
oharboe's avatar
oharboe committed
900
{
901
902
903
	/* each flush can take as much as 1-2ms on high bandwidth low latency interfaces.
	 * E.g. a JTAG over TCP/IP or USB....
	 */
oharboe's avatar
oharboe committed
904
	jtag_flush_queue_count++;
905

oharboe's avatar
oharboe committed
906
	int retval=interface_jtag_execute_queue();
907
908
	/* we keep the first error */
	if ((jtag_error==ERROR_OK)&&(retval!=ERROR_OK))
oharboe's avatar
oharboe committed
909
	{
910
		jtag_error=retval;