driver-bfsb.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include "config.h"
  25. #include <stdbool.h>
  26. #include "deviceapi.h"
  27. #include "libbitfury.h"
  28. #include "spidevc.h"
  29. #include "driver-bitfury.h"
  30. BFG_REGISTER_DRIVER(bfsb_drv)
  31. static
  32. bool bfsb_spi_txrx(struct spi_port *port)
  33. {
  34. struct cgpu_info * const proc = port->cgpu;
  35. struct bitfury_device * const bitfury = proc->device_data;
  36. spi_bfsb_select_bank(bitfury->slot);
  37. const bool rv = sys_spi_txrx(port);
  38. return rv;
  39. }
  40. static
  41. int bfsb_autodetect()
  42. {
  43. RUNONCE(0);
  44. struct cgpu_info *cgpu = NULL, *proc1 = NULL, *prev_cgpu = NULL;
  45. int proc_count = 0;
  46. applog(LOG_INFO, "INFO: bitfury_detect");
  47. spi_init();
  48. if (!sys_spi)
  49. return 0;
  50. struct bitfury_device **devicelist, *bitfury;
  51. struct spi_port *port;
  52. int i, j;
  53. struct bitfury_device dummy_bitfury;
  54. struct cgpu_info dummy_cgpu;
  55. dummy_cgpu.device_data = &dummy_bitfury;
  56. for (i = 0; i < 4; i++)
  57. {
  58. int chip_n;
  59. port = malloc(sizeof(*port));
  60. *port = *sys_spi;
  61. port->cgpu = &dummy_cgpu;
  62. port->txrx = bfsb_spi_txrx;
  63. port->speed = 625000;
  64. dummy_bitfury.slot = i;
  65. chip_n = libbitfury_detectChips1(port);
  66. if (chip_n)
  67. {
  68. applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
  69. devicelist = malloc(sizeof(*devicelist) * chip_n);
  70. for (j = 0; j < chip_n; ++j)
  71. {
  72. devicelist[j] = bitfury = malloc(sizeof(*bitfury));
  73. *bitfury = (struct bitfury_device){
  74. .spi = port,
  75. .slot = i,
  76. .fasync = j,
  77. };
  78. }
  79. cgpu = malloc(sizeof(*cgpu));
  80. *cgpu = (struct cgpu_info){
  81. .drv = &bfsb_drv,
  82. .procs = chip_n,
  83. .device_data = devicelist,
  84. };
  85. add_cgpu_slave(cgpu, prev_cgpu);
  86. proc_count += chip_n;
  87. if (!proc1)
  88. proc1 = cgpu;
  89. prev_cgpu = cgpu;
  90. }
  91. else
  92. free(port);
  93. }
  94. if (proc1)
  95. proc1->threads = 1;
  96. return proc_count;
  97. }
  98. static void bfsb_detect(void)
  99. {
  100. noserial_detect_manual(&bfsb_drv, bfsb_autodetect);
  101. }
  102. static
  103. bool bfsb_init(struct thr_info *thr)
  104. {
  105. struct bitfury_device **devicelist;
  106. struct cgpu_info *proc;
  107. struct bitfury_device *bitfury;
  108. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  109. {
  110. devicelist = proc->device_data;
  111. bitfury = devicelist[proc->proc_id];
  112. proc->device_data = bitfury;
  113. bitfury->spi->cgpu = proc;
  114. bitfury_init_chip(proc);
  115. bitfury->osc6_bits = 53;
  116. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  117. bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
  118. if (proc->proc_id == proc->procs - 1)
  119. free(devicelist);
  120. }
  121. timer_set_now(&thr->tv_poll);
  122. return true;
  123. }
  124. static void bfsb_shutdown(struct thr_info *thr)
  125. {
  126. bitfury_shutdown(thr);
  127. spi_bfsb_select_bank(-1);
  128. }
  129. static struct api_data *bfsb_api_device_detail(struct cgpu_info *cgpu)
  130. {
  131. struct bitfury_device * const bitfury = cgpu->device_data;
  132. struct api_data *root = bitfury_api_device_detail(cgpu);
  133. root = api_add_uint(root, "Slot", &(bitfury->slot), false);
  134. return root;
  135. }
  136. struct device_drv bfsb_drv = {
  137. .dname = "bfsb",
  138. .name = "BSB",
  139. .drv_detect = bfsb_detect,
  140. .minerloop = minerloop_async,
  141. .job_prepare = bitfury_job_prepare,
  142. .thread_init = bfsb_init,
  143. .poll = bitfury_do_io,
  144. .job_start = bitfury_noop_job_start,
  145. .job_process_results = bitfury_job_process_results,
  146. .get_api_extra_device_detail = bfsb_api_device_detail,
  147. .get_api_extra_device_status = bitfury_api_device_status,
  148. .set_device = bitfury_set_device,
  149. .thread_disable = bitfury_disable,
  150. .thread_enable = bitfury_enable,
  151. .thread_shutdown = bfsb_shutdown,
  152. #ifdef HAVE_CURSES
  153. .proc_wlogprint_status = bitfury_wlogprint_status,
  154. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  155. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  156. #endif
  157. };