calib.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. return;
  77. }
  78. static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
  79. enum ieee80211_band band,
  80. int16_t *nft)
  81. {
  82. switch (band) {
  83. case IEEE80211_BAND_5GHZ:
  84. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
  85. break;
  86. case IEEE80211_BAND_2GHZ:
  87. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
  88. break;
  89. default:
  90. BUG_ON(1);
  91. return false;
  92. }
  93. return true;
  94. }
  95. void ath9k_hw_reset_calibration(struct ath_hw *ah,
  96. struct ath9k_cal_list *currCal)
  97. {
  98. int i;
  99. ath9k_hw_setup_calibration(ah, currCal);
  100. currCal->calState = CAL_RUNNING;
  101. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  102. ah->meas0.sign[i] = 0;
  103. ah->meas1.sign[i] = 0;
  104. ah->meas2.sign[i] = 0;
  105. ah->meas3.sign[i] = 0;
  106. }
  107. ah->cal_samples = 0;
  108. }
  109. /* This is done for the currently configured channel */
  110. bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
  111. {
  112. struct ath_common *common = ath9k_hw_common(ah);
  113. struct ieee80211_conf *conf = &common->hw->conf;
  114. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  115. if (!ah->curchan)
  116. return true;
  117. if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
  118. return true;
  119. if (currCal == NULL)
  120. return true;
  121. if (currCal->calState != CAL_DONE) {
  122. ath_print(common, ATH_DBG_CALIBRATE,
  123. "Calibration state incorrect, %d\n",
  124. currCal->calState);
  125. return true;
  126. }
  127. if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
  128. return true;
  129. ath_print(common, ATH_DBG_CALIBRATE,
  130. "Resetting Cal %d state for channel %u\n",
  131. currCal->calData->calType, conf->channel->center_freq);
  132. ah->curchan->CalValid &= ~currCal->calData->calType;
  133. currCal->calState = CAL_WAITING;
  134. return false;
  135. }
  136. EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
  137. void ath9k_hw_start_nfcal(struct ath_hw *ah)
  138. {
  139. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  140. AR_PHY_AGC_CONTROL_ENABLE_NF);
  141. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  142. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  143. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  144. }
  145. int16_t ath9k_hw_getnf(struct ath_hw *ah,
  146. struct ath9k_channel *chan)
  147. {
  148. struct ath_common *common = ath9k_hw_common(ah);
  149. int16_t nf, nfThresh;
  150. int16_t nfarray[NUM_NF_READINGS] = { 0 };
  151. struct ath9k_nfcal_hist *h;
  152. struct ieee80211_channel *c = chan->chan;
  153. chan->channelFlags &= (~CHANNEL_CW_INT);
  154. if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
  155. ath_print(common, ATH_DBG_CALIBRATE,
  156. "NF did not complete in calibration window\n");
  157. nf = 0;
  158. chan->rawNoiseFloor = nf;
  159. return chan->rawNoiseFloor;
  160. } else {
  161. ath9k_hw_do_getnf(ah, nfarray);
  162. nf = nfarray[0];
  163. if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
  164. && nf > nfThresh) {
  165. ath_print(common, ATH_DBG_CALIBRATE,
  166. "noise floor failed detected; "
  167. "detected %d, threshold %d\n",
  168. nf, nfThresh);
  169. chan->channelFlags |= CHANNEL_CW_INT;
  170. }
  171. }
  172. h = ah->nfCalHist;
  173. ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
  174. chan->rawNoiseFloor = h[0].privNF;
  175. return chan->rawNoiseFloor;
  176. }
  177. void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah)
  178. {
  179. int i, j;
  180. s16 noise_floor;
  181. if (AR_SREV_9280(ah))
  182. noise_floor = AR_PHY_CCA_MAX_AR9280_GOOD_VALUE;
  183. else if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  184. noise_floor = AR_PHY_CCA_MAX_AR9285_GOOD_VALUE;
  185. else if (AR_SREV_9287(ah))
  186. noise_floor = AR_PHY_CCA_MAX_AR9287_GOOD_VALUE;
  187. else
  188. noise_floor = AR_PHY_CCA_MAX_AR5416_GOOD_VALUE;
  189. for (i = 0; i < NUM_NF_READINGS; i++) {
  190. ah->nfCalHist[i].currIndex = 0;
  191. ah->nfCalHist[i].privNF = noise_floor;
  192. ah->nfCalHist[i].invalidNFcount =
  193. AR_PHY_CCA_FILTERWINDOW_LENGTH;
  194. for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
  195. ah->nfCalHist[i].nfCalBuffer[j] = noise_floor;
  196. }
  197. }
  198. }
  199. s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
  200. {
  201. s16 nf;
  202. if (chan->rawNoiseFloor == 0)
  203. nf = -96;
  204. else
  205. nf = chan->rawNoiseFloor;
  206. if (!ath9k_hw_nf_in_range(ah, nf))
  207. nf = ATH_DEFAULT_NOISE_FLOOR;
  208. return nf;
  209. }
  210. EXPORT_SYMBOL(ath9k_hw_getchan_noise);