calib.c 7.9 KB

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