calib.c 12 KB

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