calib.c 35 KB

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