OpenOCD
parport.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
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  ***************************************************************************/
10 
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14 
15 #include <jtag/adapter.h>
16 #include <jtag/interface.h>
17 #include "bitbang.h"
18 
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__ */
26 
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
43 
44 #if PARPORT_USE_GIVEIO == 1 && IS_CYGWIN == 1
45 #include <windows.h>
46 #endif
47 
49 
50 #if PARPORT_USE_PPDEV == 0
51 static uint16_t parport_port;
52 #endif
54 static char *parport_device_file;
55 static uint32_t parport_toggling_time_ns = 1000;
56 static int wait_states;
57 
58 // Interface variables.
59 static uint8_t dataport_value;
60 
61 #if PARPORT_USE_PPDEV == 1
62 static int device_handle;
63 #else
64 static unsigned long dataport;
65 static unsigned long statusport;
66 #endif
67 
68 // Bitmask map for the input pins.
69 static struct {
70  uint8_t mask;
72  [10] = {0x40},
73  [11] = {0x80},
74  [12] = {0x20},
75  [13] = {0x10},
76  [15] = {0x08},
77 };
78 
79 // Generate an output pin bitmask for an adapter signal.
80 #define OUTPUT_BITMASK(gpio_index) BIT((adapter_gpio_config[(gpio_index)].gpio_num - 2))
81 
82 static enum bb_value parport_read(void)
83 {
84  int data = 0;
85 
86 #if PARPORT_USE_PPDEV == 1
87  ioctl(device_handle, PPRSTATUS, &data);
88 #else
89  data = inb(statusport);
90 #endif
91 
92  const struct adapter_gpio_config *gpio_config = &adapter_gpio_config[ADAPTER_GPIO_IDX_TDO];
93  const bool tdo_state = data & input_pin_bitmask_map[gpio_config->gpio_num].mask;
94 
95  return (tdo_state ^ gpio_config->active_low) ? BB_HIGH : BB_LOW;
96 }
97 
98 static void parport_write_data(void)
99 {
100  const uint8_t output = dataport_value;
101 
102 #if PARPORT_USE_PPDEV == 1
103  ioctl(device_handle, PPWDATA, &output);
104 #else
105 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
106  outb(dataport, output);
107 #else
108  outb(output, dataport);
109 #endif
110 #endif
111 }
112 
114 {
116 }
117 
119  bool state)
120 {
123  else
125 }
126 
127 static int parport_write(int tck, int tms, int tdi)
128 {
132 
133  for (int i = 0; i < wait_states + 1; i++)
135 
136  return ERROR_OK;
137 }
138 
139 // (1) assert or (0) deassert reset lines.
140 static int parport_reset(int trst, int srst)
141 {
142  LOG_DEBUG("trst: %i, srst: %i", trst, srst);
143 
146 
149 
151 
152  return ERROR_OK;
153 }
154 
155 static int parport_led(bool on)
156 {
158  return ERROR_OK;
159 
162 
163  return ERROR_OK;
164 }
165 
166 static int parport_speed(int speed)
167 {
168  wait_states = speed;
169  return ERROR_OK;
170 }
171 
172 static int parport_khz(int khz, int *jtag_speed)
173 {
174  if (!khz) {
175  LOG_ERROR("RCLK is not supported by the adapter");
176  return ERROR_FAIL;
177  }
178 
179  *jtag_speed = 499999 / (khz * parport_toggling_time_ns);
180 
181  return ERROR_OK;
182 }
183 
184 static int parport_speed_div(int speed, int *khz)
185 {
186  uint32_t denominator = (speed + 1) * parport_toggling_time_ns;
187 
188  *khz = (499999 + denominator) / denominator;
189 
190  return ERROR_OK;
191 }
192 
193 #if PARPORT_USE_GIVEIO == 1
194 static bool parport_get_giveio_access(void)
195 {
196  OSVERSIONINFO version;
197 
198  version.dwOSVersionInfoSize = sizeof(version);
199  if (!GetVersionEx(&version)) {
200  errno = EINVAL;
201  return false;
202  }
203 
204  if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
205  return true;
206 
207  HANDLE h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING,
208  FILE_ATTRIBUTE_NORMAL, NULL);
209 
210  if (h == INVALID_HANDLE_VALUE) {
211  errno = ENODEV;
212  return false;
213  }
214 
215  CloseHandle(h);
216 
217  return true;
218 }
219 #endif
220 
221 static const struct bitbang_interface parport_bitbang = {
222  .read = &parport_read,
223  .write = &parport_write,
224  .blink = &parport_led,
225 };
226 
227 static const struct {
229  bool required;
230 } all_signals[] = {
231  { ADAPTER_GPIO_IDX_TDO, true },
232  { ADAPTER_GPIO_IDX_TDI, true },
233  { ADAPTER_GPIO_IDX_TMS, true },
234  { ADAPTER_GPIO_IDX_TCK, true },
235  { ADAPTER_GPIO_IDX_TRST, false },
236  { ADAPTER_GPIO_IDX_SRST, false },
237  { ADAPTER_GPIO_IDX_LED, false },
238  { ADAPTER_GPIO_IDX_USER0, false },
239 };
240 
241 static int parport_init(void)
242 {
244 
245  // Check if all signals are configured properly.
246  for (size_t i = 0; i < ARRAY_SIZE(all_signals); i++) {
247  const enum adapter_gpio_config_index gpio_index = all_signals[i].gpio_index;
249 
250  if (gpio.gpio_num == ADAPTER_GPIO_NOT_SET) {
251  if (all_signals[i].required) {
252  LOG_ERROR("The signal '%s' is required and must be configured",
254  return ERROR_FAIL;
255  }
256 
257  continue;
258  }
259 
261  if (gpio.gpio_num < 10 || gpio.gpio_num > 15 || gpio.gpio_num == 14) {
262  LOG_ERROR("The '%s' signal pin must be 10, 11, 12, 13, or 15",
264  goto init_fail;
265  }
266  } else {
267  if (gpio.gpio_num < 2 || gpio.gpio_num > 9) {
268  LOG_ERROR("The '%s' signal pin must be 2, 3, 4, 5, 6, 7, 8, or 9",
270  goto init_fail;
271  }
272  }
273  }
274 
275  // Initialize signal pin states.
276  for (size_t i = 0; i < ARRAY_SIZE(all_signals); i++) {
277  const enum adapter_gpio_config_index gpio_index = all_signals[i].gpio_index;
279 
280  // The TDO (input) and LED (controlled by the adapter) pins cannot be
281  // initialized.
283  continue;
284 
285  // Do not initialize unconfigured GPIO pins.
286  if (gpio.gpio_num == ADAPTER_GPIO_NOT_SET)
287  continue;
288 
290  set_pin_state(gpio_index, true);
292  set_pin_state(gpio_index, false);
293  }
294 
295 #if PARPORT_USE_PPDEV == 1
296  if (device_handle > 0) {
297  LOG_ERROR("Parallel port is already open");
298  goto init_fail;
299  }
300 
301  if (!parport_device_file) {
302 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
303  parport_device_file = strdup("/dev/ppi0");
304 #else
305  parport_device_file = strdup("/dev/parport0");
306 #endif
307  LOG_WARNING("No parallel port specified, using %s", parport_device_file);
308  LOG_WARNING("DEPRECATED! The lack of a parallel port specification is deprecated and will no longer work in the future");
309  }
310 
311  LOG_DEBUG("Using parallel port %s", parport_device_file);
312 
313  device_handle = open(parport_device_file, O_WRONLY);
314 
315  if (device_handle < 0) {
316  int err = errno;
317  LOG_ERROR("Failed to open parallel port %s (errno = %d)",
318  parport_device_file, err);
319  LOG_ERROR("Check whether the device exists and if you have the required access rights");
320  goto init_fail;
321  }
322 
323 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
324  int retval = ioctl(device_handle, PPCLAIM);
325 
326  if (retval < 0) {
327  LOG_ERROR("Failed to claim parallel port %s", parport_device_file);
328  goto init_fail;
329  }
330 
331  int value = PARPORT_MODE_COMPAT;
332  retval = ioctl(device_handle, PPSETMODE, &value);
333 
334  if (retval < 0) {
335  LOG_ERROR("Cannot set compatible mode to device");
336  goto init_fail;
337  }
338 
339  value = IEEE1284_MODE_COMPAT;
340  retval = ioctl(device_handle, PPNEGOT, &value);
341 
342  if (retval < 0) {
343  LOG_ERROR("Cannot set compatible 1284 mode to device");
344  goto init_fail;
345  }
346 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
347 
348 #else /* not PARPORT_USE_PPDEV */
349  LOG_WARNING("DEPRECATED: Parallel port access with direct I/O is deprecated and support will be removed in the next release");
350 
351  if (!parport_port) {
352  parport_port = 0x378;
353  LOG_WARNING("No parallel port specified, using default 0x378 (LPT1)");
354  }
355 
356  LOG_DEBUG("Using parallel port 0x%x", parport_port);
357 
359  statusport = parport_port + 1;
360 
361 #if PARPORT_USE_GIVEIO == 1
362  if (!parport_get_giveio_access()) {
363 #else /* PARPORT_USE_GIVEIO */
364  if (ioperm(dataport, 3, 1) != 0) {
365 #endif /* PARPORT_USE_GIVEIO */
366  LOG_ERROR("Missing privileges for direct I/O");
367  goto init_fail;
368  }
369 
370  // Make sure parallel port is in right mode (clear tristate and interrupt).
371  #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
372  outb(parport_port + 2, 0x0);
373  #else
374  outb(0x0, parport_port + 2);
375  #endif
376 
377 #endif /* PARPORT_USE_PPDEV */
378 
379  if (parport_reset(0, 0) != ERROR_OK)
380  goto init_fail;
381 
382  if (parport_write(0, 0, 0) != ERROR_OK)
383  goto init_fail;
384 
385  if (parport_led(true) != ERROR_OK)
386  goto init_fail;
387 
389 
390  return ERROR_OK;
391 
392 init_fail:
393  free(parport_device_file);
394  return ERROR_JTAG_INIT_FAILED;
395 }
396 
397 static int parport_quit(void)
398 {
399  free(parport_device_file);
400 
401  // Deinitialize signal pin states.
402  for (size_t i = 0; i < ARRAY_SIZE(all_signals); i++) {
403  const enum adapter_gpio_config_index gpio_index = all_signals[i].gpio_index;
405 
406  // The TDO (input) and LED (controlled by the adapter) pins cannot be
407  // deinitialized.
409  continue;
410 
411  // Do not deinitialize unconfigured GPIO pins.
412  if (gpio.gpio_num == ADAPTER_GPIO_NOT_SET)
413  continue;
414 
416  set_pin_state(gpio_index, true);
418  set_pin_state(gpio_index, false);
419  }
420 
423 
424  if (parport_led(false) != ERROR_OK)
425  return ERROR_FAIL;
426 
427  return ERROR_OK;
428 }
429 
430 COMMAND_HANDLER(parport_handle_port_command)
431 {
432  if (CMD_ARGC != 1)
434 
435 #if PARPORT_USE_PPDEV == 1
436  if (parport_device_file) {
437  LOG_ERROR("The parallel port device file is already configured");
438  return ERROR_FAIL;
439  }
440 
441  char *tmp;
442 
443  // We do not use the parse_xxx() or COMMAND_PARSE_xxx() functions here since
444  // they generate an error message if parsing fails.
445  char *endptr = NULL;
446  unsigned long port_number = strtoul(CMD_ARGV[0], &endptr, 0);
447 
448  if (*endptr == '\0' && endptr != CMD_ARGV[0]) {
449  LOG_WARNING("DEPRECATED! Using a port number is deprecated, use the device file instead");
450 
451 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
452  tmp = alloc_printf("/dev/ppi%lu", port_number);
453 #else
454  tmp = alloc_printf("/dev/parport%lu", port_number);
455 #endif
456  } else {
457  tmp = strdup(CMD_ARGV[0]);
458  }
459 
460  if (!tmp) {
461  LOG_ERROR("Failed to allocate memory");
462  return ERROR_FAIL;
463  }
464 
465  free(parport_device_file);
466  parport_device_file = tmp;
467 #else
468  if (parport_port > 0) {
469  command_print(CMD, "The parallel port is already configured");
470  return ERROR_FAIL;
471  }
472 
474 #endif
475 
476  return ERROR_OK;
477 }
478 
479 // This command is only for backward compatibility and will be removed in the
480 // future.
481 COMMAND_HANDLER(parport_handle_cable_command)
482 {
483  if (CMD_ARGC != 1)
485 
486  return command_run_linef(CMD_CTX, "parport_select_cable %s", CMD_ARGV[0]);
487 }
488 
489 COMMAND_HANDLER(parport_handle_write_on_exit_command)
490 {
491  if (CMD_ARGC != 1)
493 
494  LOG_WARNING("DEPRECATED: 'parport write_on_exit' will be removed in the future, use the 'adapter gpio' command to configure the exit state for pins");
495 
497 
498  return ERROR_OK;
499 }
500 
501 COMMAND_HANDLER(parport_handle_toggling_time_command)
502 {
503  if (CMD_ARGC != 1)
505 
506  uint32_t toggling_time;
507 
508  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], toggling_time);
509 
510  if (!toggling_time) {
511  command_print(CMD, "toggling time must not be 0 ns");
513  }
514 
515  parport_toggling_time_ns = toggling_time;
516  int retval = adapter_get_speed(&wait_states);
517 
518  if (retval != ERROR_OK) {
519  /*
520  * If adapter_get_speed fails then the clock_mode has not been
521  * configured, this happens if toggling_time is called before the
522  * adapter speed is set.
523  */
524  LOG_INFO("No parallel port speed set, using zero wait states");
525  wait_states = 0;
526  }
527 
528  return ERROR_OK;
529 }
530 
531 static const struct command_registration parport_subcommand_handlers[] = {
532  {
533  .name = "port",
534  .handler = parport_handle_port_command,
535  .mode = COMMAND_CONFIG,
536  .help = "Specify the device file of the parallel port",
537  .usage = "file",
538  },
539  {
540  .name = "cable",
541  .handler = parport_handle_cable_command,
542  .mode = COMMAND_CONFIG,
543  .help = "Set the layout of the parallel port cable "
544  "used to connect to the target",
545  .usage = "cable",
546  },
547  {
548  .name = "write_on_exit",
549  .handler = parport_handle_write_on_exit_command,
550  .mode = COMMAND_CONFIG,
551  .help = "Configure the driver to write a value to the parallel port on shutdown",
552  .usage = "('on'|'off')",
553  },
554  {
555  .name = "toggling_time",
556  .handler = parport_handle_toggling_time_command,
557  .mode = COMMAND_CONFIG,
558  .help = "Configure how many nanoseconds it takes for the hardware to toggle TCK",
559  .usage = "time",
560  },
562 };
563 
564 static const struct command_registration parport_command_handlers[] = {
565  {
566  .name = "parport",
567  .mode = COMMAND_ANY,
568  .help = "perform parport management",
570  .usage = "",
571  },
573 };
574 
575 static struct jtag_interface parport_interface = {
577  .execute_queue = bitbang_execute_queue,
578 };
579 
581  .name = "parport",
582  .transport_ids = TRANSPORT_JTAG,
583  .transport_preferred_id = TRANSPORT_JTAG,
584  .commands = parport_command_handlers,
585 
586  .init = parport_init,
587  .quit = parport_quit,
588  .reset = parport_reset,
589  .speed = parport_speed,
590  .khz = parport_khz,
591  .speed_div = parport_speed_div,
592 
593  .jtag_ops = &parport_interface,
594 };
const struct adapter_gpio_config * adapter_gpio_get_config(void)
Retrieves gpio configuration set with command "adapter gpio <signal_name>".
Definition: adapter.c:1263
int adapter_get_speed(int *speed)
Definition: adapter.c:271
const char * adapter_gpio_get_name(enum adapter_gpio_config_index idx)
Retrieves gpio name.
Definition: adapter.c:1257
@ ADAPTER_GPIO_INIT_STATE_ACTIVE
Definition: adapter.h:32
@ ADAPTER_GPIO_INIT_STATE_INACTIVE
Definition: adapter.h:31
@ ADAPTER_GPIO_EXIT_STATE_ACTIVE
Definition: adapter.h:40
@ ADAPTER_GPIO_EXIT_STATE_INACTIVE
Definition: adapter.h:39
adapter_gpio_config_index
Adapter GPIO.
Definition: adapter.h:52
@ ADAPTER_GPIO_IDX_LED
Definition: adapter.h:62
@ ADAPTER_GPIO_IDX_SRST
Definition: adapter.h:61
@ ADAPTER_GPIO_IDX_TRST
Definition: adapter.h:57
@ ADAPTER_GPIO_IDX_TDI
Definition: adapter.h:54
@ ADAPTER_GPIO_IDX_TMS
Definition: adapter.h:55
@ ADAPTER_GPIO_IDX_USER0
Definition: adapter.h:63
@ ADAPTER_GPIO_IDX_TCK
Definition: adapter.h:56
@ ADAPTER_GPIO_IDX_TDO
Definition: adapter.h:53
#define ADAPTER_GPIO_NOT_SET
Definition: adapter.h:132
int bitbang_execute_queue(struct jtag_command *cmd_queue)
Definition: bitbang.c:293
bb_value
Definition: bitbang.h:17
@ BB_LOW
Definition: bitbang.h:18
@ BB_HIGH
Definition: bitbang.h:19
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:371
int command_run_linef(struct command_context *context, const char *format,...)
Definition: command.c:536
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:528
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:440
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:402
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
static uint16_t output
Definition: ftdi.c:119
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:188
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
char * alloc_printf(const char *format,...)
Definition: log.c:376
#define LOG_WARNING(expr ...)
Definition: log.h:130
#define ERROR_FAIL
Definition: log.h:174
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define LOG_INFO(expr ...)
Definition: log.h:127
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
static int parport_reset(int trst, int srst)
Definition: parport.c:140
static int parport_speed(int speed)
Definition: parport.c:166
static int wait_states
Definition: parport.c:56
static const struct command_registration parport_command_handlers[]
Definition: parport.c:564
static uint32_t parport_toggling_time_ns
Definition: parport.c:55
static int parport_write(int tck, int tms, int tdi)
Definition: parport.c:127
#define OUTPUT_BITMASK(gpio_index)
Definition: parport.c:80
static void set_pin_state(enum adapter_gpio_config_index gpio_index, bool state)
Definition: parport.c:118
static unsigned long dataport
Definition: parport.c:64
static uint16_t parport_port
Definition: parport.c:51
static char * parport_device_file
Definition: parport.c:54
static const struct bitbang_interface parport_bitbang
Definition: parport.c:221
static int parport_quit(void)
Definition: parport.c:397
static bool parport_write_exit_state
Definition: parport.c:53
static void parport_write_data(void)
Definition: parport.c:98
static struct jtag_interface parport_interface
Definition: parport.c:575
bool required
Definition: parport.c:229
uint8_t mask
Definition: parport.c:70
static int parport_speed_div(int speed, int *khz)
Definition: parport.c:184
enum adapter_gpio_config_index gpio_index
Definition: parport.c:228
static const struct adapter_gpio_config * adapter_gpio_config
Definition: parport.c:48
static const struct command_registration parport_subcommand_handlers[]
Definition: parport.c:531
static unsigned long statusport
Definition: parport.c:65
static bool is_gpio_configured(enum adapter_gpio_config_index gpio_index)
Definition: parport.c:113
static int parport_khz(int khz, int *jtag_speed)
Definition: parport.c:172
static struct @31 input_pin_bitmask_map[]
static uint8_t dataport_value
Definition: parport.c:59
static const struct @32 all_signals[]
static int parport_init(void)
Definition: parport.c:241
static enum bb_value parport_read(void)
Definition: parport.c:82
COMMAND_HANDLER(parport_handle_port_command)
Definition: parport.c:430
struct adapter_driver parport_adapter_driver
Definition: parport.c:580
static int parport_led(bool on)
Definition: parport.c:155
Represents a driver for a debugging interface.
Definition: interface.h:208
const char *const name
The name of the interface driver.
Definition: interface.h:210
Configuration options for a single GPIO.
Definition: adapter.h:68
unsigned int gpio_num
Definition: adapter.h:69
enum adapter_gpio_exit_state exit_state
Definition: adapter.h:73
enum adapter_gpio_init_state init_state
Definition: adapter.h:72
Low level callbacks (for bitbang).
Definition: bitbang.h:30
enum bb_value(* read)(void)
Sample TDO and return the value.
Definition: bitbang.h:32
const char * name
Definition: command.h:234
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:239
Represents a driver for a debugging interface.
Definition: interface.h:183
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:187
#define TRANSPORT_JTAG
Definition: transport.h:19
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define NULL
Definition: usb.h:16
uint8_t state[4]
Definition: vdebug.c:21