calib.c 13 KB

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