20 #define WIN32_LEAN_AND_MEAN
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
29 #ifdef HAVE_ARPA_INET_H
30 #include <arpa/inet.h>
57 #define VD_BUFFER_LEN 4024
58 #define VD_CHEADER_LEN 24
59 #define VD_SHEADER_LEN 16
61 #define VD_MAX_MEMORIES 20
62 #define VD_POLL_INTERVAL 500
63 #define VD_SCALE_PSTOMS 1000000000
239 return WSAGetLastError();
249 uint32_t buflen =
sizeof(
struct vd_shm);
250 struct addrinfo *ainfo =
NULL;
251 struct addrinfo ahint = { 0, AF_INET, SOCK_STREAM, 0, 0,
NULL,
NULL,
NULL };
254 hsock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
255 if (hsock == INVALID_SOCKET)
257 #elif defined __CYGWIN__
259 hsock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
264 hsock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
267 else if (setsockopt(hsock, SOL_SOCKET, SO_RCVLOWAT, &rcvwat,
sizeof(rcvwat)) < 0)
270 else if (setsockopt(hsock, SOL_SOCKET, SO_SNDBUF, (
const char *)&buflen,
sizeof(buflen)) < 0)
272 else if (setsockopt(hsock, SOL_SOCKET, SO_RCVBUF, (
const char *)&buflen,
sizeof(buflen)) < 0)
276 LOG_ERROR(
"socket_open: cannot set socket option, error %d", rc);
277 }
else if (getaddrinfo(server_addr,
NULL, &ahint, &ainfo) != 0) {
281 h_u16_to_be((uint8_t *)ainfo->ai_addr->sa_data, port);
282 if (connect(hsock, ainfo->ai_addr,
sizeof(
struct sockaddr)) < 0) {
305 char *pb = (
char *)pmem;
308 rc = recv(hsock, pb +
offset, to_receive, 0);
315 LOG_DEBUG_IO(
"socket_receive: received %d, to receive %d", rc, to_receive);
317 }
while (to_receive);
347 LOG_DEBUG_IO(
"wait_server: cmd %02" PRIx8
" done, sent %d, rcvd %d, status %d",
348 pmem->
cmd, st, rd, rc);
355 uint8_t num_pre, num_post, tdi, tms;
356 unsigned int num, anum, bytes, hwords, words;
371 while (!rc && (req <
count)) {
374 hwords = (jhdr >> 32) & 0xffff;
375 anum = jhdr & 0xffffff;
376 num_pre = (jhdr >> 27) & 0x7;
377 num_post = (jhdr >> 24) & 0x7;
379 num = anum - num_pre - num_post + 1;
381 num = anum - num_pre;
382 bytes = (num + 7) / 8;
385 if (((jhdr >> 30) & 0x3) == 3) {
395 for (
unsigned int j = 0; j < bytes; j++) {
396 tdo[j] = (pm->
rd8[
rwords * 8 + j] >> num_pre) | (pm->
rd8[
rwords * 8 + j + 1] << (8 - num_pre));
403 waddr +=
sizeof(uint64_t) / 4;
404 tdi = (pm->
wd8[
waddr * 4] >> num_pre) | (pm->
wd8[
waddr * 4 + 1] << (8 - num_pre));
405 tms = (pm->
wd8[
waddr * 4 + 4] >> num_pre) | (pm->
wd8[
waddr * 4 + 4 + 1] << (8 - num_pre));
406 LOG_DEBUG_IO(
"%04x L:%02d O:%05x @%03x DI:%02x MS:%02x DO:%02x",
408 waddr - 2, tdi, tms, (tdo ? tdo[0] : 0xdd));
414 LOG_ERROR(
"0x%x executing transaction", rc);
431 unsigned int num, awidth, wwidth;
448 while (!rc && (req <
count)) {
451 num = (rhdr >> 16) & 0x7ff;
453 awidth = (1 << ((rhdr >> 27) & 0x7));
457 if (((rhdr >> 30) & 0x3) == 2) {
468 for (
unsigned int j = 0; j < num; j++)
469 memcpy(&data[j * awidth], &pm->
rd8[(
rwords + j) * awidth], awidth);
475 waddr +=
sizeof(uint64_t) / 4;
480 waddr +=
sizeof(uint64_t) / 4 + (num * wwidth * awidth + 3) / 4;
486 LOG_ERROR(
"0x%x executing transaction", rc);
502 uint8_t
type, uint32_t period_ps, uint32_t sig_mask)
514 LOG_ERROR(
"0x%x connecting to server", rc);
539 LOG_ERROR(
"0x%x connecting to BFM %s", rc, path);
544 LOG_DEBUG(
"%s type %0x, period %dps, buffer %dx%dB signals r%04xw%04x",
581 LOG_ERROR(
"0x%x waiting %" PRIx32
" cycles", rc, cycles);
598 LOG_ERROR(
"0x%x setting signals %04" PRIx32, rc, write_mask);
602 LOG_DEBUG(
"setting signals %04" PRIx32
" to %04" PRIx32, write_mask, value);
615 LOG_ERROR(
"0x%x setting jtag_clock", rc);
619 LOG_DEBUG(
"setting jtag clock divider to %" PRIx32, value);
625 const uint8_t tms_pre, uint32_t num,
const uint8_t *tdi,
626 uint8_t num_post,
const uint8_t tms_post, uint8_t *tdo,
629 const uint32_t tobits = 8;
630 uint16_t bytes, hwords, anum, words,
waddr;
640 anum = num + num_pre + num_post - 1;
642 anum = num + num_pre;
644 words = (hwords + 1) / 2;
645 bytes = (num + 7) / 8;
651 LOG_ERROR(
"%04x L:%02d O:%05x @%04x too many bits to shift",
658 uint64_t jhdr = (tdo ? ((uint64_t)(words) << 48) : 0) + ((uint64_t)(hwords) << 32) +
659 ((tdo ? 3UL : 1UL) << 30) + (num_pre << 27) + (num_post << 24) + anum;
665 pm->
wd8[4 *
waddr] = (tdi ? (tdi[0] << num_pre) : 0);
667 if (num + num_pre <= 8)
668 pm->
wd8[4 *
waddr + 4] |= (tms_post << (num + num_pre - 1));
669 for (i = 1, j = 4 *
waddr; i < bytes; i++) {
670 if (i == bytes - 1 && num + num_pre <= bytes * tobits)
671 pm->
wd8[j + i + 4] = tms_post << ((num + num_pre - 1) % 8);
673 pm->
wd8[j + i + 4] = 0x0;
675 pm->
wd8[j + i] = 0x0;
677 pm->
wd8[j + i] = (tdi[i] << num_pre) | (tdi[i - 1] >> (8 - num_pre));
683 if (num + num_pre > bytes * tobits)
684 pm->
wd8[j + i] = (tdi[i - 1] >> (8 - num_pre));
686 if (num + num_pre <= bytes * tobits) {
687 pm->
wd8[j + i + 4] = tms_post >> (8 - (num + num_pre - 1) % 8);
689 }
else if (num + num_pre > bytes * tobits && anum <= (bytes + 1) * tobits) {
690 pm->
wd8[j + i + 4] = tms_post << ((num + num_pre - 1) % 8);
692 pm->
wd8[j + i + 4] = tms_post << ((num + num_pre - 1) % 8);
694 pm->
wd8[j + i + 4 + 5] = tms_post >> (8 - (num + num_pre - 1) % 8);
696 pm->
wd8[j + i + 4 + 1] = tms_post >> (8 - (num + num_pre - 1) % 8);
704 rd = calloc(1,
sizeof(
struct vd_rdata));
728 const uint32_t data, uint8_t aspace, uint8_t f_last)
743 uint64_t rhdr = ((uint64_t)
reg << 32) + (1UL << 30) + (2UL << 27) + (1UL << 16) + aspace;
759 uint32_t *data, uint8_t aspace, uint8_t f_last)
774 uint64_t rhdr = ((uint64_t)
reg << 32) + (2UL << 30) + (2UL << 27) + ((data ? 1UL : 0UL) << 16) + aspace;
782 rd = calloc(1,
sizeof(
struct vd_rdata));
787 rd->
rdata = (uint8_t *)data;
816 LOG_ERROR(
"0x%x opening memory %s", rc, path);
817 }
else if (ndx != pm->
rd8[2]) {
818 LOG_WARNING(
"Invalid memory index %" PRIu16
" returned. Direct memory access disabled", pm->
rd8[2]);
822 LOG_DEBUG(
"%" PRIx8
": %s memory %" PRIu32
"x%" PRIu32
"B, buffer %" PRIu32
"x%" PRIu32
"B", ndx, path,
855 LOG_ERROR(
"cannot connect to vdebug server %s:%" PRIu16,
879 LOG_INFO(
"vdebug %d connected to %s through %s:%" PRIu16,
904 uint16_t sig_val = 0xffff;
905 uint16_t sig_mask = 0;
916 LOG_INFO(
"rst trst:%d srst:%d mask:%" PRIx16
" val:%" PRIx16, trst, srst, sig_mask, sig_val);
938 for (
unsigned int i = 0; i <
cmd->num_states; i++) {
974 LOG_DEBUG_IO(
"scan len:%u fields:%u ir/!dr:%d state cur:%x end:%x",
975 num_bits,
cmd->num_fields,
cmd->ir_scan, cur,
cmd->end_state);
976 for (
unsigned int i = 0; i <
cmd->num_fields; i++) {
977 uint8_t cur_num_pre = i == 0 ? num_pre : 0;
978 uint8_t cur_tms_pre = i == 0 ? tms_pre : 0;
979 uint8_t cur_num_post = i ==
cmd->num_fields - 1 ? num_post : 0;
980 uint8_t cur_tms_post = i ==
cmd->num_fields - 1 ? tms_post : 0;
981 uint8_t cur_flush = i ==
cmd->num_fields - 1 ? f_flush : 0;
983 cmd->fields[i].num_bits,
cmd->fields[i].out_value, cur_num_post, cur_tms_post,
984 cmd->fields[i].in_value, cur_flush);
989 if (cur !=
cmd->end_state)
1025 unsigned int divval = clkmax / speed;
1026 LOG_INFO(
"jclk speed:%d kHz set, BFM divider %u", speed, divval);
1034 unsigned int divval = khz ? clkmax / khz : 1;
1035 *jtag_speed = clkmax / divval;
1036 LOG_DEBUG(
"khz speed:%d from khz:%d", *jtag_speed, khz);
1044 LOG_DEBUG(
"div khz:%d from speed:%d", *khz, speed);
1054 switch (
cmd->type) {
1077 LOG_ERROR(
"Unknown JTAG command type 0x%x encountered",
cmd->type);
1092 if (sel != (ap->
dap->
select & ~0xfull)) {
1185 char *pchar = strchr(
CMD_ARGV[0],
':');
1188 int port = atoi(++pchar);
1189 if (port < 0 || port > UINT16_MAX) {
1190 LOG_ERROR(
"invalid port number %d specified", port);
1252 if (isdigit((
unsigned char)
CMD_ARGV[0][0]))
1280 .handler = &vdebug_set_server,
1282 .help =
"set the vdebug server name or address",
1283 .usage =
"<host:port>",
1287 .handler = &vdebug_set_bfm,
1289 .help =
"set the vdebug BFM hierarchical path",
1290 .usage =
"<path> <clk_period[p|n|u]s>",
1294 .handler = &vdebug_set_mem,
1296 .help =
"set the design memory for the code load",
1297 .usage =
"<path> <base_address> <size>",
1301 .handler = &vdebug_set_batching,
1303 .help =
"set the transaction batching no|wr|rd [0|1|2]",
1308 .handler = &vdebug_set_polling,
1310 .help =
"set the polling pause, executing hardware cycles between min and max",
1311 .usage =
"<min cycles> <max cycles>",
1321 .help =
"vdebug command group",
bool transport_is_dapdirect_swd(void)
Returns true if the current debug session is using SWD as its transport.
int dap_dp_init(struct adiv5_dap *dap)
Initialize a DAP.
This defines formats and data structures used to talk to ADIv5 entities.
static bool is_adiv6(const struct adiv5_dap *dap)
Check if DAP is ADIv6.
#define ADIV5_DP_SELECT_APBANK
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
unsigned int jtag_scan_size(const struct scan_command *cmd)
tap_state_t tap_state_transition(tap_state_t cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
int tap_get_tms_path(tap_state_t from, tap_state_t to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
int tap_get_tms_path_len(tap_state_t from, tap_state_t to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
tap_state_t tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
#define DEBUG_CAP_TMS_SEQ
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
bool transport_is_jtag(void)
Returns true if the current debug session is using JTAG as its transport.
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
#define list_first_entry(ptr, type, member)
static void list_add_tail(struct list_head *new, struct list_head *head)
static int list_empty(const struct list_head *head)
static void list_del(struct list_head *entry)
static void INIT_LIST_HEAD(struct list_head *list)
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static uint32_t lh(unsigned int rd, unsigned int base, uint16_t offset) __attribute__((unused))
static int close_socket(int sock)
target_addr_t addr
Start address to search for the control block.
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
This represents an ARM Debug Interface (v5) Access Port (AP).
uint64_t ap_num
ADIv5: Number of this AP (0~255) ADIv6: Base address of this AP (4k aligned) TODO: to be more coheren...
struct adiv5_dap * dap
DAP this AP belongs to.
This represents an ARM Debug Interface (v5) Debug Access Port (DAP).
bool select_valid
Validity of DP SELECT cache.
uint64_t select
Cache for DP SELECT and SELECT1 (ADIv6) register.
Transport-neutral representation of queued DAP transactions, supporting both JTAG and SWD transports.
int(* connect)(struct adiv5_dap *dap)
connect operation for SWD
Represents a driver for a debugging interface.
unsigned int supported
Bit vector listing capabilities exposed by this driver.
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
uint32_t mem_width[VD_MAX_MEMORIES]
uint32_t mem_base[VD_MAX_MEMORIES]
char mem_path[VD_MAX_MEMORIES][128]
uint32_t mem_depth[VD_MAX_MEMORIES]
uint32_t mem_size[VD_MAX_MEMORIES]
uint8_t rd8[VD_BUFFER_LEN]
uint8_t wd8[VD_BUFFER_LEN]
static uint64_t le_to_h_u64(const uint8_t *buf)
static void h_u16_to_be(uint8_t *buf, uint16_t val)
static uint16_t le_to_h_u16(const uint8_t *buf)
static void h_u32_to_le(uint8_t *buf, uint32_t val)
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
static void h_u16_to_le(uint8_t *buf, uint16_t val)
static uint32_t le_to_h_u32(const uint8_t *buf)
static void h_u64_to_le(uint8_t *buf, uint64_t val)
static uint32_t vdebug_wait_server(int hsock, struct vd_shm *pmem)
static int vdebug_close(int hsock, struct vd_shm *pm, uint8_t type)
static int vdebug_dap_bankselect(struct adiv5_ap *ap, unsigned int reg)
static int vdebug_dap_send_sequence(struct adiv5_dap *dap, enum swd_special_seq seq)
static int vdebug_dap_queue_dp_write(struct adiv5_dap *dap, unsigned int reg, uint32_t data)
static int vdebug_jtag_div(int speed, int *khz)
static const char *const vdebug_transports[]
static int vdebug_dap_queue_ap_abort(struct adiv5_dap *dap, uint8_t *ack)
static int vdebug_reg_read(int hsock, struct vd_shm *pm, const uint32_t reg, uint32_t *data, uint8_t aspace, uint8_t f_last)
static int vdebug_open(int hsock, struct vd_shm *pm, const char *path, uint8_t type, uint32_t period_ps, uint32_t sig_mask)
static int vdebug_dap_queue_ap_read(struct adiv5_ap *ap, unsigned int reg, uint32_t *data)
static int vdebug_jtag_path_move(struct pathmove_command *cmd, uint8_t f_flush)
static int vdebug_run_jtag_queue(int hsock, struct vd_shm *pm, unsigned int count)
static int vdebug_run_reg_queue(int hsock, struct vd_shm *pm, unsigned int count)
COMMAND_HANDLER(vdebug_set_server)
static struct jtag_interface vdebug_jtag_ops
struct adapter_driver vdebug_adapter_driver
static int vdebug_jtag_shift_tap(int hsock, struct vd_shm *pm, uint8_t num_pre, const uint8_t tms_pre, uint32_t num, const uint8_t *tdi, uint8_t num_post, const uint8_t tms_post, uint8_t *tdo, uint8_t f_last)
static int vdebug_dap_queue_ap_write(struct adiv5_ap *ap, unsigned int reg, uint32_t data)
static int vdebug_socket_open(char *server_addr, uint32_t port)
static struct vd_client vdc
static int vdebug_quit(void)
static int vdebug_jtag_speed(int speed)
static const struct dap_ops vdebug_dap_ops
static int vdebug_reset(int trst, int srst)
static int vdebug_jtag_scan(struct scan_command *cmd, uint8_t f_flush)
static int vdebug_init(void)
static int vdebug_jtag_clock(int hsock, struct vd_shm *pm, uint32_t value)
static int vdebug_jtag_runtest(unsigned int num_cycles, tap_state_t state, uint8_t f_flush)
struct vd_rdata __attribute__
static int vdebug_dap_connect(struct adiv5_dap *dap)
static int vdebug_reg_write(int hsock, struct vd_shm *pm, const uint32_t reg, const uint32_t data, uint8_t aspace, uint8_t f_last)
static int vdebug_dap_queue_dp_read(struct adiv5_dap *dap, unsigned int reg, uint32_t *data)
static int vdebug_jtag_tms_seq(const uint8_t *tms, int num, uint8_t f_flush)
static int vdebug_mem_open(int hsock, struct vd_shm *pm, const char *path, uint8_t ndx)
static int vdebug_wait(int hsock, struct vd_shm *pm, uint32_t cycles)
static int vdebug_dap_run(struct adiv5_dap *dap)
static int vdebug_jtag_tlr(tap_state_t state, uint8_t f_flush)
static void vdebug_mem_close(int hsock, struct vd_shm *pm, uint8_t ndx)
static int vdebug_sig_set(int hsock, struct vd_shm *pm, uint32_t write_mask, uint32_t value)
static int vdebug_jtag_execute_queue(struct jtag_command *cmd_queue)
static int vdebug_jtag_khz(int khz, int *jtag_speed)
static int vdebug_socket_error(void)
static int vdebug_jtag_stableclocks(unsigned int num_cycles, uint8_t f_flush)
static int vdebug_socket_send(int hsock, struct vd_shm *pmem)
static int vdebug_socket_receive(int hsock, struct vd_shm *pmem)
static const struct command_registration vdebug_command[]
static struct vd_shm * pbuf
static int vdebug_sleep(int us)
static const struct command_registration vdebug_command_handlers[]