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.
 
 
 
 
 
 

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