driver-x6500.c 18 KB

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