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.
 
 
 
 
 
 

1892 lines
40 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 Rob Brown, Lou Deluxe *
  9. * rob@cobbleware.com, lou.openocd012@fixit.nospammail.net *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. /* project specific includes */
  30. #include "interface.h"
  31. #include "commands.h"
  32. #include "rlink.h"
  33. #include "st7.h"
  34. #include "ep1_cmd.h"
  35. #include "dtc_cmd.h"
  36. /* system includes */
  37. #include <usb.h>
  38. /* This feature is made useless by running the DTC all the time. When automatic, the LED is on whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */
  39. #undef AUTOMATIC_BUSY_LED
  40. /* This feature may require derating the speed due to reduced hold time. */
  41. #undef USE_HARDWARE_SHIFTER_FOR_TMS
  42. #define INTERFACE_NAME "RLink"
  43. #define USB_IDVENDOR (0x138e)
  44. #define USB_IDPRODUCT (0x9000)
  45. #define USB_EP1OUT_ADDR (0x01)
  46. #define USB_EP1OUT_SIZE (16)
  47. #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
  48. #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
  49. #define USB_EP2OUT_ADDR (0x02)
  50. #define USB_EP2OUT_SIZE (64)
  51. #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
  52. #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
  53. #define USB_EP2BANK_SIZE (512)
  54. #define USB_TIMEOUT_MS (3 * 1000)
  55. #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
  56. #define ST7_PD_NBUSY_LED ST7_PD0
  57. #define ST7_PD_NRUN_LED ST7_PD1
  58. /* low enables VPP at adapter header, high connects it to GND instead */
  59. #define ST7_PD_VPP_SEL ST7_PD6
  60. /* low: VPP = 12v, high: VPP <= 5v */
  61. #define ST7_PD_VPP_SHDN ST7_PD7
  62. /* These pins are connected together */
  63. #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
  64. #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
  65. /* Symbolic mapping between port pins and numbered IO lines */
  66. #define ST7_PA_IO1 ST7_PA1
  67. #define ST7_PA_IO2 ST7_PA2
  68. #define ST7_PA_IO4 ST7_PA4
  69. #define ST7_PA_IO8 ST7_PA6
  70. #define ST7_PA_IO10 ST7_PA7
  71. #define ST7_PB_IO5 ST7_PB5
  72. #define ST7_PC_IO9 ST7_PC1
  73. #define ST7_PC_IO3 ST7_PC2
  74. #define ST7_PC_IO7 ST7_PC3
  75. #define ST7_PE_IO6 ST7_PE5
  76. /* Symbolic mapping between numbered IO lines and adapter signals */
  77. #define ST7_PA_RTCK ST7_PA_IO0
  78. #define ST7_PA_NTRST ST7_PA_IO1
  79. #define ST7_PC_TDI ST7_PC_IO3
  80. #define ST7_PA_DBGRQ ST7_PA_IO4
  81. #define ST7_PB_NSRST ST7_PB_IO5
  82. #define ST7_PE_TMS ST7_PE_IO6
  83. #define ST7_PC_TCK ST7_PC_IO7
  84. #define ST7_PC_TDO ST7_PC_IO9
  85. #define ST7_PA_DBGACK ST7_PA_IO10
  86. static usb_dev_handle *pHDev;
  87. /*
  88. * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
  89. * This function takes care of zeroing the unused bytes before sending the packet.
  90. * Any reply packet is not handled by this function.
  91. */
  92. static
  93. int
  94. ep1_generic_commandl(
  95. usb_dev_handle *pHDev,
  96. size_t length,
  97. ...
  98. ) {
  99. uint8_t usb_buffer[USB_EP1OUT_SIZE];
  100. uint8_t *usb_buffer_p;
  101. va_list ap;
  102. int usb_ret;
  103. if (length > sizeof(usb_buffer)) {
  104. length = sizeof(usb_buffer);
  105. }
  106. usb_buffer_p = usb_buffer;
  107. va_start(ap, length);
  108. while (length > 0) {
  109. *usb_buffer_p++ = va_arg(ap, int);
  110. length--;
  111. }
  112. memset(
  113. usb_buffer_p,
  114. 0,
  115. sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
  116. );
  117. usb_ret = usb_bulk_write(
  118. pHDev,
  119. USB_EP1OUT_ADDR,
  120. (char *)usb_buffer, sizeof(usb_buffer),
  121. USB_TIMEOUT_MS
  122. );
  123. return(usb_ret);
  124. }
  125. #if 0
  126. static
  127. ssize_t
  128. ep1_memory_read(
  129. usb_dev_handle *pHDev,
  130. uint16_t addr,
  131. size_t length,
  132. uint8_t *buffer
  133. ) {
  134. uint8_t usb_buffer[USB_EP1OUT_SIZE];
  135. int usb_ret;
  136. size_t remain;
  137. ssize_t count;
  138. usb_buffer[0] = EP1_CMD_MEMORY_READ;
  139. memset(
  140. usb_buffer + 4,
  141. 0,
  142. sizeof(usb_buffer) - 4
  143. );
  144. remain = length;
  145. count = 0;
  146. while (remain) {
  147. if (remain > sizeof(usb_buffer)) {
  148. length = sizeof(usb_buffer);
  149. } else {
  150. length = remain;
  151. }
  152. usb_buffer[1] = addr >> 8;
  153. usb_buffer[2] = addr;
  154. usb_buffer[3] = length;
  155. usb_ret = usb_bulk_write(
  156. pHDev, USB_EP1OUT_ADDR,
  157. usb_buffer, sizeof(usb_buffer),
  158. USB_TIMEOUT_MS
  159. );
  160. if (usb_ret < sizeof(usb_buffer)) {
  161. break;
  162. }
  163. usb_ret = usb_bulk_read(
  164. pHDev, USB_EP1IN_ADDR,
  165. buffer, length,
  166. USB_TIMEOUT_MS
  167. );
  168. if (usb_ret < length) {
  169. break;
  170. }
  171. addr += length;
  172. buffer += length;
  173. count += length;
  174. remain -= length;
  175. }
  176. return(count);
  177. }
  178. #endif
  179. static
  180. ssize_t
  181. ep1_memory_write(
  182. usb_dev_handle *pHDev,
  183. uint16_t addr,
  184. size_t length,
  185. uint8_t const *buffer
  186. ) {
  187. uint8_t usb_buffer[USB_EP1OUT_SIZE];
  188. int usb_ret;
  189. size_t remain;
  190. ssize_t count;
  191. usb_buffer[0] = EP1_CMD_MEMORY_WRITE;
  192. remain = length;
  193. count = 0;
  194. while (remain) {
  195. if (remain > (sizeof(usb_buffer) - 4)) {
  196. length = (sizeof(usb_buffer) - 4);
  197. } else {
  198. length = remain;
  199. }
  200. usb_buffer[1] = addr >> 8;
  201. usb_buffer[2] = addr;
  202. usb_buffer[3] = length;
  203. memcpy(
  204. usb_buffer + 4,
  205. buffer,
  206. length
  207. );
  208. memset(
  209. usb_buffer + 4 + length,
  210. 0,
  211. sizeof(usb_buffer) - 4 - length
  212. );
  213. usb_ret = usb_bulk_write(
  214. pHDev, USB_EP1OUT_ADDR,
  215. (char *)usb_buffer, sizeof(usb_buffer),
  216. USB_TIMEOUT_MS
  217. );
  218. if ((size_t)usb_ret < sizeof(usb_buffer)) {
  219. break;
  220. }
  221. addr += length;
  222. buffer += length;
  223. count += length;
  224. remain -= length;
  225. }
  226. return(count);
  227. }
  228. #if 0
  229. static
  230. ssize_t
  231. ep1_memory_writel(
  232. usb_dev_handle *pHDev,
  233. uint16_t addr,
  234. size_t length,
  235. ...
  236. ) {
  237. uint8_t buffer[USB_EP1OUT_SIZE - 4];
  238. uint8_t *buffer_p;
  239. va_list ap;
  240. size_t remain;
  241. if (length > sizeof(buffer)) {
  242. length = sizeof(buffer);
  243. }
  244. remain = length;
  245. buffer_p = buffer;
  246. va_start(ap, length);
  247. while (remain > 0) {
  248. *buffer_p++ = va_arg(ap, int);
  249. remain--;
  250. }
  251. return(ep1_memory_write(pHDev, addr, length, buffer));
  252. }
  253. #endif
  254. #define DTCLOAD_COMMENT (0)
  255. #define DTCLOAD_ENTRY (1)
  256. #define DTCLOAD_LOAD (2)
  257. #define DTCLOAD_RUN (3)
  258. #define DTCLOAD_LUT_START (4)
  259. #define DTCLOAD_LUT (5)
  260. #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
  261. /* This gets set by the DTC loader */
  262. static uint8_t dtc_entry_download;
  263. /* The buffer is specially formatted to represent a valid image to load into the DTC. */
  264. static
  265. int
  266. dtc_load_from_buffer(
  267. usb_dev_handle *pHDev,
  268. const uint8_t *buffer,
  269. size_t length
  270. ) {
  271. struct header_s {
  272. uint8_t type;
  273. uint8_t length;
  274. };
  275. int usb_err;
  276. struct header_s *header;
  277. uint8_t lut_start = 0xc0;
  278. dtc_entry_download = 0;
  279. /* Stop the DTC before loading anything. */
  280. usb_err = ep1_generic_commandl(
  281. pHDev, 1,
  282. EP1_CMD_DTC_STOP
  283. );
  284. if (usb_err < 0) return(usb_err);
  285. while (length) {
  286. if (length < sizeof(*header)) {
  287. LOG_ERROR("Malformed DTC image\n");
  288. exit(1);
  289. }
  290. header = (struct header_s *)buffer;
  291. buffer += sizeof(*header);
  292. length -= sizeof(*header);
  293. if (length < (size_t)header->length + 1) {
  294. LOG_ERROR("Malformed DTC image\n");
  295. exit(1);
  296. }
  297. switch (header->type) {
  298. case DTCLOAD_COMMENT:
  299. break;
  300. case DTCLOAD_ENTRY:
  301. /* store entry addresses somewhere */
  302. if (!strncmp("download", (char *)buffer + 1, 8)) {
  303. dtc_entry_download = buffer[0];
  304. }
  305. break;
  306. case DTCLOAD_LOAD:
  307. /* Send the DTC program to ST7 RAM. */
  308. usb_err = ep1_memory_write(
  309. pHDev,
  310. DTC_LOAD_BUFFER,
  311. header->length + 1, buffer
  312. );
  313. if (usb_err < 0) return(usb_err);
  314. /* Load it into the DTC. */
  315. usb_err = ep1_generic_commandl(
  316. pHDev, 3,
  317. EP1_CMD_DTC_LOAD,
  318. (DTC_LOAD_BUFFER >> 8),
  319. DTC_LOAD_BUFFER
  320. );
  321. if (usb_err < 0) return(usb_err);
  322. break;
  323. case DTCLOAD_RUN:
  324. usb_err = ep1_generic_commandl(
  325. pHDev, 3,
  326. EP1_CMD_DTC_CALL,
  327. buffer[0],
  328. EP1_CMD_DTC_WAIT
  329. );
  330. if (usb_err < 0) return(usb_err);
  331. break;
  332. case DTCLOAD_LUT_START:
  333. lut_start = buffer[0];
  334. break;
  335. case DTCLOAD_LUT:
  336. usb_err = ep1_memory_write(
  337. pHDev,
  338. ST7_USB_BUF_EP0OUT + lut_start,
  339. header->length + 1, buffer
  340. );
  341. if (usb_err < 0) return(usb_err);
  342. break;
  343. default:
  344. LOG_ERROR("Invalid DTC image record type: 0x%02x\n", header->type);
  345. exit(1);
  346. break;
  347. }
  348. buffer += (header->length + 1);
  349. length -= (header->length + 1);
  350. }
  351. return(0);
  352. }
  353. /*
  354. * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
  355. */
  356. static
  357. int
  358. dtc_start_download(void) {
  359. int usb_err;
  360. uint8_t ep2txr;
  361. /* set up for download mode and make sure EP2 is set up to transmit */
  362. usb_err = ep1_generic_commandl(
  363. pHDev, 7,
  364. EP1_CMD_DTC_STOP,
  365. EP1_CMD_SET_UPLOAD,
  366. EP1_CMD_SET_DOWNLOAD,
  367. EP1_CMD_MEMORY_READ, /* read EP2TXR for its data toggle */
  368. ST7_EP2TXR >> 8,
  369. ST7_EP2TXR,
  370. 1
  371. );
  372. if (usb_err < 0) return(usb_err);
  373. /* read back ep2txr */
  374. usb_err = usb_bulk_read(
  375. pHDev, USB_EP1IN_ADDR,
  376. (char *)&ep2txr, 1,
  377. USB_TIMEOUT_MS
  378. );
  379. if (usb_err < 0) return(usb_err);
  380. usb_err = ep1_generic_commandl(
  381. pHDev, 13,
  382. EP1_CMD_MEMORY_WRITE, /* preinitialize poll byte */
  383. DTC_STATUS_POLL_BYTE >> 8,
  384. DTC_STATUS_POLL_BYTE,
  385. 1,
  386. 0x00,
  387. EP1_CMD_MEMORY_WRITE, /* set EP2IN to return data */
  388. ST7_EP2TXR >> 8,
  389. ST7_EP2TXR,
  390. 1,
  391. (ep2txr & ST7_EP2TXR_DTOG_TX) | ST7_EP2TXR_STAT_VALID,
  392. EP1_CMD_DTC_CALL, /* start running the DTC */
  393. dtc_entry_download,
  394. EP1_CMD_DTC_GET_CACHED_STATUS
  395. );
  396. if (usb_err < 0) return(usb_err);
  397. /* wait for completion */
  398. usb_err = usb_bulk_read(
  399. pHDev, USB_EP1IN_ADDR,
  400. (char *)&ep2txr, 1,
  401. USB_TIMEOUT_MS
  402. );
  403. return(usb_err);
  404. }
  405. static
  406. int
  407. dtc_run_download(
  408. usb_dev_handle *pHDev,
  409. uint8_t *command_buffer,
  410. int command_buffer_size,
  411. uint8_t *reply_buffer,
  412. int reply_buffer_size
  413. ) {
  414. uint8_t ep2_buffer[USB_EP2IN_SIZE];
  415. int usb_err;
  416. int i;
  417. LOG_DEBUG(": %d/%d\n", command_buffer_size, reply_buffer_size);
  418. usb_err = usb_bulk_write(
  419. pHDev,
  420. USB_EP2OUT_ADDR,
  421. (char *)command_buffer, USB_EP2BANK_SIZE,
  422. USB_TIMEOUT_MS
  423. );
  424. if (usb_err < 0) return(usb_err);
  425. /* Wait for DTC to finish running command buffer */
  426. for (i = 10;;) {
  427. usb_err = ep1_generic_commandl(
  428. pHDev, 4,
  429. EP1_CMD_MEMORY_READ,
  430. DTC_STATUS_POLL_BYTE >> 8,
  431. DTC_STATUS_POLL_BYTE,
  432. 1
  433. );
  434. if (usb_err < 0) return(usb_err);
  435. usb_err = usb_bulk_read(
  436. pHDev,
  437. USB_EP1IN_ADDR,
  438. (char *)ep2_buffer, 1,
  439. USB_TIMEOUT_MS
  440. );
  441. if (usb_err < 0) return(usb_err);
  442. if (ep2_buffer[0] & 0x01) break;
  443. if (!--i) {
  444. LOG_ERROR("%s, %d: too many retries waiting for DTC status\n",
  445. __FILE__, __LINE__
  446. );
  447. return(-ETIMEDOUT);
  448. }
  449. }
  450. if (!reply_buffer) reply_buffer_size = 0;
  451. if (reply_buffer_size) {
  452. usb_err = usb_bulk_read(
  453. pHDev,
  454. USB_EP2IN_ADDR,
  455. (char *)ep2_buffer, sizeof(ep2_buffer),
  456. USB_TIMEOUT_MS
  457. );
  458. if (usb_err < (int)sizeof(ep2_buffer)) {
  459. LOG_ERROR("%s, %d: Read of endpoint 2 returned %d\n",
  460. __FILE__, __LINE__, usb_err
  461. );
  462. return(usb_err);
  463. }
  464. memcpy(reply_buffer, ep2_buffer, reply_buffer_size);
  465. }
  466. return(usb_err);
  467. }
  468. /*
  469. * The dtc reply queue is a singly linked list that describes what to do with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate these entries.
  470. */
  471. typedef
  472. struct dtc_reply_queue_entry_s {
  473. struct dtc_reply_queue_entry_s *next;
  474. jtag_command_t *cmd; /* the command that resulted in this entry */
  475. struct {
  476. uint8_t *buffer; /* the scan buffer */
  477. int size; /* size of the scan buffer in bits */
  478. int offset; /* how many bits were already done before this? */
  479. int length; /* how many bits are processed in this operation? */
  480. enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */
  481. } scan;
  482. } dtc_reply_queue_entry_t;
  483. /*
  484. * The dtc_queue consists of a buffer of pending commands and a reply queue.
  485. * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
  486. */
  487. static
  488. struct {
  489. dtc_reply_queue_entry_t *rq_head;
  490. dtc_reply_queue_entry_t *rq_tail;
  491. uint32_t cmd_index;
  492. uint32_t reply_index;
  493. uint8_t cmd_buffer[USB_EP2BANK_SIZE];
  494. } dtc_queue;
  495. /*
  496. * The tap state queue is for accumulating TAP state changes wiithout needlessly flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to the dtc_queue.
  497. */
  498. static
  499. struct {
  500. uint32_t length;
  501. uint32_t buffer;
  502. } tap_state_queue;
  503. static
  504. int
  505. dtc_queue_init(void) {
  506. dtc_queue.rq_head = NULL;
  507. dtc_queue.rq_tail = NULL;
  508. dtc_queue.cmd_index = 0;
  509. dtc_queue.reply_index = 0;
  510. return(0);
  511. }
  512. static
  513. inline
  514. dtc_reply_queue_entry_t *
  515. dtc_queue_enqueue_reply(
  516. enum scan_type type,
  517. uint8_t *buffer,
  518. int size,
  519. int offset,
  520. int length,
  521. jtag_command_t *cmd
  522. ) {
  523. dtc_reply_queue_entry_t *rq_entry;
  524. rq_entry = malloc(sizeof(dtc_reply_queue_entry_t));
  525. if (rq_entry != NULL) {
  526. rq_entry->scan.type = type;
  527. rq_entry->scan.buffer = buffer;
  528. rq_entry->scan.size = size;
  529. rq_entry->scan.offset = offset;
  530. rq_entry->scan.length = length;
  531. rq_entry->cmd = cmd;
  532. rq_entry->next = NULL;
  533. if (dtc_queue.rq_head == NULL)
  534. dtc_queue.rq_head = rq_entry;
  535. else
  536. dtc_queue.rq_tail->next = rq_entry;
  537. dtc_queue.rq_tail = rq_entry;
  538. }
  539. return(rq_entry);
  540. }
  541. /*
  542. * Running the queue means that any pending command buffer is run and any reply data dealt with. The command buffer is then cleared for subsequent processing.
  543. * The queue is automatically run by append when it is necessary to get space for the append.
  544. */
  545. static
  546. int
  547. dtc_queue_run(void) {
  548. dtc_reply_queue_entry_t *rq_p, *rq_next;
  549. int retval;
  550. int usb_err;
  551. int bit_cnt;
  552. int x;
  553. uint8_t *dtc_p, *tdo_p;
  554. uint8_t dtc_mask, tdo_mask;
  555. uint8_t reply_buffer[USB_EP2IN_SIZE];
  556. retval = ERROR_OK;
  557. if (dtc_queue.cmd_index < 1) return(retval);
  558. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;
  559. /* run the cmd */
  560. if (dtc_queue.rq_head == NULL) {
  561. usb_err = dtc_run_download(pHDev,
  562. dtc_queue.cmd_buffer, dtc_queue.cmd_index,
  563. NULL, 0
  564. );
  565. if (usb_err < 0) {
  566. LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
  567. exit(1);
  568. }
  569. } else {
  570. usb_err = dtc_run_download(pHDev,
  571. dtc_queue.cmd_buffer, dtc_queue.cmd_index,
  572. reply_buffer, dtc_queue.reply_index
  573. );
  574. if (usb_err < 0) {
  575. LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
  576. exit(1);
  577. } else {
  578. /* process the reply, which empties the reply queue and frees its entries */
  579. dtc_p = reply_buffer;
  580. /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It was that or craft a function to do the reversal, and that wouldn't work with bit-stuffing (supplying extra bits to use mostly byte operations), or any other scheme which would throw the byte alignment off. */
  581. for (
  582. rq_p = dtc_queue.rq_head;
  583. rq_p != NULL;
  584. rq_p = rq_next
  585. ) {
  586. tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
  587. tdo_mask = 1 << (rq_p->scan.offset % 8);
  588. bit_cnt = rq_p->scan.length;
  589. if (bit_cnt >= 8) {
  590. /* bytes */
  591. dtc_mask = 1 << (8 - 1);
  592. for (
  593. ;
  594. bit_cnt;
  595. bit_cnt--
  596. ) {
  597. if (*dtc_p & dtc_mask) {
  598. *tdo_p |= tdo_mask;
  599. } else {
  600. *tdo_p &=~ tdo_mask;
  601. }
  602. dtc_mask >>= 1;
  603. if (dtc_mask == 0) {
  604. dtc_p++;
  605. dtc_mask = 1 << (8 - 1);
  606. }
  607. tdo_mask <<= 1;
  608. if (tdo_mask == 0) {
  609. tdo_p++;
  610. tdo_mask = 1;
  611. }
  612. }
  613. } else {
  614. /* extra bits or last bit */
  615. x = *dtc_p++;
  616. if ((
  617. rq_p->scan.type == SCAN_IN
  618. ) && (
  619. rq_p->scan.offset != rq_p->scan.size - 1
  620. )) {
  621. /* extra bits were sent as a full byte with padding on the end */
  622. dtc_mask = 1 << (8 - 1);
  623. } else {
  624. dtc_mask = 1 << (bit_cnt - 1);
  625. }
  626. for (
  627. ;
  628. bit_cnt;
  629. bit_cnt--
  630. ) {
  631. if (x & dtc_mask) {
  632. *tdo_p |= tdo_mask;
  633. } else {
  634. *tdo_p &=~ tdo_mask;
  635. }
  636. dtc_mask >>= 1;
  637. tdo_mask <<= 1;
  638. if (tdo_mask == 0) {
  639. tdo_p++;
  640. tdo_mask = 1;
  641. }
  642. }
  643. }
  644. if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
  645. /* feed scan buffer back into openocd and free it */
  646. if (jtag_read_buffer(rq_p->scan.buffer, rq_p->cmd->cmd.scan) != ERROR_OK) {
  647. retval = ERROR_JTAG_QUEUE_FAILED;
  648. }
  649. free(rq_p->scan.buffer);
  650. }
  651. rq_next = rq_p->next;
  652. free(rq_p);
  653. }
  654. dtc_queue.rq_head = NULL;
  655. dtc_queue.rq_tail = NULL;
  656. }
  657. }
  658. /* reset state for new appends */
  659. dtc_queue.cmd_index = 0;
  660. dtc_queue.reply_index = 0;
  661. return(retval);
  662. }
  663. static
  664. int
  665. tap_state_queue_init(void) {
  666. tap_state_queue.length = 0;
  667. tap_state_queue.buffer = 0;
  668. return(0);
  669. }
  670. static
  671. int
  672. tap_state_queue_run(void) {
  673. int i;
  674. int bits;
  675. uint8_t byte;
  676. int retval;
  677. retval = 0;
  678. if (!tap_state_queue.length) return(retval);
  679. bits = 1;
  680. byte = 0;
  681. for (i = tap_state_queue.length; i--;) {
  682. byte <<= 1;
  683. if (tap_state_queue.buffer & 1) {
  684. byte |= 1;
  685. }
  686. if ((bits >= 8) || !i) {
  687. byte <<= (8 - bits);
  688. /* make sure there's room for stop, byte op, and one byte */
  689. if (dtc_queue.cmd_index >= (sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))) {
  690. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
  691. DTC_CMD_STOP;
  692. dtc_queue_run();
  693. }
  694. #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
  695. if (bits == 8) {
  696. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
  697. DTC_CMD_SHIFT_TMS_BYTES(1);
  698. } else {
  699. #endif
  700. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
  701. DTC_CMD_SHIFT_TMS_BITS(bits);
  702. #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
  703. }
  704. #endif
  705. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
  706. byte;
  707. byte = 0;
  708. bits = 1;
  709. } else {
  710. bits++;
  711. }
  712. tap_state_queue.buffer >>= 1;
  713. }
  714. retval = tap_state_queue_init();
  715. return(retval);
  716. }
  717. static
  718. int
  719. tap_state_queue_append(
  720. uint8_t tms
  721. ) {
  722. int retval;
  723. if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
  724. retval = tap_state_queue_run();
  725. if (retval != 0) return(retval);
  726. }
  727. if (tms) {
  728. tap_state_queue.buffer |= (1 << tap_state_queue.length);
  729. }
  730. tap_state_queue.length++;
  731. return(0);
  732. }
  733. static
  734. void rlink_end_state(tap_state_t state)
  735. {
  736. if (tap_is_state_stable(state))
  737. tap_set_end_state(state);
  738. else
  739. {
  740. LOG_ERROR("BUG: %i is not a valid end state", state);
  741. exit(-1);
  742. }
  743. }
  744. static
  745. void rlink_state_move(void) {
  746. int i = 0, tms = 0;
  747. uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  748. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  749. for (i = 0; i < tms_count; i++)
  750. {
  751. tms = (tms_scan >> i) & 1;
  752. tap_state_queue_append(tms);
  753. }
  754. tap_set_state(tap_get_end_state());
  755. }
  756. static
  757. void rlink_path_move(pathmove_command_t *cmd)
  758. {
  759. int num_states = cmd->num_states;
  760. int state_count;
  761. int tms = 0;
  762. state_count = 0;
  763. while (num_states)
  764. {
  765. if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
  766. {
  767. tms = 0;
  768. }
  769. else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
  770. {
  771. tms = 1;
  772. }
  773. else
  774. {
  775. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
  776. exit(-1);
  777. }
  778. tap_state_queue_append(tms);
  779. tap_set_state(cmd->path[state_count]);
  780. state_count++;
  781. num_states--;
  782. }
  783. tap_set_end_state(tap_get_state());
  784. }
  785. static
  786. void rlink_runtest(int num_cycles)
  787. {
  788. int i;
  789. tap_state_t saved_end_state = tap_get_end_state();
  790. /* only do a state_move when we're not already in RTI */
  791. if (tap_get_state() != TAP_IDLE)
  792. {
  793. rlink_end_state(TAP_IDLE);
  794. rlink_state_move();
  795. }
  796. /* execute num_cycles */
  797. for (i = 0; i < num_cycles; i++)
  798. {
  799. tap_state_queue_append(0);
  800. }
  801. /* finish in end_state */
  802. rlink_end_state(saved_end_state);
  803. if (tap_get_state() != tap_get_end_state())
  804. rlink_state_move();
  805. }
  806. /* (1) assert or (0) deassert reset lines */
  807. static
  808. void rlink_reset(int trst, int srst)
  809. {
  810. uint8_t bitmap;
  811. int usb_err;
  812. /* Read port A for bit op */
  813. usb_err = ep1_generic_commandl(
  814. pHDev, 4,
  815. EP1_CMD_MEMORY_READ,
  816. ST7_PADR >> 8,
  817. ST7_PADR,
  818. 1
  819. );
  820. if (usb_err < 0) {
  821. LOG_ERROR("%s", usb_strerror());
  822. exit(1);
  823. }
  824. usb_err = usb_bulk_read(
  825. pHDev, USB_EP1IN_ADDR,
  826. (char *)&bitmap, 1,
  827. USB_TIMEOUT_MS
  828. );
  829. if (usb_err < 1) {
  830. LOG_ERROR("%s", usb_strerror());
  831. exit(1);
  832. }
  833. if (trst) {
  834. bitmap &= ~ST7_PA_NTRST;
  835. } else {
  836. bitmap |= ST7_PA_NTRST;
  837. }
  838. /* Write port A and read port B for bit op */
  839. /* port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0 and assert NSRST by setting DDR to 1. */
  840. usb_err = ep1_generic_commandl(
  841. pHDev, 9,
  842. EP1_CMD_MEMORY_WRITE,
  843. ST7_PADR >> 8,
  844. ST7_PADR,
  845. 1,
  846. bitmap,
  847. EP1_CMD_MEMORY_READ,
  848. ST7_PBDDR >> 8,
  849. ST7_PBDDR,
  850. 1
  851. );
  852. if (usb_err < 0) {
  853. LOG_ERROR("%s", usb_strerror());
  854. exit(1);
  855. }
  856. usb_err = usb_bulk_read(
  857. pHDev, USB_EP1IN_ADDR,
  858. (char *)&bitmap, 1,
  859. USB_TIMEOUT_MS
  860. );
  861. if (usb_err < 1) {
  862. LOG_ERROR("%s", usb_strerror());
  863. exit(1);
  864. }
  865. if (srst) {
  866. bitmap |= ST7_PB_NSRST;
  867. } else {
  868. bitmap &= ~ST7_PB_NSRST;
  869. }
  870. /* write port B and read dummy to ensure completion before returning */
  871. usb_err = ep1_generic_commandl(
  872. pHDev, 6,
  873. EP1_CMD_MEMORY_WRITE,
  874. ST7_PBDDR >> 8,
  875. ST7_PBDDR,
  876. 1,
  877. bitmap,
  878. EP1_CMD_DTC_GET_CACHED_STATUS
  879. );
  880. if (usb_err < 0) {
  881. LOG_ERROR("%s", usb_strerror());
  882. exit(1);
  883. }
  884. usb_err = usb_bulk_read(
  885. pHDev, USB_EP1IN_ADDR,
  886. (char *)&bitmap, 1,
  887. USB_TIMEOUT_MS
  888. );
  889. if (usb_err < 1) {
  890. LOG_ERROR("%s", usb_strerror());
  891. exit(1);
  892. }
  893. }
  894. static
  895. int
  896. rlink_scan(
  897. jtag_command_t *cmd,
  898. enum scan_type type,
  899. uint8_t *buffer,
  900. int scan_size
  901. ) {
  902. bool ir_scan;
  903. tap_state_t saved_end_state;
  904. int byte_bits;
  905. int extra_bits;
  906. int chunk_bits;
  907. int chunk_bytes;
  908. int x;
  909. int tdi_bit_offset;
  910. uint8_t tdi_mask, *tdi_p;
  911. uint8_t dtc_mask;
  912. if (scan_size < 1) {
  913. LOG_ERROR("scan_size cannot be less than 1 bit\n");
  914. exit(1);
  915. }
  916. ir_scan = cmd->cmd.scan->ir_scan;
  917. /* Move to the proper state before starting to shift TDI/TDO. */
  918. if (!(
  919. (!ir_scan && (tap_get_state() == TAP_DRSHIFT))
  920. ||
  921. (ir_scan && (tap_get_state() == TAP_IRSHIFT))
  922. )) {
  923. saved_end_state = tap_get_end_state();
  924. rlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  925. rlink_state_move();
  926. rlink_end_state(saved_end_state);
  927. }
  928. tap_state_queue_run();
  929. #if 0
  930. printf("scan_size = %d, type = 0x%x\n", scan_size, type);
  931. {
  932. int i;
  933. /* clear unused bits in scan buffer for ease of debugging */
  934. /* (it makes diffing output easier) */
  935. buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
  936. printf("before scan:");
  937. for (i = 0; i < (scan_size + 7) / 8; i++) {
  938. printf(" %02x", buffer[i]);
  939. }
  940. printf("\n");
  941. }
  942. #endif
  943. /* The number of bits that can be shifted as complete bytes */
  944. byte_bits = (int)(scan_size - 1) / 8 * 8;
  945. /* The number of bits left over, not counting the last bit */
  946. extra_bits = (scan_size - 1) - byte_bits;
  947. tdi_bit_offset = 0;
  948. tdi_p = buffer;
  949. tdi_mask = 1;
  950. if (extra_bits && (type == SCAN_OUT)) {
  951. /* Schedule any extra bits into the DTC command buffer, padding as needed */
  952. /* For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will fall off the end */
  953. /* make sure there's room for stop, byte op, and one byte */
  954. if (
  955. (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))
  956. ) {
  957. dtc_queue_run();
  958. }
  959. x = 0;
  960. dtc_mask = 1 << (extra_bits - 1);
  961. while (extra_bits--) {
  962. if (*tdi_p & tdi_mask) {
  963. x |= dtc_mask;
  964. }
  965. dtc_mask >>= 1;
  966. tdi_mask <<= 1;
  967. if (tdi_mask == 0) {
  968. tdi_p++;
  969. tdi_mask = 1;
  970. }
  971. }
  972. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
  973. DTC_CMD_SHIFT_TDI_BYTES(1);
  974. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
  975. }
  976. /* Loop scheduling full bytes into the DTC command buffer */
  977. while (byte_bits) {
  978. if (type == SCAN_IN) {
  979. /* make sure there's room for stop and byte op */
  980. x = (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1));
  981. } else {
  982. /* make sure there's room for stop, byte op, and at least one byte */
  983. x = (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1));
  984. }
  985. if (type != SCAN_OUT) {
  986. /* make sure there's room for at least one reply byte */
  987. x |= (dtc_queue.reply_index >= USB_EP2IN_SIZE - (1));
  988. }
  989. if (x) {
  990. dtc_queue_run();
  991. }
  992. chunk_bits = byte_bits;
  993. /* we can only use up to 16 bytes at a time */
  994. if (chunk_bits > (16 * 8)) chunk_bits = (16 * 8);
  995. if (type != SCAN_IN) {
  996. /* how much is there room for, considering stop and byte op? */
  997. x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8;
  998. if (chunk_bits > x) chunk_bits = x;
  999. }
  1000. if (type != SCAN_OUT) {
  1001. /* how much is there room for in the reply buffer? */
  1002. x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8;
  1003. if (chunk_bits > x) chunk_bits = x;
  1004. }
  1005. /* so the loop will end */
  1006. byte_bits -= chunk_bits;
  1007. if (type != SCAN_OUT) {
  1008. if (dtc_queue_enqueue_reply(
  1009. type, buffer, scan_size, tdi_bit_offset,
  1010. chunk_bits,
  1011. cmd
  1012. ) == NULL) {
  1013. LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno));
  1014. exit(1);
  1015. }
  1016. tdi_bit_offset += chunk_bits;
  1017. }
  1018. /* chunk_bits is a multiple of 8, so there are no rounding issues. */
  1019. chunk_bytes = chunk_bits / 8;
  1020. switch (type) {
  1021. case SCAN_IN:
  1022. x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes);
  1023. break;
  1024. case SCAN_OUT:
  1025. x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes);
  1026. break;
  1027. default:
  1028. x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes);
  1029. break;
  1030. }
  1031. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
  1032. if (type != SCAN_IN) {
  1033. x = 0;
  1034. dtc_mask = 1 << (8 - 1);
  1035. while (chunk_bits--) {
  1036. if (*tdi_p & tdi_mask) {
  1037. x |= dtc_mask;
  1038. }
  1039. dtc_mask >>= 1;
  1040. if (dtc_mask == 0) {
  1041. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
  1042. dtc_queue.reply_index++;
  1043. x = 0;
  1044. dtc_mask = 1 << (8 - 1);
  1045. }
  1046. tdi_mask <<= 1;
  1047. if (tdi_mask == 0) {
  1048. tdi_p++;
  1049. tdi_mask = 1;
  1050. }
  1051. }
  1052. }
  1053. }
  1054. if (extra_bits && (type != SCAN_OUT)) {
  1055. /* Schedule any extra bits into the DTC command buffer */
  1056. /* make sure there's room for stop, byte op, and one byte */
  1057. if (
  1058. (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))
  1059. ||
  1060. (dtc_queue.reply_index >= USB_EP2IN_SIZE - (1))
  1061. ) {
  1062. dtc_queue_run();
  1063. }
  1064. if (dtc_queue_enqueue_reply(
  1065. type, buffer, scan_size, tdi_bit_offset,
  1066. extra_bits,
  1067. cmd
  1068. ) == NULL) {
  1069. LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno));
  1070. exit(1);
  1071. }
  1072. tdi_bit_offset += extra_bits;
  1073. if (type == SCAN_IN) {
  1074. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
  1075. DTC_CMD_SHIFT_TDO_BYTES(1);
  1076. } else {
  1077. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
  1078. DTC_CMD_SHIFT_TDIO_BITS(extra_bits);
  1079. x = 0;
  1080. dtc_mask = 1 << (8 - 1);
  1081. while (extra_bits--) {
  1082. if (*tdi_p & tdi_mask) {
  1083. x |= dtc_mask;
  1084. }
  1085. dtc_mask >>= 1;
  1086. tdi_mask <<= 1;
  1087. if (tdi_mask == 0) {
  1088. tdi_p++;
  1089. tdi_mask = 1;
  1090. }
  1091. }
  1092. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
  1093. }
  1094. dtc_queue.reply_index++;
  1095. }
  1096. /* Schedule the last bit into the DTC command buffer */
  1097. {
  1098. /* make sure there's room for stop, and bit pair command */
  1099. if (
  1100. (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1))
  1101. ||
  1102. (dtc_queue.reply_index >= USB_EP2IN_SIZE - (1))
  1103. ) {
  1104. dtc_queue_run();
  1105. }
  1106. if (type == SCAN_OUT) {
  1107. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
  1108. DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
  1109. } else {
  1110. if (dtc_queue_enqueue_reply(
  1111. type, buffer, scan_size, tdi_bit_offset,
  1112. 1,
  1113. cmd
  1114. ) == NULL) {
  1115. LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno));
  1116. exit(1);
  1117. }
  1118. dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
  1119. DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
  1120. dtc_queue.reply_index++;
  1121. }
  1122. }
  1123. /* Move to pause state */
  1124. tap_state_queue_append(0);
  1125. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  1126. if (tap_get_state() != tap_get_end_state()) rlink_state_move();
  1127. return(0);
  1128. }
  1129. static
  1130. int rlink_execute_queue(void)
  1131. {
  1132. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  1133. int scan_size;
  1134. enum scan_type type;
  1135. uint8_t *buffer;
  1136. int retval, tmp_retval;
  1137. /* return ERROR_OK, unless something goes wrong */
  1138. retval = ERROR_OK;
  1139. #ifndef AUTOMATIC_BUSY_LED
  1140. /* turn LED on */
  1141. ep1_generic_commandl(pHDev, 2,
  1142. EP1_CMD_SET_PORTD_LEDS,
  1143. ~(ST7_PD_NBUSY_LED)
  1144. );
  1145. #endif
  1146. while (cmd)
  1147. {
  1148. switch (cmd->type)
  1149. {
  1150. case JTAG_RUNTEST:
  1151. case JTAG_STATEMOVE:
  1152. case JTAG_PATHMOVE:
  1153. case JTAG_SCAN:
  1154. break;
  1155. default:
  1156. /* some events, such as resets, need a queue flush to ensure consistency */
  1157. tap_state_queue_run();
  1158. dtc_queue_run();
  1159. break;
  1160. }
  1161. switch (cmd->type)
  1162. {
  1163. case JTAG_RESET:
  1164. #ifdef _DEBUG_JTAG_IO_
  1165. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  1166. #endif
  1167. if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
  1168. {
  1169. tap_set_state(TAP_RESET);
  1170. }
  1171. rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  1172. break;
  1173. case JTAG_RUNTEST:
  1174. #ifdef _DEBUG_JTAG_IO_
  1175. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  1176. #endif
  1177. if (cmd->cmd.runtest->end_state != -1)
  1178. rlink_end_state(cmd->cmd.runtest->end_state);
  1179. rlink_runtest(cmd->cmd.runtest->num_cycles);
  1180. break;
  1181. case JTAG_STATEMOVE:
  1182. #ifdef _DEBUG_JTAG_IO_
  1183. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  1184. #endif
  1185. if (cmd->cmd.statemove->end_state != -1)
  1186. rlink_end_state(cmd->cmd.statemove->end_state);
  1187. rlink_state_move();
  1188. break;
  1189. case JTAG_PATHMOVE:
  1190. #ifdef _DEBUG_JTAG_IO_
  1191. LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  1192. #endif
  1193. rlink_path_move(cmd->cmd.pathmove);
  1194. break;
  1195. case JTAG_SCAN:
  1196. #ifdef _DEBUG_JTAG_IO_
  1197. LOG_DEBUG("%s scan end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", cmd->cmd.scan->end_state);
  1198. #endif
  1199. if (cmd->cmd.scan->end_state != -1)
  1200. rlink_end_state(cmd->cmd.scan->end_state);
  1201. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  1202. type = jtag_scan_type(cmd->cmd.scan);
  1203. if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK) {
  1204. retval = ERROR_FAIL;
  1205. }
  1206. break;
  1207. case JTAG_SLEEP:
  1208. #ifdef _DEBUG_JTAG_IO_
  1209. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  1210. #endif
  1211. jtag_sleep(cmd->cmd.sleep->us);
  1212. break;
  1213. default:
  1214. LOG_ERROR("BUG: unknown JTAG command type encountered");
  1215. exit(-1);
  1216. }
  1217. cmd = cmd->next;
  1218. }
  1219. /* Flush the DTC queue to make sure any pending reads have been done before exiting this function */
  1220. tap_state_queue_run();
  1221. tmp_retval = dtc_queue_run();
  1222. if (tmp_retval != ERROR_OK) {
  1223. retval = tmp_retval;
  1224. }
  1225. #ifndef AUTOMATIC_BUSY_LED
  1226. /* turn LED onff */
  1227. ep1_generic_commandl(pHDev, 2,
  1228. EP1_CMD_SET_PORTD_LEDS,
  1229. ~0
  1230. );
  1231. #endif
  1232. return retval;
  1233. }
  1234. /* Using an unindexed table because it is infrequently accessed and it is short. The table must be in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
  1235. static
  1236. int rlink_speed(int speed)
  1237. {
  1238. int i;
  1239. if (speed == 0) {
  1240. /* fastest speed */
  1241. speed = rlink_speed_table[rlink_speed_table_size - 1].prescaler;
  1242. }
  1243. for (i = rlink_speed_table_size; i--;) {
  1244. if (rlink_speed_table[i].prescaler == speed) {
  1245. if (dtc_load_from_buffer(pHDev, rlink_speed_table[i].dtc, rlink_speed_table[i].dtc_size) != 0) {
  1246. LOG_ERROR("An error occurred while trying to load DTC code for speed \"%d\".\n", speed);
  1247. exit(1);
  1248. }
  1249. if (dtc_start_download() < 0) {
  1250. LOG_ERROR("%s, %d: starting DTC: %s",
  1251. __FILE__, __LINE__,
  1252. usb_strerror()
  1253. );
  1254. exit(1);
  1255. }
  1256. return ERROR_OK;
  1257. }
  1258. }
  1259. LOG_ERROR("%d is not a supported speed", speed);
  1260. return(ERROR_FAIL);
  1261. }
  1262. static
  1263. int rlink_speed_div(
  1264. int speed,
  1265. int *khz
  1266. ) {
  1267. int i;
  1268. for (i = rlink_speed_table_size; i--;) {
  1269. if (rlink_speed_table[i].prescaler == speed) {
  1270. *khz = rlink_speed_table[i].khz;
  1271. return(ERROR_OK);
  1272. }
  1273. }
  1274. LOG_ERROR("%d is not a supported speed", speed);
  1275. return(ERROR_FAIL);
  1276. }
  1277. static
  1278. int rlink_khz(
  1279. int khz,
  1280. int *speed
  1281. ) {
  1282. int i;
  1283. if (khz == 0) {
  1284. LOG_ERROR("RCLK not supported");
  1285. return ERROR_FAIL;
  1286. }
  1287. for (i = rlink_speed_table_size; i--;) {
  1288. if (rlink_speed_table[i].khz <= khz) {
  1289. *speed = rlink_speed_table[i].prescaler;
  1290. return(ERROR_OK);
  1291. }
  1292. }
  1293. LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz);
  1294. *speed = rlink_speed_table[0].prescaler;
  1295. return(ERROR_OK);
  1296. }
  1297. #if 0
  1298. static
  1299. int
  1300. handle_dtc_directory_command(
  1301. struct command_context_s *cmd_ctx,
  1302. char *cmd,
  1303. char **args,
  1304. int argc
  1305. ) {
  1306. if (argc != 1) {
  1307. LOG_ERROR("expected exactly one argument to rlink_dtc_directory <directory-path>");
  1308. return(ERROR_INVALID_ARGUMENTS);
  1309. }
  1310. printf("handle_dtc_directory_command called with \"%s\"\n", args[0]);
  1311. return(ERROR_OK);
  1312. }
  1313. #endif
  1314. static
  1315. int rlink_register_commands(struct command_context_s *cmd_ctx)
  1316. {
  1317. #ifdef _DEBUG_JTAG_IO_
  1318. LOG_DEBUG("rlink_register_commands called with cmd_ctx=%p\n", cmd_ctx);
  1319. #endif
  1320. #if 0
  1321. register_command(
  1322. cmd_ctx, NULL,
  1323. "rlink_dtc_directory",
  1324. handle_dtc_directory_command,
  1325. COMMAND_CONFIG,
  1326. "The directory in which to search for DTC load images"
  1327. );
  1328. #endif
  1329. return ERROR_OK;
  1330. }
  1331. static
  1332. int rlink_init(void)
  1333. {
  1334. struct usb_bus *busses;
  1335. struct usb_bus *bus;
  1336. int i, j, retries;
  1337. int found = 0;
  1338. int success = 0;
  1339. uint8_t reply_buffer[USB_EP1IN_SIZE];
  1340. usb_init();
  1341. usb_find_busses();
  1342. usb_find_devices();
  1343. busses = usb_get_busses();
  1344. for (bus = busses; bus; bus = bus->next)
  1345. {
  1346. struct usb_device *dev;
  1347. for (dev = bus->devices; dev; dev = dev->next)
  1348. {
  1349. if ((dev->descriptor.idVendor == USB_IDVENDOR) && (dev->descriptor.idProduct == USB_IDPRODUCT))
  1350. {
  1351. found = 1;
  1352. LOG_DEBUG("Found device on bus.\n");
  1353. do
  1354. {
  1355. if (dev->descriptor.bNumConfigurations > 1)
  1356. {
  1357. LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...\n");
  1358. break;
  1359. }
  1360. if (dev->config->bNumInterfaces > 1)
  1361. {
  1362. LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...\n");
  1363. break;
  1364. }
  1365. pHDev = usb_open(dev);
  1366. if (!pHDev)
  1367. LOG_ERROR ("Failed to open device.\n");
  1368. else
  1369. {
  1370. LOG_DEBUG("Opened device, pHDev = %p\n",pHDev);
  1371. /* usb_set_configuration required under win32 */
  1372. usb_set_configuration(pHDev, dev->config[0].bConfigurationValue);
  1373. retries = 3;
  1374. do
  1375. {
  1376. i = usb_claim_interface(pHDev,0);
  1377. if (i)
  1378. {
  1379. LOG_ERROR("usb_claim_interface: %s", usb_strerror());
  1380. #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
  1381. j = usb_detach_kernel_driver_np(pHDev, 0);
  1382. if (j)
  1383. LOG_ERROR("detach kernel driver: %s", usb_strerror());
  1384. #endif
  1385. }
  1386. else
  1387. {
  1388. LOG_DEBUG("interface claimed!\n");
  1389. break;
  1390. }
  1391. } while (--retries);
  1392. if (!i)
  1393. {
  1394. if (usb_set_altinterface(pHDev,0))
  1395. {
  1396. LOG_ERROR("Failed to set interface.\n");
  1397. break;
  1398. }
  1399. else
  1400. success = 1;
  1401. }
  1402. }
  1403. } while (0);
  1404. }
  1405. }
  1406. }
  1407. if (!found)
  1408. {
  1409. LOG_ERROR("No device found on bus.\n");
  1410. exit(1);
  1411. }
  1412. if (!success)
  1413. {
  1414. LOG_ERROR("Initialisation failed.");
  1415. exit(1);
  1416. }
  1417. /* The device starts out in an unknown state on open. As such, result reads time out, and it's not even known whether the command was accepted. So, for this first command, we issue it repeatedly until its response doesn't time out. Also, if sending a command is going to time out, we'll find that out here. */
  1418. /* It must be possible to open the device in such a way that this special magic isn't needed, but, so far, it escapes us. */
  1419. for (i = 0; i < 5; i++) {
  1420. j = ep1_generic_commandl(
  1421. pHDev, 1,
  1422. EP1_CMD_GET_FWREV
  1423. );
  1424. if (j < USB_EP1OUT_SIZE) {
  1425. LOG_ERROR("USB write error: %s", usb_strerror());
  1426. return(ERROR_FAIL);
  1427. }
  1428. j = usb_bulk_read(
  1429. pHDev, USB_EP1IN_ADDR,
  1430. (char *)reply_buffer, sizeof(reply_buffer),
  1431. 200
  1432. );
  1433. if (j != -ETIMEDOUT) break;
  1434. }
  1435. if (j < (int)sizeof(reply_buffer)) {
  1436. LOG_ERROR("USB read error: %s", usb_strerror());
  1437. return(ERROR_FAIL);
  1438. }
  1439. LOG_DEBUG(INTERFACE_NAME" firmware version: %d.%d.%d\n", reply_buffer[0], reply_buffer[1], reply_buffer[2]);
  1440. if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3)) {
  1441. LOG_WARNING("The rlink device is not of the version that the developers have played with. It may or may not work.\n");
  1442. }
  1443. /* Probe port E for adapter presence */
  1444. ep1_generic_commandl(
  1445. pHDev, 16,
  1446. EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */
  1447. ST7_PEDR >> 8,
  1448. ST7_PEDR,
  1449. 3,
  1450. 0x00, /* DR */
  1451. ST7_PE_ADAPTER_SENSE_OUT, /* DDR */
  1452. ST7_PE_ADAPTER_SENSE_OUT, /* OR */
  1453. EP1_CMD_MEMORY_READ, /* Read back */
  1454. ST7_PEDR >> 8,
  1455. ST7_PEDR,
  1456. 1,
  1457. EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */
  1458. ST7_PEDR >> 8,
  1459. ST7_PEDR,
  1460. 1,
  1461. ST7_PE_ADAPTER_SENSE_OUT
  1462. );
  1463. usb_bulk_read(
  1464. pHDev, USB_EP1IN_ADDR,
  1465. (char *)reply_buffer, 1,
  1466. USB_TIMEOUT_MS
  1467. );
  1468. if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0) {
  1469. LOG_WARNING("target detection problem\n");
  1470. }
  1471. ep1_generic_commandl(
  1472. pHDev, 11,
  1473. EP1_CMD_MEMORY_READ, /* Read back */
  1474. ST7_PEDR >> 8,
  1475. ST7_PEDR,
  1476. 1,
  1477. EP1_CMD_MEMORY_WRITE, /* float port E */
  1478. ST7_PEDR >> 8,
  1479. ST7_PEDR,
  1480. 3,
  1481. 0x00, /* DR */
  1482. 0x00, /* DDR */
  1483. 0x00 /* OR */
  1484. );
  1485. usb_bulk_read(
  1486. pHDev, USB_EP1IN_ADDR,
  1487. (char *)reply_buffer, 1,
  1488. USB_TIMEOUT_MS
  1489. );
  1490. if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0) {
  1491. LOG_WARNING("target not plugged in\n");
  1492. }
  1493. /* float ports A and B */
  1494. ep1_generic_commandl(
  1495. pHDev, 11,
  1496. EP1_CMD_MEMORY_WRITE,
  1497. ST7_PADDR >> 8,
  1498. ST7_PADDR,
  1499. 2,
  1500. 0x00,
  1501. 0x00,
  1502. EP1_CMD_MEMORY_WRITE,
  1503. ST7_PBDDR >> 8,
  1504. ST7_PBDDR,
  1505. 1,
  1506. 0x00
  1507. );
  1508. /* make sure DTC is stopped, set VPP control, set up ports A and B */
  1509. ep1_generic_commandl(
  1510. pHDev, 14,
  1511. EP1_CMD_DTC_STOP,
  1512. EP1_CMD_SET_PORTD_VPP,
  1513. ~(ST7_PD_VPP_SHDN),
  1514. EP1_CMD_MEMORY_WRITE,
  1515. ST7_PADR >> 8,
  1516. ST7_PADR,
  1517. 2,
  1518. ((~(0)) & (ST7_PA_NTRST)),
  1519. (ST7_PA_NTRST),
  1520. /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0 here and later assert NSRST by setting DDR bit to 1. */
  1521. EP1_CMD_MEMORY_WRITE,
  1522. ST7_PBDR >> 8,
  1523. ST7_PBDR,
  1524. 1,
  1525. 0x00
  1526. );
  1527. /* set LED updating mode and make sure they're unlit */
  1528. ep1_generic_commandl(
  1529. pHDev, 3,
  1530. #ifdef AUTOMATIC_BUSY_LED
  1531. EP1_CMD_LEDUE_BUSY,
  1532. #else
  1533. EP1_CMD_LEDUE_NONE,
  1534. #endif
  1535. EP1_CMD_SET_PORTD_LEDS,
  1536. ~0
  1537. );
  1538. tap_state_queue_init();
  1539. dtc_queue_init();
  1540. rlink_speed(jtag_get_speed());
  1541. rlink_reset(0, 0);
  1542. return ERROR_OK;
  1543. }
  1544. static
  1545. int rlink_quit(void)
  1546. {
  1547. /* stop DTC and make sure LEDs are off */
  1548. ep1_generic_commandl(
  1549. pHDev, 6,
  1550. EP1_CMD_DTC_STOP,
  1551. EP1_CMD_LEDUE_NONE,
  1552. EP1_CMD_SET_PORTD_LEDS,
  1553. ~0,
  1554. EP1_CMD_SET_PORTD_VPP,
  1555. ~0
  1556. );
  1557. usb_release_interface(pHDev,0);
  1558. usb_close(pHDev);
  1559. return ERROR_OK;
  1560. }
  1561. jtag_interface_t rlink_interface =
  1562. {
  1563. .name = "rlink",
  1564. .init = rlink_init,
  1565. .quit = rlink_quit,
  1566. .register_commands = rlink_register_commands,
  1567. .speed = rlink_speed,
  1568. .speed_div = rlink_speed_div,
  1569. .khz = rlink_khz,
  1570. .execute_queue = rlink_execute_queue,
  1571. };