driver-jingtian.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "deviceapi.h"
  19. #include "driver-aan.h"
  20. #include "logging.h"
  21. #include "lowl-spi.h"
  22. #include "util.h"
  23. static const int jingtian_cs_gpio[] = {14, 15, 18};
  24. static const int jingtian_max_cs = 1 << (sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio));
  25. static const uint8_t jingtian_pre_header[] = {0xb5, 0xb5};
  26. BFG_REGISTER_DRIVER(jingtian_drv)
  27. static
  28. bool jingtian_spi_txrx(struct spi_port * const port)
  29. {
  30. if (*port->chipselect_current != port->chipselect)
  31. {
  32. unsigned cs_set_low = 0, cs_set_high = 0, cur_cs_bit;
  33. bool bit_desired;
  34. for (int i = 0; i < sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio); ++i)
  35. {
  36. cur_cs_bit = (1 << i);
  37. bit_desired = (port->chipselect & cur_cs_bit);
  38. if (bit_desired == (bool)(*port->chipselect_current & cur_cs_bit))
  39. // No change needed
  40. continue;
  41. if (bit_desired)
  42. cs_set_high |= (1 << jingtian_cs_gpio[i]);
  43. else
  44. cs_set_low |= (1 << jingtian_cs_gpio[i]);
  45. }
  46. if (cs_set_low)
  47. bfg_gpio_set_low(cs_set_low);
  48. if (cs_set_high)
  49. bfg_gpio_set_high(cs_set_high);
  50. if (opt_dev_protocol)
  51. applog(LOG_DEBUG, "%s(%p): CS %d", __func__, port, port->chipselect);
  52. *port->chipselect_current = port->chipselect;
  53. }
  54. if (opt_dev_protocol)
  55. {
  56. char x[(spi_getbufsz(port) * 2) + 1];
  57. bin2hex(x, spi_gettxbuf(port), spi_getbufsz(port));
  58. applog(LOG_DEBUG, "%s(%p): %cX %s", __func__, port, 'T', x);
  59. }
  60. bool rv = linux_spi_txrx(port);
  61. if (opt_dev_protocol)
  62. {
  63. char x[(spi_getbufsz(port) * 2) + 1];
  64. bin2hex(x, spi_getrxbuf(port), spi_getbufsz(port));
  65. applog(LOG_DEBUG, "%s(%p): %cX %s", __func__, port, 'R', x);
  66. }
  67. return rv;
  68. }
  69. static
  70. void jingtian_precmd(struct spi_port * const spi)
  71. {
  72. spi_emit_buf(spi, jingtian_pre_header, sizeof(jingtian_pre_header));
  73. }
  74. static
  75. struct aan_hooks jingtian_hooks = {
  76. .precmd = jingtian_precmd,
  77. };
  78. static
  79. void jingtian_common_init(void)
  80. {
  81. RUNONCE();
  82. spi_init();
  83. for (int i = 0; i < sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio); ++i)
  84. bfg_gpio_setpin_output(jingtian_cs_gpio[i]);
  85. }
  86. static
  87. bool jingtian_detect_one(const char * const devpath)
  88. {
  89. int found = 0, chips;
  90. jingtian_common_init();
  91. const int fd = open(devpath, O_RDWR);
  92. if (fd < 0)
  93. applogr(false, LOG_DEBUG, "%s: Failed to open %s", jingtian_drv.dname, devpath);
  94. struct cgpu_info *cgpu, *prev_cgpu = NULL;
  95. struct spi_port *spi;
  96. int * const chipselect_current = malloc(sizeof(*spi->chipselect_current));
  97. *chipselect_current = -1;
  98. int devpath_len = strlen(devpath);
  99. int chipcount[jingtian_max_cs];
  100. struct spi_port *spi_a[jingtian_max_cs];
  101. for (int i = 0; i < jingtian_max_cs; ++i)
  102. {
  103. spi = spi_a[i] = calloc(sizeof(*spi), 1);
  104. spi->repr = malloc(devpath_len + 0x10);
  105. sprintf((void*)spi->repr, "%s(cs%d)", devpath, i);
  106. spi->txrx = jingtian_spi_txrx;
  107. spi->userp = &jingtian_hooks;
  108. spi->fd = fd;
  109. spi->speed = 4000000;
  110. spi->delay = 0;
  111. spi->mode = 1;
  112. spi->bits = 8;
  113. spi->chipselect = i;
  114. spi->chipselect_current = chipselect_current;
  115. }
  116. aan_detect_spi(chipcount, spi_a, jingtian_max_cs);
  117. for (int i = 0; i < jingtian_max_cs; ++i)
  118. {
  119. chips = chipcount[i];
  120. free((void*)spi_a[i]->repr);
  121. spi_a[i]->repr = NULL;
  122. if (chips <= 0)
  123. {
  124. free(spi_a[i]);
  125. continue;
  126. }
  127. cgpu = malloc(sizeof(*cgpu));
  128. *cgpu = (struct cgpu_info){
  129. .drv = &jingtian_drv,
  130. .procs = chips,
  131. .device_data = spi_a[i],
  132. };
  133. add_cgpu_slave(cgpu, prev_cgpu);
  134. prev_cgpu = cgpu;
  135. found += chips;
  136. }
  137. close(fd);
  138. if (!found)
  139. free(chipselect_current);
  140. return found;
  141. }
  142. static
  143. int jingtian_detect_auto(void)
  144. {
  145. return jingtian_detect_one("/dev/spidev0.0") ? 1 : 0;
  146. }
  147. static
  148. void jingtian_detect(void)
  149. {
  150. generic_detect(&jingtian_drv, jingtian_detect_one, jingtian_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  151. }
  152. struct device_drv jingtian_drv = {
  153. .dname = "jingtian",
  154. .name = "JTN",
  155. .drv_detect = jingtian_detect,
  156. };