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.
 
 
 
 
 
 

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