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.
 
 
 
 
 
 

439 lines
10 KiB

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