1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
26 #include <jtag/interface.h>
29 /* -ino: 060521-1036 */
30 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
31 #include <machine/sysarch.h>
32 #include <machine/cpufunc.h>
33 #define ioperm(startport, length, enable)\
34 i386_set_ioperm((startport), (length), (enable))
35 #endif /* __FreeBSD__ */
37 #if PARPORT_USE_PPDEV == 1
38 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
39 #include <dev/ppbus/ppi.h>
40 #include <dev/ppbus/ppbconf.h>
41 #define PPRSTATUS PPIGSTATUS
42 #define PPWDATA PPISDATA
44 #include <linux/parport.h>
45 #include <linux/ppdev.h>
47 #include <sys/ioctl.h>
48 #else /* not PARPORT_USE_PPDEV */
54 #if PARPORT_USE_GIVEIO == 1 && IS_CYGWIN == 1
58 /* parallel port cable description
62 uint8_t TDO_MASK; /* status port bit containing current TDO value */
63 uint8_t TRST_MASK; /* data port bit for TRST */
64 uint8_t TMS_MASK; /* data port bit for TMS */
65 uint8_t TCK_MASK; /* data port bit for TCK */
66 uint8_t TDI_MASK; /* data port bit for TDI */
67 uint8_t SRST_MASK; /* data port bit for SRST */
68 uint8_t OUTPUT_INVERT; /* data port bits that should be inverted */
69 uint8_t INPUT_INVERT; /* status port that should be inverted */
70 uint8_t PORT_INIT; /* initialize data port with this value */
71 uint8_t PORT_EXIT; /* de-initialize data port with this value */
72 uint8_t LED_MASK; /* data port bit for LED */
75 static const struct cable cables[] = {
76 /* name tdo trst tms tck tdi srst o_inv i_inv init exit led */
77 { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 },
78 { "wiggler2", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 },
79 { "wiggler_ntrst_inverted", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80, 0x80, 0x00 },
80 { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 },
81 { "arm-jtag", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x01, 0x80, 0x80, 0x80, 0x00 },
82 { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
83 { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 },
84 { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
85 { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 },
86 { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 },
87 /* Altium Universal JTAG cable. Set the cable to Xilinx Mode and wire to target as follows:
92 SOFT TCK - Target TRST
93 SOFT TDI - Target SRST
95 { "altium", 0x10, 0x20, 0x04, 0x02, 0x01, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08 },
96 { "aspo", 0x10, 0x01, 0x04, 0x08, 0x02, 0x10, 0x17, 0x00, 0x17, 0x17, 0x00 },
97 { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
101 static char *parport_cable;
102 static uint16_t parport_port;
103 static bool parport_exit;
104 static uint32_t parport_toggling_time_ns = 1000;
105 static int wait_states;
107 /* interface variables
109 static const struct cable *cable;
110 static uint8_t dataport_value;
112 #if PARPORT_USE_PPDEV == 1
113 static int device_handle;
115 static unsigned long dataport;
116 static unsigned long statusport;
119 static bb_value_t parport_read(void)
123 #if PARPORT_USE_PPDEV == 1
124 ioctl(device_handle, PPRSTATUS, &data);
126 data = inb(statusport);
129 if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
135 static inline void parport_write_data(void)
138 output = dataport_value ^ cable->OUTPUT_INVERT;
140 #if PARPORT_USE_PPDEV == 1
141 ioctl(device_handle, PPWDATA, &output);
143 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
144 outb(dataport, output);
146 outb(output, dataport);
151 static int parport_write(int tck, int tms, int tdi)
153 int i = wait_states + 1;
156 dataport_value |= cable->TCK_MASK;
158 dataport_value &= ~cable->TCK_MASK;
161 dataport_value |= cable->TMS_MASK;
163 dataport_value &= ~cable->TMS_MASK;
166 dataport_value |= cable->TDI_MASK;
168 dataport_value &= ~cable->TDI_MASK;
171 parport_write_data();
176 /* (1) assert or (0) deassert reset lines */
177 static int parport_reset(int trst, int srst)
179 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
182 dataport_value |= cable->TRST_MASK;
184 dataport_value &= ~cable->TRST_MASK;
187 dataport_value |= cable->SRST_MASK;
189 dataport_value &= ~cable->SRST_MASK;
191 parport_write_data();
196 /* turn LED on parport adapter on (1) or off (0) */
197 static int parport_led(int on)
200 dataport_value |= cable->LED_MASK;
202 dataport_value &= ~cable->LED_MASK;
204 parport_write_data();
209 static int parport_speed(int speed)
215 static int parport_khz(int khz, int *jtag_speed)
218 LOG_DEBUG("RCLK not supported");
222 *jtag_speed = 499999 / (khz * parport_toggling_time_ns);
226 static int parport_speed_div(int speed, int *khz)
228 uint32_t denominator = (speed + 1) * parport_toggling_time_ns;
230 *khz = (499999 + denominator) / denominator;
234 #if PARPORT_USE_GIVEIO == 1
235 static int parport_get_giveio_access(void)
238 OSVERSIONINFO version;
240 version.dwOSVersionInfoSize = sizeof(version);
241 if (!GetVersionEx(&version)) {
245 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
248 h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
249 if (h == INVALID_HANDLE_VALUE) {
260 static struct bitbang_interface parport_bitbang = {
261 .read = &parport_read,
262 .write = &parport_write,
263 .blink = &parport_led,
266 static int parport_init(void)
268 const struct cable *cur_cable;
269 #if PARPORT_USE_PPDEV == 1
275 if (parport_cable == NULL) {
276 parport_cable = strdup("wiggler");
277 LOG_WARNING("No parport cable specified, using default 'wiggler'");
280 while (cur_cable->name) {
281 if (strcmp(cur_cable->name, parport_cable) == 0) {
289 LOG_ERROR("No matching cable found for %s", parport_cable);
290 return ERROR_JTAG_INIT_FAILED;
293 dataport_value = cable->PORT_INIT;
295 #if PARPORT_USE_PPDEV == 1
296 if (device_handle > 0) {
297 LOG_ERROR("device is already opened");
298 return ERROR_JTAG_INIT_FAILED;
301 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
302 LOG_DEBUG("opening /dev/ppi%d...", parport_port);
304 snprintf(buffer, 256, "/dev/ppi%d", parport_port);
305 device_handle = open(buffer, O_WRONLY);
306 #else /* not __FreeBSD__, __FreeBSD_kernel__ */
307 LOG_DEBUG("opening /dev/parport%d...", parport_port);
309 snprintf(buffer, 256, "/dev/parport%d", parport_port);
310 device_handle = open(buffer, O_WRONLY);
311 #endif /* __FreeBSD__, __FreeBSD_kernel__ */
313 if (device_handle < 0) {
315 LOG_ERROR("cannot open device. check it exists and that user read and write rights are set. errno=%d", err);
316 return ERROR_JTAG_INIT_FAILED;
319 LOG_DEBUG("...open");
321 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
322 int i = ioctl(device_handle, PPCLAIM);
325 LOG_ERROR("cannot claim device");
326 return ERROR_JTAG_INIT_FAILED;
329 i = PARPORT_MODE_COMPAT;
330 i = ioctl(device_handle, PPSETMODE, &i);
332 LOG_ERROR(" cannot set compatible mode to device");
333 return ERROR_JTAG_INIT_FAILED;
336 i = IEEE1284_MODE_COMPAT;
337 i = ioctl(device_handle, PPNEGOT, &i);
339 LOG_ERROR("cannot set compatible 1284 mode to device");
340 return ERROR_JTAG_INIT_FAILED;
342 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
344 #else /* not PARPORT_USE_PPDEV */
345 if (parport_port == 0) {
346 parport_port = 0x378;
347 LOG_WARNING("No parport port specified, using default '0x378' (LPT1)");
350 dataport = parport_port;
351 statusport = parport_port + 1;
353 LOG_DEBUG("requesting privileges for parallel port 0x%lx...", dataport);
354 #if PARPORT_USE_GIVEIO == 1
355 if (parport_get_giveio_access() != 0) {
356 #else /* PARPORT_USE_GIVEIO */
357 if (ioperm(dataport, 3, 1) != 0) {
358 #endif /* PARPORT_USE_GIVEIO */
359 LOG_ERROR("missing privileges for direct i/o");
360 return ERROR_JTAG_INIT_FAILED;
362 LOG_DEBUG("...privileges granted");
364 /* make sure parallel port is in right mode (clear tristate and interrupt */
365 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
366 outb(parport_port + 2, 0x0);
368 outb(0x0, parport_port + 2);
371 #endif /* PARPORT_USE_PPDEV */
373 if (parport_reset(0, 0) != ERROR_OK)
375 if (parport_write(0, 0, 0) != ERROR_OK)
377 if (parport_led(1) != ERROR_OK)
380 bitbang_interface = &parport_bitbang;
385 static int parport_quit(void)
387 if (parport_led(0) != ERROR_OK)
391 dataport_value = cable->PORT_EXIT;
392 parport_write_data();
396 parport_cable = NULL;
401 COMMAND_HANDLER(parport_handle_parport_port_command)
404 /* only if the port wasn't overwritten by cmdline */
405 if (parport_port == 0)
406 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], parport_port);
408 LOG_ERROR("The parport port was already configured!");
413 command_print(CMD, "parport port = 0x%" PRIx16 "", parport_port);
418 COMMAND_HANDLER(parport_handle_parport_cable_command)
423 /* only if the cable name wasn't overwritten by cmdline */
424 if (parport_cable == 0) {
425 /* REVISIT first verify that it's listed in cables[] ... */
426 parport_cable = malloc(strlen(CMD_ARGV[0]) + sizeof(char));
427 strcpy(parport_cable, CMD_ARGV[0]);
430 /* REVISIT it's probably worth returning the current value ... */
435 COMMAND_HANDLER(parport_handle_write_on_exit_command)
438 return ERROR_COMMAND_SYNTAX_ERROR;
440 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], parport_exit);
445 COMMAND_HANDLER(parport_handle_parport_toggling_time_command)
449 int retval = parse_u32(CMD_ARGV[0], &ns);
451 if (retval != ERROR_OK)
455 LOG_ERROR("0 ns is not a valid parport toggling time");
459 parport_toggling_time_ns = ns;
460 retval = jtag_get_speed(&wait_states);
461 if (retval != ERROR_OK) {
462 /* if jtag_get_speed fails then the clock_mode
463 * has not been configured, this happens if parport_toggling_time is
464 * called before the adapter speed is set */
465 LOG_INFO("no parport speed set - defaulting to zero wait states");
470 command_print(CMD, "parport toggling time = %" PRIu32 " ns",
471 parport_toggling_time_ns);
476 static const struct command_registration parport_command_handlers[] = {
478 .name = "parport_port",
479 .handler = parport_handle_parport_port_command,
480 .mode = COMMAND_CONFIG,
481 .help = "Display the address of the I/O port (e.g. 0x378) "
482 "or the number of the '/dev/parport' device used. "
483 "If a parameter is provided, first change that port.",
484 .usage = "[port_number]",
487 .name = "parport_cable",
488 .handler = parport_handle_parport_cable_command,
489 .mode = COMMAND_CONFIG,
490 .help = "Set the layout of the parallel port cable "
491 "used to connect to the target.",
492 /* REVISIT there's no way to list layouts we know ... */
496 .name = "parport_write_on_exit",
497 .handler = parport_handle_write_on_exit_command,
498 .mode = COMMAND_CONFIG,
499 .help = "Configure the parallel driver to write "
500 "a known value to the parallel interface on exit.",
501 .usage = "('on'|'off')",
504 .name = "parport_toggling_time",
505 .handler = parport_handle_parport_toggling_time_command,
506 .mode = COMMAND_CONFIG,
507 .help = "Displays or assigns how many nanoseconds it "
508 "takes for the hardware to toggle TCK.",
509 .usage = "[nanoseconds]",
511 COMMAND_REGISTRATION_DONE
514 static struct jtag_interface parport_interface = {
515 .supported = DEBUG_CAP_TMS_SEQ,
516 .execute_queue = bitbang_execute_queue,
519 struct adapter_driver parport_adapter_driver = {
521 .transports = jtag_only,
522 .commands = parport_command_handlers,
524 .init = parport_init,
525 .quit = parport_quit,
526 .reset = parport_reset,
527 .speed = parport_speed,
529 .speed_div = parport_speed_div,
531 .jtag_ops = &parport_interface,