driver-x6500.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include <libusb-1.0/libusb.h>
  10. #include "dynclock.h"
  11. #include "jtag.h"
  12. #include "logging.h"
  13. #include "miner.h"
  14. #include "fpgautils.h"
  15. #include "ft232r.h"
  16. #define X6500_USB_PRODUCT "X6500 FPGA Miner"
  17. #define X6500_BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.bit"
  18. #define X6500_BITSTREAM_USERID "\2\4$B"
  19. #define X6500_MINIMUM_CLOCK 2
  20. #define X6500_DEFAULT_CLOCK 200
  21. #define X6500_MAXIMUM_CLOCK 210
  22. struct device_api x6500_api;
  23. static bool x6500_foundusb(libusb_device *dev, const char *product, const char *serial)
  24. {
  25. struct cgpu_info *x6500;
  26. x6500 = calloc(1, sizeof(*x6500));
  27. x6500->api = &x6500_api;
  28. mutex_init(&x6500->device_mutex);
  29. x6500->device_path = strdup(serial);
  30. x6500->deven = DEV_ENABLED;
  31. x6500->threads = 2;
  32. x6500->name = strdup(product);
  33. x6500->cutofftemp = 85;
  34. x6500->cgpu_data = dev;
  35. return add_cgpu(x6500);
  36. }
  37. static bool x6500_detect_one(const char *serial)
  38. {
  39. return ft232r_detect(X6500_USB_PRODUCT, serial, x6500_foundusb);
  40. }
  41. static int x6500_detect_auto()
  42. {
  43. return ft232r_detect(X6500_USB_PRODUCT, NULL, x6500_foundusb);
  44. }
  45. static void x6500_detect()
  46. {
  47. serial_detect_auto(&x6500_api, x6500_detect_one, x6500_detect_auto);
  48. }
  49. static bool x6500_prepare(struct thr_info *thr)
  50. {
  51. if (thr->device_thread)
  52. return true;
  53. struct cgpu_info *x6500 = thr->cgpu;
  54. mutex_init(&x6500->device_mutex);
  55. struct ft232r_device_handle *ftdi = ft232r_open(x6500->cgpu_data);
  56. x6500->cgpu_data = NULL;
  57. if (!ftdi)
  58. return false;
  59. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  60. return false;
  61. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  62. return false;
  63. x6500->device_ft232r = ftdi;
  64. x6500->cgpu_data = calloc(1, 1);
  65. return true;
  66. }
  67. struct x6500_fpga_data {
  68. struct jtag_port jtag;
  69. };
  70. #define bailout(...) do{return false;}while(0)
  71. #define bailout2(...) do{return false;}while(0)
  72. #define check_magic(L) do { \
  73. if (1 != fread(buf, 1, 1, f)) \
  74. bailout(LOG_ERR, "Error reading ModMiner firmware ('%c')", L); \
  75. if (buf[0] != L) \
  76. bailout(LOG_ERR, "ModMiner firmware has wrong magic ('%c')", L); \
  77. } while(0)
  78. #define read_str(eng) do { \
  79. if (1 != fread(buf, 2, 1, f)) \
  80. bailout(LOG_ERR, "Error reading ModMiner firmware (" eng " len)"); \
  81. len = (ubuf[0] << 8) | ubuf[1]; \
  82. if (len >= sizeof(buf)) \
  83. bailout(LOG_ERR, "ModMiner firmware " eng " too long"); \
  84. if (1 != fread(buf, len, 1, f)) \
  85. bailout(LOG_ERR, "Error reading ModMiner firmware (" eng ")"); \
  86. buf[len] = '\0'; \
  87. } while(0)
  88. static bool
  89. x6500_fpga_upload_bitstream(struct cgpu_info *x6500, struct ft232r_device_handle *ftdi)
  90. {
  91. struct x6500_fpga_data *state = x6500->thr[0]->cgpu_data;
  92. char buf[0x100];
  93. unsigned char *ubuf = (unsigned char*)buf;
  94. unsigned long len, flen;
  95. char *p;
  96. const char *fwfile = X6500_BITSTREAM_FILENAME;
  97. char *pdone = (char*)&x6500->cgpu_data;
  98. FILE *f = open_bitstream("x6500", fwfile);
  99. if (!f)
  100. bailout(LOG_ERR, "Error opening X6500 firmware file %s", fwfile);
  101. if (1 != fread(buf, 2, 1, f))
  102. bailout(LOG_ERR, "Error reading X6500 firmware (magic)");
  103. if (buf[0] || buf[1] != 9)
  104. bailout(LOG_ERR, "X6500 firmware has wrong magic (9)");
  105. if (-1 == fseek(f, 11, SEEK_CUR))
  106. bailout(LOG_ERR, "X6500 firmware seek failed");
  107. check_magic('a');
  108. read_str("design name");
  109. applog(LOG_DEBUG, "X6500 firmware file %s info:", fwfile);
  110. applog(LOG_DEBUG, " Design name: %s", buf);
  111. p = strrchr(buf, ';') ?: buf;
  112. p = strrchr(buf, '=') ?: p;
  113. if (p[0] == '=')
  114. ++p;
  115. unsigned long fwusercode = (unsigned long)strtoll(p, &p, 16);
  116. if (p[0] != '\0')
  117. bailout(LOG_ERR, "Bad usercode in X6500 firmware file");
  118. if (fwusercode == 0xffffffff)
  119. bailout(LOG_ERR, "X6500 firmware doesn't support user code");
  120. applog(LOG_DEBUG, " Version: %u, build %u", (fwusercode >> 8) & 0xff, fwusercode & 0xff);
  121. check_magic('b');
  122. read_str("part number");
  123. applog(LOG_DEBUG, " Part number: %s", buf);
  124. check_magic('c');
  125. read_str("build date");
  126. applog(LOG_DEBUG, " Build date: %s", buf);
  127. check_magic('d');
  128. read_str("build time");
  129. applog(LOG_DEBUG, " Build time: %s", buf);
  130. check_magic('e');
  131. if (1 != fread(buf, 4, 1, f))
  132. bailout(LOG_ERR, "Error reading X6500 firmware (data len)");
  133. len = ((unsigned long)ubuf[0] << 24) | ((unsigned long)ubuf[1] << 16) | (ubuf[2] << 8) | ubuf[3];
  134. flen = len;
  135. applog(LOG_DEBUG, " Bitstream size: %lu", len);
  136. applog(LOG_WARNING, "%s %u: Programming %s... DO NOT EXIT UNTIL COMPLETE", x6500->api->name, x6500->device_id, x6500->device_path);
  137. uint8_t dummyx;
  138. struct jtag_port jpt = {
  139. .ftdi = ftdi,
  140. .tck = 0x88,
  141. .tms = 0x44,
  142. .tdi = 0x22,
  143. .tdo = 0x11,
  144. .ignored = 0,
  145. .state = &dummyx,
  146. };
  147. struct jtag_port *jp = &jpt;
  148. uint8_t i;
  149. // Need to reset here despite previous FPGA state, since we are programming all at once
  150. jtag_reset(jp);
  151. jtag_write(jp, JTAG_REG_IR, "\xd0", 6); // JPROGRAM
  152. do {
  153. i = 0xd0; // Re-set JPROGRAM while reading status
  154. jtag_read(jp, JTAG_REG_IR, &i, 6);
  155. } while (i & 8);
  156. jtag_write(jp, JTAG_REG_IR, "\xa0", 6); // CFG_IN
  157. sleep(1);
  158. if (fread(buf, 32, 1, f) != 1)
  159. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", x6500->api->name, x6500->device_id, x6500->device_path, len);
  160. jtag_swrite(jp, JTAG_REG_DR, buf, 256);
  161. len -= 32;
  162. if (!ft232r_set_bitmode(ftdi, 0xee, 1))
  163. return false;
  164. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  165. return false;
  166. jp->async = true;
  167. ssize_t buflen;
  168. char nextstatus = 10;
  169. while (len) {
  170. buflen = len < 32 ? len : 32;
  171. if (fread(buf, buflen, 1, f) != 1)
  172. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", x6500->api->name, x6500->device_id, x6500->device_path, len);
  173. jtag_swrite_more(jp, buf, buflen * 8, len == buflen);
  174. *pdone = 100 - ((len * 100) / flen);
  175. if (*pdone >= nextstatus)
  176. {
  177. nextstatus += 10;
  178. applog(LOG_WARNING, "%s %u: Programming %s... %d%% complete...", x6500->api->name, x6500->device_id, x6500->device_path, *pdone);
  179. }
  180. len -= buflen;
  181. }
  182. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  183. return false;
  184. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  185. return false;
  186. jp->async = false;
  187. jtag_write(jp, JTAG_REG_IR, "\x30", 6); // JSTART
  188. for (i=0; i<16; ++i)
  189. jtag_run(jp);
  190. i = 0xff; // BYPASS
  191. jtag_read(jp, JTAG_REG_IR, &i, 6);
  192. if (!(i & 4))
  193. return false;
  194. applog(LOG_WARNING, "%s %u: Done programming %s", x6500->api->name, x6500->device_id, x6500->device_path);
  195. return true;
  196. }
  197. static bool x6500_fpga_init(struct thr_info *thr)
  198. {
  199. struct cgpu_info *x6500 = thr->cgpu;
  200. struct ft232r_device_handle *ftdi = x6500->device_ft232r;
  201. struct x6500_fpga_data *fpga;
  202. struct jtag_port *jp;
  203. uint8_t pinoffset = thr->device_thread ? 0x10 : 1;
  204. unsigned char buf[4];
  205. int i;
  206. if (!ftdi)
  207. return false;
  208. fpga = calloc(1, sizeof(*fpga));
  209. jp = &fpga->jtag;
  210. jp->ftdi = ftdi;
  211. jp->tck = pinoffset << 3;
  212. jp->tms = pinoffset << 2;
  213. jp->tdi = pinoffset << 1;
  214. jp->tdo = pinoffset << 0;
  215. jp->ignored = ~(fpga->jtag.tdo | fpga->jtag.tdi | fpga->jtag.tms | fpga->jtag.tck);
  216. jp->state = x6500->cgpu_data;
  217. applog(LOG_ERR, "jtag pins: tck=%02x tms=%02x tdi=%02x tdo=%02x", pinoffset << 3, pinoffset << 2, pinoffset << 1, pinoffset << 0);
  218. mutex_lock(&x6500->device_mutex);
  219. if (!jtag_reset(jp)) {
  220. mutex_unlock(&x6500->device_mutex);
  221. applog(LOG_ERR, "jtag reset failed");
  222. return false;
  223. }
  224. // while(gets(buf)) {
  225. // jtag_clock(jp, buf[0]=='/', buf[1]=='/', &buf[2]);
  226. // applog(LOG_ERR, "=> %d", (int)buf[2]);
  227. // }
  228. i = jtag_detect(jp);
  229. if (i != 1) {
  230. mutex_unlock(&x6500->device_mutex);
  231. applog(LOG_ERR, "jtag detect returned %d", i);
  232. return false;
  233. }
  234. x6500_fpga_upload_bitstream(x6500, ftdi);
  235. if (!(1
  236. && jtag_write(jp, JTAG_REG_IR, "\x10", 6)
  237. && jtag_read (jp, JTAG_REG_DR, buf, 32)
  238. && jtag_reset(jp)
  239. )) {
  240. mutex_unlock(&x6500->device_mutex);
  241. applog(LOG_ERR, "jtag error reading user code");
  242. return false;
  243. }
  244. if (memcmp(buf, X6500_BITSTREAM_USERID, 4)) {
  245. }
  246. applog(LOG_ERR, "userid: %s", bin2hex(buf, 4));
  247. mutex_unlock(&x6500->device_mutex);
  248. return false;
  249. }
  250. static void
  251. get_x6500_statline_before(char *buf, struct cgpu_info *x6500)
  252. {
  253. char info[18] = " | ";
  254. char pdone = (char)(x6500->cgpu_data);
  255. if (pdone != 101) {
  256. sprintf(&info[1], "%3d%%", pdone);
  257. info[5] = ' ';
  258. strcat(buf, info);
  259. return;
  260. }
  261. strcat(buf, " | ");
  262. }
  263. struct device_api x6500_api = {
  264. .dname = "x6500",
  265. .name = "XBS",
  266. .api_detect = x6500_detect,
  267. .thread_prepare = x6500_prepare,
  268. .thread_init = x6500_fpga_init,
  269. .get_statline_before = get_x6500_statline_before,
  270. // .scanhash = x6500_fpga_scanhash,
  271. // .thread_shutdown = x6500_fpga_shutdown,
  272. };