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.
 
 
 
 
 
 

472 lines
13 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2008 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "jtag.h"
  27. #include "bitbang.h"
  28. /* -ino: 060521-1036 */
  29. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  30. #include <machine/sysarch.h>
  31. #include <machine/cpufunc.h>
  32. #define ioperm(startport,length,enable)\
  33. i386_set_ioperm((startport), (length), (enable))
  34. #endif /* __FreeBSD__ */
  35. #if PARPORT_USE_PPDEV == 1
  36. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  37. #include <dev/ppbus/ppi.h>
  38. #include <dev/ppbus/ppbconf.h>
  39. #define PPRSTATUS PPIGSTATUS
  40. #define PPWDATA PPISDATA
  41. #else
  42. #include <linux/parport.h>
  43. #include <linux/ppdev.h>
  44. #endif
  45. #include <sys/ioctl.h>
  46. #else /* not PARPORT_USE_PPDEV */
  47. #ifndef _WIN32
  48. #include <sys/io.h>
  49. #endif
  50. #endif
  51. #if PARPORT_USE_GIVEIO == 1 && IS_CYGWIN == 1
  52. #include <windows.h>
  53. #endif
  54. /* parallel port cable description
  55. */
  56. typedef struct cable_s
  57. {
  58. char* name;
  59. u8 TDO_MASK; /* status port bit containing current TDO value */
  60. u8 TRST_MASK; /* data port bit for TRST */
  61. u8 TMS_MASK; /* data port bit for TMS */
  62. u8 TCK_MASK; /* data port bit for TCK */
  63. u8 TDI_MASK; /* data port bit for TDI */
  64. u8 SRST_MASK; /* data port bit for SRST */
  65. u8 OUTPUT_INVERT; /* data port bits that should be inverted */
  66. u8 INPUT_INVERT; /* status port that should be inverted */
  67. u8 PORT_INIT; /* initialize data port with this value */
  68. u8 PORT_EXIT; /* de-initialize data port with this value */
  69. u8 LED_MASK; /* data port bit for LED */
  70. } cable_t;
  71. static cable_t cables[] =
  72. {
  73. /* name tdo trst tms tck tdi srst o_inv i_inv init exit led */
  74. { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 },
  75. { "wiggler2", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 },
  76. { "wiggler_ntrst_inverted",
  77. 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80, 0x80, 0x00 },
  78. { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 },
  79. { "arm-jtag", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x01, 0x80, 0x80, 0x80, 0x00 },
  80. { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
  81. { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 },
  82. { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
  83. { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 },
  84. { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 },
  85. /* Altium Universal JTAG cable. Set the cable to Xilinx Mode and wire to target as follows:
  86. HARD TCK - Target TCK
  87. HARD TMS - Target TMS
  88. HARD TDI - Target TDI
  89. HARD TDO - Target TDO
  90. SOFT TCK - Target TRST
  91. SOFT TDI - Target SRST
  92. */
  93. { "altium", 0x10, 0x20, 0x04, 0x02, 0x01, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08 },
  94. { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  95. };
  96. /* configuration */
  97. static char* parport_cable = NULL;
  98. static u16 parport_port;
  99. static int parport_exit = 0;
  100. /* interface variables
  101. */
  102. static cable_t* cable;
  103. static u8 dataport_value = 0x0;
  104. #if PARPORT_USE_PPDEV == 1
  105. static int device_handle;
  106. #else
  107. static unsigned long dataport;
  108. static unsigned long statusport;
  109. #endif
  110. /* low level command set
  111. */
  112. static int parport_read(void);
  113. static void parport_write(int tck, int tms, int tdi);
  114. static void parport_reset(int trst, int srst);
  115. static void parport_led(int on);
  116. static int parport_speed(int speed);
  117. static int parport_register_commands(struct command_context_s *cmd_ctx);
  118. static int parport_init(void);
  119. static int parport_quit(void);
  120. /* interface commands */
  121. static int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  122. static int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  123. static int parport_handle_write_on_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  124. jtag_interface_t parport_interface =
  125. {
  126. .name = "parport",
  127. .execute_queue = bitbang_execute_queue,
  128. .speed = parport_speed,
  129. .register_commands = parport_register_commands,
  130. .init = parport_init,
  131. .quit = parport_quit,
  132. };
  133. static bitbang_interface_t parport_bitbang =
  134. {
  135. .read = parport_read,
  136. .write = parport_write,
  137. .reset = parport_reset,
  138. .blink = parport_led
  139. };
  140. static int parport_read(void)
  141. {
  142. int data = 0;
  143. #if PARPORT_USE_PPDEV == 1
  144. ioctl(device_handle, PPRSTATUS, & data);
  145. #else
  146. data = inb(statusport);
  147. #endif
  148. if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
  149. return 1;
  150. else
  151. return 0;
  152. }
  153. static __inline__ void parport_write_data(void)
  154. {
  155. u8 output;
  156. output = dataport_value ^ cable->OUTPUT_INVERT;
  157. #if PARPORT_USE_PPDEV == 1
  158. ioctl(device_handle, PPWDATA, &output);
  159. #else
  160. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  161. outb(dataport, output);
  162. #else
  163. outb(output, dataport);
  164. #endif
  165. #endif
  166. }
  167. static void parport_write(int tck, int tms, int tdi)
  168. {
  169. int i = jtag_speed + 1;
  170. if (tck)
  171. dataport_value |= cable->TCK_MASK;
  172. else
  173. dataport_value &= ~cable->TCK_MASK;
  174. if (tms)
  175. dataport_value |= cable->TMS_MASK;
  176. else
  177. dataport_value &= ~cable->TMS_MASK;
  178. if (tdi)
  179. dataport_value |= cable->TDI_MASK;
  180. else
  181. dataport_value &= ~cable->TDI_MASK;
  182. while (i-- > 0)
  183. parport_write_data();
  184. }
  185. /* (1) assert or (0) deassert reset lines */
  186. static void parport_reset(int trst, int srst)
  187. {
  188. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  189. if (trst == 0)
  190. dataport_value |= cable->TRST_MASK;
  191. else if (trst == 1)
  192. dataport_value &= ~cable->TRST_MASK;
  193. if (srst == 0)
  194. dataport_value |= cable->SRST_MASK;
  195. else if (srst == 1)
  196. dataport_value &= ~cable->SRST_MASK;
  197. parport_write_data();
  198. }
  199. /* turn LED on parport adapter on (1) or off (0) */
  200. static void parport_led(int on)
  201. {
  202. if (on)
  203. dataport_value |= cable->LED_MASK;
  204. else
  205. dataport_value &= ~cable->LED_MASK;
  206. parport_write_data();
  207. }
  208. static int parport_speed(int speed)
  209. {
  210. return ERROR_OK;
  211. }
  212. static int parport_register_commands(struct command_context_s *cmd_ctx)
  213. {
  214. register_command(cmd_ctx, NULL, "parport_port", parport_handle_parport_port_command,
  215. COMMAND_CONFIG, "either the address of the I/O port or the number of the ‘/dev/parport’ device");
  216. register_command(cmd_ctx, NULL, "parport_cable", parport_handle_parport_cable_command,
  217. COMMAND_CONFIG, "the layout of the parallel port cable used to connect to the target");
  218. register_command(cmd_ctx, NULL, "parport_write_on_exit", parport_handle_write_on_exit_command,
  219. COMMAND_CONFIG, "configure the parallel driver to write a known value to the parallel interface");
  220. return ERROR_OK;
  221. }
  222. #if PARPORT_USE_GIVEIO == 1
  223. static int parport_get_giveio_access(void)
  224. {
  225. HANDLE h;
  226. OSVERSIONINFO version;
  227. version.dwOSVersionInfoSize = sizeof version;
  228. if (!GetVersionEx( &version )) {
  229. errno = EINVAL;
  230. return -1;
  231. }
  232. if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
  233. return 0;
  234. h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  235. if (h == INVALID_HANDLE_VALUE) {
  236. errno = ENODEV;
  237. return -1;
  238. }
  239. CloseHandle( h );
  240. return 0;
  241. }
  242. #endif
  243. static int parport_init(void)
  244. {
  245. cable_t *cur_cable;
  246. #if PARPORT_USE_PPDEV == 1
  247. char buffer[256];
  248. int i = 0;
  249. #endif
  250. cur_cable = cables;
  251. if ((parport_cable == NULL) || (parport_cable[0] == 0))
  252. {
  253. parport_cable = "wiggler";
  254. LOG_WARNING("No parport cable specified, using default 'wiggler'");
  255. }
  256. while (cur_cable->name)
  257. {
  258. if (strcmp(cur_cable->name, parport_cable) == 0)
  259. {
  260. cable = cur_cable;
  261. break;
  262. }
  263. cur_cable++;
  264. }
  265. if (!cable)
  266. {
  267. LOG_ERROR("No matching cable found for %s", parport_cable);
  268. return ERROR_JTAG_INIT_FAILED;
  269. }
  270. dataport_value = cable->PORT_INIT;
  271. #if PARPORT_USE_PPDEV == 1
  272. if (device_handle > 0)
  273. {
  274. LOG_ERROR("device is already opened");
  275. return ERROR_JTAG_INIT_FAILED;
  276. }
  277. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  278. LOG_DEBUG("opening /dev/ppi%d...", parport_port);
  279. snprintf(buffer, 256, "/dev/ppi%d", parport_port);
  280. device_handle = open(buffer, O_WRONLY);
  281. #else /* not __FreeBSD__, __FreeBSD_kernel__ */
  282. LOG_DEBUG("opening /dev/parport%d...", parport_port);
  283. snprintf(buffer, 256, "/dev/parport%d", parport_port);
  284. device_handle = open(buffer, O_WRONLY);
  285. #endif /* __FreeBSD__, __FreeBSD_kernel__ */
  286. if (device_handle < 0)
  287. {
  288. LOG_ERROR("cannot open device. check it exists and that user read and write rights are set");
  289. return ERROR_JTAG_INIT_FAILED;
  290. }
  291. LOG_DEBUG("...open");
  292. #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
  293. i=ioctl(device_handle, PPCLAIM);
  294. if (i<0)
  295. {
  296. LOG_ERROR("cannot claim device");
  297. return ERROR_JTAG_INIT_FAILED;
  298. }
  299. i = PARPORT_MODE_COMPAT;
  300. i= ioctl(device_handle, PPSETMODE, & i);
  301. if (i<0)
  302. {
  303. LOG_ERROR(" cannot set compatible mode to device");
  304. return ERROR_JTAG_INIT_FAILED;
  305. }
  306. i = IEEE1284_MODE_COMPAT;
  307. i = ioctl(device_handle, PPNEGOT, & i);
  308. if (i<0)
  309. {
  310. LOG_ERROR("cannot set compatible 1284 mode to device");
  311. return ERROR_JTAG_INIT_FAILED;
  312. }
  313. #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
  314. #else /* not PARPORT_USE_PPDEV */
  315. if (parport_port == 0)
  316. {
  317. parport_port = 0x378;
  318. LOG_WARNING("No parport port specified, using default '0x378' (LPT1)");
  319. }
  320. dataport = parport_port;
  321. statusport = parport_port + 1;
  322. LOG_DEBUG("requesting privileges for parallel port 0x%lx...", dataport);
  323. #if PARPORT_USE_GIVEIO == 1
  324. if (parport_get_giveio_access() != 0)
  325. #else /* PARPORT_USE_GIVEIO */
  326. if (ioperm(dataport, 3, 1) != 0)
  327. #endif /* PARPORT_USE_GIVEIO */
  328. {
  329. LOG_ERROR("missing privileges for direct i/o");
  330. return ERROR_JTAG_INIT_FAILED;
  331. }
  332. LOG_DEBUG("...privileges granted");
  333. /* make sure parallel port is in right mode (clear tristate and interrupt */
  334. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  335. outb(parport_port + 2, 0x0);
  336. #else
  337. outb(0x0, parport_port + 2);
  338. #endif
  339. #endif /* PARPORT_USE_PPDEV */
  340. parport_reset(0, 0);
  341. parport_write(0, 0, 0);
  342. parport_led(1);
  343. bitbang_interface = &parport_bitbang;
  344. return ERROR_OK;
  345. }
  346. static int parport_quit(void)
  347. {
  348. parport_led(0);
  349. if (parport_exit)
  350. {
  351. dataport_value = cable->PORT_EXIT;
  352. parport_write_data();
  353. }
  354. if (parport_cable)
  355. {
  356. free(parport_cable);
  357. parport_cable = NULL;
  358. }
  359. return ERROR_OK;
  360. }
  361. static int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  362. {
  363. if (argc == 0)
  364. return ERROR_OK;
  365. /* only if the port wasn't overwritten by cmdline */
  366. if (parport_port == 0)
  367. parport_port = strtoul(args[0], NULL, 0);
  368. return ERROR_OK;
  369. }
  370. static int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  371. {
  372. if (argc == 0)
  373. return ERROR_OK;
  374. /* only if the cable name wasn't overwritten by cmdline */
  375. if (parport_cable == 0)
  376. {
  377. parport_cable = malloc(strlen(args[0]) + sizeof(char));
  378. strcpy(parport_cable, args[0]);
  379. }
  380. return ERROR_OK;
  381. }
  382. static int parport_handle_write_on_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  383. {
  384. if (argc != 1)
  385. {
  386. command_print(cmd_ctx, "usage: parport_write_on_exit <on|off>");
  387. return ERROR_OK;
  388. }
  389. if (strcmp(args[0], "on") == 0)
  390. parport_exit = 1;
  391. else if (strcmp(args[0], "off") == 0)
  392. parport_exit = 0;
  393. return ERROR_OK;
  394. }