]>
cvs.zerfleddert.de Git - usb-driver/blob - jtagkey.c
5 #include "usb-driver.h"
10 #define USBBUFSIZE 1048576
11 #define JTAG_SPEED 100000
12 #define BULK_LATENCY 2
13 #define OTHER_LATENCY 1
15 static struct ftdi_context ftdic
;
17 static int jtagkey_latency(int latency
) {
18 static int current
= 0;
21 if (current
!= latency
) {
22 DPRINTF("switching latency\n");
23 if ((ret
= ftdi_set_latency_timer(&ftdic
, latency
)) != 0) {
24 fprintf(stderr
, "unable to set latency timer: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
34 static int jtagkey_init(unsigned short vid
, unsigned short pid
) {
38 if ((ret
= ftdi_init(&ftdic
)) != 0) {
39 fprintf(stderr
, "unable to initialise libftdi: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
43 if ((ret
= ftdi_usb_open(&ftdic
, vid
, pid
)) != 0) {
44 fprintf(stderr
, "unable to open ftdi device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
48 if ((ret
= ftdi_usb_reset(&ftdic
)) != 0) {
49 fprintf(stderr
, "unable reset device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
53 if ((ret
= ftdi_set_interface(&ftdic
, INTERFACE_A
)) != 0) {
54 fprintf(stderr
, "unable to set interface: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
58 if ((ret
= ftdi_write_data_set_chunksize(&ftdic
, USBBUFSIZE
)) != 0) {
59 fprintf(stderr
, "unable to set write chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
63 if ((ret
= ftdi_read_data_set_chunksize(&ftdic
, USBBUFSIZE
)) != 0) {
64 fprintf(stderr
, "unable to set read chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
68 if ((ret
= jtagkey_latency(OTHER_LATENCY
)) != 0)
72 ftdi_write_data(&ftdic
, &c
, 1);
74 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, BITMODE_SYNCBB
)) != 0) {
75 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
79 if ((ret
= ftdi_set_baudrate(&ftdic
, JTAG_SPEED
)) != 0) {
80 fprintf(stderr
, "unable to set baudrate: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
84 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
85 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
92 int jtagkey_open(int num
) {
95 ret
= jtagkey_init(config_usb_vid(num
), config_usb_pid(num
));
103 void jtagkey_close(int handle
) {
104 if (handle
== 0xff) {
105 ftdi_disable_bitbang(&ftdic
);
106 ftdi_usb_close(&ftdic
);
111 void jtagkey_state(unsigned char data
) {
112 fprintf(stderr
,"Pins high: ");
114 if (data
& JTAGKEY_TCK
)
115 fprintf(stderr
,"TCK ");
117 if (data
& JTAGKEY_TDI
)
118 fprintf(stderr
,"TDI ");
120 if (data
& JTAGKEY_TDO
)
121 fprintf(stderr
,"TDO ");
123 if (data
& JTAGKEY_TMS
)
124 fprintf(stderr
,"TMS ");
126 if (data
& JTAGKEY_VREF
)
127 fprintf(stderr
,"VREF ");
129 fprintf(stderr
,"\n");
132 struct jtagkey_reader_arg
{
137 static void *jtagkey_reader(void *thread_arg
) {
138 struct jtagkey_reader_arg
*arg
= (struct jtagkey_reader_arg
*)thread_arg
;
142 DPRINTF("reader for %d bytes\n", arg
->num
);
143 while (i
< arg
->num
) {
144 i
+= ftdi_read_data(&ftdic
, arg
->buf
+ i
, arg
->num
- i
);
150 /* TODO: Interpret JTAG commands and transfer in MPSSE mode */
151 int jtagkey_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, int ppbase
, int ecpbase
, int num
) {
157 static unsigned char last_data
= 0;
158 static unsigned char last_write
= 0x00;
159 static unsigned char writebuf
[USBBUFSIZE
], *writepos
= writebuf
;
160 static unsigned char readbuf
[USBBUFSIZE
], *readpos
;
161 unsigned char data
, prev_data
, last_cyc_write
;
162 struct jtagkey_reader_arg targ
;
163 pthread_t reader_thread
;
166 for (i
= 0; i
< num
; i
++)
167 if (tr
[i
].cmdTrans
== PP_READ
)
170 /* Write combining */
171 if ((writepos
-writebuf
> sizeof(writebuf
)-num
) || (nread
&& writepos
-writebuf
)) {
172 unsigned char *pos
= writebuf
;
175 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos
-writebuf
, nread
, num
);
176 jtagkey_latency(BULK_LATENCY
);
178 targ
.num
= writepos
-pos
;
180 pthread_create(&reader_thread
, NULL
, &jtagkey_reader
, &targ
);
182 while (pos
< writepos
) {
185 if (len
> USBBUFSIZE
)
188 DPRINTF("combined write of %d/%d\n",len
,writepos
-pos
);
189 ftdi_write_data(&ftdic
, pos
, len
);
192 pthread_join(reader_thread
, NULL
);
197 last_cyc_write
= last_write
;
199 for (i
= 0; i
< num
; i
++) {
200 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
201 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
202 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
204 port
= (unsigned long)tr
[i
].dwPort
;
205 val
= tr
[i
].Data
.Byte
;
208 if (tr
[i
].cmdTrans
== 13)
209 DPRINTF("write byte: %d\n", val
);
211 if (tr
[i
].cmdTrans
== 13)
212 jtagmon(val
& PP_TCK
, val
& PP_TMS
, val
& PP_TDI
);
215 /* Pad writebuf for read-commands in stream */
216 *writepos
= last_data
;
217 prev_data
= last_data
;
219 if (port
== ppbase
+ PP_DATA
) {
220 DPRINTF("data port\n");
223 switch(tr
[i
].cmdTrans
) {
225 ret
= 0; /* We don't support reading of the data port */
267 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
273 if ((tr
[i
].cmdTrans
== PP_READ
) || (*writepos
!= prev_data
) || (i
== num
-1))
279 DPRINTF("writing %d bytes\n", writepos
-writebuf
);
281 *writepos
= last_data
;
284 jtagkey_latency(OTHER_LATENCY
);
286 targ
.num
= writepos
-writebuf
;
288 pthread_create(&reader_thread
, NULL
, &jtagkey_reader
, &targ
);
289 ftdi_write_data(&ftdic
, writebuf
, writepos
-writebuf
);
290 pthread_join(reader_thread
, NULL
);
294 hexdump(writebuf
, writepos
-writebuf
);
305 last_write
= last_cyc_write
;
307 for (i
= 0; i
< num
; i
++) {
308 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
309 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
310 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
312 port
= (unsigned long)tr
[i
].dwPort
;
313 val
= tr
[i
].Data
.Byte
;
315 if ((tr
[i
].cmdTrans
!= PP_READ
) && (val
== last_write
) && (i
!= num
-1))
320 if (port
== ppbase
+ PP_DATA
) {
321 if (tr
[i
].cmdTrans
== PP_WRITE
) {
324 } else if (port
== ppbase
+ PP_STATUS
) {
325 DPRINTF("status port (last write: 0x%x)\n", last_write
);
326 switch(tr
[i
].cmdTrans
) {
331 DPRINTF("READ: 0x%x\n", data
);
336 if ((data
& JTAGKEY_TDO
) && (last_write
& PP_PROG
))
339 if (~last_write
& PP_PROG
)
342 if (last_write
& 0x40)
349 ret
= 0; /* Status Port is readonly */
353 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
361 tr
[i
].Data
.Byte
= val
;
363 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
364 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
365 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
367 if (tr
[i
].cmdTrans
== 10)
368 DPRINTF("read byte: %d\n", tr
[i
].Data
.Byte
);