driver-dualminer.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. static
  40. const char sha2_golden_ob[] =
  41. "55aa0f00a08701004a548fe471fa3a9a"
  42. "1371144556c3f64d2500b4826008fe4b"
  43. "bf7698c94eba7946ce22a72f4f672614"
  44. "1a0b3287";
  45. static
  46. const char sha2_golden_nonce[] = "a2870100";
  47. static
  48. const char scrypt_golden_ob[] =
  49. "55aa1f00000000000000000000000000"
  50. "000000000000000000000000aaaaaaaa"
  51. "711c0000603ebdb6e35b05223c54f815"
  52. "5ac33123006b4192e7aafafbeb9ef654"
  53. "4d2973d700000002069b9f9e3ce8a677"
  54. "8dea3d7a00926cd6eaa9585502c9b83a"
  55. "5601f198d7fbf09be9559d6335ebad36"
  56. "3e4f147a8d9934006963030b4e54c408"
  57. "c837ebc2eeac129852a55fee1b1d88f6"
  58. "000c050000000600";
  59. static
  60. const char scrypt_golden_nonce[] = "dd0c0500";
  61. BFG_REGISTER_DRIVER(dualminer_drv)
  62. static
  63. const struct bfg_set_device_definition dualminer_set_device_funcs[];
  64. // device helper functions
  65. static
  66. void dualminer_bootstrap_device(int fd)
  67. {
  68. gc3355_reset_dtr(fd);
  69. if (opt_scrypt && !opt_dual_mode)
  70. gc3355_opt_scrypt_only_init(fd);
  71. else
  72. gc3355_dualminer_init(fd);
  73. cgsleep_ms(1);
  74. }
  75. static
  76. void dualminer_teardown_device(int fd)
  77. {
  78. // set data terminal ready (DTR) status
  79. set_serial_dtr(fd, BGV_HIGH);
  80. // set request to send (RTS) status
  81. set_serial_rts(fd, BGV_LOW);
  82. }
  83. static
  84. bool dualminer_init(struct thr_info * const thr)
  85. {
  86. struct cgpu_info * const cgpu = thr->cgpu;
  87. if (opt_scrypt)
  88. cgpu->min_nonce_diff = 1./0x10000;
  89. return icarus_init(thr);
  90. }
  91. // runs when job starts and the device has been reset (or first run)
  92. static
  93. void dualminer_init_firstrun(struct cgpu_info *icarus)
  94. {
  95. struct ICARUS_INFO *info = icarus->device_data;
  96. int fd = icarus->device_fd;
  97. dualminer_bootstrap_device(fd);
  98. if (opt_scrypt)
  99. set_serial_rts(fd, BGV_HIGH);
  100. if (opt_sha2_units == -1)
  101. {
  102. if (gc3355_get_cts_status(fd) == 1)
  103. {
  104. //1.2v - Scrypt mode
  105. opt_sha2_units = DUALMINER_1_2V_SHA2_UNITS;
  106. }
  107. else
  108. {
  109. //0.9v - Scrypt + SHA mode
  110. opt_sha2_units = DUALMINER_0_9V_SHA2_UNITS;
  111. }
  112. }
  113. gc3355_init(fd, opt_sha2_units, !opt_dual_mode);
  114. applog(LOG_DEBUG, "%"PRIpreprv": scrypt: %d, scrypt only: %d\n", icarus->proc_repr, opt_scrypt, opt_scrypt);
  115. if (gc3355_get_cts_status(fd) != 1)
  116. {
  117. // Scrypt + SHA2 mode
  118. if (opt_scrypt)
  119. info->Hs = DUALMINER_SCRYPT_DM_HASH_TIME;
  120. }
  121. applog(LOG_DEBUG, "%"PRIpreprv": dualminer: Init: pll=%d, sha2num=%d", icarus->proc_repr, opt_pll_freq, opt_sha2_units);
  122. }
  123. // ICARUS_INFO functions - icarus-common.h
  124. // runs after fd is opened but before the device detection code
  125. static
  126. bool dualminer_detect_init(const char *devpath, int fd, struct ICARUS_INFO * __maybe_unused info)
  127. {
  128. dualminer_bootstrap_device(fd);
  129. return true;
  130. }
  131. // runs each time a job starts
  132. static
  133. bool dualminer_job_start(struct thr_info * const thr)
  134. {
  135. struct cgpu_info *icarus = thr->cgpu;
  136. struct icarus_state * const state = thr->cgpu_data;
  137. int fd = icarus->device_fd;
  138. if (state->firstrun)
  139. // runs when job starts and the device has been reset (or first run)
  140. dualminer_init_firstrun(icarus);
  141. if (opt_scrypt)
  142. {
  143. if (opt_dual_mode)
  144. gc3355_dualminer_init(fd);
  145. else
  146. gc3355_scrypt_only_reset(fd);
  147. }
  148. return icarus_job_start(thr);
  149. }
  150. // device detection
  151. static
  152. bool dualminer_detect_one(const char *devpath)
  153. {
  154. struct device_drv *drv = &dualminer_drv;
  155. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  156. if (unlikely(!info))
  157. quit(1, "Failed to malloc ICARUS_INFO");
  158. *info = (struct ICARUS_INFO){
  159. .baud = DUALMINER_IO_SPEED,
  160. .timing_mode = MODE_DEFAULT,
  161. .do_icarus_timing = false,
  162. .nonce_littleendian = true,
  163. .work_division = 2,
  164. .fpga_count = 2,
  165. .detect_init_func = dualminer_detect_init,
  166. .job_start_func = dualminer_job_start
  167. };
  168. if (opt_scrypt)
  169. {
  170. info->golden_ob = (char*)scrypt_golden_ob;
  171. info->golden_nonce = (char*)scrypt_golden_nonce;
  172. info->Hs = DUALMINER_SCRYPT_SM_HASH_TIME;
  173. }
  174. else
  175. {
  176. info->golden_ob = (char*)sha2_golden_ob;
  177. info->golden_nonce = (char*)sha2_golden_nonce;
  178. info->Hs = DUALMINER_SHA2_DM_HASH_TIME;
  179. }
  180. drv_set_defaults(drv, dualminer_set_device_funcs, info, devpath, detectone_meta_info.serial, 1);
  181. if (!icarus_detect_custom(devpath, drv, info))
  182. {
  183. free(info);
  184. return false;
  185. }
  186. if (opt_scrypt)
  187. info->read_count = DUALMINER_SCRYPT_READ_COUNT; // 4.8s to read
  188. else
  189. info->read_count = DUALMINER_SHA2_READ_COUNT; // 1.6s to read
  190. return true;
  191. }
  192. // support for --set-device dualminer:dual_mode=1
  193. // most be set before probing the device
  194. static
  195. 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)
  196. {
  197. int val = atoi(setting);
  198. opt_dual_mode = val == 1;
  199. return NULL;
  200. }
  201. static
  202. const struct bfg_set_device_definition dualminer_set_device_funcs[] = {
  203. {"dual_mode", dualminer_set_dual_mode, "set to 1 to enable dual algorithm mining with two BFGMiner processes"},
  204. {NULL},
  205. };
  206. // device_drv functions - miner.h
  207. static
  208. bool dualminer_lowl_probe(const struct lowlevel_device_info * const info)
  209. {
  210. return vcom_lowl_probe_wrapper(info, dualminer_detect_one);
  211. }
  212. static
  213. void dualminer_thread_shutdown(struct thr_info *thr)
  214. {
  215. // dualminer teardown
  216. dualminer_teardown_device(thr->cgpu->device_fd);
  217. // icarus teardown
  218. do_icarus_close(thr);
  219. free(thr->cgpu_data);
  220. }
  221. static
  222. bool dualminer_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  223. {
  224. struct cgpu_info * const icarus = thr->cgpu;
  225. struct icarus_state * const state = thr->cgpu_data;
  226. struct ICARUS_INFO * const info = icarus->device_data;
  227. memset(state->ob_bin, 0, info->ob_size);
  228. if (opt_scrypt)
  229. gc3355_scrypt_prepare_work(state->ob_bin, work);
  230. else
  231. gc3355_sha2_prepare_work(state->ob_bin, work, false);
  232. return true;
  233. }
  234. // support for --set-device dualminer:clock=freq
  235. static
  236. char *dualminer_set_device(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  237. {
  238. if (strcasecmp(option, "clock") == 0)
  239. {
  240. int val = atoi(setting);
  241. opt_pll_freq = val;
  242. return NULL;
  243. }
  244. sprintf(replybuf, "Unknown option: %s", option);
  245. return replybuf;
  246. }
  247. // device_drv definition - miner.h
  248. static
  249. void dualminer_drv_init()
  250. {
  251. dualminer_drv = icarus_drv;
  252. dualminer_drv.dname = "dualminer";
  253. dualminer_drv.name = "DMU";
  254. dualminer_drv.supported_algos = POW_SCRYPT | POW_SHA256D;
  255. dualminer_drv.lowl_probe = dualminer_lowl_probe;
  256. dualminer_drv.thread_init = dualminer_init;
  257. dualminer_drv.thread_shutdown = dualminer_thread_shutdown;
  258. dualminer_drv.job_prepare = dualminer_job_prepare;
  259. dualminer_drv.set_device = dualminer_set_device;
  260. ++dualminer_drv.probe_priority;
  261. }
  262. struct device_drv dualminer_drv =
  263. {
  264. .drv_init = dualminer_drv_init,
  265. };