]>
git.zerfleddert.de Git - usb-driver/blob - jtagkey.c
ffae2fa1a99898f997114ce1e3da666500c43a93
5 #include "usb-driver.h"
9 #define USBBUFSIZE 1048576
10 #define JTAG_SPEED 100000
11 #define BULK_LATENCY 2
12 #define OTHER_LATENCY 1
14 static struct ftdi_context ftdic
;
16 static int jtagkey_latency(int latency
) {
17 static int current
= 0;
20 if (current
!= latency
) {
21 DPRINTF("switching latency\n");
22 if ((ret
= ftdi_set_latency_timer(&ftdic
, latency
)) != 0) {
23 fprintf(stderr
, "unable to set latency timer: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
33 static int jtagkey_init(unsigned short vid
, unsigned short pid
) {
37 if ((ret
= ftdi_init(&ftdic
)) != 0) {
38 fprintf(stderr
, "unable to initialise libftdi: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
42 if ((ret
= ftdi_usb_open(&ftdic
, vid
, pid
)) != 0) {
43 fprintf(stderr
, "unable to open ftdi device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
47 if ((ret
= ftdi_usb_reset(&ftdic
)) != 0) {
48 fprintf(stderr
, "unable reset device: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
52 if ((ret
= ftdi_set_interface(&ftdic
, INTERFACE_A
)) != 0) {
53 fprintf(stderr
, "unable to set interface: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
57 if ((ret
= ftdi_write_data_set_chunksize(&ftdic
, USBBUFSIZE
)) != 0) {
58 fprintf(stderr
, "unable to set write chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
62 if ((ret
= ftdi_read_data_set_chunksize(&ftdic
, USBBUFSIZE
)) != 0) {
63 fprintf(stderr
, "unable to set read chunksize: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
67 if ((ret
= jtagkey_latency(OTHER_LATENCY
)) != 0)
71 ftdi_write_data(&ftdic
, &c
, 1);
73 if ((ret
= ftdi_set_bitmode(&ftdic
, JTAGKEY_TCK
|JTAGKEY_TDI
|JTAGKEY_TMS
|JTAGKEY_OEn
, BITMODE_SYNCBB
)) != 0) {
74 fprintf(stderr
, "unable to enable bitbang mode: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
78 if ((ret
= ftdi_set_baudrate(&ftdic
, JTAG_SPEED
)) != 0) {
79 fprintf(stderr
, "unable to set baudrate: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
83 if ((ret
= ftdi_usb_purge_buffers(&ftdic
)) != 0) {
84 fprintf(stderr
, "unable to purge buffers: %d (%s)\n", ret
, ftdi_get_error_string(&ftdic
));
91 int jtagkey_open(int num
) {
94 ret
= jtagkey_init(config_usb_vid(num
), config_usb_pid(num
));
102 void jtagkey_close(int handle
) {
103 if (handle
== 0xff) {
104 ftdi_disable_bitbang(&ftdic
);
105 ftdi_usb_close(&ftdic
);
110 void jtagkey_state(unsigned char data
) {
111 fprintf(stderr
,"Pins high: ");
113 if (data
& JTAGKEY_TCK
)
114 fprintf(stderr
,"TCK ");
116 if (data
& JTAGKEY_TDI
)
117 fprintf(stderr
,"TDI ");
119 if (data
& JTAGKEY_TDO
)
120 fprintf(stderr
,"TDO ");
122 if (data
& JTAGKEY_TMS
)
123 fprintf(stderr
,"TMS ");
125 if (data
& JTAGKEY_VREF
)
126 fprintf(stderr
,"VREF ");
128 fprintf(stderr
,"\n");
131 struct jtagkey_reader_arg
{
136 static void *jtagkey_reader(void *thread_arg
) {
137 struct jtagkey_reader_arg
*arg
= (struct jtagkey_reader_arg
*)thread_arg
;
141 DPRINTF("reader for %d bytes\n", arg
->num
);
142 while (i
< arg
->num
) {
143 i
+= ftdi_read_data(&ftdic
, arg
->buf
+ i
, arg
->num
- i
);
149 /* TODO: Interpret JTAG commands and transfer in MPSSE mode */
150 int jtagkey_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, int ppbase
, int ecpbase
, int num
) {
156 static unsigned char last_data
= 0;
157 static unsigned char last_write
= 0x00;
158 static unsigned char writebuf
[USBBUFSIZE
], *writepos
= writebuf
;
159 static unsigned char readbuf
[USBBUFSIZE
], *readpos
;
160 unsigned char data
, prev_data
, last_cyc_write
;
161 struct jtagkey_reader_arg targ
;
162 pthread_t reader_thread
;
165 for (i
= 0; i
< num
; i
++)
166 if (tr
[i
].cmdTrans
== PP_READ
)
169 /* Write combining */
170 if ((writepos
-writebuf
> sizeof(writebuf
)-num
) || (nread
&& writepos
-writebuf
)) {
171 unsigned char *pos
= writebuf
;
174 DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos
-writebuf
, nread
, num
);
175 jtagkey_latency(BULK_LATENCY
);
177 targ
.num
= writepos
-pos
;
179 pthread_create(&reader_thread
, NULL
, &jtagkey_reader
, &targ
);
181 while (pos
< writepos
) {
184 if (len
> USBBUFSIZE
)
187 DPRINTF("combined write of %d/%d\n",len
,writepos
-pos
);
188 ftdi_write_data(&ftdic
, pos
, len
);
191 pthread_join(reader_thread
, NULL
);
196 last_cyc_write
= last_write
;
198 for (i
= 0; i
< num
; i
++) {
199 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
200 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
201 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
203 port
= (unsigned long)tr
[i
].dwPort
;
204 val
= tr
[i
].Data
.Byte
;
207 if (tr
[i
].cmdTrans
== 13)
208 DPRINTF("write byte: %d\n", val
);
211 /* Pad writebuf for read-commands in stream */
212 *writepos
= last_data
;
213 prev_data
= last_data
;
215 if (port
== ppbase
+ PP_DATA
) {
216 DPRINTF("data port\n");
219 switch(tr
[i
].cmdTrans
) {
221 ret
= 0; /* We don't support reading of the data port */
263 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
269 if ((tr
[i
].cmdTrans
== PP_READ
) || (*writepos
!= prev_data
) || (i
== num
-1))
275 DPRINTF("writing %d bytes\n", writepos
-writebuf
);
277 *writepos
= last_data
;
280 jtagkey_latency(OTHER_LATENCY
);
282 targ
.num
= writepos
-writebuf
;
284 pthread_create(&reader_thread
, NULL
, &jtagkey_reader
, &targ
);
285 ftdi_write_data(&ftdic
, writebuf
, writepos
-writebuf
);
286 pthread_join(reader_thread
, NULL
);
290 hexdump(writebuf
, writepos
-writebuf
);
301 last_write
= last_cyc_write
;
303 for (i
= 0; i
< num
; i
++) {
304 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
305 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
306 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
308 port
= (unsigned long)tr
[i
].dwPort
;
309 val
= tr
[i
].Data
.Byte
;
311 if ((tr
[i
].cmdTrans
!= PP_READ
) && (val
== last_write
) && (i
!= num
-1))
316 if (port
== ppbase
+ PP_DATA
) {
317 if (tr
[i
].cmdTrans
== PP_WRITE
) {
320 } else if (port
== ppbase
+ PP_STATUS
) {
321 DPRINTF("status port (last write: 0x%x)\n", last_write
);
322 switch(tr
[i
].cmdTrans
) {
327 DPRINTF("READ: 0x%x\n", data
);
332 if ((data
& JTAGKEY_TDO
) && (last_write
& PP_PROG
))
335 if (~last_write
& PP_PROG
)
338 if (last_write
& 0x40)
345 ret
= 0; /* Status Port is readonly */
349 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
[i
].cmdTrans
);
357 tr
[i
].Data
.Byte
= val
;
359 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
360 (unsigned long)tr
[i
].dwPort
, tr
[i
].cmdTrans
, tr
[i
].dwBytes
,
361 tr
[i
].fAutoinc
, tr
[i
].dwOptions
);
363 if (tr
[i
].cmdTrans
== 10)
364 DPRINTF("read byte: %d\n", tr
[i
].Data
.Byte
);