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.
 
 
 
 
 
 

426 lines
9.9 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. { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80 },
  85. { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00 },
  86. { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10 },
  87. { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00 },
  88. { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  89. };
  90. /* configuration */
  91. char* parport_cable;
  92. unsigned long parport_port;
  93. /* interface variables
  94. */
  95. static cable_t* cable;
  96. static u8 dataport_value = 0x0;
  97. #if PARPORT_USE_PPDEV == 1
  98. static int device_handle;
  99. #else
  100. static unsigned long dataport;
  101. static unsigned long statusport;
  102. #endif
  103. /* low level command set
  104. */
  105. int parport_read(void);
  106. void parport_write(int tck, int tms, int tdi);
  107. void parport_reset(int trst, int srst);
  108. int parport_speed(int speed);
  109. int parport_register_commands(struct command_context_s *cmd_ctx);
  110. int parport_init(void);
  111. int parport_quit(void);
  112. /* interface commands */
  113. int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  114. int parport_handle_parport_cable_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  115. jtag_interface_t parport_interface =
  116. {
  117. .name = "parport",
  118. .execute_queue = bitbang_execute_queue,
  119. .support_statemove = 0,
  120. .speed = parport_speed,
  121. .register_commands = parport_register_commands,
  122. .init = parport_init,
  123. .quit = parport_quit,
  124. };
  125. bitbang_interface_t parport_bitbang =
  126. {
  127. .read = parport_read,
  128. .write = parport_write,
  129. .reset = parport_reset
  130. };
  131. int parport_read(void)
  132. {
  133. int data = 0;
  134. #if PARPORT_USE_PPDEV == 1
  135. ioctl(device_handle, PPRSTATUS, & data);
  136. #else
  137. data = inb(statusport);
  138. #endif
  139. if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
  140. return 1;
  141. else
  142. return 0;
  143. }
  144. void parport_write(int tck, int tms, int tdi)
  145. {
  146. u8 output;
  147. int i = jtag_speed + 1;
  148. if (tck)
  149. dataport_value |= cable->TCK_MASK;
  150. else
  151. dataport_value &= ~cable->TCK_MASK;
  152. if (tms)
  153. dataport_value |= cable->TMS_MASK;
  154. else
  155. dataport_value &= ~cable->TMS_MASK;
  156. if (tdi)
  157. dataport_value |= cable->TDI_MASK;
  158. else
  159. dataport_value &= ~cable->TDI_MASK;
  160. output = dataport_value ^ cable->OUTPUT_INVERT;
  161. while (i-- > 0)
  162. #if PARPORT_USE_PPDEV == 1
  163. ioctl(device_handle, PPWDATA, &output);
  164. #else
  165. #ifdef __FreeBSD__
  166. outb(dataport, output);
  167. #else
  168. outb(output, dataport);
  169. #endif
  170. #endif
  171. }
  172. /* (1) assert or (0) deassert reset lines */
  173. void parport_reset(int trst, int srst)
  174. {
  175. u8 output;
  176. DEBUG("trst: %i, srst: %i", trst, srst);
  177. if (trst == 0)
  178. dataport_value |= cable->TRST_MASK;
  179. else if (trst == 1)
  180. dataport_value &= ~cable->TRST_MASK;
  181. if (srst == 0)
  182. dataport_value |= cable->SRST_MASK;
  183. else if (srst == 1)
  184. dataport_value &= ~cable->SRST_MASK;
  185. output = dataport_value ^ cable->OUTPUT_INVERT;
  186. #if PARPORT_USE_PPDEV == 1
  187. ioctl(device_handle, PPWDATA, &output);
  188. #else
  189. #ifdef __FreeBSD__
  190. outb(dataport, output);
  191. #else
  192. outb(output, dataport);
  193. #endif
  194. #endif
  195. }
  196. int parport_speed(int speed)
  197. {
  198. jtag_speed = speed;
  199. return ERROR_OK;
  200. }
  201. int parport_register_commands(struct command_context_s *cmd_ctx)
  202. {
  203. register_command(cmd_ctx, NULL, "parport_port", parport_handle_parport_port_command,
  204. COMMAND_CONFIG, NULL);
  205. register_command(cmd_ctx, NULL, "parport_cable", parport_handle_parport_cable_command,
  206. COMMAND_CONFIG, NULL);
  207. return ERROR_OK;
  208. }
  209. #if PARPORT_USE_GIVEIO == 1
  210. int parport_get_giveio_access()
  211. {
  212. HANDLE h;
  213. OSVERSIONINFO version;
  214. version.dwOSVersionInfoSize = sizeof version;
  215. if (!GetVersionEx( &version )) {
  216. errno = EINVAL;
  217. return -1;
  218. }
  219. if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
  220. return 0;
  221. h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  222. if (h == INVALID_HANDLE_VALUE) {
  223. errno = ENODEV;
  224. return -1;
  225. }
  226. CloseHandle( h );
  227. return 0;
  228. }
  229. #endif
  230. int parport_init(void)
  231. {
  232. cable_t *cur_cable;
  233. #if PARPORT_USE_PPDEV == 1
  234. char buffer[256];
  235. int i = 0;
  236. #endif
  237. cur_cable = cables;
  238. if ((parport_cable == NULL) || (parport_cable[0] == 0))
  239. {
  240. parport_cable = "wiggler";
  241. WARNING("No parport cable specified, using default 'wiggler'");
  242. }
  243. while (cur_cable->name)
  244. {
  245. if (strcmp(cur_cable->name, parport_cable) == 0)
  246. {
  247. cable = cur_cable;
  248. break;
  249. }
  250. cur_cable++;
  251. }
  252. if (!cable)
  253. {
  254. ERROR("No matching cable found for %s", parport_cable);
  255. return ERROR_JTAG_INIT_FAILED;
  256. }
  257. dataport_value = cable->PORT_INIT;
  258. #if PARPORT_USE_PPDEV == 1
  259. if (device_handle>0)
  260. {
  261. ERROR("device is already opened");
  262. return ERROR_JTAG_INIT_FAILED;
  263. }
  264. #ifdef __FreeBSD__
  265. DEBUG("opening /dev/ppi%d...", parport_port);
  266. snprintf(buffer, 256, "/dev/ppi%d", parport_port);
  267. device_handle = open(buffer, O_WRONLY);
  268. #else
  269. DEBUG("opening /dev/parport%d...", parport_port);
  270. snprintf(buffer, 256, "/dev/parport%d", parport_port);
  271. device_handle = open(buffer, O_WRONLY);
  272. #endif
  273. if (device_handle<0)
  274. {
  275. ERROR("cannot open device. check it exists and that user read and write rights are set");
  276. return ERROR_JTAG_INIT_FAILED;
  277. }
  278. DEBUG("...open");
  279. #ifndef __FreeBSD__
  280. i=ioctl(device_handle, PPCLAIM);
  281. if (i<0)
  282. {
  283. ERROR("cannot claim device");
  284. return ERROR_JTAG_INIT_FAILED;
  285. }
  286. i = PARPORT_MODE_COMPAT;
  287. i= ioctl(device_handle, PPSETMODE, & i);
  288. if (i<0)
  289. {
  290. ERROR(" cannot set compatible mode to device");
  291. return ERROR_JTAG_INIT_FAILED;
  292. }
  293. i = IEEE1284_MODE_COMPAT;
  294. i = ioctl(device_handle, PPNEGOT, & i);
  295. if (i<0)
  296. {
  297. ERROR("cannot set compatible 1284 mode to device");
  298. return ERROR_JTAG_INIT_FAILED;
  299. }
  300. #endif
  301. #else
  302. if (parport_port == 0)
  303. {
  304. parport_port = 0x378;
  305. WARNING("No parport port specified, using default '0x378' (LPT1)");
  306. }
  307. dataport = parport_port;
  308. statusport = parport_port + 1;
  309. DEBUG("requesting privileges for parallel port 0x%x...", dataport);
  310. #if PARPORT_USE_GIVEIO == 1
  311. if (parport_get_giveio_access() != 0)
  312. #else /* PARPORT_USE_GIVEIO */
  313. if (ioperm(dataport, 3, 1) != 0)
  314. #endif /* PARPORT_USE_GIVEIO */
  315. {
  316. ERROR("missing privileges for direct i/o");
  317. return ERROR_JTAG_INIT_FAILED;
  318. }
  319. DEBUG("...privileges granted");
  320. #endif /* PARPORT_USE_PPDEV */
  321. parport_reset(0, 0);
  322. parport_write(0, 0, 0);
  323. bitbang_interface = &parport_bitbang;
  324. return ERROR_OK;
  325. }
  326. int parport_quit(void)
  327. {
  328. return ERROR_OK;
  329. }
  330. int parport_handle_parport_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  331. {
  332. if (argc == 0)
  333. return ERROR_OK;
  334. /* only if the port wasn't overwritten by cmdline */
  335. if (parport_port == 0)
  336. parport_port = strtoul(args[0], NULL, 0);
  337. return ERROR_OK;
  338. }
  339. int parport_handle_parport_cable_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 cable name wasn't overwritten by cmdline */
  344. if (parport_cable == 0)
  345. {
  346. parport_cable = malloc(strlen(args[0]) + sizeof(char));
  347. strcpy(parport_cable, args[0]);
  348. }
  349. return ERROR_OK;
  350. }