ar9003_aic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (c) 2015 Qualcomm Atheros Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "hw.h"
  17. #include "hw-ops.h"
  18. #include "ar9003_mci.h"
  19. #include "ar9003_aic.h"
  20. #include "ar9003_phy.h"
  21. #include "reg_aic.h"
  22. static const u8 com_att_db_table[ATH_AIC_MAX_COM_ATT_DB_TABLE] = {
  23. 0, 3, 9, 15, 21, 27
  24. };
  25. static const u16 aic_lin_table[ATH_AIC_MAX_AIC_LIN_TABLE] = {
  26. 8191, 7300, 6506, 5799, 5168, 4606, 4105, 3659,
  27. 3261, 2906, 2590, 2309, 2057, 1834, 1634, 1457,
  28. 1298, 1157, 1031, 919, 819, 730, 651, 580,
  29. 517, 461, 411, 366, 326, 291, 259, 231,
  30. 206, 183, 163, 146, 130, 116, 103, 92,
  31. 82, 73, 65, 58, 52, 46, 41, 37,
  32. 33, 29, 26, 23, 21, 18, 16, 15,
  33. 13, 12, 10, 9, 8, 7, 7, 6,
  34. 5, 5, 4, 4, 3
  35. };
  36. static bool ar9003_hw_is_aic_enabled(struct ath_hw *ah)
  37. {
  38. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  39. /*
  40. * Disable AIC for now, until we have all the
  41. * HW code and the driver-layer support ready.
  42. */
  43. return false;
  44. if (mci_hw->config & ATH_MCI_CONFIG_DISABLE_AIC)
  45. return false;
  46. return true;
  47. }
  48. static int16_t ar9003_aic_find_valid(struct ath_aic_sram_info *cal_sram,
  49. bool dir, u8 index)
  50. {
  51. int16_t i;
  52. if (dir) {
  53. for (i = index + 1; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  54. if (cal_sram[i].valid)
  55. break;
  56. }
  57. } else {
  58. for (i = index - 1; i >= 0; i--) {
  59. if (cal_sram[i].valid)
  60. break;
  61. }
  62. }
  63. if ((i >= ATH_AIC_MAX_BT_CHANNEL) || (i < 0))
  64. i = -1;
  65. return i;
  66. }
  67. /*
  68. * type 0: aic_lin_table, 1: com_att_db_table
  69. */
  70. static int16_t ar9003_aic_find_index(u8 type, int16_t value)
  71. {
  72. int16_t i = -1;
  73. if (type == 0) {
  74. for (i = ATH_AIC_MAX_AIC_LIN_TABLE - 1; i >= 0; i--) {
  75. if (aic_lin_table[i] >= value)
  76. break;
  77. }
  78. } else if (type == 1) {
  79. for (i = 0; i < ATH_AIC_MAX_COM_ATT_DB_TABLE; i++) {
  80. if (com_att_db_table[i] > value) {
  81. i--;
  82. break;
  83. }
  84. }
  85. if (i >= ATH_AIC_MAX_COM_ATT_DB_TABLE)
  86. i = -1;
  87. }
  88. return i;
  89. }
  90. static void ar9003_aic_gain_table(struct ath_hw *ah)
  91. {
  92. u32 aic_atten_word[19], i;
  93. /* Config LNA gain difference */
  94. REG_WRITE(ah, AR_PHY_BT_COEX_4, 0x2c200a00);
  95. REG_WRITE(ah, AR_PHY_BT_COEX_5, 0x5c4e4438);
  96. /* Program gain table */
  97. aic_atten_word[0] = (0x1 & 0xf) << 14 | (0x1f & 0x1f) << 9 | (0x0 & 0xf) << 5 |
  98. (0x1f & 0x1f); /* -01 dB: 4'd1, 5'd31, 00 dB: 4'd0, 5'd31 */
  99. aic_atten_word[1] = (0x3 & 0xf) << 14 | (0x1f & 0x1f) << 9 | (0x2 & 0xf) << 5 |
  100. (0x1f & 0x1f); /* -03 dB: 4'd3, 5'd31, -02 dB: 4'd2, 5'd31 */
  101. aic_atten_word[2] = (0x5 & 0xf) << 14 | (0x1f & 0x1f) << 9 | (0x4 & 0xf) << 5 |
  102. (0x1f & 0x1f); /* -05 dB: 4'd5, 5'd31, -04 dB: 4'd4, 5'd31 */
  103. aic_atten_word[3] = (0x1 & 0xf) << 14 | (0x1e & 0x1f) << 9 | (0x0 & 0xf) << 5 |
  104. (0x1e & 0x1f); /* -07 dB: 4'd1, 5'd30, -06 dB: 4'd0, 5'd30 */
  105. aic_atten_word[4] = (0x3 & 0xf) << 14 | (0x1e & 0x1f) << 9 | (0x2 & 0xf) << 5 |
  106. (0x1e & 0x1f); /* -09 dB: 4'd3, 5'd30, -08 dB: 4'd2, 5'd30 */
  107. aic_atten_word[5] = (0x5 & 0xf) << 14 | (0x1e & 0x1f) << 9 | (0x4 & 0xf) << 5 |
  108. (0x1e & 0x1f); /* -11 dB: 4'd5, 5'd30, -10 dB: 4'd4, 5'd30 */
  109. aic_atten_word[6] = (0x1 & 0xf) << 14 | (0xf & 0x1f) << 9 | (0x0 & 0xf) << 5 |
  110. (0xf & 0x1f); /* -13 dB: 4'd1, 5'd15, -12 dB: 4'd0, 5'd15 */
  111. aic_atten_word[7] = (0x3 & 0xf) << 14 | (0xf & 0x1f) << 9 | (0x2 & 0xf) << 5 |
  112. (0xf & 0x1f); /* -15 dB: 4'd3, 5'd15, -14 dB: 4'd2, 5'd15 */
  113. aic_atten_word[8] = (0x5 & 0xf) << 14 | (0xf & 0x1f) << 9 | (0x4 & 0xf) << 5 |
  114. (0xf & 0x1f); /* -17 dB: 4'd5, 5'd15, -16 dB: 4'd4, 5'd15 */
  115. aic_atten_word[9] = (0x1 & 0xf) << 14 | (0x7 & 0x1f) << 9 | (0x0 & 0xf) << 5 |
  116. (0x7 & 0x1f); /* -19 dB: 4'd1, 5'd07, -18 dB: 4'd0, 5'd07 */
  117. aic_atten_word[10] = (0x3 & 0xf) << 14 | (0x7 & 0x1f) << 9 | (0x2 & 0xf) << 5 |
  118. (0x7 & 0x1f); /* -21 dB: 4'd3, 5'd07, -20 dB: 4'd2, 5'd07 */
  119. aic_atten_word[11] = (0x5 & 0xf) << 14 | (0x7 & 0x1f) << 9 | (0x4 & 0xf) << 5 |
  120. (0x7 & 0x1f); /* -23 dB: 4'd5, 5'd07, -22 dB: 4'd4, 5'd07 */
  121. aic_atten_word[12] = (0x7 & 0xf) << 14 | (0x7 & 0x1f) << 9 | (0x6 & 0xf) << 5 |
  122. (0x7 & 0x1f); /* -25 dB: 4'd7, 5'd07, -24 dB: 4'd6, 5'd07 */
  123. aic_atten_word[13] = (0x3 & 0xf) << 14 | (0x3 & 0x1f) << 9 | (0x2 & 0xf) << 5 |
  124. (0x3 & 0x1f); /* -27 dB: 4'd3, 5'd03, -26 dB: 4'd2, 5'd03 */
  125. aic_atten_word[14] = (0x5 & 0xf) << 14 | (0x3 & 0x1f) << 9 | (0x4 & 0xf) << 5 |
  126. (0x3 & 0x1f); /* -29 dB: 4'd5, 5'd03, -28 dB: 4'd4, 5'd03 */
  127. aic_atten_word[15] = (0x1 & 0xf) << 14 | (0x1 & 0x1f) << 9 | (0x0 & 0xf) << 5 |
  128. (0x1 & 0x1f); /* -31 dB: 4'd1, 5'd01, -30 dB: 4'd0, 5'd01 */
  129. aic_atten_word[16] = (0x3 & 0xf) << 14 | (0x1 & 0x1f) << 9 | (0x2 & 0xf) << 5 |
  130. (0x1 & 0x1f); /* -33 dB: 4'd3, 5'd01, -32 dB: 4'd2, 5'd01 */
  131. aic_atten_word[17] = (0x5 & 0xf) << 14 | (0x1 & 0x1f) << 9 | (0x4 & 0xf) << 5 |
  132. (0x1 & 0x1f); /* -35 dB: 4'd5, 5'd01, -34 dB: 4'd4, 5'd01 */
  133. aic_atten_word[18] = (0x7 & 0xf) << 14 | (0x1 & 0x1f) << 9 | (0x6 & 0xf) << 5 |
  134. (0x1 & 0x1f); /* -37 dB: 4'd7, 5'd01, -36 dB: 4'd6, 5'd01 */
  135. /* Write to Gain table with auto increment enabled. */
  136. REG_WRITE(ah, (AR_PHY_AIC_SRAM_ADDR_B0 + 0x3000),
  137. (ATH_AIC_SRAM_AUTO_INCREMENT |
  138. ATH_AIC_SRAM_GAIN_TABLE_OFFSET));
  139. for (i = 0; i < 19; i++) {
  140. REG_WRITE(ah, (AR_PHY_AIC_SRAM_DATA_B0 + 0x3000),
  141. aic_atten_word[i]);
  142. }
  143. }
  144. static u8 ar9003_aic_cal_start(struct ath_hw *ah, u8 min_valid_count)
  145. {
  146. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  147. int i;
  148. /* Write to Gain table with auto increment enabled. */
  149. REG_WRITE(ah, (AR_PHY_AIC_SRAM_ADDR_B0 + 0x3000),
  150. (ATH_AIC_SRAM_AUTO_INCREMENT |
  151. ATH_AIC_SRAM_CAL_OFFSET));
  152. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  153. REG_WRITE(ah, (AR_PHY_AIC_SRAM_DATA_B0 + 0x3000), 0);
  154. aic->aic_sram[i] = 0;
  155. }
  156. REG_WRITE(ah, AR_PHY_AIC_CTRL_0_B0,
  157. (SM(0, AR_PHY_AIC_MON_ENABLE) |
  158. SM(127, AR_PHY_AIC_CAL_MAX_HOP_COUNT) |
  159. SM(min_valid_count, AR_PHY_AIC_CAL_MIN_VALID_COUNT) |
  160. SM(37, AR_PHY_AIC_F_WLAN) |
  161. SM(1, AR_PHY_AIC_CAL_CH_VALID_RESET) |
  162. SM(0, AR_PHY_AIC_CAL_ENABLE) |
  163. SM(0x40, AR_PHY_AIC_BTTX_PWR_THR) |
  164. SM(0, AR_PHY_AIC_ENABLE)));
  165. REG_WRITE(ah, AR_PHY_AIC_CTRL_0_B1,
  166. (SM(0, AR_PHY_AIC_MON_ENABLE) |
  167. SM(1, AR_PHY_AIC_CAL_CH_VALID_RESET) |
  168. SM(0, AR_PHY_AIC_CAL_ENABLE) |
  169. SM(0x40, AR_PHY_AIC_BTTX_PWR_THR) |
  170. SM(0, AR_PHY_AIC_ENABLE)));
  171. REG_WRITE(ah, AR_PHY_AIC_CTRL_1_B0,
  172. (SM(8, AR_PHY_AIC_CAL_BT_REF_DELAY) |
  173. SM(0, AR_PHY_AIC_BT_IDLE_CFG) |
  174. SM(1, AR_PHY_AIC_STDBY_COND) |
  175. SM(37, AR_PHY_AIC_STDBY_ROT_ATT_DB) |
  176. SM(5, AR_PHY_AIC_STDBY_COM_ATT_DB) |
  177. SM(15, AR_PHY_AIC_RSSI_MAX) |
  178. SM(0, AR_PHY_AIC_RSSI_MIN)));
  179. REG_WRITE(ah, AR_PHY_AIC_CTRL_1_B1,
  180. (SM(15, AR_PHY_AIC_RSSI_MAX) |
  181. SM(0, AR_PHY_AIC_RSSI_MIN)));
  182. REG_WRITE(ah, AR_PHY_AIC_CTRL_2_B0,
  183. (SM(44, AR_PHY_AIC_RADIO_DELAY) |
  184. SM(8, AR_PHY_AIC_CAL_STEP_SIZE_CORR) |
  185. SM(12, AR_PHY_AIC_CAL_ROT_IDX_CORR) |
  186. SM(2, AR_PHY_AIC_CAL_CONV_CHECK_FACTOR) |
  187. SM(5, AR_PHY_AIC_ROT_IDX_COUNT_MAX) |
  188. SM(0, AR_PHY_AIC_CAL_SYNTH_TOGGLE) |
  189. SM(0, AR_PHY_AIC_CAL_SYNTH_AFTER_BTRX) |
  190. SM(200, AR_PHY_AIC_CAL_SYNTH_SETTLING)));
  191. REG_WRITE(ah, AR_PHY_AIC_CTRL_3_B0,
  192. (SM(2, AR_PHY_AIC_MON_MAX_HOP_COUNT) |
  193. SM(1, AR_PHY_AIC_MON_MIN_STALE_COUNT) |
  194. SM(1, AR_PHY_AIC_MON_PWR_EST_LONG) |
  195. SM(2, AR_PHY_AIC_MON_PD_TALLY_SCALING) |
  196. SM(10, AR_PHY_AIC_MON_PERF_THR) |
  197. SM(2, AR_PHY_AIC_CAL_TARGET_MAG_SETTING) |
  198. SM(1, AR_PHY_AIC_CAL_PERF_CHECK_FACTOR) |
  199. SM(1, AR_PHY_AIC_CAL_PWR_EST_LONG)));
  200. REG_WRITE(ah, AR_PHY_AIC_CTRL_4_B0,
  201. (SM(2, AR_PHY_AIC_CAL_ROT_ATT_DB_EST_ISO) |
  202. SM(3, AR_PHY_AIC_CAL_COM_ATT_DB_EST_ISO) |
  203. SM(0, AR_PHY_AIC_CAL_ISO_EST_INIT_SETTING) |
  204. SM(2, AR_PHY_AIC_CAL_COM_ATT_DB_BACKOFF) |
  205. SM(1, AR_PHY_AIC_CAL_COM_ATT_DB_FIXED)));
  206. REG_WRITE(ah, AR_PHY_AIC_CTRL_4_B1,
  207. (SM(2, AR_PHY_AIC_CAL_ROT_ATT_DB_EST_ISO) |
  208. SM(3, AR_PHY_AIC_CAL_COM_ATT_DB_EST_ISO) |
  209. SM(0, AR_PHY_AIC_CAL_ISO_EST_INIT_SETTING) |
  210. SM(2, AR_PHY_AIC_CAL_COM_ATT_DB_BACKOFF) |
  211. SM(1, AR_PHY_AIC_CAL_COM_ATT_DB_FIXED)));
  212. ar9003_aic_gain_table(ah);
  213. /* Need to enable AIC reference signal in BT modem. */
  214. REG_WRITE(ah, ATH_AIC_BT_JUPITER_CTRL,
  215. (REG_READ(ah, ATH_AIC_BT_JUPITER_CTRL) |
  216. ATH_AIC_BT_AIC_ENABLE));
  217. aic->aic_cal_start_time = REG_READ(ah, AR_TSF_L32);
  218. /* Start calibration */
  219. REG_CLR_BIT(ah, AR_PHY_AIC_CTRL_0_B1, AR_PHY_AIC_CAL_ENABLE);
  220. REG_SET_BIT(ah, AR_PHY_AIC_CTRL_0_B1, AR_PHY_AIC_CAL_CH_VALID_RESET);
  221. REG_SET_BIT(ah, AR_PHY_AIC_CTRL_0_B1, AR_PHY_AIC_CAL_ENABLE);
  222. aic->aic_caled_chan = 0;
  223. aic->aic_cal_state = AIC_CAL_STATE_STARTED;
  224. return aic->aic_cal_state;
  225. }
  226. static bool ar9003_aic_cal_post_process(struct ath_hw *ah)
  227. {
  228. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  229. struct ath_aic_sram_info cal_sram[ATH_AIC_MAX_BT_CHANNEL];
  230. struct ath_aic_out_info aic_sram[ATH_AIC_MAX_BT_CHANNEL];
  231. u32 dir_path_gain_idx, quad_path_gain_idx, value;
  232. u32 fixed_com_att_db;
  233. int8_t dir_path_sign, quad_path_sign;
  234. int16_t i;
  235. bool ret = true;
  236. memset(&cal_sram, 0, sizeof(cal_sram));
  237. memset(&aic_sram, 0, sizeof(aic_sram));
  238. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  239. value = aic->aic_sram[i];
  240. cal_sram[i].valid =
  241. MS(value, AR_PHY_AIC_SRAM_VALID);
  242. cal_sram[i].rot_quad_att_db =
  243. MS(value, AR_PHY_AIC_SRAM_ROT_QUAD_ATT_DB);
  244. cal_sram[i].vga_quad_sign =
  245. MS(value, AR_PHY_AIC_SRAM_VGA_QUAD_SIGN);
  246. cal_sram[i].rot_dir_att_db =
  247. MS(value, AR_PHY_AIC_SRAM_ROT_DIR_ATT_DB);
  248. cal_sram[i].vga_dir_sign =
  249. MS(value, AR_PHY_AIC_SRAM_VGA_DIR_SIGN);
  250. cal_sram[i].com_att_6db =
  251. MS(value, AR_PHY_AIC_SRAM_COM_ATT_6DB);
  252. if (cal_sram[i].valid) {
  253. dir_path_gain_idx = cal_sram[i].rot_dir_att_db +
  254. com_att_db_table[cal_sram[i].com_att_6db];
  255. quad_path_gain_idx = cal_sram[i].rot_quad_att_db +
  256. com_att_db_table[cal_sram[i].com_att_6db];
  257. dir_path_sign = (cal_sram[i].vga_dir_sign) ? 1 : -1;
  258. quad_path_sign = (cal_sram[i].vga_quad_sign) ? 1 : -1;
  259. aic_sram[i].dir_path_gain_lin = dir_path_sign *
  260. aic_lin_table[dir_path_gain_idx];
  261. aic_sram[i].quad_path_gain_lin = quad_path_sign *
  262. aic_lin_table[quad_path_gain_idx];
  263. }
  264. }
  265. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  266. int16_t start_idx, end_idx;
  267. if (cal_sram[i].valid)
  268. continue;
  269. start_idx = ar9003_aic_find_valid(cal_sram, 0, i);
  270. end_idx = ar9003_aic_find_valid(cal_sram, 1, i);
  271. if (start_idx < 0) {
  272. /* extrapolation */
  273. start_idx = end_idx;
  274. end_idx = ar9003_aic_find_valid(cal_sram, 1, start_idx);
  275. if (end_idx < 0) {
  276. ret = false;
  277. break;
  278. }
  279. aic_sram[i].dir_path_gain_lin =
  280. ((aic_sram[start_idx].dir_path_gain_lin -
  281. aic_sram[end_idx].dir_path_gain_lin) *
  282. (start_idx - i) + ((end_idx - i) >> 1)) /
  283. (end_idx - i) +
  284. aic_sram[start_idx].dir_path_gain_lin;
  285. aic_sram[i].quad_path_gain_lin =
  286. ((aic_sram[start_idx].quad_path_gain_lin -
  287. aic_sram[end_idx].quad_path_gain_lin) *
  288. (start_idx - i) + ((end_idx - i) >> 1)) /
  289. (end_idx - i) +
  290. aic_sram[start_idx].quad_path_gain_lin;
  291. }
  292. if (end_idx < 0) {
  293. /* extrapolation */
  294. end_idx = ar9003_aic_find_valid(cal_sram, 0, start_idx);
  295. if (end_idx < 0) {
  296. ret = false;
  297. break;
  298. }
  299. aic_sram[i].dir_path_gain_lin =
  300. ((aic_sram[start_idx].dir_path_gain_lin -
  301. aic_sram[end_idx].dir_path_gain_lin) *
  302. (i - start_idx) + ((start_idx - end_idx) >> 1)) /
  303. (start_idx - end_idx) +
  304. aic_sram[start_idx].dir_path_gain_lin;
  305. aic_sram[i].quad_path_gain_lin =
  306. ((aic_sram[start_idx].quad_path_gain_lin -
  307. aic_sram[end_idx].quad_path_gain_lin) *
  308. (i - start_idx) + ((start_idx - end_idx) >> 1)) /
  309. (start_idx - end_idx) +
  310. aic_sram[start_idx].quad_path_gain_lin;
  311. } else if (start_idx >= 0){
  312. /* interpolation */
  313. aic_sram[i].dir_path_gain_lin =
  314. (((end_idx - i) * aic_sram[start_idx].dir_path_gain_lin) +
  315. ((i - start_idx) * aic_sram[end_idx].dir_path_gain_lin) +
  316. ((end_idx - start_idx) >> 1)) /
  317. (end_idx - start_idx);
  318. aic_sram[i].quad_path_gain_lin =
  319. (((end_idx - i) * aic_sram[start_idx].quad_path_gain_lin) +
  320. ((i - start_idx) * aic_sram[end_idx].quad_path_gain_lin) +
  321. ((end_idx - start_idx) >> 1))/
  322. (end_idx - start_idx);
  323. }
  324. }
  325. /* From dir/quad_path_gain_lin to sram. */
  326. i = ar9003_aic_find_valid(cal_sram, 1, 0);
  327. if (i < 0) {
  328. i = 0;
  329. ret = false;
  330. }
  331. fixed_com_att_db = com_att_db_table[cal_sram[i].com_att_6db];
  332. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  333. int16_t rot_dir_path_att_db, rot_quad_path_att_db;
  334. aic_sram[i].sram.vga_dir_sign =
  335. (aic_sram[i].dir_path_gain_lin >= 0) ? 1 : 0;
  336. aic_sram[i].sram.vga_quad_sign=
  337. (aic_sram[i].quad_path_gain_lin >= 0) ? 1 : 0;
  338. rot_dir_path_att_db =
  339. ar9003_aic_find_index(0, abs(aic_sram[i].dir_path_gain_lin)) -
  340. fixed_com_att_db;
  341. rot_quad_path_att_db =
  342. ar9003_aic_find_index(0, abs(aic_sram[i].quad_path_gain_lin)) -
  343. fixed_com_att_db;
  344. aic_sram[i].sram.com_att_6db =
  345. ar9003_aic_find_index(1, fixed_com_att_db);
  346. aic_sram[i].sram.valid = 1;
  347. aic_sram[i].sram.rot_dir_att_db =
  348. min(max(rot_dir_path_att_db,
  349. (int16_t)ATH_AIC_MIN_ROT_DIR_ATT_DB),
  350. ATH_AIC_MAX_ROT_DIR_ATT_DB);
  351. aic_sram[i].sram.rot_quad_att_db =
  352. min(max(rot_quad_path_att_db,
  353. (int16_t)ATH_AIC_MIN_ROT_QUAD_ATT_DB),
  354. ATH_AIC_MAX_ROT_QUAD_ATT_DB);
  355. }
  356. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  357. aic->aic_sram[i] = (SM(aic_sram[i].sram.vga_dir_sign,
  358. AR_PHY_AIC_SRAM_VGA_DIR_SIGN) |
  359. SM(aic_sram[i].sram.vga_quad_sign,
  360. AR_PHY_AIC_SRAM_VGA_QUAD_SIGN) |
  361. SM(aic_sram[i].sram.com_att_6db,
  362. AR_PHY_AIC_SRAM_COM_ATT_6DB) |
  363. SM(aic_sram[i].sram.valid,
  364. AR_PHY_AIC_SRAM_VALID) |
  365. SM(aic_sram[i].sram.rot_dir_att_db,
  366. AR_PHY_AIC_SRAM_ROT_DIR_ATT_DB) |
  367. SM(aic_sram[i].sram.rot_quad_att_db,
  368. AR_PHY_AIC_SRAM_ROT_QUAD_ATT_DB));
  369. }
  370. return ret;
  371. }
  372. static void ar9003_aic_cal_done(struct ath_hw *ah)
  373. {
  374. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  375. /* Disable AIC reference signal in BT modem. */
  376. REG_WRITE(ah, ATH_AIC_BT_JUPITER_CTRL,
  377. (REG_READ(ah, ATH_AIC_BT_JUPITER_CTRL) &
  378. ~ATH_AIC_BT_AIC_ENABLE));
  379. if (ar9003_aic_cal_post_process(ah))
  380. aic->aic_cal_state = AIC_CAL_STATE_DONE;
  381. else
  382. aic->aic_cal_state = AIC_CAL_STATE_ERROR;
  383. }
  384. static u8 ar9003_aic_cal_continue(struct ath_hw *ah, bool cal_once)
  385. {
  386. struct ath_common *common = ath9k_hw_common(ah);
  387. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  388. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  389. int i, num_chan;
  390. num_chan = MS(mci_hw->config, ATH_MCI_CONFIG_AIC_CAL_NUM_CHAN);
  391. if (!num_chan) {
  392. aic->aic_cal_state = AIC_CAL_STATE_ERROR;
  393. return aic->aic_cal_state;
  394. }
  395. if (cal_once) {
  396. for (i = 0; i < 10000; i++) {
  397. if ((REG_READ(ah, AR_PHY_AIC_CTRL_0_B1) &
  398. AR_PHY_AIC_CAL_ENABLE) == 0)
  399. break;
  400. udelay(100);
  401. }
  402. }
  403. /*
  404. * Use AR_PHY_AIC_CAL_ENABLE bit instead of AR_PHY_AIC_CAL_DONE.
  405. * Sometimes CAL_DONE bit is not asserted.
  406. */
  407. if ((REG_READ(ah, AR_PHY_AIC_CTRL_0_B1) &
  408. AR_PHY_AIC_CAL_ENABLE) != 0) {
  409. ath_dbg(common, MCI, "AIC cal is not done after 40ms");
  410. goto exit;
  411. }
  412. REG_WRITE(ah, AR_PHY_AIC_SRAM_ADDR_B1,
  413. (ATH_AIC_SRAM_CAL_OFFSET | ATH_AIC_SRAM_AUTO_INCREMENT));
  414. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  415. u32 value;
  416. value = REG_READ(ah, AR_PHY_AIC_SRAM_DATA_B1);
  417. if (value & 0x01) {
  418. if (aic->aic_sram[i] == 0)
  419. aic->aic_caled_chan++;
  420. aic->aic_sram[i] = value;
  421. if (!cal_once)
  422. break;
  423. }
  424. }
  425. if ((aic->aic_caled_chan >= num_chan) || cal_once) {
  426. ar9003_aic_cal_done(ah);
  427. } else {
  428. /* Start calibration */
  429. REG_CLR_BIT(ah, AR_PHY_AIC_CTRL_0_B1, AR_PHY_AIC_CAL_ENABLE);
  430. REG_SET_BIT(ah, AR_PHY_AIC_CTRL_0_B1,
  431. AR_PHY_AIC_CAL_CH_VALID_RESET);
  432. REG_SET_BIT(ah, AR_PHY_AIC_CTRL_0_B1, AR_PHY_AIC_CAL_ENABLE);
  433. }
  434. exit:
  435. return aic->aic_cal_state;
  436. }
  437. u8 ar9003_aic_calibration(struct ath_hw *ah)
  438. {
  439. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  440. u8 cal_ret = AIC_CAL_STATE_ERROR;
  441. switch (aic->aic_cal_state) {
  442. case AIC_CAL_STATE_IDLE:
  443. cal_ret = ar9003_aic_cal_start(ah, 1);
  444. break;
  445. case AIC_CAL_STATE_STARTED:
  446. cal_ret = ar9003_aic_cal_continue(ah, false);
  447. break;
  448. case AIC_CAL_STATE_DONE:
  449. cal_ret = AIC_CAL_STATE_DONE;
  450. break;
  451. default:
  452. break;
  453. }
  454. return cal_ret;
  455. }
  456. u8 ar9003_aic_start_normal(struct ath_hw *ah)
  457. {
  458. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  459. int16_t i;
  460. if (aic->aic_cal_state != AIC_CAL_STATE_DONE)
  461. return 1;
  462. ar9003_aic_gain_table(ah);
  463. REG_WRITE(ah, AR_PHY_AIC_SRAM_ADDR_B1, ATH_AIC_SRAM_AUTO_INCREMENT);
  464. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  465. REG_WRITE(ah, AR_PHY_AIC_SRAM_DATA_B1, aic->aic_sram[i]);
  466. }
  467. /* FIXME: Replace these with proper register names */
  468. REG_WRITE(ah, 0xa6b0, 0x80);
  469. REG_WRITE(ah, 0xa6b4, 0x5b2df0);
  470. REG_WRITE(ah, 0xa6b8, 0x10762cc8);
  471. REG_WRITE(ah, 0xa6bc, 0x1219a4b);
  472. REG_WRITE(ah, 0xa6c0, 0x1e01);
  473. REG_WRITE(ah, 0xb6b4, 0xf0);
  474. REG_WRITE(ah, 0xb6c0, 0x1e01);
  475. REG_WRITE(ah, 0xb6b0, 0x81);
  476. REG_WRITE(ah, AR_PHY_65NM_CH1_RXTX4, 0x40000000);
  477. aic->aic_enabled = true;
  478. return 0;
  479. }
  480. u8 ar9003_aic_cal_reset(struct ath_hw *ah)
  481. {
  482. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  483. aic->aic_cal_state = AIC_CAL_STATE_IDLE;
  484. return aic->aic_cal_state;
  485. }
  486. u8 ar9003_aic_calibration_single(struct ath_hw *ah)
  487. {
  488. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  489. u8 cal_ret;
  490. int num_chan;
  491. num_chan = MS(mci_hw->config, ATH_MCI_CONFIG_AIC_CAL_NUM_CHAN);
  492. (void) ar9003_aic_cal_start(ah, num_chan);
  493. cal_ret = ar9003_aic_cal_continue(ah, true);
  494. return cal_ret;
  495. }
  496. void ar9003_hw_attach_aic_ops(struct ath_hw *ah)
  497. {
  498. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  499. priv_ops->is_aic_enabled = ar9003_hw_is_aic_enabled;
  500. }