driver-cairnsmore.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2012 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 "compat.h"
  10. #include "dynclock.h"
  11. #include "fpgautils.h"
  12. #include "icarus-common.h"
  13. #include "miner.h"
  14. #define CAIRNSMORE1_IO_SPEED 115200
  15. // This is a general ballpark
  16. #define CAIRNSMORE1_HASH_TIME 0.0000000024484
  17. #define CAIRNSMORE1_MINIMUM_CLOCK 5
  18. #define CAIRNSMORE1_DEFAULT_CLOCK 200
  19. #define CAIRNSMORE1_MAXIMUM_CLOCK 200
  20. struct device_api cairnsmore_api;
  21. static bool cairnsmore_detect_one(const char *devpath)
  22. {
  23. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  24. if (unlikely(!info))
  25. quit(1, "Failed to malloc ICARUS_INFO");
  26. info->baud = CAIRNSMORE1_IO_SPEED;
  27. info->work_division = 2;
  28. info->fpga_count = 2;
  29. info->quirk_reopen = false;
  30. info->Hs = CAIRNSMORE1_HASH_TIME;
  31. info->timing_mode = MODE_LONG;
  32. info->do_icarus_timing = true;
  33. if (!icarus_detect_custom(devpath, &cairnsmore_api, info)) {
  34. free(info);
  35. return false;
  36. }
  37. return true;
  38. }
  39. static int cairnsmore_detect_auto(void)
  40. {
  41. return
  42. serial_autodetect_udev (cairnsmore_detect_one, "*Cairnsmore1*") ?:
  43. serial_autodetect_devserial(cairnsmore_detect_one, "Cairnsmore1") ?:
  44. serial_autodetect_ftdi (cairnsmore_detect_one, "Cairnsmore1", NULL) ?:
  45. 0;
  46. }
  47. static void cairnsmore_detect()
  48. {
  49. // Actual serial detection is handled by Icarus driver
  50. serial_detect_auto_byname(&cairnsmore_api, cairnsmore_detect_one, cairnsmore_detect_auto);
  51. }
  52. static bool cairnsmore_send_cmd(int fd, uint8_t cmd, uint8_t data)
  53. {
  54. unsigned char pkt[64] =
  55. "\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"
  56. "vdi\xb7"
  57. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  58. "BFG0" "\xff\xff\xff\xff" "\0\0\0\0";
  59. pkt[32] = 0xda ^ cmd ^ data;
  60. pkt[33] = data;
  61. pkt[34] = cmd;
  62. return write(fd, pkt, sizeof(pkt)) == sizeof(pkt);
  63. }
  64. bool cairnsmore_supports_dynclock(int fd)
  65. {
  66. unsigned char pkts[64] =
  67. "\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"
  68. "\xe6\x3c\0\xb7" // Set frequency multiplier to 60 (150 Mhz)
  69. "\0\0\0\0\0\0\0\0\0\0\0\0" "BFG0"
  70. "\x8b\xdb\x05\x1a" "\xff\xff\xff\xff" "\x00\x00\x1e\xfd";
  71. if (write(fd, pkts, sizeof(pkts)) != sizeof(pkts))
  72. return false;
  73. uint32_t nonce = 0;
  74. {
  75. struct timeval tv_finish;
  76. struct thr_info dummy = {
  77. .work_restart = false,
  78. .work_restart_fd = -1,
  79. };
  80. icarus_gets((unsigned char*)&nonce, fd, &tv_finish, &dummy, 1);
  81. }
  82. switch (nonce) {
  83. case 0x000b1b5e: // on big endian
  84. case 0x5e1b0b00: // on little endian
  85. // Hashed the command, so it's not supported
  86. return false;
  87. default:
  88. // TODO: nonce from a real job... handle it
  89. case 0:
  90. return true;
  91. }
  92. }
  93. static bool cairnsmore_change_clock_func(struct thr_info *thr, int bestM)
  94. {
  95. struct cgpu_info *cm1 = thr->cgpu;
  96. struct ICARUS_INFO *info = cm1->cgpu_data;
  97. if (unlikely(!cairnsmore_send_cmd(cm1->device_fd, 0, bestM)))
  98. return false;
  99. // Adjust Hs expectations for frequency change
  100. info->Hs = info->Hs * (double)bestM / (double)info->dclk.freqM;
  101. char repr[0x10];
  102. sprintf(repr, "%s %u", cm1->api->name, cm1->device_id);
  103. dclk_msg_freqchange(repr, 2.5 * (double)info->dclk.freqM, 2.5 * (double)bestM, NULL);
  104. info->dclk.freqM = bestM;
  105. return true;
  106. }
  107. static bool cairnsmore_init(struct thr_info *thr)
  108. {
  109. struct cgpu_info *cm1 = thr->cgpu;
  110. struct ICARUS_INFO *info = cm1->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.freqMaxM = CAIRNSMORE1_MAXIMUM_CLOCK / 2.5;
  115. info->dclk.freqM =
  116. info->dclk.freqMDefault = CAIRNSMORE1_DEFAULT_CLOCK / 2.5;
  117. cairnsmore_send_cmd(cm1->device_fd, 0, info->dclk.freqM);
  118. applog(LOG_WARNING, "%s %u: Frequency set to %u Mhz (range: %u-%u)",
  119. cm1->api->name, cm1->device_id,
  120. CAIRNSMORE1_DEFAULT_CLOCK, CAIRNSMORE1_MINIMUM_CLOCK, CAIRNSMORE1_MAXIMUM_CLOCK
  121. );
  122. } else {
  123. applog(LOG_WARNING, "%s %u: Frequency scaling not supported",
  124. cm1->api->name, cm1->device_id
  125. );
  126. // Test failures corrupt the hash state, so next scanhash is a firstrun
  127. struct icarus_state *state = thr->cgpu_data;
  128. state->firstrun = true;
  129. }
  130. return true;
  131. }
  132. void convert_icarus_to_cairnsmore(struct cgpu_info *cm1)
  133. {
  134. struct ICARUS_INFO *info = cm1->cgpu_data;
  135. info->Hs = CAIRNSMORE1_HASH_TIME;
  136. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  137. info->timing_mode = MODE_LONG;
  138. info->do_icarus_timing = true;
  139. cm1->api = &cairnsmore_api;
  140. renumber_cgpu(cm1);
  141. cairnsmore_init(cm1->thr[0]);
  142. }
  143. static struct api_data *cairnsmore_api_extra_device_status(struct cgpu_info *cm1)
  144. {
  145. struct ICARUS_INFO *info = cm1->cgpu_data;
  146. struct api_data*root = NULL;
  147. if (info->dclk.freqM) {
  148. double frequency = 2.5 * info->dclk.freqM;
  149. root = api_add_freq(root, "Frequency", &frequency, true);
  150. }
  151. return root;
  152. }
  153. static bool cairnsmore_identify(struct cgpu_info *cm1)
  154. {
  155. struct ICARUS_INFO *info = cm1->cgpu_data;
  156. if (!info->dclk.freqM)
  157. return false;
  158. cairnsmore_send_cmd(cm1->device_fd, 1, 1);
  159. sleep(5);
  160. cairnsmore_send_cmd(cm1->device_fd, 1, 0);
  161. cm1->flash_led = true;
  162. return true;
  163. }
  164. extern struct device_api icarus_api;
  165. __attribute__((constructor(1000)))
  166. static void cairnsmore_api_init()
  167. {
  168. cairnsmore_api = icarus_api;
  169. cairnsmore_api.dname = "cairnsmore";
  170. cairnsmore_api.name = "ECM";
  171. cairnsmore_api.api_detect = cairnsmore_detect;
  172. cairnsmore_api.thread_init = cairnsmore_init;
  173. cairnsmore_api.identify_device = cairnsmore_identify;
  174. cairnsmore_api.get_api_extra_device_status = cairnsmore_api_extra_device_status;
  175. }