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.
 
 
 
 
 
 

574 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_move_map[state] != -1)
  135. 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. tms_scan[0] = amt_jtagaccel_tap_move[tap_move_map[cur_state]][tap_move_map[end_state]][0];
  160. tms_scan[1] = amt_jtagaccel_tap_move[tap_move_map[cur_state]][tap_move_map[end_state]][1];
  161. aw_scan_tms_5 = 0x40 | (tms_scan[0] & 0x1f);
  162. AMT_AW(aw_scan_tms_5);
  163. if (jtag_speed > 3 || rtck_enabled)
  164. amt_wait_scan_busy();
  165. if (tms_scan[0] & 0x80)
  166. {
  167. aw_scan_tms_5 = 0x40 | (tms_scan[1] & 0x1f);
  168. AMT_AW(aw_scan_tms_5);
  169. if (jtag_speed > 3 || rtck_enabled)
  170. amt_wait_scan_busy();
  171. }
  172. cur_state = end_state;
  173. }
  174. void amt_jtagaccel_runtest(int num_cycles)
  175. {
  176. int i = 0;
  177. u8 aw_scan_tms_5;
  178. u8 aw_scan_tms_1to4;
  179. enum tap_state saved_end_state = end_state;
  180. /* only do a state_move when we're not already in IDLE */
  181. if (cur_state != TAP_IDLE)
  182. {
  183. amt_jtagaccel_end_state(TAP_IDLE);
  184. amt_jtagaccel_state_move();
  185. }
  186. while (num_cycles - i >= 5)
  187. {
  188. aw_scan_tms_5 = 0x40;
  189. AMT_AW(aw_scan_tms_5);
  190. i += 5;
  191. }
  192. if (num_cycles - i > 0)
  193. {
  194. aw_scan_tms_1to4 = 0x80 | ((num_cycles - i - 1) & 0x3) << 4;
  195. AMT_AW(aw_scan_tms_1to4);
  196. }
  197. amt_jtagaccel_end_state(saved_end_state);
  198. if (cur_state != end_state)
  199. amt_jtagaccel_state_move();
  200. }
  201. void amt_jtagaccel_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  202. {
  203. int bits_left = scan_size;
  204. int bit_count = 0;
  205. enum tap_state saved_end_state = end_state;
  206. u8 aw_tdi_option;
  207. u8 dw_tdi_scan;
  208. u8 dr_tdo;
  209. u8 aw_tms_scan;
  210. u8 tms_scan[2];
  211. if (ir_scan)
  212. amt_jtagaccel_end_state(TAP_IRSHIFT);
  213. else
  214. amt_jtagaccel_end_state(TAP_DRSHIFT);
  215. amt_jtagaccel_state_move();
  216. amt_jtagaccel_end_state(saved_end_state);
  217. /* handle unaligned bits at the beginning */
  218. if ((scan_size - 1) % 8)
  219. {
  220. aw_tdi_option = 0x30 | (((scan_size - 1) % 8) - 1);
  221. AMT_AW(aw_tdi_option);
  222. dw_tdi_scan = buf_get_u32(buffer, bit_count, (scan_size - 1) % 8) & 0xff;
  223. AMT_DW(dw_tdi_scan);
  224. if (jtag_speed > 3 || rtck_enabled)
  225. amt_wait_scan_busy();
  226. if ((type == SCAN_IN) || (type == SCAN_IO))
  227. {
  228. AMT_DR(dr_tdo);
  229. dr_tdo = dr_tdo >> (8 - ((scan_size - 1) % 8));
  230. buf_set_u32(buffer, bit_count, (scan_size - 1) % 8, dr_tdo);
  231. }
  232. bit_count += (scan_size - 1) % 8;
  233. bits_left -= (scan_size - 1) % 8;
  234. }
  235. while (bits_left - 1 >= 8)
  236. {
  237. dw_tdi_scan = buf_get_u32(buffer, bit_count, 8) & 0xff;
  238. AMT_DW(dw_tdi_scan);
  239. if (jtag_speed > 3 || rtck_enabled)
  240. amt_wait_scan_busy();
  241. if ((type == SCAN_IN) || (type == SCAN_IO))
  242. {
  243. AMT_DR(dr_tdo);
  244. buf_set_u32(buffer, bit_count, 8, dr_tdo);
  245. }
  246. bit_count += 8;
  247. bits_left -= 8;
  248. }
  249. tms_scan[0] = amt_jtagaccel_tap_move[tap_move_map[cur_state]][tap_move_map[end_state]][0];
  250. tms_scan[1] = amt_jtagaccel_tap_move[tap_move_map[cur_state]][tap_move_map[end_state]][1];
  251. aw_tms_scan = 0x40 | (tms_scan[0] & 0x1f) | (buf_get_u32(buffer, bit_count, 1) << 5);
  252. AMT_AW(aw_tms_scan);
  253. if (jtag_speed > 3 || rtck_enabled)
  254. amt_wait_scan_busy();
  255. if ((type == SCAN_IN) || (type == SCAN_IO))
  256. {
  257. AMT_DR(dr_tdo);
  258. dr_tdo = dr_tdo >> 7;
  259. buf_set_u32(buffer, bit_count, 1, dr_tdo);
  260. }
  261. if (tms_scan[0] & 0x80)
  262. {
  263. aw_tms_scan = 0x40 | (tms_scan[1] & 0x1f);
  264. AMT_AW(aw_tms_scan);
  265. if (jtag_speed > 3 || rtck_enabled)
  266. amt_wait_scan_busy();
  267. }
  268. cur_state = end_state;
  269. }
  270. int amt_jtagaccel_execute_queue(void)
  271. {
  272. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  273. int scan_size;
  274. enum scan_type type;
  275. u8 *buffer;
  276. int retval;
  277. /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
  278. * that wasn't handled by a caller-provided error handler
  279. */
  280. retval = ERROR_OK;
  281. while (cmd)
  282. {
  283. switch (cmd->type)
  284. {
  285. case JTAG_END_STATE:
  286. #ifdef _DEBUG_JTAG_IO_
  287. LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
  288. #endif
  289. if (cmd->cmd.end_state->end_state != -1)
  290. amt_jtagaccel_end_state(cmd->cmd.end_state->end_state);
  291. break;
  292. case JTAG_RESET:
  293. #ifdef _DEBUG_JTAG_IO_
  294. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  295. #endif
  296. if (cmd->cmd.reset->trst == 1)
  297. {
  298. cur_state = TAP_RESET;
  299. }
  300. amt_jtagaccel_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  301. break;
  302. case JTAG_RUNTEST:
  303. #ifdef _DEBUG_JTAG_IO_
  304. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  305. #endif
  306. if (cmd->cmd.runtest->end_state != -1)
  307. amt_jtagaccel_end_state(cmd->cmd.runtest->end_state);
  308. amt_jtagaccel_runtest(cmd->cmd.runtest->num_cycles);
  309. break;
  310. case JTAG_STATEMOVE:
  311. #ifdef _DEBUG_JTAG_IO_
  312. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  313. #endif
  314. if (cmd->cmd.statemove->end_state != -1)
  315. amt_jtagaccel_end_state(cmd->cmd.statemove->end_state);
  316. amt_jtagaccel_state_move();
  317. break;
  318. case JTAG_SCAN:
  319. #ifdef _DEBUG_JTAG_IO_
  320. LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  321. #endif
  322. if (cmd->cmd.scan->end_state != -1)
  323. amt_jtagaccel_end_state(cmd->cmd.scan->end_state);
  324. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  325. type = jtag_scan_type(cmd->cmd.scan);
  326. amt_jtagaccel_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  327. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  328. retval = ERROR_JTAG_QUEUE_FAILED;
  329. if (buffer)
  330. free(buffer);
  331. break;
  332. case JTAG_SLEEP:
  333. #ifdef _DEBUG_JTAG_IO_
  334. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  335. #endif
  336. jtag_sleep(cmd->cmd.sleep->us);
  337. break;
  338. default:
  339. LOG_ERROR("BUG: unknown JTAG command type encountered");
  340. exit(-1);
  341. }
  342. cmd = cmd->next;
  343. }
  344. return retval;
  345. }
  346. #if PARPORT_USE_GIVEIO == 1
  347. int amt_jtagaccel_get_giveio_access(void)
  348. {
  349. HANDLE h;
  350. OSVERSIONINFO version;
  351. version.dwOSVersionInfoSize = sizeof version;
  352. if (!GetVersionEx( &version )) {
  353. errno = EINVAL;
  354. return -1;
  355. }
  356. if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
  357. return 0;
  358. h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  359. if (h == INVALID_HANDLE_VALUE) {
  360. errno = ENODEV;
  361. return -1;
  362. }
  363. CloseHandle( h );
  364. return 0;
  365. }
  366. #endif
  367. int amt_jtagaccel_init(void)
  368. {
  369. #if PARPORT_USE_PPDEV == 1
  370. char buffer[256];
  371. int i = 0;
  372. u8 control_port;
  373. #else
  374. u8 status_port;
  375. #endif
  376. u8 ar_status;
  377. #if PARPORT_USE_PPDEV == 1
  378. if (device_handle > 0)
  379. {
  380. LOG_ERROR("device is already opened");
  381. return ERROR_JTAG_INIT_FAILED;
  382. }
  383. snprintf(buffer, 256, "/dev/parport%d", amt_jtagaccel_port);
  384. device_handle = open(buffer, O_RDWR);
  385. if (device_handle < 0)
  386. {
  387. LOG_ERROR("cannot open device. check it exists and that user read and write rights are set");
  388. return ERROR_JTAG_INIT_FAILED;
  389. }
  390. i = ioctl(device_handle, PPCLAIM);
  391. if (i < 0)
  392. {
  393. LOG_ERROR("cannot claim device");
  394. return ERROR_JTAG_INIT_FAILED;
  395. }
  396. i = IEEE1284_MODE_EPP;
  397. i = ioctl(device_handle, PPSETMODE, & i);
  398. if (i < 0)
  399. {
  400. LOG_ERROR(" cannot set compatible mode to device");
  401. return ERROR_JTAG_INIT_FAILED;
  402. }
  403. control_port = 0x00;
  404. i = ioctl(device_handle, PPWCONTROL, &control_port);
  405. control_port = 0x04;
  406. i = ioctl(device_handle, PPWCONTROL, &control_port);
  407. #else
  408. if (amt_jtagaccel_port == 0)
  409. {
  410. amt_jtagaccel_port = 0x378;
  411. LOG_WARNING("No parport port specified, using default '0x378' (LPT1)");
  412. }
  413. #if PARPORT_USE_GIVEIO == 1
  414. if (amt_jtagaccel_get_giveio_access() != 0) {
  415. #else /* PARPORT_USE_GIVEIO */
  416. if (ioperm(amt_jtagaccel_port, 5, 1) != 0) {
  417. #endif /* PARPORT_USE_GIVEIO */
  418. LOG_ERROR("missing privileges for direct i/o");
  419. return ERROR_JTAG_INIT_FAILED;
  420. }
  421. /* prepare epp port */
  422. /* clear timeout */
  423. status_port = inb(amt_jtagaccel_port + 1);
  424. outb(status_port | 0x1, amt_jtagaccel_port + 1);
  425. /* reset epp port */
  426. outb(0x00, amt_jtagaccel_port + 2);
  427. outb(0x04, amt_jtagaccel_port + 2);
  428. #endif
  429. if (rtck_enabled)
  430. {
  431. /* set RTCK enable bit */
  432. aw_control_fsm |= 0x02;
  433. }
  434. /* enable JTAG port */
  435. aw_control_fsm |= 0x04;
  436. AMT_AW(aw_control_fsm);
  437. amt_jtagaccel_speed(jtag_speed);
  438. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  439. aw_control_rst &= ~0x8;
  440. else
  441. aw_control_rst |= 0x8;
  442. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  443. aw_control_rst &= ~0x2;
  444. else
  445. aw_control_rst |= 0x2;
  446. amt_jtagaccel_reset(0, 0);
  447. /* read status register */
  448. AMT_AR(ar_status);
  449. LOG_DEBUG("AR_STATUS: 0x%2.2x", ar_status);
  450. return ERROR_OK;
  451. }
  452. int amt_jtagaccel_quit(void)
  453. {
  454. return ERROR_OK;
  455. }
  456. int amt_jtagaccel_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  457. {
  458. if (argc == 0)
  459. return ERROR_OK;
  460. /* only if the port wasn't overwritten by cmdline */
  461. if (amt_jtagaccel_port == 0)
  462. amt_jtagaccel_port = strtoul(args[0], NULL, 0);
  463. return ERROR_OK;
  464. }
  465. int amt_jtagaccel_handle_rtck_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  466. {
  467. if (argc == 0)
  468. {
  469. command_print(cmd_ctx, "amt_jtagaccel RTCK feature %s", (rtck_enabled) ? "enabled" : "disabled");
  470. return ERROR_OK;
  471. }
  472. else
  473. {
  474. if (strcmp(args[0], "enabled") == 0)
  475. {
  476. rtck_enabled = 1;
  477. }
  478. else
  479. {
  480. rtck_enabled = 0;
  481. }
  482. }
  483. return ERROR_OK;
  484. }