]>
Commit | Line | Data |
---|---|---|
8121bc50 | 1 | #include <stdio.h> |
2 | #include <ftdi.h> | |
9640dec9 | 3 | #include <unistd.h> |
8121bc50 | 4 | #include "usb-driver.h" |
5e3d963b | 5 | #include "config.h" |
8121bc50 | 6 | #include "jtagkey.h" |
7 | ||
4af4753d | 8 | #define USBBUFSIZE 4096 |
d0e2002d | 9 | |
8121bc50 | 10 | static struct ftdi_context ftdic; |
4af4753d | 11 | static unsigned int usb_maxlen = 0; |
9640dec9 | 12 | static unsigned char bitbang_mode; |
8121bc50 | 13 | |
5e3d963b | 14 | static int jtagkey_init(unsigned short vid, unsigned short pid) { |
8121bc50 | 15 | int ret = 0; |
d0e2002d | 16 | unsigned char c; |
8121bc50 | 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 | ||
4af4753d | 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)); | |
8121bc50 | 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 | ||
d0e2002d | 49 | if ((ret = ftdi_set_baudrate(&ftdic, 500000)) != 0) { |
8121bc50 | 50 | fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); |
51 | return ret; | |
52 | } | |
53 | ||
d0e2002d | 54 | c = 0x00; |
55 | ftdi_write_data(&ftdic, &c, 1); | |
56 | ||
4af4753d | 57 | if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_BITBANG)) != 0) { |
8121bc50 | 58 | fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); |
59 | return ret; | |
60 | } | |
61 | ||
9640dec9 | 62 | bitbang_mode = BITMODE_BITBANG; |
63 | ||
8121bc50 | 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 | ||
5e3d963b | 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 | } | |
8121bc50 | 89 | } |
90 | ||
9640dec9 | 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"); | |
59b06a85 | 96 | |
9640dec9 | 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 | ||
8121bc50 | 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; | |
d0e2002d | 140 | int nread = 0; |
8121bc50 | 141 | unsigned long port; |
142 | unsigned char val; | |
d0e2002d | 143 | static unsigned char last_data = 0; |
e81047b8 | 144 | static unsigned char last_write = 0x00; |
d0e2002d | 145 | static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf; |
146 | static unsigned char readbuf[USBBUFSIZE], *readpos; | |
e81047b8 | 147 | unsigned char data, prev_data; |
d0e2002d | 148 | |
e81047b8 | 149 | /* Count reads */ |
d0e2002d | 150 | for (i = 0; i < num; i++) |
e81047b8 | 151 | if (tr[i].cmdTrans == PP_READ) |
d0e2002d | 152 | nread++; |
153 | ||
154 | /* Write combining */ | |
155 | if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) { | |
4af4753d | 156 | unsigned char *pos = writebuf; |
157 | int len; | |
d0e2002d | 158 | DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num); |
159 | ||
9640dec9 | 160 | jtagkey_set_bbmode(BITMODE_BITBANG); |
4af4753d | 161 | while (pos < writepos) { |
85690a3f | 162 | len = writepos-pos; |
163 | ||
4af4753d | 164 | if (len > usb_maxlen) |
165 | len = usb_maxlen; | |
d0e2002d | 166 | |
4af4753d | 167 | DPRINTF("combined write of %d/%d\n",len,writepos-pos); |
168 | ftdi_write_data(&ftdic, pos, len); | |
169 | pos += len; | |
e81047b8 | 170 | } |
4af4753d | 171 | |
d0e2002d | 172 | writepos = writebuf; |
173 | } | |
8121bc50 | 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 | ||
b122ac4b | 188 | /* Pad writebuf for read-commands in stream */ |
d0e2002d | 189 | *writepos = last_data; |
e81047b8 | 190 | prev_data = last_data; |
b122ac4b | 191 | |
8121bc50 | 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) { | |
d0e2002d | 221 | data = JTAGKEY_OEn; |
8121bc50 | 222 | DPRINTF("CTRL\n"); |
223 | } else { | |
224 | DPRINTF("!CTRL\n"); | |
225 | } | |
8121bc50 | 226 | |
d0e2002d | 227 | if (val & PP_PROG) { |
228 | DPRINTF("PROG\n"); | |
229 | } else { | |
230 | DPRINTF("!PROG\n"); | |
231 | } | |
232 | ||
233 | *writepos = data; | |
234 | ||
b122ac4b | 235 | last_data = data; |
d0e2002d | 236 | last_write = val; |
8121bc50 | 237 | break; |
238 | ||
239 | default: | |
240 | fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans); | |
241 | ret = -1; | |
242 | break; | |
243 | } | |
b122ac4b | 244 | } |
e81047b8 | 245 | |
246 | if (nread || (*writepos != prev_data)) | |
247 | writepos++; | |
b122ac4b | 248 | } |
249 | ||
d0e2002d | 250 | if (nread) |
251 | { | |
252 | DPRINTF("writing %d bytes\n", writepos-writebuf); | |
59b06a85 | 253 | |
254 | *writepos = last_data; | |
255 | writepos++; | |
256 | ||
9640dec9 | 257 | jtagkey_set_bbmode(BITMODE_SYNCBB); |
258 | ftdi_write_data(&ftdic, writebuf, writepos-writebuf); | |
3f5287b7 | 259 | |
9640dec9 | 260 | i = 0; |
261 | while (i < writepos-writebuf) { | |
262 | i += ftdi_read_data(&ftdic, readbuf, sizeof(readbuf)); | |
d0e2002d | 263 | } |
b122ac4b | 264 | |
265 | #ifdef DEBUG | |
d0e2002d | 266 | DPRINTF("write: "); |
267 | hexdump(writebuf, writepos-writebuf); | |
268 | DPRINTF("read: "); | |
269 | hexdump(readbuf, i); | |
b122ac4b | 270 | #endif |
271 | ||
d0e2002d | 272 | writepos = writebuf; |
273 | } else { | |
274 | return ret; | |
275 | } | |
276 | ||
277 | readpos = readbuf; | |
b122ac4b | 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; | |
d0e2002d | 286 | readpos++; |
b122ac4b | 287 | |
d0e2002d | 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); | |
8121bc50 | 294 | switch(tr[i].cmdTrans) { |
295 | case PP_READ: | |
d0e2002d | 296 | data = *readpos; |
59b06a85 | 297 | |
8121bc50 | 298 | #ifdef DEBUG |
299 | DPRINTF("READ: 0x%x\n", data); | |
300 | jtagkey_state(data); | |
301 | #endif | |
302 | ||
303 | val = 0x00; | |
d0e2002d | 304 | if ((data & JTAGKEY_TDO) && (last_write & PP_PROG)) |
8121bc50 | 305 | val |= PP_TDO; |
306 | ||
d0e2002d | 307 | if (!(last_write & PP_PROG)) |
308 | val |= 0x08; | |
309 | ||
8121bc50 | 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 | } | |
8121bc50 | 325 | } else { |
8121bc50 | 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 | } |