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.
 
 
 
 
 
 

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