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.
 
 
 
 
 
 

578 lines
15 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.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. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "replacements.h"
  24. #include "jtag.h"
  25. /* system includes */
  26. #ifdef _WIN32
  27. #include "errno.h"
  28. #endif /* _WIN32 */
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #if PARPORT_USE_PPDEV == 1
  32. #include <linux/parport.h>
  33. #include <linux/ppdev.h>
  34. #include <fcntl.h>
  35. #include <sys/ioctl.h>
  36. #include <unistd.h>
  37. #else /* not PARPORT_USE_PPDEV */
  38. #ifndef _WIN32
  39. #include <sys/io.h>
  40. #endif
  41. #endif
  42. #if PARPORT_USE_GIVEIO == 1
  43. #if IS_CYGWIN == 1
  44. #include <windows.h>
  45. #include <errno.h>
  46. #endif
  47. #endif
  48. #include "log.h"
  49. /* configuration */
  50. u16 amt_jtagaccel_port;
  51. /* interface variables
  52. */
  53. static u8 aw_control_rst = 0x00;
  54. static u8 aw_control_fsm = 0x10;
  55. static u8 aw_control_baudrate = 0x20;
  56. static int rtck_enabled = 0;
  57. #if PARPORT_USE_PPDEV == 1
  58. static int device_handle;
  59. int addr_mode = IEEE1284_MODE_EPP | IEEE1284_ADDR ;
  60. int data_mode = IEEE1284_MODE_EPP | IEEE1284_DATA ;
  61. #define AMT_AW(val) do { ioctl(device_handle, PPSETMODE, &addr_mode); write(device_handle, &val, 1); } while (0)
  62. #define AMT_AR(val) do { ioctl(device_handle, PPSETMODE, &addr_mode); read(device_handle, &val, 1); } while (0)
  63. #define AMT_DW(val) do { ioctl(device_handle, PPSETMODE, &data_mode); write(device_handle, &val, 1); } while (0)
  64. #define AMT_DR(val) do { ioctl(device_handle, PPSETMODE, &data_mode); read(device_handle, &val, 1); } while (0)
  65. #else
  66. #define AMT_AW(val) do { outb(val, amt_jtagaccel_port + 3); } while (0)
  67. #define AMT_AR(val) do { val = inb(amt_jtagaccel_port + 3); } while (0)
  68. #define AMT_DW(val) do { outb(val, amt_jtagaccel_port + 4); } while (0)
  69. #define AMT_DR(val) do { val = inb(amt_jtagaccel_port + 4); } while (0)
  70. #endif
  71. int amt_jtagaccel_execute_queue(void);
  72. int amt_jtagaccel_register_commands(struct command_context_s *cmd_ctx);
  73. int amt_jtagaccel_speed(int speed);
  74. int amt_jtagaccel_init(void);
  75. int amt_jtagaccel_quit(void);
  76. int amt_jtagaccel_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  77. int amt_jtagaccel_handle_rtck_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  78. /* tap_move[i][j]: tap movement command to go from state i to state j
  79. * 0: Test-Logic-Reset
  80. * 1: Run-Test/Idle
  81. * 2: Shift-DR
  82. * 3: Pause-DR
  83. * 4: Shift-IR
  84. * 5: Pause-IR
  85. */
  86. u8 amt_jtagaccel_tap_move[6][6][2] =
  87. {
  88. /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */
  89. {{0x1f, 0x00}, {0x0f, 0x00}, {0x8a, 0x04}, {0x0a, 0x00}, {0x06, 0x00}, {0x96, 0x00}}, /* RESET */
  90. {{0x1f, 0x00}, {0x00, 0x00}, {0x85, 0x08}, {0x05, 0x00}, {0x8b, 0x08}, {0x0b, 0x00}}, /* IDLE */
  91. {{0x1f, 0x00}, {0x0d, 0x00}, {0x00, 0x00}, {0x01, 0x00}, {0x8f, 0x09}, {0x8f, 0x01}}, /* DRSHIFT */
  92. {{0x1f, 0x00}, {0x0c, 0x00}, {0x08, 0x00}, {0x00, 0x00}, {0x8f, 0x09}, {0x8f, 0x01}}, /* DRPAUSE */
  93. {{0x1f, 0x00}, {0x0d, 0x00}, {0x07, 0x00}, {0x97, 0x00}, {0x00, 0x00}, {0x01, 0x00}}, /* IRSHIFT */
  94. {{0x1f, 0x00}, {0x0c, 0x00}, {0x07, 0x00}, {0x97, 0x00}, {0x08, 0x00}, {0x00, 0x00}}, /* IRPAUSE */
  95. };
  96. jtag_interface_t amt_jtagaccel_interface =
  97. {
  98. .name = "amt_jtagaccel",
  99. .execute_queue = amt_jtagaccel_execute_queue,
  100. .speed = amt_jtagaccel_speed,
  101. .register_commands = amt_jtagaccel_register_commands,
  102. .init = amt_jtagaccel_init,
  103. .quit = amt_jtagaccel_quit,
  104. };
  105. int amt_jtagaccel_register_commands(struct command_context_s *cmd_ctx)
  106. {
  107. register_command(cmd_ctx, NULL, "parport_port", amt_jtagaccel_handle_parport_port_command,
  108. COMMAND_CONFIG, NULL);
  109. register_command(cmd_ctx, NULL, "rtck", amt_jtagaccel_handle_rtck_command,
  110. COMMAND_CONFIG, NULL);
  111. return ERROR_OK;
  112. }
  113. void amt_jtagaccel_reset(int trst, int srst)
  114. {
  115. if (trst == 1)
  116. aw_control_rst |= 0x4;
  117. else if (trst == 0)
  118. aw_control_rst &= ~0x4;
  119. if (srst == 1)
  120. aw_control_rst |= 0x1;
  121. else if (srst == 0)
  122. aw_control_rst &= ~0x1;
  123. AMT_AW(aw_control_rst);
  124. }
  125. int amt_jtagaccel_speed(int speed)
  126. {
  127. aw_control_baudrate &= 0xf0;
  128. aw_control_baudrate |= speed & 0x0f;
  129. AMT_AW(aw_control_baudrate);
  130. return ERROR_OK;
  131. }
  132. void amt_jtagaccel_end_state(int state)
  133. {
  134. if (tap_is_state_stable(state))
  135. tap_set_end_state(state);
  136. else
  137. {
  138. LOG_ERROR("BUG: %i is not a valid end state", state);
  139. exit(-1);
  140. }
  141. }
  142. void amt_wait_scan_busy(void)
  143. {
  144. int timeout = 4096;
  145. u8 ar_status;
  146. AMT_AR(ar_status);
  147. while (((ar_status) & 0x80) && (timeout-- > 0))
  148. AMT_AR(ar_status);
  149. if (ar_status & 0x80)
  150. {
  151. LOG_ERROR("amt_jtagaccel timed out while waiting for end of scan, rtck was %s, last AR_STATUS: 0x%2.2x", (rtck_enabled) ? "enabled" : "disabled", ar_status);
  152. exit(-1);
  153. }
  154. }
  155. void amt_jtagaccel_state_move(void)
  156. {
  157. u8 aw_scan_tms_5;
  158. u8 tms_scan[2];
  159. tap_state_t cur_state = tap_get_state();
  160. tap_state_t end_state = tap_get_end_state();
  161. tms_scan[0] = amt_jtagaccel_tap_move[tap_move_ndx(cur_state)][tap_move_ndx(end_state)][0];
  162. tms_scan[1] = amt_jtagaccel_tap_move[tap_move_ndx(cur_state)][tap_move_ndx(end_state)][1];
  163. aw_scan_tms_5 = 0x40 | (tms_scan[0] & 0x1f);
  164. AMT_AW(aw_scan_tms_5);
  165. if (jtag_speed > 3 || rtck_enabled)
  166. amt_wait_scan_busy();
  167. if (tms_scan[0] & 0x80)
  168. {
  169. aw_scan_tms_5 = 0x40 | (tms_scan[1] & 0x1f);
  170. AMT_AW(aw_scan_tms_5);
  171. if (jtag_speed > 3 || rtck_enabled)
  172. amt_wait_scan_busy();
  173. }
  174. tap_set_state(end_state);
  175. }
  176. void amt_jtagaccel_runtest(int num_cycles)
  177. {
  178. int i = 0;
  179. u8 aw_scan_tms_5;
  180. u8 aw_scan_tms_1to4;
  181. tap_state_t saved_end_state = tap_get_end_state();
  182. /* only do a state_move when we're not already in IDLE */
  183. if (tap_get_state() != TAP_IDLE)
  184. {
  185. amt_jtagaccel_end_state(TAP_IDLE);
  186. amt_jtagaccel_state_move();
  187. }
  188. while (num_cycles - i >= 5)
  189. {
  190. aw_scan_tms_5 = 0x40;
  191. AMT_AW(aw_scan_tms_5);
  192. i += 5;
  193. }
  194. if (num_cycles - i > 0)
  195. {
  196. aw_scan_tms_1to4 = 0x80 | ((num_cycles - i - 1) & 0x3) << 4;
  197. AMT_AW(aw_scan_tms_1to4);
  198. }
  199. amt_jtagaccel_end_state(saved_end_state);
  200. if (tap_get_state() != tap_get_end_state())
  201. amt_jtagaccel_state_move();
  202. }
  203. void amt_jtagaccel_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  204. {
  205. int bits_left = scan_size;
  206. int bit_count = 0;
  207. tap_state_t saved_end_state = tap_get_end_state();
  208. u8 aw_tdi_option;
  209. u8 dw_tdi_scan;
  210. u8 dr_tdo;
  211. u8 aw_tms_scan;
  212. u8 tms_scan[2];
  213. if (ir_scan)
  214. amt_jtagaccel_end_state(TAP_IRSHIFT);
  215. else
  216. amt_jtagaccel_end_state(TAP_DRSHIFT);
  217. amt_jtagaccel_state_move();
  218. amt_jtagaccel_end_state(saved_end_state);
  219. /* handle unaligned bits at the beginning */
  220. if ((scan_size - 1) % 8)
  221. {
  222. aw_tdi_option = 0x30 | (((scan_size - 1) % 8) - 1);
  223. AMT_AW(aw_tdi_option);
  224. dw_tdi_scan = buf_get_u32(buffer, bit_count, (scan_size - 1) % 8) & 0xff;
  225. AMT_DW(dw_tdi_scan);
  226. if (jtag_speed > 3 || rtck_enabled)
  227. amt_wait_scan_busy();
  228. if ((type == SCAN_IN) || (type == SCAN_IO))
  229. {
  230. AMT_DR(dr_tdo);
  231. dr_tdo = dr_tdo >> (8 - ((scan_size - 1) % 8));
  232. buf_set_u32(buffer, bit_count, (scan_size - 1) % 8, dr_tdo);
  233. }
  234. bit_count += (scan_size - 1) % 8;
  235. bits_left -= (scan_size - 1) % 8;
  236. }
  237. while (bits_left - 1 >= 8)
  238. {
  239. dw_tdi_scan = buf_get_u32(buffer, bit_count, 8) & 0xff;
  240. AMT_DW(dw_tdi_scan);
  241. if (jtag_speed > 3 || rtck_enabled)
  242. amt_wait_scan_busy();
  243. if ((type == SCAN_IN) || (type == SCAN_IO))
  244. {
  245. AMT_DR(dr_tdo);
  246. buf_set_u32(buffer, bit_count, 8, dr_tdo);
  247. }
  248. bit_count += 8;
  249. bits_left -= 8;
  250. }
  251. tms_scan[0] = amt_jtagaccel_tap_move[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())][0];
  252. tms_scan[1] = amt_jtagaccel_tap_move[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())][1];
  253. aw_tms_scan = 0x40 | (tms_scan[0] & 0x1f) | (buf_get_u32(buffer, bit_count, 1) << 5);
  254. AMT_AW(aw_tms_scan);
  255. if (jtag_speed > 3 || rtck_enabled)
  256. amt_wait_scan_busy();
  257. if ((type == SCAN_IN) || (type == SCAN_IO))
  258. {
  259. AMT_DR(dr_tdo);
  260. dr_tdo = dr_tdo >> 7;
  261. buf_set_u32(buffer, bit_count, 1, dr_tdo);
  262. }
  263. if (tms_scan[0] & 0x80)
  264. {
  265. aw_tms_scan = 0x40 | (tms_scan[1] & 0x1f);
  266. AMT_AW(aw_tms_scan);
  267. if (jtag_speed > 3 || rtck_enabled)
  268. amt_wait_scan_busy();
  269. }
  270. tap_set_state(tap_get_end_state());
  271. }
  272. int amt_jtagaccel_execute_queue(void)
  273. {
  274. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  275. int scan_size;
  276. enum scan_type type;
  277. u8 *buffer;
  278. int retval;
  279. /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
  280. * that wasn't handled by a caller-provided error handler
  281. */
  282. retval = ERROR_OK;
  283. while (cmd)
  284. {
  285. switch (cmd->type)
  286. {
  287. case JTAG_END_STATE:
  288. #ifdef _DEBUG_JTAG_IO_
  289. LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
  290. #endif
  291. if (cmd->cmd.end_state->end_state != -1)
  292. amt_jtagaccel_end_state(cmd->cmd.end_state->end_state);
  293. break;
  294. case JTAG_RESET:
  295. #ifdef _DEBUG_JTAG_IO_
  296. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  297. #endif
  298. if (cmd->cmd.reset->trst == 1)
  299. {
  300. tap_set_state(TAP_RESET);
  301. }
  302. amt_jtagaccel_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  303. break;
  304. case JTAG_RUNTEST:
  305. #ifdef _DEBUG_JTAG_IO_
  306. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  307. #endif
  308. if (cmd->cmd.runtest->end_state != -1)
  309. amt_jtagaccel_end_state(cmd->cmd.runtest->end_state);
  310. amt_jtagaccel_runtest(cmd->cmd.runtest->num_cycles);
  311. break;
  312. case JTAG_STATEMOVE:
  313. #ifdef _DEBUG_JTAG_IO_
  314. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  315. #endif
  316. if (cmd->cmd.statemove->end_state != -1)
  317. amt_jtagaccel_end_state(cmd->cmd.statemove->end_state);
  318. amt_jtagaccel_state_move();
  319. break;
  320. case JTAG_SCAN:
  321. #ifdef _DEBUG_JTAG_IO_
  322. LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  323. #endif
  324. if (cmd->cmd.scan->end_state != -1)
  325. amt_jtagaccel_end_state(cmd->cmd.scan->end_state);
  326. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  327. type = jtag_scan_type(cmd->cmd.scan);
  328. amt_jtagaccel_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  329. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  330. retval = ERROR_JTAG_QUEUE_FAILED;
  331. if (buffer)
  332. free(buffer);
  333. break;
  334. case JTAG_SLEEP:
  335. #ifdef _DEBUG_JTAG_IO_
  336. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  337. #endif
  338. jtag_sleep(cmd->cmd.sleep->us);
  339. break;
  340. default:
  341. LOG_ERROR("BUG: unknown JTAG command type encountered");
  342. exit(-1);
  343. }
  344. cmd = cmd->next;
  345. }
  346. return retval;
  347. }
  348. #if PARPORT_USE_GIVEIO == 1
  349. int amt_jtagaccel_get_giveio_access(void)
  350. {
  351. HANDLE h;
  352. OSVERSIONINFO version;
  353. version.dwOSVersionInfoSize = sizeof version;
  354. if (!GetVersionEx( &version )) {
  355. errno = EINVAL;
  356. return -1;
  357. }
  358. if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
  359. return 0;
  360. h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  361. if (h == INVALID_HANDLE_VALUE) {
  362. errno = ENODEV;
  363. return -1;
  364. }
  365. CloseHandle( h );
  366. return 0;
  367. }
  368. #endif
  369. int amt_jtagaccel_init(void)
  370. {
  371. #if PARPORT_USE_PPDEV == 1
  372. char buffer[256];
  373. int i = 0;
  374. u8 control_port;
  375. #else
  376. u8 status_port;
  377. #endif
  378. u8 ar_status;
  379. #if PARPORT_USE_PPDEV == 1
  380. if (device_handle > 0)
  381. {
  382. LOG_ERROR("device is already opened");
  383. return ERROR_JTAG_INIT_FAILED;
  384. }
  385. snprintf(buffer, 256, "/dev/parport%d", amt_jtagaccel_port);
  386. device_handle = open(buffer, O_RDWR);
  387. if (device_handle < 0)
  388. {
  389. LOG_ERROR("cannot open device. check it exists and that user read and write rights are set");
  390. return ERROR_JTAG_INIT_FAILED;
  391. }
  392. i = ioctl(device_handle, PPCLAIM);
  393. if (i < 0)
  394. {
  395. LOG_ERROR("cannot claim device");
  396. return ERROR_JTAG_INIT_FAILED;
  397. }
  398. i = IEEE1284_MODE_EPP;
  399. i = ioctl(device_handle, PPSETMODE, & i);
  400. if (i < 0)
  401. {
  402. LOG_ERROR(" cannot set compatible mode to device");
  403. return ERROR_JTAG_INIT_FAILED;
  404. }
  405. control_port = 0x00;
  406. i = ioctl(device_handle, PPWCONTROL, &control_port);
  407. control_port = 0x04;
  408. i = ioctl(device_handle, PPWCONTROL, &control_port);
  409. #else
  410. if (amt_jtagaccel_port == 0)
  411. {
  412. amt_jtagaccel_port = 0x378;
  413. LOG_WARNING("No parport port specified, using default '0x378' (LPT1)");
  414. }
  415. #if PARPORT_USE_GIVEIO == 1
  416. if (amt_jtagaccel_get_giveio_access() != 0) {
  417. #else /* PARPORT_USE_GIVEIO */
  418. if (ioperm(amt_jtagaccel_port, 5, 1) != 0) {
  419. #endif /* PARPORT_USE_GIVEIO */
  420. LOG_ERROR("missing privileges for direct i/o");
  421. return ERROR_JTAG_INIT_FAILED;
  422. }
  423. /* prepare epp port */
  424. /* clear timeout */
  425. status_port = inb(amt_jtagaccel_port + 1);
  426. outb(status_port | 0x1, amt_jtagaccel_port + 1);
  427. /* reset epp port */
  428. outb(0x00, amt_jtagaccel_port + 2);
  429. outb(0x04, amt_jtagaccel_port + 2);
  430. #endif
  431. if (rtck_enabled)
  432. {
  433. /* set RTCK enable bit */
  434. aw_control_fsm |= 0x02;
  435. }
  436. /* enable JTAG port */
  437. aw_control_fsm |= 0x04;
  438. AMT_AW(aw_control_fsm);
  439. amt_jtagaccel_speed(jtag_speed);
  440. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  441. aw_control_rst &= ~0x8;
  442. else
  443. aw_control_rst |= 0x8;
  444. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  445. aw_control_rst &= ~0x2;
  446. else
  447. aw_control_rst |= 0x2;
  448. amt_jtagaccel_reset(0, 0);
  449. /* read status register */
  450. AMT_AR(ar_status);
  451. LOG_DEBUG("AR_STATUS: 0x%2.2x", ar_status);
  452. return ERROR_OK;
  453. }
  454. int amt_jtagaccel_quit(void)
  455. {
  456. return ERROR_OK;
  457. }
  458. int amt_jtagaccel_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  459. {
  460. if (argc == 0)
  461. return ERROR_OK;
  462. /* only if the port wasn't overwritten by cmdline */
  463. if (amt_jtagaccel_port == 0)
  464. amt_jtagaccel_port = strtoul(args[0], NULL, 0);
  465. return ERROR_OK;
  466. }
  467. int amt_jtagaccel_handle_rtck_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  468. {
  469. if (argc == 0)
  470. {
  471. command_print(cmd_ctx, "amt_jtagaccel RTCK feature %s", (rtck_enabled) ? "enabled" : "disabled");
  472. return ERROR_OK;
  473. }
  474. else
  475. {
  476. if (strcmp(args[0], "enabled") == 0)
  477. {
  478. rtck_enabled = 1;
  479. }
  480. else
  481. {
  482. rtck_enabled = 0;
  483. }
  484. }
  485. return ERROR_OK;
  486. }