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.
 
 
 
 
 
 

700 lines
16 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2012 by Jan Dakinevich *
  3. * jan.dakinevich@gmail.com *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. # include "config.h"
  20. #endif
  21. #include <helper/log.h>
  22. #include <helper/binarybuffer.h>
  23. #include <helper/command.h>
  24. #include <jtag/interface.h>
  25. #include "libusb_common.h"
  26. struct sequence {
  27. int len;
  28. void *tms;
  29. void *tdo;
  30. const void *tdi;
  31. struct sequence *next;
  32. };
  33. struct queue {
  34. struct sequence *head;
  35. struct sequence *tail;
  36. };
  37. static struct sequence *queue_add_tail(struct queue *queue, int len)
  38. {
  39. if (len <= 0) {
  40. LOG_ERROR("BUG: sequences with zero length are not allowed");
  41. return NULL;
  42. }
  43. struct sequence *next;
  44. next = malloc(sizeof(*next));
  45. if (next) {
  46. next->tms = calloc(1, DIV_ROUND_UP(len, 8));
  47. if (next->tms) {
  48. next->len = len;
  49. next->tdo = NULL;
  50. next->tdi = NULL;
  51. next->next = NULL;
  52. if (!queue->head) {
  53. /* Queue is empty at the moment */
  54. queue->head = next;
  55. } else {
  56. /* Queue already contains at least one sequence */
  57. queue->tail->next = next;
  58. }
  59. queue->tail = next;
  60. } else {
  61. free(next);
  62. next = NULL;
  63. }
  64. }
  65. if (!next)
  66. LOG_ERROR("Not enough memory");
  67. return next;
  68. }
  69. static void queue_drop_head(struct queue *queue)
  70. {
  71. struct sequence *head = queue->head->next; /* New head */
  72. free(queue->head->tms);
  73. free(queue->head);
  74. queue->head = head;
  75. }
  76. static void queue_free(struct queue *queue)
  77. {
  78. if (queue) {
  79. while (queue->head)
  80. queue_drop_head(queue);
  81. free(queue);
  82. }
  83. }
  84. static struct queue *queue_alloc(void)
  85. {
  86. struct queue *queue = malloc(sizeof(*queue));
  87. if (queue)
  88. queue->head = NULL;
  89. else
  90. LOG_ERROR("Not enough memory");
  91. return queue;
  92. }
  93. /* Size of usb communication buffer */
  94. #define OSBDM_USB_BUFSIZE 64
  95. /* Timeout for USB transfer, ms */
  96. #define OSBDM_USB_TIMEOUT 1000
  97. /* Write end point */
  98. #define OSBDM_USB_EP_WRITE 0x01
  99. /* Read end point */
  100. #define OSBDM_USB_EP_READ 0x82
  101. /* Initialize OSBDM device */
  102. #define OSBDM_CMD_INIT 0x11
  103. /* Execute special, not-BDM command. But only this
  104. * command is used for JTAG operation */
  105. #define OSBDM_CMD_SPECIAL 0x27
  106. /* Execute JTAG swap (tms/tdi -> tdo) */
  107. #define OSBDM_CMD_SPECIAL_SWAP 0x05
  108. /* Reset control */
  109. #define OSBDM_CMD_SPECIAL_SRST 0x01
  110. /* Maximum bit-length in one swap */
  111. #define OSBDM_SWAP_MAX (((OSBDM_USB_BUFSIZE - 6) / 5) * 16)
  112. /* Lists of valid VID/PID pairs
  113. */
  114. static const uint16_t osbdm_vid[] = { 0x15a2, 0x15a2, 0x15a2, 0 };
  115. static const uint16_t osbdm_pid[] = { 0x0042, 0x0058, 0x005e, 0 };
  116. struct osbdm {
  117. struct jtag_libusb_device_handle *devh; /* USB handle */
  118. uint8_t buffer[OSBDM_USB_BUFSIZE]; /* Data to send and receive */
  119. int count; /* Count data to send and to read */
  120. };
  121. /* osbdm instance
  122. */
  123. static struct osbdm osbdm_context;
  124. static int osbdm_send_and_recv(struct osbdm *osbdm)
  125. {
  126. /* Send request */
  127. int count = jtag_libusb_bulk_write(osbdm->devh, OSBDM_USB_EP_WRITE,
  128. (char *)osbdm->buffer, osbdm->count, OSBDM_USB_TIMEOUT);
  129. if (count != osbdm->count) {
  130. LOG_ERROR("OSBDM communication error: can't write");
  131. return ERROR_FAIL;
  132. }
  133. /* Save command code for next checking */
  134. uint8_t cmd_saved = osbdm->buffer[0];
  135. /* Reading answer */
  136. osbdm->count = jtag_libusb_bulk_read(osbdm->devh, OSBDM_USB_EP_READ,
  137. (char *)osbdm->buffer, OSBDM_USB_BUFSIZE, OSBDM_USB_TIMEOUT);
  138. /* Now perform basic checks for data sent by BDM device
  139. */
  140. if (osbdm->count < 0) {
  141. LOG_ERROR("OSBDM communication error: can't read");
  142. return ERROR_FAIL;
  143. }
  144. if (osbdm->count < 2) {
  145. LOG_ERROR("OSBDM communication error: reply too small");
  146. return ERROR_FAIL;
  147. }
  148. if (osbdm->count != osbdm->buffer[1]) {
  149. LOG_ERROR("OSBDM communication error: reply size mismatch");
  150. return ERROR_FAIL;
  151. }
  152. if (cmd_saved != osbdm->buffer[0]) {
  153. LOG_ERROR("OSBDM communication error: reply command mismatch");
  154. return ERROR_FAIL;
  155. }
  156. return ERROR_OK;
  157. }
  158. static int osbdm_srst(struct osbdm *osbdm, int srst)
  159. {
  160. osbdm->count = 0;
  161. (void)memset(osbdm->buffer, 0, OSBDM_USB_BUFSIZE);
  162. /* Composing request
  163. */
  164. osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL; /* Command */
  165. osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL_SRST; /* Subcommand */
  166. /* Length in bytes - not used */
  167. osbdm->buffer[osbdm->count++] = 0;
  168. osbdm->buffer[osbdm->count++] = 0;
  169. /* SRST state */
  170. osbdm->buffer[osbdm->count++] = (srst ? 0 : 0x08);
  171. /* Sending data
  172. */
  173. if (osbdm_send_and_recv(osbdm) != ERROR_OK)
  174. return ERROR_FAIL;
  175. return ERROR_OK;
  176. }
  177. static int osbdm_swap(struct osbdm *osbdm, void *tms, void *tdi,
  178. void *tdo, int length)
  179. {
  180. if (length > OSBDM_SWAP_MAX) {
  181. LOG_ERROR("BUG: bit sequence too long");
  182. return ERROR_FAIL;
  183. }
  184. if (length <= 0) {
  185. LOG_ERROR("BUG: bit sequence equal or less than 0");
  186. return ERROR_FAIL;
  187. }
  188. int swap_count = DIV_ROUND_UP(length, 16);
  189. /* cleanup */
  190. osbdm->count = 0;
  191. (void)memset(osbdm->buffer, 0, OSBDM_USB_BUFSIZE);
  192. /* Composing request
  193. */
  194. osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL; /* Command */
  195. osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL_SWAP; /* Subcommand */
  196. /* Length in bytes - not used */
  197. osbdm->buffer[osbdm->count++] = 0;
  198. osbdm->buffer[osbdm->count++] = 0;
  199. /* Swap count */
  200. osbdm->buffer[osbdm->count++] = 0;
  201. osbdm->buffer[osbdm->count++] = (uint8_t)swap_count;
  202. for (int bit_idx = 0; bit_idx < length; ) {
  203. /* Bit count in swap */
  204. int bit_count = length - bit_idx;
  205. if (bit_count > 16)
  206. bit_count = 16;
  207. osbdm->buffer[osbdm->count++] = (uint8_t)bit_count;
  208. /* Copying TMS and TDI data to output buffer */
  209. uint32_t tms_data = buf_get_u32(tms, bit_idx, bit_count);
  210. uint32_t tdi_data = buf_get_u32(tdi, bit_idx, bit_count);
  211. osbdm->buffer[osbdm->count++] = (uint8_t)(tdi_data >> 8);
  212. osbdm->buffer[osbdm->count++] = (uint8_t)tdi_data;
  213. osbdm->buffer[osbdm->count++] = (uint8_t)(tms_data >> 8);
  214. osbdm->buffer[osbdm->count++] = (uint8_t)tms_data;
  215. /* Next bit offset */
  216. bit_idx += bit_count;
  217. }
  218. assert(osbdm->count <= OSBDM_USB_BUFSIZE);
  219. /* Sending data
  220. */
  221. if (osbdm_send_and_recv(osbdm) != ERROR_OK)
  222. return ERROR_FAIL;
  223. /* Extra check
  224. */
  225. if (((osbdm->buffer[2] << 8) | osbdm->buffer[3]) != 2 * swap_count) {
  226. LOG_ERROR("OSBDM communication error: invalid swap command reply");
  227. return ERROR_FAIL;
  228. }
  229. /* Copy TDO responce
  230. */
  231. uint8_t *buffer = osbdm->buffer + 4;
  232. for (int bit_idx = 0; bit_idx < length; ) {
  233. int bit_count = length - bit_idx;
  234. if (bit_count > 16)
  235. bit_count = 16;
  236. /* Prepare data */
  237. uint32_t tdo_data = 0;
  238. tdo_data |= (*buffer++) << 8;
  239. tdo_data |= (*buffer++);
  240. tdo_data >>= (16 - bit_count);
  241. /* Copy TDO to return */
  242. buf_set_u32(tdo, bit_idx, bit_count, tdo_data);
  243. bit_idx += bit_count;
  244. }
  245. return ERROR_OK;
  246. }
  247. static int osbdm_flush(struct osbdm *osbdm, struct queue* queue)
  248. {
  249. uint8_t tms[DIV_ROUND_UP(OSBDM_SWAP_MAX, 8)];
  250. uint8_t tdi[DIV_ROUND_UP(OSBDM_SWAP_MAX, 8)];
  251. uint8_t tdo[DIV_ROUND_UP(OSBDM_SWAP_MAX, 8)];
  252. int seq_back_len = 0;
  253. while (queue->head) {
  254. (void)memset(tms, 0, sizeof(tms));
  255. (void)memset(tdi, 0, sizeof(tdi));
  256. (void)memset(tdo, 0, sizeof(tdo));
  257. int seq_len;
  258. int swap_len;
  259. struct sequence *seq;
  260. /* Copy from queue to tms/tdi streams
  261. */
  262. seq = queue->head;
  263. seq_len = seq_back_len;
  264. swap_len = 0;
  265. while (seq && swap_len != OSBDM_SWAP_MAX) {
  266. /* Count bit for copy at this iteration.
  267. * len should fit into remaining space
  268. * in tms/tdo bitstreams
  269. */
  270. int len = seq->len - seq_len;
  271. if (len > OSBDM_SWAP_MAX - swap_len)
  272. len = OSBDM_SWAP_MAX - swap_len;
  273. /* Set tms data */
  274. buf_set_buf(seq->tms, seq_len, tms, swap_len, len);
  275. /* Set tdi data if they exists */
  276. if (seq->tdi)
  277. buf_set_buf(seq->tdi, seq_len, tdi, swap_len, len);
  278. swap_len += len;
  279. seq_len += len;
  280. if (seq_len == seq->len) {
  281. seq = seq->next; /* Move to next sequence */
  282. seq_len = 0;
  283. }
  284. }
  285. if (osbdm_swap(osbdm, tms, tdi, tdo, swap_len))
  286. return ERROR_FAIL;
  287. /* Copy from tdo stream to queue
  288. */
  289. for (int swap_back_len = 0; swap_back_len < swap_len; ) {
  290. int len = queue->head->len - seq_back_len;
  291. if (len > swap_len - swap_back_len)
  292. len = swap_len - swap_back_len;
  293. if (queue->head->tdo)
  294. buf_set_buf(tdo, swap_back_len, queue->head->tdo, seq_back_len, len);
  295. swap_back_len += len;
  296. seq_back_len += len;
  297. if (seq_back_len == queue->head->len) {
  298. queue_drop_head(queue);
  299. seq_back_len = 0;
  300. }
  301. }
  302. }
  303. return ERROR_OK;
  304. }
  305. /* Basic operation for opening USB device */
  306. static int osbdm_open(struct osbdm *osbdm)
  307. {
  308. (void)memset(osbdm, 0, sizeof(*osbdm));
  309. if (jtag_libusb_open(osbdm_vid, osbdm_pid, NULL, &osbdm->devh) != ERROR_OK)
  310. return ERROR_FAIL;
  311. if (jtag_libusb_claim_interface(osbdm->devh, 0) != ERROR_OK)
  312. return ERROR_FAIL;
  313. return ERROR_OK;
  314. }
  315. static int osbdm_quit(void)
  316. {
  317. jtag_libusb_close(osbdm_context.devh);
  318. return ERROR_OK;
  319. }
  320. static int osbdm_add_pathmove(
  321. struct queue *queue,
  322. tap_state_t *path,
  323. int num_states)
  324. {
  325. assert(num_states <= 32);
  326. struct sequence *next = queue_add_tail(queue, num_states);
  327. if (!next) {
  328. LOG_ERROR("BUG: can't allocate bit sequence");
  329. return ERROR_FAIL;
  330. }
  331. uint32_t tms = 0;
  332. for (int i = 0; i < num_states; i++) {
  333. if (tap_state_transition(tap_get_state(), 1) == path[i]) {
  334. tms |= (1 << i);
  335. } else if (tap_state_transition(tap_get_state(), 0) == path[i]) {
  336. tms &= ~(1 << i); /* This line not so needed */
  337. } else {
  338. LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition",
  339. tap_state_name(tap_get_state()),
  340. tap_state_name(path[i]));
  341. return ERROR_FAIL;
  342. }
  343. tap_set_state(path[i]);
  344. }
  345. buf_set_u32(next->tms, 0, num_states, tms);
  346. tap_set_end_state(tap_get_state());
  347. return ERROR_OK;
  348. }
  349. static int osbdm_add_statemove(
  350. struct queue *queue,
  351. tap_state_t new_state,
  352. int skip_first)
  353. {
  354. int len = 0;
  355. int tms = 0;
  356. tap_set_end_state(new_state);
  357. if (tap_get_end_state() == TAP_RESET) {
  358. /* Ignore current state */
  359. tms = 0xff;
  360. len = 5;
  361. } else if (tap_get_state() != tap_get_end_state()) {
  362. tms = tap_get_tms_path(tap_get_state(), new_state);
  363. len = tap_get_tms_path_len(tap_get_state(), new_state);
  364. }
  365. if (len && skip_first) {
  366. len--;
  367. tms >>= 1;
  368. }
  369. if (len) {
  370. struct sequence *next = queue_add_tail(queue, len);
  371. if (!next) {
  372. LOG_ERROR("BUG: can't allocate bit sequence");
  373. return ERROR_FAIL;
  374. }
  375. buf_set_u32(next->tms, 0, len, tms);
  376. }
  377. tap_set_state(tap_get_end_state());
  378. return ERROR_OK;
  379. }
  380. static int osbdm_add_stableclocks(
  381. struct queue *queue,
  382. int count)
  383. {
  384. if (!tap_is_state_stable(tap_get_state())) {
  385. LOG_ERROR("BUG: current state (%s) is not stable",
  386. tap_state_name(tap_get_state()));
  387. return ERROR_FAIL;
  388. }
  389. struct sequence *next = queue_add_tail(queue, count);
  390. if (!next) {
  391. LOG_ERROR("BUG: can't allocate bit sequence");
  392. return ERROR_FAIL;
  393. }
  394. if (tap_get_state() == TAP_RESET)
  395. (void)memset(next->tms, 0xff, DIV_ROUND_UP(count, 8));
  396. return ERROR_OK;
  397. }
  398. static int osbdm_add_tms(
  399. struct queue *queue,
  400. const uint8_t *tms,
  401. int num_bits)
  402. {
  403. struct sequence *next = queue_add_tail(queue, num_bits);
  404. if (!next) {
  405. LOG_ERROR("BUG: can't allocate bit sequence");
  406. return ERROR_FAIL;
  407. }
  408. buf_set_buf(tms, 0, next->tms, 0, num_bits);
  409. return ERROR_OK;
  410. }
  411. static int osbdm_add_scan(
  412. struct queue *queue,
  413. struct scan_field *fields,
  414. int num_fields,
  415. tap_state_t end_state,
  416. bool ir_scan)
  417. {
  418. /* Move to desired shift state */
  419. if (ir_scan) {
  420. if (tap_get_state() != TAP_IRSHIFT) {
  421. if (osbdm_add_statemove(queue, TAP_IRSHIFT, 0) != ERROR_OK)
  422. return ERROR_FAIL;
  423. }
  424. } else {
  425. if (tap_get_state() != TAP_DRSHIFT) {
  426. if (osbdm_add_statemove(queue, TAP_DRSHIFT, 0) != ERROR_OK)
  427. return ERROR_FAIL;
  428. }
  429. }
  430. /* Add scan */
  431. tap_set_end_state(end_state);
  432. for (int idx = 0; idx < num_fields; idx++) {
  433. struct sequence *next = queue_add_tail(queue, fields[idx].num_bits);
  434. if (!next) {
  435. LOG_ERROR("Can't allocate bit sequence");
  436. return ERROR_FAIL;
  437. }
  438. (void)memset(next->tms, 0, DIV_ROUND_UP(fields[idx].num_bits, 8));
  439. next->tdi = fields[idx].out_value;
  440. next->tdo = fields[idx].in_value;
  441. }
  442. /* Move to end state
  443. */
  444. if (tap_get_state() != tap_get_end_state()) {
  445. /* Exit from IRSHIFT/DRSHIFT */
  446. buf_set_u32(queue->tail->tms, queue->tail->len - 1, 1, 1);
  447. /* Move with skip_first flag */
  448. if (osbdm_add_statemove(queue, tap_get_end_state(), 1) != ERROR_OK)
  449. return ERROR_FAIL;
  450. }
  451. return ERROR_OK;
  452. }
  453. static int osbdm_add_runtest(
  454. struct queue *queue,
  455. int num_cycles,
  456. tap_state_t end_state)
  457. {
  458. if (osbdm_add_statemove(queue, TAP_IDLE, 0) != ERROR_OK)
  459. return ERROR_FAIL;
  460. if (osbdm_add_stableclocks(queue, num_cycles) != ERROR_OK)
  461. return ERROR_FAIL;
  462. if (osbdm_add_statemove(queue, end_state, 0) != ERROR_OK)
  463. return ERROR_FAIL;
  464. return ERROR_OK;
  465. }
  466. static int osbdm_execute_command(
  467. struct osbdm *osbdm,
  468. struct queue *queue,
  469. struct jtag_command *cmd)
  470. {
  471. int retval = ERROR_OK;
  472. switch (cmd->type) {
  473. case JTAG_RESET:
  474. if (cmd->cmd.reset->trst) {
  475. LOG_ERROR("BUG: nTRST signal is not supported");
  476. retval = ERROR_FAIL;
  477. } else {
  478. retval = osbdm_flush(osbdm, queue);
  479. if (retval == ERROR_OK)
  480. retval = osbdm_srst(osbdm, cmd->cmd.reset->srst);
  481. }
  482. break;
  483. case JTAG_PATHMOVE:
  484. retval = osbdm_add_pathmove(
  485. queue,
  486. cmd->cmd.pathmove->path,
  487. cmd->cmd.pathmove->num_states);
  488. break;
  489. case JTAG_TLR_RESET:
  490. retval = osbdm_add_statemove(
  491. queue,
  492. cmd->cmd.statemove->end_state,
  493. 0);
  494. break;
  495. case JTAG_STABLECLOCKS:
  496. retval = osbdm_add_stableclocks(
  497. queue,
  498. cmd->cmd.stableclocks->num_cycles);
  499. break;
  500. case JTAG_TMS:
  501. retval = osbdm_add_tms(
  502. queue,
  503. cmd->cmd.tms->bits,
  504. cmd->cmd.tms->num_bits);
  505. break;
  506. case JTAG_SCAN:
  507. retval = osbdm_add_scan(
  508. queue,
  509. cmd->cmd.scan->fields,
  510. cmd->cmd.scan->num_fields,
  511. cmd->cmd.scan->end_state,
  512. cmd->cmd.scan->ir_scan);
  513. break;
  514. case JTAG_SLEEP:
  515. retval = osbdm_flush(osbdm, queue);
  516. if (retval == ERROR_OK)
  517. jtag_sleep(cmd->cmd.sleep->us);
  518. break;
  519. case JTAG_RUNTEST:
  520. retval = osbdm_add_runtest(
  521. queue,
  522. cmd->cmd.runtest->num_cycles,
  523. cmd->cmd.runtest->end_state);
  524. break;
  525. default:
  526. LOG_ERROR("BUG: unknown JTAG command type encountered");
  527. retval = ERROR_FAIL;
  528. break;
  529. }
  530. return retval;
  531. }
  532. static int osbdm_execute_queue(void)
  533. {
  534. int retval = ERROR_OK;
  535. struct queue *queue = queue_alloc();
  536. if (!queue) {
  537. LOG_ERROR("BUG: can't allocate bit queue");
  538. retval = ERROR_FAIL;
  539. } else {
  540. struct jtag_command *cmd = jtag_command_queue;
  541. while (retval == ERROR_OK && cmd) {
  542. retval = osbdm_execute_command(&osbdm_context, queue, cmd);
  543. cmd = cmd->next;
  544. }
  545. if (retval == ERROR_OK)
  546. retval = osbdm_flush(&osbdm_context, queue);
  547. queue_free(queue);
  548. }
  549. if (retval != ERROR_OK) {
  550. LOG_ERROR("FATAL: can't execute jtag command");
  551. exit(-1);
  552. }
  553. return retval;
  554. }
  555. static int osbdm_init(void)
  556. {
  557. /* Open device */
  558. if (osbdm_open(&osbdm_context) != ERROR_OK) {
  559. LOG_ERROR("Can't open OSBDM device");
  560. return ERROR_FAIL;
  561. } else {
  562. /* Device successfully opened */
  563. LOG_DEBUG("OSBDM init");
  564. }
  565. /* Perform initialize command */
  566. osbdm_context.count = 0;
  567. osbdm_context.buffer[osbdm_context.count++] = OSBDM_CMD_INIT;
  568. if (osbdm_send_and_recv(&osbdm_context) != ERROR_OK)
  569. return ERROR_FAIL;
  570. return ERROR_OK;
  571. }
  572. struct jtag_interface osbdm_interface = {
  573. .name = "osbdm",
  574. .transports = jtag_only,
  575. .execute_queue = osbdm_execute_queue,
  576. .init = osbdm_init,
  577. .quit = osbdm_quit
  578. };