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 static uint16_t parport_port;
52 static uint32_t parport_toggling_time_ns = 1000;
53 static int wait_states;
54 
55 // Interface variables.
56 static uint8_t dataport_value;
57 
58 #if PARPORT_USE_PPDEV == 1
59 static int device_handle;
60 #else
61 static unsigned long dataport;
62 static unsigned long statusport;
63 #endif
64 
65 // Bitmask map for the input pins.
66 static struct {
67  uint8_t mask;
69  [10] = {0x40},
70  [11] = {0x80},
71  [12] = {0x20},
72  [13] = {0x10},
73  [15] = {0x08},
74 };
75 
76 // Generate an output pin bitmask for an adapter signal.
77 #define OUTPUT_BITMASK(gpio_index) BIT((adapter_gpio_config[(gpio_index)].gpio_num - 2))
78 
79 static enum bb_value parport_read(void)
80 {
81  int data = 0;
82 
83 #if PARPORT_USE_PPDEV == 1
84  ioctl(device_handle, PPRSTATUS, &data);
85 #else
86  data = inb(statusport);
87 #endif
88 
89  const struct adapter_gpio_config *gpio_config = &adapter_gpio_config[ADAPTER_GPIO_IDX_TDO];
90  const bool tdo_state = data & input_pin_bitmask_map[gpio_config->gpio_num].mask;
91 
92  return (tdo_state ^ gpio_config->active_low) ? BB_HIGH : BB_LOW;
93 }
94 
95 static void parport_write_data(void)
96 {
97  const uint8_t output = dataport_value;
98 
99 #if PARPORT_USE_PPDEV == 1
100  ioctl(device_handle, PPWDATA, &output);
101 #else
102 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
103  outb(dataport, output);
104 #else
105  outb(output, dataport);
106 #endif
107 #endif
108 }
109 
111 {
113 }
114 
116  bool state)
117 {
120  else
122 }
123 
124 static int parport_write(int tck, int tms, int tdi)
125 {
129 
130  for (int i = 0; i < wait_states + 1; i++)
132 
133  return ERROR_OK;
134 }
135 
136 // (1) assert or (0) deassert reset lines.
137 static int parport_reset(int trst, int srst)
138 {
139  LOG_DEBUG("trst: %i, srst: %i", trst, srst);
140 
143 
146 
148 
149  return ERROR_OK;
150 }
151 
152 static int parport_led(bool on)
153 {
155  return ERROR_OK;
156 
159 
160  return ERROR_OK;
161 }
162 
163 static int parport_speed(int speed)
164 {
165  wait_states = speed;
166  return ERROR_OK;
167 }
168 
169 static int parport_khz(int khz, int *jtag_speed)
170 {
171  if (!khz) {
172  LOG_ERROR("RCLK is not supported by the adapter");
173  return ERROR_FAIL;
174  }
175 
176  *jtag_speed = 499999 / (khz * parport_toggling_time_ns);
177 
178  return ERROR_OK;
179 }
180 
181 static int parport_speed_div(int speed, int *khz)
182 {
183  uint32_t denominator = (speed + 1) * parport_toggling_time_ns;
184 
185  *khz = (499999 + denominator) / denominator;
186 
187  return ERROR_OK;
188 }
189 
190 #if PARPORT_USE_GIVEIO == 1
191 static bool parport_get_giveio_access(void)
192 {
193  OSVERSIONINFO version;
194 
195  version.dwOSVersionInfoSize = sizeof(version);
196  if (!GetVersionEx(&version)) {
197  errno = EINVAL;
198  return false;
199  }
200 
201  if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
202  return true;
203 
204  HANDLE h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING,
205  FILE_ATTRIBUTE_NORMAL, NULL);
206 
207  if (h == INVALID_HANDLE_VALUE) {
208  errno = ENODEV;
209  return false;
210  }
211 
212  CloseHandle(h);
213 
214  return true;
215 }
216 #endif
217 
218 static const struct bitbang_interface parport_bitbang = {
219  .read = &parport_read,
220  .write = &parport_write,
221  .blink = &parport_led,
222 };
223 
224 static const struct {
226  bool required;
227 } all_signals[] = {
228  { ADAPTER_GPIO_IDX_TDO, true },
229  { ADAPTER_GPIO_IDX_TDI, true },
230  { ADAPTER_GPIO_IDX_TMS, true },
231  { ADAPTER_GPIO_IDX_TCK, true },
232  { ADAPTER_GPIO_IDX_TRST, false },
233  { ADAPTER_GPIO_IDX_SRST, false },
234  { ADAPTER_GPIO_IDX_LED, false },
235  { ADAPTER_GPIO_IDX_USER0, false },
236 };
237 
238 static int parport_init(void)
239 {
241 
242  // Check if all signals are configured properly.
243  for (size_t i = 0; i < ARRAY_SIZE(all_signals); i++) {
244  const enum adapter_gpio_config_index gpio_index = all_signals[i].gpio_index;
246 
247  if (gpio.gpio_num == ADAPTER_GPIO_NOT_SET) {
248  if (all_signals[i].required) {
249  LOG_ERROR("The signal '%s' is required and must be configured",
251  return ERROR_FAIL;
252  }
253 
254  continue;
255  }
256 
258  if (gpio.gpio_num < 10 || gpio.gpio_num > 15 || gpio.gpio_num == 14) {
259  LOG_ERROR("The '%s' signal pin must be 10, 11, 12, 13, or 15",
261  return ERROR_FAIL;
262  }
263  } else {
264  if (gpio.gpio_num < 2 || gpio.gpio_num > 9) {
265  LOG_ERROR("The '%s' signal pin must be 2, 3, 4, 5, 6, 7, 8, or 9",
267  return ERROR_FAIL;
268  }
269  }
270  }
271 
272  // Initialize signal pin states.
273  for (size_t i = 0; i < ARRAY_SIZE(all_signals); i++) {
274  const enum adapter_gpio_config_index gpio_index = all_signals[i].gpio_index;
276 
277  // The TDO (input) and LED (controlled by the adapter) pins cannot be
278  // initialized.
280  continue;
281 
282  // Do not initialize unconfigured GPIO pins.
283  if (gpio.gpio_num == ADAPTER_GPIO_NOT_SET)
284  continue;
285 
287  set_pin_state(gpio_index, true);
289  set_pin_state(gpio_index, false);
290  }
291 
292 #if PARPORT_USE_PPDEV == 1
293  if (device_handle > 0) {
294  LOG_ERROR("Parallel port is already open");
295  return ERROR_JTAG_INIT_FAILED;
296  }
297 
298  char device_path[256];
299 
300 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
301  snprintf(device_path, sizeof(device_path), "/dev/ppi%d", parport_port);
302 #else
303  snprintf(device_path, sizeof(device_path), "/dev/parport%d", parport_port);
304 #endif /* __FreeBSD__, __FreeBSD_kernel__ */
305 
306  LOG_DEBUG("Using parallel port %s", device_path);
307 
308  device_handle = open(device_path, O_WRONLY);
309 
310  if (device_handle < 0) {
311  int err = errno;
312  LOG_ERROR("Failed to open parallel port %s (errno = %d)", device_path,
313  err);
314  LOG_ERROR("Check whether the device exists and if you have the required access rights");
315  return ERROR_JTAG_INIT_FAILED;
316  }
317 
318 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
319  int retval = ioctl(device_handle, PPCLAIM);
320 
321  if (retval < 0) {
322  LOG_ERROR("Failed to claim parallel port %s", device_path);
323  return ERROR_JTAG_INIT_FAILED;
324  }
325 
326  int value = PARPORT_MODE_COMPAT;
327  retval = ioctl(device_handle, PPSETMODE, &value);
328 
329  if (retval < 0) {
330  LOG_ERROR("Cannot set compatible mode to device");
331  return ERROR_JTAG_INIT_FAILED;
332  }
333 
334  value = IEEE1284_MODE_COMPAT;
335  retval = ioctl(device_handle, PPNEGOT, &value);
336 
337  if (retval < 0) {
338  LOG_ERROR("Cannot set compatible 1284 mode to device");
339  return ERROR_JTAG_INIT_FAILED;
340  }
341 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
342 
343 #else /* not PARPORT_USE_PPDEV */
344  if (!parport_port) {
345  parport_port = 0x378;
346  LOG_WARNING("No parallel port specified, using default 0x378 (LPT1)");
347  }
348 
349  LOG_DEBUG("Using parallel port 0x%x", parport_port);
350 
352  statusport = parport_port + 1;
353 
354 #if PARPORT_USE_GIVEIO == 1
355  if (!parport_get_giveio_access()) {
356 #else /* PARPORT_USE_GIVEIO */
357  if (ioperm(dataport, 3, 1) != 0) {
358 #endif /* PARPORT_USE_GIVEIO */
359  LOG_ERROR("Missing privileges for direct I/O");
360  return ERROR_JTAG_INIT_FAILED;
361  }
362 
363  // Make sure parallel port is in right mode (clear tristate and interrupt).
364  #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
365  outb(parport_port + 2, 0x0);
366  #else
367  outb(0x0, parport_port + 2);
368  #endif
369 
370 #endif /* PARPORT_USE_PPDEV */
371 
372  if (parport_reset(0, 0) != ERROR_OK)
373  return ERROR_FAIL;
374 
375  if (parport_write(0, 0, 0) != ERROR_OK)
376  return ERROR_FAIL;
377 
378  if (parport_led(true) != ERROR_OK)
379  return ERROR_FAIL;
380 
382 
383  return ERROR_OK;
384 }
385 
386 static int parport_quit(void)
387 {
388  // Deinitialize signal pin states.
389  for (size_t i = 0; i < ARRAY_SIZE(all_signals); i++) {
390  const enum adapter_gpio_config_index gpio_index = all_signals[i].gpio_index;
392 
393  // The TDO (input) and LED (controlled by the adapter) pins cannot be
394  // deinitialized.
396  continue;
397 
398  // Do not deinitialize unconfigured GPIO pins.
399  if (gpio.gpio_num == ADAPTER_GPIO_NOT_SET)
400  continue;
401 
403  set_pin_state(gpio_index, true);
405  set_pin_state(gpio_index, false);
406  }
407 
410 
411  if (parport_led(false) != ERROR_OK)
412  return ERROR_FAIL;
413 
414  return ERROR_OK;
415 }
416 
417 COMMAND_HANDLER(parport_handle_port_command)
418 {
419  if (CMD_ARGC != 1)
421 
422  // Only if the port wasn't overwritten by command-line.
423  if (parport_port > 0) {
424  command_print(CMD, "The parallel port is already configured");
425  return ERROR_FAIL;
426  }
427 
429 
430  return ERROR_OK;
431 }
432 
433 // This command is only for backward compatibility and will be removed in the
434 // future.
435 COMMAND_HANDLER(parport_handle_cable_command)
436 {
437  if (CMD_ARGC != 1)
439 
440  return command_run_linef(CMD_CTX, "parport_select_cable %s", CMD_ARGV[0]);
441 }
442 
443 COMMAND_HANDLER(parport_handle_write_on_exit_command)
444 {
445  if (CMD_ARGC != 1)
447 
448  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");
449 
451 
452  return ERROR_OK;
453 }
454 
455 COMMAND_HANDLER(parport_handle_toggling_time_command)
456 {
457  if (CMD_ARGC != 1)
459 
460  uint32_t toggling_time;
461 
462  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], toggling_time);
463 
464  if (!toggling_time) {
465  command_print(CMD, "toggling time must not be 0 ns");
467  }
468 
469  parport_toggling_time_ns = toggling_time;
470  int retval = adapter_get_speed(&wait_states);
471 
472  if (retval != ERROR_OK) {
473  /*
474  * If adapter_get_speed fails then the clock_mode has not been
475  * configured, this happens if toggling_time is called before the
476  * adapter speed is set.
477  */
478  LOG_INFO("No parallel port speed set, using zero wait states");
479  wait_states = 0;
480  }
481 
482  return ERROR_OK;
483 }
484 
485 static const struct command_registration parport_subcommand_handlers[] = {
486  {
487  .name = "port",
488  .handler = parport_handle_port_command,
489  .mode = COMMAND_CONFIG,
490  .help = "Configure the address of the I/O port (e.g. 0x378) "
491  "or the number of the '/dev/parport' (Linux) or '/dev/ppi' (FreeBSD) device used",
492  .usage = "port_number",
493  },
494  {
495  .name = "cable",
496  .handler = parport_handle_cable_command,
497  .mode = COMMAND_CONFIG,
498  .help = "Set the layout of the parallel port cable "
499  "used to connect to the target",
500  .usage = "cable",
501  },
502  {
503  .name = "write_on_exit",
504  .handler = parport_handle_write_on_exit_command,
505  .mode = COMMAND_CONFIG,
506  .help = "Configure the driver to write a value to the parallel port on shutdown",
507  .usage = "('on'|'off')",
508  },
509  {
510  .name = "toggling_time",
511  .handler = parport_handle_toggling_time_command,
512  .mode = COMMAND_CONFIG,
513  .help = "Configure how many nanoseconds it takes for the hardware to toggle TCK",
514  .usage = "time",
515  },
517 };
518 
519 static const struct command_registration parport_command_handlers[] = {
520  {
521  .name = "parport",
522  .mode = COMMAND_ANY,
523  .help = "perform parport management",
525  .usage = "",
526  },
528 };
529 
530 static struct jtag_interface parport_interface = {
532  .execute_queue = bitbang_execute_queue,
533 };
534 
536  .name = "parport",
537  .transport_ids = TRANSPORT_JTAG,
538  .transport_preferred_id = TRANSPORT_JTAG,
539  .commands = parport_command_handlers,
540 
541  .init = parport_init,
542  .quit = parport_quit,
543  .reset = parport_reset,
544  .speed = parport_speed,
545  .khz = parport_khz,
546  .speed_div = parport_speed_div,
547 
548  .jtag_ops = &parport_interface,
549 };
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
#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:137
static int parport_speed(int speed)
Definition: parport.c:163
static int wait_states
Definition: parport.c:53
static const struct command_registration parport_command_handlers[]
Definition: parport.c:519
static uint32_t parport_toggling_time_ns
Definition: parport.c:52
static int parport_write(int tck, int tms, int tdi)
Definition: parport.c:124
#define OUTPUT_BITMASK(gpio_index)
Definition: parport.c:77
static void set_pin_state(enum adapter_gpio_config_index gpio_index, bool state)
Definition: parport.c:115
static unsigned long dataport
Definition: parport.c:61
static uint16_t parport_port
Definition: parport.c:50
static const struct bitbang_interface parport_bitbang
Definition: parport.c:218
static int parport_quit(void)
Definition: parport.c:386
static bool parport_write_exit_state
Definition: parport.c:51
static void parport_write_data(void)
Definition: parport.c:95
static struct jtag_interface parport_interface
Definition: parport.c:530
bool required
Definition: parport.c:226
uint8_t mask
Definition: parport.c:67
static int parport_speed_div(int speed, int *khz)
Definition: parport.c:181
enum adapter_gpio_config_index gpio_index
Definition: parport.c:225
static const struct adapter_gpio_config * adapter_gpio_config
Definition: parport.c:48
static const struct command_registration parport_subcommand_handlers[]
Definition: parport.c:485
static unsigned long statusport
Definition: parport.c:62
static bool is_gpio_configured(enum adapter_gpio_config_index gpio_index)
Definition: parport.c:110
static int parport_khz(int khz, int *jtag_speed)
Definition: parport.c:169
static struct @31 input_pin_bitmask_map[]
static uint8_t dataport_value
Definition: parport.c:56
static const struct @32 all_signals[]
static int parport_init(void)
Definition: parport.c:238
static enum bb_value parport_read(void)
Definition: parport.c:79
COMMAND_HANDLER(parport_handle_port_command)
Definition: parport.c:417
struct adapter_driver parport_adapter_driver
Definition: parport.c:535
static int parport_led(bool on)
Definition: parport.c:152
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