jtag.c 71.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
 *   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

31
#define INCLUDE_JTAG_INTERFACE_H
32
#include "jtag.h"
zwelch's avatar
zwelch committed
33
#include "minidriver.h"
34

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

39

40
int jtag_flush_queue_count; /* count # of flushes for profiling / debugging purposes */
41

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

45
46
/* 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
47
*/
48
int jtag_error=ERROR_OK;
oharboe's avatar
oharboe committed
49

50
51
52
53
54
55
56
57
58
59
60
61
typedef struct cmd_queue_page_s
{
	void *address;
	size_t used;
	struct cmd_queue_page_s *next;
} cmd_queue_page_t;

#define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
static cmd_queue_page_t *cmd_queue_pages = NULL;

char* jtag_event_strings[] =
{
62
	"JTAG controller reset (RESET or TRST)"
63
64
};

65
66
67
68
69
70
71
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 }
};

72
73
74
75
int jtag_trst = 0;
int jtag_srst = 0;

jtag_command_t *jtag_command_queue = NULL;
zwelch's avatar
zwelch committed
76
static jtag_command_t **next_command_pointer = &jtag_command_queue;
77
78
static jtag_tap_t *jtag_all_taps = NULL;

79
enum reset_types jtag_reset_config = RESET_NONE;
80
81
tap_state_t cmd_queue_end_state = TAP_RESET;
tap_state_t cmd_queue_cur_state = TAP_RESET;
82
83

int jtag_verify_capture_ir = 1;
oharboe's avatar
oharboe committed
84
int jtag_verify = 1;
85
86

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

/* maximum number of JTAG devices expected in the chain
 */
92
#define JTAG_MAX_CHAIN_SIZE 20
93
94
95
96

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

oharboe's avatar
   
oharboe committed
97
/* speed in kHz*/
oharboe's avatar
   
oharboe committed
98
static int speed_khz = 0;
oharboe's avatar
   
oharboe committed
99
100
101
/* flag if the kHz speed was defined */
static int hasKHz = 0;

102
103
/* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
 */
104

105
#if BUILD_ECOSBOARD == 1
106
	extern jtag_interface_t zy1000_interface;
107
#endif
108

109
110
111
#if BUILD_PARPORT == 1
	extern jtag_interface_t parport_interface;
#endif
112

113
114
115
#if BUILD_DUMMY == 1
	extern jtag_interface_t dummy_interface;
#endif
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
146
147
148
#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

149
150
151
152
#if BUILD_JLINK == 1
	extern jtag_interface_t jlink_interface;
#endif

duane's avatar
duane committed
153
154
155
156
#if BUILD_VSLLINK == 1
	extern jtag_interface_t vsllink_interface;
#endif

157
158
159
160
#if BUILD_RLINK == 1
	extern jtag_interface_t rlink_interface;
#endif

161
162
163
164
#if BUILD_ARMJTAGEW == 1
	extern jtag_interface_t armjtagew_interface;
#endif

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

214
struct jtag_interface_s *jtag = NULL;
215
216

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

220
/* jtag commands */
221
222
223
224
225
226
227
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);
228

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

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

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

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

247
int jtag_NumTotalTaps(void)
248
249
250
251
252
253
254
255
256
257
258
259
260
{
	jtag_tap_t *t;
	int n;

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

261
int jtag_NumEnabledTaps(void)
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{
	jtag_tap_t *t;
	int n;

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

jtag_tap_t *jtag_TapByString( const char *s )
{
	jtag_tap_t *t;
	char *cp;

	t = jtag_AllTaps();
ntfreak's avatar
ntfreak committed
283
	/* try name first */
284
285
286
287
288
289
290
	while(t){
		if( 0 == strcmp( t->dotted_name, s ) ){
			break;
		} else {
			t = t->next_tap;
		}
	}
ntfreak's avatar
ntfreak committed
291
	/* backup plan is by number */
292
293
294
295
296
297
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... */
			t = jtag_TapByAbsPosition(n);
		}
	}
	return t;
}

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

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

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

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

338
339
340
int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
{
	jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
341

342
343
344
345
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
346

347
348
349
350
351
352
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
353

354
355
356
357
	(*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->priv = priv;
	(*callbacks_p)->next = NULL;
358

359
360
361
362
363
364
	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;
365

366
367
368
369
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
370

371
372
373
374
375
376
377
378
379
380
	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;
	}
381

382
383
384
385
386
387
	return ERROR_OK;
}

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

389
	LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
390

391
392
393
394
395
	while (callback)
	{
		callback->callback(event, callback->priv);
		callback = callback->next;
	}
396

397
398
399
	return ERROR_OK;
}

400
401
void jtag_queue_command(jtag_command_t * cmd)
{
zwelch's avatar
zwelch committed
402
403
	// this command goes on the end, so ensure the queue terminates
	cmd->next = NULL;
404

zwelch's avatar
zwelch committed
405
406
407
	jtag_command_t **last_cmd = next_command_pointer;
	assert(NULL != last_cmd);
	assert(NULL == *last_cmd);
408
409
	*last_cmd = cmd;

zwelch's avatar
zwelch committed
410
411
	// store location where the next command pointer will be stored
	next_command_pointer = &cmd->next;
412
413
}

414
415
416
417
void* cmd_queue_alloc(size_t size)
{
	cmd_queue_page_t **p_page = &cmd_queue_pages;
	int offset;
oharboe's avatar
oharboe committed
418
	u8 *t;
419

420
421
422
423
424
425
426
427
428
429
430
431
432
433
	/*
	 * WARNING:
	 *    We align/round the *SIZE* per below
	 *    so that all pointers returned by
	 *    this function are reasonably well
	 *    aligned.
	 *
	 * If we did not, then an "odd-length" request would cause the
	 * *next* allocation to be at an *odd* address, and because
	 * this function has the same type of api as malloc() - we
	 * must also return pointers that have the same type of
	 * alignment.
	 *
	 * What I do not/have is a reasonable portable means
434
	 * to align by...
435
436
437
438
439
440
	 *
	 * The solution here, is based on these suggestions.
	 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
	 *
	 */
	union worse_case_align {
441
442
443
444
		int i;
		long l;
		float f;
		void *v;
445
446
447
	};
#define ALIGN_SIZE  (sizeof(union worse_case_align))

448
	/* The alignment process. */
449
	size = (size + ALIGN_SIZE -1) & (~(ALIGN_SIZE-1));
450
	/* Done... */
451

452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
	if (*p_page)
	{
		while ((*p_page)->next)
			p_page = &((*p_page)->next);
		if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
			p_page = &((*p_page)->next);
	}

	if (!*p_page)
	{
		*p_page = malloc(sizeof(cmd_queue_page_t));
		(*p_page)->used = 0;
		(*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
		(*p_page)->next = NULL;
	}

	offset = (*p_page)->used;
	(*p_page)->used += size;
470

oharboe's avatar
oharboe committed
471
	t=(u8 *)((*p_page)->address);
472
473
474
	return t + offset;
}

oharboe's avatar
oharboe committed
475
void cmd_queue_free(void)
476
477
478
479
480
481
482
483
484
485
486
487
488
489
{
	cmd_queue_page_t *page = cmd_queue_pages;

	while (page)
	{
		cmd_queue_page_t *last = page;
		free(page->address);
		page = page->next;
		free(last);
	}

	cmd_queue_pages = NULL;
}

zwelch's avatar
zwelch committed
490
491
492
493
494
495
496
497
void jtag_command_queue_reset(void)
{
	cmd_queue_free();

	jtag_command_queue = NULL;
	next_command_pointer = &jtag_command_queue;
}

oharboe's avatar
oharboe committed
498
static void jtag_prelude1(void)
499
500
501
{
	if (jtag_trst == 1)
	{
502
		LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
oharboe's avatar
oharboe committed
503
		jtag_error=ERROR_JTAG_TRST_ASSERTED;
504
		return;
505
506
	}

507
	if (cmd_queue_end_state == TAP_RESET)
oharboe's avatar
oharboe committed
508
		jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
509
510
}

511
static void jtag_prelude(tap_state_t state)
512
513
{
	jtag_prelude1();
514

mifi's avatar
mifi committed
515
	if (state != TAP_INVALID)
516
		jtag_add_end_state(state);
517

oharboe's avatar
oharboe committed
518
	cmd_queue_cur_state = cmd_queue_end_state;
519
520
}

zwelch's avatar
zwelch committed
521
522
523
524
525
void jtag_alloc_in_value32(scan_field_t *field)
{
	interface_jtag_alloc_in_value32(field);
}

526
void jtag_add_ir_scan_noverify(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
527
{
oharboe's avatar
oharboe committed
528
	int retval;
529
	jtag_prelude(state);
530

531
	retval=interface_jtag_add_ir_scan(in_num_fields, in_fields, cmd_queue_end_state);
oharboe's avatar
oharboe committed
532
533
	if (retval!=ERROR_OK)
		jtag_error=retval;
534
535
536
537

}


538
539
540
541
542
543
544
545
546
/**
 * 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.
 *
 */
547
void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
548
{
oharboe's avatar
oharboe committed
549
	if (jtag_verify&&jtag_verify_capture_ir)
550
	{
zwelch's avatar
zwelch committed
551
		/* 8 x 32 bit id's is enough for all invocations */
552
553

		for (int j = 0; j < in_num_fields; j++)
554
		{
oharboe's avatar
oharboe committed
555
556
557
			/* 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.
			 */
558
559
			in_fields[j].check_value=in_fields[j].tap->expected;
			in_fields[j].check_mask=in_fields[j].tap->expected_mask;
560
		}
561
		jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
562
563
	} else
	{
564
		jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
565
	}
oharboe's avatar
oharboe committed
566
567
}

568
569
570
571
572
573
/**
 * 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
 *
 */
574
void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
575
{
oharboe's avatar
oharboe committed
576
	int retval;
577

578
	jtag_prelude(state);
579

580
	retval=interface_jtag_add_plain_ir_scan(in_num_fields, in_fields, cmd_queue_end_state);
581
582
	if (retval!=ERROR_OK)
		jtag_error=retval;
583
584
}

585
586
587
588
void jtag_add_callback(jtag_callback1_t f, u8 *in)
{
	interface_jtag_add_callback(f, in);
}
589

590
591
592
593
594
595
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);
}
596
597
598
599
600
601
602
603

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

604
static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
605
		int in_num_fields, scan_field_t *in_fields, tap_state_t state)
606
{
607
	for (int i = 0; i < in_num_fields; i++)
608
	{
609
610
611
612
613
614
615
		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;
616
617
	}

618
	jtag_add_scan(in_num_fields, in_fields, state);
619

620
	for (int i = 0; i < in_num_fields; i++)
621
	{
622
		if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
623
624
		{
			/* this is synchronous for a minidriver */
625
626
627
628
			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);
629
		}
630
		if (in_fields[i].allocated)
631
		{
632
			free(in_fields[i].in_value);
633
		}
634
		if (in_fields[i].modified)
635
		{
636
			in_fields[i].in_value = NULL;
637
638
		}
	}
639
}
640

641
void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
642
{
oharboe's avatar
oharboe committed
643
644
	if (jtag_verify)
	{
645
		jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
646
647
	} else
	{
648
		jtag_add_dr_scan(in_num_fields, in_fields, state);
oharboe's avatar
oharboe committed
649
	}
650
651
}

652
653
654
655
656
657
658
659
660
661

/**
 * 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().
 *
 */
662
void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
{
	int retval;

	jtag_prelude(state);

	retval=interface_jtag_add_dr_scan(in_num_fields, in_fields, cmd_queue_end_state);
	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
 *
 */
681
void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
682
{
oharboe's avatar
oharboe committed
683
	int retval;
684

685
	jtag_prelude(state);
686

687
	retval=interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, cmd_queue_end_state);
688
689
	if (retval!=ERROR_OK)
		jtag_error=retval;
690
691
}

zwelch's avatar
zwelch committed
692
693
694
695
696
697
void jtag_add_dr_out(jtag_tap_t* tap,
		int num_fields, const int* num_bits, const u32* value,
		tap_state_t end_state)
{
	if (end_state != TAP_INVALID)
		cmd_queue_end_state = end_state;
698

zwelch's avatar
zwelch committed
699
700
701
702
703
704
	cmd_queue_cur_state = cmd_queue_end_state;

	interface_jtag_add_dr_out(tap,
			num_fields, num_bits, value,
			cmd_queue_end_state);
}
705

706
void jtag_add_tlr(void)
707
{
708
	jtag_prelude(TAP_RESET);
709

710
	int retval;
711
	retval=interface_jtag_add_tlr();
712
713
	if (retval!=ERROR_OK)
		jtag_error=retval;
oharboe's avatar
oharboe committed
714
715
}

716
void jtag_add_pathmove(int num_states, const tap_state_t *path)
717
{
718
	tap_state_t cur_state = cmd_queue_cur_state;
oharboe's avatar
oharboe committed
719
720
721
	int i;
	int retval;

722
	/* the last state has to be a stable state */
723
	if (!tap_is_state_stable(path[num_states - 1]))
724
	{
725
		LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
726
		exit(-1);
727
	}
728
729
730

	for (i=0; i<num_states; i++)
	{
731
		if (path[i] == TAP_RESET)
732
		{
733
			LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
734
735
			exit(-1);
		}
736

737
738
		if ( tap_state_transition(cur_state, true)  != path[i]
		  && tap_state_transition(cur_state, false) != path[i])
739
		{
740
			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[i]));
741
742
743
744
			exit(-1);
		}
		cur_state = path[i];
	}
745

746
	jtag_prelude1();
747

748
	retval = interface_jtag_add_pathmove(num_states, path);
749
	cmd_queue_cur_state = path[num_states - 1];
750
751
	if (retval!=ERROR_OK)
		jtag_error=retval;
752
753
}

754
void jtag_add_runtest(int num_cycles, tap_state_t state)
oharboe's avatar
oharboe committed
755
{
oharboe's avatar
oharboe committed
756
	int retval;
757

758
	jtag_prelude(state);
759

oharboe's avatar
oharboe committed
760
	/* executed by sw or hw fifo */
oharboe's avatar
oharboe committed
761
	retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
762
763
	if (retval!=ERROR_OK)
		jtag_error=retval;
764
765
}

766
767
768
769
770

void jtag_add_clocks( int num_cycles )
{
	int retval;

771
	if( !tap_is_state_stable(cmd_queue_cur_state) )
772
773
	{
		 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
774
				 tap_state_name(cmd_queue_cur_state) );
775
776
777
		 jtag_error = ERROR_JTAG_NOT_STABLE_STATE;
		 return;
	}
778

779
780
781
782
	if( num_cycles > 0 )
	{
		jtag_prelude1();

783
784
		retval = interface_jtag_add_clocks(num_cycles);
		if (retval != ERROR_OK)
785
786
			jtag_error=retval;
	}
787
788
}

789
void jtag_add_reset(int req_tlr_or_trst, int req_srst)
790
{
791
	int trst_with_tlr = 0;
oharboe's avatar
oharboe committed
792
	int retval;
793

794
795
	/* FIX!!! there are *many* different cases here. A better
	 * approach is needed for legal combinations of transitions...
oharboe's avatar
oharboe committed
796
	 */
797
	if ((jtag_reset_config & RESET_HAS_SRST)&&
798
			(jtag_reset_config & RESET_HAS_TRST)&&
799
			((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
800
801
802
803
804
805
	{
		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
806
			/* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
807
			//LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
808
809
		}
	}
810

811
812
	/* Make sure that jtag_reset_config allows the requested reset */
	/* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
813
	if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
oharboe's avatar
oharboe committed
814
	{
815
816
817
		LOG_ERROR("BUG: requested reset would assert trst");
		jtag_error=ERROR_FAIL;
		return;
oharboe's avatar
oharboe committed
818
	}
819

820
	/* if TRST pulls SRST, we reset with TAP T-L-R */
821
	if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
822
	{
823
		trst_with_tlr = 1;
824
	}
825

826
827
	if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
	{
828
		LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
829
830
		jtag_error=ERROR_FAIL;
		return;
831
	}
832

833
	if (req_tlr_or_trst)
834
	{
835
		if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
836
837
838
839
		{
			jtag_trst = 1;
		} else
		{
840
			trst_with_tlr = 1;
841
842
843
844
		}
	} else
	{
		jtag_trst = 0;
845
	}
846

847
848
	jtag_srst = req_srst;

849
	retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
850
851
852
	if (retval!=ERROR_OK)
	{
		jtag_error=retval;
853
		return;
854
	}
855
	jtag_execute_queue();
856

857
858
	if (jtag_srst)
	{
859
		LOG_DEBUG("SRST line asserted");
860
861
862
	}
	else
	{
863
		LOG_DEBUG("SRST line released");
864
865
866
		if (jtag_nsrst_delay)
			jtag_add_sleep(jtag_nsrst_delay * 1000);
	}
867

868
	if (trst_with_tlr)
869
	{
870
		LOG_DEBUG("JTAG reset with RESET instead of TRST");
871
		jtag_add_end_state(TAP_RESET);
872
		jtag_add_tlr();
873
		jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
874
		return;
875
	}
876

877
	if (jtag_trst)
oharboe's avatar
oharboe committed
878
	{
879
880
881
		/* we just asserted nTRST, so we're now in Test-Logic-Reset,
		 * and inform possible listeners about this
		 */
882
		LOG_DEBUG("TRST line asserted");
883
		tap_set_state(TAP_RESET);
884
885
886
887
888
889
		jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
	}
	else
	{
		if (jtag_ntrst_delay)
			jtag_add_sleep(jtag_ntrst_delay * 1000);
oharboe's avatar
oharboe committed
890
891
892
	}
}

893
void jtag_add_end_state(tap_state_t state)
894
{
895
	cmd_queue_end_state = state;
896
	if ((cmd_queue_end_state == TAP_DRSHIFT)||(cmd_queue_end_state == TAP_IRSHIFT))
897
	{
898
		LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
899
	}
900
901
}

902
void jtag_add_sleep(u32 us)
903
{
oharboe's avatar
oharboe committed
904
	keep_alive(); /* we might be running on a very slow JTAG clk */
905
906
907
908
	int retval=interface_jtag_add_sleep(us);
	if (retval!=ERROR_OK)
		jtag_error=retval;
	return;
909
910
}

911
int jtag_scan_size(const scan_command_t *cmd)
912
913
914
915
916
917
918
919
920
921
922
923
924
{
	int bit_count = 0;
	int i;

	/* count bits in scan command */
	for (i = 0; i < cmd->num_fields; i++)
	{
		bit_count += cmd->fields[i].num_bits;
	}

	return bit_count;
}

925
int jtag_build_buffer(const scan_command_t *cmd, u8 **buffer)
926
927
928
{
	int bit_count = 0;
	int i;
929

930
	bit_count = jtag_scan_size(cmd);
931
	*buffer = calloc(1,CEIL(bit_count, 8));
932

933
934
	bit_count = 0;

935
#ifdef _DEBUG_JTAG_IO_
936
	LOG_DEBUG("%s num_fields: %i", cmd->ir_scan ? "IRSCAN" : "DRSCAN", cmd->num_fields);
937
#endif
938

939
940
941
942
943
	for (i = 0; i < cmd->num_fields; i++)
	{
		if (cmd->fields[i].out_value)
		{
#ifdef _DEBUG_JTAG_IO_
944
			char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : cmd->fields[i].num_bits, 16);
945
946
947
#endif
			buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
#ifdef _DEBUG_JTAG_IO_
948
			LOG_DEBUG("fields[%i].out_value[%i]: 0x%s", i, cmd->fields[i].num_bits, char_buf);
949
950
951
			free(char_buf);
#endif
		}
952
953
954
955
956
957
		else
		{
#ifdef _DEBUG_JTAG_IO_
			LOG_DEBUG("fields[%i].out_value[%i]: NULL", i, cmd->fields[i].num_bits);
#endif
		}
958

959
960
961
		bit_count += cmd->fields[i].num_bits;
	}

962
963
964
965
#ifdef _DEBUG_JTAG_IO_
	//LOG_DEBUG("bit_count totalling: %i",  bit_count );
#endif

966
967
968
	return bit_count;
}

969
int jtag_read_buffer(u8 *buffer, const scan_command_t *cmd)
970
971
972
973
{
	int i;
	int bit_count = 0;
	int retval;
974

975
976
	/* we return ERROR_OK, unless a check fails, or a handler reports a problem */
	retval = ERROR_OK;
977

978
979
980
981
982
	for (i = 0; i < cmd->num_fields; i++)
	{
		/* if neither in_value nor in_handler
		 * are specified we don't have to examine this field
		 */
983
		if (cmd->fields[i].in_value)
984
985
986
		{
			int num_bits = cmd->fields[i].num_bits;
			u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
987

988
#ifdef _DEBUG_JTAG_IO_
989
			char *char_buf = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
990
			LOG_DEBUG("fields[%i].in_value[%i]: 0x%s", i, num_bits, char_buf);