driver-x6500.c 20 KB

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