jtag.c 90 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
9
10
 *                                                                         *
 *   Copyright (C) 2009 SoftPLC Corporation                                *
 * 	 http://softplc.com                                                    *
 *   dick@softplc.com                                                      *
11
 *                                                                         *
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 *   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"

33
34
35
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
36

37

38
int jtag_flush_queue_count; /* count # of flushes for profiling / debugging purposes */
39

40
41
static void jtag_add_scan_check(void (*jtag_add_scan)(int num_fields, scan_field_t *fields, tap_state_t state),
		int num_fields, scan_field_t *fields, tap_state_t state);
oharboe's avatar
oharboe committed
42

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

48
49
50
51
52
53
54
55
56
57
58
59
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[] =
{
60
	"JTAG controller reset (RESET or TRST)"
61
62
};

63
64
65
66
67
68
69
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 }
};

70
71
72
int jtag_trst = 0;
int jtag_srst = 0;

73
74
75
76
77
78
79
80
81
#ifndef HAVE_JTAG_MINIDRIVER_H
struct jtag_callback_entry
{
	struct jtag_callback_entry *next;

	jtag_callback_t callback;
	u8 *in;
	jtag_callback_data_t data1;
	jtag_callback_data_t data2;
82
	jtag_callback_data_t data3;
83
84
85
86
87
88
89
90
};


static struct jtag_callback_entry *jtag_callback_queue_head = NULL;
static struct jtag_callback_entry *jtag_callback_queue_tail = NULL;
#endif


91
jtag_command_t *jtag_command_queue = NULL;
92
jtag_command_t **last_command_pointer = &jtag_command_queue;
93
94
static jtag_tap_t *jtag_all_taps = NULL;

95
enum reset_types jtag_reset_config = RESET_NONE;
96
97
tap_state_t cmd_queue_end_state = TAP_RESET;
tap_state_t cmd_queue_cur_state = TAP_RESET;
98
99

int jtag_verify_capture_ir = 1;
oharboe's avatar
oharboe committed
100
int jtag_verify = 1;
101
102

/* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
103
104
static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
105
106
107

/* maximum number of JTAG devices expected in the chain
 */
108
#define JTAG_MAX_CHAIN_SIZE 20
109
110
111
112

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

oharboe's avatar
   
oharboe committed
113
/* speed in kHz*/
oharboe's avatar
   
oharboe committed
114
static int speed_khz = 0;
oharboe's avatar
   
oharboe committed
115
116
117
/* flag if the kHz speed was defined */
static int hasKHz = 0;

118
119
/* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
 */
120

121
#if BUILD_ECOSBOARD == 1
122
	extern jtag_interface_t zy1000_interface;
123
#endif
124

125
126
127
#if BUILD_PARPORT == 1
	extern jtag_interface_t parport_interface;
#endif
128

129
130
131
#if BUILD_DUMMY == 1
	extern jtag_interface_t dummy_interface;
#endif
132

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#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

165
166
167
168
#if BUILD_JLINK == 1
	extern jtag_interface_t jlink_interface;
#endif

duane's avatar
duane committed
169
170
171
172
#if BUILD_VSLLINK == 1
	extern jtag_interface_t vsllink_interface;
#endif

173
174
175
176
#if BUILD_RLINK == 1
	extern jtag_interface_t rlink_interface;
#endif

177
178
179
180
#if BUILD_ARMJTAGEW == 1
	extern jtag_interface_t armjtagew_interface;
#endif

181
jtag_interface_t *jtag_interfaces[] = {
182
#if BUILD_ECOSBOARD == 1
183
	&zy1000_interface,
184
#endif
185
186
187
#if BUILD_PARPORT == 1
	&parport_interface,
#endif
188
189
190
#if BUILD_DUMMY == 1
	&dummy_interface,
#endif
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#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,
214
215
216
#endif
#if BUILD_JLINK == 1
	&jlink_interface,
217
#endif
duane's avatar
duane committed
218
219
220
#if BUILD_VSLLINK == 1
	&vsllink_interface,
#endif
221
222
#if BUILD_RLINK == 1
	&rlink_interface,
223
224
225
#endif
#if BUILD_ARMJTAGEW == 1
	&armjtagew_interface,
226
227
228
229
230
231
232
#endif
	NULL,
};

jtag_interface_t *jtag = NULL;

/* configuration */
233
static jtag_interface_t *jtag_interface = NULL;
234
int jtag_speed = 0;
oharboe's avatar
   
oharboe committed
235

236
/* forward declarations */
237
238
239
240
241
242
//void jtag_add_pathmove(int num_states, tap_state_t *path);
//void jtag_add_runtest(int num_cycles, tap_state_t endstate);
//void jtag_add_end_state(tap_state_t endstate);
//void jtag_add_sleep(u32 us);
//int jtag_execute_queue(void);
static tap_state_t tap_state_by_name(const char *name);
243
244

/* jtag commands */
245
246
247
248
249
250
251
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);
252

253
static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
254

255
256
257
258
259
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);
260
static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
261

262
static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
oharboe's avatar
oharboe committed
263
static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
264

oharboe's avatar
oharboe committed
265
266
jtag_tap_t *jtag_AllTaps(void)
{
267
	return jtag_all_taps;
268
269
};

270
int jtag_NumTotalTaps(void)
271
272
273
274
275
276
277
278
279
280
281
282
283
{
	jtag_tap_t *t;
	int n;

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

284
int jtag_NumEnabledTaps(void)
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
{
	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
306
	/* try name first */
307
308
309
310
311
312
313
	while(t){
		if( 0 == strcmp( t->dotted_name, s ) ){
			break;
		} else {
			t = t->next_tap;
		}
	}
ntfreak's avatar
ntfreak committed
314
	/* backup plan is by number */
315
316
317
318
319
320
321
322
323
324
325
326
	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;
}

327
jtag_tap_t * jtag_TapByJimObj( Jim_Interp *interp, Jim_Obj *o )
328
329
330
331
332
333
334
335
336
337
{
	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
338
	}
339
340
341
342
343
344
345
	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 */
346
jtag_tap_t * jtag_TapByAbsPosition( int n )
347
348
349
350
351
352
{
	int orig_n;
	jtag_tap_t *t;

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

354
355
356
	while( t && (n > 0)) {
		n--;
		t = t->next_tap;
357
	}
358
359
360
	return t;
}

361
362
363
int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
{
	jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
364

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

370
371
372
373
374
375
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
376

377
378
379
380
	(*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->priv = priv;
	(*callbacks_p)->next = NULL;
381

382
383
384
385
386
387
	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;
388

389
390
391
392
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
393

394
395
396
397
398
399
400
401
402
403
	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;
	}
404

405
406
407
408
409
410
	return ERROR_OK;
}

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

412
	LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
413

414
415
416
417
418
	while (callback)
	{
		callback->callback(event, callback->priv);
		callback = callback->next;
	}
419

420
421
422
423
424
425
426
427
428
429
	return ERROR_OK;
}

/* returns a pointer to the pointer of the last command in queue
 * this may be a pointer to the root pointer (jtag_command_queue)
 * or to the next member of the last but one command
 */
jtag_command_t** jtag_get_last_command_p(void)
{
/*	jtag_command_t *cmd = jtag_command_queue;
430

431
432
433
434
435
	if (cmd)
		while (cmd->next)
			cmd = cmd->next;
	else
		return &jtag_command_queue;
436

437
	return &cmd->next;*/
438

439
	return last_command_pointer;
440
441
}

442
443
444
445
446
447
448
449
450
451
452

void jtag_queue_command(jtag_command_t * cmd)
{
	jtag_command_t **last_cmd;

	last_cmd = jtag_get_last_command_p();

	*last_cmd = cmd;

	(*last_cmd)->next = NULL;

453
	last_command_pointer = &((*last_cmd)->next);
454
455
456
}


457
458
459
460
void* cmd_queue_alloc(size_t size)
{
	cmd_queue_page_t **p_page = &cmd_queue_pages;
	int offset;
oharboe's avatar
oharboe committed
461
	u8 *t;
462

463
464
465
466
467
468
469
470
471
472
473
474
475
476
	/*
	 * 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
477
	 * to align by...
478
479
480
481
482
483
	 *
	 * The solution here, is based on these suggestions.
	 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
	 *
	 */
	union worse_case_align {
484
485
486
487
		int i;
		long l;
		float f;
		void *v;
488
489
490
	};
#define ALIGN_SIZE  (sizeof(union worse_case_align))

491
	/* The alignment process. */
492
	size = (size + ALIGN_SIZE -1) & (~(ALIGN_SIZE-1));
493
	/* Done... */
494

495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
	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;
513

oharboe's avatar
oharboe committed
514
	t=(u8 *)((*p_page)->address);
515
516
517
	return t + offset;
}

oharboe's avatar
oharboe committed
518
void cmd_queue_free(void)
519
520
521
522
523
524
525
526
527
528
529
530
531
532
{
	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;
}

oharboe's avatar
oharboe committed
533
static void jtag_prelude1(void)
534
535
536
{
	if (jtag_trst == 1)
	{
537
		LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
oharboe's avatar
oharboe committed
538
		jtag_error=ERROR_JTAG_TRST_ASSERTED;
539
		return;
540
541
	}

542
	if (cmd_queue_end_state == TAP_RESET)
oharboe's avatar
oharboe committed
543
		jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
544
545
}

546
static void jtag_prelude(tap_state_t state)
547
548
{
	jtag_prelude1();
549

mifi's avatar
mifi committed
550
	if (state != TAP_INVALID)
551
		jtag_add_end_state(state);
552

oharboe's avatar
oharboe committed
553
	cmd_queue_cur_state = cmd_queue_end_state;
554
555
}

556
void jtag_add_ir_scan_noverify(int num_fields, scan_field_t *fields, tap_state_t state)
557
{
oharboe's avatar
oharboe committed
558
	int retval;
559
	jtag_prelude(state);
560

oharboe's avatar
oharboe committed
561
	retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
oharboe's avatar
oharboe committed
562
563
	if (retval!=ERROR_OK)
		jtag_error=retval;
564
565
566
567
568
569

}


void jtag_add_ir_scan(int num_fields, scan_field_t *fields, tap_state_t state)
{
oharboe's avatar
oharboe committed
570
	if (jtag_verify&&jtag_verify_capture_ir)
571
	{
oharboe's avatar
oharboe committed
572
573
574
		/* 8 x 32 bit id's is enough for all invoations */
		int j;
		for (j = 0; j < num_fields; j++)
575
		{
oharboe's avatar
oharboe committed
576
577
578
579
580
			fields[j].check_value=NULL;
			fields[j].check_mask=NULL;
			/* 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.
			 */
581
582
			fields[j].check_value=fields[j].tap->expected;
			fields[j].check_mask=fields[j].tap->expected_mask;
583
		}
oharboe's avatar
oharboe committed
584
585
586
587
		jtag_add_scan_check(jtag_add_ir_scan_noverify, num_fields, fields, state);
	} else
	{
		jtag_add_ir_scan_noverify(num_fields, fields, state);
588
	}
oharboe's avatar
oharboe committed
589
590
}

591
int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, tap_state_t state)
592
{
593
594
595
596
	jtag_tap_t *tap;
	int j;
	int x;
	int nth_tap;
oharboe's avatar
oharboe committed
597
598
	int scan_size = 0;

599
	/* allocate memory for a new list member */
600
601
602
603
604
	jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
	
	jtag_queue_command(cmd);
	
	cmd->type = JTAG_SCAN;
605
606

	/* allocate memory for ir scan command */
607
608
	cmd->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
	cmd->cmd.scan->ir_scan = true;
609
	x = jtag_NumEnabledTaps();
610
611
612
	cmd->cmd.scan->num_fields = x;	/* one field per device */
	cmd->cmd.scan->fields = cmd_queue_alloc(x  * sizeof(scan_field_t));
	cmd->cmd.scan->end_state = state;
613

614
615
616
	nth_tap = -1;
	tap = NULL;
	for(;;){
617
		int found = 0;
618

ntfreak's avatar
ntfreak committed
619
		/* do this here so it is not forgotten */
620
621
622
		tap = jtag_NextEnabledTap(tap);
		if( tap == NULL ){
			break;
623
		}
624
		nth_tap++;
oharboe's avatar
oharboe committed
625
626
627

		assert(nth_tap < x );

628
		scan_size = tap->ir_length;
629
630
631
		cmd->cmd.scan->fields[nth_tap].tap = tap;
		cmd->cmd.scan->fields[nth_tap].num_bits = scan_size;
		cmd->cmd.scan->fields[nth_tap].in_value = NULL; /* do not collect input for tap's in bypass */
632
633
634
635

		/* search the list */
		for (j = 0; j < num_fields; j++)
		{
636
			if (tap == fields[j].tap)
637
638
			{
				found = 1;
639
640
				cmd->cmd.scan->fields[nth_tap].in_value = fields[j].in_value;
				cmd->cmd.scan->fields[nth_tap].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
641

642
				tap->bypass = 0;
643
644
645
				break;
			}
		}
646

647
648
		if (!found)
		{
649
			/* if a tap isn't listed, set it to BYPASS */
650
			cmd->cmd.scan->fields[nth_tap].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
651
			tap->bypass = 1;
652
		}
653

654
		/* update device information */
655
		buf_cpy(cmd->cmd.scan->fields[nth_tap].out_value, tap->cur_instr, scan_size);
656
	}
oharboe's avatar
oharboe committed
657
	assert(nth_tap == (x-1));
658

659
660
661
	return ERROR_OK;
}

662
void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, tap_state_t state)
663
{
oharboe's avatar
oharboe committed
664
	int retval;
665

666
	jtag_prelude(state);
667

oharboe's avatar
oharboe committed
668
	retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
669
670
	if (retval!=ERROR_OK)
		jtag_error=retval;
671
672
}

673
int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, tap_state_t state)
674
{
675
	/* allocate memory for a new list member */
676

677
678
679
	jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
	
	jtag_queue_command(cmd);
680

681
	cmd->type = JTAG_SCAN;
682
683

	/* allocate memory for ir scan command */
684
685
686
687
688
	cmd->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
	cmd->cmd.scan->ir_scan = true;
	cmd->cmd.scan->num_fields = num_fields;
	cmd->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
	cmd->cmd.scan->end_state = state;
689

690
691
	for (int i = 0; i < num_fields; i++)
	{
692
693
		int num_bits = fields[i].num_bits;
		int num_bytes = CEIL(fields[i].num_bits, 8);
694
695
696
697
		cmd->cmd.scan->fields[i].tap = fields[i].tap;
		cmd->cmd.scan->fields[i].num_bits = num_bits;
		cmd->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
		cmd->cmd.scan->fields[i].in_value = fields[i].in_value;
698
	}
699

700
701
702
	return ERROR_OK;
}

703
void jtag_add_dr_scan(int num_fields, scan_field_t *fields, tap_state_t state)
704
{
oharboe's avatar
oharboe committed
705
	int retval;
706

707
	jtag_prelude(state);
708

oharboe's avatar
oharboe committed
709
	retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
710
711
	if (retval!=ERROR_OK)
		jtag_error=retval;
712
713
}

714
715
716
717
718
719
720
721

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

722
723
static void jtag_add_scan_check(void (*jtag_add_scan)(int num_fields, scan_field_t *fields, tap_state_t state),
		int num_fields, scan_field_t *fields, tap_state_t state)
724
725
726
727
728
729
730
731
732
733
734
735
736
{
	for (int i=0; i<num_fields; i++)
	{
		fields[i].allocated=0;
		fields[i].modified=0;
		if ((fields[i].check_value!=NULL)&&(fields[i].in_value==NULL))
		{
			fields[i].modified=1;
			/* we need storage space... */
#ifdef HAVE_JTAG_MINIDRIVER_H
			if (fields[i].num_bits<=32)
			{
				/* This is enough space and we're executing this synchronously */
737
				fields[i].in_value=fields[i].intmp;
738
739
740
741
742
743
744
745
746
747
748
			} else
			{
				fields[i].in_value=(u8 *)malloc(CEIL(fields[i].num_bits, 8));
				fields[i].allocated=1;
			}
#else
			fields[i].in_value=(u8 *)cmd_queue_alloc(CEIL(fields[i].num_bits, 8));
#endif
		}
	}

749
	jtag_add_scan(num_fields, fields, state);
750
751
752
753
754
755

	for (int i=0; i<num_fields; i++)
	{
		if ((fields[i].check_value!=NULL)&&(fields[i].in_value!=NULL))
		{
			/* this is synchronous for a minidriver */
oharboe's avatar
oharboe committed
756
			jtag_add_callback4(jtag_check_value_mask_callback, fields[i].in_value, (jtag_callback_data_t)fields[i].check_value, (jtag_callback_data_t)fields[i].check_mask, (jtag_callback_data_t)fields[i].num_bits);
757
758
759
760
761
762
763
764
765
766
		}
		if (fields[i].allocated)
		{
			free(fields[i].in_value);
		}
		if (fields[i].modified)
		{
			fields[i].in_value=NULL;
		}
	}
767
}
768

769
770
void jtag_add_dr_scan_check(int num_fields, scan_field_t *fields, tap_state_t state)
{
oharboe's avatar
oharboe committed
771
772
773
774
775
776
777
	if (jtag_verify)
	{
		jtag_add_scan_check(jtag_add_dr_scan, num_fields, fields, state);
	} else
	{
		jtag_add_dr_scan(num_fields, fields, state);
	}
778
779
}

780
int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, tap_state_t state)
781
{
782
783
	int j;
	int nth_tap;
784
785
786
787
	int bypass_devices = 0;
	int field_count = 0;
	int scan_size;

788
	jtag_tap_t *tap;
789
790

	/* count devices in bypass */
791
792
793
794
795
796
797
798
	tap = NULL;
	bypass_devices = 0;
	for(;;){
		tap = jtag_NextEnabledTap(tap);
		if( tap == NULL ){
			break;
		}
		if( tap->bypass ){
799
			bypass_devices++;
800
		}
801
	}
802

803
	/* allocate memory for a new list member */
804
805
806
807
808
809
	
	jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));
	
	jtag_queue_command(cmd);
	
	cmd->type = JTAG_SCAN;
810
811

	/* allocate memory for dr scan command */
812
813
814
815
816
	cmd->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
	cmd->cmd.scan->ir_scan = false;
	cmd->cmd.scan->num_fields = num_fields + bypass_devices;
	cmd->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
	cmd->cmd.scan->end_state = state;
817

818
819
820
821
822
823
824
825
	tap = NULL;
	nth_tap = -1;
	for(;;){
		nth_tap++;
		tap = jtag_NextEnabledTap(tap);
		if( tap == NULL ){
			break;
		}
826
		int found = 0;
827
		cmd->cmd.scan->fields[field_count].tap = tap;
828

829
830
		for (j = 0; j < num_fields; j++)
		{
831
			if (tap == fields[j].tap)
832
833
834
			{
				found = 1;
				scan_size = fields[j].num_bits;
835
836
837
				cmd->cmd.scan->fields[field_count].num_bits = scan_size;
				cmd->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
				cmd->cmd.scan->fields[field_count].in_value = fields[j].in_value;
838
				field_count++;
839
840
841
842
			}
		}
		if (!found)
		{
843
#ifdef _DEBUG_JTAG_IO_
844
			/* if a device isn't listed, the BYPASS register should be selected */
845
			if (! tap->bypass)
846
			{
847
				LOG_ERROR("BUG: no scan data for a device not in BYPASS");
848
849
				exit(-1);
			}
850
#endif
851
			/* program the scan field to 1 bit length, and ignore it's value */
852
853
854
			cmd->cmd.scan->fields[field_count].num_bits = 1;
			cmd->cmd.scan->fields[field_count].out_value = NULL;
			cmd->cmd.scan->fields[field_count].in_value = NULL;
855
			field_count++;
856
857
858
		}
		else
		{
859
#ifdef _DEBUG_JTAG_IO_
860
			/* if a device is listed, the BYPASS register must not be selected */
861
			if (tap->bypass)
862
			{
863
				LOG_ERROR("BUG: scan data for a device in BYPASS");
864
				exit(-1);
865
			}
866
#endif
867
868
		}
	}
869
870

	/* field_count represents the true number of fields setup*/
kc8apf's avatar
kc8apf committed
871
	cmd->cmd.scan->num_fields = field_count;
872
873
874
	return ERROR_OK;
}

875
void MINIDRIVER(interface_jtag_add_dr_out)(jtag_tap_t *target_tap,
876
		int num_fields,
877
878
		const int *num_bits,
		const u32 *value,
879
		tap_state_t end_state)
880
{
881
	int nth_tap;
882
883
884
885
	int field_count = 0;
	int scan_size;
	int bypass_devices = 0;

886
887
	jtag_tap_t *tap;

888
	/* count devices in bypass */
889
890
891
892
893
894
895
896
	tap = NULL;
	bypass_devices = 0;
	for(;;){
		tap = jtag_NextEnabledTap(tap);
		if( tap == NULL ){
			break;
		}
		if( tap->bypass ){
897
			bypass_devices++;
898
		}
899
	}
900

901
	/* allocate memory for a new list member */
902
903
904
905
906
	jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));

	jtag_queue_command(cmd);

	cmd->type = JTAG_SCAN;
907
908

	/* allocate memory for dr scan command */
909
910
911
912
913
	cmd->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
	cmd->cmd.scan->ir_scan = false;
	cmd->cmd.scan->num_fields = num_fields + bypass_devices;
	cmd->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
	cmd->cmd.scan->end_state = end_state;
914

915
916
917
918
919
920
921
922
	tap = NULL;
	nth_tap = -1;
	for(;;){
		tap = jtag_NextEnabledTap(tap);
		if( tap == NULL ){
			break;
		}
		nth_tap++;
923
		cmd->cmd.scan->fields[field_count].tap = tap;
924

925
		if (tap == target_tap)
926
927
928
929
		{
			int j;
#ifdef _DEBUG_JTAG_IO_
			/* if a device is listed, the BYPASS register must not be selected */
930
			if (tap->bypass)
931
			{
932
				LOG_ERROR("BUG: scan data for a device in BYPASS");
933
934
935
936
937
				exit(-1);
			}
#endif
			for (j = 0; j < num_fields; j++)
			{
oharboe's avatar
oharboe committed
938
				u8 out_value[4];
939
940
				scan_size = num_bits[j];
				buf_set_u32(out_value, 0, scan_size, value[j]);
941
942
943
				cmd->cmd.scan->fields[field_count].num_bits = scan_size;
				cmd->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
				cmd->cmd.scan->fields[field_count].in_value = NULL;
944
				field_count++;
945
946
947
948
949
			}
		} else
		{
#ifdef _DEBUG_JTAG_IO_
			/* if a device isn't listed, the BYPASS register should be selected */
950
			if (! tap->bypass)
951
			{
952
				LOG_ERROR("BUG: no scan data for a device not in BYPASS");
953
954
				exit(-1);
			}
955
#endif
956
			/* program the scan field to 1 bit length, and ignore it's value */
957
958
959
			cmd->cmd.scan->fields[field_count].num_bits = 1;
			cmd->cmd.scan->fields[field_count].out_value = NULL;
			cmd->cmd.scan->fields[field_count].in_value = NULL;
960
			field_count++;
961
962
963
964
		}
	}
}

965
void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, tap_state_t state)
966
{
oharboe's avatar
oharboe committed
967
	int retval;
968

969
	jtag_prelude(state);
970

oharboe's avatar
oharboe committed
971
	retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
972
973
	if (retval!=ERROR_OK)
		jtag_error=retval;
974
975
}

976
int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, tap_state_t state)
977
{
978
	/* allocate memory for a new list member */
979
980
981
982
983
	jtag_command_t * cmd = cmd_queue_alloc(sizeof(jtag_command_t));

	jtag_queue_command(cmd);

	cmd->type = JTAG_SCAN;
984
985

	/* allocate memory for scan command */
986
987
988
989
990
	cmd->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
	cmd->cmd.scan->ir_scan = false;
	cmd->cmd.scan->num_fields = num_fields;
	cmd->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
	cmd->cmd.scan->end_state = state;
991

992
	for (int i = 0; i < num_fields; i++)
993
994
995
	{
		int num_bits = fields[i].num_bits;
		int num_bytes = CEIL(fields[i].num_bits, 8);
996
997
998
999
		cmd->cmd.scan->fields[i].tap = fields[i].tap;
		cmd->cmd.scan->fields[i].num_bits = num_bits;
		cmd->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
		cmd->cmd.scan->fields[i].in_value = fields[i].in_value;
1000
	}
For faster browsing, not all history is shown. View entire blame