You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

467 lines
11 KiB

  1. /*
  2. * JTAG to VPI driver
  3. *
  4. * Copyright (C) 2013 Franck Jullien, <elec4fun@gmail.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  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.
  13. *
  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.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <jtag/interface.h>
  26. #ifdef HAVE_ARPA_INET_H
  27. #include <arpa/inet.h>
  28. #endif
  29. #define NO_TAP_SHIFT 0
  30. #define TAP_SHIFT 1
  31. #define SERVER_ADDRESS "127.0.0.1"
  32. #define SERVER_PORT 5555
  33. #define XFERT_MAX_SIZE 512
  34. #define CMD_RESET 0
  35. #define CMD_TMS_SEQ 1
  36. #define CMD_SCAN_CHAIN 2
  37. #define CMD_SCAN_CHAIN_FLIP_TMS 3
  38. #define CMD_STOP_SIMU 4
  39. int server_port = SERVER_PORT;
  40. char *server_address;
  41. int sockfd;
  42. struct sockaddr_in serv_addr;
  43. struct vpi_cmd {
  44. int cmd;
  45. unsigned char buffer_out[XFERT_MAX_SIZE];
  46. unsigned char buffer_in[XFERT_MAX_SIZE];
  47. int length;
  48. int nb_bits;
  49. };
  50. static int jtag_vpi_send_cmd(struct vpi_cmd *vpi)
  51. {
  52. int retval = write_socket(sockfd, vpi, sizeof(struct vpi_cmd));
  53. if (retval <= 0)
  54. return ERROR_FAIL;
  55. return ERROR_OK;
  56. }
  57. static int jtag_vpi_receive_cmd(struct vpi_cmd *vpi)
  58. {
  59. int retval = read_socket(sockfd, vpi, sizeof(struct vpi_cmd));
  60. if (retval < (int)sizeof(struct vpi_cmd))
  61. return ERROR_FAIL;
  62. return ERROR_OK;
  63. }
  64. /**
  65. * jtag_vpi_reset - ask to reset the JTAG device
  66. * @trst: 1 if TRST is to be asserted
  67. * @srst: 1 if SRST is to be asserted
  68. */
  69. static int jtag_vpi_reset(int trst, int srst)
  70. {
  71. struct vpi_cmd vpi;
  72. vpi.cmd = CMD_RESET;
  73. vpi.length = 0;
  74. return jtag_vpi_send_cmd(&vpi);
  75. }
  76. /**
  77. * jtag_vpi_tms_seq - ask a TMS sequence transition to JTAG
  78. * @bits: TMS bits to be written (bit0, bit1 .. bitN)
  79. * @nb_bits: number of TMS bits (between 1 and 8)
  80. *
  81. * Write a serie of TMS transitions, where each transition consists in :
  82. * - writing out TCK=0, TMS=<new_state>, TDI=<???>
  83. * - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
  84. * The function ensures that at the end of the sequence, the clock (TCK) is put
  85. * low.
  86. */
  87. static int jtag_vpi_tms_seq(const uint8_t *bits, int nb_bits)
  88. {
  89. struct vpi_cmd vpi;
  90. int nb_bytes;
  91. nb_bytes = DIV_ROUND_UP(nb_bits, 8);
  92. vpi.cmd = CMD_TMS_SEQ;
  93. memcpy(vpi.buffer_out, bits, nb_bytes);
  94. vpi.length = nb_bytes;
  95. vpi.nb_bits = nb_bits;
  96. return jtag_vpi_send_cmd(&vpi);
  97. }
  98. /**
  99. * jtag_vpi_path_move - ask a TMS sequence transition to JTAG
  100. * @cmd: path transition
  101. *
  102. * Write a serie of TMS transitions, where each transition consists in :
  103. * - writing out TCK=0, TMS=<new_state>, TDI=<???>
  104. * - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
  105. * The function ensures that at the end of the sequence, the clock (TCK) is put
  106. * low.
  107. */
  108. static int jtag_vpi_path_move(struct pathmove_command *cmd)
  109. {
  110. uint8_t trans[DIV_ROUND_UP(cmd->num_states, 8)];
  111. memset(trans, 0, DIV_ROUND_UP(cmd->num_states, 8));
  112. for (int i = 0; i < cmd->num_states; i++) {
  113. if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
  114. buf_set_u32(trans, i, 1, 1);
  115. tap_set_state(cmd->path[i]);
  116. }
  117. return jtag_vpi_tms_seq(trans, cmd->num_states);
  118. }
  119. /**
  120. * jtag_vpi_tms - ask a tms command
  121. * @cmd: tms command
  122. */
  123. static int jtag_vpi_tms(struct tms_command *cmd)
  124. {
  125. return jtag_vpi_tms_seq(cmd->bits, cmd->num_bits);
  126. }
  127. static int jtag_vpi_state_move(tap_state_t state)
  128. {
  129. if (tap_get_state() == state)
  130. return ERROR_OK;
  131. uint8_t tms_scan = tap_get_tms_path(tap_get_state(), state);
  132. int tms_len = tap_get_tms_path_len(tap_get_state(), state);
  133. int retval = jtag_vpi_tms_seq(&tms_scan, tms_len);
  134. if (retval != ERROR_OK)
  135. return retval;
  136. tap_set_state(state);
  137. return ERROR_OK;
  138. }
  139. static int jtag_vpi_queue_tdi_xfer(uint8_t *bits, int nb_bits, int tap_shift)
  140. {
  141. struct vpi_cmd vpi;
  142. int nb_bytes = DIV_ROUND_UP(nb_bits, 8);
  143. vpi.cmd = tap_shift ? CMD_SCAN_CHAIN_FLIP_TMS : CMD_SCAN_CHAIN;
  144. if (bits)
  145. memcpy(vpi.buffer_out, bits, nb_bytes);
  146. else
  147. memset(vpi.buffer_out, 0xff, nb_bytes);
  148. vpi.length = nb_bytes;
  149. vpi.nb_bits = nb_bits;
  150. int retval = jtag_vpi_send_cmd(&vpi);
  151. if (retval != ERROR_OK)
  152. return retval;
  153. retval = jtag_vpi_receive_cmd(&vpi);
  154. if (retval != ERROR_OK)
  155. return retval;
  156. if (bits)
  157. memcpy(bits, vpi.buffer_in, nb_bytes);
  158. return ERROR_OK;
  159. }
  160. /**
  161. * jtag_vpi_queue_tdi - short description
  162. * @bits: bits to be queued on TDI (or NULL if 0 are to be queued)
  163. * @nb_bits: number of bits
  164. */
  165. static int jtag_vpi_queue_tdi(uint8_t *bits, int nb_bits, int tap_shift)
  166. {
  167. int nb_xfer = DIV_ROUND_UP(nb_bits, XFERT_MAX_SIZE * 8);
  168. uint8_t *xmit_buffer = bits;
  169. int xmit_nb_bits = nb_bits;
  170. int i = 0;
  171. int retval;
  172. while (nb_xfer) {
  173. if (nb_xfer == 1) {
  174. retval = jtag_vpi_queue_tdi_xfer(&xmit_buffer[i], xmit_nb_bits, tap_shift);
  175. if (retval != ERROR_OK)
  176. return retval;
  177. } else {
  178. retval = jtag_vpi_queue_tdi_xfer(&xmit_buffer[i], XFERT_MAX_SIZE * 8, NO_TAP_SHIFT);
  179. if (retval != ERROR_OK)
  180. return retval;
  181. xmit_nb_bits -= XFERT_MAX_SIZE * 8;
  182. i += XFERT_MAX_SIZE;
  183. }
  184. nb_xfer--;
  185. }
  186. return ERROR_OK;
  187. }
  188. /**
  189. * jtag_vpi_clock_tms - clock a TMS transition
  190. * @tms: the TMS to be sent
  191. *
  192. * Triggers a TMS transition (ie. one JTAG TAP state move).
  193. */
  194. static int jtag_vpi_clock_tms(int tms)
  195. {
  196. const uint8_t tms_0 = 0;
  197. const uint8_t tms_1 = 1;
  198. return jtag_vpi_tms_seq(tms ? &tms_1 : &tms_0, 1);
  199. }
  200. /**
  201. * jtag_vpi_scan - launches a DR-scan or IR-scan
  202. * @cmd: the command to launch
  203. *
  204. * Launch a JTAG IR-scan or DR-scan
  205. *
  206. * Returns ERROR_OK if OK, ERROR_xxx if a read/write error occured.
  207. */
  208. static int jtag_vpi_scan(struct scan_command *cmd)
  209. {
  210. int scan_bits;
  211. uint8_t *buf = NULL;
  212. int retval = ERROR_OK;
  213. scan_bits = jtag_build_buffer(cmd, &buf);
  214. if (cmd->ir_scan) {
  215. retval = jtag_vpi_state_move(TAP_IRSHIFT);
  216. if (retval != ERROR_OK)
  217. return retval;
  218. } else {
  219. retval = jtag_vpi_state_move(TAP_DRSHIFT);
  220. if (retval != ERROR_OK)
  221. return retval;
  222. }
  223. if (cmd->end_state == TAP_DRSHIFT) {
  224. retval = jtag_vpi_queue_tdi(buf, scan_bits, NO_TAP_SHIFT);
  225. if (retval != ERROR_OK)
  226. return retval;
  227. } else {
  228. retval = jtag_vpi_queue_tdi(buf, scan_bits, TAP_SHIFT);
  229. if (retval != ERROR_OK)
  230. return retval;
  231. }
  232. if (cmd->end_state != TAP_DRSHIFT) {
  233. /*
  234. * As our JTAG is in an unstable state (IREXIT1 or DREXIT1), move it
  235. * forward to a stable IRPAUSE or DRPAUSE.
  236. */
  237. retval = jtag_vpi_clock_tms(0);
  238. if (retval != ERROR_OK)
  239. return retval;
  240. if (cmd->ir_scan)
  241. tap_set_state(TAP_IRPAUSE);
  242. else
  243. tap_set_state(TAP_DRPAUSE);
  244. }
  245. retval = jtag_read_buffer(buf, cmd);
  246. if (retval != ERROR_OK)
  247. return retval;
  248. if (buf)
  249. free(buf);
  250. if (cmd->end_state != TAP_DRSHIFT) {
  251. retval = jtag_vpi_state_move(cmd->end_state);
  252. if (retval != ERROR_OK)
  253. return retval;
  254. }
  255. return ERROR_OK;
  256. }
  257. static int jtag_vpi_runtest(int cycles, tap_state_t state)
  258. {
  259. int retval;
  260. retval = jtag_vpi_state_move(TAP_IDLE);
  261. if (retval != ERROR_OK)
  262. return retval;
  263. retval = jtag_vpi_queue_tdi(NULL, cycles, TAP_SHIFT);
  264. if (retval != ERROR_OK)
  265. return retval;
  266. return jtag_vpi_state_move(state);
  267. }
  268. static int jtag_vpi_stableclocks(int cycles)
  269. {
  270. return jtag_vpi_queue_tdi(NULL, cycles, TAP_SHIFT);
  271. }
  272. static int jtag_vpi_execute_queue(void)
  273. {
  274. struct jtag_command *cmd;
  275. int retval = ERROR_OK;
  276. for (cmd = jtag_command_queue; retval == ERROR_OK && cmd != NULL;
  277. cmd = cmd->next) {
  278. switch (cmd->type) {
  279. case JTAG_RESET:
  280. retval = jtag_vpi_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  281. break;
  282. case JTAG_RUNTEST:
  283. retval = jtag_vpi_runtest(cmd->cmd.runtest->num_cycles,
  284. cmd->cmd.runtest->end_state);
  285. break;
  286. case JTAG_STABLECLOCKS:
  287. retval = jtag_vpi_stableclocks(cmd->cmd.stableclocks->num_cycles);
  288. break;
  289. case JTAG_TLR_RESET:
  290. retval = jtag_vpi_state_move(cmd->cmd.statemove->end_state);
  291. break;
  292. case JTAG_PATHMOVE:
  293. retval = jtag_vpi_path_move(cmd->cmd.pathmove);
  294. break;
  295. case JTAG_TMS:
  296. retval = jtag_vpi_tms(cmd->cmd.tms);
  297. break;
  298. case JTAG_SLEEP:
  299. jtag_sleep(cmd->cmd.sleep->us);
  300. break;
  301. case JTAG_SCAN:
  302. retval = jtag_vpi_scan(cmd->cmd.scan);
  303. break;
  304. }
  305. }
  306. return retval;
  307. }
  308. static int jtag_vpi_init(void)
  309. {
  310. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  311. if (sockfd < 0) {
  312. LOG_ERROR("Could not create socket");
  313. return ERROR_FAIL;
  314. }
  315. memset(&serv_addr, 0, sizeof(serv_addr));
  316. serv_addr.sin_family = AF_INET;
  317. serv_addr.sin_port = htons(server_port);
  318. if (!server_address)
  319. server_address = strdup(SERVER_ADDRESS);
  320. serv_addr.sin_addr.s_addr = inet_addr(server_address);
  321. if (serv_addr.sin_addr.s_addr == INADDR_NONE) {
  322. LOG_ERROR("inet_addr error occured");
  323. return ERROR_FAIL;
  324. }
  325. if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  326. close(sockfd);
  327. LOG_ERROR("Can't connect to %s : %u", server_address, server_port);
  328. return ERROR_COMMAND_CLOSE_CONNECTION;
  329. }
  330. LOG_INFO("Connection to %s : %u succeed", server_address, server_port);
  331. return ERROR_OK;
  332. }
  333. static int jtag_vpi_quit(void)
  334. {
  335. free(server_address);
  336. return close(sockfd);
  337. }
  338. COMMAND_HANDLER(jtag_vpi_set_port)
  339. {
  340. if (CMD_ARGC == 0)
  341. LOG_WARNING("You need to set a port number");
  342. else
  343. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], server_port);
  344. LOG_INFO("Set server port to %u", server_port);
  345. return ERROR_OK;
  346. }
  347. COMMAND_HANDLER(jtag_vpi_set_address)
  348. {
  349. free(server_address);
  350. if (CMD_ARGC == 0) {
  351. LOG_WARNING("You need to set an address");
  352. server_address = strdup(SERVER_ADDRESS);
  353. } else
  354. server_address = strdup(CMD_ARGV[0]);
  355. LOG_INFO("Set server address to %s", server_address);
  356. return ERROR_OK;
  357. }
  358. static const struct command_registration jtag_vpi_command_handlers[] = {
  359. {
  360. .name = "jtag_vpi_set_port",
  361. .handler = &jtag_vpi_set_port,
  362. .mode = COMMAND_CONFIG,
  363. .help = "set the port of the VPI server",
  364. .usage = "description_string",
  365. },
  366. {
  367. .name = "jtag_vpi_set_address",
  368. .handler = &jtag_vpi_set_address,
  369. .mode = COMMAND_CONFIG,
  370. .help = "set the address of the VPI server",
  371. .usage = "description_string",
  372. },
  373. COMMAND_REGISTRATION_DONE
  374. };
  375. struct jtag_interface jtag_vpi_interface = {
  376. .name = "jtag_vpi",
  377. .supported = DEBUG_CAP_TMS_SEQ,
  378. .commands = jtag_vpi_command_handlers,
  379. .transports = jtag_only,
  380. .init = jtag_vpi_init,
  381. .quit = jtag_vpi_quit,
  382. .execute_queue = jtag_vpi_execute_queue,
  383. };