ar9003_aic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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(bool *cal_sram_valid,
  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_valid[i])
  55. break;
  56. }
  57. } else {
  58. for (i = index - 1; i >= 0; i--) {
  59. if (cal_sram_valid[i])
  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. bool cal_sram_valid[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_valid, 0, sizeof(cal_sram_valid));
  237. memset(&aic_sram, 0, sizeof(aic_sram));
  238. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  239. struct ath_aic_sram_info sram;
  240. value = aic->aic_sram[i];
  241. cal_sram_valid[i] = sram.valid =
  242. MS(value, AR_PHY_AIC_SRAM_VALID);
  243. sram.rot_quad_att_db =
  244. MS(value, AR_PHY_AIC_SRAM_ROT_QUAD_ATT_DB);
  245. sram.vga_quad_sign =
  246. MS(value, AR_PHY_AIC_SRAM_VGA_QUAD_SIGN);
  247. sram.rot_dir_att_db =
  248. MS(value, AR_PHY_AIC_SRAM_ROT_DIR_ATT_DB);
  249. sram.vga_dir_sign =
  250. MS(value, AR_PHY_AIC_SRAM_VGA_DIR_SIGN);
  251. sram.com_att_6db =
  252. MS(value, AR_PHY_AIC_SRAM_COM_ATT_6DB);
  253. if (sram.valid) {
  254. dir_path_gain_idx = sram.rot_dir_att_db +
  255. com_att_db_table[sram.com_att_6db];
  256. quad_path_gain_idx = sram.rot_quad_att_db +
  257. com_att_db_table[sram.com_att_6db];
  258. dir_path_sign = (sram.vga_dir_sign) ? 1 : -1;
  259. quad_path_sign = (sram.vga_quad_sign) ? 1 : -1;
  260. aic_sram[i].dir_path_gain_lin = dir_path_sign *
  261. aic_lin_table[dir_path_gain_idx];
  262. aic_sram[i].quad_path_gain_lin = quad_path_sign *
  263. aic_lin_table[quad_path_gain_idx];
  264. }
  265. }
  266. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  267. int16_t start_idx, end_idx;
  268. if (cal_sram_valid[i])
  269. continue;
  270. start_idx = ar9003_aic_find_valid(cal_sram_valid, 0, i);
  271. end_idx = ar9003_aic_find_valid(cal_sram_valid, 1, i);
  272. if (start_idx < 0) {
  273. /* extrapolation */
  274. start_idx = end_idx;
  275. end_idx = ar9003_aic_find_valid(cal_sram_valid, 1, start_idx);
  276. if (end_idx < 0) {
  277. ret = false;
  278. break;
  279. }
  280. aic_sram[i].dir_path_gain_lin =
  281. ((aic_sram[start_idx].dir_path_gain_lin -
  282. aic_sram[end_idx].dir_path_gain_lin) *
  283. (start_idx - i) + ((end_idx - i) >> 1)) /
  284. (end_idx - i) +
  285. aic_sram[start_idx].dir_path_gain_lin;
  286. aic_sram[i].quad_path_gain_lin =
  287. ((aic_sram[start_idx].quad_path_gain_lin -
  288. aic_sram[end_idx].quad_path_gain_lin) *
  289. (start_idx - i) + ((end_idx - i) >> 1)) /
  290. (end_idx - i) +
  291. aic_sram[start_idx].quad_path_gain_lin;
  292. }
  293. if (end_idx < 0) {
  294. /* extrapolation */
  295. end_idx = ar9003_aic_find_valid(cal_sram_valid, 0, start_idx);
  296. if (end_idx < 0) {
  297. ret = false;
  298. break;
  299. }
  300. aic_sram[i].dir_path_gain_lin =
  301. ((aic_sram[start_idx].dir_path_gain_lin -
  302. aic_sram[end_idx].dir_path_gain_lin) *
  303. (i - start_idx) + ((start_idx - end_idx) >> 1)) /
  304. (start_idx - end_idx) +
  305. aic_sram[start_idx].dir_path_gain_lin;
  306. aic_sram[i].quad_path_gain_lin =
  307. ((aic_sram[start_idx].quad_path_gain_lin -
  308. aic_sram[end_idx].quad_path_gain_lin) *
  309. (i - start_idx) + ((start_idx - end_idx) >> 1)) /
  310. (start_idx - end_idx) +
  311. aic_sram[start_idx].quad_path_gain_lin;
  312. } else if (start_idx >= 0){
  313. /* interpolation */
  314. aic_sram[i].dir_path_gain_lin =
  315. (((end_idx - i) * aic_sram[start_idx].dir_path_gain_lin) +
  316. ((i - start_idx) * aic_sram[end_idx].dir_path_gain_lin) +
  317. ((end_idx - start_idx) >> 1)) /
  318. (end_idx - start_idx);
  319. aic_sram[i].quad_path_gain_lin =
  320. (((end_idx - i) * aic_sram[start_idx].quad_path_gain_lin) +
  321. ((i - start_idx) * aic_sram[end_idx].quad_path_gain_lin) +
  322. ((end_idx - start_idx) >> 1))/
  323. (end_idx - start_idx);
  324. }
  325. }
  326. /* From dir/quad_path_gain_lin to sram. */
  327. i = ar9003_aic_find_valid(cal_sram_valid, 1, 0);
  328. if (i < 0) {
  329. i = 0;
  330. ret = false;
  331. }
  332. fixed_com_att_db = com_att_db_table[MS(aic->aic_sram[i],
  333. AR_PHY_AIC_SRAM_COM_ATT_6DB)];
  334. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  335. int16_t rot_dir_path_att_db, rot_quad_path_att_db;
  336. struct ath_aic_sram_info sram;
  337. sram.vga_dir_sign =
  338. (aic_sram[i].dir_path_gain_lin >= 0) ? 1 : 0;
  339. sram.vga_quad_sign =
  340. (aic_sram[i].quad_path_gain_lin >= 0) ? 1 : 0;
  341. rot_dir_path_att_db =
  342. ar9003_aic_find_index(0, abs(aic_sram[i].dir_path_gain_lin)) -
  343. fixed_com_att_db;
  344. rot_quad_path_att_db =
  345. ar9003_aic_find_index(0, abs(aic_sram[i].quad_path_gain_lin)) -
  346. fixed_com_att_db;
  347. sram.com_att_6db =
  348. ar9003_aic_find_index(1, fixed_com_att_db);
  349. sram.valid = 1;
  350. sram.rot_dir_att_db =
  351. min(max(rot_dir_path_att_db,
  352. (int16_t)ATH_AIC_MIN_ROT_DIR_ATT_DB),
  353. ATH_AIC_MAX_ROT_DIR_ATT_DB);
  354. sram.rot_quad_att_db =
  355. min(max(rot_quad_path_att_db,
  356. (int16_t)ATH_AIC_MIN_ROT_QUAD_ATT_DB),
  357. ATH_AIC_MAX_ROT_QUAD_ATT_DB);
  358. aic->aic_sram[i] = (SM(sram.vga_dir_sign,
  359. AR_PHY_AIC_SRAM_VGA_DIR_SIGN) |
  360. SM(sram.vga_quad_sign,
  361. AR_PHY_AIC_SRAM_VGA_QUAD_SIGN) |
  362. SM(sram.com_att_6db,
  363. AR_PHY_AIC_SRAM_COM_ATT_6DB) |
  364. SM(sram.valid,
  365. AR_PHY_AIC_SRAM_VALID) |
  366. SM(sram.rot_dir_att_db,
  367. AR_PHY_AIC_SRAM_ROT_DIR_ATT_DB) |
  368. SM(sram.rot_quad_att_db,
  369. AR_PHY_AIC_SRAM_ROT_QUAD_ATT_DB));
  370. }
  371. return ret;
  372. }
  373. static void ar9003_aic_cal_done(struct ath_hw *ah)
  374. {
  375. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  376. /* Disable AIC reference signal in BT modem. */
  377. REG_WRITE(ah, ATH_AIC_BT_JUPITER_CTRL,
  378. (REG_READ(ah, ATH_AIC_BT_JUPITER_CTRL) &
  379. ~ATH_AIC_BT_AIC_ENABLE));
  380. if (ar9003_aic_cal_post_process(ah))
  381. aic->aic_cal_state = AIC_CAL_STATE_DONE;
  382. else
  383. aic->aic_cal_state = AIC_CAL_STATE_ERROR;
  384. }
  385. static u8 ar9003_aic_cal_continue(struct ath_hw *ah, bool cal_once)
  386. {
  387. struct ath_common *common = ath9k_hw_common(ah);
  388. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  389. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  390. int i, num_chan;
  391. num_chan = MS(mci_hw->config, ATH_MCI_CONFIG_AIC_CAL_NUM_CHAN);
  392. if (!num_chan) {
  393. aic->aic_cal_state = AIC_CAL_STATE_ERROR;
  394. return aic->aic_cal_state;
  395. }
  396. if (cal_once) {
  397. for (i = 0; i < 10000; i++) {
  398. if ((REG_READ(ah, AR_PHY_AIC_CTRL_0_B1) &
  399. AR_PHY_AIC_CAL_ENABLE) == 0)
  400. break;
  401. udelay(100);
  402. }
  403. }
  404. /*
  405. * Use AR_PHY_AIC_CAL_ENABLE bit instead of AR_PHY_AIC_CAL_DONE.
  406. * Sometimes CAL_DONE bit is not asserted.
  407. */
  408. if ((REG_READ(ah, AR_PHY_AIC_CTRL_0_B1) &
  409. AR_PHY_AIC_CAL_ENABLE) != 0) {
  410. ath_dbg(common, MCI, "AIC cal is not done after 40ms");
  411. goto exit;
  412. }
  413. REG_WRITE(ah, AR_PHY_AIC_SRAM_ADDR_B1,
  414. (ATH_AIC_SRAM_CAL_OFFSET | ATH_AIC_SRAM_AUTO_INCREMENT));
  415. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  416. u32 value;
  417. value = REG_READ(ah, AR_PHY_AIC_SRAM_DATA_B1);
  418. if (value & 0x01) {
  419. if (aic->aic_sram[i] == 0)
  420. aic->aic_caled_chan++;
  421. aic->aic_sram[i] = value;
  422. if (!cal_once)
  423. break;
  424. }
  425. }
  426. if ((aic->aic_caled_chan >= num_chan) || cal_once) {
  427. ar9003_aic_cal_done(ah);
  428. } else {
  429. /* Start calibration */
  430. REG_CLR_BIT(ah, AR_PHY_AIC_CTRL_0_B1, AR_PHY_AIC_CAL_ENABLE);
  431. REG_SET_BIT(ah, AR_PHY_AIC_CTRL_0_B1,
  432. AR_PHY_AIC_CAL_CH_VALID_RESET);
  433. REG_SET_BIT(ah, AR_PHY_AIC_CTRL_0_B1, AR_PHY_AIC_CAL_ENABLE);
  434. }
  435. exit:
  436. return aic->aic_cal_state;
  437. }
  438. u8 ar9003_aic_calibration(struct ath_hw *ah)
  439. {
  440. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  441. u8 cal_ret = AIC_CAL_STATE_ERROR;
  442. switch (aic->aic_cal_state) {
  443. case AIC_CAL_STATE_IDLE:
  444. cal_ret = ar9003_aic_cal_start(ah, 1);
  445. break;
  446. case AIC_CAL_STATE_STARTED:
  447. cal_ret = ar9003_aic_cal_continue(ah, false);
  448. break;
  449. case AIC_CAL_STATE_DONE:
  450. cal_ret = AIC_CAL_STATE_DONE;
  451. break;
  452. default:
  453. break;
  454. }
  455. return cal_ret;
  456. }
  457. u8 ar9003_aic_start_normal(struct ath_hw *ah)
  458. {
  459. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  460. int16_t i;
  461. if (aic->aic_cal_state != AIC_CAL_STATE_DONE)
  462. return 1;
  463. ar9003_aic_gain_table(ah);
  464. REG_WRITE(ah, AR_PHY_AIC_SRAM_ADDR_B1, ATH_AIC_SRAM_AUTO_INCREMENT);
  465. for (i = 0; i < ATH_AIC_MAX_BT_CHANNEL; i++) {
  466. REG_WRITE(ah, AR_PHY_AIC_SRAM_DATA_B1, aic->aic_sram[i]);
  467. }
  468. /* FIXME: Replace these with proper register names */
  469. REG_WRITE(ah, 0xa6b0, 0x80);
  470. REG_WRITE(ah, 0xa6b4, 0x5b2df0);
  471. REG_WRITE(ah, 0xa6b8, 0x10762cc8);
  472. REG_WRITE(ah, 0xa6bc, 0x1219a4b);
  473. REG_WRITE(ah, 0xa6c0, 0x1e01);
  474. REG_WRITE(ah, 0xb6b4, 0xf0);
  475. REG_WRITE(ah, 0xb6c0, 0x1e01);
  476. REG_WRITE(ah, 0xb6b0, 0x81);
  477. REG_WRITE(ah, AR_PHY_65NM_CH1_RXTX4, 0x40000000);
  478. aic->aic_enabled = true;
  479. return 0;
  480. }
  481. u8 ar9003_aic_cal_reset(struct ath_hw *ah)
  482. {
  483. struct ath9k_hw_aic *aic = &ah->btcoex_hw.aic;
  484. aic->aic_cal_state = AIC_CAL_STATE_IDLE;
  485. return aic->aic_cal_state;
  486. }
  487. u8 ar9003_aic_calibration_single(struct ath_hw *ah)
  488. {
  489. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  490. u8 cal_ret;
  491. int num_chan;
  492. num_chan = MS(mci_hw->config, ATH_MCI_CONFIG_AIC_CAL_NUM_CHAN);
  493. (void) ar9003_aic_cal_start(ah, num_chan);
  494. cal_ret = ar9003_aic_cal_continue(ah, true);
  495. return cal_ret;
  496. }
  497. void ar9003_hw_attach_aic_ops(struct ath_hw *ah)
  498. {
  499. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  500. priv_ops->is_aic_enabled = ar9003_hw_is_aic_enabled;
  501. }