stlink: simplify handling of SWIM
[openocd.git] / src / jtag / drivers / stlink_usb.c
index a905576c89b37c9967a19855af4bd76b58f166fc..7281e584396327ab2f7bcaa279ed2915da0d76c3 100644 (file)
@@ -36,6 +36,7 @@
 #include <jtag/hla/hla_layout.h>
 #include <jtag/hla/hla_transport.h>
 #include <jtag/hla/hla_interface.h>
+#include <jtag/swim.h>
 #include <target/target.h>
 #include <transport/transport.h>
 
@@ -95,6 +96,15 @@ enum stlink_jtag_api_version {
        STLINK_JTAG_API_V3,
 };
 
+enum stlink_mode {
+       STLINK_MODE_UNKNOWN = 0,
+       STLINK_MODE_DFU,
+       STLINK_MODE_MASS,
+       STLINK_MODE_DEBUG_JTAG,
+       STLINK_MODE_DEBUG_SWD,
+       STLINK_MODE_DEBUG_SWIM
+};
+
 /** */
 struct stlink_usb_version {
        /** */
@@ -132,7 +142,7 @@ struct stlink_usb_handle_s {
        /** */
        uint32_t max_mem_packet;
        /** */
-       enum hl_transports transport;
+       enum stlink_mode st_mode;
        /** */
        struct stlink_usb_version version;
        /** */
@@ -292,16 +302,6 @@ struct stlink_usb_handle_s {
 
 #define STLINK_V3_MAX_FREQ_NB               10
 
-/** */
-enum stlink_mode {
-       STLINK_MODE_UNKNOWN = 0,
-       STLINK_MODE_DFU,
-       STLINK_MODE_MASS,
-       STLINK_MODE_DEBUG_JTAG,
-       STLINK_MODE_DEBUG_SWD,
-       STLINK_MODE_DEBUG_SWIM
-};
-
 #define REQUEST_SENSE        0x03
 #define REQUEST_SENSE_LENGTH 18
 
@@ -710,7 +710,7 @@ static int stlink_usb_error_check(void *handle)
 
        assert(handle != NULL);
 
-       if (h->transport == HL_TRANSPORT_SWIM) {
+       if (h->st_mode == STLINK_MODE_DEBUG_SWIM) {
                switch (h->databuf[0]) {
                        case STLINK_SWIM_ERR_OK:
                                return ERROR_OK;
@@ -819,13 +819,13 @@ static int stlink_cmd_allow_retry(void *handle, const uint8_t *buf, int size)
        struct stlink_usb_handle_s *h = handle;
 
        while (1) {
-               if ((h->transport != HL_TRANSPORT_SWIM) || !retries) {
+               if ((h->st_mode != STLINK_MODE_DEBUG_SWIM) || !retries) {
                        res = stlink_usb_xfer_noerrcheck(handle, buf, size);
                        if (res != ERROR_OK)
                                return res;
                }
 
-               if (h->transport == HL_TRANSPORT_SWIM) {
+               if (h->st_mode == STLINK_MODE_DEBUG_SWIM) {
                        res = stlink_swim_status(handle);
                        if (res != ERROR_OK)
                                return res;
@@ -1288,8 +1288,6 @@ static enum stlink_mode stlink_get_mode(enum hl_transports t)
                return STLINK_MODE_DEBUG_SWD;
        case HL_TRANSPORT_JTAG:
                return STLINK_MODE_DEBUG_JTAG;
-       case HL_TRANSPORT_SWIM:
-               return STLINK_MODE_DEBUG_SWIM;
        default:
                return STLINK_MODE_UNKNOWN;
        }
@@ -1368,7 +1366,7 @@ static int stlink_usb_init_mode(void *handle, bool connect_under_reset, int init
        LOG_DEBUG("MODE: 0x%02X", mode);
 
        /* set selected mode */
-       emode = stlink_get_mode(h->transport);
+       emode = h->st_mode;
 
        if (emode == STLINK_MODE_UNKNOWN) {
                LOG_ERROR("selected mode (transport) not supported");
@@ -1376,12 +1374,12 @@ static int stlink_usb_init_mode(void *handle, bool connect_under_reset, int init
        }
 
        /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
-       if (h->transport == HL_TRANSPORT_JTAG) {
+       if (emode == STLINK_MODE_DEBUG_JTAG) {
                if (h->version.flags & STLINK_F_HAS_JTAG_SET_FREQ) {
                        stlink_dump_speed_map(stlink_khz_to_speed_map_jtag, ARRAY_SIZE(stlink_khz_to_speed_map_jtag));
                        stlink_speed(h, initial_interface_speed, false);
                }
-       } else if (h->transport == HL_TRANSPORT_SWD) {
+       } else if (emode == STLINK_MODE_DEBUG_SWD) {
                if (h->version.flags & STLINK_F_HAS_SWD_SET_FREQ) {
                        stlink_dump_speed_map(stlink_khz_to_speed_map_swd, ARRAY_SIZE(stlink_khz_to_speed_map_swd));
                        stlink_speed(h, initial_interface_speed, false);
@@ -1391,7 +1389,7 @@ static int stlink_usb_init_mode(void *handle, bool connect_under_reset, int init
        if (h->version.jtag_api == STLINK_JTAG_API_V3) {
                struct speed_map map[STLINK_V3_MAX_FREQ_NB];
 
-               stlink_get_com_freq(h, (h->transport == HL_TRANSPORT_JTAG), map);
+               stlink_get_com_freq(h, (emode == STLINK_MODE_DEBUG_JTAG), map);
                stlink_dump_speed_map(map, ARRAY_SIZE(map));
                stlink_speed(h, initial_interface_speed, false);
        }
@@ -1632,7 +1630,7 @@ static int stlink_usb_idcode(void *handle, uint32_t *idcode)
        assert(handle != NULL);
 
        /* there is no swim read core id cmd */
-       if (h->transport == HL_TRANSPORT_SWIM) {
+       if (h->st_mode == STLINK_MODE_DEBUG_SWIM) {
                *idcode = 0;
                return ERROR_OK;
        }
@@ -1763,22 +1761,9 @@ static enum target_state stlink_usb_state(void *handle)
 
        assert(handle != NULL);
 
-       if (h->transport == HL_TRANSPORT_SWIM) {
-               res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
-               if (res != ERROR_OK)
-                       return TARGET_UNKNOWN;
-
-               res = stlink_swim_resync(handle);
-               if (res != ERROR_OK)
-                       return TARGET_UNKNOWN;
-
-               return ERROR_OK;
-       }
-
        if (h->reconnect_pending) {
                LOG_INFO("Previous state query failed, trying to reconnect");
-               res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
-
+               res = stlink_usb_mode_enter(handle, h->st_mode);
                if (res != ERROR_OK)
                        return TARGET_UNKNOWN;
 
@@ -1818,7 +1803,7 @@ static int stlink_usb_assert_srst(void *handle, int srst)
 
        assert(handle != NULL);
 
-       if (h->transport == HL_TRANSPORT_SWIM)
+       if (h->st_mode == STLINK_MODE_DEBUG_SWIM)
                return stlink_swim_assert_reset(handle, srst);
 
        if (h->version.stlink == 1)
@@ -1895,9 +1880,6 @@ static int stlink_usb_reset(void *handle)
 
        assert(handle != NULL);
 
-       if (h->transport == HL_TRANSPORT_SWIM)
-               return stlink_swim_generate_rst(handle);
-
        stlink_usb_init_buffer(handle, h->rx_ep, 2);
 
        h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
@@ -2319,17 +2301,12 @@ static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
 
        while (count) {
 
-               bytes_remaining = (size != 1) ? \
+               bytes_remaining = (size != 1) ?
                                stlink_max_block_size(h->max_mem_packet, addr) : stlink_usb_block(h);
 
                if (count < bytes_remaining)
                        bytes_remaining = count;
 
-               if (h->transport == HL_TRANSPORT_SWIM) {
-                       retval = stlink_swim_readbytes(handle, addr, bytes_remaining, buffer);
-                       if (retval != ERROR_OK)
-                               return retval;
-               } else
                /*
                 * all stlink support 8/32bit memory read/writes and only from
                 * stlink V2J26 there is support for 16 bit memory read/write.
@@ -2404,17 +2381,12 @@ static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
 
        while (count) {
 
-               bytes_remaining = (size != 1) ? \
+               bytes_remaining = (size != 1) ?
                                stlink_max_block_size(h->max_mem_packet, addr) : stlink_usb_block(h);
 
                if (count < bytes_remaining)
                        bytes_remaining = count;
 
-               if (h->transport == HL_TRANSPORT_SWIM) {
-                       retval = stlink_swim_writebytes(handle, addr, bytes_remaining, buffer);
-                       if (retval != ERROR_OK)
-                               return retval;
-               } else
                /*
                 * all stlink support 8/32bit memory read/writes and only from
                 * stlink V2J26 there is support for 16 bit memory read/write.
@@ -2480,17 +2452,20 @@ static int stlink_usb_override_target(const char *targetname)
 
 static int stlink_speed_swim(void *handle, int khz, bool query)
 {
+       int retval;
+
        /*
-                       we dont care what the khz rate is
                        we only have low and high speed...
                        before changing speed the SWIM_CSR HS bit
                        must be updated
         */
-       if (khz == 0)
-               stlink_swim_speed(handle, 0);
-       else
-               stlink_swim_speed(handle, 1);
-       return khz;
+       if (!query) {
+               retval = stlink_swim_speed(handle, (khz < SWIM_FREQ_HIGH) ? 0 : 1);
+               if (retval != ERROR_OK)
+                       LOG_ERROR("Unable to set adapter speed");
+       }
+
+       return (khz < SWIM_FREQ_HIGH) ? SWIM_FREQ_LOW : SWIM_FREQ_HIGH;
 }
 
 static int stlink_match_speed_map(const struct speed_map *map, unsigned int map_size, int khz, bool query)
@@ -2528,7 +2503,7 @@ static int stlink_match_speed_map(const struct speed_map *map, unsigned int map_
                match = false;
 
        if (!match && query) {
-               LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
+               LOG_INFO("Unable to match requested speed %d kHz, using %d kHz",
                                khz, map[speed_index].speed);
        }
 
@@ -2674,17 +2649,16 @@ static int stlink_speed(void *handle, int khz, bool query)
        if (!handle)
                return khz;
 
-       switch (h->transport) {
-       case HL_TRANSPORT_SWIM:
+       switch (h->st_mode) {
+       case STLINK_MODE_DEBUG_SWIM:
                return stlink_speed_swim(handle, khz, query);
-               break;
-       case HL_TRANSPORT_SWD:
+       case STLINK_MODE_DEBUG_SWD:
                if (h->version.jtag_api == STLINK_JTAG_API_V3)
                        return stlink_speed_v3(handle, false, khz, query);
                else
                        return stlink_speed_swd(handle, khz, query);
                break;
-       case HL_TRANSPORT_JTAG:
+       case STLINK_MODE_DEBUG_JTAG:
                if (h->version.jtag_api == STLINK_JTAG_API_V3)
                        return stlink_speed_v3(handle, true, khz, query);
                else
@@ -2825,7 +2799,7 @@ char *stlink_usb_get_alternate_serial(libusb_device_handle *device,
 }
 
 /** */
-static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
+static int stlink_usb_open(struct hl_interface_param_s *param, enum stlink_mode mode, void **fd)
 {
        int err, retry_count = 1;
        struct stlink_usb_handle_s *h;
@@ -2839,11 +2813,11 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
                return ERROR_FAIL;
        }
 
-       h->transport = param->transport;
+       h->st_mode = mode;
 
        for (unsigned i = 0; param->vid[i]; i++) {
                LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
-                         param->transport, param->vid[i], param->pid[i],
+                         h->st_mode, param->vid[i], param->pid[i],
                          param->serial ? param->serial : "");
        }
 
@@ -2943,16 +2917,16 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
        /* check if mode is supported */
        err = ERROR_OK;
 
-       switch (h->transport) {
-               case HL_TRANSPORT_SWD:
+       switch (h->st_mode) {
+               case STLINK_MODE_DEBUG_SWD:
                        if (h->version.jtag_api == STLINK_JTAG_API_V1)
                                err = ERROR_FAIL;
                        /* fall-through */
-               case HL_TRANSPORT_JTAG:
+               case STLINK_MODE_DEBUG_JTAG:
                        if (h->version.jtag == 0)
                                err = ERROR_FAIL;
                        break;
-               case HL_TRANSPORT_SWIM:
+               case STLINK_MODE_DEBUG_SWIM:
                        if (h->version.swim == 0)
                                err = ERROR_FAIL;
                        break;
@@ -2974,7 +2948,7 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
                goto error_open;
        }
 
-       if (h->transport == HL_TRANSPORT_SWIM) {
+       if (h->st_mode == STLINK_MODE_DEBUG_SWIM) {
                err = stlink_swim_enter(h);
                if (err != ERROR_OK) {
                        LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
@@ -3012,6 +2986,11 @@ error_open:
        return ERROR_FAIL;
 }
 
+static int stlink_usb_hl_open(struct hl_interface_param_s *param, void **fd)
+{
+       return stlink_usb_open(param, stlink_get_mode(param->transport), fd);
+}
+
 int stlink_config_trace(void *handle, bool enabled,
                enum tpiu_pin_protocol pin_protocol, uint32_t port_size,
                unsigned int *trace_freq, unsigned int traceclkin_freq,
@@ -3145,7 +3124,7 @@ static int stlink_write_dap_register(void *handle, unsigned short dap_port,
 /** */
 struct hl_layout_api_s stlink_usb_layout_api = {
        /** */
-       .open = stlink_usb_open,
+       .open = stlink_usb_hl_open,
        /** */
        .close = stlink_usb_close,
        /** */
@@ -3260,7 +3239,6 @@ static int stlink_dap_closeall_ap(void)
 static int stlink_dap_reinit_interface(void)
 {
        int retval;
-       enum stlink_mode mode;
 
        /*
         * On JTAG only, it should be enough to call stlink_usb_reset(). But on
@@ -3270,13 +3248,12 @@ static int stlink_dap_reinit_interface(void)
         * select the mode again.
         */
 
-       mode = stlink_get_mode(stlink_dap_param.transport);
        if (!stlink_dap_handle->reconnect_pending) {
                stlink_dap_handle->reconnect_pending = true;
-               stlink_usb_mode_leave(stlink_dap_handle, mode);
+               stlink_usb_mode_leave(stlink_dap_handle, stlink_dap_handle->st_mode);
        }
 
-       retval = stlink_usb_mode_enter(stlink_dap_handle, mode);
+       retval = stlink_usb_mode_enter(stlink_dap_handle, stlink_dap_handle->st_mode);
        if (retval != ERROR_OK)
                return retval;
 
@@ -3323,7 +3300,7 @@ static int stlink_dap_op_connect(struct adiv5_dap *dap)
        retval = stlink_usb_idcode(stlink_dap_handle, &idcode);
        if (retval == ERROR_OK)
                LOG_INFO("%s %#8.8" PRIx32,
-                       (stlink_dap_handle->transport == HL_TRANSPORT_JTAG) ? "JTAG IDCODE" : "SWD DPIDR",
+                       (stlink_dap_handle->st_mode == STLINK_MODE_DEBUG_JTAG) ? "JTAG IDCODE" : "SWD DPIDR",
                        idcode);
        else
                dap->do_reconnect = true;
@@ -3372,7 +3349,7 @@ static int stlink_dap_op_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
 
        data = data ? : &dummy;
        if (stlink_dap_handle->version.flags & STLINK_F_QUIRK_JTAG_DP_READ
-               && stlink_dap_handle->transport == HL_TRANSPORT_JTAG) {
+               && stlink_dap_handle->st_mode == STLINK_MODE_DEBUG_JTAG) {
                /* Quirk required in JTAG. Read RDBUFF to get the data */
                retval = stlink_read_dap_register(stlink_dap_handle,
                                        STLINK_DEBUG_PORT_ACCESS, reg, &dummy);
@@ -3508,7 +3485,7 @@ static int stlink_dap_op_run(struct adiv5_dap *dap)
        }
 
        if (ctrlstat & SSTICKYERR) {
-               if (stlink_dap_param.transport == HL_TRANSPORT_JTAG)
+               if (stlink_dap_handle->st_mode == STLINK_MODE_DEBUG_JTAG)
                        retval = stlink_dap_op_queue_dp_write(dap, DP_CTRL_STAT,
                                        ctrlstat & (dap->dp_ctrl_stat | SSTICKYERR));
                else
@@ -3542,6 +3519,68 @@ static void stlink_dap_op_quit(struct adiv5_dap *dap)
                LOG_ERROR("Error closing APs");
 }
 
+static int stlink_swim_op_srst(void)
+{
+       return stlink_swim_generate_rst(stlink_dap_handle);
+}
+
+static int stlink_swim_op_read_mem(uint32_t addr, uint32_t size,
+                                                                  uint32_t count, uint8_t *buffer)
+{
+       int retval;
+       uint32_t bytes_remaining;
+
+       LOG_DEBUG_IO("read at 0x%08x len %d*0x%08x", addr, size, count);
+       count *= size;
+
+       while (count) {
+               bytes_remaining = (count > STLINK_DATA_SIZE) ? STLINK_DATA_SIZE : count;
+               retval = stlink_swim_readbytes(stlink_dap_handle, addr, bytes_remaining, buffer);
+               if (retval != ERROR_OK)
+                       return retval;
+
+               buffer += bytes_remaining;
+               addr += bytes_remaining;
+               count -= bytes_remaining;
+       }
+
+       return ERROR_OK;
+}
+
+static int stlink_swim_op_write_mem(uint32_t addr, uint32_t size,
+                                                                       uint32_t count, const uint8_t *buffer)
+{
+       int retval;
+       uint32_t bytes_remaining;
+
+       LOG_DEBUG_IO("write at 0x%08x len %d*0x%08x", addr, size, count);
+       count *= size;
+
+       while (count) {
+               bytes_remaining = (count > STLINK_DATA_SIZE) ? STLINK_DATA_SIZE : count;
+               retval = stlink_swim_writebytes(stlink_dap_handle, addr, bytes_remaining, buffer);
+               if (retval != ERROR_OK)
+                       return retval;
+
+               buffer += bytes_remaining;
+               addr += bytes_remaining;
+               count -= bytes_remaining;
+       }
+
+       return ERROR_OK;
+}
+
+static int stlink_swim_op_reconnect(void)
+{
+       int retval;
+
+       retval = stlink_usb_mode_enter(stlink_dap_handle, STLINK_MODE_DEBUG_SWIM);
+       if (retval != ERROR_OK)
+               return retval;
+
+       return stlink_swim_resync(stlink_dap_handle);
+}
+
 static int stlink_dap_config_trace(bool enabled,
                enum tpiu_pin_protocol pin_protocol, uint32_t port_size,
                unsigned int *trace_freq, unsigned int traceclkin_freq,
@@ -3636,6 +3675,7 @@ static const struct command_registration stlink_dap_command_handlers[] = {
 static int stlink_dap_init(void)
 {
        enum reset_types jtag_reset_config = jtag_get_reset_config();
+       enum stlink_mode mode;
        int retval;
 
        LOG_DEBUG("stlink_dap_init()");
@@ -3648,19 +3688,22 @@ static int stlink_dap_init(void)
        }
 
        if (transport_is_dapdirect_swd())
-               stlink_dap_param.transport = HL_TRANSPORT_SWD;
+               mode = STLINK_MODE_DEBUG_SWD;
        else if (transport_is_dapdirect_jtag())
-               stlink_dap_param.transport = HL_TRANSPORT_JTAG;
+               mode = STLINK_MODE_DEBUG_JTAG;
+       else if (transport_is_swim())
+               mode = STLINK_MODE_DEBUG_SWIM;
        else {
                LOG_ERROR("Unsupported transport");
                return ERROR_FAIL;
        }
 
-       retval = stlink_usb_open(&stlink_dap_param, (void **)&stlink_dap_handle);
+       retval = stlink_usb_open(&stlink_dap_param, mode, (void **)&stlink_dap_handle);
        if (retval != ERROR_OK)
                return retval;
 
-       if (!(stlink_dap_handle->version.flags & STLINK_F_HAS_DAP_REG)) {
+       if ((mode != STLINK_MODE_DEBUG_SWIM) &&
+               !(stlink_dap_handle->version.flags & STLINK_F_HAS_DAP_REG)) {
                LOG_ERROR("ST-Link version does not support DAP direct transport");
                return ERROR_FAIL;
        }
@@ -3732,7 +3775,14 @@ static const struct dap_ops stlink_dap_ops = {
        .quit = stlink_dap_op_quit, /* optional */
 };
 
-static const char *const stlink_dap_transport[] = { "dapdirect_jtag", "dapdirect_swd", NULL };
+static const struct swim_driver stlink_swim_ops = {
+       .srst = stlink_swim_op_srst,
+       .read_mem = stlink_swim_op_read_mem,
+       .write_mem = stlink_swim_op_write_mem,
+       .reconnect = stlink_swim_op_reconnect,
+};
+
+static const char *const stlink_dap_transport[] = { "dapdirect_jtag", "dapdirect_swd", "swim", NULL };
 
 struct adapter_driver stlink_dap_adapter_driver = {
        .name = "st-link",
@@ -3750,4 +3800,5 @@ struct adapter_driver stlink_dap_adapter_driver = {
 
        .dap_jtag_ops = &stlink_dap_ops,
        .dap_swd_ops = &stlink_dap_ops,
+       .swim_ops = &stlink_swim_ops,
 };

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)