jtag.c 67.2 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);
}

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

zwelch's avatar
zwelch committed
436
437
	int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
	jtag_set_error(retval);
438
439
440
}


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

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

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

zwelch's avatar
zwelch committed
482
483
484
	int retval = interface_jtag_add_plain_ir_scan(
			in_num_fields, in_fields, state);
	jtag_set_error(retval);
485
486
}

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

492
493
494
495
496
497
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);
}
498
499
500
501
502
503
504
505

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

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

520
	jtag_add_scan(in_num_fields, in_fields, state);
521

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

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

554
555

/**
zwelch's avatar
zwelch committed
556
557
558
559
 * Generate a DR SCAN using the fields passed to the function.
 * For connected TAPs, the function checks in_fields and uses fields
 * specified there.  For bypassed TAPs, the function generates a dummy
 * 1-bit field.  The bypass status of TAPs is set by jtag_add_ir_scan().
560
 */
zwelch's avatar
zwelch committed
561
562
void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
		tap_state_t state)
563
564
565
{
	jtag_prelude(state);

zwelch's avatar
zwelch committed
566
567
568
	int retval;
	retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
	jtag_set_error(retval);
569
570
571
}

/**
zwelch's avatar
zwelch committed
572
573
574
 * Duplicate the scan fields passed into the function into a DR SCAN
 * command.  Unlike jtag_add_dr_scan(), this function assumes that the
 * caller handles extra fields for bypassed TAPs.
575
 */
zwelch's avatar
zwelch committed
576
577
void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
		tap_state_t state)
578
{
579
	jtag_prelude(state);
580

zwelch's avatar
zwelch committed
581
582
583
	int retval;
	retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
	jtag_set_error(retval);
584
585
}

zwelch's avatar
zwelch committed
586
587
588
589
void jtag_add_dr_out(jtag_tap_t* tap,
		int num_fields, const int* num_bits, const u32* value,
		tap_state_t end_state)
{
590
591
	assert(end_state != TAP_INVALID);

592
	cmd_queue_cur_state = end_state;
zwelch's avatar
zwelch committed
593
594
595

	interface_jtag_add_dr_out(tap,
			num_fields, num_bits, value,
596
			end_state);
zwelch's avatar
zwelch committed
597
}
598

599
void jtag_add_tlr(void)
600
{
601
	jtag_prelude(TAP_RESET);
zwelch's avatar
zwelch committed
602
	jtag_set_error(interface_jtag_add_tlr());
603
	jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
oharboe's avatar
oharboe committed
604
605
}

606
void jtag_add_pathmove(int num_states, const tap_state_t *path)
607
{
608
	tap_state_t cur_state = cmd_queue_cur_state;
oharboe's avatar
oharboe committed
609

610
	/* the last state has to be a stable state */
611
	if (!tap_is_state_stable(path[num_states - 1]))
612
	{
613
		LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
614
		exit(-1);
615
	}
616

zwelch's avatar
zwelch committed
617
	for (int i = 0; i < num_states; i++)
618
	{
619
		if (path[i] == TAP_RESET)
620
		{
621
			LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
622
623
			exit(-1);
		}
624

625
626
		if ( tap_state_transition(cur_state, true)  != path[i]
		  && tap_state_transition(cur_state, false) != path[i])
627
		{
zwelch's avatar
zwelch committed
628
629
			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
					tap_state_name(cur_state), tap_state_name(path[i]));
630
631
632
633
			exit(-1);
		}
		cur_state = path[i];
	}
634

635
	jtag_checks();
636

zwelch's avatar
zwelch committed
637
	jtag_set_error(interface_jtag_add_pathmove(num_states, path));
638
	cmd_queue_cur_state = path[num_states - 1];
639
640
}

641
void jtag_add_runtest(int num_cycles, tap_state_t state)
oharboe's avatar
oharboe committed
642
{
643
	jtag_prelude(state);
zwelch's avatar
zwelch committed
644
	jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
645
646
}

647

zwelch's avatar
zwelch committed
648
void jtag_add_clocks(int num_cycles)
649
{
zwelch's avatar
zwelch committed
650
	if (!tap_is_state_stable(cmd_queue_cur_state))
651
	{
zwelch's avatar
zwelch committed
652
653
654
		 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
				 tap_state_name(cmd_queue_cur_state));
		 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
655
656
		 return;
	}
657

zwelch's avatar
zwelch committed
658
	if (num_cycles > 0)
659
	{
660
		jtag_checks();
zwelch's avatar
zwelch committed
661
		jtag_set_error(interface_jtag_add_clocks(num_cycles));
662
	}
663
664
}

665
void jtag_add_reset(int req_tlr_or_trst, int req_srst)
666
{
667
	int trst_with_tlr = 0;
668

669
670
	/* FIX!!! there are *many* different cases here. A better
	 * approach is needed for legal combinations of transitions...
oharboe's avatar
oharboe committed
671
	 */
672
	if ((jtag_reset_config & RESET_HAS_SRST)&&
673
			(jtag_reset_config & RESET_HAS_TRST)&&
674
			((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
675
676
677
678
679
680
	{
		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
681
			/* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
682
			//LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
683
684
		}
	}
685

686
687
	/* Make sure that jtag_reset_config allows the requested reset */
	/* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
688
	if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
oharboe's avatar
oharboe committed
689
	{
690
		LOG_ERROR("BUG: requested reset would assert trst");
zwelch's avatar
zwelch committed
691
		jtag_set_error(ERROR_FAIL);
692
		return;
oharboe's avatar
oharboe committed
693
	}
694

695
	/* if TRST pulls SRST, we reset with TAP T-L-R */
696
	if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
697
	{
698
		trst_with_tlr = 1;
699
	}
700

701
702
	if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
	{
703
		LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
zwelch's avatar
zwelch committed
704
		jtag_set_error(ERROR_FAIL);
705
		return;
706
	}
707

708
	if (req_tlr_or_trst)
709
	{
710
		if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
711
712
713
714
		{
			jtag_trst = 1;
		} else
		{
715
			trst_with_tlr = 1;
716
717
718
719
		}
	} else
	{
		jtag_trst = 0;
720
	}
721

722
723
	jtag_srst = req_srst;

zwelch's avatar
zwelch committed
724
725
	int retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
	if (retval != ERROR_OK)
726
	{
zwelch's avatar
zwelch committed
727
		jtag_set_error(retval);
728
		return;
729
	}
730
	jtag_execute_queue();
731

732
733
	if (jtag_srst)
	{
734
		LOG_DEBUG("SRST line asserted");
735
736
737
	}
	else
	{
738
		LOG_DEBUG("SRST line released");
739
740
741
		if (jtag_nsrst_delay)
			jtag_add_sleep(jtag_nsrst_delay * 1000);
	}
742

743
	if (trst_with_tlr)
744
	{
745
		LOG_DEBUG("JTAG reset with RESET instead of TRST");
746
		jtag_set_end_state(TAP_RESET);
747
		jtag_add_tlr();
748
		return;
749
	}
750

751
	if (jtag_trst)
oharboe's avatar
oharboe committed
752
	{
753
754
755
		/* we just asserted nTRST, so we're now in Test-Logic-Reset,
		 * and inform possible listeners about this
		 */
756
		LOG_DEBUG("TRST line asserted");
757
		tap_set_state(TAP_RESET);
758
759
760
761
762
763
		jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
	}
	else
	{
		if (jtag_ntrst_delay)
			jtag_add_sleep(jtag_ntrst_delay * 1000);
oharboe's avatar
oharboe committed
764
765
766
	}
}

767
tap_state_t jtag_set_end_state(tap_state_t state)
768
{
769
	if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
770
	{
771
		LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
772
	}
773
774
775
776

	if (state!=TAP_INVALID)
		cmd_queue_end_state = state;
	return cmd_queue_end_state;
777
778
}

779
780
781
782
783
tap_state_t jtag_get_end_state(void)
{
	return cmd_queue_end_state;
}

784
void jtag_add_sleep(u32 us)
785
{
zwelch's avatar
zwelch committed
786
787
788
	/// @todo Here, keep_alive() appears to be a layering violation!!!
	keep_alive();
	jtag_set_error(interface_jtag_add_sleep(us));
789
790
}

791
int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
792
793
{
	int retval = ERROR_OK;
794

795
	int compare_failed = 0;
796

oharboe's avatar
oharboe committed
797
	if (in_check_mask)
798
		compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
799
	else
800
		compare_failed = buf_cmp(captured, in_check_value, num_bits);
801

802
	if (compare_failed){
803
804
805
		/* 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
806
		 */
807
		/*
808
		LOG_WARNING("TAP %s:",
oharboe's avatar
oharboe committed
809
					jtag_tap_name(field->tap));
810
					*/
811
812
		if (compare_failed)
		{
813
			char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
814
			char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
815

816
			if (in_check_mask)
817
818
			{
				char *in_check_mask_char;
819
				in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
820
				LOG_WARNING("value captured during scan didn't pass the requested check:");
oharboe's avatar
oharboe committed
821
				LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
822
							captured_char, in_check_value_char, in_check_mask_char);
823
824
825
826
				free(in_check_mask_char);
			}
			else
			{
827
				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);
828
829
830
831
			}

			free(captured_char);
			free(in_check_value_char);
832

833
834
			retval = ERROR_JTAG_QUEUE_FAILED;
		}
835

836
837
838
839
	}
	return retval;
}

840
841
void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
{
842
	assert(field->in_value != NULL);
843
844
845
846
847
848
849

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

850
	jtag_execute_queue_noclear();
851

852
	int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
853
854
855
856
857
	jtag_set_error(retval);
}



zwelch's avatar
zwelch committed
858
859
int default_interface_jtag_execute_queue(void)
{
zwelch's avatar
zwelch committed
860
861
862
863
864
865
866
867
	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
868
869
870
	return jtag->execute_queue();
}

oharboe's avatar
oharboe committed
871
void jtag_execute_queue_noclear(void)
oharboe's avatar
oharboe committed
872
{
oharboe's avatar
oharboe committed
873
	jtag_flush_queue_count++;
zwelch's avatar
zwelch committed
874
	jtag_set_error(interface_jtag_execute_queue());
875
876
}

877
878
879
880
881
int jtag_get_flush_queue_count(void)
{
	return jtag_flush_queue_count;
}

882
883
884
int jtag_execute_queue(void)
{
	jtag_execute_queue_noclear();
zwelch's avatar
zwelch committed
885
	return jtag_error_clear();
886
887
}

888
static int jtag_reset_callback(enum jtag_event event, void *priv)
889
{
890
	jtag_tap_t *tap = priv;
891

892
	LOG_DEBUG("-");
893

894
895
	if (event == JTAG_TRST_ASSERTED)
	{