rlink.c 38.4 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
5
 *   Copyright (C) 2007,2008 Øyvind Harboe                                 *
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *   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

/* project specific includes */
31
#include <jtag/interface.h>
32
#include <jtag/commands.h>
33
#include "rlink.h"
34
35
36
#include "rlink_st7.h"
#include "rlink_ep1_cmd.h"
#include "rlink_dtc_cmd.h"
37
#include "usb_common.h"
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

/* 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
69
70
71
72
73
#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
74

ntfreak's avatar
ntfreak committed
75
/* These pins are connected together */
76
77
78
#define ST7_PE_ADAPTER_SENSE_IN		ST7_PE3
#define ST7_PE_ADAPTER_SENSE_OUT	ST7_PE4

ntfreak's avatar
ntfreak committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* 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

102
103
104
105
106
107
108
109
110
111
112
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(
Freddie Chopin's avatar
Freddie Chopin committed
113
	usb_dev_handle	*pHDev_param,
114
115
116
117
118
119
120
121
	size_t		length,
	...
) {
	uint8_t		usb_buffer[USB_EP1OUT_SIZE];
	uint8_t		*usb_buffer_p;
	va_list		ap;
	int		usb_ret;

zwelch's avatar
zwelch committed
122
	if (length > sizeof(usb_buffer)) {
123
124
125
126
127
128
		length = sizeof(usb_buffer);
	}

	usb_buffer_p = usb_buffer;

	va_start(ap, length);
zwelch's avatar
zwelch committed
129
	while (length > 0) {
130
131
132
		*usb_buffer_p++ = va_arg(ap, int);
		length--;
	}
133

134
135
136
137
	memset(
		usb_buffer_p,
		0,
		sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
138
);
139
140

	usb_ret = usb_bulk_write(
Freddie Chopin's avatar
Freddie Chopin committed
141
		pHDev_param,
142
		USB_EP1OUT_ADDR,
143
		(char *)usb_buffer, sizeof(usb_buffer),
144
		USB_TIMEOUT_MS
145
);
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

	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
171
);
172
173
174
175

	remain = length;
	count = 0;

zwelch's avatar
zwelch committed
176
	while (remain) {
zwelch's avatar
zwelch committed
177
		if (remain > sizeof(usb_buffer)) {
178
179
180
181
182
183
184
185
186
			length = sizeof(usb_buffer);
		} else {
			length = remain;
		}

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

187
			usb_ret = usb_bulk_write(
188
189
190
			pHDev, USB_EP1OUT_ADDR,
			usb_buffer, sizeof(usb_buffer),
			USB_TIMEOUT_MS
191
);
192

zwelch's avatar
zwelch committed
193
		if (usb_ret < sizeof(usb_buffer)) {
194
195
			break;
		}
196

197
198
199
200
		usb_ret = usb_bulk_read(
			pHDev, USB_EP1IN_ADDR,
			buffer, length,
			USB_TIMEOUT_MS
201
);
202

zwelch's avatar
zwelch committed
203
		if (usb_ret < length) {
204
205
			break;
		}
206

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
		addr += length;
		buffer += length;
		count += length;
		remain -= length;
	}

	return(count);
}
#endif



static
ssize_t
ep1_memory_write(
Freddie Chopin's avatar
Freddie Chopin committed
222
	usb_dev_handle	*pHDev_param,
223
224
225
226
227
228
229
230
231
232
233
234
235
236
	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;

zwelch's avatar
zwelch committed
237
	while (remain) {
zwelch's avatar
zwelch committed
238
		if (remain > (sizeof(usb_buffer) - 4)) {
239
240
241
242
243
244
245
246
247
248
249
250
			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
251
);
252
253
254
255
		memset(
			usb_buffer + 4 + length,
			0,
			sizeof(usb_buffer) - 4 - length
256
);
257

258
			usb_ret = usb_bulk_write(
Freddie Chopin's avatar
Freddie Chopin committed
259
			pHDev_param, USB_EP1OUT_ADDR,
260
			(char *)usb_buffer, sizeof(usb_buffer),
261
			USB_TIMEOUT_MS
262
);
263

zwelch's avatar
zwelch committed
264
		if ((size_t)usb_ret < sizeof(usb_buffer)) {
265
266
			break;
		}
267

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
		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;

zwelch's avatar
zwelch committed
292
	if (length > sizeof(buffer)) {
293
294
295
296
297
298
299
		length = sizeof(buffer);
	}

	remain = length;
	buffer_p = buffer;

	va_start(ap, length);
zwelch's avatar
zwelch committed
300
	while (remain > 0) {
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
		*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(
Freddie Chopin's avatar
Freddie Chopin committed
327
	usb_dev_handle	*pHDev_param,
zwelch's avatar
zwelch committed
328
	const uint8_t		*buffer,
329
330
331
	size_t			length
) {
	struct header_s {
zwelch's avatar
zwelch committed
332
333
		uint8_t	type;
		uint8_t	length;
334
335
336
337
	};

	int				usb_err;
	struct header_s	*header;
zwelch's avatar
zwelch committed
338
	uint8_t				lut_start = 0xc0;
339
340
341
342
343

	dtc_entry_download = 0;

	/* Stop the DTC before loading anything. */
	usb_err = ep1_generic_commandl(
Freddie Chopin's avatar
Freddie Chopin committed
344
		pHDev_param, 1,
345
		EP1_CMD_DTC_STOP
346
);
zwelch's avatar
zwelch committed
347
	if (usb_err < 0) return(usb_err);
348

zwelch's avatar
zwelch committed
349
	while (length) {
zwelch's avatar
zwelch committed
350
		if (length < sizeof(*header)) {
351
			LOG_ERROR("Malformed DTC image");
352
353
			exit(1);
		}
354

355
356
357
358
		header = (struct header_s *)buffer;
		buffer += sizeof(*header);
		length -= sizeof(*header);

zwelch's avatar
zwelch committed
359
		if (length < (size_t)header->length + 1) {
360
			LOG_ERROR("Malformed DTC image");
361
362
			exit(1);
		}
363

zwelch's avatar
zwelch committed
364
		switch (header->type) {
365
366
367
368
369
			case DTCLOAD_COMMENT:
				break;

			case DTCLOAD_ENTRY:
				/* store entry addresses somewhere */
zwelch's avatar
zwelch committed
370
				if (!strncmp("download", (char *)buffer + 1, 8)) {
371
372
373
374
375
					dtc_entry_download = buffer[0];
				}
				break;

			case DTCLOAD_LOAD:
376
				/* Send the DTC program to ST7 RAM. */
377
				usb_err = ep1_memory_write(
Freddie Chopin's avatar
Freddie Chopin committed
378
					pHDev_param,
379
380
					DTC_LOAD_BUFFER,
					header->length + 1, buffer
381
);
zwelch's avatar
zwelch committed
382
				if (usb_err < 0) return(usb_err);
383
384
385

				/* Load it into the DTC. */
				usb_err = ep1_generic_commandl(
Freddie Chopin's avatar
Freddie Chopin committed
386
					pHDev_param, 3,
387
388
389
					EP1_CMD_DTC_LOAD,
						(DTC_LOAD_BUFFER >> 8),
						DTC_LOAD_BUFFER
390
);
zwelch's avatar
zwelch committed
391
				if (usb_err < 0) return(usb_err);
392
393
394
395
396

				break;

			case DTCLOAD_RUN:
				usb_err = ep1_generic_commandl(
Freddie Chopin's avatar
Freddie Chopin committed
397
					pHDev_param, 3,
398
399
400
					EP1_CMD_DTC_CALL,
						buffer[0],
					EP1_CMD_DTC_WAIT
401
);
zwelch's avatar
zwelch committed
402
				if (usb_err < 0) return(usb_err);
403
404
405
406
407
408

				break;

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

410
			case DTCLOAD_LUT:
411
				usb_err = ep1_memory_write(
Freddie Chopin's avatar
Freddie Chopin committed
412
					pHDev_param,
413
414
					ST7_USB_BUF_EP0OUT + lut_start,
					header->length + 1, buffer
415
);
zwelch's avatar
zwelch committed
416
				if (usb_err < 0) return(usb_err);
417
418
419
				break;

			default:
420
				LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type);
421
422
423
				exit(1);
				break;
		}
424

425
426
427
428
429
430
431
432
433
434
435
436
437
		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
438
dtc_start_download(void) {
439
	int	usb_err;
zwelch's avatar
zwelch committed
440
	uint8_t	ep2txr;
441
442
443
444

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

446
447
448
449
450
451
452
		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
453
);
zwelch's avatar
zwelch committed
454
	if (usb_err < 0) return(usb_err);
455
456
457
458

	/* read back ep2txr */
	usb_err = usb_bulk_read(
		pHDev, USB_EP1IN_ADDR,
459
		(char *)&ep2txr, 1,
460
		USB_TIMEOUT_MS
461
);
zwelch's avatar
zwelch committed
462
	if (usb_err < 0) return(usb_err);
463
464
465

	usb_err = ep1_generic_commandl(
		pHDev, 13,
466

467
468
469
470
471
472
473
474
475
476
477
478
479
		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
480
);
zwelch's avatar
zwelch committed
481
	if (usb_err < 0) return(usb_err);
482
483
484
485

	/* wait for completion */
	usb_err = usb_bulk_read(
		pHDev, USB_EP1IN_ADDR,
486
		(char *)&ep2txr, 1,
487
		USB_TIMEOUT_MS
488
);
489
490
491
492
493
494
495
496

	return(usb_err);
}


static
int
dtc_run_download(
Freddie Chopin's avatar
Freddie Chopin committed
497
	usb_dev_handle	*pHDev_param,
zwelch's avatar
zwelch committed
498
	uint8_t	*command_buffer,
499
	int	command_buffer_size,
zwelch's avatar
zwelch committed
500
	uint8_t	*reply_buffer,
501
502
	int	reply_buffer_size
) {
zwelch's avatar
zwelch committed
503
	uint8_t	ep2_buffer[USB_EP2IN_SIZE];
504
505
506
	int	usb_err;
	int	i;

Eric Wetzel's avatar
Eric Wetzel committed
507
	LOG_DEBUG(": %d/%d", command_buffer_size, reply_buffer_size);
508
509

	usb_err = usb_bulk_write(
Freddie Chopin's avatar
Freddie Chopin committed
510
		pHDev_param,
511
		USB_EP2OUT_ADDR,
512
		(char *)command_buffer, USB_EP2BANK_SIZE,
513
		USB_TIMEOUT_MS
514
);
zwelch's avatar
zwelch committed
515
	if (usb_err < 0) return(usb_err);
516
517
518


	/* Wait for DTC to finish running command buffer */
zwelch's avatar
zwelch committed
519
	for (i = 10;;) {
520
		usb_err = ep1_generic_commandl(
Freddie Chopin's avatar
Freddie Chopin committed
521
			pHDev_param, 4,
522
523
524
525
526

			EP1_CMD_MEMORY_READ,
				DTC_STATUS_POLL_BYTE >> 8,
				DTC_STATUS_POLL_BYTE,
				1
527
);
zwelch's avatar
zwelch committed
528
		if (usb_err < 0) return(usb_err);
529
530

		usb_err = usb_bulk_read(
Freddie Chopin's avatar
Freddie Chopin committed
531
			pHDev_param,
532
			USB_EP1IN_ADDR,
533
			(char *)ep2_buffer, 1,
534
			USB_TIMEOUT_MS
535
);
zwelch's avatar
zwelch committed
536
		if (usb_err < 0) return(usb_err);
537

zwelch's avatar
zwelch committed
538
		if (ep2_buffer[0] & 0x01) break;
539

zwelch's avatar
zwelch committed
540
		if (!--i) {
541
			LOG_ERROR("%s, %d: too many retries waiting for DTC status",
542
				__FILE__, __LINE__
543
);
544
545
546
547
548
			return(-ETIMEDOUT);
		}
	}


zwelch's avatar
zwelch committed
549
550
	if (!reply_buffer) reply_buffer_size = 0;
	if (reply_buffer_size) {
551
		usb_err = usb_bulk_read(
Freddie Chopin's avatar
Freddie Chopin committed
552
			pHDev_param,
553
			USB_EP2IN_ADDR,
554
			(char *)ep2_buffer, sizeof(ep2_buffer),
555
			USB_TIMEOUT_MS
556
);
557

zwelch's avatar
zwelch committed
558
		if (usb_err < (int)sizeof(ep2_buffer)) {
559
			LOG_ERROR("%s, %d: Read of endpoint 2 returned %d",
560
				__FILE__, __LINE__, usb_err
561
);
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
			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.
 */

577
struct dtc_reply_queue_entry {
Zachary T Welch's avatar
Zachary T Welch committed
578
579
	struct dtc_reply_queue_entry	*next;
	struct jtag_command	*cmd;	/* the command that resulted in this entry */
580
581

	struct {
zwelch's avatar
zwelch committed
582
		uint8_t		*buffer;	/* the scan buffer */
583
584
585
586
587
		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;
588
};
589
590
591
592
593
594
595
596
597


/*
 * 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 {
598
599
	struct dtc_reply_queue_entry	*rq_head;
	struct dtc_reply_queue_entry	*rq_tail;
600
601
	uint32_t			cmd_index;
	uint32_t			reply_index;
zwelch's avatar
zwelch committed
602
	uint8_t			cmd_buffer[USB_EP2BANK_SIZE];
603
604
605
606
607
608
609
610
611
} 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 {
612
613
	uint32_t	length;
	uint32_t	buffer;
614
615
616
617
618
619
} tap_state_queue;



static
int
620
dtc_queue_init(void) {
621
622
623
624
625
626
627
628
629
630
	dtc_queue.rq_head = NULL;
	dtc_queue.rq_tail = NULL;
	dtc_queue.cmd_index = 0;
	dtc_queue.reply_index = 0;
	return(0);
}


static
inline
631
struct dtc_reply_queue_entry *
632
633
dtc_queue_enqueue_reply(
	enum scan_type	type,
zwelch's avatar
zwelch committed
634
	uint8_t				*buffer,
635
636
637
	int				size,
	int				offset,
	int				length,
Zachary T Welch's avatar
Zachary T Welch committed
638
	struct jtag_command	*cmd
639
) {
640
	struct dtc_reply_queue_entry	*rq_entry;
641

642
	rq_entry = malloc(sizeof(struct dtc_reply_queue_entry));
zwelch's avatar
zwelch committed
643
	if (rq_entry != NULL) {
644
645
646
647
648
649
650
651
		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;

zwelch's avatar
zwelch committed
652
		if (dtc_queue.rq_head == NULL)
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
			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
671
dtc_queue_run(void) {
672
	struct dtc_reply_queue_entry	*rq_p, *rq_next;
673
674
675
676
	int			retval;
	int			usb_err;
	int			bit_cnt;
	int			x;
zwelch's avatar
zwelch committed
677
678
679
	uint8_t			*dtc_p, *tdo_p;
	uint8_t			dtc_mask, tdo_mask;
	uint8_t			reply_buffer[USB_EP2IN_SIZE];
680
681
682

	retval = ERROR_OK;

zwelch's avatar
zwelch committed
683
	if (dtc_queue.cmd_index < 1) return(retval);
684
685
686
687

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

	/* run the cmd */
zwelch's avatar
zwelch committed
688
	if (dtc_queue.rq_head == NULL) {
689
690
691
		usb_err = dtc_run_download(pHDev,
			dtc_queue.cmd_buffer, dtc_queue.cmd_index,
			NULL, 0
692
);
zwelch's avatar
zwelch committed
693
		if (usb_err < 0) {
694
			LOG_ERROR("dtc_run_download: %s", usb_strerror());
695
696
697
698
699
700
			exit(1);
		}
	} else {
		usb_err = dtc_run_download(pHDev,
			dtc_queue.cmd_buffer, dtc_queue.cmd_index,
			reply_buffer, dtc_queue.reply_index
701
);
zwelch's avatar
zwelch committed
702
		if (usb_err < 0) {
703
			LOG_ERROR("dtc_run_download: %s", usb_strerror());
704
705
706
707
708
709
710
			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. */

zwelch's avatar
zwelch committed
711
			for (
712
713
714
				rq_p = dtc_queue.rq_head;
				rq_p != NULL;
				rq_p = rq_next
715
) {
716
717
718
719
720
				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;
zwelch's avatar
zwelch committed
721
				if (bit_cnt >= 8) {
722
723
724
725
					/* bytes */

					dtc_mask = 1 << (8 - 1);

zwelch's avatar
zwelch committed
726
					for (
727
728
729
						;
						bit_cnt;
						bit_cnt--
730
) {
zwelch's avatar
zwelch committed
731
						if (*dtc_p & dtc_mask) {
732
733
734
735
							*tdo_p |= tdo_mask;
						} else {
							*tdo_p &=~ tdo_mask;
						}
736

737
						dtc_mask >>= 1;
zwelch's avatar
zwelch committed
738
						if (dtc_mask == 0) {
739
740
741
742
743
							dtc_p++;
							dtc_mask = 1 << (8 - 1);
						}

						tdo_mask <<= 1;
zwelch's avatar
zwelch committed
744
						if (tdo_mask == 0) {
745
746
747
748
749
750
751
752
							tdo_p++;
							tdo_mask = 1;
						}
					}
				} else {
					/*  extra bits or last bit */

					x = *dtc_p++;
zwelch's avatar
zwelch committed
753
					if ((
754
						rq_p->scan.type == SCAN_IN
755
) && (
756
						rq_p->scan.offset != rq_p->scan.size - 1
757
)) {
758
759
760
761
762
763
						/* 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);
					}

zwelch's avatar
zwelch committed
764
					for (
765
766
767
						;
						bit_cnt;
						bit_cnt--
768
) {
zwelch's avatar
zwelch committed
769
						if (x & dtc_mask) {
770
771
772
773
774
775
776
777
							*tdo_p |= tdo_mask;
						} else {
							*tdo_p &=~ tdo_mask;
						}

						dtc_mask >>= 1;

						tdo_mask <<= 1;
zwelch's avatar
zwelch committed
778
						if (tdo_mask == 0) {
779
780
781
							tdo_p++;
							tdo_mask = 1;
						}
782

783
784
785
					}
				}

zwelch's avatar
zwelch committed
786
				if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
787
					/* feed scan buffer back into openocd and free it */
zwelch's avatar
zwelch committed
788
					if (jtag_read_buffer(rq_p->scan.buffer, rq_p->cmd->cmd.scan) != ERROR_OK) {
789
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
						 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
815
tap_state_queue_init(void) {
816
817
818
819
820
821
822
823
	tap_state_queue.length = 0;
	tap_state_queue.buffer = 0;
	return(0);
}


static
int
824
tap_state_queue_run(void) {
825
826
	int	i;
	int	bits;
827
	uint8_t	byte_param;
828
829
830
	int	retval;

	retval = 0;
zwelch's avatar
zwelch committed
831
	if (!tap_state_queue.length) return(retval);
832
	bits = 1;
833
	byte_param = 0;
zwelch's avatar
zwelch committed
834
	for (i = tap_state_queue.length; i--;) {
835

836
		byte_param <<= 1;
zwelch's avatar
zwelch committed
837
		if (tap_state_queue.buffer & 1) {
838
			byte_param |= 1;
839
		}
zwelch's avatar
zwelch committed
840
		if ((bits >= 8) || !i) {
841
			byte_param <<= (8 - bits);
842
843

			/* make sure there's room for stop, byte op, and one byte */
zwelch's avatar
zwelch committed
844
			if (dtc_queue.cmd_index >= (sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))) {
845
846
847
848
849
850
				dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
					DTC_CMD_STOP;
				dtc_queue_run();
			}

#ifdef USE_HARDWARE_SHIFTER_FOR_TMS
zwelch's avatar
zwelch committed
851
			if (bits == 8) {
852
853
854
855
856
857
858
859
860
861
862
				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++] =
863
				byte_param;
864

865
			byte_param = 0;
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
			bits = 1;
		} else {
			bits++;
		}

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


static
int
tap_state_queue_append(
zwelch's avatar
zwelch committed
881
	uint8_t	tms
882
883
884
) {
	int	retval;

zwelch's avatar
zwelch committed
885
	if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
886
		retval = tap_state_queue_run();
zwelch's avatar
zwelch committed
887
		if (retval != 0) return(retval);
888
889
	}

zwelch's avatar
zwelch committed
890
	if (tms) {
891
892
893
894
895
896
897
898
899
		tap_state_queue.buffer |= (1 << tap_state_queue.length);
	}
	tap_state_queue.length++;

	return(0);
}


static
900
void rlink_end_state(tap_state_t state)
901
{
902
903
	if (tap_is_state_stable(state))
		tap_set_end_state(state);
904
905
906
907
908
909
910
911
912
913
914
	else
	{
		LOG_ERROR("BUG: %i is not a valid end state", state);
		exit(-1);
	}
}


static
void rlink_state_move(void) {

zwelch's avatar
zwelch committed
915
	int i = 0, tms = 0;
zwelch's avatar
zwelch committed
916
	uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
917
	int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
918

919
	for (i = 0; i < tms_count; i++)
920
921
922
923
924
	{
		tms = (tms_scan >> i) & 1;
		tap_state_queue_append(tms);
	}

925
	tap_set_state(tap_get_end_state());
926
927
928
}

static
929
void rlink_path_move(struct pathmove_command *cmd)
930
931
932
933
934
935
936
937
{
	int num_states = cmd->num_states;
	int state_count;
	int tms = 0;

	state_count = 0;
	while (num_states)
	{
938
		if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
939
940
941
		{
			tms = 0;
		}
942
		else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
943
944
945
946
947
		{
			tms = 1;
		}
		else
		{
948
			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
949
950
951
952
953
			exit(-1);
		}

		tap_state_queue_append(tms);

954
		tap_set_state(cmd->path[state_count]);
955
956
957
958
		state_count++;
		num_states--;
	}

959
	tap_set_end_state(tap_get_state());
960
961
962
963
964
965
966
967
}


static
void rlink_runtest(int num_cycles)
{
	int i;

968
	tap_state_t saved_end_state = tap_get_end_state();
969
970

	/* only do a state_move when we're not already in RTI */
971
	if (tap_get_state() != TAP_IDLE)
972
973
974
975
976
977
978
979
980
981
982
983
984
	{
		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);
985
	if (tap_get_state() != tap_get_end_state())
986
987
988
989
990
991
992
993
		rlink_state_move();
}


/* (1) assert or (0) deassert reset lines */
static
void rlink_reset(int trst, int srst)
{
zwelch's avatar
zwelch committed
994
	uint8_t			bitmap;
995
996
	int			usb_err;

ntfreak's avatar
ntfreak committed
997
998
999
1000
	/* Read port A for bit op */
	usb_err = ep1_generic_commandl(
		pHDev, 4,
		EP1_CMD_MEMORY_READ,
For faster browsing, not all history is shown. View entire blame