2022-06-26 23:06:45 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
/***************************************************************************
|
2010-03-11 22:11:30 +00:00
|
|
|
* Copyright (C) 2009-2010 by Simon Qian <SimonQian@SimonQian.com> *
|
2008-12-27 22:40:51 +00:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/* Versaloon is a programming tool for multiple MCUs.
|
2010-03-11 22:11:30 +00:00
|
|
|
* It's distributed under GPLv3.
|
2011-07-11 17:48:05 +00:00
|
|
|
* You can find it at http://www.Versaloon.com/.
|
2008-12-27 22:40:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2021-10-07 12:30:43 +00:00
|
|
|
#include <jtag/adapter.h>
|
2009-12-03 12:14:31 +00:00
|
|
|
#include <jtag/interface.h>
|
2009-12-03 12:14:30 +00:00
|
|
|
#include <jtag/commands.h>
|
2014-02-16 09:12:39 +00:00
|
|
|
#include <jtag/swd.h>
|
2014-02-18 07:55:15 +00:00
|
|
|
#include <libusb.h>
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
#include "versaloon/versaloon_include.h"
|
|
|
|
#include "versaloon/versaloon.h"
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
static int vsllink_tms_offset;
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2009-11-13 12:50:26 +00:00
|
|
|
struct pending_scan_result {
|
2010-03-11 22:11:30 +00:00
|
|
|
int src_offset;
|
|
|
|
int dest_offset;
|
2012-02-02 15:08:51 +00:00
|
|
|
int length; /* Number of bits to read */
|
|
|
|
struct scan_command *command; /* Corresponding scan command */
|
2011-07-11 17:48:05 +00:00
|
|
|
uint8_t *ack;
|
2009-06-18 07:07:12 +00:00
|
|
|
uint8_t *buffer;
|
2012-02-02 15:08:51 +00:00
|
|
|
bool last; /* indicate the last scan pending */
|
2009-11-13 12:50:26 +00:00
|
|
|
};
|
2009-01-19 07:13:02 +00:00
|
|
|
|
|
|
|
#define MAX_PENDING_SCAN_RESULTS 256
|
|
|
|
|
|
|
|
static int pending_scan_results_length;
|
2010-03-11 22:21:36 +00:00
|
|
|
static struct pending_scan_result
|
2012-02-02 15:08:51 +00:00
|
|
|
pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
|
2009-01-19 07:13:02 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
/* Queue command functions */
|
2009-04-21 05:29:23 +00:00
|
|
|
static void vsllink_end_state(tap_state_t state);
|
2010-03-11 22:11:30 +00:00
|
|
|
static void vsllink_state_move(void);
|
|
|
|
static void vsllink_path_move(int num_states, tap_state_t *path);
|
2011-07-11 17:48:05 +00:00
|
|
|
static void vsllink_tms(int num_bits, const uint8_t *bits);
|
2009-04-21 05:29:23 +00:00
|
|
|
static void vsllink_runtest(int num_cycles);
|
2010-03-11 22:11:30 +00:00
|
|
|
static void vsllink_stableclocks(int num_cycles, int tms);
|
2010-03-11 22:21:36 +00:00
|
|
|
static void vsllink_scan(bool ir_scan, enum scan_type type,
|
2012-02-02 15:08:51 +00:00
|
|
|
uint8_t *buffer, int scan_size, struct scan_command *command);
|
2019-08-31 09:08:16 +00:00
|
|
|
static int vsllink_reset(int trst, int srst);
|
2008-12-27 22:40:51 +00:00
|
|
|
|
|
|
|
/* VSLLink tap buffer functions */
|
2009-04-21 09:08:43 +00:00
|
|
|
static void vsllink_tap_append_step(int tms, int tdi);
|
2010-03-11 22:11:30 +00:00
|
|
|
static void vsllink_tap_init(void);
|
|
|
|
static int vsllink_tap_execute(void);
|
|
|
|
static void vsllink_tap_ensure_pending(int scans);
|
2010-03-11 22:21:36 +00:00
|
|
|
static void vsllink_tap_append_scan(int length, uint8_t *buffer,
|
|
|
|
struct scan_command *command);
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2014-02-16 09:12:39 +00:00
|
|
|
/* VSLLink SWD functions */
|
2015-11-13 22:48:46 +00:00
|
|
|
static int_least32_t vsllink_swd_frequency(int_least32_t hz);
|
|
|
|
static int vsllink_swd_switch_seq(enum swd_special_seq seq);
|
2014-02-16 09:12:39 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
/* VSLLink lowlevel functions */
|
2009-11-13 13:24:56 +00:00
|
|
|
struct vsllink {
|
2014-02-18 07:55:15 +00:00
|
|
|
struct libusb_context *libusb_ctx;
|
|
|
|
struct libusb_device_handle *usb_device_handle;
|
2009-11-13 13:24:56 +00:00
|
|
|
};
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
static int vsllink_usb_open(struct vsllink *vsllink);
|
2009-11-13 13:24:56 +00:00
|
|
|
static void vsllink_usb_close(struct vsllink *vsllink);
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2009-06-18 07:07:12 +00:00
|
|
|
static void vsllink_debug_buffer(uint8_t *buffer, int length);
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
static int tap_length;
|
|
|
|
static int tap_buffer_size;
|
|
|
|
static uint8_t *tms_buffer;
|
|
|
|
static uint8_t *tdi_buffer;
|
|
|
|
static uint8_t *tdo_buffer;
|
2009-04-21 09:08:43 +00:00
|
|
|
|
2014-02-16 09:12:39 +00:00
|
|
|
static bool swd_mode;
|
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
static struct vsllink *vsllink_handle;
|
2009-04-21 09:08:43 +00:00
|
|
|
|
2009-04-21 05:29:23 +00:00
|
|
|
static int vsllink_execute_queue(void)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2009-11-13 14:15:31 +00:00
|
|
|
struct jtag_command *cmd = jtag_command_queue;
|
2008-12-27 22:40:51 +00:00
|
|
|
int scan_size;
|
|
|
|
enum scan_type type;
|
2009-06-18 07:07:12 +00:00
|
|
|
uint8_t *buffer;
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO("-------------------------------------"
|
2010-03-11 22:21:36 +00:00
|
|
|
" vsllink "
|
|
|
|
"-------------------------------------");
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2021-07-03 16:51:20 +00:00
|
|
|
while (cmd) {
|
2010-03-11 22:21:36 +00:00
|
|
|
switch (cmd->type) {
|
2008-12-27 22:40:51 +00:00
|
|
|
case JTAG_RUNTEST:
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO("runtest %i cycles, end in %s",
|
2012-02-02 15:08:51 +00:00
|
|
|
cmd->cmd.runtest->num_cycles,
|
|
|
|
tap_state_name(cmd->cmd.runtest->end_state));
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-06-02 07:21:44 +00:00
|
|
|
vsllink_end_state(cmd->cmd.runtest->end_state);
|
2008-12-27 22:40:51 +00:00
|
|
|
vsllink_runtest(cmd->cmd.runtest->num_cycles);
|
|
|
|
break;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-06-05 09:30:49 +00:00
|
|
|
case JTAG_TLR_RESET:
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO("statemove end in %s",
|
2012-02-02 15:08:51 +00:00
|
|
|
tap_state_name(cmd->cmd.statemove->end_state));
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2012-02-02 15:08:51 +00:00
|
|
|
vsllink_end_state(cmd->cmd.statemove->end_state);
|
2008-12-27 22:40:51 +00:00
|
|
|
vsllink_state_move();
|
|
|
|
break;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
case JTAG_PATHMOVE:
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO("pathmove: %i states, end in %s",
|
2012-02-02 15:08:51 +00:00
|
|
|
cmd->cmd.pathmove->num_states,
|
|
|
|
tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
|
|
|
|
|
|
|
|
vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
|
2008-12-27 22:40:51 +00:00
|
|
|
break;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
case JTAG_SCAN:
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO("JTAG Scan...");
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2009-06-02 07:21:44 +00:00
|
|
|
vsllink_end_state(cmd->cmd.scan->end_state);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
scan_size = jtag_build_buffer(
|
2012-02-02 15:08:51 +00:00
|
|
|
cmd->cmd.scan, &buffer);
|
2010-03-11 22:21:36 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
if (cmd->cmd.scan->ir_scan)
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO(
|
2012-02-02 15:08:51 +00:00
|
|
|
"JTAG Scan write IR(%d bits), "
|
|
|
|
"end in %s:",
|
|
|
|
scan_size,
|
|
|
|
tap_state_name(cmd->cmd.scan->end_state));
|
2010-03-11 22:21:36 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
else
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO(
|
2012-02-02 15:08:51 +00:00
|
|
|
"JTAG Scan write DR(%d bits), "
|
|
|
|
"end in %s:",
|
|
|
|
scan_size,
|
|
|
|
tap_state_name(cmd->cmd.scan->end_state));
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2016-12-14 00:33:17 +00:00
|
|
|
if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO))
|
|
|
|
vsllink_debug_buffer(buffer, DIV_ROUND_UP(scan_size, 8));
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
type = jtag_scan_type(cmd->cmd.scan);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
vsllink_scan(cmd->cmd.scan->ir_scan,
|
|
|
|
type, buffer, scan_size,
|
|
|
|
cmd->cmd.scan);
|
2008-12-27 22:40:51 +00:00
|
|
|
break;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
case JTAG_SLEEP:
|
2020-08-18 16:55:25 +00:00
|
|
|
LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
|
2008-12-27 22:40:51 +00:00
|
|
|
vsllink_tap_execute();
|
|
|
|
jtag_sleep(cmd->cmd.sleep->us);
|
|
|
|
break;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-01-23 07:02:47 +00:00
|
|
|
case JTAG_STABLECLOCKS:
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO("add %d clocks",
|
2012-02-02 15:08:51 +00:00
|
|
|
cmd->cmd.stableclocks->num_cycles);
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
switch (tap_get_state()) {
|
2009-01-23 07:02:47 +00:00
|
|
|
case TAP_RESET:
|
2010-03-11 22:21:36 +00:00
|
|
|
/* tms must be '1' to stay
|
|
|
|
* n TAP_RESET mode
|
|
|
|
*/
|
2009-01-23 07:02:47 +00:00
|
|
|
scan_size = 1;
|
|
|
|
break;
|
|
|
|
case TAP_DRSHIFT:
|
|
|
|
case TAP_IDLE:
|
|
|
|
case TAP_DRPAUSE:
|
|
|
|
case TAP_IRSHIFT:
|
|
|
|
case TAP_IRPAUSE:
|
2010-03-11 22:21:36 +00:00
|
|
|
/* else, tms should be '0' */
|
2009-01-23 07:02:47 +00:00
|
|
|
scan_size = 0;
|
2010-03-11 22:21:36 +00:00
|
|
|
break;
|
2012-02-02 15:08:51 +00:00
|
|
|
/* above stable states are OK */
|
2009-01-23 07:02:47 +00:00
|
|
|
default:
|
2012-02-02 15:08:51 +00:00
|
|
|
LOG_ERROR("jtag_add_clocks() "
|
|
|
|
"in non-stable state \"%s\"",
|
|
|
|
tap_state_name(tap_get_state())
|
|
|
|
);
|
|
|
|
exit(-1);
|
2009-01-23 07:02:47 +00:00
|
|
|
}
|
2012-02-02 15:08:51 +00:00
|
|
|
vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
|
2009-01-23 07:02:47 +00:00
|
|
|
break;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2012-02-02 15:08:51 +00:00
|
|
|
case JTAG_TMS:
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO("add %d jtag tms",
|
2012-02-02 15:08:51 +00:00
|
|
|
cmd->cmd.tms->num_bits);
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2012-02-02 15:08:51 +00:00
|
|
|
vsllink_tms(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits);
|
|
|
|
break;
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2012-02-02 15:08:51 +00:00
|
|
|
default:
|
|
|
|
LOG_ERROR("BUG: unknown JTAG command type "
|
|
|
|
"encountered: %d", cmd->type);
|
|
|
|
exit(-1);
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
cmd = cmd->next;
|
|
|
|
}
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
return vsllink_tap_execute();
|
|
|
|
}
|
|
|
|
|
2009-04-21 05:29:23 +00:00
|
|
|
static int vsllink_speed(int speed)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2015-03-16 16:11:09 +00:00
|
|
|
if (swd_mode) {
|
2015-11-13 22:48:46 +00:00
|
|
|
vsllink_swd_frequency(speed * 1000);
|
2014-02-16 09:12:39 +00:00
|
|
|
return ERROR_OK;
|
2015-03-16 16:11:09 +00:00
|
|
|
}
|
2014-02-16 09:12:39 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
versaloon_interface.adaptors.jtag_raw.config(0, (uint16_t)speed);
|
|
|
|
return versaloon_interface.adaptors.peripheral_commit();
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
2009-04-21 05:29:23 +00:00
|
|
|
static int vsllink_khz(int khz, int *jtag_speed)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
|
|
|
*jtag_speed = khz;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-04-21 05:29:23 +00:00
|
|
|
static int vsllink_speed_div(int jtag_speed, int *khz)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
|
|
|
*khz = jtag_speed;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
static void vsllink_free_buffer(void)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2020-08-17 08:05:11 +00:00
|
|
|
free(tdi_buffer);
|
|
|
|
tdi_buffer = NULL;
|
|
|
|
|
|
|
|
free(tdo_buffer);
|
|
|
|
tdo_buffer = NULL;
|
|
|
|
|
|
|
|
free(tms_buffer);
|
|
|
|
tms_buffer = NULL;
|
2011-07-11 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int vsllink_quit(void)
|
|
|
|
{
|
|
|
|
versaloon_interface.adaptors.gpio.config(0, GPIO_SRST | GPIO_TRST,
|
|
|
|
0, 0, GPIO_SRST | GPIO_TRST);
|
|
|
|
versaloon_interface.adaptors.gpio.fini(0);
|
2014-02-16 09:12:39 +00:00
|
|
|
|
|
|
|
if (swd_mode)
|
|
|
|
versaloon_interface.adaptors.swd.fini(0);
|
|
|
|
else
|
|
|
|
versaloon_interface.adaptors.jtag_raw.fini(0);
|
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
versaloon_interface.adaptors.peripheral_commit();
|
|
|
|
versaloon_interface.fini();
|
|
|
|
|
|
|
|
vsllink_free_buffer();
|
|
|
|
vsllink_usb_close(vsllink_handle);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2021-12-08 16:46:34 +00:00
|
|
|
libusb_exit(vsllink_handle->libusb_ctx);
|
2014-02-18 07:55:15 +00:00
|
|
|
free(vsllink_handle);
|
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-16 09:12:39 +00:00
|
|
|
static int vsllink_interface_init(void)
|
2011-07-11 17:48:05 +00:00
|
|
|
{
|
2014-02-18 07:55:15 +00:00
|
|
|
vsllink_handle = malloc(sizeof(struct vsllink));
|
2021-07-03 16:51:20 +00:00
|
|
|
if (!vsllink_handle) {
|
2014-02-18 07:55:15 +00:00
|
|
|
LOG_ERROR("unable to allocate memory");
|
|
|
|
return ERROR_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
libusb_init(&vsllink_handle->libusb_ctx);
|
|
|
|
|
2021-07-03 15:18:53 +00:00
|
|
|
if (vsllink_usb_open(vsllink_handle) != ERROR_OK) {
|
2019-05-05 23:03:52 +00:00
|
|
|
LOG_ERROR("Can't find USB JTAG Interface!"
|
2012-02-02 15:08:51 +00:00
|
|
|
"Please check connection and permissions.");
|
2008-12-27 22:40:51 +00:00
|
|
|
return ERROR_JTAG_INIT_FAILED;
|
|
|
|
}
|
2010-03-11 22:21:36 +00:00
|
|
|
LOG_DEBUG("vsllink found on %04X:%04X",
|
2012-02-02 15:08:51 +00:00
|
|
|
versaloon_interface.usb_setting.vid,
|
|
|
|
versaloon_interface.usb_setting.pid);
|
2014-02-18 07:55:15 +00:00
|
|
|
versaloon_usb_device_handle = vsllink_handle->usb_device_handle;
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2021-07-03 15:18:53 +00:00
|
|
|
if (versaloon_interface.init() != ERROR_OK)
|
2011-07-11 17:48:05 +00:00
|
|
|
return ERROR_FAIL;
|
2012-02-02 15:08:51 +00:00
|
|
|
if (versaloon_interface.usb_setting.buf_size < 32) {
|
2011-07-11 17:48:05 +00:00
|
|
|
versaloon_interface.fini();
|
|
|
|
return ERROR_FAIL;
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2014-02-16 09:12:39 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vsllink_init(void)
|
|
|
|
{
|
|
|
|
int retval = vsllink_interface_init();
|
2021-07-03 14:47:35 +00:00
|
|
|
if (retval != ERROR_OK)
|
2014-02-16 09:12:39 +00:00
|
|
|
return retval;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
versaloon_interface.adaptors.gpio.init(0);
|
2014-02-16 09:12:39 +00:00
|
|
|
versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, 0, GPIO_SRST,
|
|
|
|
GPIO_SRST);
|
|
|
|
versaloon_interface.adaptors.delay.delayms(100);
|
|
|
|
versaloon_interface.adaptors.peripheral_commit();
|
|
|
|
|
|
|
|
if (swd_mode) {
|
|
|
|
versaloon_interface.adaptors.gpio.config(0, GPIO_TRST, 0,
|
|
|
|
GPIO_TRST, GPIO_TRST);
|
|
|
|
versaloon_interface.adaptors.swd.init(0);
|
2021-10-07 12:59:29 +00:00
|
|
|
vsllink_swd_frequency(adapter_get_speed_khz() * 1000);
|
2015-11-13 22:48:46 +00:00
|
|
|
vsllink_swd_switch_seq(JTAG_TO_SWD);
|
2014-02-16 09:12:39 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* malloc buffer size for tap */
|
|
|
|
tap_buffer_size = versaloon_interface.usb_setting.buf_size / 2 - 32;
|
|
|
|
vsllink_free_buffer();
|
|
|
|
tdi_buffer = malloc(tap_buffer_size);
|
|
|
|
tdo_buffer = malloc(tap_buffer_size);
|
|
|
|
tms_buffer = malloc(tap_buffer_size);
|
2021-07-03 16:51:20 +00:00
|
|
|
if ((!tdi_buffer) || (!tdo_buffer) || (!tms_buffer)) {
|
2014-02-16 09:12:39 +00:00
|
|
|
vsllink_quit();
|
|
|
|
return ERROR_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
versaloon_interface.adaptors.jtag_raw.init(0);
|
2021-10-07 12:59:29 +00:00
|
|
|
versaloon_interface.adaptors.jtag_raw.config(0, adapter_get_speed_khz());
|
2014-02-16 09:12:39 +00:00
|
|
|
versaloon_interface.adaptors.gpio.config(0, GPIO_SRST | GPIO_TRST,
|
|
|
|
GPIO_TRST, GPIO_SRST, GPIO_SRST);
|
|
|
|
}
|
|
|
|
|
2021-07-03 15:18:53 +00:00
|
|
|
if (versaloon_interface.adaptors.peripheral_commit() != ERROR_OK)
|
2011-07-11 17:48:05 +00:00
|
|
|
return ERROR_FAIL;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
vsllink_reset(0, 0);
|
|
|
|
vsllink_tap_init();
|
2009-04-21 09:08:43 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-02 15:08:51 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* Queue command implementations */
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2009-04-21 05:29:23 +00:00
|
|
|
static void vsllink_end_state(tap_state_t state)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2009-02-03 05:59:17 +00:00
|
|
|
if (tap_is_state_stable(state))
|
|
|
|
tap_set_end_state(state);
|
2010-03-11 22:21:36 +00:00
|
|
|
else {
|
2008-12-27 22:40:51 +00:00
|
|
|
LOG_ERROR("BUG: %i is not a valid end state", state);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Goes to the end state. */
|
2010-03-11 22:11:30 +00:00
|
|
|
static void vsllink_state_move(void)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2010-03-11 22:11:30 +00:00
|
|
|
int i;
|
2010-03-11 22:21:36 +00:00
|
|
|
uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
|
2012-02-02 15:08:51 +00:00
|
|
|
tap_get_end_state());
|
2010-03-11 22:21:36 +00:00
|
|
|
uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(),
|
2012-02-02 15:08:51 +00:00
|
|
|
tap_get_end_state());
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
for (i = 0; i < tms_scan_bits; i++)
|
|
|
|
vsllink_tap_append_step((tms_scan >> i) & 1, 0);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-02-03 05:59:17 +00:00
|
|
|
tap_set_state(tap_get_end_state());
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
static void vsllink_path_move(int num_states, tap_state_t *path)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2010-03-11 22:21:36 +00:00
|
|
|
for (int i = 0; i < num_states; i++) {
|
2010-03-11 22:11:30 +00:00
|
|
|
if (path[i] == tap_state_transition(tap_get_state(), false))
|
|
|
|
vsllink_tap_append_step(0, 0);
|
|
|
|
else if (path[i] == tap_state_transition(tap_get_state(), true))
|
|
|
|
vsllink_tap_append_step(1, 0);
|
2010-03-11 22:21:36 +00:00
|
|
|
else {
|
|
|
|
LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
|
2012-02-02 15:08:51 +00:00
|
|
|
tap_state_name(tap_get_state()),
|
|
|
|
tap_state_name(path[i]));
|
2008-12-27 22:40:51 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
tap_set_state(path[i]);
|
2009-04-21 09:08:43 +00:00
|
|
|
}
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-04-21 09:08:43 +00:00
|
|
|
tap_set_end_state(tap_get_state());
|
|
|
|
}
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
static void vsllink_tms(int num_bits, const uint8_t *bits)
|
|
|
|
{
|
2012-02-02 15:08:51 +00:00
|
|
|
for (int i = 0; i < num_bits; i++)
|
2011-07-11 17:48:05 +00:00
|
|
|
vsllink_tap_append_step((bits[i / 8] >> (i % 8)) & 1, 0);
|
|
|
|
}
|
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
static void vsllink_stableclocks(int num_cycles, int tms)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2010-03-11 22:21:36 +00:00
|
|
|
while (num_cycles > 0) {
|
2010-03-11 22:11:30 +00:00
|
|
|
vsllink_tap_append_step(tms, 0);
|
|
|
|
num_cycles--;
|
2009-04-21 09:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-23 07:02:47 +00:00
|
|
|
|
2009-04-21 05:29:23 +00:00
|
|
|
static void vsllink_runtest(int num_cycles)
|
2009-01-23 07:02:47 +00:00
|
|
|
{
|
2009-02-03 05:59:17 +00:00
|
|
|
tap_state_t saved_end_state = tap_get_end_state();
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
if (tap_get_state() != TAP_IDLE) {
|
|
|
|
/* enter IDLE state */
|
2009-01-23 07:02:47 +00:00
|
|
|
vsllink_end_state(TAP_IDLE);
|
|
|
|
vsllink_state_move();
|
|
|
|
}
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-01-23 07:02:47 +00:00
|
|
|
vsllink_stableclocks(num_cycles, 0);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2012-02-02 15:08:51 +00:00
|
|
|
/* post-process */
|
|
|
|
/* set end_state */
|
2009-01-19 07:13:02 +00:00
|
|
|
vsllink_end_state(saved_end_state);
|
2010-03-11 22:11:30 +00:00
|
|
|
if (tap_get_end_state() != tap_get_end_state())
|
2009-01-19 07:13:02 +00:00
|
|
|
vsllink_state_move();
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
static void vsllink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
|
2012-02-02 15:08:51 +00:00
|
|
|
int scan_size, struct scan_command *command)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2009-02-03 05:59:17 +00:00
|
|
|
tap_state_t saved_end_state;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-02-03 05:59:17 +00:00
|
|
|
saved_end_state = tap_get_end_state();
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
/* Move to appropriate scan state */
|
|
|
|
vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-02-03 05:59:17 +00:00
|
|
|
if (tap_get_state() != tap_get_end_state())
|
2010-03-11 22:11:30 +00:00
|
|
|
vsllink_state_move();
|
2009-04-21 09:08:43 +00:00
|
|
|
vsllink_end_state(saved_end_state);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-04-21 09:08:43 +00:00
|
|
|
/* Scan */
|
2010-03-11 22:11:30 +00:00
|
|
|
vsllink_tap_append_scan(scan_size, buffer, command);
|
|
|
|
|
|
|
|
/* Goto Pause and record position to insert tms:0 */
|
|
|
|
vsllink_tap_append_step(0, 0);
|
|
|
|
vsllink_tms_offset = tap_length;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-04-21 09:08:43 +00:00
|
|
|
tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-04-21 09:08:43 +00:00
|
|
|
if (tap_get_state() != tap_get_end_state())
|
|
|
|
vsllink_state_move();
|
|
|
|
}
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2019-08-31 09:08:16 +00:00
|
|
|
static int vsllink_reset(int trst, int srst)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
|
|
|
LOG_DEBUG("trst: %i, srst: %i", trst, srst);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
if (!srst)
|
|
|
|
versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, 0, GPIO_SRST, GPIO_SRST);
|
|
|
|
else
|
|
|
|
versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, GPIO_SRST, 0, 0);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2014-02-16 09:12:39 +00:00
|
|
|
if (!swd_mode) {
|
|
|
|
if (!trst)
|
|
|
|
versaloon_interface.adaptors.gpio.out(0, GPIO_TRST, GPIO_TRST);
|
|
|
|
else
|
|
|
|
versaloon_interface.adaptors.gpio.out(0, GPIO_TRST, 0);
|
|
|
|
}
|
|
|
|
|
2019-08-31 09:08:16 +00:00
|
|
|
return versaloon_interface.adaptors.peripheral_commit();
|
2009-04-21 09:08:43 +00:00
|
|
|
}
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2009-11-10 07:56:52 +00:00
|
|
|
COMMAND_HANDLER(vsllink_handle_usb_vid_command)
|
2009-04-21 09:08:43 +00:00
|
|
|
{
|
2012-02-02 15:08:51 +00:00
|
|
|
if (CMD_ARGC != 1)
|
2011-12-16 06:48:39 +00:00
|
|
|
return ERROR_COMMAND_SYNTAX_ERROR;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0],
|
|
|
|
versaloon_interface.usb_setting.vid);
|
2009-10-23 04:05:50 +00:00
|
|
|
return ERROR_OK;
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
2009-11-10 07:56:52 +00:00
|
|
|
COMMAND_HANDLER(vsllink_handle_usb_pid_command)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2012-02-02 15:08:51 +00:00
|
|
|
if (CMD_ARGC != 1)
|
2011-12-16 06:48:39 +00:00
|
|
|
return ERROR_COMMAND_SYNTAX_ERROR;
|
2011-07-11 17:48:05 +00:00
|
|
|
COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0],
|
|
|
|
versaloon_interface.usb_setting.pid);
|
2009-10-23 04:05:50 +00:00
|
|
|
return ERROR_OK;
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
2009-11-10 07:56:52 +00:00
|
|
|
COMMAND_HANDLER(vsllink_handle_usb_bulkin_command)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2012-02-02 15:08:51 +00:00
|
|
|
if (CMD_ARGC != 1)
|
2011-12-16 06:48:39 +00:00
|
|
|
return ERROR_COMMAND_SYNTAX_ERROR;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
|
|
|
|
versaloon_interface.usb_setting.ep_in);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
versaloon_interface.usb_setting.ep_in |= 0x80;
|
2009-10-23 04:05:50 +00:00
|
|
|
|
|
|
|
return ERROR_OK;
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
2009-11-10 07:56:52 +00:00
|
|
|
COMMAND_HANDLER(vsllink_handle_usb_bulkout_command)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2012-02-02 15:08:51 +00:00
|
|
|
if (CMD_ARGC != 1)
|
2011-12-16 06:48:39 +00:00
|
|
|
return ERROR_COMMAND_SYNTAX_ERROR;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
|
|
|
|
versaloon_interface.usb_setting.ep_out);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
versaloon_interface.usb_setting.ep_out &= ~0x80;
|
2009-10-23 04:05:50 +00:00
|
|
|
|
|
|
|
return ERROR_OK;
|
2009-04-21 09:08:43 +00:00
|
|
|
}
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2009-11-10 07:56:52 +00:00
|
|
|
COMMAND_HANDLER(vsllink_handle_usb_interface_command)
|
2009-04-21 09:08:43 +00:00
|
|
|
{
|
2012-02-02 15:08:51 +00:00
|
|
|
if (CMD_ARGC != 1)
|
2011-12-16 06:48:39 +00:00
|
|
|
return ERROR_COMMAND_SYNTAX_ERROR;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
|
|
|
|
versaloon_interface.usb_setting.interface);
|
2009-10-23 04:05:50 +00:00
|
|
|
return ERROR_OK;
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 15:08:51 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* VSLLink tap functions */
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
static void vsllink_tap_init(void)
|
2009-04-21 09:08:43 +00:00
|
|
|
{
|
|
|
|
tap_length = 0;
|
|
|
|
pending_scan_results_length = 0;
|
2010-03-11 22:11:30 +00:00
|
|
|
vsllink_tms_offset = 0;
|
2009-04-21 09:08:43 +00:00
|
|
|
}
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
static void vsllink_tap_ensure_pending(int scans)
|
2009-04-21 09:08:43 +00:00
|
|
|
{
|
2010-03-11 22:21:36 +00:00
|
|
|
int available_scans =
|
2012-02-02 15:08:51 +00:00
|
|
|
MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
if (scans > available_scans)
|
2008-12-27 22:40:51 +00:00
|
|
|
vsllink_tap_execute();
|
|
|
|
}
|
|
|
|
|
2009-04-21 09:08:43 +00:00
|
|
|
static void vsllink_tap_append_step(int tms, int tdi)
|
|
|
|
{
|
2010-06-17 15:12:48 +00:00
|
|
|
int index_var = tap_length / 8;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
int bit_index = tap_length % 8;
|
|
|
|
uint8_t bit = 1 << bit_index;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
if (tms)
|
2010-06-17 15:12:48 +00:00
|
|
|
tms_buffer[index_var] |= bit;
|
2009-04-21 09:08:43 +00:00
|
|
|
else
|
2010-06-17 15:12:48 +00:00
|
|
|
tms_buffer[index_var] &= ~bit;
|
2009-04-21 09:08:43 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
if (tdi)
|
2010-06-17 15:12:48 +00:00
|
|
|
tdi_buffer[index_var] |= bit;
|
2008-12-27 22:40:51 +00:00
|
|
|
else
|
2010-06-17 15:12:48 +00:00
|
|
|
tdi_buffer[index_var] &= ~bit;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
tap_length++;
|
2010-03-11 22:21:36 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
if (tap_buffer_size * 8 <= tap_length)
|
|
|
|
vsllink_tap_execute();
|
2009-04-21 09:08:43 +00:00
|
|
|
}
|
2010-03-11 22:11:30 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
static void vsllink_tap_append_scan(int length, uint8_t *buffer,
|
2012-02-02 15:08:51 +00:00
|
|
|
struct scan_command *command)
|
2009-04-21 09:08:43 +00:00
|
|
|
{
|
2009-11-13 12:50:26 +00:00
|
|
|
struct pending_scan_result *pending_scan_result;
|
2009-05-27 12:06:51 +00:00
|
|
|
int len_tmp, len_all, i;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-05-27 12:06:51 +00:00
|
|
|
len_all = 0;
|
2010-03-11 22:21:36 +00:00
|
|
|
while (len_all < length) {
|
2010-03-11 22:11:30 +00:00
|
|
|
vsllink_tap_ensure_pending(1);
|
2010-03-11 22:21:36 +00:00
|
|
|
pending_scan_result =
|
2012-02-02 15:08:51 +00:00
|
|
|
&pending_scan_results_buffer[
|
|
|
|
pending_scan_results_length];
|
2010-03-11 22:11:30 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
if ((length - len_all) > (tap_buffer_size * 8 - tap_length)) {
|
|
|
|
/* Use all memory available
|
2010-03-11 22:11:30 +00:00
|
|
|
vsllink_tap_append_step will commit automatically */
|
|
|
|
len_tmp = tap_buffer_size * 8 - tap_length;
|
|
|
|
pending_scan_result->last = false;
|
2010-03-11 22:21:36 +00:00
|
|
|
} else {
|
2009-05-27 12:06:51 +00:00
|
|
|
len_tmp = length - len_all;
|
2010-03-11 22:11:30 +00:00
|
|
|
pending_scan_result->last = true;
|
2009-05-27 12:06:51 +00:00
|
|
|
}
|
2010-03-11 22:11:30 +00:00
|
|
|
pending_scan_result->src_offset = tap_length;
|
|
|
|
pending_scan_result->dest_offset = len_all;
|
2009-05-27 12:06:51 +00:00
|
|
|
pending_scan_result->length = len_tmp;
|
|
|
|
pending_scan_result->command = command;
|
2010-03-11 22:11:30 +00:00
|
|
|
pending_scan_result->buffer = buffer;
|
|
|
|
pending_scan_results_length++;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
for (i = 0; i < len_tmp; i++) {
|
|
|
|
vsllink_tap_append_step(((len_all + i) < length-1
|
2012-02-02 15:08:51 +00:00
|
|
|
? 0 : 1),
|
|
|
|
(buffer[(len_all + i)/8]
|
|
|
|
>> ((len_all + i)%8)) & 1);
|
2009-05-27 12:06:51 +00:00
|
|
|
}
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2009-05-27 12:06:51 +00:00
|
|
|
len_all += len_tmp;
|
2009-04-21 09:08:43 +00:00
|
|
|
}
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
static int vsllink_jtag_execute(void)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
2008-12-27 22:43:56 +00:00
|
|
|
int i;
|
2008-12-27 22:40:51 +00:00
|
|
|
int result;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:11:30 +00:00
|
|
|
if (tap_length <= 0)
|
|
|
|
return ERROR_OK;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2012-02-02 15:08:51 +00:00
|
|
|
versaloon_interface.adaptors.jtag_raw.execute(0, tdi_buffer, tms_buffer,
|
2011-07-11 17:48:05 +00:00
|
|
|
tdo_buffer, tap_length);
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
result = versaloon_interface.adaptors.peripheral_commit();
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
if (result == ERROR_OK) {
|
2010-03-11 22:21:36 +00:00
|
|
|
for (i = 0; i < pending_scan_results_length; i++) {
|
|
|
|
struct pending_scan_result *pending_scan_result =
|
|
|
|
&pending_scan_results_buffer[i];
|
2010-03-11 22:11:30 +00:00
|
|
|
uint8_t *buffer = pending_scan_result->buffer;
|
|
|
|
int length = pending_scan_result->length;
|
|
|
|
int src_first = pending_scan_result->src_offset;
|
|
|
|
int dest_first = pending_scan_result->dest_offset;
|
|
|
|
bool last = pending_scan_result->last;
|
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
struct scan_command *command;
|
|
|
|
|
|
|
|
command = pending_scan_result->command;
|
2011-07-11 17:48:05 +00:00
|
|
|
buf_set_buf(tdo_buffer, src_first, buffer, dest_first, length);
|
2010-03-11 22:11:30 +00:00
|
|
|
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO(
|
2011-07-11 17:48:05 +00:00
|
|
|
"JTAG scan read(%d bits, from src %d bits to dest %d bits):",
|
|
|
|
length, src_first, dest_first);
|
2016-12-14 00:33:17 +00:00
|
|
|
if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO))
|
|
|
|
vsllink_debug_buffer(buffer + dest_first / 8, DIV_ROUND_UP(length, 7));
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
if (last) {
|
|
|
|
if (jtag_read_buffer(buffer, command)
|
2012-02-02 15:08:51 +00:00
|
|
|
!= ERROR_OK) {
|
2009-04-21 09:08:43 +00:00
|
|
|
vsllink_tap_init();
|
|
|
|
return ERROR_JTAG_QUEUE_FAILED;
|
|
|
|
}
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2020-08-17 08:05:11 +00:00
|
|
|
free(pending_scan_result->buffer);
|
2009-04-21 09:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-11 22:21:36 +00:00
|
|
|
} else {
|
2011-07-11 17:48:05 +00:00
|
|
|
LOG_ERROR("vsllink_jtag_execute failure");
|
2010-03-11 22:11:30 +00:00
|
|
|
return ERROR_JTAG_QUEUE_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
vsllink_tap_init();
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2008-12-27 22:40:51 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
static int vsllink_tap_execute(void)
|
|
|
|
{
|
2014-02-16 09:12:39 +00:00
|
|
|
if (swd_mode)
|
|
|
|
return ERROR_OK;
|
|
|
|
|
2011-07-11 17:48:05 +00:00
|
|
|
return vsllink_jtag_execute();
|
|
|
|
}
|
|
|
|
|
2014-02-16 09:12:39 +00:00
|
|
|
static int vsllink_swd_init(void)
|
|
|
|
{
|
|
|
|
LOG_INFO("VSLLink SWD mode enabled");
|
|
|
|
swd_mode = true;
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-13 22:48:46 +00:00
|
|
|
static int_least32_t vsllink_swd_frequency(int_least32_t hz)
|
2014-02-16 09:12:39 +00:00
|
|
|
{
|
|
|
|
const int_least32_t delay2hz[] = {
|
|
|
|
1850000, 235000, 130000, 102000, 85000, 72000
|
|
|
|
};
|
|
|
|
|
|
|
|
if (hz > 0) {
|
|
|
|
uint16_t delay = UINT16_MAX;
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < ARRAY_SIZE(delay2hz); i++) {
|
|
|
|
if (hz >= delay2hz[i]) {
|
|
|
|
hz = delay2hz[i];
|
|
|
|
delay = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delay == UINT16_MAX)
|
|
|
|
delay = (500000 / hz) - 1;
|
|
|
|
|
|
|
|
/* Calculate retry count after a WAIT response. This will give
|
|
|
|
* a retry timeout at about ~250 ms. 54 is the number of bits
|
|
|
|
* found in a transaction. */
|
|
|
|
uint16_t retry_count = 250 * hz / 1000 / 54;
|
|
|
|
|
|
|
|
LOG_DEBUG("SWD delay: %d, retry count: %d", delay, retry_count);
|
|
|
|
|
|
|
|
versaloon_interface.adaptors.swd.config(0, 2, retry_count, delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hz;
|
|
|
|
}
|
|
|
|
|
2015-11-13 22:48:46 +00:00
|
|
|
static int vsllink_swd_switch_seq(enum swd_special_seq seq)
|
2014-02-16 09:12:39 +00:00
|
|
|
{
|
|
|
|
switch (seq) {
|
|
|
|
case LINE_RESET:
|
|
|
|
LOG_DEBUG("SWD line reset");
|
|
|
|
versaloon_interface.adaptors.swd.seqout(0, swd_seq_line_reset,
|
|
|
|
swd_seq_line_reset_len);
|
|
|
|
break;
|
|
|
|
case JTAG_TO_SWD:
|
|
|
|
LOG_DEBUG("JTAG-to-SWD");
|
|
|
|
versaloon_interface.adaptors.swd.seqout(0, swd_seq_jtag_to_swd,
|
|
|
|
swd_seq_jtag_to_swd_len);
|
|
|
|
break;
|
|
|
|
case SWD_TO_JTAG:
|
|
|
|
LOG_DEBUG("SWD-to-JTAG");
|
|
|
|
versaloon_interface.adaptors.swd.seqout(0, swd_seq_swd_to_jtag,
|
|
|
|
swd_seq_swd_to_jtag_len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR("Sequence %d not supported", seq);
|
|
|
|
return ERROR_FAIL;
|
|
|
|
}
|
|
|
|
|
2014-09-16 13:10:13 +00:00
|
|
|
return ERROR_OK;
|
2014-02-16 09:12:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 22:48:46 +00:00
|
|
|
static void vsllink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
|
2014-02-16 09:12:39 +00:00
|
|
|
{
|
2014-09-16 13:10:13 +00:00
|
|
|
versaloon_interface.adaptors.swd.transact(0, cmd, value, NULL);
|
2014-02-16 09:12:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 22:48:46 +00:00
|
|
|
static void vsllink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
|
2014-02-16 09:12:39 +00:00
|
|
|
{
|
2014-09-16 13:10:13 +00:00
|
|
|
versaloon_interface.adaptors.swd.transact(0, cmd, &value, NULL);
|
2014-02-16 09:12:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 22:48:46 +00:00
|
|
|
static int vsllink_swd_run_queue(void)
|
2014-02-16 09:12:39 +00:00
|
|
|
{
|
2014-09-16 13:10:13 +00:00
|
|
|
return versaloon_interface.adaptors.peripheral_commit();
|
2014-02-16 09:12:39 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 15:08:51 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* VSLLink USB low-level functions */
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
static int vsllink_check_usb_strings(
|
|
|
|
struct libusb_device_handle *usb_device_handle,
|
|
|
|
struct libusb_device_descriptor *usb_desc)
|
2011-07-11 17:48:05 +00:00
|
|
|
{
|
2014-02-18 07:55:15 +00:00
|
|
|
char desc_string[256];
|
|
|
|
int retval;
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2021-10-07 20:39:45 +00:00
|
|
|
if (adapter_get_required_serial()) {
|
2014-02-18 07:55:15 +00:00
|
|
|
retval = libusb_get_string_descriptor_ascii(usb_device_handle,
|
|
|
|
usb_desc->iSerialNumber, (unsigned char *)desc_string,
|
|
|
|
sizeof(desc_string));
|
|
|
|
if (retval < 0)
|
|
|
|
return ERROR_FAIL;
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2021-10-07 20:39:45 +00:00
|
|
|
if (strncmp(desc_string, adapter_get_required_serial(),
|
2014-02-18 07:55:15 +00:00
|
|
|
sizeof(desc_string)))
|
|
|
|
return ERROR_FAIL;
|
2011-07-11 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
retval = libusb_get_string_descriptor_ascii(usb_device_handle,
|
|
|
|
usb_desc->iProduct, (unsigned char *)desc_string,
|
|
|
|
sizeof(desc_string));
|
|
|
|
if (retval < 0)
|
|
|
|
return ERROR_FAIL;
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2021-07-03 20:10:55 +00:00
|
|
|
if (!strstr(desc_string, "Versaloon"))
|
2014-02-18 07:55:15 +00:00
|
|
|
return ERROR_FAIL;
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
static int vsllink_usb_open(struct vsllink *vsllink)
|
|
|
|
{
|
|
|
|
ssize_t num_devices, i;
|
2021-04-14 21:44:57 +00:00
|
|
|
struct libusb_device **usb_devices;
|
2014-02-18 07:55:15 +00:00
|
|
|
struct libusb_device_descriptor usb_desc;
|
|
|
|
struct libusb_device_handle *usb_device_handle;
|
|
|
|
int retval;
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
num_devices = libusb_get_device_list(vsllink->libusb_ctx, &usb_devices);
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
if (num_devices <= 0)
|
|
|
|
return ERROR_FAIL;
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
for (i = 0; i < num_devices; i++) {
|
2021-04-14 21:44:57 +00:00
|
|
|
struct libusb_device *device = usb_devices[i];
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
retval = libusb_get_device_descriptor(device, &usb_desc);
|
|
|
|
if (retval != 0)
|
|
|
|
continue;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
if (usb_desc.idVendor != versaloon_interface.usb_setting.vid ||
|
|
|
|
usb_desc.idProduct != versaloon_interface.usb_setting.pid)
|
|
|
|
continue;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
retval = libusb_open(device, &usb_device_handle);
|
|
|
|
if (retval != 0)
|
|
|
|
continue;
|
2008-12-27 22:40:51 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
retval = vsllink_check_usb_strings(usb_device_handle, &usb_desc);
|
2021-07-03 14:47:35 +00:00
|
|
|
if (retval == ERROR_OK)
|
2014-02-18 07:55:15 +00:00
|
|
|
break;
|
2009-04-21 09:08:43 +00:00
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
libusb_close(usb_device_handle);
|
2009-04-21 09:08:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
libusb_free_device_list(usb_devices, 1);
|
|
|
|
|
|
|
|
if (i == num_devices)
|
|
|
|
return ERROR_FAIL;
|
|
|
|
|
|
|
|
retval = libusb_claim_interface(usb_device_handle,
|
|
|
|
versaloon_interface.usb_setting.interface);
|
|
|
|
if (retval != 0) {
|
|
|
|
LOG_ERROR("unable to claim interface");
|
|
|
|
libusb_close(usb_device_handle);
|
|
|
|
return ERROR_FAIL;
|
2009-04-21 09:08:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 07:55:15 +00:00
|
|
|
vsllink->usb_device_handle = usb_device_handle;
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vsllink_usb_close(struct vsllink *vsllink)
|
|
|
|
{
|
|
|
|
libusb_release_interface(vsllink->usb_device_handle,
|
|
|
|
versaloon_interface.usb_setting.interface);
|
|
|
|
libusb_close(vsllink->usb_device_handle);
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define BYTES_PER_LINE 16
|
|
|
|
|
2009-06-18 07:07:12 +00:00
|
|
|
static void vsllink_debug_buffer(uint8_t *buffer, int length)
|
2008-12-27 22:40:51 +00:00
|
|
|
{
|
|
|
|
char line[81];
|
|
|
|
char s[4];
|
|
|
|
int i;
|
|
|
|
int j;
|
2009-06-04 00:56:41 +00:00
|
|
|
|
2010-03-11 22:21:36 +00:00
|
|
|
for (i = 0; i < length; i += BYTES_PER_LINE) {
|
2016-12-14 00:33:17 +00:00
|
|
|
snprintf(line, 5, "%04x", i & 0xffff);
|
2010-03-11 22:21:36 +00:00
|
|
|
for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
|
2008-12-27 22:40:51 +00:00
|
|
|
snprintf(s, 4, " %02x", buffer[j]);
|
|
|
|
strcat(line, s);
|
|
|
|
}
|
2016-12-14 00:33:17 +00:00
|
|
|
LOG_DEBUG_IO("%s", line);
|
2008-12-27 22:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-10 05:21:06 +00:00
|
|
|
|
2021-07-30 16:22:41 +00:00
|
|
|
static const struct command_registration vsllink_subcommand_handlers[] = {
|
2009-11-21 05:06:28 +00:00
|
|
|
{
|
2021-07-30 16:22:41 +00:00
|
|
|
.name = "usb_vid",
|
2009-11-21 05:06:28 +00:00
|
|
|
.handler = &vsllink_handle_usb_vid_command,
|
|
|
|
.mode = COMMAND_CONFIG,
|
2019-03-30 07:42:36 +00:00
|
|
|
.help = "Set USB VID",
|
|
|
|
.usage = "<vid>",
|
2009-11-21 05:06:28 +00:00
|
|
|
},
|
|
|
|
{
|
2021-07-30 16:22:41 +00:00
|
|
|
.name = "usb_pid",
|
2009-11-21 05:06:28 +00:00
|
|
|
.handler = &vsllink_handle_usb_pid_command,
|
|
|
|
.mode = COMMAND_CONFIG,
|
2019-03-30 07:42:36 +00:00
|
|
|
.help = "Set USB PID",
|
|
|
|
.usage = "<pid>",
|
2009-11-21 05:06:28 +00:00
|
|
|
},
|
|
|
|
{
|
2021-07-30 16:22:41 +00:00
|
|
|
.name = "usb_bulkin",
|
2009-11-21 05:06:28 +00:00
|
|
|
.handler = &vsllink_handle_usb_bulkin_command,
|
|
|
|
.mode = COMMAND_CONFIG,
|
2019-03-30 07:42:36 +00:00
|
|
|
.help = "Set USB input endpoint",
|
|
|
|
.usage = "<ep_in>",
|
2009-11-21 05:06:28 +00:00
|
|
|
},
|
|
|
|
{
|
2021-07-30 16:22:41 +00:00
|
|
|
.name = "usb_bulkout",
|
2009-11-21 05:06:28 +00:00
|
|
|
.handler = &vsllink_handle_usb_bulkout_command,
|
|
|
|
.mode = COMMAND_CONFIG,
|
2019-03-30 07:42:36 +00:00
|
|
|
.help = "Set USB output endpoint",
|
|
|
|
.usage = "<ep_out>",
|
2009-11-21 05:06:28 +00:00
|
|
|
},
|
|
|
|
{
|
2021-07-30 16:22:41 +00:00
|
|
|
.name = "usb_interface",
|
2009-11-21 05:06:28 +00:00
|
|
|
.handler = &vsllink_handle_usb_interface_command,
|
|
|
|
.mode = COMMAND_CONFIG,
|
2019-03-30 07:42:36 +00:00
|
|
|
.help = "Set USB output interface",
|
|
|
|
.usage = "<interface>",
|
2009-11-21 05:06:28 +00:00
|
|
|
},
|
|
|
|
COMMAND_REGISTRATION_DONE
|
|
|
|
};
|
|
|
|
|
2021-07-30 16:22:41 +00:00
|
|
|
static const struct command_registration vsllink_command_handlers[] = {
|
|
|
|
{
|
|
|
|
.name = "vsllink",
|
|
|
|
.mode = COMMAND_ANY,
|
|
|
|
.help = "perform vsllink management",
|
|
|
|
.chain = vsllink_subcommand_handlers,
|
|
|
|
.usage = "",
|
|
|
|
},
|
|
|
|
COMMAND_REGISTRATION_DONE
|
|
|
|
};
|
|
|
|
|
2014-02-16 09:12:39 +00:00
|
|
|
static const char * const vsllink_transports[] = {"jtag", "swd", NULL};
|
|
|
|
|
|
|
|
static const struct swd_driver vsllink_swd_driver = {
|
|
|
|
.init = vsllink_swd_init,
|
|
|
|
.switch_seq = vsllink_swd_switch_seq,
|
|
|
|
.read_reg = vsllink_swd_read_reg,
|
|
|
|
.write_reg = vsllink_swd_write_reg,
|
|
|
|
.run = vsllink_swd_run_queue,
|
|
|
|
};
|
2011-07-11 17:48:05 +00:00
|
|
|
|
2019-01-22 15:31:18 +00:00
|
|
|
static struct jtag_interface vsllink_interface = {
|
2011-07-11 17:48:05 +00:00
|
|
|
.supported = DEBUG_CAP_TMS_SEQ,
|
2019-01-22 15:31:18 +00:00
|
|
|
.execute_queue = vsllink_execute_queue,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct adapter_driver vsllink_adapter_driver = {
|
|
|
|
.name = "vsllink",
|
2011-07-11 17:48:05 +00:00
|
|
|
.transports = vsllink_transports,
|
2019-01-22 15:31:18 +00:00
|
|
|
.commands = vsllink_command_handlers,
|
2010-01-03 21:07:18 +00:00
|
|
|
|
|
|
|
.init = vsllink_init,
|
|
|
|
.quit = vsllink_quit,
|
2019-08-31 09:08:16 +00:00
|
|
|
.reset = vsllink_reset,
|
2010-01-03 21:07:18 +00:00
|
|
|
.speed = vsllink_speed,
|
2019-01-22 15:31:18 +00:00
|
|
|
.khz = vsllink_khz,
|
2010-01-03 21:07:18 +00:00
|
|
|
.speed_div = vsllink_speed_div,
|
2019-01-22 15:31:18 +00:00
|
|
|
|
|
|
|
.jtag_ops = &vsllink_interface,
|
|
|
|
.swd_ops = &vsllink_swd_driver,
|
2010-01-03 21:07:18 +00:00
|
|
|
};
|