driver-x6500.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. };
  151. #define bailout2(...) do { \
  152. applog(__VA_ARGS__); \
  153. return false; \
  154. } while(0)
  155. static bool
  156. x6500_fpga_upload_bitstream(struct cgpu_info *x6500, struct jtag_port *jp1)
  157. {
  158. char buf[0x100];
  159. unsigned long len, flen;
  160. char *pdone = (char*)&x6500->cgpu_data;
  161. struct ft232r_device_handle *ftdi = jp1->a->ftdi;
  162. FILE *f = open_xilinx_bitstream(x6500, X6500_BITSTREAM_FILENAME, &len);
  163. if (!f)
  164. return false;
  165. flen = len;
  166. applog(LOG_WARNING, "%s %u: Programming %s...",
  167. x6500->api->name, x6500->device_id, x6500->device_path);
  168. // "Magic" jtag_port configured to access both FPGAs concurrently
  169. struct jtag_port jpt = {
  170. .a = jp1->a,
  171. };
  172. struct jtag_port *jp = &jpt;
  173. uint8_t i, j;
  174. x6500_jtag_set(jp, 0x11);
  175. // Need to reset here despite previous FPGA state, since we are programming all at once
  176. jtag_reset(jp);
  177. jtag_write(jp, JTAG_REG_IR, "\xd0", 6); // JPROGRAM
  178. // Poll each FPGA status individually since they might not be ready at the same time
  179. for (j = 0; j < 2; ++j) {
  180. x6500_jtag_set(jp, j ? 0x10 : 1);
  181. do {
  182. i = 0xd0; // Re-set JPROGRAM while reading status
  183. jtag_read(jp, JTAG_REG_IR, &i, 6);
  184. } while (i & 8);
  185. applog(LOG_DEBUG, "%s %u.%u: JPROGRAM ready",
  186. x6500->api->name, x6500->device_id, j);
  187. }
  188. x6500_jtag_set(jp, 0x11);
  189. jtag_write(jp, JTAG_REG_IR, "\xa0", 6); // CFG_IN
  190. sleep(1);
  191. if (fread(buf, 32, 1, f) != 1)
  192. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", x6500->api->name, x6500->device_id, x6500->device_path, len);
  193. jtag_swrite(jp, JTAG_REG_DR, buf, 256);
  194. len -= 32;
  195. // Put ft232r chip in asynchronous bitbang mode so we don't need to read back tdo
  196. // This takes upload time down from about an hour to about 3 minutes
  197. if (!ft232r_set_bitmode(ftdi, 0xee, 1))
  198. return false;
  199. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  200. return false;
  201. jp->a->async = true;
  202. ssize_t buflen;
  203. char nextstatus = 10;
  204. while (len) {
  205. buflen = len < 32 ? len : 32;
  206. if (fread(buf, buflen, 1, f) != 1)
  207. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", x6500->api->name, x6500->device_id, x6500->device_path, len);
  208. jtag_swrite_more(jp, buf, buflen * 8, len == (unsigned long)buflen);
  209. *pdone = 100 - ((len * 100) / flen);
  210. if (*pdone >= nextstatus)
  211. {
  212. nextstatus += 10;
  213. applog(LOG_WARNING, "%s %u: Programming %s... %d%% complete...", x6500->api->name, x6500->device_id, x6500->device_path, *pdone);
  214. }
  215. len -= buflen;
  216. }
  217. // Switch back to synchronous bitbang mode
  218. if (!ft232r_set_bitmode(ftdi, 0xee, 4))
  219. return false;
  220. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  221. return false;
  222. jp->a->async = false;
  223. jp->a->bufread = 0;
  224. jtag_write(jp, JTAG_REG_IR, "\x30", 6); // JSTART
  225. for (i=0; i<16; ++i)
  226. jtag_run(jp);
  227. i = 0xff; // BYPASS
  228. jtag_read(jp, JTAG_REG_IR, &i, 6);
  229. if (!(i & 4))
  230. return false;
  231. applog(LOG_WARNING, "%s %u: Done programming %s", x6500->api->name, x6500->device_id, x6500->device_path);
  232. return true;
  233. }
  234. static bool x6500_fpga_init(struct thr_info *thr)
  235. {
  236. struct cgpu_info *x6500 = thr->cgpu;
  237. struct ft232r_device_handle *ftdi = x6500->device_ft232r;
  238. struct x6500_fpga_data *fpga;
  239. struct jtag_port *jp;
  240. int fpgaid = thr->device_thread;
  241. uint8_t pinoffset = fpgaid ? 0x10 : 1;
  242. unsigned char buf[4];
  243. int i;
  244. if (!ftdi)
  245. return false;
  246. thread_reportin(thr); // HACK
  247. fpga = calloc(1, sizeof(*fpga));
  248. jp = &fpga->jtag;
  249. jp->a = x6500->cgpu_data;
  250. x6500_jtag_set(jp, pinoffset);
  251. mutex_lock(&x6500->device_mutex);
  252. if (!jtag_reset(jp)) {
  253. mutex_unlock(&x6500->device_mutex);
  254. applog(LOG_ERR, "%s %u: JTAG reset failed",
  255. x6500->api->name, x6500->device_id);
  256. return false;
  257. }
  258. i = jtag_detect(jp);
  259. if (i != 1) {
  260. mutex_unlock(&x6500->device_mutex);
  261. applog(LOG_ERR, "%s %u: JTAG detect returned %d",
  262. x6500->api->name, x6500->device_id, i);
  263. return false;
  264. }
  265. if (!(1
  266. && jtag_write(jp, JTAG_REG_IR, "\x10", 6)
  267. && jtag_read (jp, JTAG_REG_DR, buf, 32)
  268. && jtag_reset(jp)
  269. )) {
  270. mutex_unlock(&x6500->device_mutex);
  271. applog(LOG_ERR, "%s %u: JTAG error reading user code",
  272. x6500->api->name, x6500->device_id);
  273. return false;
  274. }
  275. if (memcmp(buf, X6500_BITSTREAM_USERID, 4)) {
  276. applog(LOG_ERR, "%s %u.%u: FPGA not programmed",
  277. x6500->api->name, x6500->device_id, fpgaid);
  278. if (!x6500_fpga_upload_bitstream(x6500, jp))
  279. return false;
  280. } else
  281. applog(LOG_DEBUG, "%s %u.%u: FPGA is already programmed :)",
  282. x6500->api->name, x6500->device_id, fpgaid);
  283. thr->cgpu_data = fpga;
  284. x6500_set_register(jp, 0xD, 180); // Set clock speed
  285. mutex_unlock(&x6500->device_mutex);
  286. return true;
  287. }
  288. static void
  289. get_x6500_statline_before(char *buf, struct cgpu_info *x6500)
  290. {
  291. char info[18] = " | ";
  292. char pdone = (char)(x6500->cgpu_data);
  293. if (pdone != 101) {
  294. sprintf(&info[1], "%3d%%", pdone);
  295. info[5] = ' ';
  296. strcat(buf, info);
  297. return;
  298. }
  299. strcat(buf, " | ");
  300. }
  301. static
  302. bool x6500_start_work(struct thr_info *thr, struct work *work)
  303. {
  304. struct cgpu_info *x6500 = thr->cgpu;
  305. struct x6500_fpga_data *fpga = thr->cgpu_data;
  306. struct jtag_port *jp = &fpga->jtag;
  307. char fpgaid = thr->device_thread;
  308. mutex_lock(&x6500->device_mutex);
  309. for (int i = 1, j = 0; i < 9; ++i, j += 4)
  310. x6500_set_register(jp, i, fromlebytes(work->midstate, j));
  311. for (int i = 9, j = 64; i < 12; ++i, j += 4)
  312. x6500_set_register(jp, i, fromlebytes(work->data, j));
  313. //gettimeofday(&fpga->tv_workstart, NULL);
  314. mutex_unlock(&x6500->device_mutex);
  315. if (opt_debug) {
  316. char *xdata = bin2hex(work->data, 80);
  317. applog(LOG_DEBUG, "%s %u.%u: Started work: %s",
  318. x6500->api->name, x6500->device_id, fpgaid, xdata);
  319. free(xdata);
  320. }
  321. return true;
  322. }
  323. static
  324. int64_t x6500_process_results(struct thr_info *thr, struct work *work)
  325. {
  326. struct cgpu_info *x6500 = thr->cgpu;
  327. struct x6500_fpga_data *fpga = thr->cgpu_data;
  328. struct jtag_port *jtag = &fpga->jtag;
  329. char fpgaid = thr->device_thread;
  330. uint32_t nonce;
  331. long iter;
  332. bool bad;
  333. iter = 20;
  334. while (1) {
  335. mutex_lock(&x6500->device_mutex);
  336. nonce = x6500_get_register(jtag, 0xE);
  337. mutex_unlock(&x6500->device_mutex);
  338. if (nonce != 0xffffffff) {
  339. bad = !test_nonce(work, nonce, false);
  340. if (!bad) {
  341. submit_nonce(thr, work, nonce);
  342. applog(LOG_DEBUG, "%s %u.%u: Nonce for current work: %08lx",
  343. x6500->api->name, x6500->device_id, fpgaid,
  344. (unsigned long)nonce);
  345. } else {
  346. applog(LOG_DEBUG, "%s %u.%u: Nonce with H not zero : %08lx",
  347. x6500->api->name, x6500->device_id, fpgaid,
  348. (unsigned long)nonce);
  349. ++hw_errors;
  350. ++x6500->hw_errors;
  351. }
  352. }
  353. if (thr->work_restart || !--iter)
  354. break;
  355. usleep(1000);
  356. if (thr->work_restart)
  357. break;
  358. }
  359. return 0xffffffff;
  360. }
  361. static int64_t
  362. x6500_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  363. {
  364. if (!x6500_start_work(thr, work))
  365. return -1;
  366. int64_t hashes = x6500_process_results(thr, work);
  367. if (hashes > 0)
  368. work->blk.nonce += hashes;
  369. return hashes;
  370. }
  371. struct device_api x6500_api = {
  372. .dname = "x6500",
  373. .name = "XBS",
  374. .api_detect = x6500_detect,
  375. .thread_prepare = x6500_prepare,
  376. .thread_init = x6500_fpga_init,
  377. .get_statline_before = get_x6500_statline_before,
  378. .scanhash = x6500_scanhash,
  379. // .thread_shutdown = x6500_fpga_shutdown,
  380. };