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