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