zy1000.c 17.1 KB
Newer Older
1
/***************************************************************************
2
 *   Copyright (C) 2007-2008 by Øyvind Harboe                              *
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *                                                                         *
 *   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

23
#include "embeddedice.h"
zwelch's avatar
zwelch committed
24
#include "minidriver.h"
25
#include "interface.h"
26
27
28
29
30

#include <cyg/hal/hal_io.h>             // low level i/o
#include <cyg/hal/hal_diag.h>


oharboe's avatar
oharboe committed
31
#define ZYLIN_VERSION "1.54"
32
33
#define ZYLIN_DATE __DATE__
#define ZYLIN_TIME __TIME__
oharboe's avatar
oharboe committed
34
#define ZYLIN_OPENOCD "$Revision$"
35
#define ZYLIN_OPENOCD_VERSION "Zylin JTAG ZY1000 " ZYLIN_VERSION " " ZYLIN_DATE " " ZYLIN_TIME
36
37
38

/* low level command set
 */
39
void zy1000_reset(int trst, int srst);
40
41


42
43
44
45
int zy1000_speed(int speed);
int zy1000_register_commands(struct command_context_s *cmd_ctx);
int zy1000_init(void);
int zy1000_quit(void);
46
47

/* interface commands */
48
int zy1000_handle_zy1000_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49

50
static int zy1000_khz(int khz, int *jtag_speed)
51
{
zwelch's avatar
zwelch committed
52
	if (khz == 0)
53
	{
zwelch's avatar
zwelch committed
54
		*jtag_speed = 0;
55
56
57
	}
	else
	{
zwelch's avatar
zwelch committed
58
		*jtag_speed = 64000/khz;
59
60
61
62
	}
	return ERROR_OK;
}

63
static int zy1000_speed_div(int speed, int *khz)
64
{
zwelch's avatar
zwelch committed
65
	if (speed == 0)
66
67
68
69
70
	{
		*khz = 0;
	}
	else
	{
zwelch's avatar
zwelch committed
71
		*khz = 64000/speed;
72
73
74
75
76
	}

	return ERROR_OK;
}

77
static bool readPowerDropout(void)
78
79
80
{
	cyg_uint32 state;
	// sample and clear power dropout
zwelch's avatar
zwelch committed
81
82
	HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x10, 0x80);
	HAL_READ_UINT32(ZY1000_JTAG_BASE + 0x10, state);
83
84
85
86
	bool powerDropout;
	powerDropout = (state & 0x80) != 0;
	return powerDropout;
}
87

88

89
static bool readSRST(void)
90
91
92
{
	cyg_uint32 state;
	// sample and clear SRST sensing
zwelch's avatar
zwelch committed
93
94
	HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x10, 0x00000040);
	HAL_READ_UINT32(ZY1000_JTAG_BASE + 0x10, state);
95
96
97
98
99
	bool srstAsserted;
	srstAsserted = (state & 0x40) != 0;
	return srstAsserted;
}

oharboe's avatar
oharboe committed
100
101
static int zy1000_srst_asserted(int *srst_asserted)
{
zwelch's avatar
zwelch committed
102
	*srst_asserted = readSRST();
oharboe's avatar
oharboe committed
103
104
105
	return ERROR_OK;
}

106
107
static int zy1000_power_dropout(int *dropout)
{
zwelch's avatar
zwelch committed
108
	*dropout = readPowerDropout();
109
110
111
112
113
	return ERROR_OK;
}


jtag_interface_t zy1000_interface =
114
115
{
	.name = "ZY1000",
oharboe's avatar
oharboe committed
116
	.execute_queue = NULL,
117
118
119
120
121
122
123
	.speed = zy1000_speed,
	.register_commands = zy1000_register_commands,
	.init = zy1000_init,
	.quit = zy1000_quit,
	.khz = zy1000_khz,
	.speed_div = zy1000_speed_div,
	.power_dropout = zy1000_power_dropout,
oharboe's avatar
oharboe committed
124
	.srst_asserted = zy1000_srst_asserted,
125
126
};

127
void zy1000_reset(int trst, int srst)
128
129
{
	LOG_DEBUG("zy1000 trst=%d, srst=%d", trst, srst);
zwelch's avatar
zwelch committed
130
	if (!srst)
131
	{
zwelch's avatar
zwelch committed
132
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x00000001);
133
134
135
	}
	else
	{
zwelch's avatar
zwelch committed
136
		/* Danger!!! if clk != 0 when in
137
		 * idle in TAP_IDLE, reset halt on str912 will fail.
138
		 */
zwelch's avatar
zwelch committed
139
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000001);
140
141
	}

zwelch's avatar
zwelch committed
142
	if (!trst)
143
	{
zwelch's avatar
zwelch committed
144
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x00000002);
145
146
147
148
	}
	else
	{
		/* assert reset */
zwelch's avatar
zwelch committed
149
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000002);
150
	}
151

zwelch's avatar
zwelch committed
152
	if (trst||(srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
153
154
	{
		waitIdle();
155
		/* we're now in the RESET state until trst is deasserted */
zwelch's avatar
zwelch committed
156
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_RESET);
157
158
159
	} else
	{
		/* We'll get RCLK failure when we assert TRST, so clear any false positives here */
zwelch's avatar
zwelch committed
160
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x400);
161
162
163
164
165
166
	}

	/* wait for srst to float back up */
	if (!srst)
	{
		int i;
zwelch's avatar
zwelch committed
167
		for (i = 0; i < 1000; i++)
168
169
170
171
172
173
174
175
176
177
178
		{
			// We don't want to sense our own reset, so we clear here.
			// There is of course a timing hole where we could loose
			// a "real" reset.
			if (!readSRST())
				break;

			/* wait 1ms */
			alive_sleep(1);
		}

zwelch's avatar
zwelch committed
179
		if (i == 1000)
180
181
		{
			LOG_USER("SRST didn't deassert after %dms", i);
zwelch's avatar
zwelch committed
182
		} else if (i > 1)
183
184
185
186
187
188
		{
			LOG_USER("SRST took %dms to deassert", i);
		}
	}
}

189
int zy1000_speed(int speed)
190
{
zwelch's avatar
zwelch committed
191
	if (speed == 0)
192
193
194
	{
		/*0 means RCLK*/
		speed = 0;
zwelch's avatar
zwelch committed
195
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x100);
196
197
198
199
		LOG_DEBUG("jtag_speed using RCLK");
	}
	else
	{
zwelch's avatar
zwelch committed
200
		if (speed > 8190 || speed < 2)
201
		{
202
			LOG_USER("valid ZY1000 jtag_speed=[8190,2]. Divisor is 64MHz / even values between 8190-2, i.e. min 7814Hz, max 32MHz");
203
204
205
206
			return ERROR_INVALID_ARGUMENTS;
		}

		LOG_USER("jtag_speed %d => JTAG clk=%f", speed, 64.0/(float)speed);
zwelch's avatar
zwelch committed
207
208
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x100);
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x1c, speed&~1);
209
210
211
212
	}
	return ERROR_OK;
}

213
static bool savePower;
214
215


216
static void setPower(bool power)
217
{
218
219
220
	savePower = power;
	if (power)
	{
zwelch's avatar
zwelch committed
221
		HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x14, 0x8);
222
223
	} else
	{
zwelch's avatar
zwelch committed
224
		HAL_WRITE_UINT32(ZY1000_JTAG_BASE + 0x10, 0x8);
225
	}
226
227
}

228
int handle_power_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
229
{
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
	if (argc > 1)
	{
		return ERROR_INVALID_ARGUMENTS;
	}

	if (argc == 1)
	{
		if (strcmp(args[0], "on") == 0)
		{
			setPower(1);
		}
		else if (strcmp(args[0], "off") == 0)
		{
			setPower(0);
		} else
		{
			command_print(cmd_ctx, "arg is \"on\" or \"off\"");
			return ERROR_INVALID_ARGUMENTS;
		}
	}

	command_print(cmd_ctx, "Target power %s", savePower ? "on" : "off");
252
253
254
255
256

	return ERROR_OK;
}


257
/* Give TELNET a way to find out what version this is */
oharboe's avatar
oharboe committed
258
static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
259
{
oharboe's avatar
oharboe committed
260
261
262
	if ((argc < 1) || (argc > 2))
		return JIM_ERR;
	char buff[128];
zwelch's avatar
zwelch committed
263
	const char *version_str = NULL;
oharboe's avatar
oharboe committed
264
265

	if (argc == 1)
266
	{
zwelch's avatar
zwelch committed
267
		version_str = ZYLIN_OPENOCD_VERSION;
oharboe's avatar
oharboe committed
268
	} else
269
	{
oharboe's avatar
oharboe committed
270
271
272
273
		const char *str = Jim_GetString(argv[1], NULL);
		if (strcmp("openocd", str) == 0)
		{
			int revision;
zwelch's avatar
zwelch committed
274
			revision = atol(ZYLIN_OPENOCD + strlen("XRevision: "));
oharboe's avatar
oharboe committed
275
			sprintf(buff, "%d", revision);
zwelch's avatar
zwelch committed
276
			version_str = buff;
oharboe's avatar
oharboe committed
277
278
279
		}
		else if (strcmp("zy1000", str) == 0)
		{
zwelch's avatar
zwelch committed
280
			version_str = ZYLIN_VERSION;
oharboe's avatar
oharboe committed
281
282
283
		}
		else if (strcmp("date", str) == 0)
		{
zwelch's avatar
zwelch committed
284
			version_str = ZYLIN_DATE;
oharboe's avatar
oharboe committed
285
		}
oharboe's avatar
oharboe committed
286
287
288
289
290
291
292
293
		else if (strcmp("pcb", str) == 0)
		{
#ifdef CYGPKG_HAL_NIOS2
			version_str="c";
#else
			version_str="b";
#endif
		}
oharboe's avatar
oharboe committed
294
295
296
297
		else
		{
			return JIM_ERR;
		}
298
	}
299

oharboe's avatar
oharboe committed
300
301
302
	Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));

	return JIM_OK;
303
304
}

305

oharboe's avatar
oharboe committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#ifdef CYGPKG_HAL_NIOS2
static int jim_zy1000_writefirmware(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	if (argc != 2)
		return JIM_ERR;

	int length;
	int stat;
	const char *str = Jim_GetString(argv[1], &length);

	/* BUG!!!! skip header! */
	void *firmware_address=0x4000000;
	int firmware_length=0x100000;

	if (length>firmware_length)
		return JIM_ERR;

	void *err_addr;

    if ((stat = flash_erase((void *)firmware_address, firmware_length, (void **)&err_addr)) != 0)
    {
    	return JIM_ERR;
    }

    if ((stat = flash_program(firmware_address, str, length, (void **)&err_addr)) != 0)
    	return JIM_ERR;

    return JIM_OK;
}
#endif

oharboe's avatar
oharboe committed
337
338
static int
zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp,
339
								   int argc,
oharboe's avatar
oharboe committed
340
341
342
343
344
345
346
347
348
		Jim_Obj * const *argv)
{
	if (argc != 1)
	{
		Jim_WrongNumArgs(interp, 1, argv, "powerstatus");
		return JIM_ERR;
	}

	cyg_uint32 status;
zwelch's avatar
zwelch committed
349
	ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, status);
oharboe's avatar
oharboe committed
350

zwelch's avatar
zwelch committed
351
	Jim_SetResult(interp, Jim_NewIntObj(interp, (status&0x80) != 0));
oharboe's avatar
oharboe committed
352
353
354
355

	return JIM_OK;
}

356
357
358
359
int zy1000_register_commands(struct command_context_s *cmd_ctx)
{
	register_command(cmd_ctx, NULL, "power", handle_power_command, COMMAND_ANY,
			"power <on/off> - turn power switch to target on/off. No arguments - print status.");
oharboe's avatar
oharboe committed
360
361
362

	Jim_CreateCommand(interp, "zy1000_version", jim_zy1000_version, NULL, NULL);

363

oharboe's avatar
oharboe committed
364
	Jim_CreateCommand(interp, "powerstatus", zylinjtag_Jim_Command_powerstatus, NULL, NULL);
365

oharboe's avatar
oharboe committed
366
367
368
369
370
#ifdef CYGPKG_HAL_NIOS2
	Jim_CreateCommand(interp, "updatezy1000firmware", jim_zy1000_writefirmware, NULL, NULL);
#endif


371
372
373
374
	return ERROR_OK;
}


oharboe's avatar
oharboe committed
375
376


377
378
int zy1000_init(void)
{
oharboe's avatar
oharboe committed
379
	LOG_USER("%s", ZYLIN_OPENOCD_VERSION);
380

zwelch's avatar
zwelch committed
381
	ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x30); // Turn on LED1 & LED2
382
383
384
385
386
387

	setPower(true); // on by default


	 /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */
	zy1000_reset(0, 0);
388
	zy1000_speed(jtag_get_speed());
389
390
391
392
393
394
395
396

	return ERROR_OK;
}

int zy1000_quit(void)
{

	return ERROR_OK;
397
398
399
400
401
402
403
404
405
}



int interface_jtag_execute_queue(void)
{
	cyg_uint32 empty;

	waitIdle();
zwelch's avatar
zwelch committed
406
	ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, empty);
407
	/* clear JTAG error register */
zwelch's avatar
zwelch committed
408
	ZY1000_POKE(ZY1000_JTAG_BASE + 0x14, 0x400);
409

zwelch's avatar
zwelch committed
410
	if ((empty&0x400) != 0)
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	{
		LOG_WARNING("RCLK timeout");
		/* the error is informative only as we don't want to break the firmware if there
		 * is a false positive.
		 */
//		return ERROR_FAIL;
	}
	return ERROR_OK;
}





425
static cyg_uint32 getShiftValue(void)
426
427
428
{
	cyg_uint32 value;
	waitIdle();
zwelch's avatar
zwelch committed
429
	ZY1000_PEEK(ZY1000_JTAG_BASE + 0xc, value);
430
431
432
433
	VERBOSE(LOG_INFO("getShiftValue %08x", value));
	return value;
}
#if 0
434
static cyg_uint32 getShiftValueFlip(void)
435
436
437
{
	cyg_uint32 value;
	waitIdle();
zwelch's avatar
zwelch committed
438
	ZY1000_PEEK(ZY1000_JTAG_BASE + 0x18, value);
439
440
441
442
443
444
	VERBOSE(LOG_INFO("getShiftValue %08x (flipped)", value));
	return value;
}
#endif

#if 0
445
static void shiftValueInnerFlip(const tap_state_t state, const tap_state_t endState, int repeat, cyg_uint32 value)
446
{
447
	VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", tap_state_name(state), tap_state_name(endState), repeat, value));
448
	cyg_uint32 a,b;
zwelch's avatar
zwelch committed
449
450
	a = state;
	b = endState;
zwelch's avatar
zwelch committed
451
	ZY1000_POKE(ZY1000_JTAG_BASE + 0xc, value);
zwelch's avatar
zwelch committed
452
	ZY1000_POKE(ZY1000_JTAG_BASE + 0x8, (1 << 15) | (repeat << 8) | (a << 4) | b);
453
454
455
456
	VERBOSE(getShiftValueFlip());
}
#endif

zwelch's avatar
zwelch committed
457
extern int jtag_check_value(uint8_t *captured, void *priv);
458

459
460
461
462
463
464
static void gotoEndState(tap_state_t end_state)
{
	setCurrentState(end_state);
}

static __inline void scanFields(int num_fields, const scan_field_t *fields, tap_state_t shiftState, int pause)
465
466
467
468
469
470
471
472
473
{
	int i;
	int j;
	int k;

	for (i = 0; i < num_fields; i++)
	{
		cyg_uint32 value;

zwelch's avatar
zwelch committed
474
		uint8_t *inBuffer = NULL;
475
476
477


		// figure out where to store the input data
zwelch's avatar
zwelch committed
478
		int num_bits = fields[i].num_bits;
zwelch's avatar
zwelch committed
479
		if (fields[i].in_value != NULL)
480
		{
zwelch's avatar
zwelch committed
481
			inBuffer = fields[i].in_value;
482
483
484
		}

		// here we shuffle N bits out/in
zwelch's avatar
zwelch committed
485
		j = 0;
zwelch's avatar
zwelch committed
486
		while (j < num_bits)
487
		{
488
			tap_state_t pause_state;
489
			int l;
zwelch's avatar
zwelch committed
490
491
			k = num_bits-j;
			pause_state = (shiftState == TAP_DRSHIFT)?TAP_DRSHIFT:TAP_IRSHIFT;
zwelch's avatar
zwelch committed
492
			if (k > 32)
493
			{
zwelch's avatar
zwelch committed
494
				k = 32;
495
				/* we have more to shift out */
496
			} else if (pause&&(i == num_fields-1))
497
498
			{
				/* this was the last to shift out this time */
499
				pause_state = (shiftState==TAP_DRSHIFT)?TAP_DRPAUSE:TAP_IRPAUSE;
500
501
			}

zwelch's avatar
zwelch committed
502
			// we have (num_bits + 7)/8 bytes of bits to toggle out.
503
			// bits are pushed out LSB to MSB
zwelch's avatar
zwelch committed
504
			value = 0;
zwelch's avatar
zwelch committed
505
			if (fields[i].out_value != NULL)
506
			{
zwelch's avatar
zwelch committed
507
				for (l = 0; l < k; l += 8)
508
				{
zwelch's avatar
zwelch committed
509
					value|=fields[i].out_value[(j + l)/8]<<l;
510
511
512
				}
			}
			/* mask away unused bits for easier debugging */
zwelch's avatar
zwelch committed
513
			value&=~(((uint32_t)0xffffffff) << k);
514
515
516

			shiftValueInner(shiftState, pause_state, k, value);

zwelch's avatar
zwelch committed
517
			if (inBuffer != NULL)
518
519
			{
				// data in, LSB to MSB
zwelch's avatar
zwelch committed
520
				value = getShiftValue();
521
522
523
				// we're shifting in data to MSB, shift data to be aligned for returning the value
				value >>= 32-k;

zwelch's avatar
zwelch committed
524
				for (l = 0; l < k; l += 8)
525
				{
zwelch's avatar
zwelch committed
526
					inBuffer[(j + l)/8]=(value >> l)&0xff;
527
528
				}
			}
zwelch's avatar
zwelch committed
529
			j += k;
530
531
532
533
		}
	}
}

534
int interface_jtag_set_end_state(tap_state_t state)
535
536
537
538
539
{
	return ERROR_OK;
}


oharboe's avatar
oharboe committed
540
int interface_jtag_add_ir_scan(int num_fields, const scan_field_t *fields, tap_state_t state)
541
542
{

oharboe's avatar
oharboe committed
543
	int j;
544
	int scan_size = 0;
oharboe's avatar
oharboe committed
545
	jtag_tap_t *tap, *nextTap;
zwelch's avatar
zwelch committed
546
	for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap)
547
	{
zwelch's avatar
zwelch committed
548
		nextTap = jtag_tap_next_enabled(tap);
549
		int pause = (nextTap==NULL);
oharboe's avatar
oharboe committed
550

551
		int found = 0;
552

oharboe's avatar
oharboe committed
553
		scan_size = tap->ir_length;
554
555

		/* search the list */
zwelch's avatar
zwelch committed
556
		for (j = 0; j < num_fields; j++)
557
		{
oharboe's avatar
oharboe committed
558
			if (tap == fields[j].tap)
559
560
561
			{
				found = 1;

562
				scanFields(1, fields + j, TAP_IRSHIFT, pause);
563
				/* update device information */
oharboe's avatar
oharboe committed
564
				buf_cpy(fields[j].out_value, tap->cur_instr, scan_size);
565

oharboe's avatar
oharboe committed
566
				tap->bypass = 0;
567
568
569
570
571
572
573
				break;
			}
		}

		if (!found)
		{
			/* if a device isn't listed, set it to BYPASS */
zwelch's avatar
zwelch committed
574
			uint8_t ones[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
575
576
577
578
579

			scan_field_t tmp;
			memset(&tmp, 0, sizeof(tmp));
			tmp.out_value = ones;
			tmp.num_bits = scan_size;
580
			scanFields(1, &tmp, TAP_IRSHIFT, pause);
581
			/* update device information */
oharboe's avatar
oharboe committed
582
583
			buf_cpy(tmp.out_value, tap->cur_instr, scan_size);
			tap->bypass = 1;
584
585
		}
	}
586
	gotoEndState(state);
587
588
589
590
591
592
593
594

	return ERROR_OK;
}





oharboe's avatar
oharboe committed
595
int interface_jtag_add_plain_ir_scan(int num_fields, const scan_field_t *fields, tap_state_t state)
596
{
597
598
	scanFields(num_fields, fields, TAP_IRSHIFT, 1);
	gotoEndState(state);
599
600
601
602
603
604

	return ERROR_OK;
}

/*extern jtag_command_t **jtag_get_last_command_p(void);*/

oharboe's avatar
oharboe committed
605
int interface_jtag_add_dr_scan(int num_fields, const scan_field_t *fields, tap_state_t state)
606
{
oharboe's avatar
oharboe committed
607
608
609

	int j;
	jtag_tap_t *tap, *nextTap;
zwelch's avatar
zwelch committed
610
	for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap)
611
	{
zwelch's avatar
zwelch committed
612
613
		nextTap = jtag_tap_next_enabled(tap);
		int found = 0;
614
		int pause = (nextTap==NULL);
615

zwelch's avatar
zwelch committed
616
		for (j = 0; j < num_fields; j++)
617
		{
oharboe's avatar
oharboe committed
618
			if (tap == fields[j].tap)
619
620
621
			{
				found = 1;

622
				scanFields(1, fields+j, TAP_DRSHIFT, pause);
623
624
625
626
627
628
629
630
631
632
			}
		}
		if (!found)
		{
			scan_field_t tmp;
			/* program the scan field to 1 bit length, and ignore it's value */
			tmp.num_bits = 1;
			tmp.out_value = NULL;
			tmp.in_value = NULL;

633
			scanFields(1, &tmp, TAP_DRSHIFT, pause);
634
635
636
637
638
		}
		else
		{
		}
	}
639
	gotoEndState(state);
640
641
642
	return ERROR_OK;
}

oharboe's avatar
oharboe committed
643
int interface_jtag_add_plain_dr_scan(int num_fields, const scan_field_t *fields, tap_state_t state)
644
{
645
646
	scanFields(num_fields, fields, TAP_DRSHIFT, 1);
	gotoEndState(state);
647
648
649
650
651
652
	return ERROR_OK;
}


int interface_jtag_add_tlr()
{
653
	setCurrentState(TAP_RESET);
654
655
656
657
658
659
660
661
662
663
664
	return ERROR_OK;
}




extern int jtag_nsrst_delay;
extern int jtag_ntrst_delay;

int interface_jtag_add_reset(int req_trst, int req_srst)
{
665
	zy1000_reset(req_trst, req_srst);
666
667
668
	return ERROR_OK;
}

669
static int zy1000_jtag_add_clocks(int num_cycles, tap_state_t state, tap_state_t clockstate)
670
671
{
	/* num_cycles can be 0 */
672
	setCurrentState(clockstate);
673
674
675

	/* execute num_cycles, 32 at the time. */
	int i;
zwelch's avatar
zwelch committed
676
	for (i = 0; i < num_cycles; i += 32)
677
678
	{
		int num;
zwelch's avatar
zwelch committed
679
		num = 32;
zwelch's avatar
zwelch committed
680
		if (num_cycles-i < num)
681
		{
zwelch's avatar
zwelch committed
682
			num = num_cycles-i;
683
		}
684
		shiftValueInner(clockstate, clockstate, num, 0);
685
686
687
688
689
690
	}

#if !TEST_MANUAL()
	/* finish in end_state */
	setCurrentState(state);
#else
zwelch's avatar
zwelch committed
691
	tap_state_t t = TAP_IDLE;
692
693
	/* test manual drive code on any target */
	int tms;
zwelch's avatar
zwelch committed
694
	uint8_t tms_scan = tap_get_tms_path(t, state);
695
	int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
696

697
	for (i = 0; i < tms_count; i++)
698
699
700
	{
		tms = (tms_scan >> i) & 1;
		waitIdle();
zwelch's avatar
zwelch committed
701
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x28,  tms);
702
703
	}
	waitIdle();
zwelch's avatar
zwelch committed
704
	ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, state);
705
706
707
708
709
710
#endif


	return ERROR_OK;
}

711
int interface_jtag_add_runtest(int num_cycles, tap_state_t state)
712
713
714
715
716
717
{
	return zy1000_jtag_add_clocks(num_cycles, state, TAP_IDLE);
}

int interface_jtag_add_clocks(int num_cycles)
{
718
	return zy1000_jtag_add_clocks(num_cycles, cmd_queue_cur_state, cmd_queue_cur_state);
719
720
}

721
int interface_jtag_add_sleep(uint32_t us)
722
723
724
725
726
{
	jtag_sleep(us);
	return ERROR_OK;
}

oharboe's avatar
oharboe committed
727
int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
728
729
730
731
732
733
734
735
736
{
	int state_count;
	int tms = 0;

	/*wait for the fifo to be empty*/
	waitIdle();

	state_count = 0;

zwelch's avatar
zwelch committed
737
	tap_state_t cur_state = cmd_queue_cur_state;
738
739
740

	while (num_states)
	{
741
		if (tap_state_transition(cur_state, false) == path[state_count])
742
743
744
		{
			tms = 0;
		}
745
		else if (tap_state_transition(cur_state, true) == path[state_count])
746
747
748
749
750
		{
			tms = 1;
		}
		else
		{
751
			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[state_count]));
752
753
754
755
			exit(-1);
		}

		waitIdle();
zwelch's avatar
zwelch committed
756
		ZY1000_POKE(ZY1000_JTAG_BASE + 0x28,  tms);
757
758
759
760
761
762
763

		cur_state = path[state_count];
		state_count++;
		num_states--;
	}

	waitIdle();
zwelch's avatar
zwelch committed
764
	ZY1000_POKE(ZY1000_JTAG_BASE + 0x20,  cur_state);
765
766
767
768
769
	return ERROR_OK;
}



zwelch's avatar
zwelch committed
770
void embeddedice_write_dcc(jtag_tap_t *tap, int reg_addr, uint8_t *buffer, int little, int count)
771
{
zwelch's avatar
zwelch committed
772
773
//	static int const reg_addr = 0x5;
	tap_state_t end_state = jtag_get_end_state();
zwelch's avatar
zwelch committed
774
	if (jtag_tap_next_enabled(jtag_tap_next_enabled(NULL)) == NULL)
775
776
777
778
779
780
781
	{
		/* better performance via code duplication */
		if (little)
		{
			int i;
			for (i = 0; i < count; i++)
			{
782
				shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 1));
zwelch's avatar
zwelch committed
783
				shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr | (1 << 5));
zwelch's avatar
zwelch committed
784
				buffer += 4;
785
786
787
788
789
790
			}
		} else
		{
			int i;
			for (i = 0; i < count; i++)
			{
791
				shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 0));
zwelch's avatar
zwelch committed
792
				shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr | (1 << 5));
zwelch's avatar
zwelch committed
793
				buffer += 4;
794
795
796
797
798
799
800
801
			}
		}
	}
	else
	{
		int i;
		for (i = 0; i < count; i++)
		{
oharboe's avatar
oharboe committed
802
			embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little));
803
804
805
806
807
			buffer += 4;
		}
	}
}

808