driver-dualminer.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2014 Nate Woolls
  4. * Copyright 2014 Dualminer Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <strings.h>
  18. #include "miner.h"
  19. #include "driver-icarus.h"
  20. #include "lowlevel.h"
  21. #include "lowl-vcom.h"
  22. #include "deviceapi.h"
  23. #include "logging.h"
  24. #include "util.h"
  25. #include "gc3355.h"
  26. #ifndef WIN32
  27. #include <sys/ioctl.h>
  28. #else
  29. #include <io.h>
  30. #endif
  31. #define DUALMINER_IO_SPEED 115200
  32. #define DUALMINER_SCRYPT_SM_HASH_TIME 0.00001428571429
  33. #define DUALMINER_SCRYPT_DM_HASH_TIME 0.00003333333333
  34. #define DUALMINER_SHA2_DM_HASH_TIME 0.00000000300000
  35. #define DUALMINER_SCRYPT_READ_COUNT 48 // 4.8s to read
  36. #define DUALMINER_SHA2_READ_COUNT 16 // 1.6s to read
  37. #define DUALMINER_0_9V_SHA2_UNITS 60
  38. #define DUALMINER_1_2V_SHA2_UNITS 0
  39. #define DUALMINER_DM_DEFAULT_FREQUENCY 550
  40. #define DUALMINER_SM_DEFAULT_FREQUENCY 850
  41. static
  42. const char sha2_golden_ob[] =
  43. "55aa0f00a08701004a548fe471fa3a9a"
  44. "1371144556c3f64d2500b4826008fe4b"
  45. "bf7698c94eba7946ce22a72f4f672614"
  46. "1a0b3287";
  47. static
  48. const char sha2_golden_nonce[] = "a2870100";
  49. static
  50. const char scrypt_golden_ob[] =
  51. "55aa1f00000000000000000000000000"
  52. "000000000000000000000000aaaaaaaa"
  53. "711c0000603ebdb6e35b05223c54f815"
  54. "5ac33123006b4192e7aafafbeb9ef654"
  55. "4d2973d700000002069b9f9e3ce8a677"
  56. "8dea3d7a00926cd6eaa9585502c9b83a"
  57. "5601f198d7fbf09be9559d6335ebad36"
  58. "3e4f147a8d9934006963030b4e54c408"
  59. "c837ebc2eeac129852a55fee1b1d88f6"
  60. "000c050000000600";
  61. static
  62. const char scrypt_golden_nonce[] = "dd0c0500";
  63. BFG_REGISTER_DRIVER(dualminer_drv)
  64. static
  65. const struct bfg_set_device_definition dualminer_set_device_funcs[];
  66. // device helper functions
  67. static
  68. void dualminer_bootstrap_device(int fd)
  69. {
  70. gc3355_reset_dtr(fd);
  71. if (opt_scrypt && !opt_dual_mode)
  72. gc3355_opt_scrypt_only_init(fd);
  73. else
  74. gc3355_dualminer_init(fd);
  75. cgsleep_ms(1);
  76. }
  77. static
  78. void dualminer_teardown_device(int fd)
  79. {
  80. // set data terminal ready (DTR) status
  81. set_serial_dtr(fd, BGV_HIGH);
  82. // set request to send (RTS) status
  83. set_serial_rts(fd, BGV_LOW);
  84. }
  85. static
  86. bool dualminer_init(struct thr_info * const thr)
  87. {
  88. struct cgpu_info * const cgpu = thr->cgpu;
  89. if (opt_scrypt)
  90. cgpu->min_nonce_diff = 1./0x10000;
  91. return icarus_init(thr);
  92. }
  93. // runs when job starts and the device has been reset (or first run)
  94. static
  95. void dualminer_init_firstrun(struct cgpu_info *icarus)
  96. {
  97. struct ICARUS_INFO *info = icarus->device_data;
  98. int fd = icarus->device_fd;
  99. dualminer_bootstrap_device(fd);
  100. if (opt_scrypt)
  101. set_serial_rts(fd, BGV_HIGH);
  102. gc3355_init(fd, opt_sha2_units, !opt_dual_mode);
  103. applog(LOG_DEBUG, "%"PRIpreprv": scrypt: %d, scrypt only: %d\n", icarus->proc_repr, opt_scrypt, opt_scrypt);
  104. if (gc3355_get_cts_status(fd) != 1)
  105. {
  106. // Scrypt + SHA2 mode
  107. if (opt_scrypt)
  108. info->Hs = DUALMINER_SCRYPT_DM_HASH_TIME;
  109. }
  110. applog(LOG_DEBUG, "%"PRIpreprv": dualminer: Init: pll=%d, sha2num=%d", icarus->proc_repr, opt_pll_freq, opt_sha2_units);
  111. }
  112. // set defaults for options that the user didn't specify
  113. static
  114. void dualminer_set_defaults(int fd)
  115. {
  116. // set opt_sha2_units defaults depending on dip-switch
  117. if (opt_sha2_units == -1)
  118. {
  119. // get clear to send (CTS) status
  120. if (gc3355_get_cts_status(fd) == 1)
  121. opt_sha2_units = DUALMINER_1_2V_SHA2_UNITS; // dip-switch in L position
  122. else
  123. opt_sha2_units = DUALMINER_0_9V_SHA2_UNITS; // dip-switch in B position
  124. }
  125. // set opt_pll_freq defaults depending on dip-switch
  126. if (opt_pll_freq <= 0)
  127. {
  128. // get clear to send (CTS) status
  129. if (gc3355_get_cts_status(fd) == 1)
  130. opt_pll_freq = DUALMINER_SM_DEFAULT_FREQUENCY; // 1.2v - dip-switch in L position
  131. else
  132. opt_pll_freq = DUALMINER_DM_DEFAULT_FREQUENCY; // 0.9v - dip-switch in B position
  133. }
  134. }
  135. // ICARUS_INFO functions - icarus-common.h
  136. // runs after fd is opened but before the device detection code
  137. static
  138. bool dualminer_detect_init(const char *devpath, int fd, struct ICARUS_INFO * __maybe_unused info)
  139. {
  140. dualminer_set_defaults(fd);
  141. dualminer_bootstrap_device(fd);
  142. return true;
  143. }
  144. // runs each time a job starts
  145. static
  146. bool dualminer_job_start(struct thr_info * const thr)
  147. {
  148. struct cgpu_info *icarus = thr->cgpu;
  149. struct icarus_state * const state = thr->cgpu_data;
  150. int fd = icarus->device_fd;
  151. if (state->firstrun)
  152. // runs when job starts and the device has been reset (or first run)
  153. dualminer_init_firstrun(icarus);
  154. if (opt_scrypt)
  155. {
  156. if (opt_dual_mode)
  157. gc3355_dualminer_init(fd);
  158. else
  159. gc3355_scrypt_only_reset(fd);
  160. }
  161. return icarus_job_start(thr);
  162. }
  163. // device detection
  164. static
  165. bool dualminer_detect_one(const char *devpath)
  166. {
  167. struct device_drv *drv = &dualminer_drv;
  168. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  169. if (unlikely(!info))
  170. quit(1, "Failed to malloc ICARUS_INFO");
  171. *info = (struct ICARUS_INFO){
  172. .baud = DUALMINER_IO_SPEED,
  173. .timing_mode = MODE_DEFAULT,
  174. .do_icarus_timing = false,
  175. .nonce_littleendian = true,
  176. .work_division = 2,
  177. .fpga_count = 2,
  178. .detect_init_func = dualminer_detect_init,
  179. .job_start_func = dualminer_job_start
  180. };
  181. if (opt_scrypt)
  182. {
  183. info->golden_ob = (char*)scrypt_golden_ob;
  184. info->golden_nonce = (char*)scrypt_golden_nonce;
  185. info->Hs = DUALMINER_SCRYPT_SM_HASH_TIME;
  186. }
  187. else
  188. {
  189. info->golden_ob = (char*)sha2_golden_ob;
  190. info->golden_nonce = (char*)sha2_golden_nonce;
  191. info->Hs = DUALMINER_SHA2_DM_HASH_TIME;
  192. }
  193. drv_set_defaults(drv, dualminer_set_device_funcs, info, devpath, detectone_meta_info.serial, 1);
  194. if (!icarus_detect_custom(devpath, drv, info))
  195. {
  196. free(info);
  197. return false;
  198. }
  199. if (opt_scrypt)
  200. info->read_count = DUALMINER_SCRYPT_READ_COUNT; // 4.8s to read
  201. else
  202. info->read_count = DUALMINER_SHA2_READ_COUNT; // 1.6s to read
  203. return true;
  204. }
  205. // support for --set-device dualminer:dual_mode=1
  206. // most be set before probing the device
  207. static
  208. const char *dualminer_set_dual_mode(struct cgpu_info * const proc, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  209. {
  210. int val = atoi(setting);
  211. opt_dual_mode = val == 1;
  212. return NULL;
  213. }
  214. static
  215. const struct bfg_set_device_definition dualminer_set_device_funcs[] = {
  216. {"dual_mode", dualminer_set_dual_mode, "set to 1 to enable dual algorithm mining with two BFGMiner processes"},
  217. {NULL},
  218. };
  219. // device_drv functions - miner.h
  220. static
  221. bool dualminer_lowl_probe(const struct lowlevel_device_info * const info)
  222. {
  223. return vcom_lowl_probe_wrapper(info, dualminer_detect_one);
  224. }
  225. static
  226. void dualminer_thread_shutdown(struct thr_info *thr)
  227. {
  228. // dualminer teardown
  229. dualminer_teardown_device(thr->cgpu->device_fd);
  230. // icarus teardown
  231. do_icarus_close(thr);
  232. free(thr->cgpu_data);
  233. }
  234. static
  235. bool dualminer_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  236. {
  237. struct cgpu_info * const icarus = thr->cgpu;
  238. struct icarus_state * const state = thr->cgpu_data;
  239. struct ICARUS_INFO * const info = icarus->device_data;
  240. memset(state->ob_bin, 0, info->ob_size);
  241. if (opt_scrypt)
  242. gc3355_scrypt_prepare_work(state->ob_bin, work);
  243. else
  244. gc3355_sha2_prepare_work(state->ob_bin, work, false);
  245. return true;
  246. }
  247. // support for --set-device dualminer:clock=freq
  248. static
  249. char *dualminer_set_device(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  250. {
  251. if (strcasecmp(option, "clock") == 0)
  252. {
  253. int val = atoi(setting);
  254. opt_pll_freq = val;
  255. return NULL;
  256. }
  257. sprintf(replybuf, "Unknown option: %s", option);
  258. return replybuf;
  259. }
  260. // device_drv definition - miner.h
  261. static
  262. void dualminer_drv_init()
  263. {
  264. dualminer_drv = icarus_drv;
  265. dualminer_drv.dname = "dualminer";
  266. dualminer_drv.name = "DMU";
  267. dualminer_drv.supported_algos = POW_SCRYPT | POW_SHA256D;
  268. dualminer_drv.lowl_probe = dualminer_lowl_probe;
  269. dualminer_drv.thread_init = dualminer_init;
  270. dualminer_drv.thread_shutdown = dualminer_thread_shutdown;
  271. dualminer_drv.job_prepare = dualminer_job_prepare;
  272. dualminer_drv.set_device = dualminer_set_device;
  273. ++dualminer_drv.probe_priority;
  274. }
  275. struct device_drv dualminer_drv =
  276. {
  277. .drv_init = dualminer_drv_init,
  278. };