]> git.sur5r.net Git - openocd/blobdiff - src/jtag/amt_jtagaccel.c
fix from Pavel Chromy
[openocd] / src / jtag / amt_jtagaccel.c
index 42f8bc36168a5b2458dc1c9be91189945cc5350f..de3a6d33a6f59ec61b0f34a0c5605e3fdb478a16 100644 (file)
  *   Free Software Foundation, Inc.,                                       *
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
+#ifdef HAVE_CONFIG_H
 #include "config.h"
-#include "log.h"
+#endif
+
+#include "replacements.h"
+
 #include "jtag.h"
 
 /* system includes */
-#include <sys/io.h>
+
+#ifdef _WIN32
+#include "errno.h"
+#endif /* _WIN32 */
+
 #include <string.h>
 #include <stdlib.h>
 
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <unistd.h>
+#else /* not PARPORT_USE_PPDEV */
+#ifndef _WIN32
+#include <sys/io.h>
+#endif
+#endif
+
+#if PARPORT_USE_GIVEIO == 1
+#if IS_CYGWIN == 1
+#include <windows.h>
+#include <errno.h>
+#undef ERROR
+#endif
 #endif
 
+#include "log.h"
+
 /* configuration */
-unsigned long amt_jtagaccel_port;
+u16 amt_jtagaccel_port;
 
 /* interface variables
  */
@@ -97,8 +119,6 @@ jtag_interface_t amt_jtagaccel_interface =
        
        .execute_queue = amt_jtagaccel_execute_queue,
 
-       .support_statemove = 0,
-
        .speed = amt_jtagaccel_speed,   
        .register_commands = amt_jtagaccel_register_commands,
        .init = amt_jtagaccel_init,
@@ -161,7 +181,7 @@ void amt_wait_scan_busy()
        
        if (ar_status & 0x80)
        {
-               ERROR("amt_jtagaccel timed out while waiting for end of scan, rtck was %s", (rtck_enabled) ? "enabled" : "disabled");
+               ERROR("amt_jtagaccel timed out while waiting for end of scan, rtck was %s, last AR_STATUS: 0x%2.2x", (rtck_enabled) ? "enabled" : "disabled", ar_status);
                exit(-1);
        }
 }
@@ -311,6 +331,12 @@ int amt_jtagaccel_execute_queue(void)
        int scan_size;
        enum scan_type type;
        u8 *buffer;
+       int retval;
+       
+       /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
+        * that wasn't handled by a caller-provided error handler
+        */ 
+       retval = ERROR_OK;
                
        while (cmd)
        {
@@ -359,13 +385,13 @@ int amt_jtagaccel_execute_queue(void)
                                type = jtag_scan_type(cmd->cmd.scan);
                                amt_jtagaccel_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
                                if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
-                                       return ERROR_JTAG_QUEUE_FAILED;
+                                       retval = ERROR_JTAG_QUEUE_FAILED;
                                if (buffer)
                                        free(buffer);
                                break;
                        case JTAG_SLEEP:
 #ifdef _DEBUG_JTAG_IO_
-                               DEBUG("sleep", cmd->cmd.sleep->us);
+                               DEBUG("sleep %i", cmd->cmd.sleep->us);
 #endif
                                jtag_sleep(cmd->cmd.sleep->us);
                                break;
@@ -376,8 +402,34 @@ int amt_jtagaccel_execute_queue(void)
                cmd = cmd->next;
        }
        
-       return ERROR_OK;
+       return retval;
+}
+
+#if PARPORT_USE_GIVEIO == 1
+int amt_jtagaccel_get_giveio_access()
+{
+    HANDLE h;
+    OSVERSIONINFO version;
+
+    version.dwOSVersionInfoSize = sizeof version;
+    if (!GetVersionEx( &version )) {
+        errno = EINVAL;
+        return -1;
+    }
+    if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
+        return 0;
+
+    h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
+    if (h == INVALID_HANDLE_VALUE) {
+        errno = ENODEV;
+        return -1;
+    }
+
+    CloseHandle( h );
+
+    return 0;
 }
+#endif
 
 int amt_jtagaccel_init(void)
 {
@@ -388,7 +440,8 @@ int amt_jtagaccel_init(void)
 #else
        u8 status_port;
 #endif
-
+       u8 ar_status;
+       
 #if PARPORT_USE_PPDEV == 1
        if (device_handle > 0)
        {
@@ -432,8 +485,12 @@ int amt_jtagaccel_init(void)
                amt_jtagaccel_port = 0x378;
                WARNING("No parport port specified, using default '0x378' (LPT1)");
        }
-       
+
+#if PARPORT_USE_GIVEIO == 1
+       if (amt_jtagaccel_get_giveio_access() != 0) {
+#else /* PARPORT_USE_GIVEIO */ 
        if (ioperm(amt_jtagaccel_port, 5, 1) != 0) {
+#endif /* PARPORT_USE_GIVEIO */
                ERROR("missing privileges for direct i/o");
                return ERROR_JTAG_INIT_FAILED;
        }
@@ -448,6 +505,12 @@ int amt_jtagaccel_init(void)
        outb(0x04, amt_jtagaccel_port + 2);
 #endif
        
+       if (rtck_enabled)
+       {       
+               /* set RTCK enable bit */
+               aw_control_fsm |= 0x02;
+       }
+       
        /* enable JTAG port */
        aw_control_fsm |= 0x04;
        AMT_AW(aw_control_fsm);
@@ -466,6 +529,10 @@ int amt_jtagaccel_init(void)
        
        amt_jtagaccel_reset(0, 0);
        
+       /* read status register */
+       AMT_AR(ar_status);
+       DEBUG("AR_STATUS: 0x%2.2x", ar_status);
+       
        return ERROR_OK;
 }
 
@@ -499,10 +566,10 @@ int amt_jtagaccel_handle_rtck_command(struct command_context_s *cmd_ctx, char *c
                if (strcmp(args[0], "enabled") == 0)
                {
                        rtck_enabled = 1;
-                       
-                       /* set RTCK enable bit */
-                       aw_control_fsm |= 0x02;
-                       AMT_AW(aw_control_fsm);
+               }
+               else
+               {
+                       rtck_enabled = 0;
                }
        }