driver-x6500.c 18 KB

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