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.
 
 
 
 
 
 

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