driver-x6500.c 21 KB

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