calib.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
  22. {
  23. int16_t nfval;
  24. int16_t sort[ATH9K_NF_CAL_HIST_MAX];
  25. int i, j;
  26. for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
  27. sort[i] = nfCalBuffer[i];
  28. for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
  29. for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
  30. if (sort[j] > sort[j - 1]) {
  31. nfval = sort[j];
  32. sort[j] = sort[j - 1];
  33. sort[j - 1] = nfval;
  34. }
  35. }
  36. }
  37. nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
  38. return nfval;
  39. }
  40. static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h,
  41. int16_t *nfarray)
  42. {
  43. int i;
  44. for (i = 0; i < NUM_NF_READINGS; i++) {
  45. h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
  46. if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
  47. h[i].currIndex = 0;
  48. if (h[i].invalidNFcount > 0) {
  49. h[i].invalidNFcount--;
  50. h[i].privNF = nfarray[i];
  51. } else {
  52. h[i].privNF =
  53. ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
  54. }
  55. }
  56. }
  57. static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
  58. enum ieee80211_band band,
  59. int16_t *nft)
  60. {
  61. switch (band) {
  62. case IEEE80211_BAND_5GHZ:
  63. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
  64. break;
  65. case IEEE80211_BAND_2GHZ:
  66. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
  67. break;
  68. default:
  69. BUG_ON(1);
  70. return false;
  71. }
  72. return true;
  73. }
  74. void ath9k_hw_reset_calibration(struct ath_hw *ah,
  75. struct ath9k_cal_list *currCal)
  76. {
  77. int i;
  78. ath9k_hw_setup_calibration(ah, currCal);
  79. currCal->calState = CAL_RUNNING;
  80. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  81. ah->meas0.sign[i] = 0;
  82. ah->meas1.sign[i] = 0;
  83. ah->meas2.sign[i] = 0;
  84. ah->meas3.sign[i] = 0;
  85. }
  86. ah->cal_samples = 0;
  87. }
  88. static s16 ath9k_hw_get_default_nf(struct ath_hw *ah,
  89. struct ath9k_channel *chan)
  90. {
  91. struct ath_nf_limits *limit;
  92. if (!chan || IS_CHAN_2GHZ(chan))
  93. limit = &ah->nf_2g;
  94. else
  95. limit = &ah->nf_5g;
  96. return limit->nominal;
  97. }
  98. /* This is done for the currently configured channel */
  99. bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
  100. {
  101. struct ath_common *common = ath9k_hw_common(ah);
  102. struct ieee80211_conf *conf = &common->hw->conf;
  103. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  104. if (!ah->caldata)
  105. return true;
  106. if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
  107. return true;
  108. if (currCal == NULL)
  109. return true;
  110. if (currCal->calState != CAL_DONE) {
  111. ath_print(common, ATH_DBG_CALIBRATE,
  112. "Calibration state incorrect, %d\n",
  113. currCal->calState);
  114. return true;
  115. }
  116. if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
  117. return true;
  118. ath_print(common, ATH_DBG_CALIBRATE,
  119. "Resetting Cal %d state for channel %u\n",
  120. currCal->calData->calType, conf->channel->center_freq);
  121. ah->caldata->CalValid &= ~currCal->calData->calType;
  122. currCal->calState = CAL_WAITING;
  123. return false;
  124. }
  125. EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
  126. void ath9k_hw_start_nfcal(struct ath_hw *ah, bool update)
  127. {
  128. if (ah->caldata)
  129. ah->caldata->nfcal_pending = true;
  130. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  131. AR_PHY_AGC_CONTROL_ENABLE_NF);
  132. if (update)
  133. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  134. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  135. else
  136. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  137. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  138. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  139. }
  140. void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
  141. {
  142. struct ath9k_nfcal_hist *h = NULL;
  143. unsigned i, j;
  144. int32_t val;
  145. u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
  146. struct ath_common *common = ath9k_hw_common(ah);
  147. s16 default_nf = ath9k_hw_get_default_nf(ah, chan);
  148. if (ah->caldata)
  149. h = ah->caldata->nfCalHist;
  150. for (i = 0; i < NUM_NF_READINGS; i++) {
  151. if (chainmask & (1 << i)) {
  152. s16 nfval;
  153. if (h)
  154. nfval = h[i].privNF;
  155. else
  156. nfval = default_nf;
  157. val = REG_READ(ah, ah->nf_regs[i]);
  158. val &= 0xFFFFFE00;
  159. val |= (((u32) nfval << 1) & 0x1ff);
  160. REG_WRITE(ah, ah->nf_regs[i], val);
  161. }
  162. }
  163. /*
  164. * Load software filtered NF value into baseband internal minCCApwr
  165. * variable.
  166. */
  167. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  168. AR_PHY_AGC_CONTROL_ENABLE_NF);
  169. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  170. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  171. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  172. /*
  173. * Wait for load to complete, should be fast, a few 10s of us.
  174. * The max delay was changed from an original 250us to 10000us
  175. * since 250us often results in NF load timeout and causes deaf
  176. * condition during stress testing 12/12/2009
  177. */
  178. for (j = 0; j < 1000; j++) {
  179. if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
  180. AR_PHY_AGC_CONTROL_NF) == 0)
  181. break;
  182. udelay(10);
  183. }
  184. /*
  185. * We timed out waiting for the noisefloor to load, probably due to an
  186. * in-progress rx. Simply return here and allow the load plenty of time
  187. * to complete before the next calibration interval. We need to avoid
  188. * trying to load -50 (which happens below) while the previous load is
  189. * still in progress as this can cause rx deafness. Instead by returning
  190. * here, the baseband nf cal will just be capped by our present
  191. * noisefloor until the next calibration timer.
  192. */
  193. if (j == 1000) {
  194. ath_print(common, ATH_DBG_ANY, "Timeout while waiting for nf "
  195. "to load: AR_PHY_AGC_CONTROL=0x%x\n",
  196. REG_READ(ah, AR_PHY_AGC_CONTROL));
  197. return;
  198. }
  199. /*
  200. * Restore maxCCAPower register parameter again so that we're not capped
  201. * by the median we just loaded. This will be initial (and max) value
  202. * of next noise floor calibration the baseband does.
  203. */
  204. ENABLE_REGWRITE_BUFFER(ah);
  205. for (i = 0; i < NUM_NF_READINGS; i++) {
  206. if (chainmask & (1 << i)) {
  207. val = REG_READ(ah, ah->nf_regs[i]);
  208. val &= 0xFFFFFE00;
  209. val |= (((u32) (-50) << 1) & 0x1ff);
  210. REG_WRITE(ah, ah->nf_regs[i], val);
  211. }
  212. }
  213. REGWRITE_BUFFER_FLUSH(ah);
  214. DISABLE_REGWRITE_BUFFER(ah);
  215. }
  216. static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
  217. {
  218. struct ath_common *common = ath9k_hw_common(ah);
  219. struct ath_nf_limits *limit;
  220. int i;
  221. if (IS_CHAN_2GHZ(ah->curchan))
  222. limit = &ah->nf_2g;
  223. else
  224. limit = &ah->nf_5g;
  225. for (i = 0; i < NUM_NF_READINGS; i++) {
  226. if (!nf[i])
  227. continue;
  228. ath_print(common, ATH_DBG_CALIBRATE,
  229. "NF calibrated [%s] [chain %d] is %d\n",
  230. (i >= 3 ? "ext" : "ctl"), i % 3, nf[i]);
  231. if (nf[i] > limit->max) {
  232. ath_print(common, ATH_DBG_CALIBRATE,
  233. "NF[%d] (%d) > MAX (%d), correcting to MAX",
  234. i, nf[i], limit->max);
  235. nf[i] = limit->max;
  236. } else if (nf[i] < limit->min) {
  237. ath_print(common, ATH_DBG_CALIBRATE,
  238. "NF[%d] (%d) < MIN (%d), correcting to NOM",
  239. i, nf[i], limit->min);
  240. nf[i] = limit->nominal;
  241. }
  242. }
  243. }
  244. bool ath9k_hw_getnf(struct ath_hw *ah, struct ath9k_channel *chan)
  245. {
  246. struct ath_common *common = ath9k_hw_common(ah);
  247. int16_t nf, nfThresh;
  248. int16_t nfarray[NUM_NF_READINGS] = { 0 };
  249. struct ath9k_nfcal_hist *h;
  250. struct ieee80211_channel *c = chan->chan;
  251. struct ath9k_hw_cal_data *caldata = ah->caldata;
  252. if (!caldata)
  253. return false;
  254. chan->channelFlags &= (~CHANNEL_CW_INT);
  255. if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
  256. ath_print(common, ATH_DBG_CALIBRATE,
  257. "NF did not complete in calibration window\n");
  258. nf = 0;
  259. caldata->rawNoiseFloor = nf;
  260. return false;
  261. } else {
  262. ath9k_hw_do_getnf(ah, nfarray);
  263. ath9k_hw_nf_sanitize(ah, nfarray);
  264. nf = nfarray[0];
  265. if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
  266. && nf > nfThresh) {
  267. ath_print(common, ATH_DBG_CALIBRATE,
  268. "noise floor failed detected; "
  269. "detected %d, threshold %d\n",
  270. nf, nfThresh);
  271. chan->channelFlags |= CHANNEL_CW_INT;
  272. }
  273. }
  274. h = caldata->nfCalHist;
  275. caldata->nfcal_pending = false;
  276. ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
  277. caldata->rawNoiseFloor = h[0].privNF;
  278. return true;
  279. }
  280. void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah,
  281. struct ath9k_channel *chan)
  282. {
  283. struct ath9k_nfcal_hist *h;
  284. s16 default_nf;
  285. int i, j;
  286. if (!ah->caldata)
  287. return;
  288. h = ah->caldata->nfCalHist;
  289. default_nf = ath9k_hw_get_default_nf(ah, chan);
  290. for (i = 0; i < NUM_NF_READINGS; i++) {
  291. h[i].currIndex = 0;
  292. h[i].privNF = default_nf;
  293. h[i].invalidNFcount = AR_PHY_CCA_FILTERWINDOW_LENGTH;
  294. for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
  295. h[i].nfCalBuffer[j] = default_nf;
  296. }
  297. }
  298. }
  299. s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
  300. {
  301. if (!ah->caldata || !ah->caldata->rawNoiseFloor)
  302. return ath9k_hw_get_default_nf(ah, chan);
  303. return ah->caldata->rawNoiseFloor;
  304. }
  305. EXPORT_SYMBOL(ath9k_hw_getchan_noise);