tcl.c 40.3 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
 *   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
static const Jim_Nvp nvp_jtag_tap_event[] = {
44
	{ .value = JTAG_TAP_EVENT_POST_RESET,   .name = "post-reset" },
45
46
47
48
49
	{ .value = JTAG_TAP_EVENT_ENABLE,       .name = "tap-enable" },
	{ .value = JTAG_TAP_EVENT_DISABLE,      .name = "tap-disable" },

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

51
extern jtag_interface_t *jtag_interface;
52

53
54
55
56
57
58
/* jtag commands */
static int handle_interface_list_command(struct command_context_s *cmd_ctx,
		char *cmd, char **args, int argc);
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);
59
static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
60
61
62
63
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);
oharboe's avatar
oharboe committed
64

65
static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66

67
68
69
70
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);
oharboe's avatar
oharboe committed
71
static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
72
static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
73

74
75
76
static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77

78
79
extern int jtag_examine_chain(void);
extern int jtag_validate_chain(void);
80

81
82
83
84
85
86
87
88
89
90
enum jtag_tap_cfg_param {
	JCFG_EVENT
};

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

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

91
static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, jtag_tap_t * tap)
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
{
	Jim_Nvp *n;
	Jim_Obj *o;
	int e;

	/* parse config or cget options */
	while (goi->argc > 0) {
		Jim_SetEmptyResult (goi->interp);

		e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
		if (e != JIM_OK) {
			Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
			return e;
		}

		switch (n->value) {
			case JCFG_EVENT:
				if (goi->argc == 0) {
110
					Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
111
112
113
					return JIM_ERR;
				}

114
				e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
				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;
					}
				}

				{
					jtag_tap_event_action_t *jteap;

					jteap = tap->event_action;
					/* replace existing? */
					while (jteap) {
zwelch's avatar
zwelch committed
138
						if (jteap->event == (enum jtag_event)n->value) {
139
140
141
142
143
144
							break;
						}
						jteap = jteap->next;
					}

					if (goi->isconfigure) {
145
						bool replace = true;
146
147
148
						if (jteap == NULL) {
							/* create new */
							jteap = calloc(1, sizeof (*jteap));
149
							replace = false;
150
151
						}
						jteap->event = n->value;
152
						Jim_GetOpt_Obj(goi, &o);
153
154
155
156
157
158
						if (jteap->body) {
							Jim_DecrRefCount(interp, jteap->body);
						}
						jteap->body = Jim_DuplicateObj(goi->interp, o);
						Jim_IncrRefCount(jteap->body);

159
160
161
162
163
164
						if (!replace)
						{
							/* add to head of event list */
							jteap->next = tap->event_action;
							tap->event_action = jteap;
						}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
						Jim_SetEmptyResult(goi->interp);
					} else {
						/* get */
						if (jteap == NULL) {
							Jim_SetEmptyResult(goi->interp);
						} else {
							Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
						}
					}
				}
				/* loop for more */
				break;
		}
	} /* while (goi->argc) */

	return JIM_OK;
}
182

zwelch's avatar
zwelch committed
183
184
185
186
187
188
189
190
191
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;
}
192

193
static int jim_newtap_cmd(Jim_GetOptInfo *goi)
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{
	jtag_tap_t *pTap;
	jim_wide w;
	int x;
	int e;
	int reqbits;
	Jim_Nvp *n;
	char *cp;
	const Jim_Nvp opts[] = {
#define NTAP_OPT_IRLEN     0
		{ .name = "-irlen"			,	.value = NTAP_OPT_IRLEN },
#define NTAP_OPT_IRMASK    1
		{ .name = "-irmask"			,	.value = NTAP_OPT_IRMASK },
#define NTAP_OPT_IRCAPTURE 2
		{ .name = "-ircapture"		,	.value = NTAP_OPT_IRCAPTURE },
#define NTAP_OPT_ENABLED   3
210
		{ .name = "-enable"			,	.value = NTAP_OPT_ENABLED },
211
#define NTAP_OPT_DISABLED  4
212
		{ .name = "-disable"		,	.value = NTAP_OPT_DISABLED },
213
#define NTAP_OPT_EXPECTED_ID 5
214
215
		{ .name = "-expected-id"	,	.value = NTAP_OPT_EXPECTED_ID },
		{ .name = NULL				,	.value = -1 },
216
217
	};

zwelch's avatar
zwelch committed
218
219
220
	pTap = calloc(1, sizeof(jtag_tap_t));
	if (!pTap) {
		Jim_SetResult_sprintf(goi->interp, "no memory");
221
222
		return JIM_ERR;
	}
zwelch's avatar
zwelch committed
223

224
225
226
	/*
	 * we expect CHIP + TAP + OPTIONS
	 * */
zwelch's avatar
zwelch committed
227
	if (goi->argc < 3) {
228
		Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
229
		free(pTap);
230
231
		return JIM_ERR;
	}
232
	Jim_GetOpt_String(goi, &cp, NULL);
233
234
	pTap->chip = strdup(cp);

235
	Jim_GetOpt_String(goi, &cp, NULL);
236
	pTap->tapname = strdup(cp);
oharboe's avatar
oharboe committed
237

238
	/* name + dot + name + null */
239
	x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
240
241
	cp = malloc(x);
	sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
242
243
	pTap->dotted_name = cp;

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

247
	/* deal with options */
248
249
250
251
#define NTREQ_IRLEN      1
#define NTREQ_IRCAPTURE  2
#define NTREQ_IRMASK     4

252
	/* clear them as we find them */
253
	reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
oharboe's avatar
oharboe committed
254

zwelch's avatar
zwelch committed
255
	while (goi->argc) {
256
		e = Jim_GetOpt_Nvp(goi, opts, &n);
zwelch's avatar
zwelch committed
257
		if (e != JIM_OK) {
258
			Jim_GetOpt_NvpUnknown(goi, opts, 0);
259
260
			free((void *)pTap->dotted_name);
			free(pTap);
261
262
			return e;
		}
263
		LOG_DEBUG("Processing option: %s", n->name);
zwelch's avatar
zwelch committed
264
		switch (n->value) {
265
		case NTAP_OPT_ENABLED:
zwelch's avatar
zwelch committed
266
			pTap->disabled_after_reset = false;
267
268
			break;
		case NTAP_OPT_DISABLED:
zwelch's avatar
zwelch committed
269
			pTap->disabled_after_reset = true;
270
271
			break;
		case NTAP_OPT_EXPECTED_ID:
272
		{
273
			uint32_t *new_expected_ids;
274

275
			e = Jim_GetOpt_Wide(goi, &w);
276
			if (e != JIM_OK) {
277
				Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
278
279
				free((void *)pTap->dotted_name);
				free(pTap);
280
281
282
				return e;
			}

283
			new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
284
			if (new_expected_ids == NULL) {
285
				Jim_SetResult_sprintf(goi->interp, "no memory");
286
287
				free((void *)pTap->dotted_name);
				free(pTap);
288
289
290
				return JIM_ERR;
			}

291
			memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
292
293

			new_expected_ids[pTap->expected_ids_cnt] = w;
294
295

			free(pTap->expected_ids);
296
297
			pTap->expected_ids = new_expected_ids;
			pTap->expected_ids_cnt++;
298
			break;
299
		}
300
301
302
		case NTAP_OPT_IRLEN:
		case NTAP_OPT_IRMASK:
		case NTAP_OPT_IRCAPTURE:
303
			e = Jim_GetOpt_Wide(goi, &w);
zwelch's avatar
zwelch committed
304
			if (e != JIM_OK) {
305
				Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
306
307
				free((void *)pTap->dotted_name);
				free(pTap);
308
309
				return e;
			}
zwelch's avatar
zwelch committed
310
			switch (n->value) {
311
			case NTAP_OPT_IRLEN:
zwelch's avatar
zwelch committed
312
				if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
zwelch's avatar
zwelch committed
313
					LOG_WARNING("huge IR length %d", (int) w);
314
315
316
317
				pTap->ir_length = w;
				reqbits &= (~(NTREQ_IRLEN));
				break;
			case NTAP_OPT_IRMASK:
zwelch's avatar
zwelch committed
318
319
320
				if (is_bad_irval(pTap->ir_length, w)) {
					LOG_ERROR("IR mask %x too big",
							(int) w);
321
322
					free((void *)pTap->dotted_name);
					free(pTap);
zwelch's avatar
zwelch committed
323
324
					return ERROR_FAIL;
				}
325
326
327
328
				pTap->ir_capture_mask = w;
				reqbits &= (~(NTREQ_IRMASK));
				break;
			case NTAP_OPT_IRCAPTURE:
zwelch's avatar
zwelch committed
329
330
331
				if (is_bad_irval(pTap->ir_length, w)) {
					LOG_ERROR("IR capture %x too big",
							(int) w);
332
333
					free((void *)pTap->dotted_name);
					free(pTap);
zwelch's avatar
zwelch committed
334
335
					return ERROR_FAIL;
				}
336
337
338
339
				pTap->ir_capture_value = w;
				reqbits &= (~(NTREQ_IRCAPTURE));
				break;
			}
zwelch's avatar
zwelch committed
340
		} /* switch (n->value) */
341
	} /* while (goi->argc) */
342

zwelch's avatar
zwelch committed
343
344
345
	/* default is enabled-after-reset */
	pTap->enabled = !pTap->disabled_after_reset;

346
347
348
349
350
	/* Did all the required option bits get cleared? */
	if (0 == reqbits)
	{
		jtag_tap_init(pTap);
		return ERROR_OK;
351
352
	}

353
354
355
	Jim_SetResult_sprintf(goi->interp,
			"newtap: %s missing required parameters",
			pTap->dotted_name);
356
	jtag_tap_free(pTap);
357
358
	return JIM_ERR;
}
359

zwelch's avatar
zwelch committed
360
static void jtag_tap_handle_event(jtag_tap_t *tap, enum jtag_event e)
361
362
363
364
365
366
367
368
369
370
{
	jtag_tap_event_action_t * jteap;
	int done;

	jteap = tap->event_action;

	done = 0;
	while (jteap) {
		if (jteap->event == e) {
			done = 1;
371
			LOG_DEBUG("JTAG tap: %s event: %d (%s) action: %s\n",
372
373
374
					tap->dotted_name,
					e,
					Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
375
					Jim_GetString(jteap->body, NULL));
376
377
			if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
				Jim_PrintErrorMessage(interp);
zwelch's avatar
zwelch committed
378
379
380
381
382
			} else {
				/* NOTE:  we currently assume the handlers
				 * can't fail.  That presumes later code
				 * will be verifying the scan chains ...
				 */
383
384
				if (e == JTAG_TAP_EVENT_ENABLE)
					tap->enabled = true;
385
386
387
388
389
390
391
			}
		}

		jteap = jteap->next;
	}

	if (!done) {
392
		LOG_DEBUG("event %d %s - no action",
393
				e,
394
				Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name);
395
396
397
398
	}
}


399
static int jim_jtag_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
400
401
402
403
{
	Jim_GetOptInfo goi;
	int e;
	Jim_Nvp *n;
404
	Jim_Obj *o;
405
406
407
408
409
	struct command_context_s *context;

	enum {
		JTAG_CMD_INTERFACE,
		JTAG_CMD_INIT_RESET,
410
411
412
		JTAG_CMD_NEWTAP,
		JTAG_CMD_TAPENABLE,
		JTAG_CMD_TAPDISABLE,
413
414
		JTAG_CMD_TAPISENABLED,
		JTAG_CMD_CONFIGURE,
zwelch's avatar
zwelch committed
415
416
		JTAG_CMD_CGET,
		JTAG_CMD_NAMES,
417
	};
418

419
420
421
	const Jim_Nvp jtag_cmds[] = {
		{ .name = "interface"     , .value = JTAG_CMD_INTERFACE },
		{ .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
422
423
424
425
		{ .name = "newtap"        , .value = JTAG_CMD_NEWTAP },
		{ .name = "tapisenabled"     , .value = JTAG_CMD_TAPISENABLED },
		{ .name = "tapenable"     , .value = JTAG_CMD_TAPENABLE },
		{ .name = "tapdisable"    , .value = JTAG_CMD_TAPDISABLE },
426
427
		{ .name = "configure"     , .value = JTAG_CMD_CONFIGURE },
		{ .name = "cget"          , .value = JTAG_CMD_CGET },
zwelch's avatar
zwelch committed
428
		{ .name = "names"         , .value = JTAG_CMD_NAMES },
429

430
431
432
433
		{ .name = NULL, .value = -1 },
	};

	context = Jim_GetAssocData(interp, "context");
434
	/* go past the command */
435
	Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
436

437
	e = Jim_GetOpt_Nvp(&goi, jtag_cmds, &n);
zwelch's avatar
zwelch committed
438
	if (e != JIM_OK) {
439
		Jim_GetOpt_NvpUnknown(&goi, jtag_cmds, 0);
440
441
		return e;
	}
442
		Jim_SetEmptyResult(goi.interp);
zwelch's avatar
zwelch committed
443
	switch (n->value) {
444
	case JTAG_CMD_INTERFACE:
445
446
447
		/* return the name of the interface */
		/* TCL code might need to know the exact type... */
		/* FUTURE: we allow this as a means to "set" the interface. */
zwelch's avatar
zwelch committed
448
		if (goi.argc != 0) {
449
			Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
450
451
			return JIM_ERR;
		}
452
		Jim_SetResultString(goi.interp, jtag_interface->name, -1);
453
454
		return JIM_OK;
	case JTAG_CMD_INIT_RESET:
zwelch's avatar
zwelch committed
455
		if (goi.argc != 0) {
456
			Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
457
458
459
			return JIM_ERR;
		}
		e = jtag_init_reset(context);
zwelch's avatar
zwelch committed
460
		if (e != ERROR_OK) {
461
			Jim_SetResult_sprintf(goi.interp, "error: %d", e);
462
463
464
			return JIM_ERR;
		}
		return JIM_OK;
465
	case JTAG_CMD_NEWTAP:
466
		return jim_newtap_cmd(&goi);
467
468
469
470
		break;
	case JTAG_CMD_TAPISENABLED:
	case JTAG_CMD_TAPENABLE:
	case JTAG_CMD_TAPDISABLE:
zwelch's avatar
zwelch committed
471
		if (goi.argc != 1) {
472
			Jim_SetResultString(goi.interp, "Too many parameters",-1);
473
474
			return JIM_ERR;
		}
oharboe's avatar
oharboe committed
475
476

		{
477
			jtag_tap_t *t;
zwelch's avatar
zwelch committed
478
479
480

			t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
			if (t == NULL)
481
				return JIM_ERR;
zwelch's avatar
zwelch committed
482
483

			switch (n->value) {
484
485
486
			case JTAG_CMD_TAPISENABLED:
				break;
			case JTAG_CMD_TAPENABLE:
zwelch's avatar
zwelch committed
487
488
489
490
491
492
493
494
495
496
				if (t->enabled)
					break;
				jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
				if (!t->enabled)
					break;

				/* FIXME add JTAG sanity checks, w/o TLR
				 *  - scan chain length grew by one (this)
				 *  - IDs and IR lengths are as expected
				 */
zwelch's avatar
zwelch committed
497
498

				jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
499
500
				break;
			case JTAG_CMD_TAPDISABLE:
zwelch's avatar
zwelch committed
501
502
503
504
505
506
507
508
509
510
				if (!t->enabled)
					break;
				jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
				if (t->enabled)
					break;

				/* FIXME add JTAG sanity checks, w/o TLR
				 *  - scan chain length shrank by one (this)
				 *  - IDs and IR lengths are as expected
				 */
zwelch's avatar
zwelch committed
511
512

				jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
513
514
				break;
			}
zwelch's avatar
zwelch committed
515
516
			e = t->enabled;
			Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
517
518
			return JIM_OK;
		}
519
520
521
		break;

	case JTAG_CMD_CGET:
zwelch's avatar
zwelch committed
522
		if (goi.argc < 2) {
zwelch's avatar
zwelch committed
523
524
			Jim_WrongNumArgs(goi.interp, 0, NULL,
					"cget tap_name queryparm");
525
526
527
528
529
530
531
			return JIM_ERR;
		}

		{
			jtag_tap_t *t;

			Jim_GetOpt_Obj(&goi, &o);
532
			t = jtag_tap_by_jim_obj(goi.interp, o);
zwelch's avatar
zwelch committed
533
			if (t == NULL) {
534
535
536
537
				return JIM_ERR;
			}

			goi.isconfigure = 0;
538
			return jtag_tap_configure_cmd(&goi, t);
539
540
541
542
		}
		break;

	case JTAG_CMD_CONFIGURE:
zwelch's avatar
zwelch committed
543
		if (goi.argc < 3) {
zwelch's avatar
zwelch committed
544
545
			Jim_WrongNumArgs(goi.interp, 0, NULL,
					"configure tap_name attribute value ...");
546
547
548
549
550
551
552
			return JIM_ERR;
		}

		{
			jtag_tap_t *t;

			Jim_GetOpt_Obj(&goi, &o);
553
			t = jtag_tap_by_jim_obj(goi.interp, o);
zwelch's avatar
zwelch committed
554
			if (t == NULL) {
555
556
557
558
				return JIM_ERR;
			}

			goi.isconfigure = 1;
559
			return jtag_tap_configure_cmd(&goi, t);
560
		}
zwelch's avatar
zwelch committed
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
		break;

	case JTAG_CMD_NAMES:
		if (goi.argc != 0) {
			Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
			return JIM_ERR;
		}
		Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
		{
			jtag_tap_t *tap;

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

582
	}
583

584
585
586
	return JIM_ERR;
}

587
588
589
590
591
592
593
594
595
596
597

void jtag_notify_reset(void)
{
	jtag_tap_t *tap;
	for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
	{
		jtag_tap_handle_event(tap, JTAG_TAP_EVENT_POST_RESET);
	}
}


598
599
int jtag_register_commands(struct command_context_s *cmd_ctx)
{
600
	register_jim(cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
601

602
	register_command(cmd_ctx, NULL, "interface", handle_interface_command,
oharboe's avatar
oharboe committed
603
		COMMAND_CONFIG, "try to configure interface");
604
605
606
	register_command(cmd_ctx, NULL,
		"interface_list", &handle_interface_list_command,
		COMMAND_ANY, "list all built-in interfaces");
607
	register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
zwelch's avatar
zwelch committed
608
		COMMAND_ANY, "(DEPRECATED) set jtag speed (if supported)");
609
	register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
zwelch's avatar
zwelch committed
610
611
		COMMAND_ANY, "set maximum jtag speed (if supported); "
		"parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
612
613
	register_command(cmd_ctx, NULL, "jtag_rclk", handle_jtag_rclk_command,
		COMMAND_ANY, "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed");
614
	register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
zwelch's avatar
zwelch committed
615
		COMMAND_CONFIG, "(DEPRECATED) jtag_device <ir_length> <ir_expected> <ir_mask>");
616
	register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
617
618
		COMMAND_ANY,
		"[none/trst_only/srst_only/trst_and_srst] [srst_pulls_trst/trst_pulls_srst] [combined/separate] [trst_push_pull/trst_open_drain] [srst_push_pull/srst_open_drain]");
619
	register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
620
		COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
621
	register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
622
		COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
623

624
625
626
627
628
629
630
631
632
	register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
		COMMAND_EXEC, "print current scan chain configuration");

	register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
		COMMAND_EXEC, "toggle reset lines <trst> <srst>");
	register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
		COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
	register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
		COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
633
	register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
634
	register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
oharboe's avatar
oharboe committed
635
	register_jim(cmd_ctx, "pathmove", Jim_Command_pathmove, "move JTAG to state1 then to state2, state3, etc. <state1>,<state2>,<stat3>...");
636
637

	register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
zwelch's avatar
zwelch committed
638
		COMMAND_ANY, "verify value captured during Capture-IR <enable | disable>");
oharboe's avatar
oharboe committed
639
	register_command(cmd_ctx, NULL, "verify_jtag", handle_verify_jtag_command,
zwelch's avatar
zwelch committed
640
		COMMAND_ANY, "verify value capture <enable | disable>");
641
	register_command(cmd_ctx, NULL, "tms_sequence", handle_tms_sequence_command,
zwelch's avatar
zwelch committed
642
		COMMAND_ANY, "choose short(default) or long tms_sequence <short | long>");
643
644
645
	return ERROR_OK;
}

646
647
static int default_khz(int khz, int *jtag_speed)
{
648
	LOG_ERROR("Translation from khz to jtag_speed not implemented");
649
650
651
	return ERROR_FAIL;
}

oharboe's avatar
oharboe committed
652
static int default_speed_div(int speed, int *khz)
653
{
654
	LOG_ERROR("Translation from jtag_speed to khz not implemented");
655
656
657
658
659
	return ERROR_FAIL;
}

static int default_power_dropout(int *dropout)
{
zwelch's avatar
zwelch committed
660
	*dropout = 0; /* by default we can't detect power dropout */
661
662
663
664
665
	return ERROR_OK;
}

static int default_srst_asserted(int *srst_asserted)
{
zwelch's avatar
zwelch committed
666
	*srst_asserted = 0; /* by default we can't detect srst asserted */
667
	return ERROR_OK;
668
669
}

670
671
static int handle_interface_command(struct command_context_s *cmd_ctx,
		char *cmd, char **args, int argc)
672
673
674
675
{
	/* check whether the interface is already configured */
	if (jtag_interface)
	{
676
		LOG_WARNING("Interface already configured, ignoring");
677
678
679
680
		return ERROR_OK;
	}

	/* interface name is a mandatory argument */
681
	if (argc != 1 || args[0][0] == '\0')
682
683
		return ERROR_COMMAND_SYNTAX_ERROR;

684
	for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
685
	{
686
687
688
689
690
		if (strcmp(args[0], jtag_interfaces[i]->name) != 0)
			continue;

		int retval = jtag_interfaces[i]->register_commands(cmd_ctx);
		if (ERROR_OK != retval)
691
				return retval;
692

693
		jtag_interface = jtag_interfaces[i];
694

695
696
697
698
699
700
701
702
		if (jtag_interface->khz == NULL)
			jtag_interface->khz = default_khz;
		if (jtag_interface->speed_div == NULL)
			jtag_interface->speed_div = default_speed_div;
		if (jtag_interface->power_dropout == NULL)
			jtag_interface->power_dropout = default_power_dropout;
		if (jtag_interface->srst_asserted == NULL)
			jtag_interface->srst_asserted = default_srst_asserted;
703

704
		return ERROR_OK;
705
706
707
708
709
	}

	/* no valid interface was found (i.e. the configuration option,
	 * didn't match one of the compiled-in interfaces
	 */
710
	LOG_ERROR("The specified JTAG interface was not found (%s)", args[0]);
711
712
713
714
715
716
717
718
719
720
721
	handle_interface_list_command(cmd_ctx, cmd, args, argc);
	return ERROR_JTAG_INVALID_INTERFACE;
}

static int handle_interface_list_command(struct command_context_s *cmd_ctx,
		char *cmd, char **args, int argc)
{
	if (strcmp(cmd, "interface_list") == 0 && argc > 0)
		return ERROR_COMMAND_SYNTAX_ERROR;

	command_print(cmd_ctx, "The following JTAG interfaces are available:");
722
	for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
723
724
725
726
	{
		const char *name = jtag_interfaces[i]->name;
		command_print(cmd_ctx, "%u: %s", i + 1, name);
	}
727

728
	return ERROR_OK;
729
730
}

731
static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
732
{
733
734
735
	int e;
	char buf[1024];
	Jim_Obj *newargs[ 10 ];
736
737
738
739
740
741
742
743
	/*
	 * CONVERT SYNTAX
	 * argv[-1] = command
	 * argv[ 0] = ir length
	 * argv[ 1] = ir capture
	 * argv[ 2] = ir mask
	 * argv[ 3] = not actually used by anything but in the docs
	 */
oharboe's avatar
oharboe committed
744

zwelch's avatar
zwelch committed
745
	if (argc < 4) {
746
		command_print(cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
747
		return ERROR_OK;
748
	}
749
750
	command_print(cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
	command_print(cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
751
752
				   args[0],
				   args[1],
753
				   args[2]);
754
755
756
757
758
	command_print(cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
	command_print(cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
	command_print(cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
	command_print(cmd_ctx, "And then refer to the taps by the dotted name.");

759
760
761
762
763
764
765
766
767
768
769
770
	newargs[0] = Jim_NewStringObj(interp, "jtag", -1);
	newargs[1] = Jim_NewStringObj(interp, "newtap", -1);
	sprintf(buf, "chip%d", jtag_tap_count());
	newargs[2] = Jim_NewStringObj(interp, buf, -1);
	sprintf(buf, "tap%d", jtag_tap_count());
	newargs[3] = Jim_NewStringObj(interp, buf, -1);
	newargs[4] = Jim_NewStringObj(interp, "-irlen", -1);
	newargs[5] = Jim_NewStringObj(interp, args[0], -1);
	newargs[6] = Jim_NewStringObj(interp, "-ircapture", -1);
	newargs[7] = Jim_NewStringObj(interp, args[1], -1);
	newargs[8] = Jim_NewStringObj(interp, "-irmask", -1);
	newargs[9] = Jim_NewStringObj(interp, args[2], -1);
771
772
773

	command_print(cmd_ctx, "NEW COMMAND:");
	sprintf(buf, "%s %s %s %s %s %s %s %s %s %s",
774
775
776
777
778
779
780
781
782
783
784
785
			 Jim_GetString(newargs[0], NULL),
			 Jim_GetString(newargs[1], NULL),
			 Jim_GetString(newargs[2], NULL),
			 Jim_GetString(newargs[3], NULL),
			 Jim_GetString(newargs[4], NULL),
			 Jim_GetString(newargs[5], NULL),
			 Jim_GetString(newargs[6], NULL),
			 Jim_GetString(newargs[7], NULL),
			 Jim_GetString(newargs[8], NULL),
			 Jim_GetString(newargs[9], NULL));

	e = jim_jtag_command(interp, 10, newargs);
zwelch's avatar
zwelch committed
786
	if (e != JIM_OK) {
787
		command_print(cmd_ctx, "%s", Jim_GetString(Jim_GetResult(interp), NULL));
788
789
	}
	return e;
790
791
}

792
static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
793
{
794
	jtag_tap_t *tap;
795

796
	tap = jtag_all_taps();
797
798
799
	command_print(cmd_ctx, "     TapName            | Enabled |   IdCode      Expected    IrLen IrCap  IrMask Instr     ");
	command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");

zwelch's avatar
zwelch committed
800
	while (tap) {
801
		uint32_t expected, expected_mask, cur_instr, ii;
802
803
804
		expected = buf_get_u32(tap->expected, 0, tap->ir_length);
		expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
		cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
805

806
807
808
809
810
		command_print(cmd_ctx,
					  "%2d | %-18s |    %c    | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
					  tap->abs_chain_position,
					  tap->dotted_name,
					  tap->enabled ? 'Y' : 'n',
811
812
813
814
815
816
					  (unsigned int)(tap->idcode),
					  (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
					  (unsigned int)(tap->ir_length),
					  (unsigned int)(expected),
					  (unsigned int)(expected_mask),
					  (unsigned int)(cur_instr));
817
818
819

		for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
			command_print(cmd_ctx, "   |                    |         |            | 0x%08x |      |      |      |         ",
820
						  (unsigned int)(tap->expected_ids[ii]));
821
822
		}

823
		tap = tap->next_tap;
824
825
826
827
828
	}

	return ERROR_OK;
}

829
static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
830
{
zwelch's avatar
zwelch committed
831
832
833
	int new_cfg = 0;
	int mask = 0;

834
835
	if (argc < 1)
		return ERROR_COMMAND_SYNTAX_ERROR;
836

zwelch's avatar
zwelch committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
	/* Original versions cared about the order of these tokens:
	 *   reset_config signals [combination [trst_type [srst_type]]]
	 * They also clobbered the previous configuration even on error.
	 *
	 * Here we don't care about the order, and only change values
	 * which have been explicitly specified.
	 */
	for (; argc; argc--, args++) {
		int tmp = 0;
		int m;

		/* signals */
		m = RESET_HAS_TRST | RESET_HAS_SRST;
		if (strcmp(*args, "none") == 0)
			tmp = RESET_NONE;
		else if (strcmp(*args, "trst_only") == 0)
			tmp = RESET_HAS_TRST;
		else if (strcmp(*args, "srst_only") == 0)
			tmp = RESET_HAS_SRST;
		else if (strcmp(*args, "trst_and_srst") == 0)
			tmp = RESET_HAS_TRST | RESET_HAS_SRST;
858
		else
zwelch's avatar
zwelch committed
859
860
861
862
			m = 0;
		if (mask & m) {
			LOG_ERROR("extra reset_config %s spec (%s)",
					"signal", *args);
863
864
			return ERROR_INVALID_ARGUMENTS;
		}
zwelch's avatar
zwelch committed
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
		if (m)
			goto next;

		/* combination (options for broken wiring) */
		m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
		if (strcmp(*args, "separate") == 0)
			/* separate reset lines - default */;
		else if (strcmp(*args, "srst_pulls_trst") == 0)
			tmp |= RESET_SRST_PULLS_TRST;
		else if (strcmp(*args, "trst_pulls_srst") == 0)
			tmp |= RESET_TRST_PULLS_SRST;
		else if (strcmp(*args, "combined") == 0)
			tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
		else
			m = 0;
		if (mask & m) {
			LOG_ERROR("extra reset_config %s spec (%s)",
					"combination", *args);
			return ERROR_INVALID_ARGUMENTS;
884
		}
zwelch's avatar
zwelch committed
885
886
887
888
889
890
891
892
893
		if (m)
			goto next;

		/* trst_type (NOP without HAS_TRST) */
		m = RESET_TRST_OPEN_DRAIN;
		if (strcmp(*args, "trst_open_drain") == 0)
			tmp |= RESET_TRST_OPEN_DRAIN;
		else if (strcmp(*args, "trst_push_pull") == 0)
			/* push/pull from adapter - default */;
894
		else
zwelch's avatar
zwelch committed
895
896
897
898
			m = 0;
		if (mask & m) {
			LOG_ERROR("extra reset_config %s spec (%s)",
					"trst_type", *args);
899
900
			return ERROR_INVALID_ARGUMENTS;
		}
zwelch's avatar
zwelch committed
901
902
903
904
905
906
907
908
909
		if (m)
			goto next;

		/* srst_type (NOP without HAS_SRST) */
		m |= RESET_SRST_PUSH_PULL;
		if (strcmp(*args, "srst_push_pull") == 0)
			tmp |= RESET_SRST_PUSH_PULL;
		else if (strcmp(*args, "srst_open_drain") == 0)
			/* open drain from adapter - default */;
910
		else
zwelch's avatar
zwelch committed
911
912
913
914
			m = 0;
		if (mask & m) {
			LOG_ERROR("extra reset_config %s spec (%s)",
					"srst_type", *args);
915
916
			return ERROR_INVALID_ARGUMENTS;
		}
zwelch's avatar
zwelch committed
917
918
919
920
921
922
923
924
925
926
927
928
929
		if (m)
			goto next;

		/* caller provided nonsense; fail */
		LOG_ERROR("unknown reset_config flag (%s)", *args);
		return ERROR_INVALID_ARGUMENTS;

next:
		/* Remember the bits which were specified (mask)
		 * and their new values (new_cfg).
		 */
		mask |= m;
		new_cfg |= tmp;
930
	}
931

zwelch's avatar
zwelch committed
932
	/* clear previous values of those bits, save new values */
933
934
935
936
	enum reset_types old_cfg = jtag_get_reset_config();
	old_cfg &= ~mask;
	new_cfg |= old_cfg;
	jtag_set_reset_config(new_cfg);
zwelch's avatar
zwelch committed
937

938
939
940
	return ERROR_OK;
}

941
942
static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx,
		char *cmd, char **args, int argc)
943
{
944
	if (argc > 1)
945
		return ERROR_COMMAND_SYNTAX_ERROR;
946
	if (argc == 1)
947
948
949
950
951
952
953
	{
		unsigned delay;
		int retval = parse_uint(args[0], &delay);
		if (ERROR_OK != retval)
			return retval;
		jtag_set_nsrst_delay(delay);
	}
954
	command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
955
956
957
	return ERROR_OK;
}

958
959
static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx,
		char *cmd, char **args, int argc)
960
{
961
	if (argc > 1)
962
		return ERROR_COMMAND_SYNTAX_ERROR;
963
	if (argc == 1)
964
965
966
967
968
969
970
	{
		unsigned delay;
		int retval = parse_uint(args[0], &delay);
		if (ERROR_OK != retval)
			return retval;
		jtag_set_ntrst_delay(delay);
	}
971
	command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
972
973
974
	return ERROR_OK;
}

975
static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
976
{
977
	int retval = ERROR_OK;
978

979
980
981
	command_print(cmd_ctx, "OLD SYNTAX: DEPRECATED - "
			"use jtag_khz, not jtag_speed");

982
983
	if (argc > 1)
		return ERROR_COMMAND_SYNTAX_ERROR;
oharboe's avatar
   
oharboe committed
984
	if (argc == 1)
oharboe's avatar
oharboe committed
985
986
	{
		LOG_DEBUG("handle jtag speed");
oharboe's avatar
   
oharboe committed
987

988
989
990
991
		unsigned cur_speed = 0;
		int retval = parse_uint(args[0], &cur_speed);
		if (ERROR_OK != retval)
			return retval;
992
		retval = jtag_config_speed(cur_speed);
993

oharboe's avatar
   
oharboe committed
994
	}
995
	command_print(cmd_ctx, "jtag_speed: %d", jtag_get_speed());
996

oharboe's avatar
   
oharboe committed
997
	return retval;
998
999
}

1000
static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
For faster browsing, not all history is shown. View entire blame