1 /*******************************************************************************
2  *   Driver for OpenJTAG Project (www.openjtag.org)                            *
3  *   Compatible with libftdi and ftd2xx drivers.                               *
4  *                                                                             *
5  *   Cypress CY7C65215 support                                                 *
6  *   Copyright (C) 2015 Vianney le Clément de Saint-Marcq, Essensium NV        *
7  *                      <vianney.leclement@essensium.com>                      *
8  *                                                                             *
9  *   Copyright (C) 2010 by Ivan Meleca <mileca@gmail.com>                      *
10  *                                                                             *
11  *   Copyright (C) 2013 by Ryan Corbin, GlueLogix Inc. <corbin.ryan@gmail.com> *
12  *   Updated to work with OpenOCD v0.7.0. Fixed libftdi read speed issue.      *
13  *                                                                             *
14  *   Based on usb_blaster.c                                                    *
15  *   Copyright (C) 2009 Catalin Patulea                                        *
16  *   Copyright (C) 2006 Kolja Waschk                                           *
17  *                                                                             *
18  *   And jlink.c                                                               *
19  *   Copyright (C) 2008 by Spencer Oliver                                      *
20  *   spen@spen-soft.co.uk                                                      *
21  *                                                                             *
22  *   This program is free software; you can redistribute it and/or modify      *
23  *   it under the terms of the GNU General Public License as published by      *
24  *   the Free Software Foundation; either version 2 of the License, or         *
25  *   (at your option) any later version.                                       *
26  *                                                                             *
27  *   This program is distributed in the hope that it will be useful,           *
28  *   but WITHOUT ANY WARRANTY; without even the implied warranty of            *
29  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
30  *   GNU General Public License for more details.                              *
31  *                                                                             *
32  *   You should have received a copy of the GNU General Public License         *
33  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
34  ***************************************************************************/
35 
36 /***************************************************************************
37  * Version 1.0  Tested on a MCBSTM32 board using a Cortex-M3 (stm32f103x), *
38  *              GDB and Eclipse under Linux (Ubuntu 10.04)                 *
39  *                                                                         *
40  ***************************************************************************/
41 
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45 
46 #include <jtag/interface.h>
47 #include <jtag/commands.h>
48 #include "libusb_helper.h"
49 
50 static enum {
51 	OPENJTAG_VARIANT_STANDARD,
52 	OPENJTAG_VARIANT_CY7C65215,
53 } openjtag_variant = OPENJTAG_VARIANT_STANDARD;
54 
55 static const char * const openjtag_variant_names[] = {
56 	"standard",
57 	"cy7c65215",
58 	NULL
59 };
60 
61 /*
62  * OpenJTAG-OpenOCD state conversion
63  */
64 typedef enum openjtag_tap_state {
65 	OPENJTAG_TAP_INVALID    = -1,
66 	OPENJTAG_TAP_RESET  = 0,
67 	OPENJTAG_TAP_IDLE   = 1,
68 	OPENJTAG_TAP_SELECT_DR  = 2,
69 	OPENJTAG_TAP_CAPTURE_DR = 3,
70 	OPENJTAG_TAP_SHIFT_DR   = 4,
71 	OPENJTAG_TAP_EXIT1_DR   = 5,
72 	OPENJTAG_TAP_PAUSE_DR   = 6,
73 	OPENJTAG_TAP_EXIT2_DR   = 7,
74 	OPENJTAG_TAP_UPDATE_DR  = 8,
75 	OPENJTAG_TAP_SELECT_IR  = 9,
76 	OPENJTAG_TAP_CAPURE_IR  = 10,
77 	OPENJTAG_TAP_SHIFT_IR   = 11,
78 	OPENJTAG_TAP_EXIT1_IR   = 12,
79 	OPENJTAG_TAP_PAUSE_IR   = 13,
80 	OPENJTAG_TAP_EXIT2_IR   = 14,
81 	OPENJTAG_TAP_UPDATE_IR  = 15,
82 } openjtag_tap_state_t;
83 
84 /* OPENJTAG access library includes */
85 #include "libftdi_helper.h"
86 
87 /* OpenJTAG vid/pid */
88 static uint16_t openjtag_vid = 0x0403;
89 static uint16_t openjtag_pid = 0x6001;
90 
91 static char *openjtag_device_desc;
92 
93 static struct ftdi_context ftdic;
94 
95 #define OPENJTAG_BUFFER_SIZE        504
96 #define OPENJTAG_MAX_PENDING_RESULTS    256
97 
98 struct openjtag_scan_result {
99 	uint32_t bits;          /* Length in bits*/
100 	struct scan_command *command;   /* Corresponding scan command */
101 	uint8_t *buffer;
102 };
103 
104 /* USB RX/TX buffers */
105 static int usb_tx_buf_offs;
106 static uint8_t usb_tx_buf[OPENJTAG_BUFFER_SIZE];
107 static uint32_t usb_rx_buf_len;
108 static uint8_t usb_rx_buf[OPENJTAG_BUFFER_SIZE];
109 
110 /* Pending readings */
111 static struct openjtag_scan_result openjtag_scan_result_buffer[OPENJTAG_MAX_PENDING_RESULTS];
112 static int openjtag_scan_result_count;
113 
114 static struct libusb_device_handle *usbh;
115 
116 /* CY7C65215 model only */
117 #define CY7C65215_JTAG_REQUEST  0x40  /* bmRequestType: vendor host-to-device */
118 #define CY7C65215_JTAG_ENABLE   0xD0  /* bRequest: enable JTAG */
119 #define CY7C65215_JTAG_DISABLE  0xD1  /* bRequest: disable JTAG */
120 #define CY7C65215_JTAG_READ     0xD2  /* bRequest: read buffer */
121 #define CY7C65215_JTAG_WRITE    0xD3  /* bRequest: write buffer */
122 
123 #define CY7C65215_USB_TIMEOUT   100
124 
125 static const uint16_t cy7c65215_vids[] = {0x04b4, 0};
126 static const uint16_t cy7c65215_pids[] = {0x0007, 0};
127 
128 #define CY7C65215_JTAG_CLASS     0xff
129 #define CY7C65215_JTAG_SUBCLASS  0x04
130 
131 static unsigned int ep_in, ep_out;
132 
133 #ifdef _DEBUG_USB_COMMS_
134 
135 #define DEBUG_TYPE_READ     0
136 #define DEBUG_TYPE_WRITE    1
137 #define DEBUG_TYPE_OCD_READ 2
138 #define DEBUG_TYPE_BUFFER   3
139 
140 #define LINE_LEN  16
openjtag_debug_buffer(uint8_t * buffer,int length,uint8_t type)141 static void openjtag_debug_buffer(uint8_t *buffer, int length, uint8_t type)
142 {
143 	char line[128];
144 	char s[4];
145 	int i;
146 	int j;
147 
148 	switch (type) {
149 		case DEBUG_TYPE_READ:
150 			sprintf(line, "USB READ %d bytes", length);
151 			break;
152 		case DEBUG_TYPE_WRITE:
153 			sprintf(line, "USB WRITE %d bytes", length);
154 			break;
155 		case DEBUG_TYPE_OCD_READ:
156 			sprintf(line, "TO OpenOCD %d bytes", length);
157 			break;
158 		case DEBUG_TYPE_BUFFER:
159 			sprintf(line, "Buffer %d bytes", length);
160 			break;
161 	}
162 
163 	LOG_DEBUG("%s", line);
164 
165 	for (i = 0; i < length; i += LINE_LEN) {
166 		switch (type) {
167 			case DEBUG_TYPE_READ:
168 				sprintf(line, "USB READ: %04x", i);
169 				break;
170 			case DEBUG_TYPE_WRITE:
171 				sprintf(line, "USB WRITE: %04x", i);
172 				break;
173 			case DEBUG_TYPE_OCD_READ:
174 				sprintf(line, "TO OpenOCD: %04x", i);
175 				break;
176 			case DEBUG_TYPE_BUFFER:
177 				sprintf(line, "BUFFER: %04x", i);
178 				break;
179 		}
180 
181 		for (j = i; j < i + LINE_LEN && j < length; j++) {
182 			sprintf(s, " %02x", buffer[j]);
183 			strcat(line, s);
184 		}
185 		LOG_DEBUG("%s", line);
186 	}
187 
188 }
189 
190 #endif
191 
openjtag_get_tap_state(int8_t state)192 static int8_t openjtag_get_tap_state(int8_t state)
193 {
194 
195 	switch (state) {
196 		case TAP_DREXIT2:   return OPENJTAG_TAP_EXIT2_DR;
197 		case TAP_DREXIT1:   return OPENJTAG_TAP_EXIT1_DR;
198 		case TAP_DRSHIFT:   return OPENJTAG_TAP_SHIFT_DR;
199 		case TAP_DRPAUSE:   return OPENJTAG_TAP_PAUSE_DR;
200 		case TAP_IRSELECT:  return OPENJTAG_TAP_SELECT_IR;
201 		case TAP_DRUPDATE:  return OPENJTAG_TAP_UPDATE_DR;
202 		case TAP_DRCAPTURE: return OPENJTAG_TAP_CAPTURE_DR;
203 		case TAP_DRSELECT:  return OPENJTAG_TAP_SELECT_DR;
204 		case TAP_IREXIT2:   return OPENJTAG_TAP_EXIT2_IR;
205 		case TAP_IREXIT1:   return OPENJTAG_TAP_EXIT1_IR;
206 		case TAP_IRSHIFT:   return OPENJTAG_TAP_SHIFT_IR;
207 		case TAP_IRPAUSE:   return OPENJTAG_TAP_PAUSE_IR;
208 		case TAP_IDLE:      return OPENJTAG_TAP_IDLE;
209 		case TAP_IRUPDATE:  return OPENJTAG_TAP_UPDATE_IR;
210 		case TAP_IRCAPTURE: return OPENJTAG_TAP_CAPURE_IR;
211 		case TAP_RESET:     return OPENJTAG_TAP_RESET;
212 		case TAP_INVALID:
213 		default:            return OPENJTAG_TAP_INVALID;
214 	}
215 }
216 
openjtag_buf_write_standard(uint8_t * buf,int size,uint32_t * bytes_written)217 static int openjtag_buf_write_standard(
218 	uint8_t *buf, int size, uint32_t *bytes_written)
219 {
220 	int retval;
221 #ifdef _DEBUG_USB_COMMS_
222 	openjtag_debug_buffer(buf, size, DEBUG_TYPE_WRITE);
223 #endif
224 
225 	retval = ftdi_write_data(&ftdic, buf, size);
226 	if (retval < 0) {
227 		*bytes_written = 0;
228 		LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
229 		return ERROR_JTAG_DEVICE_ERROR;
230 	}
231 
232 	*bytes_written = retval;
233 
234 	return ERROR_OK;
235 }
236 
openjtag_buf_write_cy7c65215(uint8_t * buf,int size,uint32_t * bytes_written)237 static int openjtag_buf_write_cy7c65215(
238 	uint8_t *buf, int size, uint32_t *bytes_written)
239 {
240 	int ret;
241 
242 #ifdef _DEBUG_USB_COMMS_
243 	openjtag_debug_buffer(buf, size, DEBUG_TYPE_WRITE);
244 #endif
245 
246 	if (size == 0) {
247 		*bytes_written = 0;
248 		return ERROR_OK;
249 	}
250 
251 	ret = jtag_libusb_control_transfer(usbh, CY7C65215_JTAG_REQUEST,
252 									   CY7C65215_JTAG_WRITE, size, 0,
253 									   NULL, 0, CY7C65215_USB_TIMEOUT);
254 	if (ret < 0) {
255 		LOG_ERROR("vendor command failed, error %d", ret);
256 		return ERROR_JTAG_DEVICE_ERROR;
257 	}
258 
259 	if (jtag_libusb_bulk_write(usbh, ep_out, (char *)buf, size,
260 				   CY7C65215_USB_TIMEOUT, &ret)) {
261 		LOG_ERROR("bulk write failed, error");
262 		return ERROR_JTAG_DEVICE_ERROR;
263 	}
264 	*bytes_written = ret;
265 
266 	return ERROR_OK;
267 }
268 
openjtag_buf_write(uint8_t * buf,int size,uint32_t * bytes_written)269 static int openjtag_buf_write(
270 	uint8_t *buf, int size, uint32_t *bytes_written)
271 {
272 	switch (openjtag_variant) {
273 	case OPENJTAG_VARIANT_CY7C65215:
274 		return openjtag_buf_write_cy7c65215(buf, size, bytes_written);
275 	default:
276 		return openjtag_buf_write_standard(buf, size, bytes_written);
277 	}
278 }
279 
openjtag_buf_read_standard(uint8_t * buf,uint32_t qty,uint32_t * bytes_read)280 static int openjtag_buf_read_standard(
281 	uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
282 {
283 
284 	int retval;
285 	int timeout = 5;
286 
287 	*bytes_read = 0;
288 
289 	while ((*bytes_read < qty) && timeout--) {
290 		retval = ftdi_read_data(&ftdic, buf + *bytes_read,
291 				qty - *bytes_read);
292 		if (retval < 0) {
293 			*bytes_read = 0;
294 			LOG_DEBUG_IO("ftdi_read_data: %s",
295 					ftdi_get_error_string(&ftdic));
296 			return ERROR_JTAG_DEVICE_ERROR;
297 		}
298 		*bytes_read += retval;
299 	}
300 
301 #ifdef _DEBUG_USB_COMMS_
302 	openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
303 #endif
304 
305 	return ERROR_OK;
306 }
307 
openjtag_buf_read_cy7c65215(uint8_t * buf,uint32_t qty,uint32_t * bytes_read)308 static int openjtag_buf_read_cy7c65215(
309 	uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
310 {
311 	int ret;
312 
313 	if (qty == 0) {
314 		*bytes_read = 0;
315 		goto out;
316 	}
317 
318 	ret = jtag_libusb_control_transfer(usbh, CY7C65215_JTAG_REQUEST,
319 									   CY7C65215_JTAG_READ, qty, 0,
320 									   NULL, 0, CY7C65215_USB_TIMEOUT);
321 	if (ret < 0) {
322 		LOG_ERROR("vendor command failed, error %d", ret);
323 		return ERROR_JTAG_DEVICE_ERROR;
324 	}
325 
326 	if (jtag_libusb_bulk_read(usbh, ep_in, (char *)buf, qty,
327 				  CY7C65215_USB_TIMEOUT, &ret)) {
328 		LOG_ERROR("bulk read failed, error");
329 		return ERROR_JTAG_DEVICE_ERROR;
330 	}
331 	*bytes_read = ret;
332 
333 out:
334 #ifdef _DEBUG_USB_COMMS_
335 	openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
336 #endif
337 
338 	return ERROR_OK;
339 }
340 
openjtag_buf_read(uint8_t * buf,uint32_t qty,uint32_t * bytes_read)341 static int openjtag_buf_read(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
342 {
343 	switch (openjtag_variant) {
344 	case OPENJTAG_VARIANT_CY7C65215:
345 		return openjtag_buf_read_cy7c65215(buf, qty, bytes_read);
346 	default:
347 		return openjtag_buf_read_standard(buf, qty, bytes_read);
348 	}
349 }
350 
openjtag_sendcommand(uint8_t cmd)351 static int openjtag_sendcommand(uint8_t cmd)
352 {
353 	uint32_t written;
354 	return openjtag_buf_write(&cmd, 1, &written);
355 }
356 
openjtag_speed(int speed)357 static int openjtag_speed(int speed)
358 {
359 	int clockcmd;
360 	switch (speed) {
361 		case 48000:
362 			clockcmd = 0x00;
363 			break;
364 		case 24000:
365 			clockcmd = 0x20;
366 			break;
367 		case 12000:
368 			clockcmd = 0x40;
369 			break;
370 		case 6000:
371 			clockcmd = 0x60;
372 			break;
373 		case 3000:
374 			clockcmd = 0x80;
375 			break;
376 		case 1500:
377 			clockcmd = 0xA0;
378 			break;
379 		case 750:
380 			clockcmd = 0xC0;
381 			break;
382 		case 375:
383 			clockcmd = 0xE0;
384 			break;
385 		default:
386 			clockcmd = 0xE0;
387 			LOG_WARNING("adapter speed not recognized, reverting to 375 kHz");
388 			break;
389 	}
390 	openjtag_sendcommand(clockcmd);
391 
392 	return ERROR_OK;
393 }
394 
openjtag_init_standard(void)395 static int openjtag_init_standard(void)
396 {
397 	uint8_t latency_timer;
398 
399 	/* Open by device description */
400 	if (openjtag_device_desc == NULL) {
401 		LOG_WARNING("no openjtag device description specified, "
402 				"using default 'Open JTAG Project'");
403 		openjtag_device_desc = "Open JTAG Project";
404 	}
405 
406 	if (ftdi_init(&ftdic) < 0)
407 		return ERROR_JTAG_INIT_FAILED;
408 
409 	/* context, vendor id, product id, description, serial id */
410 	if (ftdi_usb_open_desc(&ftdic, openjtag_vid, openjtag_pid, openjtag_device_desc, NULL) < 0) {
411 		LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
412 		return ERROR_JTAG_INIT_FAILED;
413 	}
414 
415 	if (ftdi_usb_reset(&ftdic) < 0) {
416 		LOG_ERROR("unable to reset ftdi device");
417 		return ERROR_JTAG_INIT_FAILED;
418 	}
419 
420 	if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
421 		LOG_ERROR("unable to set latency timer");
422 		return ERROR_JTAG_INIT_FAILED;
423 	}
424 
425 	if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
426 		LOG_ERROR("unable to get latency timer");
427 		return ERROR_JTAG_INIT_FAILED;
428 	}
429 	LOG_DEBUG("current latency timer: %u", latency_timer);
430 
431 	ftdi_disable_bitbang(&ftdic);
432 	/* was (3000000 / 4) with a comment about a bug in libftdi when using high baudrate */
433 	if (ftdi_set_baudrate(&ftdic, 3000000) < 0) {
434 		LOG_ERROR("Can't set baud rate to max: %s",
435 			ftdi_get_error_string(&ftdic));
436 		return ERROR_JTAG_DEVICE_ERROR;
437 	}
438 
439 	if (ftdi_tcioflush(&ftdic) < 0) {
440 		LOG_ERROR("ftdi flush: %s", ftdic.error_str);
441 		return ERROR_JTAG_INIT_FAILED;
442 	}
443 
444 	return ERROR_OK;
445 }
446 
openjtag_init_cy7c65215(void)447 static int openjtag_init_cy7c65215(void)
448 {
449 	int ret;
450 
451 	usbh = NULL;
452 	ret = jtag_libusb_open(cy7c65215_vids, cy7c65215_pids, NULL, &usbh, NULL);
453 	if (ret != ERROR_OK) {
454 		LOG_ERROR("unable to open cy7c65215 device");
455 		goto err;
456 	}
457 
458 	ret = jtag_libusb_choose_interface(usbh, &ep_in, &ep_out,
459 									   CY7C65215_JTAG_CLASS,
460 									   CY7C65215_JTAG_SUBCLASS, -1, LIBUSB_TRANSFER_TYPE_BULK);
461 	if (ret != ERROR_OK) {
462 		LOG_ERROR("unable to claim JTAG interface");
463 		goto err;
464 	}
465 
466 	ret = jtag_libusb_control_transfer(usbh,
467 									   CY7C65215_JTAG_REQUEST,
468 									   CY7C65215_JTAG_ENABLE,
469 									   0, 0, NULL, 0, CY7C65215_USB_TIMEOUT);
470 	if (ret < 0) {
471 		LOG_ERROR("could not enable JTAG module");
472 		goto err;
473 	}
474 
475 	return ERROR_OK;
476 
477 err:
478 	if (usbh != NULL)
479 		jtag_libusb_close(usbh);
480 	return ERROR_JTAG_INIT_FAILED;
481 }
482 
openjtag_init(void)483 static int openjtag_init(void)
484 {
485 	int ret;
486 
487 	usb_tx_buf_offs = 0;
488 	usb_rx_buf_len = 0;
489 	openjtag_scan_result_count = 0;
490 
491 	switch (openjtag_variant) {
492 	case OPENJTAG_VARIANT_CY7C65215:
493 		ret = openjtag_init_cy7c65215();
494 		break;
495 	default:
496 		ret = openjtag_init_standard();
497 	}
498 	if (ret != ERROR_OK)
499 		return ret;
500 
501 	openjtag_speed(375); /* Start at slowest adapter speed */
502 	openjtag_sendcommand(0x75); /* MSB */
503 
504 	return ERROR_OK;
505 }
506 
openjtag_quit_standard(void)507 static int openjtag_quit_standard(void)
508 {
509 	ftdi_usb_close(&ftdic);
510 	ftdi_deinit(&ftdic);
511 
512 	return ERROR_OK;
513 }
514 
openjtag_quit_cy7c65215(void)515 static int openjtag_quit_cy7c65215(void)
516 {
517 	int ret;
518 
519 	ret = jtag_libusb_control_transfer(usbh,
520 									   CY7C65215_JTAG_REQUEST,
521 									   CY7C65215_JTAG_DISABLE,
522 									   0, 0, NULL, 0, CY7C65215_USB_TIMEOUT);
523 	if (ret < 0)
524 		LOG_WARNING("could not disable JTAG module");
525 
526 	jtag_libusb_close(usbh);
527 
528 	return ERROR_OK;
529 }
530 
openjtag_quit(void)531 static int openjtag_quit(void)
532 {
533 	switch (openjtag_variant) {
534 	case OPENJTAG_VARIANT_CY7C65215:
535 		return openjtag_quit_cy7c65215();
536 	default:
537 		return openjtag_quit_standard();
538 	}
539 }
540 
openjtag_write_tap_buffer(void)541 static void openjtag_write_tap_buffer(void)
542 {
543 	uint32_t written;
544 
545 	openjtag_buf_write(usb_tx_buf, usb_tx_buf_offs, &written);
546 	openjtag_buf_read(usb_rx_buf, usb_tx_buf_offs, &usb_rx_buf_len);
547 
548 	usb_tx_buf_offs = 0;
549 }
550 
openjtag_execute_tap_queue(void)551 static int openjtag_execute_tap_queue(void)
552 {
553 	openjtag_write_tap_buffer();
554 
555 	int res_count = 0;
556 
557 	if (openjtag_scan_result_count && usb_rx_buf_len) {
558 
559 		int count;
560 		int rx_offs = 0;
561 		int len;
562 
563 		/* for every pending result */
564 		while (res_count < openjtag_scan_result_count) {
565 
566 			/* get sent bits */
567 			len = openjtag_scan_result_buffer[res_count].bits;
568 
569 			count = 0;
570 
571 			uint8_t *buffer = openjtag_scan_result_buffer[res_count].buffer;
572 
573 			while (len > 0) {
574 				if (len <= 8 && openjtag_variant != OPENJTAG_VARIANT_CY7C65215) {
575 					LOG_DEBUG_IO("bits < 8 buf = 0x%X, will be 0x%X",
576 						usb_rx_buf[rx_offs], usb_rx_buf[rx_offs] >> (8 - len));
577 					buffer[count] = usb_rx_buf[rx_offs] >> (8 - len);
578 					len = 0;
579 				} else {
580 					buffer[count] = usb_rx_buf[rx_offs];
581 					len -= 8;
582 				}
583 
584 				rx_offs++;
585 				count++;
586 			}
587 
588 #ifdef _DEBUG_USB_COMMS_
589 			openjtag_debug_buffer(buffer,
590 				DIV_ROUND_UP(openjtag_scan_result_buffer[res_count].bits, 8), DEBUG_TYPE_OCD_READ);
591 #endif
592 			jtag_read_buffer(buffer, openjtag_scan_result_buffer[res_count].command);
593 
594 			free(openjtag_scan_result_buffer[res_count].buffer);
595 
596 			res_count++;
597 		}
598 	}
599 
600 	openjtag_scan_result_count = 0;
601 
602 	return ERROR_OK;
603 }
604 
openjtag_add_byte(char buf)605 static void openjtag_add_byte(char buf)
606 {
607 
608 	if (usb_tx_buf_offs == OPENJTAG_BUFFER_SIZE) {
609 		LOG_DEBUG_IO("Forcing execute_tap_queue");
610 		LOG_DEBUG_IO("TX Buff offs=%d", usb_tx_buf_offs);
611 		openjtag_execute_tap_queue();
612 	}
613 
614 	usb_tx_buf[usb_tx_buf_offs] = buf;
615 	usb_tx_buf_offs++;
616 }
617 
openjtag_add_scan(uint8_t * buffer,int length,struct scan_command * scan_cmd)618 static void openjtag_add_scan(uint8_t *buffer, int length, struct scan_command *scan_cmd)
619 {
620 
621 	/* Ensure space to send long chains */
622 	/* We add two byte for each eight (or less) bits, one for command, one for data */
623 	if (usb_tx_buf_offs + (DIV_ROUND_UP(length, 8) * 2) >= OPENJTAG_BUFFER_SIZE) {
624 		LOG_DEBUG_IO("Forcing execute_tap_queue from scan");
625 		LOG_DEBUG_IO("TX Buff offs=%d len=%d", usb_tx_buf_offs, DIV_ROUND_UP(length, 8) * 2);
626 		openjtag_execute_tap_queue();
627 	}
628 
629 	openjtag_scan_result_buffer[openjtag_scan_result_count].bits = length;
630 	openjtag_scan_result_buffer[openjtag_scan_result_count].command = scan_cmd;
631 	openjtag_scan_result_buffer[openjtag_scan_result_count].buffer = buffer;
632 
633 	uint8_t command;
634 	uint8_t bits;
635 	int count = 0;
636 	while (length) {
637 
638 		/* write command */
639 		command = 6;
640 
641 		/* last bits? */
642 		if (length <= 8) {
643 			/* tms high */
644 			command |= (1 << 4);
645 
646 			/* bits to transfer */
647 			bits = (length - 1);
648 			command |= bits << 5;
649 			length = 0;
650 		} else {
651 			/* whole byte */
652 
653 			/* bits to transfer */
654 			command |= (7 << 5);
655 			length -= 8;
656 		}
657 
658 		openjtag_add_byte(command);
659 		openjtag_add_byte(buffer[count]);
660 		count++;
661 	}
662 
663 	openjtag_scan_result_count++;
664 }
665 
openjtag_execute_reset(struct jtag_command * cmd)666 static void openjtag_execute_reset(struct jtag_command *cmd)
667 {
668 
669 	LOG_DEBUG_IO("reset trst: %i srst %i",
670 			cmd->cmd.reset->trst, cmd->cmd.reset->srst);
671 
672 	uint8_t buf = 0x00;
673 
674 	if (cmd->cmd.reset->trst) {
675 		buf = 0x03;
676 	} else {
677 		buf |= 0x04;
678 		buf |= 0x05 << 4;
679 	}
680 
681 	openjtag_add_byte(buf);
682 }
683 
openjtag_execute_sleep(struct jtag_command * cmd)684 static void openjtag_execute_sleep(struct jtag_command *cmd)
685 {
686 	jtag_sleep(cmd->cmd.sleep->us);
687 }
688 
openjtag_set_state(uint8_t openocd_state)689 static void openjtag_set_state(uint8_t openocd_state)
690 {
691 	uint8_t state = openjtag_get_tap_state(openocd_state);
692 
693 	uint8_t buf = 0;
694 	buf = 0x01;
695 	buf |= state << 4;
696 
697 	openjtag_add_byte(buf);
698 }
699 
openjtag_execute_statemove(struct jtag_command * cmd)700 static void openjtag_execute_statemove(struct jtag_command *cmd)
701 {
702 	LOG_DEBUG_IO("state move to %i", cmd->cmd.statemove->end_state);
703 
704 	tap_set_end_state(cmd->cmd.statemove->end_state);
705 
706 	openjtag_set_state(cmd->cmd.statemove->end_state);
707 
708 	tap_set_state(tap_get_end_state());
709 }
710 
711 
openjtag_execute_scan(struct jtag_command * cmd)712 static void openjtag_execute_scan(struct jtag_command *cmd)
713 {
714 
715 	int scan_size, old_state;
716 	uint8_t *buffer;
717 
718 	LOG_DEBUG_IO("scan ends in %s", tap_state_name(cmd->cmd.scan->end_state));
719 
720 	/* get scan info */
721 	tap_set_end_state(cmd->cmd.scan->end_state);
722 	scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
723 
724 #ifdef _DEBUG_USB_COMMS_
725 	openjtag_debug_buffer(buffer, (scan_size + 7) / 8, DEBUG_TYPE_BUFFER);
726 #endif
727 	/* set state */
728 	old_state = tap_get_end_state();
729 	openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
730 	tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
731 	tap_set_end_state(old_state);
732 
733 	openjtag_add_scan(buffer, scan_size, cmd->cmd.scan);
734 
735 	openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
736 	tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
737 
738 	if (tap_get_state() != tap_get_end_state()) {
739 		openjtag_set_state(tap_get_end_state());
740 		tap_set_state(tap_get_end_state());
741 	}
742 }
743 
openjtag_execute_runtest(struct jtag_command * cmd)744 static void openjtag_execute_runtest(struct jtag_command *cmd)
745 {
746 
747 	tap_state_t end_state = cmd->cmd.runtest->end_state;
748 	tap_set_end_state(end_state);
749 
750 	/* only do a state_move when we're not already in IDLE */
751 	if (tap_get_state() != TAP_IDLE) {
752 		openjtag_set_state(TAP_IDLE);
753 		tap_set_state(TAP_IDLE);
754 	}
755 
756 	if (cmd->cmd.runtest->num_cycles > 16)
757 		LOG_WARNING("num_cycles > 16 on run test");
758 
759 	if (openjtag_variant != OPENJTAG_VARIANT_CY7C65215 ||
760 		cmd->cmd.runtest->num_cycles) {
761 		uint8_t command;
762 		command = 7;
763 		command |= ((cmd->cmd.runtest->num_cycles - 1) & 0x0F) << 4;
764 
765 		openjtag_add_byte(command);
766 	}
767 
768 	tap_set_end_state(end_state);
769 	if (tap_get_end_state() != tap_get_state()) {
770 		openjtag_set_state(end_state);
771 		tap_set_state(end_state);
772 	}
773 }
774 
openjtag_execute_command(struct jtag_command * cmd)775 static void openjtag_execute_command(struct jtag_command *cmd)
776 {
777 	LOG_DEBUG_IO("openjtag_execute_command %i", cmd->type);
778 	switch (cmd->type) {
779 	case JTAG_RESET:
780 			openjtag_execute_reset(cmd);
781 			break;
782 	case JTAG_SLEEP:
783 			openjtag_execute_sleep(cmd);
784 			break;
785 	case JTAG_TLR_RESET:
786 			openjtag_execute_statemove(cmd);
787 			break;
788 	case JTAG_SCAN:
789 			openjtag_execute_scan(cmd);
790 			break;
791 	case JTAG_RUNTEST:
792 			openjtag_execute_runtest(cmd);
793 			break;
794 	case JTAG_PATHMOVE:
795 		/* jlink_execute_pathmove(cmd); break; */
796 	default:
797 		LOG_ERROR("BUG: unknown Open JTAG command type encountered");
798 		exit(-1);
799 	}
800 }
801 
openjtag_execute_queue(void)802 static int openjtag_execute_queue(void)
803 {
804 	struct jtag_command *cmd = jtag_command_queue;
805 
806 	while (cmd != NULL) {
807 		openjtag_execute_command(cmd);
808 		cmd = cmd->next;
809 	}
810 
811 	return openjtag_execute_tap_queue();
812 }
813 
openjtag_speed_div(int speed,int * khz)814 static int openjtag_speed_div(int speed, int *khz)
815 {
816 	*khz = speed;
817 
818 	return ERROR_OK;
819 }
820 
openjtag_khz(int khz,int * jtag_speed)821 static int openjtag_khz(int khz, int *jtag_speed)
822 {
823 
824 	if (khz >= 48000)
825 		*jtag_speed = 48000;
826 	else if (khz >= 24000)
827 		*jtag_speed = 24000;
828 	else if (khz >= 12000)
829 		*jtag_speed = 12000;
830 	else if (khz >= 6000)
831 		*jtag_speed = 6000;
832 	else if (khz >= 3000)
833 		*jtag_speed = 3000;
834 	else if (khz >= 1500)
835 		*jtag_speed = 1500;
836 	else if (khz >= 750)
837 		*jtag_speed = 750;
838 	else
839 		*jtag_speed = 375;
840 
841 	return ERROR_OK;
842 }
843 
COMMAND_HANDLER(openjtag_handle_device_desc_command)844 COMMAND_HANDLER(openjtag_handle_device_desc_command)
845 {
846 	if (CMD_ARGC == 1)
847 		openjtag_device_desc = strdup(CMD_ARGV[0]);
848 	else
849 		LOG_ERROR("require exactly one argument to "
850 				  "openjtag_device_desc <description>");
851 	return ERROR_OK;
852 }
853 
COMMAND_HANDLER(openjtag_handle_variant_command)854 COMMAND_HANDLER(openjtag_handle_variant_command)
855 {
856 	if (CMD_ARGC == 1) {
857 		const char * const *name = openjtag_variant_names;
858 		int variant = 0;
859 		for (; *name; name++, variant++) {
860 			if (strcasecmp(CMD_ARGV[0], *name) == 0) {
861 				openjtag_variant = variant;
862 				return ERROR_OK;
863 			}
864 		}
865 		LOG_ERROR("unknown openjtag variant '%s'", CMD_ARGV[0]);
866 	} else {
867 		LOG_ERROR("require exactly one argument to "
868 				"openjtag_variant <variant>");
869 	}
870 	return ERROR_OK;
871 }
872 
873 static const struct command_registration openjtag_command_handlers[] = {
874 	{
875 		.name = "openjtag_device_desc",
876 		.handler = openjtag_handle_device_desc_command,
877 		.mode = COMMAND_CONFIG,
878 		.help = "set the USB device description of the OpenJTAG",
879 		.usage = "description-string",
880 	},
881 	{
882 		.name = "openjtag_variant",
883 		.handler = openjtag_handle_variant_command,
884 		.mode = COMMAND_CONFIG,
885 		.help = "set the OpenJTAG variant",
886 		.usage = "variant-string",
887 	},
888 	COMMAND_REGISTRATION_DONE
889 };
890 
891 static struct jtag_interface openjtag_interface = {
892 	.execute_queue = openjtag_execute_queue,
893 };
894 
895 struct adapter_driver openjtag_adapter_driver = {
896 	.name = "openjtag",
897 	.transports = jtag_only,
898 	.commands = openjtag_command_handlers,
899 
900 	.init = openjtag_init,
901 	.quit = openjtag_quit,
902 	.speed = openjtag_speed,
903 	.khz = openjtag_khz,
904 	.speed_div = openjtag_speed_div,
905 
906 	.jtag_ops = &openjtag_interface,
907 };
908