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 { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80 },
98 { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00 },
99 { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10 },
100 { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00 },
101 { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18 },
102 { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
107 unsigned long parport_port;
109 /* interface variables
111 static cable_t* cable;
112 static u8 dataport_value = 0x0;
114 #if PARPORT_USE_PPDEV == 1
115 static int device_handle;
117 static unsigned long dataport;
118 static unsigned long statusport;
121 /* low level command set
123 int parport_read(void);
124 void parport_write(int tck, int tms, int tdi);
125 void parport_reset(int trst, int srst);
127 int parport_speed(int speed);
128 int parport_register_commands(struct command_context_s *cmd_ctx);
129 int parport_init(void);
130 int parport_quit(void);
132 /* interface commands */
133 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
134 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
136 jtag_interface_t parport_interface =
140 .execute_queue = bitbang_execute_queue,
142 .support_pathmove = 1,
144 .speed = parport_speed,
145 .register_commands = parport_register_commands,
146 .init = parport_init,
147 .quit = parport_quit,
150 bitbang_interface_t parport_bitbang =
152 .read = parport_read,
153 .write = parport_write,
154 .reset = parport_reset
157 int parport_read(void)
161 #if PARPORT_USE_PPDEV == 1
162 ioctl(device_handle, PPRSTATUS, & data);
164 data = inb(statusport);
167 if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
173 void parport_write(int tck, int tms, int tdi)
176 int i = jtag_speed + 1;
179 dataport_value |= cable->TCK_MASK;
181 dataport_value &= ~cable->TCK_MASK;
184 dataport_value |= cable->TMS_MASK;
186 dataport_value &= ~cable->TMS_MASK;
189 dataport_value |= cable->TDI_MASK;
191 dataport_value &= ~cable->TDI_MASK;
193 output = dataport_value ^ cable->OUTPUT_INVERT;
196 #if PARPORT_USE_PPDEV == 1
197 ioctl(device_handle, PPWDATA, &output);
200 outb(dataport, output);
202 outb(output, dataport);
207 /* (1) assert or (0) deassert reset lines */
208 void parport_reset(int trst, int srst)
211 DEBUG("trst: %i, srst: %i", trst, srst);
214 dataport_value |= cable->TRST_MASK;
216 dataport_value &= ~cable->TRST_MASK;
219 dataport_value |= cable->SRST_MASK;
221 dataport_value &= ~cable->SRST_MASK;
223 output = dataport_value ^ cable->OUTPUT_INVERT;
225 #if PARPORT_USE_PPDEV == 1
226 ioctl(device_handle, PPWDATA, &output);
229 outb(dataport, output);
231 outb(output, dataport);
237 int parport_speed(int speed)
244 int parport_register_commands(struct command_context_s *cmd_ctx)
246 register_command(cmd_ctx, NULL, "parport_port", parport_handle_parport_port_command,
247 COMMAND_CONFIG, NULL);
248 register_command(cmd_ctx, NULL, "parport_cable", parport_handle_parport_cable_command,
249 COMMAND_CONFIG, NULL);
254 #if PARPORT_USE_GIVEIO == 1
255 int parport_get_giveio_access()
258 OSVERSIONINFO version;
260 version.dwOSVersionInfoSize = sizeof version;
261 if (!GetVersionEx( &version )) {
265 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
268 h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
269 if (h == INVALID_HANDLE_VALUE) {
280 int parport_init(void)
283 #if PARPORT_USE_PPDEV == 1
290 if ((parport_cable == NULL) || (parport_cable[0] == 0))
292 parport_cable = "wiggler";
293 WARNING("No parport cable specified, using default 'wiggler'");
296 while (cur_cable->name)
298 if (strcmp(cur_cable->name, parport_cable) == 0)
308 ERROR("No matching cable found for %s", parport_cable);
309 return ERROR_JTAG_INIT_FAILED;
312 dataport_value = cable->PORT_INIT;
314 #if PARPORT_USE_PPDEV == 1
315 if (device_handle > 0)
317 ERROR("device is already opened");
318 return ERROR_JTAG_INIT_FAILED;
322 DEBUG("opening /dev/ppi%d...", parport_port);
324 snprintf(buffer, 256, "/dev/ppi%d", parport_port);
325 device_handle = open(buffer, O_WRONLY);
326 #else /* not __Free_BSD */
327 DEBUG("opening /dev/parport%d...", parport_port);
329 snprintf(buffer, 256, "/dev/parport%d", parport_port);
330 device_handle = open(buffer, O_WRONLY);
331 #endif /* __FreeBSD__ */
333 if (device_handle < 0)
335 ERROR("cannot open device. check it exists and that user read and write rights are set");
336 return ERROR_JTAG_INIT_FAILED;
342 i=ioctl(device_handle, PPCLAIM);
345 ERROR("cannot claim device");
346 return ERROR_JTAG_INIT_FAILED;
349 i = PARPORT_MODE_COMPAT;
350 i= ioctl(device_handle, PPSETMODE, & i);
353 ERROR(" cannot set compatible mode to device");
354 return ERROR_JTAG_INIT_FAILED;
357 i = IEEE1284_MODE_COMPAT;
358 i = ioctl(device_handle, PPNEGOT, & i);
361 ERROR("cannot set compatible 1284 mode to device");
362 return ERROR_JTAG_INIT_FAILED;
364 #endif /* not __Free_BSD__ */
366 #else /* not PARPORT_USE_PPDEV */
367 if (parport_port == 0)
369 parport_port = 0x378;
370 WARNING("No parport port specified, using default '0x378' (LPT1)");
373 dataport = parport_port;
374 statusport = parport_port + 1;
376 DEBUG("requesting privileges for parallel port 0x%x...", dataport);
377 #if PARPORT_USE_GIVEIO == 1
378 if (parport_get_giveio_access() != 0)
379 #else /* PARPORT_USE_GIVEIO */
380 if (ioperm(dataport, 3, 1) != 0)
381 #endif /* PARPORT_USE_GIVEIO */
383 ERROR("missing privileges for direct i/o");
384 return ERROR_JTAG_INIT_FAILED;
386 DEBUG("...privileges granted");
388 /* make sure parallel port is in right mode (clear tristate and interrupt */
390 outb(parport_port + 2, 0x0);
395 #endif /* PARPORT_USE_PPDEV */
398 parport_write(0, 0, 0);
400 bitbang_interface = &parport_bitbang;
405 int parport_quit(void)
411 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
416 /* only if the port wasn't overwritten by cmdline */
417 if (parport_port == 0)
418 parport_port = strtoul(args[0], NULL, 0);
423 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
428 /* only if the cable name wasn't overwritten by cmdline */
429 if (parport_cable == 0)
431 parport_cable = malloc(strlen(args[0]) + sizeof(char));
432 strcpy(parport_cable, args[0]);