ft2232.c 53.2 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2004, 2006 by Dominic Rath                              *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
5
6
7
 *   Copyright (C) 2008 by Spencer Oliver                                  *
 *   spen@spen-soft.co.uk                                                  *
 *                                                                         *
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 *   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

#include "replacements.h"

/* project specific includes */
#include "log.h"
#include "types.h"
#include "jtag.h"
#include "configuration.h"
#include "time_support.h"

/* system includes */
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

/* FT2232 access library includes */
#if BUILD_FT2232_FTD2XX == 1
#include <ftd2xx.h>
#elif BUILD_FT2232_LIBFTDI == 1
#include <ftdi.h>
#endif

/* enable this to debug io latency
 */
#if 0
#define _DEBUG_USB_IO_
#endif

/* enable this to debug communication
 */
#if 0
#define _DEBUG_USB_COMMS_
#endif

int ft2232_execute_queue(void);

int ft2232_speed(int speed);
mifi's avatar
mifi committed
67
68
int ft2232_speed_div(int speed, int *khz);
int ft2232_khz(int khz, int *jtag_speed);
69
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
int ft2232_register_commands(struct command_context_s *cmd_ctx);
int ft2232_init(void);
int ft2232_quit(void);

int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);

char *ft2232_device_desc = NULL;
char *ft2232_serial = NULL;
char *ft2232_layout = NULL;
unsigned char ft2232_latency = 2;

#define MAX_USB_IDS	8
/* vid = pid = 0 marks the end of the list */
static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };

typedef struct ft2232_layout_s
{
	char* name;
	int(*init)(void);
	void(*reset)(int trst, int srst);
	void(*blink)(void);
} ft2232_layout_t;

/* init procedures for supported layouts */
int usbjtag_init(void);
int jtagkey_init(void);
int olimex_jtag_init(void);
int flyswatter_init(void);
int turtle_init(void);
int comstick_init(void);
int stm32stick_init(void);

/* reset procedures for supported layouts */
void usbjtag_reset(int trst, int srst);
void jtagkey_reset(int trst, int srst);
void olimex_jtag_reset(int trst, int srst);
void flyswatter_reset(int trst, int srst);
void turtle_reset(int trst, int srst);
void comstick_reset(int trst, int srst);
void stm32stick_reset(int trst, int srst);

/* blink procedures for layouts that support a blinking led */
void olimex_jtag_blink(void);
void turtle_jtag_blink(void);

ft2232_layout_t ft2232_layouts[] =
{
	{"usbjtag", usbjtag_init, usbjtag_reset, NULL},
	{"jtagkey", jtagkey_init, jtagkey_reset, NULL},
	{"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
	{"oocdlink", jtagkey_init, jtagkey_reset, NULL},
	{"signalyzer", usbjtag_init, usbjtag_reset, NULL},
	{"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
	{"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
	{"flyswatter", flyswatter_init, flyswatter_reset, NULL},
	{"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
	{"comstick", comstick_init, comstick_reset, NULL},
	{"stm32stick", stm32stick_init, stm32stick_reset, NULL},
	{NULL, NULL, NULL},
};

static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;

static ft2232_layout_t *layout;
static u8 low_output = 0x0;
static u8 low_direction = 0x0;
static u8 high_output = 0x0;
static u8 high_direction = 0x0;

#if BUILD_FT2232_FTD2XX == 1
static FT_HANDLE ftdih = NULL;
#elif BUILD_FT2232_LIBFTDI == 1
static struct ftdi_context ftdic;
#endif

static u8 *ft2232_buffer = NULL;
static int ft2232_buffer_size = 0;
static int ft2232_read_pointer = 0;
static int ft2232_expect_read = 0;
#define FT2232_BUFFER_SIZE	131072
#define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
#define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]

jtag_interface_t ft2232_interface = 
{
	.name = "ft2232",
	.execute_queue = ft2232_execute_queue,
	.speed = ft2232_speed,
ntfreak's avatar
ntfreak committed
162
163
	.speed_div = ft2232_speed_div,
	.khz = ft2232_khz, 
164
165
166
167
168
169
170
171
172
173
174
175
176
	.register_commands = ft2232_register_commands,
	.init = ft2232_init,
	.quit = ft2232_quit,
};

int ft2232_write(u8 *buf, int size, u32* bytes_written)
{
#if BUILD_FT2232_FTD2XX == 1
	FT_STATUS status;
	DWORD dw_bytes_written;
	if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
	{
		*bytes_written = dw_bytes_written;
177
		LOG_ERROR("FT_Write returned: %lu", status);
178
179
180
181
182
183
184
185
186
187
188
189
		return ERROR_JTAG_DEVICE_ERROR;
	}
	else
	{
		*bytes_written = dw_bytes_written;
		return ERROR_OK;	
	}
#elif BUILD_FT2232_LIBFTDI == 1
	int retval;
	if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
	{
		*bytes_written = 0;
190
		LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
		return ERROR_JTAG_DEVICE_ERROR;
	}
	else
	{
		*bytes_written = retval;
		return ERROR_OK;	
	}
#endif
}

int ft2232_read(u8* buf, int size, u32* bytes_read)
{
#if BUILD_FT2232_FTD2XX == 1
	DWORD dw_bytes_read;
	FT_STATUS status;
	int timeout = 5;
	*bytes_read = 0;

	while ((*bytes_read < size) && timeout--)
	{
		if ((status = FT_Read(ftdih, buf + *bytes_read, size - 
			*bytes_read, &dw_bytes_read)) != FT_OK)		
		{
			*bytes_read = 0; 
215
			LOG_ERROR("FT_Read returned: %lu", status);
216
217
218
219
220
221
222
223
224
225
226
227
228
229
			return ERROR_JTAG_DEVICE_ERROR;
		}
		*bytes_read += dw_bytes_read; 
	}
#elif BUILD_FT2232_LIBFTDI == 1
	int retval;
	int timeout = 100;
	*bytes_read = 0;
	
	while ((*bytes_read < size) && timeout--)
	{
		if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
		{
			*bytes_read = 0;
230
			LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
231
232
233
234
235
236
237
238
			return ERROR_JTAG_DEVICE_ERROR;
		}
		*bytes_read += retval;
	}
#endif

	if (*bytes_read < size)
	{
239
		LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
		return ERROR_JTAG_DEVICE_ERROR;
	}
	
	return ERROR_OK;
}

int ft2232_speed(int speed)
{
	u8 buf[3];
	int retval;
	u32 bytes_written;

	buf[0] = 0x86; /* command "set divisor" */
	buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
	buf[2] = (speed >> 8) & 0xff; /* valueH */
	
256
	LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
257
258
	if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
	{
259
		LOG_ERROR("couldn't set FT2232 TCK speed");
260
261
262
263
264
265
		return retval;
	}

	return ERROR_OK;
}

mifi's avatar
mifi committed
266
267
int ft2232_speed_div(int speed, int *khz)
{
ntfreak's avatar
ntfreak committed
268
269
270
271
272
273
	/* Take a look in the FT2232 manual, 
	 * AN2232C-01 Command Processor for
	 * MPSSE and MCU Host Bus. Chapter 3.8 */
	
	*khz = 6000 / (1+speed);
	
mifi's avatar
mifi committed
274
275
276
277
278
	return ERROR_OK;
}

int ft2232_khz(int khz, int *jtag_speed)
{
279
280
281
282
283
	if (khz==0)
	{
		LOG_ERROR("RCLK not supported");
		return ERROR_FAIL;
	}
ntfreak's avatar
ntfreak committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
	/* Take a look in the FT2232 manual, 
	 * AN2232C-01 Command Processor for
	 * MPSSE and MCU Host Bus. Chapter 3.8
	 * 
	 * We will calc here with a multiplier
	 * of 10 for better rounding later. */
	
	/* Calc speed, (6000 / khz) - 1 */
	/* Use 65000 for better rounding */
	*jtag_speed = (60000 / khz) - 10;
	
	/* Add 0.9 for rounding */
	*jtag_speed += 9;
	
	/* Calc real speed */
	*jtag_speed = *jtag_speed / 10;
	
	/* Check if speed is greater than 0 */
	if (*jtag_speed < 0)
	{
		*jtag_speed = 0;
	}
	
307
308
309
310
311
312
	/* Check max value */
	if (*jtag_speed > 0xFFFF)
	{
		*jtag_speed = 0xFFFF;
	}
	
mifi's avatar
mifi committed
313
314
315
	return ERROR_OK;
}

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
int ft2232_register_commands(struct command_context_s *cmd_ctx)
{
	register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
		COMMAND_CONFIG, NULL);
	register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
		COMMAND_CONFIG, NULL);
	register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
		COMMAND_CONFIG, NULL);
	register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
					 COMMAND_CONFIG, NULL);
	register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
					 COMMAND_CONFIG, NULL);
	return ERROR_OK;
}

void ft2232_end_state(enum tap_state state)
{
	if (tap_move_map[state] != -1)
		end_state = state;
	else
	{
337
		LOG_ERROR("BUG: %i is not a valid end state", state);
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
365
366
367
368
369
370
371
372
373
374
375
376
		exit(-1);
	}
}

void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
{
	int num_bytes = ((scan_size + 7) / 8);
	int bits_left = scan_size;
	int cur_byte = 0;

	while(num_bytes-- > 1)
	{
		buffer[cur_byte] = BUFFER_READ;
		cur_byte++;
		bits_left -= 8;
	}

	buffer[cur_byte] = 0x0;

	if (bits_left > 1)
	{
		buffer[cur_byte] = BUFFER_READ >> 1;
	}

	buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);

}

void ft2232_debug_dump_buffer(void)
{
	int i;
	char line[256];
	char *line_p = line;
	
	for (i = 0; i < ft2232_buffer_size; i++)
	{
		line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
		if (i % 16 == 15)
		{
377
			LOG_DEBUG("%s", line);
378
379
380
381
382
			line_p = line;
		}
	}
	
	if (line_p != line)
383
		LOG_DEBUG("%s", line);
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
}

int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
{
	jtag_command_t *cmd;
	u8 *buffer;
	int scan_size;
	enum scan_type type;
	int retval;
	u32 bytes_written;
	u32 bytes_read;
	
#ifdef _DEBUG_USB_IO_
	struct timeval start, inter, inter2, end;
	struct timeval d_inter, d_inter2, d_end;
#endif

#ifdef _DEBUG_USB_COMMS_
402
	LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
403
404
405
406
407
408
409
410
411
	ft2232_debug_dump_buffer();
#endif

#ifdef _DEBUG_USB_IO_
	gettimeofday(&start, NULL);	
#endif

	if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
	{
412
		LOG_ERROR("couldn't write MPSSE commands to FT2232");
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
		exit(-1);
	}
	
#ifdef _DEBUG_USB_IO_
	gettimeofday(&inter, NULL);	
#endif
	
	if (ft2232_expect_read)
	{
		int timeout = 100;
		ft2232_buffer_size = 0;
		
#ifdef _DEBUG_USB_IO_
		gettimeofday(&inter2, NULL);	
#endif
		
		if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
		{
431
			LOG_ERROR("couldn't read from FT2232");
432
433
434
435
436
437
438
439
440
441
			exit(-1);
		}
		
#ifdef _DEBUG_USB_IO_
		gettimeofday(&end, NULL);	

		timeval_subtract(&d_inter, &inter, &start);
		timeval_subtract(&d_inter2, &inter2, &start);
		timeval_subtract(&d_end, &end, &start);

442
		LOG_INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
443
444
445
446
447
448
449
#endif
	
		
		ft2232_buffer_size = bytes_read;
		
		if (ft2232_expect_read != ft2232_buffer_size)
		{
450
			LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
451
452
453
454
455
456
			ft2232_debug_dump_buffer();	

			exit(-1);
		}

#ifdef _DEBUG_USB_COMMS_
457
		LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
		ft2232_debug_dump_buffer();
#endif
	}

	ft2232_expect_read = 0;
	ft2232_read_pointer = 0;
	
	/* return ERROR_OK, unless a jtag_read_buffer returns a failed check
	 * that wasn't handled by a caller-provided error handler
	 */ 
	retval = ERROR_OK;
	
	cmd = first;
	while (cmd != last)
	{
		switch (cmd->type)
		{
			case JTAG_SCAN:
				type = jtag_scan_type(cmd->cmd.scan);
				if (type != SCAN_OUT)
				{
					scan_size = jtag_scan_size(cmd->cmd.scan);
					buffer = calloc(CEIL(scan_size, 8), 1);
					ft2232_read_scan(type, buffer, scan_size);
					if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
						retval = ERROR_JTAG_QUEUE_FAILED;
					free(buffer);
				}
				break;
			default:
				break;
		}
		cmd = cmd->next;
	}
	
	ft2232_buffer_size = 0;

	return retval;
}

void ft2232_add_pathmove(pathmove_command_t *cmd)
{
	int num_states = cmd->num_states;
	u8 tms_byte;
	int state_count;

	state_count = 0;
	while (num_states)
	{
		int bit_count = 0;
		
509
510
		int num_states_batch = num_states > 7 ? 7 : num_states;

oharboe's avatar
oharboe committed
511
		tms_byte = 0x0;
512
513
514
		/* command "Clock Data to TMS/CS Pin (no Read)" */
		BUFFER_ADD = 0x4b;
		/* number of states remaining */
515
		BUFFER_ADD = num_states_batch - 1;
516
		
517
		while (num_states_batch--)
518
519
520
521
522
523
524
		{
			if (tap_transitions[cur_state].low == cmd->path[state_count])
				buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
			else if (tap_transitions[cur_state].high == cmd->path[state_count])
				buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
			else
			{
525
				LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
				exit(-1);
			}

			cur_state = cmd->path[state_count];
			state_count++;
			num_states--;
		}
		
		BUFFER_ADD = tms_byte;
	}
	
	end_state = cur_state;
}

void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
{
	int num_bytes = (scan_size + 7) / 8;
	int bits_left = scan_size;
	int cur_byte = 0;
	int last_bit;

	if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
	{
		/* command "Clock Data to TMS/CS Pin (no Read)" */
		BUFFER_ADD = 0x4b;
		/* scan 7 bit */
		BUFFER_ADD = 0x6;
		/* TMS data bits */
		if (ir_scan)
		{
			BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
			cur_state = TAP_SI;
		}
		else
		{
			BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
			cur_state = TAP_SD;
		}
564
		/* LOG_DEBUG("added TMS scan (no read)"); */
565
566
567
568
569
570
571
572
573
574
	}
	
	/* add command for complete bytes */
	while (num_bytes > 1)
	{
		int thisrun_bytes;
		if (type == SCAN_IO)
		{
			/* Clock Data Bytes In and Out LSB First */
			BUFFER_ADD = 0x39;
575
			/* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
576
577
578
579
580
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x19;
581
			/* LOG_DEBUG("added TDI bytes (o)"); */
582
583
584
585
586
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x28;
587
			/* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
		}
		thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
		num_bytes -= thisrun_bytes;
		BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
		BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
		if (type != SCAN_IN)
		{
			/* add complete bytes */
			while(thisrun_bytes-- > 0)
			{
				BUFFER_ADD = buffer[cur_byte];
				cur_byte++;
				bits_left -= 8;
			}
		}
		else /* (type == SCAN_IN) */
		{
			bits_left -= 8 * (thisrun_bytes);
		}
	}
	
	/* the most signifcant bit is scanned during TAP movement */
	if (type != SCAN_IN)
		last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
	else
		last_bit = 0;

	/* process remaining bits but the last one */
	if (bits_left > 1)
	{
		if (type == SCAN_IO)
		{
			/* Clock Data Bits In and Out LSB First */
			BUFFER_ADD = 0x3b;
622
			/* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
623
624
625
626
627
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x1b;
628
			/* LOG_DEBUG("added TDI bits (o)"); */
629
630
631
632
633
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x2a;
634
			/* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
635
636
637
638
639
640
641
642
643
644
645
646
647
		}
		BUFFER_ADD = bits_left - 2;
		if (type != SCAN_IN)
			BUFFER_ADD = buffer[cur_byte];
	}

	if ((ir_scan && (end_state == TAP_SI)) ||
		(!ir_scan && (end_state == TAP_SD)))
	{
		if (type == SCAN_IO)
		{
			/* Clock Data Bits In and Out LSB First */
			BUFFER_ADD = 0x3b;
648
			/* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
649
650
651
652
653
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x1b;
654
			/* LOG_DEBUG("added TDI bits (o)"); */
655
656
657
658
659
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x2a;
660
			/* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
661
662
663
664
665
666
667
668
669
670
671
		}
		BUFFER_ADD = 0x0;
		BUFFER_ADD = last_bit;
	}
	else
	{
		/* move from Shift-IR/DR to end state */
		if (type != SCAN_OUT)
		{
			/* Clock Data to TMS/CS Pin with Read */
			BUFFER_ADD = 0x6b;
672
			/* LOG_DEBUG("added TMS scan (read)"); */
673
674
675
676
677
		}
		else
		{
			/* Clock Data to TMS/CS Pin (no Read) */
			BUFFER_ADD = 0x4b;
678
			/* LOG_DEBUG("added TMS scan (no read)"); */
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
		}
		BUFFER_ADD = 0x6;
		BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
		cur_state = end_state;
	}
}

int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
{
	int num_bytes = (scan_size + 7) / 8;
	int bits_left = scan_size;
	int cur_byte = 0;
	int last_bit;
	u8 *receive_buffer = malloc(CEIL(scan_size, 8));
	u8 *receive_pointer = receive_buffer;
	u32 bytes_written;
	u32 bytes_read;
	int retval;
	int thisrun_read = 0;
	
	if (cmd->ir_scan)
	{
701
		LOG_ERROR("BUG: large IR scans are not supported");
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
		exit(-1);
	}

	if (cur_state != TAP_SD)
	{
		/* command "Clock Data to TMS/CS Pin (no Read)" */
		BUFFER_ADD = 0x4b;
		/* scan 7 bit */
		BUFFER_ADD = 0x6;
		/* TMS data bits */
		BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
		cur_state = TAP_SD;
	}
	
	if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
	{
718
		LOG_ERROR("couldn't write MPSSE commands to FT2232");
719
720
		exit(-1);
	}
721
	LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
722
723
724
725
726
727
728
729
730
731
732
	ft2232_buffer_size = 0;
	
	/* add command for complete bytes */
	while (num_bytes > 1)
	{
		int thisrun_bytes;
		
		if (type == SCAN_IO)
		{
			/* Clock Data Bytes In and Out LSB First */
			BUFFER_ADD = 0x39;
733
			/* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
734
735
736
737
738
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x19;
739
			/* LOG_DEBUG("added TDI bytes (o)"); */
740
741
742
743
744
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x28;
745
			/* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
		}
		thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
		thisrun_read = thisrun_bytes;
		num_bytes -= thisrun_bytes;
		BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
		BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
		if (type != SCAN_IN)
		{
			/* add complete bytes */
			while(thisrun_bytes-- > 0)
			{
				BUFFER_ADD = buffer[cur_byte];
				cur_byte++;
				bits_left -= 8;
			}
		}
		else /* (type == SCAN_IN) */
		{
			bits_left -= 8 * (thisrun_bytes);
		}

		if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
		{
769
			LOG_ERROR("couldn't write MPSSE commands to FT2232");
770
771
			exit(-1);
		}
772
		LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
773
774
775
776
777
778
		ft2232_buffer_size = 0;
		
		if (type != SCAN_OUT)
		{
			if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
			{
779
				LOG_ERROR("couldn't read from FT2232");
780
781
				exit(-1);
			}
782
			LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
			receive_pointer += bytes_read;
		}
	}
	
	thisrun_read = 0;
	
	/* the most signifcant bit is scanned during TAP movement */
	if (type != SCAN_IN)
		last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
	else
		last_bit = 0;

	/* process remaining bits but the last one */
	if (bits_left > 1)
	{
		if (type == SCAN_IO)
		{
			/* Clock Data Bits In and Out LSB First */
			BUFFER_ADD = 0x3b;
802
			/* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
803
804
805
806
807
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x1b;
808
			/* LOG_DEBUG("added TDI bits (o)"); */
809
810
811
812
813
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x2a;
814
			/* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
		}
		BUFFER_ADD = bits_left - 2;
		if (type != SCAN_IN)
			BUFFER_ADD = buffer[cur_byte];
			
		if (type != SCAN_OUT)
			thisrun_read += 2;
	}

	if (end_state == TAP_SD)
	{
		if (type == SCAN_IO)
		{
			/* Clock Data Bits In and Out LSB First */
			BUFFER_ADD = 0x3b;
830
			/* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
831
832
833
834
835
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x1b;
836
			/* LOG_DEBUG("added TDI bits (o)"); */
837
838
839
840
841
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x2a;
842
			/* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
843
844
845
846
847
848
849
850
851
852
853
		}
		BUFFER_ADD = 0x0;
		BUFFER_ADD = last_bit;
	}
	else
	{
		/* move from Shift-IR/DR to end state */
		if (type != SCAN_OUT)
		{
			/* Clock Data to TMS/CS Pin with Read */
			BUFFER_ADD = 0x6b;
854
			/* LOG_DEBUG("added TMS scan (read)"); */
855
856
857
858
859
		}
		else
		{
			/* Clock Data to TMS/CS Pin (no Read) */
			BUFFER_ADD = 0x4b;
860
			/* LOG_DEBUG("added TMS scan (no read)"); */
861
862
863
864
865
866
867
868
869
870
871
		}
		BUFFER_ADD = 0x6;
		BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
		cur_state = end_state;
	}
	
	if (type != SCAN_OUT)
		thisrun_read += 1;
	
	if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
	{
872
		LOG_ERROR("couldn't write MPSSE commands to FT2232");
873
874
		exit(-1);
	}
875
	LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
876
877
878
879
880
881
	ft2232_buffer_size = 0;
	
	if (type != SCAN_OUT)
	{
		if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
		{
882
			LOG_ERROR("couldn't read from FT2232");
883
884
			exit(-1);
		}
885
		LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
		receive_pointer += bytes_read;
	}
	
	return ERROR_OK;
}

int ft2232_predict_scan_out(int scan_size, enum scan_type type)
{
	int predicted_size = 3;
	int num_bytes = (scan_size - 1) / 8;
	
	if (cur_state != TAP_SD)
		predicted_size += 3;
	
	if (type == SCAN_IN)	/* only from device to host */
	{
		/* complete bytes */
		predicted_size += (CEIL(num_bytes, 65536)) * 3;
		/* remaining bits - 1 (up to 7) */
		predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
	}
	else					/* host to device, or bidirectional */
	{
		/* complete bytes */
		predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
		/* remaining bits -1 (up to 7) */
		predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
	}

	return predicted_size;
}

int ft2232_predict_scan_in(int scan_size, enum scan_type type)
{
	int predicted_size = 0;
	
	if (type != SCAN_OUT)
	{
		/* complete bytes */
		predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
		/* remaining bits - 1 */
		predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
		/* last bit (from TMS scan) */
		predicted_size += 1;
	}
	
932
	/* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

	return predicted_size;
}

void usbjtag_reset(int trst, int srst)
{
	if (trst == 1)
	{
		if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
			low_direction |= nTRSTnOE;	/* switch to output pin (output is low) */
		else
			low_output &= ~nTRST;	/* switch output low */
	}
	else if (trst == 0)
	{
		if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
			low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
		else
			low_output |= nTRST; /* switch output high */
	}

	if (srst == 1)
	{
		if (jtag_reset_config & RESET_SRST_PUSH_PULL)
			low_output &= ~nSRST;	/* switch output low */
		else
			low_direction |= nSRSTnOE;	/* switch to output pin (output is low) */
	}
	else if (srst == 0)
	{
		if (jtag_reset_config & RESET_SRST_PUSH_PULL)
			low_output |= nSRST;	/* switch output high */
		else
			low_direction &= ~nSRSTnOE;	/* switch to input pin (high-Z) */
	}
	
	/* command "set data bits low byte" */
	BUFFER_ADD = 0x80;
	BUFFER_ADD = low_output;
	BUFFER_ADD = low_direction;

}

void jtagkey_reset(int trst, int srst)
{
	if (trst == 1)
	{
		if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
			high_output &= ~nTRSTnOE;
		else
			high_output &= ~nTRST;
	}
	else if (trst == 0)
	{
		if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
			high_output |= nTRSTnOE;
		else
			high_output |= nTRST;
	}

	if (srst == 1)
	{
		if (jtag_reset_config & RESET_SRST_PUSH_PULL)
			high_output &= ~nSRST;
		else
			high_output &= ~nSRSTnOE;
	}
	else if (srst == 0)