driver-cairnsmore.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include "compat.h"
  13. #include "dynclock.h"
  14. #include "fpgautils.h"
  15. #include "icarus-common.h"
  16. #include "miner.h"
  17. #define CAIRNSMORE1_IO_SPEED 115200
  18. // This is a general ballpark
  19. #define CAIRNSMORE1_HASH_TIME 0.0000000024484
  20. #define CAIRNSMORE1_MINIMUM_CLOCK 50
  21. #define CAIRNSMORE1_DEFAULT_CLOCK 200
  22. #define CAIRNSMORE1_MAXIMUM_CLOCK 210
  23. BFG_REGISTER_DRIVER(cairnsmore_drv)
  24. static bool cairnsmore_detect_one(const char *devpath)
  25. {
  26. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  27. if (unlikely(!info))
  28. quit(1, "Failed to malloc ICARUS_INFO");
  29. info->baud = CAIRNSMORE1_IO_SPEED;
  30. info->work_division = 2;
  31. info->fpga_count = 2;
  32. info->quirk_reopen = false;
  33. info->Hs = CAIRNSMORE1_HASH_TIME;
  34. info->timing_mode = MODE_LONG;
  35. info->do_icarus_timing = true;
  36. if (!icarus_detect_custom(devpath, &cairnsmore_drv, info)) {
  37. free(info);
  38. return false;
  39. }
  40. return true;
  41. }
  42. static int cairnsmore_detect_auto(void)
  43. {
  44. return serial_autodetect(cairnsmore_detect_one, "Cairnsmore1");
  45. }
  46. static void cairnsmore_detect()
  47. {
  48. // Actual serial detection is handled by Icarus driver
  49. serial_detect_auto_byname(&cairnsmore_drv, cairnsmore_detect_one, cairnsmore_detect_auto);
  50. }
  51. static bool cairnsmore_send_cmd(int fd, uint8_t cmd, uint8_t data, bool probe)
  52. {
  53. unsigned char pkt[64] =
  54. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  55. "vdi\xb7"
  56. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  57. "bfg0" "\xff\xff\xff\xff" "\xb5\0\0\0";
  58. if (unlikely(probe))
  59. pkt[61] = '\x01';
  60. pkt[32] = 0xda ^ cmd ^ data;
  61. pkt[33] = data;
  62. pkt[34] = cmd;
  63. return write(fd, pkt, sizeof(pkt)) == sizeof(pkt);
  64. }
  65. bool cairnsmore_supports_dynclock(int fd)
  66. {
  67. if (!cairnsmore_send_cmd(fd, 0, 1, true))
  68. return false;
  69. if (!cairnsmore_send_cmd(fd, 0, 1, true))
  70. return false;
  71. uint32_t nonce = 0;
  72. {
  73. struct timeval tv_finish;
  74. struct thr_info dummy = {
  75. .work_restart = false,
  76. .work_restart_notifier = {-1, -1},
  77. };
  78. icarus_gets((unsigned char*)&nonce, fd, &tv_finish, &dummy, 1);
  79. }
  80. applog(LOG_DEBUG, "Cairnsmore dynclock detection... Got %08x", nonce);
  81. switch (nonce) {
  82. case 0x00949a6f: // big endian
  83. case 0x6f9a9400: // little endian
  84. // Hashed the command, so it's not supported
  85. return false;
  86. default:
  87. applog(LOG_WARNING, "Unexpected nonce from dynclock probe: %08x", (uint32_t)be32toh(nonce));
  88. return false;
  89. case 0:
  90. return true;
  91. }
  92. }
  93. #define cairnsmore_send_cmd(fd, cmd, data) cairnsmore_send_cmd(fd, cmd, data, false)
  94. static bool cairnsmore_change_clock_func(struct thr_info *thr, int bestM)
  95. {
  96. struct cgpu_info *cm1 = thr->cgpu;
  97. struct ICARUS_INFO *info = cm1->device_data;
  98. if (unlikely(!cairnsmore_send_cmd(cm1->device_fd, 0, bestM)))
  99. return false;
  100. // Adjust Hs expectations for frequency change
  101. info->Hs = info->Hs * (double)bestM / (double)info->dclk.freqM;
  102. dclk_msg_freqchange(cm1->proc_repr, 2.5 * (double)info->dclk.freqM, 2.5 * (double)bestM, NULL);
  103. info->dclk.freqM = bestM;
  104. return true;
  105. }
  106. static bool cairnsmore_init(struct thr_info *thr)
  107. {
  108. struct cgpu_info *cm1 = thr->cgpu;
  109. struct ICARUS_INFO *info = cm1->device_data;
  110. struct icarus_state *state = thr->cgpu_data;
  111. if (cairnsmore_supports_dynclock(cm1->device_fd)) {
  112. info->dclk_change_clock_func = cairnsmore_change_clock_func;
  113. dclk_prepare(&info->dclk);
  114. info->dclk.freqMinM = CAIRNSMORE1_MINIMUM_CLOCK / 2.5;
  115. info->dclk.freqMaxM = CAIRNSMORE1_MAXIMUM_CLOCK / 2.5;
  116. info->dclk.freqM =
  117. info->dclk.freqMDefault = CAIRNSMORE1_DEFAULT_CLOCK / 2.5;
  118. cairnsmore_send_cmd(cm1->device_fd, 0, info->dclk.freqM);
  119. applog(LOG_WARNING, "%"PRIpreprv": Frequency set to %u MHz (range: %u-%u)",
  120. cm1->proc_repr,
  121. CAIRNSMORE1_DEFAULT_CLOCK, CAIRNSMORE1_MINIMUM_CLOCK, CAIRNSMORE1_MAXIMUM_CLOCK
  122. );
  123. // The dynamic-clocking firmware connects each FPGA as its own device
  124. if (!(info->user_set & 1)) {
  125. info->work_division = 1;
  126. if (!(info->user_set & 2))
  127. info->fpga_count = 1;
  128. }
  129. } else {
  130. applog(LOG_WARNING, "%"PRIpreprv": Frequency scaling not supported",
  131. cm1->proc_repr
  132. );
  133. }
  134. // Commands corrupt the hash state, so next scanhash is a firstrun
  135. state->firstrun = true;
  136. return true;
  137. }
  138. void convert_icarus_to_cairnsmore(struct cgpu_info *cm1)
  139. {
  140. struct ICARUS_INFO *info = cm1->device_data;
  141. info->Hs = CAIRNSMORE1_HASH_TIME;
  142. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  143. info->timing_mode = MODE_LONG;
  144. info->do_icarus_timing = true;
  145. cm1->drv = &cairnsmore_drv;
  146. renumber_cgpu(cm1);
  147. cairnsmore_init(cm1->thr[0]);
  148. }
  149. static struct api_data *cairnsmore_drv_extra_device_status(struct cgpu_info *cm1)
  150. {
  151. struct ICARUS_INFO *info = cm1->device_data;
  152. struct api_data*root = NULL;
  153. if (info->dclk.freqM) {
  154. double frequency = 2.5 * info->dclk.freqM;
  155. root = api_add_freq(root, "Frequency", &frequency, true);
  156. }
  157. return root;
  158. }
  159. static bool cairnsmore_identify(struct cgpu_info *cm1)
  160. {
  161. struct ICARUS_INFO *info = cm1->device_data;
  162. if (!info->dclk.freqM)
  163. return false;
  164. cairnsmore_send_cmd(cm1->device_fd, 1, 1);
  165. cgsleep_ms(5000);
  166. cairnsmore_send_cmd(cm1->device_fd, 1, 0);
  167. cm1->flash_led = true;
  168. return true;
  169. }
  170. static void cairnsmore_drv_init()
  171. {
  172. cairnsmore_drv = icarus_drv;
  173. cairnsmore_drv.dname = "cairnsmore";
  174. cairnsmore_drv.name = "ECM";
  175. cairnsmore_drv.drv_detect = cairnsmore_detect;
  176. cairnsmore_drv.thread_init = cairnsmore_init;
  177. cairnsmore_drv.identify_device = cairnsmore_identify;
  178. cairnsmore_drv.get_api_extra_device_status = cairnsmore_drv_extra_device_status;
  179. }
  180. struct device_drv cairnsmore_drv = {
  181. .drv_init = cairnsmore_drv_init,
  182. };