hla: Get stlink device back to known state at startup
[fw/openocd] / src / jtag / drivers / stlink_usb.c
index 4154b93c0afac56f4c9946f4fcde5ee75b254eac..47a9b1233fd91f3addbe029a18e8e41309688b4b 100644 (file)
 #define STLINK_TX_EP       (2|ENDPOINT_OUT)
 #define STLINK_TRACE_EP    (3|ENDPOINT_IN)
 #define STLINK_SG_SIZE     (31)
-#define STLINK_DATA_SIZE   (4*128)
+#define STLINK_DATA_SIZE   (4096)
 #define STLINK_CMD_SIZE_V2 (16)
 #define STLINK_CMD_SIZE_V1 (10)
 
+/* the current implementation of the stlink limits
+ * 8bit read/writes to max 64 bytes. */
+#define STLINK_MAX_RW8         (64)
+
 enum stlink_jtag_api_version {
        STLINK_JTAG_API_V1 = 1,
        STLINK_JTAG_API_V2,
@@ -86,6 +90,8 @@ struct stlink_usb_handle_s {
        /** */
        uint8_t databuf[STLINK_DATA_SIZE];
        /** */
+       uint32_t max_mem_packet;
+       /** */
        enum hl_transports transport;
        /** */
        struct stlink_usb_version version;
@@ -203,12 +209,10 @@ static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t siz
 /** */
 static int stlink_usb_xfer_v1_get_status(void *handle)
 {
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        /* read status */
        memset(h->cmdbuf, 0, STLINK_SG_SIZE);
 
@@ -238,12 +242,10 @@ static int stlink_usb_xfer_v1_get_status(void *handle)
 /** */
 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
 {
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
                        STLINK_WRITE_TIMEOUT) != cmdsize) {
                return ERROR_FAIL;
@@ -270,12 +272,10 @@ static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int
 static int stlink_usb_xfer_v1_get_sense(void *handle)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_RX_EP, 16);
 
        h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
@@ -299,12 +299,10 @@ static int stlink_usb_xfer_v1_get_sense(void *handle)
 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
 {
        int err, cmdsize = STLINK_CMD_SIZE_V2;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        if (h->version.stlink == 1)
                cmdsize = STLINK_SG_SIZE;
 
@@ -331,12 +329,10 @@ static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
 /** */
 static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
 {
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        assert(h->version.stlink >= 2);
 
        if (jtag_libusb_bulk_read(h->fd, STLINK_TRACE_EP, (char *)buf,
@@ -351,9 +347,7 @@ static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
 /** */
 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
 {
-       struct stlink_usb_handle_s *h;
-
-       h = (struct stlink_usb_handle_s *)handle;
+       struct stlink_usb_handle_s *h = handle;
 
        /* fill the send buffer */
        strcpy((char *)h->cmdbuf, "USBC");
@@ -370,9 +364,7 @@ static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint3
 /** */
 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
 {
-       struct stlink_usb_handle_s *h;
-
-       h = (struct stlink_usb_handle_s *)handle;
+       struct stlink_usb_handle_s *h = handle;
 
        h->direction = direction;
 
@@ -394,12 +386,10 @@ static int stlink_usb_error_check(void *handle)
 {
        int res;
        const char *err_msg = 0;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        /* TODO: no error checking yet on api V1 */
        if (h->jtag_api == STLINK_JTAG_API_V1)
                h->databuf[0] = STLINK_DEBUG_ERR_OK;
@@ -426,12 +416,10 @@ static int stlink_usb_version(void *handle)
 {
        int res;
        uint16_t v;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_RX_EP, 6);
 
        h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
@@ -470,11 +458,9 @@ static int stlink_usb_version(void *handle)
 
 static int stlink_usb_check_voltage(void *handle, float *target_voltage)
 {
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
        uint32_t adc_results[2];
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        /* only supported by stlink/v2 and for firmware >= 13 */
        if (h->version.stlink == 1 || h->version.jtag < 13)
                return ERROR_COMMAND_NOTFOUND;
@@ -506,12 +492,10 @@ static int stlink_usb_check_voltage(void *handle, float *target_voltage)
 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
 
        h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
@@ -531,12 +515,10 @@ static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
 {
        int res;
        int rx_size = 0;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        /* on api V2 we are able the read the latest command
         * status
         * TODO: we need the test on api V1 too
@@ -587,12 +569,10 @@ static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
 
        switch (type) {
@@ -630,12 +610,10 @@ static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
        int res;
        uint8_t mode;
        enum stlink_mode emode;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        res = stlink_usb_current_mode(handle, &mode);
 
        if (res != ERROR_OK)
@@ -744,12 +722,10 @@ static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
 
        h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
@@ -762,20 +738,18 @@ static int stlink_usb_idcode(void *handle, uint32_t *idcode)
 
        *idcode = le_to_h_u32(h->databuf);
 
-       LOG_DEBUG("IDCODE: 0x%08X", *idcode);
+       LOG_DEBUG("IDCODE: 0x%08" PRIX32, *idcode);
 
        return ERROR_OK;
 }
 
 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
 {
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
        int res;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
 
        h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
@@ -796,12 +770,10 @@ static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *v
 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
 
        h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
@@ -825,12 +797,10 @@ static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
 /** */
 static void stlink_usb_trace_read(void *handle)
 {
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        if (h->trace.enabled && h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
                int res;
 
@@ -849,9 +819,11 @@ static void stlink_usb_trace_read(void *handle)
 
                                res = stlink_usb_read_trace(handle, buf, size);
                                if (res == ERROR_OK) {
-                                       /* Log retrieved trace output */
-                                       if (fwrite(buf, 1, size, h->trace.output_f) > 0)
-                                               fflush(h->trace.output_f);
+                                       if (h->trace.output_f) {
+                                               /* Log retrieved trace output */
+                                               if (fwrite(buf, 1, size, h->trace.output_f) > 0)
+                                                       fflush(h->trace.output_f);
+                                       }
                                }
                        }
                }
@@ -881,12 +853,10 @@ static enum target_state stlink_usb_v2_get_status(void *handle)
 static enum target_state stlink_usb_state(void *handle)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        if (h->jtag_api == STLINK_JTAG_API_V2)
                return stlink_usb_v2_get_status(handle);
 
@@ -912,12 +882,10 @@ static enum target_state stlink_usb_state(void *handle)
 static int stlink_usb_reset(void *handle)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
 
        h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
@@ -944,12 +912,10 @@ static int stlink_usb_reset(void *handle)
 static int stlink_usb_assert_srst(void *handle, int srst)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        if (h->jtag_api == STLINK_JTAG_API_V1)
                return ERROR_COMMAND_NOTFOUND;
 
@@ -972,12 +938,10 @@ static int stlink_configure_target_trace_port(void *handle)
 {
        int res;
        uint32_t reg;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        /* configure the TPI */
 
        /* enable the trace subsystem */
@@ -1029,12 +993,10 @@ out:
 static void stlink_usb_trace_disable(void *handle)
 {
        int res = ERROR_OK;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        assert(h->version.jtag >= STLINK_TRACE_MIN_VERSION);
 
        LOG_DEBUG("Tracing: disable\n");
@@ -1053,12 +1015,10 @@ static void stlink_usb_trace_disable(void *handle)
 static int stlink_usb_trace_enable(void *handle)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        if (h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
                uint32_t trace_hz;
 
@@ -1083,7 +1043,7 @@ static int stlink_usb_trace_enable(void *handle)
 
                if (res == ERROR_OK)  {
                        h->trace.enabled = true;
-                       LOG_DEBUG("Tracing: recording at %uHz\n", trace_hz);
+                       LOG_DEBUG("Tracing: recording at %" PRIu32 "Hz\n", trace_hz);
                }
        } else {
                LOG_ERROR("Tracing is not supported by this version.");
@@ -1097,17 +1057,15 @@ static int stlink_usb_trace_enable(void *handle)
 static int stlink_usb_run(void *handle)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        if (h->jtag_api == STLINK_JTAG_API_V2) {
                res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
 
                /* Try to start tracing, if requested */
-               if (res == ERROR_OK && h->trace.output_f) {
+               if (res == ERROR_OK && h->trace.source_hz) {
                        if (stlink_usb_trace_enable(handle) == ERROR_OK)
                                LOG_DEBUG("Tracing: enabled\n");
                        else
@@ -1134,12 +1092,10 @@ static int stlink_usb_run(void *handle)
 static int stlink_usb_halt(void *handle)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        if (h->jtag_api == STLINK_JTAG_API_V2) {
                res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
 
@@ -1166,12 +1122,10 @@ static int stlink_usb_halt(void *handle)
 static int stlink_usb_step(void *handle)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        if (h->jtag_api == STLINK_JTAG_API_V2) {
                /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
                 * that the cortex-m3 currently does. */
@@ -1197,12 +1151,10 @@ static int stlink_usb_step(void *handle)
 static int stlink_usb_read_regs(void *handle)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_RX_EP, 84);
 
        h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
@@ -1223,12 +1175,10 @@ static int stlink_usb_read_regs(void *handle)
 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_RX_EP, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
 
        h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
@@ -1257,12 +1207,10 @@ static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
 
        h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
@@ -1285,12 +1233,10 @@ static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
 static int stlink_usb_get_rw_status(void *handle)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
        if (h->jtag_api == STLINK_JTAG_API_V1)
                return ERROR_OK;
 
@@ -1313,11 +1259,15 @@ static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
 {
        int res;
        uint16_t read_len = len;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
+       /* max 8bit read/write is 64bytes */
+       if (len > STLINK_MAX_RW8) {
+               LOG_DEBUG("max buffer length exceeded");
+               return ERROR_FAIL;
+       }
 
        stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
 
@@ -1347,11 +1297,15 @@ static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
                           const uint8_t *buffer)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
+       /* max 8bit read/write is 64bytes */
+       if (len > STLINK_MAX_RW8) {
+               LOG_DEBUG("max buffer length exceeded");
+               return ERROR_FAIL;
+       }
 
        stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
 
@@ -1375,13 +1329,15 @@ static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
                          uint8_t *buffer)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
-       len *= 4;
+       /* data must be a multiple of 4 and word aligned */
+       if (len % 4 || addr % 4) {
+               LOG_DEBUG("Invalid data alignment");
+               return ERROR_TARGET_UNALIGNED_ACCESS;
+       }
 
        stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
 
@@ -1407,13 +1363,15 @@ static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
                           const uint8_t *buffer)
 {
        int res;
-       struct stlink_usb_handle_s *h;
+       struct stlink_usb_handle_s *h = handle;
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
-
-       len *= 4;
+       /* data must be a multiple of 4 and word aligned */
+       if (len % 4 || addr % 4) {
+               LOG_DEBUG("Invalid data alignment");
+               return ERROR_TARGET_UNALIGNED_ACCESS;
+       }
 
        stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
 
@@ -1432,30 +1390,140 @@ static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
        return stlink_usb_get_rw_status(handle);
 }
 
+static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
+{
+       uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
+       if (max_tar_block == 0)
+               max_tar_block = 4;
+       return max_tar_block;
+}
+
 static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
                uint32_t count, uint8_t *buffer)
 {
-       if (size == 4)
-               return stlink_usb_read_mem32(handle, addr, count, buffer);
-       else
-               return stlink_usb_read_mem8(handle, addr, count, buffer);
+       int retval = ERROR_OK;
+       uint32_t bytes_remaining;
+       struct stlink_usb_handle_s *h = handle;
+
+       /* calculate byte count */
+       count *= size;
+
+       while (count) {
+
+               bytes_remaining = (size == 4) ? \
+                               stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
+
+               if (count < bytes_remaining)
+                       bytes_remaining = count;
+
+               /* the stlink only supports 8/32bit memory read/writes
+                * honour 32bit, all others will be handled as 8bit access */
+               if (size == 4) {
+
+                       /* When in jtag mode the stlink uses the auto-increment functinality.
+                        * However it expects us to pass the data correctly, this includes
+                        * alignment and any page boundaries. We already do this as part of the
+                        * adi_v5 implementation, but the stlink is a hla adapter and so this
+                        * needs implementiong manually.
+                        * currently this only affects jtag mode, according to ST they do single
+                        * access in SWD mode - but this may change and so we do it for both modes */
+
+                       /* we first need to check for any unaligned bytes */
+                       if (addr % 4) {
+
+                               uint32_t head_bytes = 4 - (addr % 4);
+                               retval = stlink_usb_read_mem8(handle, addr, head_bytes, buffer);
+                               if (retval != ERROR_OK)
+                                       return retval;
+                               buffer += head_bytes;
+                               addr += head_bytes;
+                               count -= head_bytes;
+                               bytes_remaining -= head_bytes;
+                       }
+
+                       if (bytes_remaining % 4)
+                               retval = stlink_usb_read_mem(handle, addr, 1, bytes_remaining, buffer);
+                       else
+                               retval = stlink_usb_read_mem32(handle, addr, bytes_remaining, buffer);
+               } else
+                       retval = stlink_usb_read_mem8(handle, addr, bytes_remaining, buffer);
+
+               if (retval != ERROR_OK)
+                       return retval;
+
+               buffer += bytes_remaining;
+               addr += bytes_remaining;
+               count -= bytes_remaining;
+       }
+
+       return retval;
 }
 
 static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
                uint32_t count, const uint8_t *buffer)
 {
-       if (size == 4)
-               return stlink_usb_write_mem32(handle, addr, count, buffer);
-       else
-               return stlink_usb_write_mem8(handle, addr, count, buffer);
+       int retval = ERROR_OK;
+       uint32_t bytes_remaining;
+       struct stlink_usb_handle_s *h = handle;
+
+       /* calculate byte count */
+       count *= size;
+
+       while (count) {
+
+               bytes_remaining = (size == 4) ? \
+                               stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
+
+               if (count < bytes_remaining)
+                       bytes_remaining = count;
+
+               /* the stlink only supports 8/32bit memory read/writes
+                * honour 32bit, all others will be handled as 8bit access */
+               if (size == 4) {
+
+                       /* When in jtag mode the stlink uses the auto-increment functinality.
+                        * However it expects us to pass the data correctly, this includes
+                        * alignment and any page boundaries. We already do this as part of the
+                        * adi_v5 implementation, but the stlink is a hla adapter and so this
+                        * needs implementiong manually.
+                        * currently this only affects jtag mode, according to ST they do single
+                        * access in SWD mode - but this may change and so we do it for both modes */
+
+                       /* we first need to check for any unaligned bytes */
+                       if (addr % 4) {
+
+                               uint32_t head_bytes = 4 - (addr % 4);
+                               retval = stlink_usb_write_mem8(handle, addr, head_bytes, buffer);
+                               if (retval != ERROR_OK)
+                                       return retval;
+                               buffer += head_bytes;
+                               addr += head_bytes;
+                               count -= head_bytes;
+                               bytes_remaining -= head_bytes;
+                       }
+
+                       if (bytes_remaining % 4)
+                               retval = stlink_usb_write_mem(handle, addr, 1, bytes_remaining, buffer);
+                       else
+                               retval = stlink_usb_write_mem32(handle, addr, bytes_remaining, buffer);
+
+               } else
+                       retval = stlink_usb_write_mem8(handle, addr, bytes_remaining, buffer);
+               if (retval != ERROR_OK)
+                       return retval;
+
+               buffer += bytes_remaining;
+               addr += bytes_remaining;
+               count -= bytes_remaining;
+       }
+
+       return retval;
 }
 
 /** */
 static int stlink_usb_close(void *fd)
 {
-       struct stlink_usb_handle_s *h;
-
-       h = (struct stlink_usb_handle_s *)fd;
+       struct stlink_usb_handle_s *h = fd;
 
        if (h->fd)
                jtag_libusb_close(h->fd);
@@ -1483,9 +1551,6 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 
        h->transport = param->transport;
 
-       /* set max read/write buffer size in bytes */
-       param->max_buffer = 512;
-
        const uint16_t vids[] = { param->vid, 0 };
        const uint16_t pids[] = { param->pid, 0 };
 
@@ -1524,6 +1589,55 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
                        break;
                }
 
+               /* Get the device back to a known mode
+                */
+               int times = 0;
+               uint8_t mode;
+
+               /* In some modes, the device fails to return the current
+                * mode when requested, so try a couple of times
+                */
+               for (;;) {
+                       err = stlink_usb_current_mode(h, &mode);
+
+                       if (err == ERROR_OK) {
+                               LOG_DEBUG("mode %d", mode);
+                               break;
+                       }
+                       LOG_ERROR("current mode failed");
+                       if (++times == 3)
+                               goto error_open;
+               }
+
+               enum stlink_mode emode;
+
+               /* try to exit current mode */
+               switch (mode) {
+               case STLINK_DEV_DFU_MODE:
+                       emode = STLINK_MODE_DFU;
+                       break;
+               case STLINK_DEV_DEBUG_MODE:
+                       emode = STLINK_MODE_DEBUG_SWD;
+                       break;
+               case STLINK_DEV_SWIM_MODE:
+                       emode = STLINK_MODE_DEBUG_SWIM;
+                       break;
+               case STLINK_DEV_BOOTLOADER_MODE:
+               case STLINK_DEV_MASS_MODE:
+               default:
+                       emode = STLINK_MODE_UNKNOWN;
+                       break;
+               }
+
+               LOG_DEBUG("exit mode %d", emode);
+               if (emode != STLINK_MODE_UNKNOWN) {
+
+                       err = stlink_usb_mode_leave(h, emode);
+
+                       if (err != ERROR_OK)
+                               goto error_open;
+               }
+
                /* get the device version */
                err = stlink_usb_version(h);
 
@@ -1587,17 +1701,12 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 
        api = h->version.jtag_api_max;
 
-       /* check that user has not requested certain api version
-        * and if they have check it is supported */
-       if ((param->api != 0) && (param->api <= h->version.jtag_api_max)) {
-               api = param->api;
-               LOG_INFO("using stlink api v%d", api);
-       }
+       LOG_INFO("using stlink api v%d", api);
 
        /* set the used jtag api, this will default to the newest supported version */
        h->jtag_api = api;
 
-       if (h->jtag_api >= 2 && param->trace_f && param->trace_source_hz > 0) {
+       if (h->jtag_api >= 2 && param->trace_source_hz > 0) {
                uint32_t prescale;
 
                prescale = param->trace_source_hz > STLINK_TRACE_MAX_HZ ?
@@ -1616,6 +1725,23 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
                goto error_open;
        }
 
+       /* get cpuid, so we can determine the max page size
+        * start with a safe default */
+       h->max_mem_packet = (1 << 10);
+
+       uint8_t buffer[4];
+       err = stlink_usb_read_mem32(h, CPUID, 4, buffer);
+       if (err == ERROR_OK) {
+               uint32_t cpuid = le_to_h_u32(buffer);
+               int i = (cpuid >> 4) & 0xf;
+               if (i == 4 || i == 3) {
+                       /* Cortex-M3/M4 has 4096 bytes autoincrement range */
+                       h->max_mem_packet = (1 << 12);
+               }
+       }
+
+       LOG_DEBUG("Using TAR autoincrement: %" PRIu32, h->max_mem_packet);
+
        *fd = h;
 
        return ERROR_OK;