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 
48 /* parallel port cable description
49  */
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 */
63 };
64 
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
84 */
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 }
88 };
89 
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;
96 
97 /* interface variables
98  */
99 static const struct cable *cable;
100 static uint8_t dataport_value;
101 
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
108 
110 {
111  int data = 0;
112 
113 #if PARPORT_USE_PPDEV == 1
114  ioctl(device_handle, PPRSTATUS, &data);
115 #else
116  data = inb(statusport);
117 #endif
118 
119  if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
120  return BB_HIGH;
121  else
122  return BB_LOW;
123 }
124 
125 static inline void parport_write_data(void)
126 {
127  uint8_t output;
129 
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
139 }
140 
141 static int parport_write(int tck, int tms, int tdi)
142 {
143  int i = wait_states + 1;
144 
145  if (tck)
147  else
149 
150  if (tms)
152  else
154 
155  if (tdi)
157  else
159 
160  while (i-- > 0)
162 
163  return ERROR_OK;
164 }
165 
166 /* (1) assert or (0) deassert reset lines */
167 static int parport_reset(int trst, int srst)
168 {
169  LOG_DEBUG("trst: %i, srst: %i", trst, srst);
170 
171  if (trst == 0)
173  else if (trst == 1)
175 
176  if (srst == 0)
178  else if (srst == 1)
180 
182 
183  return ERROR_OK;
184 }
185 
186 /* turn LED on parport adapter on (1) or off (0) */
187 static int parport_led(int on)
188 {
189  if (on)
191  else
193 
195 
196  return ERROR_OK;
197 }
198 
199 static int parport_speed(int speed)
200 {
201  wait_states = speed;
202  return ERROR_OK;
203 }
204 
205 static int parport_khz(int khz, int *jtag_speed)
206 {
207  if (khz == 0) {
208  LOG_DEBUG("RCLK not supported");
209  return ERROR_FAIL;
210  }
211 
212  *jtag_speed = 499999 / (khz * parport_toggling_time_ns);
213  return ERROR_OK;
214 }
215 
216 static int parport_speed_div(int speed, int *khz)
217 {
218  uint32_t denominator = (speed + 1) * parport_toggling_time_ns;
219 
220  *khz = (499999 + denominator) / denominator;
221  return ERROR_OK;
222 }
223 
224 #if PARPORT_USE_GIVEIO == 1
225 static int parport_get_giveio_access(void)
226 {
227  HANDLE h;
228  OSVERSIONINFO version;
229 
230  version.dwOSVersionInfoSize = sizeof(version);
231  if (!GetVersionEx(&version)) {
232  errno = EINVAL;
233  return -1;
234  }
235  if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
236  return 0;
237 
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;
242  }
243 
244  CloseHandle(h);
245 
246  return 0;
247 }
248 #endif
249 
250 static struct bitbang_interface parport_bitbang = {
251  .read = &parport_read,
252  .write = &parport_write,
253  .blink = &parport_led,
254  };
255 
256 static int parport_init(void)
257 {
258  const struct cable *cur_cable;
259 #if PARPORT_USE_PPDEV == 1
260  char buffer[256];
261 #endif
262 
263  cur_cable = cables;
264 
265  if (!parport_cable) {
266  parport_cable = strdup("wiggler");
267  LOG_WARNING("No parport cable specified, using default 'wiggler'");
268  }
269 
270  while (cur_cable->name) {
271  if (strcmp(cur_cable->name, parport_cable) == 0) {
272  cable = cur_cable;
273  break;
274  }
275  cur_cable++;
276  }
277 
278  if (!cable) {
279  LOG_ERROR("No matching cable found for %s", parport_cable);
280  return ERROR_JTAG_INIT_FAILED;
281  }
282 
284 
285 #if PARPORT_USE_PPDEV == 1
286  if (device_handle > 0) {
287  LOG_ERROR("device is already opened");
288  return ERROR_JTAG_INIT_FAILED;
289  }
290 
291 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
292  LOG_DEBUG("opening /dev/ppi%d...", parport_port);
293 
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);
298 
299  snprintf(buffer, 256, "/dev/parport%d", parport_port);
300  device_handle = open(buffer, O_WRONLY);
301 #endif /* __FreeBSD__, __FreeBSD_kernel__ */
302 
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;
307  }
308 
309  LOG_DEBUG("...open");
310 
311 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
312  int i = ioctl(device_handle, PPCLAIM);
313 
314  if (i < 0) {
315  LOG_ERROR("cannot claim device");
316  return ERROR_JTAG_INIT_FAILED;
317  }
318 
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;
324  }
325 
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;
331  }
332 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
333 
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)");
338  }
339 
341  statusport = parport_port + 1;
342 
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;
351  }
352  LOG_DEBUG("...privileges granted");
353 
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
360 
361 #endif /* PARPORT_USE_PPDEV */
362 
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;
369 
371 
372  return ERROR_OK;
373 }
374 
375 static int parport_quit(void)
376 {
377  if (parport_led(0) != ERROR_OK)
378  return ERROR_FAIL;
379 
380  if (parport_exit) {
383  }
384 
385  free(parport_cable);
387 
388  return ERROR_OK;
389 }
390 
391 COMMAND_HANDLER(parport_handle_parport_port_command)
392 {
393  if (CMD_ARGC == 1) {
394  /* only if the port wasn't overwritten by cmdline */
395  if (parport_port == 0)
397  else {
398  LOG_ERROR("The parport port was already configured!");
399  return ERROR_FAIL;
400  }
401  }
402 
403  command_print(CMD, "parport port = 0x%" PRIx16 "", parport_port);
404 
405  return ERROR_OK;
406 }
407 
408 COMMAND_HANDLER(parport_handle_parport_cable_command)
409 {
410  if (CMD_ARGC == 0)
411  return ERROR_OK;
412 
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;
420  }
421  strcpy(parport_cable, CMD_ARGV[0]);
422  }
423 
424  /* REVISIT it's probably worth returning the current value ... */
425 
426  return ERROR_OK;
427 }
428 
429 COMMAND_HANDLER(parport_handle_write_on_exit_command)
430 {
431  if (CMD_ARGC != 1)
433 
435 
436  return ERROR_OK;
437 }
438 
439 COMMAND_HANDLER(parport_handle_parport_toggling_time_command)
440 {
441  if (CMD_ARGC == 1) {
442  uint32_t ns;
443  int retval = parse_u32(CMD_ARGV[0], &ns);
444 
445  if (retval != ERROR_OK)
446  return retval;
447 
448  if (ns == 0) {
449  LOG_ERROR("0 ns is not a valid parport toggling time");
450  return ERROR_FAIL;
451  }
452 
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;
461  }
462  }
463 
464  command_print(CMD, "parport toggling time = %" PRIu32 " ns",
466 
467  return ERROR_OK;
468 }
469 
470 static const struct command_registration parport_subcommand_handlers[] = {
471  {
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]",
479  },
480  {
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]",
488  },
489  {
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')",
496  },
497  {
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]",
504  },
506 };
507 
508 static const struct command_registration parport_command_handlers[] = {
509  {
510  .name = "parport",
511  .mode = COMMAND_ANY,
512  .help = "perform parport management",
514  .usage = "",
515  },
517 };
518 
519 static struct jtag_interface parport_interface = {
521  .execute_queue = bitbang_execute_queue,
522 };
523 
525  .name = "parport",
526  .transports = jtag_only,
527  .commands = parport_command_handlers,
528 
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,
535 
536  .jtag_ops = &parport_interface,
537 };
const char *const jtag_only[]
Definition: adapter.c:27
int adapter_get_speed(int *speed)
Definition: adapter.c:267
int bitbang_execute_queue(struct jtag_command *cmd_queue)
Definition: bitbang.c:296
bb_value_t
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:443
#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:521
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#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:442
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ 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:187
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:553
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
static int parport_reset(int trst, int srst)
Definition: parport.c:167
static const struct cable * cable
Definition: parport.c:99
static int parport_speed(int speed)
Definition: parport.c:199
static int wait_states
Definition: parport.c:95
static const struct command_registration parport_command_handlers[]
Definition: parport.c:508
static uint32_t parport_toggling_time_ns
Definition: parport.c:94
static int parport_write(int tck, int tms, int tdi)
Definition: parport.c:141
static const struct cable cables[]
Definition: parport.c:65
static unsigned long dataport
Definition: parport.c:105
static bb_value_t parport_read(void)
Definition: parport.c:109
static uint16_t parport_port
Definition: parport.c:92
static int parport_quit(void)
Definition: parport.c:375
static int parport_led(int on)
Definition: parport.c:187
static void parport_write_data(void)
Definition: parport.c:125
static struct jtag_interface parport_interface
Definition: parport.c:519
static struct bitbang_interface parport_bitbang
Definition: parport.c:250
static char * parport_cable
Definition: parport.c:91
static int parport_speed_div(int speed, int *khz)
Definition: parport.c:216
static const struct command_registration parport_subcommand_handlers[]
Definition: parport.c:470
static unsigned long statusport
Definition: parport.c:106
static int parport_khz(int khz, int *jtag_speed)
Definition: parport.c:205
COMMAND_HANDLER(parport_handle_parport_port_command)
Definition: parport.c:391
static uint8_t dataport_value
Definition: parport.c:100
static int parport_init(void)
Definition: parport.c:256
static bool parport_exit
Definition: parport.c:93
struct adapter_driver parport_adapter_driver
Definition: parport.c:524
Represents a driver for a debugging interface.
Definition: interface.h:207
const char *const name
The name of the interface driver.
Definition: interface.h:209
Low level callbacks (for bitbang).
Definition: bitbang.h:30
bb_value_t(* read)(void)
Sample TDO and return the value.
Definition: bitbang.h:32
Definition: parport.c:50
uint8_t TDO_MASK
Definition: parport.c:52
uint8_t OUTPUT_INVERT
Definition: parport.c:58
uint8_t LED_MASK
Definition: parport.c:62
uint8_t SRST_MASK
Definition: parport.c:57
uint8_t TCK_MASK
Definition: parport.c:55
uint8_t TDI_MASK
Definition: parport.c:56
uint8_t PORT_INIT
Definition: parport.c:60
uint8_t INPUT_INVERT
Definition: parport.c:59
uint8_t TRST_MASK
Definition: parport.c:53
const char * name
Definition: parport.c:51
uint8_t PORT_EXIT
Definition: parport.c:61
uint8_t TMS_MASK
Definition: parport.c:54
const char * name
Definition: command.h:235
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:241
Represents a driver for a debugging interface.
Definition: interface.h:182
unsigned supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:186
#define NULL
Definition: usb.h:16