calib.c 9.5 KB

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