parport: output port as hex rather than dec
[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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <jtag/interface.h>
28 #include "bitbang.h"
29
30 /* -ino: 060521-1036 */
31 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
32 #include <machine/sysarch.h>
33 #include <machine/cpufunc.h>
34 #define ioperm(startport,length,enable)\
35 i386_set_ioperm((startport), (length), (enable))
36 #endif /* __FreeBSD__ */
37
38 #if PARPORT_USE_PPDEV == 1
39 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
40 #include <dev/ppbus/ppi.h>
41 #include <dev/ppbus/ppbconf.h>
42 #define PPRSTATUS PPIGSTATUS
43 #define PPWDATA PPISDATA
44 #else
45 #include <linux/parport.h>
46 #include <linux/ppdev.h>
47 #endif
48 #include <sys/ioctl.h>
49 #else /* not PARPORT_USE_PPDEV */
50 #ifndef _WIN32
51 #include <sys/io.h>
52 #endif
53 #endif
54
55 #if PARPORT_USE_GIVEIO == 1 && IS_CYGWIN == 1
56 #include <windows.h>
57 #endif
58
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 {
79 /* name tdo trst tms tck tdi srst o_inv i_inv init exit led */
80 { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 },
81 { "wiggler2", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 },
82 { "wiggler_ntrst_inverted", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80, 0x80, 0x00 },
83 { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 },
84 { "arm-jtag", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x01, 0x80, 0x80, 0x80, 0x00 },
85 { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
86 { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 },
87 { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
88 { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 },
89 { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 },
90 /* Altium Universal JTAG cable. Set the cable to Xilinx Mode and wire to target as follows:
91 HARD TCK - Target TCK
92 HARD TMS - Target TMS
93 HARD TDI - Target TDI
94 HARD TDO - Target TDO
95 SOFT TCK - Target TRST
96 SOFT TDI - Target SRST
97 */
98 { "altium", 0x10, 0x20, 0x04, 0x02, 0x01, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08 },
99 { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
100 };
101
102 /* configuration */
103 static char* parport_cable = NULL;
104 static uint16_t parport_port;
105 static bool parport_exit = 0;
106 static uint32_t parport_toggling_time_ns = 1000;
107 static int wait_states;
108
109 /* interface variables
110 */
111 static struct cable* cable;
112 static uint8_t dataport_value = 0x0;
113
114 #if PARPORT_USE_PPDEV == 1
115 static int device_handle;
116 #else
117 static unsigned long dataport;
118 static unsigned long statusport;
119 #endif
120
121 static int parport_read(void)
122 {
123 int data = 0;
124
125 #if PARPORT_USE_PPDEV == 1
126 ioctl(device_handle, PPRSTATUS, & data);
127 #else
128 data = inb(statusport);
129 #endif
130
131 if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
132 return 1;
133 else
134 return 0;
135 }
136
137 static __inline__ void parport_write_data(void)
138 {
139 uint8_t output;
140 output = dataport_value ^ cable->OUTPUT_INVERT;
141
142 #if PARPORT_USE_PPDEV == 1
143 ioctl(device_handle, PPWDATA, &output);
144 #else
145 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
146 outb(dataport, output);
147 #else
148 outb(output, dataport);
149 #endif
150 #endif
151 }
152
153 static void parport_write(int tck, int tms, int tdi)
154 {
155 int i = wait_states + 1;
156
157 if (tck)
158 dataport_value |= cable->TCK_MASK;
159 else
160 dataport_value &= ~cable->TCK_MASK;
161
162 if (tms)
163 dataport_value |= cable->TMS_MASK;
164 else
165 dataport_value &= ~cable->TMS_MASK;
166
167 if (tdi)
168 dataport_value |= cable->TDI_MASK;
169 else
170 dataport_value &= ~cable->TDI_MASK;
171
172 while (i-- > 0)
173 parport_write_data();
174 }
175
176 /* (1) assert or (0) deassert reset lines */
177 static void parport_reset(int trst, int srst)
178 {
179 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
180
181 if (trst == 0)
182 dataport_value |= cable->TRST_MASK;
183 else if (trst == 1)
184 dataport_value &= ~cable->TRST_MASK;
185
186 if (srst == 0)
187 dataport_value |= cable->SRST_MASK;
188 else if (srst == 1)
189 dataport_value &= ~cable->SRST_MASK;
190
191 parport_write_data();
192 }
193
194 /* turn LED on parport adapter on (1) or off (0) */
195 static void parport_led(int on)
196 {
197 if (on)
198 dataport_value |= cable->LED_MASK;
199 else
200 dataport_value &= ~cable->LED_MASK;
201
202 parport_write_data();
203 }
204
205 static int parport_speed(int speed)
206 {
207 wait_states = speed;
208 return ERROR_OK;
209 }
210
211 static int parport_khz(int khz, int* jtag_speed)
212 {
213 if (khz == 0) {
214 LOG_DEBUG("RCLK not supported");
215 return ERROR_FAIL;
216 }
217
218 *jtag_speed = 499999 / (khz * parport_toggling_time_ns);
219 return ERROR_OK;
220 }
221
222 static int parport_speed_div(int speed, int* khz)
223 {
224 uint32_t denominator = (speed + 1) * parport_toggling_time_ns;
225
226 *khz = (499999 + denominator) / denominator;
227 return ERROR_OK;
228 }
229
230 #if PARPORT_USE_GIVEIO == 1
231 static int parport_get_giveio_access(void)
232 {
233 HANDLE h;
234 OSVERSIONINFO version;
235
236 version.dwOSVersionInfoSize = sizeof version;
237 if (!GetVersionEx(&version)) {
238 errno = EINVAL;
239 return -1;
240 }
241 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
242 return 0;
243
244 h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
245 if (h == INVALID_HANDLE_VALUE) {
246 errno = ENODEV;
247 return -1;
248 }
249
250 CloseHandle(h);
251
252 return 0;
253 }
254 #endif
255
256 static struct bitbang_interface parport_bitbang = {
257 .read = &parport_read,
258 .write = &parport_write,
259 .reset = &parport_reset,
260 .blink = &parport_led,
261 };
262
263 static int parport_init(void)
264 {
265 struct cable *cur_cable;
266 #if PARPORT_USE_PPDEV == 1
267 char buffer[256];
268 int i = 0;
269 #endif
270
271 cur_cable = cables;
272
273 if ((parport_cable == NULL) || (parport_cable[0] == 0))
274 {
275 parport_cable = "wiggler";
276 LOG_WARNING("No parport cable specified, using default 'wiggler'");
277 }
278
279 while (cur_cable->name)
280 {
281 if (strcmp(cur_cable->name, parport_cable) == 0)
282 {
283 cable = cur_cable;
284 break;
285 }
286 cur_cable++;
287 }
288
289 if (!cable)
290 {
291 LOG_ERROR("No matching cable found for %s", parport_cable);
292 return ERROR_JTAG_INIT_FAILED;
293 }
294
295 dataport_value = cable->PORT_INIT;
296
297 #if PARPORT_USE_PPDEV == 1
298 if (device_handle > 0)
299 {
300 LOG_ERROR("device is already opened");
301 return ERROR_JTAG_INIT_FAILED;
302 }
303
304 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
305 LOG_DEBUG("opening /dev/ppi%d...", parport_port);
306
307 snprintf(buffer, 256, "/dev/ppi%d", parport_port);
308 device_handle = open(buffer, O_WRONLY);
309 #else /* not __FreeBSD__, __FreeBSD_kernel__ */
310 LOG_DEBUG("opening /dev/parport%d...", parport_port);
311
312 snprintf(buffer, 256, "/dev/parport%d", parport_port);
313 device_handle = open(buffer, O_WRONLY);
314 #endif /* __FreeBSD__, __FreeBSD_kernel__ */
315
316 if (device_handle < 0)
317 {
318 int err = errno;
319 LOG_ERROR("cannot open device. check it exists and that user read and write rights are set. errno=%d", err);
320 return ERROR_JTAG_INIT_FAILED;
321 }
322
323 LOG_DEBUG("...open");
324
325 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
326 i = ioctl(device_handle, PPCLAIM);
327 if (i < 0)
328 {
329 LOG_ERROR("cannot claim device");
330 return ERROR_JTAG_INIT_FAILED;
331 }
332
333 i = PARPORT_MODE_COMPAT;
334 i= ioctl(device_handle, PPSETMODE, & i);
335 if (i < 0)
336 {
337 LOG_ERROR(" cannot set compatible mode to device");
338 return ERROR_JTAG_INIT_FAILED;
339 }
340
341 i = IEEE1284_MODE_COMPAT;
342 i = ioctl(device_handle, PPNEGOT, & i);
343 if (i < 0)
344 {
345 LOG_ERROR("cannot set compatible 1284 mode to device");
346 return ERROR_JTAG_INIT_FAILED;
347 }
348 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
349
350 #else /* not PARPORT_USE_PPDEV */
351 if (parport_port == 0)
352 {
353 parport_port = 0x378;
354 LOG_WARNING("No parport port specified, using default '0x378' (LPT1)");
355 }
356
357 dataport = parport_port;
358 statusport = parport_port + 1;
359
360 LOG_DEBUG("requesting privileges for parallel port 0x%lx...", dataport);
361 #if PARPORT_USE_GIVEIO == 1
362 if (parport_get_giveio_access() != 0)
363 #else /* PARPORT_USE_GIVEIO */
364 if (ioperm(dataport, 3, 1) != 0)
365 #endif /* PARPORT_USE_GIVEIO */
366 {
367 LOG_ERROR("missing privileges for direct i/o");
368 return ERROR_JTAG_INIT_FAILED;
369 }
370 LOG_DEBUG("...privileges granted");
371
372 /* make sure parallel port is in right mode (clear tristate and interrupt */
373 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
374 outb(parport_port + 2, 0x0);
375 #else
376 outb(0x0, parport_port + 2);
377 #endif
378
379 #endif /* PARPORT_USE_PPDEV */
380
381 parport_reset(0, 0);
382 parport_write(0, 0, 0);
383 parport_led(1);
384
385 bitbang_interface = &parport_bitbang;
386
387 wait_states = jtag_get_speed();
388
389 return ERROR_OK;
390 }
391
392 static int parport_quit(void)
393 {
394 parport_led(0);
395
396 if (parport_exit)
397 {
398 dataport_value = cable->PORT_EXIT;
399 parport_write_data();
400 }
401
402 if (parport_cable)
403 {
404 free(parport_cable);
405 parport_cable = NULL;
406 }
407
408 return ERROR_OK;
409 }
410
411 COMMAND_HANDLER(parport_handle_parport_port_command)
412 {
413 if (CMD_ARGC == 1)
414 {
415 /* only if the port wasn't overwritten by cmdline */
416 if (parport_port == 0)
417 {
418 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], parport_port);
419 }
420 else
421 {
422 LOG_ERROR("The parport port was already configured!");
423 return ERROR_FAIL;
424 }
425 }
426
427 command_print(CMD_CTX, "parport port = 0x%" PRIx16 "", parport_port);
428
429 return ERROR_OK;
430 }
431
432 COMMAND_HANDLER(parport_handle_parport_cable_command)
433 {
434 if (CMD_ARGC == 0)
435 return ERROR_OK;
436
437 /* only if the cable name wasn't overwritten by cmdline */
438 if (parport_cable == 0)
439 {
440 parport_cable = malloc(strlen(CMD_ARGV[0]) + sizeof(char));
441 strcpy(parport_cable, CMD_ARGV[0]);
442 }
443
444 return ERROR_OK;
445 }
446
447 COMMAND_HANDLER(parport_handle_write_on_exit_command)
448 {
449 if (CMD_ARGC != 1)
450 {
451 command_print(CMD_CTX, "usage: parport_write_on_exit <on | off>");
452 return ERROR_OK;
453 }
454
455 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], parport_exit);
456
457 return ERROR_OK;
458 }
459
460 COMMAND_HANDLER(parport_handle_parport_toggling_time_command)
461 {
462 if (CMD_ARGC == 1) {
463 uint32_t ns;
464 int retval = parse_u32(CMD_ARGV[0], &ns);
465
466 if (ERROR_OK != retval)
467 return retval;
468
469 if (ns == 0) {
470 LOG_ERROR("0 ns is not a valid parport toggling time");
471 return ERROR_FAIL;
472 }
473
474 parport_toggling_time_ns = ns;
475 wait_states = jtag_get_speed();
476 }
477
478 command_print(CMD_CTX, "parport toggling time = %" PRIu32 " ns",
479 parport_toggling_time_ns);
480
481 return ERROR_OK;
482 }
483
484 static const struct command_registration parport_command_handlers[] = {
485 {
486 .name = "parport_port",
487 .handler = &parport_handle_parport_port_command,
488 .mode = COMMAND_CONFIG,
489 .help = "either the address of the I/O port "
490 "or the number of the '/dev/parport' device",
491 .usage = "[<port|devname>]",
492 },
493 {
494 .name = "parport_cable",
495 .handler = &parport_handle_parport_cable_command,
496 .mode = COMMAND_CONFIG,
497 .help = "the layout of the parallel port cable "
498 "used to connect to the target",
499 .usage = "[<layout>]",
500 },
501 {
502 .name = "parport_write_on_exit",
503 .handler = &parport_handle_write_on_exit_command,
504 .mode = COMMAND_CONFIG,
505 .help = "configure the parallel driver to write "
506 "a known value to the parallel interface",
507 .usage = "[<on|off>]",
508 },
509 {
510 .name = "parport_toggling_time",
511 .handler = &parport_handle_parport_toggling_time_command,
512 .mode = COMMAND_CONFIG,
513 .help = "time <ns> it takes for the hardware to toggle TCK",
514 .usage = "[<ns>]",
515 },
516 COMMAND_REGISTRATION_DONE
517 };
518
519 struct jtag_interface parport_interface = {
520 .name = "parport",
521 .commands = parport_command_handlers,
522
523 .init = parport_init,
524 .quit = parport_quit,
525 .khz = parport_khz,
526 .speed_div = parport_speed_div,
527 .speed = parport_speed,
528 .execute_queue = bitbang_execute_queue,
529 };

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)