driver-antminer.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright 2013-2015 Luke Dashjr
  3. * Copyright 2013-2014 Nate Woolls
  4. * Copyright 2013 Lingchao Xu
  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. #define ANTMINER_IO_SPEED 115200
  26. // ANTMINER_HASH_TIME is for U1/U2 only
  27. #define ANTMINER_HASH_TIME 0.0000000004761
  28. #define ANTMINER_STATUS_LEN 5
  29. #define ANTMINER_COMMAND_PREFIX 128
  30. #define ANTMINER_COMMAND_LED 1
  31. #define ANTMINER_COMMAND_ON 1
  32. #define ANTMINER_COMMAND_OFFSET 32
  33. BFG_REGISTER_DRIVER(antminer_drv)
  34. BFG_REGISTER_DRIVER(compac_drv)
  35. static
  36. const struct bfg_set_device_definition antminer_set_device_funcs[];
  37. static
  38. bool antminer_detect_one_with_drv(const char * const devpath, struct device_drv * const drv)
  39. {
  40. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  41. if (unlikely(!info))
  42. quit(1, "Failed to malloc ICARUS_INFO");
  43. *info = (struct ICARUS_INFO){
  44. .baud = ANTMINER_IO_SPEED,
  45. .Hs = ANTMINER_HASH_TIME,
  46. .timing_mode = MODE_LONG,
  47. .do_icarus_timing = true,
  48. .read_size = 5,
  49. .reopen_mode = IRM_NEVER,
  50. };
  51. struct cgpu_info * const dev = icarus_detect_custom(devpath, drv, info);
  52. if (!dev)
  53. {
  54. free(info);
  55. return false;
  56. }
  57. dev->set_device_funcs = antminer_set_device_funcs;
  58. info->read_timeout_ms = 75;
  59. return true;
  60. }
  61. static bool antminer_detect_one(const char * const devpath)
  62. {
  63. return antminer_detect_one_with_drv(devpath, &antminer_drv);
  64. }
  65. static
  66. bool antminer_lowl_match(const struct lowlevel_device_info * const info)
  67. {
  68. return lowlevel_match_lowlproduct(info, &lowl_vcom, "Antminer");
  69. }
  70. static
  71. bool antminer_lowl_probe(const struct lowlevel_device_info * const info)
  72. {
  73. return vcom_lowl_probe_wrapper(info, antminer_detect_one);
  74. }
  75. // Not used for anything, and needs to read a result for every chip
  76. #if 0
  77. static
  78. char *antminer_get_clock(struct cgpu_info *cgpu, char *replybuf)
  79. {
  80. uint8_t rdreg_buf[4] = {0};
  81. unsigned char rebuf[ANTMINER_STATUS_LEN] = {0};
  82. struct timeval tv_now;
  83. struct timeval tv_timeout, tv_finish;
  84. rdreg_buf[0] = 4;
  85. rdreg_buf[0] |= 0x80;
  86. rdreg_buf[1] = 0; //16-23
  87. rdreg_buf[2] = 0x04; // 8-15
  88. rdreg_buf[3] = crc5usb(rdreg_buf, 27);
  89. applog(LOG_DEBUG, "%"PRIpreprv": Get clock: %02x%02x%02x%02x", cgpu->proc_repr, rdreg_buf[0], rdreg_buf[1], rdreg_buf[2], rdreg_buf[3]);
  90. timer_set_now(&tv_now);
  91. int err = icarus_write(cgpu->proc_repr, cgpu->device_fd, rdreg_buf, sizeof(rdreg_buf));
  92. if (err != 0)
  93. {
  94. sprintf(replybuf, "invalid send get clock: comms error (err=%d)", err);
  95. return replybuf;
  96. }
  97. applog(LOG_DEBUG, "%"PRIpreprv": Get clock: OK", cgpu->proc_repr);
  98. memset(rebuf, 0, sizeof(rebuf));
  99. timer_set_delay(&tv_timeout, &tv_now, 1000000);
  100. err = icarus_read(cgpu->proc_repr, rebuf, cgpu->device_fd, &tv_finish, NULL, &tv_timeout, &tv_now, ANTMINER_STATUS_LEN);
  101. // Timeout is ok - checking specifically for an error here
  102. if (err == ICA_GETS_ERROR)
  103. {
  104. sprintf(replybuf, "invalid recv get clock: comms error (err=%d)", err);
  105. return replybuf;
  106. }
  107. applog(LOG_DEBUG, "%"PRIpreprv": Get clock: %02x%02x%02x%02x%02x", cgpu->proc_repr, rebuf[0], rebuf[1], rebuf[2], rebuf[3], rebuf[4]);
  108. return NULL;
  109. }
  110. #endif
  111. static
  112. const char *antminer_set_clock(struct cgpu_info * const cgpu, const char * const optname, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  113. {
  114. if (!setting || !*setting)
  115. return "missing clock setting";
  116. // For now we only allow hex values that use BITMAINtech's lookup table
  117. // This means values should be prefixed with an x so that later we can
  118. // accept and distinguish decimal values
  119. if (setting[0] != 'x')
  120. {
  121. sprintf(replybuf, "invalid clock: '%s' data must be prefixed with an x", setting);
  122. return replybuf;
  123. }
  124. //remove leading character
  125. const char * const hex_setting = &setting[1];
  126. uint8_t reg_data[4] = {0};
  127. if (!hex2bin(reg_data, hex_setting, strlen(hex_setting) / 2))
  128. {
  129. sprintf(replybuf, "invalid clock: '%s' data must be a hexadecimal value", hex_setting);
  130. return replybuf;
  131. }
  132. uint8_t cmd_buf[4] = {0};
  133. cmd_buf[0] = 2;
  134. cmd_buf[0] |= 0x80;
  135. cmd_buf[1] = reg_data[0]; //16-23
  136. cmd_buf[2] = reg_data[1]; // 8-15
  137. cmd_buf[3] = crc5usb(cmd_buf, 27);
  138. applog(LOG_DEBUG, "%"PRIpreprv": Set clock: %02x%02x%02x%02x", cgpu->proc_repr, cmd_buf[0], cmd_buf[1], cmd_buf[2], cmd_buf[3]);
  139. int err = icarus_write(cgpu->proc_repr, cgpu->device_fd, cmd_buf, sizeof(cmd_buf));
  140. if (err != 0)
  141. {
  142. sprintf(replybuf, "invalid send clock: '%s' comms error (err=%d)", setting, err);
  143. return replybuf;
  144. }
  145. applog(LOG_DEBUG, "%"PRIpreprv": Set clock: OK", cgpu->proc_repr);
  146. // This is confirmed required in order for the clock change to "take"
  147. cgsleep_ms(500);
  148. return NULL;
  149. }
  150. static
  151. const char *antminer_set_voltage(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  152. {
  153. if (!(newvalue && *newvalue))
  154. return "Missing voltage value";
  155. // For now we only allow hex values that use BITMAINtech's lookup table
  156. // This means values should be prefixed with an x so that later we can
  157. // accept and distinguish decimal values
  158. if (newvalue[0] != 'x' || strlen(newvalue) != 4)
  159. invalid_voltage:
  160. return "Only raw voltage configurations are currently supported using 'x' followed by 3 hexadecimal digits";
  161. char voltagecfg_hex[5];
  162. voltagecfg_hex[0] = '0';
  163. memcpy(&voltagecfg_hex[1], &newvalue[1], 3);
  164. voltagecfg_hex[4] = '\0';
  165. uint8_t cmd[4];
  166. if (!hex2bin(&cmd[1], voltagecfg_hex, 2))
  167. goto invalid_voltage;
  168. cmd[0] = 0xaa;
  169. cmd[1] |= 0xb0;
  170. cmd[3] = 0;
  171. cmd[3] = crc5usb(cmd, (4 * 8) - 5);
  172. cmd[3] |= 0xc0;
  173. if (opt_debug)
  174. {
  175. char hex[(4 * 2) + 1];
  176. bin2hex(hex, cmd, 4);
  177. applog(LOG_DEBUG, "%"PRIpreprv": Set voltage: %s", proc->proc_repr, hex);
  178. }
  179. const int err = icarus_write(proc->proc_repr, proc->device_fd, cmd, sizeof(cmd));
  180. if (err)
  181. {
  182. sprintf(replybuf, "Error sending set voltage (err=%d)", err);
  183. return replybuf;
  184. }
  185. applog(LOG_DEBUG, "%"PRIpreprv": Set voltage: OK", proc->proc_repr);
  186. return NULL;
  187. }
  188. static
  189. void antminer_flash_led(const struct cgpu_info *antminer)
  190. {
  191. const int offset = ANTMINER_COMMAND_OFFSET;
  192. uint8_t cmd_buf[4 + offset];
  193. memset(cmd_buf, 0, sizeof(cmd_buf));
  194. cmd_buf[offset + 0] = ANTMINER_COMMAND_PREFIX;
  195. cmd_buf[offset + 1] = ANTMINER_COMMAND_LED;
  196. cmd_buf[offset + 2] = ANTMINER_COMMAND_ON;
  197. cmd_buf[offset + 3] = crc5usb(cmd_buf, sizeof(cmd_buf));
  198. const int fd = antminer->device_fd;
  199. icarus_write(antminer->proc_repr, fd, (char *)(&cmd_buf), sizeof(cmd_buf));
  200. }
  201. static
  202. bool antminer_identify(struct cgpu_info *antminer)
  203. {
  204. for (int i = 0; i < 10; i++)
  205. {
  206. antminer_flash_led(antminer);
  207. cgsleep_ms(250);
  208. }
  209. return true;
  210. }
  211. static
  212. const struct bfg_set_device_definition antminer_set_device_funcs[] = {
  213. {"baud" , icarus_set_baud , "serial baud rate"},
  214. {"work_division", icarus_set_work_division, "number of pieces work is split into"},
  215. {"reopen" , icarus_set_reopen , "how often to reopen device: never, timeout, cycle, (or now for a one-shot reopen)"},
  216. {"timing" , icarus_set_timing , "timing of device; see README.FPGA"},
  217. {"clock", antminer_set_clock, "clock frequency"},
  218. {"voltage", antminer_set_voltage, "voltage ('x' followed by 3 digit hex code)"},
  219. {NULL},
  220. };
  221. static
  222. bool compac_lowl_match(const struct lowlevel_device_info * const info)
  223. {
  224. return lowlevel_match_lowlproduct(info, &lowl_vcom, "Compac", "Bitcoin");
  225. }
  226. static bool compac_detect_one(const char * const devpath)
  227. {
  228. return antminer_detect_one_with_drv(devpath, &compac_drv);
  229. }
  230. static
  231. bool compac_lowl_probe(const struct lowlevel_device_info * const info)
  232. {
  233. return vcom_lowl_probe_wrapper(info, compac_detect_one);
  234. }
  235. static
  236. void antminer_drv_init()
  237. {
  238. antminer_drv = icarus_drv;
  239. antminer_drv.dname = "antminer";
  240. antminer_drv.name = "AMU";
  241. antminer_drv.lowl_match = antminer_lowl_match;
  242. antminer_drv.lowl_probe = antminer_lowl_probe;
  243. antminer_drv.identify_device = antminer_identify;
  244. ++antminer_drv.probe_priority;
  245. compac_drv = antminer_drv;
  246. compac_drv.name = "CBM";
  247. compac_drv.lowl_match = compac_lowl_match;
  248. compac_drv.lowl_probe = compac_lowl_probe;
  249. ++compac_drv.probe_priority;
  250. }
  251. struct device_drv antminer_drv = {
  252. .drv_init = antminer_drv_init,
  253. };
  254. struct device_drv compac_drv = {
  255. .drv_init = antminer_drv_init,
  256. };