Uwe Hermann fixed some warnings.
[fw/openocd] / src / jtag / parport.c
1 /***************************************************************************\r
2  *   Copyright (C) 2005 by Dominic Rath                                    *\r
3  *   Dominic.Rath@gmx.de                                                   *\r
4  *                                                                         *\r
5  *   This program is free software; you can redistribute it and/or modify  *\r
6  *   it under the terms of the GNU General Public License as published by  *\r
7  *   the Free Software Foundation; either version 2 of the License, or     *\r
8  *   (at your option) any later version.                                   *\r
9  *                                                                         *\r
10  *   This program is distributed in the hope that it will be useful,       *\r
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *\r
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *\r
13  *   GNU General Public License for more details.                          *\r
14  *                                                                         *\r
15  *   You should have received a copy of the GNU General Public License     *\r
16  *   along with this program; if not, write to the                         *\r
17  *   Free Software Foundation, Inc.,                                       *\r
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *\r
19  ***************************************************************************/\r
20 #ifdef HAVE_CONFIG_H\r
21 #include "config.h"\r
22 #endif\r
23 \r
24 #include "replacements.h"\r
25 \r
26 #include "jtag.h"\r
27 #include "bitbang.h"\r
28 \r
29 /* system includes */\r
30 // -ino: 060521-1036\r
31 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)\r
32 \r
33 #include <sys/types.h>\r
34 #include <machine/sysarch.h>\r
35 #include <machine/cpufunc.h>\r
36 #define ioperm(startport,length,enable)\\r
37   i386_set_ioperm((startport), (length), (enable))\r
38 \r
39 #else\r
40 \r
41 #ifdef _WIN32\r
42 #include "errno.h"\r
43 #endif /* _WIN32 */\r
44 \r
45 #endif /* __FreeBSD__ */\r
46 \r
47 #include <string.h>\r
48 #include <stdlib.h>\r
49 #include <stdio.h>\r
50 \r
51 #if PARPORT_USE_PPDEV == 1\r
52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)\r
53 #include <dev/ppbus/ppi.h>\r
54 #include <dev/ppbus/ppbconf.h>\r
55 #define PPRSTATUS       PPIGSTATUS\r
56 #define PPWDATA         PPISDATA\r
57 #else\r
58 #include <linux/parport.h>\r
59 #include <linux/ppdev.h>\r
60 #endif\r
61 #include <fcntl.h>\r
62 #include <sys/ioctl.h>\r
63 #else /* not PARPORT_USE_PPDEV */\r
64 #ifndef _WIN32\r
65 #include <sys/io.h>\r
66 #endif\r
67 #endif\r
68 \r
69 #if PARPORT_USE_GIVEIO == 1\r
70 #if IS_CYGWIN == 1\r
71 #include <windows.h>\r
72 #include <errno.h>\r
73 #undef ERROR\r
74 #endif\r
75 #endif\r
76 \r
77 #include "log.h"\r
78 \r
79 /* parallel port cable description\r
80  */\r
81 typedef struct cable_s\r
82 {\r
83         char* name;\r
84         u8 TDO_MASK;    /* status port bit containing current TDO value */\r
85         u8 TRST_MASK;   /* data port bit for TRST */\r
86         u8 TMS_MASK;    /* data port bit for TMS */\r
87         u8 TCK_MASK;    /* data port bit for TCK */\r
88         u8 TDI_MASK;    /* data port bit for TDI */\r
89         u8 SRST_MASK;   /* data port bit for SRST */\r
90         u8 OUTPUT_INVERT;       /* data port bits that should be inverted */\r
91         u8 INPUT_INVERT;        /* status port that should be inverted */\r
92         u8 PORT_INIT;   /* initialize data port with this value */\r
93         u8 PORT_EXIT;   /* de-initialize data port with this value */\r
94         u8 LED_MASK;    /* data port bit for LED */\r
95 } cable_t;\r
96 \r
97 cable_t cables[] = \r
98 {       \r
99         /* name                                 tdo   trst  tms   tck   tdi   srst  o_inv i_inv init  exit  led */\r
100         { "wiggler",                    0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 },\r
101         { "wiggler2",                   0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 },\r
102         { "wiggler_ntrst_inverted",\r
103                                                         0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80, 0x80, 0x00 },\r
104         { "old_amt_wiggler",    0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 },\r
105         { "chameleon",                  0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },\r
106         { "dlc5",                               0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 },\r
107         { "triton",                             0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },\r
108         { "lattice",                    0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 },\r
109         { "flashlink",                  0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 },\r
110 /* Altium Universal JTAG cable. Set the cable to Xilinx Mode and wire to target as follows:\r
111         HARD TCK - Target TCK\r
112         HARD TMS - Target TMS\r
113         HARD TDI - Target TDI\r
114         HARD TDO - Target TDO\r
115         SOFT TCK - Target TRST\r
116         SOFT TDI - Target SRST\r
117 */\r
118         { "altium",                     0x10, 0x20, 0x04, 0x02, 0x01, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08 },\r
119         { NULL,                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }\r
120 };\r
121 \r
122 /* configuration */\r
123 char* parport_cable = NULL;\r
124 u16 parport_port;\r
125 static int parport_exit = 0;\r
126 \r
127 /* interface variables\r
128  */\r
129 static cable_t* cable;\r
130 static u8 dataport_value = 0x0;\r
131 \r
132 #if PARPORT_USE_PPDEV == 1\r
133 static int device_handle;\r
134 #else\r
135 static unsigned long dataport;\r
136 static unsigned long statusport;\r
137 #endif\r
138 \r
139 /* low level command set\r
140  */\r
141 int parport_read(void);\r
142 void parport_write(int tck, int tms, int tdi);\r
143 void parport_reset(int trst, int srst);\r
144 void parport_led(int on);\r
145 \r
146 int parport_speed(int speed);\r
147 int parport_register_commands(struct command_context_s *cmd_ctx);\r
148 int parport_init(void);\r
149 int parport_quit(void);\r
150 \r
151 /* interface commands */\r
152 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);\r
153 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);\r
154 int parport_handle_write_on_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);\r
155 \r
156 jtag_interface_t parport_interface = \r
157 {\r
158         .name = "parport",\r
159         \r
160         .execute_queue = bitbang_execute_queue,\r
161 \r
162         .speed = parport_speed, \r
163         .register_commands = parport_register_commands,\r
164         .init = parport_init,\r
165         .quit = parport_quit,\r
166 };\r
167 \r
168 bitbang_interface_t parport_bitbang =\r
169 {\r
170         .read = parport_read,\r
171         .write = parport_write,\r
172         .reset = parport_reset,\r
173         .blink = parport_led\r
174 };\r
175 \r
176 int parport_read(void)\r
177 {\r
178         int data = 0;\r
179         \r
180 #if PARPORT_USE_PPDEV == 1\r
181         ioctl(device_handle, PPRSTATUS, & data);\r
182 #else\r
183         data = inb(statusport);\r
184 #endif\r
185 \r
186         if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)\r
187                 return 1;\r
188         else\r
189                 return 0;\r
190 }\r
191 \r
192 static inline void parport_write_data(void)\r
193 {\r
194         u8 output;\r
195         output = dataport_value ^ cable->OUTPUT_INVERT;\r
196 \r
197 #if PARPORT_USE_PPDEV == 1\r
198         ioctl(device_handle, PPWDATA, &output);\r
199 #else\r
200 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)\r
201         outb(dataport, output);\r
202 #else\r
203         outb(output, dataport);\r
204 #endif\r
205 #endif\r
206 }\r
207 \r
208 void parport_write(int tck, int tms, int tdi)\r
209 {\r
210         int i = jtag_speed + 1;\r
211         \r
212         if (tck)\r
213                 dataport_value |= cable->TCK_MASK;\r
214         else\r
215                 dataport_value &= ~cable->TCK_MASK;\r
216         \r
217         if (tms)\r
218                 dataport_value |= cable->TMS_MASK;\r
219         else\r
220                 dataport_value &= ~cable->TMS_MASK;\r
221         \r
222         if (tdi)\r
223                 dataport_value |= cable->TDI_MASK;\r
224         else\r
225                 dataport_value &= ~cable->TDI_MASK;\r
226                 \r
227         while (i-- > 0)\r
228                 parport_write_data();\r
229 }\r
230 \r
231 /* (1) assert or (0) deassert reset lines */\r
232 void parport_reset(int trst, int srst)\r
233 {\r
234         DEBUG("trst: %i, srst: %i", trst, srst);\r
235 \r
236         if (trst == 0)\r
237                 dataport_value |= cable->TRST_MASK;\r
238         else if (trst == 1)\r
239                 dataport_value &= ~cable->TRST_MASK;\r
240 \r
241         if (srst == 0)\r
242                 dataport_value |= cable->SRST_MASK;\r
243         else if (srst == 1)\r
244                 dataport_value &= ~cable->SRST_MASK;\r
245         \r
246         parport_write_data();\r
247 }\r
248         \r
249 /* turn LED on parport adapter on (1) or off (0) */\r
250 void parport_led(int on)\r
251 {\r
252         if (on)\r
253                 dataport_value |= cable->LED_MASK;\r
254         else\r
255                 dataport_value &= ~cable->LED_MASK;\r
256 \r
257         parport_write_data();\r
258 }\r
259 \r
260 int parport_speed(int speed)\r
261 {\r
262         jtag_speed = speed;\r
263         \r
264         return ERROR_OK;\r
265 }\r
266 \r
267 int parport_register_commands(struct command_context_s *cmd_ctx)\r
268 {\r
269         register_command(cmd_ctx, NULL, "parport_port", parport_handle_parport_port_command,\r
270                 COMMAND_CONFIG, NULL);\r
271         register_command(cmd_ctx, NULL, "parport_cable", parport_handle_parport_cable_command,\r
272                 COMMAND_CONFIG, NULL);\r
273         register_command(cmd_ctx, NULL, "parport_write_on_exit", parport_handle_write_on_exit_command,\r
274                 COMMAND_CONFIG, NULL);\r
275 \r
276         return ERROR_OK;\r
277 }\r
278 \r
279 #if PARPORT_USE_GIVEIO == 1\r
280 int parport_get_giveio_access()\r
281 {\r
282     HANDLE h;\r
283     OSVERSIONINFO version;\r
284 \r
285     version.dwOSVersionInfoSize = sizeof version;\r
286     if (!GetVersionEx( &version )) {\r
287         errno = EINVAL;\r
288         return -1;\r
289     }\r
290     if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)\r
291         return 0;\r
292 \r
293     h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );\r
294     if (h == INVALID_HANDLE_VALUE) {\r
295         errno = ENODEV;\r
296         return -1;\r
297     }\r
298 \r
299     CloseHandle( h );\r
300 \r
301     return 0;\r
302 }\r
303 #endif\r
304 \r
305 int parport_init(void)\r
306 {\r
307         cable_t *cur_cable;\r
308 #if PARPORT_USE_PPDEV == 1\r
309         char buffer[256];\r
310         int i = 0;\r
311 #endif\r
312         \r
313         cur_cable = cables;\r
314         \r
315         if ((parport_cable == NULL) || (parport_cable[0] == 0))\r
316         {\r
317                 parport_cable = "wiggler";\r
318                 WARNING("No parport cable specified, using default 'wiggler'");\r
319         }\r
320         \r
321         while (cur_cable->name)\r
322         {\r
323                 if (strcmp(cur_cable->name, parport_cable) == 0)\r
324                 {\r
325                         cable = cur_cable;\r
326                         break;\r
327                 }\r
328                 cur_cable++;\r
329         }\r
330 \r
331         if (!cable)\r
332         {\r
333                 ERROR("No matching cable found for %s", parport_cable);\r
334                 return ERROR_JTAG_INIT_FAILED;\r
335         }\r
336         \r
337         dataport_value = cable->PORT_INIT;\r
338         \r
339 #if PARPORT_USE_PPDEV == 1\r
340         if (device_handle > 0)\r
341         {\r
342                 ERROR("device is already opened");\r
343                 return ERROR_JTAG_INIT_FAILED;\r
344         }\r
345 \r
346 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)\r
347         DEBUG("opening /dev/ppi%d...", parport_port);\r
348 \r
349         snprintf(buffer, 256, "/dev/ppi%d", parport_port);\r
350         device_handle = open(buffer, O_WRONLY);\r
351 #else /* not __FreeBSD__, __FreeBSD_kernel__ */\r
352         DEBUG("opening /dev/parport%d...", parport_port);\r
353 \r
354         snprintf(buffer, 256, "/dev/parport%d", parport_port);\r
355         device_handle = open(buffer, O_WRONLY);\r
356 #endif /* __FreeBSD__, __FreeBSD_kernel__ */\r
357 \r
358         if (device_handle < 0)\r
359         {\r
360                 ERROR("cannot open device. check it exists and that user read and write rights are set");\r
361                 return ERROR_JTAG_INIT_FAILED;\r
362         }\r
363 \r
364         DEBUG("...open");\r
365 \r
366 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)\r
367         i=ioctl(device_handle, PPCLAIM);\r
368         if (i<0)\r
369         {\r
370                 ERROR("cannot claim device");\r
371                 return ERROR_JTAG_INIT_FAILED;\r
372         }\r
373 \r
374         i = PARPORT_MODE_COMPAT;\r
375         i= ioctl(device_handle, PPSETMODE, & i);\r
376         if (i<0)\r
377         {\r
378                 ERROR(" cannot set compatible mode to device");\r
379                 return ERROR_JTAG_INIT_FAILED;\r
380         }\r
381 \r
382         i = IEEE1284_MODE_COMPAT;\r
383         i = ioctl(device_handle, PPNEGOT, & i);\r
384         if (i<0)\r
385         {\r
386                 ERROR("cannot set compatible 1284 mode to device");\r
387                 return ERROR_JTAG_INIT_FAILED;\r
388         }\r
389 #endif /* not __FreeBSD__, __FreeBSD_kernel__ */\r
390 \r
391 #else /* not PARPORT_USE_PPDEV */\r
392         if (parport_port == 0)\r
393         {\r
394                 parport_port = 0x378;\r
395                 WARNING("No parport port specified, using default '0x378' (LPT1)");\r
396         }\r
397         \r
398         dataport = parport_port;\r
399         statusport = parport_port + 1;\r
400         \r
401         DEBUG("requesting privileges for parallel port 0x%lx...", dataport);\r
402 #if PARPORT_USE_GIVEIO == 1\r
403         if (parport_get_giveio_access() != 0)\r
404 #else /* PARPORT_USE_GIVEIO */\r
405         if (ioperm(dataport, 3, 1) != 0)\r
406 #endif /* PARPORT_USE_GIVEIO */\r
407         {\r
408                 ERROR("missing privileges for direct i/o");\r
409                 return ERROR_JTAG_INIT_FAILED;\r
410         }\r
411         DEBUG("...privileges granted");\r
412         \r
413         /* make sure parallel port is in right mode (clear tristate and interrupt */\r
414         #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)\r
415                 outb(parport_port + 2, 0x0);\r
416         #else\r
417                 outb(0x0, parport_port + 2);\r
418         #endif\r
419         \r
420 #endif /* PARPORT_USE_PPDEV */\r
421         \r
422         parport_reset(0, 0);\r
423         parport_write(0, 0, 0);\r
424         parport_led(1);\r
425 \r
426         bitbang_interface = &parport_bitbang;   \r
427 \r
428         return ERROR_OK;\r
429 }\r
430 \r
431 int parport_quit(void)\r
432 {\r
433         parport_led(0);\r
434 \r
435         if (parport_exit)\r
436         {\r
437                 dataport_value = cable->PORT_EXIT;\r
438                 parport_write_data();\r
439         }\r
440         \r
441         if (parport_cable)\r
442         {\r
443                 free(parport_cable);\r
444                 parport_cable = NULL;\r
445         }\r
446         \r
447         return ERROR_OK;\r
448 }\r
449 \r
450 int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)\r
451 {\r
452         if (argc == 0)\r
453                 return ERROR_OK;\r
454 \r
455         /* only if the port wasn't overwritten by cmdline */\r
456         if (parport_port == 0)\r
457                 parport_port = strtoul(args[0], NULL, 0);\r
458 \r
459         return ERROR_OK;\r
460 }\r
461 \r
462 int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)\r
463 {\r
464         if (argc == 0)\r
465                 return ERROR_OK;\r
466 \r
467         /* only if the cable name wasn't overwritten by cmdline */\r
468         if (parport_cable == 0)\r
469         {\r
470                 parport_cable = malloc(strlen(args[0]) + sizeof(char));\r
471                 strcpy(parport_cable, args[0]);\r
472         }\r
473 \r
474         return ERROR_OK;\r
475 }\r
476 \r
477 int parport_handle_write_on_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)\r
478 {\r
479         if (argc != 1)\r
480         {\r
481                 command_print(cmd_ctx, "usage: parport_write_on_exit <on|off>");\r
482                 return ERROR_OK;\r
483         }\r
484         \r
485         if (strcmp(args[0], "on") == 0)\r
486                 parport_exit = 1;\r
487         else if (strcmp(args[0], "off") == 0)\r
488                 parport_exit = 0;\r
489         \r
490         return ERROR_OK;\r
491 }\r