]> cvs.zerfleddert.de Git - usb-driver/blob - jtagkey.c
some microoptimazation
[usb-driver] / jtagkey.c
1 #include <stdio.h>
2 #include <ftdi.h>
3 #include <unistd.h>
4 #include <pthread.h>
5 #include "usb-driver.h"
6 #include "config.h"
7 #include "jtagkey.h"
8
9 #define USBBUFSIZE 1048576
10 #define JTAG_SPEED 100000
11 #define SLOW_AND_SAFE 1
12
13 static struct ftdi_context ftdic;
14 static unsigned char bitbang_mode;
15
16 static int jtagkey_init(unsigned short vid, unsigned short pid) {
17 int ret = 0;
18 unsigned char c;
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
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 }
44
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));
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
55 c = 0x00;
56 ftdi_write_data(&ftdic, &c, 1);
57
58 if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_SYNCBB)) != 0) {
59 fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
60 return ret;
61 }
62
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
68 bitbang_mode = BITMODE_SYNCBB;
69
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
78 int 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
89 void jtagkey_close(int handle) {
90 if (handle == 0xff) {
91 ftdi_disable_bitbang(&ftdic);
92 ftdi_usb_close(&ftdic);
93 ftdi_deinit(&ftdic);
94 }
95 }
96
97 #ifndef SLOW_AND_SAFE
98 static int jtagkey_set_bbmode(unsigned char mode) {
99 int ret = 0;
100
101 if (bitbang_mode != mode) {
102 DPRINTF("switching bitbang-mode!\n");
103
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 }
122 #endif
123
124 void 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
145 struct jtagkey_reader_arg {
146 int num;
147 unsigned char *buf;
148 };
149
150 static 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
164 /* TODO: Interpret JTAG commands and transfer in MPSSE mode */
165 int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) {
166 int ret = 0;
167 int i;
168 int nread = 0;
169 unsigned long port;
170 unsigned char val;
171 static unsigned char last_data = 0;
172 static unsigned char last_write = 0x00;
173 static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
174 static unsigned char readbuf[USBBUFSIZE], *readpos;
175 unsigned char data, prev_data, last_cyc_write;
176 struct jtagkey_reader_arg targ;
177 pthread_t reader_thread;
178
179 /* Count reads */
180 for (i = 0; i < num; i++)
181 if (tr[i].cmdTrans == PP_READ)
182 nread++;
183
184 /* Write combining */
185 if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) {
186 unsigned char *pos = writebuf;
187 int len;
188
189 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num);
190
191 #ifndef SLOW_AND_SAFE
192 jtagkey_set_bbmode(BITMODE_BITBANG);
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);
198 #endif
199 while (pos < writepos) {
200 len = writepos-pos;
201
202 if (len > USBBUFSIZE)
203 len = USBBUFSIZE;
204
205 DPRINTF("combined write of %d/%d\n",len,writepos-pos);
206 ftdi_write_data(&ftdic, pos, len);
207 pos += len;
208 }
209 #ifdef SLOW_AND_SAFE
210 pthread_join(reader_thread, NULL);
211 #endif
212
213 writepos = writebuf;
214 }
215
216 last_cyc_write = last_write;
217
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
231 /* Pad writebuf for read-commands in stream */
232 *writepos = last_data;
233 prev_data = last_data;
234
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) {
264 data = JTAGKEY_OEn;
265 DPRINTF("CTRL\n");
266 } else {
267 DPRINTF("!CTRL\n");
268 }
269
270 if (val & PP_PROG) {
271 DPRINTF("PROG\n");
272 } else {
273 DPRINTF("!PROG\n");
274 }
275
276 *writepos = data;
277
278 last_data = data;
279 last_write = val;
280 break;
281
282 default:
283 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans);
284 ret = -1;
285 break;
286 }
287 }
288
289 if ((tr[i].cmdTrans == PP_READ) || (*writepos != prev_data) || (i == num-1))
290 writepos++;
291 }
292
293 if (nread)
294 {
295 DPRINTF("writing %d bytes\n", writepos-writebuf);
296
297 *writepos = last_data;
298 writepos++;
299
300 #ifndef SLOW_AND_SAFE
301 jtagkey_set_bbmode(BITMODE_SYNCBB);
302 #endif
303 targ.num = writepos-writebuf;
304 targ.buf = readbuf;
305 pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
306 ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
307 pthread_join(reader_thread, NULL);
308
309 #ifdef DEBUG
310 DPRINTF("write: ");
311 hexdump(writebuf, writepos-writebuf);
312 DPRINTF("read: ");
313 hexdump(readbuf, i);
314 #endif
315
316 writepos = writebuf;
317 } else {
318 return ret;
319 }
320
321 readpos = readbuf;
322 last_write = last_cyc_write;
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;
331
332 if ((tr[i].cmdTrans != PP_READ) && (val == last_write) && (i != num-1))
333 continue;
334
335 readpos++;
336
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);
343 switch(tr[i].cmdTrans) {
344 case PP_READ:
345 data = *readpos;
346
347 #ifdef DEBUG
348 DPRINTF("READ: 0x%x\n", data);
349 jtagkey_state(data);
350 #endif
351
352 val = 0x00;
353 if ((data & JTAGKEY_TDO) && (last_write & PP_PROG))
354 val |= PP_TDO;
355
356 if (~last_write & PP_PROG)
357 val |= 0x08;
358
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 }
374 } else {
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