1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
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. *
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. *
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 ***************************************************************************/
24 #include "replacements.h"
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))
47 #endif /* __FreeBSD__ */
53 #if PARPORT_USE_PPDEV == 1
55 #include <dev/ppbus/ppi.h>
56 #include <dev/ppbus/ppbconf.h>
57 #define PPRSTATUS PPIGSTATUS
58 #define PPWDATA PPISDATA
60 #include <linux/parport.h>
61 #include <linux/ppdev.h>
64 #include <sys/ioctl.h>
67 #if PARPORT_USE_GIVEIO == 1
77 /* parallel port cable description
79 typedef struct cable_s
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 */
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 }
109 unsigned long parport_port;
111 /* interface variables
113 static cable_t* cable;
114 static u8 dataport_value = 0x0;
116 #if PARPORT_USE_PPDEV == 1
117 static int device_handle;
119 static unsigned long dataport;
120 static unsigned long statusport;
123 /* low level command set
125 int parport_read(void);
126 void parport_write(int tck, int tms, int tdi);
127 void parport_reset(int trst, int srst);
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);
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);
138 jtag_interface_t parport_interface =
142 .execute_queue = bitbang_execute_queue,
144 .support_pathmove = 1,
146 .speed = parport_speed,
147 .register_commands = parport_register_commands,
148 .init = parport_init,
149 .quit = parport_quit,
152 bitbang_interface_t parport_bitbang =
154 .read = parport_read,
155 .write = parport_write,
156 .reset = parport_reset
159 int parport_read(void)
163 #if PARPORT_USE_PPDEV == 1
164 ioctl(device_handle, PPRSTATUS, & data);
166 data = inb(statusport);
169 if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
175 void parport_write(int tck, int tms, int tdi)
178 int i = jtag_speed + 1;
181 dataport_value |= cable->TCK_MASK;
183 dataport_value &= ~cable->TCK_MASK;
186 dataport_value |= cable->TMS_MASK;
188 dataport_value &= ~cable->TMS_MASK;
191 dataport_value |= cable->TDI_MASK;
193 dataport_value &= ~cable->TDI_MASK;
195 output = dataport_value ^ cable->OUTPUT_INVERT;
198 #if PARPORT_USE_PPDEV == 1
199 ioctl(device_handle, PPWDATA, &output);
202 outb(dataport, output);
204 outb(output, dataport);
209 /* (1) assert or (0) deassert reset lines */
210 void parport_reset(int trst, int srst)
213 DEBUG("trst: %i, srst: %i", trst, srst);
216 dataport_value |= cable->TRST_MASK;
218 dataport_value &= ~cable->TRST_MASK;
221 dataport_value |= cable->SRST_MASK;
223 dataport_value &= ~cable->SRST_MASK;
225 output = dataport_value ^ cable->OUTPUT_INVERT;
227 #if PARPORT_USE_PPDEV == 1
228 ioctl(device_handle, PPWDATA, &output);
231 outb(dataport, output);
233 outb(output, dataport);
239 int parport_speed(int speed)
246 int parport_register_commands(struct command_context_s *cmd_ctx)
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);
256 #if PARPORT_USE_GIVEIO == 1
257 int parport_get_giveio_access()
260 OSVERSIONINFO version;
262 version.dwOSVersionInfoSize = sizeof version;
263 if (!GetVersionEx( &version )) {
267 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
270 h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
271 if (h == INVALID_HANDLE_VALUE) {
282 int parport_init(void)
285 #if PARPORT_USE_PPDEV == 1
292 if ((parport_cable == NULL) || (parport_cable[0] == 0))
294 parport_cable = "wiggler";
295 WARNING("No parport cable specified, using default 'wiggler'");
298 while (cur_cable->name)
300 if (strcmp(cur_cable->name, parport_cable) == 0)
310 ERROR("No matching cable found for %s", parport_cable);
311 return ERROR_JTAG_INIT_FAILED;
314 dataport_value = cable->PORT_INIT;
316 #if PARPORT_USE_PPDEV == 1
317 if (device_handle > 0)
319 ERROR("device is already opened");
320 return ERROR_JTAG_INIT_FAILED;
324 DEBUG("opening /dev/ppi%d...", parport_port);
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);
331 snprintf(buffer, 256, "/dev/parport%d", parport_port);
332 device_handle = open(buffer, O_WRONLY);
333 #endif /* __FreeBSD__ */
335 if (device_handle < 0)
337 ERROR("cannot open device. check it exists and that user read and write rights are set");
338 return ERROR_JTAG_INIT_FAILED;
344 i=ioctl(device_handle, PPCLAIM);
347 ERROR("cannot claim device");
348 return ERROR_JTAG_INIT_FAILED;
351 i = PARPORT_MODE_COMPAT;
352 i= ioctl(device_handle, PPSETMODE, & i);
355 ERROR(" cannot set compatible mode to device");
356 return ERROR_JTAG_INIT_FAILED;
359 i = IEEE1284_MODE_COMPAT;
360 i = ioctl(device_handle, PPNEGOT, & i);
363 ERROR("cannot set compatible 1284 mode to device");
364 return ERROR_JTAG_INIT_FAILED;
366 #endif /* not __Free_BSD__ */
368 #else /* not PARPORT_USE_PPDEV */
369 if (parport_port == 0)
371 parport_port = 0x378;
372 WARNING("No parport port specified, using default '0x378' (LPT1)");
375 dataport = parport_port;
376 statusport = parport_port + 1;
378 DEBUG("requesting privileges for parallel port 0x%lx...", 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 */
385 ERROR("missing privileges for direct i/o");
386 return ERROR_JTAG_INIT_FAILED;
388 DEBUG("...privileges granted");
390 /* make sure parallel port is in right mode (clear tristate and interrupt */
392 outb(parport_port + 2, 0x0);
394 outb(0x0, parport_port + 2);
397 #endif /* PARPORT_USE_PPDEV */
400 parport_write(0, 0, 0);
402 bitbang_interface = &parport_bitbang;
407 int parport_quit(void)
413 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
418 /* only if the port wasn't overwritten by cmdline */
419 if (parport_port == 0)
420 parport_port = strtoul(args[0], NULL, 0);
425 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
430 /* only if the cable name wasn't overwritten by cmdline */
431 if (parport_cable == 0)
433 parport_cable = malloc(strlen(args[0]) + sizeof(char));
434 strcpy(parport_cable, args[0]);