hla: move memory read/write functionality to driver
[openocd.git] / src / jtag / drivers / stlink_usb.c
index 1f3fbe2cdc7d7112b7e729084979505e95910902..31f08cbb8eb2687573ad620d4817c73fbea05af4 100644 (file)
 #define ENDPOINT_IN  0x80
 #define ENDPOINT_OUT 0x00
 
+#define STLINK_WRITE_TIMEOUT 1000
+#define STLINK_READ_TIMEOUT 1000
+
 #define STLINK_NULL_EP     0
 #define STLINK_RX_EP       (1|ENDPOINT_IN)
 #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,
@@ -83,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;
@@ -210,7 +219,7 @@ static int stlink_usb_xfer_v1_get_status(void *handle)
        memset(h->cmdbuf, 0, STLINK_SG_SIZE);
 
        if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
-                               13, 1000) != 13)
+                       13, STLINK_READ_TIMEOUT) != 13)
                return ERROR_FAIL;
 
        uint32_t t1;
@@ -242,19 +251,19 @@ static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int
        h = (struct stlink_usb_handle_s *)handle;
 
        if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
-                                  1000) != cmdsize) {
+                       STLINK_WRITE_TIMEOUT) != cmdsize) {
                return ERROR_FAIL;
        }
 
        if (h->direction == STLINK_TX_EP && size) {
                if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
-                                         size, 1000) != size) {
+                               size, STLINK_WRITE_TIMEOUT) != size) {
                        LOG_DEBUG("bulk write failed");
                        return ERROR_FAIL;
                }
        } else if (h->direction == STLINK_RX_EP && size) {
                if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
-                                         size, 1000) != size) {
+                               size, STLINK_READ_TIMEOUT) != size) {
                        LOG_DEBUG("bulk read failed");
                        return ERROR_FAIL;
                }
@@ -337,7 +346,7 @@ static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
        assert(h->version.stlink >= 2);
 
        if (jtag_libusb_bulk_read(h->fd, STLINK_TRACE_EP, (char *)buf,
-                               size, 1000) != size) {
+                       size, STLINK_READ_TIMEOUT) != size) {
                LOG_ERROR("bulk trace read failed");
                return ERROR_FAIL;
        }
@@ -1314,6 +1323,12 @@ static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
 
        assert(handle != NULL);
 
+       /* max 8bit read/write is 64bytes */
+       if (len > STLINK_MAX_RW8) {
+               LOG_DEBUG("max buffer length exceeded");
+               return ERROR_FAIL;
+       }
+
        h = (struct stlink_usb_handle_s *)handle;
 
        stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
@@ -1348,6 +1363,12 @@ static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
 
        assert(handle != NULL);
 
+       /* max 8bit read/write is 64bytes */
+       if (len > STLINK_MAX_RW8) {
+               LOG_DEBUG("max buffer length exceeded");
+               return ERROR_FAIL;
+       }
+
        h = (struct stlink_usb_handle_s *)handle;
 
        stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
@@ -1376,9 +1397,13 @@ static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
+       /* 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;
+       }
 
-       len *= 4;
+       h = (struct stlink_usb_handle_s *)handle;
 
        stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
 
@@ -1408,9 +1433,13 @@ static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
 
        assert(handle != NULL);
 
-       h = (struct stlink_usb_handle_s *)handle;
+       /* 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;
+       }
 
-       len *= 4;
+       h = (struct stlink_usb_handle_s *)handle;
 
        stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
 
@@ -1429,6 +1458,136 @@ 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)
+{
+       int retval = ERROR_OK;
+       uint32_t bytes_remaining;
+       struct stlink_usb_handle_s *h = (struct stlink_usb_handle_s *)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)
+{
+       int retval = ERROR_OK;
+       uint32_t bytes_remaining;
+       struct stlink_usb_handle_s *h = (struct stlink_usb_handle_s *)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)
 {
@@ -1447,7 +1606,7 @@ static int stlink_usb_close(void *fd)
 /** */
 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 {
-       int err;
+       int err, retry_count = 1;
        struct stlink_usb_handle_s *h;
        enum stlink_jtag_api_version api;
 
@@ -1462,44 +1621,75 @@ 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 };
 
        LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
                param->vid, param->pid);
 
-       if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
-               LOG_ERROR("open failed");
-               goto error_open;
-       }
+       /*
+         On certain host USB configurations(e.g. MacBook Air)
+         STLINKv2 dongle seems to have its FW in a funky state if,
+         after plugging it in, you try to use openocd with it more
+         then once (by launching and closing openocd). In cases like
+         that initial attempt to read the FW info via
+         stlink_usb_version will fail and the device has to be reset
+         in order to become operational.
+        */
+       do {
+               if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
+                       LOG_ERROR("open failed");
+                       goto error_open;
+               }
 
-       jtag_libusb_set_configuration(h->fd, 0);
+               jtag_libusb_set_configuration(h->fd, 0);
 
-       if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
-               LOG_DEBUG("claim interface failed");
-               goto error_open;
-       }
+               if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
+                       LOG_DEBUG("claim interface failed");
+                       goto error_open;
+               }
 
-       /* wrap version for first read */
-       switch (param->pid) {
+               /* wrap version for first read */
+               switch (param->pid) {
                case 0x3744:
                        h->version.stlink = 1;
                        break;
                default:
                        h->version.stlink = 2;
                        break;
-       }
+               }
 
-       /* get the device version */
-       err = stlink_usb_version(h);
+               /* get the device version */
+               err = stlink_usb_version(h);
 
-       if (err != ERROR_OK) {
-               LOG_ERROR("read version failed");
-               goto error_open;
-       }
+               if (err == ERROR_OK) {
+                       break;
+               } else if (h->version.stlink == 1 ||
+                          retry_count == 0) {
+                       LOG_ERROR("read version failed");
+                       goto error_open;
+               } else {
+                       err = jtag_libusb_release_interface(h->fd, 0);
+                       if (err != ERROR_OK) {
+                               LOG_ERROR("release interface failed");
+                               goto error_open;
+                       }
+
+                       err = jtag_libusb_reset_device(h->fd);
+                       if (err != ERROR_OK) {
+                               LOG_ERROR("reset device failed");
+                               goto error_open;
+                       }
+
+                       jtag_libusb_close(h->fd);
+                       /*
+                         Give the device one second to settle down and
+                         reenumerate.
+                        */
+                       usleep(1 * 1000 * 1000);
+                       retry_count--;
+               }
+       } while (1);
 
        /* compare usb vid/pid */
        if ((param->vid != h->vid) || (param->pid != h->pid))
@@ -1561,6 +1751,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;
@@ -1598,13 +1805,9 @@ struct hl_layout_api_s stlink_usb_layout_api = {
        /** */
        .write_reg = stlink_usb_write_reg,
        /** */
-       .read_mem8 = stlink_usb_read_mem8,
-       /** */
-       .write_mem8 = stlink_usb_write_mem8,
-       /** */
-       .read_mem32 = stlink_usb_read_mem32,
+       .read_mem = stlink_usb_read_mem,
        /** */
-       .write_mem32 = stlink_usb_write_mem32,
+       .write_mem = stlink_usb_write_mem,
        /** */
        .write_debug_reg = stlink_usb_write_debug_reg
 };

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)