calib.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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 "ath9k.h"
  17. /* We can tune this as we go by monitoring really low values */
  18. #define ATH9K_NF_TOO_LOW -60
  19. /* AR5416 may return very high value (like -31 dBm), in those cases the nf
  20. * is incorrect and we should use the static NF value. Later we can try to
  21. * find out why they are reporting these values */
  22. static bool ath9k_hw_nf_in_range(struct ath_hw *ah, s16 nf)
  23. {
  24. if (nf > ATH9K_NF_TOO_LOW) {
  25. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  26. "noise floor value detected (%d) is "
  27. "lower than what we think is a "
  28. "reasonable value (%d)\n",
  29. nf, ATH9K_NF_TOO_LOW);
  30. return false;
  31. }
  32. return true;
  33. }
  34. static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
  35. {
  36. int16_t nfval;
  37. int16_t sort[ATH9K_NF_CAL_HIST_MAX];
  38. int i, j;
  39. for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
  40. sort[i] = nfCalBuffer[i];
  41. for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
  42. for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
  43. if (sort[j] > sort[j - 1]) {
  44. nfval = sort[j];
  45. sort[j] = sort[j - 1];
  46. sort[j - 1] = nfval;
  47. }
  48. }
  49. }
  50. nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
  51. return nfval;
  52. }
  53. static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h,
  54. int16_t *nfarray)
  55. {
  56. int i;
  57. for (i = 0; i < NUM_NF_READINGS; i++) {
  58. h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
  59. if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
  60. h[i].currIndex = 0;
  61. if (h[i].invalidNFcount > 0) {
  62. if (nfarray[i] < AR_PHY_CCA_MIN_BAD_VALUE ||
  63. nfarray[i] > AR_PHY_CCA_MAX_HIGH_VALUE) {
  64. h[i].invalidNFcount = ATH9K_NF_CAL_HIST_MAX;
  65. } else {
  66. h[i].invalidNFcount--;
  67. h[i].privNF = nfarray[i];
  68. }
  69. } else {
  70. h[i].privNF =
  71. ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
  72. }
  73. }
  74. return;
  75. }
  76. static void ath9k_hw_do_getnf(struct ath_hw *ah,
  77. int16_t nfarray[NUM_NF_READINGS])
  78. {
  79. int16_t nf;
  80. if (AR_SREV_9280_10_OR_LATER(ah))
  81. nf = MS(REG_READ(ah, AR_PHY_CCA), AR9280_PHY_MINCCA_PWR);
  82. else
  83. nf = MS(REG_READ(ah, AR_PHY_CCA), AR_PHY_MINCCA_PWR);
  84. if (nf & 0x100)
  85. nf = 0 - ((nf ^ 0x1ff) + 1);
  86. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  87. "NF calibrated [ctl] [chain 0] is %d\n", nf);
  88. nfarray[0] = nf;
  89. if (!AR_SREV_9285(ah)) {
  90. if (AR_SREV_9280_10_OR_LATER(ah))
  91. nf = MS(REG_READ(ah, AR_PHY_CH1_CCA),
  92. AR9280_PHY_CH1_MINCCA_PWR);
  93. else
  94. nf = MS(REG_READ(ah, AR_PHY_CH1_CCA),
  95. AR_PHY_CH1_MINCCA_PWR);
  96. if (nf & 0x100)
  97. nf = 0 - ((nf ^ 0x1ff) + 1);
  98. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  99. "NF calibrated [ctl] [chain 1] is %d\n", nf);
  100. nfarray[1] = nf;
  101. if (!AR_SREV_9280(ah) && !AR_SREV_9287(ah)) {
  102. nf = MS(REG_READ(ah, AR_PHY_CH2_CCA),
  103. AR_PHY_CH2_MINCCA_PWR);
  104. if (nf & 0x100)
  105. nf = 0 - ((nf ^ 0x1ff) + 1);
  106. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  107. "NF calibrated [ctl] [chain 2] is %d\n", nf);
  108. nfarray[2] = nf;
  109. }
  110. }
  111. if (AR_SREV_9280_10_OR_LATER(ah))
  112. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA),
  113. AR9280_PHY_EXT_MINCCA_PWR);
  114. else
  115. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA),
  116. AR_PHY_EXT_MINCCA_PWR);
  117. if (nf & 0x100)
  118. nf = 0 - ((nf ^ 0x1ff) + 1);
  119. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  120. "NF calibrated [ext] [chain 0] is %d\n", nf);
  121. nfarray[3] = nf;
  122. if (!AR_SREV_9285(ah)) {
  123. if (AR_SREV_9280_10_OR_LATER(ah))
  124. nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA),
  125. AR9280_PHY_CH1_EXT_MINCCA_PWR);
  126. else
  127. nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA),
  128. AR_PHY_CH1_EXT_MINCCA_PWR);
  129. if (nf & 0x100)
  130. nf = 0 - ((nf ^ 0x1ff) + 1);
  131. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  132. "NF calibrated [ext] [chain 1] is %d\n", nf);
  133. nfarray[4] = nf;
  134. if (!AR_SREV_9280(ah) && !AR_SREV_9287(ah)) {
  135. nf = MS(REG_READ(ah, AR_PHY_CH2_EXT_CCA),
  136. AR_PHY_CH2_EXT_MINCCA_PWR);
  137. if (nf & 0x100)
  138. nf = 0 - ((nf ^ 0x1ff) + 1);
  139. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  140. "NF calibrated [ext] [chain 2] is %d\n", nf);
  141. nfarray[5] = nf;
  142. }
  143. }
  144. }
  145. static bool getNoiseFloorThresh(struct ath_hw *ah,
  146. enum ieee80211_band band,
  147. int16_t *nft)
  148. {
  149. switch (band) {
  150. case IEEE80211_BAND_5GHZ:
  151. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
  152. break;
  153. case IEEE80211_BAND_2GHZ:
  154. *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
  155. break;
  156. default:
  157. BUG_ON(1);
  158. return false;
  159. }
  160. return true;
  161. }
  162. static void ath9k_hw_setup_calibration(struct ath_hw *ah,
  163. struct ath9k_cal_list *currCal)
  164. {
  165. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(0),
  166. AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
  167. currCal->calData->calCountMax);
  168. switch (currCal->calData->calType) {
  169. case IQ_MISMATCH_CAL:
  170. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
  171. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  172. "starting IQ Mismatch Calibration\n");
  173. break;
  174. case ADC_GAIN_CAL:
  175. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
  176. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  177. "starting ADC Gain Calibration\n");
  178. break;
  179. case ADC_DC_CAL:
  180. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
  181. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  182. "starting ADC DC Calibration\n");
  183. break;
  184. case ADC_DC_INIT_CAL:
  185. REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT);
  186. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  187. "starting Init ADC DC Calibration\n");
  188. break;
  189. }
  190. REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
  191. AR_PHY_TIMING_CTRL4_DO_CAL);
  192. }
  193. static void ath9k_hw_reset_calibration(struct ath_hw *ah,
  194. struct ath9k_cal_list *currCal)
  195. {
  196. int i;
  197. ath9k_hw_setup_calibration(ah, currCal);
  198. currCal->calState = CAL_RUNNING;
  199. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  200. ah->meas0.sign[i] = 0;
  201. ah->meas1.sign[i] = 0;
  202. ah->meas2.sign[i] = 0;
  203. ah->meas3.sign[i] = 0;
  204. }
  205. ah->cal_samples = 0;
  206. }
  207. static bool ath9k_hw_per_calibration(struct ath_hw *ah,
  208. struct ath9k_channel *ichan,
  209. u8 rxchainmask,
  210. struct ath9k_cal_list *currCal)
  211. {
  212. bool iscaldone = false;
  213. if (currCal->calState == CAL_RUNNING) {
  214. if (!(REG_READ(ah, AR_PHY_TIMING_CTRL4(0)) &
  215. AR_PHY_TIMING_CTRL4_DO_CAL)) {
  216. currCal->calData->calCollect(ah);
  217. ah->cal_samples++;
  218. if (ah->cal_samples >= currCal->calData->calNumSamples) {
  219. int i, numChains = 0;
  220. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  221. if (rxchainmask & (1 << i))
  222. numChains++;
  223. }
  224. currCal->calData->calPostProc(ah, numChains);
  225. ichan->CalValid |= currCal->calData->calType;
  226. currCal->calState = CAL_DONE;
  227. iscaldone = true;
  228. } else {
  229. ath9k_hw_setup_calibration(ah, currCal);
  230. }
  231. }
  232. } else if (!(ichan->CalValid & currCal->calData->calType)) {
  233. ath9k_hw_reset_calibration(ah, currCal);
  234. }
  235. return iscaldone;
  236. }
  237. /* Assumes you are talking about the currently configured channel */
  238. static bool ath9k_hw_iscal_supported(struct ath_hw *ah,
  239. enum ath9k_cal_types calType)
  240. {
  241. struct ieee80211_conf *conf = &ah->ah_sc->hw->conf;
  242. switch (calType & ah->supp_cals) {
  243. case IQ_MISMATCH_CAL: /* Both 2 GHz and 5 GHz support OFDM */
  244. return true;
  245. case ADC_GAIN_CAL:
  246. case ADC_DC_CAL:
  247. if (!(conf->channel->band == IEEE80211_BAND_2GHZ &&
  248. conf_is_ht20(conf)))
  249. return true;
  250. break;
  251. }
  252. return false;
  253. }
  254. static void ath9k_hw_iqcal_collect(struct ath_hw *ah)
  255. {
  256. int i;
  257. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  258. ah->totalPowerMeasI[i] +=
  259. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  260. ah->totalPowerMeasQ[i] +=
  261. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  262. ah->totalIqCorrMeas[i] +=
  263. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  264. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  265. "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
  266. ah->cal_samples, i, ah->totalPowerMeasI[i],
  267. ah->totalPowerMeasQ[i],
  268. ah->totalIqCorrMeas[i]);
  269. }
  270. }
  271. static void ath9k_hw_adc_gaincal_collect(struct ath_hw *ah)
  272. {
  273. int i;
  274. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  275. ah->totalAdcIOddPhase[i] +=
  276. REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  277. ah->totalAdcIEvenPhase[i] +=
  278. REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  279. ah->totalAdcQOddPhase[i] +=
  280. REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  281. ah->totalAdcQEvenPhase[i] +=
  282. REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
  283. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  284. "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
  285. "oddq=0x%08x; evenq=0x%08x;\n",
  286. ah->cal_samples, i,
  287. ah->totalAdcIOddPhase[i],
  288. ah->totalAdcIEvenPhase[i],
  289. ah->totalAdcQOddPhase[i],
  290. ah->totalAdcQEvenPhase[i]);
  291. }
  292. }
  293. static void ath9k_hw_adc_dccal_collect(struct ath_hw *ah)
  294. {
  295. int i;
  296. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  297. ah->totalAdcDcOffsetIOddPhase[i] +=
  298. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
  299. ah->totalAdcDcOffsetIEvenPhase[i] +=
  300. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
  301. ah->totalAdcDcOffsetQOddPhase[i] +=
  302. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
  303. ah->totalAdcDcOffsetQEvenPhase[i] +=
  304. (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
  305. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  306. "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
  307. "oddq=0x%08x; evenq=0x%08x;\n",
  308. ah->cal_samples, i,
  309. ah->totalAdcDcOffsetIOddPhase[i],
  310. ah->totalAdcDcOffsetIEvenPhase[i],
  311. ah->totalAdcDcOffsetQOddPhase[i],
  312. ah->totalAdcDcOffsetQEvenPhase[i]);
  313. }
  314. }
  315. static void ath9k_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
  316. {
  317. u32 powerMeasQ, powerMeasI, iqCorrMeas;
  318. u32 qCoffDenom, iCoffDenom;
  319. int32_t qCoff, iCoff;
  320. int iqCorrNeg, i;
  321. for (i = 0; i < numChains; i++) {
  322. powerMeasI = ah->totalPowerMeasI[i];
  323. powerMeasQ = ah->totalPowerMeasQ[i];
  324. iqCorrMeas = ah->totalIqCorrMeas[i];
  325. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  326. "Starting IQ Cal and Correction for Chain %d\n",
  327. i);
  328. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  329. "Orignal: Chn %diq_corr_meas = 0x%08x\n",
  330. i, ah->totalIqCorrMeas[i]);
  331. iqCorrNeg = 0;
  332. if (iqCorrMeas > 0x80000000) {
  333. iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
  334. iqCorrNeg = 1;
  335. }
  336. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  337. "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
  338. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  339. "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
  340. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
  341. iqCorrNeg);
  342. iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 128;
  343. qCoffDenom = powerMeasQ / 64;
  344. if (powerMeasQ != 0) {
  345. iCoff = iqCorrMeas / iCoffDenom;
  346. qCoff = powerMeasI / qCoffDenom - 64;
  347. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  348. "Chn %d iCoff = 0x%08x\n", i, iCoff);
  349. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  350. "Chn %d qCoff = 0x%08x\n", i, qCoff);
  351. iCoff = iCoff & 0x3f;
  352. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  353. "New: Chn %d iCoff = 0x%08x\n", i, iCoff);
  354. if (iqCorrNeg == 0x0)
  355. iCoff = 0x40 - iCoff;
  356. if (qCoff > 15)
  357. qCoff = 15;
  358. else if (qCoff <= -16)
  359. qCoff = 16;
  360. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  361. "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
  362. i, iCoff, qCoff);
  363. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
  364. AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
  365. iCoff);
  366. REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
  367. AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
  368. qCoff);
  369. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  370. "IQ Cal and Correction done for Chain %d\n",
  371. i);
  372. }
  373. }
  374. REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
  375. AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
  376. }
  377. static void ath9k_hw_adc_gaincal_calibrate(struct ath_hw *ah, u8 numChains)
  378. {
  379. u32 iOddMeasOffset, iEvenMeasOffset, qOddMeasOffset, qEvenMeasOffset;
  380. u32 qGainMismatch, iGainMismatch, val, i;
  381. for (i = 0; i < numChains; i++) {
  382. iOddMeasOffset = ah->totalAdcIOddPhase[i];
  383. iEvenMeasOffset = ah->totalAdcIEvenPhase[i];
  384. qOddMeasOffset = ah->totalAdcQOddPhase[i];
  385. qEvenMeasOffset = ah->totalAdcQEvenPhase[i];
  386. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  387. "Starting ADC Gain Cal for Chain %d\n", i);
  388. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  389. "Chn %d pwr_meas_odd_i = 0x%08x\n", i,
  390. iOddMeasOffset);
  391. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  392. "Chn %d pwr_meas_even_i = 0x%08x\n", i,
  393. iEvenMeasOffset);
  394. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  395. "Chn %d pwr_meas_odd_q = 0x%08x\n", i,
  396. qOddMeasOffset);
  397. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  398. "Chn %d pwr_meas_even_q = 0x%08x\n", i,
  399. qEvenMeasOffset);
  400. if (iOddMeasOffset != 0 && qEvenMeasOffset != 0) {
  401. iGainMismatch =
  402. ((iEvenMeasOffset * 32) /
  403. iOddMeasOffset) & 0x3f;
  404. qGainMismatch =
  405. ((qOddMeasOffset * 32) /
  406. qEvenMeasOffset) & 0x3f;
  407. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  408. "Chn %d gain_mismatch_i = 0x%08x\n", i,
  409. iGainMismatch);
  410. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  411. "Chn %d gain_mismatch_q = 0x%08x\n", i,
  412. qGainMismatch);
  413. val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
  414. val &= 0xfffff000;
  415. val |= (qGainMismatch) | (iGainMismatch << 6);
  416. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
  417. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  418. "ADC Gain Cal done for Chain %d\n", i);
  419. }
  420. }
  421. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
  422. REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
  423. AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);
  424. }
  425. static void ath9k_hw_adc_dccal_calibrate(struct ath_hw *ah, u8 numChains)
  426. {
  427. u32 iOddMeasOffset, iEvenMeasOffset, val, i;
  428. int32_t qOddMeasOffset, qEvenMeasOffset, qDcMismatch, iDcMismatch;
  429. const struct ath9k_percal_data *calData =
  430. ah->cal_list_curr->calData;
  431. u32 numSamples =
  432. (1 << (calData->calCountMax + 5)) * calData->calNumSamples;
  433. for (i = 0; i < numChains; i++) {
  434. iOddMeasOffset = ah->totalAdcDcOffsetIOddPhase[i];
  435. iEvenMeasOffset = ah->totalAdcDcOffsetIEvenPhase[i];
  436. qOddMeasOffset = ah->totalAdcDcOffsetQOddPhase[i];
  437. qEvenMeasOffset = ah->totalAdcDcOffsetQEvenPhase[i];
  438. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  439. "Starting ADC DC Offset Cal for Chain %d\n", i);
  440. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  441. "Chn %d pwr_meas_odd_i = %d\n", i,
  442. iOddMeasOffset);
  443. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  444. "Chn %d pwr_meas_even_i = %d\n", i,
  445. iEvenMeasOffset);
  446. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  447. "Chn %d pwr_meas_odd_q = %d\n", i,
  448. qOddMeasOffset);
  449. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  450. "Chn %d pwr_meas_even_q = %d\n", i,
  451. qEvenMeasOffset);
  452. iDcMismatch = (((iEvenMeasOffset - iOddMeasOffset) * 2) /
  453. numSamples) & 0x1ff;
  454. qDcMismatch = (((qOddMeasOffset - qEvenMeasOffset) * 2) /
  455. numSamples) & 0x1ff;
  456. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  457. "Chn %d dc_offset_mismatch_i = 0x%08x\n", i,
  458. iDcMismatch);
  459. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  460. "Chn %d dc_offset_mismatch_q = 0x%08x\n", i,
  461. qDcMismatch);
  462. val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
  463. val &= 0xc0000fff;
  464. val |= (qDcMismatch << 12) | (iDcMismatch << 21);
  465. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
  466. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  467. "ADC DC Offset Cal done for Chain %d\n", i);
  468. }
  469. REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
  470. REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
  471. AR_PHY_NEW_ADC_DC_OFFSET_CORR_ENABLE);
  472. }
  473. /* This is done for the currently configured channel */
  474. bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
  475. {
  476. struct ieee80211_conf *conf = &ah->ah_sc->hw->conf;
  477. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  478. if (!ah->curchan)
  479. return true;
  480. if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
  481. return true;
  482. if (currCal == NULL)
  483. return true;
  484. if (currCal->calState != CAL_DONE) {
  485. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  486. "Calibration state incorrect, %d\n",
  487. currCal->calState);
  488. return true;
  489. }
  490. if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
  491. return true;
  492. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  493. "Resetting Cal %d state for channel %u\n",
  494. currCal->calData->calType, conf->channel->center_freq);
  495. ah->curchan->CalValid &= ~currCal->calData->calType;
  496. currCal->calState = CAL_WAITING;
  497. return false;
  498. }
  499. void ath9k_hw_start_nfcal(struct ath_hw *ah)
  500. {
  501. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  502. AR_PHY_AGC_CONTROL_ENABLE_NF);
  503. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  504. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  505. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  506. }
  507. void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
  508. {
  509. struct ath9k_nfcal_hist *h;
  510. int i, j;
  511. int32_t val;
  512. const u32 ar5416_cca_regs[6] = {
  513. AR_PHY_CCA,
  514. AR_PHY_CH1_CCA,
  515. AR_PHY_CH2_CCA,
  516. AR_PHY_EXT_CCA,
  517. AR_PHY_CH1_EXT_CCA,
  518. AR_PHY_CH2_EXT_CCA
  519. };
  520. u8 chainmask;
  521. if (AR_SREV_9285(ah))
  522. chainmask = 0x9;
  523. else if (AR_SREV_9280(ah) || AR_SREV_9287(ah))
  524. chainmask = 0x1B;
  525. else
  526. chainmask = 0x3F;
  527. h = ah->nfCalHist;
  528. for (i = 0; i < NUM_NF_READINGS; i++) {
  529. if (chainmask & (1 << i)) {
  530. val = REG_READ(ah, ar5416_cca_regs[i]);
  531. val &= 0xFFFFFE00;
  532. val |= (((u32) (h[i].privNF) << 1) & 0x1ff);
  533. REG_WRITE(ah, ar5416_cca_regs[i], val);
  534. }
  535. }
  536. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  537. AR_PHY_AGC_CONTROL_ENABLE_NF);
  538. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  539. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  540. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  541. for (j = 0; j < 1000; j++) {
  542. if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
  543. AR_PHY_AGC_CONTROL_NF) == 0)
  544. break;
  545. udelay(10);
  546. }
  547. for (i = 0; i < NUM_NF_READINGS; i++) {
  548. if (chainmask & (1 << i)) {
  549. val = REG_READ(ah, ar5416_cca_regs[i]);
  550. val &= 0xFFFFFE00;
  551. val |= (((u32) (-50) << 1) & 0x1ff);
  552. REG_WRITE(ah, ar5416_cca_regs[i], val);
  553. }
  554. }
  555. }
  556. int16_t ath9k_hw_getnf(struct ath_hw *ah,
  557. struct ath9k_channel *chan)
  558. {
  559. int16_t nf, nfThresh;
  560. int16_t nfarray[NUM_NF_READINGS] = { 0 };
  561. struct ath9k_nfcal_hist *h;
  562. struct ieee80211_channel *c = chan->chan;
  563. chan->channelFlags &= (~CHANNEL_CW_INT);
  564. if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
  565. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  566. "NF did not complete in calibration window\n");
  567. nf = 0;
  568. chan->rawNoiseFloor = nf;
  569. return chan->rawNoiseFloor;
  570. } else {
  571. ath9k_hw_do_getnf(ah, nfarray);
  572. nf = nfarray[0];
  573. if (getNoiseFloorThresh(ah, c->band, &nfThresh)
  574. && nf > nfThresh) {
  575. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  576. "noise floor failed detected; "
  577. "detected %d, threshold %d\n",
  578. nf, nfThresh);
  579. chan->channelFlags |= CHANNEL_CW_INT;
  580. }
  581. }
  582. h = ah->nfCalHist;
  583. ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
  584. chan->rawNoiseFloor = h[0].privNF;
  585. return chan->rawNoiseFloor;
  586. }
  587. void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah)
  588. {
  589. int i, j;
  590. s16 noise_floor;
  591. if (AR_SREV_9280(ah))
  592. noise_floor = AR_PHY_CCA_MAX_AR9280_GOOD_VALUE;
  593. else if (AR_SREV_9285(ah))
  594. noise_floor = AR_PHY_CCA_MAX_AR9285_GOOD_VALUE;
  595. else
  596. noise_floor = AR_PHY_CCA_MAX_AR5416_GOOD_VALUE;
  597. for (i = 0; i < NUM_NF_READINGS; i++) {
  598. ah->nfCalHist[i].currIndex = 0;
  599. ah->nfCalHist[i].privNF = noise_floor;
  600. ah->nfCalHist[i].invalidNFcount =
  601. AR_PHY_CCA_FILTERWINDOW_LENGTH;
  602. for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
  603. ah->nfCalHist[i].nfCalBuffer[j] = noise_floor;
  604. }
  605. }
  606. }
  607. s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
  608. {
  609. s16 nf;
  610. if (chan->rawNoiseFloor == 0)
  611. nf = -96;
  612. else
  613. nf = chan->rawNoiseFloor;
  614. if (!ath9k_hw_nf_in_range(ah, nf))
  615. nf = ATH_DEFAULT_NOISE_FLOOR;
  616. return nf;
  617. }
  618. static void ath9k_olc_temp_compensation(struct ath_hw *ah)
  619. {
  620. u32 rddata, i;
  621. int delta, currPDADC, regval;
  622. rddata = REG_READ(ah, AR_PHY_TX_PWRCTRL4);
  623. currPDADC = MS(rddata, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
  624. if (ah->eep_ops->get_eeprom(ah, EEP_DAC_HPWR_5G))
  625. delta = (currPDADC - ah->initPDADC + 4) / 8;
  626. else
  627. delta = (currPDADC - ah->initPDADC + 5) / 10;
  628. if (delta != ah->PDADCdelta) {
  629. ah->PDADCdelta = delta;
  630. for (i = 1; i < AR9280_TX_GAIN_TABLE_SIZE; i++) {
  631. regval = ah->originalGain[i] - delta;
  632. if (regval < 0)
  633. regval = 0;
  634. REG_RMW_FIELD(ah, AR_PHY_TX_GAIN_TBL1 + i * 4,
  635. AR_PHY_TX_GAIN, regval);
  636. }
  637. }
  638. }
  639. static inline void ath9k_hw_9285_pa_cal(struct ath_hw *ah)
  640. {
  641. u32 regVal;
  642. int i, offset, offs_6_1, offs_0;
  643. u32 ccomp_org, reg_field;
  644. u32 regList[][2] = {
  645. { 0x786c, 0 },
  646. { 0x7854, 0 },
  647. { 0x7820, 0 },
  648. { 0x7824, 0 },
  649. { 0x7868, 0 },
  650. { 0x783c, 0 },
  651. { 0x7838, 0 },
  652. };
  653. if (AR_SREV_9285_11(ah)) {
  654. REG_WRITE(ah, AR9285_AN_TOP4, (AR9285_AN_TOP4_DEFAULT | 0x14));
  655. udelay(10);
  656. }
  657. for (i = 0; i < ARRAY_SIZE(regList); i++)
  658. regList[i][1] = REG_READ(ah, regList[i][0]);
  659. regVal = REG_READ(ah, 0x7834);
  660. regVal &= (~(0x1));
  661. REG_WRITE(ah, 0x7834, regVal);
  662. regVal = REG_READ(ah, 0x9808);
  663. regVal |= (0x1 << 27);
  664. REG_WRITE(ah, 0x9808, regVal);
  665. REG_RMW_FIELD(ah, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC, 1);
  666. REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1, 1);
  667. REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I, 1);
  668. REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF, 1);
  669. REG_RMW_FIELD(ah, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL, 0);
  670. REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB, 0);
  671. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL, 0);
  672. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1, 1);
  673. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV2, 0);
  674. REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT, 0);
  675. REG_RMW_FIELD(ah, AR9285_AN_RF2G8, AR9285_AN_RF2G8_PADRVGN2TAB0, 7);
  676. REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PADRVGN2TAB0, 0);
  677. ccomp_org = MS(REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_CCOMP);
  678. REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, 7);
  679. REG_WRITE(ah, AR9285_AN_TOP2, 0xca0358a0);
  680. udelay(30);
  681. REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, 0);
  682. REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 0);
  683. for (i = 6; i > 0; i--) {
  684. regVal = REG_READ(ah, 0x7834);
  685. regVal |= (1 << (19 + i));
  686. REG_WRITE(ah, 0x7834, regVal);
  687. udelay(1);
  688. regVal = REG_READ(ah, 0x7834);
  689. regVal &= (~(0x1 << (19 + i)));
  690. reg_field = MS(REG_READ(ah, 0x7840), AR9285_AN_RXTXBB1_SPARE9);
  691. regVal |= (reg_field << (19 + i));
  692. REG_WRITE(ah, 0x7834, regVal);
  693. }
  694. REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 1);
  695. udelay(1);
  696. reg_field = MS(REG_READ(ah, AR9285_AN_RF2G9), AR9285_AN_RXTXBB1_SPARE9);
  697. REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, reg_field);
  698. offs_6_1 = MS(REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_OFFS);
  699. offs_0 = MS(REG_READ(ah, AR9285_AN_RF2G3), AR9285_AN_RF2G3_PDVCCOMP);
  700. offset = (offs_6_1<<1) | offs_0;
  701. offset = offset - 0;
  702. offs_6_1 = offset>>1;
  703. offs_0 = offset & 1;
  704. REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, offs_6_1);
  705. REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, offs_0);
  706. regVal = REG_READ(ah, 0x7834);
  707. regVal |= 0x1;
  708. REG_WRITE(ah, 0x7834, regVal);
  709. regVal = REG_READ(ah, 0x9808);
  710. regVal &= (~(0x1 << 27));
  711. REG_WRITE(ah, 0x9808, regVal);
  712. for (i = 0; i < ARRAY_SIZE(regList); i++)
  713. REG_WRITE(ah, regList[i][0], regList[i][1]);
  714. REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, ccomp_org);
  715. if (AR_SREV_9285_11(ah))
  716. REG_WRITE(ah, AR9285_AN_TOP4, AR9285_AN_TOP4_DEFAULT);
  717. }
  718. bool ath9k_hw_calibrate(struct ath_hw *ah, struct ath9k_channel *chan,
  719. u8 rxchainmask, bool longcal)
  720. {
  721. bool iscaldone = true;
  722. struct ath9k_cal_list *currCal = ah->cal_list_curr;
  723. if (currCal &&
  724. (currCal->calState == CAL_RUNNING ||
  725. currCal->calState == CAL_WAITING)) {
  726. iscaldone = ath9k_hw_per_calibration(ah, chan,
  727. rxchainmask, currCal);
  728. if (iscaldone) {
  729. ah->cal_list_curr = currCal = currCal->calNext;
  730. if (currCal->calState == CAL_WAITING) {
  731. iscaldone = false;
  732. ath9k_hw_reset_calibration(ah, currCal);
  733. }
  734. }
  735. }
  736. if (longcal) {
  737. if (AR_SREV_9285_11_OR_LATER(ah))
  738. ath9k_hw_9285_pa_cal(ah);
  739. if (OLC_FOR_AR9280_20_LATER || OLC_FOR_AR9287_10_LATER)
  740. ath9k_olc_temp_compensation(ah);
  741. ath9k_hw_getnf(ah, chan);
  742. ath9k_hw_loadnf(ah, ah->curchan);
  743. ath9k_hw_start_nfcal(ah);
  744. }
  745. return iscaldone;
  746. }
  747. static bool ar9285_clc(struct ath_hw *ah, struct ath9k_channel *chan)
  748. {
  749. REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
  750. if (IS_CHAN_HT20(chan)) {
  751. REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
  752. REG_SET_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
  753. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  754. AR_PHY_AGC_CONTROL_FLTR_CAL);
  755. REG_CLR_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
  756. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
  757. if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
  758. AR_PHY_AGC_CONTROL_CAL, 0, AH_WAIT_TIMEOUT)) {
  759. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE, "offset "
  760. "calibration failed to complete in "
  761. "1ms; noisy ??\n");
  762. return false;
  763. }
  764. REG_CLR_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
  765. REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
  766. REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
  767. }
  768. REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
  769. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);
  770. REG_SET_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
  771. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
  772. if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
  773. 0, AH_WAIT_TIMEOUT)) {
  774. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE, "offset calibration "
  775. "failed to complete in 1ms; noisy ??\n");
  776. return false;
  777. }
  778. REG_SET_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
  779. REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
  780. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);
  781. return true;
  782. }
  783. bool ath9k_hw_init_cal(struct ath_hw *ah, struct ath9k_channel *chan)
  784. {
  785. if (AR_SREV_9285_12_OR_LATER(ah)) {
  786. if (!ar9285_clc(ah, chan))
  787. return false;
  788. } else {
  789. if (AR_SREV_9280_10_OR_LATER(ah)) {
  790. if (!AR_SREV_9287_10_OR_LATER(ah))
  791. REG_CLR_BIT(ah, AR_PHY_ADC_CTL,
  792. AR_PHY_ADC_CTL_OFF_PWDADC);
  793. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
  794. AR_PHY_AGC_CONTROL_FLTR_CAL);
  795. }
  796. /* Calibrate the AGC */
  797. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  798. REG_READ(ah, AR_PHY_AGC_CONTROL) |
  799. AR_PHY_AGC_CONTROL_CAL);
  800. /* Poll for offset calibration complete */
  801. if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
  802. 0, AH_WAIT_TIMEOUT)) {
  803. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  804. "offset calibration failed to complete in 1ms; "
  805. "noisy environment?\n");
  806. return false;
  807. }
  808. if (AR_SREV_9280_10_OR_LATER(ah)) {
  809. if (!AR_SREV_9287_10_OR_LATER(ah))
  810. REG_SET_BIT(ah, AR_PHY_ADC_CTL,
  811. AR_PHY_ADC_CTL_OFF_PWDADC);
  812. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  813. AR_PHY_AGC_CONTROL_FLTR_CAL);
  814. }
  815. }
  816. /* Do PA Calibration */
  817. if (AR_SREV_9285_11_OR_LATER(ah))
  818. ath9k_hw_9285_pa_cal(ah);
  819. /* Do NF Calibration after DC offset and other calibrations */
  820. REG_WRITE(ah, AR_PHY_AGC_CONTROL,
  821. REG_READ(ah, AR_PHY_AGC_CONTROL) | AR_PHY_AGC_CONTROL_NF);
  822. ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
  823. /* Enable IQ, ADC Gain and ADC DC offset CALs */
  824. if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah)) {
  825. if (ath9k_hw_iscal_supported(ah, ADC_GAIN_CAL)) {
  826. INIT_CAL(&ah->adcgain_caldata);
  827. INSERT_CAL(ah, &ah->adcgain_caldata);
  828. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  829. "enabling ADC Gain Calibration.\n");
  830. }
  831. if (ath9k_hw_iscal_supported(ah, ADC_DC_CAL)) {
  832. INIT_CAL(&ah->adcdc_caldata);
  833. INSERT_CAL(ah, &ah->adcdc_caldata);
  834. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  835. "enabling ADC DC Calibration.\n");
  836. }
  837. if (ath9k_hw_iscal_supported(ah, IQ_MISMATCH_CAL)) {
  838. INIT_CAL(&ah->iq_caldata);
  839. INSERT_CAL(ah, &ah->iq_caldata);
  840. DPRINTF(ah->ah_sc, ATH_DBG_CALIBRATE,
  841. "enabling IQ Calibration.\n");
  842. }
  843. ah->cal_list_curr = ah->cal_list;
  844. if (ah->cal_list_curr)
  845. ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
  846. }
  847. chan->CalValid = 0;
  848. return true;
  849. }
  850. const struct ath9k_percal_data iq_cal_multi_sample = {
  851. IQ_MISMATCH_CAL,
  852. MAX_CAL_SAMPLES,
  853. PER_MIN_LOG_COUNT,
  854. ath9k_hw_iqcal_collect,
  855. ath9k_hw_iqcalibrate
  856. };
  857. const struct ath9k_percal_data iq_cal_single_sample = {
  858. IQ_MISMATCH_CAL,
  859. MIN_CAL_SAMPLES,
  860. PER_MAX_LOG_COUNT,
  861. ath9k_hw_iqcal_collect,
  862. ath9k_hw_iqcalibrate
  863. };
  864. const struct ath9k_percal_data adc_gain_cal_multi_sample = {
  865. ADC_GAIN_CAL,
  866. MAX_CAL_SAMPLES,
  867. PER_MIN_LOG_COUNT,
  868. ath9k_hw_adc_gaincal_collect,
  869. ath9k_hw_adc_gaincal_calibrate
  870. };
  871. const struct ath9k_percal_data adc_gain_cal_single_sample = {
  872. ADC_GAIN_CAL,
  873. MIN_CAL_SAMPLES,
  874. PER_MAX_LOG_COUNT,
  875. ath9k_hw_adc_gaincal_collect,
  876. ath9k_hw_adc_gaincal_calibrate
  877. };
  878. const struct ath9k_percal_data adc_dc_cal_multi_sample = {
  879. ADC_DC_CAL,
  880. MAX_CAL_SAMPLES,
  881. PER_MIN_LOG_COUNT,
  882. ath9k_hw_adc_dccal_collect,
  883. ath9k_hw_adc_dccal_calibrate
  884. };
  885. const struct ath9k_percal_data adc_dc_cal_single_sample = {
  886. ADC_DC_CAL,
  887. MIN_CAL_SAMPLES,
  888. PER_MAX_LOG_COUNT,
  889. ath9k_hw_adc_dccal_collect,
  890. ath9k_hw_adc_dccal_calibrate
  891. };
  892. const struct ath9k_percal_data adc_init_dc_cal = {
  893. ADC_DC_INIT_CAL,
  894. MIN_CAL_SAMPLES,
  895. INIT_LOG_COUNT,
  896. ath9k_hw_adc_dccal_collect,
  897. ath9k_hw_adc_dccal_calibrate
  898. };