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.
 
 
 
 
 
 

515 lines
14 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 <jtag/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. struct cable {
  57. char *name;
  58. uint8_t TDO_MASK; /* status port bit containing current TDO value */
  59. uint8_t TRST_MASK; /* data port bit for TRST */
  60. uint8_t TMS_MASK; /* data port bit for TMS */
  61. uint8_t TCK_MASK; /* data port bit for TCK */
  62. uint8_t TDI_MASK; /* data port bit for TDI */
  63. uint8_t SRST_MASK; /* data port bit for SRST */
  64. uint8_t OUTPUT_INVERT; /* data port bits that should be inverted */
  65. uint8_t INPUT_INVERT; /* status port that should be inverted */
  66. uint8_t PORT_INIT; /* initialize data port with this value */
  67. uint8_t PORT_EXIT; /* de-initialize data port with this value */
  68. uint8_t LED_MASK; /* data port bit for LED */
  69. };
  70. static struct cable cables[] = {
  71. /* name tdo trst tms tck tdi srst o_inv i_inv init exit led */
  72. { "wiggler", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x80, 0x00 },
  73. { "wiggler2", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x01, 0x80, 0x80, 0x00, 0x20 },
  74. { "wiggler_ntrst_inverted", 0x80, 0x10, 0x02, 0x04, 0x08, 0x01, 0x11, 0x80, 0x80, 0x80, 0x00 },
  75. { "old_amt_wiggler", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x11, 0x80, 0x80, 0x80, 0x00 },
  76. { "arm-jtag", 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x01, 0x80, 0x80, 0x80, 0x00 },
  77. { "chameleon", 0x80, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
  78. { "dlc5", 0x10, 0x00, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00 },
  79. { "triton", 0x80, 0x08, 0x04, 0x01, 0x02, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 },
  80. { "lattice", 0x40, 0x10, 0x04, 0x02, 0x01, 0x08, 0x00, 0x00, 0x18, 0x18, 0x00 },
  81. { "flashlink", 0x20, 0x10, 0x02, 0x01, 0x04, 0x20, 0x30, 0x20, 0x00, 0x00, 0x00 },
  82. /* Altium Universal JTAG cable. Set the cable to Xilinx Mode and wire to target as follows:
  83. HARD TCK - Target TCK
  84. HARD TMS - Target TMS
  85. HARD TDI - Target TDI
  86. HARD TDO - Target TDO
  87. SOFT TCK - Target TRST
  88. SOFT TDI - Target SRST
  89. */
  90. { "altium", 0x10, 0x20, 0x04, 0x02, 0x01, 0x80, 0x00, 0x00, 0x10, 0x00, 0x08 },
  91. { NULL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  92. };
  93. /* configuration */
  94. static char *parport_cable;
  95. static uint16_t parport_port;
  96. static bool parport_exit;
  97. static uint32_t parport_toggling_time_ns = 1000;
  98. static int wait_states;
  99. /* interface variables
  100. */
  101. static struct cable *cable;
  102. static uint8_t dataport_value;
  103. #if PARPORT_USE_PPDEV == 1
  104. static int device_handle;
  105. #else
  106. static unsigned long dataport;
  107. static unsigned long statusport;
  108. #endif
  109. static int parport_read(void)
  110. {
  111. int data = 0;
  112. #if PARPORT_USE_PPDEV == 1
  113. ioctl(device_handle, PPRSTATUS, &data);
  114. #else
  115. data = inb(statusport);
  116. #endif
  117. if ((data ^ cable->INPUT_INVERT) & cable->TDO_MASK)
  118. return 1;
  119. else
  120. return 0;
  121. }
  122. static inline void parport_write_data(void)
  123. {
  124. uint8_t output;
  125. output = dataport_value ^ cable->OUTPUT_INVERT;
  126. #if PARPORT_USE_PPDEV == 1
  127. ioctl(device_handle, PPWDATA, &output);
  128. #else
  129. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  130. outb(dataport, output);
  131. #else
  132. outb(output, dataport);
  133. #endif
  134. #endif
  135. }
  136. static void parport_write(int tck, int tms, int tdi)
  137. {
  138. int i = wait_states + 1;
  139. if (tck)
  140. dataport_value |= cable->TCK_MASK;
  141. else
  142. dataport_value &= ~cable->TCK_MASK;
  143. if (tms)
  144. dataport_value |= cable->TMS_MASK;
  145. else
  146. dataport_value &= ~cable->TMS_MASK;
  147. if (tdi)
  148. dataport_value |= cable->TDI_MASK;
  149. else
  150. dataport_value &= ~cable->TDI_MASK;
  151. while (i-- > 0)
  152. parport_write_data();
  153. }
  154. /* (1) assert or (0) deassert reset lines */
  155. static void parport_reset(int trst, int srst)
  156. {
  157. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  158. if (trst == 0)
  159. dataport_value |= cable->TRST_MASK;
  160. else if (trst == 1)
  161. dataport_value &= ~cable->TRST_MASK;
  162. if (srst == 0)
  163. dataport_value |= cable->SRST_MASK;
  164. else if (srst == 1)
  165. dataport_value &= ~cable->SRST_MASK;
  166. parport_write_data();
  167. }
  168. /* turn LED on parport adapter on (1) or off (0) */
  169. static void parport_led(int on)
  170. {
  171. if (on)
  172. dataport_value |= cable->LED_MASK;
  173. else
  174. dataport_value &= ~cable->LED_MASK;
  175. parport_write_data();
  176. }
  177. static int parport_speed(int speed)
  178. {
  179. wait_states = speed;
  180. return ERROR_OK;
  181. }
  182. static int parport_khz(int khz, int *jtag_speed)
  183. {
  184. if (khz == 0) {
  185. LOG_DEBUG("RCLK not supported");
  186. return ERROR_FAIL;
  187. }
  188. *jtag_speed = 499999 / (khz * parport_toggling_time_ns);
  189. return ERROR_OK;
  190. }
  191. static int parport_speed_div(int speed, int *khz)
  192. {
  193. uint32_t denominator = (speed + 1) * parport_toggling_time_ns;
  194. *khz = (499999 + denominator) / denominator;
  195. return ERROR_OK;
  196. }
  197. #if PARPORT_USE_GIVEIO == 1
  198. static int parport_get_giveio_access(void)
  199. {
  200. HANDLE h;
  201. OSVERSIONINFO version;
  202. version.dwOSVersionInfoSize = sizeof version;
  203. if (!GetVersionEx(&version)) {
  204. errno = EINVAL;
  205. return -1;
  206. }
  207. if (version.dwPlatformId != VER_PLATFORM_WIN32_NT)
  208. return 0;
  209. h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  210. if (h == INVALID_HANDLE_VALUE) {
  211. errno = ENODEV;
  212. return -1;
  213. }
  214. CloseHandle(h);
  215. return 0;
  216. }
  217. #endif
  218. static struct bitbang_interface parport_bitbang = {
  219. .read = &parport_read,
  220. .write = &parport_write,
  221. .reset = &parport_reset,
  222. .blink = &parport_led,
  223. };
  224. static int parport_init(void)
  225. {
  226. struct cable *cur_cable;
  227. #if PARPORT_USE_PPDEV == 1
  228. char buffer[256];
  229. #endif
  230. cur_cable = cables;
  231. if ((parport_cable == NULL) || (parport_cable[0] == 0)) {
  232. parport_cable = "wiggler";
  233. LOG_WARNING("No parport cable specified, using default 'wiggler'");
  234. }
  235. while (cur_cable->name) {
  236. if (strcmp(cur_cable->name, parport_cable) == 0) {
  237. cable = cur_cable;
  238. break;
  239. }
  240. cur_cable++;
  241. }
  242. if (!cable) {
  243. LOG_ERROR("No matching cable found for %s", parport_cable);
  244. return ERROR_JTAG_INIT_FAILED;
  245. }
  246. dataport_value = cable->PORT_INIT;
  247. #if PARPORT_USE_PPDEV == 1
  248. if (device_handle > 0) {
  249. LOG_ERROR("device is already opened");
  250. return ERROR_JTAG_INIT_FAILED;
  251. }
  252. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  253. LOG_DEBUG("opening /dev/ppi%d...", parport_port);
  254. snprintf(buffer, 256, "/dev/ppi%d", parport_port);
  255. device_handle = open(buffer, O_WRONLY);
  256. #else /* not __FreeBSD__, __FreeBSD_kernel__ */
  257. LOG_DEBUG("opening /dev/parport%d...", parport_port);
  258. snprintf(buffer, 256, "/dev/parport%d", parport_port);
  259. device_handle = open(buffer, O_WRONLY);
  260. #endif /* __FreeBSD__, __FreeBSD_kernel__ */
  261. if (device_handle < 0) {
  262. int err = errno;
  263. LOG_ERROR("cannot open device. check it exists and that user read and write rights are set. errno=%d", err);
  264. return ERROR_JTAG_INIT_FAILED;
  265. }
  266. LOG_DEBUG("...open");
  267. #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
  268. int i = ioctl(device_handle, PPCLAIM);
  269. if (i < 0) {
  270. LOG_ERROR("cannot claim device");
  271. return ERROR_JTAG_INIT_FAILED;
  272. }
  273. i = PARPORT_MODE_COMPAT;
  274. i = ioctl(device_handle, PPSETMODE, &i);
  275. if (i < 0) {
  276. LOG_ERROR(" cannot set compatible mode to device");
  277. return ERROR_JTAG_INIT_FAILED;
  278. }
  279. i = IEEE1284_MODE_COMPAT;
  280. i = ioctl(device_handle, PPNEGOT, &i);
  281. if (i < 0) {
  282. LOG_ERROR("cannot set compatible 1284 mode to device");
  283. return ERROR_JTAG_INIT_FAILED;
  284. }
  285. #endif /* not __FreeBSD__, __FreeBSD_kernel__ */
  286. #else /* not PARPORT_USE_PPDEV */
  287. if (parport_port == 0) {
  288. parport_port = 0x378;
  289. LOG_WARNING("No parport port specified, using default '0x378' (LPT1)");
  290. }
  291. dataport = parport_port;
  292. statusport = parport_port + 1;
  293. LOG_DEBUG("requesting privileges for parallel port 0x%lx...", dataport);
  294. #if PARPORT_USE_GIVEIO == 1
  295. if (parport_get_giveio_access() != 0) {
  296. #else /* PARPORT_USE_GIVEIO */
  297. if (ioperm(dataport, 3, 1) != 0) {
  298. #endif /* PARPORT_USE_GIVEIO */
  299. LOG_ERROR("missing privileges for direct i/o");
  300. return ERROR_JTAG_INIT_FAILED;
  301. }
  302. LOG_DEBUG("...privileges granted");
  303. /* make sure parallel port is in right mode (clear tristate and interrupt */
  304. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  305. outb(parport_port + 2, 0x0);
  306. #else
  307. outb(0x0, parport_port + 2);
  308. #endif
  309. #endif /* PARPORT_USE_PPDEV */
  310. parport_reset(0, 0);
  311. parport_write(0, 0, 0);
  312. parport_led(1);
  313. bitbang_interface = &parport_bitbang;
  314. return ERROR_OK;
  315. }
  316. static int parport_quit(void)
  317. {
  318. parport_led(0);
  319. if (parport_exit) {
  320. dataport_value = cable->PORT_EXIT;
  321. parport_write_data();
  322. }
  323. if (parport_cable) {
  324. free(parport_cable);
  325. parport_cable = NULL;
  326. }
  327. return ERROR_OK;
  328. }
  329. COMMAND_HANDLER(parport_handle_parport_port_command)
  330. {
  331. if (CMD_ARGC == 1) {
  332. /* only if the port wasn't overwritten by cmdline */
  333. if (parport_port == 0)
  334. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], parport_port);
  335. else {
  336. LOG_ERROR("The parport port was already configured!");
  337. return ERROR_FAIL;
  338. }
  339. }
  340. command_print(CMD_CTX, "parport port = 0x%" PRIx16 "", parport_port);
  341. return ERROR_OK;
  342. }
  343. COMMAND_HANDLER(parport_handle_parport_cable_command)
  344. {
  345. if (CMD_ARGC == 0)
  346. return ERROR_OK;
  347. /* only if the cable name wasn't overwritten by cmdline */
  348. if (parport_cable == 0) {
  349. /* REVISIT first verify that it's listed in cables[] ... */
  350. parport_cable = malloc(strlen(CMD_ARGV[0]) + sizeof(char));
  351. strcpy(parport_cable, CMD_ARGV[0]);
  352. }
  353. /* REVISIT it's probably worth returning the current value ... */
  354. return ERROR_OK;
  355. }
  356. COMMAND_HANDLER(parport_handle_write_on_exit_command)
  357. {
  358. if (CMD_ARGC != 1)
  359. return ERROR_COMMAND_SYNTAX_ERROR;
  360. COMMAND_PARSE_ON_OFF(CMD_ARGV[0], parport_exit);
  361. return ERROR_OK;
  362. }
  363. COMMAND_HANDLER(parport_handle_parport_toggling_time_command)
  364. {
  365. if (CMD_ARGC == 1) {
  366. uint32_t ns;
  367. int retval = parse_u32(CMD_ARGV[0], &ns);
  368. if (ERROR_OK != retval)
  369. return retval;
  370. if (ns == 0) {
  371. LOG_ERROR("0 ns is not a valid parport toggling time");
  372. return ERROR_FAIL;
  373. }
  374. parport_toggling_time_ns = ns;
  375. retval = jtag_get_speed(&wait_states);
  376. if (retval != ERROR_OK)
  377. return retval;
  378. }
  379. command_print(CMD_CTX, "parport toggling time = %" PRIu32 " ns",
  380. parport_toggling_time_ns);
  381. return ERROR_OK;
  382. }
  383. static const struct command_registration parport_command_handlers[] = {
  384. {
  385. .name = "parport_port",
  386. .handler = parport_handle_parport_port_command,
  387. .mode = COMMAND_CONFIG,
  388. .help = "Display the address of the I/O port (e.g. 0x378) "
  389. "or the number of the '/dev/parport' device used. "
  390. "If a parameter is provided, first change that port.",
  391. .usage = "[port_number]",
  392. },
  393. {
  394. .name = "parport_cable",
  395. .handler = parport_handle_parport_cable_command,
  396. .mode = COMMAND_CONFIG,
  397. .help = "Set the layout of the parallel port cable "
  398. "used to connect to the target.",
  399. /* REVISIT there's no way to list layouts we know ... */
  400. .usage = "[layout]",
  401. },
  402. {
  403. .name = "parport_write_on_exit",
  404. .handler = parport_handle_write_on_exit_command,
  405. .mode = COMMAND_CONFIG,
  406. .help = "Configure the parallel driver to write "
  407. "a known value to the parallel interface on exit.",
  408. .usage = "('on'|'off')",
  409. },
  410. {
  411. .name = "parport_toggling_time",
  412. .handler = parport_handle_parport_toggling_time_command,
  413. .mode = COMMAND_CONFIG,
  414. .help = "Displays or assigns how many nanoseconds it "
  415. "takes for the hardware to toggle TCK.",
  416. .usage = "[nanoseconds]",
  417. },
  418. COMMAND_REGISTRATION_DONE
  419. };
  420. struct jtag_interface parport_interface = {
  421. .name = "parport",
  422. .supported = DEBUG_CAP_TMS_SEQ,
  423. .commands = parport_command_handlers,
  424. .init = parport_init,
  425. .quit = parport_quit,
  426. .khz = parport_khz,
  427. .speed_div = parport_speed_div,
  428. .speed = parport_speed,
  429. .execute_queue = bitbang_execute_queue,
  430. };