int trst_with_tlr = 0;
        int retval;
        
+       /* FIX!!! there are *many* different cases here. A better
+        * approach is needed for legal combinations of transitions...
+       */
+       if ((jtag_reset_config & RESET_HAS_SRST)&&
+                       (jtag_reset_config & RESET_HAS_TRST)&& 
+                       ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)&&
+                       ((jtag_reset_config & RESET_TRST_PULLS_SRST)==0))
+       {
+               if (((req_tlr_or_trst&&!jtag_trst)||
+                               (!req_tlr_or_trst&&jtag_trst))&&
+                               ((req_srst&&!jtag_srst)||
+                                               (!req_srst&&jtag_srst)))
+               {
+                       LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
+               }
+       }
+       
        /* 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_tlr_or_trst))
        return ERROR_OK;
 }
 
-int jtag_init(struct command_context_s *cmd_ctx)
+static int jtag_init_inner(struct command_context_s *cmd_ctx)
 {
        int validate_tries = 0;
        jtag_device_t *device;
        return ERROR_OK;
 }
 
+int jtag_init_reset(struct command_context_s *cmd_ctx)
+{
+       int retval;
+       LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / tms");
+
+       /* Reset can happen after a power cycle.
+        * 
+        * Ideally we would only assert TRST or run tms before the target reset.
+        * 
+        * However w/srst_pulls_trst, trst is asserted together with the target
+        * reset whether we want it or not.
+        * 
+        * NB! Some targets have JTAG circuitry disabled until a 
+        * trst & srst has been asserted.
+        * 
+        * NB! here we assume nsrst/ntrst delay are sufficient!
+        * 
+        * NB! order matters!!!! srst *can* disconnect JTAG circuitry
+        * 
+        */
+       jtag_add_reset(1, 0); /* TMS or TRST */
+       if (jtag_reset_config & RESET_HAS_SRST)
+       {
+               jtag_add_reset(1, 1);
+               if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
+                       jtag_add_reset(0, 1);
+       }
+       jtag_add_reset(0, 0);
+       if ((retval = jtag_execute_queue()) != ERROR_OK)
+               return retval;
+       
+       /* Check that we can communication on the JTAG chain + eventually we want to
+        * be able to perform enumeration only after OpenOCD has started 
+        * telnet and GDB server
+        * 
+        * That would allow users to more easily perform any magic they need to before
+        * reset happens.
+        */
+       return jtag_init_inner(cmd_ctx);
+}
+
+int jtag_init(struct command_context_s *cmd_ctx)
+{
+       if (jtag_init_inner(cmd_ctx)==ERROR_OK)
+       {
+               return ERROR_OK;
+       }
+       return jtag_init_reset(cmd_ctx);
+}
+
 
 static int default_khz(int khz, int *jtag_speed)
 {
 
 
 extern enum reset_types jtag_reset_config;
 
-/* JTAG subsystem */
+/* initialize JTAG chain using only a TLR reset. If init fails,
+ * try reset + init.
+ */
 extern int jtag_init(struct command_context_s *cmd_ctx);
+/* reset, then initialize JTAG chain */
+extern int jtag_init_reset(struct command_context_s *cmd_ctx);
 extern int jtag_register_commands(struct command_context_s *cmd_ctx);
 
 /* JTAG interface, can be implemented with a software or hardware fifo
  * This is why combinations such as "reset_config srst_only srst_pulls_trst"
  * are supported. 
  *
+ * only req_tlr_or_trst and srst can have a transition for a
+ * call as the effects of transitioning both at the "same time" 
+ * are undefined, but when srst_pulls_trst or vice versa,
+ * then trst & srst *must* be asserted together.
  */
 extern void jtag_add_reset(int req_tlr_or_trst, int srst);
 /* this drives the actual srst and trst pins. srst will always be 0
 
        return ERROR_OK;
 }
 
+static int daemon_startup = 0;
+
+int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+       if (argc==0)
+               return ERROR_OK;
+       if (argc > 1 )
+               return ERROR_COMMAND_SYNTAX_ERROR;
+       
+       daemon_startup = strcmp("reset", args[0])==0;
+       
+       command_print(cmd_ctx, OPENOCD_VERSION);
+
+       return ERROR_OK;
+}
+
+
 void exit_handler(void)
 {
        /* close JTAG interface */
                jtag->quit();
 }
 
+
+/* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
+int handle_init_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+       static int initialized=0;
+       if (initialized)
+               return ERROR_OK;
+       
+       initialized=1;
+       
+       command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
+
+       atexit(exit_handler);
+
+       if (jtag_init(cmd_ctx) != ERROR_OK)
+               return ERROR_FAIL;
+       LOG_DEBUG("jtag init complete");
+
+       if (target_init(cmd_ctx) != ERROR_OK)
+               return ERROR_FAIL;
+       LOG_DEBUG("target init complete");
+
+       if (flash_init_drivers(cmd_ctx) != ERROR_OK)
+               return ERROR_FAIL;
+       LOG_DEBUG("flash init complete");
+
+       if (nand_init(cmd_ctx) != ERROR_OK)
+               return ERROR_FAIL;
+       LOG_DEBUG("NAND init complete");
+
+       if (pld_init(cmd_ctx) != ERROR_OK)
+               return ERROR_FAIL;
+       LOG_DEBUG("pld init complete");
+
+       /* initialize tcp server */
+       server_init();
+
+       /* initialize telnet subsystem */
+       telnet_init("Open On-Chip Debugger");
+       gdb_init();
+
+       return ERROR_OK;
+}
+
+
 /* implementations of OpenOCD that uses multithreading needs to lock OpenOCD while calling
  * OpenOCD fn's. No-op in vanilla OpenOCD
  */
 
        register_command(cmd_ctx, NULL, "version", handle_version_command,
                                         COMMAND_EXEC, "show OpenOCD version");
-
+       register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, 
+                       "deprecated - use \"init\" and \"reset\" at end of startup script instead");
+       
        /* register subsystem commands */
        server_register_commands(cmd_ctx);
        telnet_register_commands(cmd_ctx);
        /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
        /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
 
+       register_command(cmd_ctx, NULL, "init", handle_init_command,
+                                        COMMAND_ANY, "initializes target and servers - nop on subsequent invocations");
+
        cfg_cmd_ctx = copy_command_context(cmd_ctx);
        cfg_cmd_ctx->mode = COMMAND_CONFIG;
        command_set_output_handler(cfg_cmd_ctx, configuration_output_handler, NULL);
        
        command_done(cfg_cmd_ctx);
 
-       command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
-
-       atexit(exit_handler);
-
-       if (jtag_init(cmd_ctx) != ERROR_OK)
-               return EXIT_FAILURE;
-       LOG_DEBUG("jtag init complete");
-
-       if (target_init(cmd_ctx) != ERROR_OK)
-               return EXIT_FAILURE;
-       LOG_DEBUG("target init complete");
-
-       if (flash_init_drivers(cmd_ctx) != ERROR_OK)
+       if (command_run_line(cmd_ctx, "init")!=ERROR_OK)
                return EXIT_FAILURE;
-       LOG_DEBUG("flash init complete");
-
-       if (nand_init(cmd_ctx) != ERROR_OK)
-               return EXIT_FAILURE;
-       LOG_DEBUG("NAND init complete");
-
-       if (pld_init(cmd_ctx) != ERROR_OK)
-               return EXIT_FAILURE;
-       LOG_DEBUG("pld init complete");
-
-       /* initialize tcp server */
-       server_init();
-
-       /* initialize telnet subsystem */
-       telnet_init("Open On-Chip Debugger");
-       gdb_init();
-
-       /* call any target resets */
-       if (target_init_reset(cmd_ctx) != ERROR_OK)
-               return EXIT_FAILURE;
-       LOG_DEBUG("target init reset complete");
+       
+       if (daemon_startup)
+               command_run_line(cmd_ctx, "reset");
 
        /* handle network connections */
        server_loop(cmd_ctx);
 
 
        if ((target->reset_mode == RESET_HALT) || (target->reset_mode == RESET_INIT))
        {
+               reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
+               
+               /* program EmbeddedICE Debug Control Register to deassert DBGRQ
+                * i.e. resume.
+                */
+               buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);     
+               embeddedice_store_reg(dbg_ctrl);
+
                /*
                 * Some targets do not support communication while SRST is asserted. We need to
                 * set up the reset vector catch here.
                }
        }
 
-       /* we can't know what state the target is in as we might e.g.
-        * be resetting after a power dropout, so we need to issue a tms/srst
-        */
-       
-       /* assert SRST and TRST */
-       /* system would get ouf sync if we didn't reset test-logic, too */
-       jtag_add_reset(1, 1);
-       
-       jtag_add_sleep(5000);
-
        /* here we should issue a srst only, but we may have to assert trst as well */
        if (jtag_reset_config & RESET_SRST_PULLS_TRST)
        {
                        LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
                        return ERROR_TARGET_FAILURE;
                }
+               else
+               {
+                       /* we came here in a reset_halt or reset_init sequence
+                        * debug entry was already prepared in arm7_9_prepare_reset_halt()
+                        */
+                       target->debug_reason = DBG_REASON_DBGRQ;
+                       
+                       return ERROR_OK; 
+               }
        }
 
        if (arm7_9->use_dbgrq)
 
 
 
 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 
 int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
        "little endian",
 };
 
-enum daemon_startup_mode startup_mode = DAEMON_ATTACH;
-
 static int target_continous_poll = 1;
 
 /* read a u32 from a buffer in target memory endianness */
 
        jtag->speed(jtag_speed);
 
+       if ((retval = jtag_init_reset(cmd_ctx)) != ERROR_OK)
+               return retval;
+       
+
        /* prepare reset_halt where necessary */
        target = targets;
        while (target)
        return ERROR_OK;
 }
 
-int target_init_reset(struct command_context_s *cmd_ctx)
-{
-       if (startup_mode == DAEMON_RESET)
-               target_process_reset(cmd_ctx);
-       
-       return ERROR_OK;
-}
-
 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
 {
        target_event_callback_t **callbacks_p = &target_event_callbacks;
 {
        register_command(cmd_ctx, NULL, "target", handle_target_command, COMMAND_CONFIG, "target <cpu> [reset_init default - DEPRECATED] <chainpos> <endianness> <variant> [cpu type specifc args]");
        register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
-       register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, NULL);
        register_command(cmd_ctx, NULL, "target_script", handle_target_script_command, COMMAND_CONFIG, NULL);
        register_command(cmd_ctx, NULL, "run_and_halt_time", handle_run_and_halt_time_command, COMMAND_CONFIG, "<target> <run time ms>");
        register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_ANY, "working_area <target#> <address> <size> <'backup'|'nobackup'> [virtual address]");
        return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
 }
 
-/* what to do on daemon startup */
-int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       if (argc == 1)
-       {
-               if (strcmp(args[0], "attach") == 0)
-               {
-                       startup_mode = DAEMON_ATTACH;
-                       return ERROR_OK;
-               }
-               else if (strcmp(args[0], "reset") == 0)
-               {
-                       startup_mode = DAEMON_RESET;
-                       return ERROR_OK;
-               }
-       }
-       
-       LOG_WARNING("invalid daemon_startup configuration directive: %s", args[0]);
-       return ERROR_OK;
-
-}
                
 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
 
 
 extern char *target_state_strings[];
 
-enum daemon_startup_mode
-{
-       DAEMON_ATTACH,          /* simply attach to the target */
-       DAEMON_RESET,           /* reset target (behaviour defined by reset_mode */
-};
-
-extern enum daemon_startup_mode startup_mode;
-
 enum target_reset_mode
 {
        RESET_RUN = 0,          /* reset and let target run */
         * assert_reset() can therefore make no assumptions whatsoever about the
         * state of the target 
         * 
+        * Before assert_reset() for the target is invoked, a TRST/tms and
+        * chain validation is executed. TRST should not be asserted
+        * during target assert unless there is no way around it due to
+        * the way reset's are configured.
+        * 
         */
        int (*assert_reset)(struct target_s *target);
        int (*deassert_reset)(struct target_s *target);
 
 #use combined on interfaces or targets that can't set TRST/SRST separately
 reset_config trst_and_srst srst_pulls_trst
 
+#LPCs need reset pulled while RTCK is log. 0 to activate JTAG, power-on reset is not enough
+jtag_reset 1 1
+jtag_reset 0 0
+
 #jtag scan chain
 #format L IRC IRCM IDCODE (Length, IR Capture, IR Capture Mask, IDCODE)
 jtag_device 4 0x1 0xf 0xe