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