-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjtagkey.c
374 lines (295 loc) · 8.52 KB
/
jtagkey.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <stdio.h>
#include <ftdi.h>
#include <unistd.h>
#include <pthread.h>
#include <inttypes.h>
#include "usb-driver.h"
#include "config.h"
#include "jtagkey.h"
#include "jtagmon.h"
#define USBBUFSIZE 1048576
#define JTAG_SPEED 100000
#define BULK_LATENCY 2
#define OTHER_LATENCY 1
static struct ftdi_context ftdic;
static int jtagkey_latency(int latency) {
static int current = 0;
int ret;
if (current != latency) {
DPRINTF("switching latency\n");
if ((ret = ftdi_set_latency_timer(&ftdic, latency)) != 0) {
fprintf(stderr, "unable to set latency timer: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
return ret;
}
current = latency;
}
return ret;
}
static int jtagkey_init(unsigned short vid, unsigned short pid, unsigned short iface) {
int ret = 0;
unsigned char c;
if ((ret = ftdi_init(&ftdic)) != 0) {
fprintf(stderr, "unable to initialise libftdi: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
return ret;
}
if ((ret = ftdi_set_interface(&ftdic, iface)) != 0) {
fprintf(stderr, "unable to set interface: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
return ret;
}
if ((ret = ftdi_usb_open(&ftdic, vid, pid)) != 0) {
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
return ret;
}
if ((ret = ftdi_usb_reset(&ftdic)) != 0) {
fprintf(stderr, "unable reset device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
return ret;
}
if ((ret = ftdi_write_data_set_chunksize(&ftdic, USBBUFSIZE)) != 0) {
fprintf(stderr, "unable to set write chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
return ret;
}
if ((ret = ftdi_read_data_set_chunksize(&ftdic, USBBUFSIZE)) != 0) {
fprintf(stderr, "unable to set read chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
return ret;
}
if ((ret = jtagkey_latency(OTHER_LATENCY)) != 0)
return ret;
c = 0x00;
ftdi_write_data(&ftdic, &c, 1);
if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_SYNCBB)) != 0) {
fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
return ret;
}
if ((ret = ftdi_set_baudrate(&ftdic, JTAG_SPEED)) != 0) {
fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
return ret;
}
if ((ret = ftdi_usb_purge_buffers(&ftdic)) != 0) {
fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
return ret;
}
return ret;
}
int jtagkey_open(int num) {
int ret;
ret = jtagkey_init(config_usb_vid(num), config_usb_pid(num), config_usb_iface(num));
if (ret >= 0)
ret = 0xff;
return ret;
}
void jtagkey_close(int handle) {
if (handle == 0xff) {
ftdi_disable_bitbang(&ftdic);
ftdi_usb_close(&ftdic);
ftdi_deinit(&ftdic);
}
}
#ifdef DEBUG
static void jtagkey_state(unsigned char data) {
fprintf(stderr,"Pins high: ");
if (data & JTAGKEY_TCK)
fprintf(stderr,"TCK ");
if (data & JTAGKEY_TDI)
fprintf(stderr,"TDI ");
if (data & JTAGKEY_TDO)
fprintf(stderr,"TDO ");
if (data & JTAGKEY_TMS)
fprintf(stderr,"TMS ");
if (data & JTAGKEY_VREF)
fprintf(stderr,"VREF ");
fprintf(stderr,"\n");
}
#endif
struct jtagkey_reader_arg {
int num;
unsigned char *buf;
};
static void *jtagkey_reader(void *thread_arg) {
struct jtagkey_reader_arg *arg = (struct jtagkey_reader_arg*)thread_arg;
int i;
i = 0;
DPRINTF("reader for %d bytes\n", arg->num);
while (i < arg->num) {
i += ftdi_read_data(&ftdic, arg->buf + i, arg->num - i);
}
pthread_exit(NULL);
}
/* TODO: Interpret JTAG commands and transfer in MPSSE mode */
int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) {
int ret = 0;
int i;
int nread = 0;
unsigned long port;
unsigned char val;
static unsigned char last_data = 0;
static unsigned char last_write = 0x00;
static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
static unsigned char readbuf[USBBUFSIZE], *readpos;
unsigned char data, prev_data, last_cyc_write;
struct jtagkey_reader_arg targ;
pthread_t reader_thread;
/* Count reads */
for (i = 0; i < num; i++)
if (tr[i].cmdTrans == PP_READ)
nread++;
/* Write combining */
if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) {
unsigned char *pos = writebuf;
int len;
DPRINTF("writing %zd bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num);
jtagkey_latency(BULK_LATENCY);
targ.num = writepos-pos;
targ.buf = readbuf;
pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
while (pos < writepos) {
len = writepos-pos;
if (len > USBBUFSIZE)
len = USBBUFSIZE;
DPRINTF("combined write of %d/%zd\n",len,writepos-pos);
ftdi_write_data(&ftdic, pos, len);
pos += len;
}
pthread_join(reader_thread, NULL);
writepos = writebuf;
}
last_cyc_write = last_write;
for (i = 0; i < num; i++) {
DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
(unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
tr[i].fAutoinc, tr[i].dwOptions);
port = (unsigned long)tr[i].dwPort;
val = tr[i].Data.Byte;
#ifdef DEBUG
if (tr[i].cmdTrans == 13)
DPRINTF("write byte: %d\n", val);
if (tr[i].cmdTrans == 13)
jtagmon(val & PP_TCK, val & PP_TMS, val & PP_TDI);
#endif
/* Pad writebuf for read-commands in stream */
*writepos = last_data;
prev_data = last_data;
if (port == ppbase + PP_DATA) {
DPRINTF("data port\n");
data = 0x00;
switch(tr[i].cmdTrans) {
case PP_READ:
ret = 0; /* We don't support reading of the data port */
break;
case PP_WRITE:
if (val & PP_TDI) {
data |= JTAGKEY_TDI;
DPRINTF("TDI\n");
} else {
DPRINTF("!TDI\n");
}
if (val & PP_TCK) {
data |= JTAGKEY_TCK;
DPRINTF("TCK\n");
} else {
DPRINTF("!TCK\n");
}
if (val & PP_TMS) {
data |= JTAGKEY_TMS;
DPRINTF("TMS\n");
} else {
DPRINTF("!TMS\n");
}
if (val & PP_CTRL) {
data = JTAGKEY_OEn;
DPRINTF("CTRL\n");
} else {
DPRINTF("!CTRL\n");
}
if (val & PP_PROG) {
DPRINTF("PROG\n");
} else {
DPRINTF("!PROG\n");
}
*writepos = data;
last_data = data;
last_write = val;
break;
default:
fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans);
ret = -1;
break;
}
}
if ((tr[i].cmdTrans == PP_READ) || (*writepos != prev_data) || (i == num-1))
writepos++;
}
if (nread)
{
DPRINTF("writing %zd bytes\n", writepos-writebuf);
*writepos = last_data;
writepos++;
jtagkey_latency(OTHER_LATENCY);
targ.num = writepos-writebuf;
targ.buf = readbuf;
pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
pthread_join(reader_thread, NULL);
#ifdef DEBUG
hexdump(writebuf, writepos-writebuf, "->");
hexdump(readbuf, i, "<-");
#endif
writepos = writebuf;
} else {
return ret;
}
readpos = readbuf;
last_write = last_cyc_write;
for (i = 0; i < num; i++) {
DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
(unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
tr[i].fAutoinc, tr[i].dwOptions);
port = (unsigned long)tr[i].dwPort;
val = tr[i].Data.Byte;
if ((tr[i].cmdTrans != PP_READ) && (val == last_write) && (i != num-1))
continue;
readpos++;
if (port == ppbase + PP_DATA) {
if (tr[i].cmdTrans == PP_WRITE) {
last_write = val;
}
} else if (port == ppbase + PP_STATUS) {
DPRINTF("status port (last write: 0x%x)\n", last_write);
switch(tr[i].cmdTrans) {
case PP_READ:
data = *readpos;
#ifdef DEBUG
DPRINTF("READ: 0x%x\n", data);
jtagkey_state(data);
#endif
val = 0x00;
if ((data & JTAGKEY_TDO) && (last_write & PP_PROG))
val |= PP_TDO;
if (~last_write & PP_PROG)
val |= 0x08;
if (last_write & 0x40)
val |= 0x20;
else
val |= 0x80;
break;
case PP_WRITE:
ret = 0; /* Status Port is readonly */
break;
default:
fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr[i].cmdTrans);
ret = -1;
break;
}
} else {
ret = 0;
}
tr[i].Data.Byte = val;
DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
(unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
tr[i].fAutoinc, tr[i].dwOptions);
#ifdef DEBUG
if (tr[i].cmdTrans == 10)
DPRINTF("read byte: %d\n", tr[i].Data.Byte);
#endif
}
return ret;
}