parport: fix parport_toggling_time regression
[openocd.git] / src / jtag / drivers / parport.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <jtag/interface.h>
29 #include "bitbang.h"
30
31 /* -ino: 060521-1036 */
32 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
33 #include <machine/sysarch.h>
34 #include <machine/cpufunc.h>
35 #define ioperm(startport, length, enable)\
36 i386_set_ioperm((startport), (length), (enable))
37 #endif /* __FreeBSD__ */
38
39 #if PARPORT_USE_PPDEV == 1
40 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
41 #include <dev/ppbus/ppi.h>
42 #include <dev/ppbus/ppbconf.h>
43 #define PPRSTATUS PPIGSTATUS
44 #define PPWDATA PPISDATA
45 #else
46 #include <linux/parport.h>
47 #include <linux/ppdev.h>
48 #endif
49 #include <sys/ioctl.h>
50 #else /* not PARPORT_USE_PPDEV */
51 #ifndef _WIN32
52 #include <sys/io.h>
53 #endif
54 #endif
55
56 #if PARPORT_USE_GIVEIO == 1 && IS_CYGWIN == 1
57 #include <windows.h>
58 #endif
59
60 /* parallel port cable description
61 */
62 struct cable {
63 char *name;
64 uint8_t TDO_MASK; /* status port bit containing current TDO value */
65 uint8_t TRST_MASK; /* data port bit for TRST */
66 uint8_t TMS_MASK; /* data port bit for TMS */
67 uint8_t TCK_MASK; /* data port bit for TCK */
68 uint8_t TDI_MASK; /* data port bit for TDI */
69 uint8_t SRST_MASK; /* data port bit for SRST */
70 uint8_t OUTPUT_INVERT; /* data port bits that should be inverted */
71 uint8_t INPUT_INVERT; /* status port that should be inverted */
72 uint8_t PORT_INIT; /* initialize data port with this value */
73 uint8_t PORT_EXIT; /* de-initialize data port with this value */
74 uint8_t LED_MASK; /* data port bit for LED */
75 };
76
77 static struct cable cables[] = {
78 /* name tdo trst tms tck tdi srst o_inv i_inv init exit led */
79 { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 },
80 { "wiggler2", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 },
81 { "wiggler_ntrst_inverted", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80, 0x80, 0x00 },
82 { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 },
83 { "arm-jtag", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x01, 0x80, 0x80, 0x80, 0x00 },
84 { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
85 { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 },
86 { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
87 { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 },
88 { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 },
89 /* Altium Universal JTAG cable. Set the cable to Xilinx Mode and wire to target as follows:
90 HARD TCK - Target TCK
91 HARD TMS - Target TMS
92 HARD TDI - Target TDI
93 HARD TDO - Target TDO
94 SOFT TCK - Target TRST
95 SOFT TDI - Target SRST
96 */
97 { "altium", 0x10, 0x20, 0x04, 0x02, 0x01, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08 },
98 { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
99 };
100
101 /* configuration */
102 static char *parport_cable;
103 static uint16_t parport_port;
104 static bool parport_exit;
105 static uint32_t parport_toggling_time_ns = 1000;
106 static int wait_states;
107
108 /* interface variables
109 */
110 static struct cable *cable;
111 static uint8_t dataport_value;
112
113 #if PARPORT_USE_PPDEV == 1
114 static int device_handle;
115 #else
116 static unsigned long dataport;
117 static unsigned long statusport;
118 #endif
119
120 static int parport_read(void)
121 {
122 int data = 0;
123
124 #if PARPORT_USE_PPDEV == 1
125 ioctl(device_handle, PPRSTATUS, &data);
126 #else
127 data = inb(statusport);
128 #endif
129
130 if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
131 return 1;
132 else
133 return 0;
134 }
135
136 static inline void parport_write_data(void)
137 {
138 uint8_t output;
139 output = dataport_value ^ cable->OUTPUT_INVERT;
140
141 #if PARPORT_USE_PPDEV == 1
142 ioctl(device_handle, PPWDATA, &output);
143 #else
144 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
145 outb(dataport, output);
146 #else
147 outb(output, dataport);
148 #endif
149 #endif
150 }
151
152 static void parport_write(int tck, int tms, int tdi)
153 {
154 int i = wait_states + 1;
155
156 if (tck)
157 dataport_value |= cable->TCK_MASK;
158 else
159 dataport_value &= ~cable->TCK_MASK;
160
161 if (tms)
162 dataport_value |= cable->TMS_MASK;
163 else
164 dataport_value &= ~cable->TMS_MASK;
165
166 if (tdi)
167 dataport_value |= cable->TDI_MASK;
168 else
169 dataport_value &= ~cable->TDI_MASK;
170
171 while (i-- > 0)
172 parport_write_data();
173 }
174
175 /* (1) assert or (0) deassert reset lines */
176 static void parport_reset(int trst, int srst)
177 {
178 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
179
180 if (trst == 0)
181 dataport_value |= cable->TRST_MASK;
182 else if (trst == 1)
183 dataport_value &= ~cable->TRST_MASK;
184
185 if (srst == 0)
186 dataport_value |= cable->SRST_MASK;
187 else if (srst == 1)
188 dataport_value &= ~cable->SRST_MASK;
189
190 parport_write_data();
191 }
192
193 /* turn LED on parport adapter on (1) or off (0) */
194 static void parport_led(int on)
195 {
196 if (on)
197 dataport_value |= cable->LED_MASK;
198 else
199 dataport_value &= ~cable->LED_MASK;
200
201 parport_write_data();
202 }
203
204 static int parport_speed(int speed)
205 {
206 wait_states = speed;
207 return ERROR_OK;
208 }
209
210 static int parport_khz(int khz, int *jtag_speed)
211 {
212 if (khz == 0) {
213 LOG_DEBUG("RCLK not supported");
214 return ERROR_FAIL;
215 }
216
217 *jtag_speed = 499999 / (khz * parport_toggling_time_ns);
218 return ERROR_OK;
219 }
220
221 static int parport_speed_div(int speed, int *khz)
222 {
223 uint32_t denominator = (speed + 1) * parport_toggling_time_ns;
224
225 *khz = (499999 + denominator) / denominator;
226 return ERROR_OK;
227 }
228
229 #if PARPORT_USE_GIVEIO == 1
230 static int parport_get_giveio_access(void)
231 {
232 HANDLE h;
233 OSVERSIONINFO version;
234
235 version.dwOSVersionInfoSize = sizeof version;
236 if (!GetVersionEx(&version)) {
237 errno = EINVAL;
238 return -1;
239 }
240 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
241 return 0;
242
243 h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
244 if (h == INVALID_HANDLE_VALUE) {
245 errno = ENODEV;
246 return -1;
247 }
248
249 CloseHandle(h);
250
251 return 0;
252 }
253 #endif
254
255 static struct bitbang_interface parport_bitbang = {
256 .read = &parport_read,
257 .write = &parport_write,
258 .reset = &parport_reset,
259 .blink = &parport_led,
260 };
261
262 static int parport_init(void)
263 {
264 struct cable *cur_cable;
265 #if PARPORT_USE_PPDEV == 1
266 char buffer[256];
267 #endif
268
269 cur_cable = cables;
270
271 if (parport_cable == NULL) {
272 parport_cable = strdup("wiggler");
273 LOG_WARNING("No parport cable specified, using default 'wiggler'");
274 }
275
276 while (cur_cable->name) {
277 if (strcmp(cur_cable->name, parport_cable) == 0) {
278 cable = cur_cable;
279 break;
280 }
281 cur_cable++;
282 }
283
284 if (!cable) {
285 LOG_ERROR("No matching cable found for %s", parport_cable);
286 return ERROR_JTAG_INIT_FAILED;
287 }
288
289 dataport_value = cable->PORT_INIT;
290
291 #if PARPORT_USE_PPDEV == 1
292 if (device_handle > 0) {
293 LOG_ERROR("device is already opened");
294 return ERROR_JTAG_INIT_FAILED;
295 }
296
297 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
298 LOG_DEBUG("opening /dev/ppi%d...", parport_port);
299
300 snprintf(buffer, 256, "/dev/ppi%d", parport_port);
301 device_handle = open(buffer, O_WRONLY);
302 #else /* not __FreeBSD__, __FreeBSD_kernel__ */
303 LOG_DEBUG("opening /dev/parport%d...", parport_port);
304
305 snprintf(buffer, 256, "/dev/parport%d", parport_port);
306 device_handle = open(buffer, O_WRONLY);
307 #endif /* __FreeBSD__, __FreeBSD_kernel__ */
308
309 if (device_handle < 0) {
310 int err = errno;
311 LOG_ERROR("cannot open device. check it exists and that user read and write rights are set. errno=%d", err);
312 return ERROR_JTAG_INIT_FAILED;
313 }
314
315 LOG_DEBUG("...open");
316
317 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
318 int i = ioctl(device_handle, PPCLAIM);
319
320 if (i < 0) {
321 LOG_ERROR("cannot claim device");
322 return ERROR_JTAG_INIT_FAILED;
323 }
324
325 i = PARPORT_MODE_COMPAT;
326 i = ioctl(device_handle, PPSETMODE, &i);
327 if (i < 0) {
328 LOG_ERROR(" cannot set compatible mode to device");
329 return ERROR_JTAG_INIT_FAILED;
330 }
331
332 i = IEEE1284_MODE_COMPAT;
333 i = ioctl(device_handle, PPNEGOT, &i);
334 if (i < 0) {
335 LOG_ERROR("cannot set compatible 1284 mode to device");
336 return ERROR_JTAG_INIT_FAILED;
337 }
338 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
339
340 #else /* not PARPORT_USE_PPDEV */
341 if (parport_port == 0) {
342 parport_port = 0x378;
343 LOG_WARNING("No parport port specified, using default '0x378' (LPT1)");
344 }
345
346 dataport = parport_port;
347 statusport = parport_port + 1;
348
349 LOG_DEBUG("requesting privileges for parallel port 0x%lx...", dataport);
350 #if PARPORT_USE_GIVEIO == 1
351 if (parport_get_giveio_access() != 0) {
352 #else /* PARPORT_USE_GIVEIO */
353 if (ioperm(dataport, 3, 1) != 0) {
354 #endif /* PARPORT_USE_GIVEIO */
355 LOG_ERROR("missing privileges for direct i/o");
356 return ERROR_JTAG_INIT_FAILED;
357 }
358 LOG_DEBUG("...privileges granted");
359
360 /* make sure parallel port is in right mode (clear tristate and interrupt */
361 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
362 outb(parport_port + 2, 0x0);
363 #else
364 outb(0x0, parport_port + 2);
365 #endif
366
367 #endif /* PARPORT_USE_PPDEV */
368
369 parport_reset(0, 0);
370 parport_write(0, 0, 0);
371 parport_led(1);
372
373 bitbang_interface = &parport_bitbang;
374
375 return ERROR_OK;
376 }
377
378 static int parport_quit(void)
379 {
380 parport_led(0);
381
382 if (parport_exit) {
383 dataport_value = cable->PORT_EXIT;
384 parport_write_data();
385 }
386
387 if (parport_cable) {
388 free(parport_cable);
389 parport_cable = NULL;
390 }
391
392 return ERROR_OK;
393 }
394
395 COMMAND_HANDLER(parport_handle_parport_port_command)
396 {
397 if (CMD_ARGC == 1) {
398 /* only if the port wasn't overwritten by cmdline */
399 if (parport_port == 0)
400 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], parport_port);
401 else {
402 LOG_ERROR("The parport port was already configured!");
403 return ERROR_FAIL;
404 }
405 }
406
407 command_print(CMD_CTX, "parport port = 0x%" PRIx16 "", parport_port);
408
409 return ERROR_OK;
410 }
411
412 COMMAND_HANDLER(parport_handle_parport_cable_command)
413 {
414 if (CMD_ARGC == 0)
415 return ERROR_OK;
416
417 /* only if the cable name wasn't overwritten by cmdline */
418 if (parport_cable == 0) {
419 /* REVISIT first verify that it's listed in cables[] ... */
420 parport_cable = malloc(strlen(CMD_ARGV[0]) + sizeof(char));
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 (ERROR_OK != retval)
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 = jtag_get_speed(&wait_states);
455 if (retval != ERROR_OK) {
456 /* if jtag_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_CTX, "parport toggling time = %" PRIu32 " ns",
465 parport_toggling_time_ns);
466
467 return ERROR_OK;
468 }
469
470 static const struct command_registration parport_command_handlers[] = {
471 {
472 .name = "parport_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 = "parport_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 = "parport_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 = "parport_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 struct jtag_interface parport_interface = {
509 .name = "parport",
510 .supported = DEBUG_CAP_TMS_SEQ,
511 .commands = parport_command_handlers,
512
513 .init = parport_init,
514 .quit = parport_quit,
515 .khz = parport_khz,
516 .speed_div = parport_speed_div,
517 .speed = parport_speed,
518 .execute_queue = bitbang_execute_queue,
519 };

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)