driver-x6500.c 14 KB

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