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