driver-dualminer.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright 2013-2015 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_TIMEOUT_MS 4800 // 4.8s to read
  36. #define DUALMINER_SHA2_READ_TIMEOUT_MS 1600 // 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 inline
  68. bool dualminer_is_scrypt(struct ICARUS_INFO * const info)
  69. {
  70. #ifdef USE_SCRYPT
  71. return info->scrypt;
  72. #else
  73. return false;
  74. #endif
  75. }
  76. static
  77. void dualminer_teardown_device(int fd)
  78. {
  79. // set data terminal ready (DTR) status
  80. set_serial_dtr(fd, BGV_HIGH);
  81. // set request to send (RTS) status
  82. set_serial_rts(fd, BGV_LOW);
  83. }
  84. static
  85. void dualminer_init_hashrate(struct cgpu_info * const cgpu)
  86. {
  87. int fd = cgpu->device_fd;
  88. struct ICARUS_INFO *info = cgpu->device_data;
  89. // get clear to send (CTS) status
  90. if ((gc3355_get_cts_status(fd) != 1) && // 0.9v - dip-switch set to B
  91. (dualminer_is_scrypt(info)))
  92. // adjust hash-rate for voltage
  93. info->Hs = DUALMINER_SCRYPT_DM_HASH_TIME;
  94. }
  95. // runs when job starts and the device has been reset (or first run)
  96. static
  97. void dualminer_init_firstrun(struct cgpu_info *icarus)
  98. {
  99. struct ICARUS_INFO * const info = icarus->device_data;
  100. int fd = icarus->device_fd;
  101. gc3355_init_dualminer(fd, opt_pll_freq, !info->dual_mode, false, dualminer_is_scrypt(info));
  102. dualminer_init_hashrate(icarus);
  103. applog(LOG_DEBUG, "%"PRIpreprv": dualminer: Init: pll=%d, scrypt: %d, scrypt only: %d",
  104. icarus->proc_repr,
  105. opt_pll_freq,
  106. dualminer_is_scrypt(info),
  107. dualminer_is_scrypt(info) && !info->dual_mode);
  108. }
  109. // set defaults for options that the user didn't specify
  110. static
  111. void dualminer_set_defaults(int fd)
  112. {
  113. // set opt_sha2_units defaults depending on dip-switch
  114. if (opt_sha2_units == -1)
  115. {
  116. // get clear to send (CTS) status
  117. if (gc3355_get_cts_status(fd) == 1)
  118. opt_sha2_units = DUALMINER_1_2V_SHA2_UNITS; // dip-switch in L position
  119. else
  120. opt_sha2_units = DUALMINER_0_9V_SHA2_UNITS; // dip-switch in B position
  121. }
  122. // set opt_pll_freq defaults depending on dip-switch
  123. if (opt_pll_freq <= 0)
  124. {
  125. // get clear to send (CTS) status
  126. if (gc3355_get_cts_status(fd) == 1)
  127. opt_pll_freq = DUALMINER_SM_DEFAULT_FREQUENCY; // 1.2v - dip-switch in L position
  128. else
  129. opt_pll_freq = DUALMINER_DM_DEFAULT_FREQUENCY; // 0.9v - dip-switch in B position
  130. }
  131. }
  132. float dualminer_min_nonce_diff(struct cgpu_info * const proc, const struct mining_algorithm * const malgo)
  133. {
  134. struct ICARUS_INFO * const info = proc ? proc->device_data : NULL;
  135. switch (malgo->algo)
  136. {
  137. #ifdef USE_SCRYPT
  138. case POW_SCRYPT:
  139. return ((!info) || dualminer_is_scrypt(info)) ? (1./0x10000) : -1.;
  140. #endif
  141. #ifdef USE_SHA256D
  142. case POW_SHA256D:
  143. return (info && dualminer_is_scrypt(info)) ? -1. : 1.;
  144. #endif
  145. default:
  146. return -1.;
  147. }
  148. }
  149. // ICARUS_INFO functions - icarus-common.h
  150. // runs after fd is opened but before the device detection code
  151. static
  152. bool dualminer_detect_init(const char *devpath, int fd, struct ICARUS_INFO * const info)
  153. {
  154. dualminer_set_defaults(fd);
  155. gc3355_init_dualminer(fd, opt_pll_freq, !info->dual_mode, true, dualminer_is_scrypt(info));
  156. return true;
  157. }
  158. // runs each time a job starts
  159. static
  160. bool dualminer_job_start(struct thr_info * const thr)
  161. {
  162. struct cgpu_info *icarus = thr->cgpu;
  163. struct ICARUS_INFO * const info = icarus->device_data;
  164. struct icarus_state * const state = thr->cgpu_data;
  165. int fd = icarus->device_fd;
  166. if (state->firstrun)
  167. // runs when job starts and the device has been reset (or first run)
  168. dualminer_init_firstrun(icarus);
  169. if (dualminer_is_scrypt(info))
  170. {
  171. if (info->dual_mode)
  172. gc3355_scrypt_reset(fd);
  173. else
  174. gc3355_scrypt_only_reset(fd);
  175. // See https://github.com/gridseed/gc3355-doc/blob/master/GC3355_DataSheet.pdf
  176. // WAIT: Before start a new transaction, WAIT Cycle must be inserted.
  177. // WAIT Cycle value is programmable register in UART and default wait
  178. // time is UART receive 32 bits time (One DATA Cycle).
  179. // Note: prevents register corruption
  180. cgsleep_ms(100);
  181. }
  182. return icarus_job_start(thr);
  183. }
  184. // device detection
  185. static
  186. bool dualminer_detect_one(const char *devpath)
  187. {
  188. struct device_drv *drv = &dualminer_drv;
  189. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  190. if (unlikely(!info))
  191. quit(1, "Failed to malloc ICARUS_INFO");
  192. *info = (struct ICARUS_INFO){
  193. .baud = DUALMINER_IO_SPEED,
  194. .timing_mode = MODE_DEFAULT,
  195. .do_icarus_timing = false,
  196. .nonce_littleendian = true,
  197. .work_division = 1,
  198. .detect_init_func = dualminer_detect_init,
  199. .job_start_func = dualminer_job_start,
  200. #ifdef USE_SCRYPT
  201. .scrypt = (get_mining_goal("default")->malgo->algo == POW_SCRYPT),
  202. #endif
  203. };
  204. drv_set_defaults(drv, dualminer_set_device_funcs, info, devpath, detectone_meta_info.serial, 1);
  205. if (dualminer_is_scrypt(info))
  206. {
  207. info->golden_ob = (char*)scrypt_golden_ob;
  208. info->golden_nonce = (char*)scrypt_golden_nonce;
  209. info->Hs = DUALMINER_SCRYPT_SM_HASH_TIME;
  210. }
  211. else
  212. {
  213. info->golden_ob = (char*)sha2_golden_ob;
  214. info->golden_nonce = (char*)sha2_golden_nonce;
  215. info->Hs = DUALMINER_SHA2_DM_HASH_TIME;
  216. }
  217. if (!icarus_detect_custom(devpath, drv, info))
  218. {
  219. free(info);
  220. return false;
  221. }
  222. if (dualminer_is_scrypt(info))
  223. info->read_timeout_ms = DUALMINER_SCRYPT_READ_TIMEOUT_MS; // 4.8s to read
  224. else
  225. info->read_timeout_ms = DUALMINER_SHA2_READ_TIMEOUT_MS; // 1.6s to read
  226. return true;
  227. }
  228. // support for --set-device dualminer:dual_mode=1
  229. // most be set before probing the device
  230. static
  231. 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)
  232. {
  233. struct ICARUS_INFO * const info = proc->device_data;
  234. int val = atoi(setting);
  235. info->dual_mode = val == 1;
  236. return NULL;
  237. }
  238. #ifdef USE_SCRYPT
  239. static
  240. const char *dualminer_set_scrypt(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  241. {
  242. struct ICARUS_INFO * const info = proc->device_data;
  243. info->scrypt = atoi(newvalue);
  244. return NULL;
  245. }
  246. #endif
  247. static
  248. const struct bfg_set_device_definition dualminer_set_device_funcs[] = {
  249. {"dual_mode", dualminer_set_dual_mode, "set to 1 to enable dual algorithm mining"},
  250. #ifdef USE_SCRYPT
  251. {"scrypt", dualminer_set_scrypt, "set to 1 to put in scrypt mode"},
  252. #endif
  253. {NULL},
  254. };
  255. // device_drv functions - miner.h
  256. static
  257. bool dualminer_lowl_probe(const struct lowlevel_device_info * const info)
  258. {
  259. return vcom_lowl_probe_wrapper(info, dualminer_detect_one);
  260. }
  261. static
  262. void dualminer_thread_shutdown(struct thr_info *thr)
  263. {
  264. // dualminer teardown
  265. dualminer_teardown_device(thr->cgpu->device_fd);
  266. // icarus teardown
  267. do_icarus_close(thr);
  268. free(thr->cgpu_data);
  269. }
  270. static
  271. bool dualminer_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  272. {
  273. struct cgpu_info * const icarus = thr->cgpu;
  274. struct icarus_state * const state = thr->cgpu_data;
  275. struct ICARUS_INFO * const info = icarus->device_data;
  276. memset(state->ob_bin, 0, info->ob_size);
  277. if (dualminer_is_scrypt(info))
  278. gc3355_scrypt_prepare_work(state->ob_bin, work);
  279. else
  280. gc3355_sha2_prepare_work(state->ob_bin, work);
  281. return true;
  282. }
  283. // support for --set-device dualminer:clock=freq
  284. static
  285. char *dualminer_set_device(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  286. {
  287. if (strcasecmp(option, "clock") == 0)
  288. {
  289. int val = atoi(setting);
  290. opt_pll_freq = val;
  291. return NULL;
  292. }
  293. sprintf(replybuf, "Unknown option: %s", option);
  294. return replybuf;
  295. }
  296. // device_drv definition - miner.h
  297. static
  298. void dualminer_drv_init()
  299. {
  300. dualminer_drv = icarus_drv;
  301. dualminer_drv.dname = "dualminer";
  302. dualminer_drv.name = "DMU";
  303. dualminer_drv.drv_min_nonce_diff = dualminer_min_nonce_diff;
  304. dualminer_drv.lowl_probe = dualminer_lowl_probe;
  305. dualminer_drv.thread_shutdown = dualminer_thread_shutdown;
  306. dualminer_drv.job_prepare = dualminer_job_prepare;
  307. dualminer_drv.set_device = dualminer_set_device;
  308. // currently setup specifically to probe after ZeusMiner
  309. dualminer_drv.probe_priority = -50;
  310. }
  311. struct device_drv dualminer_drv =
  312. {
  313. .drv_init = dualminer_drv_init,
  314. };