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.
 
 
 
 
 
 

797 lines
20 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Pavel Chromy *
  3. * chromy@asix.cz *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. /**
  21. * @file
  22. * Holds driver for PRESTO programmer from ASIX.
  23. * http://tools.asix.net/prg_presto.htm
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #if IS_CYGWIN == 1
  29. #include "windows.h"
  30. #endif
  31. #include <jtag/interface.h>
  32. #include <helper/time_support.h>
  33. #include "bitq.h"
  34. /* PRESTO access library includes */
  35. #if BUILD_PRESTO_FTD2XX == 1
  36. #include <ftd2xx.h>
  37. #include "ftd2xx_common.h"
  38. #elif BUILD_PRESTO_LIBFTDI == 1
  39. #include <ftdi.h>
  40. #else
  41. #error "BUG: either FTD2XX and LIBFTDI has to be used"
  42. #endif
  43. /* -------------------------------------------------------------------------- */
  44. #define FT_DEVICE_NAME_LEN 64
  45. #define FT_DEVICE_SERNUM_LEN 64
  46. #define PRESTO_VID_PID 0x0403f1a0
  47. #define PRESTO_VID (0x0403)
  48. #define PRESTO_PID (0xf1a0)
  49. #define BUFFER_SIZE (64*62)
  50. struct presto {
  51. #if BUILD_PRESTO_FTD2XX == 1
  52. FT_HANDLE handle;
  53. FT_STATUS status;
  54. #elif BUILD_PRESTO_LIBFTDI == 1
  55. struct ftdi_context ftdic;
  56. int retval;
  57. #endif
  58. char serial[FT_DEVICE_SERNUM_LEN];
  59. uint8_t buff_out[BUFFER_SIZE];
  60. int buff_out_pos;
  61. uint8_t buff_in[BUFFER_SIZE];
  62. int buff_in_exp;/* expected in buffer length */
  63. int buff_in_len;/* length of data received */
  64. int buff_in_pos;
  65. unsigned long total_out;
  66. unsigned long total_in;
  67. int jtag_tms; /* last tms state */
  68. int jtag_tck; /* last tck state */
  69. int jtag_rst; /* last trst state */
  70. int jtag_tdi_data;
  71. int jtag_tdi_count;
  72. int jtag_speed;
  73. };
  74. static struct presto presto_state;
  75. static struct presto *presto = &presto_state;
  76. static uint8_t presto_init_seq[] = {
  77. 0x80, 0xA0, 0xA8, 0xB0, 0xC0, 0xE0
  78. };
  79. static int presto_write(uint8_t *buf, uint32_t size)
  80. {
  81. #if BUILD_PRESTO_FTD2XX == 1
  82. DWORD ftbytes;
  83. presto->status = FT_Write(presto->handle, buf, size, &ftbytes);
  84. if (presto->status != FT_OK) {
  85. LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(presto->status));
  86. return ERROR_JTAG_DEVICE_ERROR;
  87. }
  88. #elif BUILD_PRESTO_LIBFTDI == 1
  89. uint32_t ftbytes;
  90. presto->retval = ftdi_write_data(&presto->ftdic, buf, size);
  91. if (presto->retval < 0) {
  92. LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&presto->ftdic));
  93. return ERROR_JTAG_DEVICE_ERROR;
  94. }
  95. ftbytes = presto->retval;
  96. #endif
  97. if (ftbytes != size) {
  98. LOG_ERROR("couldn't write the requested number of bytes to PRESTO (%u < %u)",
  99. (unsigned)ftbytes, (unsigned)size);
  100. return ERROR_JTAG_DEVICE_ERROR;
  101. }
  102. return ERROR_OK;
  103. }
  104. static int presto_read(uint8_t *buf, uint32_t size)
  105. {
  106. #if BUILD_PRESTO_FTD2XX == 1
  107. DWORD ftbytes;
  108. presto->status = FT_Read(presto->handle, buf, size, &ftbytes);
  109. if (presto->status != FT_OK) {
  110. LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(presto->status));
  111. return ERROR_JTAG_DEVICE_ERROR;
  112. }
  113. #elif BUILD_PRESTO_LIBFTDI == 1
  114. uint32_t ftbytes = 0;
  115. struct timeval timeout, now;
  116. gettimeofday(&timeout, NULL);
  117. timeval_add_time(&timeout, 1, 0); /* one second timeout */
  118. while (ftbytes < size) {
  119. presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, size - ftbytes);
  120. if (presto->retval < 0) {
  121. LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic));
  122. return ERROR_JTAG_DEVICE_ERROR;
  123. }
  124. ftbytes += presto->retval;
  125. gettimeofday(&now, NULL);
  126. if ((now.tv_sec > timeout.tv_sec) ||
  127. ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec)))
  128. break;
  129. }
  130. #endif
  131. if (ftbytes != size) {
  132. /* this is just a warning, there might have been timeout when detecting PRESTO,
  133. *which is not fatal */
  134. LOG_WARNING("couldn't read the requested number of bytes from PRESTO (%u < %u)",
  135. (unsigned)ftbytes, (unsigned)size);
  136. return ERROR_JTAG_DEVICE_ERROR;
  137. }
  138. return ERROR_OK;
  139. }
  140. #if BUILD_PRESTO_FTD2XX == 1
  141. static int presto_open_ftd2xx(char *req_serial)
  142. {
  143. uint32_t i;
  144. DWORD numdevs;
  145. DWORD vidpid;
  146. char devname[FT_DEVICE_NAME_LEN];
  147. FT_DEVICE device;
  148. BYTE presto_data;
  149. DWORD ftbytes;
  150. presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
  151. #if IS_WIN32 == 0
  152. /* Add non-standard Vid/Pid to the linux driver */
  153. presto->status = FT_SetVIDPID(PRESTO_VID, PRESTO_PID);
  154. if (presto->status != FT_OK) {
  155. LOG_ERROR("couldn't add PRESTO VID/PID");
  156. exit(-1);
  157. }
  158. #endif
  159. presto->status = FT_ListDevices(&numdevs, NULL, FT_LIST_NUMBER_ONLY);
  160. if (presto->status != FT_OK) {
  161. LOG_ERROR("FT_ListDevices failed: %s", ftd2xx_status_string(presto->status));
  162. return ERROR_JTAG_DEVICE_ERROR;
  163. }
  164. LOG_DEBUG("FTDI devices available: %" PRIu32, (uint32_t)numdevs);
  165. for (i = 0; i < numdevs; i++) {
  166. presto->status = FT_Open(i, &(presto->handle));
  167. if (presto->status != FT_OK) {
  168. /* this is not fatal, the device may be legitimately open by other process,
  169. *hence debug message only */
  170. LOG_DEBUG("FT_Open failed: %s", ftd2xx_status_string(presto->status));
  171. continue;
  172. }
  173. LOG_DEBUG("FTDI device %i open", (int)i);
  174. presto->status = FT_GetDeviceInfo(presto->handle, &device,
  175. &vidpid, presto->serial, devname, NULL);
  176. if (presto->status == FT_OK) {
  177. if (vidpid == PRESTO_VID_PID && (req_serial == NULL ||
  178. !strcmp(presto->serial, req_serial)))
  179. break;
  180. } else
  181. LOG_DEBUG("FT_GetDeviceInfo failed: %s", ftd2xx_status_string(
  182. presto->status));
  183. LOG_DEBUG("FTDI device %i does not match, closing", (int)i);
  184. FT_Close(presto->handle);
  185. presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
  186. }
  187. if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
  188. return ERROR_JTAG_DEVICE_ERROR; /* presto not open, return */
  189. presto->status = FT_SetLatencyTimer(presto->handle, 1);
  190. if (presto->status != FT_OK)
  191. return ERROR_JTAG_DEVICE_ERROR;
  192. presto->status = FT_SetTimeouts(presto->handle, 100, 0);
  193. if (presto->status != FT_OK)
  194. return ERROR_JTAG_DEVICE_ERROR;
  195. presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
  196. if (presto->status != FT_OK)
  197. return ERROR_JTAG_DEVICE_ERROR;
  198. presto_data = 0xD0;
  199. presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes);
  200. if (presto->status != FT_OK)
  201. return ERROR_JTAG_DEVICE_ERROR;
  202. /* delay between first write/read turnaround (after purge?) necessary
  203. * under Linux for unknown reason,
  204. * probably a bug in library threading */
  205. usleep(100000);
  206. presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes);
  207. if (presto->status != FT_OK)
  208. return ERROR_JTAG_DEVICE_ERROR;
  209. if (ftbytes != 1) {
  210. LOG_DEBUG("PRESTO reset");
  211. presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
  212. if (presto->status != FT_OK)
  213. return ERROR_JTAG_DEVICE_ERROR;
  214. presto->status = FT_SetBitMode(presto->handle, 0x80, 1);
  215. if (presto->status != FT_OK)
  216. return ERROR_JTAG_DEVICE_ERROR;
  217. presto->status = FT_SetBaudRate(presto->handle, 9600);
  218. if (presto->status != FT_OK)
  219. return ERROR_JTAG_DEVICE_ERROR;
  220. presto_data = 0;
  221. for (i = 0; i < 4 * 62; i++) {
  222. presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes);
  223. if (presto->status != FT_OK)
  224. return ERROR_JTAG_DEVICE_ERROR;
  225. }
  226. usleep(100000);
  227. presto->status = FT_SetBitMode(presto->handle, 0x00, 0);
  228. if (presto->status != FT_OK)
  229. return ERROR_JTAG_DEVICE_ERROR;
  230. presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
  231. if (presto->status != FT_OK)
  232. return ERROR_JTAG_DEVICE_ERROR;
  233. presto_data = 0xD0;
  234. presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes);
  235. if (presto->status != FT_OK)
  236. return ERROR_JTAG_DEVICE_ERROR;
  237. /* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason,
  238. probably a bug in library threading */
  239. usleep(100000);
  240. presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes);
  241. if (presto->status != FT_OK)
  242. return ERROR_JTAG_DEVICE_ERROR;
  243. if (ftbytes != 1) {
  244. LOG_DEBUG("PRESTO not responding");
  245. return ERROR_JTAG_DEVICE_ERROR;
  246. }
  247. }
  248. presto->status = FT_SetTimeouts(presto->handle, 0, 0);
  249. if (presto->status != FT_OK)
  250. return ERROR_JTAG_DEVICE_ERROR;
  251. presto->status = FT_Write(presto->handle, &presto_init_seq,
  252. sizeof(presto_init_seq), &ftbytes);
  253. if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
  254. return ERROR_JTAG_DEVICE_ERROR;
  255. return ERROR_OK;
  256. }
  257. #elif BUILD_PRESTO_LIBFTDI == 1
  258. static int presto_open_libftdi(char *req_serial)
  259. {
  260. uint8_t presto_data;
  261. LOG_DEBUG("searching for PRESTO using libftdi");
  262. /* initialize FTDI context structure */
  263. if (ftdi_init(&presto->ftdic) < 0) {
  264. LOG_ERROR("unable to init libftdi: %s", presto->ftdic.error_str);
  265. return ERROR_JTAG_DEVICE_ERROR;
  266. }
  267. /* context, vendor id, product id */
  268. if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0) {
  269. LOG_ERROR("unable to open PRESTO: %s", presto->ftdic.error_str);
  270. return ERROR_JTAG_DEVICE_ERROR;
  271. }
  272. if (ftdi_usb_reset(&presto->ftdic) < 0) {
  273. LOG_ERROR("unable to reset PRESTO device");
  274. return ERROR_JTAG_DEVICE_ERROR;
  275. }
  276. if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0) {
  277. LOG_ERROR("unable to set latency timer");
  278. return ERROR_JTAG_DEVICE_ERROR;
  279. }
  280. if (ftdi_usb_purge_buffers(&presto->ftdic) < 0) {
  281. LOG_ERROR("unable to purge PRESTO buffers");
  282. return ERROR_JTAG_DEVICE_ERROR;
  283. }
  284. presto_data = 0xD0;
  285. if (presto_write(&presto_data, 1) != ERROR_OK) {
  286. LOG_ERROR("error writing to PRESTO");
  287. return ERROR_JTAG_DEVICE_ERROR;
  288. }
  289. if (presto_read(&presto_data, 1) != ERROR_OK) {
  290. LOG_DEBUG("no response from PRESTO, retrying");
  291. if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
  292. return ERROR_JTAG_DEVICE_ERROR;
  293. presto_data = 0xD0;
  294. if (presto_write(&presto_data, 1) != ERROR_OK)
  295. return ERROR_JTAG_DEVICE_ERROR;
  296. if (presto_read(&presto_data, 1) != ERROR_OK) {
  297. LOG_ERROR("no response from PRESTO, giving up");
  298. return ERROR_JTAG_DEVICE_ERROR;
  299. }
  300. }
  301. if (presto_write(presto_init_seq, sizeof(presto_init_seq)) != ERROR_OK) {
  302. LOG_ERROR("error writing PRESTO init sequence");
  303. return ERROR_JTAG_DEVICE_ERROR;
  304. }
  305. return ERROR_OK;
  306. }
  307. #endif /* BUILD_PRESTO_LIBFTDI == 1 */
  308. static int presto_open(char *req_serial)
  309. {
  310. presto->buff_out_pos = 0;
  311. presto->buff_in_pos = 0;
  312. presto->buff_in_len = 0;
  313. presto->buff_in_exp = 0;
  314. presto->total_out = 0;
  315. presto->total_in = 0;
  316. presto->jtag_tms = 0;
  317. presto->jtag_tck = 0;
  318. presto->jtag_rst = 0;
  319. presto->jtag_tdi_data = 0;
  320. presto->jtag_tdi_count = 0;
  321. presto->jtag_speed = 0;
  322. #if BUILD_PRESTO_FTD2XX == 1
  323. return presto_open_ftd2xx(req_serial);
  324. #elif BUILD_PRESTO_LIBFTDI == 1
  325. return presto_open_libftdi(req_serial);
  326. #endif
  327. }
  328. static int presto_close(void)
  329. {
  330. int result = ERROR_OK;
  331. #if BUILD_PRESTO_FTD2XX == 1
  332. DWORD ftbytes;
  333. if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
  334. return result;
  335. presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
  336. if (presto->status != FT_OK)
  337. result = ERROR_JTAG_DEVICE_ERROR;
  338. presto->status = FT_Write(presto->handle,
  339. &presto_init_seq,
  340. sizeof(presto_init_seq),
  341. &ftbytes);
  342. if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
  343. result = ERROR_JTAG_DEVICE_ERROR;
  344. presto->status = FT_SetLatencyTimer(presto->handle, 16);
  345. if (presto->status != FT_OK)
  346. result = ERROR_JTAG_DEVICE_ERROR;
  347. presto->status = FT_Close(presto->handle);
  348. if (presto->status != FT_OK)
  349. result = ERROR_JTAG_DEVICE_ERROR;
  350. else
  351. presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
  352. #elif BUILD_PRESTO_LIBFTDI == 1
  353. presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq));
  354. if (presto->retval != sizeof(presto_init_seq))
  355. result = ERROR_JTAG_DEVICE_ERROR;
  356. presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16);
  357. if (presto->retval < 0)
  358. result = ERROR_JTAG_DEVICE_ERROR;
  359. presto->retval = ftdi_usb_close(&presto->ftdic);
  360. if (presto->retval < 0)
  361. result = ERROR_JTAG_DEVICE_ERROR;
  362. else
  363. ftdi_deinit(&presto->ftdic);
  364. #endif
  365. return result;
  366. }
  367. static int presto_flush(void)
  368. {
  369. if (presto->buff_out_pos == 0)
  370. return ERROR_OK;
  371. #if BUILD_PRESTO_FTD2XX == 1
  372. if (presto->status != FT_OK) {
  373. #elif BUILD_PRESTO_LIBFTDI == 1
  374. if (presto->retval < 0) {
  375. #endif
  376. LOG_DEBUG("error in previous communication, canceling I/O operation");
  377. return ERROR_JTAG_DEVICE_ERROR;
  378. }
  379. if (presto_write(presto->buff_out, presto->buff_out_pos) != ERROR_OK) {
  380. presto->buff_out_pos = 0;
  381. return ERROR_JTAG_DEVICE_ERROR;
  382. }
  383. presto->total_out += presto->buff_out_pos;
  384. presto->buff_out_pos = 0;
  385. if (presto->buff_in_exp == 0)
  386. return ERROR_OK;
  387. presto->buff_in_pos = 0;
  388. presto->buff_in_len = 0;
  389. if (presto_read(presto->buff_in, presto->buff_in_exp) != ERROR_OK) {
  390. presto->buff_in_exp = 0;
  391. return ERROR_JTAG_DEVICE_ERROR;
  392. }
  393. presto->total_in += presto->buff_in_exp;
  394. presto->buff_in_len = presto->buff_in_exp;
  395. presto->buff_in_exp = 0;
  396. return ERROR_OK;
  397. }
  398. static int presto_sendbyte(int data)
  399. {
  400. if (data == EOF)
  401. return presto_flush();
  402. if (presto->buff_out_pos < BUFFER_SIZE) {
  403. presto->buff_out[presto->buff_out_pos++] = (uint8_t)data;
  404. if (((data & 0xC0) == 0x40) || ((data & 0xD0) == 0xD0))
  405. presto->buff_in_exp++;
  406. } else
  407. return ERROR_JTAG_DEVICE_ERROR;
  408. #if BUILD_PRESTO_FTD2XX == 1
  409. if (presto->buff_out_pos >= BUFFER_SIZE)
  410. #elif BUILD_PRESTO_LIBFTDI == 1
  411. /* libftdi does not do background read, be sure that USB IN buffer does not overflow (128
  412. *bytes only!) */
  413. if (presto->buff_out_pos >= BUFFER_SIZE || presto->buff_in_exp == 128)
  414. #endif
  415. return presto_flush();
  416. return ERROR_OK;
  417. }
  418. #if 0
  419. static int presto_getbyte(void)
  420. {
  421. if (presto->buff_in_pos < presto->buff_in_len)
  422. return presto->buff_in[presto->buff_in_pos++];
  423. if (presto->buff_in_exp == 0)
  424. return -1;
  425. if (presto_flush() != ERROR_OK)
  426. return -1;
  427. if (presto->buff_in_pos < presto->buff_in_len)
  428. return presto->buff_in[presto->buff_in_pos++];
  429. return -1;
  430. }
  431. #endif
  432. /* -------------------------------------------------------------------------- */
  433. static int presto_tdi_flush(void)
  434. {
  435. if (presto->jtag_tdi_count == 0)
  436. return 0;
  437. if (presto->jtag_tck == 0) {
  438. LOG_ERROR("BUG: unexpected TAP condition, TCK low");
  439. return -1;
  440. }
  441. presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
  442. presto_sendbyte(presto->jtag_tdi_data);
  443. presto->jtag_tdi_count = 0;
  444. presto->jtag_tdi_data = 0;
  445. return 0;
  446. }
  447. static int presto_tck_idle(void)
  448. {
  449. if (presto->jtag_tck == 1) {
  450. presto_sendbyte(0xCA);
  451. presto->jtag_tck = 0;
  452. }
  453. return 0;
  454. }
  455. /* -------------------------------------------------------------------------- */
  456. static int presto_bitq_out(int tms, int tdi, int tdo_req)
  457. {
  458. int i;
  459. unsigned char cmd;
  460. if (presto->jtag_tck == 0)
  461. presto_sendbyte(0xA4); /* LED idicator - JTAG active */
  462. else if (presto->jtag_speed == 0 && !tdo_req && tms == presto->jtag_tms) {
  463. presto->jtag_tdi_data |= (tdi != 0) << presto->jtag_tdi_count;
  464. if (++presto->jtag_tdi_count == 4)
  465. presto_tdi_flush();
  466. return 0;
  467. }
  468. presto_tdi_flush();
  469. cmd = tdi ? 0xCB : 0xCA;
  470. presto_sendbyte(cmd);
  471. if (tms != presto->jtag_tms) {
  472. presto_sendbyte((tms ? 0xEC : 0xE8) | (presto->jtag_rst ? 0x02 : 0));
  473. presto->jtag_tms = tms;
  474. }
  475. /* delay with TCK low */
  476. for (i = presto->jtag_speed; i > 1; i--)
  477. presto_sendbyte(cmd);
  478. cmd |= 0x04;
  479. presto_sendbyte(cmd | (tdo_req ? 0x10 : 0));
  480. /* delay with TCK high */
  481. for (i = presto->jtag_speed; i > 1; i--)
  482. presto_sendbyte(cmd);
  483. presto->jtag_tck = 1;
  484. return 0;
  485. }
  486. static int presto_bitq_flush(void)
  487. {
  488. presto_tdi_flush();
  489. presto_tck_idle();
  490. presto_sendbyte(0xA0); /* LED idicator - JTAG idle */
  491. return presto_flush();
  492. }
  493. static int presto_bitq_in_rdy(void)
  494. {
  495. if (presto->buff_in_pos >= presto->buff_in_len)
  496. return 0;
  497. return presto->buff_in_len-presto->buff_in_pos;
  498. }
  499. static int presto_bitq_in(void)
  500. {
  501. if (presto->buff_in_pos >= presto->buff_in_len)
  502. return -1;
  503. if (presto->buff_in[presto->buff_in_pos++]&0x08)
  504. return 1;
  505. return 0;
  506. }
  507. static int presto_bitq_sleep(unsigned long us)
  508. {
  509. long waits;
  510. presto_tdi_flush();
  511. presto_tck_idle();
  512. if (us > 100000) {
  513. presto_bitq_flush();
  514. jtag_sleep(us);
  515. return 0;
  516. }
  517. waits = us / 170 + 2;
  518. while (waits--)
  519. presto_sendbyte(0x80);
  520. return 0;
  521. }
  522. static int presto_bitq_reset(int trst, int srst)
  523. {
  524. presto_tdi_flush();
  525. presto_tck_idle();
  526. /* add a delay after possible TCK transition */
  527. presto_sendbyte(0x80);
  528. presto_sendbyte(0x80);
  529. presto->jtag_rst = trst || srst;
  530. presto_sendbyte((presto->jtag_rst ? 0xEA : 0xE8) | (presto->jtag_tms ? 0x04 : 0));
  531. return 0;
  532. }
  533. static struct bitq_interface presto_bitq = {
  534. .out = &presto_bitq_out,
  535. .flush = &presto_bitq_flush,
  536. .sleep = &presto_bitq_sleep,
  537. .reset = &presto_bitq_reset,
  538. .in_rdy = &presto_bitq_in_rdy,
  539. .in = &presto_bitq_in,
  540. };
  541. /* -------------------------------------------------------------------------- */
  542. static int presto_adapter_khz(int khz, int *jtag_speed)
  543. {
  544. if (khz < 0) {
  545. *jtag_speed = 0;
  546. return ERROR_COMMAND_SYNTAX_ERROR;
  547. }
  548. if (khz >= 3000)
  549. *jtag_speed = 0;
  550. else
  551. *jtag_speed = (1000 + khz-1)/khz;
  552. return 0;
  553. }
  554. static int presto_jtag_speed_div(int speed, int *khz)
  555. {
  556. if ((speed < 0) || (speed > 1000)) {
  557. *khz = 0;
  558. return ERROR_COMMAND_SYNTAX_ERROR;
  559. }
  560. if (speed == 0)
  561. *khz = 3000;
  562. else
  563. *khz = 1000/speed;
  564. return 0;
  565. }
  566. static int presto_jtag_speed(int speed)
  567. {
  568. int khz;
  569. if (presto_jtag_speed_div(speed, &khz))
  570. return ERROR_COMMAND_SYNTAX_ERROR;
  571. presto->jtag_speed = speed;
  572. if (khz%1000 == 0)
  573. LOG_INFO("setting speed to %d, max. TCK freq. is %d MHz", speed, khz/1000);
  574. else
  575. LOG_INFO("setting speed to %d, max. TCK freq. is %d kHz", speed, khz);
  576. return 0;
  577. }
  578. static char *presto_serial;
  579. COMMAND_HANDLER(presto_handle_serial_command)
  580. {
  581. if (CMD_ARGC == 1) {
  582. if (presto_serial)
  583. free(presto_serial);
  584. presto_serial = strdup(CMD_ARGV[0]);
  585. } else
  586. return ERROR_COMMAND_SYNTAX_ERROR;
  587. return ERROR_OK;
  588. }
  589. static const struct command_registration presto_command_handlers[] = {
  590. {
  591. .name = "presto_serial",
  592. .handler = presto_handle_serial_command,
  593. .mode = COMMAND_CONFIG,
  594. .help = "Configure USB serial number of Presto device.",
  595. .usage = "serial_string",
  596. },
  597. COMMAND_REGISTRATION_DONE
  598. };
  599. static int presto_jtag_init(void)
  600. {
  601. if (presto_open(presto_serial) != ERROR_OK) {
  602. presto_close();
  603. if (presto_serial != NULL)
  604. LOG_ERROR("Cannot open PRESTO, serial number '%s'", presto_serial);
  605. else
  606. LOG_ERROR("Cannot open PRESTO");
  607. return ERROR_JTAG_INIT_FAILED;
  608. }
  609. LOG_INFO("PRESTO open, serial number '%s'", presto->serial);
  610. bitq_interface = &presto_bitq;
  611. return ERROR_OK;
  612. }
  613. static int presto_jtag_quit(void)
  614. {
  615. bitq_cleanup();
  616. presto_close();
  617. LOG_INFO("PRESTO closed");
  618. if (presto_serial) {
  619. free(presto_serial);
  620. presto_serial = NULL;
  621. }
  622. return ERROR_OK;
  623. }
  624. struct jtag_interface presto_interface = {
  625. .name = "presto",
  626. .commands = presto_command_handlers,
  627. .execute_queue = bitq_execute_queue,
  628. .speed = presto_jtag_speed,
  629. .khz = presto_adapter_khz,
  630. .speed_div = presto_jtag_speed_div,
  631. .init = presto_jtag_init,
  632. .quit = presto_jtag_quit,
  633. };