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