jtag: linuxgpiod: drop extra parenthesis
[openocd.git] / src / jtag / drivers / parport.c
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
109 static bb_value_t parport_read(void)
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;
128 output = dataport_value ^ cable->OUTPUT_INVERT;
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)
146 dataport_value |= cable->TCK_MASK;
147 else
148 dataport_value &= ~cable->TCK_MASK;
149
150 if (tms)
151 dataport_value |= cable->TMS_MASK;
152 else
153 dataport_value &= ~cable->TMS_MASK;
154
155 if (tdi)
156 dataport_value |= cable->TDI_MASK;
157 else
158 dataport_value &= ~cable->TDI_MASK;
159
160 while (i-- > 0)
161 parport_write_data();
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)
172 dataport_value |= cable->TRST_MASK;
173 else if (trst == 1)
174 dataport_value &= ~cable->TRST_MASK;
175
176 if (srst == 0)
177 dataport_value |= cable->SRST_MASK;
178 else if (srst == 1)
179 dataport_value &= ~cable->SRST_MASK;
180
181 parport_write_data();
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)
190 dataport_value |= cable->LED_MASK;
191 else
192 dataport_value &= ~cable->LED_MASK;
193
194 parport_write_data();
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
283 dataport_value = cable->PORT_INIT;
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
340 dataport = parport_port;
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
370 bitbang_interface = &parport_bitbang;
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) {
381 dataport_value = cable->PORT_EXIT;
382 parport_write_data();
383 }
384
385 free(parport_cable);
386 parport_cable = NULL;
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)
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;
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)
432 return ERROR_COMMAND_SYNTAX_ERROR;
433
434 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], parport_exit);
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
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;
461 }
462 }
463
464 command_print(CMD, "parport toggling time = %" PRIu32 " ns",
465 parport_toggling_time_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 },
505 COMMAND_REGISTRATION_DONE
506 };
507
508 static const struct command_registration parport_command_handlers[] = {
509 {
510 .name = "parport",
511 .mode = COMMAND_ANY,
512 .help = "perform parport management",
513 .chain = parport_subcommand_handlers,
514 .usage = "",
515 },
516 COMMAND_REGISTRATION_DONE
517 };
518
519 static struct jtag_interface parport_interface = {
520 .supported = DEBUG_CAP_TMS_SEQ,
521 .execute_queue = bitbang_execute_queue,
522 };
523
524 struct adapter_driver parport_adapter_driver = {
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 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)