driver-x6500.c 19 KB

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