driver-x6500.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. * Copyright 2012 Andrew Smith
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #ifdef WIN32
  12. #include <winsock2.h>
  13. #endif
  14. #include <limits.h>
  15. #include <math.h>
  16. #include <sys/time.h>
  17. #include <libusb.h>
  18. #include "compat.h"
  19. #include "deviceapi.h"
  20. #include "dynclock.h"
  21. #include "jtag.h"
  22. #include "logging.h"
  23. #include "miner.h"
  24. #include "fpgautils.h"
  25. #include "ft232r.h"
  26. #define X6500_USB_PRODUCT "X6500 FPGA Miner"
  27. #define X6500_BITSTREAM_FILENAME "fpgaminer_x6500-overclocker-0402.bit"
  28. // NOTE: X6500_BITSTREAM_USERID is bitflipped
  29. #define X6500_BITSTREAM_USERID "\x40\x20\x24\x42"
  30. #define X6500_MINIMUM_CLOCK 2
  31. #define X6500_DEFAULT_CLOCK 190
  32. #define X6500_MAXIMUM_CLOCK 250
  33. struct device_drv x6500_api;
  34. #define fromlebytes(ca, j) (ca[j] | (((uint16_t)ca[j+1])<<8) | (((uint32_t)ca[j+2])<<16) | (((uint32_t)ca[j+3])<<24))
  35. static
  36. void int2bits(uint32_t n, uint8_t *b, uint8_t bits)
  37. {
  38. uint8_t i;
  39. for (i = (bits + 7) / 8; i > 0; )
  40. b[--i] = 0;
  41. for (i = 0; i < bits; ++i) {
  42. if (n & 1)
  43. b[i/8] |= 0x80 >> (i % 8);
  44. n >>= 1;
  45. }
  46. }
  47. static
  48. uint32_t bits2int(uint8_t *b, uint8_t bits)
  49. {
  50. uint32_t n, i;
  51. n = 0;
  52. for (i = 0; i < bits; ++i)
  53. if (b[i/8] & (0x80 >> (i % 8)))
  54. n |= 1<<i;
  55. return n;
  56. }
  57. static
  58. void checksum(uint8_t *b, uint8_t bits)
  59. {
  60. uint8_t i;
  61. uint8_t checksum = 1;
  62. for(i = 0; i < bits; ++i)
  63. checksum ^= (b[i/8] & (0x80 >> (i % 8))) ? 1 : 0;
  64. if (checksum)
  65. b[i/8] |= 0x80 >> (i % 8);
  66. }
  67. static
  68. void x6500_jtag_set(struct jtag_port *jp, uint8_t pinoffset)
  69. {
  70. jp->tck = pinoffset << 3;
  71. jp->tms = pinoffset << 2;
  72. jp->tdi = pinoffset << 1;
  73. jp->tdo = pinoffset << 0;
  74. jp->ignored = ~(jp->tdo | jp->tdi | jp->tms | jp->tck);
  75. }
  76. static uint32_t x6500_get_register(struct jtag_port *jp, uint8_t addr);
  77. static
  78. void x6500_set_register(struct jtag_port *jp, uint8_t addr, uint32_t nv)
  79. {
  80. uint8_t buf[38];
  81. retry:
  82. jtag_write(jp, JTAG_REG_IR, "\x40", 6);
  83. int2bits(nv, &buf[0], 32);
  84. int2bits(addr, &buf[4], 4);
  85. buf[4] |= 8;
  86. checksum(buf, 37);
  87. jtag_write(jp, JTAG_REG_DR, buf, 38);
  88. jtag_run(jp);
  89. #ifdef DEBUG_X6500_SET_REGISTER
  90. if (x6500_get_register(jp, addr) != nv)
  91. #else
  92. if (0)
  93. #endif
  94. {
  95. applog(LOG_WARNING, "x6500_set_register failed %x=%08x", addr, nv);
  96. goto retry;
  97. }
  98. }
  99. static
  100. uint32_t x6500_get_register(struct jtag_port *jp, uint8_t addr)
  101. {
  102. uint8_t buf[4] = {0};
  103. jtag_write(jp, JTAG_REG_IR, "\x40", 6);
  104. int2bits(addr, &buf[0], 4);
  105. checksum(buf, 5);
  106. jtag_write(jp, JTAG_REG_DR, buf, 6);
  107. jtag_read (jp, JTAG_REG_DR, buf, 32);
  108. jtag_reset(jp);
  109. return bits2int(buf, 32);
  110. }
  111. static bool x6500_foundusb(libusb_device *dev, const char *product, const char *serial)
  112. {
  113. if (bfg_claim_libusb(&x6500_api, true, dev))
  114. return false;
  115. struct cgpu_info *x6500;
  116. x6500 = calloc(1, sizeof(*x6500));
  117. x6500->drv = &x6500_api;
  118. mutex_init(&x6500->device_mutex);
  119. x6500->device_path = strdup(serial);
  120. x6500->deven = DEV_ENABLED;
  121. x6500->threads = 1;
  122. x6500->procs = 2;
  123. x6500->name = strdup(product);
  124. x6500->cutofftemp = 85;
  125. x6500->device_data = dev;
  126. cgpu_copy_libusb_strings(x6500, dev);
  127. return add_cgpu(x6500);
  128. }
  129. static bool x6500_detect_one(const char *serial)
  130. {
  131. return ft232r_detect(X6500_USB_PRODUCT, serial, x6500_foundusb);
  132. }
  133. static int x6500_detect_auto()
  134. {
  135. return ft232r_detect(X6500_USB_PRODUCT, NULL, x6500_foundusb);
  136. }
  137. static void x6500_detect()
  138. {
  139. serial_detect_auto(&x6500_api, x6500_detect_one, x6500_detect_auto);
  140. }
  141. static bool x6500_prepare(struct thr_info *thr)
  142. {
  143. struct cgpu_info *x6500 = thr->cgpu;
  144. if (x6500->proc_id)
  145. return true;
  146. struct ft232r_device_handle *ftdi = ft232r_open(x6500->device_data);
  147. x6500->device_ft232r = NULL;
  148. if (!ftdi)
  149. return false;
  150. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  151. return false;
  152. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  153. return false;
  154. x6500->device_ft232r = ftdi;
  155. struct jtag_port_a *jtag_a;
  156. unsigned char *pdone = calloc(1, sizeof(*jtag_a) + 1);
  157. *pdone = 101;
  158. jtag_a = (void*)(pdone + 1);
  159. jtag_a->ftdi = ftdi;
  160. x6500->device_data = jtag_a;
  161. for (struct cgpu_info *slave = x6500->next_proc; slave; slave = slave->next_proc)
  162. {
  163. slave->device_ft232r = x6500->device_ft232r;
  164. slave->device_data = x6500->device_data;
  165. }
  166. return true;
  167. }
  168. struct x6500_fpga_data {
  169. struct jtag_port jtag;
  170. struct timeval tv_hashstart;
  171. int64_t hashes_left;
  172. struct dclk_data dclk;
  173. uint8_t freqMaxMaxM;
  174. // Time the clock was last reduced due to temperature
  175. struct timeval tv_last_cutoff_reduced;
  176. uint32_t prepwork_last_register;
  177. };
  178. #define bailout2(...) do { \
  179. applog(__VA_ARGS__); \
  180. return false; \
  181. } while(0)
  182. static bool
  183. x6500_fpga_upload_bitstream(struct cgpu_info *x6500, struct jtag_port *jp1)
  184. {
  185. char buf[0x100];
  186. unsigned long len, flen;
  187. unsigned char *pdone = (unsigned char*)x6500->device_data - 1;
  188. struct ft232r_device_handle *ftdi = jp1->a->ftdi;
  189. FILE *f = open_xilinx_bitstream(x6500->drv->dname, x6500->dev_repr, X6500_BITSTREAM_FILENAME, &len);
  190. if (!f)
  191. return false;
  192. flen = len;
  193. applog(LOG_WARNING, "%s: Programming %s...",
  194. x6500->dev_repr, x6500->device_path);
  195. x6500->status = LIFE_INIT2;
  196. // "Magic" jtag_port configured to access both FPGAs concurrently
  197. struct jtag_port jpt = {
  198. .a = jp1->a,
  199. };
  200. struct jtag_port *jp = &jpt;
  201. uint8_t i, j;
  202. x6500_jtag_set(jp, 0x11);
  203. // Need to reset here despite previous FPGA state, since we are programming all at once
  204. jtag_reset(jp);
  205. jtag_write(jp, JTAG_REG_IR, "\xd0", 6); // JPROGRAM
  206. // Poll each FPGA status individually since they might not be ready at the same time
  207. for (j = 0; j < 2; ++j) {
  208. x6500_jtag_set(jp, j ? 0x10 : 1);
  209. do {
  210. i = 0xd0; // Re-set JPROGRAM while reading status
  211. jtag_read(jp, JTAG_REG_IR, &i, 6);
  212. } while (i & 8);
  213. applog(LOG_DEBUG, "%s%c: JPROGRAM ready",
  214. x6500->dev_repr, 'a' + j);
  215. }
  216. x6500_jtag_set(jp, 0x11);
  217. jtag_write(jp, JTAG_REG_IR, "\xa0", 6); // CFG_IN
  218. cgsleep_ms(1000);
  219. if (fread(buf, 32, 1, f) != 1)
  220. bailout2(LOG_ERR, "%s: File underrun programming %s (%lu bytes left)", x6500->dev_repr, x6500->device_path, len);
  221. jtag_swrite(jp, JTAG_REG_DR, buf, 256);
  222. len -= 32;
  223. // Put ft232r chip in asynchronous bitbang mode so we don't need to read back tdo
  224. // This takes upload time down from about an hour to about 3 minutes
  225. if (!ft232r_set_bitmode(ftdi, 0xee, 1))
  226. return false;
  227. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  228. return false;
  229. jp->a->bufread = 0;
  230. jp->a->async = true;
  231. ssize_t buflen;
  232. char nextstatus = 25;
  233. while (len) {
  234. buflen = len < 32 ? len : 32;
  235. if (fread(buf, buflen, 1, f) != 1)
  236. bailout2(LOG_ERR, "%s: File underrun programming %s (%lu bytes left)", x6500->dev_repr, x6500->device_path, len);
  237. jtag_swrite_more(jp, buf, buflen * 8, len == (unsigned long)buflen);
  238. *pdone = 100 - ((len * 100) / flen);
  239. if (*pdone >= nextstatus)
  240. {
  241. nextstatus += 25;
  242. applog(LOG_WARNING, "%s: Programming %s... %d%% complete...", x6500->dev_repr, x6500->device_path, *pdone);
  243. }
  244. len -= buflen;
  245. }
  246. // Switch back to synchronous bitbang mode
  247. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  248. return false;
  249. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  250. return false;
  251. jp->a->bufread = 0;
  252. jp->a->async = false;
  253. jp->a->bufread = 0;
  254. jtag_write(jp, JTAG_REG_IR, "\x30", 6); // JSTART
  255. for (i=0; i<16; ++i)
  256. jtag_run(jp);
  257. i = 0xff; // BYPASS
  258. jtag_read(jp, JTAG_REG_IR, &i, 6);
  259. if (!(i & 4))
  260. return false;
  261. applog(LOG_WARNING, "%s: Done programming %s", x6500->dev_repr, x6500->device_path);
  262. *pdone = 101;
  263. return true;
  264. }
  265. static bool x6500_change_clock(struct thr_info *thr, int multiplier)
  266. {
  267. struct x6500_fpga_data *fpga = thr->cgpu_data;
  268. struct jtag_port *jp = &fpga->jtag;
  269. x6500_set_register(jp, 0xD, multiplier * 2);
  270. ft232r_flush(jp->a->ftdi);
  271. fpga->dclk.freqM = multiplier;
  272. return true;
  273. }
  274. static bool x6500_dclk_change_clock(struct thr_info *thr, int multiplier)
  275. {
  276. struct cgpu_info *x6500 = thr->cgpu;
  277. struct x6500_fpga_data *fpga = thr->cgpu_data;
  278. uint8_t oldFreq = fpga->dclk.freqM;
  279. if (!x6500_change_clock(thr, multiplier)) {
  280. return false;
  281. }
  282. dclk_msg_freqchange(x6500->proc_repr, oldFreq * 2, fpga->dclk.freqM * 2, NULL);
  283. return true;
  284. }
  285. static bool x6500_thread_init(struct thr_info *thr)
  286. {
  287. struct cgpu_info *x6500 = thr->cgpu;
  288. struct ft232r_device_handle *ftdi = x6500->device_ft232r;
  289. // Setup mutex request based on notifier and pthread cond
  290. notifier_init(thr->mutex_request);
  291. pthread_cond_init(&x6500->device_cond, NULL);
  292. // This works because x6500_thread_init is only called for the first processor now that they're all using the same thread
  293. for ( ; x6500; x6500 = x6500->next_proc)
  294. {
  295. thr = x6500->thr[0];
  296. struct x6500_fpga_data *fpga;
  297. struct jtag_port *jp;
  298. int fpgaid = x6500->proc_id;
  299. uint8_t pinoffset = fpgaid ? 0x10 : 1;
  300. unsigned char buf[4] = {0};
  301. int i;
  302. if (!ftdi)
  303. return false;
  304. fpga = calloc(1, sizeof(*fpga));
  305. jp = &fpga->jtag;
  306. jp->a = x6500->device_data;
  307. x6500_jtag_set(jp, pinoffset);
  308. thr->cgpu_data = fpga;
  309. x6500->status = LIFE_INIT2;
  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 == x6500->device) {
  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. fpga->dclk.freqMinM = X6500_MINIMUM_CLOCK / 2;
  345. x6500_change_clock(thr, X6500_DEFAULT_CLOCK / 2);
  346. for (i = 0; 0xffffffff != x6500_get_register(jp, 0xE); ++i)
  347. {}
  348. if (i)
  349. applog(LOG_WARNING, "%"PRIprepr": Flushed %d nonces from buffer at init",
  350. x6500->proc_repr, i);
  351. fpga->dclk.minGoodSamples = 3;
  352. fpga->freqMaxMaxM =
  353. fpga->dclk.freqMaxM = X6500_MAXIMUM_CLOCK / 2;
  354. fpga->dclk.freqMDefault = fpga->dclk.freqM;
  355. applog(LOG_WARNING, "%"PRIprepr": Frequency set to %u MHz (range: %u-%u)",
  356. x6500->proc_repr,
  357. fpga->dclk.freqM * 2,
  358. X6500_MINIMUM_CLOCK,
  359. fpga->dclk.freqMaxM * 2);
  360. }
  361. return true;
  362. }
  363. static
  364. void x6500_get_temperature(struct cgpu_info *x6500)
  365. {
  366. struct x6500_fpga_data *fpga = x6500->thr[0]->cgpu_data;
  367. struct jtag_port *jp = &fpga->jtag;
  368. struct ft232r_device_handle *ftdi = jp->a->ftdi;
  369. int i, code[2];
  370. bool sio[2];
  371. code[0] = 0;
  372. code[1] = 0;
  373. ft232r_flush(ftdi);
  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, true))) return;
  377. if (!(ft232r_set_cbus_bits(ftdi, true, true))) return;
  378. if (!(ft232r_set_cbus_bits(ftdi, false, false))) return;
  379. for (i = 16; i--; ) {
  380. if (ft232r_set_cbus_bits(ftdi, true, false)) {
  381. if (!(ft232r_get_cbus_bits(ftdi, &sio[0], &sio[1]))) {
  382. return;
  383. }
  384. } else {
  385. return;
  386. }
  387. code[0] |= sio[0] << i;
  388. code[1] |= sio[1] << i;
  389. if (!ft232r_set_cbus_bits(ftdi, false, false)) {
  390. return;
  391. }
  392. }
  393. if (!(ft232r_set_cbus_bits(ftdi, false, true))) {
  394. return;
  395. }
  396. if (!(ft232r_set_cbus_bits(ftdi, true, true))) {
  397. return;
  398. }
  399. if (!(ft232r_set_cbus_bits(ftdi, false, true))) {
  400. return;
  401. }
  402. if (!ft232r_set_bitmode(ftdi, 0xee, 4)) {
  403. return;
  404. }
  405. ft232r_purge_buffers(jp->a->ftdi, FTDI_PURGE_BOTH);
  406. jp->a->bufread = 0;
  407. x6500 = x6500->device;
  408. for (i = 0; i < 2; ++i, x6500 = x6500->next_proc) {
  409. struct thr_info *thr = x6500->thr[0];
  410. fpga = thr->cgpu_data;
  411. if (!fpga) continue;
  412. if (code[i] == 0xffff || !code[i]) {
  413. x6500->temp = 0;
  414. continue;
  415. }
  416. if ((code[i] >> 15) & 1)
  417. code[i] -= 0x10000;
  418. x6500->temp = (float)(code[i] >> 2) * 0.03125f;
  419. applog(LOG_DEBUG,"x6500_get_temperature: fpga[%d]->temp=%.1fC",
  420. i, x6500->temp);
  421. int temperature = round(x6500->temp);
  422. if (temperature > x6500->targettemp + opt_hysteresis) {
  423. struct timeval now;
  424. cgtime(&now);
  425. if (timer_elapsed(&fpga->tv_last_cutoff_reduced, &now)) {
  426. fpga->tv_last_cutoff_reduced = now;
  427. int oldFreq = fpga->dclk.freqM;
  428. if (x6500_change_clock(thr, oldFreq - 1))
  429. applog(LOG_NOTICE, "%"PRIprepr": Frequency dropped from %u to %u MHz (temp: %.1fC)",
  430. x6500->proc_repr,
  431. oldFreq * 2, fpga->dclk.freqM * 2,
  432. x6500->temp
  433. );
  434. fpga->dclk.freqMaxM = fpga->dclk.freqM;
  435. }
  436. }
  437. else
  438. if (fpga->dclk.freqMaxM < fpga->freqMaxMaxM && temperature < x6500->targettemp) {
  439. if (temperature < x6500->targettemp - opt_hysteresis) {
  440. fpga->dclk.freqMaxM = fpga->freqMaxMaxM;
  441. } else if (fpga->dclk.freqM == fpga->dclk.freqMaxM) {
  442. ++fpga->dclk.freqMaxM;
  443. }
  444. }
  445. }
  446. }
  447. static
  448. bool x6500_all_idle(struct cgpu_info *any_proc)
  449. {
  450. for (struct cgpu_info *proc = any_proc->device; proc; proc = proc->next_proc)
  451. if (proc->thr[0]->tv_poll.tv_sec != -1 || proc->deven == DEV_ENABLED)
  452. return false;
  453. return true;
  454. }
  455. static bool x6500_get_stats(struct cgpu_info *x6500)
  456. {
  457. if (x6500_all_idle(x6500)) {
  458. struct cgpu_info *cgpu = x6500->device;
  459. // Getting temperature more efficiently while running
  460. pthread_mutex_t *mutexp = &cgpu->device_mutex;
  461. mutex_lock(mutexp);
  462. notifier_wake(cgpu->thr[0]->mutex_request);
  463. pthread_cond_wait(&cgpu->device_cond, mutexp);
  464. x6500_get_temperature(x6500);
  465. pthread_cond_signal(&cgpu->device_cond);
  466. mutex_unlock(mutexp);
  467. }
  468. return true;
  469. }
  470. static
  471. bool get_x6500_upload_percent(char *buf, size_t bufsz, struct cgpu_info *x6500, __maybe_unused bool per_processor)
  472. {
  473. unsigned char pdone = *((unsigned char*)x6500->device_data - 1);
  474. if (pdone != 101) {
  475. tailsprintf(buf, bufsz, "%3d%% ", pdone);
  476. return true;
  477. }
  478. return false;
  479. }
  480. static struct api_data*
  481. get_x6500_api_extra_device_status(struct cgpu_info *x6500)
  482. {
  483. struct api_data *root = NULL;
  484. struct thr_info *thr = x6500->thr[0];
  485. struct x6500_fpga_data *fpga = thr->cgpu_data;
  486. double d;
  487. d = (double)fpga->dclk.freqM * 2;
  488. root = api_add_freq(root, "Frequency", &d, true);
  489. d = (double)fpga->dclk.freqMaxM * 2;
  490. root = api_add_freq(root, "Cool Max Frequency", &d, true);
  491. d = (double)fpga->freqMaxMaxM * 2;
  492. root = api_add_freq(root, "Max Frequency", &d, true);
  493. return root;
  494. }
  495. static
  496. bool x6500_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  497. {
  498. struct cgpu_info *x6500 = thr->cgpu;
  499. struct x6500_fpga_data *fpga = thr->cgpu_data;
  500. struct jtag_port *jp = &fpga->jtag;
  501. for (int i = 1, j = 0; i < 9; ++i, j += 4)
  502. x6500_set_register(jp, i, fromlebytes(work->midstate, j));
  503. for (int i = 9, j = 64; i < 11; ++i, j += 4)
  504. x6500_set_register(jp, i, fromlebytes(work->data, j));
  505. x6500_get_temperature(x6500);
  506. ft232r_flush(jp->a->ftdi);
  507. fpga->prepwork_last_register = fromlebytes(work->data, 72);
  508. work->blk.nonce = 0xffffffff;
  509. return true;
  510. }
  511. static int64_t calc_hashes(struct thr_info *, struct timeval *);
  512. static
  513. void x6500_job_start(struct thr_info *thr)
  514. {
  515. struct cgpu_info *x6500 = thr->cgpu;
  516. struct x6500_fpga_data *fpga = thr->cgpu_data;
  517. struct jtag_port *jp = &fpga->jtag;
  518. struct timeval tv_now;
  519. if (thr->prev_work)
  520. {
  521. dclk_preUpdate(&fpga->dclk);
  522. dclk_updateFreq(&fpga->dclk, x6500_dclk_change_clock, thr);
  523. }
  524. x6500_set_register(jp, 11, fpga->prepwork_last_register);
  525. ft232r_flush(jp->a->ftdi);
  526. timer_set_now(&tv_now);
  527. if (!thr->prev_work)
  528. fpga->tv_hashstart = tv_now;
  529. else
  530. if (thr->prev_work != thr->work)
  531. calc_hashes(thr, &tv_now);
  532. fpga->hashes_left = 0x100000000;
  533. mt_job_transition(thr);
  534. if (opt_debug) {
  535. char xdata[161];
  536. bin2hex(xdata, thr->work->data, 80);
  537. applog(LOG_DEBUG, "%"PRIprepr": Started work: %s",
  538. x6500->proc_repr, xdata);
  539. }
  540. uint32_t usecs = 0x80000000 / fpga->dclk.freqM;
  541. usecs -= 1000000;
  542. timer_set_delay(&thr->tv_morework, &tv_now, usecs);
  543. timer_set_delay(&thr->tv_poll, &tv_now, 10000);
  544. job_start_complete(thr);
  545. }
  546. static
  547. int64_t calc_hashes(struct thr_info *thr, struct timeval *tv_now)
  548. {
  549. struct x6500_fpga_data *fpga = thr->cgpu_data;
  550. struct timeval tv_delta;
  551. int64_t hashes, hashes_left;
  552. timersub(tv_now, &fpga->tv_hashstart, &tv_delta);
  553. hashes = (((int64_t)tv_delta.tv_sec * 1000000) + tv_delta.tv_usec) * fpga->dclk.freqM * 2;
  554. hashes_left = fpga->hashes_left;
  555. if (unlikely(hashes > hashes_left))
  556. hashes = hashes_left;
  557. fpga->hashes_left -= hashes;
  558. hashes_done(thr, hashes, &tv_delta, NULL);
  559. fpga->tv_hashstart = *tv_now;
  560. return hashes;
  561. }
  562. static
  563. int64_t x6500_process_results(struct thr_info *thr, struct work *work)
  564. {
  565. struct cgpu_info *x6500 = thr->cgpu;
  566. struct x6500_fpga_data *fpga = thr->cgpu_data;
  567. struct jtag_port *jtag = &fpga->jtag;
  568. struct timeval tv_now;
  569. int64_t hashes;
  570. uint32_t nonce;
  571. bool bad;
  572. while (1) {
  573. timer_set_now(&tv_now);
  574. nonce = x6500_get_register(jtag, 0xE);
  575. if (nonce != 0xffffffff) {
  576. bad = !(work && test_nonce(work, nonce, false));
  577. if (!bad) {
  578. submit_nonce(thr, work, nonce);
  579. applog(LOG_DEBUG, "%"PRIprepr": Nonce for current work: %08lx",
  580. x6500->proc_repr,
  581. (unsigned long)nonce);
  582. dclk_gotNonces(&fpga->dclk);
  583. } else if (likely(thr->prev_work) && test_nonce(thr->prev_work, nonce, false)) {
  584. submit_nonce(thr, thr->prev_work, nonce);
  585. applog(LOG_DEBUG, "%"PRIprepr": Nonce for PREVIOUS work: %08lx",
  586. x6500->proc_repr,
  587. (unsigned long)nonce);
  588. } else {
  589. inc_hw_errors(thr, work, nonce);
  590. dclk_gotNonces(&fpga->dclk);
  591. dclk_errorCount(&fpga->dclk, 1.);
  592. }
  593. // Keep reading nonce buffer until it's empty
  594. // This is necessary to avoid getting hw errors from Freq B after we've moved on to Freq A
  595. continue;
  596. }
  597. hashes = calc_hashes(thr, &tv_now);
  598. break;
  599. }
  600. return hashes;
  601. }
  602. static
  603. void x6500_fpga_poll(struct thr_info *thr)
  604. {
  605. struct x6500_fpga_data *fpga = thr->cgpu_data;
  606. x6500_process_results(thr, thr->work);
  607. if (unlikely(!fpga->hashes_left))
  608. {
  609. mt_disable_start(thr);
  610. thr->tv_poll.tv_sec = -1;
  611. }
  612. else
  613. timer_set_delay_from_now(&thr->tv_poll, 10000);
  614. }
  615. static
  616. void x6500_user_set_clock(struct cgpu_info *cgpu, const int val)
  617. {
  618. struct thr_info * const thr = cgpu->thr[0];
  619. struct x6500_fpga_data *fpga = thr->cgpu_data;
  620. const int multiplier = val / 2;
  621. fpga->dclk.freqMDefault = multiplier;
  622. }
  623. static
  624. char *x6500_set_device(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  625. {
  626. int val;
  627. if (strcasecmp(option, "help") == 0) {
  628. sprintf(replybuf, "clock: range %d-%d and a multiple of 2",
  629. X6500_MINIMUM_CLOCK, X6500_MAXIMUM_CLOCK);
  630. return replybuf;
  631. }
  632. if (strcasecmp(option, "clock") == 0) {
  633. if (!setting || !*setting) {
  634. sprintf(replybuf, "missing clock setting");
  635. return replybuf;
  636. }
  637. val = atoi(setting);
  638. if (val < X6500_MINIMUM_CLOCK || val > X6500_MAXIMUM_CLOCK || (val & 1) != 0) {
  639. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d and a multiple of 2",
  640. setting, X6500_MINIMUM_CLOCK, X6500_MAXIMUM_CLOCK);
  641. return replybuf;
  642. }
  643. x6500_user_set_clock(cgpu, val);
  644. return NULL;
  645. }
  646. sprintf(replybuf, "Unknown option: %s", option);
  647. return replybuf;
  648. }
  649. #ifdef HAVE_CURSES
  650. static
  651. void x6500_tui_wlogprint_choices(struct cgpu_info *cgpu)
  652. {
  653. wlogprint("[C]lock speed ");
  654. }
  655. static
  656. const char *x6500_tui_handle_choice(struct cgpu_info *cgpu, int input)
  657. {
  658. static char buf[0x100]; // Static for replies
  659. switch (input)
  660. {
  661. case 'c': case 'C':
  662. {
  663. int val;
  664. char *intvar;
  665. sprintf(buf, "Set clock speed (range %d-%d, multiple of 2)", X6500_MINIMUM_CLOCK, X6500_MAXIMUM_CLOCK);
  666. intvar = curses_input(buf);
  667. if (!intvar)
  668. return "Invalid clock speed\n";
  669. val = atoi(intvar);
  670. free(intvar);
  671. if (val < X6500_MINIMUM_CLOCK || val > X6500_MAXIMUM_CLOCK || (val & 1) != 0)
  672. return "Invalid clock speed\n";
  673. x6500_user_set_clock(cgpu, val);
  674. return "Clock speed changed\n";
  675. }
  676. }
  677. return NULL;
  678. }
  679. static
  680. void x6500_wlogprint_status(struct cgpu_info *cgpu)
  681. {
  682. struct x6500_fpga_data *fpga = cgpu->thr[0]->cgpu_data;
  683. wlogprint("Clock speed: %d\n", (int)(fpga->dclk.freqM * 2));
  684. }
  685. #endif
  686. struct device_drv x6500_api = {
  687. .dname = "x6500",
  688. .name = "XBS",
  689. .drv_detect = x6500_detect,
  690. .thread_prepare = x6500_prepare,
  691. .thread_init = x6500_thread_init,
  692. .get_stats = x6500_get_stats,
  693. .override_statline_temp2 = get_x6500_upload_percent,
  694. .get_api_extra_device_status = get_x6500_api_extra_device_status,
  695. .set_device = x6500_set_device,
  696. #ifdef HAVE_CURSES
  697. .proc_wlogprint_status = x6500_wlogprint_status,
  698. .proc_tui_wlogprint_choices = x6500_tui_wlogprint_choices,
  699. .proc_tui_handle_choice = x6500_tui_handle_choice,
  700. #endif
  701. .poll = x6500_fpga_poll,
  702. .minerloop = minerloop_async,
  703. .job_prepare = x6500_job_prepare,
  704. .job_start = x6500_job_start,
  705. // .thread_shutdown = x6500_fpga_shutdown,
  706. };