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 ***************************************************************************/
28 #include <sys/types.h>
29 #include <machine/sysarch.h>
30 #include <machine/cpufunc.h>
31 #define ioperm(startport,length,enable)\
32 i386_set_ioperm((startport), (length), (enable))
41 #if PARPORT_USE_PPDEV == 1
42 #include <linux/parport.h>
43 #include <linux/ppdev.h>
45 #include <sys/ioctl.h>
48 /* parallel port cable description
50 typedef struct cable_s
53 u8 TDO_MASK; /* status port bit containing current TDO value */
54 u8 TRST_MASK; /* data port bit for TRST */
55 u8 TMS_MASK; /* data port bit for TMS */
56 u8 TCK_MASK; /* data port bit for TCK */
57 u8 TDI_MASK; /* data port bit for TDI */
58 u8 SRST_MASK; /* data port bit for SRST */
59 u8 OUTPUT_INVERT; /* data port bits that should be inverted */
60 u8 INPUT_INVERT; /* status port that should be inverted */
61 u8 PORT_INIT; /* initialize data port with this value */
66 /* name tdo trst tms tck tdi srst o_inv i_inv init */
67 { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80 },
68 { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80 },
69 { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00 },
70 { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10 },
71 { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00 },
72 { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
77 unsigned long parport_port;
79 /* interface variables
81 static cable_t* cable;
82 static u8 dataport_value = 0x0;
84 #if PARPORT_USE_PPDEV == 1
85 static int device_handle;
87 static unsigned long dataport;
88 static unsigned long statusport;
91 /* low level command set
93 int parport_read(void);
94 void parport_write(int tck, int tms, int tdi);
95 void parport_reset(int trst, int srst);
97 int parport_speed(int speed);
98 int parport_register_commands(struct command_context_s *cmd_ctx);
99 int parport_init(void);
100 int parport_quit(void);
102 /* interface commands */
103 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
104 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
106 jtag_interface_t parport_interface =
110 .execute_queue = bitbang_execute_queue,
112 .support_statemove = 0,
114 .speed = parport_speed,
115 .register_commands = parport_register_commands,
116 .init = parport_init,
117 .quit = parport_quit,
120 bitbang_interface_t parport_bitbang =
122 .read = parport_read,
123 .write = parport_write,
124 .reset = parport_reset
127 int parport_read(void)
131 #if PARPORT_USE_PPDEV == 1
132 ioctl(device_handle, PPRSTATUS, & data);
134 data = inb(statusport);
137 if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
143 void parport_write(int tck, int tms, int tdi)
146 int i = jtag_speed + 1;
149 dataport_value |= cable->TCK_MASK;
151 dataport_value &= ~cable->TCK_MASK;
154 dataport_value |= cable->TMS_MASK;
156 dataport_value &= ~cable->TMS_MASK;
159 dataport_value |= cable->TDI_MASK;
161 dataport_value &= ~cable->TDI_MASK;
163 output = dataport_value ^ cable->OUTPUT_INVERT;
166 #if PARPORT_USE_PPDEV == 1
167 ioctl(device_handle, PPWDATA, &output);
170 outb(dataport, output);
172 outb(output, dataport);
177 /* (1) assert or (0) deassert reset lines */
178 void parport_reset(int trst, int srst)
181 DEBUG("trst: %i, srst: %i", trst, srst);
184 dataport_value |= cable->TRST_MASK;
186 dataport_value &= ~cable->TRST_MASK;
189 dataport_value |= cable->SRST_MASK;
191 dataport_value &= ~cable->SRST_MASK;
193 output = dataport_value ^ cable->OUTPUT_INVERT;
195 #if PARPORT_USE_PPDEV == 1
196 ioctl(device_handle, PPWDATA, &output);
199 outb(dataport, output);
201 outb(output, dataport);
207 int parport_speed(int speed)
214 int parport_register_commands(struct command_context_s *cmd_ctx)
216 register_command(cmd_ctx, NULL, "parport_port", parport_handle_parport_port_command,
217 COMMAND_CONFIG, NULL);
218 register_command(cmd_ctx, NULL, "parport_cable", parport_handle_parport_cable_command,
219 COMMAND_CONFIG, NULL);
224 int parport_init(void)
227 #if PARPORT_USE_PPDEV == 1
234 if ((parport_cable == NULL) || (parport_cable[0] == 0))
236 parport_cable = "wiggler";
237 WARNING("No parport cable specified, using default 'wiggler'");
240 while (cur_cable->name)
242 if (strcmp(cur_cable->name, parport_cable) == 0)
252 ERROR("No matching cable found for %s", parport_cable);
253 return ERROR_JTAG_INIT_FAILED;
256 dataport_value = cable->PORT_INIT;
258 #if PARPORT_USE_PPDEV == 1
261 ERROR("device is already opened");
262 return ERROR_JTAG_INIT_FAILED;
265 snprintf(buffer, 256, "/dev/parport%d", parport_port);
266 device_handle = open(buffer, O_WRONLY);
270 ERROR("cannot open device. check it exists and that user read and write rights are set");
271 return ERROR_JTAG_INIT_FAILED;
274 i=ioctl(device_handle, PPCLAIM);
277 ERROR("cannot claim device");
278 return ERROR_JTAG_INIT_FAILED;
281 i = PARPORT_MODE_COMPAT;
282 i= ioctl(device_handle, PPSETMODE, & i);
285 ERROR(" cannot set compatible mode to device");
286 return ERROR_JTAG_INIT_FAILED;
289 i = IEEE1284_MODE_COMPAT;
290 i = ioctl(device_handle, PPNEGOT, & i);
293 ERROR("cannot set compatible 1284 mode to device");
294 return ERROR_JTAG_INIT_FAILED;
297 if (parport_port == 0)
299 parport_port = 0x378;
300 WARNING("No parport port specified, using default '0x378' (LPT1)");
303 dataport = parport_port;
304 statusport = parport_port + 1;
306 if (ioperm(dataport, 3, 1) != 0) {
307 ERROR("missing privileges for direct i/o");
308 return ERROR_JTAG_INIT_FAILED;
313 parport_write(0, 0, 0);
315 bitbang_interface = &parport_bitbang;
320 int parport_quit(void)
326 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
331 /* only if the port wasn't overwritten by cmdline */
332 if (parport_port == 0)
333 parport_port = strtoul(args[0], NULL, 0);
338 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
343 /* only if the cable name wasn't overwritten by cmdline */
344 if (parport_cable == 0)
346 parport_cable = malloc(strlen(args[0]) + sizeof(char));
347 strcpy(parport_cable, args[0]);