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.
 
 
 
 
 
 

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