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.
 
 
 
 
 
 

484 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 "interface.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. uint8_t TDO_MASK; /* status port bit containing current TDO value */
  60. uint8_t TRST_MASK; /* data port bit for TRST */
  61. uint8_t TMS_MASK; /* data port bit for TMS */
  62. uint8_t TCK_MASK; /* data port bit for TCK */
  63. uint8_t TDI_MASK; /* data port bit for TDI */
  64. uint8_t SRST_MASK; /* data port bit for SRST */
  65. uint8_t OUTPUT_INVERT; /* data port bits that should be inverted */
  66. uint8_t INPUT_INVERT; /* status port that should be inverted */
  67. uint8_t PORT_INIT; /* initialize data port with this value */
  68. uint8_t PORT_EXIT; /* de-initialize data port with this value */
  69. uint8_t 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 uint16_t parport_port;
  99. static int parport_exit = 0;
  100. /* interface variables
  101. */
  102. static cable_t* cable;
  103. static uint8_t 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. uint8_t 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_get_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. int err = errno;
  289. LOG_ERROR("cannot open device. check it exists and that user read and write rights are set. errno=%d", err);
  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 == 1)
  365. {
  366. /* only if the port wasn't overwritten by cmdline */
  367. if (parport_port == 0)
  368. {
  369. int retval = parse_u16(args[0], &parport_port);
  370. if (ERROR_OK != retval)
  371. return retval;
  372. }
  373. else
  374. {
  375. LOG_ERROR("The parport port was already configured!");
  376. return ERROR_FAIL;
  377. }
  378. }
  379. command_print(cmd_ctx, "parport port = %u", parport_port);
  380. return ERROR_OK;
  381. }
  382. static int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  383. {
  384. if (argc == 0)
  385. return ERROR_OK;
  386. /* only if the cable name wasn't overwritten by cmdline */
  387. if (parport_cable == 0)
  388. {
  389. parport_cable = malloc(strlen(args[0]) + sizeof(char));
  390. strcpy(parport_cable, args[0]);
  391. }
  392. return ERROR_OK;
  393. }
  394. static int parport_handle_write_on_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  395. {
  396. if (argc != 1)
  397. {
  398. command_print(cmd_ctx, "usage: parport_write_on_exit <on | off>");
  399. return ERROR_OK;
  400. }
  401. if (strcmp(args[0], "on") == 0)
  402. parport_exit = 1;
  403. else if (strcmp(args[0], "off") == 0)
  404. parport_exit = 0;
  405. return ERROR_OK;
  406. }