6386940e8c581d326f7f62c4af57b94d152655b5
[openocd.git] / src / jtag / parport.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "replacements.h"
25
26 #include "jtag.h"
27 #include "bitbang.h"
28
29 /* system includes */
30 // -ino: 060521-1036
31 #ifdef __FreeBSD__
32
33 #include <sys/types.h>
34 #include <machine/sysarch.h>
35 #include <machine/cpufunc.h>
36 #define ioperm(startport,length,enable)\
37 i386_set_ioperm((startport), (length), (enable))
38
39 #else
40
41 #ifndef _WIN32
42 #include <sys/io.h>
43 #else
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 #ifdef __FreeBSD__
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 #endif
66
67 #if PARPORT_USE_GIVEIO == 1
68 #if IS_CYGWIN == 1
69 #include <windows.h>
70 #include <errno.h>
71 #undef ERROR
72 #endif
73 #endif
74
75 #include "log.h"
76
77 /* parallel port cable description
78 */
79 typedef struct cable_s
80 {
81 char* name;
82 u8 TDO_MASK; /* status port bit containing current TDO value */
83 u8 TRST_MASK; /* data port bit for TRST */
84 u8 TMS_MASK; /* data port bit for TMS */
85 u8 TCK_MASK; /* data port bit for TCK */
86 u8 TDI_MASK; /* data port bit for TDI */
87 u8 SRST_MASK; /* data port bit for SRST */
88 u8 OUTPUT_INVERT; /* data port bits that should be inverted */
89 u8 INPUT_INVERT; /* status port that should be inverted */
90 u8 PORT_INIT; /* initialize data port with this value */
91 } cable_t;
92
93 cable_t cables[] =
94 {
95 /* name tdo trst tms tck tdi srst o_inv i_inv init */
96 { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80 },
97 { "wiggler_ntrst_inverted", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80 },
98 { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80 },
99 { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00 },
100 { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10 },
101 { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00 },
102 { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18 },
103 { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00 },
104 { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
105 };
106
107 /* configuration */
108 char* parport_cable;
109 unsigned long parport_port;
110
111 /* interface variables
112 */
113 static cable_t* cable;
114 static u8 dataport_value = 0x0;
115
116 #if PARPORT_USE_PPDEV == 1
117 static int device_handle;
118 #else
119 static unsigned long dataport;
120 static unsigned long statusport;
121 #endif
122
123 /* low level command set
124 */
125 int parport_read(void);
126 void parport_write(int tck, int tms, int tdi);
127 void parport_reset(int trst, int srst);
128
129 int parport_speed(int speed);
130 int parport_register_commands(struct command_context_s *cmd_ctx);
131 int parport_init(void);
132 int parport_quit(void);
133
134 /* interface commands */
135 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
136 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
137
138 jtag_interface_t parport_interface =
139 {
140 .name = "parport",
141
142 .execute_queue = bitbang_execute_queue,
143
144 .support_pathmove = 1,
145
146 .speed = parport_speed,
147 .register_commands = parport_register_commands,
148 .init = parport_init,
149 .quit = parport_quit,
150 };
151
152 bitbang_interface_t parport_bitbang =
153 {
154 .read = parport_read,
155 .write = parport_write,
156 .reset = parport_reset
157 };
158
159 int parport_read(void)
160 {
161 int data = 0;
162
163 #if PARPORT_USE_PPDEV == 1
164 ioctl(device_handle, PPRSTATUS, & data);
165 #else
166 data = inb(statusport);
167 #endif
168
169 if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
170 return 1;
171 else
172 return 0;
173 }
174
175 void parport_write(int tck, int tms, int tdi)
176 {
177 u8 output;
178 int i = jtag_speed + 1;
179
180 if (tck)
181 dataport_value |= cable->TCK_MASK;
182 else
183 dataport_value &= ~cable->TCK_MASK;
184
185 if (tms)
186 dataport_value |= cable->TMS_MASK;
187 else
188 dataport_value &= ~cable->TMS_MASK;
189
190 if (tdi)
191 dataport_value |= cable->TDI_MASK;
192 else
193 dataport_value &= ~cable->TDI_MASK;
194
195 output = dataport_value ^ cable->OUTPUT_INVERT;
196
197 while (i-- > 0)
198 #if PARPORT_USE_PPDEV == 1
199 ioctl(device_handle, PPWDATA, &output);
200 #else
201 #ifdef __FreeBSD__
202 outb(dataport, output);
203 #else
204 outb(output, dataport);
205 #endif
206 #endif
207 }
208
209 /* (1) assert or (0) deassert reset lines */
210 void parport_reset(int trst, int srst)
211 {
212 u8 output;
213 DEBUG("trst: %i, srst: %i", trst, srst);
214
215 if (trst == 0)
216 dataport_value |= cable->TRST_MASK;
217 else if (trst == 1)
218 dataport_value &= ~cable->TRST_MASK;
219
220 if (srst == 0)
221 dataport_value |= cable->SRST_MASK;
222 else if (srst == 1)
223 dataport_value &= ~cable->SRST_MASK;
224
225 output = dataport_value ^ cable->OUTPUT_INVERT;
226
227 #if PARPORT_USE_PPDEV == 1
228 ioctl(device_handle, PPWDATA, &output);
229 #else
230 #ifdef __FreeBSD__
231 outb(dataport, output);
232 #else
233 outb(output, dataport);
234 #endif
235 #endif
236
237 }
238
239 int parport_speed(int speed)
240 {
241 jtag_speed = speed;
242
243 return ERROR_OK;
244 }
245
246 int parport_register_commands(struct command_context_s *cmd_ctx)
247 {
248 register_command(cmd_ctx, NULL, "parport_port", parport_handle_parport_port_command,
249 COMMAND_CONFIG, NULL);
250 register_command(cmd_ctx, NULL, "parport_cable", parport_handle_parport_cable_command,
251 COMMAND_CONFIG, NULL);
252
253 return ERROR_OK;
254 }
255
256 #if PARPORT_USE_GIVEIO == 1
257 int parport_get_giveio_access()
258 {
259 HANDLE h;
260 OSVERSIONINFO version;
261
262 version.dwOSVersionInfoSize = sizeof version;
263 if (!GetVersionEx( &version )) {
264 errno = EINVAL;
265 return -1;
266 }
267 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
268 return 0;
269
270 h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
271 if (h == INVALID_HANDLE_VALUE) {
272 errno = ENODEV;
273 return -1;
274 }
275
276 CloseHandle( h );
277
278 return 0;
279 }
280 #endif
281
282 int parport_init(void)
283 {
284 cable_t *cur_cable;
285 #if PARPORT_USE_PPDEV == 1
286 char buffer[256];
287 int i = 0;
288 #endif
289
290 cur_cable = cables;
291
292 if ((parport_cable == NULL) || (parport_cable[0] == 0))
293 {
294 parport_cable = "wiggler";
295 WARNING("No parport cable specified, using default 'wiggler'");
296 }
297
298 while (cur_cable->name)
299 {
300 if (strcmp(cur_cable->name, parport_cable) == 0)
301 {
302 cable = cur_cable;
303 break;
304 }
305 cur_cable++;
306 }
307
308 if (!cable)
309 {
310 ERROR("No matching cable found for %s", parport_cable);
311 return ERROR_JTAG_INIT_FAILED;
312 }
313
314 dataport_value = cable->PORT_INIT;
315
316 #if PARPORT_USE_PPDEV == 1
317 if (device_handle > 0)
318 {
319 ERROR("device is already opened");
320 return ERROR_JTAG_INIT_FAILED;
321 }
322
323 #ifdef __FreeBSD__
324 DEBUG("opening /dev/ppi%d...", parport_port);
325
326 snprintf(buffer, 256, "/dev/ppi%d", parport_port);
327 device_handle = open(buffer, O_WRONLY);
328 #else /* not __Free_BSD */
329 DEBUG("opening /dev/parport%d...", parport_port);
330
331 snprintf(buffer, 256, "/dev/parport%d", parport_port);
332 device_handle = open(buffer, O_WRONLY);
333 #endif /* __FreeBSD__ */
334
335 if (device_handle < 0)
336 {
337 ERROR("cannot open device. check it exists and that user read and write rights are set");
338 return ERROR_JTAG_INIT_FAILED;
339 }
340
341 DEBUG("...open");
342
343 #ifndef __FreeBSD__
344 i=ioctl(device_handle, PPCLAIM);
345 if (i<0)
346 {
347 ERROR("cannot claim device");
348 return ERROR_JTAG_INIT_FAILED;
349 }
350
351 i = PARPORT_MODE_COMPAT;
352 i= ioctl(device_handle, PPSETMODE, & i);
353 if (i<0)
354 {
355 ERROR(" cannot set compatible mode to device");
356 return ERROR_JTAG_INIT_FAILED;
357 }
358
359 i = IEEE1284_MODE_COMPAT;
360 i = ioctl(device_handle, PPNEGOT, & i);
361 if (i<0)
362 {
363 ERROR("cannot set compatible 1284 mode to device");
364 return ERROR_JTAG_INIT_FAILED;
365 }
366 #endif /* not __Free_BSD__ */
367
368 #else /* not PARPORT_USE_PPDEV */
369 if (parport_port == 0)
370 {
371 parport_port = 0x378;
372 WARNING("No parport port specified, using default '0x378' (LPT1)");
373 }
374
375 dataport = parport_port;
376 statusport = parport_port + 1;
377
378 DEBUG("requesting privileges for parallel port 0x%x...", dataport);
379 #if PARPORT_USE_GIVEIO == 1
380 if (parport_get_giveio_access() != 0)
381 #else /* PARPORT_USE_GIVEIO */
382 if (ioperm(dataport, 3, 1) != 0)
383 #endif /* PARPORT_USE_GIVEIO */
384 {
385 ERROR("missing privileges for direct i/o");
386 return ERROR_JTAG_INIT_FAILED;
387 }
388 DEBUG("...privileges granted");
389
390 /* make sure parallel port is in right mode (clear tristate and interrupt */
391 #ifdef __FreeBSD__
392 outb(parport_port + 2, 0x0);
393 #else
394 outb(0x0, parport_port + 2);
395 #endif
396
397 #endif /* PARPORT_USE_PPDEV */
398
399 parport_reset(0, 0);
400 parport_write(0, 0, 0);
401
402 bitbang_interface = &parport_bitbang;
403
404 return ERROR_OK;
405 }
406
407 int parport_quit(void)
408 {
409
410 return ERROR_OK;
411 }
412
413 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
414 {
415 if (argc == 0)
416 return ERROR_OK;
417
418 /* only if the port wasn't overwritten by cmdline */
419 if (parport_port == 0)
420 parport_port = strtoul(args[0], NULL, 0);
421
422 return ERROR_OK;
423 }
424
425 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
426 {
427 if (argc == 0)
428 return ERROR_OK;
429
430 /* only if the cable name wasn't overwritten by cmdline */
431 if (parport_cable == 0)
432 {
433 parport_cable = malloc(strlen(args[0]) + sizeof(char));
434 strcpy(parport_cable, args[0]);
435 }
436
437 return ERROR_OK;
438 }

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)