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.
 
 
 
 
 
 

687 lines
16 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Benedikt Sauter *
  3. * sauter@ixbat.de *
  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. * This file is based on Dominic Rath's amt_jtagaccel.c.
  22. *
  23. * usbprog is a free programming adapter. You can easily install
  24. * different firmware versions from an "online pool" over USB.
  25. * The adapter can be used for programming and debugging AVR and ARM
  26. * processors, as USB to RS232 converter, as JTAG interface or as
  27. * simple I/O interface (5 lines).
  28. *
  29. * http://www.embedded-projects.net/usbprog
  30. */
  31. #ifdef HAVE_CONFIG_H
  32. #include "config.h"
  33. #endif
  34. #include "replacements.h"
  35. #include "jtag.h"
  36. #include <usb.h>
  37. /* system includes */
  38. #include "log.h"
  39. #define VID 0x1781
  40. #define PID 0x0c63
  41. /* Pins at usbprog */
  42. #define TDO_BIT 0
  43. #define TDI_BIT 3
  44. #define TCK_BIT 2
  45. #define TMS_BIT 1
  46. int usbprog_execute_queue(void);
  47. int usbprog_speed(int speed);
  48. int usbprog_register_commands(struct command_context_s *cmd_ctx);
  49. int usbprog_init(void);
  50. int usbprog_quit(void);
  51. void usbprog_end_state(enum tap_state state);
  52. void usbprog_state_move(void);
  53. void usbprog_path_move(pathmove_command_t *cmd);
  54. void usbprog_runtest(int num_cycles);
  55. void usbprog_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size);
  56. jtag_interface_t usbprog_interface =
  57. {
  58. .name = "usbprog",
  59. .execute_queue = usbprog_execute_queue,
  60. .speed = usbprog_speed,
  61. .register_commands = usbprog_register_commands,
  62. .init = usbprog_init,
  63. .quit = usbprog_quit
  64. };
  65. #define UNKOWN_COMMAND 0x00
  66. #define PORT_DIRECTION 0x01
  67. #define PORT_SET 0x02
  68. #define PORT_GET 0x03
  69. #define PORT_SETBIT 0x04
  70. #define PORT_GETBIT 0x05
  71. #define WRITE_TDI 0x06
  72. #define READ_TDO 0x07
  73. #define WRITE_AND_READ 0x08
  74. #define WRITE_TMS 0x09
  75. #define WRITE_TMS_CHAIN 0x0A
  76. struct usbprog_jtag
  77. {
  78. struct usb_dev_handle* usb_handle;
  79. };
  80. struct usbprog_jtag * usbprog_jtag_handle;
  81. struct usbprog_jtag* usbprog_jtag_open();
  82. void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag);
  83. void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag);
  84. unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen);
  85. void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
  86. void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
  87. void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
  88. void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan);
  89. char tms_chain[64];
  90. int tms_chain_index;
  91. void usbprog_jtag_tms_collect(char tms_scan);
  92. void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag);
  93. void usbprog_write(int tck, int tms, int tdi);
  94. void usbprog_reset(int trst, int srst);
  95. void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction);
  96. void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value);
  97. unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag);
  98. void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value);
  99. int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit);
  100. int usbprog_speed(int speed)
  101. {
  102. return ERROR_OK;
  103. }
  104. int usbprog_register_commands(struct command_context_s *cmd_ctx)
  105. {
  106. return ERROR_OK;
  107. }
  108. int usbprog_execute_queue(void)
  109. {
  110. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  111. int scan_size;
  112. enum scan_type type;
  113. u8 *buffer;
  114. while (cmd)
  115. {
  116. switch (cmd->type)
  117. {
  118. case JTAG_END_STATE:
  119. #ifdef _DEBUG_JTAG_IO_
  120. LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
  121. #endif
  122. if (cmd->cmd.end_state->end_state != -1)
  123. usbprog_end_state(cmd->cmd.end_state->end_state);
  124. break;
  125. case JTAG_RESET:
  126. #ifdef _DEBUG_JTAG_IO_
  127. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  128. #endif
  129. if (cmd->cmd.reset->trst == 1)
  130. {
  131. cur_state = TAP_RESET;
  132. }
  133. usbprog_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  134. break;
  135. case JTAG_RUNTEST:
  136. #ifdef _DEBUG_JTAG_IO_
  137. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  138. #endif
  139. if (cmd->cmd.runtest->end_state != -1)
  140. usbprog_end_state(cmd->cmd.runtest->end_state);
  141. usbprog_runtest(cmd->cmd.runtest->num_cycles);
  142. break;
  143. case JTAG_STATEMOVE:
  144. #ifdef _DEBUG_JTAG_IO_
  145. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  146. #endif
  147. if (cmd->cmd.statemove->end_state != -1)
  148. usbprog_end_state(cmd->cmd.statemove->end_state);
  149. usbprog_state_move();
  150. break;
  151. case JTAG_PATHMOVE:
  152. #ifdef _DEBUG_JTAG_IO_
  153. LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
  154. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  155. #endif
  156. usbprog_path_move(cmd->cmd.pathmove);
  157. break;
  158. case JTAG_SCAN:
  159. #ifdef _DEBUG_JTAG_IO_
  160. LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  161. #endif
  162. if (cmd->cmd.scan->end_state != -1)
  163. usbprog_end_state(cmd->cmd.scan->end_state);
  164. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  165. type = jtag_scan_type(cmd->cmd.scan);
  166. usbprog_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  167. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  168. return ERROR_JTAG_QUEUE_FAILED;
  169. if (buffer)
  170. free(buffer);
  171. break;
  172. case JTAG_SLEEP:
  173. #ifdef _DEBUG_JTAG_IO_
  174. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  175. #endif
  176. jtag_sleep(cmd->cmd.sleep->us);
  177. break;
  178. default:
  179. LOG_ERROR("BUG: unknown JTAG command type encountered");
  180. exit(-1);
  181. }
  182. cmd = cmd->next;
  183. }
  184. return ERROR_OK;
  185. }
  186. int usbprog_init(void)
  187. {
  188. usbprog_jtag_handle = usbprog_jtag_open();
  189. tms_chain_index = 0;
  190. if (usbprog_jtag_handle == 0)
  191. {
  192. LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
  193. return ERROR_JTAG_INIT_FAILED;
  194. }
  195. LOG_INFO("USB JTAG Interface ready!");
  196. usbprog_jtag_init(usbprog_jtag_handle);
  197. usbprog_reset(0, 0);
  198. usbprog_write(0, 0, 0);
  199. return ERROR_OK;
  200. }
  201. int usbprog_quit(void)
  202. {
  203. return ERROR_OK;
  204. }
  205. /*************** jtag execute commands **********************/
  206. void usbprog_end_state(enum tap_state state)
  207. {
  208. if (tap_move_map[state] != -1)
  209. end_state = state;
  210. else
  211. {
  212. LOG_ERROR("BUG: %i is not a valid end state", state);
  213. exit(-1);
  214. }
  215. }
  216. void usbprog_state_move(void)
  217. {
  218. int i = 0, tms = 0;
  219. u8 tms_scan = TAP_MOVE(cur_state, end_state);
  220. usbprog_jtag_write_tms(usbprog_jtag_handle, (char)tms_scan);
  221. for (i = 0; i < 7; i++)
  222. {
  223. tms = (tms_scan >> i) & 1;
  224. }
  225. cur_state = end_state;
  226. }
  227. void usbprog_path_move(pathmove_command_t *cmd)
  228. {
  229. int num_states = cmd->num_states;
  230. int state_count;
  231. state_count = 0;
  232. while (num_states)
  233. {
  234. if (tap_transitions[cur_state].low == cmd->path[state_count])
  235. {
  236. /* LOG_INFO("1"); */
  237. usbprog_write(0, 0, 0);
  238. usbprog_write(1, 0, 0);
  239. }
  240. else if (tap_transitions[cur_state].high == cmd->path[state_count])
  241. {
  242. /* LOG_INFO("2"); */
  243. usbprog_write(0, 1, 0);
  244. usbprog_write(1, 1, 0);
  245. }
  246. else
  247. {
  248. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
  249. exit(-1);
  250. }
  251. cur_state = cmd->path[state_count];
  252. state_count++;
  253. num_states--;
  254. }
  255. end_state = cur_state;
  256. }
  257. void usbprog_runtest(int num_cycles)
  258. {
  259. int i;
  260. /* only do a state_move when we're not already in IDLE */
  261. if (cur_state != TAP_IDLE)
  262. {
  263. usbprog_end_state(TAP_IDLE);
  264. usbprog_state_move();
  265. }
  266. /* execute num_cycles */
  267. if (num_cycles > 0)
  268. {
  269. usbprog_jtag_tms_send(usbprog_jtag_handle);
  270. usbprog_write(0, 0, 0);
  271. }
  272. else
  273. {
  274. usbprog_jtag_tms_send(usbprog_jtag_handle);
  275. /* LOG_INFO("NUM CYCLES %i",num_cycles); */
  276. }
  277. for (i = 0; i < num_cycles; i++)
  278. {
  279. usbprog_write(1, 0, 0);
  280. usbprog_write(0, 0, 0);
  281. }
  282. /* finish in end_state */
  283. /*
  284. usbprog_end_state(saved_end_state);
  285. if (cur_state != end_state)
  286. usbprog_state_move();
  287. */
  288. }
  289. void usbprog_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  290. {
  291. enum tap_state saved_end_state = end_state;
  292. if (ir_scan)
  293. usbprog_end_state(TAP_IRSHIFT);
  294. else
  295. usbprog_end_state(TAP_DRSHIFT);
  296. /* usbprog_jtag_tms_send(usbprog_jtag_handle); */
  297. usbprog_state_move();
  298. usbprog_end_state(saved_end_state);
  299. usbprog_jtag_tms_send(usbprog_jtag_handle);
  300. if (type == SCAN_OUT)
  301. {
  302. usbprog_jtag_write_tdi(usbprog_jtag_handle,buffer, scan_size);
  303. }
  304. if (type == SCAN_IN)
  305. {
  306. usbprog_jtag_read_tdo(usbprog_jtag_handle,buffer, scan_size);
  307. }
  308. if (type == SCAN_IO)
  309. {
  310. usbprog_jtag_write_and_read(usbprog_jtag_handle,buffer, scan_size);
  311. }
  312. if (ir_scan)
  313. cur_state = TAP_IRPAUSE;
  314. else
  315. cur_state = TAP_DRPAUSE;
  316. if (cur_state != end_state)
  317. usbprog_state_move();
  318. }
  319. /*************** jtag wrapper functions *********************/
  320. void usbprog_write(int tck, int tms, int tdi)
  321. {
  322. unsigned char output_value=0x00;
  323. if (tms)
  324. output_value |= (1<<TMS_BIT);
  325. if (tdi)
  326. output_value |= (1<<TDI_BIT);
  327. if (tck)
  328. output_value |= (1<<TCK_BIT);
  329. usbprog_jtag_write_slice(usbprog_jtag_handle,output_value);
  330. }
  331. /* (1) assert or (0) deassert reset lines */
  332. void usbprog_reset(int trst, int srst)
  333. {
  334. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  335. if (trst)
  336. usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 0);
  337. else
  338. usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 1);
  339. if (srst)
  340. usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 0);
  341. else
  342. usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 1);
  343. }
  344. /*************** jtag lowlevel functions ********************/
  345. struct usb_bus *busses;
  346. struct usbprog_jtag* usbprog_jtag_open()
  347. {
  348. struct usb_bus *bus;
  349. struct usb_device *dev;
  350. struct usbprog_jtag *tmp;
  351. tmp = (struct usbprog_jtag*)malloc(sizeof(struct usbprog_jtag));
  352. usb_set_debug(10);
  353. usb_init();
  354. usb_find_busses();
  355. usb_find_devices();
  356. busses = usb_get_busses();
  357. /* find usbprog_jtag device in usb bus */
  358. for (bus = busses; bus; bus = bus->next)
  359. {
  360. for (dev = bus->devices; dev; dev = dev->next)
  361. {
  362. /* condition for sucessfully hit (too bad, I only check the vendor id)*/
  363. if (dev->descriptor.idVendor == VID && dev->descriptor.idProduct == PID)
  364. {
  365. tmp->usb_handle = usb_open(dev);
  366. usb_set_configuration(tmp->usb_handle, 1);
  367. usb_claim_interface(tmp->usb_handle, 0);
  368. usb_set_altinterface(tmp->usb_handle, 0);
  369. return tmp;
  370. }
  371. }
  372. }
  373. return 0;
  374. }
  375. void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag)
  376. {
  377. usb_close(usbprog_jtag->usb_handle);
  378. free(usbprog_jtag);
  379. }
  380. unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen)
  381. {
  382. int res = usb_bulk_write(usbprog_jtag->usb_handle, 3, msg,msglen, 100);
  383. if ((msg[0] == 2) || (msg[0] == 1) || (msg[0] == 4) || (msg[0] == 0) || \
  384. (msg[0] == 6) || (msg[0] == 0x0A) || (msg[0] == 9))
  385. return 1;
  386. if (res == msglen)
  387. {
  388. /* LOG_INFO("HALLLLOOO %i",(int)msg[0]); */
  389. res = usb_bulk_read(usbprog_jtag->usb_handle, 0x82, msg, 2, 100);
  390. if (res > 0)
  391. return (unsigned char)msg[1];
  392. else
  393. return -1;
  394. }
  395. else
  396. return -1;
  397. return 0;
  398. }
  399. void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag)
  400. {
  401. usbprog_jtag_set_direction(usbprog_jtag, 0xFE);
  402. }
  403. void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
  404. {
  405. char tmp[64]; /* fastes packet size for usb controller */
  406. int send_bits, bufindex = 0, fillindex = 0, i, loops;
  407. char swap;
  408. /* 61 byte can be transfered (488 bit) */
  409. while (size > 0)
  410. {
  411. if (size > 488)
  412. {
  413. send_bits = 488;
  414. size = size - 488;
  415. loops = 61;
  416. }
  417. else
  418. {
  419. send_bits = size;
  420. loops = size / 8;
  421. loops++;
  422. size = 0;
  423. }
  424. tmp[0] = WRITE_AND_READ;
  425. tmp[1] = (char)(send_bits >> 8); /* high */
  426. tmp[2] = (char)(send_bits); /* low */
  427. i = 0;
  428. for (i = 0; i < loops; i++)
  429. {
  430. tmp[3 + i] = buffer[bufindex];
  431. bufindex++;
  432. }
  433. if (usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000) == 64)
  434. {
  435. /* LOG_INFO("HALLLLOOO2 %i",(int)tmp[0]); */
  436. usleep(1);
  437. int timeout = 0;
  438. while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 1000) < 1)
  439. {
  440. timeout++;
  441. if (timeout > 10)
  442. break;
  443. }
  444. for (i = 0; i < loops; i++)
  445. {
  446. swap = tmp[3 + i];
  447. buffer[fillindex++] = swap;
  448. }
  449. }
  450. }
  451. }
  452. void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
  453. {
  454. char tmp[64]; /* fastes packet size for usb controller */
  455. int send_bits, fillindex = 0, i, loops;
  456. char swap;
  457. /* 61 byte can be transfered (488 bit) */
  458. while (size > 0)
  459. {
  460. if (size > 488)
  461. {
  462. send_bits = 488;
  463. size = size - 488;
  464. loops = 61;
  465. }
  466. else
  467. {
  468. send_bits = size;
  469. loops = size / 8;
  470. loops++;
  471. size = 0;
  472. }
  473. tmp[0] = WRITE_AND_READ;
  474. tmp[1] = (char)(send_bits >> 8); /* high */
  475. tmp[2] = (char)(send_bits); /* low */
  476. usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 3, 1000);
  477. /* LOG_INFO("HALLLLOOO3 %i",(int)tmp[0]); */
  478. int timeout = 0;
  479. usleep(1);
  480. while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 10) < 1)
  481. {
  482. timeout++;
  483. if (timeout > 10)
  484. break;
  485. }
  486. for (i = 0; i < loops; i++)
  487. {
  488. swap = tmp[3 + i];
  489. buffer[fillindex++] = swap;
  490. }
  491. }
  492. }
  493. void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
  494. {
  495. char tmp[64]; /* fastes packet size for usb controller */
  496. int send_bits, bufindex = 0, i, loops;
  497. /* 61 byte can be transfered (488 bit) */
  498. while (size > 0)
  499. {
  500. if (size > 488)
  501. {
  502. send_bits = 488;
  503. size = size - 488;
  504. loops = 61;
  505. }
  506. else
  507. {
  508. send_bits = size;
  509. loops = size/8;
  510. /* if(loops==0) */
  511. loops++;
  512. size = 0;
  513. }
  514. tmp[0] = WRITE_TDI;
  515. tmp[1] = (char)(send_bits >> 8); /* high */
  516. tmp[2] = (char)(send_bits); /* low */
  517. i = 0;
  518. for (i = 0; i < loops; i++)
  519. {
  520. tmp[3 + i] = buffer[bufindex];
  521. bufindex++;
  522. }
  523. usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000);
  524. }
  525. }
  526. void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan)
  527. {
  528. usbprog_jtag_tms_collect(tms_scan);
  529. }
  530. void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction)
  531. {
  532. char tmp[2];
  533. tmp[0] = PORT_DIRECTION;
  534. tmp[1] = (char)direction;
  535. usbprog_jtag_message(usbprog_jtag, tmp, 2);
  536. }
  537. void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value)
  538. {
  539. char tmp[2];
  540. tmp[0] = PORT_SET;
  541. tmp[1] = (char)value;
  542. usbprog_jtag_message(usbprog_jtag, tmp, 2);
  543. }
  544. unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag)
  545. {
  546. char tmp[2];
  547. tmp[0] = PORT_GET;
  548. tmp[1] = 0x00;
  549. return usbprog_jtag_message(usbprog_jtag, tmp, 2);
  550. }
  551. void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value)
  552. {
  553. char tmp[3];
  554. tmp[0] = PORT_SETBIT;
  555. tmp[1] = (char)bit;
  556. if (value == 1)
  557. tmp[2] = 0x01;
  558. else
  559. tmp[2] = 0x00;
  560. usbprog_jtag_message(usbprog_jtag, tmp, 3);
  561. }
  562. int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit)
  563. {
  564. char tmp[2];
  565. tmp[0] = PORT_GETBIT;
  566. tmp[1] = (char)bit;
  567. if (usbprog_jtag_message(usbprog_jtag, tmp, 2) > 0)
  568. return 1;
  569. else
  570. return 0;
  571. }
  572. void usbprog_jtag_tms_collect(char tms_scan)
  573. {
  574. tms_chain[tms_chain_index] = tms_scan;
  575. tms_chain_index++;
  576. }
  577. void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag)
  578. {
  579. int i;
  580. /* LOG_INFO("TMS SEND"); */
  581. if (tms_chain_index > 0)
  582. {
  583. char tmp[tms_chain_index + 2];
  584. tmp[0] = WRITE_TMS_CHAIN;
  585. tmp[1] = (char)(tms_chain_index);
  586. for (i = 0; i < tms_chain_index + 1; i++)
  587. tmp[2 + i] = tms_chain[i];
  588. usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, tms_chain_index + 2, 1000);
  589. tms_chain_index = 0;
  590. }
  591. }