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 { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20,0x30, 0x20, 0x00 },
103 { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
108 unsigned long parport_port;
110 /* interface variables
112 static cable_t* cable;
113 static u8 dataport_value = 0x0;
115 #if PARPORT_USE_PPDEV == 1
116 static int device_handle;
118 static unsigned long dataport;
119 static unsigned long statusport;
122 /* low level command set
124 int parport_read(void);
125 void parport_write(int tck, int tms, int tdi);
126 void parport_reset(int trst, int srst);
128 int parport_speed(int speed);
129 int parport_register_commands(struct command_context_s *cmd_ctx);
130 int parport_init(void);
131 int parport_quit(void);
133 /* interface commands */
134 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
135 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
137 jtag_interface_t parport_interface =
141 .execute_queue = bitbang_execute_queue,
143 .support_pathmove = 1,
145 .speed = parport_speed,
146 .register_commands = parport_register_commands,
147 .init = parport_init,
148 .quit = parport_quit,
151 bitbang_interface_t parport_bitbang =
153 .read = parport_read,
154 .write = parport_write,
155 .reset = parport_reset
158 int parport_read(void)
162 #if PARPORT_USE_PPDEV == 1
163 ioctl(device_handle, PPRSTATUS, & data);
165 data = inb(statusport);
168 if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
174 void parport_write(int tck, int tms, int tdi)
177 int i = jtag_speed + 1;
180 dataport_value |= cable->TCK_MASK;
182 dataport_value &= ~cable->TCK_MASK;
185 dataport_value |= cable->TMS_MASK;
187 dataport_value &= ~cable->TMS_MASK;
190 dataport_value |= cable->TDI_MASK;
192 dataport_value &= ~cable->TDI_MASK;
194 output = dataport_value ^ cable->OUTPUT_INVERT;
197 #if PARPORT_USE_PPDEV == 1
198 ioctl(device_handle, PPWDATA, &output);
201 outb(dataport, output);
203 outb(output, dataport);
208 /* (1) assert or (0) deassert reset lines */
209 void parport_reset(int trst, int srst)
212 DEBUG("trst: %i, srst: %i", trst, srst);
215 dataport_value |= cable->TRST_MASK;
217 dataport_value &= ~cable->TRST_MASK;
220 dataport_value |= cable->SRST_MASK;
222 dataport_value &= ~cable->SRST_MASK;
224 output = dataport_value ^ cable->OUTPUT_INVERT;
226 #if PARPORT_USE_PPDEV == 1
227 ioctl(device_handle, PPWDATA, &output);
230 outb(dataport, output);
232 outb(output, dataport);
238 int parport_speed(int speed)
245 int parport_register_commands(struct command_context_s *cmd_ctx)
247 register_command(cmd_ctx, NULL, "parport_port", parport_handle_parport_port_command,
248 COMMAND_CONFIG, NULL);
249 register_command(cmd_ctx, NULL, "parport_cable", parport_handle_parport_cable_command,
250 COMMAND_CONFIG, NULL);
255 #if PARPORT_USE_GIVEIO == 1
256 int parport_get_giveio_access()
259 OSVERSIONINFO version;
261 version.dwOSVersionInfoSize = sizeof version;
262 if (!GetVersionEx( &version )) {
266 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
269 h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
270 if (h == INVALID_HANDLE_VALUE) {
281 int parport_init(void)
284 #if PARPORT_USE_PPDEV == 1
291 if ((parport_cable == NULL) || (parport_cable[0] == 0))
293 parport_cable = "wiggler";
294 WARNING("No parport cable specified, using default 'wiggler'");
297 while (cur_cable->name)
299 if (strcmp(cur_cable->name, parport_cable) == 0)
309 ERROR("No matching cable found for %s", parport_cable);
310 return ERROR_JTAG_INIT_FAILED;
313 dataport_value = cable->PORT_INIT;
315 #if PARPORT_USE_PPDEV == 1
316 if (device_handle > 0)
318 ERROR("device is already opened");
319 return ERROR_JTAG_INIT_FAILED;
323 DEBUG("opening /dev/ppi%d...", parport_port);
325 snprintf(buffer, 256, "/dev/ppi%d", parport_port);
326 device_handle = open(buffer, O_WRONLY);
327 #else /* not __Free_BSD */
328 DEBUG("opening /dev/parport%d...", parport_port);
330 snprintf(buffer, 256, "/dev/parport%d", parport_port);
331 device_handle = open(buffer, O_WRONLY);
332 #endif /* __FreeBSD__ */
334 if (device_handle < 0)
336 ERROR("cannot open device. check it exists and that user read and write rights are set");
337 return ERROR_JTAG_INIT_FAILED;
343 i=ioctl(device_handle, PPCLAIM);
346 ERROR("cannot claim device");
347 return ERROR_JTAG_INIT_FAILED;
350 i = PARPORT_MODE_COMPAT;
351 i= ioctl(device_handle, PPSETMODE, & i);
354 ERROR(" cannot set compatible mode to device");
355 return ERROR_JTAG_INIT_FAILED;
358 i = IEEE1284_MODE_COMPAT;
359 i = ioctl(device_handle, PPNEGOT, & i);
362 ERROR("cannot set compatible 1284 mode to device");
363 return ERROR_JTAG_INIT_FAILED;
365 #endif /* not __Free_BSD__ */
367 #else /* not PARPORT_USE_PPDEV */
368 if (parport_port == 0)
370 parport_port = 0x378;
371 WARNING("No parport port specified, using default '0x378' (LPT1)");
374 dataport = parport_port;
375 statusport = parport_port + 1;
377 DEBUG("requesting privileges for parallel port 0x%x...", dataport);
378 #if PARPORT_USE_GIVEIO == 1
379 if (parport_get_giveio_access() != 0)
380 #else /* PARPORT_USE_GIVEIO */
381 if (ioperm(dataport, 3, 1) != 0)
382 #endif /* PARPORT_USE_GIVEIO */
384 ERROR("missing privileges for direct i/o");
385 return ERROR_JTAG_INIT_FAILED;
387 DEBUG("...privileges granted");
389 /* make sure parallel port is in right mode (clear tristate and interrupt */
391 outb(parport_port + 2, 0x0);
396 #endif /* PARPORT_USE_PPDEV */
399 parport_write(0, 0, 0);
401 bitbang_interface = &parport_bitbang;
406 int parport_quit(void)
412 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
417 /* only if the port wasn't overwritten by cmdline */
418 if (parport_port == 0)
419 parport_port = strtoul(args[0], NULL, 0);
424 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
429 /* only if the cable name wasn't overwritten by cmdline */
430 if (parport_cable == 0)
432 parport_cable = malloc(strlen(args[0]) + sizeof(char));
433 strcpy(parport_cable, args[0]);