]>
Commit | Line | Data |
---|---|---|
1 | #include <stdio.h> | |
2 | #include <ftdi.h> | |
3 | #include <unistd.h> | |
4 | #include <pthread.h> | |
5 | #include <inttypes.h> | |
6 | #include "usb-driver.h" | |
7 | #include "config.h" | |
8 | #include "jtagkey.h" | |
9 | #include "jtagmon.h" | |
10 | ||
11 | #define USBBUFSIZE 1048576 | |
12 | #define JTAG_SPEED 100000 | |
13 | #define BULK_LATENCY 2 | |
14 | #define OTHER_LATENCY 1 | |
15 | ||
16 | static struct ftdi_context ftdic; | |
17 | ||
18 | static 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 | } | |
34 | ||
35 | static int jtagkey_init(unsigned short vid, unsigned short pid) { | |
36 | int ret = 0; | |
37 | unsigned char c; | |
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 | ||
44 | if ((ret = ftdi_usb_open(&ftdic, vid, pid)) != 0) { | |
45 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); | |
46 | return ret; | |
47 | } | |
48 | ||
49 | if ((ret = ftdi_usb_reset(&ftdic)) != 0) { | |
50 | fprintf(stderr, "unable reset device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); | |
51 | return ret; | |
52 | } | |
53 | ||
54 | if ((ret = ftdi_set_interface(&ftdic, INTERFACE_A)) != 0) { | |
55 | fprintf(stderr, "unable to set interface: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); | |
56 | return ret; | |
57 | } | |
58 | ||
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 | } | |
63 | ||
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)); | |
66 | return ret; | |
67 | } | |
68 | ||
69 | if ((ret = jtagkey_latency(OTHER_LATENCY)) != 0) | |
70 | return ret; | |
71 | ||
72 | c = 0x00; | |
73 | ftdi_write_data(&ftdic, &c, 1); | |
74 | ||
75 | if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_SYNCBB)) != 0) { | |
76 | fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); | |
77 | return ret; | |
78 | } | |
79 | ||
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 | ||
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 | ||
93 | int jtagkey_open(int num) { | |
94 | int ret; | |
95 | ||
96 | ret = jtagkey_init(config_usb_vid(num), config_usb_pid(num)); | |
97 | ||
98 | if (ret >= 0) | |
99 | ret = 0xff; | |
100 | ||
101 | return ret; | |
102 | } | |
103 | ||
104 | void jtagkey_close(int handle) { | |
105 | if (handle == 0xff) { | |
106 | ftdi_disable_bitbang(&ftdic); | |
107 | ftdi_usb_close(&ftdic); | |
108 | ftdi_deinit(&ftdic); | |
109 | } | |
110 | } | |
111 | ||
112 | void jtagkey_state(unsigned char data) { | |
113 | fprintf(stderr,"Pins high: "); | |
114 | ||
115 | if (data & JTAGKEY_TCK) | |
116 | fprintf(stderr,"TCK "); | |
117 | ||
118 | if (data & JTAGKEY_TDI) | |
119 | fprintf(stderr,"TDI "); | |
120 | ||
121 | if (data & JTAGKEY_TDO) | |
122 | fprintf(stderr,"TDO "); | |
123 | ||
124 | if (data & JTAGKEY_TMS) | |
125 | fprintf(stderr,"TMS "); | |
126 | ||
127 | if (data & JTAGKEY_VREF) | |
128 | fprintf(stderr,"VREF "); | |
129 | ||
130 | fprintf(stderr,"\n"); | |
131 | } | |
132 | ||
133 | struct jtagkey_reader_arg { | |
134 | int num; | |
135 | unsigned char *buf; | |
136 | }; | |
137 | ||
138 | static void *jtagkey_reader(void *thread_arg) { | |
139 | struct jtagkey_reader_arg *arg = (struct jtagkey_reader_arg*)thread_arg; | |
140 | int i; | |
141 | ||
142 | i = 0; | |
143 | DPRINTF("reader for %d bytes\n", arg->num); | |
144 | while (i < arg->num) { | |
145 | i += ftdi_read_data(&ftdic, arg->buf + i, arg->num - i); | |
146 | } | |
147 | ||
148 | pthread_exit(NULL); | |
149 | } | |
150 | ||
151 | /* TODO: Interpret JTAG commands and transfer in MPSSE mode */ | |
152 | int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) { | |
153 | int ret = 0; | |
154 | int i; | |
155 | int nread = 0; | |
156 | unsigned long port; | |
157 | unsigned char val; | |
158 | static unsigned char last_data = 0; | |
159 | static unsigned char last_write = 0x00; | |
160 | static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf; | |
161 | static unsigned char readbuf[USBBUFSIZE], *readpos; | |
162 | unsigned char data, prev_data, last_cyc_write; | |
163 | struct jtagkey_reader_arg targ; | |
164 | pthread_t reader_thread; | |
165 | ||
166 | /* Count reads */ | |
167 | for (i = 0; i < num; i++) | |
168 | if (tr[i].cmdTrans == PP_READ) | |
169 | nread++; | |
170 | ||
171 | /* Write combining */ | |
172 | if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) { | |
173 | unsigned char *pos = writebuf; | |
174 | int len; | |
175 | ||
176 | DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num); | |
177 | jtagkey_latency(BULK_LATENCY); | |
178 | ||
179 | targ.num = writepos-pos; | |
180 | targ.buf = readbuf; | |
181 | pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ); | |
182 | ||
183 | while (pos < writepos) { | |
184 | len = writepos-pos; | |
185 | ||
186 | if (len > USBBUFSIZE) | |
187 | len = USBBUFSIZE; | |
188 | ||
189 | DPRINTF("combined write of %d/%d\n",len,writepos-pos); | |
190 | ftdi_write_data(&ftdic, pos, len); | |
191 | pos += len; | |
192 | } | |
193 | pthread_join(reader_thread, NULL); | |
194 | ||
195 | writepos = writebuf; | |
196 | } | |
197 | ||
198 | last_cyc_write = last_write; | |
199 | ||
200 | for (i = 0; i < num; i++) { | |
201 | DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n", | |
202 | (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes, | |
203 | tr[i].fAutoinc, tr[i].dwOptions); | |
204 | ||
205 | port = (unsigned long)tr[i].dwPort; | |
206 | val = tr[i].Data.Byte; | |
207 | ||
208 | #ifdef DEBUG | |
209 | if (tr[i].cmdTrans == 13) | |
210 | DPRINTF("write byte: %d\n", val); | |
211 | ||
212 | if (tr[i].cmdTrans == 13) | |
213 | jtagmon(val & PP_TCK, val & PP_TMS, val & PP_TDI); | |
214 | #endif | |
215 | ||
216 | /* Pad writebuf for read-commands in stream */ | |
217 | *writepos = last_data; | |
218 | prev_data = last_data; | |
219 | ||
220 | if (port == ppbase + PP_DATA) { | |
221 | DPRINTF("data port\n"); | |
222 | ||
223 | data = 0x00; | |
224 | switch(tr[i].cmdTrans) { | |
225 | case PP_READ: | |
226 | ret = 0; /* We don't support reading of the data port */ | |
227 | break; | |
228 | ||
229 | case PP_WRITE: | |
230 | if (val & PP_TDI) { | |
231 | data |= JTAGKEY_TDI; | |
232 | DPRINTF("TDI\n"); | |
233 | } else { | |
234 | DPRINTF("!TDI\n"); | |
235 | } | |
236 | if (val & PP_TCK) { | |
237 | data |= JTAGKEY_TCK; | |
238 | DPRINTF("TCK\n"); | |
239 | } else { | |
240 | DPRINTF("!TCK\n"); | |
241 | } | |
242 | if (val & PP_TMS) { | |
243 | data |= JTAGKEY_TMS; | |
244 | DPRINTF("TMS\n"); | |
245 | } else { | |
246 | DPRINTF("!TMS\n"); | |
247 | } | |
248 | if (val & PP_CTRL) { | |
249 | data = JTAGKEY_OEn; | |
250 | DPRINTF("CTRL\n"); | |
251 | } else { | |
252 | DPRINTF("!CTRL\n"); | |
253 | } | |
254 | ||
255 | if (val & PP_PROG) { | |
256 | DPRINTF("PROG\n"); | |
257 | } else { | |
258 | DPRINTF("!PROG\n"); | |
259 | } | |
260 | ||
261 | *writepos = data; | |
262 | ||
263 | last_data = data; | |
264 | last_write = val; | |
265 | break; | |
266 | ||
267 | default: | |
268 | fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans); | |
269 | ret = -1; | |
270 | break; | |
271 | } | |
272 | } | |
273 | ||
274 | if ((tr[i].cmdTrans == PP_READ) || (*writepos != prev_data) || (i == num-1)) | |
275 | writepos++; | |
276 | } | |
277 | ||
278 | if (nread) | |
279 | { | |
280 | DPRINTF("writing %d bytes\n", writepos-writebuf); | |
281 | ||
282 | *writepos = last_data; | |
283 | writepos++; | |
284 | ||
285 | jtagkey_latency(OTHER_LATENCY); | |
286 | ||
287 | targ.num = writepos-writebuf; | |
288 | targ.buf = readbuf; | |
289 | pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ); | |
290 | ftdi_write_data(&ftdic, writebuf, writepos-writebuf); | |
291 | pthread_join(reader_thread, NULL); | |
292 | ||
293 | #ifdef DEBUG | |
294 | DPRINTF("write: "); | |
295 | hexdump(writebuf, writepos-writebuf); | |
296 | DPRINTF("read: "); | |
297 | hexdump(readbuf, i); | |
298 | #endif | |
299 | ||
300 | writepos = writebuf; | |
301 | } else { | |
302 | return ret; | |
303 | } | |
304 | ||
305 | readpos = readbuf; | |
306 | last_write = last_cyc_write; | |
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; | |
315 | ||
316 | if ((tr[i].cmdTrans != PP_READ) && (val == last_write) && (i != num-1)) | |
317 | continue; | |
318 | ||
319 | readpos++; | |
320 | ||
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); | |
327 | switch(tr[i].cmdTrans) { | |
328 | case PP_READ: | |
329 | data = *readpos; | |
330 | ||
331 | #ifdef DEBUG | |
332 | DPRINTF("READ: 0x%x\n", data); | |
333 | jtagkey_state(data); | |
334 | #endif | |
335 | ||
336 | val = 0x00; | |
337 | if ((data & JTAGKEY_TDO) && (last_write & PP_PROG)) | |
338 | val |= PP_TDO; | |
339 | ||
340 | if (~last_write & PP_PROG) | |
341 | val |= 0x08; | |
342 | ||
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 | } | |
358 | } else { | |
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 | } |