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.
 
 
 
 
 
 

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