+
+int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opcode, uint32_t * data, size_t count)
+{
+#if 0
+ int arm11_run_instr_data_to_core_noack_inner_default(struct jtag_tap * tap, uint32_t opcode, uint32_t * data, size_t count);
+ return arm11_run_instr_data_to_core_noack_inner_default(tap, opcode, data, count);
+#else
+ static const int bits[] = {32, 2};
+ uint32_t values[] = {0, 0};
+
+ /* FIX!!!!!! the target_write_memory() API started this nasty problem
+ * with unaligned uint32_t * pointers... */
+ const uint8_t *t = (const uint8_t *)data;
+
+
+ /* bypass bits before and after */
+ int pre_bits;
+ int post_bits;
+ jtag_pre_post_bits(tap, &pre_bits, &post_bits);
+
+ bool found = false;
+ struct jtag_tap *cur_tap, *nextTap;
+ for (cur_tap = jtag_tap_next_enabled(NULL); cur_tap!= NULL; cur_tap = nextTap)
+ {
+ nextTap = jtag_tap_next_enabled(cur_tap);
+ if (cur_tap == tap)
+ {
+ found = true;
+ } else
+ {
+ if (found)
+ {
+ post_bits++;
+ } else
+ {
+ pre_bits++;
+ }
+ }
+ }
+
+ post_bits+=2;
+
+
+ while (--count > 0)
+ {
+ shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, pre_bits, 0);
+
+ uint32_t value;
+ value = *t++;
+ value |= (*t++<<8);
+ value |= (*t++<<16);
+ value |= (*t++<<24);
+
+ shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, value);
+ /* minimum 2 bits */
+ shiftValueInner(TAP_DRSHIFT, TAP_DRPAUSE, post_bits, 0);
+
+#if 1
+ /* copy & paste from arm11_dbgtap.c */
+ //TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT
+
+ waitIdle();
+ ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 1);
+ ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 1);
+ ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0);
+ ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0);
+ ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0);
+ ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 1);
+ ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0);
+ ZY1000_POKE(ZY1000_JTAG_BASE + 0x28, 0);
+ /* we don't have to wait for the queue to empty here. waitIdle(); */
+ ZY1000_POKE(ZY1000_JTAG_BASE + 0x20, TAP_DRSHIFT);
+#else
+ static const tap_state_t arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay[] =
+ {
+ TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT
+ };
+
+ jtag_add_pathmove(ARRAY_SIZE(arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay),
+ arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay);
+#endif
+ }
+
+ values[0] = *t++;
+ values[0] |= (*t++<<8);
+ values[0] |= (*t++<<16);
+ values[0] |= (*t++<<24);
+
+ /* This will happen on the last iteration updating the current tap state
+ * so we don't have to track it during the common code path */
+ jtag_add_dr_out(tap,
+ 2,
+ bits,
+ values,
+ TAP_IDLE);
+
+ return jtag_execute_queue();
+#endif
+}
+
+
+static const struct command_registration zy1000_commands[] = {
+ {
+ .name = "power",
+ .handler = handle_power_command,
+ .mode = COMMAND_ANY,
+ .help = "Turn power switch to target on/off. "
+ "With no arguments, prints status.",
+ .usage = "('on'|'off)",
+ },
+#if BUILD_ECOSBOARD
+ {
+ .name = "zy1000_version",
+ .mode = COMMAND_ANY,
+ .jim_handler = jim_zy1000_version,
+ .help = "Print version info for zy1000.",
+ .usage = "['openocd'|'zy1000'|'date'|'time'|'pcb'|'fpga']",
+ },
+#else
+ {
+ .name = "zy1000_server",
+ .mode = COMMAND_ANY,
+ .jim_handler = jim_zy1000_server,
+ .help = "Tcpip address for ZY1000 server.",
+ .usage = "address",
+ },
+#endif
+ {
+ .name = "powerstatus",
+ .mode = COMMAND_ANY,
+ .jim_handler = zylinjtag_Jim_Command_powerstatus,
+ .help = "Returns power status of target",
+ },
+#ifdef CYGPKG_HAL_NIOS2
+ {
+ .name = "updatezy1000firmware",
+ .mode = COMMAND_ANY,
+ .jim_handler = jim_zy1000_writefirmware,
+ .help = "writes firmware to flash",
+ /* .usage = "some_string", */
+ },
+#endif
+ COMMAND_REGISTRATION_DONE
+};
+
+
+static int tcp_ip = -1;
+
+/* Write large packets if we can */
+static size_t out_pos;
+static uint8_t out_buffer[16384];
+static size_t in_pos;
+static size_t in_write;
+static uint8_t in_buffer[16384];
+
+static bool flush_writes(void)
+{
+ bool ok = (write(tcp_ip, out_buffer, out_pos) == (int)out_pos);
+ out_pos = 0;
+ return ok;
+}
+
+static bool writeLong(uint32_t l)
+{
+ int i;
+ for (i = 0; i < 4; i++)
+ {
+ uint8_t c = (l >> (i*8))&0xff;
+ out_buffer[out_pos++] = c;
+ if (out_pos >= sizeof(out_buffer))
+ {
+ if (!flush_writes())
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+static bool readLong(uint32_t *out_data)
+{
+ if (out_pos > 0)
+ {
+ if (!flush_writes())
+ {
+ return false;
+ }
+ }
+
+ uint32_t data = 0;
+ int i;
+ for (i = 0; i < 4; i++)
+ {
+ uint8_t c;
+ if (in_pos == in_write)
+ {
+ /* read more */
+ int t;
+ t = read(tcp_ip, in_buffer, sizeof(in_buffer));
+ if (t < 1)
+ {
+ return false;
+ }
+ in_write = (size_t) t;
+ in_pos = 0;
+ }
+ c = in_buffer[in_pos++];
+
+ data |= (c << (i*8));
+ }
+ *out_data = data;
+ return true;
+}
+
+enum ZY1000_CMD
+{
+ ZY1000_CMD_POKE = 0x0,
+ ZY1000_CMD_PEEK = 0x8,
+ ZY1000_CMD_SLEEP = 0x1,
+};
+
+
+#if !BUILD_ECOSBOARD
+
+#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
+#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
+
+/* We initialize this late since we need to know the server address
+ * first.
+ */
+static void tcpip_open(void)
+{
+ if (tcp_ip >= 0)
+ return;
+
+ struct sockaddr_in echoServAddr; /* Echo server address */
+
+ /* Create a reliable, stream socket using TCP */
+ if ((tcp_ip = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
+ {
+ fprintf(stderr, "Failed to connect to zy1000 server\n");
+ exit(-1);
+ }
+
+ /* Construct the server address structure */
+ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
+ echoServAddr.sin_family = AF_INET; /* Internet address family */
+ echoServAddr.sin_addr.s_addr = inet_addr(tcp_server); /* Server IP address */
+ echoServAddr.sin_port = htons(7777); /* Server port */
+
+ /* Establish the connection to the echo server */
+ if (connect(tcp_ip, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
+ {
+ fprintf(stderr, "Failed to connect to zy1000 server\n");
+ exit(-1);
+ }
+
+ int flag = 1;
+ setsockopt(tcp_ip, /* socket affected */
+ IPPROTO_TCP, /* set option at TCP level */
+ TCP_NODELAY, /* name of option */
+ (char *)&flag, /* the cast is historical cruft */
+ sizeof(int)); /* length of option value */
+
+}
+
+
+/* send a poke */
+void zy1000_tcpout(uint32_t address, uint32_t data)
+{
+ tcpip_open();
+ if (!writeLong((ZY1000_CMD_POKE << 24) | address)||
+ !writeLong(data))
+ {
+ fprintf(stderr, "Could not write to zy1000 server\n");
+ exit(-1);
+ }
+}
+
+uint32_t zy1000_tcpin(uint32_t address)
+{
+ tcpip_open();
+ uint32_t data;
+ if (!writeLong((ZY1000_CMD_PEEK << 24) | address)||
+ !readLong(&data))
+ {
+ fprintf(stderr, "Could not read from zy1000 server\n");
+ exit(-1);
+ }
+ return data;
+}
+
+int interface_jtag_add_sleep(uint32_t us)
+{
+ tcpip_open();
+ if (!writeLong((ZY1000_CMD_SLEEP << 24))||
+ !writeLong(us))
+ {
+ fprintf(stderr, "Could not read from zy1000 server\n");
+ exit(-1);
+ }
+ return ERROR_OK;
+}
+
+
+#endif
+
+#if BUILD_ECOSBOARD
+static char tcpip_stack[2048];
+static cyg_thread tcpip_thread_object;
+static cyg_handle_t tcpip_thread_handle;
+
+static char watchdog_stack[2048];
+static cyg_thread watchdog_thread_object;
+static cyg_handle_t watchdog_thread_handle;
+
+/* Infinite loop peeking & poking */
+static void tcpipserver(void)
+{
+ for (;;)
+ {
+ uint32_t address;
+ if (!readLong(&address))
+ return;
+ enum ZY1000_CMD c = (address >> 24) & 0xff;
+ address &= 0xffffff;
+ switch (c)
+ {
+ case ZY1000_CMD_POKE:
+ {
+ uint32_t data;
+ if (!readLong(&data))
+ return;
+ address &= ~0x80000000;
+ ZY1000_POKE(address + ZY1000_JTAG_BASE, data);
+ break;
+ }
+ case ZY1000_CMD_PEEK:
+ {
+ uint32_t data;
+ ZY1000_PEEK(address + ZY1000_JTAG_BASE, data);
+ if (!writeLong(data))
+ return;
+ break;
+ }
+ case ZY1000_CMD_SLEEP:
+ {
+ uint32_t data;
+ if (!readLong(&data))
+ return;
+ jtag_sleep(data);
+ break;
+ }
+ default:
+ return;
+ }
+ }
+}
+
+
+static void tcpip_server(cyg_addrword_t data)
+{
+ int so_reuseaddr_option = 1;
+
+ int fd;
+ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ {
+ LOG_ERROR("error creating socket: %s", strerror(errno));
+ exit(-1);
+ }
+
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option,
+ sizeof(int));
+
+ struct sockaddr_in sin;
+ unsigned int address_size;
+ address_size = sizeof(sin);
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = INADDR_ANY;
+ sin.sin_port = htons(7777);
+
+ if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
+ {
+ LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
+ exit(-1);
+ }
+
+ if (listen(fd, 1) == -1)
+ {
+ LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
+ exit(-1);
+ }
+
+
+ for (;;)
+ {
+ tcp_ip = accept(fd, (struct sockaddr *) &sin, &address_size);
+ if (tcp_ip < 0)
+ {
+ continue;
+ }
+
+ int flag = 1;
+ setsockopt(tcp_ip, /* socket affected */
+ IPPROTO_TCP, /* set option at TCP level */
+ TCP_NODELAY, /* name of option */
+ (char *)&flag, /* the cast is historical cruft */
+ sizeof(int)); /* length of option value */
+
+ bool save_poll = jtag_poll_get_enabled();
+
+ /* polling will screw up the "connection" */
+ jtag_poll_set_enabled(false);
+
+ tcpipserver();
+
+ jtag_poll_set_enabled(save_poll);
+
+ close(tcp_ip);
+
+ }
+ close(fd);
+
+}
+
+#ifdef WATCHDOG_BASE
+/* If we connect to port 8888 we must send a char every 10s or the board resets itself */
+static void watchdog_server(cyg_addrword_t data)
+{
+ int so_reuseaddr_option = 1;
+
+ int fd;
+ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ {
+ LOG_ERROR("error creating socket: %s", strerror(errno));
+ exit(-1);
+ }
+
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &so_reuseaddr_option,
+ sizeof(int));
+
+ struct sockaddr_in sin;
+ unsigned int address_size;
+ address_size = sizeof(sin);
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = INADDR_ANY;
+ sin.sin_port = htons(8888);
+
+ if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
+ {
+ LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
+ exit(-1);
+ }
+
+ if (listen(fd, 1) == -1)
+ {
+ LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
+ exit(-1);
+ }
+
+
+ for (;;)
+ {
+ int watchdog_ip = accept(fd, (struct sockaddr *) &sin, &address_size);
+
+ /* Start watchdog, must be reset every 10 seconds. */
+ HAL_WRITE_UINT32(WATCHDOG_BASE + 4, 4);
+
+ if (watchdog_ip < 0)
+ {
+ LOG_ERROR("couldn't open watchdog socket: %s", strerror(errno));
+ exit(-1);
+ }
+
+ int flag = 1;
+ setsockopt(watchdog_ip, /* socket affected */
+ IPPROTO_TCP, /* set option at TCP level */
+ TCP_NODELAY, /* name of option */
+ (char *)&flag, /* the cast is historical cruft */
+ sizeof(int)); /* length of option value */
+
+
+ char buf;
+ for (;;)
+ {
+ if (read(watchdog_ip, &buf, 1) == 1)
+ {
+ /* Reset timer */
+ HAL_WRITE_UINT32(WATCHDOG_BASE + 8, 0x1234);
+ /* Echo so we can telnet in and see that resetting works */
+ write(watchdog_ip, &buf, 1);
+ } else
+ {
+ /* Stop tickling the watchdog, the CPU will reset in < 10 seconds
+ * now.
+ */
+ return;
+ }
+
+ }
+
+ /* Never reached */
+ }
+}
+#endif
+
+int interface_jtag_add_sleep(uint32_t us)
+{
+ jtag_sleep(us);
+ return ERROR_OK;
+}
+
+#endif
+
+
+int zy1000_init(void)
+{
+#if BUILD_ECOSBOARD
+ LOG_USER("%s", ZYLIN_OPENOCD_VERSION);
+#endif
+
+ ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x30); // Turn on LED1 & LED2
+
+ setPower(true); // on by default
+
+
+ /* deassert resets. Important to avoid infinite loop waiting for SRST to deassert */
+ zy1000_reset(0, 0);
+ zy1000_speed(jtag_get_speed());
+
+
+#if BUILD_ECOSBOARD
+ cyg_thread_create(1, tcpip_server, (cyg_addrword_t) 0, "tcip/ip server",
+ (void *) tcpip_stack, sizeof(tcpip_stack),
+ &tcpip_thread_handle, &tcpip_thread_object);
+ cyg_thread_resume(tcpip_thread_handle);
+#ifdef WATCHDOG_BASE
+ cyg_thread_create(1, watchdog_server, (cyg_addrword_t) 0, "watchdog tcip/ip server",
+ (void *) watchdog_stack, sizeof(watchdog_stack),
+ &watchdog_thread_handle, &watchdog_thread_object);
+ cyg_thread_resume(watchdog_thread_handle);
+#endif
+#endif
+
+ return ERROR_OK;
+}
+
+
+
+struct jtag_interface zy1000_interface =
+{
+ .name = "ZY1000",
+ .supported = DEBUG_CAP_TMS_SEQ,
+ .execute_queue = NULL,
+ .speed = zy1000_speed,
+ .commands = zy1000_commands,
+ .init = zy1000_init,
+ .quit = zy1000_quit,
+ .khz = zy1000_khz,
+ .speed_div = zy1000_speed_div,
+ .power_dropout = zy1000_power_dropout,
+ .srst_asserted = zy1000_srst_asserted,
+};
+