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.
 
 
 
 
 
 

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