Remove FSF address from GPL notices
[fw/openocd] / src / jtag / drivers / parport.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2008 by Spencer Oliver                                  *
6  *   spen@spen-soft.co.uk                                                  *
7  *                                                                         *
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.                                   *
12  *                                                                         *
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.                          *
17  *                                                                         *
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  ***************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <jtag/interface.h>
27 #include "bitbang.h"
28
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__ */
36
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
43 #else
44 #include <linux/parport.h>
45 #include <linux/ppdev.h>
46 #endif
47 #include <sys/ioctl.h>
48 #else /* not PARPORT_USE_PPDEV */
49 #ifndef _WIN32
50 #include <sys/io.h>
51 #endif
52 #endif
53
54 #if PARPORT_USE_GIVEIO == 1 && IS_CYGWIN == 1
55 #include <windows.h>
56 #endif
57
58 /* parallel port cable description
59  */
60 struct cable {
61         const char *name;
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 */
73 };
74
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:
88         HARD TCK - Target TCK
89         HARD TMS - Target TMS
90         HARD TDI - Target TDI
91         HARD TDO - Target TDO
92         SOFT TCK - Target TRST
93         SOFT TDI - Target SRST
94 */
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 }
98 };
99
100 /* configuration */
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;
106
107 /* interface variables
108  */
109 static const struct cable *cable;
110 static uint8_t dataport_value;
111
112 #if PARPORT_USE_PPDEV == 1
113 static int device_handle;
114 #else
115 static unsigned long dataport;
116 static unsigned long statusport;
117 #endif
118
119 static int parport_read(void)
120 {
121         int data = 0;
122
123 #if PARPORT_USE_PPDEV == 1
124         ioctl(device_handle, PPRSTATUS, &data);
125 #else
126         data = inb(statusport);
127 #endif
128
129         if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
130                 return 1;
131         else
132                 return 0;
133 }
134
135 static inline void parport_write_data(void)
136 {
137         uint8_t output;
138         output = dataport_value ^ cable->OUTPUT_INVERT;
139
140 #if PARPORT_USE_PPDEV == 1
141         ioctl(device_handle, PPWDATA, &output);
142 #else
143 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
144         outb(dataport, output);
145 #else
146         outb(output, dataport);
147 #endif
148 #endif
149 }
150
151 static void parport_write(int tck, int tms, int tdi)
152 {
153         int i = wait_states + 1;
154
155         if (tck)
156                 dataport_value |= cable->TCK_MASK;
157         else
158                 dataport_value &= ~cable->TCK_MASK;
159
160         if (tms)
161                 dataport_value |= cable->TMS_MASK;
162         else
163                 dataport_value &= ~cable->TMS_MASK;
164
165         if (tdi)
166                 dataport_value |= cable->TDI_MASK;
167         else
168                 dataport_value &= ~cable->TDI_MASK;
169
170         while (i-- > 0)
171                 parport_write_data();
172 }
173
174 /* (1) assert or (0) deassert reset lines */
175 static void parport_reset(int trst, int srst)
176 {
177         LOG_DEBUG("trst: %i, srst: %i", trst, srst);
178
179         if (trst == 0)
180                 dataport_value |= cable->TRST_MASK;
181         else if (trst == 1)
182                 dataport_value &= ~cable->TRST_MASK;
183
184         if (srst == 0)
185                 dataport_value |= cable->SRST_MASK;
186         else if (srst == 1)
187                 dataport_value &= ~cable->SRST_MASK;
188
189         parport_write_data();
190 }
191
192 /* turn LED on parport adapter on (1) or off (0) */
193 static void parport_led(int on)
194 {
195         if (on)
196                 dataport_value |= cable->LED_MASK;
197         else
198                 dataport_value &= ~cable->LED_MASK;
199
200         parport_write_data();
201 }
202
203 static int parport_speed(int speed)
204 {
205         wait_states = speed;
206         return ERROR_OK;
207 }
208
209 static int parport_khz(int khz, int *jtag_speed)
210 {
211         if (khz == 0) {
212                 LOG_DEBUG("RCLK not supported");
213                 return ERROR_FAIL;
214         }
215
216         *jtag_speed = 499999 / (khz * parport_toggling_time_ns);
217         return ERROR_OK;
218 }
219
220 static int parport_speed_div(int speed, int *khz)
221 {
222         uint32_t denominator = (speed + 1) * parport_toggling_time_ns;
223
224         *khz = (499999 + denominator) / denominator;
225         return ERROR_OK;
226 }
227
228 #if PARPORT_USE_GIVEIO == 1
229 static int parport_get_giveio_access(void)
230 {
231         HANDLE h;
232         OSVERSIONINFO version;
233
234         version.dwOSVersionInfoSize = sizeof version;
235         if (!GetVersionEx(&version)) {
236                 errno = EINVAL;
237                 return -1;
238         }
239         if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
240                 return 0;
241
242         h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
243         if (h == INVALID_HANDLE_VALUE) {
244                 errno = ENODEV;
245                 return -1;
246         }
247
248         CloseHandle(h);
249
250         return 0;
251 }
252 #endif
253
254 static struct bitbang_interface parport_bitbang = {
255                 .read = &parport_read,
256                 .write = &parport_write,
257                 .reset = &parport_reset,
258                 .blink = &parport_led,
259         };
260
261 static int parport_init(void)
262 {
263         const struct cable *cur_cable;
264 #if PARPORT_USE_PPDEV == 1
265         char buffer[256];
266 #endif
267
268         cur_cable = cables;
269
270         if (parport_cable == NULL) {
271                 parport_cable = strdup("wiggler");
272                 LOG_WARNING("No parport cable specified, using default 'wiggler'");
273         }
274
275         while (cur_cable->name) {
276                 if (strcmp(cur_cable->name, parport_cable) == 0) {
277                         cable = cur_cable;
278                         break;
279                 }
280                 cur_cable++;
281         }
282
283         if (!cable) {
284                 LOG_ERROR("No matching cable found for %s", parport_cable);
285                 return ERROR_JTAG_INIT_FAILED;
286         }
287
288         dataport_value = cable->PORT_INIT;
289
290 #if PARPORT_USE_PPDEV == 1
291         if (device_handle > 0) {
292                 LOG_ERROR("device is already opened");
293                 return ERROR_JTAG_INIT_FAILED;
294         }
295
296 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
297         LOG_DEBUG("opening /dev/ppi%d...", parport_port);
298
299         snprintf(buffer, 256, "/dev/ppi%d", parport_port);
300         device_handle = open(buffer, O_WRONLY);
301 #else /* not __FreeBSD__, __FreeBSD_kernel__ */
302         LOG_DEBUG("opening /dev/parport%d...", parport_port);
303
304         snprintf(buffer, 256, "/dev/parport%d", parport_port);
305         device_handle = open(buffer, O_WRONLY);
306 #endif /* __FreeBSD__, __FreeBSD_kernel__ */
307
308         if (device_handle < 0) {
309                 int err = errno;
310                 LOG_ERROR("cannot open device. check it exists and that user read and write rights are set. errno=%d", err);
311                 return ERROR_JTAG_INIT_FAILED;
312         }
313
314         LOG_DEBUG("...open");
315
316 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
317         int i = ioctl(device_handle, PPCLAIM);
318
319         if (i < 0) {
320                 LOG_ERROR("cannot claim device");
321                 return ERROR_JTAG_INIT_FAILED;
322         }
323
324         i = PARPORT_MODE_COMPAT;
325         i = ioctl(device_handle, PPSETMODE, &i);
326         if (i < 0) {
327                 LOG_ERROR(" cannot set compatible mode to device");
328                 return ERROR_JTAG_INIT_FAILED;
329         }
330
331         i = IEEE1284_MODE_COMPAT;
332         i = ioctl(device_handle, PPNEGOT, &i);
333         if (i < 0) {
334                 LOG_ERROR("cannot set compatible 1284 mode to device");
335                 return ERROR_JTAG_INIT_FAILED;
336         }
337 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
338
339 #else /* not PARPORT_USE_PPDEV */
340         if (parport_port == 0) {
341                 parport_port = 0x378;
342                 LOG_WARNING("No parport port specified, using default '0x378' (LPT1)");
343         }
344
345         dataport = parport_port;
346         statusport = parport_port + 1;
347
348         LOG_DEBUG("requesting privileges for parallel port 0x%lx...", dataport);
349 #if PARPORT_USE_GIVEIO == 1
350         if (parport_get_giveio_access() != 0) {
351 #else /* PARPORT_USE_GIVEIO */
352         if (ioperm(dataport, 3, 1) != 0) {
353 #endif /* PARPORT_USE_GIVEIO */
354                 LOG_ERROR("missing privileges for direct i/o");
355                 return ERROR_JTAG_INIT_FAILED;
356         }
357         LOG_DEBUG("...privileges granted");
358
359         /* make sure parallel port is in right mode (clear tristate and interrupt */
360         #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
361                 outb(parport_port + 2, 0x0);
362         #else
363                 outb(0x0, parport_port + 2);
364         #endif
365
366 #endif /* PARPORT_USE_PPDEV */
367
368         parport_reset(0, 0);
369         parport_write(0, 0, 0);
370         parport_led(1);
371
372         bitbang_interface = &parport_bitbang;
373
374         return ERROR_OK;
375 }
376
377 static int parport_quit(void)
378 {
379         parport_led(0);
380
381         if (parport_exit) {
382                 dataport_value = cable->PORT_EXIT;
383                 parport_write_data();
384         }
385
386         if (parport_cable) {
387                 free(parport_cable);
388                 parport_cable = NULL;
389         }
390
391         return ERROR_OK;
392 }
393
394 COMMAND_HANDLER(parport_handle_parport_port_command)
395 {
396         if (CMD_ARGC == 1) {
397                 /* only if the port wasn't overwritten by cmdline */
398                 if (parport_port == 0)
399                         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], parport_port);
400                 else {
401                         LOG_ERROR("The parport port was already configured!");
402                         return ERROR_FAIL;
403                 }
404         }
405
406         command_print(CMD_CTX, "parport port = 0x%" PRIx16 "", parport_port);
407
408         return ERROR_OK;
409 }
410
411 COMMAND_HANDLER(parport_handle_parport_cable_command)
412 {
413         if (CMD_ARGC == 0)
414                 return ERROR_OK;
415
416         /* only if the cable name wasn't overwritten by cmdline */
417         if (parport_cable == 0) {
418                 /* REVISIT first verify that it's listed in cables[] ... */
419                 parport_cable = malloc(strlen(CMD_ARGV[0]) + sizeof(char));
420                 strcpy(parport_cable, CMD_ARGV[0]);
421         }
422
423         /* REVISIT it's probably worth returning the current value ... */
424
425         return ERROR_OK;
426 }
427
428 COMMAND_HANDLER(parport_handle_write_on_exit_command)
429 {
430         if (CMD_ARGC != 1)
431                 return ERROR_COMMAND_SYNTAX_ERROR;
432
433         COMMAND_PARSE_ON_OFF(CMD_ARGV[0], parport_exit);
434
435         return ERROR_OK;
436 }
437
438 COMMAND_HANDLER(parport_handle_parport_toggling_time_command)
439 {
440         if (CMD_ARGC == 1) {
441                 uint32_t ns;
442                 int retval = parse_u32(CMD_ARGV[0], &ns);
443
444                 if (ERROR_OK != retval)
445                         return retval;
446
447                 if (ns == 0) {
448                         LOG_ERROR("0 ns is not a valid parport toggling time");
449                         return ERROR_FAIL;
450                 }
451
452                 parport_toggling_time_ns = ns;
453                 retval = jtag_get_speed(&wait_states);
454                 if (retval != ERROR_OK) {
455                         /* if jtag_get_speed fails then the clock_mode
456                          * has not been configured, this happens if parport_toggling_time is
457                          * called before the adapter speed is set */
458                         LOG_INFO("no parport speed set - defaulting to zero wait states");
459                         wait_states = 0;
460                 }
461         }
462
463         command_print(CMD_CTX, "parport toggling time = %" PRIu32 " ns",
464                         parport_toggling_time_ns);
465
466         return ERROR_OK;
467 }
468
469 static const struct command_registration parport_command_handlers[] = {
470         {
471                 .name = "parport_port",
472                 .handler = parport_handle_parport_port_command,
473                 .mode = COMMAND_CONFIG,
474                 .help = "Display the address of the I/O port (e.g. 0x378) "
475                         "or the number of the '/dev/parport' device used.  "
476                         "If a parameter is provided, first change that port.",
477                 .usage = "[port_number]",
478         },
479         {
480                 .name = "parport_cable",
481                 .handler = parport_handle_parport_cable_command,
482                 .mode = COMMAND_CONFIG,
483                 .help = "Set the layout of the parallel port cable "
484                         "used to connect to the target.",
485                 /* REVISIT there's no way to list layouts we know ... */
486                 .usage = "[layout]",
487         },
488         {
489                 .name = "parport_write_on_exit",
490                 .handler = parport_handle_write_on_exit_command,
491                 .mode = COMMAND_CONFIG,
492                 .help = "Configure the parallel driver to write "
493                         "a known value to the parallel interface on exit.",
494                 .usage = "('on'|'off')",
495         },
496         {
497                 .name = "parport_toggling_time",
498                 .handler = parport_handle_parport_toggling_time_command,
499                 .mode = COMMAND_CONFIG,
500                 .help = "Displays or assigns how many nanoseconds it "
501                         "takes for the hardware to toggle TCK.",
502                 .usage = "[nanoseconds]",
503         },
504         COMMAND_REGISTRATION_DONE
505 };
506
507 struct jtag_interface parport_interface = {
508         .name = "parport",
509         .supported = DEBUG_CAP_TMS_SEQ,
510         .commands = parport_command_handlers,
511
512         .init = parport_init,
513         .quit = parport_quit,
514         .khz = parport_khz,
515         .speed_div = parport_speed_div,
516         .speed = parport_speed,
517         .execute_queue = bitbang_execute_queue,
518 };