ar9003_rtt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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->rxchainmask & (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 int ar9003_hw_rtt_fill_hist_entry(struct ath_hw *ah, u8 chain, u32 index)
  102. {
  103. u32 val;
  104. val = SM(0, AR_PHY_RTT_SW_RTT_TABLE_ACCESS) |
  105. SM(0, AR_PHY_RTT_SW_RTT_TABLE_WRITE) |
  106. SM(index, AR_PHY_RTT_SW_RTT_TABLE_ADDR);
  107. REG_WRITE(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain), val);
  108. udelay(1);
  109. val |= SM(1, AR_PHY_RTT_SW_RTT_TABLE_ACCESS);
  110. REG_WRITE(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain), val);
  111. udelay(1);
  112. if (!ath9k_hw_wait(ah, AR_PHY_RTT_TABLE_SW_INTF_B(chain),
  113. AR_PHY_RTT_SW_RTT_TABLE_ACCESS, 0,
  114. RTT_ACCESS_TIMEOUT))
  115. return RTT_BAD_VALUE;
  116. val = MS(REG_READ(ah, AR_PHY_RTT_TABLE_SW_INTF_1_B(chain)),
  117. AR_PHY_RTT_SW_RTT_TABLE_DATA);
  118. return val;
  119. }
  120. void ar9003_hw_rtt_fill_hist(struct ath_hw *ah)
  121. {
  122. int chain, i;
  123. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  124. if (!(ah->rxchainmask & (1 << chain)))
  125. continue;
  126. for (i = 0; i < MAX_RTT_TABLE_ENTRY; i++) {
  127. ah->caldata->rtt_table[chain][i] =
  128. ar9003_hw_rtt_fill_hist_entry(ah, chain, i);
  129. ath_dbg(ath9k_hw_common(ah), CALIBRATE,
  130. "RTT value at idx %d, chain %d is: 0x%x\n",
  131. i, chain, ah->caldata->rtt_table[chain][i]);
  132. }
  133. }
  134. ah->caldata->rtt_done = true;
  135. }
  136. void ar9003_hw_rtt_clear_hist(struct ath_hw *ah)
  137. {
  138. int chain, i;
  139. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  140. if (!(ah->rxchainmask & (1 << chain)))
  141. continue;
  142. for (i = 0; i < MAX_RTT_TABLE_ENTRY; i++)
  143. ar9003_hw_rtt_load_hist_entry(ah, chain, i, 0);
  144. }
  145. if (ah->caldata)
  146. ah->caldata->rtt_done = false;
  147. }
  148. bool ar9003_hw_rtt_restore(struct ath_hw *ah, struct ath9k_channel *chan)
  149. {
  150. bool restore;
  151. if (!ah->caldata)
  152. return false;
  153. if (!ah->caldata->rtt_done)
  154. return false;
  155. ar9003_hw_rtt_enable(ah);
  156. ar9003_hw_rtt_set_mask(ah, 0x10);
  157. if (!ath9k_hw_rfbus_req(ah)) {
  158. ath_err(ath9k_hw_common(ah), "Could not stop baseband\n");
  159. restore = false;
  160. goto fail;
  161. }
  162. ar9003_hw_rtt_load_hist(ah);
  163. restore = ar9003_hw_rtt_force_restore(ah);
  164. fail:
  165. ath9k_hw_rfbus_done(ah);
  166. ar9003_hw_rtt_disable(ah);
  167. return restore;
  168. }