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.
 
 
 
 
 
 

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