driver-avalon7.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright 2016 Mikeqin <Fengling.Qin@gmail.com>
  3. * Copyright 2016 Con Kolivas <kernel@kolivas.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #ifndef _AVALON7_H_
  11. #define _AVALON7_H_
  12. #include "util.h"
  13. #include "i2c-context.h"
  14. #ifdef USE_AVALON7
  15. #define AVA7_FREQUENCY_MAX 1404
  16. #define AVA7_DEFAULT_FAN_MIN 5 /* % */
  17. #define AVA7_DEFAULT_FAN_MAX 100
  18. #define AVA7_DEFAULT_FAN_INTERVAL 15 /* Seconds */
  19. #define AVA7_DEFAULT_TEMP_TARGET 90
  20. #define AVA7_DEFAULT_TEMP_OVERHEAT 105
  21. #define AVA7_DEFAULT_TEMP_HYSTERESIS 5
  22. #define AVA7_DEFAULT_VOLTAGE_MIN 3889
  23. #define AVA7_DEFAULT_VOLTAGE_MAX 5059
  24. #define AVA7_INVALID_VOLTAGE 0
  25. #define AVA7_DEFAULT_VOLTAGE_STEP 78
  26. #define AVA7_DEFAULT_VOLTAGE_LEVEL_MIN 0
  27. #define AVA7_DEFAULT_VOLTAGE_LEVEL_MAX 15
  28. #define AVA7_DEFAULT_VOLTAGE_OFFSET_MIN -2
  29. #define AVA7_DEFAULT_VOLTAGE_OFFSET 0
  30. #define AVA7_DEFAULT_VOLTAGE_OFFSET_MAX 1
  31. #define AVA7_DEFAULT_FACTORY_INFO_0_MIN -15
  32. #define AVA7_DEFAULT_FACTORY_INFO_0 0
  33. #define AVA7_DEFAULT_FACTORY_INFO_0_MAX 15
  34. #define AVA7_DEFAULT_FREQUENCY_MIN 24 /* NOTE: It cann't support 0 */
  35. #define AVA7_DEFAULT_FREQUENCY_0 600
  36. #define AVA7_DEFAULT_FREQUENCY_1 636
  37. #define AVA7_DEFAULT_FREQUENCY_2 672
  38. #define AVA7_DEFAULT_FREQUENCY_3 708
  39. #define AVA7_DEFAULT_FREQUENCY_4 744
  40. #define AVA7_DEFAULT_FREQUENCY_5 780
  41. #define AVA7_DEFAULT_FREQUENCY_MAX 1404
  42. #define AVA7_DEFAULT_FREQUENCY_SEL 0
  43. #define AVA7_DEFAULT_MODULARS 7 /* Only support 6 modules maximum with one AUC */
  44. #define AVA7_DEFAULT_MINER_CNT 4
  45. #define AVA7_DEFAULT_ASIC_MAX 26
  46. #define AVA7_DEFAULT_PLL_CNT 6
  47. #define AVA7_DEFAULT_PMU_CNT 2
  48. #define AVA7_DEFAULT_POLLING_DELAY 20 /* ms */
  49. #define AVA7_DEFAULT_SMARTSPEED_OFF 0
  50. #define AVA7_DEFAULT_SMARTSPEED_MODE1 1
  51. #define AVA7_DEFAULT_SMART_SPEED (AVA7_DEFAULT_SMARTSPEED_MODE1)
  52. #define AVA7_DEFAULT_TH_PASS (162)
  53. #define AVA7_DEFAULT_TH_FAIL (10921)
  54. #define AVA7_DEFAULT_TH_INIT (0xffff / 2)
  55. #define AVA7_DEFAULT_TH_MS 1
  56. #define AVA7_DEFAULT_TH_TIMEOUT 0
  57. #define AVA7_DEFAULT_NONCE_MASK 29
  58. #define AVA7_DEFAULT_IIC_DETECT false
  59. #define AVA7_PWM_MAX 0x3FF
  60. #define AVA7_DRV_DIFFMAX 500
  61. #define AVA7_ASIC_TIMEOUT_CONST 0x2000000 /* 2 ^ 32 / 128 */
  62. #define AVA7_MODULE_DETECT_INTERVAL 30 /* 30 s */
  63. #define AVA7_AUC_VER_LEN 12 /* Version length: 12 (AUC-YYYYMMDD) */
  64. #define AVA7_AUC_SPEED 400000
  65. #define AVA7_AUC_XDELAY 19200 /* 4800 = 1ms in AUC (11U14) */
  66. #define AVA7_AUC_P_SIZE 64
  67. #define AVA7_CONNECTER_AUC 1
  68. #define AVA7_CONNECTER_IIC 2
  69. /* avalon7 protocol package type from MM protocol.h
  70. * https://github.com/Canaan-Creative/MM/blob/avalon7/firmware/protocol.h */
  71. #define AVA7_MM_VER_LEN 15
  72. #define AVA7_MM_DNA_LEN 8
  73. #define AVA7_H1 'C'
  74. #define AVA7_H2 'N'
  75. #define AVA7_P_COINBASE_SIZE (6 * 1024 + 64)
  76. #define AVA7_P_MERKLES_COUNT 30
  77. #define AVA7_P_COUNT 40
  78. #define AVA7_P_DATA_LEN 32
  79. /* Broadcase with block iic_write*/
  80. #define AVA7_P_DETECT 0x10
  81. /* Broadcase With non-block iic_write*/
  82. #define AVA7_P_STATIC 0x11
  83. #define AVA7_P_JOB_ID 0x12
  84. #define AVA7_P_COINBASE 0x13
  85. #define AVA7_P_MERKLES 0x14
  86. #define AVA7_P_HEADER 0x15
  87. #define AVA7_P_TARGET 0x16
  88. #define AVA7_P_JOB_FIN 0x17
  89. /* ss package */
  90. #define AVA7_P_MID 0xa7
  91. /* Broadcase or with I2C address */
  92. #define AVA7_P_SET 0x20
  93. #define AVA7_P_SET_FIN 0x21
  94. #define AVA7_P_SET_VOLT 0x22
  95. #define AVA7_P_SET_PMU 0x24
  96. #define AVA7_P_SET_PLL 0x25
  97. #define AVA7_P_SET_SS 0x26
  98. #define AVA7_P_SET_FAC 0x28
  99. /* Have to send with I2C address */
  100. #define AVA7_P_POLLING 0x30
  101. #define AVA7_P_SYNC 0x31
  102. #define AVA7_P_TEST 0x32
  103. #define AVA7_P_RSTMMTX 0x33
  104. #define AVA7_P_GET_VOLT 0x34
  105. /* Back to host */
  106. #define AVA7_P_ACKDETECT 0x40
  107. #define AVA7_P_STATUS 0x41
  108. #define AVA7_P_NONCE 0x42
  109. #define AVA7_P_TEST_RET 0x43
  110. #define AVA7_P_STATUS_VOLT 0x46
  111. #define AVA7_P_STATUS_PMU 0x48
  112. #define AVA7_P_STATUS_PLL 0x49
  113. #define AVA7_P_STATUS_LOG 0x4a
  114. #define AVA7_P_STATUS_ASIC 0x4b
  115. #define AVA7_P_STATUS_PVT 0x4c
  116. #define AVA7_P_STATUS_FAC 0x4d
  117. #define AVA7_MODULE_BROADCAST 0
  118. /* End of avalon7 protocol package type */
  119. #define AVA7_IIC_RESET 0xa0
  120. #define AVA7_IIC_INIT 0xa1
  121. #define AVA7_IIC_DEINIT 0xa2
  122. #define AVA7_IIC_XFER 0xa5
  123. #define AVA7_IIC_INFO 0xa6
  124. #define AVA7_FREQ_INIT_MODE 0x0
  125. #define AVA7_FREQ_PLLADJ_MODE 0x1
  126. #define AVA7_DEFAULT_FAVG_TIME (15 * 60.0)
  127. #define AVA7_DEFAULT_FREQADJ_TIME 60
  128. #define AVA7_DEFAULT_DELTA_T 0
  129. #define AVA7_DEFAULT_DELTA_FREQ 100
  130. #define AVA7_DEFAULT_FACTORY_INFO_CNT 1
  131. #define AVA7_MM711_VIN_ADC_RATIO (3.3 / 1024.0 * 27.15 / 7.15 * 1000.0 * 100.0)
  132. #define AVA7_MM721_VIN_ADC_RATIO (3.3 / 1024.0 * 27.15 / 7.15 * 1000.0 * 100.0)
  133. #define AVA7_MM741_VIN_ADC_RATIO (3.3 / 1024.0 * 27.15 / 7.15 * 1000.0 * 100.0)
  134. #define AVA7_MM761_VIN_ADC_RATIO (3.3 / 1024.0 * 26.0 / 6.0 * 1000.0 * 100.0)
  135. #define AVA7_MM711_VOUT_ADC_RATIO (3.3 / 1024.0 * 125.0 / 43.0 * 10000.0 * 100.0)
  136. #define AVA7_MM721_VOUT_ADC_RATIO (3.3 / 1024.0 * 125.0 / 43.0 * 10000.0 * 100.0)
  137. #define AVA7_MM741_VOUT_ADC_RATIO (3.3 / 1024.0 * 63.0 / 20.0 * 10000.0 * 100.0)
  138. #define AVA7_MM761_VOUT_ADC_RATIO (3.3 / 1024.0 * 26.0 / 6.0 * 10000.0 * 100.0)
  139. struct avalon7_pkg {
  140. uint8_t head[2];
  141. uint8_t type;
  142. uint8_t opt;
  143. uint8_t idx;
  144. uint8_t cnt;
  145. uint8_t data[32];
  146. uint8_t crc[2];
  147. };
  148. #define avalon7_ret avalon7_pkg
  149. struct avalon7_info {
  150. /* Public data */
  151. int64_t last_diff1;
  152. int64_t pending_diff1;
  153. double last_rej;
  154. int mm_count;
  155. int xfer_err_cnt;
  156. int pool_no;
  157. struct timeval firsthash;
  158. struct timeval last_fan_adj;
  159. struct timeval last_stratum;
  160. struct timeval last_detect;
  161. cglock_t update_lock;
  162. struct pool pool0;
  163. struct pool pool1;
  164. struct pool pool2;
  165. bool work_restart;
  166. uint32_t last_jobid;
  167. /* For connecter */
  168. char auc_version[AVA7_AUC_VER_LEN + 1];
  169. int auc_speed;
  170. int auc_xdelay;
  171. int auc_sensor;
  172. struct i2c_ctx *i2c_slaves[AVA7_DEFAULT_MODULARS];
  173. uint8_t connecter; /* AUC or IIC */
  174. /* For modulars */
  175. bool enable[AVA7_DEFAULT_MODULARS];
  176. bool reboot[AVA7_DEFAULT_MODULARS];
  177. struct timeval elapsed[AVA7_DEFAULT_MODULARS];
  178. uint8_t mm_dna[AVA7_DEFAULT_MODULARS][AVA7_MM_DNA_LEN];
  179. char mm_version[AVA7_DEFAULT_MODULARS][AVA7_MM_VER_LEN + 1]; /* It's a string */
  180. uint32_t total_asics[AVA7_DEFAULT_MODULARS];
  181. uint32_t max_ntime; /* Maximum: 7200 */
  182. int mod_type[AVA7_DEFAULT_MODULARS];
  183. uint8_t miner_count[AVA7_DEFAULT_MODULARS];
  184. uint8_t asic_count[AVA7_DEFAULT_MODULARS];
  185. uint32_t freq_mode[AVA7_DEFAULT_MODULARS];
  186. int led_indicator[AVA7_DEFAULT_MODULARS];
  187. int fan_pct[AVA7_DEFAULT_MODULARS];
  188. int fan_cpm[AVA7_DEFAULT_MODULARS];
  189. int temp[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT][5];
  190. int temp_mm[AVA7_DEFAULT_MODULARS];
  191. uint8_t cutoff[AVA7_DEFAULT_MODULARS];
  192. int temp_target[AVA7_DEFAULT_MODULARS];
  193. int temp_last_max[AVA7_DEFAULT_MODULARS];
  194. int temp_overheat[AVA7_DEFAULT_MODULARS];
  195. time_t last_temp_time[AVA7_DEFAULT_MODULARS];
  196. uint32_t set_voltage[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  197. uint32_t set_frequency[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT][AVA7_DEFAULT_PLL_CNT];
  198. uint16_t get_vin[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  199. uint32_t get_voltage[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  200. uint32_t get_pll[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT][AVA7_DEFAULT_PLL_CNT];
  201. /* spd_pass(4B), spd_fail(4B), sum_failed(4B), sum_num(4B), sum_xor(4B), PLL(6 * 4B) */
  202. uint32_t get_asic[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT][AVA7_DEFAULT_ASIC_MAX][11];
  203. int8_t factory_info[AVA7_DEFAULT_FACTORY_INFO_CNT];
  204. uint64_t local_works[AVA7_DEFAULT_MODULARS];
  205. uint64_t local_works_i[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  206. uint64_t hw_works[AVA7_DEFAULT_MODULARS];
  207. uint64_t hw_works_i[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  208. uint64_t chip_matching_work[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT][AVA7_DEFAULT_ASIC_MAX];
  209. uint32_t error_code[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT + 1];
  210. uint32_t error_crc[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  211. uint8_t error_polling_cnt[AVA7_DEFAULT_MODULARS];
  212. uint8_t power_good[AVA7_DEFAULT_MODULARS];
  213. char pmu_version[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_PMU_CNT][5];
  214. uint64_t diff1[AVA7_DEFAULT_MODULARS];
  215. uint16_t vin_adc_ratio[AVA7_DEFAULT_MODULARS];
  216. uint16_t vout_adc_ratio[AVA7_DEFAULT_MODULARS];
  217. bool conn_overloaded;
  218. };
  219. struct avalon7_iic_info {
  220. uint8_t iic_op;
  221. union {
  222. uint32_t aucParam[2];
  223. uint8_t slave_addr;
  224. } iic_param;
  225. };
  226. struct avalon7_dev_description {
  227. uint8_t dev_id_str[8];
  228. int mod_type;
  229. uint8_t miner_count; /* it should not greater than AVA7_DEFAULT_MINER_CNT */
  230. uint8_t asic_count; /* asic count each miner, it should not great than AVA7_DEFAULT_ASIC_MAX */
  231. uint16_t vin_adc_ratio;
  232. uint16_t vout_adc_ratio;
  233. uint32_t set_voltage;
  234. };
  235. #define AVA7_WRITE_SIZE (sizeof(struct avalon7_pkg))
  236. #define AVA7_READ_SIZE AVA7_WRITE_SIZE
  237. #define AVA7_SEND_OK 0
  238. #define AVA7_SEND_ERROR -1
  239. extern char *set_avalon7_fan(char *arg);
  240. extern char *set_avalon7_freq(char *arg);
  241. extern char *set_avalon7_voltage(char *arg);
  242. extern char *set_avalon7_voltage_level(char *arg);
  243. extern char *set_avalon7_voltage_offset(char *arg);
  244. extern int opt_avalon7_temp_target;
  245. extern int opt_avalon7_polling_delay;
  246. extern int opt_avalon7_aucspeed;
  247. extern int opt_avalon7_aucxdelay;
  248. extern int opt_avalon7_smart_speed;
  249. extern bool opt_avalon7_iic_detect;
  250. extern int opt_avalon7_freq_sel;
  251. extern uint32_t opt_avalon7_th_pass;
  252. extern uint32_t opt_avalon7_th_fail;
  253. extern uint32_t opt_avalon7_th_init;
  254. extern uint32_t opt_avalon7_th_ms;
  255. extern uint32_t opt_avalon7_th_timeout;
  256. extern uint32_t opt_avalon7_nonce_mask;
  257. extern bool opt_avalon7_asic_debug;
  258. #endif /* USE_AVALON7 */
  259. #endif /* _AVALON7_H_ */