driver-x6500.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright 2012 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 <libusb-1.0/libusb.h>
  10. #include "dynclock.h"
  11. #include "jtag.h"
  12. #include "logging.h"
  13. #include "miner.h"
  14. #include "fpgautils.h"
  15. #include "ft232r.h"
  16. #define X6500_USB_PRODUCT "X6500 FPGA Miner"
  17. #define X6500_BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.bit"
  18. // NOTE: X6500_BITSTREAM_USERID is bitflipped
  19. #define X6500_BITSTREAM_USERID "\x40\x20\x24\x42"
  20. #define X6500_MINIMUM_CLOCK 2
  21. #define X6500_DEFAULT_CLOCK 200
  22. #define X6500_MAXIMUM_CLOCK 210
  23. struct device_api x6500_api;
  24. #define fromlebytes(ca, j) (ca[j] | (((uint16_t)ca[j+1])<<8) | (((uint32_t)ca[j+2])<<16) | (((uint32_t)ca[j+3])<<24))
  25. static
  26. void int2bits(uint32_t n, uint8_t *b, uint8_t bits)
  27. {
  28. uint8_t i;
  29. for (i = (bits + 7) / 8; i > 0; )
  30. b[--i] = 0;
  31. for (i = 0; i < bits; ++i) {
  32. if (n & 1)
  33. b[i/8] |= 0x80 >> (i % 8);
  34. n >>= 1;
  35. }
  36. }
  37. static
  38. uint32_t bits2int(uint8_t *b, uint8_t bits)
  39. {
  40. uint32_t n, i;
  41. n = 0;
  42. for (i = 0; i < bits; ++i)
  43. if (b[i/8] & (0x80 >> (i % 8)))
  44. n |= 1<<i;
  45. return n;
  46. }
  47. static
  48. void checksum(uint8_t *b, uint8_t bits)
  49. {
  50. uint8_t i;
  51. uint8_t checksum = 1;
  52. for(i = 0; i < bits; ++i)
  53. checksum ^= (b[i/8] & (0x80 >> (i % 8))) ? 1 : 0;
  54. if (checksum)
  55. b[i/8] |= 0x80 >> (i % 8);
  56. }
  57. static
  58. void x6500_jtag_set(struct jtag_port *jp, uint8_t pinoffset)
  59. {
  60. jp->tck = pinoffset << 3;
  61. jp->tms = pinoffset << 2;
  62. jp->tdi = pinoffset << 1;
  63. jp->tdo = pinoffset << 0;
  64. jp->ignored = ~(jp->tdo | jp->tdi | jp->tms | jp->tck);
  65. }
  66. static uint32_t x6500_get_register(struct jtag_port *jp, uint8_t addr);
  67. static
  68. void x6500_set_register(struct jtag_port *jp, uint8_t addr, uint32_t nv)
  69. {
  70. uint8_t buf[38];
  71. retry:
  72. jtag_write(jp, JTAG_REG_IR, "\x40", 6);
  73. int2bits(nv, &buf[0], 32);
  74. int2bits(addr, &buf[4], 4);
  75. buf[4] |= 8;
  76. checksum(buf, 37);
  77. jtag_write(jp, JTAG_REG_DR, buf, 38);
  78. jtag_run(jp);
  79. #ifdef DEBUG_X6500_SET_REGISTER
  80. if (x6500_get_register(jp, addr) != nv)
  81. #else
  82. if (0)
  83. #endif
  84. {
  85. applog(LOG_WARNING, "x6500_set_register failed %x=%08x", addr, nv);
  86. goto retry;
  87. }
  88. }
  89. static
  90. uint32_t x6500_get_register(struct jtag_port *jp, uint8_t addr)
  91. {
  92. uint8_t buf[4];
  93. jtag_write(jp, JTAG_REG_IR, "\x40", 6);
  94. int2bits(addr, &buf[0], 4);
  95. checksum(buf, 5);
  96. jtag_write(jp, JTAG_REG_DR, buf, 6);
  97. jtag_read (jp, JTAG_REG_DR, buf, 32);
  98. jtag_reset(jp);
  99. return bits2int(buf, 32);
  100. }
  101. static bool x6500_foundusb(libusb_device *dev, const char *product, const char *serial)
  102. {
  103. struct cgpu_info *x6500;
  104. x6500 = calloc(1, sizeof(*x6500));
  105. x6500->api = &x6500_api;
  106. mutex_init(&x6500->device_mutex);
  107. x6500->device_path = strdup(serial);
  108. x6500->deven = DEV_ENABLED;
  109. x6500->threads = 2;
  110. x6500->name = strdup(product);
  111. x6500->cutofftemp = 85;
  112. x6500->cgpu_data = dev;
  113. return add_cgpu(x6500);
  114. }
  115. static bool x6500_detect_one(const char *serial)
  116. {
  117. return ft232r_detect(X6500_USB_PRODUCT, serial, x6500_foundusb);
  118. }
  119. static int x6500_detect_auto()
  120. {
  121. return ft232r_detect(X6500_USB_PRODUCT, NULL, x6500_foundusb);
  122. }
  123. static void x6500_detect()
  124. {
  125. serial_detect_auto(&x6500_api, x6500_detect_one, x6500_detect_auto);
  126. }
  127. static bool x6500_prepare(struct thr_info *thr)
  128. {
  129. if (thr->device_thread)
  130. return true;
  131. struct cgpu_info *x6500 = thr->cgpu;
  132. mutex_init(&x6500->device_mutex);
  133. struct ft232r_device_handle *ftdi = ft232r_open(x6500->cgpu_data);
  134. x6500->device_ft232r = NULL;
  135. if (!ftdi)
  136. return false;
  137. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  138. return false;
  139. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  140. return false;
  141. x6500->device_ft232r = ftdi;
  142. struct jtag_port_a *jtag_a;
  143. jtag_a = calloc(1, sizeof(*jtag_a));
  144. jtag_a->ftdi = ftdi;
  145. x6500->cgpu_data = jtag_a;
  146. return true;
  147. }
  148. struct x6500_fpga_data {
  149. struct jtag_port jtag;
  150. struct work prevwork;
  151. };
  152. #define bailout2(...) do { \
  153. applog(__VA_ARGS__); \
  154. return false; \
  155. } while(0)
  156. static bool
  157. x6500_fpga_upload_bitstream(struct cgpu_info *x6500, struct jtag_port *jp1)
  158. {
  159. char buf[0x100];
  160. unsigned long len, flen;
  161. char *pdone = (char*)&x6500->cgpu_data;
  162. struct ft232r_device_handle *ftdi = jp1->a->ftdi;
  163. FILE *f = open_xilinx_bitstream(x6500, X6500_BITSTREAM_FILENAME, &len);
  164. if (!f)
  165. return false;
  166. flen = len;
  167. applog(LOG_WARNING, "%s %u: Programming %s...",
  168. x6500->api->name, x6500->device_id, x6500->device_path);
  169. // "Magic" jtag_port configured to access both FPGAs concurrently
  170. struct jtag_port jpt = {
  171. .a = jp1->a,
  172. };
  173. struct jtag_port *jp = &jpt;
  174. uint8_t i, j;
  175. x6500_jtag_set(jp, 0x11);
  176. // Need to reset here despite previous FPGA state, since we are programming all at once
  177. jtag_reset(jp);
  178. jtag_write(jp, JTAG_REG_IR, "\xd0", 6); // JPROGRAM
  179. // Poll each FPGA status individually since they might not be ready at the same time
  180. for (j = 0; j < 2; ++j) {
  181. x6500_jtag_set(jp, j ? 0x10 : 1);
  182. do {
  183. i = 0xd0; // Re-set JPROGRAM while reading status
  184. jtag_read(jp, JTAG_REG_IR, &i, 6);
  185. } while (i & 8);
  186. applog(LOG_DEBUG, "%s %u.%u: JPROGRAM ready",
  187. x6500->api->name, x6500->device_id, j);
  188. }
  189. x6500_jtag_set(jp, 0x11);
  190. jtag_write(jp, JTAG_REG_IR, "\xa0", 6); // CFG_IN
  191. sleep(1);
  192. if (fread(buf, 32, 1, f) != 1)
  193. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", x6500->api->name, x6500->device_id, x6500->device_path, len);
  194. jtag_swrite(jp, JTAG_REG_DR, buf, 256);
  195. len -= 32;
  196. // Put ft232r chip in asynchronous bitbang mode so we don't need to read back tdo
  197. // This takes upload time down from about an hour to about 3 minutes
  198. if (!ft232r_set_bitmode(ftdi, 0xee, 1))
  199. return false;
  200. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  201. return false;
  202. jp->a->async = true;
  203. ssize_t buflen;
  204. char nextstatus = 10;
  205. while (len) {
  206. buflen = len < 32 ? len : 32;
  207. if (fread(buf, buflen, 1, f) != 1)
  208. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", x6500->api->name, x6500->device_id, x6500->device_path, len);
  209. jtag_swrite_more(jp, buf, buflen * 8, len == (unsigned long)buflen);
  210. *pdone = 100 - ((len * 100) / flen);
  211. if (*pdone >= nextstatus)
  212. {
  213. nextstatus += 10;
  214. applog(LOG_WARNING, "%s %u: Programming %s... %d%% complete...", x6500->api->name, x6500->device_id, x6500->device_path, *pdone);
  215. }
  216. len -= buflen;
  217. }
  218. // Switch back to synchronous bitbang mode
  219. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  220. return false;
  221. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  222. return false;
  223. jp->a->async = false;
  224. jp->a->bufread = 0;
  225. jtag_write(jp, JTAG_REG_IR, "\x30", 6); // JSTART
  226. for (i=0; i<16; ++i)
  227. jtag_run(jp);
  228. i = 0xff; // BYPASS
  229. jtag_read(jp, JTAG_REG_IR, &i, 6);
  230. if (!(i & 4))
  231. return false;
  232. applog(LOG_WARNING, "%s %u: Done programming %s", x6500->api->name, x6500->device_id, x6500->device_path);
  233. return true;
  234. }
  235. static bool x6500_fpga_init(struct thr_info *thr)
  236. {
  237. struct cgpu_info *x6500 = thr->cgpu;
  238. struct ft232r_device_handle *ftdi = x6500->device_ft232r;
  239. struct x6500_fpga_data *fpga;
  240. struct jtag_port *jp;
  241. int fpgaid = thr->device_thread;
  242. uint8_t pinoffset = fpgaid ? 0x10 : 1;
  243. unsigned char buf[4];
  244. int i;
  245. if (!ftdi)
  246. return false;
  247. thread_reportin(thr); // HACK
  248. fpga = calloc(1, sizeof(*fpga));
  249. jp = &fpga->jtag;
  250. jp->a = x6500->cgpu_data;
  251. x6500_jtag_set(jp, pinoffset);
  252. mutex_lock(&x6500->device_mutex);
  253. if (!jtag_reset(jp)) {
  254. mutex_unlock(&x6500->device_mutex);
  255. applog(LOG_ERR, "%s %u: JTAG reset failed",
  256. x6500->api->name, x6500->device_id);
  257. return false;
  258. }
  259. i = jtag_detect(jp);
  260. if (i != 1) {
  261. mutex_unlock(&x6500->device_mutex);
  262. applog(LOG_ERR, "%s %u: JTAG detect returned %d",
  263. x6500->api->name, x6500->device_id, i);
  264. return false;
  265. }
  266. if (!(1
  267. && jtag_write(jp, JTAG_REG_IR, "\x10", 6)
  268. && jtag_read (jp, JTAG_REG_DR, buf, 32)
  269. && jtag_reset(jp)
  270. )) {
  271. mutex_unlock(&x6500->device_mutex);
  272. applog(LOG_ERR, "%s %u: JTAG error reading user code",
  273. x6500->api->name, x6500->device_id);
  274. return false;
  275. }
  276. if (memcmp(buf, X6500_BITSTREAM_USERID, 4)) {
  277. applog(LOG_ERR, "%s %u.%u: FPGA not programmed",
  278. x6500->api->name, x6500->device_id, fpgaid);
  279. if (!x6500_fpga_upload_bitstream(x6500, jp))
  280. return false;
  281. } else
  282. applog(LOG_DEBUG, "%s %u.%u: FPGA is already programmed :)",
  283. x6500->api->name, x6500->device_id, fpgaid);
  284. thr->cgpu_data = fpga;
  285. x6500_set_register(jp, 0xD, 180); // Set clock speed
  286. ft232r_flush(jp->a->ftdi);
  287. mutex_unlock(&x6500->device_mutex);
  288. return true;
  289. }
  290. static void
  291. get_x6500_statline_before(char *buf, struct cgpu_info *x6500)
  292. {
  293. char info[18] = " | ";
  294. char pdone = (char)(x6500->cgpu_data);
  295. if (pdone != 101) {
  296. sprintf(&info[1], "%3d%%", pdone);
  297. info[5] = ' ';
  298. strcat(buf, info);
  299. return;
  300. }
  301. strcat(buf, " | ");
  302. }
  303. static
  304. bool x6500_start_work(struct thr_info *thr, struct work *work)
  305. {
  306. struct cgpu_info *x6500 = thr->cgpu;
  307. struct x6500_fpga_data *fpga = thr->cgpu_data;
  308. struct jtag_port *jp = &fpga->jtag;
  309. char fpgaid = thr->device_thread;
  310. mutex_lock(&x6500->device_mutex);
  311. for (int i = 1, j = 0; i < 9; ++i, j += 4)
  312. x6500_set_register(jp, i, fromlebytes(work->midstate, j));
  313. for (int i = 9, j = 64; i < 12; ++i, j += 4)
  314. x6500_set_register(jp, i, fromlebytes(work->data, j));
  315. ft232r_flush(jp->a->ftdi);
  316. //gettimeofday(&fpga->tv_workstart, NULL);
  317. mutex_unlock(&x6500->device_mutex);
  318. if (opt_debug) {
  319. char *xdata = bin2hex(work->data, 80);
  320. applog(LOG_DEBUG, "%s %u.%u: Started work: %s",
  321. x6500->api->name, x6500->device_id, fpgaid, xdata);
  322. free(xdata);
  323. }
  324. return true;
  325. }
  326. static
  327. int64_t x6500_process_results(struct thr_info *thr, struct work *work)
  328. {
  329. struct cgpu_info *x6500 = thr->cgpu;
  330. struct x6500_fpga_data *fpga = thr->cgpu_data;
  331. struct jtag_port *jtag = &fpga->jtag;
  332. char fpgaid = thr->device_thread;
  333. uint32_t nonce;
  334. long iter;
  335. bool bad;
  336. iter = 200;
  337. while (1) {
  338. mutex_lock(&x6500->device_mutex);
  339. nonce = x6500_get_register(jtag, 0xE);
  340. mutex_unlock(&x6500->device_mutex);
  341. if (nonce != 0xffffffff) {
  342. bad = !test_nonce(work, nonce, false);
  343. if (!bad) {
  344. submit_nonce(thr, work, nonce);
  345. applog(LOG_DEBUG, "%s %u.%u: Nonce for current work: %08lx",
  346. x6500->api->name, x6500->device_id, fpgaid,
  347. (unsigned long)nonce);
  348. } else if (test_nonce(&fpga->prevwork, nonce, false)) {
  349. submit_nonce(thr, &fpga->prevwork, nonce);
  350. applog(LOG_DEBUG, "%s %u.%u: Nonce for PREVIOUS work: %08lx",
  351. x6500->api->name, x6500->device_id, fpgaid,
  352. (unsigned long)nonce);
  353. } else {
  354. applog(LOG_DEBUG, "%s %u.%u: Nonce with H not zero : %08lx",
  355. x6500->api->name, x6500->device_id, fpgaid,
  356. (unsigned long)nonce);
  357. ++hw_errors;
  358. ++x6500->hw_errors;
  359. }
  360. }
  361. if (thr->work_restart || !--iter)
  362. break;
  363. usleep(1000);
  364. if (thr->work_restart)
  365. break;
  366. }
  367. memcpy(&fpga->prevwork, work, sizeof(fpga->prevwork));
  368. return 0xffffffff;
  369. }
  370. static int64_t
  371. x6500_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  372. {
  373. if (!x6500_start_work(thr, work))
  374. return -1;
  375. int64_t hashes = x6500_process_results(thr, work);
  376. if (hashes > 0)
  377. work->blk.nonce += hashes;
  378. return hashes;
  379. }
  380. struct device_api x6500_api = {
  381. .dname = "x6500",
  382. .name = "XBS",
  383. .api_detect = x6500_detect,
  384. .thread_prepare = x6500_prepare,
  385. .thread_init = x6500_fpga_init,
  386. .get_statline_before = get_x6500_statline_before,
  387. .scanhash = x6500_scanhash,
  388. // .thread_shutdown = x6500_fpga_shutdown,
  389. };