driver-x6500.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. #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 = 1;
  117. x6500->procs = 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. struct cgpu_info *x6500 = thr->cgpu;
  138. if (x6500->proc_id)
  139. return true;
  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. for (struct cgpu_info *slave = x6500->next_proc; slave; slave = slave->next_proc)
  156. {
  157. slave->device_ft232r = x6500->device_ft232r;
  158. slave->cgpu_data = x6500->cgpu_data;
  159. }
  160. return true;
  161. }
  162. struct x6500_fpga_data {
  163. struct jtag_port jtag;
  164. struct timeval tv_hashstart;
  165. struct dclk_data dclk;
  166. uint8_t freqMaxMaxM;
  167. // Time the clock was last reduced due to temperature
  168. time_t last_cutoff_reduced;
  169. float temp;
  170. uint32_t prepwork_last_register;
  171. };
  172. #define bailout2(...) do { \
  173. applog(__VA_ARGS__); \
  174. return false; \
  175. } while(0)
  176. static bool
  177. x6500_fpga_upload_bitstream(struct cgpu_info *x6500, struct jtag_port *jp1)
  178. {
  179. char buf[0x100];
  180. unsigned long len, flen;
  181. unsigned char *pdone = (unsigned char*)x6500->cgpu_data - 1;
  182. struct ft232r_device_handle *ftdi = jp1->a->ftdi;
  183. FILE *f = open_xilinx_bitstream(x6500->api->dname, x6500->dev_repr, X6500_BITSTREAM_FILENAME, &len);
  184. if (!f)
  185. return false;
  186. flen = len;
  187. applog(LOG_WARNING, "%s: Programming %s...",
  188. x6500->dev_repr, x6500->device_path);
  189. x6500->status = LIFE_INIT;
  190. // "Magic" jtag_port configured to access both FPGAs concurrently
  191. struct jtag_port jpt = {
  192. .a = jp1->a,
  193. };
  194. struct jtag_port *jp = &jpt;
  195. uint8_t i, j;
  196. x6500_jtag_set(jp, 0x11);
  197. // Need to reset here despite previous FPGA state, since we are programming all at once
  198. jtag_reset(jp);
  199. jtag_write(jp, JTAG_REG_IR, "\xd0", 6); // JPROGRAM
  200. // Poll each FPGA status individually since they might not be ready at the same time
  201. for (j = 0; j < 2; ++j) {
  202. x6500_jtag_set(jp, j ? 0x10 : 1);
  203. do {
  204. i = 0xd0; // Re-set JPROGRAM while reading status
  205. jtag_read(jp, JTAG_REG_IR, &i, 6);
  206. } while (i & 8);
  207. applog(LOG_DEBUG, "%s%c: JPROGRAM ready",
  208. x6500->dev_repr, 'a' + j);
  209. }
  210. x6500_jtag_set(jp, 0x11);
  211. jtag_write(jp, JTAG_REG_IR, "\xa0", 6); // CFG_IN
  212. sleep(1);
  213. if (fread(buf, 32, 1, f) != 1)
  214. bailout2(LOG_ERR, "%s: File underrun programming %s (%lu bytes left)", x6500->dev_repr, x6500->device_path, len);
  215. jtag_swrite(jp, JTAG_REG_DR, buf, 256);
  216. len -= 32;
  217. // Put ft232r chip in asynchronous bitbang mode so we don't need to read back tdo
  218. // This takes upload time down from about an hour to about 3 minutes
  219. if (!ft232r_set_bitmode(ftdi, 0xee, 1))
  220. return false;
  221. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  222. return false;
  223. jp->a->bufread = 0;
  224. jp->a->async = true;
  225. ssize_t buflen;
  226. char nextstatus = 25;
  227. while (len) {
  228. buflen = len < 32 ? len : 32;
  229. if (fread(buf, buflen, 1, f) != 1)
  230. bailout2(LOG_ERR, "%s: File underrun programming %s (%lu bytes left)", x6500->dev_repr, x6500->device_path, len);
  231. jtag_swrite_more(jp, buf, buflen * 8, len == (unsigned long)buflen);
  232. *pdone = 100 - ((len * 100) / flen);
  233. if (*pdone >= nextstatus)
  234. {
  235. nextstatus += 25;
  236. applog(LOG_WARNING, "%s: Programming %s... %d%% complete...", x6500->dev_repr, x6500->device_path, *pdone);
  237. }
  238. len -= buflen;
  239. }
  240. // Switch back to synchronous bitbang mode
  241. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  242. return false;
  243. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  244. return false;
  245. jp->a->bufread = 0;
  246. jp->a->async = false;
  247. jp->a->bufread = 0;
  248. jtag_write(jp, JTAG_REG_IR, "\x30", 6); // JSTART
  249. for (i=0; i<16; ++i)
  250. jtag_run(jp);
  251. i = 0xff; // BYPASS
  252. jtag_read(jp, JTAG_REG_IR, &i, 6);
  253. if (!(i & 4))
  254. return false;
  255. applog(LOG_WARNING, "%s: Done programming %s", x6500->dev_repr, x6500->device_path);
  256. *pdone = 101;
  257. return true;
  258. }
  259. static bool x6500_change_clock(struct thr_info *thr, int multiplier)
  260. {
  261. struct x6500_fpga_data *fpga = thr->cgpu_data;
  262. struct jtag_port *jp = &fpga->jtag;
  263. x6500_set_register(jp, 0xD, multiplier * 2);
  264. ft232r_flush(jp->a->ftdi);
  265. fpga->dclk.freqM = multiplier;
  266. return true;
  267. }
  268. static bool x6500_dclk_change_clock(struct thr_info *thr, int multiplier)
  269. {
  270. struct cgpu_info *x6500 = thr->cgpu;
  271. pthread_mutex_t *mutexp = &x6500->device->device_mutex;
  272. struct x6500_fpga_data *fpga = thr->cgpu_data;
  273. uint8_t oldFreq = fpga->dclk.freqM;
  274. mutex_lock(mutexp);
  275. if (!x6500_change_clock(thr, multiplier)) {
  276. mutex_unlock(mutexp);
  277. return false;
  278. }
  279. mutex_unlock(mutexp);
  280. dclk_msg_freqchange(x6500->proc_repr, oldFreq * 2, fpga->dclk.freqM * 2, NULL);
  281. return true;
  282. }
  283. static bool x6500_thread_init(struct thr_info *thr)
  284. {
  285. struct cgpu_info *x6500 = thr->cgpu;
  286. pthread_mutex_t *mutexp = &x6500->device->device_mutex;
  287. struct ft232r_device_handle *ftdi = x6500->device_ft232r;
  288. for ( ; x6500; x6500 = x6500->next_proc)
  289. {
  290. thr = x6500->thr[0];
  291. struct x6500_fpga_data *fpga;
  292. struct jtag_port *jp;
  293. int fpgaid = x6500->proc_id;
  294. uint8_t pinoffset = fpgaid ? 0x10 : 1;
  295. unsigned char buf[4] = {0};
  296. int i;
  297. if (!ftdi)
  298. return false;
  299. fpga = calloc(1, sizeof(*fpga));
  300. jp = &fpga->jtag;
  301. jp->a = x6500->cgpu_data;
  302. x6500_jtag_set(jp, pinoffset);
  303. thr->cgpu_data = fpga;
  304. mutex_lock(mutexp);
  305. if (!jtag_reset(jp)) {
  306. mutex_unlock(mutexp);
  307. applog(LOG_ERR, "%s: JTAG reset failed",
  308. x6500->dev_repr);
  309. return false;
  310. }
  311. i = jtag_detect(jp);
  312. if (i != 1) {
  313. mutex_unlock(mutexp);
  314. applog(LOG_ERR, "%s: JTAG detect returned %d",
  315. x6500->dev_repr, i);
  316. return false;
  317. }
  318. if (!(1
  319. && jtag_write(jp, JTAG_REG_IR, "\x10", 6)
  320. && jtag_read (jp, JTAG_REG_DR, buf, 32)
  321. && jtag_reset(jp)
  322. )) {
  323. mutex_unlock(mutexp);
  324. applog(LOG_ERR, "%s: JTAG error reading user code",
  325. x6500->dev_repr);
  326. return false;
  327. }
  328. if (memcmp(buf, X6500_BITSTREAM_USERID, 4)) {
  329. applog(LOG_ERR, "%"PRIprepr": FPGA not programmed",
  330. x6500->proc_repr);
  331. if (!x6500_fpga_upload_bitstream(x6500, jp))
  332. return false;
  333. } else if (opt_force_dev_init && x6500->status == LIFE_INIT) {
  334. applog(LOG_DEBUG, "%"PRIprepr": FPGA is already programmed, but --force-dev-init is set",
  335. x6500->proc_repr);
  336. if (!x6500_fpga_upload_bitstream(x6500, jp))
  337. return false;
  338. } else
  339. applog(LOG_DEBUG, "%s"PRIprepr": FPGA is already programmed :)",
  340. x6500->proc_repr);
  341. dclk_prepare(&fpga->dclk);
  342. x6500_change_clock(thr, X6500_DEFAULT_CLOCK / 2);
  343. for (i = 0; 0xffffffff != x6500_get_register(jp, 0xE); ++i)
  344. {}
  345. mutex_unlock(mutexp);
  346. if (i)
  347. applog(LOG_WARNING, "%"PRIprepr": Flushed %d nonces from buffer at init",
  348. x6500->proc_repr, i);
  349. fpga->dclk.minGoodSamples = 3;
  350. fpga->freqMaxMaxM =
  351. fpga->dclk.freqMaxM = X6500_MAXIMUM_CLOCK / 2;
  352. fpga->dclk.freqMDefault = fpga->dclk.freqM;
  353. applog(LOG_WARNING, "%"PRIprepr": Frequency set to %u MHz (range: %u-%u)",
  354. x6500->proc_repr,
  355. fpga->dclk.freqM * 2,
  356. X6500_MINIMUM_CLOCK,
  357. fpga->dclk.freqMaxM * 2);
  358. }
  359. return true;
  360. }
  361. static
  362. void x6500_get_temperature(struct cgpu_info *x6500)
  363. {
  364. struct x6500_fpga_data *fpga = x6500->thr[0]->cgpu_data;
  365. struct jtag_port *jp = &fpga->jtag;
  366. struct ft232r_device_handle *ftdi = jp->a->ftdi;
  367. int i, code[2];
  368. bool sio[2];
  369. code[0] = 0;
  370. code[1] = 0;
  371. ft232r_flush(ftdi);
  372. if (!(ft232r_set_cbus_bits(ftdi, false, true))) return;
  373. if (!(ft232r_set_cbus_bits(ftdi, true, true))) return;
  374. if (!(ft232r_set_cbus_bits(ftdi, false, true))) return;
  375. if (!(ft232r_set_cbus_bits(ftdi, true, true))) return;
  376. if (!(ft232r_set_cbus_bits(ftdi, false, false))) return;
  377. for (i = 16; i--; ) {
  378. if (ft232r_set_cbus_bits(ftdi, true, false)) {
  379. if (!(ft232r_get_cbus_bits(ftdi, &sio[0], &sio[1]))) {
  380. return;
  381. }
  382. } else {
  383. return;
  384. }
  385. code[0] |= sio[0] << i;
  386. code[1] |= sio[1] << i;
  387. if (!ft232r_set_cbus_bits(ftdi, false, false)) {
  388. return;
  389. }
  390. }
  391. if (!(ft232r_set_cbus_bits(ftdi, false, true))) {
  392. return;
  393. }
  394. if (!(ft232r_set_cbus_bits(ftdi, true, true))) {
  395. return;
  396. }
  397. if (!(ft232r_set_cbus_bits(ftdi, false, true))) {
  398. return;
  399. }
  400. if (!ft232r_set_bitmode(ftdi, 0xee, 4)) {
  401. return;
  402. }
  403. ft232r_purge_buffers(jp->a->ftdi, FTDI_PURGE_BOTH);
  404. jp->a->bufread = 0;
  405. x6500 = x6500->device;
  406. for (i = 0; i < 2; ++i, x6500 = x6500->next_proc) {
  407. struct thr_info *thr = x6500->thr[0];
  408. fpga = thr->cgpu_data;
  409. if (!fpga) continue;
  410. if (code[i] == 0xffff || !code[i]) {
  411. fpga->temp = 0;
  412. continue;
  413. }
  414. if ((code[i] >> 15) & 1)
  415. code[i] -= 0x10000;
  416. fpga->temp = (float)(code[i] >> 2) * 0.03125f;
  417. applog(LOG_DEBUG,"x6500_get_temperature: fpga[%d]->temp=%.1fC",i,fpga->temp);
  418. int temperature = round(fpga->temp);
  419. if (temperature > x6500->targettemp + opt_hysteresis) {
  420. time_t now = time(NULL);
  421. if (fpga->last_cutoff_reduced != now) {
  422. fpga->last_cutoff_reduced = now;
  423. int oldFreq = fpga->dclk.freqM;
  424. if (x6500_change_clock(thr, oldFreq - 1))
  425. applog(LOG_NOTICE, "%"PRIprepr": Frequency dropped from %u to %u MHz (temp: %.1fC)",
  426. x6500->proc_repr,
  427. oldFreq * 2, fpga->dclk.freqM * 2,
  428. fpga->temp
  429. );
  430. fpga->dclk.freqMaxM = fpga->dclk.freqM;
  431. }
  432. }
  433. else
  434. if (fpga->dclk.freqMaxM < fpga->freqMaxMaxM && temperature < x6500->targettemp) {
  435. if (temperature < x6500->targettemp - opt_hysteresis) {
  436. fpga->dclk.freqMaxM = fpga->freqMaxMaxM;
  437. } else if (fpga->dclk.freqM == fpga->dclk.freqMaxM) {
  438. ++fpga->dclk.freqMaxM;
  439. }
  440. }
  441. }
  442. }
  443. static bool x6500_get_stats(struct cgpu_info *x6500)
  444. {
  445. float hottest = 0;
  446. if (x6500->deven != DEV_ENABLED) {
  447. // Getting temperature more efficiently while enabled
  448. // NOTE: Don't need to mess with mutex here, since the device is disabled
  449. x6500_get_temperature(x6500);
  450. }
  451. for (int i = x6500->threads; i--; ) {
  452. struct thr_info *thr = x6500->thr[i];
  453. struct x6500_fpga_data *fpga = thr->cgpu_data;
  454. if (!fpga)
  455. continue;
  456. float temp = fpga->temp;
  457. if (temp > hottest)
  458. hottest = temp;
  459. }
  460. x6500->temp = hottest;
  461. return true;
  462. }
  463. static
  464. bool get_x6500_upload_percent(char *buf, struct cgpu_info *x6500)
  465. {
  466. char info[18] = " | ";
  467. unsigned char pdone = *((unsigned char*)x6500->cgpu_data - 1);
  468. if (pdone != 101) {
  469. sprintf(&info[1], "%3d%%", pdone);
  470. info[5] = ' ';
  471. strcat(buf, info);
  472. return true;
  473. }
  474. return false;
  475. }
  476. static
  477. void get_x6500_statline_before(char *buf, struct cgpu_info *x6500)
  478. {
  479. if (get_x6500_upload_percent(buf, x6500))
  480. return;
  481. char info[18] = " | ";
  482. struct x6500_fpga_data *fpga = x6500->thr[0]->cgpu_data;
  483. if (fpga->temp) {
  484. sprintf(&info[1], "%.1fC", fpga->temp);
  485. info[strlen(info)] = ' ';
  486. strcat(buf, info);
  487. return;
  488. }
  489. strcat(buf, " | ");
  490. }
  491. static
  492. void get_x6500_dev_statline_before(char *buf, struct cgpu_info *x6500)
  493. {
  494. if (get_x6500_upload_percent(buf, x6500))
  495. return;
  496. char info[18] = " | ";
  497. struct x6500_fpga_data *fpga0 = x6500->thr[0]->cgpu_data;
  498. struct x6500_fpga_data *fpga1 = x6500->next_proc->thr[0]->cgpu_data;
  499. if (x6500->temp) {
  500. sprintf(&info[1], "%.1fC/%.1fC", fpga0->temp, fpga1->temp);
  501. info[strlen(info)] = ' ';
  502. strcat(buf, info);
  503. return;
  504. }
  505. strcat(buf, " | ");
  506. }
  507. static struct api_data*
  508. get_x6500_api_extra_device_status(struct cgpu_info *x6500)
  509. {
  510. struct api_data *root = NULL;
  511. struct thr_info *thr = x6500->thr[0];
  512. struct x6500_fpga_data *fpga = thr->cgpu_data;
  513. double d;
  514. if (fpga->temp)
  515. root = api_add_temp(root, "Temperature", &fpga->temp, true);
  516. d = (double)fpga->dclk.freqM * 2 * 1000000.;
  517. root = api_add_freq(root, "Frequency", &d, true);
  518. d = (double)fpga->dclk.freqMaxM * 2 * 1000000.;
  519. root = api_add_freq(root, "Cool Max Frequency", &d, true);
  520. d = (double)fpga->freqMaxMaxM * 2 * 1000000.;
  521. root = api_add_freq(root, "Max Frequency", &d, true);
  522. return root;
  523. }
  524. static
  525. bool x6500_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  526. {
  527. struct cgpu_info *x6500 = thr->cgpu;
  528. pthread_mutex_t *mutexp = &x6500->device->device_mutex;
  529. struct x6500_fpga_data *fpga = thr->cgpu_data;
  530. struct jtag_port *jp = &fpga->jtag;
  531. mutex_lock(mutexp);
  532. for (int i = 1, j = 0; i < 9; ++i, j += 4)
  533. x6500_set_register(jp, i, fromlebytes(work->midstate, j));
  534. for (int i = 9, j = 64; i < 11; ++i, j += 4)
  535. x6500_set_register(jp, i, fromlebytes(work->data, j));
  536. x6500_get_temperature(x6500);
  537. mutex_unlock(mutexp);
  538. ft232r_flush(jp->a->ftdi);
  539. fpga->prepwork_last_register = fromlebytes(work->data, 72);
  540. work->blk.nonce = 0xffffffff;
  541. return true;
  542. }
  543. static
  544. void x6500_job_start(struct thr_info *thr)
  545. {
  546. struct cgpu_info *x6500 = thr->cgpu;
  547. pthread_mutex_t *mutexp = &x6500->device->device_mutex;
  548. struct x6500_fpga_data *fpga = thr->cgpu_data;
  549. struct jtag_port *jp = &fpga->jtag;
  550. struct timeval tv_now;
  551. if (thr->prev_work)
  552. {
  553. dclk_preUpdate(&fpga->dclk);
  554. dclk_updateFreq(&fpga->dclk, x6500_dclk_change_clock, thr);
  555. }
  556. mutex_lock(mutexp);
  557. x6500_set_register(jp, 11, fpga->prepwork_last_register);
  558. ft232r_flush(jp->a->ftdi);
  559. gettimeofday(&tv_now, NULL);
  560. if (!thr->prev_work)
  561. fpga->tv_hashstart = tv_now;
  562. mutex_unlock(mutexp);
  563. if (opt_debug) {
  564. char *xdata = bin2hex(thr->next_work->data, 80);
  565. applog(LOG_DEBUG, "%"PRIprepr": Started work: %s",
  566. x6500->proc_repr, xdata);
  567. free(xdata);
  568. }
  569. uint32_t usecs = 0x80000000 / fpga->dclk.freqM;
  570. usecs -= 1000000;
  571. timer_set_delay(&thr->tv_morework, &tv_now, usecs);
  572. timer_set_delay(&thr->tv_poll, &tv_now, 10000);
  573. }
  574. static
  575. int64_t calc_hashes(struct thr_info *thr, struct timeval *tv_now)
  576. {
  577. struct x6500_fpga_data *fpga = thr->cgpu_data;
  578. struct timeval tv_delta;
  579. int64_t hashes;
  580. timersub(tv_now, &fpga->tv_hashstart, &tv_delta);
  581. hashes = (((int64_t)tv_delta.tv_sec * 1000000) + tv_delta.tv_usec) * fpga->dclk.freqM * 2;
  582. if (unlikely(hashes > 0x100000000))
  583. hashes = 0x100000000;
  584. hashes_done(thr, hashes, &tv_delta, NULL);
  585. fpga->tv_hashstart = *tv_now;
  586. return hashes;
  587. }
  588. static
  589. int64_t x6500_process_results(struct thr_info *thr, struct work *work)
  590. {
  591. struct cgpu_info *x6500 = thr->cgpu;
  592. pthread_mutex_t *mutexp = &x6500->device->device_mutex;
  593. struct x6500_fpga_data *fpga = thr->cgpu_data;
  594. struct jtag_port *jtag = &fpga->jtag;
  595. struct timeval tv_now;
  596. int64_t hashes;
  597. uint32_t nonce;
  598. bool bad;
  599. while (1) {
  600. mutex_lock(mutexp);
  601. gettimeofday(&tv_now, NULL);
  602. nonce = x6500_get_register(jtag, 0xE);
  603. mutex_unlock(mutexp);
  604. if (nonce != 0xffffffff) {
  605. bad = !test_nonce(work, nonce, false);
  606. if (!bad) {
  607. submit_nonce(thr, work, nonce);
  608. applog(LOG_DEBUG, "%"PRIprepr": Nonce for current work: %08lx",
  609. x6500->proc_repr,
  610. (unsigned long)nonce);
  611. dclk_gotNonces(&fpga->dclk);
  612. } else if (likely(thr->prev_work) && test_nonce(thr->prev_work, nonce, false)) {
  613. submit_nonce(thr, thr->prev_work, nonce);
  614. applog(LOG_DEBUG, "%"PRIprepr": Nonce for PREVIOUS work: %08lx",
  615. x6500->proc_repr,
  616. (unsigned long)nonce);
  617. } else {
  618. applog(LOG_DEBUG, "%"PRIprepr": Nonce with H not zero : %08lx",
  619. x6500->proc_repr,
  620. (unsigned long)nonce);
  621. ++hw_errors;
  622. ++x6500->hw_errors;
  623. dclk_gotNonces(&fpga->dclk);
  624. dclk_errorCount(&fpga->dclk, 1.);
  625. }
  626. // Keep reading nonce buffer until it's empty
  627. // This is necessary to avoid getting hw errors from Freq B after we've moved on to Freq A
  628. continue;
  629. }
  630. hashes = calc_hashes(thr, &tv_now);
  631. break;
  632. }
  633. return hashes;
  634. }
  635. static
  636. void x6500_fpga_poll(struct thr_info *thr)
  637. {
  638. x6500_process_results(thr, thr->work);
  639. timer_set_delay_from_now(&thr->tv_poll, 10000);
  640. }
  641. struct device_api x6500_api = {
  642. .dname = "x6500",
  643. .name = "XBS",
  644. .api_detect = x6500_detect,
  645. .get_dev_statline_before = get_x6500_dev_statline_before,
  646. .thread_prepare = x6500_prepare,
  647. .thread_init = x6500_thread_init,
  648. .get_stats = x6500_get_stats,
  649. .get_statline_before = get_x6500_statline_before,
  650. .get_api_extra_device_status = get_x6500_api_extra_device_status,
  651. .poll = x6500_fpga_poll,
  652. .minerloop = minerloop_async,
  653. .job_prepare = x6500_job_prepare,
  654. .job_start = x6500_job_start,
  655. // .thread_shutdown = x6500_fpga_shutdown,
  656. };