jtag.c 65.5 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
 * assigned chain position to new TAPs
 */
74
static unsigned 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
static int handle_interface_list_command(struct command_context_s *cmd_ctx,
		char *cmd, char **args, int argc);
228
229
230
231
232
233
234
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);
235

236
static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
237

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

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

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

253
unsigned jtag_tap_count(void)
254
{
zwelch's avatar
zwelch committed
255
	return jtag_num_taps;
256
}
257

258
unsigned jtag_tap_count_enabled(void)
259
{
zwelch's avatar
zwelch committed
260
261
262
263
264
	jtag_tap_t *t = jtag_all_taps();
	unsigned n = 0;
	while(t)
	{
		if (t->enabled)
265
266
267
268
269
270
			n++;
		t = t->next_tap;
	}
	return n;
}

zwelch's avatar
zwelch committed
271
/// Append a new TAP to the chain of all taps.
272
void jtag_tap_add(struct jtag_tap_s *t)
zwelch's avatar
zwelch committed
273
274
275
{
	t->abs_chain_position = jtag_num_taps++;

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

282
jtag_tap_t *jtag_tap_by_string(const char *s)
283
{
284
285
286
287
288
289
290
291
292
293
	/* try by name first */
	jtag_tap_t *t = jtag_all_taps();
	while (t)
	{
		if (0 == strcmp(t->dotted_name, s))
			return t;
		t = t->next_tap;
	}

	/* no tap found by name, so try to parse the name as a number */
294
	char *cp;
295
296
297
	unsigned n = strtoul(s, &cp, 0);
	if ((s == cp) || (*cp != 0))
		return NULL;
298

299
	return jtag_tap_by_position(n);
300
301
}

zwelch's avatar
zwelch committed
302
jtag_tap_t *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
303
{
zwelch's avatar
zwelch committed
304
305
306
	const char *cp = Jim_GetString(o, NULL);
	jtag_tap_t *t = cp ? jtag_tap_by_string(cp) : NULL;
	if (NULL == cp)
307
		cp = "(unknown)";
zwelch's avatar
zwelch committed
308
309
	if (NULL == t)
		Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
310
311
312
313
	return t;
}

/* returns a pointer to the n-th device in the scan chain */
314
jtag_tap_t *jtag_tap_by_position(unsigned n)
315
{
zwelch's avatar
zwelch committed
316
	jtag_tap_t *t = jtag_all_taps();
oharboe's avatar
oharboe committed
317

zwelch's avatar
zwelch committed
318
	while (t && n-- > 0)
319
		t = t->next_tap;
zwelch's avatar
zwelch committed
320

321
322
323
	return t;
}

zwelch's avatar
zwelch committed
324
325
326
327
328
329
const char *jtag_tap_name(const jtag_tap_t *tap)
{
	return (tap == NULL) ? "(unknown)" : tap->dotted_name;
}


330
331
332
int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
{
	jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
333

334
335
336
337
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
338

339
340
341
342
343
344
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
345

346
347
348
349
	(*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->priv = priv;
	(*callbacks_p)->next = NULL;
350

351
352
353
354
355
356
	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;
357

358
359
360
361
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
362

363
364
365
366
367
368
369
370
371
372
	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;
	}
373

374
375
376
377
378
379
	return ERROR_OK;
}

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

381
	LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
382

383
384
385
386
387
	while (callback)
	{
		callback->callback(event, callback->priv);
		callback = callback->next;
	}
388

389
390
391
	return ERROR_OK;
}

392
static void jtag_checks(void)
393
{
394
	assert(jtag_trst == 0);
395
396
}

397
static void jtag_prelude(tap_state_t state)
398
{
399
	jtag_checks();
400

401
402
	assert(state!=TAP_INVALID);

403
	cmd_queue_cur_state = state;
404
405
}

zwelch's avatar
zwelch committed
406
407
408
409
410
void jtag_alloc_in_value32(scan_field_t *field)
{
	interface_jtag_alloc_in_value32(field);
}

zwelch's avatar
zwelch committed
411
412
void jtag_add_ir_scan_noverify(int in_count, const scan_field_t *in_fields,
		tap_state_t state)
413
414
{
	jtag_prelude(state);
415

zwelch's avatar
zwelch committed
416
417
	int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
	jtag_set_error(retval);
418
419
420
}


421
void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
422
{
oharboe's avatar
oharboe committed
423
	if (jtag_verify&&jtag_verify_capture_ir)
424
	{
zwelch's avatar
zwelch committed
425
		/* 8 x 32 bit id's is enough for all invocations */
426
427

		for (int j = 0; j < in_num_fields; j++)
428
		{
oharboe's avatar
oharboe committed
429
430
431
			/* 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.
			 */
432
433
			in_fields[j].check_value=in_fields[j].tap->expected;
			in_fields[j].check_mask=in_fields[j].tap->expected_mask;
434
		}
435
		jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
436
437
	} else
	{
438
		jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
439
	}
oharboe's avatar
oharboe committed
440
441
}

zwelch's avatar
zwelch committed
442
443
void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields,
		tap_state_t state)
444
{
445
	jtag_prelude(state);
446

zwelch's avatar
zwelch committed
447
448
449
	int retval = interface_jtag_add_plain_ir_scan(
			in_num_fields, in_fields, state);
	jtag_set_error(retval);
450
451
}

452
453
454
455
void jtag_add_callback(jtag_callback1_t f, u8 *in)
{
	interface_jtag_add_callback(f, in);
}
456

457
458
459
460
461
462
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);
}
463
464
465
466
467
468
469
470

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

471
static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
472
		int in_num_fields, scan_field_t *in_fields, tap_state_t state)
473
{
474
	for (int i = 0; i < in_num_fields; i++)
475
	{
476
477
478
479
480
481
482
		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;
483
484
	}

485
	jtag_add_scan(in_num_fields, in_fields, state);
486

487
	for (int i = 0; i < in_num_fields; i++)
488
	{
489
		if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
490
491
		{
			/* this is synchronous for a minidriver */
492
493
494
495
			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);
496
		}
497
		if (in_fields[i].allocated)
498
		{
499
			free(in_fields[i].in_value);
500
		}
501
		if (in_fields[i].modified)
502
		{
503
			in_fields[i].in_value = NULL;
504
505
		}
	}
506
}
507

508
void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
509
{
oharboe's avatar
oharboe committed
510
511
	if (jtag_verify)
	{
512
		jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
513
514
	} else
	{
515
		jtag_add_dr_scan(in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
516
	}
517
518
}

519

zwelch's avatar
zwelch committed
520
521
void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
		tap_state_t state)
522
523
524
{
	jtag_prelude(state);

zwelch's avatar
zwelch committed
525
526
527
	int retval;
	retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
	jtag_set_error(retval);
528
529
}

zwelch's avatar
zwelch committed
530
531
void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
		tap_state_t state)
532
{
533
	jtag_prelude(state);
534

zwelch's avatar
zwelch committed
535
536
537
	int retval;
	retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
	jtag_set_error(retval);
538
539
}

zwelch's avatar
zwelch committed
540
541
542
543
void jtag_add_dr_out(jtag_tap_t* tap,
		int num_fields, const int* num_bits, const u32* value,
		tap_state_t end_state)
{
544
545
	assert(end_state != TAP_INVALID);

546
	cmd_queue_cur_state = end_state;
zwelch's avatar
zwelch committed
547
548
549

	interface_jtag_add_dr_out(tap,
			num_fields, num_bits, value,
550
			end_state);
zwelch's avatar
zwelch committed
551
}
552

553
void jtag_add_tlr(void)
554
{
555
	jtag_prelude(TAP_RESET);
zwelch's avatar
zwelch committed
556
	jtag_set_error(interface_jtag_add_tlr());
557
	jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
oharboe's avatar
oharboe committed
558
559
}

560
void jtag_add_pathmove(int num_states, const tap_state_t *path)
561
{
562
	tap_state_t cur_state = cmd_queue_cur_state;
oharboe's avatar
oharboe committed
563

564
	/* the last state has to be a stable state */
565
	if (!tap_is_state_stable(path[num_states - 1]))
566
	{
567
		LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
568
569
		jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
		return;
570
	}
571

zwelch's avatar
zwelch committed
572
	for (int i = 0; i < num_states; i++)
573
	{
574
		if (path[i] == TAP_RESET)
575
		{
576
			LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
577
578
			jtag_set_error(ERROR_JTAG_STATE_INVALID);
			return;
579
		}
580

581
582
		if ( tap_state_transition(cur_state, true)  != path[i]
		  && tap_state_transition(cur_state, false) != path[i])
583
		{
zwelch's avatar
zwelch committed
584
585
			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
					tap_state_name(cur_state), tap_state_name(path[i]));
586
587
			jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
			return;
588
589
590
		}
		cur_state = path[i];
	}
591

592
	jtag_checks();
593

zwelch's avatar
zwelch committed
594
	jtag_set_error(interface_jtag_add_pathmove(num_states, path));
595
	cmd_queue_cur_state = path[num_states - 1];
596
597
}

598
void jtag_add_runtest(int num_cycles, tap_state_t state)
oharboe's avatar
oharboe committed
599
{
600
	jtag_prelude(state);
zwelch's avatar
zwelch committed
601
	jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
602
603
}

604

zwelch's avatar
zwelch committed
605
void jtag_add_clocks(int num_cycles)
606
{
zwelch's avatar
zwelch committed
607
	if (!tap_is_state_stable(cmd_queue_cur_state))
608
	{
zwelch's avatar
zwelch committed
609
610
611
		 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);
612
613
		 return;
	}
614

zwelch's avatar
zwelch committed
615
	if (num_cycles > 0)
616
	{
617
		jtag_checks();
zwelch's avatar
zwelch committed
618
		jtag_set_error(interface_jtag_add_clocks(num_cycles));
619
	}
620
621
}

622
void jtag_add_reset(int req_tlr_or_trst, int req_srst)
623
{
624
	int trst_with_tlr = 0;
625

626
627
	/* FIX!!! there are *many* different cases here. A better
	 * approach is needed for legal combinations of transitions...
oharboe's avatar
oharboe committed
628
	 */
629
	if ((jtag_reset_config & RESET_HAS_SRST)&&
630
			(jtag_reset_config & RESET_HAS_TRST)&&
631
			((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
632
633
634
635
636
637
	{
		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
638
			/* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
639
			//LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
640
641
		}
	}
642

643
644
	/* Make sure that jtag_reset_config allows the requested reset */
	/* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
645
	if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
oharboe's avatar
oharboe committed
646
	{
647
		LOG_ERROR("BUG: requested reset would assert trst");
zwelch's avatar
zwelch committed
648
		jtag_set_error(ERROR_FAIL);
649
		return;
oharboe's avatar
oharboe committed
650
	}
651

652
	/* if TRST pulls SRST, we reset with TAP T-L-R */
653
	if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
654
	{
655
		trst_with_tlr = 1;
656
	}
657

658
659
	if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
	{
660
		LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
zwelch's avatar
zwelch committed
661
		jtag_set_error(ERROR_FAIL);
662
		return;
663
	}
664

665
	if (req_tlr_or_trst)
666
	{
667
		if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
668
669
670
671
		{
			jtag_trst = 1;
		} else
		{
672
			trst_with_tlr = 1;
673
674
675
676
		}
	} else
	{
		jtag_trst = 0;
677
	}
678

679
680
	jtag_srst = req_srst;

zwelch's avatar
zwelch committed
681
682
	int retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
	if (retval != ERROR_OK)
683
	{
zwelch's avatar
zwelch committed
684
		jtag_set_error(retval);
685
		return;
686
	}
687
	jtag_execute_queue();
688

689
690
	if (jtag_srst)
	{
691
		LOG_DEBUG("SRST line asserted");
692
693
694
	}
	else
	{
695
		LOG_DEBUG("SRST line released");
696
697
698
		if (jtag_nsrst_delay)
			jtag_add_sleep(jtag_nsrst_delay * 1000);
	}
699

700
	if (trst_with_tlr)
701
	{
702
		LOG_DEBUG("JTAG reset with RESET instead of TRST");
703
		jtag_set_end_state(TAP_RESET);
704
		jtag_add_tlr();
705
		return;
706
	}
707

708
	if (jtag_trst)
oharboe's avatar
oharboe committed
709
	{
710
711
712
		/* we just asserted nTRST, so we're now in Test-Logic-Reset,
		 * and inform possible listeners about this
		 */
713
		LOG_DEBUG("TRST line asserted");
714
		tap_set_state(TAP_RESET);
715
716
717
718
719
720
		jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
	}
	else
	{
		if (jtag_ntrst_delay)
			jtag_add_sleep(jtag_ntrst_delay * 1000);
oharboe's avatar
oharboe committed
721
722
723
	}
}

724
tap_state_t jtag_set_end_state(tap_state_t state)
725
{
726
	if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
727
	{
728
		LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
729
	}
730
731
732
733

	if (state!=TAP_INVALID)
		cmd_queue_end_state = state;
	return cmd_queue_end_state;
734
735
}

736
737
738
739
740
tap_state_t jtag_get_end_state(void)
{
	return cmd_queue_end_state;
}

741
void jtag_add_sleep(u32 us)
742
{
zwelch's avatar
zwelch committed
743
744
745
	/// @todo Here, keep_alive() appears to be a layering violation!!!
	keep_alive();
	jtag_set_error(interface_jtag_add_sleep(us));
746
747
}

748
int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
749
750
{
	int retval = ERROR_OK;
751

752
	int compare_failed = 0;
753

oharboe's avatar
oharboe committed
754
	if (in_check_mask)
755
		compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
756
	else
757
		compare_failed = buf_cmp(captured, in_check_value, num_bits);
758

759
	if (compare_failed){
760
761
762
		/* 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
763
		 */
764
		/*
765
		LOG_WARNING("TAP %s:",
oharboe's avatar
oharboe committed
766
					jtag_tap_name(field->tap));
767
					*/
768
769
		if (compare_failed)
		{
770
			char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
771
			char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
772

773
			if (in_check_mask)
774
775
			{
				char *in_check_mask_char;
776
				in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
777
				LOG_WARNING("value captured during scan didn't pass the requested check:");
oharboe's avatar
oharboe committed
778
				LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
779
							captured_char, in_check_value_char, in_check_mask_char);
780
781
782
783
				free(in_check_mask_char);
			}
			else
			{
784
				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);
785
786
787
788
			}

			free(captured_char);
			free(in_check_value_char);
789

790
791
			retval = ERROR_JTAG_QUEUE_FAILED;
		}
792

793
794
795
796
	}
	return retval;
}

797
798
void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
{
799
	assert(field->in_value != NULL);
800
801
802
803
804
805
806

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

807
	jtag_execute_queue_noclear();
808

809
	int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
810
811
812
813
814
	jtag_set_error(retval);
}



zwelch's avatar
zwelch committed
815
816
int default_interface_jtag_execute_queue(void)
{
zwelch's avatar
zwelch committed
817
818
819
820
821
822
823
824
	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
825
826
827
	return jtag->execute_queue();
}

oharboe's avatar
oharboe committed
828
void jtag_execute_queue_noclear(void)
oharboe's avatar
oharboe committed
829
{
oharboe's avatar
oharboe committed
830
	jtag_flush_queue_count++;
zwelch's avatar
zwelch committed
831
	jtag_set_error(interface_jtag_execute_queue());
832
833
}

834
835
836
837
838
int jtag_get_flush_queue_count(void)
{
	return jtag_flush_queue_count;
}

839
840
841
int jtag_execute_queue(void)
{
	jtag_execute_queue_noclear();
zwelch's avatar
zwelch committed
842
	return jtag_error_clear();
843
844
}

845
static int jtag_reset_callback(enum jtag_event event, void *priv)
846
{
847
	jtag_tap_t *tap = priv;
848

849
	LOG_DEBUG("-");
850

851
852
	if (event == JTAG_TRST_ASSERTED)
	{
853
854
		buf_set_ones(tap->cur_instr, tap->ir_length);
		tap->bypass = 1;
855
	}
856

857
858
859
860
861
	return ERROR_OK;
}

void jtag_sleep(u32 us)
{
862
	alive_sleep(us/1000);
863
864
}