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.
 
 
 
 
 
 

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