rlink.c 39.7 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
28
29
30
31
32
33
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
 *   Copyright (C) 2007,2008 yvind Harboe                                 *
 *   oyvind.harboe@zylin.com                                               *
 *                                                                         *
 *   Copyright (C) 2008 Rob Brown, Lou Deluxe                              *
 *   rob@cobbleware.com, lou.openocd012@fixit.nospammail.net               *
 *                                                                         *
 *   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

/* system includes */
#include <errno.h>
#include <string.h>
#include <usb.h>
34
#include <stdint.h>
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
67
68
69
70
71
72
73
74
75

/* project specific includes */
#include "log.h"
#include "types.h"
#include "jtag.h"
#include "configuration.h"
#include "rlink.h"
#include "st7.h"
#include "ep1_cmd.h"
#include "dtc_cmd.h"


/* This feature is made useless by running the DTC all the time.  When automatic, the LED is on whenever the DTC is running.  Otherwise, USB messages are sent to turn it on and off. */
#undef AUTOMATIC_BUSY_LED

/* This feature may require derating the speed due to reduced hold time. */
#undef USE_HARDWARE_SHIFTER_FOR_TMS


#define INTERFACE_NAME		"RLink"

#define USB_IDVENDOR		(0x138e)
#define USB_IDPRODUCT		(0x9000)

#define USB_EP1OUT_ADDR		(0x01)
#define USB_EP1OUT_SIZE		(16)
#define USB_EP1IN_ADDR		(USB_EP1OUT_ADDR | 0x80)
#define USB_EP1IN_SIZE		(USB_EP1OUT_SIZE)

#define USB_EP2OUT_ADDR		(0x02)
#define USB_EP2OUT_SIZE		(64)
#define USB_EP2IN_ADDR		(USB_EP2OUT_ADDR | 0x80)
#define USB_EP2IN_SIZE		(USB_EP2OUT_SIZE)
#define USB_EP2BANK_SIZE	(512)

#define USB_TIMEOUT_MS		(3 * 1000)

#define DTC_STATUS_POLL_BYTE	(ST7_USB_BUF_EP0OUT + 0xff)


#define ST7_PD_NBUSY_LED		ST7_PD0
ntfreak's avatar
ntfreak committed
76
77
78
79
80
#define ST7_PD_NRUN_LED			ST7_PD1
/* low enables VPP at adapter header, high connects it to GND instead */
#define ST7_PD_VPP_SEL			ST7_PD6
/* low: VPP = 12v, high: VPP <= 5v */
#define ST7_PD_VPP_SHDN			ST7_PD7
81

ntfreak's avatar
ntfreak committed
82
/* These pins are connected together */
83
84
85
#define ST7_PE_ADAPTER_SENSE_IN		ST7_PE3
#define ST7_PE_ADAPTER_SENSE_OUT	ST7_PE4

ntfreak's avatar
ntfreak committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Symbolic mapping between port pins and numbered IO lines */
#define ST7_PA_IO1	ST7_PA1
#define ST7_PA_IO2	ST7_PA2
#define ST7_PA_IO4	ST7_PA4
#define ST7_PA_IO8	ST7_PA6
#define ST7_PA_IO10	ST7_PA7
#define ST7_PB_IO5	ST7_PB5
#define ST7_PC_IO9	ST7_PC1
#define ST7_PC_IO3	ST7_PC2
#define ST7_PC_IO7	ST7_PC3
#define ST7_PE_IO6	ST7_PE5

/* Symbolic mapping between numbered IO lines and adapter signals */
#define ST7_PA_RTCK	ST7_PA_IO0
#define ST7_PA_NTRST	ST7_PA_IO1
#define ST7_PC_TDI	ST7_PC_IO3
#define ST7_PA_DBGRQ	ST7_PA_IO4
#define ST7_PB_NSRST	ST7_PB_IO5
#define ST7_PE_TMS	ST7_PE_IO6
#define ST7_PC_TCK	ST7_PC_IO7
#define ST7_PC_TDO	ST7_PC_IO9
#define ST7_PA_DBGACK	ST7_PA_IO10

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
static usb_dev_handle *pHDev;


/*
 * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
 * This function takes care of zeroing the unused bytes before sending the packet.
 * Any reply packet is not handled by this function.
 */
static
int
ep1_generic_commandl(
	usb_dev_handle	*pHDev,
	size_t		length,
	...
) {
	uint8_t		usb_buffer[USB_EP1OUT_SIZE];
	uint8_t		*usb_buffer_p;
	va_list		ap;
	int		usb_ret;

	if(length > sizeof(usb_buffer)) {
		length = sizeof(usb_buffer);
	}

	usb_buffer_p = usb_buffer;

	va_start(ap, length);
	while(length > 0) {
		*usb_buffer_p++ = va_arg(ap, int);
		length--;
	}
140

141
142
143
144
145
146
147
148
149
	memset(
		usb_buffer_p,
		0,
		sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
	);

	usb_ret = usb_bulk_write(
		pHDev,
		USB_EP1OUT_ADDR,
150
		(char *)usb_buffer, sizeof(usb_buffer),
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
		USB_TIMEOUT_MS
	);

	return(usb_ret);
}



#if 0
static
ssize_t
ep1_memory_read(
	usb_dev_handle	*pHDev,
	uint16_t	addr,
	size_t		length,
	uint8_t		*buffer
) {
	uint8_t		usb_buffer[USB_EP1OUT_SIZE];
	int		usb_ret;
	size_t		remain;
	ssize_t		count;

	usb_buffer[0] = EP1_CMD_MEMORY_READ;
	memset(
		usb_buffer + 4,
		0,
		sizeof(usb_buffer) - 4
	);

	remain = length;
	count = 0;

	while(remain) {
		if(remain > sizeof(usb_buffer)) {
			length = sizeof(usb_buffer);
		} else {
			length = remain;
		}

		usb_buffer[1] = addr >> 8;
		usb_buffer[2] = addr;
		usb_buffer[3] = length;

194
			usb_ret = usb_bulk_write(
195
196
197
198
199
200
201
202
			pHDev, USB_EP1OUT_ADDR,
			usb_buffer, sizeof(usb_buffer),
			USB_TIMEOUT_MS
		);

		if(usb_ret < sizeof(usb_buffer)) {
			break;
		}
203

204
205
206
207
208
209
210
211
212
		usb_ret = usb_bulk_read(
			pHDev, USB_EP1IN_ADDR,
			buffer, length,
			USB_TIMEOUT_MS
		);

		if(usb_ret < length) {
			break;
		}
213

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
		addr += length;
		buffer += length;
		count += length;
		remain -= length;
	}

	return(count);
}
#endif



static
ssize_t
ep1_memory_write(
	usb_dev_handle	*pHDev,
	uint16_t	addr,
	size_t		length,
	uint8_t	const	*buffer
) {
	uint8_t		usb_buffer[USB_EP1OUT_SIZE];
	int		usb_ret;
	size_t		remain;
	ssize_t		count;

	usb_buffer[0] = EP1_CMD_MEMORY_WRITE;

	remain = length;
	count = 0;

	while(remain) {
		if(remain > (sizeof(usb_buffer) - 4)) {
			length = (sizeof(usb_buffer) - 4);
		} else {
			length = remain;
		}

		usb_buffer[1] = addr >> 8;
		usb_buffer[2] = addr;
		usb_buffer[3] = length;
		memcpy(
			usb_buffer + 4,
			buffer,
			length
		);
		memset(
			usb_buffer + 4 + length,
			0,
			sizeof(usb_buffer) - 4 - length
		);

265
			usb_ret = usb_bulk_write(
266
			pHDev, USB_EP1OUT_ADDR,
267
			(char *)usb_buffer, sizeof(usb_buffer),
268
269
270
			USB_TIMEOUT_MS
		);

271
		if((size_t)usb_ret < sizeof(usb_buffer)) {
272
273
			break;
		}
274

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
		addr += length;
		buffer += length;
		count += length;
		remain -= length;
	}

	return(count);
}


#if 0
static
ssize_t
ep1_memory_writel(
	usb_dev_handle	*pHDev,
	uint16_t	addr,
	size_t		length,
	...
) {
	uint8_t		buffer[USB_EP1OUT_SIZE - 4];
	uint8_t		*buffer_p;
	va_list		ap;
	size_t		remain;

	if(length > sizeof(buffer)) {
		length = sizeof(buffer);
	}

	remain = length;
	buffer_p = buffer;

	va_start(ap, length);
	while(remain > 0) {
		*buffer_p++ = va_arg(ap, int);
		remain--;
	}

	return(ep1_memory_write(pHDev, addr, length, buffer));
}
#endif


#define DTCLOAD_COMMENT		(0)
#define DTCLOAD_ENTRY		(1)
#define DTCLOAD_LOAD		(2)
#define DTCLOAD_RUN			(3)
#define DTCLOAD_LUT_START	(4)
#define DTCLOAD_LUT			(5)

#define DTC_LOAD_BUFFER		ST7_USB_BUF_EP2UIDO

/* This gets set by the DTC loader */
static uint8_t dtc_entry_download;


/* The buffer is specially formatted to represent a valid image to load into the DTC. */
static
int
dtc_load_from_buffer(
	usb_dev_handle	*pHDev,
	const u8		*buffer,
	size_t			length
) {
	struct header_s {
		u8	type;
		u8	length;
	};

	int				usb_err;
	struct header_s	*header;
	u8				lut_start = 0xc0;

	dtc_entry_download = 0;

	/* Stop the DTC before loading anything. */
	usb_err = ep1_generic_commandl(
		pHDev, 1,
		EP1_CMD_DTC_STOP
	);
	if(usb_err < 0) return(usb_err);

	while(length) {
		if(length < sizeof(*header)) {
			LOG_ERROR("Malformed DTC image\n");
			exit(1);
		}
361

362
363
364
365
		header = (struct header_s *)buffer;
		buffer += sizeof(*header);
		length -= sizeof(*header);

366
		if(length < (size_t)header->length + 1) {
367
368
369
			LOG_ERROR("Malformed DTC image\n");
			exit(1);
		}
370

371
372
373
374
375
376
		switch(header->type) {
			case DTCLOAD_COMMENT:
				break;

			case DTCLOAD_ENTRY:
				/* store entry addresses somewhere */
377
				if(!strncmp("download", (char *)buffer + 1, 8)) {
378
379
380
381
382
					dtc_entry_download = buffer[0];
				}
				break;

			case DTCLOAD_LOAD:
383
				/* Send the DTC program to ST7 RAM. */
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
				usb_err = ep1_memory_write(
					pHDev,
					DTC_LOAD_BUFFER,
					header->length + 1, buffer
				);
				if(usb_err < 0) return(usb_err);

				/* Load it into the DTC. */
				usb_err = ep1_generic_commandl(
					pHDev, 3,
					EP1_CMD_DTC_LOAD,
						(DTC_LOAD_BUFFER >> 8),
						DTC_LOAD_BUFFER
				);
				if(usb_err < 0) return(usb_err);

				break;

			case DTCLOAD_RUN:
				usb_err = ep1_generic_commandl(
					pHDev, 3,
					EP1_CMD_DTC_CALL,
						buffer[0],
					EP1_CMD_DTC_WAIT
				);
				if(usb_err < 0) return(usb_err);

				break;

			case DTCLOAD_LUT_START:
				lut_start = buffer[0];
				break;
416

417
			case DTCLOAD_LUT:
418
				usb_err = ep1_memory_write(
419
420
421
422
423
424
425
426
427
428
429
430
					pHDev,
					ST7_USB_BUF_EP0OUT + lut_start,
					header->length + 1, buffer
				);
				if(usb_err < 0) return(usb_err);
				break;

			default:
				LOG_ERROR("Invalid DTC image record type: 0x%02x\n", header->type);
				exit(1);
				break;
		}
431

432
433
434
435
436
437
438
439
440
441
442
443
444
		buffer += (header->length + 1);
		length -= (header->length + 1);
	}

	return(0);
}


/*
 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
 */
static
int
445
dtc_start_download(void) {
446
447
448
449
450
451
	int	usb_err;
	u8	ep2txr;

	/* set up for download mode and make sure EP2 is set up to transmit */
	usb_err = ep1_generic_commandl(
		pHDev, 7,
452

453
454
455
456
457
458
459
460
461
462
463
464
465
		EP1_CMD_DTC_STOP,
		EP1_CMD_SET_UPLOAD,
		EP1_CMD_SET_DOWNLOAD,
		EP1_CMD_MEMORY_READ,	/* read EP2TXR for its data toggle */
			ST7_EP2TXR >> 8,
			ST7_EP2TXR,
			1
	);
	if(usb_err < 0) return(usb_err);

	/* read back ep2txr */
	usb_err = usb_bulk_read(
		pHDev, USB_EP1IN_ADDR,
466
		(char *)&ep2txr, 1,
467
468
469
470
471
472
		USB_TIMEOUT_MS
	);
	if(usb_err < 0) return(usb_err);

	usb_err = ep1_generic_commandl(
		pHDev, 13,
473

474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
		EP1_CMD_MEMORY_WRITE,	/* preinitialize poll byte */
			DTC_STATUS_POLL_BYTE >> 8,
			DTC_STATUS_POLL_BYTE,
			1,
			0x00,
		EP1_CMD_MEMORY_WRITE,	/* set EP2IN to return data */
			ST7_EP2TXR >> 8,
			ST7_EP2TXR,
			1,
			(ep2txr & ST7_EP2TXR_DTOG_TX) | ST7_EP2TXR_STAT_VALID,
		EP1_CMD_DTC_CALL,	/* start running the DTC */
			dtc_entry_download,
		EP1_CMD_DTC_GET_CACHED_STATUS
	);
	if(usb_err < 0) return(usb_err);

	/* wait for completion */
	usb_err = usb_bulk_read(
		pHDev, USB_EP1IN_ADDR,
493
		(char *)&ep2txr, 1,
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
		USB_TIMEOUT_MS
	);

	return(usb_err);
}


static
int
dtc_run_download(
	usb_dev_handle	*pHDev,
	u8	*command_buffer,
	int	command_buffer_size,
	u8	*reply_buffer,
	int	reply_buffer_size
) {
	u8	ep2_buffer[USB_EP2IN_SIZE];
	int	usb_err;
	int	i;

	LOG_DEBUG(": %d/%d\n", command_buffer_size, reply_buffer_size);

	usb_err = usb_bulk_write(
		pHDev,
		USB_EP2OUT_ADDR,
519
		(char *)command_buffer, USB_EP2BANK_SIZE,
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
		USB_TIMEOUT_MS
	);
	if(usb_err < 0) return(usb_err);


	/* Wait for DTC to finish running command buffer */
	for(i = 5;;) {
		usb_err = ep1_generic_commandl(
			pHDev, 4,

			EP1_CMD_MEMORY_READ,
				DTC_STATUS_POLL_BYTE >> 8,
				DTC_STATUS_POLL_BYTE,
				1
		);
		if(usb_err < 0) return(usb_err);

		usb_err = usb_bulk_read(
			pHDev,
			USB_EP1IN_ADDR,
540
			(char *)ep2_buffer, 1,
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
			USB_TIMEOUT_MS
		);
		if(usb_err < 0) return(usb_err);

		if(ep2_buffer[0] & 0x01) break;

		if(!--i) {
			LOG_ERROR("%s, %d: too many retries waiting for DTC status\n",
				__FILE__, __LINE__
			);
			return(-ETIMEDOUT);
		}
	}


	if(!reply_buffer) reply_buffer_size = 0;
	if(reply_buffer_size) {
		usb_err = usb_bulk_read(
			pHDev,
			USB_EP2IN_ADDR,
561
			(char *)ep2_buffer, sizeof(ep2_buffer),
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
			USB_TIMEOUT_MS
		);

		if(usb_err < (int)sizeof(ep2_buffer)) {
			LOG_ERROR("%s, %d: Read of endpoint 2 returned %d\n",
				__FILE__, __LINE__, usb_err
			);
			return(usb_err);
		}

		memcpy(reply_buffer, ep2_buffer, reply_buffer_size);

	}

	return(usb_err);
}


/*
 * The dtc reply queue is a singly linked list that describes what to do with the reply packet that comes from the DTC.  Only SCAN_IN and SCAN_IO generate these entries.
 */

typedef
struct dtc_reply_queue_entry_s {
	struct dtc_reply_queue_entry_s	*next;
	jtag_command_t	*cmd;	/* the command that resulted in this entry */

	struct {
		u8		*buffer;	/* the scan buffer */
		int		size;		/* size of the scan buffer in bits */
		int		offset;		/* how many bits were already done before this? */
		int		length;		/* how many bits are processed in this operation? */
		enum scan_type	type;		/* SCAN_IN/SCAN_OUT/SCAN_IO */
	} scan;
} dtc_reply_queue_entry_t;


/*
 * The dtc_queue consists of a buffer of pending commands and a reply queue.
 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
 */

static
struct {
	dtc_reply_queue_entry_t	*rq_head;
	dtc_reply_queue_entry_t	*rq_tail;
608
609
	u32			cmd_index;
	u32			reply_index;
610
611
612
613
614
615
616
617
618
619
	u8			cmd_buffer[USB_EP2BANK_SIZE];
} dtc_queue;


/*
 * The tap state queue is for accumulating TAP state changes wiithout needlessly flushing the dtc_queue.  When it fills or is run, it adds the accumulated bytes to the dtc_queue.
 */

static
struct {
620
	u32	length;
621
622
623
624
625
626
627
	u32	buffer;
} tap_state_queue;



static
int
628
dtc_queue_init(void) {
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
	dtc_queue.rq_head = NULL;
	dtc_queue.rq_tail = NULL;
	dtc_queue.cmd_index = 0;
	dtc_queue.reply_index = 0;
	return(0);
}


static
inline
dtc_reply_queue_entry_t *
dtc_queue_enqueue_reply(
	enum scan_type	type,
	u8				*buffer,
	int				size,
	int				offset,
	int				length,
	jtag_command_t	*cmd
) {
	dtc_reply_queue_entry_t	*rq_entry;

	rq_entry = malloc(sizeof(dtc_reply_queue_entry_t));
	if(rq_entry != NULL) {
		rq_entry->scan.type = type;
		rq_entry->scan.buffer = buffer;
		rq_entry->scan.size = size;
		rq_entry->scan.offset = offset;
		rq_entry->scan.length = length;
		rq_entry->cmd = cmd;
		rq_entry->next = NULL;

		if(dtc_queue.rq_head == NULL)
			dtc_queue.rq_head = rq_entry;
		else
			dtc_queue.rq_tail->next = rq_entry;

		dtc_queue.rq_tail = rq_entry;
	}

	return(rq_entry);
}


/*
 * Running the queue means that any pending command buffer is run and any reply data dealt with.  The command buffer is then cleared for subsequent processing.
 * The queue is automatically run by append when it is necessary to get space for the append.
*/

static
int
679
dtc_queue_run(void) {
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
	dtc_reply_queue_entry_t	*rq_p, *rq_next;
	int			retval;
	int			usb_err;
	int			bit_cnt;
	int			x;
	u8			*dtc_p, *tdo_p;
	u8			dtc_mask, tdo_mask;
	u8			reply_buffer[USB_EP2IN_SIZE];

	retval = ERROR_OK;

	if(dtc_queue.cmd_index < 1) return(retval);

	dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;

	/* run the cmd */
	if(dtc_queue.rq_head == NULL) {
		usb_err = dtc_run_download(pHDev,
			dtc_queue.cmd_buffer, dtc_queue.cmd_index,
			NULL, 0
700
		);
701
702
703
704
705
706
707
708
		if(usb_err < 0) {
			LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
			exit(1);
		}
	} else {
		usb_err = dtc_run_download(pHDev,
			dtc_queue.cmd_buffer, dtc_queue.cmd_index,
			reply_buffer, dtc_queue.reply_index
709
		);
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
		if(usb_err < 0) {
			LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
			exit(1);
		} else {
			/* process the reply, which empties the reply queue and frees its entries */
			dtc_p = reply_buffer;

			/* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons.   It was that or craft a function to do the reversal, and that wouldn't work with bit-stuffing (supplying extra bits to use mostly byte operations), or any other scheme which would throw the byte alignment off. */

			for(
				rq_p = dtc_queue.rq_head;
				rq_p != NULL;
				rq_p = rq_next
			) {
				tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
				tdo_mask = 1 << (rq_p->scan.offset % 8);


				bit_cnt = rq_p->scan.length;
				if(bit_cnt >= 8) {
					/* bytes */

					dtc_mask = 1 << (8 - 1);

					for(
						;
						bit_cnt;
						bit_cnt--
					) {
						if(*dtc_p & dtc_mask) {
							*tdo_p |= tdo_mask;
						} else {
							*tdo_p &=~ tdo_mask;
						}
744

745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
						dtc_mask >>= 1;
						if(dtc_mask == 0) {
							dtc_p++;
							dtc_mask = 1 << (8 - 1);
						}

						tdo_mask <<= 1;
						if(tdo_mask == 0) {
							tdo_p++;
							tdo_mask = 1;
						}
					}
				} else {
					/*  extra bits or last bit */

					x = *dtc_p++;
					if((
						rq_p->scan.type == SCAN_IN
					) && (
						rq_p->scan.offset != rq_p->scan.size - 1
					)) {
						/* extra bits were sent as a full byte with padding on the end */
						dtc_mask = 1 << (8 - 1);
					} else {
						dtc_mask = 1 << (bit_cnt - 1);
					}

					for(
						;
						bit_cnt;
						bit_cnt--
					) {
						if(x & dtc_mask) {
							*tdo_p |= tdo_mask;
						} else {
							*tdo_p &=~ tdo_mask;
						}

						dtc_mask >>= 1;

						tdo_mask <<= 1;
						if(tdo_mask == 0) {
							tdo_p++;
							tdo_mask = 1;
						}
790

791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
					}
				}

				if((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
					/* feed scan buffer back into openocd and free it */
					if(jtag_read_buffer(rq_p->scan.buffer, rq_p->cmd->cmd.scan) != ERROR_OK) {
						 retval = ERROR_JTAG_QUEUE_FAILED;
					}
					free(rq_p->scan.buffer);
				}

				rq_next = rq_p->next;
				free(rq_p);
			}
			dtc_queue.rq_head = NULL;
			dtc_queue.rq_tail = NULL;
		}

	}


	/* reset state for new appends */
	dtc_queue.cmd_index = 0;
	dtc_queue.reply_index = 0;

	return(retval);
}



static
int
823
tap_state_queue_init(void) {
824
825
826
827
828
829
830
831
	tap_state_queue.length = 0;
	tap_state_queue.buffer = 0;
	return(0);
}


static
int
832
tap_state_queue_run(void) {
833
834
835
836
837
838
839
840
841
842
	int	i;
	int	bits;
	u8	byte;
	int	retval;

	retval = 0;
	if(!tap_state_queue.length) return(retval);
	bits = 1;
	byte = 0;
	for(i = tap_state_queue.length; i--;) {
843

844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
		byte <<= 1;
		if(tap_state_queue.buffer & 1) {
			byte |= 1;
		}
		if((bits >= 8) || !i) {
			byte <<= (8 - bits);

			/* make sure there's room for stop, byte op, and one byte */
			if(dtc_queue.cmd_index >= (sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))) {
				dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
					DTC_CMD_STOP;
				dtc_queue_run();
			}

#ifdef USE_HARDWARE_SHIFTER_FOR_TMS
			if(bits == 8) {
				dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
					DTC_CMD_SHIFT_TMS_BYTES(1);
			} else {
#endif
				dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
					DTC_CMD_SHIFT_TMS_BITS(bits);
#ifdef USE_HARDWARE_SHIFTER_FOR_TMS
			}
#endif

			dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
				byte;

			byte = 0;
			bits = 1;
		} else {
			bits++;
		}

		tap_state_queue.buffer >>= 1;
	}
	retval = tap_state_queue_init();
	return(retval);
}


static
int
tap_state_queue_append(
	u8	tms
) {
	int	retval;

	if(tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
		retval = tap_state_queue_run();
		if(retval != 0) return(retval);
	}

	if(tms) {
		tap_state_queue.buffer |= (1 << tap_state_queue.length);
	}
	tap_state_queue.length++;

	return(0);
}


static
908
void rlink_end_state(tap_state_t state)
909
{
910
911
	if (tap_is_state_stable(state))
		tap_set_end_state(state);
912
913
914
915
916
917
918
919
920
921
922
923
	else
	{
		LOG_ERROR("BUG: %i is not a valid end state", state);
		exit(-1);
	}
}


static
void rlink_state_move(void) {

	int i=0, tms=0;
924
	u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
925
926
927
928
929
930
931

	for (i = 0; i < 7; i++)
	{
		tms = (tms_scan >> i) & 1;
		tap_state_queue_append(tms);
	}

932
	tap_set_state(tap_get_end_state());
933
934
935
936
937
938
939
940
941
942
943
944
}

static
void rlink_path_move(pathmove_command_t *cmd)
{
	int num_states = cmd->num_states;
	int state_count;
	int tms = 0;

	state_count = 0;
	while (num_states)
	{
945
		if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
946
947
948
		{
			tms = 0;
		}
949
		else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
950
951
952
953
954
		{
			tms = 1;
		}
		else
		{
955
			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
956
957
958
959
960
			exit(-1);
		}

		tap_state_queue_append(tms);

961
		tap_set_state(cmd->path[state_count]);
962
963
964
965
		state_count++;
		num_states--;
	}

966
	tap_set_end_state(tap_get_state());
967
968
969
970
971
972
973
974
}


static
void rlink_runtest(int num_cycles)
{
	int i;

975
	tap_state_t saved_end_state = tap_get_end_state();
976
977

	/* only do a state_move when we're not already in RTI */
978
	if (tap_get_state() != TAP_IDLE)
979
980
981
982
983
984
985
986
987
988
989
990
991
	{
		rlink_end_state(TAP_IDLE);
		rlink_state_move();
	}

	/* execute num_cycles */
	for (i = 0; i < num_cycles; i++)
	{
		tap_state_queue_append(0);
	}

	/* finish in end_state */
	rlink_end_state(saved_end_state);
992
	if (tap_get_state() != tap_get_end_state())
993
994
995
996
997
998
999
1000
		rlink_state_move();
}


/* (1) assert or (0) deassert reset lines */
static
void rlink_reset(int trst, int srst)
{