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.
 
 
 
 
 
 

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