jtag: remove useless declarations
[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 "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 typedef struct cable_s
63 {
64 char* name;
65 uint8_t TDO_MASK; /* status port bit containing current TDO value */
66 uint8_t TRST_MASK; /* data port bit for TRST */
67 uint8_t TMS_MASK; /* data port bit for TMS */
68 uint8_t TCK_MASK; /* data port bit for TCK */
69 uint8_t TDI_MASK; /* data port bit for TDI */
70 uint8_t SRST_MASK; /* data port bit for SRST */
71 uint8_t OUTPUT_INVERT; /* data port bits that should be inverted */
72 uint8_t INPUT_INVERT; /* status port that should be inverted */
73 uint8_t PORT_INIT; /* initialize data port with this value */
74 uint8_t PORT_EXIT; /* de-initialize data port with this value */
75 uint8_t LED_MASK; /* data port bit for LED */
76 } cable_t;
77
78 static cable_t cables[] =
79 {
80 /* name tdo trst tms tck tdi srst o_inv i_inv init exit led */
81 { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 },
82 { "wiggler2", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 },
83 { "wiggler_ntrst_inverted",
84 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80, 0x80, 0x00 },
85 { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 },
86 { "arm-jtag", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x01, 0x80, 0x80, 0x80, 0x00 },
87 { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
88 { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 },
89 { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
90 { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 },
91 { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 },
92 /* Altium Universal JTAG cable. Set the cable to Xilinx Mode and wire to target as follows:
93 HARD TCK - Target TCK
94 HARD TMS - Target TMS
95 HARD TDI - Target TDI
96 HARD TDO - Target TDO
97 SOFT TCK - Target TRST
98 SOFT TDI - Target SRST
99 */
100 { "altium", 0x10, 0x20, 0x04, 0x02, 0x01, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08 },
101 { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
102 };
103
104 /* configuration */
105 static char* parport_cable = NULL;
106 static uint16_t parport_port;
107 static int parport_exit = 0;
108
109 /* interface variables
110 */
111 static cable_t* 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 = jtag_get_speed() + 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 return ERROR_OK;
208 }
209
210 #if PARPORT_USE_GIVEIO == 1
211 static int parport_get_giveio_access(void)
212 {
213 HANDLE h;
214 OSVERSIONINFO version;
215
216 version.dwOSVersionInfoSize = sizeof version;
217 if (!GetVersionEx(&version)) {
218 errno = EINVAL;
219 return -1;
220 }
221 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
222 return 0;
223
224 h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
225 if (h == INVALID_HANDLE_VALUE) {
226 errno = ENODEV;
227 return -1;
228 }
229
230 CloseHandle(h);
231
232 return 0;
233 }
234 #endif
235
236 static bitbang_interface_t parport_bitbang = {
237 .read = &parport_read,
238 .write = &parport_write,
239 .reset = &parport_reset,
240 .blink = &parport_led,
241 };
242
243 static int parport_init(void)
244 {
245 cable_t *cur_cable;
246 #if PARPORT_USE_PPDEV == 1
247 char buffer[256];
248 int i = 0;
249 #endif
250
251 cur_cable = cables;
252
253 if ((parport_cable == NULL) || (parport_cable[0] == 0))
254 {
255 parport_cable = "wiggler";
256 LOG_WARNING("No parport cable specified, using default 'wiggler'");
257 }
258
259 while (cur_cable->name)
260 {
261 if (strcmp(cur_cable->name, parport_cable) == 0)
262 {
263 cable = cur_cable;
264 break;
265 }
266 cur_cable++;
267 }
268
269 if (!cable)
270 {
271 LOG_ERROR("No matching cable found for %s", parport_cable);
272 return ERROR_JTAG_INIT_FAILED;
273 }
274
275 dataport_value = cable->PORT_INIT;
276
277 #if PARPORT_USE_PPDEV == 1
278 if (device_handle > 0)
279 {
280 LOG_ERROR("device is already opened");
281 return ERROR_JTAG_INIT_FAILED;
282 }
283
284 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
285 LOG_DEBUG("opening /dev/ppi%d...", parport_port);
286
287 snprintf(buffer, 256, "/dev/ppi%d", parport_port);
288 device_handle = open(buffer, O_WRONLY);
289 #else /* not __FreeBSD__, __FreeBSD_kernel__ */
290 LOG_DEBUG("opening /dev/parport%d...", parport_port);
291
292 snprintf(buffer, 256, "/dev/parport%d", parport_port);
293 device_handle = open(buffer, O_WRONLY);
294 #endif /* __FreeBSD__, __FreeBSD_kernel__ */
295
296 if (device_handle < 0)
297 {
298 int err = errno;
299 LOG_ERROR("cannot open device. check it exists and that user read and write rights are set. errno=%d", err);
300 return ERROR_JTAG_INIT_FAILED;
301 }
302
303 LOG_DEBUG("...open");
304
305 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
306 i = ioctl(device_handle, PPCLAIM);
307 if (i < 0)
308 {
309 LOG_ERROR("cannot claim device");
310 return ERROR_JTAG_INIT_FAILED;
311 }
312
313 i = PARPORT_MODE_COMPAT;
314 i= ioctl(device_handle, PPSETMODE, & i);
315 if (i < 0)
316 {
317 LOG_ERROR(" cannot set compatible mode to device");
318 return ERROR_JTAG_INIT_FAILED;
319 }
320
321 i = IEEE1284_MODE_COMPAT;
322 i = ioctl(device_handle, PPNEGOT, & i);
323 if (i < 0)
324 {
325 LOG_ERROR("cannot set compatible 1284 mode to device");
326 return ERROR_JTAG_INIT_FAILED;
327 }
328 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
329
330 #else /* not PARPORT_USE_PPDEV */
331 if (parport_port == 0)
332 {
333 parport_port = 0x378;
334 LOG_WARNING("No parport port specified, using default '0x378' (LPT1)");
335 }
336
337 dataport = parport_port;
338 statusport = parport_port + 1;
339
340 LOG_DEBUG("requesting privileges for parallel port 0x%lx...", dataport);
341 #if PARPORT_USE_GIVEIO == 1
342 if (parport_get_giveio_access() != 0)
343 #else /* PARPORT_USE_GIVEIO */
344 if (ioperm(dataport, 3, 1) != 0)
345 #endif /* PARPORT_USE_GIVEIO */
346 {
347 LOG_ERROR("missing privileges for direct i/o");
348 return ERROR_JTAG_INIT_FAILED;
349 }
350 LOG_DEBUG("...privileges granted");
351
352 /* make sure parallel port is in right mode (clear tristate and interrupt */
353 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
354 outb(parport_port + 2, 0x0);
355 #else
356 outb(0x0, parport_port + 2);
357 #endif
358
359 #endif /* PARPORT_USE_PPDEV */
360
361 parport_reset(0, 0);
362 parport_write(0, 0, 0);
363 parport_led(1);
364
365 bitbang_interface = &parport_bitbang;
366
367 return ERROR_OK;
368 }
369
370 static int parport_quit(void)
371 {
372 parport_led(0);
373
374 if (parport_exit)
375 {
376 dataport_value = cable->PORT_EXIT;
377 parport_write_data();
378 }
379
380 if (parport_cable)
381 {
382 free(parport_cable);
383 parport_cable = NULL;
384 }
385
386 return ERROR_OK;
387 }
388
389 static int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
390 {
391 if (argc == 1)
392 {
393 /* only if the port wasn't overwritten by cmdline */
394 if (parport_port == 0)
395 {
396 COMMAND_PARSE_NUMBER(u16, args[0], parport_port);
397 }
398 else
399 {
400 LOG_ERROR("The parport port was already configured!");
401 return ERROR_FAIL;
402 }
403 }
404
405 command_print(cmd_ctx, "parport port = %u", parport_port);
406
407 return ERROR_OK;
408 }
409
410 static int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
411 {
412 if (argc == 0)
413 return ERROR_OK;
414
415 /* only if the cable name wasn't overwritten by cmdline */
416 if (parport_cable == 0)
417 {
418 parport_cable = malloc(strlen(args[0]) + sizeof(char));
419 strcpy(parport_cable, args[0]);
420 }
421
422 return ERROR_OK;
423 }
424
425 static int parport_handle_write_on_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
426 {
427 if (argc != 1)
428 {
429 command_print(cmd_ctx, "usage: parport_write_on_exit <on | off>");
430 return ERROR_OK;
431 }
432
433 if (strcmp(args[0], "on") == 0)
434 parport_exit = 1;
435 else if (strcmp(args[0], "off") == 0)
436 parport_exit = 0;
437
438 return ERROR_OK;
439 }
440
441 static int parport_register_commands(struct command_context_s *cmd_ctx)
442 {
443 register_command(cmd_ctx, NULL, "parport_port",
444 parport_handle_parport_port_command, COMMAND_CONFIG,
445 "either the address of the I/O port "
446 "or the number of the '/dev/parport' device");
447
448 register_command(cmd_ctx, NULL, "parport_cable",
449 parport_handle_parport_cable_command, COMMAND_CONFIG,
450 "the layout of the parallel port cable "
451 "used to connect to the target");
452
453 register_command(cmd_ctx, NULL, "parport_write_on_exit",
454 parport_handle_write_on_exit_command, COMMAND_CONFIG,
455 "configure the parallel driver to write "
456 "a known value to the parallel interface");
457
458 return ERROR_OK;
459 }
460
461 jtag_interface_t parport_interface = {
462 .name = "parport",
463 .register_commands = &parport_register_commands,
464 .init = &parport_init,
465 .quit = &parport_quit,
466 .speed = &parport_speed,
467 .execute_queue = &bitbang_execute_queue,
468 };