]> git.zerfleddert.de Git - usb-driver/blame - jtagkey.c
correctly set the baudrate, this speeds up things a bit
[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
8121bc50 164int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) {
165 int ret = 0;
166 int i;
d0e2002d 167 int nread = 0;
8121bc50 168 unsigned long port;
169 unsigned char val;
d0e2002d 170 static unsigned char last_data = 0;
e81047b8 171 static unsigned char last_write = 0x00;
d0e2002d 172 static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
173 static unsigned char readbuf[USBBUFSIZE], *readpos;
e81047b8 174 unsigned char data, prev_data;
3cc5a0bc 175 struct jtagkey_reader_arg targ;
176 pthread_t reader_thread;
d0e2002d 177
e81047b8 178 /* Count reads */
d0e2002d 179 for (i = 0; i < num; i++)
e81047b8 180 if (tr[i].cmdTrans == PP_READ)
d0e2002d 181 nread++;
182
183 /* Write combining */
184 if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) {
4af4753d 185 unsigned char *pos = writebuf;
186 int len;
3cc5a0bc 187
d0e2002d 188 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num);
189
75417531 190#ifndef SLOW_AND_SAFE
9640dec9 191 jtagkey_set_bbmode(BITMODE_BITBANG);
3cc5a0bc 192#endif
193#ifdef SLOW_AND_SAFE
194 targ.num = writepos-pos;
195 targ.buf = readbuf;
196 pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
75417531 197#endif
4af4753d 198 while (pos < writepos) {
85690a3f 199 len = writepos-pos;
200
3cc5a0bc 201 if (len > USBBUFSIZE)
202 len = USBBUFSIZE;
d0e2002d 203
4af4753d 204 DPRINTF("combined write of %d/%d\n",len,writepos-pos);
205 ftdi_write_data(&ftdic, pos, len);
206 pos += len;
e81047b8 207 }
3cc5a0bc 208#ifdef SLOW_AND_SAFE
209 pthread_join(reader_thread, NULL);
210#endif
4af4753d 211
d0e2002d 212 writepos = writebuf;
213 }
8121bc50 214
215 for (i = 0; i < num; i++) {
216 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
217 (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
218 tr[i].fAutoinc, tr[i].dwOptions);
219
220 port = (unsigned long)tr[i].dwPort;
221 val = tr[i].Data.Byte;
222
223#ifdef DEBUG
224 if (tr[i].cmdTrans == 13)
225 DPRINTF("write byte: %d\n", val);
226#endif
227
b122ac4b 228 /* Pad writebuf for read-commands in stream */
d0e2002d 229 *writepos = last_data;
e81047b8 230 prev_data = last_data;
b122ac4b 231
8121bc50 232 if (port == ppbase + PP_DATA) {
233 DPRINTF("data port\n");
234
235 data = 0x00;
236 switch(tr[i].cmdTrans) {
237 case PP_READ:
238 ret = 0; /* We don't support reading of the data port */
239 break;
240
241 case PP_WRITE:
242 if (val & PP_TDI) {
243 data |= JTAGKEY_TDI;
244 DPRINTF("TDI\n");
245 } else {
246 DPRINTF("!TDI\n");
247 }
248 if (val & PP_TCK) {
249 data |= JTAGKEY_TCK;
250 DPRINTF("TCK\n");
251 } else {
252 DPRINTF("!TCK\n");
253 }
254 if (val & PP_TMS) {
255 data |= JTAGKEY_TMS;
256 DPRINTF("TMS\n");
257 } else {
258 DPRINTF("!TMS\n");
259 }
260 if (val & PP_CTRL) {
d0e2002d 261 data = JTAGKEY_OEn;
8121bc50 262 DPRINTF("CTRL\n");
263 } else {
264 DPRINTF("!CTRL\n");
265 }
8121bc50 266
d0e2002d 267 if (val & PP_PROG) {
268 DPRINTF("PROG\n");
269 } else {
270 DPRINTF("!PROG\n");
271 }
272
273 *writepos = data;
274
b122ac4b 275 last_data = data;
d0e2002d 276 last_write = val;
8121bc50 277 break;
278
279 default:
280 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans);
281 ret = -1;
282 break;
283 }
b122ac4b 284 }
e81047b8 285
3cc5a0bc 286 if (nread || (*writepos != prev_data) || (i == num-1))
e81047b8 287 writepos++;
b122ac4b 288 }
289
d0e2002d 290 if (nread)
291 {
292 DPRINTF("writing %d bytes\n", writepos-writebuf);
59b06a85 293
294 *writepos = last_data;
295 writepos++;
296
3cc5a0bc 297#ifndef SLOW_AND_SAFE
9640dec9 298 jtagkey_set_bbmode(BITMODE_SYNCBB);
3cc5a0bc 299#endif
300 targ.num = writepos-writebuf;
301 targ.buf = readbuf;
302 pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
9640dec9 303 ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
3cc5a0bc 304 pthread_join(reader_thread, NULL);
b122ac4b 305
306#ifdef DEBUG
d0e2002d 307 DPRINTF("write: ");
308 hexdump(writebuf, writepos-writebuf);
309 DPRINTF("read: ");
310 hexdump(readbuf, i);
b122ac4b 311#endif
312
d0e2002d 313 writepos = writebuf;
314 } else {
315 return ret;
316 }
317
318 readpos = readbuf;
b122ac4b 319
320 for (i = 0; i < num; i++) {
321 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
322 (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
323 tr[i].fAutoinc, tr[i].dwOptions);
324
325 port = (unsigned long)tr[i].dwPort;
326 val = tr[i].Data.Byte;
d0e2002d 327 readpos++;
b122ac4b 328
d0e2002d 329 if (port == ppbase + PP_DATA) {
330 if (tr[i].cmdTrans == PP_WRITE) {
331 last_write = val;
332 }
333 } else if (port == ppbase + PP_STATUS) {
334 DPRINTF("status port (last write: 0x%x)\n", last_write);
8121bc50 335 switch(tr[i].cmdTrans) {
336 case PP_READ:
d0e2002d 337 data = *readpos;
59b06a85 338
8121bc50 339#ifdef DEBUG
340 DPRINTF("READ: 0x%x\n", data);
341 jtagkey_state(data);
342#endif
343
344 val = 0x00;
d0e2002d 345 if ((data & JTAGKEY_TDO) && (last_write & PP_PROG))
8121bc50 346 val |= PP_TDO;
347
d0e2002d 348 if (!(last_write & PP_PROG))
349 val |= 0x08;
350
8121bc50 351 if (last_write & 0x40)
352 val |= 0x20;
353 else
354 val |= 0x80;
355 break;
356
357 case PP_WRITE:
358 ret = 0; /* Status Port is readonly */
359 break;
360
361 default:
362 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans);
363 ret = -1;
364 break;
365 }
8121bc50 366 } else {
8121bc50 367 ret = 0;
368 }
369
370 tr[i].Data.Byte = val;
371
372 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
373 (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
374 tr[i].fAutoinc, tr[i].dwOptions);
375#ifdef DEBUG
376 if (tr[i].cmdTrans == 10)
377 DPRINTF("read byte: %d\n", tr[i].Data.Byte);
378#endif
379 }
380
381 return ret;
382}
Impressum, Datenschutz