ar9003_rtt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2010-2011 Atheros Communications 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_phy.h"
  19. #include "ar9003_rtt.h"
  20. #define RTT_RESTORE_TIMEOUT 1000
  21. #define RTT_ACCESS_TIMEOUT 100
  22. #define RTT_BAD_VALUE 0x0bad0bad
  23. /*
  24. * RTT (Radio Retention Table) hardware implementation information
  25. *
  26. * There is an internal table (i.e. the rtt) for each chain (or bank).
  27. * Each table contains 6 entries and each entry is corresponding to
  28. * a specific calibration parameter as depicted below.
  29. * 0~2 - DC offset DAC calibration: loop, low, high (offsetI/Q_...)
  30. * 3 - Filter cal (filterfc)
  31. * 4 - RX gain settings
  32. * 5 - Peak detector offset calibration (agc_caldac)
  33. */
  34. void ar9003_hw_rtt_enable(struct ath_hw *ah)
  35. {
  36. REG_WRITE(ah, AR_PHY_RTT_CTRL, 1);
  37. }
  38. void ar9003_hw_rtt_disable(struct ath_hw *ah)
  39. {
  40. REG_WRITE(ah, AR_PHY_RTT_CTRL, 0);
  41. }
  42. void ar9003_hw_rtt_set_mask(struct ath_hw *ah, u32 rtt_mask)
  43. {
  44. REG_RMW_FIELD(ah, AR_PHY_RTT_CTRL,
  45. AR_PHY_RTT_CTRL_RESTORE_MASK, rtt_mask);
  46. }
  47. bool ar9003_hw_rtt_force_restore(struct ath_hw *ah)
  48. {
  49. if (!ath9k_hw_wait(ah, AR_PHY_RTT_CTRL,
  50. AR_PHY_RTT_CTRL_FORCE_RADIO_RESTORE,
  51. 0, RTT_RESTORE_TIMEOUT))
  52. return false;
  53. REG_RMW_FIELD(ah, AR_PHY_RTT_CTRL,
  54. AR_PHY_RTT_CTRL_FORCE_RADIO_RESTORE, 1);
  55. if (!ath9k_hw_wait(ah, AR_PHY_RTT_CTRL,
  56. AR_PHY_RTT_CTRL_FORCE_RADIO_RESTORE,
  57. 0, RTT_RESTORE_TIMEOUT))
  58. return false;
  59. return true;
  60. }
  61. static void ar9003_hw_rtt_load_hist_entry(struct ath_hw *ah, u8 chain,
  62. u32 index, u32 data28)
  63. {
  64. u32 val;
  65. val = SM(data28, AR_PHY_RTT_SW_RTT_TABLE_DATA);
  66. REG_WRITE(ah, AR_PHY_RTT_TABLE_SW_INTF_1_B(chain), val);
  67. val = SM(0, AR_PHY_RTT_SW_RTT_TABLE_ACCESS) |
  68. SM(1, AR_PHY_RTT_SW_RTT_TABLE_WRITE) |
  69. SM(index, AR_PHY_RTT_SW_RTT_TABLE_ADDR);
  70. REG_WRITE(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain), val);
  71. udelay(1);
  72. val |= SM(1, AR_PHY_RTT_SW_RTT_TABLE_ACCESS);
  73. REG_WRITE(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain), val);
  74. udelay(1);
  75. if (!ath9k_hw_wait(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain),
  76. AR_PHY_RTT_SW_RTT_TABLE_ACCESS, 0,
  77. RTT_ACCESS_TIMEOUT))
  78. return;
  79. val &= ~SM(1, AR_PHY_RTT_SW_RTT_TABLE_WRITE);
  80. REG_WRITE(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain), val);
  81. udelay(1);
  82. ath9k_hw_wait(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain),
  83. AR_PHY_RTT_SW_RTT_TABLE_ACCESS, 0,
  84. RTT_ACCESS_TIMEOUT);
  85. }
  86. void ar9003_hw_rtt_load_hist(struct ath_hw *ah)
  87. {
  88. int chain, i;
  89. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  90. if (!(ah->caps.rx_chainmask & (1 << chain)))
  91. continue;
  92. for (i = 0; i < MAX_RTT_TABLE_ENTRY; i++) {
  93. ar9003_hw_rtt_load_hist_entry(ah, chain, i,
  94. ah->caldata->rtt_table[chain][i]);
  95. ath_dbg(ath9k_hw_common(ah), CALIBRATE,
  96. "Load RTT value at idx %d, chain %d: 0x%x\n",
  97. i, chain, ah->caldata->rtt_table[chain][i]);
  98. }
  99. }
  100. }
  101. static void ar9003_hw_patch_rtt(struct ath_hw *ah, int index, int chain)
  102. {
  103. int agc, caldac;
  104. if (!test_bit(SW_PKDET_DONE, &ah->caldata->cal_flags))
  105. return;
  106. if ((index != 5) || (chain >= 2))
  107. return;
  108. agc = REG_READ_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
  109. AR_PHY_65NM_RXRF_AGC_AGC_OVERRIDE);
  110. if (!agc)
  111. return;
  112. caldac = ah->caldata->caldac[chain];
  113. ah->caldata->rtt_table[chain][index] &= 0xFFFF05FF;
  114. caldac = (caldac & 0x20) | ((caldac & 0x1F) << 7);
  115. ah->caldata->rtt_table[chain][index] |= (caldac << 4);
  116. }
  117. static int ar9003_hw_rtt_fill_hist_entry(struct ath_hw *ah, u8 chain, u32 index)
  118. {
  119. u32 val;
  120. val = SM(0, AR_PHY_RTT_SW_RTT_TABLE_ACCESS) |
  121. SM(0, AR_PHY_RTT_SW_RTT_TABLE_WRITE) |
  122. SM(index, AR_PHY_RTT_SW_RTT_TABLE_ADDR);
  123. REG_WRITE(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain), val);
  124. udelay(1);
  125. val |= SM(1, AR_PHY_RTT_SW_RTT_TABLE_ACCESS);
  126. REG_WRITE(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain), val);
  127. udelay(1);
  128. if (!ath9k_hw_wait(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain),
  129. AR_PHY_RTT_SW_RTT_TABLE_ACCESS, 0,
  130. RTT_ACCESS_TIMEOUT))
  131. return RTT_BAD_VALUE;
  132. val = MS(REG_READ(ah, AR_PHY_RTT_TABLE_SW_INTF_1_B(chain)),
  133. AR_PHY_RTT_SW_RTT_TABLE_DATA);
  134. return val;
  135. }
  136. void ar9003_hw_rtt_fill_hist(struct ath_hw *ah)
  137. {
  138. int chain, i;
  139. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  140. if (!(ah->caps.rx_chainmask & (1 << chain)))
  141. continue;
  142. for (i = 0; i < MAX_RTT_TABLE_ENTRY; i++) {
  143. ah->caldata->rtt_table[chain][i] =
  144. ar9003_hw_rtt_fill_hist_entry(ah, chain, i);
  145. ar9003_hw_patch_rtt(ah, i, chain);
  146. ath_dbg(ath9k_hw_common(ah), CALIBRATE,
  147. "RTT value at idx %d, chain %d is: 0x%x\n",
  148. i, chain, ah->caldata->rtt_table[chain][i]);
  149. }
  150. }
  151. set_bit(RTT_DONE, &ah->caldata->cal_flags);
  152. }
  153. void ar9003_hw_rtt_clear_hist(struct ath_hw *ah)
  154. {
  155. int chain, i;
  156. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  157. if (!(ah->caps.rx_chainmask & (1 << chain)))
  158. continue;
  159. for (i = 0; i < MAX_RTT_TABLE_ENTRY; i++)
  160. ar9003_hw_rtt_load_hist_entry(ah, chain, i, 0);
  161. }
  162. if (ah->caldata)
  163. clear_bit(RTT_DONE, &ah->caldata->cal_flags);
  164. }
  165. bool ar9003_hw_rtt_restore(struct ath_hw *ah, struct ath9k_channel *chan)
  166. {
  167. bool restore;
  168. if (!ah->caldata)
  169. return false;
  170. if (test_bit(SW_PKDET_DONE, &ah->caldata->cal_flags)) {
  171. if (IS_CHAN_2GHZ(chan)){
  172. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(0),
  173. AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR,
  174. ah->caldata->caldac[0]);
  175. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(1),
  176. AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR,
  177. ah->caldata->caldac[1]);
  178. } else {
  179. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(0),
  180. AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR,
  181. ah->caldata->caldac[0]);
  182. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(1),
  183. AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR,
  184. ah->caldata->caldac[1]);
  185. }
  186. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(1),
  187. AR_PHY_65NM_RXRF_AGC_AGC_OVERRIDE, 0x1);
  188. REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(0),
  189. AR_PHY_65NM_RXRF_AGC_AGC_OVERRIDE, 0x1);
  190. }
  191. if (!test_bit(RTT_DONE, &ah->caldata->cal_flags))
  192. return false;
  193. ar9003_hw_rtt_enable(ah);
  194. if (test_bit(SW_PKDET_DONE, &ah->caldata->cal_flags))
  195. ar9003_hw_rtt_set_mask(ah, 0x30);
  196. else
  197. ar9003_hw_rtt_set_mask(ah, 0x10);
  198. if (!ath9k_hw_rfbus_req(ah)) {
  199. ath_err(ath9k_hw_common(ah), "Could not stop baseband\n");
  200. restore = false;
  201. goto fail;
  202. }
  203. ar9003_hw_rtt_load_hist(ah);
  204. restore = ar9003_hw_rtt_force_restore(ah);
  205. fail:
  206. ath9k_hw_rfbus_done(ah);
  207. ar9003_hw_rtt_disable(ah);
  208. return restore;
  209. }