zy1000: allow it to build on linux host for testing purposes
[openocd.git] / src / jtag / zy1000 / zy1000.c
index 65d84026079504d56d8016d67925f3f1b3104098..907b96578616885f560acb9f2e510f62c798040e 100644 (file)
 #include <target/embeddedice.h>
 #include <jtag/minidriver.h>
 #include <jtag/interface.h>
+#include <time.h>
+
+
+#if BUILD_ECOSBOARD
 #include "zy1000_version.h"
 
 #include <cyg/hal/hal_io.h>             // low level i/o
 #include <cyg/hal/hal_diag.h>
 
-#include <time.h>
-
 #ifdef CYGPKG_HAL_NIOS2
 #include <cyg/hal/io.h>
 #include <cyg/firmwareutil/firmwareutil.h>
@@ -66,6 +68,7 @@
 #define ZYLIN_OPENOCD GIT_OPENOCD_VERSION
 #define ZYLIN_OPENOCD_VERSION "ZY1000 " ZYLIN_VERSION " " ZYLIN_DATE
 
+#endif
 
 static int zy1000_khz(int khz, int *jtag_speed)
 {
@@ -96,7 +99,7 @@ static int zy1000_speed_div(int speed, int *khz)
 
 static bool readPowerDropout(void)
 {
-       cyg_uint32 state;
+       uint32_t state;
        // sample and clear power dropout
        ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x80);
        ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, state);
@@ -108,7 +111,7 @@ static bool readPowerDropout(void)
 
 static bool readSRST(void)
 {
-       cyg_uint32 state;
+       uint32_t state;
        // sample and clear SRST sensing
        ZY1000_POKE(ZY1000_JTAG_BASE + 0x10, 0x00000040);
        ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, state);
@@ -260,6 +263,7 @@ COMMAND_HANDLER(handle_power_command)
 }
 
 
+#if BUILD_ECOSBOARD
 /* Give TELNET a way to find out what version this is */
 static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 {
@@ -308,7 +312,7 @@ static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv
                         * and actual FPGA
                         */
                        static char *fpga_id = "0x12345678 0x12345678 0x12345678 0x12345678";
-                       cyg_uint32 id, timestamp;
+                       uint32_t id, timestamp;
                        HAL_READ_UINT32(SYSID_BASE, id);
                        HAL_READ_UINT32(SYSID_BASE+4, timestamp);
                        sprintf(fpga_id, "0x%08x 0x%08x 0x%08x 0x%08x", id, timestamp, SYSID_ID, SYSID_TIMESTAMP);
@@ -333,7 +337,7 @@ static int jim_zy1000_version(Jim_Interp *interp, int argc, Jim_Obj *const *argv
 
        return JIM_OK;
 }
-
+#endif
 
 #ifdef CYGPKG_HAL_NIOS2
 
@@ -353,7 +357,7 @@ static void report_info(void *data, const char * format, va_list args)
 
 struct cyg_upgrade_info firmware_info =
 {
-               (cyg_uint8 *)0x84000000,
+               (uint8_t *)0x84000000,
                "/ram/firmware.phi",
                "Firmware",
                0x0300000,
@@ -401,7 +405,7 @@ zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp,
                return JIM_ERR;
        }
 
-       cyg_uint32 status;
+       uint32_t status;
        ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, status);
 
        Jim_SetResult(interp, Jim_NewIntObj(interp, (status&0x80) != 0));
@@ -414,7 +418,9 @@ zylinjtag_Jim_Command_powerstatus(Jim_Interp *interp,
 
 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
 
@@ -438,7 +444,7 @@ int zy1000_quit(void)
 
 int interface_jtag_execute_queue(void)
 {
-       cyg_uint32 empty;
+       uint32_t empty;
 
        waitIdle();
        ZY1000_PEEK(ZY1000_JTAG_BASE + 0x10, empty);
@@ -460,18 +466,18 @@ int interface_jtag_execute_queue(void)
 
 
 
-static cyg_uint32 getShiftValue(void)
+static uint32_t getShiftValue(void)
 {
-       cyg_uint32 value;
+       uint32_t value;
        waitIdle();
        ZY1000_PEEK(ZY1000_JTAG_BASE + 0xc, value);
        VERBOSE(LOG_INFO("getShiftValue %08x", value));
        return value;
 }
 #if 0
-static cyg_uint32 getShiftValueFlip(void)
+static uint32_t getShiftValueFlip(void)
 {
-       cyg_uint32 value;
+       uint32_t value;
        waitIdle();
        ZY1000_PEEK(ZY1000_JTAG_BASE + 0x18, value);
        VERBOSE(LOG_INFO("getShiftValue %08x (flipped)", value));
@@ -480,10 +486,10 @@ static cyg_uint32 getShiftValueFlip(void)
 #endif
 
 #if 0
-static void shiftValueInnerFlip(const tap_state_t state, const tap_state_t endState, int repeat, cyg_uint32 value)
+static void shiftValueInnerFlip(const tap_state_t state, const tap_state_t endState, int repeat, uint32_t value)
 {
        VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", tap_state_name(state), tap_state_name(endState), repeat, value));
-       cyg_uint32 a,b;
+       uint32_t a,b;
        a = state;
        b = endState;
        ZY1000_POKE(ZY1000_JTAG_BASE + 0xc, value);
@@ -492,103 +498,93 @@ static void shiftValueInnerFlip(const tap_state_t state, const tap_state_t endSt
 }
 #endif
 
-static void gotoEndState(tap_state_t end_state)
-{
-       setCurrentState(end_state);
-}
-
-static __inline void scanFields(int num_fields, const struct scan_field *fields, tap_state_t shiftState, int pause)
+// here we shuffle N bits out/in
+static __inline void scanBits(const uint8_t *out_value, uint8_t *in_value, int num_bits, bool pause, tap_state_t shiftState, tap_state_t end_state)
 {
-       int i;
-       int j;
-       int k;
-
-       for (i = 0; i < num_fields; i++)
+       tap_state_t pause_state = shiftState;
+       for (int j = 0; j < num_bits; j += 32)
        {
-               cyg_uint32 value;
-
-               uint8_t *inBuffer = NULL;
-
-
-               // figure out where to store the input data
-               int num_bits = fields[i].num_bits;
-               if (fields[i].in_value != NULL)
+               int k = num_bits - j;
+               if (k > 32)
+               {
+                       k = 32;
+                       /* we have more to shift out */
+               } else if (pause)
                {
-                       inBuffer = fields[i].in_value;
+                       /* this was the last to shift out this time */
+                       pause_state = end_state;
                }
 
-               // here we shuffle N bits out/in
-               j = 0;
-               while (j < num_bits)
+               // we have (num_bits + 7)/8 bytes of bits to toggle out.
+               // bits are pushed out LSB to MSB
+               uint32_t value;
+               value = 0;
+               if (out_value != NULL)
                {
-                       tap_state_t pause_state;
-                       int l;
-                       k = num_bits-j;
-                       pause_state = (shiftState == TAP_DRSHIFT)?TAP_DRSHIFT:TAP_IRSHIFT;
-                       if (k > 32)
-                       {
-                               k = 32;
-                               /* we have more to shift out */
-                       } else if (pause&&(i == num_fields-1))
+                       for (int l = 0; l < k; l += 8)
                        {
-                               /* this was the last to shift out this time */
-                               pause_state = (shiftState==TAP_DRSHIFT)?TAP_DRPAUSE:TAP_IRPAUSE;
+                               value|=out_value[(j + l)/8]<<l;
                        }
+               }
+               /* mask away unused bits for easier debugging */
+               if (k < 32)
+               {
+                       value&=~(((uint32_t)0xffffffff) << k);
+               } else
+               {
+                       /* Shifting by >= 32 is not defined by the C standard
+                        * and will in fact shift by &0x1f bits on nios */
+               }
 
-                       // we have (num_bits + 7)/8 bytes of bits to toggle out.
-                       // bits are pushed out LSB to MSB
-                       value = 0;
-                       if (fields[i].out_value != NULL)
-                       {
-                               for (l = 0; l < k; l += 8)
-                               {
-                                       value|=fields[i].out_value[(j + l)/8]<<l;
-                               }
-                       }
-                       /* mask away unused bits for easier debugging */
-                       if (k < 32)
-                       {
-                               value&=~(((uint32_t)0xffffffff) << k);
-                       } else
-                       {
-                               /* Shifting by >= 32 is not defined by the C standard
-                                * and will in fact shift by &0x1f bits on nios */
-                       }
+               shiftValueInner(shiftState, pause_state, k, value);
 
-                       shiftValueInner(shiftState, pause_state, k, value);
+               if (in_value != NULL)
+               {
+                       // data in, LSB to MSB
+                       value = getShiftValue();
+                       // we're shifting in data to MSB, shift data to be aligned for returning the value
+                       value >>= 32-k;
 
-                       if (inBuffer != NULL)
+                       for (int l = 0; l < k; l += 8)
                        {
-                               // data in, LSB to MSB
-                               value = getShiftValue();
-                               // we're shifting in data to MSB, shift data to be aligned for returning the value
-                               value >>= 32-k;
-
-                               for (l = 0; l < k; l += 8)
-                               {
-                                       inBuffer[(j + l)/8]=(value >> l)&0xff;
-                               }
+                               in_value[(j + l)/8]=(value >> l)&0xff;
                        }
-                       j += k;
                }
        }
 }
 
+static __inline void scanFields(int num_fields, const struct scan_field *fields, tap_state_t shiftState, tap_state_t end_state)
+{
+       for (int i = 0; i < num_fields; i++)
+       {
+               scanBits(fields[i].out_value,
+                               fields[i].in_value,
+                               fields[i].num_bits,
+                               (i == num_fields-1),
+                               shiftState,
+                               end_state);
+       }
+}
+
 int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field *fields, tap_state_t state)
 {
        int scan_size = 0;
        struct jtag_tap *tap, *nextTap;
+       tap_state_t pause_state = TAP_IRSHIFT;
 
        for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap)
        {
                nextTap = jtag_tap_next_enabled(tap);
-               bool pause = (nextTap==NULL);
+               if (nextTap==NULL)
+               {
+                       pause_state = state;
+               }
                scan_size = tap->ir_length;
 
                /* search the list */
                if (tap == active)
                {
-                       scanFields(1, fields, TAP_IRSHIFT, pause);
+                       scanFields(1, fields, TAP_IRSHIFT, pause_state);
                        /* update device information */
                        buf_cpy(fields[0].out_value, tap->cur_instr, scan_size);
 
@@ -597,12 +593,11 @@ int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field
                {
                        /* if a device isn't listed, set it to BYPASS */
                        assert(scan_size <= 32);
-                       shiftValueInner(TAP_IRSHIFT, pause?TAP_IRPAUSE:TAP_IRSHIFT, scan_size, 0xffffffff);
+                       shiftValueInner(TAP_IRSHIFT, pause_state, scan_size, 0xffffffff);
 
                        tap->bypass = 1;
                }
        }
-       gotoEndState(state);
 
        return ERROR_OK;
 }
@@ -613,51 +608,41 @@ int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field
 
 int interface_jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
 {
-       struct scan_field field;
-       field.num_bits  = num_bits;
-       field.out_value = out_bits;
-       field.in_value  = in_bits;
-
-       scanFields(1, &field, TAP_IRSHIFT, 1);
-       gotoEndState(state);
-
+       scanBits(out_bits, in_bits, num_bits, true, TAP_IRSHIFT, state);
        return ERROR_OK;
 }
 
 int interface_jtag_add_dr_scan(struct jtag_tap *active, int num_fields, const struct scan_field *fields, tap_state_t state)
 {
        struct jtag_tap *tap, *nextTap;
+       tap_state_t pause_state = TAP_DRSHIFT;
        for (tap = jtag_tap_next_enabled(NULL); tap!= NULL; tap = nextTap)
        {
                nextTap = jtag_tap_next_enabled(tap);
-               bool pause = (nextTap==NULL);
+               if (nextTap==NULL)
+               {
+                       pause_state = state;
+               }
 
                /* Find a range of fields to write to this tap */
                if (tap == active)
                {
                        assert(!tap->bypass);
 
-                       scanFields(num_fields, fields, TAP_DRSHIFT, pause);
+                       scanFields(num_fields, fields, TAP_DRSHIFT, pause_state);
                } else
                {
                        /* Shift out a 0 for disabled tap's */
                        assert(tap->bypass);
-                       shiftValueInner(TAP_DRSHIFT, pause?TAP_DRPAUSE:TAP_DRSHIFT, 1, 0);
+                       shiftValueInner(TAP_DRSHIFT, pause_state, 1, 0);
                }
        }
-       gotoEndState(state);
        return ERROR_OK;
 }
 
 int interface_jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
 {
-       struct scan_field field;
-       field.num_bits  = num_bits;
-       field.out_value = out_bits;
-       field.in_value  = in_bits;
-
-       scanFields(1, &field, TAP_DRSHIFT, 1);
-       gotoEndState(state);
+       scanBits(out_bits, in_bits, num_bits, true, TAP_DRSHIFT, state);
        return ERROR_OK;
 }
 
@@ -775,7 +760,7 @@ int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
 
        uint8_t seq[16];
        memset(seq, 0, sizeof(seq));
-       assert(num_states < (sizeof(seq) * 8));
+       assert(num_states < (int)((sizeof(seq) * 8)));
 
        while (num_states)
        {
@@ -803,34 +788,43 @@ int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
        return interface_add_tms_seq(state_count, seq, cur_state);
 }
 
-void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, uint8_t *buffer, int little, int count)
+static void jtag_pre_post_bits(struct jtag_tap *tap, int *pre, int *post)
 {
-//     static int const reg_addr = 0x5;
-       tap_state_t end_state = jtag_get_end_state();
-       if (jtag_tap_next_enabled(jtag_tap_next_enabled(NULL)) == NULL)
+       /* bypass bits before and after */
+       int pre_bits = 0;
+       int post_bits = 0;
+
+       bool found = false;
+       struct jtag_tap *cur_tap, *nextTap;
+       for (cur_tap = jtag_tap_next_enabled(NULL); cur_tap!= NULL; cur_tap = nextTap)
        {
-               /* better performance via code duplication */
-               if (little)
+               nextTap = jtag_tap_next_enabled(cur_tap);
+               if (cur_tap == tap)
                {
-                       int i;
-                       for (i = 0; i < count; i++)
-                       {
-                               shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 1));
-                               shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr | (1 << 5));
-                               buffer += 4;
-                       }
+                       found = true;
                } else
                {
-                       int i;
-                       for (i = 0; i < count; i++)
+                       if (found)
                        {
-                               shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, 0));
-                               shiftValueInner(TAP_DRSHIFT, end_state, 6, reg_addr | (1 << 5));
-                               buffer += 4;
+                               post_bits++;
+                       } else
+                       {
+                               pre_bits++;
                        }
                }
        }
-       else
+       *pre = pre_bits;
+       *post = post_bits;
+}
+
+void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, uint8_t *buffer, int little, int count)
+{
+
+       int pre_bits;
+       int post_bits;
+       jtag_pre_post_bits(tap, &pre_bits, &post_bits);
+
+       if (pre_bits + post_bits + 6 > 32)
        {
                int i;
                for (i = 0; i < count; i++)
@@ -838,6 +832,19 @@ void embeddedice_write_dcc(struct jtag_tap *tap, int reg_addr, uint8_t *buffer,
                        embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little));
                        buffer += 4;
                }
+       } else
+       {
+               shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, pre_bits, 0);
+               int i;
+               for (i = 0; i < count - 1; i++)
+               {
+                       /* Fewer pokes means we get to use the FIFO more efficiently */
+                       shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, little));
+                       shiftValueInner(TAP_DRSHIFT, TAP_IDLE, 6 + post_bits + pre_bits, (reg_addr | (1 << 5)));
+                       buffer += 4;
+               }
+               shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, fast_target_buffer_get_u32(buffer, little));
+               shiftValueInner(TAP_DRSHIFT, TAP_IDLE, 6 + post_bits, (reg_addr | (1 << 5)));
        }
 }
 
@@ -858,8 +865,9 @@ int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opc
 
 
        /* bypass bits before and after */
-       int pre_bits = 0;
-       int post_bits = 0;
+       int pre_bits;
+       int post_bits;
+       jtag_pre_post_bits(tap, &pre_bits, &post_bits);
 
        bool found = false;
        struct jtag_tap *cur_tap, *nextTap;
@@ -895,6 +903,7 @@ int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap * tap, uint32_t opc
                value |= (*t++<<24);
 
                shiftValueInner(TAP_DRSHIFT, TAP_DRSHIFT, 32, value);
+               /* minimum 2 bits */
                shiftValueInner(TAP_DRSHIFT, TAP_DRPAUSE, post_bits, 0);
 
 #if 1
@@ -950,6 +959,7 @@ static const struct command_registration zy1000_commands[] = {
                        "With no arguments, prints status.",
                .usage = "('on'|'off)",
        },
+#if BUILD_ECOSBOARD
        {
                .name = "zy1000_version",
                .mode = COMMAND_ANY,
@@ -957,6 +967,7 @@ static const struct command_registration zy1000_commands[] = {
                .help = "Print version info for zy1000.",
                .usage = "['openocd'|'zy1000'|'date'|'time'|'pcb'|'fpga']",
        },
+#endif
        {
                .name = "powerstatus",
                .mode = COMMAND_ANY,

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)