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.
 
 
 
 
 
 

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