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.
 
 
 
 
 
 

846 lines
21 KiB

  1. /*******************************************************************************
  2. * Driver for OpenJTAG Project (www.openjtag.org) *
  3. * Compatible with libftdi and ftd2xx drivers. *
  4. * *
  5. * Copyright (C) 2010 by Ivan Meleca <mileca@gmail.com> *
  6. * *
  7. * Copyright (C) 2013 by Ryan Corbin, GlueLogix Inc. <corbin.ryan@gmail.com> *
  8. * Updated to work with OpenOCD v0.7.0. Fixed libftdi read speed issue. *
  9. * *
  10. * Based on usb_blaster.c *
  11. * Copyright (C) 2009 Catalin Patulea *
  12. * Copyright (C) 2006 Kolja Waschk *
  13. * *
  14. * And jlink.c *
  15. * Copyright (C) 2008 by Spencer Oliver *
  16. * spen@spen-soft.co.uk *
  17. * *
  18. * This program is free software; you can redistribute it and/or modify *
  19. * it under the terms of the GNU General Public License as published by *
  20. * the Free Software Foundation; either version 2 of the License, or *
  21. * (at your option) any later version. *
  22. * *
  23. * This program is distributed in the hope that it will be useful, *
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  26. * GNU General Public License for more details. *
  27. * *
  28. * You should have received a copy of the GNU General Public License *
  29. * along with this program; if not, write to the *
  30. * Free Software Foundation, Inc., *
  31. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  32. ***************************************************************************/
  33. /***************************************************************************
  34. * Version 1.0 Tested on a MCBSTM32 board using a Cortex M3 (stm32f103x), *
  35. * GDB and Eclipse under Linux (Ubuntu 10.04) *
  36. * *
  37. ***************************************************************************/
  38. #ifdef HAVE_CONFIG_H
  39. #include "config.h"
  40. #endif
  41. #include <jtag/interface.h>
  42. #include <jtag/commands.h>
  43. #include "usb_common.h"
  44. /*
  45. * OpenJTAG-OpenOCD state conversion
  46. */
  47. typedef enum openjtag_tap_state {
  48. OPENJTAG_TAP_INVALID = -1,
  49. OPENJTAG_TAP_RESET = 0,
  50. OPENJTAG_TAP_IDLE = 1,
  51. OPENJTAG_TAP_SELECT_DR = 2,
  52. OPENJTAG_TAP_CAPTURE_DR = 3,
  53. OPENJTAG_TAP_SHIFT_DR = 4,
  54. OPENJTAG_TAP_EXIT1_DR = 5,
  55. OPENJTAG_TAP_PAUSE_DR = 6,
  56. OPENJTAG_TAP_EXIT2_DR = 7,
  57. OPENJTAG_TAP_UPDATE_DR = 8,
  58. OPENJTAG_TAP_SELECT_IR = 9,
  59. OPENJTAG_TAP_CAPURE_IR = 10,
  60. OPENJTAG_TAP_SHIFT_IR = 11,
  61. OPENJTAG_TAP_EXIT1_IR = 12,
  62. OPENJTAG_TAP_PAUSE_IR = 13,
  63. OPENJTAG_TAP_EXIT2_IR = 14,
  64. OPENJTAG_TAP_UPDATE_IR = 15,
  65. } openjtag_tap_state_t;
  66. #if (BUILD_OPENJTAG_FTD2XX == 1 && BUILD_OPENJTAG_LIBFTDI == 1)
  67. #error "BUILD_OPENJTAG_FTD2XX && BUILD_OPENJTAG_LIBFTDI "
  68. "are mutually exclusive"
  69. #elif (BUILD_OPENJTAG_FTD2XX != 1 && BUILD_OPENJTAG_LIBFTDI != 1)
  70. #error "BUILD_OPENJTAG_FTD2XX || BUILD_OPENJTAG_LIBFTDI must be chosen"
  71. #endif
  72. /* OPENJTAG access library includes */
  73. #if BUILD_OPENJTAG_FTD2XX == 1
  74. #include <ftd2xx.h>
  75. #elif BUILD_OPENJTAG_LIBFTDI == 1
  76. #include <ftdi.h>
  77. #endif
  78. /* OpenJTAG vid/pid */
  79. static uint16_t openjtag_vid = 0x0403;
  80. static uint16_t openjtag_pid = 0x6001;
  81. static char *openjtag_device_desc;
  82. #if BUILD_OPENJTAG_FTD2XX == 1
  83. static FT_HANDLE ftdih;
  84. #elif BUILD_OPENJTAG_LIBFTDI == 1
  85. static struct ftdi_context ftdic;
  86. #endif
  87. #define OPENJTAG_BUFFER_SIZE 504
  88. #define OPENJTAG_MAX_PENDING_RESULTS 256
  89. struct openjtag_scan_result {
  90. uint32_t bits; /* Length in bits*/
  91. struct scan_command *command; /* Corresponding scan command */
  92. uint8_t *buffer;
  93. };
  94. /* USB RX/TX buffers */
  95. static int usb_tx_buf_offs;
  96. static uint8_t usb_tx_buf[OPENJTAG_BUFFER_SIZE];
  97. static uint32_t usb_rx_buf_len;
  98. static uint8_t usb_rx_buf[OPENJTAG_BUFFER_SIZE];
  99. /* Pending readings */
  100. static struct openjtag_scan_result openjtag_scan_result_buffer[OPENJTAG_MAX_PENDING_RESULTS];
  101. static int openjtag_scan_result_count;
  102. /* Openocd usb handler */
  103. struct openocd {
  104. struct usb_dev_handle *usb_handle;
  105. };
  106. #ifdef _DEBUG_USB_COMMS_
  107. #define DEBUG_TYPE_READ 0
  108. #define DEBUG_TYPE_WRITE 1
  109. #define DEBUG_TYPE_OCD_READ 2
  110. #define DEBUG_TYPE_BUFFER 3
  111. #define LINE_LEN 16
  112. static void openjtag_debug_buffer(uint8_t *buffer, int length, uint8_t type)
  113. {
  114. char line[128];
  115. char s[4];
  116. int i;
  117. int j;
  118. switch (type) {
  119. case DEBUG_TYPE_READ:
  120. sprintf(line, "USB READ %d bytes", length);
  121. break;
  122. case DEBUG_TYPE_WRITE:
  123. sprintf(line, "USB WRITE %d bytes", length);
  124. break;
  125. case DEBUG_TYPE_OCD_READ:
  126. sprintf(line, "TO OpenOCD %d bytes", length);
  127. break;
  128. case DEBUG_TYPE_BUFFER:
  129. sprintf(line, "Buffer %d bytes", length);
  130. break;
  131. }
  132. LOG_DEBUG("%s", line);
  133. for (i = 0; i < length; i += LINE_LEN) {
  134. switch (type) {
  135. case DEBUG_TYPE_READ:
  136. sprintf(line, "USB READ: %04x", i);
  137. break;
  138. case DEBUG_TYPE_WRITE:
  139. sprintf(line, "USB WRITE: %04x", i);
  140. break;
  141. case DEBUG_TYPE_OCD_READ:
  142. sprintf(line, "TO OpenOCD: %04x", i);
  143. break;
  144. case DEBUG_TYPE_BUFFER:
  145. sprintf(line, "BUFFER: %04x", i);
  146. break;
  147. }
  148. for (j = i; j < i + LINE_LEN && j < length; j++) {
  149. sprintf(s, " %02x", buffer[j]);
  150. strcat(line, s);
  151. }
  152. LOG_DEBUG("%s", line);
  153. }
  154. }
  155. #endif
  156. static int8_t openjtag_get_tap_state(int8_t state)
  157. {
  158. switch (state) {
  159. case TAP_DREXIT2: return OPENJTAG_TAP_EXIT2_DR;
  160. case TAP_DREXIT1: return OPENJTAG_TAP_EXIT1_DR;
  161. case TAP_DRSHIFT: return OPENJTAG_TAP_SHIFT_DR;
  162. case TAP_DRPAUSE: return OPENJTAG_TAP_PAUSE_DR;
  163. case TAP_IRSELECT: return OPENJTAG_TAP_SELECT_IR;
  164. case TAP_DRUPDATE: return OPENJTAG_TAP_UPDATE_DR;
  165. case TAP_DRCAPTURE: return OPENJTAG_TAP_CAPTURE_DR;
  166. case TAP_DRSELECT: return OPENJTAG_TAP_SELECT_DR;
  167. case TAP_IREXIT2: return OPENJTAG_TAP_EXIT2_IR;
  168. case TAP_IREXIT1: return OPENJTAG_TAP_EXIT1_IR;
  169. case TAP_IRSHIFT: return OPENJTAG_TAP_SHIFT_IR;
  170. case TAP_IRPAUSE: return OPENJTAG_TAP_PAUSE_IR;
  171. case TAP_IDLE: return OPENJTAG_TAP_IDLE;
  172. case TAP_IRUPDATE: return OPENJTAG_TAP_UPDATE_IR;
  173. case TAP_IRCAPTURE: return OPENJTAG_TAP_CAPURE_IR;
  174. case TAP_RESET: return OPENJTAG_TAP_RESET;
  175. case TAP_INVALID:
  176. default: return OPENJTAG_TAP_INVALID;
  177. }
  178. }
  179. static int openjtag_buf_write(
  180. uint8_t *buf, int size, uint32_t *bytes_written)
  181. {
  182. #if BUILD_OPENJTAG_FTD2XX == 1
  183. FT_STATUS status;
  184. DWORD dw_bytes_written;
  185. #ifdef _DEBUG_USB_COMMS_
  186. openjtag_debug_buffer(buf, size, DEBUG_TYPE_WRITE);
  187. #endif
  188. status = FT_Write(ftdih, buf, size, &dw_bytes_written);
  189. if (status != FT_OK) {
  190. *bytes_written = dw_bytes_written;
  191. LOG_ERROR("FT_Write returned: %u", status);
  192. return ERROR_JTAG_DEVICE_ERROR;
  193. }
  194. *bytes_written = dw_bytes_written;
  195. return ERROR_OK;
  196. #elif BUILD_OPENJTAG_LIBFTDI == 1
  197. int retval;
  198. #ifdef _DEBUG_USB_COMMS_
  199. openjtag_debug_buffer(buf, size, DEBUG_TYPE_WRITE);
  200. #endif
  201. retval = ftdi_write_data(&ftdic, buf, size);
  202. if (retval < 0) {
  203. *bytes_written = 0;
  204. LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
  205. return ERROR_JTAG_DEVICE_ERROR;
  206. }
  207. *bytes_written += retval;
  208. return ERROR_OK;
  209. #endif
  210. }
  211. static int openjtag_buf_read(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
  212. {
  213. #if BUILD_OPENJTAG_FTD2XX == 1
  214. DWORD dw_bytes_read;
  215. FT_STATUS status;
  216. int timeout = 50;
  217. *bytes_read = 0;
  218. while (qty && (*bytes_read < qty) && timeout--) {
  219. status = FT_Read(ftdih, buf + *bytes_read,
  220. qty - *bytes_read, &dw_bytes_read);
  221. if (status != FT_OK) {
  222. *bytes_read = dw_bytes_read;
  223. LOG_ERROR("FT_Read returned: %u", status);
  224. return ERROR_JTAG_DEVICE_ERROR;
  225. }
  226. *bytes_read += dw_bytes_read;
  227. }
  228. #ifdef _DEBUG_USB_COMMS_
  229. openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
  230. #endif
  231. return ERROR_OK;
  232. #elif BUILD_OPENJTAG_LIBFTDI == 1
  233. int retval;
  234. int timeout = 5;
  235. *bytes_read = 0;
  236. while ((*bytes_read < qty) && timeout--) {
  237. retval = ftdi_read_data(&ftdic, buf + *bytes_read,
  238. qty - *bytes_read);
  239. if (retval < 0) {
  240. *bytes_read = 0;
  241. DEBUG_JTAG_IO("ftdi_read_data: %s",
  242. ftdi_get_error_string(&ftdic));
  243. return ERROR_JTAG_DEVICE_ERROR;
  244. }
  245. *bytes_read += retval;
  246. }
  247. #ifdef _DEBUG_USB_COMMS_
  248. openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
  249. #endif
  250. #endif
  251. return ERROR_OK;
  252. }
  253. static int openjtag_sendcommand(uint8_t cmd)
  254. {
  255. uint32_t written;
  256. return openjtag_buf_write(&cmd, 1, &written);
  257. }
  258. static int openjtag_speed(int speed)
  259. {
  260. int clockcmd;
  261. switch (speed) {
  262. case 48000:
  263. clockcmd = 0x00;
  264. break;
  265. case 24000:
  266. clockcmd = 0x20;
  267. break;
  268. case 12000:
  269. clockcmd = 0x40;
  270. break;
  271. case 6000:
  272. clockcmd = 0x60;
  273. break;
  274. case 3000:
  275. clockcmd = 0x80;
  276. break;
  277. case 1500:
  278. clockcmd = 0xA0;
  279. break;
  280. case 750:
  281. clockcmd = 0xC0;
  282. break;
  283. case 375:
  284. clockcmd = 0xE0;
  285. break;
  286. default:
  287. clockcmd = 0xE0;
  288. LOG_WARNING("adapter speed not recognized, reverting to 375 kHz");
  289. break;
  290. }
  291. openjtag_sendcommand(clockcmd);
  292. return ERROR_OK;
  293. }
  294. static int openjtag_init(void)
  295. {
  296. uint8_t latency_timer;
  297. #if BUILD_OPENJTAG_FTD2XX == 1
  298. FT_STATUS status;
  299. #endif
  300. usb_tx_buf_offs = 0;
  301. usb_rx_buf_len = 0;
  302. openjtag_scan_result_count = 0;
  303. #if BUILD_OPENJTAG_FTD2XX == 1
  304. LOG_DEBUG("'openjtag' interface using FTD2XX");
  305. #elif BUILD_OPENJTAG_LIBFTDI == 1
  306. LOG_DEBUG("'openjtag' interface using libftdi");
  307. #endif
  308. /* Open by device description */
  309. if (openjtag_device_desc == NULL) {
  310. LOG_WARNING("no openjtag device description specified, "
  311. "using default 'Open JTAG Project'");
  312. openjtag_device_desc = "Open JTAG Project";
  313. }
  314. #if BUILD_OPENJTAG_FTD2XX == 1
  315. #if IS_WIN32 == 0
  316. /* Add non-standard Vid/Pid to the linux driver */
  317. status = FT_SetVIDPID(openjtag_vid, openjtag_pid);
  318. if (status != FT_OK) {
  319. LOG_WARNING("couldn't add %4.4x:%4.4x",
  320. openjtag_vid, openjtag_pid);
  321. }
  322. #endif
  323. status = FT_OpenEx(openjtag_device_desc, FT_OPEN_BY_DESCRIPTION,
  324. &ftdih);
  325. if (status != FT_OK) {
  326. DWORD num_devices;
  327. LOG_ERROR("unable to open ftdi device: %u", status);
  328. status = FT_ListDevices(&num_devices, NULL,
  329. FT_LIST_NUMBER_ONLY);
  330. if (status == FT_OK) {
  331. char **desc_array = malloc(sizeof(char *)
  332. * (num_devices + 1));
  333. unsigned int i;
  334. for (i = 0; i < num_devices; i++)
  335. desc_array[i] = malloc(64);
  336. desc_array[num_devices] = NULL;
  337. status = FT_ListDevices(desc_array, &num_devices,
  338. FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
  339. if (status == FT_OK) {
  340. LOG_ERROR("ListDevices: %u\n", num_devices);
  341. for (i = 0; i < num_devices; i++)
  342. LOG_ERROR("%i: %s", i, desc_array[i]);
  343. }
  344. for (i = 0; i < num_devices; i++)
  345. free(desc_array[i]);
  346. free(desc_array);
  347. } else {
  348. LOG_ERROR("ListDevices: NONE\n");
  349. }
  350. return ERROR_JTAG_INIT_FAILED;
  351. }
  352. status = FT_SetLatencyTimer(ftdih, 2);
  353. if (status != FT_OK) {
  354. LOG_ERROR("unable to set latency timer: %u", status);
  355. return ERROR_JTAG_INIT_FAILED;
  356. }
  357. status = FT_GetLatencyTimer(ftdih, &latency_timer);
  358. if (status != FT_OK) {
  359. LOG_ERROR("unable to get latency timer: %u", status);
  360. return ERROR_JTAG_INIT_FAILED;
  361. }
  362. LOG_DEBUG("current latency timer: %i", latency_timer);
  363. status = FT_SetBitMode(ftdih, 0x00, 0x40);
  364. if (status != FT_OK) {
  365. LOG_ERROR("unable to disable bit i/o mode: %u", status);
  366. return ERROR_JTAG_INIT_FAILED;
  367. }
  368. status = FT_SetTimeouts(ftdih, 50, 0);
  369. if (status != FT_OK) {
  370. LOG_ERROR("unable to set timeouts: %u", status);
  371. return ERROR_JTAG_INIT_FAILED;
  372. }
  373. status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
  374. if (status != FT_OK) {
  375. LOG_ERROR("unable to FT_Purge() %u", status);
  376. return ERROR_JTAG_INIT_FAILED;
  377. }
  378. #elif BUILD_OPENJTAG_LIBFTDI == 1
  379. if (ftdi_init(&ftdic) < 0)
  380. return ERROR_JTAG_INIT_FAILED;
  381. /* context, vendor id, product id, description, serial id */
  382. if (ftdi_usb_open_desc(&ftdic, openjtag_vid, openjtag_pid, openjtag_device_desc, NULL) < 0) {
  383. LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
  384. return ERROR_JTAG_INIT_FAILED;
  385. }
  386. if (ftdi_usb_reset(&ftdic) < 0) {
  387. LOG_ERROR("unable to reset ftdi device");
  388. return ERROR_JTAG_INIT_FAILED;
  389. }
  390. if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
  391. LOG_ERROR("unable to set latency timer");
  392. return ERROR_JTAG_INIT_FAILED;
  393. }
  394. if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
  395. LOG_ERROR("unable to get latency timer");
  396. return ERROR_JTAG_INIT_FAILED;
  397. }
  398. LOG_DEBUG("current latency timer: %u", latency_timer);
  399. ftdi_disable_bitbang(&ftdic);
  400. /* was (3000000 / 4) with a comment about a bug in libftdi when using high baudrate */
  401. if (ftdi_set_baudrate(&ftdic, 3000000) < 0) {
  402. LOG_ERROR("Can't set baud rate to max: %s",
  403. ftdi_get_error_string(&ftdic));
  404. return ERROR_JTAG_DEVICE_ERROR;
  405. };
  406. #endif
  407. #if BUILD_OPENJTAG_FTD2XX == 1
  408. status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
  409. if (status != FT_OK)
  410. return ERROR_JTAG_INIT_FAILED;
  411. #elif BUILD_OPENJTAG_LIBFTDI == 1
  412. if (ftdi_usb_purge_buffers(&ftdic) < 0) {
  413. LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
  414. return ERROR_JTAG_INIT_FAILED;
  415. }
  416. #endif
  417. /* OpenJTAG speed */
  418. openjtag_sendcommand(0xE0); /*Start at slowest adapter speed*/
  419. /* MSB */
  420. openjtag_sendcommand(0x75);
  421. return ERROR_OK;
  422. }
  423. static int openjtag_quit(void)
  424. {
  425. #if BUILD_OPENJTAG_FTD2XX == 1
  426. FT_Close(ftdih);
  427. #elif BUILD_OPENJTAG_LIBFTDI == 1
  428. ftdi_usb_close(&ftdic);
  429. ftdi_deinit(&ftdic);
  430. #endif
  431. return ERROR_OK;
  432. }
  433. static void openjtag_write_tap_buffer(void)
  434. {
  435. uint32_t written;
  436. openjtag_buf_write(usb_tx_buf, usb_tx_buf_offs, &written);
  437. openjtag_buf_read(usb_rx_buf, usb_tx_buf_offs, &usb_rx_buf_len);
  438. usb_tx_buf_offs = 0;
  439. }
  440. static int openjtag_execute_tap_queue(void)
  441. {
  442. openjtag_write_tap_buffer();
  443. int res_count = 0;
  444. if (openjtag_scan_result_count && usb_rx_buf_len) {
  445. int count;
  446. int rx_offs = 0;
  447. int len;
  448. /* for every pending result */
  449. while (res_count < openjtag_scan_result_count) {
  450. /* get sent bits */
  451. len = openjtag_scan_result_buffer[res_count].bits;
  452. count = 0;
  453. uint8_t *buffer = openjtag_scan_result_buffer[res_count].buffer;
  454. while (len) {
  455. if (len <= 8) {
  456. DEBUG_JTAG_IO("bits < 8 buf = 0x%X, will be 0x%X",
  457. usb_rx_buf[rx_offs], usb_rx_buf[rx_offs] >> (8 - len));
  458. buffer[count] = usb_rx_buf[rx_offs] >> (8 - len);
  459. len = 0;
  460. } else {
  461. buffer[count] = usb_rx_buf[rx_offs];
  462. len -= 8;
  463. }
  464. rx_offs++;
  465. count++;
  466. }
  467. #ifdef _DEBUG_USB_COMMS_
  468. openjtag_debug_buffer(buffer,
  469. DIV_ROUND_UP(openjtag_scan_result_buffer[res_count].bits, 8), DEBUG_TYPE_OCD_READ);
  470. #endif
  471. jtag_read_buffer(buffer, openjtag_scan_result_buffer[res_count].command);
  472. if (openjtag_scan_result_buffer[res_count].buffer)
  473. free(openjtag_scan_result_buffer[res_count].buffer);
  474. res_count++;
  475. }
  476. }
  477. openjtag_scan_result_count = 0;
  478. return ERROR_OK;
  479. }
  480. static void openjtag_add_byte(char buf)
  481. {
  482. if (usb_tx_buf_offs == OPENJTAG_BUFFER_SIZE) {
  483. DEBUG_JTAG_IO("Forcing execute_tap_queue");
  484. DEBUG_JTAG_IO("TX Buff offs=%d", usb_tx_buf_offs);
  485. openjtag_execute_tap_queue();
  486. }
  487. usb_tx_buf[usb_tx_buf_offs] = buf;
  488. usb_tx_buf_offs++;
  489. }
  490. static void openjtag_add_scan(uint8_t *buffer, int length, struct scan_command *scan_cmd)
  491. {
  492. /* Ensure space to send long chains */
  493. /* We add two byte for each eight (or less) bits, one for command, one for data */
  494. if (usb_tx_buf_offs + (DIV_ROUND_UP(length, 8) * 2) >= OPENJTAG_BUFFER_SIZE) {
  495. DEBUG_JTAG_IO("Forcing execute_tap_queue from scan");
  496. DEBUG_JTAG_IO("TX Buff offs=%d len=%d", usb_tx_buf_offs, DIV_ROUND_UP(length, 8) * 2);
  497. openjtag_execute_tap_queue();
  498. }
  499. openjtag_scan_result_buffer[openjtag_scan_result_count].bits = length;
  500. openjtag_scan_result_buffer[openjtag_scan_result_count].command = scan_cmd;
  501. openjtag_scan_result_buffer[openjtag_scan_result_count].buffer = buffer;
  502. uint8_t command;
  503. uint8_t bits;
  504. int count = 0;
  505. while (length) {
  506. /* write command */
  507. command = 6;
  508. /* last bits? */
  509. if (length <= 8) {
  510. /* tms high */
  511. command |= (1 << 4);
  512. /* bits to transfer */
  513. bits = (length - 1);
  514. command |= bits << 5;
  515. length = 0;
  516. } else {
  517. /* whole byte */
  518. /* bits to transfer */
  519. bits = 7;
  520. command |= (7 << 5);
  521. length -= 8;
  522. }
  523. openjtag_add_byte(command);
  524. openjtag_add_byte(buffer[count]);
  525. count++;
  526. }
  527. openjtag_scan_result_count++;
  528. }
  529. static void openjtag_execute_reset(struct jtag_command *cmd)
  530. {
  531. DEBUG_JTAG_IO("reset trst: %i srst %i",
  532. cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  533. uint8_t buf = 0x00;
  534. if (cmd->cmd.reset->trst) {
  535. buf = 0x03;
  536. } else {
  537. buf |= 0x04;
  538. buf |= 0x05 << 4;
  539. }
  540. openjtag_add_byte(buf);
  541. }
  542. static void openjtag_execute_sleep(struct jtag_command *cmd)
  543. {
  544. jtag_sleep(cmd->cmd.sleep->us);
  545. }
  546. static void openjtag_set_state(uint8_t openocd_state)
  547. {
  548. int8_t state = openjtag_get_tap_state(openocd_state);
  549. uint8_t buf = 0;
  550. buf = 0x01;
  551. buf |= state << 4;
  552. openjtag_add_byte(buf);
  553. }
  554. static void openjtag_execute_statemove(struct jtag_command *cmd)
  555. {
  556. DEBUG_JTAG_IO("state move to %i", cmd->cmd.statemove->end_state);
  557. tap_set_end_state(cmd->cmd.statemove->end_state);
  558. openjtag_set_state(cmd->cmd.statemove->end_state);
  559. tap_set_state(tap_get_end_state());
  560. }
  561. static void openjtag_execute_scan(struct jtag_command *cmd)
  562. {
  563. int scan_size, old_state;
  564. uint8_t *buffer;
  565. DEBUG_JTAG_IO("scan ends in %s", tap_state_name(cmd->cmd.scan->end_state));
  566. /* get scan info */
  567. tap_set_end_state(cmd->cmd.scan->end_state);
  568. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  569. #ifdef _DEBUG_USB_COMMS_
  570. openjtag_debug_buffer(buffer, (scan_size + 7) / 8, DEBUG_TYPE_BUFFER);
  571. #endif
  572. /* set state */
  573. old_state = tap_get_end_state();
  574. openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  575. tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  576. tap_set_end_state(old_state);
  577. openjtag_add_scan(buffer, scan_size, cmd->cmd.scan);
  578. openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  579. tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  580. if (tap_get_state() != tap_get_end_state()) {
  581. openjtag_set_state(tap_get_end_state());
  582. tap_set_state(tap_get_end_state());
  583. }
  584. }
  585. static void openjtag_execute_runtest(struct jtag_command *cmd)
  586. {
  587. tap_state_t end_state = cmd->cmd.runtest->end_state;
  588. tap_set_end_state(end_state);
  589. /* only do a state_move when we're not already in IDLE */
  590. if (tap_get_state() != TAP_IDLE) {
  591. openjtag_set_state(TAP_IDLE);
  592. tap_set_state(TAP_IDLE);
  593. }
  594. if (cmd->cmd.runtest->num_cycles > 16)
  595. LOG_WARNING("num_cycles > 16 on run test");
  596. uint8_t command;
  597. command = 7;
  598. command |= ((cmd->cmd.runtest->num_cycles - 1) & 0x0F) << 4;
  599. openjtag_add_byte(command);
  600. tap_set_end_state(end_state);
  601. if (tap_get_end_state() != tap_get_state()) {
  602. openjtag_set_state(end_state);
  603. tap_set_state(end_state);
  604. }
  605. }
  606. static void openjtag_execute_command(struct jtag_command *cmd)
  607. {
  608. DEBUG_JTAG_IO("openjtag_execute_command %i", cmd->type);
  609. switch (cmd->type) {
  610. case JTAG_RESET:
  611. openjtag_execute_reset(cmd);
  612. break;
  613. case JTAG_SLEEP:
  614. openjtag_execute_sleep(cmd);
  615. break;
  616. case JTAG_TLR_RESET:
  617. openjtag_execute_statemove(cmd);
  618. break;
  619. case JTAG_SCAN:
  620. openjtag_execute_scan(cmd);
  621. break;
  622. case JTAG_RUNTEST:
  623. openjtag_execute_runtest(cmd);
  624. break;
  625. case JTAG_PATHMOVE:
  626. /* jlink_execute_pathmove(cmd); break; */
  627. default:
  628. LOG_ERROR("BUG: unknown Open JTAG command type encountered");
  629. exit(-1);
  630. }
  631. }
  632. static int openjtag_execute_queue(void)
  633. {
  634. struct jtag_command *cmd = jtag_command_queue;
  635. while (cmd != NULL) {
  636. openjtag_execute_command(cmd);
  637. cmd = cmd->next;
  638. }
  639. return openjtag_execute_tap_queue();
  640. }
  641. static int openjtag_speed_div(int speed, int *khz)
  642. {
  643. *khz = speed;
  644. return ERROR_OK;
  645. }
  646. static int openjtag_khz(int khz, int *jtag_speed)
  647. {
  648. if (khz >= 48000)
  649. *jtag_speed = 48000;
  650. else if (khz >= 24000)
  651. *jtag_speed = 24000;
  652. else if (khz >= 12000)
  653. *jtag_speed = 12000;
  654. else if (khz >= 6000)
  655. *jtag_speed = 6000;
  656. else if (khz >= 3000)
  657. *jtag_speed = 3000;
  658. else if (khz >= 1500)
  659. *jtag_speed = 1500;
  660. else if (khz >= 750)
  661. *jtag_speed = 750;
  662. else
  663. *jtag_speed = 375;
  664. return ERROR_OK;
  665. }
  666. COMMAND_HANDLER(openjtag_handle_device_desc_command)
  667. {
  668. if (CMD_ARGC == 1)
  669. openjtag_device_desc = strdup(CMD_ARGV[0]);
  670. else
  671. LOG_ERROR("require exactly one argument to "
  672. "openjtag_device_desc <description>");
  673. return ERROR_OK;
  674. }
  675. static const struct command_registration openjtag_command_handlers[] = {
  676. {
  677. .name = "openjtag_device_desc",
  678. .handler = openjtag_handle_device_desc_command,
  679. .mode = COMMAND_CONFIG,
  680. .help = "set the USB device description of the OpenJTAG",
  681. .usage = "description-string",
  682. },
  683. COMMAND_REGISTRATION_DONE
  684. };
  685. struct jtag_interface openjtag_interface = {
  686. .name = "openjtag",
  687. .commands = openjtag_command_handlers,
  688. .execute_queue = openjtag_execute_queue,
  689. .speed = openjtag_speed,
  690. .speed_div = openjtag_speed_div,
  691. .khz = openjtag_khz,
  692. .init = openjtag_init,
  693. .quit = openjtag_quit,
  694. };