-
Notifications
You must be signed in to change notification settings - Fork 5
/
cable_parallel.c
546 lines (446 loc) · 13.9 KB
/
cable_parallel.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/* cable_parallel.c - Parallel cable drivers (XPC3 and XESS) for the Advanced JTAG Bridge
Copyright (C) 2001 Marko Mlinar, [email protected]
Copyright (C) 2004 Gy�rgy Jeney, [email protected]
UNIX parallel port control through device file added by:
Copyright (C) 2011 Raul Fajardo, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#ifdef __LINUX_HOST__
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/ppdev.h>
#include <linux/parport.h>
#endif
#ifdef __CYGWIN_HOST__
#include <sys/io.h> // for inb(), outb()
#endif
#ifdef __FREEBSD_HOST__
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/dev/ppbus/ppi.h>
#include <sys/dev/ppbus/ppbconf.h>
#endif
#include "cable_parallel.h"
#include "errcodes.h"
#ifdef __PARALLEL_TIMER_BUSY_WAIT__
#if ((_POSIX_TIMERS) && (_POSIX_CPUTIME))
#define PARALLEL_USE_PROCESS_TIMER
#endif
#endif
jtag_cable_t xpc3_cable_driver = {
.name = "xpc3",
.inout_func = cable_xpc3_inout,
.out_func = cable_xpc3_out,
.init_func = cable_parallel_init,
.opt_func = cable_parallel_opt,
.bit_out_func = cable_common_write_bit,
.bit_inout_func = cable_common_read_write_bit,
.stream_out_func = cable_common_write_stream,
.stream_inout_func = cable_common_read_stream,
.flush_func = NULL,
#ifdef __CYGWIN_HOST__
.opts = "p:",
.help = "-p [port] Which port to use when communicating with the parport hardware (eg. 0x378)\n"
#else
.opts = "d:",
.help = "-d [device file] Device file to use when communicating with the parport hardware (eg. /dev/parport0)\n"
#endif
};
jtag_cable_t bb2_cable_driver = {
.name = "bb2",
.inout_func = cable_bb2_inout,
.out_func = cable_bb2_out,
.init_func = cable_parallel_init,
.opt_func = cable_parallel_opt,
.bit_out_func = cable_common_write_bit,
.bit_inout_func = cable_common_read_write_bit,
.stream_out_func = cable_common_write_stream,
.stream_inout_func = cable_common_read_stream,
.flush_func = NULL,
#ifdef __CYGWIN_HOST__
.opts = "p:",
.help = "-p [port] Which port to use when communicating with the parport hardware (eg. 0x378)\n"
#else
.opts = "d:",
.help = "-d [device file] Device file to use when communicating with the parport hardware (eg. /dev/parport0)\n"
#endif
};
jtag_cable_t xess_cable_driver = {
.name = "xess",
.inout_func = cable_xess_inout,
.out_func = cable_xess_out,
.init_func = cable_parallel_init,
.opt_func = cable_parallel_opt,
.bit_out_func = cable_common_write_bit,
.bit_inout_func = cable_common_read_write_bit,
.stream_out_func = cable_common_write_stream,
.stream_inout_func = cable_common_read_stream,
.flush_func = NULL,
#ifdef __CYGWIN_HOST__
.opts = "p:",
.help = "-p [port] Which port to use when communicating with the parport hardware (eg. 0x378)\n"
#else
.opts = "d:",
.help = "-d [device file] Device file to use when communicating with the parport hardware (eg. /dev/parport0)\n"
#endif
};
// Common functions used by both cable types
static int cable_parallel_out(uint8_t value);
static int cable_parallel_inout(uint8_t value, uint8_t *inval);
#ifdef __PARALLEL_TIMER_BUSY_WAIT__
#ifndef PARALLEL_USE_PROCESS_TIMER
struct timeval last_tv;
#endif
#endif
// If cygwin, we use inb / outb for parallel port access
#ifdef __CYGWIN_HOST__
#define LPT_READ (base+1)
#define LPT_WRITE base
static int base = 0x378;
/////////////////////////////////////////////////////////////////////////////////
/*-------------------------------------[ Parallel port specific functions ]---*/
///////////////////////////////////////////////////////////////////////////////
int cable_parallel_init()
{
if (ioperm(base, 3, 1)) {
fprintf(stderr, "Couldn't get the port at %x\n", base);
perror("Root privileges are required.\n");
return APP_ERR_INIT_FAILED;
}
printf("Connected to parallel port at %x\n", base);
printf("Dropping root privileges.\n");
setreuid(getuid(), getuid());
#ifdef __PARALLEL_TIMER_BUSY_WAIT__
#ifdef PARALLEL_USE_PROCESS_TIMER
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 0;
clock_settime(CLOCK_PROCESS_CPUTIME_ID, &ts);
#else
gettimeofday(&last_tv, NULL);
#endif
#endif
return APP_ERR_NONE;
}
int cable_parallel_opt(int c, char *str)
{
switch(c) {
case 'p':
if(!sscanf(str, "%x", &base)) {
fprintf(stderr, "p parameter must have a hex number as parameter\n");
return APP_ERR_BAD_PARAM;
}
break;
default:
fprintf(stderr, "Unknown parameter '%c'\n", c);
return APP_ERR_BAD_PARAM;
}
return APP_ERR_NONE;
}
/*----------------------------------------------[ common helper functions ]---*/
// 'static' for internal access only
static int cable_parallel_out(uint8_t value)
{
outb(value, LPT_WRITE);
return APP_ERR_NONE;
}
static int cable_parallel_inout(uint8_t value, uint8_t *inval)
{
*inval = inb(LPT_READ);
outb(value, LPT_WRITE);
return APP_ERR_NONE;
}
// For Linux / BSD, we use open / ioctl for parallel port access,
// so that we don't need root permissions
#else // ! defined __CYGWIN_HOST__
#ifdef __FREEBSD_HOST__
static int PPORT_PUT_DATA = PPISDATA;
static int PPORT_GET_DATA = PPIGSTATUS;
#else
static int PPORT_PUT_DATA = PPWDATA;
static int PPORT_GET_DATA = PPRSTATUS;
#endif
static int fd;
static char default_dev_str[20] = "/dev/parport0";
static char *devsys = default_dev_str;
/////////////////////////////////////////////////////////////////////////////////
/*-------------------------------------[ Parallel port specific functions ]---*/
///////////////////////////////////////////////////////////////////////////////
int cable_parallel_init()
{
int mode = IEEE1284_MODE_COMPAT;
fd = open(devsys, O_RDWR | O_NONBLOCK);
if (fd == -1)
{
perror("Unable to open the device desriptor\n");
fprintf(stderr, "Check permission of %s (eg. 'ls -la %s').\n", devsys, devsys);
fprintf(stderr, "Your user ID can be added to %s's group to allow for unprivileged access.\n", devsys);
return APP_ERR_INIT_FAILED;
}
// I don't know if these ioctl() are supported under FreeBSD
#ifdef __LINUX_HOST__
if (ioctl(fd, PPCLAIM) == -1)
{
perror("Fail to claim the parallel port device interface.\n");
return APP_ERR_INIT_FAILED;
}
if (ioctl(fd, PPSETMODE, &mode) == -1)
{
perror("Setting compatibility mode on parallel port device failed.\n");
return APP_ERR_INIT_FAILED;
}
#endif
#ifdef __PARALLEL_TIMER_BUSY_WAIT__
#ifdef PARALLEL_USE_PROCESS_TIMER
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 0;
clock_settime(CLOCK_PROCESS_CPUTIME_ID, &ts);
#else
gettimeofday(&last_tv, NULL);
#endif
#endif
return APP_ERR_NONE;
}
int cable_parallel_opt(int c, char *str)
{
switch(c) {
case 'd':
devsys = strdup(str);
break;
default:
fprintf(stderr, "Unknown parameter '%c'\n", c);
return APP_ERR_BAD_PARAM;
}
return APP_ERR_NONE;
}
/*----------------------------------------------[ common helper functions ]---*/
// 'static' for internal access only
static int cable_parallel_out(uint8_t value)
{
ioctl(fd, PPORT_PUT_DATA, &value);
return APP_ERR_NONE;
}
static int cable_parallel_inout(uint8_t value, uint8_t *inval)
{
ioctl(fd, PPORT_GET_DATA, inval);
ioctl(fd, PPORT_PUT_DATA, &value);
return APP_ERR_NONE;
}
#endif // !defined __CYGWIN_HOST__
/*-----------------------------------------[ Physical board wait function ]---*/
/* Multiple users have reported poor performance of parallel cables,
* which has been traced to various sleep functions sleeping much longer than
* microseconds. The same users have reported error-free functionality
* and an order of magnitude improvement in upload speed with no wait.
* Other users have reported errors when running without a wait.
* Impact apparently limits the frequency of parallel JTAG cables
* to 200 kHz, and some clones fail at higher speeds.
*/
#ifdef __PARALLEL_SLEEP_WAIT__
void cable_parallel_phys_wait()
{
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 2500;
nanosleep(&ts, NULL);
}
#else
#ifdef __PARALLEL_TIMER_BUSY_WAIT__
#ifndef PARALLEL_USE_PROCESS_TIMER
/* Helper function needed if process timer isn't implemented */
/* do x-y */
int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
{
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
tv_usec is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
#endif
void cable_parallel_phys_wait()
{
/* This busy wait attempts to make the frequency exactly 200kHz,
* including the processing time between ticks.
* This means a period of 5us, or half a period of 2.5us.
*/
#ifdef PARALLEL_USE_PROCESS_TIMER
struct timespec ts;
do
{
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
} while((ts.tv_sec == 0) && (ts.tv_nsec < 2500));
/* Doing the set after the check means that processing time
* is not added to the wait. */
ts.tv_sec = 0;
ts.tv_nsec = 0;
clock_settime(CLOCK_PROCESS_CPUTIME_ID, &ts);
#else
struct timeval now_tv;
struct timeval results_tv;
do
{
gettimeofday(&now_tv, NULL);
timeval_subtract (&results_tv, &now_tv, &last_tv);
} while((results_tv.tv_sec == 0) && (results_tv.tv_usec < 3));
last_tv = now_tv;
#endif
}
#else // NO WAIT
void cable_parallel_phys_wait()
{
// No wait, run max speed
}
#endif
#endif
/*----------------------------------------------[ xpc3 specific functions ]---*/
jtag_cable_t *cable_xpc3_get_driver(void)
{
return &xpc3_cable_driver;
}
int cable_xpc3_out(uint8_t value)
{
uint8_t out = 0;
cable_parallel_phys_wait(); // Limit the max clock rate if necessary
/* First convert the bits in value byte to the ones that the cable wants */
if(value & TCLK_BIT)
out |= 0x02; /* D1 pin 3 */
if(value & TRST_BIT)
out |= 0x10; /* Not used */
if(value & TDI_BIT)
out |= 0x01; /* D0 pin 2 */
if(value & TMS_BIT)
out |= 0x04; /* D2 pin 4 */
return cable_parallel_out(out);
}
int cable_xpc3_inout(uint8_t value, uint8_t *inval)
{
uint8_t in;
int retval;
uint8_t out = 0;
cable_parallel_phys_wait(); // Limit the max clock rate if necessary
/* First convert the bits in value byte to the ones that the cable wants */
if(value & TCLK_BIT)
out |= 0x02; /* D1 pin 3 */
if(value & TRST_BIT)
out |= 0x10; /* Not used */
if(value & TDI_BIT)
out |= 0x01; /* D0 pin 2 */
if(value & TMS_BIT)
out |= 0x04; /* D2 pin 4 */
retval = cable_parallel_inout(out, &in);
if(in & 0x10) /* S6 pin 13 */
*inval = 1;
else
*inval = 0;
return retval;
}
/*----------------------------------------------[ bb2 specific functions ]---*/
jtag_cable_t *cable_bb2_get_driver(void)
{
return &bb2_cable_driver;
}
int cable_bb2_out(uint8_t value)
{
uint8_t out = 0;
cable_parallel_phys_wait(); // Limit the max clock rate if necessary
/* First convert the bits in value byte to the ones that the cable wants */
if(value & TCLK_BIT)
out |= 0x01; /* D0 pin 2 */
if(value & TDI_BIT)
out |= 0x40; /* D7 pin 8 */
if(value & TMS_BIT)
out |= 0x02; /* D1 pin 3 */
return cable_parallel_out(out);
}
int cable_bb2_inout(uint8_t value, uint8_t *inval)
{
uint8_t in;
int retval;
uint8_t out = 0;
cable_parallel_phys_wait(); // Limit the max clock rate if necessary
/* First convert the bits in value byte to the ones that the cable wants */
if(value & TCLK_BIT)
out |= 0x01; /* D0 pin 2 */
if(value & TDI_BIT)
out |= 0x40; /* D7 pin 8 */
if(value & TMS_BIT)
out |= 0x02; /* D1 pin 3 */
retval = cable_parallel_inout(out, &in);
if(in & 0x80) /* S7 pin 11 */
*inval = 0;
else
*inval = 1;
return retval;
}
/*----------------------------------------------[ xess specific functions ]---*/
jtag_cable_t *cable_xess_get_driver(void)
{
return &xess_cable_driver;
}
int cable_xess_out(uint8_t value)
{
uint8_t out = 0;
cable_parallel_phys_wait(); // Limit the max clock rate if necessary
/* First convert the bits in value byte to the ones that the cable wants */
if(value & TCLK_BIT)
out |= 0x04; /* D2 pin 4 */
if(value & TRST_BIT)
out |= 0x08; /* D3 pin 5 */
if(value & TDI_BIT)
out |= 0x10; /* D4 pin 6 */
if(value & TMS_BIT)
out |= 0x20; /* D3 pin 5 */
return cable_parallel_out(out);
}
int cable_xess_inout(uint8_t value, uint8_t *inval)
{
uint8_t in;
int retval;
uint8_t out = 0;
cable_parallel_phys_wait(); // Limit the max clock rate if necessary
/* First convert the bits in value byte to the ones that the cable wants */
if(value & TCLK_BIT)
out |= 0x04; /* D2 pin 4 */
if(value & TRST_BIT)
out |= 0x08; /* D3 pin 5 */
if(value & TDI_BIT)
out |= 0x10; /* D4 pin 6 */
if(value & TMS_BIT)
out |= 0x20; /* D3 pin 5 */
retval = cable_parallel_inout(out, &in);
if(in & 0x20) /* S5 pin 12*/
*inval = 1;
else
*inval = 0;
return retval;
}