driver-x6500.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * Copyright 2012-2014 Luke Dashjr
  3. * Copyright 2013 Nate Woolls
  4. * Copyright 2012 Andrew Smith
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include <limits.h>
  13. #include <math.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <sys/time.h>
  17. #include <libusb.h>
  18. #include "binloader.h"
  19. #include "compat.h"
  20. #include "deviceapi.h"
  21. #include "dynclock.h"
  22. #include "jtag.h"
  23. #include "logging.h"
  24. #include "miner.h"
  25. #include "ft232r.h"
  26. #include "lowlevel.h"
  27. #include "lowl-usb.h"
  28. #define X6500_USB_PRODUCT "X6500 FPGA Miner"
  29. #define X6500_BITSTREAM_FILENAME "fpgaminer_x6500-overclocker-0402.bit"
  30. // NOTE: X6500_BITSTREAM_USERID is bitflipped
  31. #define X6500_BITSTREAM_USERID "\x40\x20\x24\x42"
  32. #define X6500_MINIMUM_CLOCK 2
  33. #define X6500_DEFAULT_CLOCK 190
  34. #define X6500_MAXIMUM_CLOCK 250
  35. BFG_REGISTER_DRIVER(x6500_api)
  36. #define fromlebytes(ca, j) (ca[j] | (((uint16_t)ca[j+1])<<8) | (((uint32_t)ca[j+2])<<16) | (((uint32_t)ca[j+3])<<24))
  37. static
  38. void int2bits(uint32_t n, uint8_t *b, uint8_t bits)
  39. {
  40. uint8_t i;
  41. for (i = (bits + 7) / 8; i > 0; )
  42. b[--i] = 0;
  43. for (i = 0; i < bits; ++i) {
  44. if (n & 1)
  45. b[i/8] |= 0x80 >> (i % 8);
  46. n >>= 1;
  47. }
  48. }
  49. static
  50. uint32_t bits2int(uint8_t *b, uint8_t bits)
  51. {
  52. uint32_t n, i;
  53. n = 0;
  54. for (i = 0; i < bits; ++i)
  55. if (b[i/8] & (0x80 >> (i % 8)))
  56. n |= 1<<i;
  57. return n;
  58. }
  59. static
  60. void checksum(uint8_t *b, uint8_t bits)
  61. {
  62. uint8_t i;
  63. uint8_t checksum = 1;
  64. for(i = 0; i < bits; ++i)
  65. checksum ^= (b[i/8] & (0x80 >> (i % 8))) ? 1 : 0;
  66. if (checksum)
  67. b[i/8] |= 0x80 >> (i % 8);
  68. }
  69. static
  70. void x6500_jtag_set(struct jtag_port *jp, uint8_t pinoffset)
  71. {
  72. jp->tck = pinoffset << 3;
  73. jp->tms = pinoffset << 2;
  74. jp->tdi = pinoffset << 1;
  75. jp->tdo = pinoffset << 0;
  76. jp->ignored = ~(jp->tdo | jp->tdi | jp->tms | jp->tck);
  77. }
  78. static uint32_t x6500_get_register(struct jtag_port *jp, uint8_t addr);
  79. static
  80. void x6500_set_register(struct jtag_port *jp, uint8_t addr, uint32_t nv)
  81. {
  82. uint8_t buf[38];
  83. retry:
  84. jtag_write(jp, JTAG_REG_IR, "\x40", 6);
  85. int2bits(nv, &buf[0], 32);
  86. int2bits(addr, &buf[4], 4);
  87. buf[4] |= 8;
  88. checksum(buf, 37);
  89. jtag_write(jp, JTAG_REG_DR, buf, 38);
  90. jtag_run(jp);
  91. #ifdef DEBUG_X6500_SET_REGISTER
  92. if (x6500_get_register(jp, addr) != nv)
  93. #else
  94. if (0)
  95. #endif
  96. {
  97. applog(LOG_WARNING, "x6500_set_register failed %x=%08x", addr, nv);
  98. goto retry;
  99. }
  100. }
  101. static
  102. uint32_t x6500_get_register(struct jtag_port *jp, uint8_t addr)
  103. {
  104. uint8_t buf[4] = {0};
  105. jtag_write(jp, JTAG_REG_IR, "\x40", 6);
  106. int2bits(addr, &buf[0], 4);
  107. checksum(buf, 5);
  108. jtag_write(jp, JTAG_REG_DR, buf, 6);
  109. jtag_read (jp, JTAG_REG_DR, buf, 32);
  110. jtag_reset(jp);
  111. return bits2int(buf, 32);
  112. }
  113. static
  114. bool x6500_lowl_match(const struct lowlevel_device_info * const info)
  115. {
  116. return lowlevel_match_lowlproduct(info, &lowl_ft232r, X6500_USB_PRODUCT);
  117. }
  118. static
  119. bool x6500_lowl_probe(const struct lowlevel_device_info * const info)
  120. {
  121. const char * const product = info->product;
  122. const char * const serial = info->serial;
  123. if (info->lowl != &lowl_ft232r)
  124. {
  125. if (info->lowl != &lowl_usb)
  126. applog(LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not ft232r!",
  127. __func__, product, serial);
  128. return false;
  129. }
  130. libusb_device * const dev = info->lowl_data;
  131. if (bfg_claim_libusb(&x6500_api, true, dev))
  132. return false;
  133. struct cgpu_info *x6500;
  134. x6500 = calloc(1, sizeof(*x6500));
  135. x6500->drv = &x6500_api;
  136. x6500->device_path = strdup(serial);
  137. x6500->deven = DEV_ENABLED;
  138. x6500->threads = 1;
  139. x6500->procs = 2;
  140. x6500->name = strdup(product);
  141. x6500->cutofftemp = 85;
  142. x6500->device_data = lowlevel_ref(info);
  143. cgpu_copy_libusb_strings(x6500, dev);
  144. return add_cgpu(x6500);
  145. }
  146. static bool x6500_prepare(struct thr_info *thr)
  147. {
  148. struct cgpu_info *x6500 = thr->cgpu;
  149. if (x6500->proc_id)
  150. return true;
  151. struct ft232r_device_handle *ftdi = ft232r_open(x6500->device_data);
  152. lowlevel_devinfo_free(x6500->device_data);
  153. x6500->device_ft232r = NULL;
  154. if (!ftdi)
  155. return false;
  156. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  157. return false;
  158. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  159. return false;
  160. x6500->device_ft232r = ftdi;
  161. struct jtag_port_a *jtag_a;
  162. unsigned char *pdone = calloc(1, sizeof(*jtag_a) + 1);
  163. *pdone = 101;
  164. jtag_a = (void*)(pdone + 1);
  165. jtag_a->ftdi = ftdi;
  166. x6500->device_data = jtag_a;
  167. for (struct cgpu_info *slave = x6500->next_proc; slave; slave = slave->next_proc)
  168. {
  169. slave->device_ft232r = x6500->device_ft232r;
  170. slave->device_data = x6500->device_data;
  171. }
  172. return true;
  173. }
  174. struct x6500_fpga_data {
  175. struct jtag_port jtag;
  176. struct timeval tv_hashstart;
  177. int64_t hashes_left;
  178. struct dclk_data dclk;
  179. uint8_t freqMaxMaxM;
  180. // Time the clock was last reduced due to temperature
  181. struct timeval tv_last_cutoff_reduced;
  182. uint32_t prepwork_last_register;
  183. };
  184. #define bailout2(...) do { \
  185. applog(__VA_ARGS__); \
  186. return false; \
  187. } while(0)
  188. static bool
  189. x6500_fpga_upload_bitstream(struct cgpu_info *x6500, struct jtag_port *jp1)
  190. {
  191. char buf[0x100];
  192. unsigned long len, flen;
  193. unsigned char *pdone = (unsigned char*)x6500->device_data - 1;
  194. struct ft232r_device_handle *ftdi = jp1->a->ftdi;
  195. FILE *f = open_xilinx_bitstream(x6500->drv->dname, x6500->dev_repr, X6500_BITSTREAM_FILENAME, &len);
  196. if (!f)
  197. return false;
  198. flen = len;
  199. applog(LOG_WARNING, "%s: Programming %s...",
  200. x6500->dev_repr, x6500->device_path);
  201. x6500->status = LIFE_INIT2;
  202. // "Magic" jtag_port configured to access both FPGAs concurrently
  203. struct jtag_port jpt = {
  204. .a = jp1->a,
  205. };
  206. struct jtag_port *jp = &jpt;
  207. uint8_t i, j;
  208. x6500_jtag_set(jp, 0x11);
  209. // Need to reset here despite previous FPGA state, since we are programming all at once
  210. jtag_reset(jp);
  211. jtag_write(jp, JTAG_REG_IR, "\xd0", 6); // JPROGRAM
  212. // Poll each FPGA status individually since they might not be ready at the same time
  213. for (j = 0; j < 2; ++j) {
  214. x6500_jtag_set(jp, j ? 0x10 : 1);
  215. do {
  216. i = 0xd0; // Re-set JPROGRAM while reading status
  217. jtag_read(jp, JTAG_REG_IR, &i, 6);
  218. } while (i & 8);
  219. applog(LOG_DEBUG, "%s%c: JPROGRAM ready",
  220. x6500->dev_repr, 'a' + j);
  221. }
  222. x6500_jtag_set(jp, 0x11);
  223. jtag_write(jp, JTAG_REG_IR, "\xa0", 6); // CFG_IN
  224. cgsleep_ms(1000);
  225. if (fread(buf, 32, 1, f) != 1)
  226. bailout2(LOG_ERR, "%s: File underrun programming %s (%lu bytes left)", x6500->dev_repr, x6500->device_path, len);
  227. jtag_swrite(jp, JTAG_REG_DR, buf, 256);
  228. len -= 32;
  229. // Put ft232r chip in asynchronous bitbang mode so we don't need to read back tdo
  230. // This takes upload time down from about an hour to about 3 minutes
  231. if (!ft232r_set_bitmode(ftdi, 0xee, 1))
  232. return false;
  233. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  234. return false;
  235. jp->a->bufread = 0;
  236. jp->a->async = true;
  237. ssize_t buflen;
  238. char nextstatus = 25;
  239. while (len) {
  240. buflen = len < 32 ? len : 32;
  241. if (fread(buf, buflen, 1, f) != 1)
  242. bailout2(LOG_ERR, "%s: File underrun programming %s (%lu bytes left)", x6500->dev_repr, x6500->device_path, len);
  243. jtag_swrite_more(jp, buf, buflen * 8, len == (unsigned long)buflen);
  244. *pdone = 100 - ((len * 100) / flen);
  245. if (*pdone >= nextstatus)
  246. {
  247. nextstatus += 25;
  248. applog(LOG_WARNING, "%s: Programming %s... %d%% complete...", x6500->dev_repr, x6500->device_path, *pdone);
  249. }
  250. len -= buflen;
  251. }
  252. // Switch back to synchronous bitbang mode
  253. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  254. return false;
  255. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  256. return false;
  257. jp->a->bufread = 0;
  258. jp->a->async = false;
  259. jp->a->bufread = 0;
  260. jtag_write(jp, JTAG_REG_IR, "\x30", 6); // JSTART
  261. for (i=0; i<16; ++i)
  262. jtag_run(jp);
  263. i = 0xff; // BYPASS
  264. jtag_read(jp, JTAG_REG_IR, &i, 6);
  265. if (!(i & 4))
  266. return false;
  267. applog(LOG_WARNING, "%s: Done programming %s", x6500->dev_repr, x6500->device_path);
  268. *pdone = 101;
  269. return true;
  270. }
  271. static bool x6500_change_clock(struct thr_info *thr, int multiplier)
  272. {
  273. struct x6500_fpga_data *fpga = thr->cgpu_data;
  274. struct jtag_port *jp = &fpga->jtag;
  275. x6500_set_register(jp, 0xD, multiplier * 2);
  276. ft232r_flush(jp->a->ftdi);
  277. fpga->dclk.freqM = multiplier;
  278. return true;
  279. }
  280. static bool x6500_dclk_change_clock(struct thr_info *thr, int multiplier)
  281. {
  282. struct cgpu_info *x6500 = thr->cgpu;
  283. struct x6500_fpga_data *fpga = thr->cgpu_data;
  284. uint8_t oldFreq = fpga->dclk.freqM;
  285. if (!x6500_change_clock(thr, multiplier)) {
  286. return false;
  287. }
  288. dclk_msg_freqchange(x6500->proc_repr, oldFreq * 2, fpga->dclk.freqM * 2, NULL);
  289. return true;
  290. }
  291. static bool x6500_thread_init(struct thr_info *thr)
  292. {
  293. struct cgpu_info *x6500 = thr->cgpu;
  294. struct ft232r_device_handle *ftdi = x6500->device_ft232r;
  295. cgpu_setup_control_requests(x6500);
  296. // This works because x6500_thread_init is only called for the first processor now that they're all using the same thread
  297. for ( ; x6500; x6500 = x6500->next_proc)
  298. {
  299. thr = x6500->thr[0];
  300. struct x6500_fpga_data *fpga;
  301. struct jtag_port *jp;
  302. int fpgaid = x6500->proc_id;
  303. uint8_t pinoffset = fpgaid ? 0x10 : 1;
  304. unsigned char buf[4] = {0};
  305. int i;
  306. if (!ftdi)
  307. return false;
  308. fpga = calloc(1, sizeof(*fpga));
  309. jp = &fpga->jtag;
  310. jp->a = x6500->device_data;
  311. x6500_jtag_set(jp, pinoffset);
  312. thr->cgpu_data = fpga;
  313. x6500->status = LIFE_INIT2;
  314. if (!jtag_reset(jp)) {
  315. applog(LOG_ERR, "%s: JTAG reset failed",
  316. x6500->dev_repr);
  317. return false;
  318. }
  319. i = jtag_detect(jp);
  320. if (i != 1) {
  321. applog(LOG_ERR, "%s: JTAG detect returned %d",
  322. x6500->dev_repr, i);
  323. return false;
  324. }
  325. if (!(1
  326. && jtag_write(jp, JTAG_REG_IR, "\x10", 6)
  327. && jtag_read (jp, JTAG_REG_DR, buf, 32)
  328. && jtag_reset(jp)
  329. )) {
  330. applog(LOG_ERR, "%s: JTAG error reading user code",
  331. x6500->dev_repr);
  332. return false;
  333. }
  334. if (memcmp(buf, X6500_BITSTREAM_USERID, 4)) {
  335. applog(LOG_ERR, "%"PRIprepr": FPGA not programmed",
  336. x6500->proc_repr);
  337. if (!x6500_fpga_upload_bitstream(x6500, jp))
  338. return false;
  339. } else if (opt_force_dev_init && x6500 == x6500->device) {
  340. applog(LOG_DEBUG, "%"PRIprepr": FPGA is already programmed, but --force-dev-init is set",
  341. x6500->proc_repr);
  342. if (!x6500_fpga_upload_bitstream(x6500, jp))
  343. return false;
  344. } else
  345. applog(LOG_DEBUG, "%s"PRIprepr": FPGA is already programmed :)",
  346. x6500->proc_repr);
  347. dclk_prepare(&fpga->dclk);
  348. fpga->dclk.freqMinM = X6500_MINIMUM_CLOCK / 2;
  349. x6500_change_clock(thr, X6500_DEFAULT_CLOCK / 2);
  350. for (i = 0; 0xffffffff != x6500_get_register(jp, 0xE); ++i)
  351. {}
  352. if (i)
  353. applog(LOG_WARNING, "%"PRIprepr": Flushed %d nonces from buffer at init",
  354. x6500->proc_repr, i);
  355. fpga->dclk.minGoodSamples = 3;
  356. fpga->freqMaxMaxM =
  357. fpga->dclk.freqMaxM = X6500_MAXIMUM_CLOCK / 2;
  358. fpga->dclk.freqMDefault = fpga->dclk.freqM;
  359. applog(LOG_WARNING, "%"PRIprepr": Frequency set to %u MHz (range: %u-%u)",
  360. x6500->proc_repr,
  361. fpga->dclk.freqM * 2,
  362. X6500_MINIMUM_CLOCK,
  363. fpga->dclk.freqMaxM * 2);
  364. }
  365. return true;
  366. }
  367. static
  368. void x6500_get_temperature(struct cgpu_info *x6500)
  369. {
  370. struct x6500_fpga_data *fpga = x6500->thr[0]->cgpu_data;
  371. struct jtag_port *jp = &fpga->jtag;
  372. struct ft232r_device_handle *ftdi = jp->a->ftdi;
  373. int i, code[2];
  374. bool sio[2];
  375. code[0] = 0;
  376. code[1] = 0;
  377. ft232r_flush(ftdi);
  378. if (!(ft232r_set_cbus_bits(ftdi, false, true))) return;
  379. if (!(ft232r_set_cbus_bits(ftdi, true, true))) return;
  380. if (!(ft232r_set_cbus_bits(ftdi, false, true))) return;
  381. if (!(ft232r_set_cbus_bits(ftdi, true, true))) return;
  382. if (!(ft232r_set_cbus_bits(ftdi, false, false))) return;
  383. for (i = 16; i--; ) {
  384. if (ft232r_set_cbus_bits(ftdi, true, false)) {
  385. if (!(ft232r_get_cbus_bits(ftdi, &sio[0], &sio[1]))) {
  386. return;
  387. }
  388. } else {
  389. return;
  390. }
  391. code[0] |= sio[0] << i;
  392. code[1] |= sio[1] << i;
  393. if (!ft232r_set_cbus_bits(ftdi, false, false)) {
  394. return;
  395. }
  396. }
  397. if (!(ft232r_set_cbus_bits(ftdi, false, true))) {
  398. return;
  399. }
  400. if (!(ft232r_set_cbus_bits(ftdi, true, true))) {
  401. return;
  402. }
  403. if (!(ft232r_set_cbus_bits(ftdi, false, true))) {
  404. return;
  405. }
  406. if (!ft232r_set_bitmode(ftdi, 0xee, 4)) {
  407. return;
  408. }
  409. ft232r_purge_buffers(jp->a->ftdi, FTDI_PURGE_BOTH);
  410. jp->a->bufread = 0;
  411. x6500 = x6500->device;
  412. for (i = 0; i < 2; ++i, x6500 = x6500->next_proc) {
  413. struct thr_info *thr = x6500->thr[0];
  414. fpga = thr->cgpu_data;
  415. if (!fpga) continue;
  416. if (code[i] == 0xffff || !code[i]) {
  417. x6500->temp = 0;
  418. continue;
  419. }
  420. if ((code[i] >> 15) & 1)
  421. code[i] -= 0x10000;
  422. x6500->temp = (float)(code[i] >> 2) * 0.03125f;
  423. applog(LOG_DEBUG,"x6500_get_temperature: fpga[%d]->temp=%.1fC",
  424. i, x6500->temp);
  425. int temperature = round(x6500->temp);
  426. if (temperature > x6500->targettemp + opt_hysteresis) {
  427. struct timeval now;
  428. cgtime(&now);
  429. if (timer_elapsed(&fpga->tv_last_cutoff_reduced, &now)) {
  430. fpga->tv_last_cutoff_reduced = now;
  431. int oldFreq = fpga->dclk.freqM;
  432. if (x6500_change_clock(thr, oldFreq - 1))
  433. applog(LOG_NOTICE, "%"PRIprepr": Frequency dropped from %u to %u MHz (temp: %.1fC)",
  434. x6500->proc_repr,
  435. oldFreq * 2, fpga->dclk.freqM * 2,
  436. x6500->temp
  437. );
  438. fpga->dclk.freqMaxM = fpga->dclk.freqM;
  439. }
  440. }
  441. else
  442. if (fpga->dclk.freqMaxM < fpga->freqMaxMaxM && temperature < x6500->targettemp) {
  443. if (temperature < x6500->targettemp - opt_hysteresis) {
  444. fpga->dclk.freqMaxM = fpga->freqMaxMaxM;
  445. } else if (fpga->dclk.freqM == fpga->dclk.freqMaxM) {
  446. ++fpga->dclk.freqMaxM;
  447. }
  448. }
  449. }
  450. }
  451. static
  452. bool x6500_all_idle(struct cgpu_info *any_proc)
  453. {
  454. for (struct cgpu_info *proc = any_proc->device; proc; proc = proc->next_proc)
  455. if (proc->thr[0]->tv_poll.tv_sec != -1 || proc->deven == DEV_ENABLED)
  456. return false;
  457. return true;
  458. }
  459. static bool x6500_get_stats(struct cgpu_info *x6500)
  460. {
  461. if (x6500_all_idle(x6500)) {
  462. struct cgpu_info *cgpu = x6500->device;
  463. // Getting temperature more efficiently while running
  464. cgpu_request_control(cgpu);
  465. x6500_get_temperature(x6500);
  466. cgpu_release_control(cgpu);
  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__async(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. void x6500_user_set_max_clock(struct cgpu_info *cgpu, const int val)
  625. {
  626. struct thr_info * const thr = cgpu->thr[0];
  627. struct x6500_fpga_data *fpga = thr->cgpu_data;
  628. const int multiplier = val / 2;
  629. fpga->freqMaxMaxM =
  630. fpga->dclk.freqMaxM = multiplier;
  631. }
  632. static
  633. char *x6500_set_device(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  634. {
  635. int val;
  636. if (strcasecmp(option, "help") == 0) {
  637. sprintf(replybuf, "clock: range %d-%d and a multiple of 2\nmaxclock: default %d, range %d-%d and a multiple of 2",
  638. X6500_MINIMUM_CLOCK, X6500_MAXIMUM_CLOCK, X6500_MAXIMUM_CLOCK, X6500_MINIMUM_CLOCK, X6500_MAXIMUM_CLOCK);
  639. return replybuf;
  640. }
  641. if (strcasecmp(option, "clock") == 0) {
  642. if (!setting || !*setting) {
  643. sprintf(replybuf, "missing clock setting");
  644. return replybuf;
  645. }
  646. val = atoi(setting);
  647. if (val < X6500_MINIMUM_CLOCK || val > X6500_MAXIMUM_CLOCK || (val & 1) != 0) {
  648. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d and a multiple of 2",
  649. setting, X6500_MINIMUM_CLOCK, X6500_MAXIMUM_CLOCK);
  650. return replybuf;
  651. }
  652. x6500_user_set_clock(cgpu, val);
  653. return NULL;
  654. }
  655. if (strcasecmp(option, "maxclock") == 0) {
  656. if (!setting || !*setting) {
  657. sprintf(replybuf, "missing maxclock setting");
  658. return replybuf;
  659. }
  660. val = atoi(setting);
  661. if (val < X6500_MINIMUM_CLOCK || val > X6500_MAXIMUM_CLOCK || (val & 1) != 0) {
  662. sprintf(replybuf, "invalid maxclock: '%s' valid range %d-%d and a multiple of 2",
  663. setting, X6500_MINIMUM_CLOCK, X6500_MAXIMUM_CLOCK);
  664. return replybuf;
  665. }
  666. x6500_user_set_max_clock(cgpu, val);
  667. applog(LOG_NOTICE, "%"PRIpreprv": Maximum frequency reset to %u MHz",
  668. cgpu->proc_repr,
  669. val
  670. );
  671. return NULL;
  672. }
  673. sprintf(replybuf, "Unknown option: %s", option);
  674. return replybuf;
  675. }
  676. #ifdef HAVE_CURSES
  677. static
  678. void x6500_tui_wlogprint_choices(struct cgpu_info *cgpu)
  679. {
  680. wlogprint("[C]lock speed ");
  681. }
  682. static
  683. const char *x6500_tui_handle_choice(struct cgpu_info *cgpu, int input)
  684. {
  685. static char buf[0x100]; // Static for replies
  686. switch (input)
  687. {
  688. case 'c': case 'C':
  689. {
  690. int val;
  691. char *intvar;
  692. sprintf(buf, "Set clock speed (range %d-%d, multiple of 2)", X6500_MINIMUM_CLOCK, X6500_MAXIMUM_CLOCK);
  693. intvar = curses_input(buf);
  694. if (!intvar)
  695. return "Invalid clock speed\n";
  696. val = atoi(intvar);
  697. free(intvar);
  698. if (val < X6500_MINIMUM_CLOCK || val > X6500_MAXIMUM_CLOCK || (val & 1) != 0)
  699. return "Invalid clock speed\n";
  700. x6500_user_set_clock(cgpu, val);
  701. return "Clock speed changed\n";
  702. }
  703. }
  704. return NULL;
  705. }
  706. static
  707. void x6500_wlogprint_status(struct cgpu_info *cgpu)
  708. {
  709. struct x6500_fpga_data *fpga = cgpu->thr[0]->cgpu_data;
  710. wlogprint("Clock speed: %d\n", (int)(fpga->dclk.freqM * 2));
  711. }
  712. #endif
  713. struct device_drv x6500_api = {
  714. .dname = "x6500",
  715. .name = "XBS",
  716. .lowl_match = x6500_lowl_match,
  717. .lowl_probe = x6500_lowl_probe,
  718. .thread_prepare = x6500_prepare,
  719. .thread_init = x6500_thread_init,
  720. .get_stats = x6500_get_stats,
  721. .override_statline_temp2 = get_x6500_upload_percent,
  722. .get_api_extra_device_status = get_x6500_api_extra_device_status,
  723. .set_device = x6500_set_device,
  724. #ifdef HAVE_CURSES
  725. .proc_wlogprint_status = x6500_wlogprint_status,
  726. .proc_tui_wlogprint_choices = x6500_tui_wlogprint_choices,
  727. .proc_tui_handle_choice = x6500_tui_handle_choice,
  728. #endif
  729. .poll = x6500_fpga_poll,
  730. .minerloop = minerloop_async,
  731. .job_prepare = x6500_job_prepare,
  732. .job_start = x6500_job_start,
  733. // .thread_shutdown = x6500_fpga_shutdown,
  734. };