driver-bfsb.c 4.9 KB

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