]> git.zerfleddert.de Git - usb-driver/blame - jtagkey.c
config parser to associate ftdi2232 devices with parallel port
[usb-driver] / jtagkey.c
CommitLineData
8121bc50 1#include <stdio.h>
2#include <ftdi.h>
9640dec9 3#include <unistd.h>
3cc5a0bc 4#include <pthread.h>
8121bc50 5#include "usb-driver.h"
5e3d963b 6#include "config.h"
8121bc50 7#include "jtagkey.h"
8
3cc5a0bc 9#define USBBUFSIZE 1048576
66c1fd53 10#define JTAG_SPEED 100000
75417531 11#define SLOW_AND_SAFE 1
d0e2002d 12
8121bc50 13static struct ftdi_context ftdic;
9640dec9 14static unsigned char bitbang_mode;
8121bc50 15
5e3d963b 16static int jtagkey_init(unsigned short vid, unsigned short pid) {
8121bc50 17 int ret = 0;
d0e2002d 18 unsigned char c;
8121bc50 19
20 if ((ret = ftdi_init(&ftdic)) != 0) {
21 fprintf(stderr, "unable to initialise libftdi: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
22 return ret;
23 }
24
25 if ((ret = ftdi_usb_open(&ftdic, vid, pid)) != 0) {
26 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
27 return ret;
28 }
29
30 if ((ret = ftdi_usb_reset(&ftdic)) != 0) {
31 fprintf(stderr, "unable reset device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
32 return ret;
33 }
34
35 if ((ret = ftdi_set_interface(&ftdic, INTERFACE_A)) != 0) {
36 fprintf(stderr, "unable to set interface: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
37 return ret;
38 }
39
3cc5a0bc 40 if ((ret = ftdi_write_data_set_chunksize(&ftdic, USBBUFSIZE)) != 0) {
41 fprintf(stderr, "unable to set write chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
42 return ret;
43 }
4af4753d 44
3cc5a0bc 45 if ((ret = ftdi_read_data_set_chunksize(&ftdic, USBBUFSIZE)) != 0) {
46 fprintf(stderr, "unable to set read chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
8121bc50 47 return ret;
48 }
49
50 if ((ret = ftdi_set_latency_timer(&ftdic, 1)) != 0) {
51 fprintf(stderr, "unable to set latency timer: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
52 return ret;
53 }
54
d0e2002d 55 c = 0x00;
56 ftdi_write_data(&ftdic, &c, 1);
57
75417531 58 if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_SYNCBB)) != 0) {
8121bc50 59 fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
60 return ret;
61 }
62
66c1fd53 63 if ((ret = ftdi_set_baudrate(&ftdic, JTAG_SPEED)) != 0) {
64 fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
65 return ret;
66 }
67
75417531 68 bitbang_mode = BITMODE_SYNCBB;
9640dec9 69
8121bc50 70 if ((ret = ftdi_usb_purge_buffers(&ftdic)) != 0) {
71 fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
72 return ret;
73 }
74
75 return ret;
76}
77
5e3d963b 78int jtagkey_open(int num) {
79 int ret;
80
81 ret = jtagkey_init(config_usb_vid(num), config_usb_pid(num));
82
83 if (ret >= 0)
84 ret = 0xff;
85
86 return ret;
87}
88
89void jtagkey_close(int handle) {
90 if (handle == 0xff) {
91 ftdi_disable_bitbang(&ftdic);
92 ftdi_usb_close(&ftdic);
93 ftdi_deinit(&ftdic);
94 }
8121bc50 95}
96
3cc5a0bc 97#ifndef SLOW_AND_SAFE
9640dec9 98static int jtagkey_set_bbmode(unsigned char mode) {
99 int ret = 0;
100
101 if (bitbang_mode != mode) {
102 DPRINTF("switching bitbang-mode!\n");
59b06a85 103
9640dec9 104 /* Wait for the latency-timer to kick in */
105 usleep(2);
106 if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, mode)) != 0) {
107 fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
108 return ret;
109 }
110 if ((ret = ftdi_usb_purge_buffers(&ftdic)) != 0) {
111 fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
112 return ret;
113 }
114 /* Wait for the FTDI2232 to settle */
115 usleep(2);
116
117 bitbang_mode = mode;
118 }
119
120 return ret;
121}
3cc5a0bc 122#endif
9640dec9 123
8121bc50 124void jtagkey_state(unsigned char data) {
125 fprintf(stderr,"Pins high: ");
126
127 if (data & JTAGKEY_TCK)
128 fprintf(stderr,"TCK ");
129
130 if (data & JTAGKEY_TDI)
131 fprintf(stderr,"TDI ");
132
133 if (data & JTAGKEY_TDO)
134 fprintf(stderr,"TDO ");
135
136 if (data & JTAGKEY_TMS)
137 fprintf(stderr,"TMS ");
138
139 if (data & JTAGKEY_VREF)
140 fprintf(stderr,"VREF ");
141
142 fprintf(stderr,"\n");
143}
144
3cc5a0bc 145struct jtagkey_reader_arg {
146 int num;
147 unsigned char *buf;
148};
149
150static void *jtagkey_reader(void *thread_arg) {
151 struct jtagkey_reader_arg *arg = (struct jtagkey_reader_arg*)thread_arg;
152
153 int i;
154
155 i = 0;
156 DPRINTF("reader for %d bytes\n", arg->num);
157 while (i < arg->num) {
158 i += ftdi_read_data(&ftdic, arg->buf + i, arg->num - i);
159 }
160
161 pthread_exit(NULL);
162}
163
617583d0 164/* TODO: Interpret JTAG commands and transfer in MPSSE mode */
8121bc50 165int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) {
166 int ret = 0;
167 int i;
d0e2002d 168 int nread = 0;
8121bc50 169 unsigned long port;
170 unsigned char val;
d0e2002d 171 static unsigned char last_data = 0;
e81047b8 172 static unsigned char last_write = 0x00;
d0e2002d 173 static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
174 static unsigned char readbuf[USBBUFSIZE], *readpos;
617583d0 175 unsigned char data, prev_data, last_cyc_write;
3cc5a0bc 176 struct jtagkey_reader_arg targ;
177 pthread_t reader_thread;
d0e2002d 178
e81047b8 179 /* Count reads */
d0e2002d 180 for (i = 0; i < num; i++)
e81047b8 181 if (tr[i].cmdTrans == PP_READ)
d0e2002d 182 nread++;
183
184 /* Write combining */
185 if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) {
4af4753d 186 unsigned char *pos = writebuf;
187 int len;
3cc5a0bc 188
d0e2002d 189 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num);
190
75417531 191#ifndef SLOW_AND_SAFE
9640dec9 192 jtagkey_set_bbmode(BITMODE_BITBANG);
3cc5a0bc 193#endif
194#ifdef SLOW_AND_SAFE
195 targ.num = writepos-pos;
196 targ.buf = readbuf;
197 pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
75417531 198#endif
4af4753d 199 while (pos < writepos) {
85690a3f 200 len = writepos-pos;
201
3cc5a0bc 202 if (len > USBBUFSIZE)
203 len = USBBUFSIZE;
d0e2002d 204
4af4753d 205 DPRINTF("combined write of %d/%d\n",len,writepos-pos);
206 ftdi_write_data(&ftdic, pos, len);
207 pos += len;
e81047b8 208 }
3cc5a0bc 209#ifdef SLOW_AND_SAFE
210 pthread_join(reader_thread, NULL);
211#endif
4af4753d 212
d0e2002d 213 writepos = writebuf;
214 }
8121bc50 215
617583d0 216 last_cyc_write = last_write;
217
8121bc50 218 for (i = 0; i < num; i++) {
219 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
220 (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
221 tr[i].fAutoinc, tr[i].dwOptions);
222
223 port = (unsigned long)tr[i].dwPort;
224 val = tr[i].Data.Byte;
225
226#ifdef DEBUG
227 if (tr[i].cmdTrans == 13)
228 DPRINTF("write byte: %d\n", val);
229#endif
230
b122ac4b 231 /* Pad writebuf for read-commands in stream */
d0e2002d 232 *writepos = last_data;
e81047b8 233 prev_data = last_data;
b122ac4b 234
8121bc50 235 if (port == ppbase + PP_DATA) {
236 DPRINTF("data port\n");
237
238 data = 0x00;
239 switch(tr[i].cmdTrans) {
240 case PP_READ:
241 ret = 0; /* We don't support reading of the data port */
242 break;
243
244 case PP_WRITE:
245 if (val & PP_TDI) {
246 data |= JTAGKEY_TDI;
247 DPRINTF("TDI\n");
248 } else {
249 DPRINTF("!TDI\n");
250 }
251 if (val & PP_TCK) {
252 data |= JTAGKEY_TCK;
253 DPRINTF("TCK\n");
254 } else {
255 DPRINTF("!TCK\n");
256 }
257 if (val & PP_TMS) {
258 data |= JTAGKEY_TMS;
259 DPRINTF("TMS\n");
260 } else {
261 DPRINTF("!TMS\n");
262 }
263 if (val & PP_CTRL) {
d0e2002d 264 data = JTAGKEY_OEn;
8121bc50 265 DPRINTF("CTRL\n");
266 } else {
267 DPRINTF("!CTRL\n");
268 }
8121bc50 269
d0e2002d 270 if (val & PP_PROG) {
271 DPRINTF("PROG\n");
272 } else {
273 DPRINTF("!PROG\n");
274 }
275
276 *writepos = data;
277
b122ac4b 278 last_data = data;
d0e2002d 279 last_write = val;
8121bc50 280 break;
281
282 default:
283 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans);
284 ret = -1;
285 break;
286 }
b122ac4b 287 }
e81047b8 288
617583d0 289 if ((tr[i].cmdTrans == PP_READ) || (*writepos != prev_data) || (i == num-1))
e81047b8 290 writepos++;
b122ac4b 291 }
292
d0e2002d 293 if (nread)
294 {
295 DPRINTF("writing %d bytes\n", writepos-writebuf);
59b06a85 296
297 *writepos = last_data;
298 writepos++;
299
3cc5a0bc 300#ifndef SLOW_AND_SAFE
9640dec9 301 jtagkey_set_bbmode(BITMODE_SYNCBB);
3cc5a0bc 302#endif
303 targ.num = writepos-writebuf;
304 targ.buf = readbuf;
305 pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
9640dec9 306 ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
3cc5a0bc 307 pthread_join(reader_thread, NULL);
b122ac4b 308
309#ifdef DEBUG
d0e2002d 310 DPRINTF("write: ");
311 hexdump(writebuf, writepos-writebuf);
312 DPRINTF("read: ");
313 hexdump(readbuf, i);
b122ac4b 314#endif
315
d0e2002d 316 writepos = writebuf;
317 } else {
318 return ret;
319 }
320
321 readpos = readbuf;
617583d0 322 last_write = last_cyc_write;
b122ac4b 323
324 for (i = 0; i < num; i++) {
325 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
326 (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
327 tr[i].fAutoinc, tr[i].dwOptions);
328
329 port = (unsigned long)tr[i].dwPort;
330 val = tr[i].Data.Byte;
617583d0 331
332 if ((tr[i].cmdTrans != PP_READ) && (val == last_write) && (i != num-1))
333 continue;
334
d0e2002d 335 readpos++;
b122ac4b 336
d0e2002d 337 if (port == ppbase + PP_DATA) {
338 if (tr[i].cmdTrans == PP_WRITE) {
339 last_write = val;
340 }
341 } else if (port == ppbase + PP_STATUS) {
342 DPRINTF("status port (last write: 0x%x)\n", last_write);
8121bc50 343 switch(tr[i].cmdTrans) {
344 case PP_READ:
d0e2002d 345 data = *readpos;
59b06a85 346
8121bc50 347#ifdef DEBUG
348 DPRINTF("READ: 0x%x\n", data);
349 jtagkey_state(data);
350#endif
351
352 val = 0x00;
d0e2002d 353 if ((data & JTAGKEY_TDO) && (last_write & PP_PROG))
8121bc50 354 val |= PP_TDO;
355
617583d0 356 if (~last_write & PP_PROG)
d0e2002d 357 val |= 0x08;
358
8121bc50 359 if (last_write & 0x40)
360 val |= 0x20;
361 else
362 val |= 0x80;
363 break;
364
365 case PP_WRITE:
366 ret = 0; /* Status Port is readonly */
367 break;
368
369 default:
370 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans);
371 ret = -1;
372 break;
373 }
8121bc50 374 } else {
8121bc50 375 ret = 0;
376 }
377
378 tr[i].Data.Byte = val;
379
380 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
381 (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
382 tr[i].fAutoinc, tr[i].dwOptions);
383#ifdef DEBUG
384 if (tr[i].cmdTrans == 10)
385 DPRINTF("read byte: %d\n", tr[i].Data.Byte);
386#endif
387 }
388
389 return ret;
390}
Impressum, Datenschutz