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"
29 #define _DEBUG_GW16012_IO_
38 #include <sys/types.h>
39 #include <machine/sysarch.h>
40 #include <machine/cpufunc.h>
41 #define ioperm(startport,length,enable)\
42 i386_set_ioperm((startport), (length), (enable))
52 #endif /* __FreeBSD__ */
60 #if PARPORT_USE_PPDEV == 1
62 #include <dev/ppbus/ppi.h>
63 #include <dev/ppbus/ppbconf.h>
64 #define PPRSTATUS PPIGSTATUS
65 #define PPWDATA PPISDATA
67 #include <linux/parport.h>
68 #include <linux/ppdev.h>
71 #include <sys/ioctl.h>
74 #if PARPORT_USE_GIVEIO == 1
85 unsigned long gw16012_port;
87 /* interface variables
89 static u8 gw16012_msb = 0x0;
90 static u8 gw16012_control_value = 0x0;
92 #if PARPORT_USE_PPDEV == 1
93 static int device_handle;
96 int gw16012_execute_queue(void);
97 int gw16012_register_commands(struct command_context_s *cmd_ctx);
98 int gw16012_speed(int speed);
99 int gw16012_init(void);
100 int gw16012_quit(void);
102 int gw16012_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
104 jtag_interface_t gw16012_interface =
108 .execute_queue = gw16012_execute_queue,
110 .support_pathmove = 0,
112 .speed = gw16012_speed,
113 .register_commands = gw16012_register_commands,
114 .init = gw16012_init,
115 .quit = gw16012_quit,
118 int gw16012_register_commands(struct command_context_s *cmd_ctx)
120 register_command(cmd_ctx, NULL, "parport_port", gw16012_handle_parport_port_command,
121 COMMAND_CONFIG, NULL);
126 void gw16012_data(u8 value)
128 value = (value & 0x7f) | gw16012_msb;
129 gw16012_msb ^= 0x80; /* toggle MSB */
131 #ifdef _DEBUG_GW16012_IO_
132 DEBUG("%2.2x", value);
135 #if PARPORT_USE_PPDEV == 1
136 ioctl(device_handle, PPWDATA, &value);
139 outb(gw16012_port, value);
141 outb(value, gw16012_port);
146 void gw16012_control(u8 value)
148 if (value != gw16012_control_value)
150 gw16012_control_value = value;
152 #ifdef _DEBUG_GW16012_IO_
153 DEBUG("%2.2x", gw16012_control_value);
156 #if PARPORT_USE_PPDEV == 1
157 ioctl(device_handle, PPWCONTROL, &gw16012_control_value);
160 outb(gw16012_port + 2, gw16012_control_value);
162 outb(gw16012_control_value, gw16012_port + 2);
168 void gw16012_input(u8 *value)
170 #if PARPORT_USE_PPDEV == 1
171 ioctl(device_handle, PPRSTATUS, value);
173 *value = inb(gw16012_port + 1);
176 #ifdef _DEBUG_GW16012_IO_
177 DEBUG("%2.2x", *value);
181 /* (1) assert or (0) deassert reset lines */
182 void gw16012_reset(int trst, int srst)
184 DEBUG("trst: %i, srst: %i", trst, srst);
187 gw16012_control(0x0d);
189 gw16012_control(0x0c);
192 gw16012_control(0x0a);
194 gw16012_control(0x0b);
197 int gw16012_speed(int speed)
203 void gw16012_end_state(state)
205 if (tap_move_map[state] != -1)
209 ERROR("BUG: %i is not a valid end state", state);
214 void gw16012_state_move(void)
217 u8 tms_scan = TAP_MOVE(cur_state, end_state);
219 gw16012_control(0x0); /* single-bit mode */
221 for (i = 0; i < 7; i++)
223 tms = (tms_scan >> i) & 1;
224 gw16012_data(tms << 1); /* output next TMS bit */
227 cur_state = end_state;
230 void gw16012_path_move(pathmove_command_t *cmd)
232 int num_states = cmd->num_states;
238 gw16012_control(0x0); /* single-bit mode */
239 if (tap_transitions[cur_state].low == cmd->path[state_count])
241 gw16012_data(0x0); /* TCK cycle with TMS low */
243 else if (tap_transitions[cur_state].high == cmd->path[state_count])
245 gw16012_data(0x2); /* TCK cycle with TMS high */
249 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
253 cur_state = cmd->path[state_count];
258 end_state = cur_state;
261 void gw16012_runtest(int num_cycles)
263 enum tap_state saved_end_state = end_state;
266 /* only do a state_move when we're not already in RTI */
267 if (cur_state != TAP_RTI)
269 gw16012_end_state(TAP_RTI);
270 gw16012_state_move();
273 for (i = 0; i < num_cycles; i++)
275 gw16012_control(0x0); /* single-bit mode */
276 gw16012_data(0x0); /* TMS cycle with TMS low */
279 gw16012_end_state(saved_end_state);
280 if (cur_state != end_state)
281 gw16012_state_move();
284 void gw16012_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
286 int bits_left = scan_size;
288 enum tap_state saved_end_state = end_state;
289 u8 scan_out, scan_in;
292 gw16012_end_state(TAP_SI);
294 gw16012_end_state(TAP_SD);
296 gw16012_state_move();
297 gw16012_end_state(saved_end_state);
299 while (type == SCAN_OUT && ((bits_left - 1) > 7))
301 gw16012_control(0x2); /* seven-bit mode */
302 scan_out = buf_get_u32(buffer, bit_count, 7);
303 gw16012_data(scan_out);
308 gw16012_control(0x0); /* single-bit mode */
309 while (bits_left-- > 0)
312 if (bits_left == 0) /* last bit */
314 if ((ir_scan && (end_state == TAP_SI))
315 || (!ir_scan && (end_state == TAP_SD)))
325 scan_out = buf_get_u32(buffer, bit_count, 1);
326 gw16012_data(scan_out | tms);
327 if (type != SCAN_OUT)
329 gw16012_input(&scan_in);
330 buf_set_u32(buffer, bit_count, 1, ((scan_in & 0x08) >> 3));
335 if (!((ir_scan && (end_state == TAP_SI)) ||
336 (!ir_scan && (end_state == TAP_SD))))
344 if (cur_state != end_state)
345 gw16012_state_move();
349 int gw16012_execute_queue(void)
351 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
361 #ifdef _DEBUG_JTAG_IO_
362 DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
364 if (cmd->cmd.end_state->end_state != -1)
365 gw16012_end_state(cmd->cmd.end_state->end_state);
368 #ifdef _DEBUG_JTAG_IO_
369 DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
371 if (cmd->cmd.reset->trst == 1)
375 gw16012_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
378 #ifdef _DEBUG_JTAG_IO_
379 DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
381 if (cmd->cmd.runtest->end_state != -1)
382 gw16012_end_state(cmd->cmd.runtest->end_state);
383 gw16012_runtest(cmd->cmd.runtest->num_cycles);
386 #ifdef _DEBUG_JTAG_IO_
387 DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
389 if (cmd->cmd.statemove->end_state != -1)
390 gw16012_end_state(cmd->cmd.statemove->end_state);
391 gw16012_state_move();
394 #ifdef _DEBUG_JTAG_IO_
395 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
397 gw16012_path_move(cmd->cmd.pathmove);
400 if (cmd->cmd.scan->end_state != -1)
401 gw16012_end_state(cmd->cmd.scan->end_state);
402 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
403 type = jtag_scan_type(cmd->cmd.scan);
404 #ifdef _DEBUG_JTAG_IO_
405 DEBUG("%s scan (%i) %i bit end in %i", (cmd->cmd.scan->ir_scan) ? "ir" : "dr",
406 type, scan_size, cmd->cmd.scan->end_state);
408 gw16012_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
409 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
410 return ERROR_JTAG_QUEUE_FAILED;
415 #ifdef _DEBUG_JTAG_IO_
416 DEBUG("sleep", cmd->cmd.sleep->us);
418 jtag_sleep(cmd->cmd.sleep->us);
421 ERROR("BUG: unknown JTAG command type encountered");
430 #if PARPORT_USE_GIVEIO == 1
431 int gw16012_get_giveio_access()
434 OSVERSIONINFO version;
436 version.dwOSVersionInfoSize = sizeof version;
437 if (!GetVersionEx( &version )) {
441 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
444 h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
445 if (h == INVALID_HANDLE_VALUE) {
456 int gw16012_init(void)
458 #if PARPORT_USE_PPDEV == 1
465 #if PARPORT_USE_PPDEV == 1
468 ERROR("device is already opened");
469 return ERROR_JTAG_INIT_FAILED;
473 DEBUG("opening /dev/ppi%d...", gw16012_port);
475 snprintf(buffer, 256, "/dev/ppi%d", gw16012_port);
476 device_handle = open(buffer, O_WRONLY);
478 DEBUG("opening /dev/parport%d...", gw16012_port);
480 snprintf(buffer, 256, "/dev/parport%d", gw16012_port);
481 device_handle = open(buffer, O_WRONLY);
485 ERROR("cannot open device. check it exists and that user read and write rights are set");
486 return ERROR_JTAG_INIT_FAILED;
492 i=ioctl(device_handle, PPCLAIM);
495 ERROR("cannot claim device");
496 return ERROR_JTAG_INIT_FAILED;
499 i = PARPORT_MODE_COMPAT;
500 i= ioctl(device_handle, PPSETMODE, & i);
503 ERROR(" cannot set compatible mode to device");
504 return ERROR_JTAG_INIT_FAILED;
507 i = IEEE1284_MODE_COMPAT;
508 i = ioctl(device_handle, PPNEGOT, & i);
511 ERROR("cannot set compatible 1284 mode to device");
512 return ERROR_JTAG_INIT_FAILED;
516 if (gw16012_port == 0)
518 gw16012_port = 0x378;
519 WARNING("No gw16012 port specified, using default '0x378' (LPT1)");
522 DEBUG("requesting privileges for parallel port 0x%x...", gw16012_port);
523 #if PARPORT_USE_GIVEIO == 1
524 if (gw16012_get_giveio_access() != 0)
525 #else /* PARPORT_USE_GIVEIO */
526 if (ioperm(gw16012_port, 3, 1) != 0)
527 #endif /* PARPORT_USE_GIVEIO */
529 ERROR("missing privileges for direct i/o");
530 return ERROR_JTAG_INIT_FAILED;
532 DEBUG("...privileges granted");
533 #endif /* PARPORT_USE_PPDEV */
535 gw16012_input(&status_port);
536 gw16012_msb = (status_port & 0x80) ^ 0x80;
538 gw16012_speed(jtag_speed);
544 int gw16012_quit(void)
550 int gw16012_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
555 /* only if the port wasn't overwritten by cmdline */
556 if (gw16012_port == 0)
557 gw16012_port = strtoul(args[0], NULL, 0);