arm-jtag-ew: -Wshadow fix
[openocd.git] / src / jtag / drivers / gw16012.c
blobcb29d33aac99e0d864d58b52eeea115ddc5528b8
1 /***************************************************************************
2 * Copyright (C) 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <jtag/interface.h>
25 #include <jtag/commands.h>
28 #if 1
29 #define _DEBUG_GW16012_IO_
30 #endif
32 /* system includes */
33 /* -ino: 060521-1036 */
34 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
36 #include <machine/sysarch.h>
37 #include <machine/cpufunc.h>
38 #define ioperm(startport,length,enable)\
39 i386_set_ioperm((startport), (length), (enable))
41 #else
43 #endif /* __FreeBSD__, __FreeBSD_kernel__ */
46 #if PARPORT_USE_PPDEV == 1
47 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
48 #include <dev/ppbus/ppi.h>
49 #include <dev/ppbus/ppbconf.h>
50 #define PPRSTATUS PPIGSTATUS
51 #define PPWDATA PPISDATA
52 #else
53 #include <linux/parport.h>
54 #include <linux/ppdev.h>
55 #endif
56 #include <fcntl.h>
57 #include <sys/ioctl.h>
58 #else /* not PARPORT_USE_PPDEV */
59 #ifndef _WIN32
60 #include <sys/io.h>
61 #endif
62 #endif
64 #if PARPORT_USE_GIVEIO == 1 && IS_CYGWIN == 1
65 #include <windows.h>
66 #endif
69 /* configuration */
70 uint16_t gw16012_port;
72 /* interface variables
74 static uint8_t gw16012_msb = 0x0;
75 static uint8_t gw16012_control_value = 0x0;
77 #if PARPORT_USE_PPDEV == 1
78 static int device_handle;
79 #endif
81 static void gw16012_data(uint8_t value)
83 value = (value & 0x7f) | gw16012_msb;
84 gw16012_msb ^= 0x80; /* toggle MSB */
86 #ifdef _DEBUG_GW16012_IO_
87 LOG_DEBUG("%2.2x", value);
88 #endif
90 #if PARPORT_USE_PPDEV == 1
91 ioctl(device_handle, PPWDATA, &value);
92 #else
93 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
94 outb(gw16012_port, value);
95 #else
96 outb(value, gw16012_port);
97 #endif
98 #endif
101 static void gw16012_control(uint8_t value)
103 if (value != gw16012_control_value)
105 gw16012_control_value = value;
107 #ifdef _DEBUG_GW16012_IO_
108 LOG_DEBUG("%2.2x", gw16012_control_value);
109 #endif
111 #if PARPORT_USE_PPDEV == 1
112 ioctl(device_handle, PPWCONTROL, &gw16012_control_value);
113 #else
114 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
115 outb(gw16012_port + 2, gw16012_control_value);
116 #else
117 outb(gw16012_control_value, gw16012_port + 2);
118 #endif
119 #endif
123 static void gw16012_input(uint8_t *value)
125 #if PARPORT_USE_PPDEV == 1
126 ioctl(device_handle, PPRSTATUS, value);
127 #else
128 *value = inb(gw16012_port + 1);
129 #endif
131 #ifdef _DEBUG_GW16012_IO_
132 LOG_DEBUG("%2.2x", *value);
133 #endif
136 /* (1) assert or (0) deassert reset lines */
137 static void gw16012_reset(int trst, int srst)
139 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
141 if (trst == 0)
142 gw16012_control(0x0d);
143 else if (trst == 1)
144 gw16012_control(0x0c);
146 if (srst == 0)
147 gw16012_control(0x0a);
148 else if (srst == 1)
149 gw16012_control(0x0b);
152 static int gw16012_speed(int speed)
155 return ERROR_OK;
158 static void gw16012_end_state(tap_state_t state)
160 if (tap_is_state_stable(state))
161 tap_set_end_state(state);
162 else
164 LOG_ERROR("BUG: %i is not a valid end state", state);
165 exit(-1);
169 static void gw16012_state_move(void)
171 int i = 0, tms = 0;
172 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
173 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
175 gw16012_control(0x0); /* single-bit mode */
177 for (i = 0; i < tms_count; i++)
179 tms = (tms_scan >> i) & 1;
180 gw16012_data(tms << 1); /* output next TMS bit */
183 tap_set_state(tap_get_end_state());
186 static void gw16012_path_move(struct pathmove_command *cmd)
188 int num_states = cmd->num_states;
189 int state_count;
191 state_count = 0;
192 while (num_states)
194 gw16012_control(0x0); /* single-bit mode */
195 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
197 gw16012_data(0x0); /* TCK cycle with TMS low */
199 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
201 gw16012_data(0x2); /* TCK cycle with TMS high */
203 else
205 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
206 exit(-1);
209 tap_set_state(cmd->path[state_count]);
210 state_count++;
211 num_states--;
214 tap_set_end_state(tap_get_state());
217 static void gw16012_runtest(int num_cycles)
219 tap_state_t saved_end_state = tap_get_end_state();
220 int i;
222 /* only do a state_move when we're not already in IDLE */
223 if (tap_get_state() != TAP_IDLE)
225 gw16012_end_state(TAP_IDLE);
226 gw16012_state_move();
229 for (i = 0; i < num_cycles; i++)
231 gw16012_control(0x0); /* single-bit mode */
232 gw16012_data(0x0); /* TMS cycle with TMS low */
235 gw16012_end_state(saved_end_state);
236 if (tap_get_state() != tap_get_end_state())
237 gw16012_state_move();
240 static void gw16012_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
242 int bits_left = scan_size;
243 int bit_count = 0;
244 tap_state_t saved_end_state = tap_get_end_state();
245 uint8_t scan_out, scan_in;
247 /* only if we're not already in the correct Shift state */
248 if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) || (ir_scan && (tap_get_state() == TAP_IRSHIFT))))
250 if (ir_scan)
251 gw16012_end_state(TAP_IRSHIFT);
252 else
253 gw16012_end_state(TAP_DRSHIFT);
255 gw16012_state_move();
256 gw16012_end_state(saved_end_state);
259 while (type == SCAN_OUT && ((bits_left - 1) > 7))
261 gw16012_control(0x2); /* seven-bit mode */
262 scan_out = buf_get_u32(buffer, bit_count, 7);
263 gw16012_data(scan_out);
264 bit_count += 7;
265 bits_left -= 7;
268 gw16012_control(0x0); /* single-bit mode */
269 while (bits_left-- > 0)
271 uint8_t tms = 0;
273 scan_out = buf_get_u32(buffer, bit_count, 1);
275 if (bits_left == 0) /* last bit */
277 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
278 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
280 tms = 0;
282 else
284 tms = 2;
288 gw16012_data(scan_out | tms);
290 if (type != SCAN_OUT)
292 gw16012_input(&scan_in);
293 buf_set_u32(buffer, bit_count, 1, ((scan_in & 0x08) >> 3));
296 bit_count++;
299 if (!((ir_scan && (tap_get_end_state() == TAP_IRSHIFT)) ||
300 (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))))
302 gw16012_data(0x0);
303 if (ir_scan)
304 tap_set_state(TAP_IRPAUSE);
305 else
306 tap_set_state(TAP_DRPAUSE);
308 if (tap_get_state() != tap_get_end_state())
309 gw16012_state_move();
313 static int gw16012_execute_queue(void)
315 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
316 int scan_size;
317 enum scan_type type;
318 uint8_t *buffer;
319 int retval;
321 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
322 * that wasn't handled by a caller-provided error handler
324 retval = ERROR_OK;
326 while (cmd)
328 switch (cmd->type)
330 case JTAG_RESET:
331 #ifdef _DEBUG_JTAG_IO_
332 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
333 #endif
334 if (cmd->cmd.reset->trst == 1)
336 tap_set_state(TAP_RESET);
338 gw16012_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
339 break;
340 case JTAG_RUNTEST:
341 #ifdef _DEBUG_JTAG_IO_
342 LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
343 #endif
344 gw16012_end_state(cmd->cmd.runtest->end_state);
345 gw16012_runtest(cmd->cmd.runtest->num_cycles);
346 break;
347 case JTAG_TLR_RESET:
348 #ifdef _DEBUG_JTAG_IO_
349 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
350 #endif
351 gw16012_end_state(cmd->cmd.statemove->end_state);
352 gw16012_state_move();
353 break;
354 case JTAG_PATHMOVE:
355 #ifdef _DEBUG_JTAG_IO_
356 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
357 #endif
358 gw16012_path_move(cmd->cmd.pathmove);
359 break;
360 case JTAG_SCAN:
361 gw16012_end_state(cmd->cmd.scan->end_state);
362 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
363 type = jtag_scan_type(cmd->cmd.scan);
364 #ifdef _DEBUG_JTAG_IO_
365 LOG_DEBUG("%s scan (%i) %i bit end in %i", (cmd->cmd.scan->ir_scan) ? "ir" : "dr",
366 type, scan_size, cmd->cmd.scan->end_state);
367 #endif
368 gw16012_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
369 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
370 retval = ERROR_JTAG_QUEUE_FAILED;
371 if (buffer)
372 free(buffer);
373 break;
374 case JTAG_SLEEP:
375 #ifdef _DEBUG_JTAG_IO_
376 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
377 #endif
378 jtag_sleep(cmd->cmd.sleep->us);
379 break;
380 default:
381 LOG_ERROR("BUG: unknown JTAG command type encountered");
382 exit(-1);
384 cmd = cmd->next;
387 return retval;
390 #if PARPORT_USE_GIVEIO == 1
391 static int gw16012_get_giveio_access(void)
393 HANDLE h;
394 OSVERSIONINFO version;
396 version.dwOSVersionInfoSize = sizeof version;
397 if (!GetVersionEx(&version)) {
398 errno = EINVAL;
399 return -1;
401 if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
402 return 0;
404 h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
405 if (h == INVALID_HANDLE_VALUE) {
406 errno = ENODEV;
407 return -1;
410 CloseHandle(h);
412 return 0;
414 #endif
416 #if PARPORT_USE_PPDEV == 1
418 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
420 #define GW16012_PPDEV_NAME "ppi"
422 static int gw16012_init_ioctls(void)
424 int temp = 0;
425 temp = ioctl(device_handle, PPCLAIM);
426 if (temp < 0)
428 LOG_ERROR("cannot claim device");
429 return ERROR_JTAG_INIT_FAILED;
432 temp = PARPORT_MODE_COMPAT;
433 temp = ioctl(device_handle, PPSETMODE, &temp);
434 if (temp < 0)
436 LOG_ERROR(" cannot set compatible mode to device");
437 return ERROR_JTAG_INIT_FAILED;
440 temp = IEEE1284_MODE_COMPAT;
441 temp = ioctl(device_handle, PPNEGOT, &temp);
442 if (temp < 0)
444 LOG_ERROR("cannot set compatible 1284 mode to device");
445 return ERROR_JTAG_INIT_FAILED;
447 return ERROR_OK;
449 #else
451 #define GW16012_PPDEV_NAME "parport"
453 static int gw16012_init_ioctls(void)
455 return ERROR_OK;
458 #endif // defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
460 static int gw16012_init_device(void)
462 const char *device_name = GW16012_PPDEV_NAME;
463 char buffer[256];
465 if (device_handle > 0)
467 LOG_ERROR("device is already opened");
468 return ERROR_JTAG_INIT_FAILED;
471 snprintf(buffer, 256, "/dev/%s%d", device_name, gw16012_port);
472 LOG_DEBUG("opening %s...", buffer);
474 device_handle = open(buffer, O_WRONLY);
475 if (device_handle < 0)
477 LOG_ERROR("cannot open device. check it exists and that user read and write rights are set");
478 return ERROR_JTAG_INIT_FAILED;
481 LOG_DEBUG("...open");
483 if (gw16012_init_ioctls() != ERROR_OK)
484 return ERROR_JTAG_INIT_FAILED;
486 return ERROR_OK;
489 #else // PARPORT_USE_PPDEV
491 static int gw16012_init_device(void)
493 if (gw16012_port == 0)
495 gw16012_port = 0x378;
496 LOG_WARNING("No gw16012 port specified, using default '0x378' (LPT1)");
499 LOG_DEBUG("requesting privileges for parallel port 0x%lx...", (long unsigned)(gw16012_port));
500 #if PARPORT_USE_GIVEIO == 1
501 if (gw16012_get_giveio_access() != 0)
502 #else /* PARPORT_USE_GIVEIO */
503 if (ioperm(gw16012_port, 3, 1) != 0)
504 #endif /* PARPORT_USE_GIVEIO */
506 LOG_ERROR("missing privileges for direct i/o");
507 return ERROR_JTAG_INIT_FAILED;
509 LOG_DEBUG("...privileges granted");
511 /* make sure parallel port is in right mode (clear tristate and interrupt */
512 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
513 outb(gw16012_port + 2, 0x0);
514 #else
515 outb(0x0, gw16012_port + 2);
516 #endif
517 return ERROR_OK;
520 #endif // PARPORT_USE_PPDEV
522 static int gw16012_init(void)
524 uint8_t status_port;
526 if (gw16012_init_device() != ERROR_OK)
527 return ERROR_JTAG_INIT_FAILED;
529 gw16012_input(&status_port);
530 gw16012_msb = (status_port & 0x80) ^ 0x80;
532 gw16012_speed(jtag_get_speed());
533 gw16012_reset(0, 0);
535 return ERROR_OK;
538 static int gw16012_quit(void)
541 return ERROR_OK;
544 COMMAND_HANDLER(gw16012_handle_parport_port_command)
546 if (CMD_ARGC == 1)
548 /* only if the port wasn't overwritten by cmdline */
549 if (gw16012_port == 0)
551 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], gw16012_port);
553 else
555 LOG_ERROR("The parport port was already configured!");
556 return ERROR_FAIL;
560 command_print(CMD_CTX, "parport port = %u", gw16012_port);
562 return ERROR_OK;
565 static const struct command_registration gw16012_command_handlers[] = {
567 .name = "parport_port",
568 .handler = gw16012_handle_parport_port_command,
569 .mode = COMMAND_CONFIG,
570 .help = "Display the address of the I/O port (e.g. 0x378) "
571 "or the number of the '/dev/parport' device used. "
572 "If a parameter is provided, first change that port.",
573 .usage = "[port_number]",
575 COMMAND_REGISTRATION_DONE
578 struct jtag_interface gw16012_interface = {
579 .name = "gw16012",
580 .commands = gw16012_command_handlers,
582 .init = gw16012_init,
583 .quit = gw16012_quit,
584 .speed = gw16012_speed,
585 .execute_queue = gw16012_execute_queue,