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