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.
 
 
 
 
 
 

181 lines
4.9 KiB

  1. /*
  2. * Driver for USB-JTAG, Altera USB-Blaster and compatibles
  3. *
  4. * Inspired from original code from Kolja Waschk's USB-JTAG project
  5. * (http://www.ixo.de/info/usb_jtag/), and from openocd project.
  6. *
  7. * Copyright (C) 2012 Robert Jarzmik robert.jarzmik@free.fr
  8. * Copyright (C) 2011 Ali Lown ali@lown.me.uk
  9. * Copyright (C) 2009 Catalin Patulea cat@vv.carleton.ca
  10. * Copyright (C) 2006 Kolja Waschk usbjtag@ixo.de
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. #include <jtag/interface.h>
  30. #include <jtag/commands.h>
  31. #include "ublast_access.h"
  32. #include <ftd2xx.h>
  33. #include "jtag/drivers/ftd2xx_common.h"
  34. static FT_HANDLE *ublast_getftdih(struct ublast_lowlevel *low)
  35. {
  36. return low->priv;
  37. }
  38. static int ublast_ftd2xx_write(struct ublast_lowlevel *low, uint8_t *buf, int size,
  39. uint32_t *bytes_written)
  40. {
  41. FT_STATUS status;
  42. DWORD dw_bytes_written;
  43. FT_HANDLE *ftdih = ublast_getftdih(low);
  44. status = FT_Write(*ftdih, buf, size, &dw_bytes_written);
  45. if (status != FT_OK) {
  46. *bytes_written = dw_bytes_written;
  47. LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
  48. return ERROR_JTAG_DEVICE_ERROR;
  49. }
  50. *bytes_written = dw_bytes_written;
  51. return ERROR_OK;
  52. }
  53. static int ublast_ftd2xx_read(struct ublast_lowlevel *low, uint8_t *buf,
  54. unsigned size, uint32_t *bytes_read)
  55. {
  56. DWORD dw_bytes_read;
  57. FT_STATUS status;
  58. FT_HANDLE *ftdih = ublast_getftdih(low);
  59. status = FT_Read(*ftdih, buf, size, &dw_bytes_read);
  60. if (status != FT_OK) {
  61. *bytes_read = dw_bytes_read;
  62. LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
  63. return ERROR_JTAG_DEVICE_ERROR;
  64. }
  65. *bytes_read = dw_bytes_read;
  66. return ERROR_OK;
  67. }
  68. static int ublast_ftd2xx_init(struct ublast_lowlevel *low)
  69. {
  70. FT_STATUS status;
  71. FT_HANDLE *ftdih = ublast_getftdih(low);
  72. uint8_t latency_timer;
  73. LOG_INFO("usb blaster interface using FTD2XX");
  74. /* Open by device description */
  75. if (low->ublast_device_desc == NULL) {
  76. LOG_WARNING("no usb blaster device description specified, "
  77. "using default 'USB-Blaster'");
  78. low->ublast_device_desc = "USB-Blaster";
  79. }
  80. #if IS_WIN32 == 0
  81. /* Add non-standard Vid/Pid to the linux driver */
  82. status = FT_SetVIDPID(low->ublast_vid, low->ublast_pid);
  83. if (status != FT_OK) {
  84. LOG_WARNING("couldn't add %4.4x:%4.4x",
  85. low->ublast_vid, low->ublast_pid);
  86. }
  87. #endif
  88. status = FT_OpenEx(low->ublast_device_desc, FT_OPEN_BY_DESCRIPTION,
  89. ftdih);
  90. if (status != FT_OK) {
  91. DWORD num_devices;
  92. LOG_ERROR("unable to open ftdi device: %s",
  93. ftd2xx_status_string(status));
  94. status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
  95. if (status == FT_OK) {
  96. char **desc_array =
  97. malloc(sizeof(char *) * (num_devices + 1));
  98. unsigned int i;
  99. for (i = 0; i < num_devices; i++)
  100. desc_array[i] = malloc(64);
  101. desc_array[num_devices] = NULL;
  102. status = FT_ListDevices(desc_array, &num_devices,
  103. FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
  104. if (status == FT_OK) {
  105. LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
  106. for (i = 0; i < num_devices; i++)
  107. LOG_ERROR("%i: %s", i, desc_array[i]);
  108. }
  109. for (i = 0; i < num_devices; i++)
  110. free(desc_array[i]);
  111. free(desc_array);
  112. } else {
  113. printf("ListDevices: NONE\n");
  114. }
  115. return ERROR_JTAG_INIT_FAILED;
  116. }
  117. status = FT_SetLatencyTimer(*ftdih, 2);
  118. if (status != FT_OK) {
  119. LOG_ERROR("unable to set latency timer: %s",
  120. ftd2xx_status_string(status));
  121. return ERROR_JTAG_INIT_FAILED;
  122. }
  123. status = FT_GetLatencyTimer(*ftdih, &latency_timer);
  124. if (status != FT_OK)
  125. LOG_ERROR("unable to get latency timer: %s",
  126. ftd2xx_status_string(status));
  127. else
  128. LOG_DEBUG("current latency timer: %i", latency_timer);
  129. status = FT_SetBitMode(*ftdih, 0x00, 0);
  130. if (status != FT_OK) {
  131. LOG_ERROR("unable to disable bit i/o mode: %s",
  132. ftd2xx_status_string(status));
  133. return ERROR_JTAG_INIT_FAILED;
  134. }
  135. return ERROR_OK;
  136. }
  137. static int ublast_ftd2xx_quit(struct ublast_lowlevel *low)
  138. {
  139. FT_HANDLE *ftdih = ublast_getftdih(low);
  140. FT_Close(*ftdih);
  141. return ERROR_OK;
  142. }
  143. static struct ublast_lowlevel_priv {
  144. FT_HANDLE ftdih;
  145. } info;
  146. static struct ublast_lowlevel low = {
  147. .open = ublast_ftd2xx_init,
  148. .close = ublast_ftd2xx_quit,
  149. .read = ublast_ftd2xx_read,
  150. .write = ublast_ftd2xx_write,
  151. .priv = &info,
  152. };
  153. struct ublast_lowlevel *ublast_register_ftd2xx(void)
  154. {
  155. return &low;
  156. }