driver-x6500.c 21 KB

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