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.
 
 
 
 
 
 

576 lines
13 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, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. /**
  19. * @file
  20. * Holds driver for PRESTO programmer from ASIX.
  21. * http://tools.asix.net/prg_presto.htm
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #if IS_CYGWIN == 1
  27. #include "windows.h"
  28. #endif
  29. #include <jtag/interface.h>
  30. #include <helper/time_support.h>
  31. #include "bitq.h"
  32. /* PRESTO access library includes */
  33. #include <ftdi.h>
  34. /* -------------------------------------------------------------------------- */
  35. #define FT_DEVICE_NAME_LEN 64
  36. #define FT_DEVICE_SERNUM_LEN 64
  37. #define PRESTO_VID_PID 0x0403f1a0
  38. #define PRESTO_VID (0x0403)
  39. #define PRESTO_PID (0xf1a0)
  40. #define BUFFER_SIZE (64*62)
  41. struct presto {
  42. struct ftdi_context ftdic;
  43. int retval;
  44. char serial[FT_DEVICE_SERNUM_LEN];
  45. uint8_t buff_out[BUFFER_SIZE];
  46. int buff_out_pos;
  47. uint8_t buff_in[BUFFER_SIZE];
  48. int buff_in_exp;/* expected in buffer length */
  49. int buff_in_len;/* length of data received */
  50. int buff_in_pos;
  51. unsigned long total_out;
  52. unsigned long total_in;
  53. int jtag_tms; /* last tms state */
  54. int jtag_tck; /* last tck state */
  55. int jtag_rst; /* last trst state */
  56. int jtag_tdi_data;
  57. int jtag_tdi_count;
  58. int jtag_speed;
  59. };
  60. static struct presto presto_state;
  61. static struct presto *presto = &presto_state;
  62. static uint8_t presto_init_seq[] = {
  63. 0x80, 0xA0, 0xA8, 0xB0, 0xC0, 0xE0
  64. };
  65. static int presto_write(uint8_t *buf, uint32_t size)
  66. {
  67. uint32_t ftbytes;
  68. presto->retval = ftdi_write_data(&presto->ftdic, buf, size);
  69. if (presto->retval < 0) {
  70. LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&presto->ftdic));
  71. return ERROR_JTAG_DEVICE_ERROR;
  72. }
  73. ftbytes = presto->retval;
  74. if (ftbytes != size) {
  75. LOG_ERROR("couldn't write the requested number of bytes to PRESTO (%u < %u)",
  76. (unsigned)ftbytes, (unsigned)size);
  77. return ERROR_JTAG_DEVICE_ERROR;
  78. }
  79. return ERROR_OK;
  80. }
  81. static int presto_read(uint8_t *buf, uint32_t size)
  82. {
  83. uint32_t ftbytes = 0;
  84. struct timeval timeout, now;
  85. gettimeofday(&timeout, NULL);
  86. timeval_add_time(&timeout, 1, 0); /* one second timeout */
  87. while (ftbytes < size) {
  88. presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, size - ftbytes);
  89. if (presto->retval < 0) {
  90. LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic));
  91. return ERROR_JTAG_DEVICE_ERROR;
  92. }
  93. ftbytes += presto->retval;
  94. gettimeofday(&now, NULL);
  95. if ((now.tv_sec > timeout.tv_sec) ||
  96. ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec)))
  97. break;
  98. }
  99. if (ftbytes != size) {
  100. /* this is just a warning, there might have been timeout when detecting PRESTO,
  101. *which is not fatal */
  102. LOG_WARNING("couldn't read the requested number of bytes from PRESTO (%u < %u)",
  103. (unsigned)ftbytes, (unsigned)size);
  104. return ERROR_JTAG_DEVICE_ERROR;
  105. }
  106. return ERROR_OK;
  107. }
  108. static int presto_open_libftdi(char *req_serial)
  109. {
  110. uint8_t presto_data;
  111. LOG_DEBUG("searching for PRESTO using libftdi");
  112. /* initialize FTDI context structure */
  113. if (ftdi_init(&presto->ftdic) < 0) {
  114. LOG_ERROR("unable to init libftdi: %s", presto->ftdic.error_str);
  115. return ERROR_JTAG_DEVICE_ERROR;
  116. }
  117. /* context, vendor id, product id */
  118. if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0) {
  119. LOG_ERROR("unable to open PRESTO: %s", presto->ftdic.error_str);
  120. return ERROR_JTAG_DEVICE_ERROR;
  121. }
  122. if (ftdi_usb_reset(&presto->ftdic) < 0) {
  123. LOG_ERROR("unable to reset PRESTO device");
  124. return ERROR_JTAG_DEVICE_ERROR;
  125. }
  126. if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0) {
  127. LOG_ERROR("unable to set latency timer");
  128. return ERROR_JTAG_DEVICE_ERROR;
  129. }
  130. if (ftdi_usb_purge_buffers(&presto->ftdic) < 0) {
  131. LOG_ERROR("unable to purge PRESTO buffers");
  132. return ERROR_JTAG_DEVICE_ERROR;
  133. }
  134. presto_data = 0xD0;
  135. if (presto_write(&presto_data, 1) != ERROR_OK) {
  136. LOG_ERROR("error writing to PRESTO");
  137. return ERROR_JTAG_DEVICE_ERROR;
  138. }
  139. if (presto_read(&presto_data, 1) != ERROR_OK) {
  140. LOG_DEBUG("no response from PRESTO, retrying");
  141. if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
  142. return ERROR_JTAG_DEVICE_ERROR;
  143. presto_data = 0xD0;
  144. if (presto_write(&presto_data, 1) != ERROR_OK)
  145. return ERROR_JTAG_DEVICE_ERROR;
  146. if (presto_read(&presto_data, 1) != ERROR_OK) {
  147. LOG_ERROR("no response from PRESTO, giving up");
  148. return ERROR_JTAG_DEVICE_ERROR;
  149. }
  150. }
  151. if (presto_write(presto_init_seq, sizeof(presto_init_seq)) != ERROR_OK) {
  152. LOG_ERROR("error writing PRESTO init sequence");
  153. return ERROR_JTAG_DEVICE_ERROR;
  154. }
  155. return ERROR_OK;
  156. }
  157. static int presto_open(char *req_serial)
  158. {
  159. presto->buff_out_pos = 0;
  160. presto->buff_in_pos = 0;
  161. presto->buff_in_len = 0;
  162. presto->buff_in_exp = 0;
  163. presto->total_out = 0;
  164. presto->total_in = 0;
  165. presto->jtag_tms = 0;
  166. presto->jtag_tck = 0;
  167. presto->jtag_rst = 0;
  168. presto->jtag_tdi_data = 0;
  169. presto->jtag_tdi_count = 0;
  170. presto->jtag_speed = 0;
  171. return presto_open_libftdi(req_serial);
  172. }
  173. static int presto_close(void)
  174. {
  175. int result = ERROR_OK;
  176. presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq));
  177. if (presto->retval != sizeof(presto_init_seq))
  178. result = ERROR_JTAG_DEVICE_ERROR;
  179. presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16);
  180. if (presto->retval < 0)
  181. result = ERROR_JTAG_DEVICE_ERROR;
  182. presto->retval = ftdi_usb_close(&presto->ftdic);
  183. if (presto->retval < 0)
  184. result = ERROR_JTAG_DEVICE_ERROR;
  185. else
  186. ftdi_deinit(&presto->ftdic);
  187. return result;
  188. }
  189. static int presto_flush(void)
  190. {
  191. if (presto->buff_out_pos == 0)
  192. return ERROR_OK;
  193. if (presto->retval < 0) {
  194. LOG_DEBUG("error in previous communication, canceling I/O operation");
  195. return ERROR_JTAG_DEVICE_ERROR;
  196. }
  197. if (presto_write(presto->buff_out, presto->buff_out_pos) != ERROR_OK) {
  198. presto->buff_out_pos = 0;
  199. return ERROR_JTAG_DEVICE_ERROR;
  200. }
  201. presto->total_out += presto->buff_out_pos;
  202. presto->buff_out_pos = 0;
  203. if (presto->buff_in_exp == 0)
  204. return ERROR_OK;
  205. presto->buff_in_pos = 0;
  206. presto->buff_in_len = 0;
  207. if (presto_read(presto->buff_in, presto->buff_in_exp) != ERROR_OK) {
  208. presto->buff_in_exp = 0;
  209. return ERROR_JTAG_DEVICE_ERROR;
  210. }
  211. presto->total_in += presto->buff_in_exp;
  212. presto->buff_in_len = presto->buff_in_exp;
  213. presto->buff_in_exp = 0;
  214. return ERROR_OK;
  215. }
  216. static int presto_sendbyte(int data)
  217. {
  218. if (data == EOF)
  219. return presto_flush();
  220. if (presto->buff_out_pos < BUFFER_SIZE) {
  221. presto->buff_out[presto->buff_out_pos++] = (uint8_t)data;
  222. if (((data & 0xC0) == 0x40) || ((data & 0xD0) == 0xD0))
  223. presto->buff_in_exp++;
  224. } else
  225. return ERROR_JTAG_DEVICE_ERROR;
  226. /* libftdi does not do background read, be sure that USB IN buffer does not overflow (128
  227. *bytes only!) */
  228. if (presto->buff_out_pos >= BUFFER_SIZE || presto->buff_in_exp == 128)
  229. return presto_flush();
  230. return ERROR_OK;
  231. }
  232. #if 0
  233. static int presto_getbyte(void)
  234. {
  235. if (presto->buff_in_pos < presto->buff_in_len)
  236. return presto->buff_in[presto->buff_in_pos++];
  237. if (presto->buff_in_exp == 0)
  238. return -1;
  239. if (presto_flush() != ERROR_OK)
  240. return -1;
  241. if (presto->buff_in_pos < presto->buff_in_len)
  242. return presto->buff_in[presto->buff_in_pos++];
  243. return -1;
  244. }
  245. #endif
  246. /* -------------------------------------------------------------------------- */
  247. static int presto_tdi_flush(void)
  248. {
  249. if (presto->jtag_tdi_count == 0)
  250. return 0;
  251. if (presto->jtag_tck == 0) {
  252. LOG_ERROR("BUG: unexpected TAP condition, TCK low");
  253. return -1;
  254. }
  255. presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
  256. presto_sendbyte(presto->jtag_tdi_data);
  257. presto->jtag_tdi_count = 0;
  258. presto->jtag_tdi_data = 0;
  259. return 0;
  260. }
  261. static int presto_tck_idle(void)
  262. {
  263. if (presto->jtag_tck == 1) {
  264. presto_sendbyte(0xCA);
  265. presto->jtag_tck = 0;
  266. }
  267. return 0;
  268. }
  269. /* -------------------------------------------------------------------------- */
  270. static int presto_bitq_out(int tms, int tdi, int tdo_req)
  271. {
  272. int i;
  273. unsigned char cmd;
  274. if (presto->jtag_tck == 0)
  275. presto_sendbyte(0xA4); /* LED idicator - JTAG active */
  276. else if (presto->jtag_speed == 0 && !tdo_req && tms == presto->jtag_tms) {
  277. presto->jtag_tdi_data |= (tdi != 0) << presto->jtag_tdi_count;
  278. if (++presto->jtag_tdi_count == 4)
  279. presto_tdi_flush();
  280. return 0;
  281. }
  282. presto_tdi_flush();
  283. cmd = tdi ? 0xCB : 0xCA;
  284. presto_sendbyte(cmd);
  285. if (tms != presto->jtag_tms) {
  286. presto_sendbyte((tms ? 0xEC : 0xE8) | (presto->jtag_rst ? 0x02 : 0));
  287. presto->jtag_tms = tms;
  288. }
  289. /* delay with TCK low */
  290. for (i = presto->jtag_speed; i > 1; i--)
  291. presto_sendbyte(cmd);
  292. cmd |= 0x04;
  293. presto_sendbyte(cmd | (tdo_req ? 0x10 : 0));
  294. /* delay with TCK high */
  295. for (i = presto->jtag_speed; i > 1; i--)
  296. presto_sendbyte(cmd);
  297. presto->jtag_tck = 1;
  298. return 0;
  299. }
  300. static int presto_bitq_flush(void)
  301. {
  302. presto_tdi_flush();
  303. presto_tck_idle();
  304. presto_sendbyte(0xA0); /* LED idicator - JTAG idle */
  305. return presto_flush();
  306. }
  307. static int presto_bitq_in_rdy(void)
  308. {
  309. if (presto->buff_in_pos >= presto->buff_in_len)
  310. return 0;
  311. return presto->buff_in_len-presto->buff_in_pos;
  312. }
  313. static int presto_bitq_in(void)
  314. {
  315. if (presto->buff_in_pos >= presto->buff_in_len)
  316. return -1;
  317. if (presto->buff_in[presto->buff_in_pos++]&0x08)
  318. return 1;
  319. return 0;
  320. }
  321. static int presto_bitq_sleep(unsigned long us)
  322. {
  323. long waits;
  324. presto_tdi_flush();
  325. presto_tck_idle();
  326. if (us > 100000) {
  327. presto_bitq_flush();
  328. jtag_sleep(us);
  329. return 0;
  330. }
  331. waits = us / 170 + 2;
  332. while (waits--)
  333. presto_sendbyte(0x80);
  334. return 0;
  335. }
  336. static int presto_bitq_reset(int trst, int srst)
  337. {
  338. presto_tdi_flush();
  339. presto_tck_idle();
  340. /* add a delay after possible TCK transition */
  341. presto_sendbyte(0x80);
  342. presto_sendbyte(0x80);
  343. presto->jtag_rst = trst || srst;
  344. presto_sendbyte((presto->jtag_rst ? 0xEA : 0xE8) | (presto->jtag_tms ? 0x04 : 0));
  345. return 0;
  346. }
  347. static struct bitq_interface presto_bitq = {
  348. .out = &presto_bitq_out,
  349. .flush = &presto_bitq_flush,
  350. .sleep = &presto_bitq_sleep,
  351. .reset = &presto_bitq_reset,
  352. .in_rdy = &presto_bitq_in_rdy,
  353. .in = &presto_bitq_in,
  354. };
  355. /* -------------------------------------------------------------------------- */
  356. static int presto_adapter_khz(int khz, int *jtag_speed)
  357. {
  358. if (khz < 0) {
  359. *jtag_speed = 0;
  360. return ERROR_COMMAND_SYNTAX_ERROR;
  361. }
  362. if (khz >= 3000)
  363. *jtag_speed = 0;
  364. else
  365. *jtag_speed = (1000 + khz-1)/khz;
  366. return 0;
  367. }
  368. static int presto_jtag_speed_div(int speed, int *khz)
  369. {
  370. if ((speed < 0) || (speed > 1000)) {
  371. *khz = 0;
  372. return ERROR_COMMAND_SYNTAX_ERROR;
  373. }
  374. if (speed == 0)
  375. *khz = 3000;
  376. else
  377. *khz = 1000/speed;
  378. return 0;
  379. }
  380. static int presto_jtag_speed(int speed)
  381. {
  382. int khz;
  383. if (presto_jtag_speed_div(speed, &khz))
  384. return ERROR_COMMAND_SYNTAX_ERROR;
  385. presto->jtag_speed = speed;
  386. if (khz%1000 == 0)
  387. LOG_INFO("setting speed to %d, max. TCK freq. is %d MHz", speed, khz/1000);
  388. else
  389. LOG_INFO("setting speed to %d, max. TCK freq. is %d kHz", speed, khz);
  390. return 0;
  391. }
  392. static char *presto_serial;
  393. COMMAND_HANDLER(presto_handle_serial_command)
  394. {
  395. if (CMD_ARGC == 1) {
  396. if (presto_serial)
  397. free(presto_serial);
  398. presto_serial = strdup(CMD_ARGV[0]);
  399. } else
  400. return ERROR_COMMAND_SYNTAX_ERROR;
  401. return ERROR_OK;
  402. }
  403. static const struct command_registration presto_command_handlers[] = {
  404. {
  405. .name = "presto_serial",
  406. .handler = presto_handle_serial_command,
  407. .mode = COMMAND_CONFIG,
  408. .help = "Configure USB serial number of Presto device.",
  409. .usage = "serial_string",
  410. },
  411. COMMAND_REGISTRATION_DONE
  412. };
  413. static int presto_jtag_init(void)
  414. {
  415. if (presto_open(presto_serial) != ERROR_OK) {
  416. presto_close();
  417. if (presto_serial != NULL)
  418. LOG_ERROR("Cannot open PRESTO, serial number '%s'", presto_serial);
  419. else
  420. LOG_ERROR("Cannot open PRESTO");
  421. return ERROR_JTAG_INIT_FAILED;
  422. }
  423. LOG_INFO("PRESTO open, serial number '%s'", presto->serial);
  424. bitq_interface = &presto_bitq;
  425. return ERROR_OK;
  426. }
  427. static int presto_jtag_quit(void)
  428. {
  429. bitq_cleanup();
  430. presto_close();
  431. LOG_INFO("PRESTO closed");
  432. if (presto_serial) {
  433. free(presto_serial);
  434. presto_serial = NULL;
  435. }
  436. return ERROR_OK;
  437. }
  438. struct jtag_interface presto_interface = {
  439. .name = "presto",
  440. .commands = presto_command_handlers,
  441. .execute_queue = bitq_execute_queue,
  442. .speed = presto_jtag_speed,
  443. .khz = presto_adapter_khz,
  444. .speed_div = presto_jtag_speed_div,
  445. .init = presto_jtag_init,
  446. .quit = presto_jtag_quit,
  447. };