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.
 
 
 
 
 
 

583 lines
14 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 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 1
  26. #define _DEBUG_GW16012_IO_
  27. #endif
  28. /* system includes */
  29. /* -ino: 060521-1036 */
  30. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  31. #include <machine/sysarch.h>
  32. #include <machine/cpufunc.h>
  33. #define ioperm(startport,length,enable)\
  34. i386_set_ioperm((startport), (length), (enable))
  35. #else
  36. #endif /* __FreeBSD__, __FreeBSD_kernel__ */
  37. #if PARPORT_USE_PPDEV == 1
  38. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  39. #include <dev/ppbus/ppi.h>
  40. #include <dev/ppbus/ppbconf.h>
  41. #define PPRSTATUS PPIGSTATUS
  42. #define PPWDATA PPISDATA
  43. #else
  44. #include <linux/parport.h>
  45. #include <linux/ppdev.h>
  46. #endif
  47. #include <fcntl.h>
  48. #include <sys/ioctl.h>
  49. #else /* not PARPORT_USE_PPDEV */
  50. #ifndef _WIN32
  51. #include <sys/io.h>
  52. #endif
  53. #endif
  54. #if PARPORT_USE_GIVEIO == 1 && IS_CYGWIN == 1
  55. #include <windows.h>
  56. #endif
  57. /* configuration */
  58. u16 gw16012_port;
  59. /* interface variables
  60. */
  61. static u8 gw16012_msb = 0x0;
  62. static u8 gw16012_control_value = 0x0;
  63. #if PARPORT_USE_PPDEV == 1
  64. static int device_handle;
  65. #endif
  66. static int gw16012_execute_queue(void);
  67. static int gw16012_register_commands(struct command_context_s *cmd_ctx);
  68. static int gw16012_speed(int speed);
  69. static int gw16012_init(void);
  70. static int gw16012_quit(void);
  71. static int gw16012_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  72. jtag_interface_t gw16012_interface =
  73. {
  74. .name = "gw16012",
  75. .execute_queue = gw16012_execute_queue,
  76. .speed = gw16012_speed,
  77. .register_commands = gw16012_register_commands,
  78. .init = gw16012_init,
  79. .quit = gw16012_quit,
  80. };
  81. static int gw16012_register_commands(struct command_context_s *cmd_ctx)
  82. {
  83. register_command(cmd_ctx, NULL, "parport_port", gw16012_handle_parport_port_command,
  84. COMMAND_CONFIG, NULL);
  85. return ERROR_OK;
  86. }
  87. static void gw16012_data(u8 value)
  88. {
  89. value = (value & 0x7f) | gw16012_msb;
  90. gw16012_msb ^= 0x80; /* toggle MSB */
  91. #ifdef _DEBUG_GW16012_IO_
  92. LOG_DEBUG("%2.2x", value);
  93. #endif
  94. #if PARPORT_USE_PPDEV == 1
  95. ioctl(device_handle, PPWDATA, &value);
  96. #else
  97. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  98. outb(gw16012_port, value);
  99. #else
  100. outb(value, gw16012_port);
  101. #endif
  102. #endif
  103. }
  104. static void gw16012_control(u8 value)
  105. {
  106. if (value != gw16012_control_value)
  107. {
  108. gw16012_control_value = value;
  109. #ifdef _DEBUG_GW16012_IO_
  110. LOG_DEBUG("%2.2x", gw16012_control_value);
  111. #endif
  112. #if PARPORT_USE_PPDEV == 1
  113. ioctl(device_handle, PPWCONTROL, &gw16012_control_value);
  114. #else
  115. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  116. outb(gw16012_port + 2, gw16012_control_value);
  117. #else
  118. outb(gw16012_control_value, gw16012_port + 2);
  119. #endif
  120. #endif
  121. }
  122. }
  123. static void gw16012_input(u8 *value)
  124. {
  125. #if PARPORT_USE_PPDEV == 1
  126. ioctl(device_handle, PPRSTATUS, value);
  127. #else
  128. *value = inb(gw16012_port + 1);
  129. #endif
  130. #ifdef _DEBUG_GW16012_IO_
  131. LOG_DEBUG("%2.2x", *value);
  132. #endif
  133. }
  134. /* (1) assert or (0) deassert reset lines */
  135. static void gw16012_reset(int trst, int srst)
  136. {
  137. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  138. if (trst == 0)
  139. gw16012_control(0x0d);
  140. else if (trst == 1)
  141. gw16012_control(0x0c);
  142. if (srst == 0)
  143. gw16012_control(0x0a);
  144. else if (srst == 1)
  145. gw16012_control(0x0b);
  146. }
  147. static int gw16012_speed(int speed)
  148. {
  149. return ERROR_OK;
  150. }
  151. static void gw16012_end_state(tap_state_t state)
  152. {
  153. if (tap_is_state_stable(state))
  154. tap_set_end_state(state);
  155. else
  156. {
  157. LOG_ERROR("BUG: %i is not a valid end state", state);
  158. exit(-1);
  159. }
  160. }
  161. static void gw16012_state_move(void)
  162. {
  163. int i=0, tms=0;
  164. u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  165. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  166. gw16012_control(0x0); /* single-bit mode */
  167. for (i = 0; i < tms_count; i++)
  168. {
  169. tms = (tms_scan >> i) & 1;
  170. gw16012_data(tms << 1); /* output next TMS bit */
  171. }
  172. tap_set_state(tap_get_end_state());
  173. }
  174. static void gw16012_path_move(pathmove_command_t *cmd)
  175. {
  176. int num_states = cmd->num_states;
  177. int state_count;
  178. state_count = 0;
  179. while (num_states)
  180. {
  181. gw16012_control(0x0); /* single-bit mode */
  182. if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
  183. {
  184. gw16012_data(0x0); /* TCK cycle with TMS low */
  185. }
  186. else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
  187. {
  188. gw16012_data(0x2); /* TCK cycle with TMS high */
  189. }
  190. else
  191. {
  192. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
  193. exit(-1);
  194. }
  195. tap_set_state(cmd->path[state_count]);
  196. state_count++;
  197. num_states--;
  198. }
  199. tap_set_end_state(tap_get_state());
  200. }
  201. static void gw16012_runtest(int num_cycles)
  202. {
  203. tap_state_t saved_end_state = tap_get_end_state();
  204. int i;
  205. /* only do a state_move when we're not already in IDLE */
  206. if (tap_get_state() != TAP_IDLE)
  207. {
  208. gw16012_end_state(TAP_IDLE);
  209. gw16012_state_move();
  210. }
  211. for (i = 0; i < num_cycles; i++)
  212. {
  213. gw16012_control(0x0); /* single-bit mode */
  214. gw16012_data(0x0); /* TMS cycle with TMS low */
  215. }
  216. gw16012_end_state(saved_end_state);
  217. if (tap_get_state() != tap_get_end_state())
  218. gw16012_state_move();
  219. }
  220. static void gw16012_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  221. {
  222. int bits_left = scan_size;
  223. int bit_count = 0;
  224. tap_state_t saved_end_state = tap_get_end_state();
  225. u8 scan_out, scan_in;
  226. /* only if we're not already in the correct Shift state */
  227. if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) || (ir_scan && (tap_get_state() == TAP_IRSHIFT))))
  228. {
  229. if (ir_scan)
  230. gw16012_end_state(TAP_IRSHIFT);
  231. else
  232. gw16012_end_state(TAP_DRSHIFT);
  233. gw16012_state_move();
  234. gw16012_end_state(saved_end_state);
  235. }
  236. while (type == SCAN_OUT && ((bits_left - 1) > 7))
  237. {
  238. gw16012_control(0x2); /* seven-bit mode */
  239. scan_out = buf_get_u32(buffer, bit_count, 7);
  240. gw16012_data(scan_out);
  241. bit_count += 7;
  242. bits_left -= 7;
  243. }
  244. gw16012_control(0x0); /* single-bit mode */
  245. while (bits_left-- > 0)
  246. {
  247. u8 tms = 0;
  248. scan_out = buf_get_u32(buffer, bit_count, 1);
  249. if (bits_left == 0) /* last bit */
  250. {
  251. if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
  252. || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
  253. {
  254. tms = 0;
  255. }
  256. else
  257. {
  258. tms = 2;
  259. }
  260. }
  261. gw16012_data(scan_out | tms);
  262. if (type != SCAN_OUT)
  263. {
  264. gw16012_input(&scan_in);
  265. buf_set_u32(buffer, bit_count, 1, ((scan_in & 0x08) >> 3));
  266. }
  267. bit_count++;
  268. }
  269. if (!((ir_scan && (tap_get_end_state() == TAP_IRSHIFT)) ||
  270. (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))))
  271. {
  272. gw16012_data(0x0);
  273. if (ir_scan)
  274. tap_set_state(TAP_IRPAUSE);
  275. else
  276. tap_set_state(TAP_DRPAUSE);
  277. if (tap_get_state() != tap_get_end_state())
  278. gw16012_state_move();
  279. }
  280. }
  281. static int gw16012_execute_queue(void)
  282. {
  283. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  284. int scan_size;
  285. enum scan_type type;
  286. u8 *buffer;
  287. int retval;
  288. /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
  289. * that wasn't handled by a caller-provided error handler
  290. */
  291. retval = ERROR_OK;
  292. while (cmd)
  293. {
  294. switch (cmd->type)
  295. {
  296. case JTAG_RESET:
  297. #ifdef _DEBUG_JTAG_IO_
  298. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  299. #endif
  300. if (cmd->cmd.reset->trst == 1)
  301. {
  302. tap_set_state(TAP_RESET);
  303. }
  304. gw16012_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  305. break;
  306. case JTAG_RUNTEST:
  307. #ifdef _DEBUG_JTAG_IO_
  308. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  309. #endif
  310. gw16012_end_state(cmd->cmd.runtest->end_state);
  311. gw16012_runtest(cmd->cmd.runtest->num_cycles);
  312. break;
  313. case JTAG_STATEMOVE:
  314. #ifdef _DEBUG_JTAG_IO_
  315. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  316. #endif
  317. gw16012_end_state(cmd->cmd.statemove->end_state);
  318. gw16012_state_move();
  319. break;
  320. case JTAG_PATHMOVE:
  321. #ifdef _DEBUG_JTAG_IO_
  322. LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  323. #endif
  324. gw16012_path_move(cmd->cmd.pathmove);
  325. break;
  326. case JTAG_SCAN:
  327. gw16012_end_state(cmd->cmd.scan->end_state);
  328. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  329. type = jtag_scan_type(cmd->cmd.scan);
  330. #ifdef _DEBUG_JTAG_IO_
  331. LOG_DEBUG("%s scan (%i) %i bit end in %i", (cmd->cmd.scan->ir_scan) ? "ir" : "dr",
  332. type, scan_size, cmd->cmd.scan->end_state);
  333. #endif
  334. gw16012_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  335. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  336. retval = ERROR_JTAG_QUEUE_FAILED;
  337. if (buffer)
  338. free(buffer);
  339. break;
  340. case JTAG_SLEEP:
  341. #ifdef _DEBUG_JTAG_IO_
  342. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  343. #endif
  344. jtag_sleep(cmd->cmd.sleep->us);
  345. break;
  346. default:
  347. LOG_ERROR("BUG: unknown JTAG command type encountered");
  348. exit(-1);
  349. }
  350. cmd = cmd->next;
  351. }
  352. return retval;
  353. }
  354. #if PARPORT_USE_GIVEIO == 1
  355. static int gw16012_get_giveio_access(void)
  356. {
  357. HANDLE h;
  358. OSVERSIONINFO version;
  359. version.dwOSVersionInfoSize = sizeof version;
  360. if (!GetVersionEx( &version )) {
  361. errno = EINVAL;
  362. return -1;
  363. }
  364. if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
  365. return 0;
  366. h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  367. if (h == INVALID_HANDLE_VALUE) {
  368. errno = ENODEV;
  369. return -1;
  370. }
  371. CloseHandle( h );
  372. return 0;
  373. }
  374. #endif
  375. #if PARPORT_USE_PPDEV == 1
  376. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  377. #define GW16012_PPDEV_NAME "ppi"
  378. static int gw16012_init_ioctls(void)
  379. {
  380. int temp = 0;
  381. temp = ioctl(device_handle, PPCLAIM);
  382. if (temp < 0)
  383. {
  384. LOG_ERROR("cannot claim device");
  385. return ERROR_JTAG_INIT_FAILED;
  386. }
  387. temp = PARPORT_MODE_COMPAT;
  388. temp = ioctl(device_handle, PPSETMODE, &temp);
  389. if (temp < 0)
  390. {
  391. LOG_ERROR(" cannot set compatible mode to device");
  392. return ERROR_JTAG_INIT_FAILED;
  393. }
  394. temp = IEEE1284_MODE_COMPAT;
  395. temp = ioctl(device_handle, PPNEGOT, &temp);
  396. if (temp < 0)
  397. {
  398. LOG_ERROR("cannot set compatible 1284 mode to device");
  399. return ERROR_JTAG_INIT_FAILED;
  400. }
  401. return ERROR_OK;
  402. }
  403. #else
  404. #define GW16012_PPDEV_NAME "parport"
  405. static int gw16012_init_ioctls(void)
  406. {
  407. return ERROR_OK;
  408. }
  409. #endif // defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  410. static int gw16012_init_device(void)
  411. {
  412. const char *device_name = GW16012_PPDEV_NAME;
  413. char buffer[256];
  414. if (device_handle > 0)
  415. {
  416. LOG_ERROR("device is already opened");
  417. return ERROR_JTAG_INIT_FAILED;
  418. }
  419. snprintf(buffer, 256, "/dev/%s%d", device_name, gw16012_port);
  420. LOG_DEBUG("opening %s...", buffer);
  421. device_handle = open(buffer, O_WRONLY);
  422. if (device_handle<0)
  423. {
  424. LOG_ERROR("cannot open device. check it exists and that user read and write rights are set");
  425. return ERROR_JTAG_INIT_FAILED;
  426. }
  427. LOG_DEBUG("...open");
  428. if (gw16012_init_ioctls() != ERROR_OK)
  429. return ERROR_JTAG_INIT_FAILED;
  430. return ERROR_OK;
  431. }
  432. #else // PARPORT_USE_PPDEV
  433. static int gw16012_init_device(void)
  434. {
  435. if (gw16012_port == 0)
  436. {
  437. gw16012_port = 0x378;
  438. LOG_WARNING("No gw16012 port specified, using default '0x378' (LPT1)");
  439. }
  440. LOG_DEBUG("requesting privileges for parallel port 0x%lx...", (long unsigned)(gw16012_port) );
  441. #if PARPORT_USE_GIVEIO == 1
  442. if (gw16012_get_giveio_access() != 0)
  443. #else /* PARPORT_USE_GIVEIO */
  444. if (ioperm(gw16012_port, 3, 1) != 0)
  445. #endif /* PARPORT_USE_GIVEIO */
  446. {
  447. LOG_ERROR("missing privileges for direct i/o");
  448. return ERROR_JTAG_INIT_FAILED;
  449. }
  450. LOG_DEBUG("...privileges granted");
  451. /* make sure parallel port is in right mode (clear tristate and interrupt */
  452. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  453. outb(gw16012_port + 2, 0x0);
  454. #else
  455. outb(0x0, gw16012_port + 2);
  456. #endif
  457. return ERROR_OK;
  458. }
  459. #endif // PARPORT_USE_PPDEV
  460. static int gw16012_init(void)
  461. {
  462. u8 status_port;
  463. if (gw16012_init_device() != ERROR_OK)
  464. return ERROR_JTAG_INIT_FAILED;
  465. gw16012_input(&status_port);
  466. gw16012_msb = (status_port & 0x80) ^ 0x80;
  467. gw16012_speed(jtag_speed);
  468. gw16012_reset(0, 0);
  469. return ERROR_OK;
  470. }
  471. static int gw16012_quit(void)
  472. {
  473. return ERROR_OK;
  474. }
  475. static int gw16012_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  476. {
  477. if (argc == 0)
  478. return ERROR_OK;
  479. /* only if the port wasn't overwritten by cmdline */
  480. if (gw16012_port == 0)
  481. gw16012_port = strtoul(args[0], NULL, 0);
  482. return ERROR_OK;
  483. }