driver-x6500.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. // NOTE: X6500_BITSTREAM_USERID is bitflipped
  19. #define X6500_BITSTREAM_USERID "\x40\x20\x24\x42"
  20. #define X6500_MINIMUM_CLOCK 2
  21. #define X6500_DEFAULT_CLOCK 200
  22. #define X6500_MAXIMUM_CLOCK 210
  23. struct device_api x6500_api;
  24. #define fromlebytes(ca, j) (ca[j] | (((uint16_t)ca[j+1])<<8) | (((uint32_t)ca[j+2])<<16) | (((uint32_t)ca[j+3])<<24))
  25. static
  26. void int2bits(uint32_t n, uint8_t *b, uint8_t bits)
  27. {
  28. uint8_t i;
  29. for (i = (bits + 7) / 8; i > 0; )
  30. b[--i] = 0;
  31. for (i = 0; i < bits; ++i) {
  32. if (n & 1)
  33. b[i/8] |= 0x80 >> (i % 8);
  34. n >>= 1;
  35. }
  36. }
  37. static
  38. uint32_t bits2int(uint8_t *b, uint8_t bits)
  39. {
  40. uint32_t n, i;
  41. n = 0;
  42. for (i = 0; i < bits; ++i)
  43. if (b[i/8] & (0x80 >> (i % 8)))
  44. n |= 1<<i;
  45. return n;
  46. }
  47. static
  48. void bitendianflip(void *n, size_t bits)
  49. {
  50. size_t i;
  51. uint8_t *b = n;
  52. // NOTE: this doesn't work with non-byte boundaries
  53. bits /= 8;
  54. for (i = 0; i < bits; ++i)
  55. b[i] = ((b[i] & 1) ? 0x80 : 0)
  56. | ((b[i] & 2) ? 0x40 : 0)
  57. | ((b[i] & 4) ? 0x20 : 0)
  58. | ((b[i] & 8) ? 0x10 : 0)
  59. | ((b[i] & 0x10) ? 8 : 0)
  60. | ((b[i] & 0x20) ? 4 : 0)
  61. | ((b[i] & 0x40) ? 2 : 0)
  62. | ((b[i] & 0x80) ? 1 : 0);
  63. }
  64. static
  65. void checksum(uint8_t *b, uint8_t bits)
  66. {
  67. uint8_t i;
  68. uint8_t checksum = 1;
  69. for(i = 0; i < bits; ++i)
  70. checksum ^= (b[i/8] & (0x80 >> (i % 8))) ? 1 : 0;
  71. if (checksum)
  72. b[i/8] |= 0x80 >> (i % 8);
  73. }
  74. static
  75. void x6500_set_register(struct jtag_port *jp, uint8_t addr, uint32_t nv)
  76. {
  77. uint8_t buf[38];
  78. jtag_write(jp, JTAG_REG_IR, "\x40", 6);
  79. int2bits(nv, &buf[0], 32);
  80. int2bits(addr, &buf[4], 4);
  81. buf[4] |= 8;
  82. checksum(buf, 37);
  83. jtag_write(jp, JTAG_REG_DR, buf, 38);
  84. jtag_run(jp);
  85. }
  86. static
  87. uint32_t x6500_get_register(struct jtag_port *jp, uint8_t addr)
  88. {
  89. uint8_t buf[4];
  90. jtag_write(jp, JTAG_REG_IR, "\x40", 6);
  91. int2bits(addr, &buf[0], 4);
  92. checksum(buf, 5);
  93. jtag_write(jp, JTAG_REG_DR, buf, 6);
  94. jtag_read (jp, JTAG_REG_DR, buf, 32);
  95. jtag_reset(jp);
  96. bitendianflip(buf, 32);
  97. return bits2int(buf, 32);
  98. }
  99. static bool x6500_foundusb(libusb_device *dev, const char *product, const char *serial)
  100. {
  101. struct cgpu_info *x6500;
  102. x6500 = calloc(1, sizeof(*x6500));
  103. x6500->api = &x6500_api;
  104. mutex_init(&x6500->device_mutex);
  105. x6500->device_path = strdup(serial);
  106. x6500->deven = DEV_ENABLED;
  107. x6500->threads = 2;
  108. x6500->name = strdup(product);
  109. x6500->cutofftemp = 85;
  110. x6500->cgpu_data = dev;
  111. return add_cgpu(x6500);
  112. }
  113. static bool x6500_detect_one(const char *serial)
  114. {
  115. return ft232r_detect(X6500_USB_PRODUCT, serial, x6500_foundusb);
  116. }
  117. static int x6500_detect_auto()
  118. {
  119. return ft232r_detect(X6500_USB_PRODUCT, NULL, x6500_foundusb);
  120. }
  121. static void x6500_detect()
  122. {
  123. serial_detect_auto(&x6500_api, x6500_detect_one, x6500_detect_auto);
  124. }
  125. static bool x6500_prepare(struct thr_info *thr)
  126. {
  127. if (thr->device_thread)
  128. return true;
  129. struct cgpu_info *x6500 = thr->cgpu;
  130. mutex_init(&x6500->device_mutex);
  131. struct ft232r_device_handle *ftdi = ft232r_open(x6500->cgpu_data);
  132. x6500->device_ft232r = NULL;
  133. if (!ftdi)
  134. return false;
  135. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  136. return false;
  137. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  138. return false;
  139. x6500->device_ft232r = ftdi;
  140. struct jtag_port_a *jtag_a;
  141. jtag_a = calloc(1, sizeof(*jtag_a));
  142. jtag_a->ftdi = ftdi;
  143. x6500->cgpu_data = jtag_a;
  144. return true;
  145. }
  146. struct x6500_fpga_data {
  147. struct jtag_port jtag;
  148. };
  149. #define bailout2(...) do { \
  150. applog(__VA_ARGS__); \
  151. return false; \
  152. } while(0)
  153. static bool
  154. x6500_fpga_upload_bitstream(struct cgpu_info *x6500, struct jtag_port *jp1)
  155. {
  156. char buf[0x100];
  157. unsigned long len, flen;
  158. char *pdone = (char*)&x6500->cgpu_data;
  159. struct ft232r_device_handle *ftdi = jp1->a->ftdi;
  160. FILE *f = open_xilinx_bitstream(x6500, X6500_BITSTREAM_FILENAME, &len);
  161. if (!f)
  162. return false;
  163. flen = len;
  164. applog(LOG_WARNING, "%s %u: Programming %s...",
  165. x6500->api->name, x6500->device_id, x6500->device_path);
  166. // "Magic" jtag_port configured to access both FPGAs concurrently
  167. struct jtag_port jpt = {
  168. .a = jp1->a,
  169. .tck = 0x88,
  170. .tms = 0x44,
  171. .tdi = 0x22,
  172. .tdo = 0x11,
  173. .ignored = 0,
  174. };
  175. struct jtag_port *jp = &jpt;
  176. uint8_t i;
  177. // Need to reset here despite previous FPGA state, since we are programming all at once
  178. jtag_reset(jp);
  179. jtag_write(jp, JTAG_REG_IR, "\xd0", 6); // JPROGRAM
  180. do {
  181. i = 0xd0; // Re-set JPROGRAM while reading status
  182. jtag_read(jp, JTAG_REG_IR, &i, 6);
  183. } while (i & 8);
  184. jtag_write(jp, JTAG_REG_IR, "\xa0", 6); // CFG_IN
  185. sleep(1);
  186. if (fread(buf, 32, 1, f) != 1)
  187. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", x6500->api->name, x6500->device_id, x6500->device_path, len);
  188. jtag_swrite(jp, JTAG_REG_DR, buf, 256);
  189. len -= 32;
  190. // Put ft232r chip in asynchronous bitbang mode so we don't need to read back tdo
  191. // This takes upload time down from about an hour to about 3 minutes
  192. if (!ft232r_set_bitmode(ftdi, 0xee, 1))
  193. return false;
  194. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  195. return false;
  196. jp->a->async = true;
  197. ssize_t buflen;
  198. char nextstatus = 10;
  199. while (len) {
  200. buflen = len < 32 ? len : 32;
  201. if (fread(buf, buflen, 1, f) != 1)
  202. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", x6500->api->name, x6500->device_id, x6500->device_path, len);
  203. jtag_swrite_more(jp, buf, buflen * 8, len == (unsigned long)buflen);
  204. *pdone = 100 - ((len * 100) / flen);
  205. if (*pdone >= nextstatus)
  206. {
  207. nextstatus += 10;
  208. applog(LOG_WARNING, "%s %u: Programming %s... %d%% complete...", x6500->api->name, x6500->device_id, x6500->device_path, *pdone);
  209. }
  210. len -= buflen;
  211. }
  212. // Switch back to synchronous bitbang mode
  213. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  214. return false;
  215. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  216. return false;
  217. jp->a->async = false;
  218. jp->a->bufread = 0;
  219. jtag_write(jp, JTAG_REG_IR, "\x30", 6); // JSTART
  220. for (i=0; i<16; ++i)
  221. jtag_run(jp);
  222. i = 0xff; // BYPASS
  223. jtag_read(jp, JTAG_REG_IR, &i, 6);
  224. if (!(i & 4))
  225. return false;
  226. applog(LOG_WARNING, "%s %u: Done programming %s", x6500->api->name, x6500->device_id, x6500->device_path);
  227. return true;
  228. }
  229. static bool x6500_fpga_init(struct thr_info *thr)
  230. {
  231. struct cgpu_info *x6500 = thr->cgpu;
  232. struct ft232r_device_handle *ftdi = x6500->device_ft232r;
  233. struct x6500_fpga_data *fpga;
  234. struct jtag_port *jp;
  235. int fpgaid = thr->device_thread;
  236. uint8_t pinoffset = fpgaid ? 0x10 : 1;
  237. unsigned char buf[4];
  238. int i;
  239. if (!ftdi)
  240. return false;
  241. fpga = calloc(1, sizeof(*fpga));
  242. jp = &fpga->jtag;
  243. jp->a = x6500->cgpu_data;
  244. jp->tck = pinoffset << 3;
  245. jp->tms = pinoffset << 2;
  246. jp->tdi = pinoffset << 1;
  247. jp->tdo = pinoffset << 0;
  248. jp->ignored = ~(fpga->jtag.tdo | fpga->jtag.tdi | fpga->jtag.tms | fpga->jtag.tck);
  249. mutex_lock(&x6500->device_mutex);
  250. if (!jtag_reset(jp)) {
  251. mutex_unlock(&x6500->device_mutex);
  252. applog(LOG_ERR, "%s %u: JTAG reset failed",
  253. x6500->api->name, x6500->device_id);
  254. return false;
  255. }
  256. i = jtag_detect(jp);
  257. if (i != 1) {
  258. mutex_unlock(&x6500->device_mutex);
  259. applog(LOG_ERR, "%s %u: JTAG detect returned %d",
  260. x6500->api->name, x6500->device_id, i);
  261. return false;
  262. }
  263. if (!(1
  264. && jtag_write(jp, JTAG_REG_IR, "\x10", 6)
  265. && jtag_read (jp, JTAG_REG_DR, buf, 32)
  266. && jtag_reset(jp)
  267. )) {
  268. mutex_unlock(&x6500->device_mutex);
  269. applog(LOG_ERR, "%s %u: JTAG error reading user code",
  270. x6500->api->name, x6500->device_id);
  271. return false;
  272. }
  273. if (memcmp(buf, X6500_BITSTREAM_USERID, 4)) {
  274. applog(LOG_ERR, "%s %u.%u: FPGA not programmed",
  275. x6500->api->name, x6500->device_id, fpgaid);
  276. if (!x6500_fpga_upload_bitstream(x6500, jp))
  277. return false;
  278. } else
  279. applog(LOG_DEBUG, "%s %u.%u: FPGA is already programmed :)",
  280. x6500->api->name, x6500->device_id, fpgaid);
  281. thr->cgpu_data = fpga;
  282. x6500_set_register(jp, 0xD, 200); // Set clock speed
  283. mutex_unlock(&x6500->device_mutex);
  284. return true;
  285. }
  286. static void
  287. get_x6500_statline_before(char *buf, struct cgpu_info *x6500)
  288. {
  289. char info[18] = " | ";
  290. char pdone = (char)(x6500->cgpu_data);
  291. if (pdone != 101) {
  292. sprintf(&info[1], "%3d%%", pdone);
  293. info[5] = ' ';
  294. strcat(buf, info);
  295. return;
  296. }
  297. strcat(buf, " | ");
  298. }
  299. static
  300. bool x6500_start_work(struct thr_info *thr, struct work *work)
  301. {
  302. struct cgpu_info *x6500 = thr->cgpu;
  303. struct x6500_fpga_data *fpga = thr->cgpu_data;
  304. char fpgaid = thr->device_thread;
  305. mutex_lock(&x6500->device_mutex);
  306. for (int i = 1, j = 0; i < 9; ++i, j += 4)
  307. x6500_set_register(&fpga->jtag, i, fromlebytes(work->midstate, j));
  308. for (int i = 9, j = 64; i < 12; ++i, j += 4)
  309. x6500_set_register(&fpga->jtag, i, fromlebytes(work->data, j));
  310. //gettimeofday(&fpga->tv_workstart, NULL);
  311. mutex_unlock(&x6500->device_mutex);
  312. if (opt_debug) {
  313. char *xdata = bin2hex(work->data, 80);
  314. applog(LOG_DEBUG, "%s %u.%u: Started work: %s",
  315. x6500->api->name, x6500->device_id, fpgaid, xdata);
  316. free(xdata);
  317. }
  318. return true;
  319. }
  320. static
  321. int64_t x6500_process_results(struct thr_info *thr, struct work *work)
  322. {
  323. struct cgpu_info *x6500 = thr->cgpu;
  324. struct x6500_fpga_data *fpga = thr->cgpu_data;
  325. struct jtag_port *jtag = &fpga->jtag;
  326. char fpgaid = thr->device_thread;
  327. uint32_t nonce;
  328. long iter;
  329. bool bad;
  330. iter = 200;
  331. while (1) {
  332. mutex_lock(&x6500->device_mutex);
  333. nonce = x6500_get_register(jtag, 0xE);
  334. mutex_unlock(&x6500->device_mutex);
  335. if (nonce != 0xffffffff) {
  336. bad = !test_nonce(work, nonce, false);
  337. if (!bad) {
  338. submit_nonce(thr, work, nonce);
  339. applog(LOG_DEBUG, "%s %u.%u: Nonce for current work: %08lx",
  340. x6500->api->name, x6500->device_id, fpgaid,
  341. (unsigned long)nonce);
  342. } else {
  343. applog(LOG_DEBUG, "%s %u.%u: Nonce with H not zero : %08lx",
  344. x6500->api->name, x6500->device_id, fpgaid,
  345. (unsigned long)nonce);
  346. ++hw_errors;
  347. ++x6500->hw_errors;
  348. }
  349. }
  350. if (thr->work_restart || !--iter)
  351. break;
  352. usleep(1000);
  353. if (thr->work_restart)
  354. break;
  355. }
  356. return 10000000;
  357. }
  358. static int64_t
  359. x6500_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  360. {
  361. if (!x6500_start_work(thr, work))
  362. return -1;
  363. int64_t hashes = x6500_process_results(thr, work);
  364. if (hashes > 0)
  365. work->blk.nonce += hashes;
  366. return hashes;
  367. }
  368. struct device_api x6500_api = {
  369. .dname = "x6500",
  370. .name = "XBS",
  371. .api_detect = x6500_detect,
  372. .thread_prepare = x6500_prepare,
  373. .thread_init = x6500_fpga_init,
  374. .get_statline_before = get_x6500_statline_before,
  375. .scanhash = x6500_scanhash,
  376. // .thread_shutdown = x6500_fpga_shutdown,
  377. };