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.
 
 
 
 
 
 

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