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