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.
 
 
 
 
 
 

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