driver-x6500.c 20 KB

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