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.
 
 
 
 
 
 

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