calib.c 11 KB

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