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.
 
 
 
 
 
 

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