driver-avalon7.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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_NTIME_OFFSET 40
  50. #define AVA7_DEFAULT_SMARTSPEED_OFF 0
  51. #define AVA7_DEFAULT_SMARTSPEED_MODE1 1
  52. #define AVA7_DEFAULT_SMART_SPEED (AVA7_DEFAULT_SMARTSPEED_MODE1)
  53. #define AVA7_DEFAULT_TH_PASS (162)
  54. #define AVA7_DEFAULT_TH_FAIL (10921)
  55. #define AVA7_DEFAULT_TH_INIT (0xffff / 2)
  56. #define AVA7_DEFAULT_TH_MS 1
  57. #define AVA7_DEFAULT_TH_TIMEOUT 0
  58. #define AVA7_DEFAULT_NONCE_MASK 29
  59. #define AVA7_DEFAULT_IIC_DETECT false
  60. #define AVA7_PWM_MAX 0x3FF
  61. #define AVA7_DRV_DIFFMAX 500
  62. #define AVA7_ASIC_TIMEOUT_CONST 0x2000000 /* 2 ^ 32 / 128 */
  63. #define AVA7_MODULE_DETECT_INTERVAL 30 /* 30 s */
  64. #define AVA7_AUC_VER_LEN 12 /* Version length: 12 (AUC-YYYYMMDD) */
  65. #define AVA7_AUC_SPEED 400000
  66. #define AVA7_AUC_XDELAY 19200 /* 4800 = 1ms in AUC (11U14) */
  67. #define AVA7_AUC_P_SIZE 64
  68. #define AVA7_CONNECTER_AUC 1
  69. #define AVA7_CONNECTER_IIC 2
  70. /* avalon7 protocol package type from MM protocol.h
  71. * https://github.com/Canaan-Creative/MM/blob/avalon7/firmware/protocol.h */
  72. #define AVA7_MM_VER_LEN 15
  73. #define AVA7_MM_DNA_LEN 8
  74. #define AVA7_H1 'C'
  75. #define AVA7_H2 'N'
  76. #define AVA7_P_COINBASE_SIZE (6 * 1024 + 64)
  77. #define AVA7_P_MERKLES_COUNT 30
  78. #define AVA7_P_COUNT 40
  79. #define AVA7_P_DATA_LEN 32
  80. /* Broadcase with block iic_write*/
  81. #define AVA7_P_DETECT 0x10
  82. /* Broadcase With non-block iic_write*/
  83. #define AVA7_P_STATIC 0x11
  84. #define AVA7_P_JOB_ID 0x12
  85. #define AVA7_P_COINBASE 0x13
  86. #define AVA7_P_MERKLES 0x14
  87. #define AVA7_P_HEADER 0x15
  88. #define AVA7_P_TARGET 0x16
  89. #define AVA7_P_JOB_FIN 0x17
  90. /* ss package */
  91. #define AVA7_P_MID 0xa7
  92. /* Broadcase or with I2C address */
  93. #define AVA7_P_SET 0x20
  94. #define AVA7_P_SET_FIN 0x21
  95. #define AVA7_P_SET_VOLT 0x22
  96. #define AVA7_P_SET_PMU 0x24
  97. #define AVA7_P_SET_PLL 0x25
  98. #define AVA7_P_SET_SS 0x26
  99. #define AVA7_P_PAIRS 0x27
  100. #define AVA7_P_SET_FAC 0x28
  101. /* Have to send with I2C address */
  102. #define AVA7_P_POLLING 0x30
  103. #define AVA7_P_SYNC 0x31
  104. #define AVA7_P_TEST 0x32
  105. #define AVA7_P_RSTMMTX 0x33
  106. #define AVA7_P_GET_VOLT 0x34
  107. /* Back to host */
  108. #define AVA7_P_ACKDETECT 0x40
  109. #define AVA7_P_STATUS 0x41
  110. #define AVA7_P_NONCE 0x42
  111. #define AVA7_P_TEST_RET 0x43
  112. #define AVA7_P_STATUS_VOLT 0x46
  113. #define AVA7_P_STATUS_PMU 0x48
  114. #define AVA7_P_STATUS_PLL 0x49
  115. #define AVA7_P_STATUS_LOG 0x4a
  116. #define AVA7_P_STATUS_ASIC 0x4b
  117. #define AVA7_P_STATUS_PVT 0x4c
  118. #define AVA7_P_STATUS_FAC 0x4d
  119. #define AVA7_MODULE_BROADCAST 0
  120. /* End of avalon7 protocol package type */
  121. #define AVA7_IIC_RESET 0xa0
  122. #define AVA7_IIC_INIT 0xa1
  123. #define AVA7_IIC_DEINIT 0xa2
  124. #define AVA7_IIC_XFER 0xa5
  125. #define AVA7_IIC_INFO 0xa6
  126. #define AVA7_FREQ_INIT_MODE 0x0
  127. #define AVA7_FREQ_PLLADJ_MODE 0x1
  128. #define AVA7_DEFAULT_FAVG_TIME (15 * 60.0)
  129. #define AVA7_DEFAULT_FREQADJ_TIME 60
  130. #define AVA7_DEFAULT_DELTA_T 0
  131. #define AVA7_DEFAULT_DELTA_FREQ 100
  132. #define AVA7_DEFAULT_FACTORY_INFO_CNT 1
  133. #define AVA7_MM711_VIN_ADC_RATIO (3.3 / 1024.0 * 27.15 / 7.15 * 1000.0 * 100.0)
  134. #define AVA7_MM721_VIN_ADC_RATIO (3.3 / 1024.0 * 27.15 / 7.15 * 1000.0 * 100.0)
  135. #define AVA7_MM741_VIN_ADC_RATIO (3.3 / 1024.0 * 27.15 / 7.15 * 1000.0 * 100.0)
  136. #define AVA7_MM761_VIN_ADC_RATIO (3.3 / 1024.0 * 26.0 / 6.0 * 1000.0 * 100.0)
  137. #define AVA7_MM711_VOUT_ADC_RATIO (3.3 / 1024.0 * 125.0 / 43.0 * 10000.0 * 100.0)
  138. #define AVA7_MM721_VOUT_ADC_RATIO (3.3 / 1024.0 * 125.0 / 43.0 * 10000.0 * 100.0)
  139. #define AVA7_MM741_VOUT_ADC_RATIO (3.3 / 1024.0 * 63.0 / 20.0 * 10000.0 * 100.0)
  140. #define AVA7_MM761_VOUT_ADC_RATIO (3.3 / 1024.0 * 26.0 / 6.0 * 10000.0 * 100.0)
  141. struct avalon7_pkg {
  142. uint8_t head[2];
  143. uint8_t type;
  144. uint8_t opt;
  145. uint8_t idx;
  146. uint8_t cnt;
  147. uint8_t data[32];
  148. uint8_t crc[2];
  149. };
  150. #define avalon7_ret avalon7_pkg
  151. struct avalon7_info {
  152. /* Public data */
  153. int64_t last_diff1;
  154. int64_t pending_diff1;
  155. double last_rej;
  156. int mm_count;
  157. int xfer_err_cnt;
  158. int pool_no;
  159. struct timeval firsthash;
  160. struct timeval last_fan_adj;
  161. struct timeval last_stratum;
  162. struct timeval last_detect;
  163. cglock_t update_lock;
  164. struct pool pool0;
  165. struct pool pool1;
  166. struct pool pool2;
  167. bool work_restart;
  168. uint32_t last_jobid;
  169. /* For connecter */
  170. char auc_version[AVA7_AUC_VER_LEN + 1];
  171. int auc_speed;
  172. int auc_xdelay;
  173. int auc_sensor;
  174. struct i2c_ctx *i2c_slaves[AVA7_DEFAULT_MODULARS];
  175. uint8_t connecter; /* AUC or IIC */
  176. /* For modulars */
  177. bool enable[AVA7_DEFAULT_MODULARS];
  178. bool reboot[AVA7_DEFAULT_MODULARS];
  179. struct timeval elapsed[AVA7_DEFAULT_MODULARS];
  180. uint8_t mm_dna[AVA7_DEFAULT_MODULARS][AVA7_MM_DNA_LEN];
  181. char mm_version[AVA7_DEFAULT_MODULARS][AVA7_MM_VER_LEN + 1]; /* It's a string */
  182. uint32_t total_asics[AVA7_DEFAULT_MODULARS];
  183. uint32_t max_ntime; /* Maximum: 7200 */
  184. int mod_type[AVA7_DEFAULT_MODULARS];
  185. uint8_t miner_count[AVA7_DEFAULT_MODULARS];
  186. uint8_t asic_count[AVA7_DEFAULT_MODULARS];
  187. uint32_t freq_mode[AVA7_DEFAULT_MODULARS];
  188. int led_indicator[AVA7_DEFAULT_MODULARS];
  189. int fan_pct[AVA7_DEFAULT_MODULARS];
  190. int fan_cpm[AVA7_DEFAULT_MODULARS];
  191. int temp[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT][5];
  192. int temp_mm[AVA7_DEFAULT_MODULARS];
  193. uint8_t cutoff[AVA7_DEFAULT_MODULARS];
  194. int temp_target[AVA7_DEFAULT_MODULARS];
  195. int temp_last_max[AVA7_DEFAULT_MODULARS];
  196. int temp_overheat[AVA7_DEFAULT_MODULARS];
  197. time_t last_temp_time[AVA7_DEFAULT_MODULARS];
  198. uint32_t set_voltage[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  199. uint32_t set_frequency[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT][AVA7_DEFAULT_PLL_CNT];
  200. uint16_t get_vin[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  201. uint32_t get_voltage[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  202. uint32_t get_pll[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT][AVA7_DEFAULT_PLL_CNT];
  203. /* spd_pass(4B), spd_fail(4B), sum_failed(4B), sum_num(4B), sum_xor(4B), PLL(6 * 4B) */
  204. uint32_t get_asic[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT][AVA7_DEFAULT_ASIC_MAX][11];
  205. int8_t factory_info[AVA7_DEFAULT_FACTORY_INFO_CNT];
  206. uint64_t local_works[AVA7_DEFAULT_MODULARS];
  207. uint64_t local_works_i[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  208. uint64_t hw_works[AVA7_DEFAULT_MODULARS];
  209. uint64_t hw_works_i[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  210. uint64_t chip_matching_work[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT][AVA7_DEFAULT_ASIC_MAX];
  211. uint32_t error_code[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT + 1];
  212. uint32_t error_crc[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_MINER_CNT];
  213. uint8_t error_polling_cnt[AVA7_DEFAULT_MODULARS];
  214. uint8_t power_good[AVA7_DEFAULT_MODULARS];
  215. char pmu_version[AVA7_DEFAULT_MODULARS][AVA7_DEFAULT_PMU_CNT][5];
  216. uint64_t diff1[AVA7_DEFAULT_MODULARS];
  217. uint16_t vin_adc_ratio[AVA7_DEFAULT_MODULARS];
  218. uint16_t vout_adc_ratio[AVA7_DEFAULT_MODULARS];
  219. bool conn_overloaded;
  220. uint64_t mm_got_pairs[AVA7_DEFAULT_MODULARS];
  221. uint64_t mm_got_invalid_pairs[AVA7_DEFAULT_MODULARS];
  222. uint64_t gen_pairs[AVA7_DEFAULT_MODULARS];
  223. /* SSP */
  224. pthread_t ssp_thr;
  225. };
  226. struct avalon7_iic_info {
  227. uint8_t iic_op;
  228. union {
  229. uint32_t aucParam[2];
  230. uint8_t slave_addr;
  231. } iic_param;
  232. };
  233. struct avalon7_dev_description {
  234. uint8_t dev_id_str[8];
  235. int mod_type;
  236. uint8_t miner_count; /* it should not greater than AVA7_DEFAULT_MINER_CNT */
  237. uint8_t asic_count; /* asic count each miner, it should not great than AVA7_DEFAULT_ASIC_MAX */
  238. uint16_t vin_adc_ratio;
  239. uint16_t vout_adc_ratio;
  240. uint32_t set_voltage;
  241. };
  242. #define AVA7_WRITE_SIZE (sizeof(struct avalon7_pkg))
  243. #define AVA7_READ_SIZE AVA7_WRITE_SIZE
  244. #define AVA7_SEND_OK 0
  245. #define AVA7_SEND_ERROR -1
  246. extern char *set_avalon7_fan(char *arg);
  247. extern char *set_avalon7_freq(char *arg);
  248. extern char *set_avalon7_voltage(char *arg);
  249. extern char *set_avalon7_voltage_level(char *arg);
  250. extern char *set_avalon7_voltage_offset(char *arg);
  251. extern int opt_avalon7_temp_target;
  252. extern int opt_avalon7_polling_delay;
  253. extern int opt_avalon7_aucspeed;
  254. extern int opt_avalon7_aucxdelay;
  255. extern int opt_avalon7_smart_speed;
  256. extern bool opt_avalon7_iic_detect;
  257. extern int opt_avalon7_freq_sel;
  258. extern uint32_t opt_avalon7_th_pass;
  259. extern uint32_t opt_avalon7_th_fail;
  260. extern uint32_t opt_avalon7_th_init;
  261. extern uint32_t opt_avalon7_th_ms;
  262. extern uint32_t opt_avalon7_th_timeout;
  263. extern uint32_t opt_avalon7_nonce_mask;
  264. extern bool opt_avalon7_asic_debug;
  265. extern bool opt_avalon7_ssplus_enable;
  266. #endif /* USE_AVALON7 */
  267. #endif /* _AVALON7_H_ */