]>
Commit | Line | Data |
---|---|---|
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 | int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) { | |
165 | int ret = 0; | |
166 | int i; | |
167 | int nread = 0; | |
168 | unsigned long port; | |
169 | unsigned char val; | |
170 | static unsigned char last_data = 0; | |
171 | static unsigned char last_write = 0x00; | |
172 | static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf; | |
173 | static unsigned char readbuf[USBBUFSIZE], *readpos; | |
174 | unsigned char data, prev_data; | |
175 | struct jtagkey_reader_arg targ; | |
176 | pthread_t reader_thread; | |
177 | ||
178 | /* Count reads */ | |
179 | for (i = 0; i < num; i++) | |
180 | if (tr[i].cmdTrans == PP_READ) | |
181 | nread++; | |
182 | ||
183 | /* Write combining */ | |
184 | if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) { | |
185 | unsigned char *pos = writebuf; | |
186 | int len; | |
187 | ||
188 | DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num); | |
189 | ||
190 | #ifndef SLOW_AND_SAFE | |
191 | jtagkey_set_bbmode(BITMODE_BITBANG); | |
192 | #endif | |
193 | #ifdef SLOW_AND_SAFE | |
194 | targ.num = writepos-pos; | |
195 | targ.buf = readbuf; | |
196 | pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ); | |
197 | #endif | |
198 | while (pos < writepos) { | |
199 | len = writepos-pos; | |
200 | ||
201 | if (len > USBBUFSIZE) | |
202 | len = USBBUFSIZE; | |
203 | ||
204 | DPRINTF("combined write of %d/%d\n",len,writepos-pos); | |
205 | ftdi_write_data(&ftdic, pos, len); | |
206 | pos += len; | |
207 | } | |
208 | #ifdef SLOW_AND_SAFE | |
209 | pthread_join(reader_thread, NULL); | |
210 | #endif | |
211 | ||
212 | writepos = writebuf; | |
213 | } | |
214 | ||
215 | for (i = 0; i < num; i++) { | |
216 | DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n", | |
217 | (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes, | |
218 | tr[i].fAutoinc, tr[i].dwOptions); | |
219 | ||
220 | port = (unsigned long)tr[i].dwPort; | |
221 | val = tr[i].Data.Byte; | |
222 | ||
223 | #ifdef DEBUG | |
224 | if (tr[i].cmdTrans == 13) | |
225 | DPRINTF("write byte: %d\n", val); | |
226 | #endif | |
227 | ||
228 | /* Pad writebuf for read-commands in stream */ | |
229 | *writepos = last_data; | |
230 | prev_data = last_data; | |
231 | ||
232 | if (port == ppbase + PP_DATA) { | |
233 | DPRINTF("data port\n"); | |
234 | ||
235 | data = 0x00; | |
236 | switch(tr[i].cmdTrans) { | |
237 | case PP_READ: | |
238 | ret = 0; /* We don't support reading of the data port */ | |
239 | break; | |
240 | ||
241 | case PP_WRITE: | |
242 | if (val & PP_TDI) { | |
243 | data |= JTAGKEY_TDI; | |
244 | DPRINTF("TDI\n"); | |
245 | } else { | |
246 | DPRINTF("!TDI\n"); | |
247 | } | |
248 | if (val & PP_TCK) { | |
249 | data |= JTAGKEY_TCK; | |
250 | DPRINTF("TCK\n"); | |
251 | } else { | |
252 | DPRINTF("!TCK\n"); | |
253 | } | |
254 | if (val & PP_TMS) { | |
255 | data |= JTAGKEY_TMS; | |
256 | DPRINTF("TMS\n"); | |
257 | } else { | |
258 | DPRINTF("!TMS\n"); | |
259 | } | |
260 | if (val & PP_CTRL) { | |
261 | data = JTAGKEY_OEn; | |
262 | DPRINTF("CTRL\n"); | |
263 | } else { | |
264 | DPRINTF("!CTRL\n"); | |
265 | } | |
266 | ||
267 | if (val & PP_PROG) { | |
268 | DPRINTF("PROG\n"); | |
269 | } else { | |
270 | DPRINTF("!PROG\n"); | |
271 | } | |
272 | ||
273 | *writepos = data; | |
274 | ||
275 | last_data = data; | |
276 | last_write = val; | |
277 | break; | |
278 | ||
279 | default: | |
280 | fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans); | |
281 | ret = -1; | |
282 | break; | |
283 | } | |
284 | } | |
285 | ||
286 | if (nread || (*writepos != prev_data) || (i == num-1)) | |
287 | writepos++; | |
288 | } | |
289 | ||
290 | if (nread) | |
291 | { | |
292 | DPRINTF("writing %d bytes\n", writepos-writebuf); | |
293 | ||
294 | *writepos = last_data; | |
295 | writepos++; | |
296 | ||
297 | #ifndef SLOW_AND_SAFE | |
298 | jtagkey_set_bbmode(BITMODE_SYNCBB); | |
299 | #endif | |
300 | targ.num = writepos-writebuf; | |
301 | targ.buf = readbuf; | |
302 | pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ); | |
303 | ftdi_write_data(&ftdic, writebuf, writepos-writebuf); | |
304 | pthread_join(reader_thread, NULL); | |
305 | ||
306 | #ifdef DEBUG | |
307 | DPRINTF("write: "); | |
308 | hexdump(writebuf, writepos-writebuf); | |
309 | DPRINTF("read: "); | |
310 | hexdump(readbuf, i); | |
311 | #endif | |
312 | ||
313 | writepos = writebuf; | |
314 | } else { | |
315 | return ret; | |
316 | } | |
317 | ||
318 | readpos = readbuf; | |
319 | ||
320 | for (i = 0; i < num; i++) { | |
321 | DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n", | |
322 | (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes, | |
323 | tr[i].fAutoinc, tr[i].dwOptions); | |
324 | ||
325 | port = (unsigned long)tr[i].dwPort; | |
326 | val = tr[i].Data.Byte; | |
327 | readpos++; | |
328 | ||
329 | if (port == ppbase + PP_DATA) { | |
330 | if (tr[i].cmdTrans == PP_WRITE) { | |
331 | last_write = val; | |
332 | } | |
333 | } else if (port == ppbase + PP_STATUS) { | |
334 | DPRINTF("status port (last write: 0x%x)\n", last_write); | |
335 | switch(tr[i].cmdTrans) { | |
336 | case PP_READ: | |
337 | data = *readpos; | |
338 | ||
339 | #ifdef DEBUG | |
340 | DPRINTF("READ: 0x%x\n", data); | |
341 | jtagkey_state(data); | |
342 | #endif | |
343 | ||
344 | val = 0x00; | |
345 | if ((data & JTAGKEY_TDO) && (last_write & PP_PROG)) | |
346 | val |= PP_TDO; | |
347 | ||
348 | if (!(last_write & PP_PROG)) | |
349 | val |= 0x08; | |
350 | ||
351 | if (last_write & 0x40) | |
352 | val |= 0x20; | |
353 | else | |
354 | val |= 0x80; | |
355 | break; | |
356 | ||
357 | case PP_WRITE: | |
358 | ret = 0; /* Status Port is readonly */ | |
359 | break; | |
360 | ||
361 | default: | |
362 | fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans); | |
363 | ret = -1; | |
364 | break; | |
365 | } | |
366 | } else { | |
367 | ret = 0; | |
368 | } | |
369 | ||
370 | tr[i].Data.Byte = val; | |
371 | ||
372 | DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n", | |
373 | (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes, | |
374 | tr[i].fAutoinc, tr[i].dwOptions); | |
375 | #ifdef DEBUG | |
376 | if (tr[i].cmdTrans == 10) | |
377 | DPRINTF("read byte: %d\n", tr[i].Data.Byte); | |
378 | #endif | |
379 | } | |
380 | ||
381 | return ret; | |
382 | } |