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