1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2008 by Spencer Oliver *
8 * spen@spen-soft.co.uk *
9 ***************************************************************************/
15 #include <jtag/adapter.h>
16 #include <jtag/interface.h>
19 /* -ino: 060521-1036 */
20 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
21 #include <machine/sysarch.h>
22 #include <machine/cpufunc.h>
23 #define ioperm(startport, length, enable)\
24 i386_set_ioperm((startport), (length), (enable))
25 #endif /* __FreeBSD__ */
27 #if PARPORT_USE_PPDEV == 1
28 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
29 #include <dev/ppbus/ppi.h>
30 #include <dev/ppbus/ppbconf.h>
31 #define PPRSTATUS PPIGSTATUS
32 #define PPWDATA PPISDATA
34 #include <linux/parport.h>
35 #include <linux/ppdev.h>
37 #include <sys/ioctl.h>
38 #else /* not PARPORT_USE_PPDEV */
44 #if PARPORT_USE_GIVEIO == 1 && IS_CYGWIN == 1
48 /* parallel port cable description
52 uint8_t TDO_MASK
; /* status port bit containing current TDO value */
53 uint8_t TRST_MASK
; /* data port bit for TRST */
54 uint8_t TMS_MASK
; /* data port bit for TMS */
55 uint8_t TCK_MASK
; /* data port bit for TCK */
56 uint8_t TDI_MASK
; /* data port bit for TDI */
57 uint8_t SRST_MASK
; /* data port bit for SRST */
58 uint8_t OUTPUT_INVERT
; /* data port bits that should be inverted */
59 uint8_t INPUT_INVERT
; /* status port that should be inverted */
60 uint8_t PORT_INIT
; /* initialize data port with this value */
61 uint8_t PORT_EXIT
; /* de-initialize data port with this value */
62 uint8_t LED_MASK
; /* data port bit for LED */
65 static const struct cable cables
[] = {
66 /* name tdo trst tms tck tdi srst o_inv i_inv init exit led */
67 { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 },
68 { "wiggler2", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 },
69 { "wiggler_ntrst_inverted", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80, 0x80, 0x00 },
70 { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 },
71 { "arm-jtag", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x01, 0x80, 0x80, 0x80, 0x00 },
72 { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
73 { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 },
74 { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
75 { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 },
76 { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 },
77 /* Altium Universal JTAG cable. Set the cable to Xilinx Mode and wire to target as follows:
82 SOFT TCK - Target TRST
83 SOFT TDI - Target SRST
85 { "altium", 0x10, 0x20, 0x04, 0x02, 0x01, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08 },
86 { "aspo", 0x10, 0x01, 0x04, 0x08, 0x02, 0x10, 0x17, 0x00, 0x17, 0x17, 0x00 },
87 { NULL
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
91 static char *parport_cable
;
92 static uint16_t parport_port
;
93 static bool parport_exit
;
94 static uint32_t parport_toggling_time_ns
= 1000;
95 static int wait_states
;
97 /* interface variables
99 static const struct cable
*cable
;
100 static uint8_t dataport_value
;
102 #if PARPORT_USE_PPDEV == 1
103 static int device_handle
;
105 static unsigned long dataport
;
106 static unsigned long statusport
;
109 static bb_value_t
parport_read(void)
113 #if PARPORT_USE_PPDEV == 1
114 ioctl(device_handle
, PPRSTATUS
, &data
);
116 data
= inb(statusport
);
119 if ((data
^ cable
->INPUT_INVERT
) & cable
->TDO_MASK
)
125 static inline void parport_write_data(void)
128 output
= dataport_value
^ cable
->OUTPUT_INVERT
;
130 #if PARPORT_USE_PPDEV == 1
131 ioctl(device_handle
, PPWDATA
, &output
);
133 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
134 outb(dataport
, output
);
136 outb(output
, dataport
);
141 static int parport_write(int tck
, int tms
, int tdi
)
143 int i
= wait_states
+ 1;
146 dataport_value
|= cable
->TCK_MASK
;
148 dataport_value
&= ~cable
->TCK_MASK
;
151 dataport_value
|= cable
->TMS_MASK
;
153 dataport_value
&= ~cable
->TMS_MASK
;
156 dataport_value
|= cable
->TDI_MASK
;
158 dataport_value
&= ~cable
->TDI_MASK
;
161 parport_write_data();
166 /* (1) assert or (0) deassert reset lines */
167 static int parport_reset(int trst
, int srst
)
169 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
172 dataport_value
|= cable
->TRST_MASK
;
174 dataport_value
&= ~cable
->TRST_MASK
;
177 dataport_value
|= cable
->SRST_MASK
;
179 dataport_value
&= ~cable
->SRST_MASK
;
181 parport_write_data();
186 /* turn LED on parport adapter on (1) or off (0) */
187 static int parport_led(int on
)
190 dataport_value
|= cable
->LED_MASK
;
192 dataport_value
&= ~cable
->LED_MASK
;
194 parport_write_data();
199 static int parport_speed(int speed
)
205 static int parport_khz(int khz
, int *jtag_speed
)
208 LOG_DEBUG("RCLK not supported");
212 *jtag_speed
= 499999 / (khz
* parport_toggling_time_ns
);
216 static int parport_speed_div(int speed
, int *khz
)
218 uint32_t denominator
= (speed
+ 1) * parport_toggling_time_ns
;
220 *khz
= (499999 + denominator
) / denominator
;
224 #if PARPORT_USE_GIVEIO == 1
225 static int parport_get_giveio_access(void)
228 OSVERSIONINFO version
;
230 version
.dwOSVersionInfoSize
= sizeof(version
);
231 if (!GetVersionEx(&version
)) {
235 if (version
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
)
238 h
= CreateFile("\\\\.\\giveio", GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
239 if (h
== INVALID_HANDLE_VALUE
) {
250 static struct bitbang_interface parport_bitbang
= {
251 .read
= &parport_read
,
252 .write
= &parport_write
,
253 .blink
= &parport_led
,
256 static int parport_init(void)
258 const struct cable
*cur_cable
;
259 #if PARPORT_USE_PPDEV == 1
265 if (!parport_cable
) {
266 parport_cable
= strdup("wiggler");
267 LOG_WARNING("No parport cable specified, using default 'wiggler'");
270 while (cur_cable
->name
) {
271 if (strcmp(cur_cable
->name
, parport_cable
) == 0) {
279 LOG_ERROR("No matching cable found for %s", parport_cable
);
280 return ERROR_JTAG_INIT_FAILED
;
283 dataport_value
= cable
->PORT_INIT
;
285 #if PARPORT_USE_PPDEV == 1
286 if (device_handle
> 0) {
287 LOG_ERROR("device is already opened");
288 return ERROR_JTAG_INIT_FAILED
;
291 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
292 LOG_DEBUG("opening /dev/ppi%d...", parport_port
);
294 snprintf(buffer
, 256, "/dev/ppi%d", parport_port
);
295 device_handle
= open(buffer
, O_WRONLY
);
296 #else /* not __FreeBSD__, __FreeBSD_kernel__ */
297 LOG_DEBUG("opening /dev/parport%d...", parport_port
);
299 snprintf(buffer
, 256, "/dev/parport%d", parport_port
);
300 device_handle
= open(buffer
, O_WRONLY
);
301 #endif /* __FreeBSD__, __FreeBSD_kernel__ */
303 if (device_handle
< 0) {
305 LOG_ERROR("cannot open device. check it exists and that user read and write rights are set. errno=%d", err
);
306 return ERROR_JTAG_INIT_FAILED
;
309 LOG_DEBUG("...open");
311 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
312 int i
= ioctl(device_handle
, PPCLAIM
);
315 LOG_ERROR("cannot claim device");
316 return ERROR_JTAG_INIT_FAILED
;
319 i
= PARPORT_MODE_COMPAT
;
320 i
= ioctl(device_handle
, PPSETMODE
, &i
);
322 LOG_ERROR(" cannot set compatible mode to device");
323 return ERROR_JTAG_INIT_FAILED
;
326 i
= IEEE1284_MODE_COMPAT
;
327 i
= ioctl(device_handle
, PPNEGOT
, &i
);
329 LOG_ERROR("cannot set compatible 1284 mode to device");
330 return ERROR_JTAG_INIT_FAILED
;
332 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
334 #else /* not PARPORT_USE_PPDEV */
335 if (parport_port
== 0) {
336 parport_port
= 0x378;
337 LOG_WARNING("No parport port specified, using default '0x378' (LPT1)");
340 dataport
= parport_port
;
341 statusport
= parport_port
+ 1;
343 LOG_DEBUG("requesting privileges for parallel port 0x%lx...", dataport
);
344 #if PARPORT_USE_GIVEIO == 1
345 if (parport_get_giveio_access() != 0) {
346 #else /* PARPORT_USE_GIVEIO */
347 if (ioperm(dataport
, 3, 1) != 0) {
348 #endif /* PARPORT_USE_GIVEIO */
349 LOG_ERROR("missing privileges for direct i/o");
350 return ERROR_JTAG_INIT_FAILED
;
352 LOG_DEBUG("...privileges granted");
354 /* make sure parallel port is in right mode (clear tristate and interrupt */
355 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
356 outb(parport_port
+ 2, 0x0);
358 outb(0x0, parport_port
+ 2);
361 #endif /* PARPORT_USE_PPDEV */
363 if (parport_reset(0, 0) != ERROR_OK
)
365 if (parport_write(0, 0, 0) != ERROR_OK
)
367 if (parport_led(1) != ERROR_OK
)
370 bitbang_interface
= &parport_bitbang
;
375 static int parport_quit(void)
377 if (parport_led(0) != ERROR_OK
)
381 dataport_value
= cable
->PORT_EXIT
;
382 parport_write_data();
386 parport_cable
= NULL
;
391 COMMAND_HANDLER(parport_handle_parport_port_command
)
394 /* only if the port wasn't overwritten by cmdline */
395 if (parport_port
== 0)
396 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], parport_port
);
398 LOG_ERROR("The parport port was already configured!");
403 command_print(CMD
, "parport port = 0x%" PRIx16
"", parport_port
);
408 COMMAND_HANDLER(parport_handle_parport_cable_command
)
413 /* only if the cable name wasn't overwritten by cmdline */
414 if (!parport_cable
) {
415 /* REVISIT first verify that it's listed in cables[] ... */
416 parport_cable
= malloc(strlen(CMD_ARGV
[0]) + sizeof(char));
417 if (!parport_cable
) {
418 LOG_ERROR("Out of memory");
421 strcpy(parport_cable
, CMD_ARGV
[0]);
424 /* REVISIT it's probably worth returning the current value ... */
429 COMMAND_HANDLER(parport_handle_write_on_exit_command
)
432 return ERROR_COMMAND_SYNTAX_ERROR
;
434 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], parport_exit
);
439 COMMAND_HANDLER(parport_handle_parport_toggling_time_command
)
443 int retval
= parse_u32(CMD_ARGV
[0], &ns
);
445 if (retval
!= ERROR_OK
)
449 LOG_ERROR("0 ns is not a valid parport toggling time");
453 parport_toggling_time_ns
= ns
;
454 retval
= adapter_get_speed(&wait_states
);
455 if (retval
!= ERROR_OK
) {
456 /* if adapter_get_speed fails then the clock_mode
457 * has not been configured, this happens if parport_toggling_time is
458 * called before the adapter speed is set */
459 LOG_INFO("no parport speed set - defaulting to zero wait states");
464 command_print(CMD
, "parport toggling time = %" PRIu32
" ns",
465 parport_toggling_time_ns
);
470 static const struct command_registration parport_subcommand_handlers
[] = {
473 .handler
= parport_handle_parport_port_command
,
474 .mode
= COMMAND_CONFIG
,
475 .help
= "Display the address of the I/O port (e.g. 0x378) "
476 "or the number of the '/dev/parport' device used. "
477 "If a parameter is provided, first change that port.",
478 .usage
= "[port_number]",
482 .handler
= parport_handle_parport_cable_command
,
483 .mode
= COMMAND_CONFIG
,
484 .help
= "Set the layout of the parallel port cable "
485 "used to connect to the target.",
486 /* REVISIT there's no way to list layouts we know ... */
490 .name
= "write_on_exit",
491 .handler
= parport_handle_write_on_exit_command
,
492 .mode
= COMMAND_CONFIG
,
493 .help
= "Configure the parallel driver to write "
494 "a known value to the parallel interface on exit.",
495 .usage
= "('on'|'off')",
498 .name
= "toggling_time",
499 .handler
= parport_handle_parport_toggling_time_command
,
500 .mode
= COMMAND_CONFIG
,
501 .help
= "Displays or assigns how many nanoseconds it "
502 "takes for the hardware to toggle TCK.",
503 .usage
= "[nanoseconds]",
505 COMMAND_REGISTRATION_DONE
508 static const struct command_registration parport_command_handlers
[] = {
512 .help
= "perform parport management",
513 .chain
= parport_subcommand_handlers
,
516 COMMAND_REGISTRATION_DONE
519 static struct jtag_interface parport_interface
= {
520 .supported
= DEBUG_CAP_TMS_SEQ
,
521 .execute_queue
= bitbang_execute_queue
,
524 struct adapter_driver parport_adapter_driver
= {
526 .transports
= jtag_only
,
527 .commands
= parport_command_handlers
,
529 .init
= parport_init
,
530 .quit
= parport_quit
,
531 .reset
= parport_reset
,
532 .speed
= parport_speed
,
534 .speed_div
= parport_speed_div
,
536 .jtag_ops
= &parport_interface
,