driver-avalon4.h.bak 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright 2013-2015 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2012-2014 Xiangfu <xiangfu@openmobilefree.com>
  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 _AVALON4_H_
  11. #define _AVALON4_H_
  12. #include "util.h"
  13. #include "i2c-context.h"
  14. #ifdef WIN32
  15. #include <windows.h>
  16. #endif
  17. #ifdef USE_AVALON4
  18. #define AVA4_DEFAULT_FAN_MIN 10 /* % */
  19. #define AVA4_DEFAULT_FAN_MAX 100
  20. /* Percentage required to make sure fan starts spinning, then we can go down */
  21. #define AVA4_DEFAULT_FAN_START 15
  22. #define AVA4_FREEZESAFE_FAN 10
  23. #define AVA4_DEFAULT_TEMP_TARGET 65
  24. #define AVA4_DEFAULT_TEMP_OVERHEAT 85
  25. #define AVA4_MM40_TEMP_TARGET 42
  26. #define AVA4_MM40_TEMP_OVERHEAT 65
  27. #define AVA4_MM60_TEMP_FREQADJ 70
  28. #define AVA4_DEFAULT_VOLTAGE_MIN 4000
  29. #define AVA4_DEFAULT_VOLTAGE_MAX 9000
  30. #define AVA4_FREEZESAFE_VOLTAGE 4000
  31. #define AVA4_DEFAULT_FREQUENCY_MIN 100
  32. #define AVA4_DEFAULT_FREQUENCY_MAX 1000
  33. #define AVA4_FREEZESAFE_FREQUENCY 100
  34. #define AVA4_MM60_FREQUENCY_MAX 500
  35. #define AVA4_DEFAULT_MODULARS 7 /* Only support 6 modules maximum with one AUC */
  36. #define AVA4_DEFAULT_MINER_MAX 10
  37. #define AVA4_DEFAULT_ASIC_MAX 40
  38. #define AVA4_DEFAULT_ADC_MAX 6 /* RNTC1-4, VCC12, VCC3VC */
  39. #define AVA4_DEFAULT_PLL_MAX 7
  40. #define AVA4_DEFAULT_MINER_CNT 10
  41. #define AVA4_DEFAULT_ASIC_CNT 4
  42. #define AVA4_MM50_MINER_CNT 2
  43. #define AVA4_MM50_ASIC_CNT 16
  44. #define AVA4_MM60_MINER_CNT 2
  45. #define AVA4_MM60_ASIC_CNT 40
  46. #define AVA4_DEFAULT_VOLTAGE 6875
  47. #define AVA4_DEFAULT_FREQUENCY 200
  48. #define AVA4_DEFAULT_POLLING_DELAY 20 /* ms */
  49. #define AVA4_DEFAULT_ADJ_TIMES 6
  50. #define AVA4_DEFAULT_NTCB 3450
  51. #define AVA4_DEFAULT_NCHECK true
  52. #define AVA4_DEFAULT_SPEED_BINGO 255
  53. #define AVA4_DEFAULT_SPEED_ERROR 3
  54. #define AVA4_DEFAULT_SMARTSPEED_OFF 0
  55. #define AVA4_DEFAULT_SMARTSPEED_MODE1 1
  56. #define AVA4_DEFAULT_SMARTSPEED_MODE2 2
  57. #define AVA4_DEFAULT_SMARTSPEED_MODE3 3
  58. #define AVA4_DEFAULT_SMART_SPEED (AVA4_DEFAULT_SMARTSPEED_MODE3)
  59. #define AVA4_DEFAULT_IIC_DETECT false
  60. #define AVA4_DH_INC 0.03
  61. #define AVA4_DH_DEC 0.002
  62. #define AVA4_PWM_MAX 0x3FF
  63. #define AVA4_ADC_MAX 0x3FF
  64. #define AVA4_DRV_DIFFMAX 1024
  65. #define AVA4_AUC_VER_LEN 12 /* Version length: 12 (AUC-YYYYMMDD) */
  66. #define AVA4_AUC_SPEED 400000
  67. #define AVA4_AUC_XDELAY 19200 /* 4800 = 1ms in AUC (11U14) */
  68. #define AVA4_AUC_P_SIZE 64
  69. #define AVA4_MOD_CUSTOM 0x0
  70. #define AVA4_MOD_ECO 0x1
  71. #define AVA4_MOD_NORMAL 0x2
  72. #define AVA4_MOD_TURBO 0x3
  73. #define AVA4_CONNECTER_AUC 1
  74. #define AVA4_CONNECTER_IIC 2
  75. /* Avalon4 protocol package type from MM protocol.h
  76. * https://github.com/Canaan-Creative/MM/blob/avalon4/firmware/protocol.h */
  77. #define AVA4_MM_VER_LEN 15
  78. #define AVA4_MM_DNA_LEN 8
  79. #define AVA4_H1 'C'
  80. #define AVA4_H2 'N'
  81. #define AVA4_P_COINBASE_SIZE (6 * 1024 + 64)
  82. #define AVA4_P_MERKLES_COUNT 30
  83. #define AVA4_P_COUNT 40
  84. #define AVA4_P_DATA_LEN 32
  85. /* Broadcase with block iic_write*/
  86. #define AVA4_P_DETECT 0x10
  87. /* Broadcase With non-block iic_write*/
  88. #define AVA4_P_STATIC 0x11
  89. #define AVA4_P_JOB_ID 0x12
  90. #define AVA4_P_COINBASE 0x13
  91. #define AVA4_P_MERKLES 0x14
  92. #define AVA4_P_HEADER 0x15
  93. #define AVA4_P_TARGET 0x16
  94. /* Broadcase or Address */
  95. #define AVA4_P_SET 0x20
  96. #define AVA4_P_FINISH 0x21
  97. #define AVA4_P_SET_VOLT 0x22
  98. #define AVA4_P_SET_FREQ 0x23
  99. /* Have to with I2C address */
  100. #define AVA4_P_POLLING 0x30
  101. #define AVA4_P_REQUIRE 0x31
  102. #define AVA4_P_TEST 0x32
  103. #define AVA4_P_RSTMMTX 0x33
  104. #define AVA4_P_GET_VOLT 0x34
  105. /* Back to host */
  106. #define AVA4_P_ACKDETECT 0x40
  107. #define AVA4_P_STATUS 0x41
  108. #define AVA4_P_NONCE 0x42
  109. #define AVA4_P_TEST_RET 0x43
  110. #define AVA4_P_STATUS_LW 0x44
  111. #define AVA4_P_STATUS_HW 0x45
  112. #define AVA4_P_STATUS_VOLT 0x46
  113. #define AVA4_P_STATUS_MA 0x47
  114. #define AVA4_P_STATUS_M 0x48
  115. #define AVA4_MODULE_BROADCAST 0
  116. /* Endof Avalon4 protocol package type */
  117. #define AVA4_MM40_PREFIXSTR "40"
  118. #define AVA4_MM41_PREFIXSTR "41"
  119. #define AVA4_MM50_PREFIXSTR "50"
  120. #define AVA4_MM60_PREFIXSTR "60"
  121. #define AVA4_MM_VERNULL "NONE"
  122. #define AVA4_TYPE_MM40 40
  123. #define AVA4_TYPE_MM41 41
  124. #define AVA4_TYPE_MM50 50
  125. #define AVA4_TYPE_MM60 60
  126. #define AVA4_TYPE_NULL 00
  127. #define AVA4_IIC_RESET 0xa0
  128. #define AVA4_IIC_INIT 0xa1
  129. #define AVA4_IIC_DEINIT 0xa2
  130. #define AVA4_IIC_XFER 0xa5
  131. #define AVA4_IIC_INFO 0xa6
  132. #define AVA4_FREQ_INIT_MODE 0x0
  133. #define AVA4_FREQ_CUTOFF_MODE 0x1
  134. #define AVA4_FREQ_TEMPADJ_MODE 0x2
  135. #define AVA4_FREQ_PLLADJ_MODE 0x3
  136. /* pll check range [0, 7680], 0 means turn off check */
  137. #define AVA4_DEFAULT_LEAST_PLL 768
  138. #define AVA4_DEFAULT_MOST_PLL 256
  139. /* seconds */
  140. #define AVA4_DEFAULT_FDEC_TIME 60.0
  141. #define AVA4_DEFAULT_FINC_TIME 1200.0
  142. #define AVA4_DEFAULT_FAVG_TIME (15 * 60.0)
  143. #define AVA4_DEFAULT_FREQADJ_TIME 60
  144. #define AVA4_DEFAULT_DELTA_T 0
  145. #define AVA4_DEFAULT_DELTA_FREQ 100
  146. struct avalon4_pkg {
  147. uint8_t head[2];
  148. uint8_t type;
  149. uint8_t opt;
  150. uint8_t idx;
  151. uint8_t cnt;
  152. uint8_t data[32];
  153. uint8_t crc[2];
  154. };
  155. #define avalon4_ret avalon4_pkg
  156. struct avalon4_info {
  157. cglock_t update_lock;
  158. int polling_first;
  159. int polling_err_cnt[AVA4_DEFAULT_MODULARS];
  160. int xfer_err_cnt;
  161. int pool_no;
  162. struct pool pool0;
  163. struct pool pool1;
  164. struct pool pool2;
  165. struct timeval last_fan;
  166. struct timeval last_stratum;
  167. char auc_version[AVA4_AUC_VER_LEN + 1];
  168. int auc_speed;
  169. int auc_xdelay;
  170. int auc_temp;
  171. int mm_count;
  172. uint8_t connecter;
  173. unsigned int set_frequency[AVA4_DEFAULT_MODULARS][3];
  174. unsigned int set_smart_frequency[AVA4_DEFAULT_MODULARS][3];
  175. int set_frequency_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX][AVA4_DEFAULT_ASIC_MAX][3];
  176. int set_voltage[AVA4_DEFAULT_MODULARS];
  177. uint16_t set_voltage_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  178. int8_t set_voltage_offset[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  179. int mod_type[AVA4_DEFAULT_MODULARS];
  180. bool enable[AVA4_DEFAULT_MODULARS];
  181. struct timeval elapsed[AVA4_DEFAULT_MODULARS];
  182. struct timeval firsthash;
  183. char mm_version[AVA4_DEFAULT_MODULARS][AVA4_MM_VER_LEN + 1];
  184. uint8_t mm_dna[AVA4_DEFAULT_MODULARS][AVA4_MM_DNA_LEN + 1];
  185. int get_voltage[AVA4_DEFAULT_MODULARS];
  186. int get_voltage_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  187. int get_frequency[AVA4_DEFAULT_MODULARS];
  188. int power_good[AVA4_DEFAULT_MODULARS];
  189. int error_code[AVA4_DEFAULT_MODULARS];
  190. int fan_pct[AVA4_DEFAULT_MODULARS];
  191. int fan[AVA4_DEFAULT_MODULARS];
  192. int temp[AVA4_DEFAULT_MODULARS];
  193. int led_red[AVA4_DEFAULT_MODULARS];
  194. uint16_t adc[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_ADC_MAX];
  195. uint16_t pll_sel[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_PLL_MAX];
  196. uint64_t local_works[AVA4_DEFAULT_MODULARS];
  197. uint64_t local_works_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  198. uint64_t hw_works[AVA4_DEFAULT_MODULARS];
  199. uint64_t hw_works_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  200. uint32_t local_work[AVA4_DEFAULT_MODULARS];
  201. uint32_t hw_work[AVA4_DEFAULT_MODULARS];
  202. uint32_t lw5[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_ADJ_TIMES];
  203. uint32_t lw5_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX][AVA4_DEFAULT_ADJ_TIMES];
  204. uint32_t hw5[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_ADJ_TIMES];
  205. uint32_t hw5_i[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX][AVA4_DEFAULT_ADJ_TIMES];
  206. int i_5s;
  207. struct timeval last_30s;
  208. struct timeval last_5s;
  209. struct timeval last_finc[AVA4_DEFAULT_MODULARS];
  210. struct timeval last_fdec[AVA4_DEFAULT_MODULARS];
  211. struct timeval last_favg[AVA4_DEFAULT_MODULARS];
  212. struct timeval last_fadj;
  213. struct timeval last_tcheck;
  214. int matching_work[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  215. int chipmatching_work[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX][AVA4_DEFAULT_ASIC_MAX];
  216. uint8_t saved[AVA4_DEFAULT_MODULARS];
  217. uint8_t adjflag[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX];
  218. uint8_t cutoff[AVA4_DEFAULT_MODULARS];
  219. uint8_t miner_count[AVA4_DEFAULT_MODULARS];
  220. uint8_t asic_count[AVA4_DEFAULT_MODULARS];
  221. int ntime_offset[AVA4_DEFAULT_MODULARS];
  222. bool autov[AVA4_DEFAULT_MODULARS];
  223. uint8_t ma_sum[AVA4_DEFAULT_MODULARS][AVA4_DEFAULT_MINER_MAX][AVA4_DEFAULT_ASIC_MAX];
  224. uint32_t newnonce;
  225. uint32_t total_asics[AVA4_DEFAULT_MODULARS];
  226. int toverheat[AVA4_DEFAULT_MODULARS];
  227. int temp_target[AVA4_DEFAULT_MODULARS];
  228. uint8_t speed_bingo[AVA4_DEFAULT_MODULARS];
  229. uint8_t speed_error[AVA4_DEFAULT_MODULARS];
  230. uint32_t freq_mode[AVA4_DEFAULT_MODULARS];
  231. struct i2c_ctx *i2c_slaves[AVA4_DEFAULT_MODULARS];
  232. int last_maxtemp[AVA4_DEFAULT_MODULARS];
  233. };
  234. struct avalon4_iic_info {
  235. uint8_t iic_op;
  236. union {
  237. uint32_t aucParam[2];
  238. uint8_t slave_addr;
  239. } iic_param;
  240. };
  241. #define AVA4_WRITE_SIZE (sizeof(struct avalon4_pkg))
  242. #define AVA4_READ_SIZE AVA4_WRITE_SIZE
  243. #define AVA4_SEND_OK 0
  244. #define AVA4_SEND_ERROR -1
  245. extern char *set_avalon4_fan(char *arg);
  246. extern char *set_avalon4_temp(char *arg);
  247. extern char *set_avalon4_freq(char *arg);
  248. extern char *set_avalon4_voltage(char *arg);
  249. extern bool opt_avalon4_autov;
  250. extern bool opt_avalon4_freezesafe;
  251. extern int opt_avalon4_temp_target;
  252. extern int opt_avalon4_overheat;
  253. extern int opt_avalon4_polling_delay;
  254. extern int opt_avalon4_aucspeed;
  255. extern int opt_avalon4_aucxdelay;
  256. extern int opt_avalon4_ntime_offset;
  257. extern int opt_avalon4_miningmode;
  258. extern int opt_avalon4_ntcb;
  259. extern int opt_avalon4_freq_min;
  260. extern int opt_avalon4_freq_max;
  261. extern bool opt_avalon4_noncecheck;
  262. extern int opt_avalon4_smart_speed;
  263. extern int opt_avalon4_speed_bingo;
  264. extern int opt_avalon4_speed_error;
  265. extern int opt_avalon4_least_pll_check;
  266. extern int opt_avalon4_most_pll_check;
  267. extern bool opt_avalon4_iic_detect;
  268. extern int opt_avalon4_freqadj_time;
  269. extern int opt_avalon4_delta_temp;
  270. extern int opt_avalon4_delta_freq;
  271. extern int opt_avalon4_freqadj_temp;
  272. #endif /* USE_AVALON4 */
  273. #endif /* _AVALON4_H_ */