driver-avalonlc3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright 2016-2017 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 _AVALONLC3_H_
  11. #define _AVALONLC3_H_
  12. #include "util.h"
  13. #include "i2c-context.h"
  14. #ifdef USE_AVALONLC3
  15. #define AVALC3_FREQUENCY_MAX 1404
  16. #define AVALC3_DEFAULT_FAN_MIN 5 /* % */
  17. #define AVALC3_DEFAULT_FAN_MAX 100
  18. #define AVALC3_DEFAULT_TEMP_TARGET 90
  19. #define AVALC3_DEFAULT_TEMP_OVERHEAT 105
  20. #define AVALC3_DEFAULT_VOLTAGE_LEVEL_MIN 0
  21. #define AVALC3_DEFAULT_VOLTAGE_LEVEL_MAX 31
  22. #define AVALC3_INVALID_VOLTAGE_LEVEL -1
  23. #define AVALC3_DEFAULT_VOLTAGE_LEVEL_OFFSET_MIN -2
  24. #define AVALC3_DEFAULT_VOLTAGE_LEVEL_OFFSET 0
  25. #define AVALC3_DEFAULT_VOLTAGE_LEVEL_OFFSET_MAX 1
  26. #define AVALC3_INVALID_ASIC_OTP -1
  27. #define AVALC3_DEFAULT_FACTORY_INFO_0_MIN -15
  28. #define AVALC3_DEFAULT_FACTORY_INFO_0 0
  29. #define AVALC3_DEFAULT_FACTORY_INFO_0_MAX 15
  30. #define AVALC3_DEFAULT_FACTORY_INFO_0_CNT 1
  31. #define AVALC3_DEFAULT_FACTORY_INFO_0_IGNORE 16
  32. #define AVALC3_DEFAULT_FACTORY_INFO_1_CNT 3
  33. #define AVALC3_DEFAULT_OVERCLOCKING_OFF 0
  34. #define AVALC3_DEFAULT_OVERCLOCKING_ON 1
  35. #define AVALC3_DEFAULT_FREQUENCY_0M 0
  36. #define AVALC3_DEFAULT_FREQUENCY_500M 500
  37. #define AVALC3_DEFAULT_FREQUENCY_MAX 1200
  38. #define AVALC3_DEFAULT_FREQUENCY (AVALC3_DEFAULT_FREQUENCY_MAX)
  39. #define AVALC3_DEFAULT_FREQUENCY_SEL 3
  40. #define AVALC3_DEFAULT_MODULARS 7 /* Only support 6 modules maximum with one AUC */
  41. #define AVALC3_DEFAULT_MINER_CNT 4
  42. #define AVALC3_DEFAULT_ASIC_MAX 34
  43. #define AVALC3_DEFAULT_PLL_CNT 4
  44. #define AVALC3_DEFAULT_CORE_VOLT_CNT 8
  45. #define AVALC3_DEFAULT_POLLING_DELAY 20 /* ms */
  46. #define AVALC3_DEFAULT_NTIME_OFFSET 2
  47. #define AVALC3_DEFAULT_SMARTSPEED_OFF 0
  48. #define AVALC3_DEFAULT_SMARTSPEED_MODE1 1
  49. #define AVALC3_DEFAULT_SMART_SPEED (AVALC3_DEFAULT_SMARTSPEED_MODE1)
  50. #define AVALC3_DEFAULT_TH_PASS 200
  51. #define AVALC3_DEFAULT_TH_FAIL 7000
  52. #define AVALC3_DEFAULT_TH_INIT 32767
  53. #define AVALC3_DEFAULT_TH_ADD 1
  54. #define AVALC3_DEFAULT_TH_MS 5
  55. #define AVALC3_DEFAULT_TH_TIMEOUT 16000
  56. #define AVALC3_DEFAULT_NONCE_MASK 27
  57. #define AVALC3_DEFAULT_NONCE_CHECK 1
  58. #define AVALC3_DEFAULT_MUX_L2H 0
  59. #define AVALC3_DEFAULT_MUX_H2L 1
  60. #define AVALC3_DEFAULT_H2LTIME0_SPD 3
  61. #define AVALC3_DEFAULT_ROLL_ENABLE 1
  62. #define AVALC3_DEFAULT_SPDLOW 2
  63. #define AVALC3_DEFAULT_SPDHIGH 3
  64. #define AVALC3_DEFAULT_TBASE 0
  65. /* PID CONTROLLER*/
  66. #define AVALC3_DEFAULT_PID_P 2
  67. #define AVALC3_DEFAULT_PID_I 5
  68. #define AVALC3_DEFAULT_PID_D 0
  69. #define AVALC3_DEFAULT_PID_TEMP_MIN 50
  70. #define AVALC3_DEFAULT_PID_TEMP_MAX 100
  71. #define AVALC3_DEFAULT_IIC_DETECT false
  72. #define AVALC3_PWM_MAX 0x3FF
  73. #define AVALC3_DRV_DIFFMAX 2700
  74. #define AVALC3_ASIC_TIMEOUT_CONST 419430400 /* (2^32 * 1000) / (256 * 40) */
  75. #define AVALC3_MODULE_DETECT_INTERVAL 30 /* 30 s */
  76. #define AVALC3_AUC_VER_LEN 12 /* Version length: 12 (AUC-YYYYMMDD) */
  77. #define AVALC3_AUC_SPEED 400000
  78. #define AVALC3_AUC_XDELAY 19200 /* 4800 = 1ms in AUC (11U14) */
  79. #define AVALC3_AUC_P_SIZE 64
  80. #define AVALC3_CONNECTER_AUC 1
  81. #define AVALC3_CONNECTER_IIC 2
  82. /* avalonlc3 protocol package type from MM protocol.h */
  83. #define AVALC3_MM_VER_LEN 15
  84. #define AVALC3_MM_DNA_LEN 8
  85. #define AVALC3_H1 'C'
  86. #define AVALC3_H2 'N'
  87. #define AVALC3_P_COINBASE_SIZE (6 * 1024 + 64)
  88. #define AVALC3_P_MERKLES_COUNT 30
  89. #define AVALC3_P_COUNT 40
  90. #define AVALC3_P_DATA_LEN 32
  91. #define AVALC3_OTP_LEN 32
  92. /* Broadcase with block iic_write*/
  93. #define AVALC3_P_DETECT 0x10
  94. /* Broadcase With non-block iic_write*/
  95. #define AVALC3_P_STATIC 0x11
  96. #define AVALC3_P_JOB_ID 0x12
  97. #define AVALC3_P_COINBASE 0x13
  98. #define AVALC3_P_MERKLES 0x14
  99. #define AVALC3_P_HEADER 0x15
  100. #define AVALC3_P_TARGET 0x16
  101. #define AVALC3_P_JOB_FIN 0x17
  102. /* Broadcase or with I2C address */
  103. #define AVALC3_P_SET 0x20
  104. #define AVALC3_P_SET_FIN 0x21
  105. #define AVALC3_P_SET_VOLT 0x22
  106. #define AVALC3_P_SET_PMU 0x24
  107. #define AVALC3_P_SET_PLL 0x25
  108. #define AVALC3_P_SET_SS 0x26
  109. /* 0x27 reserved */
  110. #define AVALC3_P_SET_FAC 0x28
  111. #define AVALC3_P_SET_OC 0x29
  112. /* Have to send with I2C address */
  113. #define AVALC3_P_POLLING 0x30
  114. #define AVALC3_P_SYNC 0x31
  115. #define AVALC3_P_TEST 0x32
  116. #define AVALC3_P_RSTMMTX 0x33
  117. #define AVALC3_P_GET_VOLT 0x34
  118. /* Back to host */
  119. #define AVALC3_P_ACKDETECT 0x40
  120. #define AVALC3_P_STATUS 0x41
  121. #define AVALC3_P_NONCE 0x42
  122. #define AVALC3_P_TEST_RET 0x43
  123. #define AVALC3_P_STATUS_VOLT 0x46
  124. #define AVALC3_P_STATUS_POWER 0x48
  125. #define AVALC3_P_STATUS_PLL 0x49
  126. #define AVALC3_P_STATUS_LOG 0x4a
  127. #define AVALC3_P_STATUS_ASIC 0x4b
  128. #define AVALC3_P_STATUS_PVT 0x4c
  129. #define AVALC3_P_STATUS_FAC 0x4d
  130. #define AVALC3_P_STATUS_OC 0x4e
  131. #define AVALC3_P_STATUS_OTP 0x4f
  132. #define AVALC3_P_SET_ASIC_OTP 0x50
  133. #define AVALC3_MODULE_BROADCAST 0
  134. /* End of avalonlc3 protocol package type */
  135. #define AVALC3_IIC_RESET 0xa0
  136. #define AVALC3_IIC_INIT 0xa1
  137. #define AVALC3_IIC_DEINIT 0xa2
  138. #define AVALC3_IIC_XFER 0xa5
  139. #define AVALC3_IIC_INFO 0xa6
  140. #define AVALC3_FREQ_INIT_MODE 0x0
  141. #define AVALC3_FREQ_PLLADJ_MODE 0x1
  142. #define AVALC3_DEFAULT_FACTORY_INFO_CNT (AVALC3_DEFAULT_FACTORY_INFO_0_CNT + AVALC3_DEFAULT_FACTORY_INFO_1_CNT)
  143. #define AVALC3_DEFAULT_POWER_INFO_CNT 6
  144. #define AVALC3_DEFAULT_OVERCLOCKING_CNT 1
  145. #define AVALC3_OTP_INDEX_READ_STEP 27
  146. #define AVALC3_OTP_INDEX_ASIC_NUM 28
  147. #define AVALC3_OTP_INDEX_CYCLE_HIT 29
  148. #define AVALC3_OTP_INFO_LOTIDCRC_OFFSET 0
  149. #define AVALC3_OTP_INFO_LOTID_OFFSET 6
  150. struct avalonlc3_pkg {
  151. uint8_t head[2];
  152. uint8_t type;
  153. uint8_t opt;
  154. uint8_t idx;
  155. uint8_t cnt;
  156. uint8_t data[32];
  157. uint8_t crc[2];
  158. };
  159. #define avalonlc3_ret avalonlc3_pkg
  160. struct avalonlc3_info {
  161. /* Public data */
  162. int64_t last_diff1;
  163. int64_t pending_diff1;
  164. double last_rej;
  165. int mm_count;
  166. int xfer_err_cnt;
  167. int pool_no;
  168. struct timeval firsthash;
  169. struct timeval last_fan_adj;
  170. struct timeval last_stratum;
  171. struct timeval last_detect;
  172. cglock_t update_lock;
  173. struct pool pool0;
  174. struct pool pool1;
  175. struct pool pool2;
  176. bool work_restart;
  177. uint32_t last_jobid;
  178. /* For connecter */
  179. char auc_version[AVALC3_AUC_VER_LEN + 1];
  180. int auc_speed;
  181. int auc_xdelay;
  182. int auc_sensor;
  183. struct i2c_ctx *i2c_slaves[AVALC3_DEFAULT_MODULARS];
  184. uint8_t connecter; /* AUC or IIC */
  185. /* For modulars */
  186. bool enable[AVALC3_DEFAULT_MODULARS];
  187. bool reboot[AVALC3_DEFAULT_MODULARS];
  188. struct timeval elapsed[AVALC3_DEFAULT_MODULARS];
  189. uint8_t mm_dna[AVALC3_DEFAULT_MODULARS][AVALC3_MM_DNA_LEN];
  190. char mm_version[AVALC3_DEFAULT_MODULARS][AVALC3_MM_VER_LEN + 1]; /* It's a string */
  191. uint32_t total_asics[AVALC3_DEFAULT_MODULARS];
  192. uint32_t max_ntime; /* Maximum: 7200 */
  193. uint8_t otp_info[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT][AVALC3_OTP_LEN + 1];
  194. int mod_type[AVALC3_DEFAULT_MODULARS];
  195. uint8_t miner_count[AVALC3_DEFAULT_MODULARS];
  196. uint8_t asic_count[AVALC3_DEFAULT_MODULARS];
  197. uint32_t freq_mode[AVALC3_DEFAULT_MODULARS];
  198. int led_indicator[AVALC3_DEFAULT_MODULARS];
  199. int fan_pct[AVALC3_DEFAULT_MODULARS];
  200. int fan_cpm[AVALC3_DEFAULT_MODULARS];
  201. int temp[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT][AVALC3_DEFAULT_ASIC_MAX];
  202. int temp_mm[AVALC3_DEFAULT_MODULARS];
  203. uint32_t core_volt[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT] \
  204. [AVALC3_DEFAULT_ASIC_MAX][AVALC3_DEFAULT_CORE_VOLT_CNT];
  205. uint8_t cutoff[AVALC3_DEFAULT_MODULARS];
  206. int temp_target[AVALC3_DEFAULT_MODULARS];
  207. int temp_overheat[AVALC3_DEFAULT_MODULARS];
  208. /* pid controler*/
  209. int pid_p[AVALC3_DEFAULT_MODULARS];
  210. int pid_i[AVALC3_DEFAULT_MODULARS];
  211. int pid_d[AVALC3_DEFAULT_MODULARS];
  212. double pid_u[AVALC3_DEFAULT_MODULARS];
  213. int pid_e[AVALC3_DEFAULT_MODULARS][3];
  214. int pid_0[AVALC3_DEFAULT_MODULARS];
  215. int set_voltage_level[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT];
  216. uint32_t set_frequency[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT][AVALC3_DEFAULT_PLL_CNT];
  217. uint32_t get_frequency[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT][AVALC3_DEFAULT_ASIC_MAX][AVALC3_DEFAULT_PLL_CNT];
  218. int set_asic_otp[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT];
  219. uint16_t get_voltage[AVALC3_DEFAULT_MODULARS][1]; /* Output is the same */
  220. uint32_t get_pll[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT][AVALC3_DEFAULT_PLL_CNT];
  221. uint32_t get_asic[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT][AVALC3_DEFAULT_ASIC_MAX][6];
  222. int8_t factory_info[AVALC3_DEFAULT_FACTORY_INFO_CNT];
  223. int8_t overclocking_info[AVALC3_DEFAULT_OVERCLOCKING_CNT];
  224. uint64_t local_works[AVALC3_DEFAULT_MODULARS];
  225. uint64_t local_works_i[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT];
  226. uint64_t hw_works[AVALC3_DEFAULT_MODULARS];
  227. uint64_t hw_works_i[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT];
  228. uint64_t chip_matching_work[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT][AVALC3_DEFAULT_ASIC_MAX];
  229. uint32_t error_code[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT + 1];
  230. uint32_t error_crc[AVALC3_DEFAULT_MODULARS][AVALC3_DEFAULT_MINER_CNT];
  231. uint8_t error_polling_cnt[AVALC3_DEFAULT_MODULARS];
  232. uint64_t diff1[AVALC3_DEFAULT_MODULARS];
  233. uint16_t vin_adc_ratio[AVALC3_DEFAULT_MODULARS];
  234. uint16_t vout_adc_ratio[AVALC3_DEFAULT_MODULARS];
  235. uint16_t power_info[AVALC3_DEFAULT_POWER_INFO_CNT];
  236. bool conn_overloaded;
  237. };
  238. struct avalonlc3_iic_info {
  239. uint8_t iic_op;
  240. union {
  241. uint32_t aucParam[2];
  242. uint8_t slave_addr;
  243. } iic_param;
  244. };
  245. struct avalonlc3_dev_description {
  246. uint8_t dev_id_str[8];
  247. int mod_type;
  248. uint8_t miner_count; /* it should not greater than AVALC3_DEFAULT_MINER_CNT */
  249. uint8_t asic_count; /* asic count each miner, it should not great than AVALC3_DEFAULT_ASIC_MAX */
  250. int set_voltage_level;
  251. uint16_t set_freq[AVALC3_DEFAULT_PLL_CNT];
  252. int set_asic_otp;
  253. };
  254. #define AVALC3_WRITE_SIZE (sizeof(struct avalonlc3_pkg))
  255. #define AVALC3_READ_SIZE AVALC3_WRITE_SIZE
  256. #define AVALC3_SEND_OK 0
  257. #define AVALC3_SEND_ERROR -1
  258. extern char *set_avalonlc3_fan(char *arg);
  259. extern char *set_avalonlc3_freq(char *arg);
  260. extern char *set_avalonlc3_voltage_level(char *arg);
  261. extern char *set_avalonlc3_voltage_level_offset(char *arg);
  262. extern char *set_avalonlc3_asic_otp(char *arg);
  263. extern int opt_avalonlc3_temp_target;
  264. extern int opt_avalonlc3_polling_delay;
  265. extern int opt_avalonlc3_aucspeed;
  266. extern int opt_avalonlc3_aucxdelay;
  267. extern int opt_avalonlc3_smart_speed;
  268. extern bool opt_avalonlc3_iic_detect;
  269. extern int opt_avalonlc3_freq_sel;
  270. extern uint32_t opt_avalonlc3_th_pass;
  271. extern uint32_t opt_avalonlc3_th_fail;
  272. extern uint32_t opt_avalonlc3_th_init;
  273. extern uint32_t opt_avalonlc3_th_ms;
  274. extern uint32_t opt_avalonlc3_th_timeout;
  275. extern uint32_t opt_avalonlc3_th_add;
  276. extern uint32_t opt_avalonlc3_nonce_mask;
  277. extern uint32_t opt_avalonlc3_nonce_check;
  278. extern uint32_t opt_avalonlc3_mux_l2h;
  279. extern uint32_t opt_avalonlc3_mux_h2l;
  280. extern uint32_t opt_avalonlc3_h2ltime0_spd;
  281. extern uint32_t opt_avalonlc3_roll_enable;
  282. extern uint32_t opt_avalonlc3_spdlow;
  283. extern uint32_t opt_avalonlc3_spdhigh;
  284. extern uint32_t opt_avalonlc3_tbase;
  285. extern uint32_t opt_avalonlc3_pid_p;
  286. extern uint32_t opt_avalonlc3_pid_i;
  287. extern uint32_t opt_avalonlc3_pid_d;
  288. #endif /* USE_AVALONLC3 */
  289. #endif /* _AVALONLC3_H_ */