driver-avalon9.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 _AVALON9_H_
  11. #define _AVALON9_H_
  12. #include "util.h"
  13. #include "i2c-context.h"
  14. #ifdef USE_AVALON9
  15. #define AVA9_DEFAULT_FAN_MIN 5 /* % */
  16. #define AVA9_DEFAULT_FAN_MAX 100
  17. #define AVA9_DEFAULT_TEMP_TARGET 93
  18. #define AVA9_DEFAULT_TEMP_OVERHEAT 105
  19. #define AVA9_DEFAULT_VOLTAGE_LEVEL_MIN -15
  20. #define AVA9_DEFAULT_VOLTAGE_LEVEL_MAX 15
  21. #define AVA9_INVALID_VOLTAGE_LEVEL -16
  22. #define AVA9_DEFAULT_VOLTAGE_LEVEL_OFFSET_MIN -2
  23. #define AVA9_DEFAULT_VOLTAGE_LEVEL_OFFSET 0
  24. #define AVA9_DEFAULT_VOLTAGE_LEVEL_OFFSET_MAX 1
  25. #define AVA9_DEFAULT_FACTORY_INFO_0_MIN -15
  26. #define AVA9_DEFAULT_FACTORY_INFO_0_MAX 15
  27. #define AVA9_DEFAULT_FACTORY_INFO_0_CNT 1
  28. #define AVA9_DEFAULT_FACTORY_INFO_0_IGNORE 16
  29. #define AVA9_DEFAULT_FACTORY_INFO_1_CNT 3
  30. #define AVA9_DEFAULT_FACTORY_INFO_2_CNT 4
  31. #define AVA9_DEFAULT_AJUST_VOLT_UP_INIT 5380
  32. #define AVA9_DEFAULT_AJUST_VOLT_UP_FACTOR 6
  33. #define AVA9_DEFAULT_AJUST_VOLT_UP_THRESHOLD 5180
  34. #define AVA9_DEFAULT_AJUST_VOLT_DOWN_INIT 5100
  35. #define AVA9_DEFAULT_AJUST_VOLT_DOWN_FACTOR 4
  36. #define AVA9_DEFAULT_AJUST_VOLT_DOWN_THRESHOLD 5210
  37. #define AVA9_DEFAULT_AJUST_VOLT_TIME 600
  38. #define AVA9_DEFAULT_AJUST_VOLT_ENABLE 1
  39. #define AVA9_DEFAULT_OVERCLOCKING_OFF 0
  40. #define AVA9_DEFAULT_OVERCLOCKING_ON 1
  41. #define AVA9_DEFAULT_FREQUENCY_0M 0
  42. #define AVA9_DEFAULT_FREQUENCY_650M 650
  43. #define AVA9_DEFAULT_FREQUENCY_700M 700
  44. #define AVA9_DEFAULT_FREQUENCY_725M 725
  45. #define AVA9_DEFAULT_FREQUENCY_750M 750
  46. #define AVA9_DEFAULT_FREQUENCY_775M 775
  47. #define AVA9_DEFAULT_FREQUENCY_787M 787
  48. #define AVA9_DEFAULT_FREQUENCY_800M 800
  49. #define AVA9_DEFAULT_FREQUENCY_850M 850
  50. #define AVA9_DEFAULT_FREQUENCY_MAX 1200
  51. #define AVA9_DEFAULT_FREQUENCY_IGNORE 1250
  52. #define AVA9_DEFAULT_FREQUENCY_SEL 7
  53. #define AVA9_DEFAULT_MODULARS 7 /* Only support 6 modules maximum with one AUC */
  54. #define AVA9_DEFAULT_MINER_CNT 4
  55. #define AVA9_DEFAULT_ASIC_MAX 26
  56. #define AVA9_DEFAULT_PLL_CNT 7
  57. #define AVA9_DEFAULT_PMU_CNT 2
  58. #define AVA9_DEFAULT_CORE_VOLT_CNT 8
  59. #define AVA9_DEFAULT_RO_CHANNEL_CNT 12
  60. #define AVA9_DEFAULT_POLLING_DELAY 20 /* ms */
  61. #define AVA9_DEFAULT_NTIME_OFFSET 2
  62. #define AVA9_DEFAULT_SMARTSPEED_OFF 0
  63. #define AVA9_DEFAULT_SMARTSPEED_MODE1 1
  64. #define AVA9_DEFAULT_SMART_SPEED (AVA9_DEFAULT_SMARTSPEED_MODE1)
  65. #define AVA9_DEFAULT_TH_PASS 8
  66. #define AVA9_DEFAULT_TH_FAIL 1000
  67. #define AVA9_DEFAULT_TH_INIT 32767
  68. #define AVA9_DEFAULT_TH_ADD 0
  69. #define AVA9_DEFAULT_TH_MS 2
  70. #define AVA9_DEFAULT_TH_MSSEL 0
  71. #define AVA9_DEFAULT_TH_TIMEOUT 1550000
  72. #define AVA9_DEFAULT_NONCE_MASK 24
  73. #define AVA9_DEFAULT_NONCE_CHECK 1
  74. #define AVA9_DEFAULT_MUX_L2H 0
  75. #define AVA9_DEFAULT_MUX_H2L 1
  76. #define AVA9_DEFAULT_H2LTIME0_SPD 3
  77. #define AVA9_DEFAULT_ROLL_ENABLE 1
  78. #define AVA9_DEFAULT_SPDLOW 6
  79. #define AVA9_DEFAULT_SPDHIGH 7
  80. #define AVA9_DEFAULT_TBASE 0
  81. #define AVA9_INVALID_SPDLOW 0
  82. /* PID CONTROLLER*/
  83. #define AVA9_DEFAULT_PID_P 1
  84. #define AVA9_DEFAULT_PID_I 5
  85. #define AVA9_DEFAULT_PID_D 0
  86. #define AVA9_DEFAULT_PID_TEMP_MIN_DIFF 5
  87. #define AVA9_DEFAULT_PID_TEMP_MAX 105
  88. #define AVA9_DEFAULT_LV2_TH_MS 0
  89. #define AVA9_DEFAULT_LV3_TH_MS 0
  90. #define AVA9_DEFAULT_LV4_TH_MS 0
  91. #define AVA9_DEFAULT_LV5_TH_MS 0
  92. #define AVA9_DEFAULT_LV6_TH_MS 0
  93. #define AVA9_DEFAULT_LV7_TH_MS 0
  94. #define AVA9_DEFAULT_LV2_TH_ADD 0
  95. #define AVA9_DEFAULT_LV3_TH_ADD 0
  96. #define AVA9_DEFAULT_LV4_TH_ADD 0
  97. #define AVA9_DEFAULT_LV5_TH_ADD 0
  98. #define AVA9_DEFAULT_LV6_TH_ADD 0
  99. #define AVA9_DEFAULT_LV7_TH_ADD 0
  100. #define AVA9_DEFAULT_IIC_DETECT false
  101. #define AVA9_PWM_MAX 0x3FF
  102. #define AVA9_DRV_DIFFMAX 2911
  103. #define AVA9_ASIC_TIMEOUT_CONST 419430400 /* (2^32 * 1000) / (256 * 40) */
  104. #define AVA9_MODULE_DETECT_INTERVAL 30 /* 30 s */
  105. #define AVA9_AUC_VER_LEN 12 /* Version length: 12 (AUC-YYYYMMDD) */
  106. #define AVA9_AUC_SPEED 400000
  107. #define AVA9_AUC_XDELAY 24000 /* 4800 = 1ms in AUC (11U14) */
  108. #define AVA9_AUC_P_SIZE 64
  109. #define AVA9_CONNECTER_AUC 1
  110. #define AVA9_CONNECTER_IIC 2
  111. /* avalon9 protocol package type from MM protocol.h */
  112. #define AVA9_MM_VER_LEN 15
  113. #define AVA9_MM_DNA_LEN 8
  114. #define AVA9_H1 'C'
  115. #define AVA9_H2 'N'
  116. #define AVA9_P_COINBASE_SIZE (6 * 1024 + 64)
  117. #define AVA9_P_MERKLES_COUNT 30
  118. #define AVA9_P_COUNT 40
  119. #define AVA9_P_DATA_LEN 32
  120. /* Broadcase with block iic_write*/
  121. #define AVA9_P_DETECT 0x10
  122. /* Broadcase With non-block iic_write*/
  123. #define AVA9_P_STATIC 0x11
  124. #define AVA9_P_JOB_ID 0x12
  125. #define AVA9_P_COINBASE 0x13
  126. #define AVA9_P_MERKLES 0x14
  127. #define AVA9_P_HEADER 0x15
  128. #define AVA9_P_TARGET 0x16
  129. #define AVA9_P_JOB_FIN 0x17
  130. /* Broadcase or with I2C address */
  131. #define AVA9_P_SET 0x20
  132. #define AVA9_P_SET_FIN 0x21
  133. #define AVA9_P_SET_VOLT 0x22
  134. #define AVA9_P_SET_PMU 0x24
  135. #define AVA9_P_SET_PLL 0x25
  136. #define AVA9_P_SET_SS 0x26
  137. /* 0x27 reserved */
  138. #define AVA9_P_SET_FAC 0x28
  139. #define AVA9_P_SET_OC 0x29
  140. #define AVA9_P_SET_SS_PARA_EN 0x2b
  141. /* Have to send with I2C address */
  142. #define AVA9_P_POLLING 0x30
  143. #define AVA9_P_SYNC 0x31
  144. #define AVA9_P_TEST 0x32
  145. #define AVA9_P_RSTMMTX 0x33
  146. #define AVA9_P_GET_VOLT 0x34
  147. /* Back to host */
  148. #define AVA9_P_ACKDETECT 0x40
  149. #define AVA9_P_STATUS 0x41
  150. #define AVA9_P_NONCE 0x42
  151. #define AVA9_P_TEST_RET 0x43
  152. #define AVA9_P_STATUS_VOLT 0x46
  153. #define AVA9_P_STATUS_PMU 0x48
  154. #define AVA9_P_STATUS_PLL 0x49
  155. #define AVA9_P_STATUS_LOG 0x4a
  156. #define AVA9_P_STATUS_ASIC 0x4b
  157. #define AVA9_P_STATUS_PVT 0x4c
  158. #define AVA9_P_STATUS_FAC 0x4d
  159. #define AVA9_P_STATUS_OC 0x4e
  160. #define AVA9_P_STATUS_PVT_RO 0x4f
  161. #define AVA9_P_SET_ADJUST_VOLT 0x51
  162. #define AVA9_P_STATUS_ASIC_PLL 0x52
  163. #define AVA9_P_STATUS_SS_PARA 0x53
  164. #define AVA9_MODULE_BROADCAST 0
  165. /* End of avalon9 protocol package type */
  166. #define AVA9_IIC_RESET 0xa0
  167. #define AVA9_IIC_INIT 0xa1
  168. #define AVA9_IIC_DEINIT 0xa2
  169. #define AVA9_IIC_XFER 0xa5
  170. #define AVA9_IIC_INFO 0xa6
  171. #define AVA9_FREQ_INIT_MODE 0x0
  172. #define AVA9_FREQ_PLLADJ_MODE 0x1
  173. #define AVA9_DEFAULT_FACTORY_INFO_CNT (AVA9_DEFAULT_FACTORY_INFO_0_CNT + AVA9_DEFAULT_FACTORY_INFO_1_CNT + AVA9_DEFAULT_FACTORY_INFO_2_CNT)
  174. #define AVA9_DEFAULT_OVERCLOCKING_CNT 1
  175. #define AVA9_MM921_VIN_ADC_RATIO (3.3 / 4095.0 * 25.62 / 5.62 * 1000.0 * 100.0)
  176. #define AVA9_MM921_VOUT_ADC_RATIO (3.3 / 4095.0 * 72.3 / 20.0 * 10000.0 * 100.0)
  177. #define AVA9_DEFAULT_MM921_SPDLOW 6
  178. #define AVA9_MM920_VIN_ADC_RATIO (3.3 / 4095.0 * 25.62 / 5.62 * 1000.0 * 100.0)
  179. #define AVA9_MM920_VOUT_ADC_RATIO (3.3 / 4095.0 * 72.3 / 20.0 * 10000.0 * 100.0)
  180. #define AVA9_DEFAULT_MM920_SPDLOW 6
  181. #define AVA9_DEFAULT_WU 279400
  182. #define AVA9_DEFAULT_WU_MIN 279000
  183. #define AVA9_DEFAULT_WU_MAX 288000
  184. #define AVA9_DEFAULT_DH_MIN 3.7
  185. #define AVA9_DEFAULT_DH_MAX 5.0
  186. #define AVA9_DEFAULT_GHSMM_MIN 21000.0
  187. #define AVA9_ADJUST_VOLT_CNT 3
  188. #define AVA9_ADJUST_VOLT_STEP 1
  189. #define AVA9_ADJUST_FREQ_STEP 25
  190. #define AVA9_ADJUST_FREQ_MAX 825
  191. struct avalon9_pkg {
  192. uint8_t head[2];
  193. uint8_t type;
  194. uint8_t opt;
  195. uint8_t idx;
  196. uint8_t cnt;
  197. uint8_t data[32];
  198. uint8_t crc[2];
  199. };
  200. #define avalon9_ret avalon9_pkg
  201. struct avalon9_info {
  202. /* Public data */
  203. int64_t last_diff1;
  204. int64_t pending_diff1;
  205. double last_rej;
  206. int mm_count;
  207. int xfer_err_cnt;
  208. int pool_no;
  209. struct timeval firsthash;
  210. struct timeval last_fan_adj;
  211. struct timeval last_stratum;
  212. struct timeval last_detect;
  213. cglock_t update_lock;
  214. struct pool pool0;
  215. struct pool pool1;
  216. struct pool pool2;
  217. bool work_restart;
  218. uint32_t last_jobid;
  219. /* For connecter */
  220. char auc_version[AVA9_AUC_VER_LEN + 1];
  221. int auc_speed;
  222. int auc_xdelay;
  223. int auc_sensor;
  224. struct i2c_ctx *i2c_slaves[AVA9_DEFAULT_MODULARS];
  225. uint8_t connecter; /* AUC or IIC */
  226. /* For modulars */
  227. bool enable[AVA9_DEFAULT_MODULARS];
  228. bool reboot[AVA9_DEFAULT_MODULARS];
  229. struct timeval elapsed[AVA9_DEFAULT_MODULARS];
  230. uint8_t mm_dna[AVA9_DEFAULT_MODULARS][AVA9_MM_DNA_LEN];
  231. char mm_version[AVA9_DEFAULT_MODULARS][AVA9_MM_VER_LEN + 1]; /* It's a string */
  232. uint32_t total_asics[AVA9_DEFAULT_MODULARS];
  233. uint32_t max_ntime; /* Maximum: 7200 */
  234. int mod_type[AVA9_DEFAULT_MODULARS];
  235. uint8_t miner_count[AVA9_DEFAULT_MODULARS];
  236. uint8_t asic_count[AVA9_DEFAULT_MODULARS];
  237. uint32_t freq_mode[AVA9_DEFAULT_MODULARS];
  238. int led_indicator[AVA9_DEFAULT_MODULARS];
  239. int fan_pct[AVA9_DEFAULT_MODULARS];
  240. int fan_cpm[AVA9_DEFAULT_MODULARS];
  241. int temp[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT][AVA9_DEFAULT_ASIC_MAX];
  242. int temp_mm[AVA9_DEFAULT_MODULARS];
  243. uint32_t core_volt[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT][AVA9_DEFAULT_ASIC_MAX];
  244. uint32_t pvt_ro[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT][AVA9_DEFAULT_ASIC_MAX][AVA9_DEFAULT_RO_CHANNEL_CNT];
  245. uint8_t cutoff[AVA9_DEFAULT_MODULARS];
  246. int temp_target[AVA9_DEFAULT_MODULARS];
  247. int temp_overheat[AVA9_DEFAULT_MODULARS];
  248. /* pid controler*/
  249. int pid_p[AVA9_DEFAULT_MODULARS];
  250. int pid_i[AVA9_DEFAULT_MODULARS];
  251. int pid_d[AVA9_DEFAULT_MODULARS];
  252. double pid_u[AVA9_DEFAULT_MODULARS];
  253. int pid_e[AVA9_DEFAULT_MODULARS][3];
  254. int pid_0[AVA9_DEFAULT_MODULARS];
  255. int set_voltage_level[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT];
  256. uint32_t set_frequency[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT][AVA9_DEFAULT_PLL_CNT];
  257. uint32_t get_frequency[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT][AVA9_DEFAULT_ASIC_MAX][AVA9_DEFAULT_PLL_CNT];
  258. uint16_t get_vin[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT];
  259. uint32_t get_voltage[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT];
  260. uint32_t get_pll[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT][AVA9_DEFAULT_PLL_CNT];
  261. uint32_t get_asic[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT][AVA9_DEFAULT_ASIC_MAX][2 + AVA9_DEFAULT_PLL_CNT];
  262. int8_t factory_info[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_FACTORY_INFO_CNT];
  263. int8_t overclocking_info[AVA9_DEFAULT_OVERCLOCKING_CNT];
  264. uint64_t local_works[AVA9_DEFAULT_MODULARS];
  265. uint64_t local_works_i[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT];
  266. uint64_t hw_works[AVA9_DEFAULT_MODULARS];
  267. uint64_t hw_works_i[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT];
  268. uint64_t chip_matching_work[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT][AVA9_DEFAULT_ASIC_MAX];
  269. uint32_t error_code[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT + 1];
  270. uint32_t error_crc[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_MINER_CNT];
  271. uint8_t error_polling_cnt[AVA9_DEFAULT_MODULARS];
  272. uint8_t power_good[AVA9_DEFAULT_MODULARS];
  273. char pmu_version[AVA9_DEFAULT_MODULARS][AVA9_DEFAULT_PMU_CNT][5];
  274. uint64_t diff1[AVA9_DEFAULT_MODULARS];
  275. uint16_t vin_adc_ratio[AVA9_DEFAULT_MODULARS];
  276. uint16_t vout_adc_ratio[AVA9_DEFAULT_MODULARS];
  277. uint8_t ss_para_en[AVA9_DEFAULT_MODULARS];
  278. uint8_t ss_para_target_temp[AVA9_DEFAULT_MODULARS];
  279. bool conn_overloaded;
  280. };
  281. struct avalon9_iic_info {
  282. uint8_t iic_op;
  283. union {
  284. uint32_t aucParam[2];
  285. uint8_t slave_addr;
  286. } iic_param;
  287. };
  288. struct avalon9_dev_description {
  289. uint8_t dev_id_str[8];
  290. int mod_type;
  291. uint8_t miner_count; /* it should not greater than AVA9_DEFAULT_MINER_CNT */
  292. uint8_t asic_count; /* asic count each miner, it should not great than AVA9_DEFAULT_ASIC_MAX */
  293. uint16_t vin_adc_ratio;
  294. uint16_t vout_adc_ratio;
  295. int set_voltage_level;
  296. uint16_t set_freq[AVA9_DEFAULT_PLL_CNT];
  297. };
  298. #define AVA9_WRITE_SIZE (sizeof(struct avalon9_pkg))
  299. #define AVA9_READ_SIZE AVA9_WRITE_SIZE
  300. #define AVA9_SEND_OK 0
  301. #define AVA9_SEND_ERROR -1
  302. extern char *set_avalon9_fan(char *arg);
  303. extern char *set_avalon9_freq(char *arg);
  304. extern char *set_avalon9_voltage_level(char *arg);
  305. extern char *set_avalon9_voltage_level_offset(char *arg);
  306. extern char *set_avalon9_adjust_volt_info(char *arg);
  307. extern int opt_avalon9_temp_target;
  308. extern int opt_avalon9_polling_delay;
  309. extern int opt_avalon9_aucspeed;
  310. extern int opt_avalon9_aucxdelay;
  311. extern int opt_avalon9_smart_speed;
  312. extern bool opt_avalon9_iic_detect;
  313. extern int opt_avalon9_freq_sel;
  314. extern uint32_t opt_avalon9_th_pass;
  315. extern uint32_t opt_avalon9_th_fail;
  316. extern uint32_t opt_avalon9_th_init;
  317. extern uint32_t opt_avalon9_th_ms;
  318. extern uint32_t opt_avalon9_th_timeout;
  319. extern uint32_t opt_avalon9_th_add;
  320. extern uint32_t opt_avalon9_th_mssel;
  321. extern uint32_t opt_avalon9_nonce_mask;
  322. extern uint32_t opt_avalon9_nonce_check;
  323. extern uint32_t opt_avalon9_mux_l2h;
  324. extern uint32_t opt_avalon9_mux_h2l;
  325. extern uint32_t opt_avalon9_h2ltime0_spd;
  326. extern uint32_t opt_avalon9_roll_enable;
  327. extern uint32_t opt_avalon9_spdlow;
  328. extern uint32_t opt_avalon9_spdhigh;
  329. extern uint32_t opt_avalon9_tbase;
  330. extern uint32_t opt_avalon9_lv2_th_ms;
  331. extern uint32_t opt_avalon9_lv3_th_ms;
  332. extern uint32_t opt_avalon9_lv4_th_ms;
  333. extern uint32_t opt_avalon9_lv5_th_ms;
  334. extern uint32_t opt_avalon9_lv6_th_ms;
  335. extern uint32_t opt_avalon9_lv7_th_ms;
  336. extern uint32_t opt_avalon9_lv2_th_add;
  337. extern uint32_t opt_avalon9_lv3_th_add;
  338. extern uint32_t opt_avalon9_lv4_th_add;
  339. extern uint32_t opt_avalon9_lv5_th_add;
  340. extern uint32_t opt_avalon9_lv6_th_add;
  341. extern uint32_t opt_avalon9_lv7_th_add;
  342. extern uint32_t opt_avalon9_pid_p;
  343. extern uint32_t opt_avalon9_pid_i;
  344. extern uint32_t opt_avalon9_pid_d;
  345. #endif /* USE_AVALON9 */
  346. #endif /* _AVALON9_H_ */