X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/usb-driver/blobdiff_plain/8121bc50c29cae60e7ea5b5be5cbafffb8ba3563..66c1fd53cb58160b7997f99879f2cf546a97e6b8:/jtagkey.c diff --git a/jtagkey.c b/jtagkey.c index 0078e47..129cfe1 100644 --- a/jtagkey.c +++ b/jtagkey.c @@ -1,12 +1,21 @@ #include #include +#include +#include #include "usb-driver.h" +#include "config.h" #include "jtagkey.h" +#define USBBUFSIZE 1048576 +#define JTAG_SPEED 100000 +#define SLOW_AND_SAFE 1 + static struct ftdi_context ftdic; +static unsigned char bitbang_mode; -int jtagkey_init(unsigned short vid, unsigned short pid) { +static int jtagkey_init(unsigned short vid, unsigned short pid) { int ret = 0; + unsigned char c; if ((ret = ftdi_init(&ftdic)) != 0) { fprintf(stderr, "unable to initialise libftdi: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); @@ -28,26 +37,36 @@ int jtagkey_init(unsigned short vid, unsigned short pid) { return ret; } - if ((ret = ftdi_write_data_set_chunksize(&ftdic, 1)) != 0) { + 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; } + 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_latency_timer(&ftdic, 1)) != 0) { fprintf(stderr, "unable to set latency timer: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); return ret; } - if ((ret = ftdi_set_baudrate(&ftdic, 230400)) != 0) { - fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); + c = 0x00; + ftdi_write_data(&ftdic, &c, 1); + + if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_SYNCBB)) != 0) { + fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); return ret; } - if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, 1)) != 0) { - fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); + 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; } + 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; @@ -56,12 +75,52 @@ int jtagkey_init(unsigned short vid, unsigned short pid) { return ret; } -void jtagkey_close() { - ftdi_disable_bitbang(&ftdic); - ftdi_usb_close(&ftdic); - ftdi_deinit(&ftdic); +int jtagkey_open(int num) { + int ret; + + ret = jtagkey_init(config_usb_vid(num), config_usb_pid(num)); + + if (ret >= 0) + ret = 0xff; + + return ret; +} + +void jtagkey_close(int handle) { + if (handle == 0xff) { + ftdi_disable_bitbang(&ftdic); + ftdi_usb_close(&ftdic); + ftdi_deinit(&ftdic); + } } +#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) { fprintf(stderr,"Pins high: "); @@ -83,13 +142,75 @@ void jtagkey_state(unsigned char data) { fprintf(stderr,"\n"); } +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); +} + int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) { int ret = 0; int i; + int nread = 0; unsigned long port; unsigned char val; - static unsigned char last_write = 0; - unsigned char data; + static unsigned char last_data = 0; + static unsigned char last_write = 0x00; + static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf; + static unsigned char readbuf[USBBUFSIZE], *readpos; + unsigned char data, prev_data; + struct jtagkey_reader_arg targ; + pthread_t reader_thread; + + /* Count reads */ + for (i = 0; i < num; i++) + if (tr[i].cmdTrans == PP_READ) + nread++; + + /* Write combining */ + 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); + +#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); + ftdi_write_data(&ftdic, pos, len); + pos += len; + } +#ifdef SLOW_AND_SAFE + pthread_join(reader_thread, NULL); +#endif + + writepos = writebuf; + } for (i = 0; i < num; i++) { DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n", @@ -104,6 +225,10 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, DPRINTF("write byte: %d\n", val); #endif + /* Pad writebuf for read-commands in stream */ + *writepos = last_data; + prev_data = last_data; + if (port == ppbase + PP_DATA) { DPRINTF("data port\n"); @@ -133,19 +258,21 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, DPRINTF("!TMS\n"); } if (val & PP_CTRL) { - data |= JTAGKEY_OEn; + data = JTAGKEY_OEn; DPRINTF("CTRL\n"); } else { DPRINTF("!CTRL\n"); } - ftdi_write_data(&ftdic, &data, 1); -#if 0 - do { - ftdi_read_pins(&ftdic, &tmp); - } while ((tmp & (JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_TCK|JTAGKEY_TDI)) != data); -#endif + if (val & PP_PROG) { + DPRINTF("PROG\n"); + } else { + DPRINTF("!PROG\n"); + } + *writepos = data; + + last_data = data; last_write = val; break; @@ -154,20 +281,73 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, ret = -1; break; } + } + + if (nread || (*writepos != prev_data) || (i == num-1)) + writepos++; + } + + if (nread) + { + DPRINTF("writing %d bytes\n", writepos-writebuf); + + *writepos = last_data; + writepos++; + +#ifndef SLOW_AND_SAFE + jtagkey_set_bbmode(BITMODE_SYNCBB); +#endif + 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); +#endif + + writepos = writebuf; + } else { + return ret; + } + + readpos = readbuf; + + 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, + tr[i].fAutoinc, tr[i].dwOptions); + + port = (unsigned long)tr[i].dwPort; + val = tr[i].Data.Byte; + readpos++; + + if (port == ppbase + PP_DATA) { + if (tr[i].cmdTrans == PP_WRITE) { + last_write = val; + } } else if (port == ppbase + PP_STATUS) { - DPRINTF("status port (last write: %d)\n", last_write); + DPRINTF("status port (last write: 0x%x)\n", last_write); switch(tr[i].cmdTrans) { case PP_READ: - ftdi_read_pins(&ftdic, &data); + data = *readpos; + #ifdef DEBUG DPRINTF("READ: 0x%x\n", data); jtagkey_state(data); #endif val = 0x00; - if (data & JTAGKEY_TDO) + if ((data & JTAGKEY_TDO) && (last_write & PP_PROG)) val |= PP_TDO; + if (!(last_write & PP_PROG)) + val |= 0x08; + if (last_write & 0x40) val |= 0x20; else @@ -183,16 +363,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, ret = -1; break; } - } else if (port == ppbase + PP_CONTROL) { - DPRINTF("control port\n"); - } else if ((port == ecpbase + PP_ECP_CFGA) && ecpbase) { - DPRINTF("ECP_CFGA port\n"); - } else if ((port == ecpbase + PP_ECP_CFGB) && ecpbase) { - DPRINTF("ECP_CFGB port\n"); - } else if ((port == ecpbase + PP_ECP_ECR) && ecpbase) { - DPRINTF("ECP_ECR port\n"); } else { - DPRINTF("access to unsupported address range!\n"); ret = 0; }