tcl.c 33.8 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
Øyvind Harboe's avatar
Øyvind Harboe committed
5
 *   Copyright (C) 2007-2010 Ø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
 *   Copyright (C) 2009 Zachary T Welch                                    *
 *   zw@superlucidity.net                                                  *
 *                                                                         *
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *   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
35
#include "minidriver.h"
36
#include "interface.h"
37
#include "interfaces.h"
38

39
40
41
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
42

43
44
45
46
47
/**
 * @file
 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
 */

48
static const Jim_Nvp nvp_jtag_tap_event[] = {
49
50
	{ .value = JTAG_TRST_ASSERTED,		.name = "post-reset" },
	{ .value = JTAG_TAP_EVENT_SETUP,	.name = "setup" },
51
52
53
54
55
	{ .value = JTAG_TAP_EVENT_ENABLE,       .name = "tap-enable" },
	{ .value = JTAG_TAP_EVENT_DISABLE,      .name = "tap-disable" },

	{ .name = NULL, .value = -1 }
};
56

57
extern struct jtag_interface *jtag_interface;
58

59
60
61
62
63
64
65
struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
{
	const char *cp = Jim_GetString(o, NULL);
	struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
	if (NULL == cp)
		cp = "(unknown)";
	if (NULL == t)
Øyvind Harboe's avatar
Øyvind Harboe committed
66
		Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
67
68
69
	return t;
}

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
143
144
145
146
147
static bool scan_is_safe(tap_state_t state)
{
	switch (state)
	{
	case TAP_RESET:
	case TAP_IDLE:
	case TAP_DRPAUSE:
	case TAP_IRPAUSE:
		return true;
	default:
		return false;
	}
}

static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
{
	int retval;
	struct scan_field *fields;
	int num_fields;
	int field_count = 0;
	int i, e;
	struct jtag_tap *tap;
	tap_state_t endstate;

	/* args[1] = device
	 * args[2] = num_bits
	 * args[3] = hex string
	 * ... repeat num bits and hex string ...
	 *
	 * .. optionally:
	*     args[N-2] = "-endstate"
	 *     args[N-1] = statename
	 */
	if ((argc < 4) || ((argc % 2) != 0))
	{
		Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
		return JIM_ERR;
	}

	endstate = TAP_IDLE;

	script_debug(interp, "drscan", argc, args);

	/* validate arguments as numbers */
	e = JIM_OK;
	for (i = 2; i < argc; i += 2)
	{
		long bits;
		const char *cp;

		e = Jim_GetLong(interp, args[i], &bits);
		/* If valid - try next arg */
		if (e == JIM_OK) {
			continue;
		}

		/* Not valid.. are we at the end? */
		if (((i + 2) != argc)) {
			/* nope, then error */
			return e;
		}

		/* it could be: "-endstate FOO"
		 * e.g. DRPAUSE so we can issue more instructions
		 * before entering RUN/IDLE and executing them.
		 */

		/* get arg as a string. */
		cp = Jim_GetString(args[i], NULL);
		/* is it the magic? */
		if (0 == strcmp("-endstate", cp)) {
			/* is the statename valid? */
			cp = Jim_GetString(args[i + 1], NULL);

			/* see if it is a valid state name */
			endstate = tap_state_by_name(cp);
			if (endstate < 0) {
				/* update the error message */
Øyvind Harboe's avatar
Øyvind Harboe committed
148
				Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
			} else {
				if (!scan_is_safe(endstate))
					LOG_WARNING("drscan with unsafe "
							"endstate \"%s\"", cp);

				/* valid - so clear the error */
				e = JIM_OK;
				/* and remove the last 2 args */
				argc -= 2;
			}
		}

		/* Still an error? */
		if (e != JIM_OK) {
			return e; /* too bad */
		}
	} /* validate args */

	tap = jtag_tap_by_jim_obj(interp, args[1]);
	if (tap == NULL) {
		return JIM_ERR;
	}

	num_fields = (argc-2)/2;
	fields = malloc(sizeof(struct scan_field) * num_fields);
	for (i = 2; i < argc; i += 2)
	{
		long bits;
		int len;
		const char *str;

		Jim_GetLong(interp, args[i], &bits);
		str = Jim_GetString(args[i + 1], &len);

		fields[field_count].num_bits = bits;
Øyvind Harboe's avatar
Øyvind Harboe committed
184
185
186
187
		void * t = malloc(DIV_ROUND_UP(bits, 8));
		fields[field_count].out_value = t;
		str_to_buf(str, len, t, bits, 0);
		fields[field_count].in_value = t;
188
189
190
		field_count++;
	}

Øyvind Harboe's avatar
Øyvind Harboe committed
191
	jtag_add_dr_scan(tap, num_fields, fields, endstate);
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

	retval = jtag_execute_queue();
	if (retval != ERROR_OK)
	{
		Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
		return JIM_ERR;
	}

	field_count = 0;
	Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
	for (i = 2; i < argc; i += 2)
	{
		long bits;
		char *str;

		Jim_GetLong(interp, args[i], &bits);
		str = buf_to_str(fields[field_count].in_value, bits, 16);
Øyvind Harboe's avatar
Øyvind Harboe committed
209
		free((void *)fields[field_count].out_value);
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

		Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
		free(str);
		field_count++;
	}

	Jim_SetResult(interp, list);

	free(fields);

	return JIM_OK;
}


static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
{
	tap_state_t states[8];

	if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
	{
		Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
		return JIM_ERR;
	}

	script_debug(interp, "pathmove", argc, args);

	int i;
	for (i = 0; i < argc-1; i++)
	{
		const char *cp;
		cp = Jim_GetString(args[i + 1], NULL);
		states[i] = tap_state_by_name(cp);
		if (states[i] < 0)
		{
			/* update the error message */
Øyvind Harboe's avatar
Øyvind Harboe committed
245
			Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
			return JIM_ERR;
		}
	}

	if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
	{
		Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
		return JIM_ERR;
	}

	jtag_add_pathmove(argc-2, states + 1);

	if (jtag_execute_queue()!= ERROR_OK)
	{
		Jim_SetResultString(interp, "pathmove: failed",-1);
		return JIM_ERR;
	}

	return JIM_OK;
}


static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
{
	script_debug(interp, "flush_count", argc, args);

	Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));

	return JIM_OK;
}

David Brownell's avatar
David Brownell committed
277
278
279
280
281
282
283
284
285
286
/* REVISIT Just what about these should "move" ... ?
 * These registrations, into the main JTAG table?
 *
 * There's a minor compatibility issue, these all show up twice;
 * that's not desirable:
 *  - jtag drscan ... NOT DOCUMENTED!
 *  - drscan ...
 *
 * The "irscan" command (for example) doesn't show twice.
 */
287
288
289
290
static const struct command_registration jtag_command_handlers_to_move[] = {
	{
		.name = "drscan",
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
291
292
293
294
		.jim_handler = Jim_Command_drscan,
		.help = "Execute Data Register (DR) scan for one TAP.  "
			"Other TAPs must be in BYPASS mode.",
		.usage = "tap_name [num_bits value]* ['-endstate' state_name]",
295
296
297
298
	},
	{
		.name = "flush_count",
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
299
300
301
		.jim_handler = Jim_Command_flush_count,
		.help = "Returns the number of times the JTAG queue "
			"has been flushed.",
302
303
304
305
	},
	{
		.name = "pathmove",
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
306
307
308
309
		.jim_handler = Jim_Command_pathmove,
		.usage = "start_state state1 [state2 [state3 ...]]",
		.help = "Move JTAG state machine from current state "
			"(start_state) to state1, then state2, state3, etc.",
310
311
312
313
314
	},
	COMMAND_REGISTRATION_DONE
};


315
316
317
318
319
320
321
322
323
324
enum jtag_tap_cfg_param {
	JCFG_EVENT
};

static Jim_Nvp nvp_config_opts[] = {
	{ .name = "-event",      .value = JCFG_EVENT },

	{ .name = NULL,          .value = -1 }
};

Zachary T Welch's avatar
Zachary T Welch committed
325
static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
326
{
Zachary T Welch's avatar
Zachary T Welch committed
327
328
329
330
331
332
	if (goi->argc == 0)
	{
		Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
		return JIM_ERR;
	}

333
	Jim_Nvp *n;
Zachary T Welch's avatar
Zachary T Welch committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
	int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
	if (e != JIM_OK)
	{
		Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
		return e;
	}

	if (goi->isconfigure) {
		if (goi->argc != 1) {
			Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
			return JIM_ERR;
		}
	} else {
		if (goi->argc != 0) {
			Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
			return JIM_ERR;
		}
	}

	struct jtag_tap_event_action *jteap  = tap->event_action;
	/* replace existing event body */
	bool found = false;
	while (jteap)
	{
		if (jteap->event == (enum jtag_event)n->value)
		{
			found = true;
			break;
		}
		jteap = jteap->next;
	}
365

Zachary T Welch's avatar
Zachary T Welch committed
366
367
368
369
370
371
372
	Jim_SetEmptyResult(goi->interp);

	if (goi->isconfigure)
	{
		if (!found)
			jteap = calloc(1, sizeof(*jteap));
		else if (NULL != jteap->body)
373
			Jim_DecrRefCount(goi->interp, jteap->body);
Zachary T Welch's avatar
Zachary T Welch committed
374

375
		jteap->interp = goi->interp;
Zachary T Welch's avatar
Zachary T Welch committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
		jteap->event = n->value;

		Jim_Obj *o;
		Jim_GetOpt_Obj(goi, &o);
		jteap->body = Jim_DuplicateObj(goi->interp, o);
		Jim_IncrRefCount(jteap->body);

		if (!found)
		{
			/* add to head of event list */
			jteap->next = tap->event_action;
			tap->event_action = jteap;
		}
	}
	else if (found)
	{
392
		jteap->interp = goi->interp;
Zachary T Welch's avatar
Zachary T Welch committed
393
394
395
396
397
398
399
400
		Jim_SetResult(goi->interp,
			Jim_DuplicateObj(goi->interp, jteap->body));
	}
	return JIM_OK;
}

static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
{
401
	/* parse config or cget options */
Zachary T Welch's avatar
Zachary T Welch committed
402
403
	while (goi->argc > 0)
	{
404
405
		Jim_SetEmptyResult (goi->interp);

Zachary T Welch's avatar
Zachary T Welch committed
406
407
408
409
		Jim_Nvp *n;
		int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
		if (e != JIM_OK)
		{
410
411
412
413
			Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
			return e;
		}

Zachary T Welch's avatar
Zachary T Welch committed
414
415
416
417
418
419
420
421
		switch (n->value)
		{
		case JCFG_EVENT:
			e = jtag_tap_configure_event(goi, tap);
			if (e != JIM_OK)
				return e;
			break;
		default:
Øyvind Harboe's avatar
Øyvind Harboe committed
422
			Jim_SetResultFormatted(goi->interp, "unknown event: %s", n->name);
Zachary T Welch's avatar
Zachary T Welch committed
423
			return JIM_ERR;
424
		}
Zachary T Welch's avatar
Zachary T Welch committed
425
	}
426
427
428

	return JIM_OK;
}
429

zwelch's avatar
zwelch committed
430
431
432
433
434
435
436
437
438
static int is_bad_irval(int ir_length, jim_wide w)
{
	jim_wide v = 1;

	v <<= ir_length;
	v -= 1;
	v = ~v;
	return (w & v) != 0;
}
439

440
441
442
443
444
445
static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
		struct jtag_tap *pTap)
{
	jim_wide w;
	int e = Jim_GetOpt_Wide(goi, &w);
	if (e != JIM_OK) {
Øyvind Harboe's avatar
Øyvind Harboe committed
446
		Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
447
448
449
450
451
452
453
		return e;
	}

	unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
	uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
	if (new_expected_ids == NULL)
	{
Øyvind Harboe's avatar
Øyvind Harboe committed
454
		Jim_SetResultFormatted(goi->interp, "no memory");
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
		return JIM_ERR;
	}

	memcpy(new_expected_ids, pTap->expected_ids, expected_len);

	new_expected_ids[pTap->expected_ids_cnt] = w;

	free(pTap->expected_ids);
	pTap->expected_ids = new_expected_ids;
	pTap->expected_ids_cnt++;

	return JIM_OK;
}

#define NTAP_OPT_IRLEN     0
#define NTAP_OPT_IRMASK    1
#define NTAP_OPT_IRCAPTURE 2
#define NTAP_OPT_ENABLED   3
#define NTAP_OPT_DISABLED  4
#define NTAP_OPT_EXPECTED_ID 5
475
#define NTAP_OPT_VERSION   6
476
477
478
479
480
481
482
483

static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
		struct jtag_tap *pTap)
{
	jim_wide w;
	int e = Jim_GetOpt_Wide(goi, &w);
	if (e != JIM_OK)
	{
Øyvind Harboe's avatar
Øyvind Harboe committed
484
		Jim_SetResultFormatted(goi->interp,
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
				"option: %s bad parameter", n->name);
		free((void *)pTap->dotted_name);
		return e;
	}
	switch (n->value) {
	case NTAP_OPT_IRLEN:
		if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
		{
			LOG_WARNING("%s: huge IR length %d",
					pTap->dotted_name, (int) w);
		}
		pTap->ir_length = w;
		break;
	case NTAP_OPT_IRMASK:
		if (is_bad_irval(pTap->ir_length, w))
		{
			LOG_ERROR("%s: IR mask %x too big",
					pTap->dotted_name,
					(int) w);
			return JIM_ERR;
		}
		if ((w & 3) != 3)
			LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
		pTap->ir_capture_mask = w;
		break;
	case NTAP_OPT_IRCAPTURE:
		if (is_bad_irval(pTap->ir_length, w))
		{
			LOG_ERROR("%s: IR capture %x too big",
					pTap->dotted_name, (int) w);
			return JIM_ERR;
		}
		if ((w & 3) != 1)
			LOG_WARNING("%s: nonstandard IR value",
					pTap->dotted_name);
		pTap->ir_capture_value = w;
		break;
	default:
		return JIM_ERR;
	}
	return JIM_OK;
}

528
static int jim_newtap_cmd(Jim_GetOptInfo *goi)
529
{
530
	struct jtag_tap *pTap;
531
532
533
534
535
536
537
538
	int x;
	int e;
	Jim_Nvp *n;
	char *cp;
	const Jim_Nvp opts[] = {
		{ .name = "-irlen"			,	.value = NTAP_OPT_IRLEN },
		{ .name = "-irmask"			,	.value = NTAP_OPT_IRMASK },
		{ .name = "-ircapture"		,	.value = NTAP_OPT_IRCAPTURE },
539
540
541
		{ .name = "-enable"			,	.value = NTAP_OPT_ENABLED },
		{ .name = "-disable"		,	.value = NTAP_OPT_DISABLED },
		{ .name = "-expected-id"	,	.value = NTAP_OPT_EXPECTED_ID },
542
		{ .name = "-ignore-version"	,	.value = NTAP_OPT_VERSION },
543
		{ .name = NULL				,	.value = -1 },
544
545
	};

546
	pTap = calloc(1, sizeof(struct jtag_tap));
zwelch's avatar
zwelch committed
547
	if (!pTap) {
Øyvind Harboe's avatar
Øyvind Harboe committed
548
		Jim_SetResultFormatted(goi->interp, "no memory");
549
550
		return JIM_ERR;
	}
zwelch's avatar
zwelch committed
551

552
553
554
	/*
	 * we expect CHIP + TAP + OPTIONS
	 * */
zwelch's avatar
zwelch committed
555
	if (goi->argc < 3) {
Øyvind Harboe's avatar
Øyvind Harboe committed
556
		Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
557
		free(pTap);
558
559
		return JIM_ERR;
	}
560
	Jim_GetOpt_String(goi, &cp, NULL);
561
562
	pTap->chip = strdup(cp);

563
	Jim_GetOpt_String(goi, &cp, NULL);
564
	pTap->tapname = strdup(cp);
oharboe's avatar
oharboe committed
565

566
	/* name + dot + name + null */
567
	x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
568
569
	cp = malloc(x);
	sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
570
571
	pTap->dotted_name = cp;

oharboe's avatar
oharboe committed
572
	LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
573
574
			  pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);

575
576
577
578
579
580
	/* IEEE specifies that the two LSBs of an IR scan are 01, so make
	 * that the default.  The "-irlen" and "-irmask" options are only
	 * needed to cope with nonstandard TAPs, or to specify more bits.
	 */
	pTap->ir_capture_mask = 0x03;
	pTap->ir_capture_value = 0x01;
581

zwelch's avatar
zwelch committed
582
	while (goi->argc) {
583
		e = Jim_GetOpt_Nvp(goi, opts, &n);
zwelch's avatar
zwelch committed
584
		if (e != JIM_OK) {
585
			Jim_GetOpt_NvpUnknown(goi, opts, 0);
586
587
			free((void *)pTap->dotted_name);
			free(pTap);
588
589
			return e;
		}
590
		LOG_DEBUG("Processing option: %s", n->name);
zwelch's avatar
zwelch committed
591
		switch (n->value) {
592
		case NTAP_OPT_ENABLED:
zwelch's avatar
zwelch committed
593
			pTap->disabled_after_reset = false;
594
595
			break;
		case NTAP_OPT_DISABLED:
zwelch's avatar
zwelch committed
596
			pTap->disabled_after_reset = true;
597
598
			break;
		case NTAP_OPT_EXPECTED_ID:
599
600
601
			e = jim_newtap_expected_id(n, goi, pTap);
			if (JIM_OK != e)
			{
602
603
				free((void *)pTap->dotted_name);
				free(pTap);
604
605
				return e;
			}
606
607
608
609
			break;
		case NTAP_OPT_IRLEN:
		case NTAP_OPT_IRMASK:
		case NTAP_OPT_IRCAPTURE:
610
611
612
			e = jim_newtap_ir_param(n, goi, pTap);
			if (JIM_OK != e)
			{
613
614
				free((void *)pTap->dotted_name);
				free(pTap);
615
616
				return e;
			}
617
			break;
618
619
620
		case NTAP_OPT_VERSION:
			pTap->ignore_version = true;
			break;
zwelch's avatar
zwelch committed
621
		} /* switch (n->value) */
622
	} /* while (goi->argc) */
623

zwelch's avatar
zwelch committed
624
625
626
	/* default is enabled-after-reset */
	pTap->enabled = !pTap->disabled_after_reset;

627
	/* Did all the required option bits get cleared? */
628
	if (pTap->ir_length != 0)
629
630
	{
		jtag_tap_init(pTap);
631
		return JIM_OK;
632
633
	}

Øyvind Harboe's avatar
Øyvind Harboe committed
634
	Jim_SetResultFormatted(goi->interp,
635
			"newtap: %s missing IR length",
636
			pTap->dotted_name);
637
	jtag_tap_free(pTap);
638
639
	return JIM_ERR;
}
640

641
static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
642
{
643
	struct jtag_tap_event_action * jteap;
644

645
646
647
648
649
650
651
652
653
654
	for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
	{
		if (jteap->event != e)
			continue;

		Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
		LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
				tap->dotted_name, e, nvp->name,
				Jim_GetString(jteap->body, NULL));

655
		if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
656
		{
Øyvind Harboe's avatar
Øyvind Harboe committed
657
			Jim_MakeErrorMessage(jteap->interp);
Eric Wetzel's avatar
Eric Wetzel committed
658
			LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
			continue;
		}

		switch (e)
		{
		case JTAG_TAP_EVENT_ENABLE:
		case JTAG_TAP_EVENT_DISABLE:
			/* NOTE:  we currently assume the handlers
			 * can't fail.  Right here is where we should
			 * really be verifying the scan chains ...
			 */
			tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
			LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
				tap->enabled ? "enabled" : "disabled");
			break;
		default:
			break;
676
677
678
679
		}
	}
}

680
681
682
683
684
685
686
687
static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
	if (goi.argc != 0) {
		Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
		return JIM_ERR;
	}
688
	struct command_context *context = current_command_context(interp);
689
690
	int e = jtag_init_inner(context);
	if (e != ERROR_OK) {
Øyvind Harboe's avatar
Øyvind Harboe committed
691
692
693
		Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
		Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
		Jim_FreeNewObj(goi.interp, eObj);
694
695
696
697
		return JIM_ERR;
	}
	return JIM_OK;
}
zwelch's avatar
zwelch committed
698

699
700
701
702
703
704
705
706
static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
	if (goi.argc != 0) {
		Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
		return JIM_ERR;
	}
707
	struct command_context *context = current_command_context(interp);
708
709
	int e = jtag_init_reset(context);
	if (e != ERROR_OK) {
Øyvind Harboe's avatar
Øyvind Harboe committed
710
711
712
		Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
		Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
		Jim_FreeNewObj(goi.interp, eObj);
713
714
715
716
		return JIM_ERR;
	}
	return JIM_OK;
}
zwelch's avatar
zwelch committed
717

718
int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
719
720
721
722
723
{
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
	return jim_newtap_cmd(&goi);
}
zwelch's avatar
zwelch committed
724

725
726
727
728
729
730
731
static bool jtag_tap_enable(struct jtag_tap *t)
{
	if (t->enabled)
		return false;
	jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
	if (!t->enabled)
		return false;
732

733
734
735
736
737
738
739
740
741
742
743
744
745
746
	/* FIXME add JTAG sanity checks, w/o TLR
	 *  - scan chain length grew by one (this)
	 *  - IDs and IR lengths are as expected
	 */
	jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
	return true;
}
static bool jtag_tap_disable(struct jtag_tap *t)
{
	if (!t->enabled)
		return false;
	jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
	if (t->enabled)
		return false;
747

748
749
750
751
752
753
754
	/* FIXME add JTAG sanity checks, w/o TLR
	 *  - scan chain length shrank by one (this)
	 *  - IDs and IR lengths are as expected
	 */
	jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
	return true;
}
755

756
757
758
759
760
761
static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	const char *cmd_name = Jim_GetString(argv[0], NULL);
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
	if (goi.argc != 1) {
Øyvind Harboe's avatar
Øyvind Harboe committed
762
		Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
763
764
		return JIM_ERR;
	}
765

766
	struct jtag_tap *t;
767

768
769
770
	t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
	if (t == NULL)
		return JIM_ERR;
771

772
773
774
775
	if (strcasecmp(cmd_name, "tapisenabled") == 0) {
		// do nothing, just return the value
	} else if (strcasecmp(cmd_name, "tapenable") == 0) {
		if (!jtag_tap_enable(t))
776
			LOG_WARNING("failed to enable tap %s", t->dotted_name);
777
778
	} else if (strcasecmp(cmd_name, "tapdisable") == 0) {
		if (!jtag_tap_disable(t))
779
			LOG_WARNING("failed to disable tap %s", t->dotted_name);
780
781
782
783
784
785
786
787
	} else {
		LOG_ERROR("command '%s' unknown", cmd_name);
		return JIM_ERR;
	}
	bool e = t->enabled;
	Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
	return JIM_OK;
}
788

789
790
791
792
793
794
795
796
797
798
799
static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	const char *cmd_name = Jim_GetString(argv[0], NULL);
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
	goi.isconfigure = !strcmp(cmd_name, "configure");
	if (goi.argc < 2 + goi.isconfigure) {
		Jim_WrongNumArgs(goi.interp, 0, NULL,
				"<tap_name> <attribute> ...");
		return JIM_ERR;
	}
800

801
	struct jtag_tap *t;
zwelch's avatar
zwelch committed
802

803
804
805
806
807
808
	Jim_Obj *o;
	Jim_GetOpt_Obj(&goi, &o);
	t = jtag_tap_by_jim_obj(goi.interp, o);
	if (t == NULL) {
		return JIM_ERR;
	}
zwelch's avatar
zwelch committed
809

810
811
	return jtag_tap_configure_cmd(&goi, t);
}
zwelch's avatar
zwelch committed
812

813
814
815
816
817
818
819
static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	Jim_GetOptInfo goi;
	Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
	if (goi.argc != 0) {
		Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
		return JIM_ERR;
820
	}
821
822
	Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
	struct jtag_tap *tap;
823

824
825
826
827
828
829
830
	for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
		Jim_ListAppendElement(goi.interp,
			Jim_GetResult(goi.interp),
			Jim_NewStringObj(goi.interp,
				tap->dotted_name, -1));
	}
	return JIM_OK;
831
832
}

833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
COMMAND_HANDLER(handle_jtag_init_command)
{
	if (CMD_ARGC != 0)
		return ERROR_COMMAND_SYNTAX_ERROR;

	static bool jtag_initialized = false;
	if (jtag_initialized)
	{
		LOG_INFO("'jtag init' has already been called");
		return ERROR_OK;
	}
	jtag_initialized = true;

	LOG_DEBUG("Initializing jtag devices...");
	return jtag_init(CMD_CTX);
}

850
static const struct command_registration jtag_subcommand_handlers[] = {
851
852
	{
		.name = "init",
853
		.mode = COMMAND_ANY,
David Brownell's avatar
David Brownell committed
854
		.handler = handle_jtag_init_command,
855
856
		.help = "initialize jtag scan chain",
	},
857
858
859
	{
		.name = "arp_init",
		.mode = COMMAND_ANY,
David Brownell's avatar
David Brownell committed
860
861
862
863
		.jim_handler = jim_jtag_arp_init,
		.help = "Validates JTAG scan chain against the list of "
			"declared TAPs using just the four standard JTAG "
			"signals.",
864
865
866
867
	},
	{
		.name = "arp_init-reset",
		.mode = COMMAND_ANY,
David Brownell's avatar
David Brownell committed
868
869
870
		.jim_handler = jim_jtag_arp_init_reset,
		.help = "Uses TRST and SRST to try resetting everything on "
			"the JTAG scan chain, then performs 'jtag arp_init'."
871
872
873
874
	},
	{
		.name = "newtap",
		.mode = COMMAND_CONFIG,
David Brownell's avatar
David Brownell committed
875
876
877
878
879
880
881
882
883
		.jim_handler = jim_jtag_newtap,
		.help = "Create a new TAP instance named basename.tap_type, "
			"and appends it to the scan chain.",
		.usage = "basename tap_type '-irlen' count "
			"['-enable'|'-disable'] "
			"['-expected_id' number] "
			"['-ignore-version'] "
			"['-ircapture' number] "
			"['-mask' number] ",
884
885
886
887
	},
	{
		.name = "tapisenabled",
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
888
889
890
891
		.jim_handler = jim_jtag_tap_enabler,
		.help = "Returns a Tcl boolean (0/1) indicating whether "
			"the TAP is enabled (1) or not (0).",
		.usage = "tap_name",
892
893
894
895
	},
	{
		.name = "tapenable",
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
896
897
898
899
		.jim_handler = jim_jtag_tap_enabler,
		.help = "Try to enable the specified TAP using the "
			"'tap-enable' TAP event.",
		.usage = "tap_name",
900
901
902
903
	},
	{
		.name = "tapdisable",
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
904
905
906
907
		.jim_handler = jim_jtag_tap_enabler,
		.help = "Try to disable the specified TAP using the "
			"'tap-disable' TAP event.",
		.usage = "tap_name",
908
909
910
911
	},
	{
		.name = "configure",
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
912
913
914
915
		.jim_handler = jim_jtag_configure,
		.help = "Provide a Tcl handler for the specified "
			"TAP event.",
		.usage = "tap_name '-event' event_name handler",
916
917
918
919
	},
	{
		.name = "cget",
		.mode = COMMAND_EXEC,
David Brownell's avatar
David Brownell committed
920
921
922
923
		.jim_handler = jim_jtag_configure,
		.help = "Return any Tcl handler for the specified "
			"TAP event.",
		.usage = "tap_name '-event' event_name",
924
925
926
927
	},
	{
		.name = "names",
		.mode = COMMAND_ANY,
David Brownell's avatar
David Brownell committed
928
929
		.jim_handler = jim_jtag_names,
		.help = "Returns list of all JTAG tap names.",
930
	},
931
932
933
	{
		.chain = jtag_command_handlers_to_move,
	},
934
935
	COMMAND_REGISTRATION_DONE
};
936

937
void jtag_notify_event(enum jtag_event event)
938
{
939
	struct jtag_tap *tap;
940

941
	for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
942
		jtag_tap_handle_event(tap, event);
943
944
945
}


946
COMMAND_HANDLER(handle_scan_chain_command)
947
{
948
	struct jtag_tap *tap;
949
	char expected_id[12];
950

951
	tap = jtag_all_taps();
952
953
954
955
	command_print(CMD_CTX,
"   TapName             Enabled  IdCode     Expected   IrLen IrCap IrMask");
	command_print(CMD_CTX,
"-- ------------------- -------- ---------- ---------- ----- ----- ------");
956

zwelch's avatar
zwelch committed
957
	while (tap) {
958
		uint32_t expected, expected_mask, ii;
959
960
961
962
963
964
965
966

		snprintf(expected_id, sizeof expected_id, "0x%08x",
				(unsigned)((tap->expected_ids_cnt > 0)
					? tap->expected_ids[0]
					: 0));
		if (tap->ignore_version)
			expected_id[2] = '*';

967
968
		expected = buf_get_u32(tap->expected, 0, tap->ir_length);
		expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
969

970
		command_print(CMD_CTX,
971
	"%2d %-18s     %c     0x%08x %s %5d 0x%02x  0x%02x",
972
973
974
					  tap->abs_chain_position,
					  tap->dotted_name,
					  tap->enabled ? 'Y' : 'n',
975
					  (unsigned int)(tap->idcode),
976
					  expected_id,
977
978
					  (unsigned int)(tap->ir_length),
					  (unsigned int)(expected),
979
					  (unsigned int)(expected_mask));
980
981

		for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
982
983
984
985
986
987
			snprintf(expected_id, sizeof expected_id, "0x%08x",
					(unsigned) tap->expected_ids[1]);
			if (tap->ignore_version)
				expected_id[2] = '*';

			command_print(CMD_CTX,
988
	"                                           %s",
989
						  expected_id);
990
991
		}

992
		tap = tap->next_tap;
993
994
995
996
997
	}

	return ERROR_OK;
}

998
COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
999
{
1000
	if (CMD_ARGC > 1)
For faster browsing, not all history is shown. View entire blame