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

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)