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.
 
 
 
 
 
 

628 lines
17 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 <jtag/interface.h>
  35. #include <jtag/commands.h>
  36. #include "usb_common.h"
  37. #define VID 0x1781
  38. #define PID 0x0c63
  39. /* Pins at usbprog */
  40. #define TDO_BIT 0
  41. #define TDI_BIT 3
  42. #define TCK_BIT 2
  43. #define TMS_BIT 1
  44. static void usbprog_end_state(tap_state_t state);
  45. static void usbprog_state_move(void);
  46. static void usbprog_path_move(struct pathmove_command *cmd);
  47. static void usbprog_runtest(int num_cycles);
  48. static void usbprog_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size);
  49. #define UNKNOWN_COMMAND 0x00
  50. #define PORT_DIRECTION 0x01
  51. #define PORT_SET 0x02
  52. #define PORT_GET 0x03
  53. #define PORT_SETBIT 0x04
  54. #define PORT_GETBIT 0x05
  55. #define WRITE_TDI 0x06
  56. #define READ_TDO 0x07
  57. #define WRITE_AND_READ 0x08
  58. #define WRITE_TMS 0x09
  59. #define WRITE_TMS_CHAIN 0x0A
  60. struct usbprog_jtag {
  61. struct usb_dev_handle *usb_handle;
  62. };
  63. static struct usbprog_jtag *usbprog_jtag_handle;
  64. static struct usbprog_jtag *usbprog_jtag_open(void);
  65. /* static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag); */
  66. static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag);
  67. static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen);
  68. static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char *buffer, int size);
  69. static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char *buffer, int size);
  70. static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char *buffer, int size);
  71. static void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan);
  72. static char tms_chain[64];
  73. static int tms_chain_index;
  74. static void usbprog_jtag_tms_collect(char tms_scan);
  75. static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag);
  76. static void usbprog_write(int tck, int tms, int tdi);
  77. static void usbprog_reset(int trst, int srst);
  78. static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction);
  79. static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag, unsigned char value);
  80. /* static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag); */
  81. static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag, int bit, int value);
  82. /* static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit); */
  83. static int usbprog_speed(int speed)
  84. {
  85. return ERROR_OK;
  86. }
  87. static int usbprog_execute_queue(void)
  88. {
  89. struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
  90. int scan_size;
  91. enum scan_type type;
  92. uint8_t *buffer;
  93. while (cmd) {
  94. switch (cmd->type) {
  95. case JTAG_RESET:
  96. #ifdef _DEBUG_JTAG_IO_
  97. LOG_DEBUG("reset trst: %i srst %i",
  98. cmd->cmd.reset->trst,
  99. cmd->cmd.reset->srst);
  100. #endif
  101. if (cmd->cmd.reset->trst == 1)
  102. tap_set_state(TAP_RESET);
  103. usbprog_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  104. break;
  105. case JTAG_RUNTEST:
  106. #ifdef _DEBUG_JTAG_IO_
  107. LOG_DEBUG("runtest %i cycles, end in %i",
  108. cmd->cmd.runtest->num_cycles,
  109. cmd->cmd.runtest->end_state);
  110. #endif
  111. usbprog_end_state(cmd->cmd.runtest->end_state);
  112. usbprog_runtest(cmd->cmd.runtest->num_cycles);
  113. break;
  114. case JTAG_TLR_RESET:
  115. #ifdef _DEBUG_JTAG_IO_
  116. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  117. #endif
  118. usbprog_end_state(cmd->cmd.statemove->end_state);
  119. usbprog_state_move();
  120. break;
  121. case JTAG_PATHMOVE:
  122. #ifdef _DEBUG_JTAG_IO_
  123. LOG_DEBUG("pathmove: %i states, end in %i",
  124. cmd->cmd.pathmove->num_states,
  125. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  126. #endif
  127. usbprog_path_move(cmd->cmd.pathmove);
  128. break;
  129. case JTAG_SCAN:
  130. #ifdef _DEBUG_JTAG_IO_
  131. LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  132. #endif
  133. usbprog_end_state(cmd->cmd.scan->end_state);
  134. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  135. type = jtag_scan_type(cmd->cmd.scan);
  136. usbprog_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  137. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  138. return ERROR_JTAG_QUEUE_FAILED;
  139. if (buffer)
  140. free(buffer);
  141. break;
  142. case JTAG_SLEEP:
  143. #ifdef _DEBUG_JTAG_IO_
  144. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  145. #endif
  146. jtag_sleep(cmd->cmd.sleep->us);
  147. break;
  148. default:
  149. LOG_ERROR("BUG: unknown JTAG command type encountered");
  150. exit(-1);
  151. }
  152. cmd = cmd->next;
  153. }
  154. return ERROR_OK;
  155. }
  156. static int usbprog_init(void)
  157. {
  158. usbprog_jtag_handle = usbprog_jtag_open();
  159. tms_chain_index = 0;
  160. if (usbprog_jtag_handle == 0) {
  161. LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
  162. return ERROR_JTAG_INIT_FAILED;
  163. }
  164. LOG_INFO("USB JTAG Interface ready!");
  165. usbprog_jtag_init(usbprog_jtag_handle);
  166. usbprog_reset(0, 0);
  167. usbprog_write(0, 0, 0);
  168. return ERROR_OK;
  169. }
  170. static int usbprog_quit(void)
  171. {
  172. return ERROR_OK;
  173. }
  174. /*************** jtag execute commands **********************/
  175. static void usbprog_end_state(tap_state_t state)
  176. {
  177. if (tap_is_state_stable(state))
  178. tap_set_end_state(state);
  179. else {
  180. LOG_ERROR("BUG: %i is not a valid end state", state);
  181. exit(-1);
  182. }
  183. }
  184. static void usbprog_state_move(void)
  185. {
  186. uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  187. usbprog_jtag_write_tms(usbprog_jtag_handle, (char)tms_scan);
  188. tap_set_state(tap_get_end_state());
  189. }
  190. static void usbprog_path_move(struct pathmove_command *cmd)
  191. {
  192. int num_states = cmd->num_states;
  193. int state_count;
  194. /* There may be queued transitions, and before following a specified
  195. path, we must flush those queued transitions */
  196. usbprog_jtag_tms_send(usbprog_jtag_handle);
  197. state_count = 0;
  198. while (num_states) {
  199. if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count]) {
  200. /* LOG_INFO("1"); */
  201. usbprog_write(0, 0, 0);
  202. usbprog_write(1, 0, 0);
  203. } else if (tap_state_transition(tap_get_state(),
  204. true) == cmd->path[state_count]) {
  205. /* LOG_INFO("2"); */
  206. usbprog_write(0, 1, 0);
  207. usbprog_write(1, 1, 0);
  208. } else {
  209. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  210. tap_state_name(tap_get_state()),
  211. tap_state_name(cmd->path[state_count]));
  212. exit(-1);
  213. }
  214. tap_set_state(cmd->path[state_count]);
  215. state_count++;
  216. num_states--;
  217. }
  218. tap_set_end_state(tap_get_state());
  219. }
  220. static void usbprog_runtest(int num_cycles)
  221. {
  222. int i;
  223. /* only do a state_move when we're not already in IDLE */
  224. if (tap_get_state() != TAP_IDLE) {
  225. usbprog_end_state(TAP_IDLE);
  226. usbprog_state_move();
  227. }
  228. /* execute num_cycles */
  229. if (num_cycles > 0) {
  230. usbprog_jtag_tms_send(usbprog_jtag_handle);
  231. usbprog_write(0, 0, 0);
  232. } else {
  233. usbprog_jtag_tms_send(usbprog_jtag_handle);
  234. /* LOG_INFO("NUM CYCLES %i",num_cycles); */
  235. }
  236. for (i = 0; i < num_cycles; i++) {
  237. usbprog_write(1, 0, 0);
  238. usbprog_write(0, 0, 0);
  239. }
  240. #ifdef _DEBUG_JTAG_IO_
  241. LOG_DEBUG("runtest: cur_state %s end_state %s", tap_state_name(
  242. tap_get_state()), tap_state_name(tap_get_end_state()));
  243. #endif
  244. /* finish in end_state */
  245. /*
  246. usbprog_end_state(saved_end_state);
  247. if (tap_get_state() != tap_get_end_state())
  248. usbprog_state_move();
  249. */
  250. }
  251. static void usbprog_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
  252. {
  253. tap_state_t saved_end_state = tap_get_end_state();
  254. if (ir_scan)
  255. usbprog_end_state(TAP_IRSHIFT);
  256. else
  257. usbprog_end_state(TAP_DRSHIFT);
  258. /* Only move if we're not already there */
  259. if (tap_get_state() != tap_get_end_state())
  260. usbprog_state_move();
  261. usbprog_end_state(saved_end_state);
  262. usbprog_jtag_tms_send(usbprog_jtag_handle);
  263. void (*f)(struct usbprog_jtag *usbprog_jtag, char *buffer_local, int size);
  264. switch (type) {
  265. case SCAN_OUT:
  266. f = &usbprog_jtag_write_tdi;
  267. break;
  268. case SCAN_IN:
  269. f = &usbprog_jtag_read_tdo;
  270. break;
  271. case SCAN_IO:
  272. f = &usbprog_jtag_write_and_read;
  273. break;
  274. default:
  275. LOG_ERROR("unknown scan type: %i", type);
  276. exit(-1);
  277. }
  278. f(usbprog_jtag_handle, (char *)buffer, scan_size);
  279. /* The adapter does the transition to PAUSE internally */
  280. if (ir_scan)
  281. tap_set_state(TAP_IRPAUSE);
  282. else
  283. tap_set_state(TAP_DRPAUSE);
  284. if (tap_get_state() != tap_get_end_state())
  285. usbprog_state_move();
  286. }
  287. /*************** jtag wrapper functions *********************/
  288. static void usbprog_write(int tck, int tms, int tdi)
  289. {
  290. unsigned char output_value = 0x00;
  291. if (tms)
  292. output_value |= (1 << TMS_BIT);
  293. if (tdi)
  294. output_value |= (1 << TDI_BIT);
  295. if (tck)
  296. output_value |= (1 << TCK_BIT);
  297. usbprog_jtag_write_slice(usbprog_jtag_handle, output_value);
  298. }
  299. /* (1) assert or (0) deassert reset lines */
  300. static void usbprog_reset(int trst, int srst)
  301. {
  302. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  303. if (trst)
  304. usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 0);
  305. else
  306. usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 1);
  307. if (srst)
  308. usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 0);
  309. else
  310. usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 1);
  311. }
  312. /*************** jtag lowlevel functions ********************/
  313. struct usb_bus *busses;
  314. struct usbprog_jtag *usbprog_jtag_open(void)
  315. {
  316. usb_set_debug(10);
  317. usb_init();
  318. const uint16_t vids[] = { VID, 0 };
  319. const uint16_t pids[] = { PID, 0 };
  320. struct usb_dev_handle *dev;
  321. if (jtag_usb_open(vids, pids, &dev) != ERROR_OK)
  322. return NULL;
  323. struct usbprog_jtag *tmp = malloc(sizeof(struct usbprog_jtag));
  324. tmp->usb_handle = dev;
  325. usb_set_configuration(dev, 1);
  326. usb_claim_interface(dev, 0);
  327. usb_set_altinterface(dev, 0);
  328. return tmp;
  329. }
  330. #if 0
  331. static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag)
  332. {
  333. usb_close(usbprog_jtag->usb_handle);
  334. free(usbprog_jtag);
  335. }
  336. #endif
  337. static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen)
  338. {
  339. int res = usb_bulk_write(usbprog_jtag->usb_handle, 3, msg, msglen, 100);
  340. if ((msg[0] == 2) || (msg[0] == 1) || (msg[0] == 4) || (msg[0] == 0) || \
  341. (msg[0] == 6) || (msg[0] == 0x0A) || (msg[0] == 9))
  342. return 1;
  343. if (res == msglen) {
  344. /* LOG_INFO("HALLLLOOO %i",(int)msg[0]); */
  345. res = usb_bulk_read(usbprog_jtag->usb_handle, 0x82, msg, 2, 100);
  346. if (res > 0)
  347. return (unsigned char)msg[1];
  348. else
  349. return -1;
  350. } else
  351. return -1;
  352. return 0;
  353. }
  354. static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag)
  355. {
  356. usbprog_jtag_set_direction(usbprog_jtag, 0xFE);
  357. }
  358. static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char *buffer, int size)
  359. {
  360. char tmp[64]; /* fastes packet size for usb controller */
  361. int send_bits, bufindex = 0, fillindex = 0, i, loops;
  362. char swap;
  363. /* 61 byte can be transfered (488 bit) */
  364. while (size > 0) {
  365. if (size > 488) {
  366. send_bits = 488;
  367. size = size - 488;
  368. loops = 61;
  369. } else {
  370. send_bits = size;
  371. loops = size / 8;
  372. loops++;
  373. size = 0;
  374. }
  375. tmp[0] = WRITE_AND_READ;
  376. tmp[1] = (char)(send_bits >> 8); /* high */
  377. tmp[2] = (char)(send_bits); /* low */
  378. for (i = 0; i < loops; i++) {
  379. tmp[3 + i] = buffer[bufindex];
  380. bufindex++;
  381. }
  382. if (usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000) == 64) {
  383. /* LOG_INFO("HALLLLOOO2 %i",(int)tmp[0]); */
  384. usleep(1);
  385. int timeout = 0;
  386. while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 1000) < 1) {
  387. timeout++;
  388. if (timeout > 10)
  389. break;
  390. }
  391. for (i = 0; i < loops; i++) {
  392. swap = tmp[3 + i];
  393. buffer[fillindex++] = swap;
  394. }
  395. }
  396. }
  397. }
  398. static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char *buffer, int size)
  399. {
  400. char tmp[64]; /* fastes packet size for usb controller */
  401. int send_bits, fillindex = 0, i, loops;
  402. char swap;
  403. /* 61 byte can be transfered (488 bit) */
  404. while (size > 0) {
  405. if (size > 488) {
  406. send_bits = 488;
  407. size = size - 488;
  408. loops = 61;
  409. } else {
  410. send_bits = size;
  411. loops = size / 8;
  412. loops++;
  413. size = 0;
  414. }
  415. tmp[0] = WRITE_AND_READ;
  416. tmp[1] = (char)(send_bits >> 8); /* high */
  417. tmp[2] = (char)(send_bits); /* low */
  418. usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 3, 1000);
  419. /* LOG_INFO("HALLLLOOO3 %i",(int)tmp[0]); */
  420. int timeout = 0;
  421. usleep(1);
  422. while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 10) < 1) {
  423. timeout++;
  424. if (timeout > 10)
  425. break;
  426. }
  427. for (i = 0; i < loops; i++) {
  428. swap = tmp[3 + i];
  429. buffer[fillindex++] = swap;
  430. }
  431. }
  432. }
  433. static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char *buffer, int size)
  434. {
  435. char tmp[64]; /* fastes packet size for usb controller */
  436. int send_bits, bufindex = 0, i, loops;
  437. /* 61 byte can be transfered (488 bit) */
  438. while (size > 0) {
  439. if (size > 488) {
  440. send_bits = 488;
  441. size = size - 488;
  442. loops = 61;
  443. } else {
  444. send_bits = size;
  445. loops = size/8;
  446. /* if (loops == 0) */
  447. loops++;
  448. size = 0;
  449. }
  450. tmp[0] = WRITE_TDI;
  451. tmp[1] = (char)(send_bits >> 8); /* high */
  452. tmp[2] = (char)(send_bits); /* low */
  453. for (i = 0; i < loops; i++) {
  454. tmp[3 + i] = buffer[bufindex];
  455. bufindex++;
  456. }
  457. usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000);
  458. }
  459. }
  460. static void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan)
  461. {
  462. usbprog_jtag_tms_collect(tms_scan);
  463. }
  464. static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction)
  465. {
  466. char tmp[2];
  467. tmp[0] = PORT_DIRECTION;
  468. tmp[1] = (char)direction;
  469. usbprog_jtag_message(usbprog_jtag, tmp, 2);
  470. }
  471. static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag, unsigned char value)
  472. {
  473. char tmp[2];
  474. tmp[0] = PORT_SET;
  475. tmp[1] = (char)value;
  476. usbprog_jtag_message(usbprog_jtag, tmp, 2);
  477. }
  478. #if 0
  479. static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag)
  480. {
  481. char tmp[2];
  482. tmp[0] = PORT_GET;
  483. tmp[1] = 0x00;
  484. return usbprog_jtag_message(usbprog_jtag, tmp, 2);
  485. }
  486. #endif
  487. static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag, int bit, int value)
  488. {
  489. char tmp[3];
  490. tmp[0] = PORT_SETBIT;
  491. tmp[1] = (char)bit;
  492. if (value == 1)
  493. tmp[2] = 0x01;
  494. else
  495. tmp[2] = 0x00;
  496. usbprog_jtag_message(usbprog_jtag, tmp, 3);
  497. }
  498. #if 0
  499. static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit)
  500. {
  501. char tmp[2];
  502. tmp[0] = PORT_GETBIT;
  503. tmp[1] = (char)bit;
  504. if (usbprog_jtag_message(usbprog_jtag, tmp, 2) > 0)
  505. return 1;
  506. else
  507. return 0;
  508. }
  509. #endif
  510. static void usbprog_jtag_tms_collect(char tms_scan)
  511. {
  512. tms_chain[tms_chain_index] = tms_scan;
  513. tms_chain_index++;
  514. }
  515. static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag)
  516. {
  517. int i;
  518. /* LOG_INFO("TMS SEND"); */
  519. if (tms_chain_index > 0) {
  520. char tmp[tms_chain_index + 2];
  521. tmp[0] = WRITE_TMS_CHAIN;
  522. tmp[1] = (char)(tms_chain_index);
  523. for (i = 0; i < tms_chain_index + 1; i++)
  524. tmp[2 + i] = tms_chain[i];
  525. usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, tms_chain_index + 2, 1000);
  526. tms_chain_index = 0;
  527. }
  528. }
  529. struct jtag_interface usbprog_interface = {
  530. .name = "usbprog",
  531. .execute_queue = usbprog_execute_queue,
  532. .speed = usbprog_speed,
  533. .init = usbprog_init,
  534. .quit = usbprog_quit
  535. };