driver-jingtian.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2014 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 <stdlib.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <sys/ioctl.h>
  19. #include <linux/types.h>
  20. #include <linux/spi/spidev.h>
  21. #include "deviceapi.h"
  22. #include "driver-aan.h"
  23. #include "logging.h"
  24. #include "lowl-spi.h"
  25. #include "util.h"
  26. static const int jingtian_cs_gpio[] = {14, 15, 18};
  27. static const int jingtian_spi_disable_gpio = 25;
  28. static const int jingtian_reset_gpio = 3;
  29. static const int jingtian_max_cs = 1 << (sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio));
  30. static const uint8_t jingtian_pre_header[] = {0xb5, 0xb5};
  31. #define JINGTIAN_REGISTER_EXTRA_SIZE 2
  32. BFG_REGISTER_DRIVER(jingtian_drv)
  33. static
  34. bool jingtian_spi_txrx(struct spi_port * const port)
  35. {
  36. if (*port->chipselect_current != port->chipselect)
  37. {
  38. unsigned cs_set_low = 0, cs_set_high = 0, cur_cs_bit;
  39. bool bit_desired;
  40. for (int i = 0; i < sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio); ++i)
  41. {
  42. cur_cs_bit = (1 << i);
  43. bit_desired = (port->chipselect & cur_cs_bit);
  44. if (bit_desired == (bool)(*port->chipselect_current & cur_cs_bit))
  45. // No change needed
  46. continue;
  47. if (bit_desired)
  48. cs_set_high |= (1 << jingtian_cs_gpio[i]);
  49. else
  50. cs_set_low |= (1 << jingtian_cs_gpio[i]);
  51. }
  52. bfg_gpio_set_high(1 << jingtian_spi_disable_gpio);
  53. if (cs_set_low)
  54. bfg_gpio_set_low(cs_set_low);
  55. if (cs_set_high)
  56. bfg_gpio_set_high(cs_set_high);
  57. bfg_gpio_set_low(1 << jingtian_spi_disable_gpio);
  58. if (opt_dev_protocol)
  59. applog(LOG_DEBUG, "%s(%p): CS %d", __func__, port, port->chipselect);
  60. *port->chipselect_current = port->chipselect;
  61. }
  62. if (opt_dev_protocol)
  63. {
  64. char x[(spi_getbufsz(port) * 2) + 1];
  65. bin2hex(x, spi_gettxbuf(port), spi_getbufsz(port));
  66. applog(LOG_DEBUG, "%s(%p): %cX %s", __func__, port, 'T', x);
  67. }
  68. bool rv = linux_spi_txrx(port);
  69. if (opt_dev_protocol)
  70. {
  71. char x[(spi_getbufsz(port) * 2) + 1];
  72. bin2hex(x, spi_getrxbuf(port), spi_getbufsz(port));
  73. applog(LOG_DEBUG, "%s(%p): %cX %s", __func__, port, 'R', x);
  74. }
  75. return rv;
  76. }
  77. static
  78. void jingtian_precmd(struct spi_port * const spi)
  79. {
  80. spi_emit_buf(spi, jingtian_pre_header, sizeof(jingtian_pre_header));
  81. }
  82. static
  83. bool jingtian_read_reg(struct spi_port * const spi, const uint8_t chip, void * const out_buf, const struct timeval * const tvp_timeout)
  84. {
  85. if (!aan_read_reg_direct(spi, chip, out_buf, tvp_timeout))
  86. return false;
  87. spi_emit_nop(spi, JINGTIAN_REGISTER_EXTRA_SIZE);
  88. if (!spi_txrx(spi))
  89. applogr(false, LOG_DEBUG, "%s: %s failed", __func__, "spi_txrx");
  90. struct cgpu_info * const dev = spi->cgpu;
  91. if (unlikely(!dev))
  92. return true;
  93. struct cgpu_info * const proc = aan_proc_for_chipid(dev, chip);
  94. uint8_t * const rx = spi_getrxbuf(spi);
  95. proc->temp = upk_u16be(rx, 0);
  96. return true;
  97. }
  98. static
  99. struct aan_hooks jingtian_hooks = {
  100. .precmd = jingtian_precmd,
  101. .read_reg = jingtian_read_reg,
  102. };
  103. static
  104. void jingtian_common_init(void)
  105. {
  106. RUNONCE();
  107. spi_init();
  108. for (int i = 0; i < sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio); ++i)
  109. bfg_gpio_setpin_output(jingtian_cs_gpio[i]);
  110. bfg_gpio_setpin_output(jingtian_spi_disable_gpio);
  111. bfg_gpio_set_high(1 << jingtian_spi_disable_gpio);
  112. bfg_gpio_setpin_output(jingtian_reset_gpio);
  113. bfg_gpio_set_high(1 << jingtian_reset_gpio);
  114. cgsleep_ms(200);
  115. bfg_gpio_set_low(1 << jingtian_reset_gpio);
  116. }
  117. static
  118. bool jingtian_detect_one(const char * const devpath)
  119. {
  120. int found = 0, chips;
  121. jingtian_common_init();
  122. struct spi_port spi_cfg;
  123. memset(&spi_cfg, 0, sizeof(spi_cfg));
  124. spi_cfg.speed = 4000000;
  125. spi_cfg.delay = 0;
  126. spi_cfg.mode = SPI_MODE_1;
  127. spi_cfg.bits = 8;
  128. if (spi_open(&spi_cfg, devpath) < 0)
  129. applogr(false, LOG_DEBUG, "%s: Failed to open %s", jingtian_drv.dname, devpath);
  130. struct cgpu_info *cgpu, *prev_cgpu = NULL;
  131. struct spi_port *spi;
  132. int * const chipselect_current = malloc(sizeof(*spi->chipselect_current));
  133. *chipselect_current = -1;
  134. int devpath_len = strlen(devpath);
  135. int chipcount[jingtian_max_cs];
  136. struct spi_port *spi_a[jingtian_max_cs];
  137. for (int i = 0; i < jingtian_max_cs; ++i)
  138. {
  139. spi = spi_a[i] = calloc(sizeof(*spi), 1);
  140. memcpy(spi, &spi_cfg, sizeof(*spi));
  141. spi->repr = malloc(devpath_len + 0x10);
  142. sprintf((void*)spi->repr, "%s(cs%d)", devpath, i);
  143. spi->txrx = jingtian_spi_txrx;
  144. spi->userp = &jingtian_hooks;
  145. spi->chipselect = i;
  146. spi->chipselect_current = chipselect_current;
  147. }
  148. aan_detect_spi(chipcount, spi_a, jingtian_max_cs);
  149. for (int i = 0; i < jingtian_max_cs; ++i)
  150. {
  151. chips = chipcount[i];
  152. free((void*)spi_a[i]->repr);
  153. spi_a[i]->repr = NULL;
  154. if (chips <= 0)
  155. {
  156. free(spi_a[i]);
  157. continue;
  158. }
  159. cgpu = malloc(sizeof(*cgpu));
  160. *cgpu = (struct cgpu_info){
  161. .drv = &jingtian_drv,
  162. .procs = chips,
  163. .threads = prev_cgpu ? 0 : 1,
  164. .device_data = spi_a[i],
  165. .device_path = strdup(devpath),
  166. .set_device_funcs = aan_set_device_funcs,
  167. };
  168. spi_a[i]->cgpu = cgpu;
  169. add_cgpu_slave(cgpu, prev_cgpu);
  170. prev_cgpu = cgpu;
  171. found += chips;
  172. }
  173. close(spi_cfg.fd);
  174. if (!found)
  175. free(chipselect_current);
  176. return found;
  177. }
  178. static
  179. int jingtian_detect_auto(void)
  180. {
  181. return jingtian_detect_one("/dev/spidev0.0") ? 1 : 0;
  182. }
  183. static
  184. void jingtian_detect(void)
  185. {
  186. generic_detect(&jingtian_drv, jingtian_detect_one, jingtian_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  187. }
  188. static
  189. bool jingtian_init(struct thr_info * const master_thr)
  190. {
  191. struct cgpu_info * const master_dev = master_thr->cgpu;
  192. const char * const devpath = master_dev->device_path;
  193. const int fd = open(devpath, O_RDWR);
  194. if (fd < 0)
  195. applogr(false, LOG_ERR, "%s: Failed to open %s", master_dev->dev_repr, devpath);
  196. for_each_managed_proc(proc, master_dev)
  197. {
  198. struct spi_port * const spi = proc->device_data;
  199. spi->fd = fd;
  200. }
  201. return aan_init(master_thr);
  202. }
  203. struct device_drv jingtian_drv = {
  204. .dname = "jingtian",
  205. .name = "JTN",
  206. .drv_detect = jingtian_detect,
  207. .thread_init = jingtian_init,
  208. .minerloop = minerloop_queue,
  209. .queue_append = aan_queue_append,
  210. .queue_flush = aan_queue_flush,
  211. .poll = aan_poll,
  212. .get_api_extra_device_status = aan_api_device_status,
  213. };