calib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #include <linux/sort.h>
  19. #include <linux/export.h>
  20. /* Common calibration code */
  21. static int rcmp_i16(const void *x, const void *y)
  22. {
  23. /* Sort in reverse order. */
  24. return *(int16_t *)y - *(int16_t *)x;
  25. }
  26. static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
  27. {
  28. int16_t nfcal[ATH9K_NF_CAL_HIST_MAX];
  29. memcpy(nfcal, nfCalBuffer, sizeof(nfcal));
  30. sort(nfcal, ATH9K_NF_CAL_HIST_MAX, sizeof(int16_t), rcmp_i16, NULL);
  31. return nfcal[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
  32. }
  33. static struct ath_nf_limits *ath9k_hw_get_nf_limits(struct ath_hw *ah,
  34. struct ath9k_channel *chan)
  35. {
  36. struct ath_nf_limits *limit;
  37. if (!chan || IS_CHAN_2GHZ(chan))
  38. limit = &ah->nf_2g;
  39. else
  40. limit = &ah->nf_5g;
  41. return limit;
  42. }
  43. static s16 ath9k_hw_get_default_nf(struct ath_hw *ah,
  44. struct ath9k_channel *chan,
  45. int chain)
  46. {
  47. s16 calib_nf = ath9k_hw_get_nf_limits(ah, chan)->cal[chain];
  48. if (calib_nf)
  49. return calib_nf;
  50. else
  51. return ath9k_hw_get_nf_limits(ah, chan)->nominal;
  52. }
  53. s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan,
  54. s16 nf)
  55. {
  56. s8 noise = ATH_DEFAULT_NOISE_FLOOR;
  57. if (nf) {
  58. s8 delta = nf - ATH9K_NF_CAL_NOISE_THRESH -
  59. ath9k_hw_get_default_nf(ah, chan, 0);
  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, CALIBRATE,
  97. "NFmid[%d] (%d) > MAX (%d), %s\n",
  98. i, h[i].privNF, limit->max,
  99. (test_bit(NFCAL_INTF, &cal->cal_flags) ?
  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 (!test_bit(NFCAL_INTF, &cal->cal_flags))
  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. clear_bit(NFCAL_INTF, &cal->cal_flags);
  120. }
  121. static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
  122. enum nl80211_band band,
  123. int16_t *nft)
  124. {
  125. switch (band) {
  126. case NL80211_BAND_5GHZ:
  127. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
  128. break;
  129. case NL80211_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. ah->cal_start_time = jiffies;
  144. currCal->calState = CAL_RUNNING;
  145. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  146. ah->meas0.sign[i] = 0;
  147. ah->meas1.sign[i] = 0;
  148. ah->meas2.sign[i] = 0;
  149. ah->meas3.sign[i] = 0;
  150. }
  151. ah->cal_samples = 0;
  152. }
  153. /* This is done for the currently configured channel */
  154. bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
  155. {
  156. struct ath_common *common = ath9k_hw_common(ah);
  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, CALIBRATE, "Calibration state incorrect, %d\n",
  166. currCal->calState);
  167. return true;
  168. }
  169. currCal = ah->cal_list;
  170. do {
  171. ath_dbg(common, CALIBRATE, "Resetting Cal %d state for channel %u\n",
  172. currCal->calData->calType,
  173. ah->curchan->chan->center_freq);
  174. ah->caldata->CalValid &= ~currCal->calData->calType;
  175. currCal->calState = CAL_WAITING;
  176. currCal = currCal->calNext;
  177. } while (currCal != ah->cal_list);
  178. return false;
  179. }
  180. EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
  181. void ath9k_hw_start_nfcal(struct ath_hw *ah, bool update)
  182. {
  183. if (ah->caldata)
  184. set_bit(NFCAL_PENDING, &ah->caldata->cal_flags);
  185. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah),
  186. AR_PHY_AGC_CONTROL_ENABLE_NF);
  187. if (update)
  188. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL(ah),
  189. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  190. else
  191. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah),
  192. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  193. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah), AR_PHY_AGC_CONTROL_NF);
  194. }
  195. int ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
  196. {
  197. struct ath9k_nfcal_hist *h = NULL;
  198. unsigned i, j;
  199. u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
  200. struct ath_common *common = ath9k_hw_common(ah);
  201. s16 default_nf = ath9k_hw_get_nf_limits(ah, chan)->nominal;
  202. u32 bb_agc_ctl = REG_READ(ah, AR_PHY_AGC_CONTROL(ah));
  203. if (ah->caldata)
  204. h = ah->caldata->nfCalHist;
  205. ENABLE_REG_RMW_BUFFER(ah);
  206. for (i = 0; i < NUM_NF_READINGS; i++) {
  207. if (chainmask & (1 << i)) {
  208. s16 nfval;
  209. if ((i >= AR5416_MAX_CHAINS) && !IS_CHAN_HT40(chan))
  210. continue;
  211. if (ah->nf_override)
  212. nfval = ah->nf_override;
  213. else if (h)
  214. nfval = h[i].privNF;
  215. else {
  216. /* Try to get calibrated noise floor value */
  217. nfval =
  218. ath9k_hw_get_nf_limits(ah, chan)->cal[i];
  219. if (nfval > -60 || nfval < -127)
  220. nfval = default_nf;
  221. }
  222. REG_RMW(ah, ah->nf_regs[i],
  223. (((u32) nfval << 1) & 0x1ff), 0x1ff);
  224. }
  225. }
  226. /*
  227. * stop NF cal if ongoing to ensure NF load completes immediately
  228. * (or after end rx/tx frame if ongoing)
  229. */
  230. if (bb_agc_ctl & AR_PHY_AGC_CONTROL_NF) {
  231. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL(ah), AR_PHY_AGC_CONTROL_NF);
  232. REG_RMW_BUFFER_FLUSH(ah);
  233. ENABLE_REG_RMW_BUFFER(ah);
  234. }
  235. /*
  236. * Load software filtered NF value into baseband internal minCCApwr
  237. * variable.
  238. */
  239. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL(ah),
  240. AR_PHY_AGC_CONTROL_ENABLE_NF);
  241. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL(ah),
  242. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  243. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah), AR_PHY_AGC_CONTROL_NF);
  244. REG_RMW_BUFFER_FLUSH(ah);
  245. /*
  246. * Wait for load to complete, should be fast, a few 10s of us.
  247. * The max delay was changed from an original 250us to 22.2 msec.
  248. * This would increase timeout to the longest possible frame
  249. * (11n max length 22.1 msec)
  250. */
  251. for (j = 0; j < 22200; j++) {
  252. if ((REG_READ(ah, AR_PHY_AGC_CONTROL(ah)) &
  253. AR_PHY_AGC_CONTROL_NF) == 0)
  254. break;
  255. udelay(10);
  256. }
  257. /*
  258. * Restart NF so it can continue.
  259. */
  260. if (bb_agc_ctl & AR_PHY_AGC_CONTROL_NF) {
  261. ENABLE_REG_RMW_BUFFER(ah);
  262. if (bb_agc_ctl & AR_PHY_AGC_CONTROL_ENABLE_NF)
  263. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah),
  264. AR_PHY_AGC_CONTROL_ENABLE_NF);
  265. if (bb_agc_ctl & AR_PHY_AGC_CONTROL_NO_UPDATE_NF)
  266. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah),
  267. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  268. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah), AR_PHY_AGC_CONTROL_NF);
  269. REG_RMW_BUFFER_FLUSH(ah);
  270. }
  271. /*
  272. * We timed out waiting for the noisefloor to load, probably due to an
  273. * in-progress rx. Simply return here and allow the load plenty of time
  274. * to complete before the next calibration interval. We need to avoid
  275. * trying to load -50 (which happens below) while the previous load is
  276. * still in progress as this can cause rx deafness. Instead by returning
  277. * here, the baseband nf cal will just be capped by our present
  278. * noisefloor until the next calibration timer.
  279. */
  280. if (j == 22200) {
  281. ath_dbg(common, ANY,
  282. "Timeout while waiting for nf to load: AR_PHY_AGC_CONTROL=0x%x\n",
  283. REG_READ(ah, AR_PHY_AGC_CONTROL(ah)));
  284. return -ETIMEDOUT;
  285. }
  286. /*
  287. * Restore maxCCAPower register parameter again so that we're not capped
  288. * by the median we just loaded. This will be initial (and max) value
  289. * of next noise floor calibration the baseband does.
  290. */
  291. ENABLE_REG_RMW_BUFFER(ah);
  292. for (i = 0; i < NUM_NF_READINGS; i++) {
  293. if (chainmask & (1 << i)) {
  294. if ((i >= AR5416_MAX_CHAINS) && !IS_CHAN_HT40(chan))
  295. continue;
  296. REG_RMW(ah, ah->nf_regs[i],
  297. (((u32) (-50) << 1) & 0x1ff), 0x1ff);
  298. }
  299. }
  300. REG_RMW_BUFFER_FLUSH(ah);
  301. return 0;
  302. }
  303. EXPORT_SYMBOL(ath9k_hw_loadnf);
  304. static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
  305. {
  306. struct ath_common *common = ath9k_hw_common(ah);
  307. struct ath_nf_limits *limit;
  308. int i;
  309. if (IS_CHAN_2GHZ(ah->curchan))
  310. limit = &ah->nf_2g;
  311. else
  312. limit = &ah->nf_5g;
  313. for (i = 0; i < NUM_NF_READINGS; i++) {
  314. if (!nf[i])
  315. continue;
  316. ath_dbg(common, CALIBRATE,
  317. "NF calibrated [%s] [chain %d] is %d\n",
  318. (i >= 3 ? "ext" : "ctl"), i % 3, nf[i]);
  319. if (nf[i] > limit->max) {
  320. ath_dbg(common, CALIBRATE,
  321. "NF[%d] (%d) > MAX (%d), correcting to MAX\n",
  322. i, nf[i], limit->max);
  323. nf[i] = limit->max;
  324. } else if (nf[i] < limit->min) {
  325. ath_dbg(common, CALIBRATE,
  326. "NF[%d] (%d) < MIN (%d), correcting to NOM\n",
  327. i, nf[i], limit->min);
  328. nf[i] = limit->nominal;
  329. }
  330. }
  331. }
  332. bool ath9k_hw_getnf(struct ath_hw *ah, struct ath9k_channel *chan)
  333. {
  334. struct ath_common *common = ath9k_hw_common(ah);
  335. int16_t nf, nfThresh;
  336. int16_t nfarray[NUM_NF_READINGS] = { 0 };
  337. struct ath9k_nfcal_hist *h;
  338. struct ieee80211_channel *c = chan->chan;
  339. struct ath9k_hw_cal_data *caldata = ah->caldata;
  340. if (REG_READ(ah, AR_PHY_AGC_CONTROL(ah)) & AR_PHY_AGC_CONTROL_NF) {
  341. ath_dbg(common, CALIBRATE,
  342. "NF did not complete in calibration window\n");
  343. return false;
  344. }
  345. ath9k_hw_do_getnf(ah, nfarray);
  346. ath9k_hw_nf_sanitize(ah, nfarray);
  347. nf = nfarray[0];
  348. if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
  349. && nf > nfThresh) {
  350. ath_dbg(common, CALIBRATE,
  351. "noise floor failed detected; detected %d, threshold %d\n",
  352. nf, nfThresh);
  353. }
  354. if (!caldata) {
  355. chan->noisefloor = nf;
  356. return false;
  357. }
  358. h = caldata->nfCalHist;
  359. clear_bit(NFCAL_PENDING, &caldata->cal_flags);
  360. ath9k_hw_update_nfcal_hist_buffer(ah, caldata, nfarray);
  361. chan->noisefloor = h[0].privNF;
  362. ah->noise = ath9k_hw_getchan_noise(ah, chan, chan->noisefloor);
  363. return true;
  364. }
  365. EXPORT_SYMBOL(ath9k_hw_getnf);
  366. void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah,
  367. struct ath9k_channel *chan)
  368. {
  369. struct ath9k_nfcal_hist *h;
  370. int i, j, k = 0;
  371. ah->caldata->channel = chan->channel;
  372. ah->caldata->channelFlags = chan->channelFlags;
  373. h = ah->caldata->nfCalHist;
  374. for (i = 0; i < NUM_NF_READINGS; i++) {
  375. h[i].currIndex = 0;
  376. h[i].privNF = ath9k_hw_get_default_nf(ah, chan, k);
  377. h[i].invalidNFcount = AR_PHY_CCA_FILTERWINDOW_LENGTH;
  378. for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++)
  379. h[i].nfCalBuffer[j] = h[i].privNF;
  380. if (++k >= AR5416_MAX_CHAINS)
  381. k = 0;
  382. }
  383. }
  384. void ath9k_hw_bstuck_nfcal(struct ath_hw *ah)
  385. {
  386. struct ath9k_hw_cal_data *caldata = ah->caldata;
  387. if (unlikely(!caldata))
  388. return;
  389. /*
  390. * If beacons are stuck, the most likely cause is interference.
  391. * Triggering a noise floor calibration at this point helps the
  392. * hardware adapt to a noisy environment much faster.
  393. * To ensure that we recover from stuck beacons quickly, let
  394. * the baseband update the internal NF value itself, similar to
  395. * what is being done after a full reset.
  396. */
  397. if (!test_bit(NFCAL_PENDING, &caldata->cal_flags))
  398. ath9k_hw_start_nfcal(ah, true);
  399. else if (!(REG_READ(ah, AR_PHY_AGC_CONTROL(ah)) & AR_PHY_AGC_CONTROL_NF))
  400. ath9k_hw_getnf(ah, ah->curchan);
  401. set_bit(NFCAL_INTF, &caldata->cal_flags);
  402. }
  403. EXPORT_SYMBOL(ath9k_hw_bstuck_nfcal);