driver-metabank.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 Anatoly Legkodymov
  4. * Copyright 2013 Luke Dashjr
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. //#define USE_METABANK2
  25. #include "config.h"
  26. #include <stdbool.h>
  27. #include "deviceapi.h"
  28. #include "driver-bitfury.h"
  29. #include "libbitfury.h"
  30. #include "spidevc.h"
  31. #include "tm_i2c.h"
  32. static int renew_voltage = 1;
  33. static struct timeval mb_stat[32];
  34. typedef struct {
  35. double temp;
  36. double vc;
  37. } slots_stat_struct;
  38. static slots_stat_struct slots[32] = { { .temp = 0, } };
  39. BFG_REGISTER_DRIVER(metabank_drv)
  40. static
  41. bool metabank_spi_txrx(struct spi_port *port)
  42. {
  43. static int current_slot = -1;
  44. struct cgpu_info * const proc = port->cgpu;
  45. struct bitfury_device * const bitfury = proc->device_data;
  46. if (current_slot != bitfury->slot)
  47. {
  48. if (current_slot != -1)
  49. tm_i2c_clear_oe(current_slot);
  50. tm_i2c_set_oe(bitfury->slot);
  51. current_slot = bitfury->slot;
  52. }
  53. const bool rv = sys_spi_txrx(port);
  54. return rv;
  55. }
  56. static
  57. int metabank_autodetect()
  58. {
  59. RUNONCE(0);
  60. struct cgpu_info *cgpu = NULL, *proc1 = NULL, *prev_cgpu = NULL;
  61. struct bitfury_device **devicelist, *bitfury;
  62. struct spi_port *port;
  63. int i, j;
  64. int proc_count = 0;
  65. bool slot_on[32];
  66. struct bitfury_device dummy_bitfury;
  67. struct cgpu_info dummy_cgpu;
  68. applog(LOG_INFO, "INFO: bitfury_detect");
  69. spi_init();
  70. if (!sys_spi)
  71. return 0;
  72. if (tm_i2c_init() < 0) {
  73. applog(LOG_DEBUG, "%s: I2C init error", metabank_drv.dname);
  74. return 0;
  75. }
  76. dummy_cgpu.device_data = &dummy_bitfury;
  77. for (i = 0; i < 32; i++) {
  78. slot_on[i] = 0;
  79. timer_set_now(&mb_stat[i]);
  80. }
  81. for (i = 0; i < 32; i++) {
  82. int slot_detected = tm_i2c_detect(i) != -1;
  83. slot_on[i] = slot_detected;
  84. tm_i2c_clear_oe(i);
  85. cgsleep_ms(1);
  86. }
  87. for (i = 0; i < 32; i++) {
  88. if (slot_on[i]) {
  89. int chip_n;
  90. port = malloc(sizeof(*port));
  91. *port = *sys_spi;
  92. port->cgpu = &dummy_cgpu;
  93. port->txrx = metabank_spi_txrx;
  94. dummy_bitfury.slot = i;
  95. chip_n = libbitfury_detectChips1(port);
  96. if (chip_n)
  97. {
  98. applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
  99. devicelist = malloc(sizeof(*devicelist) * chip_n);
  100. for (j = 0; j < chip_n; ++j)
  101. {
  102. devicelist[j] = bitfury = malloc(sizeof(*bitfury));
  103. *bitfury = (struct bitfury_device){
  104. .spi = port,
  105. .slot = i,
  106. .fasync = j,
  107. };
  108. }
  109. cgpu = malloc(sizeof(*cgpu));
  110. *cgpu = (struct cgpu_info){
  111. .drv = &metabank_drv,
  112. .procs = chip_n,
  113. .device_data = devicelist,
  114. .cutofftemp = 80,
  115. };
  116. add_cgpu_slave(cgpu, prev_cgpu);
  117. #ifdef USE_METABANK2
  118. // ### Set all vltage to 0.835V ###
  119. applog(LOG_INFO, "Set voltage!");
  120. applog(LOG_WARNING, "Slot[%02d]: %.3f V %.1f C", i,
  121. tm_i2c_set_voltage_abs(i, 0.835), tm_i2c_gettemp(i));
  122. #endif
  123. proc_count += chip_n;
  124. if (!proc1)
  125. proc1 = cgpu;
  126. prev_cgpu = cgpu;
  127. }
  128. else
  129. {
  130. slot_on[i] = 0;
  131. free(port);
  132. }
  133. }
  134. }
  135. if (proc1)
  136. proc1->threads = 1;
  137. return proc_count;
  138. }
  139. static void metabank_detect(void)
  140. {
  141. noserial_detect_manual(&metabank_drv, metabank_autodetect);
  142. }
  143. static
  144. bool metabank_init(struct thr_info *thr)
  145. {
  146. struct bitfury_device **devicelist;
  147. struct cgpu_info *proc;
  148. struct bitfury_device *bitfury;
  149. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  150. {
  151. devicelist = proc->device_data;
  152. bitfury = devicelist[proc->proc_id];
  153. proc->device_data = bitfury;
  154. bitfury->spi->cgpu = proc;
  155. bitfury_init_chip(proc);
  156. #ifndef USE_METABANK2
  157. bitfury->osc6_bits = 53;
  158. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  159. bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
  160. #else
  161. bitfury->osc6_bits = 56;
  162. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  163. bitfury_init_freq_stat(&bitfury->chip_stat, 54, 56);
  164. #endif
  165. if (proc->proc_id == proc->procs - 1)
  166. free(devicelist);
  167. }
  168. timer_set_now(&thr->tv_poll);
  169. return true;
  170. }
  171. static void metabank_shutdown(struct thr_info *thr)
  172. {
  173. bitfury_shutdown(thr);
  174. tm_i2c_close();
  175. }
  176. static bool metabank_get_stats(struct cgpu_info *cgpu)
  177. {
  178. struct bitfury_device *bitfury = cgpu->device_data;
  179. float t;
  180. struct timeval now;
  181. struct cgpu_info *proc, *cproc;
  182. unsigned slot = bitfury->slot;
  183. slots_stat_struct *stat = &slots[bitfury->slot];
  184. timer_set_now(&now);
  185. if (timer_elapsed(&mb_stat[slot], &now) >= 10)
  186. {
  187. copy_time(&mb_stat[slot], &now);
  188. slot = bitfury->slot;
  189. t = tm_i2c_gettemp(slot);
  190. if (t < -27 || t > 250) //Sometimes tm_i2c_gettemp() returns strange result, ignoring it.
  191. return false;
  192. cgpu->temp = t;
  193. #ifndef USE_METABANK2
  194. bitfury->volt = tm_i2c_getcore0(slot);
  195. stat->temp = t;
  196. stat->vc = bitfury->volt;
  197. #else
  198. if (t > 75) // Drop Down voltage
  199. {
  200. tm_i2c_set_vid(slot, 0);
  201. applog(LOG_WARNING, "-----=====!!!!! Slot %u set low voltage !!!!!=====-----", slot);
  202. }
  203. bitfury->volt = (slot & 1) ? tm_i2c_getcore1(slot) : tm_i2c_getcore0(slot);
  204. stat->temp = t;
  205. stat->vc = bitfury->volt;
  206. if (stat->vc < 0.8 && !renew_voltage && t > 0 && t < 50 && timer_elapsed(&bitfury->tv_stat_long, &now) < 60) { // No overheat return voltage
  207. renew_voltage = 1;
  208. applog(LOG_WARNING, "Restore voltage after overheat");
  209. proc = cgpu->device;
  210. slot = 99;
  211. while(proc) {
  212. bitfury = (struct bitfury_device *) proc->device_data;
  213. if (bitfury->slot != slot) {
  214. slot = bitfury->slot;
  215. applog(LOG_WARNING, "%s slot[%02d]: %.3f V %.1f C",
  216. proc->dev_repr, slot, tm_i2c_set_voltage_abs(slot, 0.835), tm_i2c_gettemp(slot));
  217. }
  218. proc = proc->next_proc;
  219. }
  220. }
  221. if (renew_voltage && timer_elapsed(&bitfury->tv_stat_long, &now) > 660) { // set new voltage after 15 min
  222. int count = 0;
  223. double mhz;
  224. applog(LOG_WARNING, "Set new voltage!");
  225. proc = cgpu->device;
  226. while(proc) {
  227. cproc = proc;
  228. mhz = 0;
  229. count = proc->procs;
  230. for(int i = 0; i < cproc->procs; i++) {
  231. bitfury = (struct bitfury_device *) proc->device_data;
  232. if (bitfury->mhz > 150)
  233. mhz += bitfury->mhz;
  234. else
  235. count--;
  236. proc = proc->next_proc;
  237. }
  238. mhz /= (double)count;
  239. if (mhz < 220) {
  240. applog(LOG_WARNING, "Dev : %s slot[%u]=(%.0f, %d) new voltage: %.3f V\t%.1f C",
  241. cproc->dev_repr, bitfury->slot, mhz, count, tm_i2c_set_voltage_abs(bitfury->slot, 0.91), tm_i2c_gettemp(bitfury->slot));
  242. } else {
  243. applog(LOG_WARNING, "Dev : %s slot[%u]=(%.0f, %d) no changes",
  244. cproc->dev_repr, bitfury->slot, mhz, count);
  245. }
  246. };
  247. renew_voltage = 0;
  248. }
  249. #endif
  250. } else {
  251. cgpu->temp = stat->temp;
  252. bitfury->volt = stat->vc;
  253. }
  254. return true;
  255. }
  256. static struct api_data *metabank_api_extra_device_detail(struct cgpu_info *cgpu)
  257. {
  258. struct api_data *root = NULL;
  259. struct bitfury_device * const bitfury = cgpu->device_data;
  260. root = bitfury_api_device_detail(cgpu);
  261. root = api_add_uint(root, "Slot", &(bitfury->slot), false);
  262. return root;
  263. }
  264. static struct api_data *metabank_api_extra_device_status(struct cgpu_info *cgpu)
  265. {
  266. struct api_data *root = NULL;
  267. float vc0, vc1;
  268. struct bitfury_device * const bitfury = cgpu->device_data;
  269. root = bitfury_api_device_status(cgpu);
  270. vc0 = tm_i2c_getcore0(bitfury->slot);
  271. vc1 = tm_i2c_getcore1(bitfury->slot);
  272. bitfury->volt = (bitfury->slot & 1) ? vc1 : vc0;
  273. root = api_add_volts(root, "Slot VC0", &vc0, true);
  274. root = api_add_volts(root, "Slot VC1", &vc1, true);
  275. return root;
  276. }
  277. struct device_drv metabank_drv = {
  278. .dname = "metabank",
  279. .name = "MBF",
  280. .drv_detect = metabank_detect,
  281. .thread_init = metabank_init,
  282. .thread_enable = bitfury_enable,
  283. .thread_disable = bitfury_disable,
  284. .minerloop = minerloop_async,
  285. .job_prepare = bitfury_job_prepare,
  286. .job_start = bitfury_noop_job_start,
  287. .poll = bitfury_do_io,
  288. .job_process_results = bitfury_job_process_results,
  289. .thread_shutdown = metabank_shutdown,
  290. .get_api_extra_device_detail = metabank_api_extra_device_detail,
  291. .get_api_extra_device_status = metabank_api_extra_device_status,
  292. .get_stats = metabank_get_stats,
  293. .set_device = bitfury_set_device,
  294. #ifdef HAVE_CURSES
  295. .proc_wlogprint_status = bitfury_wlogprint_status,
  296. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  297. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  298. #endif
  299. };