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"
31 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
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))
45 #endif /* __FreeBSD__ */
51 #if PARPORT_USE_PPDEV == 1
52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
53 #include <dev/ppbus/ppi.h>
54 #include <dev/ppbus/ppbconf.h>
55 #define PPRSTATUS PPIGSTATUS
56 #define PPWDATA PPISDATA
58 #include <linux/parport.h>
59 #include <linux/ppdev.h>
62 #include <sys/ioctl.h>
63 #else /* not PARPORT_USE_PPDEV */
69 #if PARPORT_USE_GIVEIO == 1
79 /* parallel port cable description
81 typedef struct cable_s
84 u8 TDO_MASK; /* status port bit containing current TDO value */
85 u8 TRST_MASK; /* data port bit for TRST */
86 u8 TMS_MASK; /* data port bit for TMS */
87 u8 TCK_MASK; /* data port bit for TCK */
88 u8 TDI_MASK; /* data port bit for TDI */
89 u8 SRST_MASK; /* data port bit for SRST */
90 u8 OUTPUT_INVERT; /* data port bits that should be inverted */
91 u8 INPUT_INVERT; /* status port that should be inverted */
92 u8 PORT_INIT; /* initialize data port with this value */
93 u8 PORT_EXIT; /* de-initialize data port with this value */
94 u8 LED_MASK; /* data port bit for LED */
99 /* name tdo trst tms tck tdi srst o_inv i_inv init exit led */
100 { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 },
101 { "wiggler2", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 },
102 { "wiggler_ntrst_inverted",
103 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80, 0x80, 0x00 },
104 { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 },
105 { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
106 { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 },
107 { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
108 { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 },
109 { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 },
110 /* Altium Universal JTAG cable. Set the cable to Xilinx Mode and wire to target as follows:
111 HARD TCK - Target TCK
112 HARD TMS - Target TMS
113 HARD TDI - Target TDI
114 HARD TDO - Target TDO
115 SOFT TCK - Target TRST
116 SOFT TDI - Target SRST
118 { "altium", 0x10, 0x20, 0x04, 0x02, 0x01, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08 },
119 { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
124 unsigned long parport_port;
126 /* interface variables
128 static cable_t* cable;
129 static u8 dataport_value = 0x0;
131 #if PARPORT_USE_PPDEV == 1
132 static int device_handle;
134 static unsigned long dataport;
135 static unsigned long statusport;
138 /* low level command set
140 int parport_read(void);
141 void parport_write(int tck, int tms, int tdi);
142 void parport_reset(int trst, int srst);
143 void parport_led(int on);
145 int parport_speed(int speed);
146 int parport_register_commands(struct command_context_s *cmd_ctx);
147 int parport_init(void);
148 int parport_quit(void);
150 /* interface commands */
151 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
152 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
154 jtag_interface_t parport_interface =
158 .execute_queue = bitbang_execute_queue,
160 .support_pathmove = 1,
162 .speed = parport_speed,
163 .register_commands = parport_register_commands,
164 .init = parport_init,
165 .quit = parport_quit,
168 bitbang_interface_t parport_bitbang =
170 .read = parport_read,
171 .write = parport_write,
172 .reset = parport_reset,
176 int parport_read(void)
180 #if PARPORT_USE_PPDEV == 1
181 ioctl(device_handle, PPRSTATUS, & data);
183 data = inb(statusport);
186 if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
192 static inline void parport_write_data(void)
195 output = dataport_value ^ cable->OUTPUT_INVERT;
197 #if PARPORT_USE_PPDEV == 1
198 ioctl(device_handle, PPWDATA, &output);
200 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
201 outb(dataport, output);
203 outb(output, dataport);
208 void parport_write(int tck, int tms, int tdi)
210 int i = jtag_speed + 1;
213 dataport_value |= cable->TCK_MASK;
215 dataport_value &= ~cable->TCK_MASK;
218 dataport_value |= cable->TMS_MASK;
220 dataport_value &= ~cable->TMS_MASK;
223 dataport_value |= cable->TDI_MASK;
225 dataport_value &= ~cable->TDI_MASK;
228 parport_write_data();
231 /* (1) assert or (0) deassert reset lines */
232 void parport_reset(int trst, int srst)
234 DEBUG("trst: %i, srst: %i", trst, srst);
237 dataport_value |= cable->TRST_MASK;
239 dataport_value &= ~cable->TRST_MASK;
242 dataport_value |= cable->SRST_MASK;
244 dataport_value &= ~cable->SRST_MASK;
246 parport_write_data();
249 /* turn LED on parport adapter on (1) or off (0) */
250 void parport_led(int on)
254 dataport_value |= cable->LED_MASK;
256 dataport_value &= ~cable->LED_MASK;
258 parport_write_data();
261 int parport_speed(int speed)
268 int parport_register_commands(struct command_context_s *cmd_ctx)
270 register_command(cmd_ctx, NULL, "parport_port", parport_handle_parport_port_command,
271 COMMAND_CONFIG, NULL);
272 register_command(cmd_ctx, NULL, "parport_cable", parport_handle_parport_cable_command,
273 COMMAND_CONFIG, NULL);
278 #if PARPORT_USE_GIVEIO == 1
279 int parport_get_giveio_access()
282 OSVERSIONINFO version;
284 version.dwOSVersionInfoSize = sizeof version;
285 if (!GetVersionEx( &version )) {
289 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
292 h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
293 if (h == INVALID_HANDLE_VALUE) {
304 int parport_init(void)
307 #if PARPORT_USE_PPDEV == 1
314 if ((parport_cable == NULL) || (parport_cable[0] == 0))
316 parport_cable = "wiggler";
317 WARNING("No parport cable specified, using default 'wiggler'");
320 while (cur_cable->name)
322 if (strcmp(cur_cable->name, parport_cable) == 0)
332 ERROR("No matching cable found for %s", parport_cable);
333 return ERROR_JTAG_INIT_FAILED;
336 dataport_value = cable->PORT_INIT;
338 #if PARPORT_USE_PPDEV == 1
339 if (device_handle > 0)
341 ERROR("device is already opened");
342 return ERROR_JTAG_INIT_FAILED;
345 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
346 DEBUG("opening /dev/ppi%d...", parport_port);
348 snprintf(buffer, 256, "/dev/ppi%d", parport_port);
349 device_handle = open(buffer, O_WRONLY);
350 #else /* not __FreeBSD__, __FreeBSD_kernel__ */
351 DEBUG("opening /dev/parport%d...", parport_port);
353 snprintf(buffer, 256, "/dev/parport%d", parport_port);
354 device_handle = open(buffer, O_WRONLY);
355 #endif /* __FreeBSD__, __FreeBSD_kernel__ */
357 if (device_handle < 0)
359 ERROR("cannot open device. check it exists and that user read and write rights are set");
360 return ERROR_JTAG_INIT_FAILED;
365 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
366 i=ioctl(device_handle, PPCLAIM);
369 ERROR("cannot claim device");
370 return ERROR_JTAG_INIT_FAILED;
373 i = PARPORT_MODE_COMPAT;
374 i= ioctl(device_handle, PPSETMODE, & i);
377 ERROR(" cannot set compatible mode to device");
378 return ERROR_JTAG_INIT_FAILED;
381 i = IEEE1284_MODE_COMPAT;
382 i = ioctl(device_handle, PPNEGOT, & i);
385 ERROR("cannot set compatible 1284 mode to device");
386 return ERROR_JTAG_INIT_FAILED;
388 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
390 #else /* not PARPORT_USE_PPDEV */
391 if (parport_port == 0)
393 parport_port = 0x378;
394 WARNING("No parport port specified, using default '0x378' (LPT1)");
397 dataport = parport_port;
398 statusport = parport_port + 1;
400 DEBUG("requesting privileges for parallel port 0x%lx...", dataport);
401 #if PARPORT_USE_GIVEIO == 1
402 if (parport_get_giveio_access() != 0)
403 #else /* PARPORT_USE_GIVEIO */
404 if (ioperm(dataport, 3, 1) != 0)
405 #endif /* PARPORT_USE_GIVEIO */
407 ERROR("missing privileges for direct i/o");
408 return ERROR_JTAG_INIT_FAILED;
410 DEBUG("...privileges granted");
412 /* make sure parallel port is in right mode (clear tristate and interrupt */
413 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
414 outb(parport_port + 2, 0x0);
416 outb(0x0, parport_port + 2);
419 #endif /* PARPORT_USE_PPDEV */
422 parport_write(0, 0, 0);
425 bitbang_interface = &parport_bitbang;
430 int parport_quit(void)
435 dataport_value = cable->PORT_EXIT;
436 parport_write_data();
440 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
445 /* only if the port wasn't overwritten by cmdline */
446 if (parport_port == 0)
447 parport_port = strtoul(args[0], NULL, 0);
452 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
457 /* only if the cable name wasn't overwritten by cmdline */
458 if (parport_cable == 0)
460 parport_cable = malloc(strlen(args[0]) + sizeof(char));
461 strcpy(parport_cable, args[0]);