]> cvs.zerfleddert.de Git - usb-driver/blobdiff - jtagkey.c
add one supported and one untested cable
[usb-driver] / jtagkey.c
index 41f0fd60723df3d2e472ada46b1a2cd8da1d0690..d068cf50fdd84b9d4adedd99b3ca8ef1f6128555 100644 (file)
--- a/jtagkey.c
+++ b/jtagkey.c
@@ -1,16 +1,36 @@
 #include <stdio.h>
 #include <ftdi.h>
 #include <unistd.h>
+#include <pthread.h>
+#include <inttypes.h>
 #include "usb-driver.h"
 #include "config.h"
 #include "jtagkey.h"
+#include "jtagmon.h"
 
-#define USBBUFSIZE 4096
-#define SLOW_AND_SAFE 1
+#define USBBUFSIZE 1048576
+#define JTAG_SPEED 100000
+#define BULK_LATENCY 2
+#define OTHER_LATENCY 1
 
 static struct ftdi_context ftdic;
-static unsigned int usb_maxlen = 0;
-static unsigned char bitbang_mode;
+
+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) {
        int ret = 0;
@@ -36,25 +56,18 @@ static int jtagkey_init(unsigned short vid, unsigned short pid) {
                return ret;
        }
 
-
-#ifdef SLOW_AND_SAFE
-       usb_maxlen = 384;
-#else
-       if ((ret = ftdi_write_data_get_chunksize(&ftdic, &usb_maxlen))  != 0) {
-               fprintf(stderr, "unable to get write chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
+       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;
        }
-#endif
 
-       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 = ftdi_read_data_set_chunksize(&ftdic, USBBUFSIZE))  != 0) {
+               fprintf(stderr, "unable to set read chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
                return ret;
        }
 
-       if ((ret = ftdi_set_baudrate(&ftdic, 500000))  != 0) {
-               fprintf(stderr, "unable to set baudrate: %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);
@@ -64,7 +77,10 @@ static int jtagkey_init(unsigned short vid, unsigned short pid) {
                return ret;
        }
 
-       bitbang_mode = BITMODE_SYNCBB;
+       if ((ret = ftdi_set_baudrate(&ftdic, JTAG_SPEED))  != 0) {
+               fprintf(stderr, "unable to set baudrate: %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));
@@ -93,32 +109,8 @@ void jtagkey_close(int handle) {
        }
 }
 
-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;
-}
-
-void jtagkey_state(unsigned char data) {
+#ifdef DEBUG
+static void jtagkey_state(unsigned char data) {
        fprintf(stderr,"Pins high: ");
 
        if (data & JTAGKEY_TCK)
@@ -138,7 +130,27 @@ void jtagkey_state(unsigned char data) {
        
        fprintf(stderr,"\n");
 }
+#endif
+
+struct jtagkey_reader_arg {
+       int             num;
+       unsigned char   *buf;
+};
 
+static void *jtagkey_reader(void *thread_arg) {
+       struct jtagkey_reader_arg *arg = (struct jtagkey_reader_arg*)thread_arg;
+       int i;
+
+       i = 0;
+       DPRINTF("reader for %d bytes\n", arg->num);
+       while (i < arg->num) {
+               i += ftdi_read_data(&ftdic, arg->buf + i, arg->num - i);
+       }
+       
+       pthread_exit(NULL);
+}
+
+/* TODO: Interpret JTAG commands and transfer in MPSSE mode */
 int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) {
        int ret = 0;
        int i;
@@ -149,7 +161,9 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
        static unsigned char last_write = 0x00;
        static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
        static unsigned char readbuf[USBBUFSIZE], *readpos;
-       unsigned char data, prev_data;
+       unsigned char data, prev_data, last_cyc_write;
+       struct jtagkey_reader_arg targ;
+       pthread_t reader_thread;
 
        /* Count reads */
        for (i = 0; i < num; i++)
@@ -160,31 +174,31 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
        if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) {
                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);
+               jtagkey_latency(BULK_LATENCY);
+
+               targ.num = writepos-pos;
+               targ.buf = readbuf;
+               pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
 
-#ifndef SLOW_AND_SAFE
-               jtagkey_set_bbmode(BITMODE_BITBANG);
-#endif
                while (pos < writepos) {
                        len = writepos-pos;
 
-                       if (len > usb_maxlen)
-                               len = usb_maxlen;
+                       if (len > USBBUFSIZE)
+                               len = USBBUFSIZE;
 
                        DPRINTF("combined write of %d/%d\n",len,writepos-pos);
                        ftdi_write_data(&ftdic, pos, len);
-#ifdef SLOW_AND_SAFE
-                       i = 0;
-                       while (i < len) {
-                               i += ftdi_read_data(&ftdic, readbuf, sizeof(readbuf));
-                       }
-#endif
                        pos += len;
                }
+               pthread_join(reader_thread, NULL);
 
                writepos = writebuf;
        }
 
+       last_cyc_write = last_write;
+
        for (i = 0; i < num; i++) {
                DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
                                (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
@@ -196,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 */
@@ -256,7 +273,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                        }
                }
 
-               if (nread || (*writepos != prev_data))
+               if ((tr[i].cmdTrans == PP_READ) || (*writepos != prev_data) || (i == num-1))
                        writepos++;
        }
 
@@ -267,19 +284,17 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                *writepos = last_data;
                writepos++;
 
-               jtagkey_set_bbmode(BITMODE_SYNCBB);
-               ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
+               jtagkey_latency(OTHER_LATENCY);
 
-               i = 0;
-               while (i < writepos-writebuf) {
-                       i += ftdi_read_data(&ftdic, readbuf, sizeof(readbuf));
-               }
+               targ.num = writepos-writebuf;
+               targ.buf = readbuf;
+               pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
+               ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
+               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;
@@ -288,6 +303,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
        }
 
        readpos = readbuf;
+       last_write = last_cyc_write;
 
        for (i = 0; i < num; i++) {
                DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
@@ -296,6 +312,10 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
 
                port = (unsigned long)tr[i].dwPort;
                val = tr[i].Data.Byte;
+
+               if ((tr[i].cmdTrans != PP_READ) && (val == last_write) && (i != num-1))
+                       continue;
+
                readpos++;
 
                if (port == ppbase + PP_DATA) {
@@ -317,7 +337,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                                        if ((data & JTAGKEY_TDO) && (last_write & PP_PROG))
                                                val |= PP_TDO;
 
-                                       if (!(last_write & PP_PROG))
+                                       if (~last_write & PP_PROG)
                                                val |= 0x08;
 
                                        if (last_write & 0x40)
Impressum, Datenschutz