presto.c 19.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/***************************************************************************
 *   Copyright (C) 2007 by Pavel Chromy                                    *
 *   chromy@asix.cz                                                        *
 *                                                                         *
 *   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

#if IS_CYGWIN == 1
#include "windows.h"
#endif

28
#include "interface.h"
29
30
31
32
#include "time_support.h"
#include "bitq.h"


33
34
35
36
37
/* PRESTO access library includes */
#if BUILD_PRESTO_FTD2XX == 1
#include <ftd2xx.h>
#elif BUILD_PRESTO_LIBFTDI == 1
#include <ftdi.h>
38
39
#else
#error "BUG: either FTD2XX and LIBFTDI has to be used"
40
#endif
41

42
43
44
45
46
47
static int presto_jtag_speed(int speed);
static int presto_jtag_khz(int khz, int *jtag_speed);
static int presto_jtag_speed_div(int speed, int *khz);
static int presto_jtag_register_commands(struct command_context_s *cmd_ctx);
static int presto_jtag_init(void);
static int presto_jtag_quit(void);
48
49
50
51
52
53

jtag_interface_t presto_interface =
{
	.name = "presto",
	.execute_queue = bitq_execute_queue,
	.speed = presto_jtag_speed,
oharboe's avatar
oharboe committed
54
55
	.khz = presto_jtag_khz,
	.speed_div = presto_jtag_speed_div,
56
57
58
59
60
	.register_commands = presto_jtag_register_commands,
	.init = presto_jtag_init,
	.quit = presto_jtag_quit,
};

61
62
63
64
65
66
static int presto_bitq_out(int tms, int tdi, int tdo_req);
static int presto_bitq_flush(void);
static int presto_bitq_sleep(unsigned long us);
static int presto_bitq_reset(int trst, int srst);
static int presto_bitq_in_rdy(void);
static int presto_bitq_in(void);
67

68
static bitq_interface_t presto_bitq =
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{
	.out = presto_bitq_out,
	.flush = presto_bitq_flush,
	.sleep = presto_bitq_sleep,
	.reset = presto_bitq_reset,
	.in_rdy = presto_bitq_in_rdy,
	.in = presto_bitq_in,
};

/* -------------------------------------------------------------------------- */

#define FT_DEVICE_NAME_LEN 64
#define FT_DEVICE_SERNUM_LEN 64

#define PRESTO_VID_PID 0x0403f1a0
84
85
#define PRESTO_VID (0x0403)
#define PRESTO_PID (0xf1a0)
86
87
88
89
90

#define BUFFER_SIZE (64*62)

typedef struct presto_s
{
91
#if BUILD_PRESTO_FTD2XX == 1
92
93
	FT_HANDLE handle;
	FT_STATUS status;
94
95
96
97
#elif BUILD_PRESTO_LIBFTDI == 1
	struct ftdi_context ftdic;
	int retval;
#endif
98

99
100
	char serial[FT_DEVICE_SERNUM_LEN];

zwelch's avatar
zwelch committed
101
	uint8_t buff_out[BUFFER_SIZE];
102
103
	int buff_out_pos;

zwelch's avatar
zwelch committed
104
	uint8_t buff_in[BUFFER_SIZE];
105
106
107
108
109
110
111
112
113
	int buff_in_exp; /* expected in buffer length */
	int buff_in_len; /* length of data received */
	int buff_in_pos;

	unsigned long total_out;
	unsigned long total_in;

	int jtag_tms; /* last tms state */
	int jtag_tck; /* last tck state */
oharboe's avatar
oharboe committed
114
	int jtag_rst; /* last trst state */
115
116
117
118

	int jtag_tdi_data;
	int jtag_tdi_count;

oharboe's avatar
oharboe committed
119
	int jtag_speed;
zwelch's avatar
zwelch committed
120

121
122
} presto_t;

123
124
static presto_t presto_state;
static presto_t *presto = &presto_state;
125

zwelch's avatar
zwelch committed
126
static uint8_t presto_init_seq[] =
127
{
128
	0x80, 0xA0, 0xA8, 0xB0, 0xC0, 0xE0
129
130
};

131
static int presto_write(uint8_t *buf, uint32_t size)
132
133
{
#if BUILD_PRESTO_FTD2XX == 1
134
135
	DWORD ftbytes;
	if ((presto->status = FT_Write(presto->handle, buf, size, &ftbytes)) != FT_OK)
136
	{
137
		LOG_ERROR("FT_Write returned: %lu", presto->status);
138
139
		return ERROR_JTAG_DEVICE_ERROR;
	}
140

141
#elif BUILD_PRESTO_LIBFTDI == 1
142
	uint32_t ftbytes;
143
144
	if ((presto->retval = ftdi_write_data(&presto->ftdic, buf, size)) < 0)
	{
145
		LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&presto->ftdic));
146
147
		return ERROR_JTAG_DEVICE_ERROR;
	}
148
149
150
151
	ftbytes = presto->retval;
#endif

	if (ftbytes != size)
152
	{
153
154
		LOG_ERROR("couldn't write the requested number of bytes to PRESTO (%u < %u)", 
			  (unsigned)ftbytes, (unsigned)size);
155
		return ERROR_JTAG_DEVICE_ERROR;
156
	}
157
158

	return ERROR_OK;
159
}
160

161
static int presto_read(uint8_t* buf, uint32_t size)
162
163
{
#if BUILD_PRESTO_FTD2XX == 1
164
165
	DWORD ftbytes;
	if ((presto->status = FT_Read(presto->handle, buf, size, &ftbytes)) != FT_OK)
166
	{
167
		LOG_ERROR("FT_Read returned: %lu", presto->status);
168
		return ERROR_JTAG_DEVICE_ERROR;
169
	}
170

171
#elif BUILD_PRESTO_LIBFTDI == 1
172
	uint32_t ftbytes = 0;
173
174
175
176
177
178

	struct timeval timeout, now;
	gettimeofday(&timeout, NULL);
	timeval_add_time(&timeout, 1, 0); /* one second timeout */

	while (ftbytes < size)
179
	{
180
		if ((presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, size - ftbytes)) < 0)
181
		{
182
			LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic));
183
184
			return ERROR_JTAG_DEVICE_ERROR;
		}
185
186
187
188
189
		ftbytes += presto->retval;

		gettimeofday(&now, NULL);
		if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec)))
			break;
190
191
192
	}
#endif

193
	if (ftbytes != size)
194
	{
195
		/* this is just a warning, there might have been timeout when detecting PRESTO, which is not fatal */
196
197
		LOG_WARNING("couldn't read the requested number of bytes from PRESTO (%u < %u)", 
			    (unsigned)ftbytes, (unsigned)size);
198
199
		return ERROR_JTAG_DEVICE_ERROR;
	}
200

201
202
203
204
	return ERROR_OK;
}

#if BUILD_PRESTO_FTD2XX == 1
205
static int presto_open_ftd2xx(char *req_serial)
206
{
207
	uint32_t i;
208
209
210
211
212
213
	DWORD numdevs;
	DWORD vidpid;
	char devname[FT_DEVICE_NAME_LEN];
	FT_DEVICE device;

	BYTE presto_data;
214
	DWORD ftbytes;
215

216
	presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
217

218
219
220
221
#if IS_WIN32 == 0
	/* Add non-standard Vid/Pid to the linux driver */
	if ((presto->status = FT_SetVIDPID(PRESTO_VID, PRESTO_PID)) != FT_OK)
	{
222
		LOG_ERROR("couldn't add PRESTO VID/PID");
223
224
225
		exit(-1);
	}
#endif
226

227
	if ((presto->status = FT_ListDevices(&numdevs, NULL, FT_LIST_NUMBER_ONLY)) != FT_OK)
228
	{
229
		LOG_ERROR("FT_ListDevices failed: %i", (int)presto->status);
230
		return ERROR_JTAG_DEVICE_ERROR;
231
	}
232

233
	LOG_DEBUG("FTDI devices available: %lu", numdevs);
234
235
	for (i = 0; i < numdevs; i++)
	{
236
		if ((presto->status = FT_Open(i, &(presto->handle))) != FT_OK)
237
		{
238
			/* this is not fatal, the device may be legitimately open by other process, hence debug message only */
239
			LOG_DEBUG("FT_Open failed: %i", (int)presto->status);
240
241
			continue;
		}
242
		LOG_DEBUG("FTDI device %i open", i);
243

244
245
		if ((presto->status = FT_GetDeviceInfo(presto->handle, &device, &vidpid,
				presto->serial, devname, NULL)) == FT_OK)
246
247
248
		{
			if (vidpid == PRESTO_VID_PID
					&& (req_serial == NULL || !strcmp(presto->serial, req_serial)))
249
				break;
250
		}
251
		else
252
			LOG_DEBUG("FT_GetDeviceInfo failed: %lu", presto->status);
253

254
		LOG_DEBUG("FTDI device %i does not match, closing", i);
255
		FT_Close(presto->handle);
256
		presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
257
258
	}

259
	if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
260
261
		return ERROR_JTAG_DEVICE_ERROR; /* presto not open, return */

262
	if ((presto->status = FT_SetLatencyTimer(presto->handle, 1)) != FT_OK)
263
		return ERROR_JTAG_DEVICE_ERROR;
264
265


266
	if ((presto->status = FT_SetTimeouts(presto->handle, 100, 0)) != FT_OK)
267
		return ERROR_JTAG_DEVICE_ERROR;
268

269
	if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
270
		return ERROR_JTAG_DEVICE_ERROR;
271

272
273
	presto_data = 0xD0;
	if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
274
		return ERROR_JTAG_DEVICE_ERROR;
275

276
277
278
	/* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason,
	   probably a bug in library threading */
	usleep(100000);
279
	if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
280
		return ERROR_JTAG_DEVICE_ERROR;
281

282
283
	if (ftbytes!=1)
	{
284
		LOG_DEBUG("PRESTO reset");
285

286
		if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
287
288
289
			return ERROR_JTAG_DEVICE_ERROR;
		if ((presto->status = FT_SetBitMode(presto->handle, 0x80, 1)) != FT_OK)
			return ERROR_JTAG_DEVICE_ERROR;
290
		if ((presto->status = FT_SetBaudRate(presto->handle, 9600)) != FT_OK)
291
			return ERROR_JTAG_DEVICE_ERROR;
292
293
294
295

		presto_data = 0;
		for (i = 0; i < 4 * 62; i++)
			if ((presto->status=FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
296
				return ERROR_JTAG_DEVICE_ERROR;
297
298
299

		usleep(100000);

300
		if ((presto->status = FT_SetBitMode(presto->handle, 0x00, 0)) != FT_OK)
301
			return ERROR_JTAG_DEVICE_ERROR;
302

303
		if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
304
			return ERROR_JTAG_DEVICE_ERROR;
305

306
307
		presto_data = 0xD0;
		if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
308
			return ERROR_JTAG_DEVICE_ERROR;
309

310
311
312
		/* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason,
		   probably a bug in library threading */
		usleep(100000);
313
		if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
314
			return ERROR_JTAG_DEVICE_ERROR;
315

316
		if (ftbytes!=1)
317
		{
318
			LOG_DEBUG("PRESTO not responding");
319
			return ERROR_JTAG_DEVICE_ERROR;
320
		}
321
	}
322

323
	if ((presto->status = FT_SetTimeouts(presto->handle, 0, 0)) != FT_OK)
324
		return ERROR_JTAG_DEVICE_ERROR;
325
326


327
	presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes);
328
329
	if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
		return ERROR_JTAG_DEVICE_ERROR;
330

331
	return ERROR_OK;
332
333
}

334
#elif BUILD_PRESTO_LIBFTDI == 1
335
static int presto_open_libftdi(char *req_serial)
336
{
zwelch's avatar
zwelch committed
337
	uint8_t presto_data;
338

339
	LOG_DEBUG("searching for PRESTO using libftdi");
340
341
342
343

	/* initialize FTDI context structure */
	if (ftdi_init(&presto->ftdic) < 0)
	{
344
		LOG_ERROR("unable to init libftdi: %s", presto->ftdic.error_str);
345
346
		return ERROR_JTAG_DEVICE_ERROR;
	}
347
348
349
350

	/* context, vendor id, product id */
	if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0)
	{
351
		LOG_ERROR("unable to open PRESTO: %s", presto->ftdic.error_str);
352
		return ERROR_JTAG_DEVICE_ERROR;
353
	}
354

355
356
	if (ftdi_usb_reset(&presto->ftdic) < 0)
	{
357
		LOG_ERROR("unable to reset PRESTO device");
358
		return ERROR_JTAG_DEVICE_ERROR;
359
360
361
362
	}

	if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0)
	{
363
		LOG_ERROR("unable to set latency timer");
364
		return ERROR_JTAG_DEVICE_ERROR;
365
366
367
368
	}

	if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
	{
369
		LOG_ERROR("unable to purge PRESTO buffers");
370
		return ERROR_JTAG_DEVICE_ERROR;
371
	}
372

373
	presto_data = 0xD0;
374
375
	if (presto_write(&presto_data, 1) != ERROR_OK)
	{
376
		LOG_ERROR("error writing to PRESTO");
377
		return ERROR_JTAG_DEVICE_ERROR;
378
379
380
381
	}

	if (presto_read(&presto_data, 1) != ERROR_OK)
	{
382
		LOG_DEBUG("no response from PRESTO, retrying");
383
384
385
386
387
388
389
390
391
392

		if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
			return ERROR_JTAG_DEVICE_ERROR;

		presto_data = 0xD0;
		if (presto_write(&presto_data, 1) != ERROR_OK)
			return ERROR_JTAG_DEVICE_ERROR;

		if (presto_read(&presto_data, 1) != ERROR_OK)
		{
393
			LOG_ERROR("no response from PRESTO, giving up");
394
395
396
397
398
399
			return ERROR_JTAG_DEVICE_ERROR;
		}
	}

	if (presto_write(presto_init_seq, sizeof(presto_init_seq)) != ERROR_OK)
	{
400
		LOG_ERROR("error writing PRESTO init sequence");
401
		return ERROR_JTAG_DEVICE_ERROR;
402
403
	}

404
405
406
407
	return ERROR_OK;
}
#endif /* BUILD_PRESTO_LIBFTDI == 1 */

408
static int presto_open(char *req_serial)
409
410
411
412
413
414
415
416
417
418
419
{
	presto->buff_out_pos=0;
	presto->buff_in_pos=0;
	presto->buff_in_len=0;
	presto->buff_in_exp=0;

	presto->total_out=0;
	presto->total_in=0;

	presto->jtag_tms=0;
	presto->jtag_tck=0;
oharboe's avatar
oharboe committed
420
	presto->jtag_rst=0;
421
422
423
	presto->jtag_tdi_data=0;
	presto->jtag_tdi_count=0;

oharboe's avatar
oharboe committed
424
	presto->jtag_speed=0;
zwelch's avatar
zwelch committed
425

426
427
428
429
430
431
#if BUILD_PRESTO_FTD2XX == 1
	return presto_open_ftd2xx(req_serial);
#elif BUILD_PRESTO_LIBFTDI == 1
	return presto_open_libftdi(req_serial);
#endif
}
432

433
static int presto_close(void)
434
435
{

436
	int result = ERROR_OK;
437

438
#if BUILD_PRESTO_FTD2XX == 1
439
	unsigned long ftbytes;
440

441
442
	if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
		return result;
443

444
	presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
445
	if (presto->status != FT_OK)
446
447
448
449
450
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes);
	if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
		result = ERROR_JTAG_DEVICE_ERROR;
451

452
	if ((presto->status = FT_SetLatencyTimer(presto->handle, 16)) != FT_OK)
453
		result = ERROR_JTAG_DEVICE_ERROR;
454
455

	if ((presto->status = FT_Close(presto->handle)) != FT_OK)
456
		result = ERROR_JTAG_DEVICE_ERROR;
457
458
	else
		presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
459

460
#elif BUILD_PRESTO_LIBFTDI == 1
461
462

	if ((presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq))) != sizeof(presto_init_seq))
463
		result = ERROR_JTAG_DEVICE_ERROR;
464

465
466
	if ((presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16)) < 0)
		result = ERROR_JTAG_DEVICE_ERROR;
467

468
469
470
471
	if ((presto->retval = ftdi_usb_close(&presto->ftdic)) < 0)
		result = ERROR_JTAG_DEVICE_ERROR;
	else
		ftdi_deinit(&presto->ftdic);
472
#endif
473
474
475
476

	return result;
}

477
static int presto_flush(void)
478
{
479
480
	if (presto->buff_out_pos == 0)
		return ERROR_OK;
481

482
483
484
#if BUILD_PRESTO_FTD2XX == 1
	if (presto->status != FT_OK)
#elif BUILD_PRESTO_LIBFTDI == 1
485
	if (presto->retval < 0)
486
#endif
487
	{
488
		LOG_DEBUG("error in previous communication, canceling I/O operation");
489
		return ERROR_JTAG_DEVICE_ERROR;
490
491
	}

492
	if (presto_write(presto->buff_out, presto->buff_out_pos) != ERROR_OK)
493
	{
494
495
		presto->buff_out_pos = 0;
		return ERROR_JTAG_DEVICE_ERROR;
496
497
	}

498
	presto->total_out += presto->buff_out_pos;
499
	presto->buff_out_pos = 0;
500

501
502
	if (presto->buff_in_exp == 0)
		return ERROR_OK;
503

504
505
	presto->buff_in_pos = 0;
	presto->buff_in_len = 0;
506

507
	if (presto_read(presto->buff_in, presto->buff_in_exp) != ERROR_OK)
508
	{
509
510
		presto->buff_in_exp = 0;
		return ERROR_JTAG_DEVICE_ERROR;
511
512
	}

513
	presto->total_in += presto->buff_in_exp;
514
515
	presto->buff_in_len = presto->buff_in_exp;
	presto->buff_in_exp = 0;
516

517
	return ERROR_OK;
518
519
}

520
static int presto_sendbyte(int data)
521
{
522
	if (data == EOF) return presto_flush();
523
524
525

	if (presto->buff_out_pos < BUFFER_SIZE)
	{
zwelch's avatar
zwelch committed
526
		presto->buff_out[presto->buff_out_pos++] = (uint8_t)data;
527
		if (((data & 0xC0) == 0x40) || ((data & 0xD0)== 0xD0))
528
529
			presto->buff_in_exp++;
	}
530
531
	else
		return ERROR_JTAG_DEVICE_ERROR;
532

533
#if BUILD_PRESTO_FTD2XX == 1
534
	if (presto->buff_out_pos >= BUFFER_SIZE)
535
536
537
538
#elif BUILD_PRESTO_LIBFTDI == 1
	/* libftdi does not do background read, be sure that USB IN buffer does not overflow (128 bytes only!) */
	if (presto->buff_out_pos >= BUFFER_SIZE || presto->buff_in_exp==128)
#endif
539
		return presto_flush();
540

541
	return ERROR_OK;
542
543
}

544
545
#if 0
static int presto_getbyte(void)
546
{
547
	if (presto->buff_in_pos < presto->buff_in_len)
548
549
		return presto->buff_in[presto->buff_in_pos++];

550
551
	if (presto->buff_in_exp == 0)
		return -1;
552

553
554
	if (presto_flush() != ERROR_OK)
		return -1;
555
556
557
558
559
560

	if (presto->buff_in_pos<presto->buff_in_len)
		return presto->buff_in[presto->buff_in_pos++];

	return -1;
}
561
#endif
562
563
564

/* -------------------------------------------------------------------------- */

565
static int presto_tdi_flush(void)
oharboe's avatar
oharboe committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579
{
	if (presto->jtag_tdi_count == 0)
		return 0;

	if (presto->jtag_tck == 0)
	{
		LOG_ERROR("BUG: unexpected TAP condition, TCK low");
		return -1;
	}

	presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
	presto_sendbyte(presto->jtag_tdi_data);
	presto->jtag_tdi_count = 0;
	presto->jtag_tdi_data = 0;
zwelch's avatar
zwelch committed
580

oharboe's avatar
oharboe committed
581
582
583
	return 0;
}

584
static int presto_tck_idle(void)
oharboe's avatar
oharboe committed
585
586
587
588
589
590
{
	if (presto->jtag_tck == 1)
	{
		presto_sendbyte(0xCA);
		presto->jtag_tck = 0;
	}
zwelch's avatar
zwelch committed
591

oharboe's avatar
oharboe committed
592
593
594
595
596
	return 0;
}

/* -------------------------------------------------------------------------- */

597
static int presto_bitq_out(int tms, int tdi, int tdo_req)
598
{
oharboe's avatar
oharboe committed
599
600
	int i;
	unsigned char cmd;
601

602
	if (presto->jtag_tck == 0)
603
	{
oharboe's avatar
oharboe committed
604
		presto_sendbyte(0xA4); /* LED idicator - JTAG active */
605
	}
oharboe's avatar
oharboe committed
606
	else if (presto->jtag_speed == 0 && !tdo_req && tms == presto->jtag_tms)
607
	{
oharboe's avatar
oharboe committed
608
		presto->jtag_tdi_data |= (tdi != 0) << presto->jtag_tdi_count;
609

610
		if (++presto->jtag_tdi_count == 4)
oharboe's avatar
oharboe committed
611
			presto_tdi_flush();
612

oharboe's avatar
oharboe committed
613
		return 0;
614
615
	}

oharboe's avatar
oharboe committed
616
	presto_tdi_flush();
617

oharboe's avatar
oharboe committed
618
619
	cmd = tdi ? 0xCB : 0xCA;
	presto_sendbyte(cmd);
620

621
	if (tms != presto->jtag_tms)
622
	{
oharboe's avatar
oharboe committed
623
		presto_sendbyte((tms ? 0xEC : 0xE8) | (presto->jtag_rst ? 0x02 : 0));
624
		presto->jtag_tms = tms;
625
626
	}

oharboe's avatar
oharboe committed
627
628
629
630
631
632
633
634
635
636
637
638
	/* delay with TCK low */
	for (i=presto->jtag_speed; i>1; i--)
		presto_sendbyte(cmd);

	cmd |= 0x04;
	presto_sendbyte(cmd | (tdo_req ? 0x10 : 0));

	/* delay with TCK high */
	for (i=presto->jtag_speed; i>1; i--)
		presto_sendbyte(cmd);

	presto->jtag_tck = 1;
639
640
641
642

	return 0;
}

643
static int presto_bitq_flush(void)
644
{
oharboe's avatar
oharboe committed
645
646
	presto_tdi_flush();
	presto_tck_idle();
647

oharboe's avatar
oharboe committed
648
	presto_sendbyte(0xA0); /* LED idicator - JTAG idle */
649
650
651
652

	return presto_flush();
}

653
static int presto_bitq_in_rdy(void)
654
{
655
656
	if (presto->buff_in_pos>=presto->buff_in_len)
		return 0;
657
658
659
	return presto->buff_in_len-presto->buff_in_pos;
}

660
static int presto_bitq_in(void)
661
{
662
663
	if (presto->buff_in_pos>=presto->buff_in_len)
		return -1;
664
665
666
667
	if (presto->buff_in[presto->buff_in_pos++]&0x08) return 1;
	return 0;
}

668
static int presto_bitq_sleep(unsigned long us)
669
670
671
{
	long waits;

oharboe's avatar
oharboe committed
672
673
	presto_tdi_flush();
	presto_tck_idle();
674

675
	if (us > 100000)
676
677
678
679
680
681
	{
		presto_bitq_flush();
		jtag_sleep(us);
		return 0;
	}

682
683
684
	waits = us / 170 + 2;
	while (waits--)
		presto_sendbyte(0x80);
685
686
687
688

	return 0;
}

689
static int presto_bitq_reset(int trst, int srst)
690
{
oharboe's avatar
oharboe committed
691
692
	presto_tdi_flush();
	presto_tck_idle();
693

oharboe's avatar
oharboe committed
694
695
696
	/* add a delay after possible TCK transition */
	presto_sendbyte(0x80);
	presto_sendbyte(0x80);
697

oharboe's avatar
oharboe committed
698
699
	presto->jtag_rst = trst || srst;
	presto_sendbyte((presto->jtag_rst ? 0xEA : 0xE8) | (presto->jtag_tms ? 0x04 : 0));
700
701
702
703
704
705

	return 0;
}

/* -------------------------------------------------------------------------- */

706
static int presto_jtag_khz(int khz, int *jtag_speed)
707
{
oharboe's avatar
oharboe committed
708
709
710
711
712
	if (khz < 0)
	{
		*jtag_speed=0;
		return ERROR_INVALID_ARGUMENTS;
	}
713

oharboe's avatar
oharboe committed
714
715
	if (khz >= 3000) *jtag_speed = 0;
	else *jtag_speed = (1000+khz-1)/khz;
zwelch's avatar
zwelch committed
716

oharboe's avatar
oharboe committed
717
718
719
	return 0;
}

720
static int presto_jtag_speed_div(int speed, int *khz)
721
{
oharboe's avatar
oharboe committed
722
723
724
725
726
727
728
729
	if ((speed < 0) || (speed > 1000))
	{
		*khz=0;
		return ERROR_INVALID_ARGUMENTS;
	}

	if (speed == 0) *khz = 3000;
	else *khz = 1000/speed;
zwelch's avatar
zwelch committed
730

oharboe's avatar
oharboe committed
731
732
733
	return 0;
}

734
static int presto_jtag_speed(int speed)
oharboe's avatar
oharboe committed
735
736
{
	int khz;
zwelch's avatar
zwelch committed
737

oharboe's avatar
oharboe committed
738
	if (presto_jtag_speed_div(speed, &khz))
739
740
741
742
	{
		return ERROR_INVALID_ARGUMENTS;
	}

oharboe's avatar
oharboe committed
743
	presto->jtag_speed = speed;
zwelch's avatar
zwelch committed
744

oharboe's avatar
oharboe committed
745
746
747
748
	if (khz%1000 == 0)
		LOG_INFO("setting speed to %d, max. TCK freq. is %d MHz", speed, khz/1000);
	else
		LOG_INFO("setting speed to %d, max. TCK freq. is %d kHz", speed, khz);
zwelch's avatar
zwelch committed
749

oharboe's avatar
oharboe committed
750
	return 0;
751
752
}

753
static char *presto_serial;
754

755
static int presto_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
756
757
758
{
	if (argc == 1)
	{
759
760
		if (presto_serial)
			free(presto_serial);
761
762
763
764
		presto_serial = strdup(args[0]);
	}
	else
	{
765
		LOG_ERROR("expected exactly one argument to presto_serial <serial-number>");
766
767
768
769
770
	}

	return ERROR_OK;
}

771
static int presto_jtag_register_commands(struct command_context_s *cmd_ctx)
772
773
774
775
776
777
{
	register_command(cmd_ctx, NULL, "presto_serial", presto_handle_serial_command,
		COMMAND_CONFIG, NULL);
	return ERROR_OK;
}

778
static int presto_jtag_init(void)
779
{
780
	if (presto_open(presto_serial) != ERROR_OK)
781
782
	{
		presto_close();
783
		if (presto_serial != NULL)
784
			LOG_ERROR("Cannot open PRESTO, serial number '%s'", presto_serial);
785
		else
786
			LOG_ERROR("Cannot open PRESTO");
787
788
		return ERROR_JTAG_INIT_FAILED;
	}
789
	LOG_INFO("PRESTO open, serial number '%s'", presto->serial);
790

791
	/* use JTAG speed setting from configuration file */
792
	presto_jtag_speed(jtag_get_speed());
793

794
	bitq_interface = &presto_bitq;
795
796
797
	return ERROR_OK;
}

798
static int presto_jtag_quit(void)
799
800
801
{
	bitq_cleanup();
	presto_close();
802
	LOG_INFO("PRESTO closed");
803
804
805
806

	if (presto_serial)
	{
		free(presto_serial);
807
		presto_serial = NULL;
808
809
810
811
	}

	return ERROR_OK;
}