driver-x6500.c 12 KB

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