]> git.zerfleddert.de Git - usb-driver/blob - jtagkey.c
e17afc8895a71aa55da0bd7bb6bcd5c091a848f0
[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 SLOW_AND_SAFE 1
11
12 static struct ftdi_context ftdic;
13 static unsigned char bitbang_mode;
14
15 static int jtagkey_init(unsigned short vid, unsigned short pid) {
16 int ret = 0;
17 unsigned char c;
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
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 }
43
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));
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
54 if ((ret = ftdi_set_baudrate(&ftdic, 500000)) != 0) {
55 fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
56 return ret;
57 }
58
59 c = 0x00;
60 ftdi_write_data(&ftdic, &c, 1);
61
62 if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_SYNCBB)) != 0) {
63 fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
64 return ret;
65 }
66
67 bitbang_mode = BITMODE_SYNCBB;
68
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
77 int 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
88 void jtagkey_close(int handle) {
89 if (handle == 0xff) {
90 ftdi_disable_bitbang(&ftdic);
91 ftdi_usb_close(&ftdic);
92 ftdi_deinit(&ftdic);
93 }
94 }
95
96 #ifndef SLOW_AND_SAFE
97 static int jtagkey_set_bbmode(unsigned char mode) {
98 int ret = 0;
99
100 if (bitbang_mode != mode) {
101 DPRINTF("switching bitbang-mode!\n");
102
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 }
121 #endif
122
123 void 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
144 struct jtagkey_reader_arg {
145 int num;
146 unsigned char *buf;
147 };
148
149 static 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
163 int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) {
164 int ret = 0;
165 int i;
166 int nread = 0;
167 unsigned long port;
168 unsigned char val;
169 static unsigned char last_data = 0;
170 static unsigned char last_write = 0x00;
171 static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
172 static unsigned char readbuf[USBBUFSIZE], *readpos;
173 unsigned char data, prev_data;
174 struct jtagkey_reader_arg targ;
175 pthread_t reader_thread;
176
177 /* Count reads */
178 for (i = 0; i < num; i++)
179 if (tr[i].cmdTrans == PP_READ)
180 nread++;
181
182 /* Write combining */
183 if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) {
184 unsigned char *pos = writebuf;
185 int len;
186
187 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num);
188
189 #ifndef SLOW_AND_SAFE
190 jtagkey_set_bbmode(BITMODE_BITBANG);
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);
196 #endif
197 while (pos < writepos) {
198 len = writepos-pos;
199
200 if (len > USBBUFSIZE)
201 len = USBBUFSIZE;
202
203 DPRINTF("combined write of %d/%d\n",len,writepos-pos);
204 ftdi_write_data(&ftdic, pos, len);
205 pos += len;
206 }
207 #ifdef SLOW_AND_SAFE
208 pthread_join(reader_thread, NULL);
209 #endif
210
211 writepos = writebuf;
212 }
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
227 /* Pad writebuf for read-commands in stream */
228 *writepos = last_data;
229 prev_data = last_data;
230
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) {
260 data = JTAGKEY_OEn;
261 DPRINTF("CTRL\n");
262 } else {
263 DPRINTF("!CTRL\n");
264 }
265
266 if (val & PP_PROG) {
267 DPRINTF("PROG\n");
268 } else {
269 DPRINTF("!PROG\n");
270 }
271
272 *writepos = data;
273
274 last_data = data;
275 last_write = val;
276 break;
277
278 default:
279 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans);
280 ret = -1;
281 break;
282 }
283 }
284
285 if (nread || (*writepos != prev_data) || (i == num-1))
286 writepos++;
287 }
288
289 if (nread)
290 {
291 DPRINTF("writing %d bytes\n", writepos-writebuf);
292
293 *writepos = last_data;
294 writepos++;
295
296 #ifndef SLOW_AND_SAFE
297 jtagkey_set_bbmode(BITMODE_SYNCBB);
298 #endif
299 targ.num = writepos-writebuf;
300 targ.buf = readbuf;
301 pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
302 ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
303 pthread_join(reader_thread, NULL);
304
305 #ifdef DEBUG
306 DPRINTF("write: ");
307 hexdump(writebuf, writepos-writebuf);
308 DPRINTF("read: ");
309 hexdump(readbuf, i);
310 #endif
311
312 writepos = writebuf;
313 } else {
314 return ret;
315 }
316
317 readpos = readbuf;
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;
326 readpos++;
327
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);
334 switch(tr[i].cmdTrans) {
335 case PP_READ:
336 data = *readpos;
337
338 #ifdef DEBUG
339 DPRINTF("READ: 0x%x\n", data);
340 jtagkey_state(data);
341 #endif
342
343 val = 0x00;
344 if ((data & JTAGKEY_TDO) && (last_write & PP_PROG))
345 val |= PP_TDO;
346
347 if (!(last_write & PP_PROG))
348 val |= 0x08;
349
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 }
365 } else {
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