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.
 
 
 
 
 
 

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