jtag: linuxgpiod: drop extra parenthesis
[openocd.git] / src / jtag / drivers / parport.c
blobd26a51048fec10a1a2ed832581349933955493a0
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 * *
7 * Copyright (C) 2008 by Spencer Oliver *
8 * spen@spen-soft.co.uk *
9 ***************************************************************************/
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
15 #include <jtag/adapter.h>
16 #include <jtag/interface.h>
17 #include "bitbang.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
33 #else
34 #include <linux/parport.h>
35 #include <linux/ppdev.h>
36 #endif
37 #include <sys/ioctl.h>
38 #else /* not PARPORT_USE_PPDEV */
39 #ifndef _WIN32
40 #include <sys/io.h>
41 #endif
42 #endif
44 #if PARPORT_USE_GIVEIO == 1 && IS_CYGWIN == 1
45 #include <windows.h>
46 #endif
48 /* parallel port cable description
50 struct cable {
51 const char *name;
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:
78 HARD TCK - Target TCK
79 HARD TMS - Target TMS
80 HARD TDI - Target TDI
81 HARD TDO - Target TDO
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 }
90 /* configuration */
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;
104 #else
105 static unsigned long dataport;
106 static unsigned long statusport;
107 #endif
109 static bb_value_t parport_read(void)
111 int data = 0;
113 #if PARPORT_USE_PPDEV == 1
114 ioctl(device_handle, PPRSTATUS, &data);
115 #else
116 data = inb(statusport);
117 #endif
119 if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
120 return BB_HIGH;
121 else
122 return BB_LOW;
125 static inline void parport_write_data(void)
127 uint8_t output;
128 output = dataport_value ^ cable->OUTPUT_INVERT;
130 #if PARPORT_USE_PPDEV == 1
131 ioctl(device_handle, PPWDATA, &output);
132 #else
133 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
134 outb(dataport, output);
135 #else
136 outb(output, dataport);
137 #endif
138 #endif
141 static int parport_write(int tck, int tms, int tdi)
143 int i = wait_states + 1;
145 if (tck)
146 dataport_value |= cable->TCK_MASK;
147 else
148 dataport_value &= ~cable->TCK_MASK;
150 if (tms)
151 dataport_value |= cable->TMS_MASK;
152 else
153 dataport_value &= ~cable->TMS_MASK;
155 if (tdi)
156 dataport_value |= cable->TDI_MASK;
157 else
158 dataport_value &= ~cable->TDI_MASK;
160 while (i-- > 0)
161 parport_write_data();
163 return ERROR_OK;
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);
171 if (trst == 0)
172 dataport_value |= cable->TRST_MASK;
173 else if (trst == 1)
174 dataport_value &= ~cable->TRST_MASK;
176 if (srst == 0)
177 dataport_value |= cable->SRST_MASK;
178 else if (srst == 1)
179 dataport_value &= ~cable->SRST_MASK;
181 parport_write_data();
183 return ERROR_OK;
186 /* turn LED on parport adapter on (1) or off (0) */
187 static int parport_led(int on)
189 if (on)
190 dataport_value |= cable->LED_MASK;
191 else
192 dataport_value &= ~cable->LED_MASK;
194 parport_write_data();
196 return ERROR_OK;
199 static int parport_speed(int speed)
201 wait_states = speed;
202 return ERROR_OK;
205 static int parport_khz(int khz, int *jtag_speed)
207 if (khz == 0) {
208 LOG_DEBUG("RCLK not supported");
209 return ERROR_FAIL;
212 *jtag_speed = 499999 / (khz * parport_toggling_time_ns);
213 return ERROR_OK;
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;
221 return ERROR_OK;
224 #if PARPORT_USE_GIVEIO == 1
225 static int parport_get_giveio_access(void)
227 HANDLE h;
228 OSVERSIONINFO version;
230 version.dwOSVersionInfoSize = sizeof(version);
231 if (!GetVersionEx(&version)) {
232 errno = EINVAL;
233 return -1;
235 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
236 return 0;
238 h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
239 if (h == INVALID_HANDLE_VALUE) {
240 errno = ENODEV;
241 return -1;
244 CloseHandle(h);
246 return 0;
248 #endif
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
260 char buffer[256];
261 #endif
263 cur_cable = cables;
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) {
272 cable = cur_cable;
273 break;
275 cur_cable++;
278 if (!cable) {
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) {
304 int err = errno;
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);
314 if (i < 0) {
315 LOG_ERROR("cannot claim device");
316 return ERROR_JTAG_INIT_FAILED;
319 i = PARPORT_MODE_COMPAT;
320 i = ioctl(device_handle, PPSETMODE, &i);
321 if (i < 0) {
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);
328 if (i < 0) {
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);
357 #else
358 outb(0x0, parport_port + 2);
359 #endif
361 #endif /* PARPORT_USE_PPDEV */
363 if (parport_reset(0, 0) != ERROR_OK)
364 return ERROR_FAIL;
365 if (parport_write(0, 0, 0) != ERROR_OK)
366 return ERROR_FAIL;
367 if (parport_led(1) != ERROR_OK)
368 return ERROR_FAIL;
370 bitbang_interface = &parport_bitbang;
372 return ERROR_OK;
375 static int parport_quit(void)
377 if (parport_led(0) != ERROR_OK)
378 return ERROR_FAIL;
380 if (parport_exit) {
381 dataport_value = cable->PORT_EXIT;
382 parport_write_data();
385 free(parport_cable);
386 parport_cable = NULL;
388 return ERROR_OK;
391 COMMAND_HANDLER(parport_handle_parport_port_command)
393 if (CMD_ARGC == 1) {
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);
397 else {
398 LOG_ERROR("The parport port was already configured!");
399 return ERROR_FAIL;
403 command_print(CMD, "parport port = 0x%" PRIx16 "", parport_port);
405 return ERROR_OK;
408 COMMAND_HANDLER(parport_handle_parport_cable_command)
410 if (CMD_ARGC == 0)
411 return ERROR_OK;
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");
419 return ERROR_FAIL;
421 strcpy(parport_cable, CMD_ARGV[0]);
424 /* REVISIT it's probably worth returning the current value ... */
426 return ERROR_OK;
429 COMMAND_HANDLER(parport_handle_write_on_exit_command)
431 if (CMD_ARGC != 1)
432 return ERROR_COMMAND_SYNTAX_ERROR;
434 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], parport_exit);
436 return ERROR_OK;
439 COMMAND_HANDLER(parport_handle_parport_toggling_time_command)
441 if (CMD_ARGC == 1) {
442 uint32_t ns;
443 int retval = parse_u32(CMD_ARGV[0], &ns);
445 if (retval != ERROR_OK)
446 return retval;
448 if (ns == 0) {
449 LOG_ERROR("0 ns is not a valid parport toggling time");
450 return ERROR_FAIL;
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");
460 wait_states = 0;
464 command_print(CMD, "parport toggling time = %" PRIu32 " ns",
465 parport_toggling_time_ns);
467 return ERROR_OK;
470 static const struct command_registration parport_subcommand_handlers[] = {
472 .name = "port",
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]",
481 .name = "cable",
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 ... */
487 .usage = "[layout]",
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[] = {
510 .name = "parport",
511 .mode = COMMAND_ANY,
512 .help = "perform parport management",
513 .chain = parport_subcommand_handlers,
514 .usage = "",
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 = {
525 .name = "parport",
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,
533 .khz = parport_khz,
534 .speed_div = parport_speed_div,
536 .jtag_ops = &parport_interface,