calib.c 34 KB

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