1 /***************************************************************************
2 * Copyright (C) 2006 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"
34 #include <sys/types.h>
35 #include <machine/sysarch.h>
36 #include <machine/cpufunc.h>
37 #define ioperm(startport,length,enable)\
38 i386_set_ioperm((startport), (length), (enable))
48 #endif /* __FreeBSD__ */
56 #if PARPORT_USE_PPDEV == 1
58 #include <dev/ppbus/ppi.h>
59 #include <dev/ppbus/ppbconf.h>
60 #define PPRSTATUS PPIGSTATUS
61 #define PPWDATA PPISDATA
63 #include <linux/parport.h>
64 #include <linux/ppdev.h>
67 #include <sys/ioctl.h>
70 #if PARPORT_USE_GIVEIO == 1
81 unsigned long gw16012_port;
83 /* interface variables
85 static u8 gw16012_msb = 0x0;
86 static u8 gw16012_control_value = 0x0;
88 #if PARPORT_USE_PPDEV == 1
89 static int device_handle;
92 int gw16012_execute_queue(void);
93 int gw16012_register_commands(struct command_context_s *cmd_ctx);
94 int gw16012_speed(int speed);
95 int gw16012_init(void);
96 int gw16012_quit(void);
98 int gw16012_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
100 jtag_interface_t gw16012_interface =
104 .execute_queue = gw16012_execute_queue,
106 .support_pathmove = 0,
108 .speed = gw16012_speed,
109 .register_commands = gw16012_register_commands,
110 .init = gw16012_init,
111 .quit = gw16012_quit,
114 int gw16012_register_commands(struct command_context_s *cmd_ctx)
116 register_command(cmd_ctx, NULL, "parport_port", gw16012_handle_parport_port_command,
117 COMMAND_CONFIG, NULL);
122 void gw16012_data(u8 value)
124 value = (value & 0x7f) | gw16012_msb;
125 gw16012_msb ^= 0x80; /* toggle MSB */
127 #if PARPORT_USE_PPDEV == 1
128 ioctl(device_handle, PPWDATA, &value);
131 outb(gw16012_port, value);
133 outb(value, gw16012_port);
138 void gw16012_control(u8 value)
140 if (value != gw16012_control_value)
142 gw16012_control_value = value;
144 #if PARPORT_USE_PPDEV == 1
145 ioctl(device_handle, PPWCONTROL, &gw16012_control_value);
148 outb(gw16012_port + 2, gw16012_control_value);
150 outb(gw16012_control_value, gw16012_port + 2);
156 void gw16012_input(u8 *value)
158 #if PARPORT_USE_PPDEV == 1
159 ioctl(device_handle, PPRSTATUS, value);
161 *value = inb(gw16012_port + 1);
165 /* (1) assert or (0) deassert reset lines */
166 void gw16012_reset(int trst, int srst)
168 DEBUG("trst: %i, srst: %i", trst, srst);
171 gw16012_control(0x0d);
173 gw16012_control(0x0c);
176 gw16012_control(0x0a);
178 gw16012_control(0x0b);
181 int gw16012_speed(int speed)
187 void gw16012_end_state(state)
189 if (tap_move_map[state] != -1)
193 ERROR("BUG: %i is not a valid end state", state);
198 void gw16012_state_move(void)
201 u8 tms_scan = TAP_MOVE(cur_state, end_state);
203 gw16012_control(0x0); /* single-bit mode */
205 for (i = 0; i < 7; i++)
207 tms = (tms_scan >> i) & 1;
208 gw16012_data(tms << 1); /* output next TMS bit */
211 cur_state = end_state;
214 void gw16012_runtest(int num_cycles)
216 enum tap_state saved_end_state = end_state;
219 /* only do a state_move when we're not already in RTI */
220 if (cur_state != TAP_RTI)
222 gw16012_end_state(TAP_RTI);
223 gw16012_state_move();
226 for (i = 0; i < num_cycles; i++)
228 gw16012_control(0x0); /* single-bit mode */
229 gw16012_data(0x0); /* TMS cycle with TMS low */
232 gw16012_end_state(saved_end_state);
233 if (cur_state != end_state)
234 gw16012_state_move();
237 void gw16012_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
239 int bits_left = scan_size;
241 enum tap_state saved_end_state = end_state;
242 u8 scan_out, scan_in;
245 gw16012_end_state(TAP_SI);
247 gw16012_end_state(TAP_SD);
249 gw16012_state_move();
250 gw16012_end_state(saved_end_state);
252 while (type == SCAN_OUT && ((bits_left - 1) > 7))
254 gw16012_control(0x2); /* seven-bit mode */
255 scan_out = buf_get_u32(buffer, bit_count, 7);
256 gw16012_data(scan_out);
261 gw16012_control(0x0); /* single-bit mode */
262 while (bits_left-- > 0)
265 if (bits_left == 0) /* last bit */
267 if ((ir_scan && (end_state == TAP_SI))
268 || (!ir_scan && (end_state == TAP_SD)))
278 scan_out = buf_get_u32(buffer, bit_count, 1);
279 gw16012_data(scan_out | tms);
280 if (type != SCAN_OUT)
282 gw16012_input(&scan_in);
283 buf_set_u32(buffer, bit_count, 1, ((scan_in & 0x08) >> 3));
288 if (!((ir_scan && (end_state == TAP_SI)) ||
289 (!ir_scan && (end_state == TAP_SD))))
297 if (cur_state != end_state)
298 gw16012_state_move();
302 int gw16012_execute_queue(void)
304 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
314 #ifdef _DEBUG_JTAG_IO_
315 DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
317 if (cmd->cmd.end_state->end_state != -1)
318 gw16012_end_state(cmd->cmd.end_state->end_state);
321 #ifdef _DEBUG_JTAG_IO_
322 DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
324 if (cmd->cmd.reset->trst == 1)
328 gw16012_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
331 #ifdef _DEBUG_JTAG_IO_
332 DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
334 if (cmd->cmd.runtest->end_state != -1)
335 gw16012_end_state(cmd->cmd.runtest->end_state);
336 gw16012_runtest(cmd->cmd.runtest->num_cycles);
339 #ifdef _DEBUG_JTAG_IO_
340 DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
342 if (cmd->cmd.statemove->end_state != -1)
343 gw16012_end_state(cmd->cmd.statemove->end_state);
344 gw16012_state_move();
347 if (cmd->cmd.scan->end_state != -1)
348 gw16012_end_state(cmd->cmd.scan->end_state);
349 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
350 type = jtag_scan_type(cmd->cmd.scan);
351 #ifdef _DEBUG_JTAG_IO_
352 DEBUG("%s scan (%i) %i bit end in %i", (cmd->cmd.scan->ir_scan) ? "ir" : "dr",
353 type, scan_size, cmd->cmd.scan->end_state);
355 gw16012_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
356 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
357 return ERROR_JTAG_QUEUE_FAILED;
362 #ifdef _DEBUG_JTAG_IO_
363 DEBUG("sleep", cmd->cmd.sleep->us);
365 jtag_sleep(cmd->cmd.sleep->us);
368 ERROR("BUG: unknown JTAG command type encountered");
377 #if PARPORT_USE_GIVEIO == 1
378 int gw16012_get_giveio_access()
381 OSVERSIONINFO version;
383 version.dwOSVersionInfoSize = sizeof version;
384 if (!GetVersionEx( &version )) {
388 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
391 h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
392 if (h == INVALID_HANDLE_VALUE) {
403 int gw16012_init(void)
405 #if PARPORT_USE_PPDEV == 1
412 #if PARPORT_USE_PPDEV == 1
415 ERROR("device is already opened");
416 return ERROR_JTAG_INIT_FAILED;
420 DEBUG("opening /dev/ppi%d...", gw16012_port);
422 snprintf(buffer, 256, "/dev/ppi%d", gw16012_port);
423 device_handle = open(buffer, O_WRONLY);
425 DEBUG("opening /dev/parport%d...", gw16012_port);
427 snprintf(buffer, 256, "/dev/parport%d", gw16012_port);
428 device_handle = open(buffer, O_WRONLY);
432 ERROR("cannot open device. check it exists and that user read and write rights are set");
433 return ERROR_JTAG_INIT_FAILED;
439 i=ioctl(device_handle, PPCLAIM);
442 ERROR("cannot claim device");
443 return ERROR_JTAG_INIT_FAILED;
446 i = PARPORT_MODE_COMPAT;
447 i= ioctl(device_handle, PPSETMODE, & i);
450 ERROR(" cannot set compatible mode to device");
451 return ERROR_JTAG_INIT_FAILED;
454 i = IEEE1284_MODE_COMPAT;
455 i = ioctl(device_handle, PPNEGOT, & i);
458 ERROR("cannot set compatible 1284 mode to device");
459 return ERROR_JTAG_INIT_FAILED;
463 if (gw16012_port == 0)
465 gw16012_port = 0x378;
466 WARNING("No gw16012 port specified, using default '0x378' (LPT1)");
469 DEBUG("requesting privileges for parallel port 0x%x...", gw16012_port);
470 #if PARPORT_USE_GIVEIO == 1
471 if (gw16012_get_giveio_access() != 0)
472 #else /* PARPORT_USE_GIVEIO */
473 if (ioperm(gw16012_port, 3, 1) != 0)
474 #endif /* PARPORT_USE_GIVEIO */
476 ERROR("missing privileges for direct i/o");
477 return ERROR_JTAG_INIT_FAILED;
479 DEBUG("...privileges granted");
480 #endif /* PARPORT_USE_PPDEV */
482 gw16012_input(&status_port);
483 gw16012_msb = (status_port & 0x80) ^ 0x80;
485 gw16012_speed(jtag_speed);
491 int gw16012_quit(void)
497 int gw16012_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
502 /* only if the port wasn't overwritten by cmdline */
503 if (gw16012_port == 0)
504 gw16012_port = strtoul(args[0], NULL, 0);