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