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.
 
 
 
 
 
 

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