+void zy1000_flush_callbackqueue(void)
+{
+ /* we have to flush the read queue so we have access to
+ the data the callbacks will use
+ */
+ zy1000_flush_readqueue();
+ int i;
+ for (i = 0; i < callbackqueue_pos; i++)
+ {
+ struct callbackentry *entry = &callbackqueue[i];
+ jtag_set_error(entry->callback(entry->data0, entry->data1, entry->data2, entry->data3));
+ }
+ callbackqueue_pos = 0;
+}
+
+static void writeShiftValue(uint8_t *data, int bits)
+{
+ waitIdle();
+
+ if (!writeLong((ZY1000_CMD_PEEK << 24) | (ZY1000_JTAG_BASE + 0xc)))
+ {
+ fprintf(stderr, "Could not read from zy1000 server\n");
+ exit(-1);
+ }
+
+ if (readqueue_pos >= readqueue_size)
+ {
+ zy1000_flush_readqueue();
+ }
+
+ readqueue[readqueue_pos].dest = data;
+ readqueue[readqueue_pos].bits = bits;
+ readqueue_pos++;
+}
+
+#else
+
+static void writeShiftValue(uint8_t *data, int bits)
+{
+ uint32_t value;
+ waitIdle();
+ ZY1000_PEEK(ZY1000_JTAG_BASE + 0xc, value);
+ VERBOSE(LOG_INFO("getShiftValue %08x", value));
+
+ // data in, LSB to MSB
+ // we're shifting in data to MSB, shift data to be aligned for returning the value
+ value >>= 32 - bits;
+
+ for (int l = 0; l < bits; l += 8)
+ {
+ data[l/8]=(value >> l)&0xff;
+ }
+}
+
+#endif
+
+#if BUILD_ZY1000_MASTER
+
+pthread_t thread;
+
+#if BUILD_ECOSBOARD
+static char watchdog_stack[2048];
+static cyg_thread watchdog_thread_object;
+static cyg_handle_t watchdog_thread_handle;
+#endif
+
+/* 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;
+ /* Wait for some us */
+ usleep(data);
+ break;
+ }
+ case ZY1000_CMD_WAITIDLE:
+ {
+ waitIdle();
+ break;
+ }
+ default:
+ return;
+ }
+ }
+}
+
+
+static void *tcpip_server(void *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);
+
+ }
+ /* Never reached actually */
+ close(fd);
+
+ return NULL;
+}
+
+#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
+
+#endif
+
+#if BUILD_ZY1000_MASTER
+int interface_jtag_add_sleep(uint32_t us)
+{
+ jtag_sleep(us);
+ return ERROR_OK;
+}
+#endif
+
+#if BUILD_ZY1000_MASTER && !BUILD_ECOSBOARD
+volatile void *zy1000_jtag_master;
+#include <sys/mman.h>
+#endif
+
+int zy1000_init(void)
+{
+#if BUILD_ECOSBOARD
+ LOG_USER("%s", ZYLIN_OPENOCD_VERSION);
+#elif BUILD_ZY1000_MASTER
+ int fd;
+ if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
+ {
+ LOG_ERROR("No access to /dev/mem");
+ return ERROR_FAIL;
+ }
+#ifndef REGISTERS_BASE
+#define REGISTERS_BASE 0x9002000
+#define REGISTERS_SPAN 128
+#endif
+
+ zy1000_jtag_master = mmap(0, REGISTERS_SPAN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, REGISTERS_BASE);
+
+ if(zy1000_jtag_master == (void *) -1)
+ {
+ close(fd);
+ LOG_ERROR("No access to /dev/mem");
+ return ERROR_FAIL;
+ }
+#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);
+ int jtag_speed_var;
+ int retval = jtag_get_speed(&jtag_speed_var);
+ if (retval != ERROR_OK)
+ return retval;
+ zy1000_speed(jtag_speed_var);
+
+#if BUILD_ZY1000_MASTER
+ pthread_create(&thread, NULL, tcpip_server, NULL);
+
+#if BUILD_ECOSBOARD
+#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
+#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,
+};