driver-x6500.c 17 KB

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