Pavel Chromy: the attached patch refines PRESTO support and makes it work with libftdi.
[fw/openocd] / src / jtag / jtag.c
index 974328c52833d7b651dd542897287a053a6bf333..48cd0595646eafdebcb545da92105388f7079464 100644 (file)
 #include "command.h"
 #include "log.h"
 #include "interpreter.h"
+#include "target.h"
 
 #include "stdlib.h"
 #include "string.h"
 #include <unistd.h>
 
+
+
+/* note that this is not marked as static as it must be available from outside jtag.c for those 
+   that implement the jtag_xxx() minidriver layer 
+*/
+int jtag_error=ERROR_OK; 
+
+
 char* tap_state_strings[16] =
 {
        "tlr", 
@@ -204,8 +213,8 @@ jtag_interface_t *jtag_interfaces[] = {
 jtag_interface_t *jtag = NULL;
 
 /* configuration */
-char* jtag_interface = NULL;
-int jtag_speed = -1;
+jtag_interface_t *jtag_interface = NULL;
+int jtag_speed = 0;
 
 
 /* forward declarations */
@@ -379,19 +388,40 @@ void cmd_queue_free()
        cmd_queue_pages = NULL;
 }
 
-int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state, void *dummy_anachronism)
+int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
 {
-       jtag_command_t **last_cmd;
-       jtag_device_t *device;
-       int i, j;
-       int scan_size = 0;
-
        if (jtag_trst == 1)
        {
                WARNING("JTAG command queued, while TRST is low (TAP in reset)");
+               jtag_error=ERROR_JTAG_TRST_ASSERTED;
                return ERROR_JTAG_TRST_ASSERTED;
        }
 
+       if (state != -1)
+               cmd_queue_end_state = state;
+
+       if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
+               jtag_call_event_callbacks(JTAG_TRST_RELEASED);
+       
+       if (cmd_queue_end_state == TAP_TLR)
+               jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
+       
+       cmd_queue_cur_state = cmd_queue_end_state;
+       
+       int retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
+       if (retval!=ERROR_OK)
+               jtag_error=retval;
+       return retval;
+}
+
+int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
+{      
+       jtag_command_t **last_cmd;
+       jtag_device_t *device;
+       int i, j;
+       int scan_size = 0;
+
+
        last_cmd = jtag_get_last_command_p();
        
        /* allocate memory for a new list member */
@@ -406,18 +436,7 @@ int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state,
        (*last_cmd)->cmd.scan->num_fields = jtag_num_devices;   /* one field per device */
        (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
        (*last_cmd)->cmd.scan->end_state = state;
-               
-       if (state != -1)
-               cmd_queue_end_state = state;
 
-       if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
-               jtag_call_event_callbacks(JTAG_TRST_RELEASED);
-       
-       if (cmd_queue_end_state == TAP_TLR)
-               jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
-       
-       cmd_queue_cur_state = cmd_queue_end_state;
-               
        for (i = 0; i < jtag_num_devices; i++)
        {
                int found = 0;
@@ -472,17 +491,33 @@ int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state,
        return ERROR_OK;
 }
 
-int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state, void *dummy_anachronism)
+int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
 {
-       jtag_command_t **last_cmd;
-       int i;
-
        if (jtag_trst == 1)
        {
                WARNING("JTAG command queued, while TRST is low (TAP in reset)");
-               return ERROR_JTAG_TRST_ASSERTED;
+               return jtag_error=ERROR_JTAG_TRST_ASSERTED;
        }
 
+       if (state != -1)
+               cmd_queue_end_state = state;
+
+       if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
+               jtag_call_event_callbacks(JTAG_TRST_RELEASED);
+       
+       if (cmd_queue_end_state == TAP_TLR)
+               jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
+               
+       cmd_queue_cur_state = cmd_queue_end_state;
+       
+       return interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
+}
+
+int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
+{
+       int i;
+       jtag_command_t **last_cmd;
+       
        last_cmd = jtag_get_last_command_p();
        
        /* allocate memory for a new list member */
@@ -498,17 +533,6 @@ int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state
        (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
        (*last_cmd)->cmd.scan->end_state = state;
 
-       if (state != -1)
-               cmd_queue_end_state = state;
-
-       if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
-               jtag_call_event_callbacks(JTAG_TRST_RELEASED);
-       
-       if (cmd_queue_end_state == TAP_TLR)
-               jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
-               
-       cmd_queue_cur_state = cmd_queue_end_state;
-       
        for (i = 0; i < num_fields; i++)
        {
                int num_bits = fields[i].num_bits;
@@ -526,20 +550,37 @@ int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state
        return ERROR_OK;
 }
 
-int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state, void *dummy_anachronism)
+int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
+{
+       if (jtag_trst == 1)
+       {
+               WARNING("JTAG command queued, while TRST is low (TAP in reset)");
+               return jtag_error=ERROR_JTAG_TRST_ASSERTED;
+       }
+
+       if (state != -1)
+               cmd_queue_end_state = state;
+
+       if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
+               jtag_call_event_callbacks(JTAG_TRST_RELEASED);
+       
+       if (cmd_queue_end_state == TAP_TLR)
+               jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
+                       
+       cmd_queue_cur_state = cmd_queue_end_state;
+
+       return interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
+}
+
+int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
 {
        int i, j;
        int bypass_devices = 0;
        int field_count = 0;
-       jtag_command_t **last_cmd = jtag_get_last_command_p();
-       jtag_device_t *device = jtag_devices;
        int scan_size;
 
-       if (jtag_trst == 1)
-       {
-               WARNING("JTAG command queued, while TRST is low (TAP in reset)");
-               return ERROR_JTAG_TRST_ASSERTED;
-       }
+       jtag_command_t **last_cmd = jtag_get_last_command_p();
+       jtag_device_t *device = jtag_devices;
 
        /* count devices in bypass */
        while (device)
@@ -562,17 +603,6 @@ int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state,
        (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
        (*last_cmd)->cmd.scan->end_state = state;
        
-       if (state != -1)
-               cmd_queue_end_state = state;
-
-       if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
-               jtag_call_event_callbacks(JTAG_TRST_RELEASED);
-       
-       if (cmd_queue_end_state == TAP_TLR)
-               jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
-                       
-       cmd_queue_cur_state = cmd_queue_end_state;
-       
        for (i = 0; i < jtag_num_devices; i++)
        {
                int found = 0;
@@ -596,13 +626,14 @@ int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state,
                }
                if (!found)
                {
+#ifdef _DEBUG_JTAG_IO_
                        /* if a device isn't listed, the BYPASS register should be selected */
                        if (!jtag_get_device(i)->bypass)
                        {
                                ERROR("BUG: no scan data for a device not in BYPASS");
                                exit(-1);
                        }
-       
+#endif 
                        /* program the scan field to 1 bit length, and ignore it's value */
                        (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
                        (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
@@ -615,40 +646,116 @@ int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state,
                }
                else
                {
+#ifdef _DEBUG_JTAG_IO_
                        /* if a device is listed, the BYPASS register must not be selected */
                        if (jtag_get_device(i)->bypass)
                        {
-                               WARNING("scan data for a device in BYPASS");
+                               ERROR("BUG: scan data for a device in BYPASS");
+                               exit(-1);
                        }
+#endif
                }
        }
        return ERROR_OK;
 }
 
-int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state, void *dummy_anachronism)
+void MINIDRIVER(interface_jtag_add_dr_out)(int device_num, 
+               int num_fields,
+               int *num_bits,
+               u32 *value,
+               enum tap_state end_state)
 {
        int i;
+       int field_count = 0;
+       int scan_size;
+       int bypass_devices = 0;
+
        jtag_command_t **last_cmd = jtag_get_last_command_p();
-       
-       if (jtag_trst == 1)
+       jtag_device_t *device = jtag_devices;
+       /* count devices in bypass */
+       while (device)
        {
-               WARNING("JTAG command queued, while TRST is low (TAP in reset)");
-               return ERROR_JTAG_TRST_ASSERTED;
+               if (device->bypass)
+                       bypass_devices++;
+               device = device->next;
        }
-
+       
        /* allocate memory for a new list member */
        *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
        last_comand_pointer = &((*last_cmd)->next);
        (*last_cmd)->next = NULL;
        (*last_cmd)->type = JTAG_SCAN;
 
-       /* allocate memory for scan command */
+       /* allocate memory for dr scan command */
        (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
        (*last_cmd)->cmd.scan->ir_scan = 0;
-       (*last_cmd)->cmd.scan->num_fields = num_fields;
-       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
-       (*last_cmd)->cmd.scan->end_state = state;
-               
+       (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
+       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
+       (*last_cmd)->cmd.scan->end_state = end_state;
+
+       for (i = 0; i < jtag_num_devices; i++)
+       {
+               (*last_cmd)->cmd.scan->fields[field_count].device = i;
+
+               if (i == device_num)
+               {
+                       int j;
+#ifdef _DEBUG_JTAG_IO_
+                       /* if a device is listed, the BYPASS register must not be selected */
+                       if (jtag_get_device(i)->bypass)
+                       {
+                               ERROR("BUG: scan data for a device in BYPASS");
+                               exit(-1);
+                       }
+#endif
+                       for (j = 0; j < num_fields; j++)
+                       {
+                               u8 out_value[4];
+                               scan_size = num_bits[j];
+                               buf_set_u32(out_value, 0, scan_size, value[j]);
+                               (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
+                               (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
+                               (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
+                               (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
+                               (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
+                               (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
+                               (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
+                               (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
+                       }
+               } else
+               {
+#ifdef _DEBUG_JTAG_IO_
+                       /* if a device isn't listed, the BYPASS register should be selected */
+                       if (!jtag_get_device(i)->bypass)
+                       {
+                               ERROR("BUG: no scan data for a device not in BYPASS");
+                               exit(-1);
+                       }
+#endif 
+                       /* program the scan field to 1 bit length, and ignore it's value */
+                       (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
+                       (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
+                       (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
+                       (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
+                       (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
+                       (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
+                       (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
+                       (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
+               }
+       }
+}
+
+
+
+
+int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
+{
+       if (jtag_trst == 1)
+       {
+               WARNING("JTAG command queued, while TRST is low (TAP in reset)");
+               return jtag_error=ERROR_JTAG_TRST_ASSERTED;
+       }
+
        if (state != -1)
                cmd_queue_end_state = state;
 
@@ -659,6 +766,27 @@ int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state
                jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
                        
        cmd_queue_cur_state = cmd_queue_end_state;
+
+       return interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
+}
+
+int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
+{
+       int i;
+       jtag_command_t **last_cmd = jtag_get_last_command_p();
+       
+       /* allocate memory for a new list member */
+       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       last_comand_pointer = &((*last_cmd)->next);
+       (*last_cmd)->next = NULL;
+       (*last_cmd)->type = JTAG_SCAN;
+
+       /* allocate memory for scan command */
+       (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
+       (*last_cmd)->cmd.scan->ir_scan = 0;
+       (*last_cmd)->cmd.scan->num_fields = num_fields;
+       (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
+       (*last_cmd)->cmd.scan->end_state = state;
        
        for (i = 0; i < num_fields; i++)
        {
@@ -679,23 +807,12 @@ int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state
 }
 int jtag_add_statemove(enum tap_state state)
 {
-       jtag_command_t **last_cmd = jtag_get_last_command_p();
-       
        if (jtag_trst == 1)
        {
                WARNING("JTAG command queued, while TRST is low (TAP in reset)");
-               return ERROR_JTAG_TRST_ASSERTED;
+               return jtag_error=ERROR_JTAG_TRST_ASSERTED;
        }
 
-       /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
-       last_comand_pointer = &((*last_cmd)->next);
-       (*last_cmd)->next = NULL;
-       (*last_cmd)->type = JTAG_STATEMOVE;
-
-       (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
-       (*last_cmd)->cmd.statemove->end_state = state;
-       
        if (state != -1)
                cmd_queue_end_state = state;
 
@@ -706,28 +823,73 @@ int jtag_add_statemove(enum tap_state state)
                jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
                        
        cmd_queue_cur_state = cmd_queue_end_state;
+
+       return interface_jtag_add_statemove(cmd_queue_end_state);
+}
+
+int MINIDRIVER(interface_jtag_add_statemove)(enum tap_state state)
+{
+       jtag_command_t **last_cmd = jtag_get_last_command_p();
+       
+       /* allocate memory for a new list member */
+       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       last_comand_pointer = &((*last_cmd)->next);
+       (*last_cmd)->next = NULL;
+       (*last_cmd)->type = JTAG_STATEMOVE;
+
+       (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
+       (*last_cmd)->cmd.statemove->end_state = state;
+       
        
        return ERROR_OK;
 }
 
 int jtag_add_pathmove(int num_states, enum tap_state *path)
 {
-       jtag_command_t **last_cmd = jtag_get_last_command_p();
-       int i;
-       
        if (jtag_trst == 1)
        {
                WARNING("JTAG command queued, while TRST is low (TAP in reset)");
-               return ERROR_JTAG_TRST_ASSERTED;
+               return jtag_error=ERROR_JTAG_TRST_ASSERTED;
        }
        
        /* the last state has to be a stable state */
        if (tap_move_map[path[num_states - 1]] == -1)
        {
                ERROR("TAP path doesn't finish in a stable state");
-               return ERROR_JTAG_NOT_IMPLEMENTED;
+               return jtag_error=ERROR_JTAG_NOT_IMPLEMENTED;
+       }
+       
+       if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
+               jtag_call_event_callbacks(JTAG_TRST_RELEASED);
+       
+       if (cmd_queue_end_state == TAP_TLR)
+               jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
+       
+
+       enum tap_state cur_state=cmd_queue_cur_state;
+       int i;
+       for (i=0; i<num_states; i++)
+       {
+               if ((tap_transitions[cur_state].low != path[i])&&
+                               (tap_transitions[cur_state].high != path[i]))
+               {
+                       ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
+                       exit(-1);
+               }
+               cur_state = path[i];
        }
        
+       cmd_queue_cur_state = path[num_states - 1];
+
+       return interface_jtag_add_pathmove(num_states, path);
+}
+
+
+int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
+{
+       jtag_command_t **last_cmd = jtag_get_last_command_p();
+       int i;
+       
        /* allocate memory for a new list member */
        *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
        last_comand_pointer = &((*last_cmd)->next);
@@ -740,28 +902,14 @@ int jtag_add_pathmove(int num_states, enum tap_state *path)
        
        for (i = 0; i < num_states; i++)
                (*last_cmd)->cmd.pathmove->path[i] = path[i];
-
-       if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
-               jtag_call_event_callbacks(JTAG_TRST_RELEASED);
-       
-       if (cmd_queue_end_state == TAP_TLR)
-               jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
-       
-       cmd_queue_cur_state = path[num_states - 1];
        
        return ERROR_OK;
 }
 
-int jtag_add_runtest(int num_cycles, enum tap_state state)
+int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
 {
        jtag_command_t **last_cmd = jtag_get_last_command_p();
        
-       if (jtag_trst == 1)
-       {
-               WARNING("JTAG command queued, while TRST is low (TAP in reset)");
-               return ERROR_JTAG_TRST_ASSERTED;
-       }
-
        /* allocate memory for a new list member */
        *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
        (*last_cmd)->next = NULL;
@@ -772,6 +920,18 @@ int jtag_add_runtest(int num_cycles, enum tap_state state)
        (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
        (*last_cmd)->cmd.runtest->end_state = state;
        
+       return ERROR_OK;
+}
+
+int jtag_add_runtest(int num_cycles, enum tap_state state)
+{
+       if (jtag_trst == 1)
+       {
+               jtag_error=ERROR_JTAG_QUEUE_FAILED;
+               WARNING("JTAG command queued, while TRST is low (TAP in reset)");
+               return ERROR_JTAG_TRST_ASSERTED;
+       }
+       
        if (state != -1)
                cmd_queue_end_state = state;
 
@@ -783,14 +943,14 @@ int jtag_add_runtest(int num_cycles, enum tap_state state)
                        
        cmd_queue_cur_state = cmd_queue_end_state;
        
-       return ERROR_OK;
+       /* executed by sw or hw fifo */
+       return interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
 }
 
 int jtag_add_reset(int req_trst, int req_srst)
 {
        int trst_with_tms = 0;
-       
-       jtag_command_t **last_cmd = jtag_get_last_command_p();
+       int retval;
        
        if (req_trst == -1)
                req_trst = jtag_trst;
@@ -801,7 +961,9 @@ int jtag_add_reset(int req_trst, int req_srst)
        /* Make sure that jtag_reset_config allows the requested reset */
        /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
        if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
-               return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
+       {
+               return jtag_error=ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
+       }
                
        /* if TRST pulls SRST, we reset with TAP T-L-R */
        if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
@@ -813,7 +975,7 @@ int jtag_add_reset(int req_trst, int req_srst)
        if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
        {
                ERROR("requested nSRST assertion, but the current configuration doesn't support this");
-               return ERROR_JTAG_RESET_CANT_SRST;
+               return jtag_error=ERROR_JTAG_RESET_CANT_SRST;
        }
        
        if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
@@ -821,20 +983,17 @@ int jtag_add_reset(int req_trst, int req_srst)
                req_trst = 0;
                trst_with_tms = 1;
        }
-       
-       /* allocate memory for a new list member */
-       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
-       (*last_cmd)->next = NULL;
-       last_comand_pointer = &((*last_cmd)->next);
-       (*last_cmd)->type = JTAG_RESET;
-
-       (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
-       (*last_cmd)->cmd.reset->trst = req_trst;
-       (*last_cmd)->cmd.reset->srst = req_srst;
 
        jtag_trst = req_trst;
        jtag_srst = req_srst;
 
+       retval = interface_jtag_add_reset(req_trst, req_srst);
+       if (retval!=ERROR_OK)
+       {
+               jtag_error=retval;
+               return retval;
+       }
+
        if (jtag_srst)
        {
                jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
@@ -848,47 +1007,53 @@ int jtag_add_reset(int req_trst, int req_srst)
        
        if (trst_with_tms)
        {
-               last_cmd = &((*last_cmd)->next);
-               
-               /* allocate memory for a new list member */
-               *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
-               (*last_cmd)->next = NULL;
-               last_comand_pointer = &((*last_cmd)->next);
-               (*last_cmd)->type = JTAG_STATEMOVE;
-
-               (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
-               (*last_cmd)->cmd.statemove->end_state = TAP_TLR;
-               
                jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
-               cmd_queue_cur_state = TAP_TLR;
-               cmd_queue_end_state = TAP_TLR;
+               jtag_add_end_state(TAP_TLR);
+               jtag_add_statemove(TAP_TLR);
                
                return ERROR_OK;
        }
+       
+       if (jtag_trst)
+       {
+               /* we just asserted nTRST, so we're now in Test-Logic-Reset,
+                * and inform possible listeners about this
+                */
+               cmd_queue_cur_state = TAP_TLR;
+               jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
+       }
        else
        {
-               if (jtag_trst)
-               {
-                       /* we just asserted nTRST, so we're now in Test-Logic-Reset,
-                        * and inform possible listeners about this
-                        */
-                       cmd_queue_cur_state = TAP_TLR;
-                       jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
-               }
-               else
-               {
-                       /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
-                        * but we might want to add a delay to give the TAP time to settle
-                        */
-                       if (jtag_ntrst_delay)
-                               jtag_add_sleep(jtag_ntrst_delay * 1000);
-               }
+               /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
+                * but we might want to add a delay to give the TAP time to settle
+                */
+               if (jtag_ntrst_delay)
+                       jtag_add_sleep(jtag_ntrst_delay * 1000);
        }
+       
+       return retval;
+       
+}
+
+int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
+{
+       jtag_command_t **last_cmd = jtag_get_last_command_p();
 
+       /* allocate memory for a new list member */
+       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       (*last_cmd)->next = NULL;
+       last_comand_pointer = &((*last_cmd)->next);
+       (*last_cmd)->type = JTAG_RESET;
+
+       (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
+       (*last_cmd)->cmd.reset->trst = req_trst;
+       (*last_cmd)->cmd.reset->srst = req_srst;
+
+       
        return ERROR_OK;
 }
 
-int jtag_add_end_state(enum tap_state state)
+int MINIDRIVER(interface_jtag_add_end_state)(enum tap_state state)
 {
        jtag_command_t **last_cmd = jtag_get_last_command_p();
        
@@ -901,13 +1066,18 @@ int jtag_add_end_state(enum tap_state state)
        (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
        (*last_cmd)->cmd.end_state->end_state = state;
 
+       return ERROR_OK;
+}
+
+int jtag_add_end_state(enum tap_state state)
+{
        if (state != -1)
                cmd_queue_end_state = state;
-       
-       return ERROR_OK;
+       int retval = interface_jtag_add_end_state(cmd_queue_end_state);
+       return retval;
 }
 
-int jtag_add_sleep(u32 us)
+int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
 {
        jtag_command_t **last_cmd = jtag_get_last_command_p();
        
@@ -923,6 +1093,11 @@ int jtag_add_sleep(u32 us)
        return ERROR_OK;
 }
 
+int jtag_add_sleep(u32 us)
+{
+       return interface_jtag_add_sleep(us); 
+}
+
 int jtag_scan_size(scan_command_t *cmd)
 {
        int bit_count = 0;
@@ -1106,7 +1281,7 @@ enum scan_type jtag_scan_type(scan_command_t *cmd)
        return type;
 }
 
-int jtag_execute_queue(void)
+int MINIDRIVER(interface_jtag_execute_queue)(void)
 {
        int retval;
 
@@ -1120,6 +1295,17 @@ int jtag_execute_queue(void)
        return retval;
 }
 
+int jtag_execute_queue(void)
+{
+       int retval=interface_jtag_execute_queue();
+       if (retval==ERROR_OK)
+       {
+       retval=jtag_error;
+       }
+       jtag_error=ERROR_OK;
+       return retval;
+}
+
 int jtag_reset_callback(enum jtag_event event, void *priv)
 {
        jtag_device_t *device = priv;
@@ -1142,11 +1328,10 @@ void jtag_sleep(u32 us)
 
 /* Try to examine chain layout according to IEEE 1149.1 Â§12
  */
-int jtag_examine_chain()
+int jtag_examine_chain(u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4] )
 {
        jtag_device_t *device = jtag_devices;
        scan_field_t field;
-       u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
        int i;
        int bit_count;
        int device_count = 0;
@@ -1168,7 +1353,7 @@ int jtag_examine_chain()
                buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
        }
        
-       jtag_add_plain_dr_scan(1, &field, TAP_TLR, NULL);
+       jtag_add_plain_dr_scan(1, &field, TAP_TLR);
        jtag_execute_queue();
        
        for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
@@ -1264,7 +1449,7 @@ int jtag_validate_chain()
        field.in_handler = NULL;
        field.in_handler_priv = NULL;
        
-       jtag_add_plain_ir_scan(1, &field, TAP_TLR, NULL);
+       jtag_add_plain_ir_scan(1, &field, TAP_TLR);
        jtag_execute_queue();
        
        device = jtag_devices;
@@ -1332,105 +1517,184 @@ int jtag_register_commands(struct command_context_s *cmd_ctx)
        return ERROR_OK;
 }
 
-int jtag_init(struct command_context_s *cmd_ctx)
+int jtag_interface_init(struct command_context_s *cmd_ctx)
 {
-       int i, validate_tries = 0;
+       if (!jtag_interface)
+       {
+               /* nothing was previously specified by "interface" command */
+               ERROR("JTAG interface has to be specified, see \"interface\" command");
+               return ERROR_JTAG_INVALID_INTERFACE;
+       }
+
+       if (jtag_interface->init() != ERROR_OK)
+               return ERROR_JTAG_INIT_FAILED;
+
+       jtag = jtag_interface;
+       return ERROR_OK;
+}
+
+extern int jtag_init_chain(struct command_context_s *cmd_ctx);
+
+static int jtag_sense_handler(void *priv)
+{
+       struct command_context_s *cmd_ctx;
+       cmd_ctx=(struct command_context_s *)priv;
+
+       static int scan_complete = 0;
+       if (!scan_complete)
+       {
+               if (jtag_init_chain(cmd_ctx)==ERROR_OK)
+               {
+                       scan_complete = 1;
+               }
+               return ERROR_OK;
+       }
        
+       return ERROR_OK;
+}
+
+/* OpenOCD will start telnet/gdb servers before the JTAG chain has
+ * been enumerated. This is to allow e.g. GDB init script to
+ * run monitor commands to initialize the target so jtag_init_chain()
+ * will succeed.
+ * 
+ * A timer callback is added where sensing is retried once every second
+ * until it succeeds.
+ */
+int jtag_init(struct command_context_s *cmd_ctx)
+{
+       jtag_device_t *device;
+
        DEBUG("-");
+       
+       if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
+               return ERROR_JTAG_INIT_FAILED;
 
-       if (jtag_speed == -1)
-               jtag_speed = 0;
+       device = jtag_devices;
+       jtag_ir_scan_size = 0;
+       jtag_num_devices = 0;
+       while (device != NULL)
+       {
+               jtag_ir_scan_size += device->ir_length;
+               jtag_num_devices++;
+               device = device->next;
+       }
        
-       if (jtag_interface && (jtag_interface[0] != 0))
-               /* configuration var 'jtag_interface' is set, and not empty */
-               for (i = 0; jtag_interfaces[i]; i++)
-               {
-                       if (strcmp(jtag_interface, jtag_interfaces[i]->name) == 0)
-                       {
-                               jtag_device_t *device;
-                               device = jtag_devices;
+       jtag_add_statemove(TAP_TLR);
+       jtag_execute_queue();
+
+       target_register_timer_callback(jtag_sense_handler, 1000, 1, cmd_ctx);
        
-                               if (jtag_interfaces[i]->init() != ERROR_OK)
-                                       return ERROR_JTAG_INIT_FAILED;
-                               jtag = jtag_interfaces[i];
+       return ERROR_OK;
+       }
+       
+static int jtag_test_chain(u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4])
+{
+       jtag_add_statemove(TAP_TLR);
+       jtag_execute_queue();
 
-                               jtag_ir_scan_size = 0;
-                               jtag_num_devices = 0;
-                               while (device != NULL)
-                               {
-                                       jtag_ir_scan_size += device->ir_length;
-                                       jtag_num_devices++;
-                                       device = device->next;
-                               }
-                               
-                               jtag_add_statemove(TAP_TLR);
-                               jtag_execute_queue();
+       /* examine chain first, as this could discover the real chain layout */
+       if (jtag_examine_chain(idcode_buffer)!=ERROR_OK)
+       {
+               WARNING("trying to validate configured JTAG chain anyway...");
+       }
+       
+       return jtag_validate_chain();
+}
 
-                               /* examine chain first, as this could discover the real chain layout */
-                               if (jtag_examine_chain() != ERROR_OK)
+/* Unless we can do this successfully 10 times, we're not
+ * satisfied with the quality of the JTAG communication.
+ * 
+ * Since we're continously repeating this operation, be a bit
+ * wary of filling the log with megabytes of data.
+ * 
+ * Keep increasing the jtag_divisor until we've got a solid
+ * result.
+ */
+int jtag_init_chain(struct command_context_s *cmd_ctx)
+{
+       int i, j;
+       int retval;
+       for (i=jtag_speed; i<64; i++)
+               {
+               u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
+               jtag_speed=i;
+               if ((retval=jtag->speed(jtag_speed))!=ERROR_OK)
+                       continue;
+               for (j=0; j<10; j++)
+               {
+                       u8 idcode_current[JTAG_MAX_CHAIN_SIZE * 4];
+                       enum log_levels save_log_level=debug_level;
+                       /* avoid spamming log */
+                       debug_level=LOG_SILENT;
+                       retval=jtag_test_chain(idcode_current);
+                       if (retval==ERROR_OK)
+                       {
+                               if (j==0)
                                {
-                                       ERROR("trying to validate configured JTAG chain anyway...");
-                               }
-                               
-                               while (jtag_validate_chain() != ERROR_OK)
+                                       memcpy(idcode_buffer, idcode_current, sizeof(idcode_buffer));
+                               } else
                                {
-                                       validate_tries++;
-                                       if (validate_tries > 5)
-                                       {
-                                               ERROR("Could not validate JTAG chain, exit");
-                                               jtag = NULL;
-                                               return ERROR_JTAG_INVALID_INTERFACE;
-                                       }
-                                       usleep(10000);
+                                       retval=(memcmp(idcode_buffer, idcode_current, sizeof(idcode_buffer))==0)?ERROR_OK:ERROR_JTAG_INIT_FAILED;
                                }
-
-                               return ERROR_OK;
+                       }
+                       debug_level = save_log_level;
+                       if (retval!=ERROR_OK)
+                       {
+                               break;
                        }
                }
-       
-       /* no valid interface was found (i.e. the configuration option,
-        * didn't match one of the compiled-in interfaces
-        */
-       ERROR("No valid jtag interface found (%s)", jtag_interface);
-       ERROR("compiled-in jtag interfaces:");
-       for (i = 0; jtag_interfaces[i]; i++)
-       {
-               ERROR("%i: %s", i, jtag_interfaces[i]->name);
+               if (retval==ERROR_OK)
+               {
+                       /* Print out result  */
+                       INFO("Succeeded jtag chain test jtag_speed=%d", jtag_speed);
+                       return jtag_test_chain(idcode_buffer);
+               }
+               DEBUG("Failed jtag chain test, dropping clock rate. Trying jtag_speed=%d\n", i+1);
        }
-       
-       jtag = NULL;
-       return ERROR_JTAG_INVALID_INTERFACE;
+       return retval;
 }
 
 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
        int i;
-       
-       /* only if the configuration var isn't overwritten from cmdline */
-       if (!jtag_interface)
+
+       /* check whether the interface is already configured */
+       if (jtag_interface)
+       {
+               WARNING("Interface already configured, ignoring");
+               return ERROR_OK;
+       }
+
+       /* interface name is a mandatory argument */
+       if (argc < 1 || args[0][0] == '\0')
+       {
+               return ERROR_COMMAND_SYNTAX_ERROR;
+       }
+
+       for (i=0; jtag_interfaces[i]; i++)
        {
-               if (args[0] && (args[0][0] != 0))
+               if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
                {
-                       for (i=0; jtag_interfaces[i]; i++)
-                       {
-                               if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
-                               {
-                                       if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
-                                               exit(-1);
-                               
-                                       jtag_interface = jtag_interfaces[i]->name;
-               
-                                       return ERROR_OK;
-                               }
-                       }
+                       if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
+                               exit(-1);
+
+                       jtag_interface = jtag_interfaces[i];
+                       return ERROR_OK;
                }
-               
-               /* remember the requested interface name, so we can complain about it later */
-               jtag_interface = strdup(args[0]);
-               DEBUG("'interface' command didn't specify a valid interface");
        }
-       
-       return ERROR_OK;
+
+       /* no valid interface was found (i.e. the configuration option,
+        * didn't match one of the compiled-in interfaces
+        */
+       ERROR("No valid jtag interface found (%s)", args[0]);
+       ERROR("compiled-in jtag interfaces:");
+       for (i = 0; jtag_interfaces[i]; i++)
+       {
+               ERROR("%i: %s", i, jtag_interfaces[i]->name);
+       }
+
+       return ERROR_JTAG_INVALID_INTERFACE;
 }
 
 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
@@ -1593,12 +1857,11 @@ int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char
 
        if (argc > 0)
        {
+               jtag_speed = strtoul(args[0], NULL, 0);
                /* this command can be called during CONFIG, 
                 * in which case jtag isn't initialized */
                if (jtag)
-                       jtag->speed(strtoul(args[0], NULL, 0));
-               else
-                       jtag_speed = strtoul(args[0], NULL, 0);
+                       jtag->speed(jtag_speed);
        }
 
        return ERROR_OK;
@@ -1610,7 +1873,7 @@ int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char *
 
        if (argc < 1)
        {
-               command_print(cmd_ctx, "usage: endstate <tap_state>");
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
        else
        {
@@ -1632,13 +1895,12 @@ int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char
 {
        int trst = -1;
        int srst = -1;
-       char *usage = "usage: jtag_reset <trst> <srst>";
        int retval;
        
-       if (argc < 1)
+       if (argc < 2)
        {
-               command_print(cmd_ctx, usage);
-               return ERROR_OK;
+               return ERROR_COMMAND_SYNTAX_ERROR;
+
        }
 
        if (args[0][0] == '1')
@@ -1647,8 +1909,7 @@ int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char
                trst = 0;
        else
        {
-               command_print(cmd_ctx, usage);
-               return ERROR_OK;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
        if (args[1][0] == '1')
@@ -1657,10 +1918,12 @@ int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char
                srst = 0;
        else
        {
-               command_print(cmd_ctx, usage);
-               return ERROR_OK;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
+       if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
+               return ERROR_JTAG_INIT_FAILED;
+
        if ((retval = jtag_add_reset(trst, srst)) != ERROR_OK)
        {
                switch (retval)
@@ -1684,8 +1947,7 @@ int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **
 {
        if (argc < 1)
        {
-               command_print(cmd_ctx, "usage: runtest <num_cycles>");
-               return ERROR_OK;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
        jtag_add_runtest(strtol(args[0], NULL, 0), -1);
@@ -1725,8 +1987,7 @@ int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **a
        
        if ((argc < 2) || (argc % 2))
        {
-               command_print(cmd_ctx, "usage: irscan <device> <instr> [dev2] [instr2] ...");
-               return ERROR_OK;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
        fields = malloc(sizeof(scan_field_t) * argc / 2);
@@ -1745,7 +2006,7 @@ int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **a
                fields[i].in_handler_priv = NULL;
        }
 
-       jtag_add_ir_scan(argc / 2, fields, -1, NULL);
+       jtag_add_ir_scan(argc / 2, fields, -1);
        jtag_execute_queue();
 
        for (i = 0; i < argc / 2; i++)
@@ -1766,8 +2027,7 @@ int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **a
        
        if ((argc < 2) || (argc % 2))
        {
-               command_print(cmd_ctx, "usage: drscan <device> <var> [dev2] [var2]");
-               return ERROR_OK;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
        for (i = 0; i < argc; i+=2)
@@ -1805,33 +2065,38 @@ int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **a
                }
        }
 
-       jtag_add_dr_scan(num_fields, fields, -1, NULL);
+       jtag_add_dr_scan(num_fields, fields, -1);
        jtag_execute_queue();
        
        for (i = 0; i < argc / 2; i++)
                free(fields[i].out_value);
 
        free(fields);
-
+       
        return ERROR_OK;
 }
 
 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
-       if (argc == 0)
-       {
-               command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
-               return ERROR_OK;
-       }
-       
-       if (strcmp(args[0], "enable") == 0)
+       if (argc == 1)
        {
-               jtag_verify_capture_ir = 1;
-       }
-       else if (strcmp(args[0], "disable") == 0)
+               if (strcmp(args[0], "enable") == 0)
+               {
+                       jtag_verify_capture_ir = 1;
+               }
+               else if (strcmp(args[0], "disable") == 0)
+               {
+                       jtag_verify_capture_ir = 0;
+               } else
+               {
+                       return ERROR_COMMAND_SYNTAX_ERROR;
+               }
+       } else if (argc != 0)
        {
-               jtag_verify_capture_ir = 0;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
        
+       command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
+       
        return ERROR_OK;
 }