driver-x6500.c 21 KB

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