X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/usb-driver/blobdiff_plain/617583d07236b4b34e5b5105d99e0f40bf2bbb4f..7c932f66e13171aece712ea8bb018bee3252b7ad:/jtagkey.c diff --git a/jtagkey.c b/jtagkey.c index fc762d3..04bd0cd 100644 --- a/jtagkey.c +++ b/jtagkey.c @@ -2,18 +2,37 @@ #include #include #include +#include #include "usb-driver.h" #include "config.h" #include "jtagkey.h" +#include "jtagmon.h" #define USBBUFSIZE 1048576 #define JTAG_SPEED 100000 -#define SLOW_AND_SAFE 1 +#define BULK_LATENCY 2 +#define OTHER_LATENCY 1 static struct ftdi_context ftdic; -static unsigned char bitbang_mode; -static int jtagkey_init(unsigned short vid, unsigned short pid) { +static int jtagkey_latency(int latency) { + static int current = 0; + int ret; + + if (current != latency) { + DPRINTF("switching latency\n"); + if ((ret = ftdi_set_latency_timer(&ftdic, latency)) != 0) { + fprintf(stderr, "unable to set latency timer: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); + return ret; + } + + current = latency; + } + + return ret; +} + +static int jtagkey_init(unsigned short vid, unsigned short pid, unsigned short iface) { int ret = 0; unsigned char c; @@ -22,6 +41,11 @@ static int jtagkey_init(unsigned short vid, unsigned short pid) { return ret; } + if ((ret = ftdi_set_interface(&ftdic, iface)) != 0) { + fprintf(stderr, "unable to set interface: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); + return ret; + } + if ((ret = ftdi_usb_open(&ftdic, vid, pid)) != 0) { fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); return ret; @@ -32,11 +56,6 @@ static int jtagkey_init(unsigned short vid, unsigned short pid) { return ret; } - if ((ret = ftdi_set_interface(&ftdic, INTERFACE_A)) != 0) { - fprintf(stderr, "unable to set interface: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); - return ret; - } - if ((ret = ftdi_write_data_set_chunksize(&ftdic, USBBUFSIZE)) != 0) { fprintf(stderr, "unable to set write chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); return ret; @@ -47,10 +66,8 @@ static int jtagkey_init(unsigned short vid, unsigned short pid) { return ret; } - if ((ret = ftdi_set_latency_timer(&ftdic, 1)) != 0) { - fprintf(stderr, "unable to set latency timer: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); + if ((ret = jtagkey_latency(OTHER_LATENCY)) != 0) return ret; - } c = 0x00; ftdi_write_data(&ftdic, &c, 1); @@ -65,8 +82,6 @@ static int jtagkey_init(unsigned short vid, unsigned short pid) { return ret; } - bitbang_mode = BITMODE_SYNCBB; - if ((ret = ftdi_usb_purge_buffers(&ftdic)) != 0) { fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); return ret; @@ -78,7 +93,7 @@ static int jtagkey_init(unsigned short vid, unsigned short pid) { int jtagkey_open(int num) { int ret; - ret = jtagkey_init(config_usb_vid(num), config_usb_pid(num)); + ret = jtagkey_init(config_usb_vid(num), config_usb_pid(num), config_usb_iface(num)); if (ret >= 0) ret = 0xff; @@ -94,34 +109,8 @@ void jtagkey_close(int handle) { } } -#ifndef SLOW_AND_SAFE -static int jtagkey_set_bbmode(unsigned char mode) { - int ret = 0; - - if (bitbang_mode != mode) { - DPRINTF("switching bitbang-mode!\n"); - - /* Wait for the latency-timer to kick in */ - usleep(2); - if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, mode)) != 0) { - fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); - return ret; - } - if ((ret = ftdi_usb_purge_buffers(&ftdic)) != 0) { - fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); - return ret; - } - /* Wait for the FTDI2232 to settle */ - usleep(2); - - bitbang_mode = mode; - } - - return ret; -} -#endif - -void jtagkey_state(unsigned char data) { +#ifdef DEBUG +static void jtagkey_state(unsigned char data) { fprintf(stderr,"Pins high: "); if (data & JTAGKEY_TCK) @@ -141,6 +130,7 @@ void jtagkey_state(unsigned char data) { fprintf(stderr,"\n"); } +#endif struct jtagkey_reader_arg { int num; @@ -149,7 +139,6 @@ struct jtagkey_reader_arg { static void *jtagkey_reader(void *thread_arg) { struct jtagkey_reader_arg *arg = (struct jtagkey_reader_arg*)thread_arg; - int i; i = 0; @@ -186,29 +175,24 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, unsigned char *pos = writebuf; int len; - DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num); + DPRINTF("writing %zd bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num); + jtagkey_latency(BULK_LATENCY); -#ifndef SLOW_AND_SAFE - jtagkey_set_bbmode(BITMODE_BITBANG); -#endif -#ifdef SLOW_AND_SAFE targ.num = writepos-pos; targ.buf = readbuf; pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ); -#endif + while (pos < writepos) { len = writepos-pos; if (len > USBBUFSIZE) len = USBBUFSIZE; - DPRINTF("combined write of %d/%d\n",len,writepos-pos); + DPRINTF("combined write of %d/%zd\n",len,writepos-pos); ftdi_write_data(&ftdic, pos, len); pos += len; } -#ifdef SLOW_AND_SAFE pthread_join(reader_thread, NULL); -#endif writepos = writebuf; } @@ -226,6 +210,9 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, #ifdef DEBUG if (tr[i].cmdTrans == 13) DPRINTF("write byte: %d\n", val); + + if (tr[i].cmdTrans == 13) + jtagmon(val & PP_TCK, val & PP_TMS, val & PP_TDI); #endif /* Pad writebuf for read-commands in stream */ @@ -292,14 +279,13 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, if (nread) { - DPRINTF("writing %d bytes\n", writepos-writebuf); + DPRINTF("writing %zd bytes\n", writepos-writebuf); *writepos = last_data; writepos++; -#ifndef SLOW_AND_SAFE - jtagkey_set_bbmode(BITMODE_SYNCBB); -#endif + jtagkey_latency(OTHER_LATENCY); + targ.num = writepos-writebuf; targ.buf = readbuf; pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ); @@ -307,10 +293,8 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, pthread_join(reader_thread, NULL); #ifdef DEBUG - DPRINTF("write: "); - hexdump(writebuf, writepos-writebuf); - DPRINTF("read: "); - hexdump(readbuf, i); + hexdump(writebuf, writepos-writebuf, "->"); + hexdump(readbuf, i, "<-"); #endif writepos = writebuf;