stlink: remove usb timeout magic numbers
[openocd.git] / src / jtag / drivers / jtag_vpi.c
blob84cd947064860b20b560e560e106dca53b1f2df8
1 /*
2 * JTAG to VPI driver
4 * Copyright (C) 2013 Franck Jullien, <elec4fun@gmail.com>
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include <jtag/interface.h>
26 #include <arpa/inet.h>
28 #define NO_TAP_SHIFT 0
29 #define TAP_SHIFT 1
31 #define SERVER_ADDRESS "127.0.0.1"
32 #define SERVER_PORT 5555
34 #define XFERT_MAX_SIZE 512
36 #define CMD_RESET 0
37 #define CMD_TMS_SEQ 1
38 #define CMD_SCAN_CHAIN 2
39 #define CMD_SCAN_CHAIN_FLIP_TMS 3
40 #define CMD_STOP_SIMU 4
42 int server_port = SERVER_PORT;
44 int sockfd;
45 struct sockaddr_in serv_addr;
47 struct vpi_cmd {
48 int cmd;
49 unsigned char buffer_out[XFERT_MAX_SIZE];
50 unsigned char buffer_in[XFERT_MAX_SIZE];
51 int length;
52 int nb_bits;
55 static int jtag_vpi_send_cmd(struct vpi_cmd *vpi)
57 int retval = write(sockfd, vpi, sizeof(struct vpi_cmd));
58 if (retval <= 0)
59 return ERROR_FAIL;
61 return ERROR_OK;
64 static int jtag_vpi_receive_cmd(struct vpi_cmd *vpi)
66 int retval = read(sockfd, vpi, sizeof(struct vpi_cmd));
67 if (retval < (int)sizeof(struct vpi_cmd))
68 return ERROR_FAIL;
70 return ERROR_OK;
73 /**
74 * jtag_vpi_reset - ask to reset the JTAG device
75 * @trst: 1 if TRST is to be asserted
76 * @srst: 1 if SRST is to be asserted
78 static int jtag_vpi_reset(int trst, int srst)
80 struct vpi_cmd vpi;
82 vpi.cmd = CMD_RESET;
83 vpi.length = 0;
84 return jtag_vpi_send_cmd(&vpi);
87 /**
88 * jtag_vpi_tms_seq - ask a TMS sequence transition to JTAG
89 * @bits: TMS bits to be written (bit0, bit1 .. bitN)
90 * @nb_bits: number of TMS bits (between 1 and 8)
92 * Write a serie of TMS transitions, where each transition consists in :
93 * - writing out TCK=0, TMS=<new_state>, TDI=<???>
94 * - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
95 * The function ensures that at the end of the sequence, the clock (TCK) is put
96 * low.
98 static int jtag_vpi_tms_seq(const uint8_t *bits, int nb_bits)
100 struct vpi_cmd vpi;
101 int nb_bytes;
103 nb_bytes = (nb_bits / 8) + !!(nb_bits % 8);
105 vpi.cmd = CMD_TMS_SEQ;
106 memcpy(vpi.buffer_out, bits, nb_bytes);
107 vpi.length = nb_bytes;
108 vpi.nb_bits = nb_bits;
110 return jtag_vpi_send_cmd(&vpi);
114 * jtag_vpi_path_move - ask a TMS sequence transition to JTAG
115 * @cmd: path transition
117 * Write a serie of TMS transitions, where each transition consists in :
118 * - writing out TCK=0, TMS=<new_state>, TDI=<???>
119 * - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
120 * The function ensures that at the end of the sequence, the clock (TCK) is put
121 * low.
124 static int jtag_vpi_path_move(struct pathmove_command *cmd)
126 uint16_t trans = 0;
127 int retval;
128 int i;
130 for (i = 0; i < cmd->num_states; i++) {
131 if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
132 trans = trans | 1;
133 trans = trans << 1;
136 retval = jtag_vpi_tms_seq((uint8_t *)&trans, 1);
137 if (retval != ERROR_OK)
138 return retval;
140 tap_set_state(cmd->path[i]);
142 return ERROR_OK;
146 * jtag_vpi_tms - ask a tms command
147 * @cmd: tms command
149 static int jtag_vpi_tms(struct tms_command *cmd)
151 return jtag_vpi_tms_seq(cmd->bits, cmd->num_bits);
154 static int jtag_vpi_state_move(tap_state_t state)
156 if (tap_get_state() == state)
157 return ERROR_OK;
159 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), state);
160 int tms_len = tap_get_tms_path_len(tap_get_state(), state);
162 int retval = jtag_vpi_tms_seq(&tms_scan, tms_len);
163 if (retval != ERROR_OK)
164 return retval;
166 tap_set_state(state);
168 return ERROR_OK;
171 static int jtag_vpi_queue_tdi_xfer(uint8_t *bits, int nb_bits, int tap_shift)
173 struct vpi_cmd vpi;
174 int nb_bytes = (nb_bits / 8) + !!(nb_bits % 8);
176 vpi.cmd = tap_shift ? CMD_SCAN_CHAIN_FLIP_TMS : CMD_SCAN_CHAIN;
178 if (bits)
179 memcpy(vpi.buffer_out, bits, nb_bytes);
180 else
181 memset(vpi.buffer_out, 0xff, nb_bytes);
183 vpi.length = nb_bytes;
184 vpi.nb_bits = nb_bits;
186 int retval = jtag_vpi_send_cmd(&vpi);
187 if (retval != ERROR_OK)
188 return retval;
190 retval = jtag_vpi_receive_cmd(&vpi);
191 if (retval != ERROR_OK)
192 return retval;
194 if (bits)
195 memcpy(bits, vpi.buffer_in, nb_bytes);
197 return ERROR_OK;
201 * jtag_vpi_queue_tdi - short description
202 * @bits: bits to be queued on TDI (or NULL if 0 are to be queued)
203 * @nb_bits: number of bits
205 static int jtag_vpi_queue_tdi(uint8_t *bits, int nb_bits, int tap_shift)
207 int nb_xfer = (nb_bits / (XFERT_MAX_SIZE * 8)) + !!(nb_bits % (XFERT_MAX_SIZE * 8));
208 uint8_t *xmit_buffer = bits;
209 int xmit_nb_bits = nb_bits;
210 int i = 0;
211 int retval;
213 while (nb_xfer) {
215 if (nb_xfer == 1) {
216 retval = jtag_vpi_queue_tdi_xfer(&xmit_buffer[i], xmit_nb_bits, tap_shift);
217 if (retval != ERROR_OK)
218 return retval;
219 } else {
220 retval = jtag_vpi_queue_tdi_xfer(&xmit_buffer[i], XFERT_MAX_SIZE * 8, NO_TAP_SHIFT);
221 if (retval != ERROR_OK)
222 return retval;
223 xmit_nb_bits -= XFERT_MAX_SIZE * 8;
224 i += XFERT_MAX_SIZE;
227 nb_xfer--;
230 return ERROR_OK;
234 * jtag_vpi_clock_tms - clock a TMS transition
235 * @tms: the TMS to be sent
237 * Triggers a TMS transition (ie. one JTAG TAP state move).
239 static int jtag_vpi_clock_tms(int tms)
241 const uint8_t tms_0 = 0;
242 const uint8_t tms_1 = 1;
244 return jtag_vpi_tms_seq(tms ? &tms_1 : &tms_0, 1);
248 * jtag_vpi_scan - launches a DR-scan or IR-scan
249 * @cmd: the command to launch
251 * Launch a JTAG IR-scan or DR-scan
253 * Returns ERROR_OK if OK, ERROR_xxx if a read/write error occured.
255 static int jtag_vpi_scan(struct scan_command *cmd)
257 int scan_bits;
258 uint8_t *buf = NULL;
259 int retval = ERROR_OK;
261 scan_bits = jtag_build_buffer(cmd, &buf);
263 if (cmd->ir_scan) {
264 retval = jtag_vpi_state_move(TAP_IRSHIFT);
265 if (retval != ERROR_OK)
266 return retval;
267 } else {
268 retval = jtag_vpi_state_move(TAP_DRSHIFT);
269 if (retval != ERROR_OK)
270 return retval;
273 if (cmd->end_state == TAP_DRSHIFT) {
274 retval = jtag_vpi_queue_tdi(buf, scan_bits, NO_TAP_SHIFT);
275 if (retval != ERROR_OK)
276 return retval;
277 } else {
278 retval = jtag_vpi_queue_tdi(buf, scan_bits, TAP_SHIFT);
279 if (retval != ERROR_OK)
280 return retval;
283 if (cmd->end_state != TAP_DRSHIFT) {
285 * As our JTAG is in an unstable state (IREXIT1 or DREXIT1), move it
286 * forward to a stable IRPAUSE or DRPAUSE.
288 retval = jtag_vpi_clock_tms(0);
289 if (retval != ERROR_OK)
290 return retval;
292 if (cmd->ir_scan)
293 tap_set_state(TAP_IRPAUSE);
294 else
295 tap_set_state(TAP_DRPAUSE);
298 retval = jtag_read_buffer(buf, cmd);
299 if (retval != ERROR_OK)
300 return retval;
302 if (buf)
303 free(buf);
305 if (cmd->end_state != TAP_DRSHIFT) {
306 retval = jtag_vpi_state_move(cmd->end_state);
307 if (retval != ERROR_OK)
308 return retval;
311 return ERROR_OK;
314 static int jtag_vpi_runtest(int cycles, tap_state_t state)
316 int retval;
318 retval = jtag_vpi_state_move(TAP_IDLE);
319 if (retval != ERROR_OK)
320 return retval;
322 retval = jtag_vpi_queue_tdi(NULL, cycles, TAP_SHIFT);
323 if (retval != ERROR_OK)
324 return retval;
326 return jtag_vpi_state_move(state);
329 static int jtag_vpi_stableclocks(int cycles)
331 return jtag_vpi_queue_tdi(NULL, cycles, TAP_SHIFT);
334 static int jtag_vpi_execute_queue(void)
336 struct jtag_command *cmd;
337 int retval = ERROR_OK;
339 for (cmd = jtag_command_queue; retval == ERROR_OK && cmd != NULL;
340 cmd = cmd->next) {
341 switch (cmd->type) {
342 case JTAG_RESET:
343 retval = jtag_vpi_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
344 break;
345 case JTAG_RUNTEST:
346 retval = jtag_vpi_runtest(cmd->cmd.runtest->num_cycles,
347 cmd->cmd.runtest->end_state);
348 break;
349 case JTAG_STABLECLOCKS:
350 retval = jtag_vpi_stableclocks(cmd->cmd.stableclocks->num_cycles);
351 break;
352 case JTAG_TLR_RESET:
353 retval = jtag_vpi_state_move(cmd->cmd.statemove->end_state);
354 break;
355 case JTAG_PATHMOVE:
356 retval = jtag_vpi_path_move(cmd->cmd.pathmove);
357 break;
358 case JTAG_TMS:
359 retval = jtag_vpi_tms(cmd->cmd.tms);
360 break;
361 case JTAG_SLEEP:
362 jtag_sleep(cmd->cmd.sleep->us);
363 break;
364 case JTAG_SCAN:
365 retval = jtag_vpi_scan(cmd->cmd.scan);
366 break;
370 return retval;
373 static int jtag_vpi_init(void)
375 sockfd = socket(AF_INET, SOCK_STREAM, 0);
376 if (sockfd < 0) {
377 LOG_ERROR("Could not create socket");
378 return ERROR_FAIL;
381 memset(&serv_addr, 0, sizeof(serv_addr));
383 serv_addr.sin_family = AF_INET;
384 serv_addr.sin_port = htons(server_port);
386 if (inet_pton(AF_INET, SERVER_ADDRESS, &serv_addr.sin_addr) <= 0) {
387 LOG_ERROR("inet_pton error occured");
388 return ERROR_FAIL;
391 if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
392 close(sockfd);
393 LOG_ERROR("Can't connect to %s : %u", SERVER_ADDRESS, server_port);
394 return ERROR_COMMAND_CLOSE_CONNECTION;
397 LOG_INFO("Connection to %s : %u succeed", SERVER_ADDRESS, server_port);
399 return ERROR_OK;
402 static int jtag_vpi_quit(void)
404 return close(sockfd);
407 COMMAND_HANDLER(jtag_vpi_set_port)
409 if (CMD_ARGC == 0)
410 LOG_WARNING("You need to set a port number");
411 else
412 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], server_port);
414 LOG_INFO("Set server port to %u", server_port);
416 return ERROR_OK;
420 static const struct command_registration jtag_vpi_command_handlers[] = {
422 .name = "jtag_vpi_set_port",
423 .handler = &jtag_vpi_set_port,
424 .mode = COMMAND_CONFIG,
425 .help = "set the port of the VPI server",
426 .usage = "description_string",
428 COMMAND_REGISTRATION_DONE
431 struct jtag_interface jtag_vpi_interface = {
432 .name = "jtag_vpi",
433 .supported = DEBUG_CAP_TMS_SEQ,
434 .commands = jtag_vpi_command_handlers,
435 .transports = jtag_only,
437 .init = jtag_vpi_init,
438 .quit = jtag_vpi_quit,
439 .execute_queue = jtag_vpi_execute_queue,