aboutsummaryrefslogtreecommitdiff
path: root/src/jtag/drivers/osbdm.c
blob: 93abd6409e840010c367e1ade9387dd10519e1a7 (plain)
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
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
67
68
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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
265
266
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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/***************************************************************************
 *   Copyright (C) 2012 by Jan Dakinevich                                  *
 *   jan.dakinevich@gmail.com                                              *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#	include "config.h"
#endif

#include <helper/log.h>
#include <helper/binarybuffer.h>
#include <helper/command.h>
#include <jtag/interface.h>
#include "libusb_common.h"

struct sequence {
	int len;
	void *tms;
	void *tdo;
	const void *tdi;
	struct sequence *next;
};

struct queue {
	struct sequence *head;
	struct sequence *tail;
};

static struct sequence *queue_add_tail(struct queue *queue, int len)
{
	if (len <= 0) {
		LOG_ERROR("BUG: sequences with zero length are not allowed");
		return NULL;
	}

	struct sequence *next;
	next = (struct sequence *)malloc(sizeof(*next));
	if (next) {
		next->tms = calloc(1, DIV_ROUND_UP(len, 8));
		if (next->tms) {
			next->len = len;
			next->tdo = NULL;
			next->tdi = NULL;
			next->next = NULL;

			if (!queue->head) {
				/* Queue is empty at the moment */
				queue->head = next;
			} else {
				/* Queue already contains at least one sequence */
				queue->tail->next = next;
			}

			queue->tail = next;
		} else {
			free(next);
			next = NULL;
		}
	}

	if (!next)
		LOG_ERROR("Not enough memory");

	return next;
}

static void queue_drop_head(struct queue *queue)
{
	struct sequence *head = queue->head->next; /* New head */
	free(queue->head->tms);
	free(queue->head);
	queue->head = head;
}

static void queue_free(struct queue *queue)
{
	if (queue) {
		while (queue->head)
			queue_drop_head(queue);

		free(queue);
	}
}

static struct queue *queue_alloc(void)
{
	struct queue *queue = (struct queue *)malloc(sizeof(struct queue));
	if (queue)
		queue->head = NULL;
	else
		LOG_ERROR("Not enough memory");

	return queue;
}

/* Size of usb communnication buffer */
#define OSBDM_USB_BUFSIZE 64
/* Timeout for USB transfer, ms */
#define OSBDM_USB_TIMEOUT 1000
/* Write end point */
#define OSBDM_USB_EP_WRITE 0x01
/* Read end point */
#define OSBDM_USB_EP_READ 0x82

/* Initialize OSBDM device */
#define OSBDM_CMD_INIT 0x11
/* Execute special, not-BDM command. But only this
 * command is used for JTAG operation */
#define OSBDM_CMD_SPECIAL 0x27
/* Execute JTAG swap (tms/tdi -> tdo) */
#define OSBDM_CMD_SPECIAL_SWAP 0x05
/* Reset control */
#define OSBDM_CMD_SPECIAL_SRST 0x01
/* Maximum bit-length in one swap */
#define OSBDM_SWAP_MAX (((OSBDM_USB_BUFSIZE - 6) / 5) * 16)

/* Lists of valid VID/PID pairs
 */
static const uint16_t osbdm_vid[] = { 0x15a2, 0x15a2, 0x15a2, 0 };
static const uint16_t osbdm_pid[] = { 0x0042, 0x0058, 0x005e, 0 };

struct osbdm {
	struct jtag_libusb_device_handle *devh; /* USB handle */
	uint8_t buffer[OSBDM_USB_BUFSIZE]; /* Data to send and receive */
	int count; /* Count data to send and to read */
};

/* osbdm instance
 */
static struct osbdm osbdm_context;

static int osbdm_send_and_recv(struct osbdm *osbdm)
{
	/* Send request */
	int count = jtag_libusb_bulk_write(osbdm->devh, OSBDM_USB_EP_WRITE,
		(char *)osbdm->buffer, osbdm->count, OSBDM_USB_TIMEOUT);

	if (count != osbdm->count) {
		LOG_ERROR("OSBDM communnication error: can't write");
		return ERROR_FAIL;
	}

	/* Save command code for next checking */
	uint8_t cmd_saved = osbdm->buffer[0];

	/* Reading answer */
	osbdm->count = jtag_libusb_bulk_read(osbdm->devh, OSBDM_USB_EP_READ,
		(char *)osbdm->buffer, OSBDM_USB_BUFSIZE, OSBDM_USB_TIMEOUT);

	/* Now perform basic checks for data sent by BDM device
	 */

	if (osbdm->count < 0) {
		LOG_ERROR("OSBDM communnication error: can't read");
		return ERROR_FAIL;
	}

	if (osbdm->count < 2) {
		LOG_ERROR("OSBDM communnication error: answer too small");
		return ERROR_FAIL;
	}

	if (osbdm->count != osbdm->buffer[1])  {
		LOG_ERROR("OSBDM communnication error: answer size mismatch");
		return ERROR_FAIL;
	}

	if (cmd_saved != osbdm->buffer[0]) {
		LOG_ERROR("OSBDM communnication error: answer command mismatch");
		return ERROR_FAIL;
	}

	return ERROR_OK;
}

static int osbdm_srst(struct osbdm *osbdm, int srst)
{
	osbdm->count = 0;
	(void)memset(osbdm->buffer, 0, OSBDM_USB_BUFSIZE);

	/* Composing request
	 */
	osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL; /* Command */
	osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL_SRST; /* Subcommand */
	/* Length in bytes - not used */
	osbdm->buffer[osbdm->count++] = 0;
	osbdm->buffer[osbdm->count++] = 0;
	/* SRST state */
	osbdm->buffer[osbdm->count++] = (srst ? 0 : 0x08);

	/* Sending data
	 */
	if (osbdm_send_and_recv(osbdm) != ERROR_OK)
		return ERROR_FAIL;

	return ERROR_OK;
}

static int osbdm_swap(struct osbdm *osbdm, void *tms, void *tdi,
	void *tdo, int length)
{
	if (length > OSBDM_SWAP_MAX) {
		LOG_ERROR("BUG: bit sequence too long");
		return ERROR_FAIL;
	}

	if (length <= 0) {
		LOG_ERROR("BUG: bit sequence equal or less to 0");
		return ERROR_FAIL;
	}

	int swap_count = DIV_ROUND_UP(length, 16);

	/* cleanup */
	osbdm->count = 0;
	(void)memset(osbdm->buffer, 0, OSBDM_USB_BUFSIZE);

	/* Composing request
	 */

	osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL; /* Command */
	osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL_SWAP; /* Subcommand */
	/* Length in bytes - not used */
	osbdm->buffer[osbdm->count++] = 0;
	osbdm->buffer[osbdm->count++] = 0;
	/* Swap count */
	osbdm->buffer[osbdm->count++] = 0;
	osbdm->buffer[osbdm->count++] = (uint8_t)swap_count;

	for (int bit_idx = 0; bit_idx < length; ) {
		/* Bit count in swap */
		int bit_count = length - bit_idx;
		if (bit_count > 16)
			bit_count = 16;

		osbdm->buffer[osbdm->count++] = (uint8_t)bit_count;

		/* Copying TMS and TDI data to output buffer */
		uint32_t tms_data = buf_get_u32(tms, bit_idx, bit_count);
		uint32_t tdi_data = buf_get_u32(tdi, bit_idx, bit_count);
		osbdm->buffer[osbdm->count++] = (uint8_t)(tdi_data >> 8);
		osbdm->buffer[osbdm->count++] = (uint8_t)tdi_data;
		osbdm->buffer[osbdm->count++] = (uint8_t)(tms_data >> 8);
		osbdm->buffer[osbdm->count++] = (uint8_t)tms_data;

		/* Next bit offset */
		bit_idx += bit_count;
	}

	assert(osbdm->count <= OSBDM_USB_BUFSIZE);

	/* Sending data
	 */
	if (osbdm_send_and_recv(osbdm) != ERROR_OK)
		return ERROR_FAIL;

	/*	Extra check
	 */
	if (((osbdm->buffer[2] << 8) | osbdm->buffer[3]) != 2 * swap_count) {
		LOG_ERROR("OSBDM communnication error: not proper answer to swap command");
		return ERROR_FAIL;
	}

	/* Copy TDO responce
	 */
	uint8_t *buffer = (uint8_t *)osbdm->buffer + 4;
	for (int bit_idx = 0; bit_idx < length; ) {
		int bit_count = length - bit_idx;
		if (bit_count > 16)
			bit_count = 16;

		/* Prepare data */
		uint32_t tdo_data = 0;
		tdo_data |= (*buffer++) << 8;
		tdo_data |= (*buffer++);
		tdo_data >>= (16 - bit_count);

		/* Copy TDO to return */
		buf_set_u32(tdo, bit_idx, bit_count, tdo_data);

		bit_idx += bit_count;
	}

	return ERROR_OK;
}

static int osbdm_flush(struct osbdm *osbdm, struct queue* queue)
{
	uint8_t tms[DIV_ROUND_UP(OSBDM_SWAP_MAX, 8)];
	uint8_t tdi[DIV_ROUND_UP(OSBDM_SWAP_MAX, 8)];
	uint8_t tdo[DIV_ROUND_UP(OSBDM_SWAP_MAX, 8)];

	int seq_back_len = 0;

	while (queue->head) {
		(void)memset(tms, 0, sizeof(tms));
		(void)memset(tdi, 0, sizeof(tdi));
		(void)memset(tdo, 0, sizeof(tdo));

		int seq_len;
		int swap_len;
		struct sequence *seq;

		/* Copy from queue to tms/tdi streams
		 */
		seq = queue->head;
		seq_len = seq_back_len;
		swap_len = 0;

		while (seq && swap_len != OSBDM_SWAP_MAX) {
			/* Count bit for copy at this iteration.
			 * len should fit into remaining space
			 * in tms/tdo bitstreams
			 */
			int len = seq->len - seq_len;
			if (len > OSBDM_SWAP_MAX - swap_len)
				len = OSBDM_SWAP_MAX - swap_len;

			/* Set tms data */
			buf_set_buf(seq->tms, seq_len, tms, swap_len, len);

			/* Set tdi data if they exists */
			if (seq->tdi)
				buf_set_buf(seq->tdi, seq_len, tdi, swap_len, len);

			swap_len += len;
			seq_len += len;
			if (seq_len == seq->len) {
				seq = seq->next; /* Move to next sequence */
				seq_len = 0;
			}
		}

		if (osbdm_swap(osbdm, tms, tdi, tdo, swap_len))
			return ERROR_FAIL;

		/* Copy from tdo stream to queue
		 */

		for (int swap_back_len = 0; swap_back_len < swap_len; ) {
			int len = queue->head->len - seq_back_len;
			if (len > swap_len - swap_back_len)
				len = swap_len - swap_back_len;

			if (queue->head->tdo)
				buf_set_buf(tdo, swap_back_len,	queue->head->tdo, seq_back_len, len);

			swap_back_len += len;
			seq_back_len += len;
			if (seq_back_len == queue->head->len) {
				queue_drop_head(queue);
				seq_back_len = 0;
			}
		}
	}

	return ERROR_OK;
}

/*	Basic operation for opening USB device */
static int osbdm_open(struct osbdm *osbdm)
{
	(void)memset(osbdm, 0, sizeof(*osbdm));
	if (jtag_libusb_open(osbdm_vid, osbdm_pid, &osbdm->devh) != ERROR_OK)
		return ERROR_FAIL;

	if (jtag_libusb_claim_interface(osbdm->devh, 0) != ERROR_OK)
		return ERROR_FAIL;

	return ERROR_OK;
}

static int osbdm_quit(void)
{
	jtag_libusb_close(osbdm_context.devh);
	return ERROR_OK;
}

static int osbdm_add_pathmove(
	struct queue *queue,
	tap_state_t *path,
	int num_states)
{
	assert(num_states <= 32);

	struct sequence *next = queue_add_tail(queue, num_states);
	if (!next) {
		LOG_ERROR("BUG: can't allocate bit sequence");
		return ERROR_FAIL;
	}

	uint32_t tms = 0;
	for (int i = 0; i < num_states; i++) {
		if (tap_state_transition(tap_get_state(), 1) == path[i]) {
			tms |= (1 << i);
		} else if (tap_state_transition(tap_get_state(), 0) == path[i]) {
			tms &= ~(1 << i); /* This line not so needed */
		} else {
			LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition",
				tap_state_name(tap_get_state()),
				tap_state_name(path[i]));
			return ERROR_FAIL;
		}

		tap_set_state(path[i]);
	}

	buf_set_u32(next->tms, 0, num_states, tms);
	tap_set_end_state(tap_get_state());

	return ERROR_OK;
}

static int osbdm_add_statemove(
	struct queue *queue,
	tap_state_t new_state,
	int skip_first)
{
	int len = 0;
	int tms;

	tap_set_end_state(new_state);
	if (tap_get_end_state() == TAP_RESET) {
		/* Ignore current state */
		tms = 0xff;
		len = 5;
	} else if (tap_get_state() != tap_get_end_state()) {
		tms = tap_get_tms_path(tap_get_state(), new_state);
		len = tap_get_tms_path_len(tap_get_state(), new_state);
	}

	if (len && skip_first) {
		len--;
		tms >>= 1;
	}

	if (len) {
		struct sequence *next = queue_add_tail(queue, len);
		if (!next) {
			LOG_ERROR("BUG: can't allocate bit sequence");
			return ERROR_FAIL;
		}
		buf_set_u32(next->tms, 0, len, tms);
	}

	tap_set_state(tap_get_end_state());
	return ERROR_OK;
}

static int osbdm_add_stableclocks(
	struct queue *queue,
	int count)
{
	if (!tap_is_state_stable(tap_get_state())) {
		LOG_ERROR("BUG: current state (%s) is not stable",
			tap_state_name(tap_get_state()));
		return ERROR_FAIL;
	}

	struct sequence *next = queue_add_tail(queue, count);
	if (!next) {
		LOG_ERROR("BUG: can't allocate bit sequence");
		return ERROR_FAIL;
	}

	if (tap_get_state() == TAP_RESET)
		(void)memset(next->tms, 0xff, DIV_ROUND_UP(count, 8));

	return ERROR_OK;
}

static int osbdm_add_tms(
	struct queue *queue,
	const uint8_t *tms,
	int num_bits)
{
	struct sequence *next = queue_add_tail(queue, num_bits);
	if (!next) {
		LOG_ERROR("BUG: can't allocate bit sequence");
		return ERROR_FAIL;
	}
	buf_set_buf(tms, 0, next->tms, 0, num_bits);

	return ERROR_OK;
}

static int osbdm_add_scan(
	struct queue *queue,
	struct scan_field *fields,
	int num_fields,
	tap_state_t end_state,
	bool ir_scan)
{
	/* Move to desired shift state */
	if (ir_scan) {
		if (tap_get_state() != TAP_IRSHIFT) {
			if (osbdm_add_statemove(queue, TAP_IRSHIFT, 0) != ERROR_OK)
				return ERROR_FAIL;
		}
	} else {
		if (tap_get_state() != TAP_DRSHIFT) {
			if (osbdm_add_statemove(queue, TAP_DRSHIFT, 0) != ERROR_OK)
				return ERROR_FAIL;
		}
	}

	/* Add scan */
	tap_set_end_state(end_state);
	for (int idx = 0; idx < num_fields; idx++) {
		struct sequence *next = queue_add_tail(queue, fields[idx].num_bits);
		if (!next) {
			LOG_ERROR("Can't allocate bit sequence");
			return ERROR_FAIL;
		}

		(void)memset(next->tms, 0, DIV_ROUND_UP(fields[idx].num_bits, 8));
		next->tdi = fields[idx].out_value;
		next->tdo = fields[idx].in_value;
	}

	/* Move to end state
	 */
	if (tap_get_state() != tap_get_end_state()) {
		/* Exit from IRSHIFT/DRSHIFT */
		buf_set_u32(queue->tail->tms, queue->tail->len - 1, 1, 1);

		/* Move with skip_first flag */
		if (osbdm_add_statemove(queue, tap_get_end_state(), 1) != ERROR_OK)
			return ERROR_FAIL;
	}

	return ERROR_OK;
}

static int osbdm_add_runtest(
	struct queue *queue,
	int num_cycles,
	tap_state_t end_state)
{
	if (osbdm_add_statemove(queue, TAP_IDLE, 0) != ERROR_OK)
		return ERROR_FAIL;

	if (osbdm_add_stableclocks(queue, num_cycles) != ERROR_OK)
		return ERROR_FAIL;

	if (osbdm_add_statemove(queue, end_state, 0) != ERROR_OK)
		return ERROR_FAIL;

	return ERROR_OK;
}

static int osbdm_execute_command(
	struct osbdm *osbdm,
	struct queue *queue,
	struct jtag_command *cmd)
{
	int retval = ERROR_OK;

	switch (cmd->type) {
	case JTAG_RESET:
		if (cmd->cmd.reset->trst) {
			LOG_ERROR("BUG: nTRST signal is not supported");
			retval = ERROR_FAIL;
		} else {
			retval = osbdm_flush(osbdm, queue);
			if (retval == ERROR_OK)
				retval = osbdm_srst(osbdm, cmd->cmd.reset->srst);
		}
		break;

	case JTAG_PATHMOVE:
		retval = osbdm_add_pathmove(
			queue,
			cmd->cmd.pathmove->path,
			cmd->cmd.pathmove->num_states);
		break;

	case JTAG_TLR_RESET:
		retval = osbdm_add_statemove(
			queue,
			cmd->cmd.statemove->end_state,
			0);
		break;

	case JTAG_STABLECLOCKS:
		retval = osbdm_add_stableclocks(
			queue,
			cmd->cmd.stableclocks->num_cycles);
		break;

	case JTAG_TMS:
		retval = osbdm_add_tms(
			queue,
			cmd->cmd.tms->bits,
			cmd->cmd.tms->num_bits);
		break;

	case JTAG_SCAN:
		retval = osbdm_add_scan(
			queue,
			cmd->cmd.scan->fields,
			cmd->cmd.scan->num_fields,
			cmd->cmd.scan->end_state,
			cmd->cmd.scan->ir_scan);
		break;

	case JTAG_SLEEP:
		retval = osbdm_flush(osbdm, queue);
		if (retval == ERROR_OK)
			jtag_sleep(cmd->cmd.sleep->us);
		break;

	case JTAG_RUNTEST:
		retval = osbdm_add_runtest(
			queue,
			cmd->cmd.runtest->num_cycles,
			cmd->cmd.runtest->end_state);
		break;

	default:
		LOG_ERROR("BUG: unknown JTAG command type encountered");
		retval = ERROR_FAIL;
		break;
	}

	return retval;
}

static int osbdm_execute_queue(void)
{
	int retval = ERROR_OK;

	struct queue *queue = queue_alloc();
	if (!queue) {
		LOG_ERROR("BUG: can't allocate bit queue");
		retval = ERROR_FAIL;
	} else {
		struct jtag_command *cmd = jtag_command_queue;

		while (retval == ERROR_OK && cmd) {
			retval = osbdm_execute_command(&osbdm_context, queue, cmd);
			cmd = cmd->next;
		}

		if (retval == ERROR_OK)
			retval = osbdm_flush(&osbdm_context, queue);

		queue_free(queue);
	}

	if (retval != ERROR_OK) {
		LOG_ERROR("FATAL: can't execute jtag command");
		exit(-1);
	}

	return retval;
}

static int osbdm_init(void)
{
	/* Open device */
	if (osbdm_open(&osbdm_context) != ERROR_OK) {
		LOG_ERROR("Can't open OSBDM device");
		return ERROR_FAIL;
	} else {
		/* Device successfully opened */
		LOG_INFO("OSBDM has opened");
	}

	/* Perform initialize command */
	osbdm_context.count = 0;
	osbdm_context.buffer[osbdm_context.count++] = OSBDM_CMD_INIT;
	if (osbdm_send_and_recv(&osbdm_context) != ERROR_OK)
		return ERROR_FAIL;

	return ERROR_OK;
}

struct jtag_interface osbdm_interface = {
	.name = "osbdm",

	.transports = jtag_only,
	.execute_queue = osbdm_execute_queue,

	.init = osbdm_init,
	.quit = osbdm_quit
};