jtag.c 67.6 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
#elif defined(BUILD_MINIDRIVER_DUMMY)
101
	extern jtag_interface_t minidummy_interface;
102
#else // standard drivers
103
104
105
#if BUILD_PARPORT == 1
	extern jtag_interface_t parport_interface;
#endif
106

107
108
109
#if BUILD_DUMMY == 1
	extern jtag_interface_t dummy_interface;
#endif
110

111
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
#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

143
144
145
146
#if BUILD_JLINK == 1
	extern jtag_interface_t jlink_interface;
#endif

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

151
152
153
154
#if BUILD_RLINK == 1
	extern jtag_interface_t rlink_interface;
#endif

155
156
157
#if BUILD_ARMJTAGEW == 1
	extern jtag_interface_t armjtagew_interface;
#endif
158
#endif // standard drivers
159

160
161
162
163
164
165
166
/**
 * The list of built-in JTAG interfaces, containing entries for those
 * drivers that were enabled by the @c configure script.
 *
 * The list should be defined to contain either one minidriver interface
 * or some number of standard driver interfaces, never both.
 */
167
jtag_interface_t *jtag_interfaces[] = {
168
#if BUILD_ECOSBOARD == 1
169
	&zy1000_interface,
170
#elif defined(BUILD_MINIDRIVER_DUMMY)
171
	&minidummy_interface,
172
#else // standard drivers
173
174
175
#if BUILD_PARPORT == 1
	&parport_interface,
#endif
176
177
178
#if BUILD_DUMMY == 1
	&dummy_interface,
#endif
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#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,
202
203
204
#endif
#if BUILD_JLINK == 1
	&jlink_interface,
205
#endif
duane's avatar
duane committed
206
207
208
#if BUILD_VSLLINK == 1
	&vsllink_interface,
#endif
209
210
#if BUILD_RLINK == 1
	&rlink_interface,
211
212
213
#endif
#if BUILD_ARMJTAGEW == 1
	&armjtagew_interface,
214
#endif
215
#endif // standard drivers
216
217
218
	NULL,
};

219
struct jtag_interface_s *jtag = NULL;
220
221

/* configuration */
222
static jtag_interface_t *jtag_interface = NULL;
223
int jtag_speed = 0;
oharboe's avatar
   
oharboe committed
224

225
/* jtag commands */
226
227
228
229
230
231
232
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);
233

234
static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
235

236
237
238
239
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);
240
static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
241

242
static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
oharboe's avatar
oharboe committed
243
static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
244
static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245

246
jtag_tap_t *jtag_all_taps(void)
oharboe's avatar
oharboe committed
247
{
248
	return __jtag_all_taps;
249
250
};

251
int jtag_tap_count(void)
252
{
zwelch's avatar
zwelch committed
253
	return jtag_num_taps;
254
}
255

256
unsigned jtag_tap_count_enabled(void)
257
258
{
	jtag_tap_t *t;
259
	unsigned n;
260
261

	n = 0;
262
	t = jtag_all_taps();
263
264
265
266
267
268
269
270
271
	while(t){
		if( t->enabled ){
			n++;
		}
		t = t->next_tap;
	}
	return n;
}

zwelch's avatar
zwelch committed
272
273
274
275
276
/// 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++;

277
	jtag_tap_t **tap = &__jtag_all_taps;
zwelch's avatar
zwelch committed
278
279
280
281
282
	while(*tap != NULL)
		tap = &(*tap)->next_tap;
	*tap = t;
}

283
jtag_tap_t *jtag_tap_by_string( const char *s )
284
285
286
287
{
	jtag_tap_t *t;
	char *cp;

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

310
jtag_tap_t * jtag_tap_by_jim_obj( Jim_Interp *interp, Jim_Obj *o )
311
312
313
314
315
316
317
318
319
{
	jtag_tap_t *t;
	const char *cp;

	cp = Jim_GetString( o, NULL );
	if(cp == NULL){
		cp = "(unknown)";
		t = NULL;
	}  else {
320
		t = jtag_tap_by_string( cp );
oharboe's avatar
oharboe committed
321
	}
322
323
324
325
326
327
328
	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 */
329
jtag_tap_t * jtag_tap_by_abs_position( int n )
330
331
332
333
334
{
	int orig_n;
	jtag_tap_t *t;

	orig_n = n;
335
	t = jtag_all_taps();
oharboe's avatar
oharboe committed
336

337
338
339
	while( t && (n > 0)) {
		n--;
		t = t->next_tap;
340
	}
341
342
343
	return t;
}

zwelch's avatar
zwelch committed
344
345
346
347
348
349
const char *jtag_tap_name(const jtag_tap_t *tap)
{
	return (tap == NULL) ? "(unknown)" : tap->dotted_name;
}


350
351
352
int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
{
	jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
353

354
355
356
357
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
358

359
360
361
362
363
364
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
365

366
367
368
369
	(*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->priv = priv;
	(*callbacks_p)->next = NULL;
370

371
372
373
374
375
376
	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;
377

378
379
380
381
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
382

383
384
385
386
387
388
389
390
391
392
	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;
	}
393

394
395
396
397
398
399
	return ERROR_OK;
}

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

401
	LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
402

403
404
405
406
407
	while (callback)
	{
		callback->callback(event, callback->priv);
		callback = callback->next;
	}
408

409
410
411
	return ERROR_OK;
}

412
static void jtag_checks(void)
413
{
414
	assert(jtag_trst == 0);
415
416
}

417
static void jtag_prelude(tap_state_t state)
418
{
419
	jtag_checks();
420

421
422
	assert(state!=TAP_INVALID);

423
	cmd_queue_cur_state = state;
424
425
}

zwelch's avatar
zwelch committed
426
427
428
429
430
void jtag_alloc_in_value32(scan_field_t *field)
{
	interface_jtag_alloc_in_value32(field);
}

431
void jtag_add_ir_scan_noverify(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
432
{
oharboe's avatar
oharboe committed
433
	int retval;
434
	jtag_prelude(state);
435

436
	retval=interface_jtag_add_ir_scan(in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
437
438
	if (retval!=ERROR_OK)
		jtag_error=retval;
439
440
441
442

}


443
444
445
446
447
448
449
450
451
/**
 * 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.
 *
 */
452
void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
453
{
oharboe's avatar
oharboe committed
454
	if (jtag_verify&&jtag_verify_capture_ir)
455
	{
zwelch's avatar
zwelch committed
456
		/* 8 x 32 bit id's is enough for all invocations */
457
458

		for (int j = 0; j < in_num_fields; j++)
459
		{
oharboe's avatar
oharboe committed
460
461
462
			/* 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.
			 */
463
464
			in_fields[j].check_value=in_fields[j].tap->expected;
			in_fields[j].check_mask=in_fields[j].tap->expected_mask;
465
		}
466
		jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
467
468
	} else
	{
469
		jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
470
	}
oharboe's avatar
oharboe committed
471
472
}

473
474
475
476
477
478
/**
 * 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
 *
 */
479
void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
480
{
oharboe's avatar
oharboe committed
481
	int retval;
482

483
	jtag_prelude(state);
484

485
	retval=interface_jtag_add_plain_ir_scan(in_num_fields, in_fields, state);
486
487
	if (retval!=ERROR_OK)
		jtag_error=retval;
488
489
}

490
491
492
493
void jtag_add_callback(jtag_callback1_t f, u8 *in)
{
	interface_jtag_add_callback(f, in);
}
494

495
496
497
498
499
500
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);
}
501
502
503
504
505
506
507
508

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);
}

509
static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
510
		int in_num_fields, scan_field_t *in_fields, tap_state_t state)
511
{
512
	for (int i = 0; i < in_num_fields; i++)
513
	{
514
515
516
517
518
519
520
		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;
521
522
	}

523
	jtag_add_scan(in_num_fields, in_fields, state);
524

525
	for (int i = 0; i < in_num_fields; i++)
526
	{
527
		if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
528
529
		{
			/* this is synchronous for a minidriver */
530
531
532
533
			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);
534
		}
535
		if (in_fields[i].allocated)
536
		{
537
			free(in_fields[i].in_value);
538
		}
539
		if (in_fields[i].modified)
540
		{
541
			in_fields[i].in_value = NULL;
542
543
		}
	}
544
}
545

546
void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
547
{
oharboe's avatar
oharboe committed
548
549
	if (jtag_verify)
	{
550
		jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
551
552
	} else
	{
553
		jtag_add_dr_scan(in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
554
	}
555
556
}

557
558
559
560
561
562
563
564
565
566

/**
 * 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().
 *
 */
567
void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
568
569
570
571
572
{
	int retval;

	jtag_prelude(state);

573
	retval=interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
574
575
576
577
578
579
580
581
582
583
584
585
	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
 *
 */
586
void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
587
{
oharboe's avatar
oharboe committed
588
	int retval;
589

590
	jtag_prelude(state);
591

592
	retval=interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
593
594
	if (retval!=ERROR_OK)
		jtag_error=retval;
595
596
}

zwelch's avatar
zwelch committed
597
598
599
600
void jtag_add_dr_out(jtag_tap_t* tap,
		int num_fields, const int* num_bits, const u32* value,
		tap_state_t end_state)
{
601
602
	assert(end_state != TAP_INVALID);

603
	cmd_queue_cur_state = end_state;
zwelch's avatar
zwelch committed
604
605
606

	interface_jtag_add_dr_out(tap,
			num_fields, num_bits, value,
607
			end_state);
zwelch's avatar
zwelch committed
608
}
609

610
void jtag_add_tlr(void)
611
{
612
	jtag_prelude(TAP_RESET);
613

614
	int retval;
615
	retval=interface_jtag_add_tlr();
616
617
	if (retval!=ERROR_OK)
		jtag_error=retval;
618
619
	
	jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
oharboe's avatar
oharboe committed
620
621
}

622
void jtag_add_pathmove(int num_states, const tap_state_t *path)
623
{
624
	tap_state_t cur_state = cmd_queue_cur_state;
oharboe's avatar
oharboe committed
625
626
627
	int i;
	int retval;

628
	/* the last state has to be a stable state */
629
	if (!tap_is_state_stable(path[num_states - 1]))
630
	{
631
		LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
632
		exit(-1);
633
	}
634
635
636

	for (i=0; i<num_states; i++)
	{
637
		if (path[i] == TAP_RESET)
638
		{
639
			LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
640
641
			exit(-1);
		}
642

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

652
	jtag_checks();
653

654
	retval = interface_jtag_add_pathmove(num_states, path);
655
	cmd_queue_cur_state = path[num_states - 1];
656
657
	if (retval!=ERROR_OK)
		jtag_error=retval;
658
659
}

660
void jtag_add_runtest(int num_cycles, tap_state_t state)
oharboe's avatar
oharboe committed
661
{
oharboe's avatar
oharboe committed
662
	int retval;
663

664
	jtag_prelude(state);
665

oharboe's avatar
oharboe committed
666
	/* executed by sw or hw fifo */
667
	retval=interface_jtag_add_runtest(num_cycles, state);
668
669
	if (retval!=ERROR_OK)
		jtag_error=retval;
670
671
}

672
673
674
675
676

void jtag_add_clocks( int num_cycles )
{
	int retval;

677
	if( !tap_is_state_stable(cmd_queue_cur_state) )
678
679
	{
		 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
680
				 tap_state_name(cmd_queue_cur_state) );
681
682
683
		 jtag_error = ERROR_JTAG_NOT_STABLE_STATE;
		 return;
	}
684

685
686
	if( num_cycles > 0 )
	{
687
		jtag_checks();
688

689
690
		retval = interface_jtag_add_clocks(num_cycles);
		if (retval != ERROR_OK)
691
692
			jtag_error=retval;
	}
693
694
}

695
void jtag_add_reset(int req_tlr_or_trst, int req_srst)
696
{
697
	int trst_with_tlr = 0;
oharboe's avatar
oharboe committed
698
	int retval;
699

700
701
	/* FIX!!! there are *many* different cases here. A better
	 * approach is needed for legal combinations of transitions...
oharboe's avatar
oharboe committed
702
	 */
703
	if ((jtag_reset_config & RESET_HAS_SRST)&&
704
			(jtag_reset_config & RESET_HAS_TRST)&&
705
			((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
706
707
708
709
710
711
	{
		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
712
			/* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
713
			//LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
714
715
		}
	}
716

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

726
	/* if TRST pulls SRST, we reset with TAP T-L-R */
727
	if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
728
	{
729
		trst_with_tlr = 1;
730
	}
731

732
733
	if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
	{
734
		LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
735
736
		jtag_error=ERROR_FAIL;
		return;
737
	}
738

739
	if (req_tlr_or_trst)
740
	{
741
		if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
742
743
744
745
		{
			jtag_trst = 1;
		} else
		{
746
			trst_with_tlr = 1;
747
748
749
750
		}
	} else
	{
		jtag_trst = 0;
751
	}
752

753
754
	jtag_srst = req_srst;

755
	retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
756
757
758
	if (retval!=ERROR_OK)
	{
		jtag_error=retval;
759
		return;
760
	}
761
	jtag_execute_queue();
762

763
764
	if (jtag_srst)
	{
765
		LOG_DEBUG("SRST line asserted");
766
767
768
	}
	else
	{
769
		LOG_DEBUG("SRST line released");
770
771
772
		if (jtag_nsrst_delay)
			jtag_add_sleep(jtag_nsrst_delay * 1000);
	}
773

774
	if (trst_with_tlr)
775
	{
776
		LOG_DEBUG("JTAG reset with RESET instead of TRST");
777
		jtag_set_end_state(TAP_RESET);
778
		jtag_add_tlr();
779
		return;
780
	}
781

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

798
tap_state_t jtag_set_end_state(tap_state_t state)
799
{
800
	if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
801
	{
802
		LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
803
	}
804
805
806
807

	if (state!=TAP_INVALID)
		cmd_queue_end_state = state;
	return cmd_queue_end_state;
808
809
}

810
811
812
813
814
tap_state_t jtag_get_end_state(void)
{
	return cmd_queue_end_state;
}

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

824
int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
825
826
{
	int retval = ERROR_OK;
827

828
	int compare_failed = 0;
829

oharboe's avatar
oharboe committed
830
	if (in_check_mask)
831
		compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
832
	else
833
		compare_failed = buf_cmp(captured, in_check_value, num_bits);
834

835
	if (compare_failed){
836
837
838
		/* 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
839
		 */
840
		/*
841
		LOG_WARNING("TAP %s:",
oharboe's avatar
oharboe committed
842
					jtag_tap_name(field->tap));
843
					*/
844
845
		if (compare_failed)
		{
846
			char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
847
			char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
848

849
			if (in_check_mask)
850
851
			{
				char *in_check_mask_char;
852
				in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
853
				LOG_WARNING("value captured during scan didn't pass the requested check:");
oharboe's avatar
oharboe committed
854
				LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
855
							captured_char, in_check_value_char, in_check_mask_char);
856
857
858
859
				free(in_check_mask_char);
			}
			else
			{
860
				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);
861
862
863
864
			}

			free(captured_char);
			free(in_check_value_char);
865

866
867
			retval = ERROR_JTAG_QUEUE_FAILED;
		}
868

869
870
871
872
	}
	return retval;
}

873
874
void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
{
875
	assert(field->in_value != NULL);
876
877
878
879
880
881
882

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

883
	jtag_execute_queue_noclear();
884

885
	int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
886
887
888
889
890
	jtag_set_error(retval);
}



zwelch's avatar
zwelch committed
891
892
int default_interface_jtag_execute_queue(void)
{
zwelch's avatar
zwelch committed
893
894
895
896
897
898
899
900
	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
901
902
903
	return jtag->execute_queue();
}

oharboe's avatar
oharboe committed
904
void jtag_execute_queue_noclear(void)
oharboe's avatar
oharboe committed
905
{
906
907
908
	/* 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
909
	jtag_flush_queue_count++;