calib.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2008-2009 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. /* Common calibration code */
  19. /* We can tune this as we go by monitoring really low values */
  20. #define ATH9K_NF_TOO_LOW -60
  21. /* AR5416 may return very high value (like -31 dBm), in those cases the nf
  22. * is incorrect and we should use the static NF value. Later we can try to
  23. * find out why they are reporting these values */
  24. static bool ath9k_hw_nf_in_range(struct ath_hw *ah, s16 nf)
  25. {
  26. if (nf > ATH9K_NF_TOO_LOW) {
  27. ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
  28. "noise floor value detected (%d) is "
  29. "lower than what we think is a "
  30. "reasonable value (%d)\n",
  31. nf, ATH9K_NF_TOO_LOW);
  32. return false;
  33. }
  34. return true;
  35. }
  36. static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
  37. {
  38. int16_t nfval;
  39. int16_t sort[ATH9K_NF_CAL_HIST_MAX];
  40. int i, j;
  41. for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
  42. sort[i] = nfCalBuffer[i];
  43. for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
  44. for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
  45. if (sort[j] > sort[j - 1]) {
  46. nfval = sort[j];
  47. sort[j] = sort[j - 1];
  48. sort[j - 1] = nfval;
  49. }
  50. }
  51. }
  52. nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
  53. return nfval;
  54. }
  55. static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h,
  56. int16_t *nfarray)
  57. {
  58. int i;
  59. for (i = 0; i < NUM_NF_READINGS; i++) {
  60. h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
  61. if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
  62. h[i].currIndex = 0;
  63. if (h[i].invalidNFcount > 0) {
  64. if (nfarray[i] < AR_PHY_CCA_MIN_BAD_VALUE ||
  65. nfarray[i] > AR_PHY_CCA_MAX_HIGH_VALUE) {
  66. h[i].invalidNFcount = ATH9K_NF_CAL_HIST_MAX;
  67. } else {
  68. h[i].invalidNFcount--;
  69. h[i].privNF = nfarray[i];
  70. }
  71. } else {
  72. h[i].privNF =
  73. ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
  74. }
  75. }
  76. }
  77. static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
  78. enum ieee80211_band band,
  79. int16_t *nft)
  80. {
  81. switch (band) {
  82. case IEEE80211_BAND_5GHZ:
  83. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
  84. break;
  85. case IEEE80211_BAND_2GHZ:
  86. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
  87. break;
  88. default:
  89. BUG_ON(1);
  90. return false;
  91. }
  92. return true;
  93. }
  94. void ath9k_hw_reset_calibration(struct ath_hw *ah,
  95. struct ath9k_cal_list *currCal)
  96. {
  97. int i;
  98. ath9k_hw_setup_calibration(ah, currCal);
  99. currCal->calState = CAL_RUNNING;
  100. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  101. ah->meas0.sign[i] = 0;
  102. ah->meas1.sign[i] = 0;
  103. ah->meas2.sign[i] = 0;
  104. ah->meas3.sign[i] = 0;
  105. }
  106. ah->cal_samples = 0;
  107. }
  108. /* This is done for the currently configured channel */
  109. bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
  110. {
  111. struct ath_common *common = ath9k_hw_common(ah);
  112. struct ieee80211_conf *conf = &common->hw->conf;
  113. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  114. if (!ah->curchan)
  115. return true;
  116. if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
  117. return true;
  118. if (currCal == NULL)
  119. return true;
  120. if (currCal->calState != CAL_DONE) {
  121. ath_print(common, ATH_DBG_CALIBRATE,
  122. "Calibration state incorrect, %d\n",
  123. currCal->calState);
  124. return true;
  125. }
  126. if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
  127. return true;
  128. ath_print(common, ATH_DBG_CALIBRATE,
  129. "Resetting Cal %d state for channel %u\n",
  130. currCal->calData->calType, conf->channel->center_freq);
  131. ah->curchan->CalValid &= ~currCal->calData->calType;
  132. currCal->calState = CAL_WAITING;
  133. return false;
  134. }
  135. EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
  136. void ath9k_hw_start_nfcal(struct ath_hw *ah)
  137. {
  138. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  139. AR_PHY_AGC_CONTROL_ENABLE_NF);
  140. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  141. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  142. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  143. }
  144. int16_t ath9k_hw_getnf(struct ath_hw *ah,
  145. struct ath9k_channel *chan)
  146. {
  147. struct ath_common *common = ath9k_hw_common(ah);
  148. int16_t nf, nfThresh;
  149. int16_t nfarray[NUM_NF_READINGS] = { 0 };
  150. struct ath9k_nfcal_hist *h;
  151. struct ieee80211_channel *c = chan->chan;
  152. chan->channelFlags &= (~CHANNEL_CW_INT);
  153. if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
  154. ath_print(common, ATH_DBG_CALIBRATE,
  155. "NF did not complete in calibration window\n");
  156. nf = 0;
  157. chan->rawNoiseFloor = nf;
  158. return chan->rawNoiseFloor;
  159. } else {
  160. ath9k_hw_do_getnf(ah, nfarray);
  161. nf = nfarray[0];
  162. if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
  163. && nf > nfThresh) {
  164. ath_print(common, ATH_DBG_CALIBRATE,
  165. "noise floor failed detected; "
  166. "detected %d, threshold %d\n",
  167. nf, nfThresh);
  168. chan->channelFlags |= CHANNEL_CW_INT;
  169. }
  170. }
  171. h = ah->nfCalHist;
  172. ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
  173. chan->rawNoiseFloor = h[0].privNF;
  174. return chan->rawNoiseFloor;
  175. }
  176. void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah)
  177. {
  178. int i, j;
  179. s16 noise_floor;
  180. if (AR_SREV_9280(ah))
  181. noise_floor = AR_PHY_CCA_MAX_AR9280_GOOD_VALUE;
  182. else if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  183. noise_floor = AR_PHY_CCA_MAX_AR9285_GOOD_VALUE;
  184. else if (AR_SREV_9287(ah))
  185. noise_floor = AR_PHY_CCA_MAX_AR9287_GOOD_VALUE;
  186. else
  187. noise_floor = AR_PHY_CCA_MAX_AR5416_GOOD_VALUE;
  188. for (i = 0; i < NUM_NF_READINGS; i++) {
  189. ah->nfCalHist[i].currIndex = 0;
  190. ah->nfCalHist[i].privNF = noise_floor;
  191. ah->nfCalHist[i].invalidNFcount =
  192. AR_PHY_CCA_FILTERWINDOW_LENGTH;
  193. for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
  194. ah->nfCalHist[i].nfCalBuffer[j] = noise_floor;
  195. }
  196. }
  197. }
  198. s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
  199. {
  200. s16 nf;
  201. if (chan->rawNoiseFloor == 0)
  202. nf = -96;
  203. else
  204. nf = chan->rawNoiseFloor;
  205. if (!ath9k_hw_nf_in_range(ah, nf))
  206. nf = ATH_DEFAULT_NOISE_FLOOR;
  207. return nf;
  208. }
  209. EXPORT_SYMBOL(ath9k_hw_getchan_noise);