jtag.c 67.4 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

/* maximum number of JTAG devices expected in the chain
 */
89
#define JTAG_MAX_CHAIN_SIZE 20
90
91
92
93

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

oharboe's avatar
   
oharboe committed
94
/* speed in kHz*/
oharboe's avatar
   
oharboe committed
95
static int speed_khz = 0;
oharboe's avatar
   
oharboe committed
96
/* flag if the kHz speed was defined */
zwelch's avatar
zwelch committed
97
static bool hasKHz = false;
oharboe's avatar
   
oharboe committed
98

99
100
/* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
 */
101

102
#if BUILD_ECOSBOARD == 1
103
	extern jtag_interface_t zy1000_interface;
104
#endif
105

106
107
108
#if BUILD_PARPORT == 1
	extern jtag_interface_t parport_interface;
#endif
109

110
111
112
#if BUILD_DUMMY == 1
	extern jtag_interface_t dummy_interface;
#endif
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
145
#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

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

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

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

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

162
jtag_interface_t *jtag_interfaces[] = {
163
#if BUILD_ECOSBOARD == 1
164
	&zy1000_interface,
165
#endif
166
167
168
#if BUILD_PARPORT == 1
	&parport_interface,
#endif
169
170
171
#if BUILD_DUMMY == 1
	&dummy_interface,
#endif
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#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,
195
196
197
#endif
#if BUILD_JLINK == 1
	&jlink_interface,
198
#endif
duane's avatar
duane committed
199
200
201
#if BUILD_VSLLINK == 1
	&vsllink_interface,
#endif
202
203
#if BUILD_RLINK == 1
	&rlink_interface,
204
205
206
#endif
#if BUILD_ARMJTAGEW == 1
	&armjtagew_interface,
207
208
209
210
#endif
	NULL,
};

211
struct jtag_interface_s *jtag = NULL;
212
213

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

217
/* jtag commands */
218
219
220
221
222
223
224
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);
225

226
static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
227

228
229
230
231
232
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);
233
static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
234

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

oharboe's avatar
oharboe committed
239
240
jtag_tap_t *jtag_AllTaps(void)
{
241
	return __jtag_all_taps;
242
243
};

244
int jtag_NumTotalTaps(void)
245
{
zwelch's avatar
zwelch committed
246
	return jtag_num_taps;
247
248
}

249
int jtag_NumEnabledTaps(void)
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
{
	jtag_tap_t *t;
	int n;

	n = 0;
	t = jtag_AllTaps();
	while(t){
		if( t->enabled ){
			n++;
		}
		t = t->next_tap;
	}
	return n;
}

zwelch's avatar
zwelch committed
265
266
267
268
269
/// 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++;

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

276
277
278
279
280
281
jtag_tap_t *jtag_TapByString( const char *s )
{
	jtag_tap_t *t;
	char *cp;

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

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

	cp = Jim_GetString( o, NULL );
	if(cp == NULL){
		cp = "(unknown)";
		t = NULL;
	}  else {
		t = jtag_TapByString( cp );
oharboe's avatar
oharboe committed
314
	}
315
316
317
318
319
320
321
	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 */
322
jtag_tap_t * jtag_TapByAbsPosition( int n )
323
324
325
326
327
328
{
	int orig_n;
	jtag_tap_t *t;

	orig_n = n;
	t = jtag_AllTaps();
oharboe's avatar
oharboe committed
329

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

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


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

347
348
349
350
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
351

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

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

364
365
366
367
368
369
	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;
370

371
372
373
374
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
375

376
377
378
379
380
381
382
383
384
385
	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;
	}
386

387
388
389
390
391
392
	return ERROR_OK;
}

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

394
	LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
395

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

402
403
404
	return ERROR_OK;
}

405
static void jtag_checks(void)
406
{
407
	assert(jtag_trst == 0);
408
409
}

410
static void jtag_prelude(tap_state_t state)
411
{
412
	jtag_checks();
413

414
415
	assert(state!=TAP_INVALID);

416
	cmd_queue_cur_state = state;
417
418
}

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

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

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

}


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

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

466
467
468
469
470
471
/**
 * 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
 *
 */
472
void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
473
{
oharboe's avatar
oharboe committed
474
	int retval;
475

476
	jtag_prelude(state);
477

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

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

488
489
490
491
492
493
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);
}
494
495
496
497
498
499
500
501

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

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

516
	jtag_add_scan(in_num_fields, in_fields, state);
517

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

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

550
551
552
553
554
555
556
557
558
559

/**
 * 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().
 *
 */
560
void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
561
562
563
564
565
{
	int retval;

	jtag_prelude(state);

566
	retval=interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
567
568
569
570
571
572
573
574
575
576
577
578
	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
 *
 */
579
void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
580
{
oharboe's avatar
oharboe committed
581
	int retval;
582

583
	jtag_prelude(state);
584

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

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

596
	cmd_queue_cur_state = end_state;
zwelch's avatar
zwelch committed
597
598
599

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

603
void jtag_add_tlr(void)
604
{
605
	jtag_prelude(TAP_RESET);
606

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

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

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

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

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

645
	jtag_checks();
646

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

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

657
	jtag_prelude(state);
658

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

665
666
667
668
669

void jtag_add_clocks( int num_cycles )
{
	int retval;

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

678
679
	if( num_cycles > 0 )
	{
680
		jtag_checks();
681

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

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

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

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

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

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

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

746
747
	jtag_srst = req_srst;

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

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

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

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

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

	if (state!=TAP_INVALID)
		cmd_queue_end_state = state;
	return cmd_queue_end_state;
801
802
}

803
804
805
806
807
tap_state_t jtag_get_end_state(void)
{
	return cmd_queue_end_state;
}

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

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

821
	int compare_failed = 0;
822

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

828
	if (compare_failed){
829
830
831
		/* 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
832
		 */
833
		/*
834
		LOG_WARNING("TAP %s:",
oharboe's avatar
oharboe committed
835
					jtag_tap_name(field->tap));
836
					*/
837
838
		if (compare_failed)
		{
839
			char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
840
			char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
841

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

			free(captured_char);
			free(in_check_value_char);
858

859
860
			retval = ERROR_JTAG_QUEUE_FAILED;
		}
861

862
863
864
865
	}
	return retval;
}

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

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

876
	jtag_execute_queue_noclear();
877

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



zwelch's avatar
zwelch committed
884
885
int default_interface_jtag_execute_queue(void)
{
zwelch's avatar
zwelch committed
886
887
888
889
890
891
892
893
	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
894
895
896
	return jtag->execute_queue();
}

oharboe's avatar
oharboe committed
897
void jtag_execute_queue_noclear(void)
oharboe's avatar
oharboe committed
898
{
899
900
901
	/* 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
902
	jtag_flush_queue_count++;
903

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

912
913
914
915
916
int jtag_get_flush_queue_count(void)
{
	return jtag_flush_queue_count;
}

917
918
919
920
921
int jtag_execute_queue(void)
{
	int retval;
	jtag_execute_queue_noclear();
	retval=jtag_error;
oharboe's avatar
oharboe committed
922
	jtag_error=ERROR_OK;
923
924
925
	return retval;
}

926
static int jtag_reset_callback(enum jtag_event event, void *priv)
927
{
928
	jtag_tap_t *tap = priv;
drath's avatar