driver-antminer.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #define ANTMINER_HASH_TIME 0.0000000004761
  27. #define ANTMINER_STATUS_LEN 5
  28. #define ANTMINER_COMMAND_PREFIX 128
  29. #define ANTMINER_COMMAND_LED 1
  30. #define ANTMINER_COMMAND_ON 1
  31. #define ANTMINER_COMMAND_OFFSET 32
  32. BFG_REGISTER_DRIVER(antminer_drv)
  33. static
  34. const struct bfg_set_device_definition antminer_set_device_funcs[];
  35. static
  36. bool antminer_detect_one(const char *devpath)
  37. {
  38. struct device_drv *drv = &antminer_drv;
  39. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  40. if (unlikely(!info))
  41. quit(1, "Failed to malloc ICARUS_INFO");
  42. *info = (struct ICARUS_INFO){
  43. .baud = ANTMINER_IO_SPEED,
  44. .Hs = ANTMINER_HASH_TIME,
  45. .timing_mode = MODE_DEFAULT,
  46. .read_size = 5,
  47. };
  48. struct cgpu_info * const dev = icarus_detect_custom(devpath, drv, info);
  49. if (!dev)
  50. {
  51. free(info);
  52. return false;
  53. }
  54. dev->set_device_funcs = antminer_set_device_funcs;
  55. info->read_timeout_ms = 1500;
  56. return true;
  57. }
  58. static
  59. bool antminer_lowl_probe(const struct lowlevel_device_info * const info)
  60. {
  61. return vcom_lowl_probe_wrapper(info, antminer_detect_one);
  62. }
  63. static
  64. char *antminer_get_clock(struct cgpu_info *cgpu, char *replybuf)
  65. {
  66. uint8_t rdreg_buf[4] = {0};
  67. unsigned char rebuf[ANTMINER_STATUS_LEN] = {0};
  68. struct timeval tv_now;
  69. struct timeval tv_timeout, tv_finish;
  70. rdreg_buf[0] = 4;
  71. rdreg_buf[0] |= 0x80;
  72. rdreg_buf[1] = 0; //16-23
  73. rdreg_buf[2] = 0x04; // 8-15
  74. rdreg_buf[3] = crc5usb(rdreg_buf, 27);
  75. 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]);
  76. timer_set_now(&tv_now);
  77. int err = icarus_write(cgpu->proc_repr, cgpu->device_fd, rdreg_buf, sizeof(rdreg_buf));
  78. if (err != 0)
  79. {
  80. sprintf(replybuf, "invalid send get clock: comms error (err=%d)", err);
  81. return replybuf;
  82. }
  83. applog(LOG_DEBUG, "%"PRIpreprv": Get clock: OK", cgpu->proc_repr);
  84. memset(rebuf, 0, sizeof(rebuf));
  85. timer_set_delay(&tv_timeout, &tv_now, 1000000);
  86. err = icarus_read(cgpu->proc_repr, rebuf, cgpu->device_fd, &tv_finish, NULL, &tv_timeout, &tv_now, ANTMINER_STATUS_LEN);
  87. // Timeout is ok - checking specifically for an error here
  88. if (err == ICA_GETS_ERROR)
  89. {
  90. sprintf(replybuf, "invalid recv get clock: comms error (err=%d)", err);
  91. return replybuf;
  92. }
  93. applog(LOG_DEBUG, "%"PRIpreprv": Get clock: %02x%02x%02x%02x%02x", cgpu->proc_repr, rebuf[0], rebuf[1], rebuf[2], rebuf[3], rebuf[4]);
  94. return NULL;
  95. }
  96. static
  97. 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)
  98. {
  99. if (!setting || !*setting)
  100. return "missing clock setting";
  101. // For now we only allow hex values that use BITMAINtech's lookup table
  102. // This means values should be prefixed with an x so that later we can
  103. // accept and distinguish decimal values
  104. if (setting[0] != 'x')
  105. {
  106. sprintf(replybuf, "invalid clock: '%s' data must be prefixed with an x", setting);
  107. return replybuf;
  108. }
  109. //remove leading character
  110. const char * const hex_setting = &setting[1];
  111. uint8_t reg_data[4] = {0};
  112. if (!hex2bin(reg_data, hex_setting, strlen(hex_setting) / 2))
  113. {
  114. sprintf(replybuf, "invalid clock: '%s' data must be a hexadecimal value", hex_setting);
  115. return replybuf;
  116. }
  117. uint8_t cmd_buf[4] = {0};
  118. cmd_buf[0] = 2;
  119. cmd_buf[0] |= 0x80;
  120. cmd_buf[1] = reg_data[0]; //16-23
  121. cmd_buf[2] = reg_data[1]; // 8-15
  122. cmd_buf[3] = crc5usb(cmd_buf, 27);
  123. 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]);
  124. int err = icarus_write(cgpu->proc_repr, cgpu->device_fd, cmd_buf, sizeof(cmd_buf));
  125. if (err != 0)
  126. {
  127. sprintf(replybuf, "invalid send clock: '%s' comms error (err=%d)", setting, err);
  128. return replybuf;
  129. }
  130. applog(LOG_DEBUG, "%"PRIpreprv": Set clock: OK", cgpu->proc_repr);
  131. // This is confirmed required in order for the clock change to "take"
  132. cgsleep_ms(500);
  133. return antminer_get_clock(cgpu, replybuf);
  134. }
  135. static
  136. void antminer_flash_led(const struct cgpu_info *antminer)
  137. {
  138. const int offset = ANTMINER_COMMAND_OFFSET;
  139. uint8_t cmd_buf[4 + offset];
  140. memset(cmd_buf, 0, sizeof(cmd_buf));
  141. cmd_buf[offset + 0] = ANTMINER_COMMAND_PREFIX;
  142. cmd_buf[offset + 1] = ANTMINER_COMMAND_LED;
  143. cmd_buf[offset + 2] = ANTMINER_COMMAND_ON;
  144. cmd_buf[offset + 3] = crc5usb(cmd_buf, sizeof(cmd_buf));
  145. const int fd = antminer->device_fd;
  146. icarus_write(antminer->proc_repr, fd, (char *)(&cmd_buf), sizeof(cmd_buf));
  147. }
  148. static
  149. bool antminer_identify(struct cgpu_info *antminer)
  150. {
  151. for (int i = 0; i < 10; i++)
  152. {
  153. antminer_flash_led(antminer);
  154. cgsleep_ms(250);
  155. }
  156. return true;
  157. }
  158. static
  159. const struct bfg_set_device_definition antminer_set_device_funcs[] = {
  160. {"baud" , icarus_set_baud , "serial baud rate"},
  161. {"work_division", icarus_set_work_division, "number of pieces work is split into"},
  162. {"reopen" , icarus_set_reopen , "how often to reopen device: never, timeout, cycle, (or now for a one-shot reopen)"},
  163. {"timing" , icarus_set_timing , "timing of device; see README.FPGA"},
  164. {"clock", antminer_set_clock, "clock frequency"},
  165. {NULL},
  166. };
  167. static
  168. void antminer_drv_init()
  169. {
  170. antminer_drv = icarus_drv;
  171. antminer_drv.dname = "antminer";
  172. antminer_drv.name = "AMU";
  173. antminer_drv.lowl_probe = antminer_lowl_probe;
  174. antminer_drv.identify_device = antminer_identify;
  175. ++antminer_drv.probe_priority;
  176. }
  177. struct device_drv antminer_drv = {
  178. .drv_init = antminer_drv_init,
  179. };