driver-antminer.c 6.1 KB

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